Skip to content

Commit fb5d910

Browse files
committed
release: v1.0.2 - configure translator variants
1 parent 0f146a0 commit fb5d910

8 files changed

Lines changed: 211 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Add to `~/.config/opencode/opencode.jsonc`:
3535
"plugin": [
3636
["opencode-translate", {
3737
"model": "openai/gpt-5.4-mini", // model to use for translation
38-
"lang": "Korean" // language you speak
38+
"variant": "minimal", // optional model variant / thinking effort
39+
"lang": "Korean" // language you speak
3940
}]
4041
]
4142
}
@@ -56,6 +57,7 @@ All subsequent messages in the same session are translated automatically — no
5657
| Option | Type | Default | Description |
5758
| --- | --- | --- | --- |
5859
| `model` | string | required | Translator model in `provider/model-id` form |
60+
| `variant` | string | optional | Translator model variant / thinking effort (for example, `"minimal"`, `"high"`, or `"max"`) |
5961
| `lang` | string | required | Language you speak (e.g. `"Korean"`, `"Japanese"`) |
6062
| `trigger` | string[] | `["$en"]` | Keywords that activate translation |
6163
| `verbose` | boolean | `false` | Print translation logs |

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opencode-translate",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "OpenCode plugin that lets the user chat in a configured language while the main chat loop only sees English.",
55
"type": "module",
66
"main": "src/index.ts",

‎src/constants/options.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function resolveOptions(options: Record<string, unknown>): ResolvedTransl
2121
`[${PLUGIN_NAME}:INVALID_OPTIONS] options.lang is required. Set it to the user's language, e.g. "Korean" or "Japanese".`,
2222
)
2323
}
24+
const variant = typeof options.variant === "string" ? options.variant.trim() : ""
2425

2526
const rawTrigger = Array.isArray(options.trigger)
2627
? options.trigger
@@ -31,6 +32,7 @@ export function resolveOptions(options: Record<string, unknown>): ResolvedTransl
3132

3233
return {
3334
model,
35+
...(variant ? { variant } : {}),
3436
trigger: trigger.length > 0 ? trigger : [...DEFAULT_TRIGGER],
3537
lang,
3638
verbose: options.verbose === true,

‎src/constants/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ProviderSource = "env" | "config" | "custom" | "api"
66

77
export interface ResolvedTranslateOptions {
88
model: string
9+
variant?: string
910
trigger: string[]
1011
lang: string
1112
verbose: boolean
@@ -67,6 +68,7 @@ export interface ProviderModelInfo {
6768
}
6869
headers?: Record<string, string>
6970
options?: Record<string, unknown>
71+
variants?: Record<string, Record<string, unknown>>
7072
capabilities?: {
7173
temperature?: boolean
7274
}

‎src/translator/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { buildSystemPrompt, buildUserPrompt, unwrapEchoedTextEnvelope } from "..
1313
import { __resetSyntheticPartIDForTest } from "./part-id"
1414
import {
1515
__resetProviderFactoryCacheForTest,
16+
buildVariantProviderOptions,
1617
instantiateModel,
1718
instantiateProvider,
1819
loadFactory,
@@ -73,6 +74,7 @@ export function createTranslator(
7374
const { providerID, modelID } = parseTranslatorModel(options.model)
7475
const credentials = await credentialResolver.resolve(options.model)
7576
const modelInfo = resolveModelInfo(credentials.provider, modelID)
77+
const variantProviderOptions = buildVariantProviderOptions(providerID, modelID, modelInfo, options.variant)
7678
const factory = await loadFactory(providerID, modelInfo)
7779
const provider = instantiateProvider(factory, providerID, credentials, modelInfo)
7880
const providerOptions = { ...(credentials.provider?.options ?? {}), ...(modelInfo.options ?? {}) }
@@ -85,6 +87,7 @@ export function createTranslator(
8587
model,
8688
system: buildSystemPrompt(input),
8789
...(supportsTemperature(providerID, modelID, modelInfo) ? { temperature: 0 } : {}),
90+
...(variantProviderOptions ? { providerOptions: variantProviderOptions } : {}),
8891
prompt: buildUserPrompt(input),
8992
}),
9093
timeoutMs,

‎src/translator/provider.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AuthInfo, FetchLike, ProviderInfo, ProviderModelInfo } from "../constants"
1+
import { type AuthInfo, type FetchLike, PLUGIN_NAME, type ProviderInfo, type ProviderModelInfo } from "../constants"
22

33
const providerFactoryCache = new Map<string, unknown>()
44

@@ -23,6 +23,26 @@ const CREATE_EXPORT_FALLBACK: Record<string, string[]> = {
2323
"@openrouter/ai-sdk-provider": ["createOpenRouter", "openrouter"],
2424
}
2525

26+
const PROVIDER_OPTIONS_KEY: Record<string, string> = {
27+
"@ai-sdk/amazon-bedrock": "bedrock",
28+
"@ai-sdk/amazon-bedrock/mantle": "openai",
29+
"@ai-sdk/anthropic": "anthropic",
30+
"@ai-sdk/azure": "openai",
31+
"@ai-sdk/gateway": "gateway",
32+
"@ai-sdk/github-copilot": "openai",
33+
"@ai-sdk/google": "google",
34+
"@ai-sdk/google-vertex": "vertex",
35+
"@ai-sdk/google-vertex/anthropic": "anthropic",
36+
"@ai-sdk/openai": "openai",
37+
"@openrouter/ai-sdk-provider": "openrouter",
38+
"ai-gateway-provider": "openaiCompatible",
39+
}
40+
41+
type JsonValue = null | string | number | boolean | JsonObject | JsonArray
42+
type JsonObject = { [key: string]: JsonValue | undefined }
43+
type JsonArray = JsonValue[]
44+
type VariantProviderOptions = Record<string, JsonObject>
45+
2646
interface ProviderCredentials {
2747
provider?: ProviderInfo
2848
authInfo?: AuthInfo
@@ -73,6 +93,40 @@ export function resolveModelInfo(provider: ProviderInfo | undefined, modelID: st
7393
return provider?.models?.[modelID] ?? { id: modelID, api: { id: modelID } }
7494
}
7595

96+
function sdkProviderOptionsKey(providerID: string, model?: ProviderModelInfo): string {
97+
const packageName = model?.api?.npm
98+
if (packageName && PROVIDER_OPTIONS_KEY[packageName]) return PROVIDER_OPTIONS_KEY[packageName]
99+
if (packageName === "@ai-sdk/openai-compatible" || packageName === "@ai-sdk/openai") return providerID.split(".")[0]
100+
return providerID
101+
}
102+
103+
function invalidVariantError(providerID: string, modelID: string, model: ProviderModelInfo, variant: string) {
104+
const variants = Object.keys(model.variants ?? {}).sort()
105+
const modelName = `${providerID}/${modelID}`
106+
if (variants.length === 0) {
107+
return new Error(
108+
`[${PLUGIN_NAME}:INVALID_VARIANT] options.variant "${variant}" is not available for "${modelName}". This model has no configurable variants.`,
109+
)
110+
}
111+
return new Error(
112+
`[${PLUGIN_NAME}:INVALID_VARIANT] options.variant "${variant}" is not available for "${modelName}". Available variants: ${variants.join(", ")}.`,
113+
)
114+
}
115+
116+
export function buildVariantProviderOptions(
117+
providerID: string,
118+
modelID: string,
119+
model: ProviderModelInfo,
120+
variant?: string,
121+
): VariantProviderOptions | undefined {
122+
if (!variant) return undefined
123+
const selected = model.variants?.[variant]
124+
if (!selected) throw invalidVariantError(providerID, modelID, model, variant)
125+
const providerOptions = selected as JsonObject
126+
if (model.api?.npm === "@ai-sdk/azure") return { openai: providerOptions, azure: providerOptions }
127+
return { [sdkProviderOptionsKey(providerID, model)]: providerOptions }
128+
}
129+
76130
function headerRecord(value: unknown): Record<string, string> {
77131
if (!value || typeof value !== "object" || Array.isArray(value)) return {}
78132
return Object.fromEntries(

‎test/constants/options.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@ describe("option resolution", () => {
77
resolveOptions({
88
lang: "Korean",
99
model: " anthropic/claude-haiku-4-5 ",
10+
variant: " minimal ",
1011
trigger: ["", "$go", 123],
1112
verbose: "yes",
1213
}),
1314
).toEqual({
1415
model: "anthropic/claude-haiku-4-5",
16+
variant: "minimal",
1517
trigger: ["$go"],
1618
lang: "Korean",
1719
verbose: false,
1820
})
1921
})
2022

23+
test("resolveOptions omits blank variants", () => {
24+
expect(resolveOptions({ lang: "Korean", model: "anthropic/claude-haiku-4-5", variant: " " })).toEqual({
25+
model: "anthropic/claude-haiku-4-5",
26+
trigger: ["$en"],
27+
lang: "Korean",
28+
verbose: false,
29+
})
30+
})
31+
2132
test("resolveOptions accepts legacy triggerKeywords configuration", () => {
2233
expect(
2334
resolveOptions({

‎test/translator/create-translator.test.ts‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,140 @@ test("OpenAI reasoning translators omit unsupported temperature", async () => {
130130
direction: "inbound",
131131
})
132132
expect(request?.temperature).toBeUndefined()
133+
expect(request?.providerOptions).toBeUndefined()
134+
})
135+
136+
test("translator passes configured OpenAI variant as providerOptions", async () => {
137+
let request: Record<string, unknown> | undefined
138+
const translator = createTranslator(fakeClient([]), testOptions({ model: "openai/gpt-5.5", variant: "minimal" }), {
139+
credentialResolver: {
140+
resolve: async () => ({
141+
providerID: "openai",
142+
provider: {
143+
id: "openai",
144+
source: "env" as const,
145+
env: ["OPENAI_API_KEY"],
146+
models: {
147+
"gpt-5.5": {
148+
id: "gpt-5.5",
149+
api: { id: "gpt-5.5", npm: "@ai-sdk/openai" },
150+
variants: { minimal: { reasoningEffort: "minimal" } },
151+
},
152+
},
153+
},
154+
apiKey: "test-key",
155+
mode: "apiKey" as const,
156+
}),
157+
isMissingCredentialError: () => false,
158+
authUnavailable: () => new Error("unused"),
159+
envFallback: "OPENAI_API_KEY",
160+
},
161+
generateTextImpl: async (input) => {
162+
request = input as Record<string, unknown>
163+
return { text: "hello" } as never
164+
},
165+
sleep: async () => undefined,
166+
})
167+
168+
await translator.translateText({
169+
text: "안녕",
170+
sourceLanguage: "Korean",
171+
targetLanguage: "English",
172+
direction: "inbound",
173+
})
174+
expect(request?.providerOptions).toEqual({ openai: { reasoningEffort: "minimal" } })
175+
})
176+
177+
test("translator passes configured Anthropic variant as providerOptions", async () => {
178+
let request: Record<string, unknown> | undefined
179+
const translator = createTranslator(
180+
fakeClient([]),
181+
testOptions({ model: "anthropic/claude-opus-4-8", variant: "max" }),
182+
{
183+
credentialResolver: {
184+
resolve: async () => ({
185+
providerID: "anthropic",
186+
provider: {
187+
id: "anthropic",
188+
source: "env" as const,
189+
env: ["ANTHROPIC_API_KEY"],
190+
models: {
191+
"claude-opus-4-8": {
192+
id: "claude-opus-4-8",
193+
api: { id: "claude-opus-4-8", npm: "@ai-sdk/anthropic" },
194+
variants: { max: { thinking: { type: "enabled", budgetTokens: 31_999 } } },
195+
},
196+
},
197+
},
198+
apiKey: "test-key",
199+
mode: "apiKey" as const,
200+
}),
201+
isMissingCredentialError: () => false,
202+
authUnavailable: () => new Error("unused"),
203+
envFallback: "ANTHROPIC_API_KEY",
204+
},
205+
generateTextImpl: async (input) => {
206+
request = input as Record<string, unknown>
207+
return { text: "hello" } as never
208+
},
209+
sleep: async () => undefined,
210+
},
211+
)
212+
213+
await translator.translateText({
214+
text: "안녕",
215+
sourceLanguage: "Korean",
216+
targetLanguage: "English",
217+
direction: "inbound",
218+
})
219+
expect(request?.providerOptions).toEqual({
220+
anthropic: { thinking: { type: "enabled", budgetTokens: 31_999 } },
221+
})
222+
})
223+
224+
test("unknown variants fail before generateText", async () => {
225+
let calls = 0
226+
const translator = createTranslator(fakeClient([]), testOptions({ variant: "max" }), {
227+
credentialResolver: {
228+
resolve: async () => ({
229+
providerID: "anthropic",
230+
provider: {
231+
id: "anthropic",
232+
source: "env" as const,
233+
env: ["ANTHROPIC_API_KEY"],
234+
models: {
235+
"claude-haiku-4-5": {
236+
id: "claude-haiku-4-5",
237+
api: { id: "claude-haiku-4-5", npm: "@ai-sdk/anthropic" },
238+
variants: { high: { thinking: { type: "enabled", budgetTokens: 16_000 } } },
239+
},
240+
},
241+
},
242+
apiKey: "test-key",
243+
mode: "apiKey" as const,
244+
}),
245+
isMissingCredentialError: () => false,
246+
authUnavailable: () => new Error("unused"),
247+
envFallback: "ANTHROPIC_API_KEY",
248+
},
249+
generateTextImpl: async () => {
250+
calls += 1
251+
return { text: "hello" } as never
252+
},
253+
sleep: async () => undefined,
254+
})
255+
256+
await expect(
257+
translator.translateText({
258+
text: "안녕",
259+
sourceLanguage: "Korean",
260+
targetLanguage: "English",
261+
direction: "inbound",
262+
}),
263+
).rejects.toThrow(
264+
'[opencode-translate:INVALID_VARIANT] options.variant "max" is not available for "anthropic/claude-haiku-4-5". Available variants: high.',
265+
)
266+
expect(calls).toBe(0)
133267
})
134268

135269
test("missing credentials surface the exact auth-unavailable error", async () => {

0 commit comments

Comments
 (0)