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
307export const ProjectEntriesFailure = Schema.Literals([
308 "workspace_root_not_found",
309 "workspace_root_create_failed",
310 "workspace_root_stat_failed",
311 "workspace_root_not_directory",
312 "search_index_create_failed",
313 "search_index_scan_timed_out",
314 "search_index_search_failed",
315 "directory_list_failed",
316]);
317export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
318
319type ProjectEntriesFailureContext = {
320 readonly failure: ProjectEntriesFailure;
321 readonly normalizedCwd?: string;
322 readonly timeout?: string;
323 readonly detail?: string;
324 readonly cause?: unknown;
325};
326
327function decodedProjectErrorMessage(props: object): string | undefined {
328 if (!("message" in props)) return undefined;
329 return typeof props.message === "string" ? props.message : undefined;
330}
331
332export class ProjectSearchEntriesError extends Schema.TaggedError<ProjectSearchEntriesError>()(
333 "ProjectSearchEntriesError",
334 {
335 cwd: Schema.optional(TrimmedNonEmptyString),
336 queryLength: Schema.optional(NonNegativeInt),
337 limit: Schema.optional(PositiveInt),
338 failure: Schema.optional(ProjectEntriesFailure),
339 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
340 timeout: Schema.optional(TrimmedNonEmptyString),
341 detail: Schema.optional(TrimmedNonEmptyString),
342 message: TrimmedNonEmptyString,
343 cause: Schema.optional(Schema.Defect()),
344 },
345) {
346 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
347 constructor(
348 props: ProjectEntriesFailureContext & {
349 readonly cwd: string;
350 readonly queryLength: number;
351 readonly limit: number;
352 },
353 ) {
354 super({
355 ...props,
356 message:
357 decodedProjectErrorMessage(props) ??
358 `Failed to search workspace entries in '${props.cwd}'.`,
359 } as any);
360 }
361}
362
363export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
364 "ProjectSearchContentsError",
365 {
366 cwd: Schema.optional(TrimmedNonEmptyString),
367 queryLength: Schema.optional(NonNegativeInt),
368 limit: Schema.optional(PositiveInt),
369 failure: Schema.optional(ProjectEntriesFailure),
370 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
371 timeout: Schema.optional(TrimmedNonEmptyString),
372 detail: Schema.optional(TrimmedNonEmptyString),
373 message: TrimmedNonEmptyString,
374 cause: Schema.optional(Schema.Defect()),
375 },
376) {
377 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
378 constructor(
379 props: ProjectEntriesFailureContext & {
380 readonly cwd: string;
381 readonly queryLength: number;
382 readonly limit: number;
383 },
384 ) {
385 super({
386 ...props,
387 message:
388 decodedProjectErrorMessage(props) ??
389 `Failed to search workspace contents in '${props.cwd}'.`,
390 } as any);
391 }
392}
393
394export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
395 "ProjectListEntriesError",
396 {
397 cwd: Schema.optional(TrimmedNonEmptyString),
398 failure: Schema.optional(ProjectEntriesFailure),
399 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
400 timeout: Schema.optional(TrimmedNonEmptyString),
401 detail: Schema.optional(TrimmedNonEmptyString),
402 message: TrimmedNonEmptyString,
403 cause: Schema.optional(Schema.Defect()),
404 },
405) {
406 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
407 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
408 super({
409 ...props,
410 message:
411 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
412 } as any);
413 }
414}
415
416export const ProjectReadFileInput = Schema.Struct({
417 cwd: TrimmedNonEmptyString,
418 // Workspace-relative, or an absolute host path for a file outside the
419 // workspace. Only workspace-relative paths can be written back.
420 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
421});
422export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
423
424export const ProjectReadFileResult = Schema.Struct({
425 relativePath: TrimmedNonEmptyString,
426 contents: Schema.String,
427 byteLength: NonNegativeInt,
428 truncated: Schema.Boolean,
429});
430export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
431
432export const ProjectFileFailure = Schema.Literals([
433 "workspace_path_outside_root",
434 "resolved_path_outside_root",
435 "path_not_file",
436 "binary_file",
437 "operation_failed",
438]);
439export type ProjectFileFailure = typeof ProjectFileFailure.Type;
440
441export const ProjectFileOperation = Schema.Literals([
442 "realpath-workspace-root",
443 "realpath-target",
444 "open",
445 "stat",
446 "read",
447 "close",
448 "make-directory",
449 "write-file",
450]);
451export type ProjectFileOperation = typeof ProjectFileOperation.Type;
452
453type ProjectFileFailureContext = {
454 readonly cwd: string;
455 readonly relativePath: string;
456 readonly failure: ProjectFileFailure;
457 readonly resolvedPath?: string;
458 readonly resolvedWorkspaceRoot?: string;
459 readonly operation?: ProjectFileOperation;
460 readonly operationPath?: string;
461 readonly cause?: unknown;
462};
463
464export class ProjectReadFileError extends Schema.TaggedError<ProjectReadFileError>()(
465 "ProjectReadFileError",
466 {
467 cwd: Schema.optional(TrimmedNonEmptyString),
468 relativePath: Schema.optional(TrimmedNonEmptyString),
469 failure: Schema.optional(ProjectFileFailure),
470 resolvedPath: Schema.optional(TrimmedNonEmptyString),
471 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
472 operation: Schema.optional(ProjectFileOperation),
473 operationPath: Schema.optional(TrimmedNonEmptyString),
474 message: TrimmedNonEmptyString,
475 cause: Schema.optional(Schema.Defect()),
476 },
477) {
478 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
479 constructor(props: ProjectFileFailureContext) {
480 super({
481 ...props,
482 message:
483 decodedProjectErrorMessage(props) ??
484 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
485 } as any);
486 }
487}
488
489export const ProjectWriteFileInput = Schema.Struct({
490 cwd: TrimmedNonEmptyString,
491 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
492 contents: Schema.String,
493});
494export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
495
496export const ProjectWriteFileResult = Schema.Struct({
497 relativePath: TrimmedNonEmptyString,
498});
499export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
500
501export const ProjectEnsureScratchResult = Schema.Struct({
502 projectId: ProjectId,
503});
504export type ProjectEnsureScratchResult = typeof ProjectEnsureScratchResult.Type;
505
506export const ProjectCreateNewInput = Schema.Struct({
507 name: TrimmedNonEmptyString.check(Schema.isMaxLength(200)),
508});
509export type ProjectCreateNewInput = typeof ProjectCreateNewInput.Type;
510
511export const ProjectCreateNewResult = Schema.Struct({
512 projectId: ProjectId,
513 workspaceRoot: TrimmedNonEmptyString,
514 /** The project and its files exist either way. */
515 commitError: Schema.optionalKey(TrimmedNonEmptyString),
516});
517export type ProjectCreateNewResult = typeof ProjectCreateNewResult.Type;
518
519export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
520 "ProjectWriteFileError",
521 {
522 cwd: Schema.optional(TrimmedNonEmptyString),
523 relativePath: Schema.optional(TrimmedNonEmptyString),
524 failure: Schema.optional(ProjectFileFailure),
525 resolvedPath: Schema.optional(TrimmedNonEmptyString),
526 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
527 operation: Schema.optional(ProjectFileOperation),
528 operationPath: Schema.optional(TrimmedNonEmptyString),
529 message: TrimmedNonEmptyString,
530 cause: Schema.optional(Schema.Defect()),
531 },
532) {
533 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
534 constructor(props: ProjectFileFailureContext) {
535 super({
536 ...props,
537 message:
538 decodedProjectErrorMessage(props) ??
539 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
540 } as any);
541 }
542}