Skip to content

Commit 88bfaf2

Browse files
committed
fix: leave decision cost out without a catalog price; keep ultra_high off video
1 parent aa641dc commit 88bfaf2

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎packages/ai/src/protocols/google-decisions.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ const mediaPart = (request: DecisionRequest, asset: Media.Asset, resolution: str
115115
const block = {
116116
type,
117117
mime_type: asset.mediaType.toLowerCase(),
118-
...(resolution !== undefined && (type === "image" || type === "video") ? { resolution } : {}),
118+
// `ultra_high` is image-only, so video keeps Google's default for it.
119+
...(resolution !== undefined && (type === "image" || (type === "video" && resolution !== "ultra_high"))
120+
? { resolution }
121+
: {}),
119122
}
120123
const inline = asset.inline()
121124
if (inline) return Effect.succeed({ ...block, data: inline.base64 })

‎packages/ai/test/decision.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,30 @@ describe("Decision", () => {
579579
),
580580
)
581581

582+
it.effect("sends an ultra_high resolution to Google Decisions images but not video", () =>
583+
ai.decision
584+
.generate({
585+
model: Google.configure({ apiKey: "google-key" }).decision("gemini-decision-1"),
586+
input: [
587+
{ type: "media", media: png },
588+
{ type: "media", media: Media.url("https://assets.test/clip.mp4", { mediaType: "video/mp4" }) },
589+
],
590+
questions: [{ name: "match", type: "predicate", instructions: "Does the clip match the image?" }],
591+
providerOptions: { mediaResolution: "ultra_high" },
592+
})
593+
.pipe(
594+
Effect.provide(
595+
client((input) => {
596+
expect(JSON.parse(input.text).input).toEqual([
597+
{ type: "image", mime_type: "image/png", resolution: "ultra_high", data: "AQID" },
598+
{ type: "video", mime_type: "video/mp4", uri: "https://assets.test/clip.mp4" },
599+
])
600+
return Effect.succeed(json(input, { answers: { match: { type: "noul", noul: 0.5, confidence: 0.5 } } }))
601+
}),
602+
),
603+
),
604+
)
605+
582606
it.effect("reports Google Decisions safety blocks as content policy errors", () =>
583607
Effect.gen(function* () {
584608
const error = yield* ai.decision

‎packages/core/src/generate.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export const layer = Layer.effect(
211211
Effect.mapError((error) => providerError(input.model, error)),
212212
)
213213
const usage = response.usage
214-
// Hosts that don't report what they charged are priced from the catalog, as sessions are.
214+
// Hosts that don't report what they charged are priced from the catalog, as sessions are, when it has a price.
215215
const catalog =
216216
usage === undefined || usage.cost !== undefined
217217
? undefined
@@ -221,7 +221,8 @@ export const layer = Layer.effect(
221221
const cost =
222222
usage?.cost !== undefined
223223
? Money.USD.make(usage.cost)
224-
: catalog && usage
224+
: // An unpriced catalog entry decodes to all-zero prices, which would read as a real $0.
225+
catalog?.cost.some((price) => price.input > 0 || price.output > 0) && usage
225226
? SessionUsage.calculateCost(catalog.cost, {
226227
input: usage.inputTokens ?? 0,
227228
output: usage.outputTokens ?? 0,

‎packages/core/test/generate.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe("image and decision generation", () => {
323323
Provider.Info.make({
324324
...Provider.Info.empty(providerID),
325325
activation: "enabled",
326-
settings:
326+
settings:
327327
// A local host like LM Studio is enabled with no key.
328328
providerID === "lmstudio"
329329
? { baseURL: "http://127.0.0.1:1234/v1", provider: "lmstudio", apiKey: "" }
@@ -641,6 +641,14 @@ describe("image and decision generation", () => {
641641
expect(result.tokens).toEqual({ input: 12, output: 0 })
642642
// The host reports no cost, so the catalog's input price applies.
643643
expect(result.cost).toBe(Money.USD.make((12 * 0.5) / 1_000_000))
644+
// Without a catalog price there is nothing to calculate, so no cost is reported rather than $0.
645+
const unpriced = yield* generate.decisions({
646+
model: ref("decide-host", "decide-gemini-unpriced"),
647+
input: [{ type: "text", text: "Is this a photo?" }],
648+
questions: [{ type: "predicate", name: "photo", instructions: "Is this a photo?" }],
649+
})
650+
expect(unpriced.tokens).toEqual({ input: 12, output: 0 })
651+
expect(unpriced.cost).toBeUndefined()
644652
}),
645653
)
646654

@@ -732,6 +740,15 @@ function decisionCatalog() {
732740
},
733741
},
734742
},
743+
"decide-gemini-unpriced": {
744+
...model("decide-gemini-unpriced"),
745+
api: {
746+
"gemini-decisions": {
747+
base_url: "https://gemini.test/v1beta",
748+
sdk: { opencodeai: "@opencode/ai/providers/google/decisions" },
749+
},
750+
},
751+
},
735752
},
736753
},
737754
}),

0 commit comments

Comments
 (0)