-
Notifications
You must be signed in to change notification settings - Fork 0
feat: preserve safe provider termination metadata #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
206c0e4
52dcaeb
be1bcb4
c1d173c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import type { LanguageModelV2Middleware } from "@ai-sdk/provider" | ||
| import z from "zod" | ||
|
|
||
| export namespace ProviderTermination { | ||
| const VALUE_LIMIT = 128 | ||
| const DIAGNOSTIC_LIMIT = 2048 | ||
| function Field(limit: number) { | ||
| return z.object({ | ||
| status: z.enum(["available", "unavailable", "redacted"]), | ||
| value: z.string().max(limit).optional(), | ||
| truncated: z.boolean(), | ||
| }) | ||
| } | ||
| type Field = z.infer<ReturnType<typeof Field>> | ||
|
|
||
| export const Info = z | ||
| .object({ | ||
| providerID: z.string(), | ||
| modelID: z.string(), | ||
| normalizedReason: z.string(), | ||
| rawReason: Field(VALUE_LIMIT), | ||
| requestID: Field(VALUE_LIMIT), | ||
| diagnostic: Field(DIAGNOSTIC_LIMIT), | ||
| }) | ||
| .meta({ ref: "ProviderTermination" }) | ||
| export type Info = z.infer<typeof Info> | ||
|
|
||
| const reasons = new Set([ | ||
| "FINISH_REASON_UNSPECIFIED", | ||
| "STOP", | ||
| "MAX_TOKENS", | ||
| "SAFETY", | ||
| "RECITATION", | ||
| "LANGUAGE", | ||
| "OTHER", | ||
| "BLOCKLIST", | ||
| "PROHIBITED_CONTENT", | ||
| "SPII", | ||
| "MALFORMED_FUNCTION_CALL", | ||
| "IMAGE_SAFETY", | ||
| "UNEXPECTED_TOOL_CALL", | ||
| "NO_IMAGE", | ||
| "IMAGE_PROHIBITED_CONTENT", | ||
| "IMAGE_OTHER", | ||
| "IMAGE_RECITATION", | ||
| ]) | ||
|
|
||
| function field(value: unknown, allowed: (value: string) => boolean, limit = VALUE_LIMIT): Field { | ||
| if (typeof value !== "string" || !value) return { status: "unavailable", truncated: false } | ||
| if (value.length > limit || !allowed(value)) return { status: "redacted", truncated: value.length > limit } | ||
| return { status: "available", value, truncated: false } | ||
| } | ||
|
|
||
| function record(value: unknown): Record<string, unknown> | undefined { | ||
| if (typeof value !== "object" || value === null || Array.isArray(value)) return | ||
| return value as Record<string, unknown> | ||
| } | ||
|
|
||
| export function middleware(input: { providerID: string; modelID: string; npm: string }): LanguageModelV2Middleware { | ||
| const google = input.npm === "@ai-sdk/google" || input.npm === "@ai-sdk/google-vertex" | ||
| return { | ||
| async transformParams({ type, params }) { | ||
| return type === "stream" && google ? { ...params, includeRawChunks: true } : params | ||
| }, | ||
| async wrapStream({ doStream }) { | ||
| const result = await doStream() | ||
| let rawReason = field(undefined, () => false) | ||
| let diagnostic = field(undefined, () => false) | ||
| const headers = result.response?.headers | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 result.response?.headers never exists in v2; requestID dead. --- a/packages/cli/src/provider/termination.ts
+++ b/packages/cli/src/provider/termination.ts
@@ -61,7 +61,7 @@
const result = await doStream()
let rawReason = field(undefined, () => false)
let diagnostic = field(undefined, () => false)
- const headers = result.response?.headers
+ const headers = result.responseHeaders
const requestID = field(
headers?.["x-request-id"] ?? headers?.["x-goog-request-id"],
🤖 Fix with your agentWhy this mattersThe middleware declares async wrapStream({ doStream }) {
const result = await doStream()
let rawReason = field(undefined, () => false)
let diagnostic = field(undefined, () => false)
const headers = result.response?.headers
const requestID = field(
headers?.["x-request-id"] ?? headers?.["x-goog-request-id"],
(value) => /^[a-zA-Z0-9_-]+$/.test(value) && !/^(sk-|gh[pousr]_|github_pat_|AIza|AKIA|ASIA)/.test(value),
)
return { |
||
| // A provider/proxy can echo arbitrary credentials into an ID header. | ||
| // Format allowlists cannot distinguish an opaque ID from an opaque key. | ||
| const requestID = field(headers?.["x-request-id"] || headers?.["x-goog-request-id"], () => false) | ||
| return { | ||
| ...result, | ||
| stream: result.stream.pipeThrough( | ||
| new TransformStream({ | ||
| transform(chunk, controller) { | ||
| if (chunk.type === "raw") { | ||
| if (google) { | ||
| const candidates = record(chunk.rawValue)?.candidates | ||
| const candidate = Array.isArray(candidates) ? record(candidates[0]) : undefined | ||
| if (candidate?.finishReason != null) { | ||
| rawReason = field(candidate.finishReason, (value) => reasons.has(value)) | ||
| // Provider messages can contain prompts or tool arguments. Retain only | ||
| // presence/size information; do not collect their free-form contents. | ||
| diagnostic = field(candidate.finishMessage, () => false, DIAGNOSTIC_LIMIT) | ||
| } | ||
| } | ||
| // Raw payloads must never escape this boundary to stream consumers. | ||
| return | ||
| } | ||
| if (chunk.type !== "finish") { | ||
| controller.enqueue(chunk) | ||
| return | ||
| } | ||
| const termination: Info = { | ||
| providerID: input.providerID, | ||
| modelID: input.modelID, | ||
| normalizedReason: chunk.finishReason, | ||
| rawReason, | ||
| requestID, | ||
| diagnostic, | ||
| } | ||
| controller.enqueue({ | ||
| ...chunk, | ||
| providerMetadata: { | ||
| ...chunk.providerMetadata, | ||
| aictrl: { ...chunk.providerMetadata?.aictrl, termination }, | ||
| }, | ||
| }) | ||
| }, | ||
| }), | ||
| ), | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export function from(metadata: unknown): Info | undefined { | ||
| const result = Info.safeParse(record(record(metadata)?.aictrl)?.termination) | ||
| return result.success ? result.data : undefined | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { expect, test } from "bun:test" | ||
| import path from "path" | ||
|
|
||
| test("generate publishes the termination schema used by SDK codegen", async () => { | ||
| const proc = Bun.spawn( | ||
| ["bun", "run", "--conditions=browser", path.resolve(import.meta.dir, "../../src/index.ts"), "generate"], | ||
| { | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }, | ||
| ) | ||
| const [stdout, stderr, exit] = await Promise.all([ | ||
| new Response(proc.stdout).text(), | ||
| new Response(proc.stderr).text(), | ||
| proc.exited, | ||
| ]) | ||
| expect(exit, stderr).toBe(0) | ||
| const spec = JSON.parse(stdout) | ||
| const part = spec.components.schemas.StepFinishPart | ||
| const diagnostic = spec.components.schemas.ProviderTermination | ||
| expect(spec.openapi).toBe("3.1.1") | ||
| expect(part.properties.termination).toEqual({ $ref: "#/components/schemas/ProviderTermination" }) | ||
| expect(part.$schema).toBe("https://json-schema.org/draft/2020-12/schema") | ||
| expect(diagnostic.$schema).toBe("https://json-schema.org/draft/2020-12/schema") | ||
| expect(part.id).toBeUndefined() | ||
| expect(diagnostic.id).toBeUndefined() | ||
| expect(part.$id).toBeUndefined() | ||
| expect(diagnostic.$id).toBeUndefined() | ||
| expect(part.properties.type.const).toBe("step-finish") | ||
| expect(stdout).not.toMatch(/"ref"\s*:/) | ||
| expect(stdout).not.toMatch(/"nullable"\s*:/) | ||
| expect(part.required).not.toContain("termination") | ||
| expect(diagnostic.required).toContain("normalizedReason") | ||
| expect(diagnostic.properties.rawReason.properties.value.maxLength).toBe(128) | ||
| expect(diagnostic.properties.diagnostic.properties.value.maxLength).toBe(2048) | ||
| expect(diagnostic.properties.requestID.properties.status.enum).toEqual(["available", "unavailable", "redacted"]) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Termination block splits heading from section intro sentence.
🤖 Fix with your agent
Why this matters
The 57-line termination subsection is inserted directly under
### step_start / step_finish, pushing the section's own one-line description "Emitted at step boundaries during multi-step tool use." to after the JSON example and policy paragraphs. Readers now hit termination-specific detail before being told what the events are, inverting the intro-then-detail structure used by surrounding sections.