Open History to see the last commit
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 ThreadId,
14 TrimmedNonEmptyString,
15 TrimmedString,
16 type UnknownUnionMember,
17} from "./baseSchemas.ts";
18
19const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
20const PROJECT_SEARCH_CONTENTS_MAX_LIMIT = 500;
21const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
22const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
23
24export const ProjectScriptIcon = Schema.Literals([
25 "play",
26 "test",
27 "lint",
28 "configure",
29 "build",
30 "debug",
31]);
32export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
33
34export const ProjectScript = Schema.Struct({
35 id: TrimmedNonEmptyString,
36 name: TrimmedNonEmptyString,
37 command: TrimmedNonEmptyString,
38 icon: ProjectScriptIcon,
39 runOnWorktreeCreate: Schema.Boolean,
40 runOnSettle: Schema.optional(Schema.Boolean),
41 stopOnSettle: Schema.optional(Schema.Boolean),
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});
97export const ProjectFaviconPath = TrimmedNonEmptyString.check(
98 Schema.isMaxLength(1024),
99 Schema.isPattern(/\.(?:avif|gif|icns|ico|jpe?g|png|svg|webp)$/i),
100);
101export type ProjectFaviconPath = typeof ProjectFaviconPath.Type;
102
103export const ProjectIconOverride = Schema.Union([
104 ProjectLucideIcon,
105 ProjectEmojiIcon,
106 ProjectMonogramIcon,
107]);
108export type ProjectIconOverride = typeof ProjectIconOverride.Type;
109
110const ProjectLucideIconWithLegacyMonogram = Schema.Struct({
111 ...ProjectLucideIcon.fields,
112 monogramText: Schema.optional(ProjectMonogramText),
113 monogram: Schema.optional(ProjectMonogramText),
114});
115const projectIconMembers = [
116 ProjectLucideIconWithLegacyMonogram,
117 ProjectEmojiIcon,
118 ProjectMonogramIcon,
119] as const;
120type ProjectIconMember = (typeof projectIconMembers)[number]["Type"];
121
122const fromLegacyMonogram = (icon: ProjectIconMember): ProjectIconOverride => {
123 if (icon.kind !== "lucide") return icon;
124 const text = icon.monogramText ?? icon.monogram;
125 return text === undefined
126 ? { kind: "lucide", name: icon.name, color: icon.color }
127 : { kind: "monogram", text, color: icon.color };
128};
129
130export const StoredProjectIcon = Schema.Union(projectIconMembers).pipe(
131 Schema.decodeTo(
132 Schema.toType(ProjectIconOverride),
133 SchemaTransformation.transform<ProjectIconOverride, ProjectIconMember>({
134 decode: fromLegacyMonogram,
135 encode: (icon) => icon,
136 }),
137 ),
138);
139
140export const ReceivedProjectIcon = ForwardCompatibleUnion(projectIconMembers, "kind").pipe(
141 Schema.decodeTo(
142 Schema.NullOr(Schema.toType(ProjectIconOverride)),
143 SchemaTransformation.transform<
144 ProjectIconOverride | null,
145 ProjectIconMember | UnknownUnionMember<"kind">
146 >({
147 decode: (icon) => (isUnknownUnionMember(icon) ? null : fromLegacyMonogram(icon)),
148 encode: (icon) => icon as ProjectIconMember,
149 }),
150 ),
151);
152
153export const Project = Schema.Struct({
154 id: ProjectId,
155 title: TrimmedNonEmptyString,
156 workspaceRoot: TrimmedNonEmptyString,
157 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
158 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
159 projectIcon: Schema.optional(Schema.NullOr(ReceivedProjectIcon)),
160 defaultModelSelection: Schema.NullOr(ModelSelection),
161 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
162 // Opt-in because background sync performs network I/O and may move the checkout.
163 autoPull: Schema.optional(Schema.Boolean),
164 scripts: Schema.Array(ProjectScript),
165 pinnedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
166 archivedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
167 createdAt: IsoDateTime,
168 updatedAt: IsoDateTime,
169 deletedAt: Schema.NullOr(IsoDateTime),
170});
171export type Project = typeof Project.Type;
172
173export const ProjectSnapshot = Schema.Struct({
174 projects: Schema.Array(Project),
175 updatedAt: IsoDateTime,
176});
177export type ProjectSnapshot = typeof ProjectSnapshot.Type;
178
179export const ProjectCreatePayload = Schema.Struct({
180 title: TrimmedNonEmptyString,
181 workspaceRoot: TrimmedNonEmptyString,
182 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
183 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
184 scripts: Schema.optional(Schema.Array(ProjectScript)),
185});
186export type ProjectCreatePayload = typeof ProjectCreatePayload.Type;
187
188export const ProjectUpdatePayload = Schema.Struct({
189 title: Schema.optional(TrimmedNonEmptyString),
190 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
191 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
192 autoPull: Schema.optional(Schema.Boolean),
193 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
194 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
195 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
196 scripts: Schema.optional(Schema.Array(ProjectScript)),
197 pinned: Schema.optional(Schema.Boolean),
198 archived: Schema.optional(
199 Schema.Boolean.annotate({
200 description: "Threads and files are kept.",
201 }),
202 ),
203});
204export type ProjectUpdatePayload = typeof ProjectUpdatePayload.Type;
205
206export const ProjectMutation = Schema.Union([
207 Schema.Struct({
208 type: Schema.Literal("project.create"),
209 commandId: CommandId,
210 projectId: ProjectId,
211 ...ProjectCreatePayload.fields,
212 }),
213 Schema.Struct({
214 type: Schema.Literal("project.update"),
215 commandId: CommandId,
216 projectId: ProjectId,
217 ...ProjectUpdatePayload.fields,
218 }),
219 Schema.Struct({
220 type: Schema.Literal("project.delete"),
221 commandId: CommandId,
222 projectId: ProjectId,
223 force: Schema.optional(Schema.Boolean),
224 }),
225]);
226export type ProjectMutation = typeof ProjectMutation.Type;
227
228export class ProjectMutationError extends Schema.TaggedError<ProjectMutationError>()(
229 "ProjectMutationError",
230 {
231 commandId: CommandId,
232 message: Schema.String,
233 cause: Schema.optional(Schema.Defect()),
234 },
235) {}
236
237export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
238export type ProjectEntryKind = typeof ProjectEntryKind.Type;
239
240export const ProjectSearchEntriesInput = Schema.Struct({
241 cwd: TrimmedNonEmptyString,
242 query: TrimmedString.check(Schema.isMaxLength(256)),
243 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
244 kind: Schema.optional(ProjectEntryKind),
245 imageOnly: Schema.optional(Schema.Boolean),
246});
247export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
248
249export const ProjectEntry = Schema.Struct({
250 path: TrimmedNonEmptyString,
251 kind: ProjectEntryKind,
252 ignored: Schema.optional(Schema.Boolean),
253});
254export type ProjectEntry = typeof ProjectEntry.Type;
255
256export const ProjectSearchEntriesResult = Schema.Struct({
257 entries: Schema.Array(ProjectEntry),
258 truncated: Schema.Boolean,
259});
260export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
261
262export const ProjectSearchContentsInput = Schema.Struct({
263 cwd: TrimmedNonEmptyString,
264 // Preserve whitespace because it is significant in content queries.
265 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
266 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
267 caseSensitive: Schema.Boolean,
268 wholeWord: Schema.Boolean,
269 useRegex: Schema.Boolean,
270});
271export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
272
273export const ProjectContentMatchRange = Schema.Struct({
274 start: NonNegativeInt,
275 end: NonNegativeInt,
276});
277export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
278
279export const ProjectContentMatch = Schema.Struct({
280 path: TrimmedNonEmptyString,
281 lineNumber: PositiveInt,
282 lineContent: Schema.String,
283 matchRanges: Schema.Array(ProjectContentMatchRange),
284});
285export type ProjectContentMatch = typeof ProjectContentMatch.Type;
286
287export const ProjectSearchContentsResult = Schema.Struct({
288 matches: Schema.Array(ProjectContentMatch),
289 truncated: Schema.Boolean,
290 regexFallbackError: Schema.optional(Schema.String),
291});
292export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
293
294export const ProjectListEntriesInput = Schema.Struct({
295 cwd: TrimmedNonEmptyString,
296 directoryPath: Schema.optional(TrimmedString),
297});
298export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
299
300export const ProjectListEntriesResult = Schema.Struct({
301 entries: Schema.Array(ProjectEntry),
302 truncated: Schema.Boolean,
303});
304export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
305
306// Untracked files git ignores. Wholly ignored folders such as `node_modules`
307// are left out, so the list stays small enough to pick from.
308export const ProjectListIgnoredFilesInput = Schema.Struct({
309 cwd: TrimmedNonEmptyString,
310});
311export type ProjectListIgnoredFilesInput = typeof ProjectListIgnoredFilesInput.Type;
312
313export const ProjectIgnoredFile = Schema.Struct({
314 path: TrimmedNonEmptyString,
315 byteLength: NonNegativeInt,
316});
317export type ProjectIgnoredFile = typeof ProjectIgnoredFile.Type;
318
319export const ProjectListIgnoredFilesResult = Schema.Struct({
320 files: Schema.Array(ProjectIgnoredFile),
321 truncated: Schema.Boolean,
322});
323export type ProjectListIgnoredFilesResult = typeof ProjectListIgnoredFilesResult.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;
348}
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 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
365 constructor(
366 props: ProjectEntriesFailureContext & {
367 readonly cwd: string;
368 readonly queryLength: number;
369 readonly limit: number;
370 },
371 ) {
372 super({
373 ...props,
374 message:
375 decodedProjectErrorMessage(props) ??
376 `Failed to search workspace entries in '${props.cwd}'.`,
377 } as any);
378 }
379}
380
381export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
382 "ProjectSearchContentsError",
383 {
384 cwd: Schema.optional(TrimmedNonEmptyString),
385 queryLength: Schema.optional(NonNegativeInt),
386 limit: Schema.optional(PositiveInt),
387 failure: Schema.optional(ProjectEntriesFailure),
388 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
389 timeout: Schema.optional(TrimmedNonEmptyString),
390 detail: Schema.optional(TrimmedNonEmptyString),
391 message: TrimmedNonEmptyString,
392 cause: Schema.optional(Schema.Defect()),
393 },
394) {
395 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
396 constructor(
397 props: ProjectEntriesFailureContext & {
398 readonly cwd: string;
399 readonly queryLength: number;
400 readonly limit: number;
401 },
402 ) {
403 super({
404 ...props,
405 message:
406 decodedProjectErrorMessage(props) ??
407 `Failed to search workspace contents in '${props.cwd}'.`,
408 } as any);
409 }
410}
411
412export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
413 "ProjectListEntriesError",
414 {
415 cwd: Schema.optional(TrimmedNonEmptyString),
416 failure: Schema.optional(ProjectEntriesFailure),
417 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
418 timeout: Schema.optional(TrimmedNonEmptyString),
419 detail: Schema.optional(TrimmedNonEmptyString),
420 message: TrimmedNonEmptyString,
421 cause: Schema.optional(Schema.Defect()),
422 },
423) {
424 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
425 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
426 super({
427 ...props,
428 message:
429 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
430 } as any);
431 }
432}
433
434export const ProjectReadFileInput = Schema.Struct({
435 cwd: TrimmedNonEmptyString,
436 // Workspace-relative or absolute host path; only workspace-relative paths are writable.
437 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
438 // Optional files can be absent without failing the read.
439 allowMissing: Schema.optional(Schema.Boolean),
440});
441export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
442
443export const ProjectReadFileResult = Schema.Struct({
444 relativePath: TrimmedNonEmptyString,
445 contents: Schema.String,
446 byteLength: NonNegativeInt,
447 truncated: Schema.Boolean,
448 // Omitted by older servers; false only for an absent optional file.
449 exists: Schema.optional(Schema.Boolean),
450});
451export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
452
453export const ProjectFileFailure = Schema.Literals([
454 "workspace_path_outside_root",
455 "resolved_path_outside_root",
456 "path_not_file",
457 "binary_file",
458 "operation_failed",
459]);
460export type ProjectFileFailure = typeof ProjectFileFailure.Type;
461
462export const ProjectFileOperation = Schema.Literals([
463 "realpath-workspace-root",
464 "realpath-target",
465 "open",
466 "stat",
467 "read",
468 "close",
469 "make-directory",
470 "write-file",
471]);
472export type ProjectFileOperation = typeof ProjectFileOperation.Type;
473
474type ProjectFileFailureContext = {
475 readonly cwd: string;
476 readonly relativePath: string;
477 readonly failure: ProjectFileFailure;
478 readonly resolvedPath?: string;
479 readonly resolvedWorkspaceRoot?: string;
480 readonly operation?: ProjectFileOperation;
481 readonly operationPath?: string;
482 readonly cause?: unknown;
483};
484
485export class ProjectReadFileError extends Schema.TaggedError<ProjectReadFileError>()(
486 "ProjectReadFileError",
487 {
488 cwd: Schema.optional(TrimmedNonEmptyString),
489 relativePath: Schema.optional(TrimmedNonEmptyString),
490 failure: Schema.optional(ProjectFileFailure),
491 resolvedPath: Schema.optional(TrimmedNonEmptyString),
492 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
493 operation: Schema.optional(ProjectFileOperation),
494 operationPath: Schema.optional(TrimmedNonEmptyString),
495 message: TrimmedNonEmptyString,
496 cause: Schema.optional(Schema.Defect()),
497 },
498) {
499 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
500 constructor(props: ProjectFileFailureContext) {
501 super({
502 ...props,
503 message:
504 decodedProjectErrorMessage(props) ??
505 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
506 } as any);
507 }
508}
509
510export const ProjectWriteFileInput = Schema.Struct({
511 cwd: TrimmedNonEmptyString,
512 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
513 contents: Schema.String,
514});
515export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
516
517export const ProjectWriteFileResult = Schema.Struct({
518 relativePath: TrimmedNonEmptyString,
519});
520export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
521
522export const ProjectEnsureScratchResult = Schema.Struct({
523 projectId: ProjectId,
524});
525export type ProjectEnsureScratchResult = typeof ProjectEnsureScratchResult.Type;
526
527export const ProjectCreateNewInput = Schema.Struct({
528 name: TrimmedNonEmptyString.check(Schema.isMaxLength(200)),
529});
530export type ProjectCreateNewInput = typeof ProjectCreateNewInput.Type;
531
532export const ProjectCreateNewResult = Schema.Struct({
533 projectId: ProjectId,
534 workspaceRoot: TrimmedNonEmptyString,
535 /** The project and its files exist either way. */
536 commitError: Schema.optionalKey(TrimmedNonEmptyString),
537});
538export type ProjectCreateNewResult = typeof ProjectCreateNewResult.Type;
539
540export const ProjectCreateFromThreadInput = Schema.Struct({
541 threadId: ThreadId,
542});
543export type ProjectCreateFromThreadInput = typeof ProjectCreateFromThreadInput.Type;
544
545export const ProjectCreateFromThreadResult = Schema.Struct({
546 projectId: ProjectId,
547});
548export type ProjectCreateFromThreadResult = typeof ProjectCreateFromThreadResult.Type;
549
550export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
551 "ProjectWriteFileError",
552 {
553 cwd: Schema.optional(TrimmedNonEmptyString),
554 relativePath: Schema.optional(TrimmedNonEmptyString),
555 failure: Schema.optional(ProjectFileFailure),
556 resolvedPath: Schema.optional(TrimmedNonEmptyString),
557 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
558 operation: Schema.optional(ProjectFileOperation),
559 operationPath: Schema.optional(TrimmedNonEmptyString),
560 message: TrimmedNonEmptyString,
561 cause: Schema.optional(Schema.Defect()),
562 },
563) {
564 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
565 constructor(props: ProjectFileFailureContext) {
566 super({
567 ...props,
568 message:
569 decodedProjectErrorMessage(props) ??
570 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
571 } as any);
572 }
573}