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 ProjectChange = Schema.Union([
176 Schema.Struct({ type: Schema.Literal("project.upserted"), project: Project }),
177 Schema.Struct({
178 type: Schema.Literal("project.deleted"),
179 projectId: ProjectId,
180 deletedAt: IsoDateTime,
181 }),
182]);
183export type ProjectChange = typeof ProjectChange.Type;
184
185export const ProjectCreatePayload = Schema.Struct({
186 title: TrimmedNonEmptyString,
187 workspaceRoot: TrimmedNonEmptyString,
188 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
189 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
190 scripts: Schema.optional(Schema.Array(ProjectScript)),
191});
192export type ProjectCreatePayload = typeof ProjectCreatePayload.Type;
193
194export const ProjectUpdatePayload = Schema.Struct({
195 title: Schema.optional(TrimmedNonEmptyString),
196 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
197 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
198 autoPull: Schema.optional(Schema.Boolean),
199 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
200 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
201 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
202 scripts: Schema.optional(Schema.Array(ProjectScript)),
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 // Whitespace is significant in content queries (" foo", regex trailing
265 // spaces), so the query is deliberately not trimmed on the wire.
266 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
267 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
268 caseSensitive: Schema.Boolean,
269 wholeWord: Schema.Boolean,
270 useRegex: Schema.Boolean,
271});
272export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
273
274export const ProjectContentMatchRange = Schema.Struct({
275 start: NonNegativeInt,
276 end: NonNegativeInt,
277});
278export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
279
280export const ProjectContentMatch = Schema.Struct({
281 path: TrimmedNonEmptyString,
282 lineNumber: PositiveInt,
283 lineContent: Schema.String,
284 matchRanges: Schema.Array(ProjectContentMatchRange),
285});
286export type ProjectContentMatch = typeof ProjectContentMatch.Type;
287
288export const ProjectSearchContentsResult = Schema.Struct({
289 matches: Schema.Array(ProjectContentMatch),
290 truncated: Schema.Boolean,
291 regexFallbackError: Schema.optional(Schema.String),
292});
293export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
294
295export const ProjectListEntriesInput = Schema.Struct({
296 cwd: TrimmedNonEmptyString,
297 directoryPath: Schema.optional(TrimmedString),
298});
299export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
300
301export const ProjectListEntriesResult = Schema.Struct({
302 entries: Schema.Array(ProjectEntry),
303 truncated: Schema.Boolean,
304});
305export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
306
307// Untracked files git ignores. Wholly ignored folders such as `node_modules`
308// are left out, so the list stays small enough to pick from.
309export const ProjectListIgnoredFilesInput = Schema.Struct({
310 cwd: TrimmedNonEmptyString,
311});
312export type ProjectListIgnoredFilesInput = typeof ProjectListIgnoredFilesInput.Type;
313
314export const ProjectIgnoredFile = Schema.Struct({
315 path: TrimmedNonEmptyString,
316 byteLength: NonNegativeInt,
317});
318export type ProjectIgnoredFile = typeof ProjectIgnoredFile.Type;
319
320export const ProjectListIgnoredFilesResult = Schema.Struct({
321 files: Schema.Array(ProjectIgnoredFile),
322 truncated: Schema.Boolean,
323});
324export type ProjectListIgnoredFilesResult = typeof ProjectListIgnoredFilesResult.Type;
325
326export const ProjectEntriesFailure = Schema.Literals([
327 "workspace_root_not_found",
328 "workspace_root_create_failed",
329 "workspace_root_stat_failed",
330 "workspace_root_not_directory",
331 "search_index_create_failed",
332 "search_index_scan_timed_out",
333 "search_index_search_failed",
334 "directory_list_failed",
335]);
336export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
337
338type ProjectEntriesFailureContext = {
339 readonly failure: ProjectEntriesFailure;
340 readonly normalizedCwd?: string;
341 readonly timeout?: string;
342 readonly detail?: string;
343 readonly cause?: unknown;
344};
345
346function decodedProjectErrorMessage(props: object): string | undefined {
347 if (!("message" in props)) return undefined;
348 return typeof props.message === "string" ? props.message : undefined;
349}
350
351export class ProjectSearchEntriesError extends Schema.TaggedError<ProjectSearchEntriesError>()(
352 "ProjectSearchEntriesError",
353 {
354 cwd: Schema.optional(TrimmedNonEmptyString),
355 queryLength: Schema.optional(NonNegativeInt),
356 limit: Schema.optional(PositiveInt),
357 failure: Schema.optional(ProjectEntriesFailure),
358 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
359 timeout: Schema.optional(TrimmedNonEmptyString),
360 detail: Schema.optional(TrimmedNonEmptyString),
361 message: TrimmedNonEmptyString,
362 cause: Schema.optional(Schema.Defect()),
363 },
364) {
365 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
366 constructor(
367 props: ProjectEntriesFailureContext & {
368 readonly cwd: string;
369 readonly queryLength: number;
370 readonly limit: number;
371 },
372 ) {
373 super({
374 ...props,
375 message:
376 decodedProjectErrorMessage(props) ??
377 `Failed to search workspace entries in '${props.cwd}'.`,
378 } as any);
379 }
380}
381
382export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
383 "ProjectSearchContentsError",
384 {
385 cwd: Schema.optional(TrimmedNonEmptyString),
386 queryLength: Schema.optional(NonNegativeInt),
387 limit: Schema.optional(PositiveInt),
388 failure: Schema.optional(ProjectEntriesFailure),
389 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
390 timeout: Schema.optional(TrimmedNonEmptyString),
391 detail: Schema.optional(TrimmedNonEmptyString),
392 message: TrimmedNonEmptyString,
393 cause: Schema.optional(Schema.Defect()),
394 },
395) {
396 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
397 constructor(
398 props: ProjectEntriesFailureContext & {
399 readonly cwd: string;
400 readonly queryLength: number;
401 readonly limit: number;
402 },
403 ) {
404 super({
405 ...props,
406 message:
407 decodedProjectErrorMessage(props) ??
408 `Failed to search workspace contents in '${props.cwd}'.`,
409 } as any);
410 }
411}
412
413export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
414 "ProjectListEntriesError",
415 {
416 cwd: Schema.optional(TrimmedNonEmptyString),
417 failure: Schema.optional(ProjectEntriesFailure),
418 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
419 timeout: Schema.optional(TrimmedNonEmptyString),
420 detail: Schema.optional(TrimmedNonEmptyString),
421 message: TrimmedNonEmptyString,
422 cause: Schema.optional(Schema.Defect()),
423 },
424) {
425 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
426 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
427 super({
428 ...props,
429 message:
430 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
431 } as any);
432 }
433}
434
435export const ProjectReadFileInput = Schema.Struct({
436 cwd: TrimmedNonEmptyString,
437 // Workspace-relative, or an absolute host path for a file outside the
438 // workspace. Only workspace-relative paths can be written back.
439 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
440 // Optional files can be absent without failing the read.
441 allowMissing: Schema.optional(Schema.Boolean),
442});
443export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
444
445export const ProjectReadFileResult = Schema.Struct({
446 relativePath: TrimmedNonEmptyString,
447 contents: Schema.String,
448 byteLength: NonNegativeInt,
449 truncated: Schema.Boolean,
450 // Omitted by older servers; false only for an absent optional file.
451 exists: Schema.optional(Schema.Boolean),
452});
453export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
454
455export const ProjectFileFailure = Schema.Literals([
456 "workspace_path_outside_root",
457 "resolved_path_outside_root",
458 "path_not_file",
459 "binary_file",
460 "operation_failed",
461]);
462export type ProjectFileFailure = typeof ProjectFileFailure.Type;
463
464export const ProjectFileOperation = Schema.Literals([
465 "realpath-workspace-root",
466 "realpath-target",
467 "open",
468 "stat",
469 "read",
470 "close",
471 "make-directory",
472 "write-file",
473]);
474export type ProjectFileOperation = typeof ProjectFileOperation.Type;
475
476type ProjectFileFailureContext = {
477 readonly cwd: string;
478 readonly relativePath: string;
479 readonly failure: ProjectFileFailure;
480 readonly resolvedPath?: string;
481 readonly resolvedWorkspaceRoot?: string;
482 readonly operation?: ProjectFileOperation;
483 readonly operationPath?: string;
484 readonly cause?: unknown;
485};
486
487export class ProjectReadFileError extends Schema.TaggedError<ProjectReadFileError>()(
488 "ProjectReadFileError",
489 {
490 cwd: Schema.optional(TrimmedNonEmptyString),
491 relativePath: Schema.optional(TrimmedNonEmptyString),
492 failure: Schema.optional(ProjectFileFailure),
493 resolvedPath: Schema.optional(TrimmedNonEmptyString),
494 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
495 operation: Schema.optional(ProjectFileOperation),
496 operationPath: Schema.optional(TrimmedNonEmptyString),
497 message: TrimmedNonEmptyString,
498 cause: Schema.optional(Schema.Defect()),
499 },
500) {
501 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
502 constructor(props: ProjectFileFailureContext) {
503 super({
504 ...props,
505 message:
506 decodedProjectErrorMessage(props) ??
507 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
508 } as any);
509 }
510}
511
512export const ProjectWriteFileInput = Schema.Struct({
513 cwd: TrimmedNonEmptyString,
514 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
515 contents: Schema.String,
516});
517export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
518
519export const ProjectWriteFileResult = Schema.Struct({
520 relativePath: TrimmedNonEmptyString,
521});
522export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
523
524export const ProjectEnsureScratchResult = Schema.Struct({
525 projectId: ProjectId,
526});
527export type ProjectEnsureScratchResult = typeof ProjectEnsureScratchResult.Type;
528
529export const ProjectCreateNewInput = Schema.Struct({
530 name: TrimmedNonEmptyString.check(Schema.isMaxLength(200)),
531});
532export type ProjectCreateNewInput = typeof ProjectCreateNewInput.Type;
533
534export const ProjectCreateNewResult = Schema.Struct({
535 projectId: ProjectId,
536 workspaceRoot: TrimmedNonEmptyString,
537 /** The project and its files exist either way. */
538 commitError: Schema.optionalKey(TrimmedNonEmptyString),
539});
540export type ProjectCreateNewResult = typeof ProjectCreateNewResult.Type;
541
542export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
543 "ProjectWriteFileError",
544 {
545 cwd: Schema.optional(TrimmedNonEmptyString),
546 relativePath: Schema.optional(TrimmedNonEmptyString),
547 failure: Schema.optional(ProjectFileFailure),
548 resolvedPath: Schema.optional(TrimmedNonEmptyString),
549 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
550 operation: Schema.optional(ProjectFileOperation),
551 operationPath: Schema.optional(TrimmedNonEmptyString),
552 message: TrimmedNonEmptyString,
553 cause: Schema.optional(Schema.Defect()),
554 },
555) {
556 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
557 constructor(props: ProjectFileFailureContext) {
558 super({
559 ...props,
560 message:
561 decodedProjectErrorMessage(props) ??
562 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
563 } as any);
564 }
565}