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

project.ts

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