Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

t3ProjectFile.ts

179 lines8.0 KB

1import * as Schema from "effect/Schema";
2import * as SchemaTransformation from "effect/SchemaTransformation";
3​
4import { ThreadEnvMode, WorktreeSubmodules } from "./environment.ts";
5import { ProjectScriptIcon } from "./project.ts";
6import type { ProjectScopedServerSettingKey, ServerSettings } from "./settings.ts";
7​
8export const T3_PROJECT_FILE_NAME = "t3.json";
9​
10export const T3_PROJECT_FILE_SCHEMA_URL = "https://t3.codes/schema/t3.json";
11​
12const T3_PROJECT_FILE_PATH_MAX_LENGTH = 512;
13const T3_PROJECT_FILE_COMMAND_MAX_LENGTH = 4096;
14const T3_PROJECT_FILE_MAX_SCRIPTS = 50;
15​
16// Encoded-side annotations survive into JSON Schema; decoding still trims and rejects empty strings.
17const trimmedNonEmpty = (annotations: { readonly description: string }, maxLength?: number) => {
18 const annotated = Schema.String.annotate(annotations);
19 const encoded =
20 maxLength === undefined
21 ? annotated.check(Schema.isNonEmpty())
22 : annotated.check(Schema.isNonEmpty(), Schema.isMaxLength(maxLength));
23 return encoded.pipe(Schema.decodeTo(encoded, SchemaTransformation.trim()));
24};
25​
26export const T3ProjectFileScript = Schema.Struct({
27 name: trimmedNonEmpty({
28 description: "Display name for the script, shown in the T3 Code scripts menu.",
29 }),
30 command: trimmedNonEmpty({
31 description: "Shell command executed in a T3 Code terminal at the project root.",
32 }),
33 icon: Schema.optionalKey(
34 ProjectScriptIcon.annotate({
35 description: 'Icon shown next to the script in the scripts menu. Defaults to "play".',
36 }),
37 ),
38 runOnWorktreeCreate: Schema.optionalKey(
39 Schema.Boolean.annotate({
40 description:
41 "When true, the script runs automatically after a worktree is created for a new thread.",
42 }),
43 ),
44 runOnSettle: Schema.optionalKey(
45 Schema.Boolean.annotate({
46 description:
47 "When true, the script runs in the thread's worktree each time the thread settles, for example to delete build output. Threads without their own worktree skip it.",
48 }),
49 ),
50 stopOnSettle: Schema.optionalKey(
51 Schema.Boolean.annotate({
52 description:
53 "When true, T3 stops this action's terminal and child processes when its thread settles.",
54 }),
55 ),
56 async: Schema.optionalKey(
57 Schema.Boolean.annotate({
58 description:
59 "Only for runOnWorktreeCreate scripts. When true (the default), the agent starts while the script is still running. Set false to hold the agent until the script exits.",
60 }),
61 ),
62 previewUrl: Schema.optionalKey(
63 trimmedNonEmpty({
64 description:
65 "URL opened in the in-app browser preview when this script runs. Only honored on the desktop build.",
66 }),
67 ),
68 autoOpenPreview: Schema.optionalKey(
69 Schema.Boolean.annotate({
70 description:
71 "When true, automatically open the preview panel at `previewUrl` the moment the script starts.",
72 }),
73 ),
74}).annotate({
75 description: "A project script that team members can import into T3 Code.",
76});
77export type T3ProjectFileScript = typeof T3ProjectFileScript.Type;
78​
79export const T3ProjectFile = Schema.Struct({
80 $schema: Schema.optionalKey(
81 Schema.String.annotate({
82 description: `URL of the JSON Schema for this file, typically "${T3_PROJECT_FILE_SCHEMA_URL}".`,
83 }),
84 ),
85 iconPath: Schema.optionalKey(
86 trimmedNonEmpty(
87 {
88 description:
89 'Workspace-relative path to the project icon (e.g. "assets/logo.svg"). Checked before T3 Code\'s built-in icon locations.',
90 },
91 T3_PROJECT_FILE_PATH_MAX_LENGTH,
92 ),
93 ),
94 defaultThreadEnvMode: Schema.optionalKey(
95 ThreadEnvMode.annotate({
96 description:
97 'Where new threads start for this repository: "worktree" for a fresh git worktree, "local" for the current checkout. A per-project setting in T3 Code overrides this; when neither is set, the global default applies.',
98 }),
99 ),
100 worktreeSubmodules: Schema.optionalKey(
101 WorktreeSubmodules.annotate({
102 description:
103 'How new worktrees populate git submodules: "recursive" (the default) initializes nested submodules too, "top-level" initializes only those declared by this repository, and "none" leaves every submodule empty for a setup script to handle. A project or environment setting in T3 Code overrides this.',
104 }),
105 ),
106 ignoreRepositories: Schema.optionalKey(
107 Schema.Array(Schema.String).annotate({
108 description:
109 "Repositories inside the project folder that T3 Code should leave out of the project, as paths relative to it, such as vendor/big. They are then neither reviewed on their own nor given worktrees.",
110 }),
111 ),
112 worktreeLayout: Schema.optionalKey(
113 Schema.Struct({
114 create: trimmedNonEmpty(
115 {
116 description:
117 "Shell command that lays out a thread's folder for a project tracking several repositories, in place of T3 Code's own worktrees and links. It runs in the project folder with T3_WORKTREE_ROOT (the folder to fill), T3_WORKTREE_PROJECT_PATH (where the project folder goes inside it), T3_WORKTREE_BRANCH, T3_WORKTREE_BASE, T3_WORKTREE_REPOSITORIES (the chosen repositories, one path per line) and T3_PROJECT_ROOT set. Each of the project's repositories it checks out at the same relative place is adopted as the thread's checkout of it, and must be a git worktree of that repository on T3_WORKTREE_BRANCH. It runs again when the folder goes missing, so it must cope with the branch already existing. It has ten minutes.",
118 },
119 T3_PROJECT_FILE_COMMAND_MAX_LENGTH,
120 ),
121 remove: Schema.optionalKey(
122 trimmedNonEmpty(
123 {
124 description:
125 "Shell command that takes the thread's folder down again, with the same variables. It can run on a folder that is partly gone, and should delete what create generated. T3 Code removes the checkouts it leaves behind.",
126 },
127 T3_PROJECT_FILE_COMMAND_MAX_LENGTH,
128 ),
129 ),
130 }).annotate({
131 description:
132 "Lets the project create and remove a worktree thread's folder itself, for layouts with shared build output or workspace metadata.",
133 }),
134 ),
135 worktreeSparsePatternsPath: Schema.optionalKey(
136 trimmedNonEmpty(
137 {
138 description:
139 'Repository-relative path to a git sparse-checkout pattern file in non-cone syntax (e.g. ".t3/sparse-checkout"). New worktrees check out only the files it matches. Both files are read from the branch being checked out; a missing pattern file fails the worktree instead of checking out everything.',
140 },
141 T3_PROJECT_FILE_PATH_MAX_LENGTH,
142 ),
143 ),
144 scripts: Schema.optionalKey(
145 Schema.Array(T3ProjectFileScript)
146 .annotate({
147 description: "Project scripts shared with everyone who opens this repository in T3 Code.",
148 })
149 .check(Schema.isMaxLength(T3_PROJECT_FILE_MAX_SCRIPTS)),
150 ),
151}).annotate({
152 title: "T3 project file",
153 description:
154 "Checked-in project configuration for T3 Code (t3.json at the repository root). See https://t3.codes for documentation.",
155});
156export type T3ProjectFile = typeof T3ProjectFile.Type;
157​
158/** Keys must be nullable in ServerSettings so both environment and project overrides can inherit t3.json. */
159export const PROJECT_FILE_BACKED_SETTINGS = {
160 defaultThreadEnvMode: { field: "defaultThreadEnvMode", builtIn: "local" },
161 worktreeSubmodules: { field: "worktreeSubmodules", builtIn: "recursive" },
162} as const satisfies {
163 // providerInstances overrides individual fields, so it cannot use whole-setting file inheritance.
164 readonly [K in ProjectScopedServerSettingKey & keyof ServerSettings]?: {
165 readonly field: {
166 readonly [F in keyof T3ProjectFile]: T3ProjectFile[F] extends
167 | Exclude<ServerSettings[K], null>
168 | undefined
169 ? F
170 : never;
171 }[keyof T3ProjectFile];
172 readonly builtIn: Exclude<ServerSettings[K], null>;
173 };
174};
175export type ProjectFileBackedSettingKey = keyof typeof PROJECT_FILE_BACKED_SETTINGS;
176​
177export type ResolvedServerSettings = Omit<ServerSettings, ProjectFileBackedSettingKey> & {
178 readonly [K in ProjectFileBackedSettingKey]: Exclude<ServerSettings[K], null>;
179};