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

project.ts

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