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