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

t3ProjectFile.ts

166 lines6.8 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​
8/** File name of the checked-in T3 project file, resolved at the workspace root. */
9export const T3_PROJECT_FILE_NAME = "t3.json";
10​
11/** Public URL of the published JSON Schema for {@link T3ProjectFile}. */
12export const T3_PROJECT_FILE_SCHEMA_URL = "https://t3.codes/schema/t3.json";
13​
14const T3_PROJECT_FILE_PATH_MAX_LENGTH = 512;
15const T3_PROJECT_FILE_MAX_SCRIPTS = 50;
16const T3_PROJECT_FILE_MAX_WORKTREE_DISPOSABLE_PATHS = 100;
17​
18// Annotations go on the encoded (string) side so they survive into the
19// published JSON Schema; decoding still trims and re-validates non-emptiness.
20const trimmedNonEmpty = (annotations: { readonly description: string }, maxLength?: number) => {
21 const annotated = Schema.String.annotate(annotations);
22 const encoded =
23 maxLength === undefined
24 ? annotated.check(Schema.isNonEmpty())
25 : annotated.check(Schema.isNonEmpty(), Schema.isMaxLength(maxLength));
26 return encoded.pipe(Schema.decodeTo(encoded, SchemaTransformation.trim()));
27};
28​
29export const T3ProjectFileScript = Schema.Struct({
30 name: trimmedNonEmpty({
31 description: "Display name for the script, shown in the T3 Code scripts menu.",
32 }),
33 command: trimmedNonEmpty({
34 description: "Shell command executed in a T3 Code terminal at the project root.",
35 }),
36 icon: Schema.optionalKey(
37 ProjectScriptIcon.annotate({
38 description: 'Icon shown next to the script in the scripts menu. Defaults to "play".',
39 }),
40 ),
41 runOnWorktreeCreate: Schema.optionalKey(
42 Schema.Boolean.annotate({
43 description:
44 "When true, the script runs automatically after a worktree is created for a new thread.",
45 }),
46 ),
47 runOnSettle: Schema.optionalKey(
48 Schema.Boolean.annotate({
49 description:
50 "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.",
51 }),
52 ),
53 async: Schema.optionalKey(
54 Schema.Boolean.annotate({
55 description:
56 "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.",
57 }),
58 ),
59 previewUrl: Schema.optionalKey(
60 trimmedNonEmpty({
61 description:
62 "URL opened in the in-app browser preview when this script runs. Only honored on the desktop build.",
63 }),
64 ),
65 autoOpenPreview: Schema.optionalKey(
66 Schema.Boolean.annotate({
67 description:
68 "When true, automatically open the preview panel at `previewUrl` the moment the script starts.",
69 }),
70 ),
71}).annotate({
72 description: "A project script that team members can import into T3 Code.",
73});
74export type T3ProjectFileScript = typeof T3ProjectFileScript.Type;
75​
76export const T3ProjectFile = Schema.Struct({
77 $schema: Schema.optionalKey(
78 Schema.String.annotate({
79 description: `URL of the JSON Schema for this file, typically "${T3_PROJECT_FILE_SCHEMA_URL}".`,
80 }),
81 ),
82 iconPath: Schema.optionalKey(
83 trimmedNonEmpty(
84 {
85 description:
86 'Workspace-relative path to the project icon (e.g. "assets/logo.svg"). Checked before T3 Code\'s built-in icon locations.',
87 },
88 T3_PROJECT_FILE_PATH_MAX_LENGTH,
89 ),
90 ),
91 defaultThreadEnvMode: Schema.optionalKey(
92 ThreadEnvMode.annotate({
93 description:
94 '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.',
95 }),
96 ),
97 worktreeSubmodules: Schema.optionalKey(
98 WorktreeSubmodules.annotate({
99 description:
100 '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.',
101 }),
102 ),
103 worktreeDisposablePaths: Schema.optionalKey(
104 Schema.Array(
105 trimmedNonEmpty(
106 {
107 description:
108 'Workspace-relative ignored path (e.g. "vendor" or ".env") that the setup recreates. A directory covers everything beneath it.',
109 },
110 T3_PROJECT_FILE_PATH_MAX_LENGTH,
111 ),
112 )
113 .annotate({
114 description:
115 "Ignored paths that automatic worktree cleanup may delete with the worktree. Other ignored files, except node_modules, keep a worktree from being removed.",
116 })
117 .check(Schema.isMaxLength(T3_PROJECT_FILE_MAX_WORKTREE_DISPOSABLE_PATHS)),
118 ),
119 scripts: Schema.optionalKey(
120 Schema.Array(T3ProjectFileScript)
121 .annotate({
122 description: "Project scripts shared with everyone who opens this repository in T3 Code.",
123 })
124 .check(Schema.isMaxLength(T3_PROJECT_FILE_MAX_SCRIPTS)),
125 ),
126}).annotate({
127 title: "T3 project file",
128 description:
129 "Checked-in project configuration for T3 Code (t3.json at the repository root). See https://t3.codes for documentation.",
130});
131export type T3ProjectFile = typeof T3ProjectFile.Type;
132​
133/**
134 * Settings a repository can also declare in t3.json. A key here must be
135 * nullable on `ServerSettings` (null means inherit) so both the project
136 * override and the environment value can defer to the file; `field` names
137 * the t3.json field carrying the same value and `builtIn` is what applies
138 * when every tier is unset. `resolveProjectSettings` walks project override,
139 * environment value, file, built-in, so listing a key here is the whole
140 * change for a new file-backed setting.
141 */
142export const PROJECT_FILE_BACKED_SETTINGS = {
143 defaultThreadEnvMode: { field: "defaultThreadEnvMode", builtIn: "local" },
144 worktreeSubmodules: { field: "worktreeSubmodules", builtIn: "recursive" },
145} as const satisfies {
146 readonly [K in ProjectScopedServerSettingKey]?: {
147 readonly field: {
148 readonly [F in keyof T3ProjectFile]: T3ProjectFile[F] extends
149 | Exclude<ServerSettings[K], null>
150 | undefined
151 ? F
152 : never;
153 }[keyof T3ProjectFile];
154 readonly builtIn: Exclude<ServerSettings[K], null>;
155 };
156};
157export type ProjectFileBackedSettingKey = keyof typeof PROJECT_FILE_BACKED_SETTINGS;
158​
159/**
160 * `ServerSettings` with every file-backed key resolved to a concrete value.
161 * What `resolveProjectSettings(...).settings` produces once a t3.json (or
162 * its absence) has been accounted for.
163 */
164export type ResolvedServerSettings = Omit<ServerSettings, ProjectFileBackedSettingKey> & {
165 readonly [K in ProjectFileBackedSettingKey]: Exclude<ServerSettings[K], null>;
166};