| ... | @@ -6,6 +6,7 @@ import * as Scope from "effect/Scope"; | ... | @@ -6,6 +6,7 @@ import * as Scope from "effect/Scope"; |
| 6 | import * as Semaphore from "effect/Semaphore"; | 6 | import * as Semaphore from "effect/Semaphore"; |
| 7 | | 7 | |
| 8 | import { | 8 | import { |
| | 9 | NonNegativeInt, |
| 9 | TextGenerationError, | 10 | TextGenerationError, |
| 10 | type ChatAttachment, | 11 | type ChatAttachment, |
| 11 | type ModelSelection, | 12 | type ModelSelection, |
| ... | @@ -33,11 +34,106 @@ import * as OpenCodeRuntime from "../provider/opencodeRuntime.ts"; | ... | @@ -33,11 +34,106 @@ import * as OpenCodeRuntime from "../provider/opencodeRuntime.ts"; |
| 33 | | 34 | |
| 34 | const OPENCODE_TEXT_GENERATION_IDLE_TTL = "30 seconds"; | 35 | const OPENCODE_TEXT_GENERATION_IDLE_TTL = "30 seconds"; |
| 35 | | 36 | |
| 36 | function getOpenCodePromptErrorMessage(error: unknown): string | null { | 37 | const OpenCodeTextGenerationOperation = Schema.Literals([ |
| | 38 | "generateCommitMessage", |
| | 39 | "generatePrContent", |
| | 40 | "generateBranchName", |
| | 41 | "generateThreadTitle", |
| | 42 | ]); |
| | 43 | |
| | 44 | type OpenCodeTextGenerationOperation = typeof OpenCodeTextGenerationOperation.Type; |
| | 45 | |
| | 46 | const openCodeTextGenerationErrorContext = { |
| | 47 | operation: OpenCodeTextGenerationOperation, |
| | 48 | cwd: Schema.String, |
| | 49 | }; |
| | 50 | |
| | 51 | export class OpenCodeTextGenerationSessionRequestError extends Schema.TaggedErrorClass<OpenCodeTextGenerationSessionRequestError>()( |
| | 52 | "OpenCodeTextGenerationSessionRequestError", |
| | 53 | { |
| | 54 | ...openCodeTextGenerationErrorContext, |
| | 55 | cause: Schema.Defect(), |
| | 56 | }, |
| | 57 | ) { |
| | 58 | override get message(): string { |
| | 59 | return `OpenCode session creation request failed for ${this.operation} in ${this.cwd}.`; |
| | 60 | } |
| | 61 | } |
| | 62 | |
| | 63 | export class OpenCodeTextGenerationSessionPayloadError extends Schema.TaggedErrorClass<OpenCodeTextGenerationSessionPayloadError>()( |
| | 64 | "OpenCodeTextGenerationSessionPayloadError", |
| | 65 | openCodeTextGenerationErrorContext, |
| | 66 | ) { |
| | 67 | override get message(): string { |
| | 68 | return `OpenCode session.create returned no session payload for ${this.operation} in ${this.cwd}.`; |
| | 69 | } |
| | 70 | } |
| | 71 | |
| | 72 | const openCodePromptErrorContext = { |
| | 73 | ...openCodeTextGenerationErrorContext, |
| | 74 | sessionId: Schema.String, |
| | 75 | providerId: Schema.String, |
| | 76 | modelId: Schema.String, |
| | 77 | }; |
| | 78 | |
| | 79 | export class OpenCodeTextGenerationPromptRequestError extends Schema.TaggedErrorClass<OpenCodeTextGenerationPromptRequestError>()( |
| | 80 | "OpenCodeTextGenerationPromptRequestError", |
| | 81 | { |
| | 82 | ...openCodePromptErrorContext, |
| | 83 | cause: Schema.Defect(), |
| | 84 | }, |
| | 85 | ) { |
| | 86 | override get message(): string { |
| | 87 | return `OpenCode prompt request failed for ${this.operation} in ${this.cwd} using ${this.providerId}/${this.modelId} (session ${this.sessionId}).`; |
| | 88 | } |
| | 89 | } |
| | 90 | |
| | 91 | export class OpenCodeTextGenerationPromptResponseError extends Schema.TaggedErrorClass<OpenCodeTextGenerationPromptResponseError>()( |
| | 92 | "OpenCodeTextGenerationPromptResponseError", |
| | 93 | { |
| | 94 | ...openCodePromptErrorContext, |
| | 95 | providerErrorName: Schema.optional(Schema.String), |
| | 96 | providerMessage: Schema.String, |
| | 97 | }, |
| | 98 | ) { |
| | 99 | override get message(): string { |
| | 100 | const providerError = this.providerErrorName ? ` ${this.providerErrorName}` : ""; |
| | 101 | return `OpenCode prompt${providerError} failed for ${this.operation} in ${this.cwd} using ${this.providerId}/${this.modelId} (session ${this.sessionId}): ${this.providerMessage}`; |
| | 102 | } |
| | 103 | } |
| | 104 | |
| | 105 | export class OpenCodeTextGenerationEmptyOutputError extends Schema.TaggedErrorClass<OpenCodeTextGenerationEmptyOutputError>()( |
| | 106 | "OpenCodeTextGenerationEmptyOutputError", |
| | 107 | { |
| | 108 | ...openCodePromptErrorContext, |
| | 109 | responsePartCount: NonNegativeInt, |
| | 110 | textPartCount: NonNegativeInt, |
| | 111 | }, |
| | 112 | ) { |
| | 113 | override get message(): string { |
| | 114 | return `OpenCode returned empty output for ${this.operation} in ${this.cwd} using ${this.providerId}/${this.modelId} (session ${this.sessionId}, ${this.responsePartCount} response parts, ${this.textPartCount} text parts).`; |
| | 115 | } |
| | 116 | } |
| | 117 | |
| | 118 | interface OpenCodePromptFailure { |
| | 119 | readonly name?: string; |
| | 120 | readonly message: string; |
| | 121 | } |
| | 122 | |
| | 123 | interface OpenCodeTextPart { |
| | 124 | readonly type: "text"; |
| | 125 | readonly text: string; |
| | 126 | } |
| | 127 | |
| | 128 | function getOpenCodePromptFailure(error: unknown): OpenCodePromptFailure | null { |
| 37 | if (!error || typeof error !== "object") { | 129 | if (!error || typeof error !== "object") { |
| 38 | return null; | 130 | return null; |
| 39 | } | 131 | } |
| 40 | | 132 | |
| | 133 | const name = |
| | 134 | "name" in error && typeof error.name === "string" && error.name.trim().length > 0 |
| | 135 | ? error.name.trim() |
| | 136 | : undefined; |
| 41 | const message = | 137 | const message = |
| 42 | "data" in error && | 138 | "data" in error && |
| 43 | error.data && | 139 | error.data && |
| ... | @@ -47,31 +143,34 @@ function getOpenCodePromptErrorMessage(error: unknown): string | null { | ... | @@ -47,31 +143,34 @@ function getOpenCodePromptErrorMessage(error: unknown): string | null { |
| 47 | ? error.data.message.trim() | 143 | ? error.data.message.trim() |
| 48 | : ""; | 144 | : ""; |
| 49 | if (message.length > 0) { | 145 | if (message.length > 0) { |
| 50 | return message; | 146 | return { |
| | 147 | ...(name ? { name } : {}), |
| | 148 | message, |
| | 149 | }; |
| 51 | } | 150 | } |
| 52 | | 151 | |
| 53 | if ("name" in error && typeof error.name === "string") { | 152 | if (name) { |
| 54 | const name = error.name.trim(); | 153 | return { name, message: name }; |
| 55 | return name.length > 0 ? name : null; | | |
| 56 | } | 154 | } |
| 57 | | 155 | |
| 58 | return null; | 156 | return null; |
| 59 | } | 157 | } |
| 60 | | 158 | |
| | 159 | function isOpenCodeTextPart(part: unknown): part is OpenCodeTextPart { |
| | 160 | return ( |
| | 161 | part !== null && |
| | 162 | typeof part === "object" && |
| | 163 | "type" in part && |
| | 164 | part.type === "text" && |
| | 165 | "text" in part && |
| | 166 | typeof part.text === "string" |
| | 167 | ); |
| | 168 | } |
| | 169 | |
| 61 | function getOpenCodeTextResponse(parts: ReadonlyArray<unknown> | undefined): string { | 170 | function getOpenCodeTextResponse(parts: ReadonlyArray<unknown> | undefined): string { |
| 62 | return (parts ?? []) | 171 | return (parts ?? []) |
| 63 | .flatMap((part) => { | 172 | .filter(isOpenCodeTextPart) |
| 64 | if (!part || typeof part !== "object") { | 173 | .map((part) => part.text) |
| 65 | return []; | | |
| 66 | } | | |
| 67 | if (!("type" in part) || part.type !== "text") { | | |
| 68 | return []; | | |
| 69 | } | | |
| 70 | if (!("text" in part) || typeof part.text !== "string") { | | |
| 71 | return []; | | |
| 72 | } | | |
| 73 | return [part.text]; | | |
| 74 | }) | | |
| 75 | .join("") | 174 | .join("") |
| 76 | .trim(); | 175 | .trim(); |
| 77 | } | 176 | } |
| ... | @@ -260,11 +359,7 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration" | ... | @@ -260,11 +359,7 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration" |
| 260 | ); | 359 | ); |
| 261 | | 360 | |
| 262 | const runOpenCodeJson = Effect.fn("runOpenCodeJson")(function* <S extends Schema.Top>(input: { | 361 | const runOpenCodeJson = Effect.fn("runOpenCodeJson")(function* <S extends Schema.Top>(input: { |
| 263 | readonly operation: | 362 | readonly operation: OpenCodeTextGenerationOperation; |
| 264 | | "generateCommitMessage" | | |
| 265 | | "generatePrContent" | | |
| 266 | | "generateBranchName" | | |
| 267 | | "generateThreadTitle"; | | |
| 268 | readonly cwd: string; | 363 | readonly cwd: string; |
| 269 | readonly prompt: string; | 364 | readonly prompt: string; |
| 270 | readonly outputSchemaJson: S; | 365 | readonly outputSchemaJson: S; |
| ... | @@ -285,54 +380,121 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration" | ... | @@ -285,54 +380,121 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration" |
| 285 | resolveAttachmentPath({ attachmentsDir: serverConfig.attachmentsDir, attachment }), | 380 | resolveAttachmentPath({ attachmentsDir: serverConfig.attachmentsDir, attachment }), |
| 286 | }); | 381 | }); |
| 287 | | 382 | |
| 288 | const runAgainstServer = (server: Pick<OpenCodeRuntime.OpenCodeServerConnection, "url">) => | 383 | const runAgainstServer = Effect.fn("runOpenCodeJson.runAgainstServer")( |
| 289 | Effect.tryPromise({ | 384 | function* (server: Pick<OpenCodeRuntime.OpenCodeServerConnection, "url">) { |
| 290 | try: async () => { | 385 | const client = openCodeRuntime.createOpenCodeSdkClient({ |
| 291 | const client = openCodeRuntime.createOpenCodeSdkClient({ | 386 | baseUrl: server.url, |
| 292 | baseUrl: server.url, | 387 | directory: input.cwd, |
| 293 | directory: input.cwd, | 388 | ...(openCodeSettings.serverUrl.length > 0 && openCodeSettings.serverPassword |
| 294 | ...(openCodeSettings.serverUrl.length > 0 && openCodeSettings.serverPassword | 389 | ? { serverPassword: openCodeSettings.serverPassword } |
| 295 | ? { serverPassword: openCodeSettings.serverPassword } | 390 | : {}), |
| 296 | : {}), | 391 | }); |
| | 392 | const session = yield* Effect.tryPromise({ |
| | 393 | try: () => |
| | 394 | client.session.create({ |
| | 395 | title: `T3 Code ${input.operation}`, |
| | 396 | permission: [{ permission: "*", pattern: "*", action: "deny" }], |
| | 397 | }), |
| | 398 | catch: (cause) => |
| | 399 | new OpenCodeTextGenerationSessionRequestError({ |
| | 400 | operation: input.operation, |
| | 401 | cwd: input.cwd, |
| | 402 | cause, |
| | 403 | }), |
| | 404 | }); |
| | 405 | if (!session.data) { |
| | 406 | return yield* new OpenCodeTextGenerationSessionPayloadError({ |
| | 407 | operation: input.operation, |
| | 408 | cwd: input.cwd, |
| 297 | }); | 409 | }); |
| 298 | const session = await client.session.create({ | 410 | } |
| 299 | title: `T3 Code ${input.operation}`, | 411 | const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent"); |
| 300 | permission: [{ permission: "*", pattern: "*", action: "deny" }], | 412 | const selectedVariant = getModelSelectionStringOptionValue(input.modelSelection, "variant"); |
| | 413 | const promptContext = { |
| | 414 | operation: input.operation, |
| | 415 | cwd: input.cwd, |
| | 416 | sessionId: session.data.id, |
| | 417 | providerId: parsedModel.providerID, |
| | 418 | modelId: parsedModel.modelID, |
| | 419 | }; |
| | 420 | |
| | 421 | const result = yield* Effect.tryPromise({ |
| | 422 | try: () => |
| | 423 | client.session.prompt({ |
| | 424 | sessionID: session.data.id, |
| | 425 | model: parsedModel, |
| | 426 | ...(selectedAgent ? { agent: selectedAgent } : {}), |
| | 427 | ...(selectedVariant ? { variant: selectedVariant } : {}), |
| | 428 | parts: [{ type: "text", text: input.prompt }, ...fileParts], |
| | 429 | }), |
| | 430 | catch: (cause) => |
| | 431 | new OpenCodeTextGenerationPromptRequestError({ |
| | 432 | ...promptContext, |
| | 433 | cause, |
| | 434 | }), |
| | 435 | }); |
| | 436 | const promptFailure = getOpenCodePromptFailure(result.data?.info?.error); |
| | 437 | if (promptFailure) { |
| | 438 | return yield* new OpenCodeTextGenerationPromptResponseError({ |
| | 439 | ...promptContext, |
| | 440 | ...(promptFailure.name ? { providerErrorName: promptFailure.name } : {}), |
| | 441 | providerMessage: promptFailure.message, |
| 301 | }); | 442 | }); |
| 302 | if (!session.data) { | 443 | } |
| 303 | throw new Error("OpenCode session.create returned no session payload."); | 444 | const responseParts = result.data?.parts ?? []; |
| 304 | } | 445 | const rawText = getOpenCodeTextResponse(responseParts); |
| 305 | const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent"); | 446 | if (rawText.length === 0) { |
| 306 | const selectedVariant = getModelSelectionStringOptionValue( | 447 | return yield* new OpenCodeTextGenerationEmptyOutputError({ |
| 307 | input.modelSelection, | 448 | ...promptContext, |
| 308 | "variant", | 449 | responsePartCount: responseParts.length, |
| 309 | ); | 450 | textPartCount: responseParts.filter(isOpenCodeTextPart).length, |
| 310 | | | |
| 311 | const result = await client.session.prompt({ | | |
| 312 | sessionID: session.data.id, | | |
| 313 | model: parsedModel, | | |
| 314 | ...(selectedAgent ? { agent: selectedAgent } : {}), | | |
| 315 | ...(selectedVariant ? { variant: selectedVariant } : {}), | | |
| 316 | parts: [{ type: "text", text: input.prompt }, ...fileParts], | | |
| 317 | }); | 451 | }); |
| 318 | const info = result.data?.info; | 452 | } |
| 319 | const errorMessage = getOpenCodePromptErrorMessage(info?.error); | 453 | return rawText; |
| 320 | if (errorMessage) { | 454 | }, |
| 321 | throw new Error(errorMessage); | 455 | Effect.catchTags({ |
| 322 | } | 456 | OpenCodeTextGenerationSessionRequestError: (cause) => |
| 323 | const rawText = getOpenCodeTextResponse(result.data?.parts); | 457 | Effect.fail( |
| 324 | if (rawText.length === 0) { | 458 | new TextGenerationError({ |
| 325 | throw new Error("OpenCode returned empty output."); | 459 | operation: cause.operation, |
| 326 | } | 460 | detail: "OpenCode session.create request failed.", |
| 327 | return rawText; | 461 | cause, |
| 328 | }, | 462 | }), |
| 329 | catch: (cause) => | 463 | ), |
| 330 | new TextGenerationError({ | 464 | OpenCodeTextGenerationSessionPayloadError: (cause) => |
| 331 | operation: input.operation, | 465 | Effect.fail( |
| 332 | detail: OpenCodeRuntime.openCodeRuntimeErrorDetail(cause), | 466 | new TextGenerationError({ |
| 333 | cause, | 467 | operation: cause.operation, |
| 334 | }), | 468 | detail: "OpenCode session.create returned no session payload.", |
| 335 | }); | 469 | cause, |
| | 470 | }), |
| | 471 | ), |
| | 472 | OpenCodeTextGenerationPromptRequestError: (cause) => |
| | 473 | Effect.fail( |
| | 474 | new TextGenerationError({ |
| | 475 | operation: cause.operation, |
| | 476 | detail: "OpenCode session.prompt request failed.", |
| | 477 | cause, |
| | 478 | }), |
| | 479 | ), |
| | 480 | OpenCodeTextGenerationPromptResponseError: (cause) => |
| | 481 | Effect.fail( |
| | 482 | new TextGenerationError({ |
| | 483 | operation: cause.operation, |
| | 484 | detail: cause.providerMessage, |
| | 485 | cause, |
| | 486 | }), |
| | 487 | ), |
| | 488 | OpenCodeTextGenerationEmptyOutputError: (cause) => |
| | 489 | Effect.fail( |
| | 490 | new TextGenerationError({ |
| | 491 | operation: cause.operation, |
| | 492 | detail: "OpenCode returned empty output.", |
| | 493 | cause, |
| | 494 | }), |
| | 495 | ), |
| | 496 | }), |
| | 497 | ); |
| 336 | | 498 | |
| 337 | const rawOutput = | 499 | const rawOutput = |
| 338 | openCodeSettings.serverUrl.length > 0 | 500 | openCodeSettings.serverUrl.length > 0 |