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

project.ts

456 lines14.8 KB

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