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

environment.ts

301 lines15.4 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3​
4import {
5 EnvironmentId,
6 ForwardCompatibleOptional,
7 GitRefName,
8 ProjectId,
9 ThreadId,
10 TrimmedNonEmptyString,
11} from "./baseSchemas.ts";
12import { SourceControlProviderKind } from "./sourceControl.ts";
13​
14export const ORCHESTRATION_PROTOCOL_VERSION = 2;
15export const ORCHESTRATION_PROTOCOL_VERSION_TEXT = "2";
16export const ORCHESTRATION_PROTOCOL_QUERY_PARAM = "orchestrationProtocol";
17export const ORCHESTRATION_PROTOCOL_HEADER = "x-t3-orchestration-protocol";
18​
19/** Wire shapes a client understands, comma-separated; a client that omits one gets the older shape. */
20export const CLIENT_CAPABILITIES_QUERY_PARAM = "clientCapabilities";
21/** Secret fields may hold a secret provider reference instead of a string. */
22export const SETTINGS_SECRET_REFERENCES_CAPABILITY = "settingsSecretReferences";
23/** Usage limit sources may be of kind "http", not only "cliproxy". */
24export const HTTP_USAGE_LIMIT_SOURCES_CAPABILITY = "httpUsageLimitSources";
25​
26export const ExecutionEnvironmentPlatformOs = Schema.Literals([
27 "darwin",
28 "linux",
29 "windows",
30 "unknown",
31]);
32export type ExecutionEnvironmentPlatformOs = typeof ExecutionEnvironmentPlatformOs.Type;
33​
34export const ExecutionEnvironmentPlatformArch = Schema.Literals(["arm64", "x64", "other"]);
35export type ExecutionEnvironmentPlatformArch = typeof ExecutionEnvironmentPlatformArch.Type;
36​
37export const ENVIRONMENT_MACHINE_KINDS = [
38 "server",
39 "cloud",
40 "linux",
41 "desktop",
42 "laptop",
43 "mac-mini",
44 "mac-studio",
45] as const;
46export const EnvironmentMachineKind = Schema.Literals(ENVIRONMENT_MACHINE_KINDS);
47export type EnvironmentMachineKind = typeof EnvironmentMachineKind.Type;
48export const isEnvironmentMachineKind = Schema.is(EnvironmentMachineKind);
49​
50export const ExecutionEnvironmentPlatform = Schema.Struct({
51 os: ExecutionEnvironmentPlatformOs,
52 arch: ExecutionEnvironmentPlatformArch,
53 machine: ForwardCompatibleOptional(EnvironmentMachineKind),
54});
55​
56// Kept outside settings.ts to avoid an orchestration import cycle.
57export const ThreadEnvMode = Schema.Literals(["local", "worktree"]);
58export type ThreadEnvMode = typeof ThreadEnvMode.Type;
59​
60export const WorktreeSubmodules = Schema.Literals(["recursive", "top-level", "none"]);
61export type WorktreeSubmodules = typeof WorktreeSubmodules.Type;
62export type ExecutionEnvironmentPlatform = typeof ExecutionEnvironmentPlatform.Type;
63​
64export const ServerSelfUpdateMethod = Schema.Literals(["boot-service", "respawn", "desktop-app"]);
65export type ServerSelfUpdateMethod = typeof ServerSelfUpdateMethod.Type;
66​
67// Not trimmed: a path that differs from the installed one would update the wrong home.
68const InstalledPath = Schema.String.check(Schema.isNonEmpty());
69​
70export const ServerInstallation = Schema.Union([
71 Schema.Struct({ kind: Schema.Literals(["npx", "pnpm-dlx", "bunx"]) }),
72 Schema.Struct({ kind: Schema.Literal("npm-global"), prefix: TrimmedNonEmptyString }),
73 // A launcher (script, or a legacy symlink) into a downloaded runtime, as the install script and `t3 update` leave.
74 Schema.Struct({
75 kind: Schema.Literal("t3-launcher"),
76 launcher: InstalledPath,
77 baseDir: InstalledPath,
78 }),
79]);
80export type ServerInstallation = typeof ServerInstallation.Type;
81​
82export const ServerSelfUpdateCapability = Schema.Literals([
83 "boot-service",
84 "respawn",
85 "desktop-managed",
86]);
87export type ServerSelfUpdateCapability = typeof ServerSelfUpdateCapability.Type;
88​
89export const ExecutionEnvironmentCapabilities = Schema.Struct({
90 repositoryIdentity: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
91 connectionProbe: Schema.optionalKey(Schema.Boolean),
92 /** A DPoP session from a pairing link renews at /oauth/token; pair with a bearer where absent. */
93 accessTokenRenewal: Schema.optionalKey(Schema.Boolean),
94 attachmentUploads: Schema.optionalKey(Schema.Boolean),
95 questionAttachments: Schema.optionalKey(Schema.Boolean),
96 fileAttachments: Schema.optionalKey(
97 Schema.Struct({
98 maxUploadBytes: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)),
99 }),
100 ),
101 pullRequests: Schema.optionalKey(Schema.Boolean),
102 pullRequestChecks: Schema.optionalKey(Schema.Boolean),
103 issues: Schema.optionalKey(Schema.Boolean),
104 /** Side chats: hidden `lineage.sideChat` forks. Older servers ignore the fork flag. */
105 sideChats: Schema.optionalKey(Schema.Boolean),
106 vcsBranchStatus: Schema.optionalKey(Schema.Boolean),
107 /** Every protocol-2 server sends this; older mobile builds still read it. */
108 inlineMessageContext: Schema.optionalKey(Schema.Boolean),
109 requiredWorktreeBootstrap: Schema.optionalKey(Schema.Boolean),
110 threadSettlement: Schema.optionalKey(Schema.Boolean),
111 threadAutoSettlement: Schema.optionalKey(Schema.Boolean),
112 storageCleanup: Schema.optionalKey(Schema.Boolean),
113 storageCleanupRun: Schema.optionalKey(Schema.Boolean),
114 databaseStorage: Schema.optionalKey(Schema.Boolean),
115 otlpEndpointCheck: Schema.optionalKey(Schema.Boolean),
116 /** Serves `server.checkSecretSource`; hide the secret check where it is absent. */
117 secretSourceCheck: Schema.optionalKey(Schema.Boolean),
118 projectWorktreeCleanup: Schema.optionalKey(Schema.Boolean),
119 settledWorktreeCleanup: Schema.optionalKey(Schema.Boolean),
120 worktreesDirectory: Schema.optionalKey(Schema.Boolean),
121 /** Sent by macOS servers, which honor the fastWorktreeCheckout setting. */
122 fastWorktreeCheckout: Schema.optionalKey(Schema.Boolean),
123 threadAutoSettlementScope: Schema.optionalKey(Schema.Boolean),
124 threadRestartContinuation: Schema.optionalKey(Schema.Boolean),
125 projectSettingsOverrides: Schema.optionalKey(Schema.Boolean),
126 /** Hide the switch when absent; older servers silently drop per-project provider patches. */
127 projectProviderInstanceOverrides: Schema.optionalKey(Schema.Boolean),
128 threadSnooze: Schema.optionalKey(Schema.Boolean),
129 /** Serves `orchestration.forceStopThread`; hide Force stop where it is absent. */
130 threadForceStop: Schema.optionalKey(Schema.Boolean),
131 /** Sends `hostPressureUpdated` to config subscribers that ask for it. */
132 hostPressure: Schema.optionalKey(Schema.Boolean),
133 /** Every protocol-2 server sends this; older mobile builds still read it. */
134 environmentThemes: Schema.optionalKey(Schema.Boolean),
135 usageLimitSources: Schema.optionalKey(Schema.Boolean),
136 /** Honors `workspaceSnapshotsOnDemand`; older servers send every snapshot and drop them in the stream. */
137 providerWorkspaceSnapshotsOnDemand: Schema.optionalKey(Schema.Boolean),
138 /** Honors `modelCatalogOnly`; older servers drop the field and probe every provider. */
139 modelCatalogRefresh: Schema.optionalKey(Schema.Boolean),
140 usagePriceOverrides: Schema.optionalKey(Schema.Boolean),
141 usageModelAliases: Schema.optionalKey(Schema.Boolean),
142 threadPinning: Schema.optionalKey(Schema.Boolean),
143 threadPinReorder: Schema.optionalKey(Schema.Boolean),
144 threadActiveReorder: Schema.optionalKey(Schema.Boolean),
145 threadAutoSettleOptOut: Schema.optionalKey(Schema.Boolean),
146 threadProjectFromScratch: Schema.optionalKey(Schema.Boolean),
147 threadTitleRegeneration: Schema.optionalKey(Schema.Boolean),
148 threadVisitedTracking: Schema.optionalKey(Schema.Boolean),
149 threadPullRequestLinking: Schema.optionalKey(Schema.Boolean),
150 serverResolvedCommandContext: Schema.optionalKey(Schema.Boolean),
151 threadStop: Schema.optionalKey(Schema.Boolean),
152 /** Absent on older servers and on Windows hosts, which cannot stop a discovered server. */
153 localServerStop: Schema.optionalKey(Schema.Boolean),
154 /** Honors `enablePreviewTunnel`; hide the switch where absent, since the tunnel stays on there. */
155 previewTunnelSetting: Schema.optionalKey(Schema.Boolean),
156 threadPullRequests: Schema.optionalKey(Schema.Boolean),
157 threadPullRequestWatch: Schema.optionalKey(Schema.Boolean),
158 pullRequestStackActions: Schema.optionalKey(Schema.Boolean),
159 serverSelfUpdate: Schema.optionalKey(ServerSelfUpdateCapability),
160 /** Server answers server.getNpmRelease using its configured npm registry. */
161 npmReleaseLookup: Schema.optionalKey(Schema.Boolean),
162 /** Manual commands must update this install, not the host's default global prefix. */
163 serverInstallation: ForwardCompatibleOptional(ServerInstallation),
164 serverSelfUpdateProgress: Schema.optionalKey(Schema.Boolean),
165 serverUpdateStaging: Schema.optionalKey(Schema.Boolean),
166 serverUpdateThreadContinuation: Schema.optionalKey(Schema.Boolean),
167 /** Only explicit false skips Live Activity seeding; older servers may still publish. */
168 agentActivityPublishing: Schema.optionalKey(Schema.Boolean),
169 projectCloneTracking: Schema.optionalKey(Schema.Boolean),
170 /** Server keeps `pinned` and `archived` from project.update. Older servers drop them. */
171 projectOrganization: Schema.optionalKey(Schema.Boolean),
172 threadWorkspaceMoves: Schema.optionalKey(Schema.Boolean),
173 /** Server answers projects.listIgnoredFiles. */
174 ignoredFileListing: Schema.optionalKey(Schema.Boolean),
175 /** projects.listIgnoredFiles honors `query`. Older servers ignore it. */
176 ignoredFileSearch: Schema.optionalKey(Schema.Boolean),
177 environmentIcon: Schema.optionalKey(Schema.Boolean),
178 desktopAppUpdate: Schema.optionalKey(Schema.Boolean),
179 /** The desktop app switches update tracks when a server update request names one. */
180 desktopAppChannelSwitch: Schema.optionalKey(Schema.Boolean),
181 /** Subscribe to command output only when advertised; older servers never send it. */
182 commandOutputStreaming: Schema.optionalKey(Schema.Boolean),
183 /** Hosts headless Chromium tabs streamed over /api/preview-stream for clients without a local runtime. */
184 serverBrowser: Schema.optionalKey(Schema.Boolean),
185 /** Server links to other environments (`PeerLink`) and its agents can work
186 in them. Absent on servers from before linking, which clients must not
187 offer linking for. */
188 peerLinks: Schema.optionalKey(Schema.Boolean),
189 /** Server's `/mcp` narrows an OAuth client's modes from `T3-Mode-Limit`, so
190 a linked environment's calls keep its agent's limits. Another environment
191 links to this one only when it is set. */
192 mcpModeLimitHeader: Schema.optionalKey(Schema.Boolean),
193 /** Work a linked environment starts here stays within its link
194 (`linkOrigin`). Another environment links to this one only when it is
195 set, since without it that work could change the user's own threads. */
196 linkFence: Schema.optionalKey(Schema.Boolean),
197 /** Serves a thread's stream, history pages and item detail to a link's session
198 (`PEER_LINK_THREAD_STREAM_ROUTE`), so a client of the linking environment can read it. */
199 linkThreadStream: Schema.optionalKey(Schema.Boolean),
200 /** A full-access link can ask this server for a one-time pairing credential for a client
201 of the linking environment. Absent on servers from before it. */
202 linkClientPairing: Schema.optionalKey(Schema.Boolean),
203 /** The same route also mints a pairing code for a link from another environment. */
204 linkPairingForLink: Schema.optionalKey(Schema.Boolean),
205 /** Accepts `t3_thread_import`, for threads handed off here from a linked environment. */
206 threadImport: Schema.optionalKey(Schema.Boolean),
207 /** Mints pairing codes that approve one agent sign-in at a set level (`AUTO-…`). */
208 mcpApprovalCodes: Schema.optionalKey(Schema.Boolean),
209 peerLinkPairingCodes: Schema.optionalKey(Schema.Boolean),
210 /** Agent sign-ins on its T3 Connect address are approved on the hosted app, without a code. */
211 mcpHostedApproval: Schema.optionalKey(Schema.Boolean),
212 /** Server answers server.prepareTriage. */
213 triage: Schema.optionalKey(Schema.Boolean),
214 /** Server accepts `{ type: "once" }` scheduled tasks; older servers reject them on save. */
215 oneTimeSchedules: Schema.optionalKey(Schema.Boolean),
216 /** Server honors `freshContext` on sends; older servers ignore it and keep the old context. */
217 freshContext: Schema.optionalKey(Schema.Boolean),
218 /** Server saves and runs scheduled task `threadMode`; older servers ignore it. */
219 scheduledTaskThreadModes: Schema.optionalKey(Schema.Boolean),
220 /** Server can transcribe 16 kHz mono PCM audio on the environment machine. */
221 voiceTranscription: Schema.optionalKey(Schema.Boolean),
222 cloudVoiceTranscription: Schema.optionalKey(Schema.Boolean),
223 /**
224 * The server config's flags of the same names. A server that names them here lets a client
225 * load the shell snapshot before the socket opens, and answers its `resumeFrom`.
226 */
227 shellThreadFamilies: Schema.optionalKey(Schema.Boolean),
228 shellOlderSettled: Schema.optionalKey(Schema.Boolean),
229 htmlPreviewTheme: Schema.optionalKey(Schema.Boolean),
230 /** Publishes a pull request refresh when linked-thread answers change; older servers need polling. */
231 linkedThreadRefreshes: Schema.optionalKey(Schema.Boolean),
232 /** Honors the `agentFeedback` setting; older servers drop it, so hide its switch where absent. */
233 agentFeedback: Schema.optionalKey(Schema.Boolean),
234 /** Answers `issues.linkedThreads`, the threads an issue is linked to. */
235 issueLinkedThreads: Schema.optionalKey(Schema.Boolean),
236 /** Keeps more than one account per standalone issue tracker; older servers keep one. */
237 issueTrackerAccounts: Schema.optionalKey(Schema.Boolean),
238});
239export type ExecutionEnvironmentCapabilities = typeof ExecutionEnvironmentCapabilities.Type;
240​
241export const ExecutionEnvironmentDescriptor = Schema.Struct({
242 environmentId: EnvironmentId,
243 label: TrimmedNonEmptyString,
244 platform: ExecutionEnvironmentPlatform,
245 serverVersion: TrimmedNonEmptyString,
246 orchestrationProtocolVersion: Schema.optionalKey(Schema.Int),
247 /** Highest migration this server's database holds; absent on older servers. */
248 databaseMigration: Schema.optionalKey(Schema.Int),
249 capabilities: ExecutionEnvironmentCapabilities,
250});
251export type ExecutionEnvironmentDescriptor = typeof ExecutionEnvironmentDescriptor.Type;
252​
253export const RepositoryIdentityLocator = Schema.Struct({
254 source: Schema.Literal("git-remote"),
255 remoteName: GitRefName,
256 remoteUrl: TrimmedNonEmptyString,
257});
258export type RepositoryIdentityLocator = typeof RepositoryIdentityLocator.Type;
259​
260export const RepositoryOrigin = Schema.Struct({
261 canonicalKey: TrimmedNonEmptyString,
262 remoteUrl: Schema.optionalKey(TrimmedNonEmptyString),
263 displayName: Schema.optionalKey(TrimmedNonEmptyString),
264});
265export type RepositoryOrigin = typeof RepositoryOrigin.Type;
266​
267export const RepositoryIdentity = Schema.Struct({
268 canonicalKey: TrimmedNonEmptyString,
269 locator: RepositoryIdentityLocator,
270 webUrl: Schema.optionalKey(TrimmedNonEmptyString),
271 /** Work tree root, or the git dir for a bare repository. */
272 rootPath: Schema.optionalKey(TrimmedNonEmptyString),
273 displayName: Schema.optionalKey(TrimmedNonEmptyString),
274 provider: Schema.optionalKey(SourceControlProviderKind),
275 owner: Schema.optionalKey(TrimmedNonEmptyString),
276 name: Schema.optionalKey(TrimmedNonEmptyString),
277 origin: Schema.optionalKey(RepositoryOrigin),
278});
279export type RepositoryIdentity = typeof RepositoryIdentity.Type;
280​
281export function repositoryGroupingKeyOf(identity: RepositoryIdentity): string {
282 return identity.origin?.canonicalKey ?? identity.canonicalKey;
284​
285export function repositoryGroupingDisplayNameOf(identity: RepositoryIdentity): string | undefined {
286 return identity.origin
287 ? (identity.origin.displayName ?? identity.origin.canonicalKey)
288 : identity.displayName;
290​
291export const ScopedProjectRef = Schema.Struct({
292 environmentId: EnvironmentId,
293 projectId: ProjectId,
294});
295export type ScopedProjectRef = typeof ScopedProjectRef.Type;
296​
297export const ScopedThreadRef = Schema.Struct({
298 environmentId: EnvironmentId,
299 threadId: ThreadId,
300});
301export type ScopedThreadRef = typeof ScopedThreadRef.Type;