Repository navigation
feat: add opencode provider support #1758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
115982c
feat: add opencode provider
nexxeln 2811ba6
feat(git): add opencode text generation
nexxeln 1e1f31c
Merge branch 'main' into nxl/opencode-adapter
juliusmarminge 5ebbf95
Add OpenCode composer adapter support
juliusmarminge d362997
Reuse OpenCode server between text generation calls
juliusmarminge 4d1cf0b
Support configured OpenCode server URLs
juliusmarminge ea16d83
Add OpenCode server password support
juliusmarminge 21450f6
Merge origin/main into t3code/pr-1758/nxl/opencode-adapter
juliusmarminge b7f73ce
Merge origin/main into t3code/pr-1758/nxl/opencode-adapter
juliusmarminge 8a3159c
Tighten OpenCode adapter session and permission handling
juliusmarminge c69f377
Harden OpenCode defaults and race cleanup
juliusmarminge e7dd9ba
Merge origin/main into t3code/pr-1758/nxl/opencode-adapter
juliusmarminge 81717b0
Handle OpenCode text rollback and full-thread reverts
juliusmarminge 4de7f3a
Log provider events with emitting thread IDs
juliusmarminge 0827397
Merge origin/main into t3code/pr-1758/nxl/opencode-adapter
juliusmarminge 2f09cf7
fix: address opencode adapter review feedback
juliusmarminge fe6d56a
Skip OpenCode version probe when provider is disabled
juliusmarminge a687afc
Merge remote-tracking branch 'origin/main' into nxl/opencode-adapter
juliusmarminge 570f819
Remove stale `remove` mock from OpenCode adapter test
juliusmarminge b5d3ea7
Fix missing stdout/stderr in OpenCode server startup failure
juliusmarminge 027ed78
Detect macOS quarantine when OpenCode server is SIGKILL'd
juliusmarminge d5c083b
Improve SIGKILL diagnostics with crash report inspection
juliusmarminge e2d5c2f
Harden OpenCode adapter and surface provider
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| import { Effect, Layer, Schema } from "effect"; | ||
|
|
||
| import { | ||
| TextGenerationError, | ||
| type ChatAttachment, | ||
| type OpenCodeModelSelection, | ||
| } from "@t3tools/contracts"; | ||
| import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git"; | ||
|
|
||
| import { ServerConfig } from "../../config.ts"; | ||
| import { resolveAttachmentPath } from "../../attachmentStore.ts"; | ||
| import { ServerSettingsService } from "../../serverSettings.ts"; | ||
| import { | ||
| buildBranchNamePrompt, | ||
| buildCommitMessagePrompt, | ||
| buildPrContentPrompt, | ||
| buildThreadTitlePrompt, | ||
| } from "../Prompts.ts"; | ||
| import { type TextGenerationShape, TextGeneration } from "../Services/TextGeneration.ts"; | ||
| import { | ||
| sanitizeCommitSubject, | ||
| sanitizePrTitle, | ||
| sanitizeThreadTitle, | ||
| toJsonSchemaObject, | ||
| } from "../Utils.ts"; | ||
| import { | ||
| createOpenCodeSdkClient, | ||
| parseOpenCodeModelSlug, | ||
| startOpenCodeServerProcess, | ||
| toOpenCodeFileParts, | ||
| } from "../../provider/opencodeRuntime.ts"; | ||
|
|
||
| const makeOpenCodeTextGeneration = Effect.gen(function* () { | ||
| const serverConfig = yield* ServerConfig; | ||
| const serverSettingsService = yield* ServerSettingsService; | ||
|
|
||
| const runOpenCodeJson = Effect.fn("runOpenCodeJson")(function* <S extends Schema.Top>(input: { | ||
| readonly operation: | ||
| | "generateCommitMessage" | ||
| | "generatePrContent" | ||
| | "generateBranchName" | ||
| | "generateThreadTitle"; | ||
| readonly cwd: string; | ||
| readonly prompt: string; | ||
| readonly outputSchemaJson: S; | ||
| readonly modelSelection: OpenCodeModelSelection; | ||
| readonly attachments?: ReadonlyArray<ChatAttachment> | undefined; | ||
| }) { | ||
| const parsedModel = parseOpenCodeModelSlug(input.modelSelection.model); | ||
| if (!parsedModel) { | ||
| return yield* new TextGenerationError({ | ||
| operation: input.operation, | ||
| detail: "OpenCode model selection must use the 'provider/model' format.", | ||
| }); | ||
| } | ||
|
|
||
| const settings = yield* serverSettingsService.getSettings.pipe( | ||
| Effect.map((value) => value.providers.opencode), | ||
| Effect.orElseSucceed(() => ({ enabled: true, binaryPath: "opencode", customModels: [] })), | ||
| ); | ||
|
|
||
| const fileParts = toOpenCodeFileParts({ | ||
| attachments: input.attachments, | ||
| resolveAttachmentPath: (attachment) => | ||
| resolveAttachmentPath({ attachmentsDir: serverConfig.attachmentsDir, attachment }), | ||
| }); | ||
|
|
||
| const structuredOutput = yield* Effect.acquireUseRelease( | ||
| Effect.tryPromise({ | ||
| try: () => startOpenCodeServerProcess({ binaryPath: settings.binaryPath }), | ||
| catch: (cause) => | ||
| new TextGenerationError({ | ||
| operation: input.operation, | ||
| detail: cause instanceof Error ? cause.message : "Failed to start OpenCode server.", | ||
| cause, | ||
| }), | ||
| }), | ||
| (server) => | ||
| Effect.tryPromise({ | ||
| try: async () => { | ||
| const client = createOpenCodeSdkClient({ baseUrl: server.url, directory: input.cwd }); | ||
| const session = await client.session.create({ | ||
| title: `T3 Code ${input.operation}`, | ||
| permission: [{ permission: "*", pattern: "*", action: "deny" }], | ||
| }); | ||
| if (!session.data) { | ||
| throw new Error("OpenCode session.create returned no session payload."); | ||
| } | ||
|
|
||
| const result = await client.session.prompt({ | ||
| sessionID: session.data.id, | ||
| model: parsedModel, | ||
| ...(input.modelSelection.options?.agent | ||
| ? { agent: input.modelSelection.options.agent } | ||
| : {}), | ||
| ...(input.modelSelection.options?.variant | ||
| ? { variant: input.modelSelection.options.variant } | ||
| : {}), | ||
| format: { | ||
| type: "json_schema", | ||
| schema: toJsonSchemaObject(input.outputSchemaJson) as Record<string, unknown>, | ||
| }, | ||
| parts: [{ type: "text", text: input.prompt }, ...fileParts], | ||
| }); | ||
| const structured = result.data?.info.structured; | ||
| if (structured === undefined) { | ||
| throw new Error("OpenCode returned no structured output."); | ||
| } | ||
| return structured; | ||
| }, | ||
| catch: (cause) => | ||
| new TextGenerationError({ | ||
| operation: input.operation, | ||
| detail: | ||
| cause instanceof Error ? cause.message : "OpenCode text generation request failed.", | ||
| cause, | ||
| }), | ||
| }), | ||
| (server) => Effect.sync(() => server.close()), | ||
| ); | ||
|
juliusmarminge marked this conversation as resolved.
Outdated
|
||
|
|
||
| return yield* Schema.decodeUnknownEffect(input.outputSchemaJson)(structuredOutput).pipe( | ||
| Effect.catchTag("SchemaError", (cause) => | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| Effect.fail( | ||
| new TextGenerationError({ | ||
| operation: input.operation, | ||
| detail: "OpenCode returned invalid structured output.", | ||
| cause, | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| const generateCommitMessage: TextGenerationShape["generateCommitMessage"] = Effect.fn( | ||
| "OpenCodeTextGeneration.generateCommitMessage", | ||
| )(function* (input) { | ||
| if (input.modelSelection.provider !== "opencode") { | ||
| return yield* new TextGenerationError({ | ||
| operation: "generateCommitMessage", | ||
| detail: "Invalid model selection.", | ||
| }); | ||
| } | ||
|
|
||
| const { prompt, outputSchema } = buildCommitMessagePrompt({ | ||
| branch: input.branch, | ||
| stagedSummary: input.stagedSummary, | ||
| stagedPatch: input.stagedPatch, | ||
| includeBranch: input.includeBranch === true, | ||
| }); | ||
| const generated = yield* runOpenCodeJson({ | ||
| operation: "generateCommitMessage", | ||
| cwd: input.cwd, | ||
| prompt, | ||
| outputSchemaJson: outputSchema, | ||
| modelSelection: input.modelSelection, | ||
| }); | ||
|
|
||
| return { | ||
| subject: sanitizeCommitSubject(generated.subject), | ||
| body: generated.body.trim(), | ||
| ...("branch" in generated && typeof generated.branch === "string" | ||
| ? { branch: sanitizeFeatureBranchName(generated.branch) } | ||
| : {}), | ||
| }; | ||
| }); | ||
|
|
||
| const generatePrContent: TextGenerationShape["generatePrContent"] = Effect.fn( | ||
| "OpenCodeTextGeneration.generatePrContent", | ||
| )(function* (input) { | ||
| if (input.modelSelection.provider !== "opencode") { | ||
| return yield* new TextGenerationError({ | ||
| operation: "generatePrContent", | ||
| detail: "Invalid model selection.", | ||
| }); | ||
| } | ||
|
|
||
| const { prompt, outputSchema } = buildPrContentPrompt({ | ||
| baseBranch: input.baseBranch, | ||
| headBranch: input.headBranch, | ||
| commitSummary: input.commitSummary, | ||
| diffSummary: input.diffSummary, | ||
| diffPatch: input.diffPatch, | ||
| }); | ||
| const generated = yield* runOpenCodeJson({ | ||
| operation: "generatePrContent", | ||
| cwd: input.cwd, | ||
| prompt, | ||
| outputSchemaJson: outputSchema, | ||
| modelSelection: input.modelSelection, | ||
| }); | ||
|
|
||
| return { | ||
| title: sanitizePrTitle(generated.title), | ||
| body: generated.body.trim(), | ||
| }; | ||
| }); | ||
|
|
||
| const generateBranchName: TextGenerationShape["generateBranchName"] = Effect.fn( | ||
| "OpenCodeTextGeneration.generateBranchName", | ||
| )(function* (input) { | ||
| if (input.modelSelection.provider !== "opencode") { | ||
| return yield* new TextGenerationError({ | ||
| operation: "generateBranchName", | ||
| detail: "Invalid model selection.", | ||
| }); | ||
| } | ||
|
|
||
| const { prompt, outputSchema } = buildBranchNamePrompt({ | ||
| message: input.message, | ||
| attachments: input.attachments, | ||
| }); | ||
| const generated = yield* runOpenCodeJson({ | ||
| operation: "generateBranchName", | ||
| cwd: input.cwd, | ||
| prompt, | ||
| outputSchemaJson: outputSchema, | ||
| modelSelection: input.modelSelection, | ||
| attachments: input.attachments, | ||
| }); | ||
|
|
||
| return { | ||
| branch: sanitizeBranchFragment(generated.branch), | ||
| }; | ||
| }); | ||
|
|
||
| const generateThreadTitle: TextGenerationShape["generateThreadTitle"] = Effect.fn( | ||
| "OpenCodeTextGeneration.generateThreadTitle", | ||
| )(function* (input) { | ||
| if (input.modelSelection.provider !== "opencode") { | ||
| return yield* new TextGenerationError({ | ||
| operation: "generateThreadTitle", | ||
| detail: "Invalid model selection.", | ||
| }); | ||
| } | ||
|
|
||
| const { prompt, outputSchema } = buildThreadTitlePrompt({ | ||
| message: input.message, | ||
| attachments: input.attachments, | ||
| }); | ||
| const generated = yield* runOpenCodeJson({ | ||
| operation: "generateThreadTitle", | ||
| cwd: input.cwd, | ||
| prompt, | ||
| outputSchemaJson: outputSchema, | ||
| modelSelection: input.modelSelection, | ||
| attachments: input.attachments, | ||
| }); | ||
|
|
||
| return { | ||
| title: sanitizeThreadTitle(generated.title), | ||
| }; | ||
| }); | ||
|
|
||
| return { | ||
| generateCommitMessage, | ||
| generatePrContent, | ||
| generateBranchName, | ||
| generateThreadTitle, | ||
| } satisfies TextGenerationShape; | ||
| }); | ||
|
|
||
| export const OpenCodeTextGenerationLive = Layer.effect(TextGeneration, makeOpenCodeTextGeneration); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.