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

t3ProjectFile.ts

142 lines5.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​
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_MAX_SCRIPTS = 50;
14​
15// Annotations go on the encoded (string) side so they survive into the
16// published JSON Schema; decoding still trims and re-validates non-emptiness.
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 async: Schema.optionalKey(
51 Schema.Boolean.annotate({
52 description:
53 "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.",
54 }),
55 ),
56 previewUrl: Schema.optionalKey(
57 trimmedNonEmpty({
58 description:
59 "URL opened in the in-app browser preview when this script runs. Only honored on the desktop build.",
60 }),
61 ),
62 autoOpenPreview: Schema.optionalKey(
63 Schema.Boolean.annotate({
64 description:
65 "When true, automatically open the preview panel at `previewUrl` the moment the script starts.",
66 }),
67 ),
68}).annotate({
69 description: "A project script that team members can import into T3 Code.",
70});
71export type T3ProjectFileScript = typeof T3ProjectFileScript.Type;
72​
73export const T3ProjectFile = Schema.Struct({
74 $schema: Schema.optionalKey(
75 Schema.String.annotate({
76 description: `URL of the JSON Schema for this file, typically "${T3_PROJECT_FILE_SCHEMA_URL}".`,
77 }),
78 ),
79 iconPath: Schema.optionalKey(
80 trimmedNonEmpty(
81 {
82 description:
83 'Workspace-relative path to the project icon (e.g. "assets/logo.svg"). Checked before T3 Code\'s built-in icon locations.',
84 },
85 T3_PROJECT_FILE_PATH_MAX_LENGTH,
86 ),
87 ),
88 defaultThreadEnvMode: Schema.optionalKey(
89 ThreadEnvMode.annotate({
90 description:
91 '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.',
92 }),
93 ),
94 worktreeSubmodules: Schema.optionalKey(
95 WorktreeSubmodules.annotate({
96 description:
97 '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.',
98 }),
99 ),
100 scripts: Schema.optionalKey(
101 Schema.Array(T3ProjectFileScript)
102 .annotate({
103 description: "Project scripts shared with everyone who opens this repository in T3 Code.",
104 })
105 .check(Schema.isMaxLength(T3_PROJECT_FILE_MAX_SCRIPTS)),
106 ),
107}).annotate({
108 title: "T3 project file",
109 description:
110 "Checked-in project configuration for T3 Code (t3.json at the repository root). See https://t3.codes for documentation.",
111});
112export type T3ProjectFile = typeof T3ProjectFile.Type;
113​
114/**
115 * A key here must be nullable on `ServerSettings` (null means inherit) so both
116 * the project override and the environment value can defer to the file; `field`
117 * names the t3.json field carrying the same value and `builtIn` is what applies
118 * when every tier is unset.
119 */
120export const PROJECT_FILE_BACKED_SETTINGS = {
121 defaultThreadEnvMode: { field: "defaultThreadEnvMode", builtIn: "local" },
122 worktreeSubmodules: { field: "worktreeSubmodules", builtIn: "recursive" },
123} as const satisfies {
124 // `providerInstances` is scoped per project but overrides field by field
125 // rather than replacing a whole `ServerSettings` value (see its doc comment
126 // in settings.ts), so it cannot be file-backed and is excluded here.
127 readonly [K in ProjectScopedServerSettingKey & keyof ServerSettings]?: {
128 readonly field: {
129 readonly [F in keyof T3ProjectFile]: T3ProjectFile[F] extends
130 | Exclude<ServerSettings[K], null>
131 | undefined
132 ? F
133 : never;
134 }[keyof T3ProjectFile];
135 readonly builtIn: Exclude<ServerSettings[K], null>;
136 };
137};
138export type ProjectFileBackedSettingKey = keyof typeof PROJECT_FILE_BACKED_SETTINGS;
139​
140export type ResolvedServerSettings = Omit<ServerSettings, ProjectFileBackedSettingKey> & {
141 readonly [K in ProjectFileBackedSettingKey]: Exclude<ServerSettings[K], null>;
142};