Skip to content

Commit 289ceab

Browse files
committed
fix(core): resolve voice keys through Integration and accept bare model names
1 parent 430a80e commit 289ceab

4 files changed

Lines changed: 92 additions & 66 deletions

File tree

‎packages/ai/src/experimental/realtime.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ export interface RealtimeConnectInput {
2525
}
2626

2727
const engines = [
28-
{ prefix: "gpt-live-", id: "openai-live", provider: "openai", env: "OPENAI_API_KEY" },
29-
{ prefix: "gpt-realtime-", id: "openai-realtime", provider: "openai", env: "OPENAI_API_KEY" },
30-
{ prefix: "grok-voice-", id: "xai-realtime", provider: "xai", env: "XAI_API_KEY" },
28+
{ family: "gpt-live", id: "openai-live", provider: "openai" },
29+
{ family: "gpt-realtime", id: "openai-realtime", provider: "openai" },
30+
{ family: "grok-voice", id: "xai-realtime", provider: "xai" },
3131
] as const
3232

33-
export const engine = (model: string) => engines.find((item) => model.startsWith(item.prefix))
33+
// Matches the bare family name (`gpt-realtime`) and its versions (`gpt-realtime-2.1`, `gpt-realtime-mini`).
34+
export const engine = (model: string) =>
35+
engines.find((item) => model === item.family || model.startsWith(`${item.family}-`))
3436

3537
export const connect = Effect.fn("Realtime.connect")(function* (input: RealtimeConnectInput) {
3638
const resolved = engine(input.model)

‎packages/ai/test/realtime.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect } from "bun:test"
1+
import { describe, expect, test } from "bun:test"
22
import { Effect, Redacted } from "effect"
33
import { HttpClientRequest } from "effect/http"
44
import { Realtime } from "../src/experimental.js"
@@ -102,6 +102,13 @@ describe("experimental Realtime", () => {
102102
),
103103
)
104104

105+
test("routes bare and versioned model names to their engine", () => {
106+
expect(
107+
["gpt-realtime", "gpt-realtime-2.1", "gpt-live", "grok-voice-latest"].map((model) => Realtime.engine(model)?.id),
108+
).toEqual(["openai-realtime", "openai-realtime", "openai-live", "xai-realtime"])
109+
expect(Realtime.engine("gpt-realtimex")).toBeUndefined()
110+
})
111+
105112
it.effect("rejects unsupported transports before network I/O", () =>
106113
Effect.gen(function* () {
107114
const liveWs = yield* Realtime.connect({

‎packages/core/src/voice.ts‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
} from "@opencode/ai/experimental"
1010
import { RequestExecutor } from "@opencode/ai/route"
1111
import { makeLocationNode } from "@opencode/util/effect/app-node"
12-
import { Config, Context, Effect, Layer, Option, Redacted, Schema } from "effect"
13-
import { Credential } from "./credential.js"
12+
import { Context, Effect, Layer, Redacted, Schema } from "effect"
1413
import { requestExecutor } from "./effect/app-node-platform.js"
1514
import { Integration } from "./integration.js"
1615

@@ -41,26 +40,24 @@ export const layer = Layer.effect(
4140
Service,
4241
Effect.gen(function* () {
4342
const executor = yield* RequestExecutor.Service
44-
const credentials = yield* Credential.Service
43+
const integrations = yield* Integration.Service
4544

4645
const session = Effect.fn("Voice.session")(function* (input: SessionInput) {
4746
const engine = Realtime.engine(input.model)
4847
if (!engine) return yield* new InvalidRequestError({ message: `Unsupported voice model: ${input.model}` })
4948

50-
// `Credential.list` returns stored rows ordered `asc(active), asc(time_created)`, so `findLast`
51-
// selects the active credential when it is a key, or else the newest stored key credential.
52-
const saved = yield* credentials.list(Integration.ID.make(engine.provider))
53-
const stored = saved.findLast(
54-
(item): item is Credential.Info & { readonly value: Credential.Key } =>
55-
item.value.type === "key" && Boolean(item.value.key),
49+
// Integration lists the active credential first, then newer stored credentials, then its env vars.
50+
const connection = (yield* integrations.get(Integration.ID.make(engine.provider)))?.connections.find(
51+
(item) => item.type === "env" || item.method === "key",
5652
)
57-
const apiKey = stored
58-
? Option.some(Redacted.make(stored.value.key))
59-
: yield* Config.option(Config.Redacted(engine.env)).pipe(
60-
Effect.map(Option.filter((key) => Redacted.value(key) !== "")),
61-
Effect.mapError((error) => new UnavailableError({ service: engine.provider, message: error.message })),
62-
)
63-
if (Option.isNone(apiKey))
53+
const credential = connection
54+
? yield* integrations.connection
55+
.resolve(connection)
56+
.pipe(
57+
Effect.mapError((error) => new UnavailableError({ service: engine.provider, message: error.message })),
58+
)
59+
: undefined
60+
if (credential?.type !== "key")
6461
return yield* new UnavailableError({
6562
service: engine.provider,
6663
message:
@@ -69,7 +66,7 @@ export const layer = Layer.effect(
6966
: "Voice needs an xAI API key on the server (a stored key or XAI_API_KEY).",
7067
})
7168

72-
return yield* Realtime.connect({ ...input, apiKey: apiKey.value }).pipe(
69+
return yield* Realtime.connect({ ...input, apiKey: Redacted.make(credential.key) }).pipe(
7370
Effect.provideService(RequestExecutor.Service, executor),
7471
Effect.mapError(
7572
(error: AIError): Error =>
@@ -87,5 +84,5 @@ export const layer = Layer.effect(
8784
export const node = makeLocationNode({
8885
service: Service,
8986
layer,
90-
deps: [Credential.node, requestExecutor],
87+
deps: [Integration.node, requestExecutor],
9188
})

‎packages/core/test/voice.test.ts‎

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,25 @@ import { RequestExecutor } from "@opencode/ai/route"
33
import { Credential } from "@opencode/core/credential"
44
import { Integration } from "@opencode/core/integration"
55
import { Voice } from "@opencode/core/voice"
6-
import { ConfigProvider, Effect, Layer } from "effect"
6+
import { Effect, Layer } from "effect"
77
import { HttpClient, type HttpClientRequest, HttpClientResponse } from "effect/http"
88
import { testEffect } from "./lib/effect"
99

1010
const it = testEffect(Layer.empty)
1111

12-
const keyCred = (id: string, key: string) =>
13-
new Credential.Info({
14-
id: Credential.ID.make(id),
15-
integrationID: Integration.ID.make("openai"),
16-
label: id,
17-
value: Credential.Key.make({ type: "key", key }),
18-
})
19-
20-
const oauthCred = new Credential.Info({
21-
id: Credential.ID.make("oauth"),
22-
integrationID: Integration.ID.make("openai"),
23-
label: "ChatGPT",
24-
value: Credential.OAuth.make({
25-
type: "oauth",
26-
methodID: Integration.MethodID.make("chatgpt"),
27-
refresh: "rt",
28-
access: "at_oauth",
29-
expires: Date.now() + 3600_000,
30-
}),
12+
const credential = (id: string, method: "key" | "oauth") => ({
13+
type: "credential" as const,
14+
id: Credential.ID.make(id),
15+
label: id,
16+
method,
3117
})
3218

33-
it.effect("resolves credentials (active key, stored key over OAuth, env fallback, unavailable error)", () =>
19+
const keys: Record<string, string> = { active: "sk-active", stored: "sk-stored", XAI_API_KEY: "xai-env-key" }
20+
21+
it.effect("uses the first key or env connection Integration lists and never a ChatGPT sign-in", () =>
3422
Effect.gen(function* () {
3523
const auths: string[] = []
36-
const record = (req: HttpClientRequest.HttpClientRequest) => {
37-
auths.push(req.headers.authorization ?? "")
38-
return Response.json({ session: { id: "s1" }, transport: { sdp: "v=0\r\no=answer\r\n" } })
39-
}
40-
const call = (model: string, stored: ReadonlyArray<Credential.Info>, onReq = record) =>
24+
const call = (model: string, connections: Integration.Info["connections"]) =>
4125
Effect.flatMap(Voice.Service, (voice) =>
4226
voice.session({
4327
model,
@@ -48,12 +32,54 @@ it.effect("resolves credentials (active key, stored key over OAuth, env fallback
4832
Voice.layer.pipe(
4933
Layer.provide(
5034
Layer.merge(
51-
Layer.mock(Credential.Service, { list: () => Effect.succeed([...stored]) }),
35+
Layer.mock(Integration.Service, {
36+
revision: () => 0,
37+
get: (id) => Effect.succeed({ id, name: id, methods: [], connections }),
38+
connection: {
39+
active: () => Effect.die("unused"),
40+
resolve: (connection) =>
41+
connection.type === "credential" && connection.method === "oauth"
42+
? Effect.die("a ChatGPT sign-in was resolved")
43+
: Effect.succeed(
44+
Credential.Key.make({
45+
type: "key",
46+
key: keys[connection.type === "env" ? connection.name : connection.id],
47+
}),
48+
),
49+
key: () => Effect.die("unused"),
50+
external: () => Effect.die("unused"),
51+
activate: () => Effect.die("unused"),
52+
update: () => Effect.die("unused"),
53+
remove: () => Effect.die("unused"),
54+
status: () => Effect.die("unused"),
55+
},
56+
oauth: {
57+
connect: () => Effect.die("unused"),
58+
status: () => Effect.die("unused"),
59+
complete: () => Effect.die("unused"),
60+
cancel: () => Effect.die("unused"),
61+
},
62+
command: {
63+
connect: () => Effect.die("unused"),
64+
status: () => Effect.die("unused"),
65+
cancel: () => Effect.die("unused"),
66+
},
67+
}),
5268
RequestExecutor.layer.pipe(
5369
Layer.provide(
5470
Layer.succeed(
5571
HttpClient.HttpClient,
56-
HttpClient.make((req) => Effect.sync(() => HttpClientResponse.fromWeb(req, onReq(req)))),
72+
HttpClient.make((req: HttpClientRequest.HttpClientRequest) =>
73+
Effect.sync(() => {
74+
auths.push(req.headers.authorization ?? "")
75+
return HttpClientResponse.fromWeb(
76+
req,
77+
model.startsWith("grok-")
78+
? Response.json({ value: "ek_xai", expires_at: 1760000999 })
79+
: Response.json({ transport: { sdp: "v=0\r\no=answer\r\n" } }),
80+
)
81+
}),
82+
),
5783
),
5884
),
5985
),
@@ -63,25 +89,19 @@ it.effect("resolves credentials (active key, stored key over OAuth, env fallback
6389
),
6490
)
6591

66-
// Active credential is last in `Credential.list`: active key wins, and OAuth is skipped for stored key.
67-
yield* call("gpt-live-1", [keyCred("old", "sk-old"), keyCred("active", "sk-active")])
68-
yield* call("gpt-live-1", [keyCred("stored", "sk-stored"), oauthCred])
69-
expect(auths).toEqual(["Bearer sk-active", "Bearer sk-stored"])
70-
71-
const oauthOnly = yield* call("gpt-live-1", [oauthCred]).pipe(Effect.flip)
72-
expect(oauthOnly._tag).toBe("Voice.UnavailableError")
73-
expect(oauthOnly.message).toContain("OpenAI API key")
74-
75-
const grok = yield* call("grok-voice-3.0", [], (req) => {
76-
auths.push(req.headers.authorization ?? "")
77-
return Response.json({ value: "ek_xai", expires_at: 1760000999 })
78-
})
79-
expect(auths.at(-1)).toBe("Bearer xai-env-key")
92+
yield* call("gpt-live-1", [credential("active", "key"), credential("stored", "key")])
93+
yield* call("gpt-live-1", [credential("chatgpt", "oauth"), credential("stored", "key")])
94+
const grok = yield* call("grok-voice-3.0", [{ type: "env", name: "XAI_API_KEY" }])
95+
expect(auths).toEqual(["Bearer sk-active", "Bearer sk-stored", "Bearer xai-env-key"])
8096
expect(grok).toEqual({
8197
type: "websocket",
8298
url: "wss://api.x.ai/v1/realtime?model=grok-voice-3.0",
8399
token: "ek_xai",
84100
expiresAt: 1760000999,
85101
})
86-
}).pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { XAI_API_KEY: "xai-env-key" } })))),
102+
103+
const signInOnly = yield* call("gpt-live-1", [credential("chatgpt", "oauth")]).pipe(Effect.flip)
104+
expect(signInOnly._tag).toBe("Voice.UnavailableError")
105+
expect(signInOnly.message).toContain("OpenAI API key")
106+
}),
87107
)

0 commit comments

Comments
 (0)