-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Open
nexxeln
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
nexxeln:nxl/opencode-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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()), | ||
| ); | ||
|
|
||
| return yield* Schema.decodeUnknownEffect(input.outputSchemaJson)(structuredOutput).pipe( | ||
| Effect.catchTag("SchemaError", (cause) => | ||
| 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.