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

project.ts

446 lines14.6 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 ProjectMutation = Schema.Union([
117 Schema.Struct({
118 type: Schema.Literal("project.create"),
119 commandId: CommandId,
120 projectId: ProjectId,
121 title: TrimmedNonEmptyString,
122 workspaceRoot: TrimmedNonEmptyString,
123 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
124 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
125 scripts: Schema.optional(Schema.Array(ProjectScript)),
126 }),
127 Schema.Struct({
128 type: Schema.Literal("project.update"),
129 commandId: CommandId,
130 projectId: ProjectId,
131 title: Schema.optional(TrimmedNonEmptyString),
132 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
133 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
134 autoPull: Schema.optional(Schema.Boolean),
135 projectIcon: Schema.optional(Schema.NullOr(ProjectIconOverride)),
136 faviconPath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
137 defaultThreadEnvMode: Schema.optional(Schema.NullOr(ThreadEnvMode)),
138 scripts: Schema.optional(Schema.Array(ProjectScript)),
139 }),
140 Schema.Struct({
141 type: Schema.Literal("project.delete"),
142 commandId: CommandId,
143 projectId: ProjectId,
144 force: Schema.optional(Schema.Boolean),
145 }),
146]);
147export type ProjectMutation = typeof ProjectMutation.Type;
148​
149export class ProjectMutationError extends Schema.TaggedErrorClass<ProjectMutationError>()(
150 "ProjectMutationError",
151 {
152 commandId: CommandId,
153 message: Schema.String,
154 cause: Schema.optional(Schema.Defect()),
155 },
156) {}
157​
158export const ProjectEntryKind = Schema.Literals(["file", "directory"]);
159export type ProjectEntryKind = typeof ProjectEntryKind.Type;
160​
161export const ProjectSearchEntriesInput = Schema.Struct({
162 cwd: TrimmedNonEmptyString,
163 // An empty query is a bounded browse: the index returns frecency-ordered
164 // entries, which the file picker uses for its initial results.
165 query: TrimmedString.check(Schema.isMaxLength(256)),
166 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_ENTRIES_MAX_LIMIT)),
167 kind: Schema.optional(ProjectEntryKind),
168 imageOnly: Schema.optional(Schema.Boolean),
169});
170export type ProjectSearchEntriesInput = typeof ProjectSearchEntriesInput.Type;
171​
172export const ProjectEntry = Schema.Struct({
173 path: TrimmedNonEmptyString,
174 kind: ProjectEntryKind,
175});
176export type ProjectEntry = typeof ProjectEntry.Type;
177​
178export const ProjectSearchEntriesResult = Schema.Struct({
179 entries: Schema.Array(ProjectEntry),
180 truncated: Schema.Boolean,
181});
182export type ProjectSearchEntriesResult = typeof ProjectSearchEntriesResult.Type;
183​
184export const ProjectSearchContentsInput = Schema.Struct({
185 cwd: TrimmedNonEmptyString,
186 // Whitespace is significant in content queries (" foo", regex trailing
187 // spaces), so the query is deliberately not trimmed on the wire.
188 query: Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(256)),
189 limit: PositiveInt.check(Schema.isLessThanOrEqualTo(PROJECT_SEARCH_CONTENTS_MAX_LIMIT)),
190 caseSensitive: Schema.Boolean,
191 wholeWord: Schema.Boolean,
192 useRegex: Schema.Boolean,
193});
194export type ProjectSearchContentsInput = typeof ProjectSearchContentsInput.Type;
195​
196export const ProjectContentMatchRange = Schema.Struct({
197 start: NonNegativeInt,
198 end: NonNegativeInt,
199});
200export type ProjectContentMatchRange = typeof ProjectContentMatchRange.Type;
201​
202export const ProjectContentMatch = Schema.Struct({
203 path: TrimmedNonEmptyString,
204 lineNumber: PositiveInt,
205 lineContent: Schema.String,
206 matchRanges: Schema.Array(ProjectContentMatchRange),
207});
208export type ProjectContentMatch = typeof ProjectContentMatch.Type;
209​
210export const ProjectSearchContentsResult = Schema.Struct({
211 matches: Schema.Array(ProjectContentMatch),
212 truncated: Schema.Boolean,
213 regexFallbackError: Schema.optional(Schema.String),
214});
215export type ProjectSearchContentsResult = typeof ProjectSearchContentsResult.Type;
216​
217export const ProjectListEntriesInput = Schema.Struct({
218 cwd: TrimmedNonEmptyString,
219});
220export type ProjectListEntriesInput = typeof ProjectListEntriesInput.Type;
221​
222export const ProjectListEntriesResult = Schema.Struct({
223 entries: Schema.Array(ProjectEntry),
224 truncated: Schema.Boolean,
225});
226export type ProjectListEntriesResult = typeof ProjectListEntriesResult.Type;
227​
228export const ProjectEntriesFailure = Schema.Literals([
229 "workspace_root_not_found",
230 "workspace_root_create_failed",
231 "workspace_root_stat_failed",
232 "workspace_root_not_directory",
233 "search_index_create_failed",
234 "search_index_scan_timed_out",
235 "search_index_search_failed",
236]);
237export type ProjectEntriesFailure = typeof ProjectEntriesFailure.Type;
238​
239type ProjectEntriesFailureContext = {
240 readonly failure: ProjectEntriesFailure;
241 readonly normalizedCwd?: string;
242 readonly timeout?: string;
243 readonly detail?: string;
244 readonly cause?: unknown;
245};
246​
247function decodedProjectErrorMessage(props: object): string | undefined {
248 if (!("message" in props)) return undefined;
249 return typeof props.message === "string" ? props.message : undefined;
251​
252export class ProjectSearchEntriesError extends Schema.TaggedErrorClass<ProjectSearchEntriesError>()(
253 "ProjectSearchEntriesError",
254 {
255 cwd: Schema.optional(TrimmedNonEmptyString),
256 queryLength: Schema.optional(NonNegativeInt),
257 limit: Schema.optional(PositiveInt),
258 failure: Schema.optional(ProjectEntriesFailure),
259 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
260 timeout: Schema.optional(TrimmedNonEmptyString),
261 detail: Schema.optional(TrimmedNonEmptyString),
262 message: TrimmedNonEmptyString,
263 cause: Schema.optional(Schema.Defect()),
264 },
265) {
266 // The structured fields are optional on the wire so newer peers can decode legacy message-only
267 // failures. New application code must provide them through this constructor.
268 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
269 constructor(
270 props: ProjectEntriesFailureContext & {
271 readonly cwd: string;
272 readonly queryLength: number;
273 readonly limit: number;
274 },
275 ) {
276 super({
277 ...props,
278 message:
279 decodedProjectErrorMessage(props) ??
280 `Failed to search workspace entries in '${props.cwd}'.`,
281 } as any);
282 }
284​
285export class ProjectSearchContentsError extends Schema.TaggedErrorClass<ProjectSearchContentsError>()(
286 "ProjectSearchContentsError",
287 {
288 cwd: Schema.optional(TrimmedNonEmptyString),
289 queryLength: Schema.optional(NonNegativeInt),
290 limit: Schema.optional(PositiveInt),
291 failure: Schema.optional(ProjectEntriesFailure),
292 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
293 timeout: Schema.optional(TrimmedNonEmptyString),
294 detail: Schema.optional(TrimmedNonEmptyString),
295 message: TrimmedNonEmptyString,
296 cause: Schema.optional(Schema.Defect()),
297 },
298) {
299 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
300 constructor(
301 props: ProjectEntriesFailureContext & {
302 readonly cwd: string;
303 readonly queryLength: number;
304 readonly limit: number;
305 },
306 ) {
307 super({
308 ...props,
309 message:
310 decodedProjectErrorMessage(props) ??
311 `Failed to search workspace contents in '${props.cwd}'.`,
312 } as any);
313 }
315​
316export class ProjectListEntriesError extends Schema.TaggedErrorClass<ProjectListEntriesError>()(
317 "ProjectListEntriesError",
318 {
319 cwd: Schema.optional(TrimmedNonEmptyString),
320 failure: Schema.optional(ProjectEntriesFailure),
321 normalizedCwd: Schema.optional(TrimmedNonEmptyString),
322 timeout: Schema.optional(TrimmedNonEmptyString),
323 detail: Schema.optional(TrimmedNonEmptyString),
324 message: TrimmedNonEmptyString,
325 cause: Schema.optional(Schema.Defect()),
326 },
327) {
328 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
329 constructor(props: ProjectEntriesFailureContext & { readonly cwd: string }) {
330 super({
331 ...props,
332 message:
333 decodedProjectErrorMessage(props) ?? `Failed to list workspace entries in '${props.cwd}'.`,
334 } as any);
335 }
337​
338export const ProjectReadFileInput = Schema.Struct({
339 cwd: TrimmedNonEmptyString,
340 // Workspace-relative, or an absolute host path for a file outside the
341 // workspace. Only workspace-relative paths can be written back.
342 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_READ_FILE_PATH_MAX_LENGTH)),
343});
344export type ProjectReadFileInput = typeof ProjectReadFileInput.Type;
345​
346export const ProjectReadFileResult = Schema.Struct({
347 relativePath: TrimmedNonEmptyString,
348 contents: Schema.String,
349 byteLength: NonNegativeInt,
350 truncated: Schema.Boolean,
351});
352export type ProjectReadFileResult = typeof ProjectReadFileResult.Type;
353​
354export const ProjectFileFailure = Schema.Literals([
355 "workspace_path_outside_root",
356 "resolved_path_outside_root",
357 "path_not_file",
358 "binary_file",
359 "operation_failed",
360]);
361export type ProjectFileFailure = typeof ProjectFileFailure.Type;
362​
363export const ProjectFileOperation = Schema.Literals([
364 "realpath-workspace-root",
365 "realpath-target",
366 "open",
367 "stat",
368 "read",
369 "close",
370 "make-directory",
371 "write-file",
372]);
373export type ProjectFileOperation = typeof ProjectFileOperation.Type;
374​
375type ProjectFileFailureContext = {
376 readonly cwd: string;
377 readonly relativePath: string;
378 readonly failure: ProjectFileFailure;
379 readonly resolvedPath?: string;
380 readonly resolvedWorkspaceRoot?: string;
381 readonly operation?: ProjectFileOperation;
382 readonly operationPath?: string;
383 readonly cause?: unknown;
384};
385​
386export class ProjectReadFileError extends Schema.TaggedErrorClass<ProjectReadFileError>()(
387 "ProjectReadFileError",
388 {
389 cwd: Schema.optional(TrimmedNonEmptyString),
390 relativePath: Schema.optional(TrimmedNonEmptyString),
391 failure: Schema.optional(ProjectFileFailure),
392 resolvedPath: Schema.optional(TrimmedNonEmptyString),
393 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
394 operation: Schema.optional(ProjectFileOperation),
395 operationPath: Schema.optional(TrimmedNonEmptyString),
396 message: TrimmedNonEmptyString,
397 cause: Schema.optional(Schema.Defect()),
398 },
399) {
400 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
401 constructor(props: ProjectFileFailureContext) {
402 super({
403 ...props,
404 message:
405 decodedProjectErrorMessage(props) ??
406 `Failed to read workspace file '${props.relativePath}' in '${props.cwd}'.`,
407 } as any);
408 }
410​
411export const ProjectWriteFileInput = Schema.Struct({
412 cwd: TrimmedNonEmptyString,
413 relativePath: TrimmedNonEmptyString.check(Schema.isMaxLength(PROJECT_WRITE_FILE_PATH_MAX_LENGTH)),
414 contents: Schema.String,
415});
416export type ProjectWriteFileInput = typeof ProjectWriteFileInput.Type;
417​
418export const ProjectWriteFileResult = Schema.Struct({
419 relativePath: TrimmedNonEmptyString,
420});
421export type ProjectWriteFileResult = typeof ProjectWriteFileResult.Type;
422​
423export class ProjectWriteFileError extends Schema.TaggedErrorClass<ProjectWriteFileError>()(
424 "ProjectWriteFileError",
425 {
426 cwd: Schema.optional(TrimmedNonEmptyString),
427 relativePath: Schema.optional(TrimmedNonEmptyString),
428 failure: Schema.optional(ProjectFileFailure),
429 resolvedPath: Schema.optional(TrimmedNonEmptyString),
430 resolvedWorkspaceRoot: Schema.optional(TrimmedNonEmptyString),
431 operation: Schema.optional(ProjectFileOperation),
432 operationPath: Schema.optional(TrimmedNonEmptyString),
433 message: TrimmedNonEmptyString,
434 cause: Schema.optional(Schema.Defect()),
435 },
436) {
437 // @effect-diagnostics-next-line overriddenSchemaConstructor:off
438 constructor(props: ProjectFileFailureContext) {
439 super({
440 ...props,
441 message:
442 decodedProjectErrorMessage(props) ??
443 `Failed to write workspace file '${props.relativePath}' in '${props.cwd}'.`,
444 } as any);
445 }