Skip to content

Commit d2905b8

Browse files
authored
feat(ai): add Vertex AI Interactions route (#54175)
1 parent 229b422 commit d2905b8

10 files changed

Lines changed: 382 additions & 1 deletion

File tree

‎packages/ai/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployme
3232
`Google.configure({ apiKey }).interactions(modelID)` selects the Interactions API; `.model(modelID)` still selects
3333
GenerateContent. The package entrypoint is `@opencode/ai/providers/google/interactions`.
3434

35+
For Vertex AI, use `GoogleVertexInteractions.configure({ project, location: "global" }).model(modelID)` or
36+
`@opencode/ai/providers/google-vertex/interactions`. It uses Application Default Credentials by default, accepts
37+
an explicit `accessToken` or `auth` override, and supports Express mode with `apiKey`. Vertex metadata and raw usage
38+
live under `providerMetadata.vertex`. The default is still full-history replay with `store: false`.
39+
3540
```ts
3641
const model = Google.configure({ apiKey }).interactions("gemini-3.8-flash")
3742
const response = yield* LLM.generate({
@@ -1316,13 +1321,14 @@ APIs have separate entrypoints:
13161321
- `@opencode/ai/providers/openai-compatible/responses`
13171322
- `@opencode/ai/providers/anthropic-compatible`
13181323
- `@opencode/ai/providers/google-vertex/gemini`
1324+
- `@opencode/ai/providers/google-vertex/interactions`
13191325
- `@opencode/ai/providers/google-vertex/chat`
13201326
- `@opencode/ai/providers/google-vertex/responses`
13211327
- `@opencode/ai/providers/google-vertex/messages`
13221328

13231329
OpenAI Responses has one semantic route and uses HTTP by default. Advanced callers may supply a per-call WebSocket channel executor through `StreamOptions`; transport policy does not change provider settings, model identity, or route identity. The provider-neutral Open Responses implementation owns the reusable WebSocket request and event contract, while each provider opts in with its own handshake and connection policy. Azure follows the same Chat/Responses split at `providers/azure/chat` and `providers/azure/responses`. Generic OpenAI-compatible Chat remains at `providers/openai-compatible`; the Responses adapter at `providers/openai-compatible/responses` uses the provider-neutral Open Responses protocol. OpenAI Responses extends that baseline with OpenAI tools, event variants, metadata, and defaults. Generic Anthropic Messages-compatible providers use `providers/anthropic-compatible`, which the named Anthropic provider composes. Google Gemini and Amazon Bedrock expose their single native API through their existing provider paths.
13241330

1325-
Vertex Gemini, Vertex Chat, Vertex Responses, and Vertex Messages are separate API entrypoints. All accept `project`, `location`, and an optional `accessToken`; when no explicit token or auth override is supplied they lazily use Google Application Default Credentials. Vertex Gemini instead selects express mode when `apiKey` or `GOOGLE_VERTEX_API_KEY` is present. Vertex Chat targets MaaS models through the OpenAI-compatible Chat Completions endpoint, while Vertex Responses targets Grok models and defaults `store` to `false` as required by Vertex. `providers/google-vertex` remains the default alias for `providers/google-vertex/gemini`.
1331+
Vertex Gemini, Vertex Interactions, Vertex Chat, Vertex Responses, and Vertex Messages are separate API entrypoints. All accept `project`, `location`, and an optional `accessToken`; when no explicit token or auth override is supplied they lazily use Google Application Default Credentials. Vertex Gemini and Interactions instead select express mode when `apiKey` or `GOOGLE_VERTEX_API_KEY` is present (explicit `project` or `location` selects ADC over an ambient key). Vertex Chat targets MaaS models through the OpenAI-compatible Chat Completions endpoint, while Vertex Responses targets Grok models and defaults `store` to `false` as required by Vertex. `providers/google-vertex` remains the default alias for `providers/google-vertex/gemini`.
13261332

13271333
Tuned Vertex Gemini deployments use model ids shaped like `endpoints/1234567890` and require OAuth or ADC; Vertex express-mode API keys support publisher models only.
13281334

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import type { ProviderPackage } from "../provider-package.js"
2+
import { GoogleInteractions } from "../protocols/google-interactions.js"
3+
import { Auth } from "../route/auth.js"
4+
import { Route, type RouteDefaultsInput } from "../route/client.js"
5+
import { Endpoint } from "../route/endpoint.js"
6+
import { Framing } from "../route/framing.js"
7+
import { ProviderConfigurationError, ProviderID, type ModelID } from "../schema/index.js"
8+
import { GoogleVertexShared } from "./google-vertex-shared.js"
9+
10+
export type GoogleInteractionsOptionsInput = GoogleInteractions.OptionsInput
11+
12+
export const id = ProviderID.make("google-vertex")
13+
14+
export type Config = RouteDefaultsInput &
15+
GoogleVertexShared.ApiKeyOptions & {
16+
readonly baseURL?: string
17+
readonly location?: string
18+
readonly project?: string
19+
readonly providerOptions?: GoogleInteractions.ProviderOptionsInput
20+
}
21+
22+
export type Settings = ProviderPackage.Settings &
23+
GoogleInteractions.ProviderOptionsInput &
24+
(
25+
| { readonly accessToken?: string; readonly apiKey?: never }
26+
| { readonly accessToken?: never; readonly apiKey?: string }
27+
) & {
28+
readonly baseURL?: string
29+
readonly location?: string
30+
readonly project?: string
31+
}
32+
33+
const route = Route.make({
34+
id: "google-vertex-interactions",
35+
provider: id,
36+
providerMetadataKey: "vertex",
37+
protocol: GoogleInteractions.protocol,
38+
endpoint: Endpoint.path("/interactions", { query: { alt: "sse" } }),
39+
auth: Auth.none,
40+
headers: ({ request }): Record<string, string> => {
41+
const serviceTier = request.providerOptions?.serviceTier
42+
return typeof serviceTier === "string" ? { "x-vertex-ai-llm-shared-request-type": serviceTier } : {}
43+
},
44+
framing: Framing.sse,
45+
})
46+
47+
export const routes = [route]
48+
49+
const configuredRoute = (input: Config) => {
50+
const {
51+
accessToken: _accessToken,
52+
apiKey: _apiKey,
53+
auth: _auth,
54+
baseURL,
55+
location: inputLocation,
56+
project: inputProject,
57+
...rest
58+
} = input
59+
const apiKey = GoogleVertexShared.apiKey(input)
60+
const location = GoogleVertexShared.location(inputLocation, "global")
61+
const project = GoogleVertexShared.project(inputProject)
62+
return route.with({
63+
...rest,
64+
endpoint: {
65+
baseURL:
66+
baseURL ??
67+
`https://${GoogleVertexShared.host(location)}/v1beta1/${apiKey === undefined ? `projects/${GoogleVertexShared.requireProject(project)}/` : ""}locations/${location}`,
68+
},
69+
auth: apiKey === undefined ? GoogleVertexShared.oauth(input, project) : Auth.header("x-goog-api-key", apiKey),
70+
})
71+
}
72+
73+
export const configure = (input: Config = {}) => {
74+
const route = configuredRoute(input)
75+
return {
76+
id,
77+
model: (modelID: string | ModelID) => route.model<GoogleInteractions.ProviderOptionsInput>({ id: modelID }),
78+
configure,
79+
}
80+
}
81+
82+
export const provider = {
83+
id,
84+
configure,
85+
}
86+
87+
export const model: ProviderPackage.Definition<Settings, GoogleInteractions.ProviderOptionsInput>["model"] = (
88+
modelID,
89+
{ accessToken, apiKey, baseURL, body, headers, location, project, ...providerOptions },
90+
) => {
91+
if (apiKey !== undefined && accessToken !== undefined)
92+
throw new ProviderConfigurationError({
93+
provider: id,
94+
message: "Google Vertex apiKey cannot be combined with accessToken or auth",
95+
})
96+
return configure({
97+
...(apiKey === undefined ? { accessToken } : { apiKey }),
98+
baseURL,
99+
headers,
100+
http: body === undefined ? undefined : { body },
101+
location,
102+
project,
103+
providerOptions,
104+
}).model(modelID)
105+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { model } from "../google-vertex-interactions.js"
2+
export type { Settings } from "../google-vertex-interactions.js"

‎packages/ai/src/providers/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export * as Fireworks from "./fireworks.js"
2222
export * as Google from "./google.js"
2323
export * as GoogleVertex from "./google-vertex.js"
2424
export * as GoogleVertexChat from "./google-vertex-chat.js"
25+
export * as GoogleVertexInteractions from "./google-vertex-interactions.js"
2526
export * as GoogleVertexMessages from "./google-vertex-messages.js"
2627
export * as GoogleVertexMistral from "./google-vertex-mistral.js"
2728
export * as GoogleVertexResponses from "./google-vertex-responses.js"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { LLM } from "../../src/index.js"
2+
import { GoogleVertexInteractions } from "../../src/providers.js"
3+
import { Auth } from "../../src/route/auth.js"
4+
import { model } from "../../src/providers/google-vertex/interactions.js"
5+
6+
const selected = GoogleVertexInteractions.configure({ project: "project" }).model("gemini-3.8-flash")
7+
8+
LLM.request({
9+
model: selected,
10+
prompt: "Hello",
11+
providerOptions: { thinkingLevel: "low", thinkingSummaries: "auto", serviceTier: "flex", store: false },
12+
})
13+
14+
LLM.request({
15+
model: selected,
16+
prompt: "Hello",
17+
// @ts-expect-error Interactions store must be boolean.
18+
providerOptions: { store: "yes" },
19+
})
20+
21+
GoogleVertexInteractions.configure({ auth: Auth.bearer("token"), project: "project" })
22+
GoogleVertexInteractions.configure({ apiKey: "key" })
23+
// @ts-expect-error API keys and OAuth credentials are mutually exclusive.
24+
GoogleVertexInteractions.configure({ apiKey: "key", accessToken: "token" })
25+
// @ts-expect-error API keys and auth overrides are mutually exclusive.
26+
GoogleVertexInteractions.configure({ apiKey: "key", auth: Auth.bearer("token") })
27+
model("gemini-3.8-flash", { project: "project", thinkingLevel: "low" })
28+
// @ts-expect-error Package settings also reject conflicting credentials.
29+
model("gemini-3.8-flash", { apiKey: "key", accessToken: "token" })

‎packages/ai/test/provider-package.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,29 @@ describe("provider package entrypoints", () => {
451451
})
452452
})
453453

454+
test("maps Vertex Interactions package settings onto the shared protocol", async () => {
455+
const GoogleVertexInteractions = await import("@opencode/ai/providers/google-vertex/interactions")
456+
const selected = GoogleVertexInteractions.model("gemini-3.8-flash", {
457+
accessToken: "fixture",
458+
project: "vertex-project",
459+
location: "global",
460+
headers: { "x-application": "opencode" },
461+
body: { generation_config: { temperature: 0.5 } },
462+
thinkingLevel: "low",
463+
store: false,
464+
})
465+
expect(selected.route.id).toBe("google-vertex-interactions")
466+
expect(selected.route.protocol).toBe("google-interactions")
467+
expect(selected.route.endpoint).toMatchObject({
468+
baseURL: "https://aiplatform.googleapis.com/v1beta1/projects/vertex-project/locations/global",
469+
path: "/interactions",
470+
query: { alt: "sse" },
471+
})
472+
expect(selected.route.defaults.headers).toEqual({ "x-application": "opencode" })
473+
expect(selected.route.defaults.http?.body).toEqual({ generation_config: { temperature: 0.5 } })
474+
expect(selected.route.defaults.providerOptions).toEqual({ thinkingLevel: "low", store: false })
475+
})
476+
454477
test("rejects conflicting Vertex auth settings at runtime", async () => {
455478
const GoogleVertex = await import("@opencode/ai/providers/google-vertex")
456479
const GoogleVertexChat = await import("@opencode/ai/providers/google-vertex/chat")

0 commit comments

Comments
 (0)