| ... | ... | @@ -6,6 +6,7 @@ import * as Scope from "effect/Scope"; |
| 6 | 6 | import * as Semaphore from "effect/Semaphore"; |
| 7 | 7 | |
| 8 | 8 | import { |
| 9 | NonNegativeInt, |
| 9 | 10 | TextGenerationError, |
| 10 | 11 | type ChatAttachment, |
| 11 | 12 | type ModelSelection, |
| ... | ... | @@ -33,11 +34,106 @@ import * as OpenCodeRuntime from "../provider/opencodeRuntime.ts"; |
| 33 | 34 | |
| 34 | 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 | 129 | if (!error || typeof error !== "object") { |
| 38 | 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 | 137 | const message = |
| 42 | 138 | "data" in error && |
| 43 | 139 | error.data && |
| ... | ... | @@ -47,31 +143,34 @@ function getOpenCodePromptErrorMessage(error: unknown): string | null { |
| 47 | 143 | ? error.data.message.trim() |
| 48 | 144 | : ""; |
| 49 | 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") { |
| 54 | | const name = error.name.trim(); |
| 55 | | return name.length > 0 ? name : null; |
| 152 | if (name) { |
| 153 | return { name, message: name }; |
| 56 | 154 | } |
| 57 | 155 | |
| 58 | 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 | 170 | function getOpenCodeTextResponse(parts: ReadonlyArray<unknown> | undefined): string { |
| 62 | 171 | return (parts ?? []) |
| 63 | | .flatMap((part) => { |
| 64 | | if (!part || typeof part !== "object") { |
| 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 | | }) |
| 172 | .filter(isOpenCodeTextPart) |
| 173 | .map((part) => part.text) |
| 75 | 174 | .join("") |
| 76 | 175 | .trim(); |
| 77 | 176 | } |
| ... | ... | @@ -260,11 +359,7 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration" |
| 260 | 359 | ); |
| 261 | 360 | |
| 262 | 361 | const runOpenCodeJson = Effect.fn("runOpenCodeJson")(function* <S extends Schema.Top>(input: { |
| 263 | | readonly operation: |
| 264 | | | "generateCommitMessage" |
| 265 | | | "generatePrContent" |
| 266 | | | "generateBranchName" |
| 267 | | | "generateThreadTitle"; |
| 362 | readonly operation: OpenCodeTextGenerationOperation; |
| 268 | 363 | readonly cwd: string; |
| 269 | 364 | readonly prompt: string; |
| 270 | 365 | readonly outputSchemaJson: S; |
| ... | ... | @@ -285,54 +380,121 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration" |
| 285 | 380 | resolveAttachmentPath({ attachmentsDir: serverConfig.attachmentsDir, attachment }), |
| 286 | 381 | }); |
| 287 | 382 | |
| 288 | | const runAgainstServer = (server: Pick<OpenCodeRuntime.OpenCodeServerConnection, "url">) => |
| 289 | | Effect.tryPromise({ |
| 290 | | try: async () => { |
| 291 | | const client = openCodeRuntime.createOpenCodeSdkClient({ |
| 292 | | baseUrl: server.url, |
| 293 | | directory: input.cwd, |
| 294 | | ...(openCodeSettings.serverUrl.length > 0 && openCodeSettings.serverPassword |
| 295 | | ? { serverPassword: openCodeSettings.serverPassword } |
| 296 | | : {}), |
| 383 | const runAgainstServer = Effect.fn("runOpenCodeJson.runAgainstServer")( |
| 384 | function* (server: Pick<OpenCodeRuntime.OpenCodeServerConnection, "url">) { |
| 385 | const client = openCodeRuntime.createOpenCodeSdkClient({ |
| 386 | baseUrl: server.url, |
| 387 | directory: input.cwd, |
| 388 | ...(openCodeSettings.serverUrl.length > 0 && openCodeSettings.serverPassword |
| 389 | ? { serverPassword: openCodeSettings.serverPassword } |
| 390 | : {}), |
| 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({ |
| 299 | | title: `T3 Code ${input.operation}`, |
| 300 | | permission: [{ permission: "*", pattern: "*", action: "deny" }], |
| 410 | } |
| 411 | const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent"); |
| 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) { |
| 303 | | throw new Error("OpenCode session.create returned no session payload."); |
| 304 | | } |
| 305 | | const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent"); |
| 306 | | const selectedVariant = getModelSelectionStringOptionValue( |
| 307 | | input.modelSelection, |
| 308 | | "variant", |
| 309 | | ); |
| 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], |
| 443 | } |
| 444 | const responseParts = result.data?.parts ?? []; |
| 445 | const rawText = getOpenCodeTextResponse(responseParts); |
| 446 | if (rawText.length === 0) { |
| 447 | return yield* new OpenCodeTextGenerationEmptyOutputError({ |
| 448 | ...promptContext, |
| 449 | responsePartCount: responseParts.length, |
| 450 | textPartCount: responseParts.filter(isOpenCodeTextPart).length, |
| 317 | 451 | }); |
| 318 | | const info = result.data?.info; |
| 319 | | const errorMessage = getOpenCodePromptErrorMessage(info?.error); |
| 320 | | if (errorMessage) { |
| 321 | | throw new Error(errorMessage); |
| 322 | | } |
| 323 | | const rawText = getOpenCodeTextResponse(result.data?.parts); |
| 324 | | if (rawText.length === 0) { |
| 325 | | throw new Error("OpenCode returned empty output."); |
| 326 | | } |
| 327 | | return rawText; |
| 328 | | }, |
| 329 | | catch: (cause) => |
| 330 | | new TextGenerationError({ |
| 331 | | operation: input.operation, |
| 332 | | detail: OpenCodeRuntime.openCodeRuntimeErrorDetail(cause), |
| 333 | | cause, |
| 334 | | }), |
| 335 | | }); |
| 452 | } |
| 453 | return rawText; |
| 454 | }, |
| 455 | Effect.catchTags({ |
| 456 | OpenCodeTextGenerationSessionRequestError: (cause) => |
| 457 | Effect.fail( |
| 458 | new TextGenerationError({ |
| 459 | operation: cause.operation, |
| 460 | detail: "OpenCode session.create request failed.", |
| 461 | cause, |
| 462 | }), |
| 463 | ), |
| 464 | OpenCodeTextGenerationSessionPayloadError: (cause) => |
| 465 | Effect.fail( |
| 466 | new TextGenerationError({ |
| 467 | operation: cause.operation, |
| 468 | detail: "OpenCode session.create returned no session payload.", |
| 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 | 499 | const rawOutput = |
| 338 | 500 | openCodeSettings.serverUrl.length > 0 |