Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

project.ts

564 lines18.5 KB

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