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