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

project.ts

509 lines16.8 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 IsoDateTime,
8 NonNegativeInt,
9 PositiveInt,
10 ProjectId,
11 TrimmedNonEmptyString,
12 TrimmedString,
13} from "./baseSchemas.ts";
14​
15const PROJECT_SEARCH_ENTRIES_MAX_LIMIT = 200;
16const PROJECT_SEARCH_CONTENTS_MAX_LIMIT = 500;
17const PROJECT_WRITE_FILE_PATH_MAX_LENGTH = 512;
18const PROJECT_READ_FILE_PATH_MAX_LENGTH = 512;
19​
20export const ProjectScriptIcon = Schema.Literals([
21 "play",
22 "test",
23 "lint",
24 "configure",
25 "build",
26 "debug",
27]);
28export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
29​
30export const ProjectScript = Schema.Struct({
31 id: TrimmedNonEmptyString,
32 name: TrimmedNonEmptyString,
33 command: TrimmedNonEmptyString,
34 icon: ProjectScriptIcon,
35 runOnWorktreeCreate: Schema.Boolean,
36 /** Start the agent while setup runs unless explicitly disabled. */
37 async: Schema.optional(Schema.Boolean),
38 previewUrl: Schema.optional(TrimmedNonEmptyString),
39 autoOpenPreview: Schema.optional(Schema.Boolean),
40});
41export type ProjectScript = typeof ProjectScript.Type;
42​
43export const ProjectIconColor = Schema.Literals([
44 "gray",
45 "red",
46 "orange",
47 "amber",
48 "yellow",
49 "lime",
50 "green",
51 "emerald",
52 "teal",
53 "cyan",
54 "sky",
55 "blue",
56 "indigo",
57 "violet",
58 "purple",
59 "fuchsia",
60 "pink",
61 "rose",
62]);
63export type ProjectIconColor = typeof ProjectIconColor.Type;
64​
65const ProjectLucideIconName = TrimmedNonEmptyString.check(
66 Schema.isMaxLength(64),
67 Schema.isPattern(/^[a-z0-9]+(?:-[a-z0-9]+)*$/),
68);
69​
70const ProjectEmoji = TrimmedNonEmptyString.check(Schema.isMaxLength(32));
71​
72// Grapheme-count validation belongs to the server command boundary, not snapshot decoding.
73export const ProjectMonogramText = TrimmedNonEmptyString.check(
74 Schema.isMaxLength(32),
75 Schema.isPattern(/^[\p{L}\p{N}][\p{L}\p{N}\p{M}\u200c\u200d]*$/u),
76);
77​
78const ProjectLucideIcon = Schema.Struct({
79 kind: Schema.Literal("lucide"),
80 name: ProjectLucideIconName,
81 color: ProjectIconColor,
82});
83const ProjectEmojiIcon = Schema.Struct({
84 kind: Schema.Literal("emoji"),
85 emoji: ProjectEmoji,
86});
87const ProjectMonogramIcon = Schema.Struct({
88 kind: Schema.Literal("monogram"),
89 text: ProjectMonogramText,
90 color: ProjectIconColor,
91});
92const ProjectIcon = Schema.Union([ProjectLucideIcon, ProjectEmojiIcon, ProjectMonogramIcon]);
93const ProjectLucideIconWire = Schema.Struct({
94 ...ProjectLucideIcon.fields,
95 monogramText: Schema.optional(ProjectMonogramText),
96 monogram: Schema.optional(ProjectMonogramText),
97});
98​
99// Older peers only know lucide/emoji. Keep monograms out of their validated
100// `monogram` field too: old grapheme counters can reject otherwise valid text.
101export const ProjectIconOverride = Schema.Union([
102 ProjectLucideIconWire,
103 ProjectEmojiIcon,
104 ProjectMonogramIcon,
105]).pipe(
106 Schema.decodeTo(
107 ProjectIcon,
108 SchemaTransformation.transform({
109 decode: (icon): typeof ProjectIcon.Type => {
110 if (icon.kind !== "lucide") return icon;
111 const text = icon.monogramText ?? icon.monogram;
112 return text === undefined
113 ? { kind: "lucide", name: icon.name, color: icon.color }
114 : { kind: "monogram", text, color: icon.color };
115 },
116 encode: (icon) =>
117 icon.kind === "monogram"
118 ? {
119 kind: "lucide" as const,
120 name: "folder-code",
121 color: icon.color,
122 monogramText: icon.text,
123 }
124 : icon,
125 }),
126 ),
127);
128export type ProjectIconOverride = typeof ProjectIconOverride.Type;
129​
130export const Project = Schema.Struct({
131 id: ProjectId,
132 title: TrimmedNonEmptyString,
133 workspaceRoot: TrimmedNonEmptyString,
134 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
135 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
136 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
137 defaultModelSelection: Schema.NullOr(ModelSelection),
138 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
139 // Opt-in because background sync performs network I/O and may move the checkout.
140 autoPull: Schema.optional(Schema.Boolean),
141 scripts: Schema.Array(ProjectScript),
142 createdAt: IsoDateTime,
143 updatedAt: IsoDateTime,
144 deletedAt: Schema.NullOr(IsoDateTime),
145});
146export type Project = typeof Project.Type;
147​
148export const ProjectSnapshot = Schema.Struct({
149 projects: Schema.Array(Project),
150 updatedAt: IsoDateTime,
151});
152export type ProjectSnapshot = typeof ProjectSnapshot.Type;
153​
154export const ProjectChange = Schema.Union([
155 Schema.Struct({ type: Schema.Literal("project.upserted"), project: Project }),
156 Schema.Struct({
157 type: Schema.Literal("project.deleted"),
158 projectId: ProjectId,
159 deletedAt: IsoDateTime,
160 }),
161]);
162export type ProjectChange = typeof ProjectChange.Type;
163​
164export const ProjectCreatePayload = Schema.Struct({
165 title: TrimmedNonEmptyString,
166 workspaceRoot: TrimmedNonEmptyString,
167 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
168 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
169 scripts: Schema.optional(Schema.Array(ProjectScript)),
170});
171export type ProjectCreatePayload = typeof ProjectCreatePayload.Type;
172​
173export const ProjectUpdatePayload = Schema.Struct({
174 title: Schema.optional(TrimmedNonEmptyString),
175 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
176 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
177 autoPull: Schema.optional(Schema.Boolean),
178 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
179 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
180 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
181 scripts: Schema.optional(Schema.Array(ProjectScript)),
182});
183export type ProjectUpdatePayload = typeof ProjectUpdatePayload.Type;
184​
185export const ProjectMutation = Schema.Union([
186 Schema.Struct({
187 type: Schema.Literal("project.create"),
188 commandId: CommandId,
189 projectId: ProjectId,
190 ...ProjectCreatePayload.fields,
191 }),
192 Schema.Struct({
193 type: Schema.Literal("project.update"),
194 commandId: CommandId,
195 projectId: ProjectId,
196 ...ProjectUpdatePayload.fields,
197 }),
198 Schema.Struct({
199 type: Schema.Literal("project.delete"),
200 commandId: CommandId,
201 projectId: ProjectId,
202 force: Schema.optional(Schema.Boolean),
203 }),
204]);
205export type ProjectMutation = typeof ProjectMutation.Type;
206​
207export class ProjectMutationError extends Schema.TaggedError<ProjectMutationError>()(
208 "ProjectMutationError",
209 {
210 commandId: CommandId,
211 message: Schema.String,
212 cause: Schema.optional(Schema.Defect()),
213 },
214) {}
215​
216export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
217export type ProjectEntryKind = typeof ProjectEntryKind.Type;
218​
219export const ProjectSearchEntriesInput = Schema.Struct({
220 cwd: TrimmedNonEmptyString,
221 // An empty query is a bounded browse: the index returns frecency-ordered
222 // entries, which the file picker uses for its initial results.
223 query: TrimmedString.check(Schema.isMaxLength(256)),
224 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
225 kind: Schema.optional(ProjectEntryKind),
226 imageOnly: Schema.optional(Schema.Boolean),
227});
228export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
229​
230export const ProjectEntry = Schema.Struct({
231 path: TrimmedNonEmptyString,
232 kind: ProjectEntryKind,
233 ignored: Schema.optional(Schema.Boolean),
234});
235export type ProjectEntry = typeof ProjectEntry.Type;
236​
237export const ProjectSearchEntriesResult = Schema.Struct({
238 entries: Schema.Array(ProjectEntry),
239 truncated: Schema.Boolean,
240});
241export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
242​
243export const ProjectSearchContentsInput = Schema.Struct({
244 cwd: TrimmedNonEmptyString,
245 // Whitespace is significant in content queries (" foo", regex trailing
246 // spaces), so the query is deliberately not trimmed on the wire.
247 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
248 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
249 caseSensitive: Schema.Boolean,
250 wholeWord: Schema.Boolean,
251 useRegex: Schema.Boolean,
252});
253export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
254​
255export const ProjectContentMatchRange = Schema.Struct({
256 start: NonNegativeInt,
257 end: NonNegativeInt,
258});
259export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
260​
261export const ProjectContentMatch = Schema.Struct({
262 path: TrimmedNonEmptyString,
263 lineNumber: PositiveInt,
264 lineContent: Schema.String,
265 matchRanges: Schema.Array(ProjectContentMatchRange),
266});
267export type ProjectContentMatch = typeof ProjectContentMatch.Type;
268​
269export const ProjectSearchContentsResult = Schema.Struct({
270 matches: Schema.Array(ProjectContentMatch),
271 truncated: Schema.Boolean,
272 regexFallbackError: Schema.optional(Schema.String),
273});
274export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
275​
276export const ProjectListEntriesInput = Schema.Struct({
277 cwd: TrimmedNonEmptyString,
278 // Present for immediate filesystem children, including ignored entries; empty means root.
279 // Omitted preserves the indexed recursive listing used by older clients.
280 directoryPath: Schema.optional(TrimmedString),
281});
282export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
283​
284export const ProjectListEntriesResult = Schema.Struct({
285 entries: Schema.Array(ProjectEntry),
286 truncated: Schema.Boolean,
287});
288export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
289​
290export const ProjectEntriesFailure = Schema.Literals([
291 "workspace_root_not_found",
292 "workspace_root_create_failed",
293 "workspace_root_stat_failed",
294 "workspace_root_not_directory",
295 "search_index_create_failed",
296 "search_index_scan_timed_out",
297 "search_index_search_failed",
298 "directory_list_failed",
299]);
300export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
301​
302type ProjectEntriesFailureContext = {
303 readonly failure: ProjectEntriesFailure;
304 readonly normalizedCwd?: string;
305 readonly timeout?: string;
306 readonly detail?: string;
307 readonly cause?: unknown;
308};
309​
310function decodedProjectErrorMessage(props: object): string | undefined {
311 if (!("message" in props)) return undefined;
312 return typeof props.message === "string" ? props.message : undefined;
314​
315export class ProjectSearchEntriesError extends Schema.TaggedError<ProjectSearchEntriesError>()(
316 "ProjectSearchEntriesError",
317 {
318 cwd: Schema.optional(TrimmedNonEmptyString),
319 queryLength: Schema.optional(NonNegativeInt),
320 limit: Schema.optional(PositiveInt),
321 failure: Schema.optional(ProjectEntriesFailure),
322 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
323 timeout: Schema.optional(TrimmedNonEmptyString),
324 detail: Schema.optional(TrimmedNonEmptyString),
325 message: TrimmedNonEmptyString,
326 cause: Schema.optional(Schema.Defect()),
327 },
328) {
329 // The structured fields are optional on the wire so newer peers can decode legacy message-only
330 // failures. New application code must provide them through this constructor.
331 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
332 constructor(
333 props: ProjectEntriesFailureContext & {
334 readonly cwd: string;
335 readonly queryLength: number;
336 readonly limit: number;
337 },
338 ) {
339 super({
340 ...props,
341 message:
342 decodedProjectErrorMessage(props) ??
343 `Failed to search workspace entries in '${props.cwd}'.`,
344 } as any);
345 }
347​
348export class ProjectSearchContentsError extends Schema.TaggedError<ProjectSearchContentsError>()(
349 "ProjectSearchContentsError",
350 {
351 cwd: Schema.optional(TrimmedNonEmptyString),
352 queryLength: Schema.optional(NonNegativeInt),
353 limit: Schema.optional(PositiveInt),
354 failure: Schema.optional(ProjectEntriesFailure),
355 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
356 timeout: Schema.optional(TrimmedNonEmptyString),
357 detail: Schema.optional(TrimmedNonEmptyString),
358 message: TrimmedNonEmptyString,
359 cause: Schema.optional(Schema.Defect()),
360 },
361) {
362 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
363 constructor(
364 props: ProjectEntriesFailureContext & {
365 readonly cwd: string;
366 readonly queryLength: number;
367 readonly limit: number;
368 },
369 ) {
370 super({
371 ...props,
372 message:
373 decodedProjectErrorMessage(props) ??
374 `Failed to search workspace contents in '${props.cwd}'.`,
375 } as any);
376 }
378​
379export class ProjectListEntriesError extends Schema.TaggedError<ProjectListEntriesError>()(
380 "ProjectListEntriesError",
381 {
382 cwd: Schema.optional(TrimmedNonEmptyString),
383 failure: Schema.optional(ProjectEntriesFailure),
384 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
385 timeout: Schema.optional(TrimmedNonEmptyString),
386 detail: Schema.optional(TrimmedNonEmptyString),
387 message: TrimmedNonEmptyString,
388 cause: Schema.optional(Schema.Defect()),
389 },
390) {
391 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
392 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
393 super({
394 ...props,
395 message:
396 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
397 } as any);
398 }
400​
401export const ProjectReadFileInput = Schema.Struct({
402 cwd: TrimmedNonEmptyString,
403 // Workspace-relative, or an absolute host path for a file outside the
404 // workspace. Only workspace-relative paths can be written back.
405 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
406});
407export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
408​
409export const ProjectReadFileResult = Schema.Struct({
410 relativePath: TrimmedNonEmptyString,
411 contents: Schema.String,
412 byteLength: NonNegativeInt,
413 truncated: Schema.Boolean,
414});
415export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
416​
417export const ProjectFileFailure = Schema.Literals([
418 "workspace_path_outside_root",
419 "resolved_path_outside_root",
420 "path_not_file",
421 "binary_file",
422 "operation_failed",
423]);
424export type ProjectFileFailure = typeof ProjectFileFailure.Type;
425​
426export const ProjectFileOperation = Schema.Literals([
427 "realpath-workspace-root",
428 "realpath-target",
429 "open",
430 "stat",
431 "read",
432 "close",
433 "make-directory",
434 "write-file",
435]);
436export type ProjectFileOperation = typeof ProjectFileOperation.Type;
437​
438type ProjectFileFailureContext = {
439 readonly cwd: string;
440 readonly relativePath: string;
441 readonly failure: ProjectFileFailure;
442 readonly resolvedPath?: string;
443 readonly resolvedWorkspaceRoot?: string;
444 readonly operation?: ProjectFileOperation;
445 readonly operationPath?: string;
446 readonly cause?: unknown;
447};
448​
449export class ProjectReadFileError extends Schema.TaggedError<ProjectReadFileError>()(
450 "ProjectReadFileError",
451 {
452 cwd: Schema.optional(TrimmedNonEmptyString),
453 relativePath: Schema.optional(TrimmedNonEmptyString),
454 failure: Schema.optional(ProjectFileFailure),
455 resolvedPath: Schema.optional(TrimmedNonEmptyString),
456 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
457 operation: Schema.optional(ProjectFileOperation),
458 operationPath: Schema.optional(TrimmedNonEmptyString),
459 message: TrimmedNonEmptyString,
460 cause: Schema.optional(Schema.Defect()),
461 },
462) {
463 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
464 constructor(props: ProjectFileFailureContext) {
465 super({
466 ...props,
467 message:
468 decodedProjectErrorMessage(props) ??
469 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
470 } as any);
471 }
473​
474export const ProjectWriteFileInput = Schema.Struct({
475 cwd: TrimmedNonEmptyString,
476 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
477 contents: Schema.String,
478});
479export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
480​
481export const ProjectWriteFileResult = Schema.Struct({
482 relativePath: TrimmedNonEmptyString,
483});
484export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
485​
486export class ProjectWriteFileError extends Schema.TaggedError<ProjectWriteFileError>()(
487 "ProjectWriteFileError",
488 {
489 cwd: Schema.optional(TrimmedNonEmptyString),
490 relativePath: Schema.optional(TrimmedNonEmptyString),
491 failure: Schema.optional(ProjectFileFailure),
492 resolvedPath: Schema.optional(TrimmedNonEmptyString),
493 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
494 operation: Schema.optional(ProjectFileOperation),
495 operationPath: Schema.optional(TrimmedNonEmptyString),
496 message: TrimmedNonEmptyString,
497 cause: Schema.optional(Schema.Defect()),
498 },
499) {
500 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
501 constructor(props: ProjectFileFailureContext) {
502 super({
503 ...props,
504 message:
505 decodedProjectErrorMessage(props) ??
506 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
507 } as any);
508 }