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 async: Schema.optional(Schema.Boolean),
42 previewUrl: Schema.optional(TrimmedNonEmptyString),
43 autoOpenPreview: Schema.optional(Schema.Boolean),
44});
45export type ProjectScript = typeof ProjectScript.Type;
46
47export const ProjectIconColor = Schema.Literals([
48 "gray",
49 "red",
50 "orange",
51 "amber",
52 "yellow",
53 "lime",
54 "green",
55 "emerald",
56 "teal",
57 "cyan",
58 "sky",
59 "blue",
60 "indigo",
61 "violet",
62 "purple",
63 "fuchsia",
64 "pink",
65 "rose",
66]);
67export type ProjectIconColor = typeof ProjectIconColor.Type;
68
69const ProjectLucideIconName = TrimmedNonEmptyString.check(
70 Schema.isMaxLength(64),
71 Schema.isPattern(/^[a-z0-9]+(?:-[a-z0-9]+)*$/),
72);
73
74const ProjectEmoji = TrimmedNonEmptyString.check(Schema.isMaxLength(32));
75
76// Grapheme-count validation belongs to the server command boundary, not snapshot decoding.
77export const ProjectMonogramText = TrimmedNonEmptyString.check(
78 Schema.isMaxLength(32),
79 Schema.isPattern(/^[\p{L}\p{N}][\p{L}\p{N}\p{M}\u200c\u200d]*$/u),
80);
81
82const ProjectLucideIcon = Schema.Struct({
83 kind: Schema.Literal("lucide"),
84 name: ProjectLucideIconName,
85 color: ProjectIconColor,
86});
87const ProjectEmojiIcon = Schema.Struct({
88 kind: Schema.Literal("emoji"),
89 emoji: ProjectEmoji,
90});
91const ProjectMonogramIcon = Schema.Struct({
92 kind: Schema.Literal("monogram"),
93 text: ProjectMonogramText,
94 color: ProjectIconColor,
95});
96export const ProjectFaviconPath = TrimmedNonEmptyString.check(
97 Schema.isMaxLength(1024),
98 Schema.isPattern(/\.(?:avif|gif|icns|ico|jpe?g|png|svg|webp)$/i),
99);
100export type ProjectFaviconPath = typeof ProjectFaviconPath.Type;
101
102export const ProjectIconOverride = Schema.Union([
103 ProjectLucideIcon,
104 ProjectEmojiIcon,
105 ProjectMonogramIcon,
106]);
107export type ProjectIconOverride = typeof ProjectIconOverride.Type;
108
109const ProjectLucideIconWithLegacyMonogram = Schema.Struct({
110 ...ProjectLucideIcon.fields,
111 monogramText: Schema.optional(ProjectMonogramText),
112 monogram: Schema.optional(ProjectMonogramText),
113});
114const projectIconMembers = [
115 ProjectLucideIconWithLegacyMonogram,
116 ProjectEmojiIcon,
117 ProjectMonogramIcon,
118] as const;
119type ProjectIconMember = (typeof projectIconMembers)[number]["Type"];
120
121const fromLegacyMonogram = (icon: ProjectIconMember): ProjectIconOverride => {
122 if (icon.kind !== "lucide") return icon;
123 const text = icon.monogramText ?? icon.monogram;
124 return text === undefined
125 ? { kind: "lucide", name: icon.name, color: icon.color }
126 : { kind: "monogram", text, color: icon.color };
127};
128
129export const StoredProjectIcon = Schema.Union(projectIconMembers).pipe(
130 Schema.decodeTo(
131 Schema.toType(ProjectIconOverride),
132 SchemaTransformation.transform<ProjectIconOverride, ProjectIconMember>({
133 decode: fromLegacyMonogram,
134 encode: (icon) => icon,
135 }),
136 ),
137);
138
139export const ReceivedProjectIcon = ForwardCompatibleUnion(projectIconMembers, "kind").pipe(
140 Schema.decodeTo(
141 Schema.NullOr(Schema.toType(ProjectIconOverride)),
142 SchemaTransformation.transform<
143 ProjectIconOverride | null,
144 ProjectIconMember | UnknownUnionMember<"kind">
145 >({
146 decode: (icon) => (isUnknownUnionMember(icon) ? null : fromLegacyMonogram(icon)),
147 encode: (icon) => icon as ProjectIconMember,
148 }),
149 ),
150);
151
152export const Project = Schema.Struct({
153 id: ProjectId,
154 title: TrimmedNonEmptyString,
155 workspaceRoot: TrimmedNonEmptyString,
156 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
157 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
158 projectIcon: Schema.optional(Schema.NullOr(ReceivedProjectIcon)),
159 defaultModelSelection: Schema.NullOr(ModelSelection),
160 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
161 // Opt-in because background sync performs network I/O and may move the checkout.
162 autoPull: Schema.optional(Schema.Boolean),
163 scripts: Schema.Array(ProjectScript),
164 pinnedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
165 archivedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
166 createdAt: IsoDateTime,
167 updatedAt: IsoDateTime,
168 deletedAt: Schema.NullOr(IsoDateTime),
169});
170export type Project = typeof Project.Type;
171
172export const ProjectSnapshot = Schema.Struct({
173 projects: Schema.Array(Project),
174 updatedAt: IsoDateTime,
175});
176export type ProjectSnapshot = typeof ProjectSnapshot.Type;
177
178export const ProjectCreatePayload = Schema.Struct({
179 title: TrimmedNonEmptyString,
180 workspaceRoot: TrimmedNonEmptyString,
181 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
182 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
183 scripts: Schema.optional(Schema.Array(ProjectScript)),
184});
185export type ProjectCreatePayload = typeof ProjectCreatePayload.Type;
186
187export const ProjectUpdatePayload = Schema.Struct({
188 title: Schema.optional(TrimmedNonEmptyString),
189 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
190 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
191 autoPull: Schema.optional(Schema.Boolean),
192 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
193 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
194 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
195 scripts: Schema.optional(Schema.Array(ProjectScript)),
196 pinned: Schema.optional(Schema.Boolean),
197 archived: Schema.optional(
198 Schema.Boolean.annotate({
199 description: "Threads and files are kept.",
200 }),
201 ),
202});
203export type ProjectUpdatePayload = typeof ProjectUpdatePayload.Type;
204
205export const ProjectMutation = Schema.Union([
206 Schema.Struct({
207 type: Schema.Literal("project.create"),
208 commandId: CommandId,
209 projectId: ProjectId,
210 ...ProjectCreatePayload.fields,
211 }),
212 Schema.Struct({
213 type: Schema.Literal("project.update"),
214 commandId: CommandId,
215 projectId: ProjectId,
216 ...ProjectUpdatePayload.fields,
217 }),
218 Schema.Struct({
219 type: Schema.Literal("project.delete"),
220 commandId: CommandId,
221 projectId: ProjectId,
222 force: Schema.optional(Schema.Boolean),
223 }),
224]);
225export type ProjectMutation = typeof ProjectMutation.Type;
226
227export class ProjectMutationError extends Schema.TaggedError<ProjectMutationError>()(
228 "ProjectMutationError",
229 {
230 commandId: CommandId,
231 message: Schema.String,
232 cause: Schema.optional(Schema.Defect()),
233 },
234) {}
235
236export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
237export type ProjectEntryKind = typeof ProjectEntryKind.Type;
238
239export const ProjectSearchEntriesInput = Schema.Struct({
240 cwd: TrimmedNonEmptyString,
241 query: TrimmedString.check(Schema.isMaxLength(256)),
242 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
243 kind: Schema.optional(ProjectEntryKind),
244 imageOnly: Schema.optional(Schema.Boolean),
245});
246export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
247
248export const ProjectEntry = Schema.Struct({
249 path: TrimmedNonEmptyString,
250 kind: ProjectEntryKind,
251 ignored: Schema.optional(Schema.Boolean),
252});
253export type ProjectEntry = typeof ProjectEntry.Type;
254
255export const ProjectSearchEntriesResult = Schema.Struct({
256 entries: Schema.Array(ProjectEntry),
257 truncated: Schema.Boolean,
258});
259export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
260
261export const ProjectSearchContentsInput = Schema.Struct({
262 cwd: TrimmedNonEmptyString,
263 // Preserve whitespace because it is significant in content queries.
264 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
265 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
266 caseSensitive: Schema.Boolean,
267 wholeWord: Schema.Boolean,
268 useRegex: Schema.Boolean,
269});
270export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
271
272export const ProjectContentMatchRange = Schema.Struct({
273 start: NonNegativeInt,
274 end: NonNegativeInt,
275});
276export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
277
278export const ProjectContentMatch = Schema.Struct({
279 path: TrimmedNonEmptyString,
280 lineNumber: PositiveInt,
281 lineContent: Schema.String,
282 matchRanges: Schema.Array(ProjectContentMatchRange),
283});
284export type ProjectContentMatch = typeof ProjectContentMatch.Type;
285
286export const ProjectSearchContentsResult = Schema.Struct({
287 matches: Schema.Array(ProjectContentMatch),
288 truncated: Schema.Boolean,
289 regexFallbackError: Schema.optional(Schema.String),
290});
291export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
292
293export const ProjectListEntriesInput = Schema.Struct({
294 cwd: TrimmedNonEmptyString,
295 directoryPath: Schema.optional(TrimmedString),
296});
297export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
298
299export const ProjectListEntriesResult = Schema.Struct({
300 entries: Schema.Array(ProjectEntry),
301 truncated: Schema.Boolean,
302});
303export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
304
305// Untracked files git ignores. Wholly ignored folders such as `node_modules`
306// are left out, so the list stays small enough to pick from.
307export const ProjectListIgnoredFilesInput = Schema.Struct({
308 cwd: TrimmedNonEmptyString,
309});
310export type ProjectListIgnoredFilesInput = typeof ProjectListIgnoredFilesInput.Type;
311
312export const ProjectIgnoredFile = Schema.Struct({
313 path: TrimmedNonEmptyString,
314 byteLength: NonNegativeInt,
315});
316export type ProjectIgnoredFile = typeof ProjectIgnoredFile.Type;
317
318export const ProjectListIgnoredFilesResult = Schema.Struct({
319 files: Schema.Array(ProjectIgnoredFile),
320 truncated: Schema.Boolean,
321});
322export type ProjectListIgnoredFilesResult = typeof ProjectListIgnoredFilesResult.Type;
323
324export const ProjectEntriesFailure = Schema.Literals([
325 "workspace_root_not_found",
326 "workspace_root_create_failed",
327 "workspace_root_stat_failed",
328 "workspace_root_not_directory",
329 "search_index_create_failed",
330 "search_index_scan_timed_out",
331 "search_index_search_failed",
332 "directory_list_failed",
333]);
334export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
335
336type ProjectEntriesFailureContext = {
337 readonly failure: ProjectEntriesFailure;
338 readonly normalizedCwd?: string;
339 readonly timeout?: string;
340 readonly detail?: string;
341 readonly cause?: unknown;
342};
343
344function decodedProjectErrorMessage(props: object): string | undefined {
345 if (!("message" in props)) return undefined;
346 return typeof props.message === "string" ? props.message : undefined;
347}
348
349export class ProjectSearchEntriesError extends Schema.TaggedError<ProjectSearchEntriesError>()(
350 "ProjectSearchEntriesError",
351 {
352 cwd: Schema.optional(TrimmedNonEmptyString),
353 queryLength: Schema.optional(NonNegativeInt),
354 limit: Schema.optional(PositiveInt),
355 failure: Schema.optional(ProjectEntriesFailure),
356 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
357 timeout: Schema.optional(TrimmedNonEmptyString),
358 detail: Schema.optional(TrimmedNonEmptyString),
359 message: TrimmedNonEmptyString,
360 cause: Schema.optional(Schema.Defect()),
361 },
362) {
363 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
364 constructor(
365 props: ProjectEntriesFailureContext & {
366 readonly cwd: string;
367 readonly queryLength: number;
368 readonly limit: number;
369 },
370 ) {
371 super({
372 ...props,
373 message:
374 decodedProjectErrorMessage(props) ??
375 `Failed to search workspace entries in '${props.cwd}'.`,
376 } as any);
377 }
378}
379
380export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
381 "ProjectSearchContentsError",
382 {
383 cwd: Schema.optional(TrimmedNonEmptyString),
384 queryLength: Schema.optional(NonNegativeInt),
385 limit: Schema.optional(PositiveInt),
386 failure: Schema.optional(ProjectEntriesFailure),
387 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
388 timeout: Schema.optional(TrimmedNonEmptyString),
389 detail: Schema.optional(TrimmedNonEmptyString),
390 message: TrimmedNonEmptyString,
391 cause: Schema.optional(Schema.Defect()),
392 },
393) {
394 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
395 constructor(
396 props: ProjectEntriesFailureContext & {
397 readonly cwd: string;
398 readonly queryLength: number;
399 readonly limit: number;
400 },
401 ) {
402 super({
403 ...props,
404 message:
405 decodedProjectErrorMessage(props) ??
406 `Failed to search workspace contents in '${props.cwd}'.`,
407 } as any);
408 }
409}
410
411export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
412 "ProjectListEntriesError",
413 {
414 cwd: Schema.optional(TrimmedNonEmptyString),
415 failure: Schema.optional(ProjectEntriesFailure),
416 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
417 timeout: Schema.optional(TrimmedNonEmptyString),
418 detail: Schema.optional(TrimmedNonEmptyString),
419 message: TrimmedNonEmptyString,
420 cause: Schema.optional(Schema.Defect()),
421 },
422) {
423 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
424 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
425 super({
426 ...props,
427 message:
428 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
429 } as any);
430 }
431}
432
433export const ProjectReadFileInput = Schema.Struct({
434 cwd: TrimmedNonEmptyString,
435 // Workspace-relative or absolute host path; only workspace-relative paths are writable.
436 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
437 // Optional files can be absent without failing the read.
438 allowMissing: Schema.optional(Schema.Boolean),
439});
440export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
441
442export const ProjectReadFileResult = Schema.Struct({
443 relativePath: TrimmedNonEmptyString,
444 contents: Schema.String,
445 byteLength: NonNegativeInt,
446 truncated: Schema.Boolean,
447 // Omitted by older servers; false only for an absent optional file.
448 exists: Schema.optional(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 }
507}
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
521export const ProjectEnsureScratchResult = Schema.Struct({
522 projectId: ProjectId,
523});
524export type ProjectEnsureScratchResult = typeof ProjectEnsureScratchResult.Type;
525
526export const ProjectCreateNewInput = Schema.Struct({
527 name: TrimmedNonEmptyString.check(Schema.isMaxLength(200)),
528});
529export type ProjectCreateNewInput = typeof ProjectCreateNewInput.Type;
530
531export const ProjectCreateNewResult = Schema.Struct({
532 projectId: ProjectId,
533 workspaceRoot: TrimmedNonEmptyString,
534 /** The project and its files exist either way. */
535 commitError: Schema.optionalKey(TrimmedNonEmptyString),
536});
537export type ProjectCreateNewResult = typeof ProjectCreateNewResult.Type;
538
539export const ProjectCreateFromThreadInput = Schema.Struct({
540 threadId: ThreadId,
541});
542export type ProjectCreateFromThreadInput = typeof ProjectCreateFromThreadInput.Type;
543
544export const ProjectCreateFromThreadResult = Schema.Struct({
545 projectId: ProjectId,
546});
547export type ProjectCreateFromThreadResult = typeof ProjectCreateFromThreadResult.Type;
548
549export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
550 "ProjectWriteFileError",
551 {
552 cwd: Schema.optional(TrimmedNonEmptyString),
553 relativePath: Schema.optional(TrimmedNonEmptyString),
554 failure: Schema.optional(ProjectFileFailure),
555 resolvedPath: Schema.optional(TrimmedNonEmptyString),
556 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
557 operation: Schema.optional(ProjectFileOperation),
558 operationPath: Schema.optional(TrimmedNonEmptyString),
559 message: TrimmedNonEmptyString,
560 cause: Schema.optional(Schema.Defect()),
561 },
562) {
563 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
564 constructor(props: ProjectFileFailureContext) {
565 super({
566 ...props,
567 message:
568 decodedProjectErrorMessage(props) ??
569 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
570 } as any);
571 }
572}