|
| 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 | +} |
0 commit comments