mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
chore(server): revert hosted GPT-Live on the connected OpenCode Console (#58461)
This commit is contained in:
@@ -57,7 +57,6 @@ import { Npm } from "@opencode/util/npm"
|
||||
import { Permission } from "../permission.js"
|
||||
import { Reference } from "../reference.js"
|
||||
import { WebSearch } from "../websearch.js"
|
||||
import { Voice } from "../voice.js"
|
||||
import { Ripgrep } from "../ripgrep.js"
|
||||
import { Session } from "../session.js"
|
||||
import { SessionCompaction } from "../session/compaction.js"
|
||||
@@ -153,7 +152,6 @@ const services = [
|
||||
SkillDiscovery.Service,
|
||||
Tool.Service,
|
||||
ToolOutput.Service,
|
||||
Voice.Service,
|
||||
Watcher.Service,
|
||||
WellKnown.Service,
|
||||
Worktree.Service,
|
||||
@@ -208,7 +206,6 @@ export const requirements = LayerNode.group([
|
||||
SkillDiscovery.node,
|
||||
Tool.node,
|
||||
ToolOutput.node,
|
||||
Voice.node,
|
||||
Watcher.node,
|
||||
WellKnown.node,
|
||||
Worktree.node,
|
||||
|
||||
@@ -24,7 +24,6 @@ import { ManagedPolicy } from "../../managed-policy.js"
|
||||
import { Model } from "../../model.js"
|
||||
import { Provider } from "../../provider.js"
|
||||
import { Variant } from "../../variant.js"
|
||||
import { Voice } from "../../voice.js"
|
||||
import { WebSearch } from "../../websearch.js"
|
||||
import { ConfigPolicy } from "@opencode/schema/config/policy"
|
||||
import { ConfigProvider } from "@opencode/schema/config/provider"
|
||||
@@ -65,13 +64,6 @@ const RemoteResponse = Schema.Struct({
|
||||
Schema.Struct({ ...Mcp.RemoteConfig.fields, auth: Schema.Literal("console").pipe(Schema.optional) }),
|
||||
),
|
||||
}).pipe(Schema.optional),
|
||||
// Hosted voice models the Console starts for the caller's workspace, the inference base that creates them, and the
|
||||
// workspace's display name, which older Consoles omit.
|
||||
voice: Schema.Struct({
|
||||
baseURL: Schema.String,
|
||||
models: Schema.Array(Schema.String),
|
||||
workspace: Schema.String.pipe(Schema.optional),
|
||||
}).pipe(Schema.optional),
|
||||
// Organization policy compiled for the authenticated caller; omitted when there is none.
|
||||
// `experimental.policies` remains accepted alongside top-level `policies`.
|
||||
policies: RemotePolicies,
|
||||
@@ -101,10 +93,6 @@ const SsoRequired = Schema.Struct({
|
||||
connectionId: Schema.String,
|
||||
})
|
||||
const InvalidGrant = Schema.Struct({ error: Schema.Literal("invalid_grant") })
|
||||
const LiveSession = Schema.fromJsonString(
|
||||
Schema.Struct({ session: Schema.Struct({ id: Schema.String }), transport: Schema.Struct({ sdp: Schema.String }) }),
|
||||
)
|
||||
const InferenceError = Schema.fromJsonString(Schema.Struct({ error: Schema.Struct({ message: Schema.String }) }))
|
||||
// Console's 401 when the session behind the access token was revoked or expired.
|
||||
const Unauthorized = Schema.Struct({ _tag: Schema.Literal("Unauthorized") })
|
||||
const signedOutMessage = "Reconnect OpenCode Console to continue"
|
||||
@@ -206,9 +194,7 @@ function oauth(http: HttpClient.HttpClient) {
|
||||
} satisfies IntegrationOAuthMethodRegistration
|
||||
}
|
||||
|
||||
export const OpencodePlugin = define<
|
||||
HttpClient.HttpClient | Bus.Service | ManagedPolicy.Service | Voice.Service | Scope.Scope
|
||||
>({
|
||||
export const OpencodePlugin = define<HttpClient.HttpClient | Bus.Service | ManagedPolicy.Service | Scope.Scope>({
|
||||
id: "opencode.provider.opencode",
|
||||
effect: Effect.fn(function* (ctx) {
|
||||
const disableFree = ["1", "true"].includes(process.env.OPENCODE_DISABLE_FREE_MODELS?.toLowerCase() ?? "")
|
||||
@@ -218,7 +204,6 @@ export const OpencodePlugin = define<
|
||||
// and whether it evaluates the policies it is being sent.
|
||||
const http = HttpClient.mapRequest(client, HttpClientRequest.setHeader("User-Agent", App.useragent(ctx.app)))
|
||||
const managed = yield* ManagedPolicy.Service
|
||||
const voice = yield* Voice.Service
|
||||
const scope = yield* Scope.Scope
|
||||
const loading = Semaphore.makeUnsafe(1)
|
||||
type ActiveConnection = Effect.Success<ReturnType<typeof ctx.integration.connection.active>>
|
||||
@@ -277,105 +262,12 @@ export const OpencodePlugin = define<
|
||||
),
|
||||
)
|
||||
})
|
||||
// The credential of the connection a snapshot was built from, while it is still the active one.
|
||||
const resolveActive = Effect.fnUntraced(function* (connection: NonNullable<ActiveConnection>) {
|
||||
const active = yield* ctx.integration.connection.active("opencode")
|
||||
if (!active || IntegrationConnection.key(active) !== IntegrationConnection.key(connection))
|
||||
return yield* Effect.fail(new Error("OpenCode Console connection changed"))
|
||||
const credential = yield* ctx.integration.connection.resolve(active)
|
||||
if (!credential || credential.type === "external")
|
||||
return yield* Effect.fail(new Error("OpenCode Console is not connected"))
|
||||
return credential
|
||||
})
|
||||
|
||||
// Hosted GPT-Live: the server's apps start sessions on the Console's account, billed to the workspace, with
|
||||
// the credential of the connection whose config offered it.
|
||||
const consoleVoice = (
|
||||
connection: NonNullable<ActiveConnection>,
|
||||
offer: NonNullable<typeof RemoteResponse.Type.voice>,
|
||||
): Voice.Host => {
|
||||
const send = (path: string, body: unknown) =>
|
||||
Effect.gen(function* () {
|
||||
const credential = yield* resolveActive(connection)
|
||||
const response = yield* HttpClient.withScope(http).execute(
|
||||
HttpClientRequest.post(`${offer.baseURL}${path}`).pipe(
|
||||
HttpClientRequest.acceptJson,
|
||||
HttpClientRequest.setHeaders(credentialHeaders(credential)),
|
||||
HttpClientRequest.bodyJsonUnsafe(body),
|
||||
),
|
||||
)
|
||||
return { status: response.status, text: yield* response.text }
|
||||
}).pipe(
|
||||
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }),
|
||||
Effect.scoped,
|
||||
Effect.timeoutOrElse({
|
||||
duration: Duration.seconds(20),
|
||||
orElse: () => Effect.fail(new Error("OpenCode Console didn't answer in time")),
|
||||
}),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new Voice.UnavailableError({
|
||||
service: "opencode",
|
||||
message: cause instanceof Error ? cause.message : String(cause),
|
||||
}),
|
||||
),
|
||||
Effect.flatMap((reply): Effect.Effect<string, Voice.Error> => {
|
||||
if (reply.status >= 200 && reply.status < 300) return Effect.succeed(reply.text)
|
||||
if (reply.status === 401)
|
||||
return Effect.fail(new Voice.UnavailableError({ service: "opencode", message: `${signedOutMessage}.` }))
|
||||
const message =
|
||||
Option.getOrUndefined(Schema.decodeUnknownOption(InferenceError)(reply.text))?.error.message ??
|
||||
`OpenCode Console refused the voice request (HTTP ${reply.status})`
|
||||
return Effect.fail(
|
||||
reply.status === 400 || reply.status === 404
|
||||
? new Voice.InvalidRequestError({ message })
|
||||
: new Voice.UnavailableError({ service: "opencode", message }),
|
||||
)
|
||||
}),
|
||||
)
|
||||
return {
|
||||
integration: "opencode",
|
||||
models: offer.models,
|
||||
workspace: offer.workspace,
|
||||
session: (input) =>
|
||||
send("/live/sessions", { session: { ...input.session, model: input.model }, transport: input.transport }).pipe(
|
||||
Effect.flatMap((text) =>
|
||||
Schema.decodeUnknownEffect(LiveSession)(text).pipe(
|
||||
Effect.mapError(
|
||||
() =>
|
||||
new Voice.UnavailableError({
|
||||
service: "opencode",
|
||||
message: "OpenCode Console returned an invalid voice session",
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.map((created) => ({
|
||||
transport: { type: "webrtc" as const, sdp: created.transport.sdp },
|
||||
id: created.session.id,
|
||||
})),
|
||||
),
|
||||
instruct: (input) =>
|
||||
send(`/live/sessions/${encodeURIComponent(input.id)}/instructions`, {
|
||||
instructions: input.instructions,
|
||||
}).pipe(Effect.asVoid),
|
||||
}
|
||||
}
|
||||
|
||||
// Statements and hosted voice ride on the snapshot, so a credential switch, disconnect, or 404 replaces them too.
|
||||
// Statements ride on the snapshot, so a credential switch, disconnect, or 404 replaces them too.
|
||||
const publish = (next: typeof snapshot) =>
|
||||
Effect.all(
|
||||
[
|
||||
managed.set({
|
||||
statements: [...(next.config?.experimental?.policies ?? []), ...(next.config?.policies ?? [])],
|
||||
organization: next.organization,
|
||||
}),
|
||||
voice.host(
|
||||
next.connection && next.config?.voice ? consoleVoice(next.connection, next.config.voice) : undefined,
|
||||
),
|
||||
],
|
||||
{ discard: true },
|
||||
)
|
||||
managed.set({
|
||||
statements: [...(next.config?.experimental?.policies ?? []), ...(next.config?.policies ?? [])],
|
||||
organization: next.organization,
|
||||
})
|
||||
|
||||
yield* ctx.integration.transform((editor) => {
|
||||
editor.update("opencode", (integration) => {
|
||||
@@ -507,7 +399,18 @@ export const OpencodePlugin = define<
|
||||
name: "OpenCode Web Search",
|
||||
execute: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const credential = yield* resolveActive(connection)
|
||||
const active = yield* ctx.integration.connection.active("opencode")
|
||||
if (
|
||||
!active ||
|
||||
(connection.type === "credential"
|
||||
? active.type !== "credential" || active.id !== connection.id
|
||||
: active.type !== "env" || active.name !== connection.name)
|
||||
) {
|
||||
return yield* Effect.fail(new Error("OpenCode Console connection changed"))
|
||||
}
|
||||
const credential = yield* ctx.integration.connection.resolve(active)
|
||||
if (!credential || credential.type === "external")
|
||||
return yield* Effect.fail(new Error("OpenCode Console is not connected"))
|
||||
const metadata = credential.metadata
|
||||
const orgID = typeof metadata?.orgID === "string" ? metadata.orgID : undefined
|
||||
const token = credential.type === "oauth" ? credential.access : credential.key
|
||||
|
||||
+12
-72
@@ -19,10 +19,6 @@ export interface ModelInfo {
|
||||
readonly provider: string
|
||||
readonly transports: readonly RealtimeTransportType[]
|
||||
readonly available: boolean
|
||||
/** The integration whose connection starts its sessions: the provider's for a stored key, or a host's. */
|
||||
readonly integration?: string
|
||||
/** The workspace its sessions bill to, for display: set only when a host that names one runs it. */
|
||||
readonly workspace?: string
|
||||
}
|
||||
|
||||
export interface Models {
|
||||
@@ -36,30 +32,6 @@ export interface SessionInput {
|
||||
readonly transport: RealtimeTransportInput
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
readonly transport: RealtimeTransport
|
||||
/** A hosted session's ID, which `instruct` names it by. */
|
||||
readonly id?: string
|
||||
}
|
||||
|
||||
export interface InstructInput {
|
||||
readonly id: string
|
||||
readonly instructions: string
|
||||
}
|
||||
|
||||
/**
|
||||
* An integration that runs sessions for some models on its own account instead of a stored provider key, such as
|
||||
* the OpenCode Console's hosted GPT-Live. Its sessions' backend instructions change through it.
|
||||
*/
|
||||
export interface Host {
|
||||
readonly integration: string
|
||||
readonly models: readonly string[]
|
||||
/** The workspace its sessions bill to, for display. */
|
||||
readonly workspace?: string
|
||||
readonly session: (input: SessionInput) => Effect.Effect<Session, Error>
|
||||
readonly instruct: (input: InstructInput) => Effect.Effect<void, Error>
|
||||
}
|
||||
|
||||
export class InvalidRequestError extends Schema.TaggedError<InvalidRequestError>()("Voice.InvalidRequestError", {
|
||||
message: Schema.String,
|
||||
}) {}
|
||||
@@ -73,11 +45,7 @@ export type Error = InvalidRequestError | UnavailableError
|
||||
|
||||
export interface Interface {
|
||||
readonly models: () => Effect.Effect<Models>
|
||||
readonly session: (input: SessionInput) => Effect.Effect<Session, Error>
|
||||
/** Replaces the backend instructions of a running session the host started. */
|
||||
readonly instruct: (input: InstructInput) => Effect.Effect<void, Error>
|
||||
/** Sets the host whose models start on it instead of a stored key, or removes it. */
|
||||
readonly host: (host: Host | undefined) => Effect.Effect<void>
|
||||
readonly session: (input: SessionInput) => Effect.Effect<RealtimeTransport, Error>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/Voice") {}
|
||||
@@ -87,7 +55,6 @@ export const layer = Layer.effect(
|
||||
Effect.gen(function* () {
|
||||
const executor = yield* RequestExecutor.Service
|
||||
const integrations = yield* Integration.Service
|
||||
let hosted: Host | undefined
|
||||
|
||||
// Integration lists the active credential first, then newer stored credentials, then non-empty env vars.
|
||||
const findConnection = (provider: string) =>
|
||||
@@ -96,26 +63,15 @@ export const layer = Layer.effect(
|
||||
.pipe(Effect.map((info) => info?.connections.find((item) => item.type === "env" || item.method === "key")))
|
||||
|
||||
const models = Effect.fn("Voice.models")(function* () {
|
||||
const host = hosted
|
||||
const list = yield* Effect.forEach(Realtime.engines, (engine): Effect.Effect<ModelInfo> =>
|
||||
host?.models.includes(engine.model)
|
||||
? Effect.succeed({
|
||||
model: engine.model,
|
||||
provider: engine.provider,
|
||||
transports: engine.transports,
|
||||
available: true,
|
||||
integration: host.integration,
|
||||
...(host.workspace === undefined ? {} : { workspace: host.workspace }),
|
||||
})
|
||||
: findConnection(engine.provider).pipe(
|
||||
Effect.map((connection) => ({
|
||||
model: engine.model,
|
||||
provider: engine.provider,
|
||||
transports: engine.transports,
|
||||
available: Boolean(connection),
|
||||
...(connection ? { integration: engine.provider } : {}),
|
||||
})),
|
||||
),
|
||||
const list = yield* Effect.forEach(Realtime.engines, (engine) =>
|
||||
findConnection(engine.provider).pipe(
|
||||
Effect.map((connection) => ({
|
||||
model: engine.model,
|
||||
provider: engine.provider,
|
||||
transports: engine.transports,
|
||||
available: Boolean(connection),
|
||||
})),
|
||||
),
|
||||
)
|
||||
const first = list.find((item) => item.available)?.model
|
||||
return first ? { models: list, default: first } : { models: list }
|
||||
@@ -124,8 +80,6 @@ export const layer = Layer.effect(
|
||||
const session = Effect.fn("Voice.session")(function* (input: SessionInput) {
|
||||
const engine = Realtime.engine(input.model)
|
||||
if (!engine) return yield* new InvalidRequestError({ message: `Unsupported voice model: ${input.model}` })
|
||||
// A host offers a model on purpose, so it runs the model's sessions even where a stored key could.
|
||||
if (hosted?.models.includes(input.model)) return yield* hosted.session(input)
|
||||
|
||||
const connection = yield* findConnection(engine.provider)
|
||||
const credential = connection
|
||||
@@ -141,7 +95,7 @@ export const layer = Layer.effect(
|
||||
message: missingKeyMessage[engine.provider],
|
||||
})
|
||||
|
||||
const transport = yield* Realtime.connect({ ...input, apiKey: Redacted.make(credential.key) }).pipe(
|
||||
return yield* Realtime.connect({ ...input, apiKey: Redacted.make(credential.key) }).pipe(
|
||||
Effect.provideService(RequestExecutor.Service, executor),
|
||||
Effect.mapError(
|
||||
(error: AIError): Error =>
|
||||
@@ -150,23 +104,9 @@ export const layer = Layer.effect(
|
||||
: new UnavailableError({ message: error.message, service: engine.provider }),
|
||||
),
|
||||
)
|
||||
return { transport }
|
||||
})
|
||||
|
||||
const instruct = Effect.fn("Voice.instruct")(function* (input: InstructInput) {
|
||||
if (!hosted) return yield* new InvalidRequestError({ message: "Only hosted voice sessions take instructions" })
|
||||
return yield* hosted.instruct(input)
|
||||
})
|
||||
|
||||
return Service.of({
|
||||
models,
|
||||
session,
|
||||
instruct,
|
||||
host: (host) =>
|
||||
Effect.sync(() => {
|
||||
hosted = host
|
||||
}),
|
||||
})
|
||||
return Service.of({ models, session })
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@ import { SkillDiscovery } from "@opencode/core/skill/discovery"
|
||||
import { Watcher } from "@opencode/core/filesystem/watcher"
|
||||
import { Tool } from "@opencode/core/tool"
|
||||
import { Vcs } from "@opencode/core/vcs"
|
||||
import { Voice } from "@opencode/core/voice"
|
||||
import { WebSearch } from "@opencode/core/websearch"
|
||||
import { Worktree } from "@opencode/core/worktree"
|
||||
import { WorktreeStrategies } from "@opencode/core/worktree/strategies"
|
||||
@@ -108,7 +107,6 @@ export const makePluginTestLayer = (permission = permissionLayer) =>
|
||||
SkillDiscovery.node,
|
||||
Tool.node,
|
||||
Vcs.node,
|
||||
Voice.node,
|
||||
Watcher.node,
|
||||
WebSearch.node,
|
||||
Worktree.node,
|
||||
|
||||
@@ -22,7 +22,6 @@ import { PluginHost } from "@opencode/core/plugin/host"
|
||||
import { OpencodePlugin } from "@opencode/core/plugin/provider/opencode"
|
||||
import { Provider } from "@opencode/core/provider"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { Voice } from "@opencode/core/voice"
|
||||
import { WebSearch } from "@opencode/core/websearch"
|
||||
import { withEnv } from "../fixture/env"
|
||||
import { emptyMcp } from "../fixture/mcp"
|
||||
@@ -2029,150 +2028,6 @@ describe("OpencodePlugin", () => {
|
||||
)
|
||||
})
|
||||
|
||||
describe("OpencodePlugin hosted voice", () => {
|
||||
// A Console whose config offers hosted GPT-Live while `offered`, answering sessions with `reply`.
|
||||
const voiceConsole = (reply: () => Response) =>
|
||||
Effect.sync(() => {
|
||||
const state = { offered: true }
|
||||
const requests: { path: string; authorization: string | null; orgID: string | null; body: unknown }[] = []
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: async (request) => {
|
||||
const url = new URL(request.url)
|
||||
if (url.pathname === "/api/v2/config")
|
||||
return Response.json({
|
||||
providers: {},
|
||||
...(state.offered
|
||||
? {
|
||||
voice: {
|
||||
baseURL: `${url.origin}/inference/openai/v1`,
|
||||
models: ["gpt-live-1"],
|
||||
workspace: "Anomaly",
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
})
|
||||
requests.push({
|
||||
path: url.pathname,
|
||||
authorization: request.headers.get("authorization"),
|
||||
orgID: request.headers.get("x-org-id"),
|
||||
body: await request.json(),
|
||||
})
|
||||
if (url.pathname.endsWith("/instructions")) return new Response(null, { status: 204 })
|
||||
return reply()
|
||||
},
|
||||
})
|
||||
return { server, state, requests }
|
||||
})
|
||||
|
||||
const connect = (server: ReturnType<typeof Bun.serve>) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.OAuth.make({
|
||||
type: "oauth",
|
||||
methodID: Integration.MethodID.make("device"),
|
||||
access: "secret",
|
||||
refresh: "refresh",
|
||||
expires: Date.now() + 600_000,
|
||||
metadata: { server: server.url.origin, orgID: "org_test" },
|
||||
}),
|
||||
})
|
||||
yield* addPlugin().pipe(inProcess(server))
|
||||
})
|
||||
|
||||
const offer = {
|
||||
model: "gpt-live-1",
|
||||
session: { audio: { output: { voice: "marin" } } },
|
||||
transport: { type: "webrtc", sdp: "v=0" },
|
||||
} as const
|
||||
|
||||
it.effect("starts GPT-Live sessions on the Console with the connection's credential while it offers them", () =>
|
||||
Effect.acquireUseRelease(
|
||||
voiceConsole(() => Response.json({ session: { id: "sess_console" }, transport: { sdp: "answer:console" } })),
|
||||
({ server, state, requests }) =>
|
||||
Effect.gen(function* () {
|
||||
const voice = yield* Voice.Service
|
||||
yield* connect(server)
|
||||
const live = Effect.map(voice.models(), (list) => list.models.find((item) => item.model === "gpt-live-1"))
|
||||
|
||||
expect(yield* live).toMatchObject({ integration: "opencode", workspace: "Anomaly" })
|
||||
expect(yield* voice.session(offer)).toEqual({
|
||||
transport: { type: "webrtc", sdp: "answer:console" },
|
||||
id: "sess_console",
|
||||
})
|
||||
yield* voice.instruct({ id: "sess_console", instructions: "The screen shows ses_1." })
|
||||
expect(requests).toEqual([
|
||||
{
|
||||
path: "/inference/openai/v1/live/sessions",
|
||||
authorization: "Bearer secret",
|
||||
orgID: "org_test",
|
||||
body: {
|
||||
session: { audio: { output: { voice: "marin" } }, model: "gpt-live-1" },
|
||||
transport: { type: "webrtc", sdp: "v=0" },
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/inference/openai/v1/live/sessions/sess_console/instructions",
|
||||
authorization: "Bearer secret",
|
||||
orgID: "org_test",
|
||||
body: { instructions: "The screen shows ses_1." },
|
||||
},
|
||||
])
|
||||
|
||||
state.offered = false
|
||||
yield* TestClock.adjust("1 minute")
|
||||
yield* drain
|
||||
expect((yield* live)?.integration).not.toBe("opencode")
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
for (const item of [
|
||||
{
|
||||
name: "a refusal's reason",
|
||||
reply: () =>
|
||||
Response.json(
|
||||
{ error: { type: "permission_error", message: "GPT-Live isn't enabled for this workspace" } },
|
||||
{ status: 403 },
|
||||
),
|
||||
expected: {
|
||||
_tag: "Voice.UnavailableError",
|
||||
service: "opencode",
|
||||
message: "GPT-Live isn't enabled for this workspace",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "an invalid session as the caller's",
|
||||
reply: () =>
|
||||
Response.json(
|
||||
{ error: { type: "invalid_request_error", message: "Stored GPT-Live sessions aren't available" } },
|
||||
{ status: 400 },
|
||||
),
|
||||
expected: { _tag: "Voice.InvalidRequestError", message: "Stored GPT-Live sessions aren't available" },
|
||||
},
|
||||
{
|
||||
name: "a rejected credential as a reconnect",
|
||||
reply: () => Response.json({ error: { message: "Invalid credential" } }, { status: 401 }),
|
||||
expected: { _tag: "Voice.UnavailableError", message: "Reconnect OpenCode Console to continue." },
|
||||
},
|
||||
])
|
||||
it.effect(`reports ${item.name}`, () =>
|
||||
Effect.acquireUseRelease(
|
||||
voiceConsole(item.reply),
|
||||
({ server }) =>
|
||||
Effect.gen(function* () {
|
||||
const voice = yield* Voice.Service
|
||||
yield* connect(server)
|
||||
expect(yield* voice.session(offer).pipe(Effect.flip)).toMatchObject(item.expected)
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
for (const key of [undefined, "secret"]) {
|
||||
testEffect(Layer.empty).effect(
|
||||
`disabled free models with ${key ? "an API key" : "a Console login after startup"}`,
|
||||
|
||||
@@ -109,20 +109,16 @@ it.effect("uses the first key or env connection Integration lists and never a Ch
|
||||
const gemini = yield* call("gemini-3.8-live", [{ type: "env", name: "GEMINI_API_KEY" }])
|
||||
expect(auths).toEqual(["Bearer sk-active", "Bearer sk-stored", "Bearer xai-env-key", "gemini-env-key"])
|
||||
expect(grok).toEqual({
|
||||
transport: {
|
||||
type: "websocket",
|
||||
url: "wss://api.x.ai/v1/realtime?model=grok-voice-3.0",
|
||||
token: "ek_xai",
|
||||
expiresAt: 1760000999,
|
||||
},
|
||||
type: "websocket",
|
||||
url: "wss://api.x.ai/v1/realtime?model=grok-voice-3.0",
|
||||
token: "ek_xai",
|
||||
expiresAt: 1760000999,
|
||||
})
|
||||
expect(gemini).toEqual({
|
||||
transport: {
|
||||
type: "websocket",
|
||||
url: "wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained",
|
||||
token: "auth_tokens/ek_gemini",
|
||||
expiresAt: 1760000060,
|
||||
},
|
||||
type: "websocket",
|
||||
url: "wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained",
|
||||
token: "auth_tokens/ek_gemini",
|
||||
expiresAt: 1760000060,
|
||||
})
|
||||
|
||||
const signInOnly = yield* call("gpt-live-1", [credential("chatgpt", "oauth")]).pipe(Effect.flip)
|
||||
@@ -161,14 +157,8 @@ it.effect("lists voice models with availability and default without resolving cr
|
||||
models: [
|
||||
{ model: "gpt-live-1", provider: "openai", transports: ["webrtc"], available: false },
|
||||
{ model: "gpt-realtime-2.1", provider: "openai", transports: ["webrtc", "websocket"], available: false },
|
||||
{ model: "grok-voice-3.0", provider: "xai", transports: ["websocket"], available: true, integration: "xai" },
|
||||
{
|
||||
model: "gemini-3.8-live",
|
||||
provider: "google",
|
||||
transports: ["websocket"],
|
||||
available: true,
|
||||
integration: "google",
|
||||
},
|
||||
{ model: "grok-voice-3.0", provider: "xai", transports: ["websocket"], available: true },
|
||||
{ model: "gemini-3.8-live", provider: "google", transports: ["websocket"], available: true },
|
||||
],
|
||||
default: "grok-voice-3.0",
|
||||
})
|
||||
@@ -185,85 +175,3 @@ it.effect("lists voice models with availability and default without resolving cr
|
||||
expect("default" in none).toBe(false)
|
||||
}),
|
||||
)
|
||||
|
||||
// An OpenAI key the server could use instead, and the requests that reach OpenAI with it.
|
||||
const withStoredKey = (requests: string[]) =>
|
||||
Voice.layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.merge(
|
||||
mockIntegration(
|
||||
(id) => (id === "openai" ? [credential("stored", "key")] : []),
|
||||
() => Effect.succeed(Credential.Key.make({ type: "key", key: "sk-stored" })),
|
||||
),
|
||||
RequestExecutor.layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.succeed(
|
||||
HttpClient.HttpClient,
|
||||
HttpClient.make((req: HttpClientRequest.HttpClientRequest) =>
|
||||
Effect.sync(() => {
|
||||
requests.push(req.headers.authorization ?? "")
|
||||
return HttpClientResponse.fromWeb(req, Response.json({ transport: { sdp: "answer:openai" } }))
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
const hostedLive = (instructed: Voice.InstructInput[]): Voice.Host => ({
|
||||
integration: "opencode",
|
||||
models: ["gpt-live-1"],
|
||||
workspace: "Anomaly",
|
||||
session: (input) =>
|
||||
Effect.succeed({
|
||||
transport: { type: "webrtc", sdp: input.transport.type === "webrtc" ? `answer:hosted:${input.transport.sdp}` : "" },
|
||||
id: "sess_hosted",
|
||||
}),
|
||||
instruct: (input) =>
|
||||
Effect.sync(() => {
|
||||
instructed.push(input)
|
||||
}),
|
||||
})
|
||||
|
||||
const offer = { model: "gpt-live-1", transport: { type: "webrtc", sdp: "v=0" } } as const
|
||||
|
||||
it.effect("starts a host's models and relays their instructions on it, even beside a stored key", () => {
|
||||
const requests: string[] = []
|
||||
return Effect.gen(function* () {
|
||||
const voice = yield* Voice.Service
|
||||
const instructed: Voice.InstructInput[] = []
|
||||
yield* voice.host(hostedLive(instructed))
|
||||
|
||||
expect(yield* voice.session(offer)).toEqual({
|
||||
transport: { type: "webrtc", sdp: "answer:hosted:v=0" },
|
||||
id: "sess_hosted",
|
||||
})
|
||||
expect((yield* voice.models()).models.find((item) => item.model === "gpt-live-1")).toEqual({
|
||||
model: "gpt-live-1",
|
||||
provider: "openai",
|
||||
transports: ["webrtc"],
|
||||
available: true,
|
||||
integration: "opencode",
|
||||
workspace: "Anomaly",
|
||||
})
|
||||
yield* voice.instruct({ id: "sess_hosted", instructions: "The screen shows ses_1." })
|
||||
expect(instructed).toEqual([{ id: "sess_hosted", instructions: "The screen shows ses_1." }])
|
||||
expect(requests).toEqual([])
|
||||
}).pipe(Effect.provide(withStoredKey(requests)))
|
||||
})
|
||||
|
||||
it.effect("goes back to the stored key once the host is removed, and refuses instructions", () => {
|
||||
const requests: string[] = []
|
||||
return Effect.gen(function* () {
|
||||
const voice = yield* Voice.Service
|
||||
yield* voice.host(hostedLive([]))
|
||||
yield* voice.host(undefined)
|
||||
|
||||
expect(yield* voice.session(offer)).toEqual({ transport: { type: "webrtc", sdp: "answer:openai" } })
|
||||
expect(requests).toEqual(["Bearer sk-stored"])
|
||||
const refused = yield* voice.instruct({ id: "sess_hosted", instructions: "Hello" }).pipe(Effect.flip)
|
||||
expect(refused._tag).toBe("Voice.InvalidRequestError")
|
||||
}).pipe(Effect.provide(withStoredKey(requests)))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user