mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(ai): add Google Decisions (#63369)
This commit is contained in:
committed by
GitHub
Unverified
parent
532d099c2d
commit
0bd8cee7c2
@@ -77,6 +77,7 @@
|
||||
"./providers/google-vertex/messages": "./src/providers/google-vertex/messages.ts",
|
||||
"./providers/google-vertex/mistral": "./src/providers/google-vertex/mistral.ts",
|
||||
"./providers/google-vertex/responses": "./src/providers/google-vertex/responses.ts",
|
||||
"./providers/google/decisions": "./src/providers/google/decisions.ts",
|
||||
"./providers/google/embeddings": "./src/providers/google/embeddings.ts",
|
||||
"./providers/google/images": "./src/providers/google/images.ts",
|
||||
"./providers/google/interactions": "./src/providers/google/interactions.ts",
|
||||
@@ -141,6 +142,7 @@
|
||||
"./protocols/cloudflare-run-decisions": "./src/protocols/cloudflare-run-decisions.ts",
|
||||
"./protocols/cloudflare-workers-ai-images": "./src/protocols/cloudflare-workers-ai-images.ts",
|
||||
"./protocols/deepgram-transcription": "./src/protocols/deepgram-transcription.ts",
|
||||
"./protocols/google-decisions": "./src/protocols/google-decisions.ts",
|
||||
"./protocols/google-images": "./src/protocols/google-images.ts",
|
||||
"./protocols/google-speech": "./src/protocols/google-speech.ts",
|
||||
"./protocols/google-transcription": "./src/protocols/google-transcription.ts",
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
import { Effect, Schema } from "effect"
|
||||
import { DecisionResponse, DecisionUsage, type DecisionPart, type DecisionRequest } from "../decision.js"
|
||||
import type { Media } from "../media.js"
|
||||
import { DecisionRoute } from "../route/decision.js"
|
||||
import { Endpoint } from "../route/endpoint.js"
|
||||
import { MediaProtocol } from "../route/media-protocol.js"
|
||||
import {
|
||||
AIError,
|
||||
ProviderID,
|
||||
UnsupportedOperationError,
|
||||
mergeJsonRecords,
|
||||
type ProviderOptions,
|
||||
} from "../schema/index.js"
|
||||
import { encodeJson } from "../utils/json.js"
|
||||
import { JsonObject, knownString, ProviderShared } from "./shared.js"
|
||||
import { SystemOne } from "./system-one.js"
|
||||
import { MediaInput } from "./utils/media-input.js"
|
||||
|
||||
const ID = "google-decisions"
|
||||
const NAME = "Google Decisions"
|
||||
export const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta"
|
||||
|
||||
const Options = Schema.Struct({
|
||||
/** Token budget for every image and video input; `ultra_high` applies to images only. */
|
||||
mediaResolution: Schema.optional(knownString<"low" | "medium" | "high" | "ultra_high">()),
|
||||
})
|
||||
/** Known fields map to their wire names; anything else is sent as given. */
|
||||
export type OptionsInput = ProviderOptions & typeof Options.Encoded
|
||||
|
||||
const Usage = Schema.StructWithRest(
|
||||
Schema.Struct({
|
||||
total_input_tokens: Schema.optional(Schema.Number),
|
||||
total_output_tokens: Schema.optional(Schema.Number),
|
||||
total_tokens: Schema.optional(Schema.Number),
|
||||
}),
|
||||
[JsonObject],
|
||||
)
|
||||
const Response = Schema.Struct({
|
||||
answers: Schema.Record(Schema.String, SystemOne.Answer),
|
||||
usage: Schema.optional(Usage),
|
||||
})
|
||||
|
||||
const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options))
|
||||
|
||||
const fromRequest = Effect.fn("GoogleDecisions.fromRequest")(function* (request: DecisionRequest) {
|
||||
const options = yield* decodeOptions(request.providerOptions ?? {})
|
||||
const { mediaResolution: _, ...native } = request.providerOptions ?? {}
|
||||
return {
|
||||
...mergeJsonRecords(native, request.http?.body),
|
||||
model: request.model.id,
|
||||
input: separateText(
|
||||
yield* Effect.forEach(request.input, (part) => inputPart(request, part, options.mediaResolution)),
|
||||
),
|
||||
questions: {
|
||||
...(yield* SystemOne.encodeQuestions(request.questions)),
|
||||
// Google names System One's predicate criteria `true_description` and `false_description`.
|
||||
...Object.fromEntries(
|
||||
request.questions.flatMap((question) =>
|
||||
question.type === "predicate" && question.criteria !== undefined
|
||||
? [
|
||||
[
|
||||
question.name,
|
||||
{
|
||||
type: "noul",
|
||||
instructions: question.instructions,
|
||||
criteria: {
|
||||
...(question.criteria.true === undefined ? {} : { true_description: question.criteria.true }),
|
||||
...(question.criteria.false === undefined ? {} : { false_description: question.criteria.false }),
|
||||
},
|
||||
},
|
||||
],
|
||||
]
|
||||
: [],
|
||||
),
|
||||
),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const inputPart = (
|
||||
request: DecisionRequest,
|
||||
part: DecisionPart,
|
||||
resolution: string | undefined,
|
||||
): Effect.Effect<Record<string, unknown>, AIError> => {
|
||||
if (part.type === "text") return Effect.succeed({ type: "text", text: part.text })
|
||||
// Google reads every text block as text; JSON is serialized so its keys are never parsed as content blocks.
|
||||
if (part.type === "json") return Effect.succeed({ type: "text", text: encodeJson(part.value) })
|
||||
return mediaPart(request, part.media, resolution)
|
||||
}
|
||||
|
||||
// Google joins adjacent text blocks with no separator, so each one after another starts on a new line.
|
||||
const separateText = (blocks: ReadonlyArray<Record<string, unknown>>) =>
|
||||
blocks.map((block, index) =>
|
||||
block.type === "text" && blocks[index - 1]?.type === "text" ? { ...block, text: `\n${block.text}` } : block,
|
||||
)
|
||||
|
||||
const mediaPart = (request: DecisionRequest, asset: Media.Asset, resolution: string | undefined) => {
|
||||
const type =
|
||||
asset.kind === "image" || asset.kind === "audio" || asset.kind === "video"
|
||||
? asset.kind
|
||||
: asset.mediaType.toLowerCase() === "application/pdf"
|
||||
? "document"
|
||||
: undefined
|
||||
if (type === undefined)
|
||||
return Effect.fail(
|
||||
new AIError({
|
||||
reason: new UnsupportedOperationError({
|
||||
operation: "decision.media",
|
||||
provider: request.model.provider,
|
||||
route: request.model.route.id,
|
||||
message: `${NAME} takes images, audio, video and PDFs; got ${asset.mediaType}`,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
const block = {
|
||||
type,
|
||||
mime_type: asset.mediaType.toLowerCase(),
|
||||
// `ultra_high` is image-only, so video keeps Google's default for it.
|
||||
...(resolution !== undefined && (type === "image" || (type === "video" && resolution !== "ultra_high"))
|
||||
? { resolution }
|
||||
: {}),
|
||||
}
|
||||
const inline = asset.inline()
|
||||
if (inline) return Effect.succeed({ ...block, data: inline.base64 })
|
||||
// Google fetches URLs itself, so one that needs our download credentials can't be forwarded.
|
||||
if (asset.source.type === "url" && /^https:\/\//i.test(asset.source.url) && asset.headers === undefined)
|
||||
return Effect.succeed({ ...block, uri: asset.source.url })
|
||||
const file = MediaInput.refID(asset, ProviderID.make("google"))
|
||||
if (file !== undefined) return Effect.succeed({ ...block, uri: file })
|
||||
return Effect.fail(
|
||||
ProviderShared.invalidRequest(
|
||||
`${NAME} accepts inline media, public HTTPS URLs and Google Files API URIs; materialize ${asset.source.type} media first`,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const decodeDocument = MediaProtocol.identity({ id: ID, name: NAME, provider: "google" }).decodeJson(Response)
|
||||
|
||||
export const protocol = DecisionRoute.protocol({
|
||||
id: ID,
|
||||
name: NAME,
|
||||
body: { from: fromRequest },
|
||||
response: {
|
||||
decode: Effect.fn("GoogleDecisions.decodeResponse")(function* (response, context) {
|
||||
const output = yield* decodeDocument(response)
|
||||
const data = output.value
|
||||
const answers = data.answers
|
||||
const names = context.request.questions.map((question) => question.name)
|
||||
if (Object.keys(answers).length !== names.length || !names.every((name) => Object.hasOwn(answers, name)))
|
||||
return yield* output.invalid(`${NAME} answers do not match the requested questions`)
|
||||
return new DecisionResponse({
|
||||
answers: context.request.questions.map((question) => SystemOne.toAnswer(question, answers[question.name])),
|
||||
usage:
|
||||
data.usage === undefined
|
||||
? undefined
|
||||
: new DecisionUsage({
|
||||
inputTokens: data.usage.total_input_tokens,
|
||||
outputTokens: data.usage.total_output_tokens,
|
||||
totalTokens: ProviderShared.totalTokens(
|
||||
data.usage.total_input_tokens,
|
||||
data.usage.total_output_tokens,
|
||||
data.usage.total_tokens,
|
||||
),
|
||||
}),
|
||||
providerMetadata:
|
||||
data.usage === undefined ? undefined : { [context.providerMetadataKey]: { usage: data.usage } },
|
||||
})
|
||||
}),
|
||||
},
|
||||
})
|
||||
|
||||
export const route = DecisionRoute.make({
|
||||
id: ID,
|
||||
provider: "google",
|
||||
protocol,
|
||||
endpoint: Endpoint.path("/decisions", { baseURL: DEFAULT_BASE_URL }),
|
||||
})
|
||||
|
||||
export * as GoogleDecisions from "./google-decisions.js"
|
||||
@@ -3,6 +3,7 @@ export * as BedrockConverse from "./bedrock-converse.js"
|
||||
export * as CloudflareRunDecisions from "./cloudflare-run-decisions.js"
|
||||
export * as CohereChat from "./cohere-chat.js"
|
||||
export * as Gemini from "./gemini.js"
|
||||
export * as GoogleDecisions from "./google-decisions.js"
|
||||
export * as GoogleEmbedContent from "./google-embed-content.js"
|
||||
export * as GoogleInteractions from "./google-interactions.js"
|
||||
export * as GoogleVertexPredictEmbeddings from "./google-vertex-predict-embeddings.js"
|
||||
|
||||
@@ -94,7 +94,7 @@ const encodeQuestions = (questions: ReadonlyArray<DecisionQuestion>) =>
|
||||
if (new Set(keys).size !== keys.length)
|
||||
return Effect.fail(
|
||||
ProviderShared.invalidRequest(
|
||||
`System One choice options are strings; "${question.name}" has a boolean and a string with the same text`,
|
||||
`Decision choice options are sent as string keys; "${question.name}" has a boolean and a string with the same text`,
|
||||
),
|
||||
)
|
||||
return Effect.succeed([
|
||||
@@ -294,4 +294,13 @@ export const route = DecisionRoute.make({
|
||||
endpoint: Endpoint.path("/systemone", { baseURL: DEFAULT_BASE_URL }),
|
||||
})
|
||||
|
||||
export const SystemOne = { Response, encodeBody, toResponse, protocol, route } as const
|
||||
export const SystemOne = {
|
||||
Answer,
|
||||
Response,
|
||||
encodeBody,
|
||||
encodeQuestions,
|
||||
toAnswer,
|
||||
toResponse,
|
||||
protocol,
|
||||
route,
|
||||
} as const
|
||||
|
||||
@@ -131,6 +131,12 @@ const CONTENT_POLICY_CODES = new Set([
|
||||
"refusal",
|
||||
"datainspectionfailed",
|
||||
"moderation_blocked",
|
||||
// Gemini Decisions rejects blocked input with these `error.code` strings on a 400.
|
||||
"safety",
|
||||
"prohibited_content",
|
||||
"blocklist",
|
||||
"image_safety",
|
||||
"content_blocked",
|
||||
])
|
||||
// OpenCode Zen replaces upstream codes outside its allow-list but keeps the original
|
||||
// as a `[code]` label at the start of the rewritten message.
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
GoogleEmbedContent,
|
||||
type OptionsInput as GoogleEmbeddingOptionsInput,
|
||||
} from "../protocols/google-embed-content.js"
|
||||
import { GoogleDecisions } from "../protocols/google-decisions.js"
|
||||
import { GoogleInteractions } from "../protocols/google-interactions.js"
|
||||
import { GoogleImages } from "../protocols/google-images.js"
|
||||
import { GoogleSpeech } from "../protocols/google-speech.js"
|
||||
@@ -23,6 +24,7 @@ export type { GoogleVideoOptions } from "../protocols/google-video.js"
|
||||
export type GeminiOptionsInput = Gemini.OptionsInput
|
||||
export type GeminiProviderOptionsInput = Gemini.ProviderOptionsInput
|
||||
export type GoogleInteractionsOptionsInput = GoogleInteractions.OptionsInput
|
||||
export type GoogleDecisionsOptionsInput = GoogleDecisions.OptionsInput
|
||||
|
||||
export const id = ProviderID.make("google")
|
||||
|
||||
@@ -77,6 +79,10 @@ export const configure = (input: Config = {}) => {
|
||||
transcription: (modelID: string | ModelID) => GoogleTranscription.model({ ...media, id: modelID }),
|
||||
embedding: (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
embeddingRoute.model<GoogleEmbeddingOptionsInput>({ id: modelID, providerOptions }),
|
||||
decision: (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
GoogleDecisions.route
|
||||
.with({ endpoint: { baseURL: media.baseURL }, auth: media.auth, headers: media.headers, http: media.http })
|
||||
.model<GoogleDecisions.OptionsInput>({ id: modelID, providerOptions }),
|
||||
configure,
|
||||
}
|
||||
}
|
||||
@@ -92,6 +98,10 @@ export const embeddingModel = (modelID: string, settings?: Settings) => {
|
||||
const input = toConfig(settings)
|
||||
return configure(input).embedding(modelID, input.providerOptions)
|
||||
}
|
||||
export const decisionModel = (modelID: string, settings?: Settings) => {
|
||||
const input = toConfig(settings)
|
||||
return configure(input).decision(modelID, input.providerOptions)
|
||||
}
|
||||
|
||||
export const image = provider.image
|
||||
export const embedding = provider.embedding
|
||||
@@ -100,3 +110,4 @@ export const interactions = provider.interactions
|
||||
export const video = provider.video
|
||||
export const speech = provider.speech
|
||||
export const transcription = provider.transcription
|
||||
export const decision = provider.decision
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export { decisionModel as model } from "../google.js"
|
||||
export type { Settings } from "../google.js"
|
||||
@@ -5,6 +5,7 @@ import { ai, DecisionClient, DecisionUsage, Media } from "../src/effect.js"
|
||||
import {
|
||||
Baseten,
|
||||
CloudflareWorkersAI,
|
||||
Google,
|
||||
OpenAI,
|
||||
OpenCode,
|
||||
OpenRouter,
|
||||
@@ -487,6 +488,179 @@ describe("Decision", () => {
|
||||
}).pipe(Effect.provide(offline)),
|
||||
)
|
||||
|
||||
it.effect("decides through Google Decisions with every media kind", () =>
|
||||
ai.decision
|
||||
.generate({
|
||||
model: Google.configure({
|
||||
apiKey: "google-key",
|
||||
baseURL: "https://google.test/v1beta",
|
||||
http: { query: { trace: "1" } },
|
||||
}).decision("gemini-decision-1"),
|
||||
input: [
|
||||
{ type: "text", text: "Ticket:" },
|
||||
{ type: "json", value: { ticket: "Please refund the duplicate charge." } },
|
||||
{ type: "media", media: png },
|
||||
{ type: "media", media: Media.base64("AAAA", "audio/wav") },
|
||||
{ type: "media", media: Media.url("https://assets.test/clip.mp4", { mediaType: "video/mp4" }) },
|
||||
{ type: "media", media: Media.base64("JVBERi0=", "application/pdf") },
|
||||
{ type: "media", media: Media.ref("google", "https://google.test/v1beta/files/abc", "image/png") },
|
||||
],
|
||||
questions: [
|
||||
...questions.slice(0, 2),
|
||||
{ ...questions[2], criteria: { true: "They want money back.", false: "Anything else." } },
|
||||
],
|
||||
providerOptions: { mediaResolution: "low", labels: { team: "support" } },
|
||||
})
|
||||
.pipe(
|
||||
Effect.tap((response) =>
|
||||
Effect.sync(() => {
|
||||
expect(response.answers).toEqual(expectedAnswers)
|
||||
expect(response.usage).toEqual(new DecisionUsage({ inputTokens: 147, outputTokens: 0, totalTokens: 147 }))
|
||||
expect(response.providerMetadata?.google?.usage).toEqual({
|
||||
total_input_tokens: 147,
|
||||
input_tokens_by_modality: [{ modality: "text", tokens: 147 }],
|
||||
total_output_tokens: 0,
|
||||
total_tokens: 147,
|
||||
})
|
||||
}),
|
||||
),
|
||||
Effect.provide(
|
||||
client((input) => {
|
||||
expect(input.request.url).toBe("https://google.test/v1beta/decisions?trace=1")
|
||||
expect(input.request.headers["x-goog-api-key"]).toBe("google-key")
|
||||
expect(JSON.parse(input.text)).toEqual({
|
||||
labels: { team: "support" },
|
||||
model: "gemini-decision-1",
|
||||
input: [
|
||||
{ type: "text", text: "Ticket:" },
|
||||
{ type: "text", text: '\n{"ticket":"Please refund the duplicate charge."}' },
|
||||
{ type: "image", mime_type: "image/png", resolution: "low", data: "AQID" },
|
||||
{ type: "audio", mime_type: "audio/wav", data: "AAAA" },
|
||||
{ type: "video", mime_type: "video/mp4", resolution: "low", uri: "https://assets.test/clip.mp4" },
|
||||
{ type: "document", mime_type: "application/pdf", data: "JVBERi0=" },
|
||||
{
|
||||
type: "image",
|
||||
mime_type: "image/png",
|
||||
resolution: "low",
|
||||
uri: "https://google.test/v1beta/files/abc",
|
||||
},
|
||||
],
|
||||
questions: {
|
||||
department: {
|
||||
type: "choice",
|
||||
instructions: "Which team should handle this?",
|
||||
criteria: { billing: "Payments and refunds", technical: null },
|
||||
},
|
||||
urgency: {
|
||||
type: "score",
|
||||
instructions: "How urgent is this?",
|
||||
criteria: ["Can wait", "Needs attention: Same day", "Blocking"],
|
||||
},
|
||||
refund: {
|
||||
type: "noul",
|
||||
instructions: "Is the customer asking for a refund?",
|
||||
criteria: { true_description: "They want money back.", false_description: "Anything else." },
|
||||
},
|
||||
},
|
||||
})
|
||||
return Effect.succeed(
|
||||
json(input, {
|
||||
answers: { ...systemOneAnswers, refund: { type: "noul", noul: 0.97, confidence: 0.97 } },
|
||||
usage: {
|
||||
total_input_tokens: 147,
|
||||
input_tokens_by_modality: [{ modality: "text", tokens: 147 }],
|
||||
total_output_tokens: 0,
|
||||
total_tokens: 147,
|
||||
},
|
||||
}),
|
||||
)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("sends an ultra_high resolution to Google Decisions images but not video", () =>
|
||||
ai.decision
|
||||
.generate({
|
||||
model: Google.configure({ apiKey: "google-key" }).decision("gemini-decision-1"),
|
||||
input: [
|
||||
{ type: "media", media: png },
|
||||
{ type: "media", media: Media.url("https://assets.test/clip.mp4", { mediaType: "video/mp4" }) },
|
||||
],
|
||||
questions: [{ name: "match", type: "predicate", instructions: "Does the clip match the image?" }],
|
||||
providerOptions: { mediaResolution: "ultra_high" },
|
||||
})
|
||||
.pipe(
|
||||
Effect.provide(
|
||||
client((input) => {
|
||||
expect(JSON.parse(input.text).input).toEqual([
|
||||
{ type: "image", mime_type: "image/png", resolution: "ultra_high", data: "AQID" },
|
||||
{ type: "video", mime_type: "video/mp4", uri: "https://assets.test/clip.mp4" },
|
||||
])
|
||||
return Effect.succeed(json(input, { answers: { match: { type: "noul", noul: 0.5, confidence: 0.5 } } }))
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("reports Google Decisions safety blocks as content policy errors", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* ai.decision
|
||||
.generate({
|
||||
model: Google.configure({ apiKey: "google-key" }).decision("gemini-decision-1"),
|
||||
input: [{ type: "text", text: "blocked" }],
|
||||
questions: [{ name: "safe", type: "predicate", instructions: "Is this safe?" }],
|
||||
})
|
||||
.pipe(Effect.flip)
|
||||
expect(error.reason._tag).toBe("ContentPolicy")
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
client((input) =>
|
||||
Effect.succeed(
|
||||
json(
|
||||
input,
|
||||
{
|
||||
error: {
|
||||
code: "safety",
|
||||
message:
|
||||
"Request blocked due to safety violations (harmful content). Please modify your input and retry.",
|
||||
},
|
||||
},
|
||||
{ status: 400 },
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("rejects media Google Decisions can't read before network I/O", () =>
|
||||
Effect.gen(function* () {
|
||||
const model = Google.configure({ apiKey: "google-key" }).decision("gemini-decision-1")
|
||||
const question = [{ name: "readable", type: "predicate", instructions: "Is it readable?" }] as const
|
||||
const csv = yield* ai.decision
|
||||
.generate({ model, input: [{ type: "media", media: Media.base64("YSxi", "text/csv") }], questions: question })
|
||||
.pipe(Effect.flip)
|
||||
expect(csv.reason._tag).toBe("UnsupportedOperation")
|
||||
const files = yield* ai.decision
|
||||
.generate({
|
||||
model,
|
||||
input: [{ type: "media", media: Media.ref("openai", "file-abc", "image/png") }],
|
||||
questions: question,
|
||||
})
|
||||
.pipe(Effect.flip)
|
||||
expect(files.reason._tag).toBe("InvalidRequest")
|
||||
const http = yield* ai.decision
|
||||
.generate({
|
||||
model,
|
||||
input: [{ type: "media", media: Media.url("http://assets.test/a.png", { mediaType: "image/png" }) }],
|
||||
questions: question,
|
||||
})
|
||||
.pipe(Effect.flip)
|
||||
expect(http.reason._tag).toBe("InvalidRequest")
|
||||
}).pipe(Effect.provide(offline)),
|
||||
)
|
||||
|
||||
it.effect("decides through Vercel AI Gateway's OpenAI Decisions endpoint", () =>
|
||||
ai.decision
|
||||
.generate({
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"version": 1,
|
||||
"metadata": {
|
||||
"model": "gemini-decision-1",
|
||||
"tags": [
|
||||
"prefix:google-decision",
|
||||
"provider:google",
|
||||
"protocol:google-decisions"
|
||||
],
|
||||
"name": "google-decision/decides-choice-score-and-predicate-questions",
|
||||
"recordedAt": "2026-11-11T18:28:08.454Z"
|
||||
},
|
||||
"interactions": [
|
||||
{
|
||||
"transport": "http",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "https://generativelanguage.googleapis.com/v1beta/decisions",
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"body": "{\"model\":\"gemini-decision-1\",\"input\":[{\"type\":\"text\",\"text\":\"I was charged twice for the same invoice. Please refund the duplicate payment today.\"}],\"questions\":{\"department\":{\"type\":\"choice\",\"instructions\":\"Which team should handle this support request?\",\"criteria\":{\"billing\":\"Payments, invoices, refunds, or failed charges\",\"technical\":\"Bugs, outages, or integrations\",\"sales\":\"Pricing, upgrades, or new accounts\"}},\"urgency\":{\"type\":\"score\",\"instructions\":\"How urgent is this support request?\",\"criteria\":[\"Can wait for normal support\",\"Needs prompt attention\",\"Actively blocking revenue\"]},\"refund\":{\"type\":\"noul\",\"instructions\":\"Is the customer asking for a refund?\"}}}"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"body": "{\"answers\":{\"department\":{\"confidence\":0.9978076976696219,\"choice\":\"billing\",\"probabilities\":{\"billing\":0.9978076976696219,\"sales\":0.000687029007305657,\"technical\":0.001505273323072618},\"type\":\"choice\"},\"refund\":{\"confidence\":0.99,\"noul\":0.99,\"type\":\"noul\"},\"urgency\":{\"confidence\":0.5717345111874732,\"score\":1.1772012843064368,\"probabilities\":{\"1\":0.5717345111874732,\"0\":0.1255321022530451,\"2\":0.3027333865594818},\"legend\":{\"1\":\"Needs prompt attention\",\"2\":\"Actively blocking revenue\",\"0\":\"Can wait for normal support\"},\"type\":\"score\"}},\"usage\":{\"total_tokens\":242,\"total_input_tokens\":242,\"input_tokens_by_modality\":[{\"modality\":\"text\",\"tokens\":242}],\"total_output_tokens\":0}}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
CloudflareAIGateway,
|
||||
CloudflareWorkersAI,
|
||||
DigitalOcean,
|
||||
Google,
|
||||
OpenAI,
|
||||
OpenCode,
|
||||
OpenRouter,
|
||||
@@ -71,6 +72,14 @@ const openai = recordedTests({
|
||||
metadata: { model: "gpt-6-luna" },
|
||||
})
|
||||
|
||||
const google = recordedTests({
|
||||
prefix: "google-decision",
|
||||
provider: "google",
|
||||
protocol: "google-decisions",
|
||||
requires: ["GOOGLE_API_KEY"],
|
||||
metadata: { model: "gemini-decision-1" },
|
||||
})
|
||||
|
||||
const cloudflareOptions = {
|
||||
redact: { url: (url: string) => url.replace(/\/client\/v4\/accounts\/[^/]+\//, "/client/v4/accounts/{account}/") },
|
||||
}
|
||||
@@ -136,6 +145,17 @@ describe("Decision recorded", () => {
|
||||
),
|
||||
)
|
||||
|
||||
google.effect("decides choice score and predicate questions", () =>
|
||||
assertDecision(
|
||||
Google.configure({ apiKey: process.env.GOOGLE_API_KEY ?? "fixture" }).decision("gemini-decision-1"),
|
||||
).pipe(
|
||||
Effect.map((response) => {
|
||||
expect(response.usage?.outputTokens).toBe(0)
|
||||
expect(response.providerMetadata?.google?.usage).toMatchObject({ input_tokens_by_modality: expect.any(Array) })
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
cloudflareWorkersAI.effect("decides choice score and predicate questions", () =>
|
||||
assertDecision(
|
||||
CloudflareWorkersAI.configure({
|
||||
|
||||
@@ -299,6 +299,16 @@ const decisions: ReadonlyArray<Case<DecisionModel>> = [
|
||||
auth: ["authorization", "Bearer do-key"],
|
||||
provider: "digitalocean",
|
||||
},
|
||||
{
|
||||
name: "google",
|
||||
load: async () => {
|
||||
const { model } = await import("@opencode/ai/providers/google/decisions")
|
||||
return model("gemini-decision-eap", { ...shared, apiKey: "google-key" })
|
||||
},
|
||||
url: "https://generativelanguage.googleapis.com/v1beta/decisions",
|
||||
auth: ["x-goog-api-key", "google-key"],
|
||||
provider: "google",
|
||||
},
|
||||
{
|
||||
name: "vercel-ai-gateway",
|
||||
load: async () => {
|
||||
|
||||
@@ -88,6 +88,7 @@ const builtins = new Map<string, () => Promise<unknown>>([
|
||||
["@opencode/ai/providers/fireworks", () => import("@opencode/ai/providers/fireworks")],
|
||||
["@opencode/ai/providers/fireworks/embeddings", () => import("@opencode/ai/providers/fireworks/embeddings")],
|
||||
["@opencode/ai/providers/google", () => import("@opencode/ai/providers/google")],
|
||||
["@opencode/ai/providers/google/decisions", () => import("@opencode/ai/providers/google/decisions")],
|
||||
["@opencode/ai/providers/google/interactions", () => import("@opencode/ai/providers/google/interactions")],
|
||||
["@opencode/ai/providers/google/embeddings", () => import("@opencode/ai/providers/google/embeddings")],
|
||||
["@opencode/ai/providers/google/images", () => import("@opencode/ai/providers/google/images")],
|
||||
|
||||
@@ -417,6 +417,14 @@ describe("image and decision generation", () => {
|
||||
request,
|
||||
Response.json({ model: "clef", answers: { photo: { type: "noul", noul: 0.9 } } }),
|
||||
)
|
||||
if (request.url === "https://gemini.test/v1beta/decisions")
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({
|
||||
answers: { photo: { type: "noul", noul: 0.8, confidence: 0.8 } },
|
||||
usage: { total_input_tokens: 12, total_output_tokens: 0, total_tokens: 12 },
|
||||
}),
|
||||
)
|
||||
if (request.url === "https://openrouter.ai/api/v1/systemone")
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
@@ -606,6 +614,34 @@ describe("image and decision generation", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
mediaIt.effect("routes a gemini-decisions catalog entry to the Google decisions entrypoint", () =>
|
||||
Effect.gen(function* () {
|
||||
requests.length = 0
|
||||
const generate = yield* Generate.Service
|
||||
const result = yield* generate.decisions({
|
||||
model: ref("decide-host", "decide-gemini"),
|
||||
input: [
|
||||
{ type: "text", text: "Is this a photo?" },
|
||||
{ type: "file", data: "aW1hZ2U=", mime: "image/png" },
|
||||
],
|
||||
questions: [{ type: "predicate", name: "photo", instructions: "Is this a photo?" }],
|
||||
})
|
||||
expect(requests[0]).toMatchObject({
|
||||
url: "https://gemini.test/v1beta/decisions",
|
||||
body: {
|
||||
model: "decide-gemini",
|
||||
input: [
|
||||
{ type: "text", text: "Is this a photo?" },
|
||||
{ type: "image", mime_type: "image/png", data: "aW1hZ2U=" },
|
||||
],
|
||||
questions: { photo: { type: "noul", instructions: "Is this a photo?" } },
|
||||
},
|
||||
})
|
||||
expect(result.answers).toEqual([{ type: "predicate", name: "photo", probability: 0.8 }])
|
||||
expect(result.tokens).toEqual({ input: 12, output: 0 })
|
||||
}),
|
||||
)
|
||||
|
||||
mediaIt.effect("sends a variant's reasoning setting and maps provider rejections", () =>
|
||||
Effect.gen(function* () {
|
||||
requests.length = 0
|
||||
@@ -684,6 +720,15 @@ function decisionCatalog() {
|
||||
"decide-state": model("decide-state", { media_input: "state" }),
|
||||
"decide-unknown": model("decide-unknown", { media_input: "somewhere_new" }),
|
||||
"decide-override": model("decide-override", { media_input: "state" }),
|
||||
"decide-gemini": {
|
||||
...model("decide-gemini"),
|
||||
api: {
|
||||
"gemini-decisions": {
|
||||
base_url: "https://gemini.test/v1beta",
|
||||
sdk: { opencodeai: "@opencode/ai/providers/google/decisions" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user