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

project.ts

564 lines19.0 KB

1import * as Schema from "effect/Schema";
2import * as SchemaTransformation from "effect/SchemaTransformation";
3import { RepositoryIdentity, ThreadEnvMode } from "./environment.ts";
4import { ModelSelection } from "./modelSelection.ts";
5import {
6 CommandId,
7 ForwardCompatibleUnion,
8 isUnknownUnionMember,
9 IsoDateTime,
10 NonNegativeInt,
11 PositiveInt,
12 ProjectId,
13 TrimmedNonEmptyString,
14 TrimmedString,
15 type UnknownUnionMember,
16} from "./baseSchemas.ts";
17​
18const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
19const PROJECT_SEARCH_CONTENTS_MAX_LIMIT = 500;
20const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
21const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
22​
23export const ProjectScriptIcon = Schema.Literals([
24 "play",
25 "test",
26 "lint",
27 "configure",
28 "build",
29 "debug",
30]);
31export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
32​
33export const ProjectScript = Schema.Struct({
34 id: TrimmedNonEmptyString,
35 name: TrimmedNonEmptyString,
36 command: TrimmedNonEmptyString,
37 icon: ProjectScriptIcon,
38 runOnWorktreeCreate: Schema.Boolean,
39 /** Run in the thread's worktree each time the thread settles. */
40 runOnSettle: Schema.optional(Schema.Boolean),
41 /** Start the agent while setup runs unless explicitly disabled. */
42 async: Schema.optional(Schema.Boolean),
43 previewUrl: Schema.optional(TrimmedNonEmptyString),
44 autoOpenPreview: Schema.optional(Schema.Boolean),
45});
46export type ProjectScript = typeof ProjectScript.Type;
47​
48export const ProjectIconColor = Schema.Literals([
49 "gray",
50 "red",
51 "orange",
52 "amber",
53 "yellow",
54 "lime",
55 "green",
56 "emerald",
57 "teal",
58 "cyan",
59 "sky",
60 "blue",
61 "indigo",
62 "violet",
63 "purple",
64 "fuchsia",
65 "pink",
66 "rose",
67]);
68export type ProjectIconColor = typeof ProjectIconColor.Type;
69​
70const ProjectLucideIconName = TrimmedNonEmptyString.check(
71 Schema.isMaxLength(64),
72 Schema.isPattern(/^[a-z0-9]+(?:-[a-z0-9]+)*$/),
73);
74​
75const ProjectEmoji = TrimmedNonEmptyString.check(Schema.isMaxLength(32));
76​
77// Grapheme-count validation belongs to the server command boundary, not snapshot decoding.
78export const ProjectMonogramText = TrimmedNonEmptyString.check(
79 Schema.isMaxLength(32),
80 Schema.isPattern(/^[\p{L}\p{N}][\p{L}\p{N}\p{M}\u200c\u200d]*$/u),
81);
82​
83const ProjectLucideIcon = Schema.Struct({
84 kind: Schema.Literal("lucide"),
85 name: ProjectLucideIconName,
86 color: ProjectIconColor,
87});
88const ProjectEmojiIcon = Schema.Struct({
89 kind: Schema.Literal("emoji"),
90 emoji: ProjectEmoji,
91});
92const ProjectMonogramIcon = Schema.Struct({
93 kind: Schema.Literal("monogram"),
94 text: ProjectMonogramText,
95 color: ProjectIconColor,
96});
97/** A workspace-relative image a project may use as its favicon. */
98export const ProjectFaviconPath = TrimmedNonEmptyString.check(
99 Schema.isMaxLength(1024),
100 Schema.isPattern(/\.(?:avif|gif|ico|jpe?g|png|svg|webp)$/i),
101);
102export type ProjectFaviconPath = typeof ProjectFaviconPath.Type;
103​
104/** A project's chosen icon, as clients set it. */
105export const ProjectIconOverride = Schema.Union([
106 ProjectLucideIcon,
107 ProjectEmojiIcon,
108 ProjectMonogramIcon,
109]);
110export type ProjectIconOverride = typeof ProjectIconOverride.Type;
111​
112/**
113 * Before v2, servers sent a monogram as a lucide icon carrying its text, so
114 * older clients showed a folder. Icons stored then, and v2 servers released
115 * before this change, still use that shape; it is read here, never written.
116 */
117const ProjectLucideIconWithLegacyMonogram = Schema.Struct({
118 ...ProjectLucideIcon.fields,
119 monogramText: Schema.optional(ProjectMonogramText),
120 monogram: Schema.optional(ProjectMonogramText),
121});
122const projectIconMembers = [
123 ProjectLucideIconWithLegacyMonogram,
124 ProjectEmojiIcon,
125 ProjectMonogramIcon,
126] as const;
127type ProjectIconMember = (typeof projectIconMembers)[number]["Type"];
128​
129const fromLegacyMonogram = (icon: ProjectIconMember): ProjectIconOverride => {
130 if (icon.kind !== "lucide") return icon;
131 const text = icon.monogramText ?? icon.monogram;
132 return text === undefined
133 ? { kind: "lucide", name: icon.name, color: icon.color }
134 : { kind: "monogram", text, color: icon.color };
135};
136​
137/** An icon the server stores; it reads legacy monograms and writes the plain shape. */
138export const StoredProjectIcon = Schema.Union(projectIconMembers).pipe(
139 Schema.decodeTo(
140 Schema.toType(ProjectIconOverride),
141 SchemaTransformation.transform<ProjectIconOverride, ProjectIconMember>({
142 decode: fromLegacyMonogram,
143 encode: (icon) => icon,
144 }),
145 ),
146);
147​
148/**
149 * An icon as clients receive it. A kind from a newer server decodes as no
150 * override, so the client shows the project's default icon.
151 */
152export const ReceivedProjectIcon = ForwardCompatibleUnion(projectIconMembers, "kind").pipe(
153 Schema.decodeTo(
154 Schema.NullOr(Schema.toType(ProjectIconOverride)),
155 SchemaTransformation.transform<
156 ProjectIconOverride | null,
157 ProjectIconMember | UnknownUnionMember<"kind">
158 >({
159 decode: (icon) => (isUnknownUnionMember(icon) ? null : fromLegacyMonogram(icon)),
160 encode: (icon) => icon as ProjectIconMember,
161 }),
162 ),
163);
164​
165export const Project = Schema.Struct({
166 id: ProjectId,
167 title: TrimmedNonEmptyString,
168 workspaceRoot: TrimmedNonEmptyString,
169 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
170 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
171 projectIcon: Schema.optional(Schema.NullOr(ReceivedProjectIcon)),
172 defaultModelSelection: Schema.NullOr(ModelSelection),
173 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
174 // Opt-in because background sync performs network I/O and may move the checkout.
175 autoPull: Schema.optional(Schema.Boolean),
176 scripts: Schema.Array(ProjectScript),
177 createdAt: IsoDateTime,
178 updatedAt: IsoDateTime,
179 deletedAt: Schema.NullOr(IsoDateTime),
180});
181export type Project = typeof Project.Type;
182​
183export const ProjectSnapshot = Schema.Struct({
184 projects: Schema.Array(Project),
185 updatedAt: IsoDateTime,
186});
187export type ProjectSnapshot = typeof ProjectSnapshot.Type;
188​
189export const ProjectChange = Schema.Union([
190 Schema.Struct({ type: Schema.Literal("project.upserted"), project: Project }),
191 Schema.Struct({
192 type: Schema.Literal("project.deleted"),
193 projectId: ProjectId,
194 deletedAt: IsoDateTime,
195 }),
196]);
197export type ProjectChange = typeof ProjectChange.Type;
198​
199export const ProjectCreatePayload = Schema.Struct({
200 title: TrimmedNonEmptyString,
201 workspaceRoot: TrimmedNonEmptyString,
202 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
203 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
204 scripts: Schema.optional(Schema.Array(ProjectScript)),
205});
206export type ProjectCreatePayload = typeof ProjectCreatePayload.Type;
207​
208export const ProjectUpdatePayload = Schema.Struct({
209 title: Schema.optional(TrimmedNonEmptyString),
210 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
211 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
212 autoPull: Schema.optional(Schema.Boolean),
213 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
214 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
215 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
216 scripts: Schema.optional(Schema.Array(ProjectScript)),
217});
218export type ProjectUpdatePayload = typeof ProjectUpdatePayload.Type;
219​
220export const ProjectMutation = Schema.Union([
221 Schema.Struct({
222 type: Schema.Literal("project.create"),
223 commandId: CommandId,
224 projectId: ProjectId,
225 ...ProjectCreatePayload.fields,
226 }),
227 Schema.Struct({
228 type: Schema.Literal("project.update"),
229 commandId: CommandId,
230 projectId: ProjectId,
231 ...ProjectUpdatePayload.fields,
232 }),
233 Schema.Struct({
234 type: Schema.Literal("project.delete"),
235 commandId: CommandId,
236 projectId: ProjectId,
237 force: Schema.optional(Schema.Boolean),
238 }),
239]);
240export type ProjectMutation = typeof ProjectMutation.Type;
241​
242export class ProjectMutationError extends Schema.TaggedError<ProjectMutationError>()(
243 "ProjectMutationError",
244 {
245 commandId: CommandId,
246 message: Schema.String,
247 cause: Schema.optional(Schema.Defect()),
248 },
249) {}
250​
251export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
252export type ProjectEntryKind = typeof ProjectEntryKind.Type;
253​
254export const ProjectSearchEntriesInput = Schema.Struct({
255 cwd: TrimmedNonEmptyString,
256 // An empty query is a bounded browse: the index returns frecency-ordered
257 // entries, which the file picker uses for its initial results.
258 query: TrimmedString.check(Schema.isMaxLength(256)),
259 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
260 kind: Schema.optional(ProjectEntryKind),
261 imageOnly: Schema.optional(Schema.Boolean),
262});
263export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
264​
265export const ProjectEntry = Schema.Struct({
266 path: TrimmedNonEmptyString,
267 kind: ProjectEntryKind,
268 ignored: Schema.optional(Schema.Boolean),
269});
270export type ProjectEntry = typeof ProjectEntry.Type;
271​
272export const ProjectSearchEntriesResult = Schema.Struct({
273 entries: Schema.Array(ProjectEntry),
274 truncated: Schema.Boolean,
275});
276export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
277​
278export const ProjectSearchContentsInput = Schema.Struct({
279 cwd: TrimmedNonEmptyString,
280 // Whitespace is significant in content queries (" foo", regex trailing
281 // spaces), so the query is deliberately not trimmed on the wire.
282 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
283 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
284 caseSensitive: Schema.Boolean,
285 wholeWord: Schema.Boolean,
286 useRegex: Schema.Boolean,
287});
288export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
289​
290export const ProjectContentMatchRange = Schema.Struct({
291 start: NonNegativeInt,
292 end: NonNegativeInt,
293});
294export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
295​
296export const ProjectContentMatch = Schema.Struct({
297 path: TrimmedNonEmptyString,
298 lineNumber: PositiveInt,
299 lineContent: Schema.String,
300 matchRanges: Schema.Array(ProjectContentMatchRange),
301});
302export type ProjectContentMatch = typeof ProjectContentMatch.Type;
303​
304export const ProjectSearchContentsResult = Schema.Struct({
305 matches: Schema.Array(ProjectContentMatch),
306 truncated: Schema.Boolean,
307 regexFallbackError: Schema.optional(Schema.String),
308});
309export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
310​
311export const ProjectListEntriesInput = Schema.Struct({
312 cwd: TrimmedNonEmptyString,
313 // Present for immediate filesystem children, including ignored entries; empty means root.
314 // Omitted preserves the indexed recursive listing used by older clients.
315 directoryPath: Schema.optional(TrimmedString),
316});
317export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
318​
319export const ProjectListEntriesResult = Schema.Struct({
320 entries: Schema.Array(ProjectEntry),
321 truncated: Schema.Boolean,
322});
323export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
324​
325export const ProjectEntriesFailure = Schema.Literals([
326 "workspace_root_not_found",
327 "workspace_root_create_failed",
328 "workspace_root_stat_failed",
329 "workspace_root_not_directory",
330 "search_index_create_failed",
331 "search_index_scan_timed_out",
332 "search_index_search_failed",
333 "directory_list_failed",
334]);
335export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
336​
337type ProjectEntriesFailureContext = {
338 readonly failure: ProjectEntriesFailure;
339 readonly normalizedCwd?: string;
340 readonly timeout?: string;
341 readonly detail?: string;
342 readonly cause?: unknown;
343};
344​
345function decodedProjectErrorMessage(props: object): string | undefined {
346 if (!("message" in props)) return undefined;
347 return typeof props.message === "string" ? props.message : undefined;
349​
350export class ProjectSearchEntriesError extends Schema.TaggedError<ProjectSearchEntriesError>()(
351 "ProjectSearchEntriesError",
352 {
353 cwd: Schema.optional(TrimmedNonEmptyString),
354 queryLength: Schema.optional(NonNegativeInt),
355 limit: Schema.optional(PositiveInt),
356 failure: Schema.optional(ProjectEntriesFailure),
357 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
358 timeout: Schema.optional(TrimmedNonEmptyString),
359 detail: Schema.optional(TrimmedNonEmptyString),
360 message: TrimmedNonEmptyString,
361 cause: Schema.optional(Schema.Defect()),
362 },
363) {
364 // The structured fields are optional on the wire so newer peers can decode legacy message-only
365 // failures. New application code must provide them through this constructor.
366 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
367 constructor(
368 props: ProjectEntriesFailureContext & {
369 readonly cwd: string;
370 readonly queryLength: number;
371 readonly limit: number;
372 },
373 ) {
374 super({
375 ...props,
376 message:
377 decodedProjectErrorMessage(props) ??
378 `Failed to search workspace entries in '${props.cwd}'.`,
379 } as any);
380 }
382​
383export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
384 "ProjectSearchContentsError",
385 {
386 cwd: Schema.optional(TrimmedNonEmptyString),
387 queryLength: Schema.optional(NonNegativeInt),
388 limit: Schema.optional(PositiveInt),
389 failure: Schema.optional(ProjectEntriesFailure),
390 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
391 timeout: Schema.optional(TrimmedNonEmptyString),
392 detail: Schema.optional(TrimmedNonEmptyString),
393 message: TrimmedNonEmptyString,
394 cause: Schema.optional(Schema.Defect()),
395 },
396) {
397 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
398 constructor(
399 props: ProjectEntriesFailureContext & {
400 readonly cwd: string;
401 readonly queryLength: number;
402 readonly limit: number;
403 },
404 ) {
405 super({
406 ...props,
407 message:
408 decodedProjectErrorMessage(props) ??
409 `Failed to search workspace contents in '${props.cwd}'.`,
410 } as any);
411 }
413​
414export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
415 "ProjectListEntriesError",
416 {
417 cwd: Schema.optional(TrimmedNonEmptyString),
418 failure: Schema.optional(ProjectEntriesFailure),
419 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
420 timeout: Schema.optional(TrimmedNonEmptyString),
421 detail: Schema.optional(TrimmedNonEmptyString),
422 message: TrimmedNonEmptyString,
423 cause: Schema.optional(Schema.Defect()),
424 },
425) {
426 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
427 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
428 super({
429 ...props,
430 message:
431 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
432 } as any);
433 }
435​
436export const ProjectReadFileInput = Schema.Struct({
437 cwd: TrimmedNonEmptyString,
438 // Workspace-relative, or an absolute host path for a file outside the
439 // workspace. Only workspace-relative paths can be written back.
440 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
441});
442export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
443​
444export const ProjectReadFileResult = Schema.Struct({
445 relativePath: TrimmedNonEmptyString,
446 contents: Schema.String,
447 byteLength: NonNegativeInt,
448 truncated: Schema.Boolean,
449});
450export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
451​
452export const ProjectFileFailure = Schema.Literals([
453 "workspace_path_outside_root",
454 "resolved_path_outside_root",
455 "path_not_file",
456 "binary_file",
457 "operation_failed",
458]);
459export type ProjectFileFailure = typeof ProjectFileFailure.Type;
460​
461export const ProjectFileOperation = Schema.Literals([
462 "realpath-workspace-root",
463 "realpath-target",
464 "open",
465 "stat",
466 "read",
467 "close",
468 "make-directory",
469 "write-file",
470]);
471export type ProjectFileOperation = typeof ProjectFileOperation.Type;
472​
473type ProjectFileFailureContext = {
474 readonly cwd: string;
475 readonly relativePath: string;
476 readonly failure: ProjectFileFailure;
477 readonly resolvedPath?: string;
478 readonly resolvedWorkspaceRoot?: string;
479 readonly operation?: ProjectFileOperation;
480 readonly operationPath?: string;
481 readonly cause?: unknown;
482};
483​
484export class ProjectReadFileError extends Schema.TaggedError<ProjectReadFileError>()(
485 "ProjectReadFileError",
486 {
487 cwd: Schema.optional(TrimmedNonEmptyString),
488 relativePath: Schema.optional(TrimmedNonEmptyString),
489 failure: Schema.optional(ProjectFileFailure),
490 resolvedPath: Schema.optional(TrimmedNonEmptyString),
491 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
492 operation: Schema.optional(ProjectFileOperation),
493 operationPath: Schema.optional(TrimmedNonEmptyString),
494 message: TrimmedNonEmptyString,
495 cause: Schema.optional(Schema.Defect()),
496 },
497) {
498 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
499 constructor(props: ProjectFileFailureContext) {
500 super({
501 ...props,
502 message:
503 decodedProjectErrorMessage(props) ??
504 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
505 } as any);
506 }
508​
509export const ProjectWriteFileInput = Schema.Struct({
510 cwd: TrimmedNonEmptyString,
511 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
512 contents: Schema.String,
513});
514export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
515​
516export const ProjectWriteFileResult = Schema.Struct({
517 relativePath: TrimmedNonEmptyString,
518});
519export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
520​
521/** The environment's Scratch project, created on first request. */
522export const ProjectEnsureScratchResult = Schema.Struct({
523 projectId: ProjectId,
524});
525export type ProjectEnsureScratchResult = typeof ProjectEnsureScratchResult.Type;
526​
527/** A project started from just a name, in a new folder the server makes. */
528export const ProjectCreateNewInput = Schema.Struct({
529 name: TrimmedNonEmptyString.check(Schema.isMaxLength(200)),
530});
531export type ProjectCreateNewInput = typeof ProjectCreateNewInput.Type;
532​
533export const ProjectCreateNewResult = Schema.Struct({
534 projectId: ProjectId,
535 workspaceRoot: TrimmedNonEmptyString,
536 /** Why the first commit failed. The project and its files exist either way. */
537 commitError: Schema.optionalKey(TrimmedNonEmptyString),
538});
539export type ProjectCreateNewResult = typeof ProjectCreateNewResult.Type;
540​
541export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
542 "ProjectWriteFileError",
543 {
544 cwd: Schema.optional(TrimmedNonEmptyString),
545 relativePath: Schema.optional(TrimmedNonEmptyString),
546 failure: Schema.optional(ProjectFileFailure),
547 resolvedPath: Schema.optional(TrimmedNonEmptyString),
548 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
549 operation: Schema.optional(ProjectFileOperation),
550 operationPath: Schema.optional(TrimmedNonEmptyString),
551 message: TrimmedNonEmptyString,
552 cause: Schema.optional(Schema.Defect()),
553 },
554) {
555 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
556 constructor(props: ProjectFileFailureContext) {
557 super({
558 ...props,
559 message:
560 decodedProjectErrorMessage(props) ??
561 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
562 } as any);
563 }