@braintrust/proxy
Version:
A proxy server that load balances across AI providers.
251 lines (249 loc) • 7.78 kB
JavaScript
// types/openai.ts
import { z } from "zod";
var chatCompletionMessageReasoningSchema = z.object({
id: z.string().nullish().transform((x) => x != null ? x : void 0),
content: z.string().nullish().transform((x) => x != null ? x : void 0)
}).describe(
"Note: This is not part of the OpenAI API spec, but we added it for interoperability with multiple reasoning models."
);
var completionUsageSchema = z.object({
completion_tokens: z.number(),
prompt_tokens: z.number(),
total_tokens: z.number(),
completion_tokens_details: z.object({
accepted_prediction_tokens: z.number().optional(),
audio_tokens: z.number().optional(),
reasoning_tokens: z.number().optional(),
rejected_prediction_tokens: z.number().optional()
}).optional(),
prompt_tokens_details: z.object({
audio_tokens: z.number().optional(),
cached_tokens: z.number().optional(),
cache_creation_tokens: z.number().optional().describe(
"Extension to support Anthropic `cache_creation_input_tokens`"
)
}).optional()
});
// types/anthropic.ts
import {
attachmentReferenceSchema
} from "@braintrust/core/typespecs";
import { z as z2 } from "zod";
var cacheControlSchema = z2.object({
type: z2.enum(["ephemeral"])
});
var anthropicBase64ImageSourceSchema = z2.object({
type: z2.literal("base64"),
media_type: z2.enum(["image/jpeg", "image/png", "image/gif", "image/webp"]),
data: z2.string()
});
var anthropicUrlImageSourceSchema = z2.object({
type: z2.literal("url"),
url: z2.preprocess((val) => {
if (typeof val === "string") {
return val;
}
const parsed = attachmentReferenceSchema.safeParse(val);
if (parsed.success) {
return JSON.stringify(parsed.data);
}
return val;
}, z2.string())
});
var anthropicFileSourceSchema = z2.object({
type: z2.literal("file"),
file_id: z2.string()
});
var anthropicContentPartImageSchema = z2.object({
type: z2.literal("image"),
source: z2.union([
anthropicBase64ImageSourceSchema,
anthropicUrlImageSourceSchema,
anthropicFileSourceSchema
]),
cache_control: cacheControlSchema.optional()
});
var anthropicContentPartTextSchema = z2.object({
type: z2.literal("text"),
text: z2.string(),
cache_control: cacheControlSchema.optional()
});
var anthropicToolUseContentPartSchema = z2.object({
type: z2.literal("tool_use"),
id: z2.string(),
name: z2.string(),
input: z2.record(z2.any()),
cache_control: cacheControlSchema.optional()
});
var anthropicServerToolUseContentPartSchema = z2.object({
type: z2.literal("server_tool_use"),
id: z2.string(),
name: z2.enum(["web_search", "code_execution"]),
input: z2.record(z2.any()),
cache_control: cacheControlSchema.optional()
});
var anthropicWebSearchToolResultErrorSchema = z2.object({
type: z2.literal("web_search_tool_result_error"),
errorCode: z2.enum([
"invalid_tool_input",
"unavailable",
"max_uses_exceeded",
"too_many_requests",
"query_too_long"
])
});
var anthropicWebSearchToolResultSuccessSchema = z2.object({
type: z2.literal("web_search_result"),
url: z2.string(),
page_age: z2.number().nullish(),
title: z2.string(),
encrypted_content: z2.string()
});
var anthropicWebSearchToolResultContentPartSchema = z2.object({
type: z2.literal("web_search_tool_result"),
tool_use_id: z2.string(),
content: z2.union([
anthropicWebSearchToolResultErrorSchema,
z2.array(anthropicWebSearchToolResultSuccessSchema)
]),
cache_control: cacheControlSchema.nullish()
});
var anthropicCodeExecutionToolResultErrorSchema = z2.object({
type: z2.literal("code_execution_tool_result_error"),
errorCode: z2.enum([
"invalid_tool_input",
"unavailable",
"too_many_requests",
"query_too_long"
])
});
var anthropicCodeExecutionToolResultSuccessSchema = z2.object({
type: z2.literal("code_execution_result"),
return_code: z2.number(),
stderr: z2.string(),
stdout: z2.string(),
content: z2.array(
z2.object({
type: z2.literal("code_execution_output"),
file_id: z2.string()
})
)
});
var anthropicCodeExecutionToolResultContentPartSchema = z2.object({
type: z2.literal("code_execution_tool_result"),
tool_use_id: z2.string(),
content: z2.union([
anthropicCodeExecutionToolResultErrorSchema,
anthropicCodeExecutionToolResultSuccessSchema
]),
cache_control: cacheControlSchema.nullish()
});
var anthropicMCPToolUseContentPartSchema = z2.object({
type: z2.literal("mcp_tool_use"),
id: z2.string(),
name: z2.string(),
input: z2.record(z2.any()),
server_name: z2.string(),
cache_control: cacheControlSchema.nullish()
});
var anthropicMCPToolResultContentPartSchema = z2.object({
type: z2.literal("mcp_tool_result"),
tool_use_id: z2.string(),
is_error: z2.boolean(),
content: z2.union([
z2.string(),
z2.array(
z2.object({
type: z2.literal("text"),
text: z2.string(),
// This is a simplification of the strict citation schema
citations: z2.array(z2.record(z2.any())).nullish(),
cache_control: cacheControlSchema.nullish()
})
)
]),
cache_control: cacheControlSchema.nullish()
});
var anthropicTextImageContentBlockSchema = z2.union([
z2.string(),
z2.array(
z2.union([anthropicContentPartTextSchema, anthropicContentPartImageSchema])
)
]);
var anthropicToolResultContentPartSchema = z2.object({
type: z2.literal("tool_result"),
tool_use_id: z2.string(),
content: anthropicTextImageContentBlockSchema.optional(),
is_error: z2.boolean().optional(),
cache_control: cacheControlSchema.nullish()
});
var anthropicPDFSchema = z2.object({
media_type: z2.literal("application/pdf"),
data: z2.string(),
type: z2.literal("base64")
});
var anthropicPlainTextSchema = z2.object({
media_type: z2.literal("text/plain"),
data: z2.string(),
type: z2.literal("text")
});
var anthropicURLPDFSchema = z2.object({
url: z2.string(),
type: z2.literal("url")
});
var anthropicDocumentContentPartSchema = z2.object({
type: z2.literal("document"),
source: z2.union([
anthropicPDFSchema,
anthropicPlainTextSchema,
anthropicURLPDFSchema,
anthropicTextImageContentBlockSchema,
anthropicFileSourceSchema
]),
citations: z2.object({
enabled: z2.boolean().optional()
}).optional(),
context: z2.string().nullish(),
title: z2.string().nullish(),
cache_control: cacheControlSchema.nullish()
});
var anthropicThinkingContentPartSchema = z2.object({
type: z2.literal("thinking"),
thinking: z2.string(),
signature: z2.string()
});
var anthropicRedactedThinkingContentPartSchema = z2.object({
type: z2.literal("redacted_thinking"),
data: z2.string()
});
var anthropicContainerUploadContentPartSchema = z2.object({
type: z2.literal("container_upload"),
file_id: z2.string(),
cache_control: cacheControlSchema.nullish()
});
var anthropicContentPartSchema = z2.union([
anthropicContentPartTextSchema,
anthropicContentPartImageSchema,
anthropicToolUseContentPartSchema,
anthropicToolResultContentPartSchema,
anthropicServerToolUseContentPartSchema,
anthropicWebSearchToolResultContentPartSchema,
anthropicCodeExecutionToolResultContentPartSchema,
anthropicMCPToolUseContentPartSchema,
anthropicMCPToolResultContentPartSchema,
anthropicDocumentContentPartSchema,
anthropicThinkingContentPartSchema,
anthropicRedactedThinkingContentPartSchema,
anthropicContainerUploadContentPartSchema
]);
var anthropicMessageParamSchema = z2.object({
role: z2.enum(["system", "user", "assistant"]),
content: z2.union([z2.string(), z2.array(anthropicContentPartSchema)])
});
export {
anthropicContentPartImageSchema,
anthropicContentPartSchema,
anthropicMessageParamSchema,
chatCompletionMessageReasoningSchema,
completionUsageSchema
};