handleSelect(
model.id,
diff --git a/components/provider-credentials-fields.tsx b/components/provider-credentials-fields.tsx
index 8a9bf0d1..4b97716e 100644
--- a/components/provider-credentials-fields.tsx
+++ b/components/provider-credentials-fields.tsx
@@ -13,7 +13,12 @@ import {
} from "@/components/ui/select"
import { useDictionary } from "@/hooks/use-dictionary"
import { formatMessage } from "@/lib/i18n/utils"
-import { PROVIDER_INFO, type ProviderName } from "@/lib/types/model-config"
+import {
+ chatRequestUrl,
+ normalizeBaseUrl,
+ PROVIDER_INFO,
+ type ProviderName,
+} from "@/lib/types/model-config"
// Logical secret field. The caller owns the actual input — plaintext for the
// user dialog, write-only masked for the admin panel — supplied via
@@ -26,7 +31,7 @@ export type SecretField =
| "vertexApiKey"
// AWS regions offered for Bedrock (shared by both screens)
-const AWS_REGIONS: Array<[string, string]> = [
+export const AWS_REGIONS: Array<[string, string]> = [
["us-east-1", "N. Virginia"],
["us-east-2", "Ohio"],
["us-west-2", "Oregon"],
@@ -77,6 +82,7 @@ export function ProviderCredentialsFields({
const baseUrlLabel = formatMessage(dict.modelConfig.baseUrlWithExample, {
example: info.defaultBaseUrl || "https://api.example.com/v1",
})
+ const requestUrl = baseUrl ? chatRequestUrl(provider, baseUrl) : null
// EdgeOne needs no credentials — the caller supplies just a test button
if (provider === "edgeone") {
@@ -208,17 +214,29 @@ export function ProviderCredentialsFields({
>
) : (
<>
- {/* API Key */}
+ {/* API Key, with a link to where the provider issues keys */}
-
-
- {dict.modelConfig.apiKey}
- {provider === "ollama" &&
- ` ${dict.modelConfig.optional}`}
-
+
+
+
+ {dict.modelConfig.apiKey}
+ {provider === "ollama" &&
+ ` ${dict.modelConfig.optional}`}
+
+ {info.apiKeyUrl && (
+
+ {dict.modelConfig.getApiKey}
+
+ )}
+
{renderSecret({ field: "apiKey", id: "api-key" })}
@@ -238,12 +256,28 @@ export function ProviderCredentialsFields({
onChange={(e) =>
onChange("baseUrl", e.target.value)
}
+ // Drop a pasted endpoint path such as /chat/completions
+ onBlur={(e) => {
+ const normalized = normalizeBaseUrl(
+ e.target.value,
+ )
+ if (normalized !== e.target.value) {
+ onChange("baseUrl", normalized)
+ }
+ }}
placeholder={
info.defaultBaseUrl ||
dict.modelConfig.customEndpoint
}
className="h-9 rounded-xl font-mono text-xs"
/>
+ {requestUrl && (
+
+ {formatMessage(dict.modelConfig.requestUrl, {
+ url: requestUrl,
+ })}
+
+ )}
{provider === "minimax" && (
{dict.modelConfig.minimaxBaseUrlHint}
diff --git a/contexts/diagram-context.tsx b/contexts/diagram-context.tsx
index 6a139a08..0295a98f 100644
--- a/contexts/diagram-context.tsx
+++ b/contexts/diagram-context.tsx
@@ -1,26 +1,34 @@
"use client"
import type React from "react"
-import { createContext, useContext, useEffect, useRef, useState } from "react"
+import { createContext, useCallback, useContext, useRef, useState } from "react"
import type { DrawIoEmbedRef, EventExport } from "react-drawio"
import { toast } from "sonner"
import type { ExportFormat } from "@/components/save-dialog"
import { getApiEndpoint } from "@/lib/base-path"
import {
- extractDiagramXML,
- isRealDiagram,
- validateAndFixXml,
-} from "../lib/utils"
+ BLANK_MXFILE,
+ normalizeToMxfile,
+} from "@/packages/mcp-server/src/pages.ts"
+import { validateAndFixXml } from "@/packages/mcp-server/src/xml-validation.ts"
+import { extractDiagramXML, isRealDiagram } from "../lib/utils"
interface DiagramContextType {
chartXML: string
+ // chartXML right away, before the re-render (loadDiagram sets both)
+ chartXMLRef: React.MutableRefObject
latestSvg: string
diagramHistory: { svg: string; xml: string }[]
setDiagramHistory: (history: { svg: string; xml: string }[]) => void
loadDiagram: (chart: string, skipValidation?: boolean) => string | null
- handleExport: () => void
- handleExportWithoutHistory: () => void
- resolverRef: React.MutableRefObject<((value: string) => void) | null>
+ // Both return the export's tag (empty when draw.io is not there yet)
+ handleExport: () => string
+ handleExportWithoutHistory: () => string
+ // Pending exports by tag; a history or plain export's resolver gets the
+ // first page's XML
+ exportResolversRef: React.MutableRefObject<
+ Record void>
+ >
drawioRef: React.MutableRefObject
handleDiagramExport: (data: EventExport) => void
handleDiagramAutoSave: (data: { xml?: string }) => void
@@ -42,6 +50,12 @@ interface DiagramContextType {
const DiagramContext = createContext(undefined)
+// Every export carries a tag in the request's `message` field. draw.io
+// echoes the request back in the export event, so each result reaches its
+// own caller. Tags end in a request number, so a late result never answers
+// a newer request.
+type ExportTag = "thumbnail" | "validation"
+
export function DiagramProvider({ children }: { children: React.ReactNode }) {
const [chartXML, setChartXML] = useState("")
const [latestSvg, setLatestSvg] = useState("")
@@ -52,11 +66,13 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
const [showSaveDialog, setShowSaveDialog] = useState(false)
const hasCalledOnLoadRef = useRef(false)
const drawioRef = useRef(null)
- const resolverRef = useRef<((value: string) => void) | null>(null)
- // Resolver for PNG export (used for VLM validation)
- const pngResolverRef = useRef<((value: string) => void) | null>(null)
- // Track if we're expecting an export for history (user-initiated)
- const expectHistoryExportRef = useRef(false)
+ // Pending exports, keyed by their export tag
+ const exportResolversRef = useRef<
+ Record void>
+ >({})
+ // Pending history exports: the document each one was asked for
+ const historyXmlRef = useRef(new Map())
+ const exportSeqRef = useRef(0)
// Track latest chartXML for restoration after remount
const chartXMLRef = useRef("")
@@ -76,94 +92,85 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
setIsDrawioReady(false)
}
- // Keep chartXMLRef in sync with state for restoration after remount
- useEffect(() => {
- chartXMLRef.current = chartXML
- }, [chartXML])
-
- // Track if we're expecting an export for file save (stores raw export data)
- const saveResolverRef = useRef<{
- resolver: ((data: string, fullDiagramXML?: string) => void) | null
- format: ExportFormat | null
- }>({ resolver: null, format: null })
+ // Update chartXML and its ref together, so callbacks that read the ref
+ // (export handler, autosave) see the new value right away
+ const updateChartXML = (xml: string) => {
+ chartXMLRef.current = xml
+ setChartXML(xml)
+ }
const handleExport = () => {
- if (drawioRef.current) {
- // Mark that this export should be saved to history
- expectHistoryExportRef.current = true
- drawioRef.current.exportDiagram({
- format: "xmlsvg",
- })
- }
+ if (!drawioRef.current) return ""
+ // Save this export to history, with the document shown now:
+ // chartXML can change before the result comes back
+ const tag = `history-${++exportSeqRef.current}`
+ historyXmlRef.current.set(tag, chartXMLRef.current)
+ drawioRef.current.exportDiagram({
+ format: "xmlsvg",
+ message: tag,
+ })
+ return tag
}
const handleExportWithoutHistory = () => {
- if (drawioRef.current) {
- // Export without saving to history (for edit_diagram fetching current state)
- drawioRef.current.exportDiagram({
- format: "xmlsvg",
- })
- }
+ if (!drawioRef.current) return ""
+ // Export without saving to history (for edit_diagram fetching current state)
+ const tag = `fetch-${++exportSeqRef.current}`
+ drawioRef.current.exportDiagram({
+ format: "xmlsvg",
+ message: tag,
+ })
+ return tag
}
- // Get current diagram as SVG for thumbnail (used by session storage)
- const getThumbnailSvg = async (): Promise => {
+ // Export with a tag in `message` (draw.io echoes it back in the export
+ // event) and wait for that result. Resolves to null on timeout, which is
+ // expected occasionally.
+ // (Reads refs only, so it keeps one identity)
+ const requestTaggedExport = useCallback(
+ (tag: ExportTag, format: "xmlsvg" | "png", timeoutMs: number) =>
+ new Promise((resolve) => {
+ const id = `${tag}-${++exportSeqRef.current}`
+ const finish = (value: string | null) => {
+ clearTimeout(timer)
+ delete exportResolversRef.current[id]
+ resolve(value)
+ }
+ const timer = setTimeout(() => finish(null), timeoutMs)
+ exportResolversRef.current[id] = finish
+ drawioRef.current?.exportDiagram({ format, message: id })
+ }),
+ [],
+ )
+
+ // Get current diagram as SVG for thumbnail (used by session storage).
+ // One identity: the chat's auto-save depends on it, and each thumbnail
+ // renders this provider again (latestSvg), which would otherwise start
+ // the next save
+ const getThumbnailSvg = useCallback(async (): Promise => {
if (!drawioRef.current) return null
// Don't export if diagram is empty
- if (!isRealDiagram(chartXML)) return null
-
- try {
- const svgData = await Promise.race([
- new Promise((resolve) => {
- resolverRef.current = resolve
- drawioRef.current?.exportDiagram({ format: "xmlsvg" })
- }),
- new Promise((_, reject) =>
- setTimeout(() => reject(new Error("Export timeout")), 3000),
- ),
- ])
+ if (!isRealDiagram(chartXMLRef.current)) return null
+ // xmlsvg exports return an SVG data URL
+ const svgData = await requestTaggedExport("thumbnail", "xmlsvg", 3000)
+ if (svgData?.startsWith("data:image/svg")) {
// Update latestSvg so it's available for future saves
- if (svgData?.includes(" => {
if (!drawioRef.current) return null
// Don't export if diagram is empty
- if (!isRealDiagram(chartXML)) return null
+ if (!isRealDiagram(chartXMLRef.current)) return null
- try {
- const pngData = await Promise.race([
- new Promise((resolve) => {
- pngResolverRef.current = resolve
- drawioRef.current?.exportDiagram({ format: "png" })
- }),
- new Promise((_, reject) =>
- setTimeout(
- () => reject(new Error("PNG export timeout")),
- 5000,
- ),
- ),
- ])
-
- // PNG data should be a base64 data URL
- if (pngData?.startsWith("data:image/png")) {
- return pngData
- }
- return null
- } catch {
- // Timeout is expected occasionally - don't log as error
- return null
- }
+ const pngData = await requestTaggedExport("validation", "png", 5000)
+ // PNG data should be a base64 data URL
+ return pngData?.startsWith("data:image/png") ? pngData : null
}
const loadDiagram = (
@@ -172,9 +179,11 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
): string | null => {
let xmlToLoad = chart
- // Validate XML structure before loading (unless skipped for internal use)
+ // Validate XML structure before loading (unless skipped for internal
+ // use). Not strict: the XML may hold the user's own diagram, and the
+ // tool handlers check model XML strictly before it gets here.
if (!skipValidation) {
- const validation = validateAndFixXml(chart)
+ const validation = validateAndFixXml(chart, { strict: false })
if (!validation.valid) {
console.warn(
"[loadDiagram] Validation error:",
@@ -193,7 +202,7 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
}
// Keep chartXML in sync even when diagrams are injected (e.g., display_diagram tool)
- setChartXML(xmlToLoad)
+ updateChartXML(xmlToLoad)
if (drawioRef.current) {
drawioRef.current.load({
@@ -205,26 +214,14 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
}
const handleDiagramExport = (data: EventExport) => {
- // Handle PNG export for VLM validation
- if (pngResolverRef.current && data.data?.startsWith("data:image/png")) {
- pngResolverRef.current(data.data)
- pngResolverRef.current = null
+ // Thumbnail, validation PNG and file save exports go only to their
+ // own caller
+ const tag = data.message?.message
+ if (/^(thumbnail|validation|save)-/.test(tag ?? "")) {
+ exportResolversRef.current[tag as string]?.(data.data, data.xml)
return
}
- // Handle save to file if requested (process raw data before extraction)
- if (saveResolverRef.current.resolver) {
- const format = saveResolverRef.current.format
- saveResolverRef.current.resolver(data.data, data.xml)
- saveResolverRef.current = { resolver: null, format: null }
- // For non-xmlsvg formats, skip XML extraction as it will fail
- // Only drawio (which uses xmlsvg internally) has the content attribute
- // xmlsvg is saved directly as SVG file, no need for extraction
- if (format === "png" || format === "svg" || format === "xmlsvg") {
- return
- }
- }
-
// Don't write chartXML here: exports don't change the diagram, and
// data.xml from xmlsvg exports has compressed payloads that
// would break edit_diagram/display_diagram. Autosave keeps chartXML
@@ -235,39 +232,49 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
// Only add to history if this was a user-initiated export
// Limit to 20 entries to prevent memory leaks during long sessions
const MAX_HISTORY_SIZE = 20
- if (expectHistoryExportRef.current) {
+ const askedXml =
+ tag !== undefined ? historyXmlRef.current.get(tag) : undefined
+ if (askedXml !== undefined) {
+ historyXmlRef.current.delete(tag as string)
+ // Store the full multi-page document (extractedXML is only the
+ // first page), so restoring a version keeps every page
+ const historyXml = askedXml || extractedXML
setDiagramHistory((prev) => {
const newHistory = [
...prev,
{
svg: data.data,
- xml: extractedXML,
+ xml: historyXml,
},
]
// Keep only the last MAX_HISTORY_SIZE entries (circular buffer)
return newHistory.slice(-MAX_HISTORY_SIZE)
})
- expectHistoryExportRef.current = false
}
- if (resolverRef.current) {
- resolverRef.current(extractedXML)
- resolverRef.current = null
+ // The chat's own export (onFetchChart), not another one in flight
+ const resolve =
+ tag !== undefined ? exportResolversRef.current[tag] : undefined
+ if (resolve) {
+ delete exportResolversRef.current[tag as string]
+ resolve(extractedXML)
}
}
+ // react-drawio registers this callback once per iframe mount, so it must
+ // read refs: state captured in its closure would stay stale after a remount
const handleDiagramAutoSave = (data: { xml?: string }) => {
if (!data?.xml) return
- // Don't overwrite a pending restore - if we have a real diagram in state
- // but DrawIO isn't ready yet, it means we're waiting to restore
- if (!isDrawioReady && isRealDiagram(chartXML)) {
+ // Don't overwrite a pending restore - if we have a real diagram but
+ // DrawIO hasn't loaded yet, it means we're waiting to restore
+ if (!hasCalledOnLoadRef.current && isRealDiagram(chartXMLRef.current)) {
return
}
- setChartXML(data.xml)
+ updateChartXML(data.xml)
}
const clearDiagram = () => {
- const emptyDiagram = ` `
+ const emptyDiagram = BLANK_MXFILE
// Skip validation for trusted internal template (loadDiagram also sets chartXML)
loadDiagram(emptyDiagram, true)
setLatestSvg("")
@@ -289,83 +296,88 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
const drawioFormat =
format === "drawio" || format === "xmlsvg" ? "xmlsvg" : format
- // Set up the resolver before triggering export
- saveResolverRef.current = {
- resolver: (exportData: string, fullDiagramXML?: string) => {
- let fileContent: string | Blob
- let mimeType: string
- let extension: string
+ // Each save has its own tag, so two at once never swap results
+ const tag = `save-${++exportSeqRef.current}`
+ exportResolversRef.current[tag] = (
+ exportData: string,
+ fullDiagramXML?: string,
+ ) => {
+ delete exportResolversRef.current[tag]
+ let fileContent: string | Blob
+ let mimeType: string
+ let extension: string
- if (format === "drawio") {
- // Prefer the complete document from the export event so all pages are saved.
- const xml = fullDiagramXML?.trim()
- ? fullDiagramXML
- : extractDiagramXML(exportData)
- let xmlContent = xml
- if (!xml.includes("${xml} `
- }
- fileContent = xmlContent
- mimeType = "application/xml"
- extension = ".drawio"
- } else if (format === "png") {
- // PNG data comes as base64 data URL
- fileContent = exportData
- mimeType = "image/png"
- extension = ".png"
- } else if (format === "xmlsvg") {
- // Editable SVG: pass data URL directly (like PNG)
- fileContent = exportData
- mimeType = "image/svg+xml"
- extension = ".drawio.svg"
- } else {
- // SVG format (view-only)
- fileContent = exportData
- mimeType = "image/svg+xml"
- extension = ".svg"
- }
+ if (format === "drawio") {
+ // Prefer the complete document from the export event so all pages are saved.
+ const xml = fullDiagramXML?.trim()
+ ? fullDiagramXML
+ : extractDiagramXML(exportData)
+ fileContent =
+ normalizeToMxfile(xml, {
+ pageId: "page-1",
+ pageName: "Page-1",
+ }) ?? xml
+ mimeType = "application/xml"
+ extension = ".drawio"
+ } else if (format === "png") {
+ // PNG data comes as base64 data URL
+ fileContent = exportData
+ mimeType = "image/png"
+ extension = ".png"
+ } else if (format === "xmlsvg") {
+ // Editable SVG: pass data URL directly (like PNG)
+ fileContent = exportData
+ mimeType = "image/svg+xml"
+ extension = ".drawio.svg"
+ } else {
+ // SVG format (view-only)
+ fileContent = exportData
+ mimeType = "image/svg+xml"
+ extension = ".svg"
+ }
- // Log save event to Langfuse (flags the trace)
- logSaveToLangfuse(filename, format, sessionId)
+ // Log save event to Langfuse (flags the trace)
+ logSaveToLangfuse(filename, format, sessionId)
- // Handle download
- let url: string
- if (
- typeof fileContent === "string" &&
- fileContent.startsWith("data:")
- ) {
- // Already a data URL (PNG)
- url = fileContent
- } else {
- const blob = new Blob([fileContent], { type: mimeType })
- url = URL.createObjectURL(blob)
- }
+ // Handle download
+ let url: string
+ if (
+ typeof fileContent === "string" &&
+ fileContent.startsWith("data:")
+ ) {
+ // Already a data URL (PNG)
+ url = fileContent
+ } else {
+ const blob = new Blob([fileContent], { type: mimeType })
+ url = URL.createObjectURL(blob)
+ }
- const a = document.createElement("a")
- a.href = url
- a.download = `${filename}${extension}`
- document.body.appendChild(a)
- a.click()
- document.body.removeChild(a)
+ const a = document.createElement("a")
+ a.href = url
+ a.download = `${filename}${extension}`
+ document.body.appendChild(a)
+ a.click()
+ document.body.removeChild(a)
- // Show success toast after download is initiated
- if (successMessage) {
- toast.success(successMessage, {
- position: "bottom-left",
- duration: 2500,
- })
- }
+ // Show success toast after download is initiated
+ if (successMessage) {
+ toast.success(successMessage, {
+ position: "bottom-left",
+ duration: 2500,
+ })
+ }
- // Delay URL revocation to ensure download completes
- if (!url.startsWith("data:")) {
- setTimeout(() => URL.revokeObjectURL(url), 100)
- }
- },
- format,
+ // Delay URL revocation to ensure download completes
+ if (!url.startsWith("data:")) {
+ setTimeout(() => URL.revokeObjectURL(url), 100)
+ }
}
// Export diagram - callback will be handled in handleDiagramExport
- drawioRef.current.exportDiagram({ format: drawioFormat })
+ drawioRef.current.exportDiagram({
+ format: drawioFormat,
+ message: tag,
+ })
}
// Log save event to Langfuse (just flags the trace, doesn't send content)
@@ -389,13 +401,14 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
."`
| gcp2 | 297 | `mxgraph.gcp2` | Google Cloud Platform - Compute Engine, BigQuery, GKE, etc. | [gcp2.md](./gcp2.md) |
| alibaba_cloud | 273 | `mxgraph.alibaba_cloud` | Alibaba Cloud - ECS, OSS, RDS, SLB, VPC, etc. | [alibaba_cloud.md](./alibaba_cloud.md) |
| openstack | 18 | `mxgraph.openstack` | OpenStack cloud platform icons | [openstack.md](./openstack.md) |
-| digitalocean | 74 | `mxgraph.digitalocean` | DigitalOcean - Droplets, Spaces, Kubernetes, etc. | [digitalocean.md](./digitalocean.md) |
| salesforce | 96 | `mxgraph.salesforce` | Salesforce platform icons | [salesforce.md](./salesforce.md) |
## Networking & Infrastructure
@@ -20,7 +19,6 @@ Reference: `style="shape=mxgraph.."`
|---------|--------|--------|-------------|------|
| cisco19 | 232 | `mxgraph.cisco19` | Cisco network equipment - routers, switches, firewalls | [cisco19.md](./cisco19.md) |
| network | 58 | `mxgraph.networks` | General network diagram symbols | [network.md](./network.md) |
-| arista | 45 | `mxgraph.arista` | Arista network switches and equipment | [arista.md](./arista.md) |
| kubernetes | 40 | `mxgraph.kubernetes` | Kubernetes - pods, services, deployments, nodes | [kubernetes.md](./kubernetes.md) |
| vvd | 93 | `mxgraph.vvd` | VMware Validated Design icons | [vvd.md](./vvd.md) |
| rack | 11 | `mxgraph.rack` | Server rack and data center equipment | [rack.md](./rack.md) |
@@ -30,7 +28,6 @@ Reference: `style="shape=mxgraph.."`
| Library | Shapes | Prefix | Description | File |
|---------|--------|--------|-------------|------|
| bpmn | 39 | `mxgraph.bpmn` | Business Process Model and Notation - events, gateways, tasks | [bpmn.md](./bpmn.md) |
-| eip | 36 | `mxgraph.eip` | Enterprise Integration Patterns - messaging, routing | [eip.md](./eip.md) |
| lean_mapping | 13 | `mxgraph.lean_mapping` | Lean/Value Stream Mapping symbols | [lean_mapping.md](./lean_mapping.md) |
## General Diagrams
@@ -48,6 +45,7 @@ Reference: `style="shape=mxgraph.."`
| Library | Shapes | Prefix | Description | File |
|---------|--------|--------|-------------|------|
| android | 17 | `mxgraph.android` | Android UI mockup components | [android.md](./android.md) |
+| material_design | 300 | `image=https://fonts.gstatic.com/...` | Google Material Icons (SVG images) | [material_design.md](./material_design.md) |
## Enterprise Software
@@ -73,6 +71,5 @@ Reference: `style="shape=mxgraph.."`
| Library | Shapes | Prefix | Description | File |
|---------|--------|--------|-------------|------|
| webicons | 176 | `mxgraph.webicons` | Web/social media logos - GitHub, Twitter, AWS, etc. | [webicons.md](./webicons.md) |
-| un-ocha-icons | 242 | `mxgraph.un-ocha-icons` | UN OCHA humanitarian icons | [un-ocha-icons.md](./un-ocha-icons.md) |
-**Total: 33 libraries, 4,281 shapes**
+**Total: 30 libraries, 4,184 shapes**
diff --git a/edge-functions/api/edgeai/chat/completions.ts b/edge-functions/api/edgeai/chat/completions.ts
index eafd4de1..19112ab9 100644
--- a/edge-functions/api/edgeai/chat/completions.ts
+++ b/edge-functions/api/edgeai/chat/completions.ts
@@ -67,41 +67,64 @@ const MODEL_ALIASES: Record = {
"deepseek-v3-0324": "@tx/deepseek-ai/deepseek-v3-0324",
}
-const CORS_HEADERS = {
- "Access-Control-Allow-Origin": "*",
- "Access-Control-Allow-Methods": "POST, OPTIONS",
- "Access-Control-Allow-Headers": "Content-Type, Authorization",
-}
-
/**
- * Create standardized response with CORS headers
+ * Create standardized JSON response
*/
function createResponse(body: any, status = 200, extraHeaders = {}): Response {
return new Response(JSON.stringify(body), {
status,
headers: {
"Content-Type": "application/json",
- ...CORS_HEADERS,
...extraHeaders,
},
})
}
-/**
- * Handle OPTIONS request for CORS preflight
- */
-function handleOptionsRequest(): Response {
- return new Response(null, {
- headers: {
- ...CORS_HEADERS,
- "Access-Control-Max-Age": "86400",
- },
- })
+// Only the app's own server (/api/chat, /api/validate-model) calls this
+// function, so no CORS headers are sent: other sites' pages can't call it
+// from a browser and spend the deployment's Edge AI quota.
+// Same rule as lib/access-code.ts, but reading the edge function's env.
+// No codes configured (or env unavailable) means no check.
+function hasValidAccessCode(request: Request, env: any): boolean {
+ const accessCodes: string[] =
+ env?.ACCESS_CODE_LIST?.split(",")
+ .map((code: string) => code.trim())
+ .filter(Boolean) || []
+ if (accessCodes.length === 0) return true
+ const accessCode = request.headers.get("x-access-code")
+ return !!accessCode && accessCodes.includes(accessCode)
}
-export async function onRequest({ request, env: _env }: any) {
- if (request.method === "OPTIONS") {
- return handleOptionsRequest()
+export async function onRequest({ request, env }: any) {
+ // Requiring JSON also makes any cross-site browser request need a CORS
+ // preflight, which fails without CORS headers. Only the type before any
+ // parameters counts: "text/plain; x=application/json" needs none.
+ const mediaType = (request.headers.get("content-type") ?? "")
+ .split(";")[0]
+ .trim()
+ .toLowerCase()
+ if (request.method !== "POST" || mediaType !== "application/json") {
+ return createResponse(
+ {
+ error: {
+ message: "Expected a POST request with a JSON body",
+ type: "invalid_request_error",
+ },
+ },
+ 400,
+ )
+ }
+
+ if (!hasValidAccessCode(request, env)) {
+ return createResponse(
+ {
+ error: {
+ message: "Invalid or missing access code",
+ type: "invalid_request_error",
+ },
+ },
+ 401,
+ )
}
request.headers.delete("accept-encoding")
@@ -153,7 +176,7 @@ export async function onRequest({ request, env: _env }: any) {
type: "invalid_request_error",
},
},
- 429,
+ 400,
)
}
@@ -216,7 +239,6 @@ export async function onRequest({ request, env: _env }: any) {
"Cache-Control": "no-cache, no-store, no-transform",
"X-Accel-Buffering": "no",
Connection: "keep-alive",
- ...CORS_HEADERS,
},
})
} catch (error: any) {
diff --git a/electron/electron.d.ts b/electron/electron.d.ts
index e268b100..4c54bdcc 100644
--- a/electron/electron.d.ts
+++ b/electron/electron.d.ts
@@ -60,10 +60,6 @@ declare global {
maximize: () => void
/** Close the window */
close: () => void
- /** Open file dialog and return file path */
- openFile: () => Promise
- /** Save data to file via save dialog */
- saveFile: (data: string) => Promise
/** Get proxy configuration */
getProxy: () => Promise
/** Set proxy configuration (saves and restarts server) */
@@ -74,6 +70,15 @@ declare global {
>
/** Set user's preferred locale */
setUserLocale: (locale: string) => Promise
+ /**
+ * Call back after the server restarted on the same port (another
+ * preset); returns a function that stops the calls
+ */
+ onServerRestarted?: (callback: () => void) => () => void
+ /** A chat was saved: open this port next launch */
+ chatSaved?: () => Promise
+ /** The page loaded with this many chats */
+ chatsLoaded?: (count: number) => Promise
}
/** Settings window Electron API */
diff --git a/electron/main/app-menu.ts b/electron/main/app-menu.ts
index bd778294..bb38ab9d 100644
--- a/electron/main/app-menu.ts
+++ b/electron/main/app-menu.ts
@@ -32,6 +32,68 @@ export function rebuildAppMenu(): void {
buildAppMenu()
}
+// Number of the latest preset switch
+let lastSwitch = 0
+
+/**
+ * Apply a preset and restart the server so it takes effect.
+ * If the restart fails, go back to the previous preset and restart again,
+ * so the running server always matches the saved current preset.
+ * Throws an error describing the outcome on failure.
+ */
+export async function switchPreset(
+ id: string,
+): Promise> {
+ const previousPresetId = getCurrentPresetId()
+ const env = applyPresetToEnv(id)
+ if (!env) {
+ throw new Error("Preset not found")
+ }
+ const switchNumber = ++lastSwitch
+ rebuildAppMenu()
+
+ // In development, scripts/electron-dev.mjs restarts the Next.js dev server
+ if (!app.isPackaged) {
+ return env
+ }
+
+ try {
+ await restartNextServer()
+ return env
+ } catch (error) {
+ console.error("Failed to restart server:", error)
+ const reason = error instanceof Error ? error.message : String(error)
+
+ // A newer switch started meanwhile (also of this same preset), or
+ // the preset was deleted: its own restart follows, and undoing
+ // would lose that choice
+ if (switchNumber !== lastSwitch || getCurrentPresetId() !== id) {
+ throw new Error(
+ `The server could not be restarted.\n\nError: ${reason}`,
+ )
+ }
+
+ // Revert to previous preset on failure
+ if (!previousPresetId || !applyPresetToEnv(previousPresetId)) {
+ setCurrentPreset(null)
+ }
+ // Rebuild menu to restore previous checkmark state
+ rebuildAppMenu()
+
+ try {
+ await restartNextServer()
+ } catch (retryError) {
+ console.error("Failed to restart server again:", retryError)
+ throw new Error(
+ `The server could not be restarted.\n\nPlease restart the app.\n\nError: ${reason}`,
+ )
+ }
+ throw new Error(
+ `The server could not be restarted.\n\nThe previous configuration has been restored.\n\nError: ${reason}`,
+ )
+ }
+}
+
/**
* Get the menu template with translations
*/
@@ -192,32 +254,14 @@ function buildConfigMenu(
type: "radio",
checked: preset.id === currentPresetId,
click: async () => {
- const previousPresetId = getCurrentPresetId()
- const env = applyPresetToEnv(preset.id)
-
- if (env) {
- try {
- await restartNextServer()
- rebuildAppMenu() // Rebuild menu to update checkmarks
- } catch (error) {
- console.error("Failed to restart server:", error)
-
- // Revert to previous preset on failure
- if (previousPresetId) {
- applyPresetToEnv(previousPresetId)
- } else {
- setCurrentPreset(null)
- }
-
- // Rebuild menu to restore previous checkmark state
- rebuildAppMenu()
-
- // Show error dialog to notify user
- dialog.showErrorBox(
- "Configuration Error",
- `Failed to apply preset "${preset.name}". The server could not be restarted.\n\nThe previous configuration has been restored.\n\nError: ${error instanceof Error ? error.message : String(error)}`,
- )
- }
+ try {
+ await switchPreset(preset.id)
+ } catch (error) {
+ // Show error dialog to notify user
+ dialog.showErrorBox(
+ "Configuration Error",
+ `Failed to apply preset "${preset.name}". ${error instanceof Error ? error.message : String(error)}`,
+ )
}
},
}))
diff --git a/electron/main/config-manager.ts b/electron/main/config-manager.ts
index 234c79cc..1c3499f7 100644
--- a/electron/main/config-manager.ts
+++ b/electron/main/config-manager.ts
@@ -1,5 +1,11 @@
import { randomUUID } from "node:crypto"
-import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
+import {
+ existsSync,
+ mkdirSync,
+ readFileSync,
+ renameSync,
+ writeFileSync,
+} from "node:fs"
import path from "node:path"
import { app, safeStorage } from "electron"
@@ -30,7 +36,9 @@ let hasWarnedAboutPlaintext = false
* Warns if encryption is not available (API key stored in plaintext)
*/
function encryptValue(value: string): string {
- if (!value) {
+ // Already encrypted (a value that could not be decrypted): keep it as is
+ // instead of wrapping it in a second layer of encryption
+ if (!value || value.startsWith(ENCRYPTED_PREFIX)) {
return value
}
@@ -61,6 +69,7 @@ function encryptValue(value: string): string {
/**
* Decrypt a sensitive value using safeStorage
* Returns the original value if it's not encrypted or decryption fails
+ * (so saving writes the stored ciphertext back unchanged)
*/
function decryptValue(value: string): string {
if (!value || !value.startsWith(ENCRYPTED_PREFIX)) {
@@ -150,6 +159,10 @@ function getConfigFilePath(): string {
return path.join(userDataPath, CONFIG_FILE_NAME)
}
+// The presets file exists but the last read failed: a save now would
+// replace the user's presets with the empty list that read returned
+let presetsUnreadable = false
+
/**
* Load presets from the config file
* Decrypts sensitive fields automatically
@@ -158,6 +171,25 @@ export function loadPresets(): ConfigPresetsFile {
const configPath = getConfigFilePath()
if (!existsSync(configPath)) {
+ // Nothing left that a save could overwrite
+ presetsUnreadable = false
+ return {
+ version: 1,
+ currentPresetId: null,
+ presets: [],
+ userLocale: undefined,
+ }
+ }
+
+ let content: string
+ try {
+ content = readFileSync(configPath, "utf-8")
+ presetsUnreadable = false
+ } catch (error) {
+ // Often only for now (on Windows an antivirus scanner can hold the
+ // file): keep the file, and refuse saves based on this empty list
+ console.error("Failed to read config presets:", error)
+ presetsUnreadable = true
return {
version: 1,
currentPresetId: null,
@@ -167,7 +199,6 @@ export function loadPresets(): ConfigPresetsFile {
}
try {
- const content = readFileSync(configPath, "utf-8")
const data = JSON.parse(content) as ConfigPresetsFile
// Decrypt sensitive fields in each preset
@@ -179,6 +210,17 @@ export function loadPresets(): ConfigPresetsFile {
return data
} catch (error) {
console.error("Failed to load config presets:", error)
+ // Move the unreadable file aside so the next save can't overwrite
+ // the user's presets with an empty list
+ const backupPath = `${configPath}.corrupt-${Date.now()}`
+ try {
+ renameSync(configPath, backupPath)
+ console.error(`Unreadable config presets moved to ${backupPath}`)
+ } catch (renameError) {
+ // Still there: refuse saves that would overwrite it
+ console.error("Failed to back up config presets:", renameError)
+ presetsUnreadable = true
+ }
return {
version: 1,
currentPresetId: null,
@@ -193,6 +235,11 @@ export function loadPresets(): ConfigPresetsFile {
* Encrypts sensitive fields automatically
*/
export function savePresets(data: ConfigPresetsFile): void {
+ if (presetsUnreadable) {
+ throw new Error(
+ "The presets file could not be read, so it was not overwritten. Please try again.",
+ )
+ }
const configPath = getConfigFilePath()
const userDataPath = app.getPath("userData")
@@ -211,7 +258,11 @@ export function savePresets(data: ConfigPresetsFile): void {
}
try {
- writeFileSync(configPath, JSON.stringify(dataToSave, null, 2), "utf-8")
+ // Write a temp file and rename it, so a crash mid-write can't leave
+ // a truncated config file
+ const tempPath = `${configPath}.tmp`
+ writeFileSync(tempPath, JSON.stringify(dataToSave, null, 2), "utf-8")
+ renameSync(tempPath, configPath)
} catch (error) {
console.error("Failed to save config presets:", error)
throw error
@@ -307,9 +358,10 @@ export function deletePreset(id: string): boolean {
data.presets.splice(index, 1)
- // Clear current preset if it was deleted
+ // Clear current preset (and its env vars) if it was deleted
if (data.currentPresetId === id) {
data.currentPresetId = null
+ setPresetEnv(null)
}
savePresets(data)
@@ -322,13 +374,15 @@ export function deletePreset(id: string): boolean {
export function setCurrentPreset(id: string | null): boolean {
const data = loadPresets()
+ let preset: ConfigPreset | null = null
if (id !== null) {
- const preset = data.presets.find((p) => p.id === id)
+ preset = data.presets.find((p) => p.id === id) || null
if (!preset) {
return false
}
}
+ setPresetEnv(preset)
data.currentPresetId = id
savePresets(data)
return true
@@ -365,78 +419,23 @@ const PROVIDER_ENV_MAP: Record = {
}
/**
- * Apply preset environment variables to the current process
- * Returns the environment variables that were applied
- */
-export function applyPresetToEnv(id: string): Record | null {
- const data = loadPresets()
- const preset = data.presets.find((p) => p.id === id)
-
- if (!preset) {
- return null
- }
-
- const appliedEnv: Record = {}
- const provider = preset.config.AI_PROVIDER?.toLowerCase()
-
- for (const [key, value] of Object.entries(preset.config)) {
- if (value !== undefined && value !== "") {
- // Map generic AI_API_KEY to provider-specific key
- if (
- key === "AI_API_KEY" &&
- provider &&
- PROVIDER_ENV_MAP[provider]
- ) {
- const providerApiKey = PROVIDER_ENV_MAP[provider].apiKey
- if (providerApiKey) {
- process.env[providerApiKey] = value
- appliedEnv[providerApiKey] = value
- }
- }
- // Map generic AI_BASE_URL to provider-specific key
- else if (
- key === "AI_BASE_URL" &&
- provider &&
- PROVIDER_ENV_MAP[provider]
- ) {
- const providerBaseUrl = PROVIDER_ENV_MAP[provider].baseUrl
- if (providerBaseUrl) {
- process.env[providerBaseUrl] = value
- appliedEnv[providerBaseUrl] = value
- }
- }
- // Apply other env vars directly
- else {
- process.env[key] = value
- appliedEnv[key] = value
- }
- }
- }
-
- // Set as current preset
- data.currentPresetId = id
- savePresets(data)
-
- return appliedEnv
-}
-
-/**
- * Get environment variables from current preset
+ * Map a preset's config to environment variables
* Maps generic AI_API_KEY/AI_BASE_URL to provider-specific keys
*/
-export function getCurrentPresetEnv(): Record {
- const preset = getCurrentPreset()
- if (!preset) {
- return {}
- }
-
+function presetToEnv(preset: ConfigPreset): Record {
const env: Record = {}
const provider = preset.config.AI_PROVIDER?.toLowerCase()
for (const [key, value] of Object.entries(preset.config)) {
if (value !== undefined && value !== "") {
+ // A key that could not be decrypted is useless to the server
+ if (value.startsWith(ENCRYPTED_PREFIX)) {
+ console.warn(
+ `Preset "${preset.name}": ${key} could not be decrypted. Please enter it again in Settings.`,
+ )
+ }
// Map generic AI_API_KEY to provider-specific key
- if (
+ else if (
key === "AI_API_KEY" &&
provider &&
PROVIDER_ENV_MAP[provider]
@@ -466,6 +465,90 @@ export function getCurrentPresetEnv(): Record {
return env
}
+/**
+ * Values that env vars had before a preset first set them
+ * (from the system or .env files), and the keys the active preset set
+ */
+const originalEnv: Record = {}
+let presetEnvKeys: string[] = []
+
+/**
+ * Replace the env vars of the previous preset with those of the given preset
+ * (null leaves no preset applied). Restoring first means switching presets
+ * never leaves the previous preset's base URL, model or key behind.
+ */
+function setPresetEnv(preset: ConfigPreset | null): Record {
+ for (const key of presetEnvKeys) {
+ if (originalEnv[key] === undefined) {
+ delete process.env[key]
+ } else {
+ process.env[key] = originalEnv[key]
+ }
+ }
+
+ const env = preset ? presetToEnv(preset) : {}
+ for (const [key, value] of Object.entries(env)) {
+ if (!(key in originalEnv)) {
+ originalEnv[key] = process.env[key]
+ }
+ process.env[key] = value
+ }
+ presetEnvKeys = Object.keys(env)
+
+ writeDevPresetEnv(env)
+ return env
+}
+
+const DEV_ENV_FILE_NAME = "dev-preset-env.json"
+
+/**
+ * Development only: write the active preset's env vars (decrypted and mapped)
+ * for scripts/electron-dev.mjs, which restarts the Next.js dev server when
+ * this file changes. The dev server can't decrypt the config file itself.
+ */
+function writeDevPresetEnv(env: Record): void {
+ if (app.isPackaged) {
+ return
+ }
+ try {
+ const filePath = path.join(app.getPath("userData"), DEV_ENV_FILE_NAME)
+ writeFileSync(filePath, JSON.stringify(env, null, 2), {
+ encoding: "utf-8",
+ mode: 0o600,
+ })
+ } catch (error) {
+ console.error("Failed to write dev preset env:", error)
+ }
+}
+
+/**
+ * Apply preset environment variables to the current process
+ * Returns the environment variables that were applied
+ */
+export function applyPresetToEnv(id: string): Record | null {
+ const data = loadPresets()
+ const preset = data.presets.find((p) => p.id === id)
+
+ if (!preset) {
+ return null
+ }
+
+ const appliedEnv = setPresetEnv(preset)
+
+ // Set as current preset
+ data.currentPresetId = id
+ savePresets(data)
+
+ return appliedEnv
+}
+
+/**
+ * Apply the saved current preset's environment variables (used at startup)
+ */
+export function applyCurrentPresetToEnv(): void {
+ setPresetEnv(getCurrentPreset())
+}
+
/**
* Get user's preferred locale from config
* Returns undefined if not set
diff --git a/electron/main/env-loader.ts b/electron/main/env-loader.ts
index 8182b11a..563b3729 100644
--- a/electron/main/env-loader.ts
+++ b/electron/main/env-loader.ts
@@ -28,6 +28,25 @@ export function loadEnvFile(): void {
console.log("No .env file found, using system environment variables")
}
+/**
+ * Index of the quote that closes a value starting with a quote, or -1. A
+ * backslash before the quote character escapes it, as in dotenv; the
+ * backslash stays in the value. As in dotenv, an escaped quote with only a
+ * comment or nothing after it still closes the value when no other quote
+ * does ("C:\dir\" keeps its trailing backslash).
+ */
+function findClosingQuote(value: string): number {
+ const quote = value[0]
+ let lastEscaped = -1
+ for (let i = 1; i < value.length; i++) {
+ if (value[i] === "\\" && value[i + 1] === quote) {
+ i++
+ if (/^\s*(#.*)?$/.test(value.slice(i + 1))) lastEscaped = i
+ } else if (value[i] === quote) return i
+ }
+ return lastEscaped
+}
+
/**
* Parse and load environment variables from a file
*/
@@ -48,12 +67,28 @@ function loadEnvFromFile(filePath: string): void {
const key = trimmed.slice(0, equalIndex).trim()
let value = trimmed.slice(equalIndex + 1).trim()
- // Remove surrounding quotes
+ const quote = value[0]
+ const closingQuote =
+ quote === '"' || quote === "'" ? findClosingQuote(value) : -1
if (
- (value.startsWith('"') && value.endsWith('"')) ||
- (value.startsWith("'") && value.endsWith("'"))
+ closingQuote > 0 &&
+ /^\s*(#.*)?$/.test(value.slice(closingQuote + 1))
) {
- value = value.slice(1, -1)
+ // Quoted value, then nothing or a comment: keep what is
+ // inside the quotes, as dotenv reads it
+ value = value.slice(1, closingQuote)
+ } else {
+ // Unquoted value: drop an inline comment ("value # comment").
+ // A value quoted from start to end with quotes inside (JSON
+ // with an apostrophe) loses only the outer two, as in dotenv.
+ value = value.replace(/\s+#.*$/, "")
+ if (
+ closingQuote > 0 &&
+ value.length > 1 &&
+ value.endsWith(quote)
+ ) {
+ value = value.slice(1, -1)
+ }
}
// Don't override existing environment variables
diff --git a/electron/main/index.ts b/electron/main/index.ts
index 6c613da2..08313b1a 100644
--- a/electron/main/index.ts
+++ b/electron/main/index.ts
@@ -1,12 +1,17 @@
import { app, BrowserWindow, dialog, shell } from "electron"
import { buildAppMenu } from "./app-menu"
-import { getCurrentPresetEnv } from "./config-manager"
+import { applyCurrentPresetToEnv } from "./config-manager"
import { loadEnvFile } from "./env-loader"
import { registerIpcHandlers } from "./ipc-handlers"
import { startNextServer, stopNextServer } from "./next-server"
import { applyProxyToEnv } from "./proxy-manager"
import { registerSettingsWindowHandlers } from "./settings-window"
-import { createWindow, getMainWindow } from "./window-manager"
+import {
+ createWindow,
+ getAppUrl,
+ getMainWindow,
+ isAppUrl,
+} from "./window-manager"
// Single instance lock
const gotTheLock = app.requestSingleInstanceLock()
@@ -28,16 +33,14 @@ if (!gotTheLock) {
// Apply proxy settings from saved config
applyProxyToEnv()
- // Apply saved preset environment variables (overrides .env)
- const presetEnv = getCurrentPresetEnv()
- for (const [key, value] of Object.entries(presetEnv)) {
- process.env[key] = value
- }
-
- const isDev = process.env.NODE_ENV === "development"
- let serverUrl: string | null = null
+ const isDev = !app.isPackaged
app.whenReady().then(async () => {
+ // Apply saved preset environment variables (overrides .env).
+ // Must run after ready: on Windows and Linux safeStorage can't
+ // decrypt the API key before that.
+ applyCurrentPresetToEnv()
+
// Register IPC handlers
registerIpcHandlers()
registerSettingsWindowHandlers()
@@ -46,6 +49,7 @@ if (!gotTheLock) {
buildAppMenu()
try {
+ let serverUrl: string
if (isDev) {
// Development: use the dev server URL
serverUrl =
@@ -69,8 +73,9 @@ if (!gotTheLock) {
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
- if (serverUrl) {
- createWindow(serverUrl)
+ const appUrl = getAppUrl()
+ if (appUrl) {
+ createWindow(appUrl)
}
}
})
@@ -87,24 +92,49 @@ if (!gotTheLock) {
stopNextServer()
})
+ // Pages allowed inside app windows: the app server and draw.io
+ const isInAppUrl = (url: string): boolean => {
+ if (isAppUrl(url)) return true
+ try {
+ const { hostname } = new URL(url)
+ return ["diagrams.net", "draw.io"].some(
+ (domain) =>
+ hostname === domain || hostname.endsWith(`.${domain}`),
+ )
+ } catch {
+ return false
+ }
+ }
+
+ const isWebUrl = (url: string): boolean =>
+ url.startsWith("http://") || url.startsWith("https://")
+
// Open external links in default browser
app.on("web-contents-created", (_, contents) => {
contents.setWindowOpenHandler(({ url }) => {
- // Allow diagrams.net iframe
- if (
- url.includes("diagrams.net") ||
- url.includes("draw.io") ||
- url.startsWith("http://localhost") ||
- url.startsWith("http://127.0.0.1")
- ) {
+ if (isInAppUrl(url)) {
return { action: "allow" }
}
// Open other links in external browser
- if (url.startsWith("http://") || url.startsWith("https://")) {
+ if (isWebUrl(url)) {
shell.openExternal(url)
return { action: "deny" }
}
return { action: "allow" }
})
+
+ // Clicking a plain link would otherwise replace the app page with
+ // an external site that keeps the preload API. Only the page
+ // itself may navigate there; draw.io stays in its frame (this event
+ // is for the main frame only)
+ contents.on("will-navigate", (event) => {
+ if (isAppUrl(event.url)) {
+ return
+ }
+ event.preventDefault()
+ if (isWebUrl(event.url)) {
+ shell.openExternal(event.url)
+ }
+ })
})
}
diff --git a/electron/main/ipc-handlers.ts b/electron/main/ipc-handlers.ts
index cc4155f6..8ccddd60 100644
--- a/electron/main/ipc-handlers.ts
+++ b/electron/main/ipc-handlers.ts
@@ -1,7 +1,6 @@
-import { app, BrowserWindow, dialog, ipcMain } from "electron"
-import { rebuildAppMenu } from "./app-menu"
+import { app, BrowserWindow, type IpcMainInvokeEvent, ipcMain } from "electron"
+import { rebuildAppMenu, switchPreset } from "./app-menu"
import {
- applyPresetToEnv,
type ConfigPreset,
createPreset,
deletePreset,
@@ -14,12 +13,14 @@ import {
updatePreset,
} from "./config-manager"
import { restartNextServer } from "./next-server"
+import { noteNoChats, rememberChatPort } from "./port-manager"
import {
applyProxyToEnv,
getProxyConfig,
type ProxyConfig,
saveProxyConfig,
} from "./proxy-manager"
+import { isAppUrl } from "./window-manager"
/**
* Allowed configuration keys for presets
@@ -48,16 +49,44 @@ function sanitizePresetConfig(
return sanitized
}
+/**
+ * Register an IPC handler that only answers the app's own pages
+ * (the main window on the app server, or the local settings page).
+ * A main window that somehow ends up on an external site still gets the
+ * preload API, so its calls must be rejected here.
+ */
+function handle(
+ channel: string,
+ listener: (event: IpcMainInvokeEvent, ...args: Args) => unknown,
+): void {
+ ipcMain.handle(channel, (event, ...args) => {
+ const url = event.senderFrame?.url
+ if (!isAppUrl(url) && !url?.startsWith("file://")) {
+ throw new Error(`Blocked "${channel}" from untrusted page: ${url}`)
+ }
+ return listener(event, ...(args as Args))
+ })
+}
+
/**
* Register all IPC handlers
*/
export function registerIpcHandlers(): void {
// ==================== App Info ====================
- ipcMain.handle("get-version", () => {
+ handle("get-version", () => {
return app.getVersion()
})
+ // ==================== Where the chats are ====================
+
+ // The page saved a chat, or loaded without any: decides which port
+ // (and so which origin's chats) the next launch opens
+ handle("chat-saved", () => rememberChatPort())
+ handle("chats-loaded", (_event, count: unknown) => {
+ if (count === 0) noteNoChats()
+ })
+
// ==================== Window Controls ====================
ipcMain.on("window-minimize", (event) => {
@@ -79,84 +108,30 @@ export function registerIpcHandlers(): void {
win?.close()
})
- // ==================== File Dialogs ====================
-
- ipcMain.handle("dialog-open-file", async (event) => {
- const win = BrowserWindow.fromWebContents(event.sender)
- if (!win) return null
-
- const result = await dialog.showOpenDialog(win, {
- properties: ["openFile"],
- filters: [
- { name: "Draw.io Files", extensions: ["drawio", "xml"] },
- { name: "All Files", extensions: ["*"] },
- ],
- })
-
- if (result.canceled || result.filePaths.length === 0) {
- return null
- }
-
- // Read the file content
- const fs = await import("node:fs/promises")
- try {
- const content = await fs.readFile(result.filePaths[0], "utf-8")
- return content
- } catch (error) {
- console.error("Failed to read file:", error)
- return null
- }
- })
-
- ipcMain.handle("dialog-save-file", async (event, data: string) => {
- const win = BrowserWindow.fromWebContents(event.sender)
- if (!win) return false
-
- const result = await dialog.showSaveDialog(win, {
- filters: [
- { name: "Draw.io Files", extensions: ["drawio"] },
- { name: "XML Files", extensions: ["xml"] },
- ],
- })
-
- if (result.canceled || !result.filePath) {
- return false
- }
-
- const fs = await import("node:fs/promises")
- try {
- await fs.writeFile(result.filePath, data, "utf-8")
- return true
- } catch (error) {
- console.error("Failed to save file:", error)
- return false
- }
- })
-
// ==================== Config Presets ====================
- ipcMain.handle("config-presets:get-all", () => {
+ handle("config-presets:get-all", () => {
return getAllPresets()
})
- ipcMain.handle("config-presets:get-current", () => {
+ handle("config-presets:get-current", () => {
return getCurrentPreset()
})
- ipcMain.handle("config-presets:get-current-id", () => {
+ handle("config-presets:get-current-id", () => {
return getCurrentPresetId()
})
- ipcMain.handle(
+ handle(
"config-presets:save",
- (
+ async (
_event,
preset: Omit & {
id?: string
},
) => {
// Validate preset name
- if (typeof preset.name !== "string" || !preset.name.trim()) {
+ if (typeof preset?.name !== "string" || !preset.name.trim()) {
throw new Error("Invalid preset name")
}
@@ -165,42 +140,48 @@ export function registerIpcHandlers(): void {
if (preset.id) {
// Update existing preset
- return updatePreset(preset.id, {
+ const updated = updatePreset(preset.id, {
name: preset.name.trim(),
config: sanitizedConfig,
})
+ // Re-apply the active preset so the edit takes effect
+ if (updated && updated.id === getCurrentPresetId()) {
+ await switchPreset(updated.id)
+ } else {
+ rebuildAppMenu()
+ }
+ return updated
}
// Create new preset
- return createPreset({
+ const created = createPreset({
name: preset.name.trim(),
config: sanitizedConfig,
})
+ rebuildAppMenu()
+ return created
},
)
- ipcMain.handle("config-presets:delete", (_event, id: string) => {
- return deletePreset(id)
+ handle("config-presets:delete", async (_event, id: string) => {
+ const wasCurrent = id === getCurrentPresetId()
+ // Deleting the active preset also clears its env vars
+ const deleted = deletePreset(id)
+ rebuildAppMenu()
+
+ // Restart so the server stops using the deleted preset
+ if (deleted && wasCurrent && app.isPackaged) {
+ await restartNextServer()
+ }
+ return deleted
})
- ipcMain.handle("config-presets:apply", async (_event, id: string) => {
- const env = applyPresetToEnv(id)
- if (!env) {
- return { success: false, error: "Preset not found" }
- }
-
- const isDev = process.env.NODE_ENV === "development"
-
- if (isDev) {
- // In development mode, the config file change will trigger
- // the file watcher in electron-dev.mjs to restart Next.js
- // We just need to save the preset (already done in applyPresetToEnv)
- return { success: true, env, devMode: true }
- }
-
- // Production mode: restart the Next.js server to apply new environment variables
+ handle("config-presets:apply", async (_event, id: string) => {
try {
- await restartNextServer()
- return { success: true, env }
+ const env = await switchPreset(id)
+ // In development mode, electron-dev.mjs restarts Next.js
+ return app.isPackaged
+ ? { success: true, env }
+ : { success: true, env, devMode: true }
} catch (error) {
return {
success: false,
@@ -212,30 +193,39 @@ export function registerIpcHandlers(): void {
}
})
- ipcMain.handle(
- "config-presets:set-current",
- (_event, id: string | null) => {
- return setCurrentPreset(id)
- },
- )
+ handle("config-presets:set-current", (_event, id: string | null) => {
+ return setCurrentPreset(id)
+ })
// ==================== Proxy Settings ====================
- ipcMain.handle("get-proxy", () => {
+ handle("get-proxy", () => {
return getProxyConfig()
})
- ipcMain.handle("set-proxy", async (_event, config: ProxyConfig) => {
+ handle("set-proxy", async (_event, config: ProxyConfig) => {
+ const isOptionalString = (value: unknown) =>
+ value === undefined || typeof value === "string"
+ if (
+ typeof config !== "object" ||
+ config === null ||
+ !isOptionalString(config.httpProxy) ||
+ !isOptionalString(config.httpsProxy)
+ ) {
+ return { success: false, error: "Invalid proxy settings" }
+ }
+
try {
// Save config to file
- saveProxyConfig(config)
+ saveProxyConfig({
+ httpProxy: config.httpProxy,
+ httpsProxy: config.httpsProxy,
+ })
// Apply to current process environment
applyProxyToEnv()
- const isDev = process.env.NODE_ENV === "development"
-
- if (isDev) {
+ if (!app.isPackaged) {
// In development, env vars are already applied
// Next.js dev server may need manual restart
return { success: true, devMode: true }
@@ -257,11 +247,11 @@ export function registerIpcHandlers(): void {
// ==================== User Locale ====================
- ipcMain.handle("get-user-locale", () => {
+ handle("get-user-locale", () => {
return getUserLocale()
})
- ipcMain.handle("set-user-locale", (_event, locale: string) => {
+ handle("set-user-locale", (_event, locale: string) => {
// Validate locale is one of the supported values
if (!["en", "zh", "ja", "zh-Hant"].includes(locale)) {
return { success: false, error: "Invalid locale" }
diff --git a/electron/main/next-server.ts b/electron/main/next-server.ts
index 7ca82fc2..0a09ed9f 100644
--- a/electron/main/next-server.ts
+++ b/electron/main/next-server.ts
@@ -7,9 +7,20 @@ import {
getServerUrl,
isPortAvailable,
} from "./port-manager"
+import { setAppUrl } from "./window-manager"
let serverProcess: UtilityProcess | null = null
+// Start and restart run one at a time, so overlapping calls (e.g. two quick
+// preset switches) can't leave two servers running
+let serverQueue: Promise = Promise.resolve()
+
+function runExclusive(task: () => Promise): Promise {
+ const result = serverQueue.then(task)
+ serverQueue = result.catch(() => {})
+ return result
+}
+
/**
* Get the path to the standalone server resources
* In packaged app: resources/standalone
@@ -45,7 +56,11 @@ async function waitForServer(url: string, timeout = 30000): Promise {
* Start the Next.js standalone server using Electron's utilityProcess
* This API is designed for running Node.js code in the background
*/
-export async function startNextServer(): Promise {
+export function startNextServer(): Promise {
+ return runExclusive(startServer)
+}
+
+async function startServer(): Promise {
const resourcePath = getResourcePath()
const serverPath = path.join(resourcePath, "server.js")
@@ -71,6 +86,13 @@ export async function startNextServer(): Promise {
HOSTNAME: "127.0.0.1",
// Enable Node.js built-in proxy support for fetch (Node.js 24+)
NODE_USE_ENV_PROXY: "1",
+ // The preset keys are the user's own, not a server's
+ NEXT_AI_DRAWIO_DESKTOP: "1",
+ }
+
+ // Keep requests to local model servers (e.g. Ollama) off the proxy
+ if (!process.env.NO_PROXY && !process.env.no_proxy) {
+ env.NO_PROXY = "localhost,127.0.0.1,[::1]"
}
// Set cache directory to a writable location (user's app data folder)
@@ -96,23 +118,27 @@ export async function startNextServer(): Promise {
// Use Electron's utilityProcess API for running Node.js in background
// This is the recommended way to run Node.js code in Electron
- serverProcess = utilityProcess.fork(serverPath, [], {
+ const proc = utilityProcess.fork(serverPath, [], {
cwd: resourcePath,
env,
stdio: "pipe",
})
+ serverProcess = proc
- serverProcess.stdout?.on("data", (data) => {
+ proc.stdout?.on("data", (data) => {
console.log(`[Next.js] ${data.toString().trim()}`)
})
- serverProcess.stderr?.on("data", (data) => {
+ proc.stderr?.on("data", (data) => {
console.error(`[Next.js Error] ${data.toString().trim()}`)
})
- serverProcess.on("exit", (code) => {
+ proc.on("exit", (code) => {
console.log(`Next.js server exited with code ${code}`)
- serverProcess = null
+ // An old server can exit after a new one started; keep the new one
+ if (serverProcess === proc) {
+ serverProcess = null
+ }
})
const url = getServerUrl()
@@ -126,39 +152,36 @@ export async function startNextServer(): Promise {
* Stop the Next.js server process and wait for it to exit
*/
export async function stopNextServer(): Promise {
- if (serverProcess) {
- console.log("Stopping Next.js server...")
+ const proc = serverProcess
+ if (!proc) {
+ return
+ }
+ console.log("Stopping Next.js server...")
+ serverProcess = null
- // Create a promise that resolves when the process exits
- const exitPromise = new Promise((resolve) => {
- const proc = serverProcess
- if (!proc) {
- resolve()
- return
- }
-
- const onExit = () => {
- resolve()
- }
-
- proc.once("exit", onExit)
-
- // Timeout after 5 seconds
- setTimeout(() => {
- proc.removeListener("exit", onExit)
- resolve()
- }, 5000)
+ // Resolves true when the process exits, false after the timeout
+ const waitForExit = (ms: number) =>
+ new Promise((resolve) => {
+ proc.once("exit", () => resolve(true))
+ setTimeout(() => resolve(false), ms)
})
- serverProcess.kill()
- serverProcess = null
+ proc.kill()
- // Wait for process to exit
- await exitPromise
-
- // Additional wait for OS to release port
- await new Promise((resolve) => setTimeout(resolve, 500))
+ // Next.js waits for open requests (e.g. a streaming reply) before it
+ // exits, so force kill it if it is still running after 5 seconds
+ if (!(await waitForExit(5000)) && proc.pid) {
+ console.warn("Next.js server did not exit in time, force killing it")
+ try {
+ process.kill(proc.pid, "SIGKILL")
+ } catch (error) {
+ console.error("Failed to force kill Next.js server:", error)
+ }
+ await waitForExit(2000)
}
+
+ // Additional wait for OS to release port
+ await new Promise((resolve) => setTimeout(resolve, 500))
}
/**
@@ -184,15 +207,19 @@ async function waitForServerStop(timeout = 5000): Promise {
/**
* Restart the Next.js server with new environment variables
*/
-export async function restartNextServer(): Promise {
- console.log("Restarting Next.js server...")
+export function restartNextServer(): Promise {
+ return runExclusive(async () => {
+ console.log("Restarting Next.js server...")
- // Stop the current server and wait for it to exit
- await stopNextServer()
+ // Stop the current server and wait for it to exit
+ await stopNextServer()
- // Wait for the port to be released
- await waitForServerStop()
+ // Wait for the port to be released
+ await waitForServerStop()
- // Start the server again
- return startNextServer()
+ // Start the server again, and follow it if it moved to another port
+ const url = await startServer()
+ setAppUrl(url)
+ return url
+ })
}
diff --git a/electron/main/port-manager.ts b/electron/main/port-manager.ts
index 299ff427..509bc9ab 100644
--- a/electron/main/port-manager.ts
+++ b/electron/main/port-manager.ts
@@ -1,4 +1,6 @@
+import { existsSync, readFileSync, writeFileSync } from "node:fs"
import net from "node:net"
+import path from "node:path"
import { app } from "electron"
/**
@@ -23,6 +25,69 @@ const PORT_CONFIG = {
*/
let allocatedPort: number | null = null
+/**
+ * Whether chats are saved under http://127.0.0.1:: Electron keeps
+ * each origin's IndexedDB in its own folder
+ */
+function hasStoredData(port: number): boolean {
+ return existsSync(
+ path.join(
+ app.getPath("userData"),
+ "IndexedDB",
+ `http_127.0.0.1_${port}.indexeddb.leveldb`,
+ ),
+ )
+}
+
+// The two fixed production ports, the only ones whose origin (and so its
+// chats and settings) is the same at every launch
+const HOME_PORTS = [PORT_CONFIG.legacyProduction, PORT_CONFIG.production]
+
+const chatPortFile = () => path.join(app.getPath("userData"), "chat-port.json")
+
+/** The fixed port where a chat was last saved, if known */
+function readChatPort(): number | null {
+ try {
+ const { port } = JSON.parse(readFileSync(chatPortFile(), "utf-8"))
+ return HOME_PORTS.includes(port) ? port : null
+ } catch {
+ return null
+ }
+}
+
+function writeChatPort(port: number): void {
+ try {
+ writeFileSync(chatPortFile(), JSON.stringify({ port }))
+ } catch (error) {
+ console.warn("Could not save the chat port:", error)
+ }
+}
+
+/**
+ * The page saved a chat: open on this port next time. Chats of the two
+ * ports cannot be shown together (each origin has its own storage), so the
+ * app opens where the user last worked. A launch that had to use the other
+ * port and saved nothing does not move it.
+ */
+export function rememberChatPort(): void {
+ const port = allocatedPort
+ if (!app.isPackaged || port === null || !HOME_PORTS.includes(port)) return
+ if (readChatPort() !== port) writeChatPort(port)
+}
+
+/**
+ * The page loaded without any chats. Before any chat was saved under this
+ * version (no file yet), the user's chats may be on the other fixed port,
+ * where an older version opened: try it first next time.
+ */
+export function noteNoChats(): void {
+ const port = allocatedPort
+ if (!app.isPackaged || port === null || !HOME_PORTS.includes(port)) return
+ if (existsSync(chatPortFile())) return
+ const other = HOME_PORTS.find((p) => p !== port)
+ if (other !== undefined && hasStoredData(other)) writeChatPort(other)
+}
+
/**
* Check if a specific port is available
*/
@@ -44,7 +109,8 @@ export function isPortAvailable(port: number): Promise {
/**
* Find an available port
* - In development: uses fixed port (6002)
- * - In production: uses fixed port (13370) to preserve localStorage
+ * - In production: uses the legacy port (61337), then 13370, to preserve
+ * localStorage; 13370 first when only it has saved chats
* - Falls back to sequential ports if preferred port is unavailable
* - Last resort: lets the OS assign a port (port 0)
*
@@ -69,21 +135,26 @@ export async function findAvailablePort(reuseExisting = true): Promise {
allocatedPort = null
}
- // In production, try legacy port first to preserve existing users' localStorage
- if (!isDev) {
- const legacyPort = PORT_CONFIG.legacyProduction
- if (await isPortAvailable(legacyPort)) {
- allocatedPort = legacyPort
- return legacyPort
+ // In production, first the port where a chat was last saved. Without
+ // one, the legacy port first to preserve existing users' data, unless
+ // only the new port has data: their app started on 13370 while Windows
+ // reserved 61337, and 61337 being free now would hide it
+ const chatPort = isDev ? null : readChatPort()
+ const candidates = isDev
+ ? [preferredPort]
+ : chatPort !== null
+ ? [chatPort, ...HOME_PORTS.filter((p) => p !== chatPort)]
+ : hasStoredData(PORT_CONFIG.production) &&
+ !hasStoredData(PORT_CONFIG.legacyProduction)
+ ? [PORT_CONFIG.production, PORT_CONFIG.legacyProduction]
+ : [PORT_CONFIG.legacyProduction, PORT_CONFIG.production]
+ for (const port of candidates) {
+ if (await isPortAvailable(port)) {
+ allocatedPort = port
+ return port
}
}
- // Try preferred port
- if (await isPortAvailable(preferredPort)) {
- allocatedPort = preferredPort
- return preferredPort
- }
-
console.warn(
`Preferred port ${preferredPort} is in use, finding alternative...`,
)
diff --git a/electron/main/proxy-manager.ts b/electron/main/proxy-manager.ts
index cb41de56..3f4c2f8a 100644
--- a/electron/main/proxy-manager.ts
+++ b/electron/main/proxy-manager.ts
@@ -13,18 +13,22 @@ function getConfigPath(): string {
/**
* Load proxy configuration from JSON file
+ * Returns null if the user never saved proxy settings (or the file is invalid)
*/
-export function loadProxyConfig(): ProxyConfig {
+export function loadProxyConfig(): ProxyConfig | null {
try {
const configPath = getConfigPath()
if (fs.existsSync(configPath)) {
- const data = fs.readFileSync(configPath, "utf-8")
- return JSON.parse(data) as ProxyConfig
+ const data = JSON.parse(fs.readFileSync(configPath, "utf-8"))
+ if (data && typeof data === "object" && !Array.isArray(data)) {
+ return data as ProxyConfig
+ }
+ console.error("Ignoring invalid proxy config:", data)
}
} catch (error) {
console.error("Failed to load proxy config:", error)
}
- return {}
+ return null
}
/**
@@ -33,7 +37,11 @@ export function loadProxyConfig(): ProxyConfig {
export function saveProxyConfig(config: ProxyConfig): void {
try {
const configPath = getConfigPath()
- fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8")
+ // Write a temp file and rename it, so a crash mid-write can't leave
+ // a truncated file
+ const tempPath = `${configPath}.tmp`
+ fs.writeFileSync(tempPath, JSON.stringify(config, null, 2), "utf-8")
+ fs.renameSync(tempPath, configPath)
} catch (error) {
console.error("Failed to save proxy config:", error)
throw error
@@ -47,6 +55,11 @@ export function saveProxyConfig(config: ProxyConfig): void {
export function applyProxyToEnv(): void {
const config = loadProxyConfig()
+ // No saved settings: keep proxy vars inherited from the system or .env
+ if (!config) {
+ return
+ }
+
if (config.httpProxy) {
process.env.HTTP_PROXY = config.httpProxy
process.env.http_proxy = config.httpProxy
diff --git a/electron/main/window-manager.ts b/electron/main/window-manager.ts
index 7b515715..e533f500 100644
--- a/electron/main/window-manager.ts
+++ b/electron/main/window-manager.ts
@@ -3,6 +3,9 @@ import { app, BrowserWindow, screen } from "electron"
let mainWindow: BrowserWindow | null = null
+// URL of the app server the main window loads
+let appUrl: string | null = null
+
/**
* Get the icon path based on platform
* Note: electron-builder converts icon.png during packaging,
@@ -28,6 +31,7 @@ function getIconPath(): string | undefined {
* Create the main application window
*/
export function createWindow(serverUrl: string): BrowserWindow {
+ appUrl = serverUrl
const { width, height } = screen.getPrimaryDisplay().workAreaSize
mainWindow = new BrowserWindow({
@@ -56,7 +60,7 @@ export function createWindow(serverUrl: string): BrowserWindow {
})
// Open DevTools in development
- if (process.env.NODE_ENV === "development") {
+ if (!app.isPackaged) {
mainWindow.webContents.openDevTools()
}
@@ -93,3 +97,39 @@ export function createWindow(serverUrl: string): BrowserWindow {
export function getMainWindow(): BrowserWindow | null {
return mainWindow
}
+
+/**
+ * Get the app server URL the main window loads
+ */
+export function getAppUrl(): string | null {
+ return appUrl
+}
+
+/**
+ * Point the main window at the restarted app server (it can come up on a
+ * different port). On the same port the page fetches the new preset's
+ * server models instead of sending the old preset's choice; it is not
+ * reloaded, which would drop unsent attachments.
+ */
+export function setAppUrl(url: string): void {
+ if (url === appUrl) {
+ mainWindow?.webContents.send("server-restarted")
+ return
+ }
+ appUrl = url
+ mainWindow?.loadURL(url)
+}
+
+/**
+ * Check if a URL belongs to the app server (same origin)
+ */
+export function isAppUrl(url: string | undefined): boolean {
+ if (!url || !appUrl) {
+ return false
+ }
+ try {
+ return new URL(url).origin === new URL(appUrl).origin
+ } catch {
+ return false
+ }
+}
diff --git a/electron/preload/index.ts b/electron/preload/index.ts
index b648ca13..f24249f7 100644
--- a/electron/preload/index.ts
+++ b/electron/preload/index.ts
@@ -18,10 +18,6 @@ contextBridge.exposeInMainWorld("electronAPI", {
maximize: () => ipcRenderer.send("window-maximize"),
close: () => ipcRenderer.send("window-close"),
- // File operations
- openFile: () => ipcRenderer.invoke("dialog-open-file"),
- saveFile: (data: string) => ipcRenderer.invoke("dialog-save-file", data),
-
// Proxy settings
getProxy: () => ipcRenderer.invoke("get-proxy"),
setProxy: (config: { httpProxy?: string; httpsProxy?: string }) =>
@@ -31,4 +27,18 @@ contextBridge.exposeInMainWorld("electronAPI", {
getUserLocale: () => ipcRenderer.invoke("get-user-locale"),
setUserLocale: (locale: string) =>
ipcRenderer.invoke("set-user-locale", locale),
+
+ // A chat was saved, or the page loaded with this many chats: the next
+ // launch opens the port where the chats are
+ chatSaved: () => ipcRenderer.invoke("chat-saved"),
+ chatsLoaded: (count: number) => ipcRenderer.invoke("chats-loaded", count),
+
+ // The server restarted on the same port (another preset)
+ onServerRestarted: (callback: () => void) => {
+ const listener = () => callback()
+ ipcRenderer.on("server-restarted", listener)
+ return () => {
+ ipcRenderer.removeListener("server-restarted", listener)
+ }
+ },
})
diff --git a/electron/settings/settings.js b/electron/settings/settings.js
index 235b2fe5..b71928de 100644
--- a/electron/settings/settings.js
+++ b/electron/settings/settings.js
@@ -213,6 +213,9 @@ async function savePreset() {
}
})
+ // closeModal() clears editingPresetId, so remember it for the toast
+ const isEdit = Boolean(editingPresetId)
+
try {
saveBtn.disabled = true
saveBtn.innerHTML = ' '
@@ -220,10 +223,7 @@ async function savePreset() {
await window.settingsAPI.savePreset(preset)
await loadPresets()
closeModal()
- showToast(
- editingPresetId ? "Preset updated" : "Preset created",
- "success",
- )
+ showToast(isEdit ? "Preset updated" : "Preset created", "success")
} catch (error) {
console.error("Failed to save preset:", error)
showToast("Failed to save preset", "error")
@@ -265,8 +265,6 @@ async function applyPreset(id) {
const result = await window.settingsAPI.applyPreset(id)
if (result.success) {
- currentPresetId = id
- renderPresets()
showToast("Preset applied, server restarting...", "success")
} else {
showToast(result.error || "Failed to apply preset", "error")
@@ -274,6 +272,9 @@ async function applyPreset(id) {
} catch (error) {
console.error("Failed to apply preset:", error)
showToast("Failed to apply preset", "error")
+ } finally {
+ // Reload to show the active preset and reset the Apply button
+ await loadPresets()
}
}
diff --git a/env.example b/env.example
index df8d9118..bf22cff9 100644
--- a/env.example
+++ b/env.example
@@ -12,7 +12,8 @@ AI_PROVIDER=bedrock
AI_MODEL=global.anthropic.claude-sonnet-4-5-20250929-v1:0
# Output limit, all providers (default: 64000). Shared by reasoning and the diagram XML,
-# so a thinking model can spend it all before the tool call. Users can override it in Settings.
+# so a thinking model can spend it all before the tool call. Users can lower it in Settings,
+# and raise it only when they use their own API key, so this also caps cost on server keys.
# If a model's own ceiling is lower, the request is retried with that ceiling automatically.
# MAX_OUTPUT_TOKENS=64000
@@ -43,7 +44,6 @@ AI_MODEL=global.anthropic.claude-sonnet-4-5-20250929-v1:0
# Google Generative AI Configuration
# GOOGLE_GENERATIVE_AI_API_KEY=...
# GOOGLE_BASE_URL=https://generativelanguage.googleapis.com/v1beta # Optional: Custom endpoint
-# GOOGLE_CANDIDATE_COUNT=1 # Optional: Number of candidates to generate
# GOOGLE_TOP_K=40 # Optional: Top K sampling parameter
# GOOGLE_TOP_P=0.95 # Optional: Nucleus sampling parameter
# Note: Gemini 2.5/3 models automatically enable reasoning display (includeThoughts: true)
@@ -70,7 +70,7 @@ AI_MODEL=global.anthropic.claude-sonnet-4-5-20250929-v1:0
# AZURE_REASONING_SUMMARY=detailed
# Ollama Configuration (Local or Cloud)
-# OLLAMA_BASE_URL=https://ollama.com/api # Optional, defaults to Ollama Cloud
+# OLLAMA_BASE_URL=https://ollama.com/api # Optional: Ollama Cloud; defaults to local Ollama (http://127.0.0.1:11434)
# OLLAMA_API_KEY=your-ollama-cloud-api-key # Optional: For Ollama Cloud or authenticated remote instances
# OLLAMA_ENABLE_THINKING=true # Optional: Enable thinking for models that support it (e.g., qwen3)
@@ -123,9 +123,14 @@ AI_MODEL=global.anthropic.claude-sonnet-4-5-20250929-v1:0
# Temperature (Optional)
# Controls randomness in AI responses. Lower = more deterministic.
-# Leave unset for models that don't support temperature (e.g., GPT-5.1 reasoning models)
+# Leave unset for models that don't support temperature (e.g., GPT-5.1 reasoning models).
+# Claude 4.7 and later reject it; the request is then retried without it.
# TEMPERATURE=0
+# Debug Logging (Optional)
+# Log the structure of the messages each chat request sends to the model
+# DEBUG_LLM_PAYLOAD=true
+
# Access Control (Optional)
# ACCESS_CODE_LIST=your-secret-code,another-code
diff --git a/hooks/use-diagram-tool-handlers.ts b/hooks/use-diagram-tool-handlers.ts
index d1970d85..ad67ec28 100644
--- a/hooks/use-diagram-tool-handlers.ts
+++ b/hooks/use-diagram-tool-handlers.ts
@@ -1,5 +1,4 @@
import type { MutableRefObject } from "react"
-import { useRef } from "react"
import type { DiagramOperation } from "@/components/chat/types"
import type {
ValidationState,
@@ -7,10 +6,15 @@ import type {
} from "@/components/chat/ValidationCard"
import type { ValidationResult } from "@/lib/diagram-validator"
import { formatValidationFeedback } from "@/lib/diagram-validator"
-import { isMxCellXmlComplete, wrapWithMxFile } from "@/lib/utils"
+import { isMxCellXmlComplete } from "@/lib/utils"
+import { editDiagram } from "@/packages/mcp-server/src/edit-diagram.ts"
+import { prepareNewDiagram } from "@/packages/mcp-server/src/new-diagram.ts"
const DEBUG = process.env.NODE_ENV === "development"
+// display_diagram replaces the document with this one page
+const NEW_PAGE = { pageId: "page-1", pageName: "Page-1" }
+
interface ToolCall {
toolCallId: string
toolName: string
@@ -48,6 +52,10 @@ type ValidateDiagramFn = (
interface UseDiagramToolHandlersParams {
partialXmlRef: MutableRefObject
editDiagramOriginalXmlRef: MutableRefObject>
+ // Tool calls the streaming preview must leave alone (shared with it)
+ processedToolCallsRef: MutableRefObject>
+ // Failed VLM validations in the current user turn (reset on each user message)
+ validationRetryCountRef: MutableRefObject
chartXMLRef: MutableRefObject
onDisplayChart: (xml: string, skipValidation?: boolean) => string | null
onFetchChart: (saveToHistory?: boolean) => Promise
@@ -56,6 +64,11 @@ interface UseDiagramToolHandlersParams {
validateDiagram?: ValidateDiagramFn
enableVlmValidation?: boolean
sessionId?: string
+ // Called when a screenshot check begins; the function it returns
+ // tells whether the user pressed Stop in this turn, also after the next
+ // message was sent. A check that has not started then is skipped (one
+ // already running is cancelled by the caller).
+ watchStop?: () => () => boolean
onValidationStateChange?: (
toolCallId: string,
state: ValidationState,
@@ -72,6 +85,8 @@ interface UseDiagramToolHandlersParams {
export function useDiagramToolHandlers({
partialXmlRef,
editDiagramOriginalXmlRef,
+ processedToolCallsRef,
+ validationRetryCountRef,
chartXMLRef,
onDisplayChart,
onFetchChart,
@@ -80,11 +95,9 @@ export function useDiagramToolHandlers({
validateDiagram,
enableVlmValidation = true,
sessionId,
+ watchStop,
onValidationStateChange,
}: UseDiagramToolHandlersParams) {
- // Track validation retry count per tool call
- const validationRetryCountRef = useRef>(new Map())
-
// Helper to update validation state
const updateValidationState = (
toolCallId: string,
@@ -114,18 +127,39 @@ export function useDiagramToolHandlers({
)
}
+ processedToolCallsRef.current.add(toolCall.toolCallId)
+ // Only display_diagram, edit_diagram and a completing append_diagram
+ // put their result on the canvas. Other tools (get_shape_library,
+ // which the server runs, still arrives here) leave the stored
+ // originals for the preview code to undo.
if (toolCall.toolName === "display_diagram") {
- await handleDisplayDiagram(toolCall, addToolOutput)
+ await handleDisplayDiagram(toolCall, addToolOutput, takeOriginals())
} else if (toolCall.toolName === "edit_diagram") {
- await handleEditDiagram(toolCall, addToolOutput)
+ await handleEditDiagram(toolCall, addToolOutput, takeOriginals())
} else if (toolCall.toolName === "append_diagram") {
handleAppendDiagram(toolCall, addToolOutput)
}
}
+ // Stored originals belong to previews not handled yet: this call's, and
+ // those of earlier calls with invalid input, which never get to the
+ // handler. The first is the diagram before all of them. A call that
+ // draws its result replaces those previews, so the preview code must
+ // neither draw them again nor undo them later. Returns that first one.
+ const takeOriginals = (): string | undefined => {
+ const [originalXml] = editDiagramOriginalXmlRef.current.values()
+ for (const id of editDiagramOriginalXmlRef.current.keys()) {
+ processedToolCallsRef.current.add(id)
+ }
+ editDiagramOriginalXmlRef.current.clear()
+ return originalXml
+ }
+
+ // originalXml: the diagram before the streamed previews, if any were drawn
const handleDisplayDiagram = async (
toolCall: ToolCall,
addToolOutput: AddToolOutputFn,
+ originalXml: string | undefined,
) => {
const { xml } = toolCall.input as { xml: string }
@@ -174,14 +208,18 @@ NEXT STEP: Call append_diagram with the continuation XML.
const finalXml = xml
partialXmlRef.current = "" // Reset any partial from previous truncation
- // Wrap raw XML with full mxfile structure for draw.io
- const fullXml = wrapWithMxFile(finalXml)
-
- // loadDiagram validates and returns error if invalid
- const validationError = onDisplayChart(fullXml)
+ // Wrap, validate and auto-fix the model's XML like the MCP server's
+ // create_new_diagram, then load it
+ const prepared = prepareNewDiagram(finalXml, NEW_PAGE)
+ const validationError = prepared.ok
+ ? onDisplayChart(prepared.xml, true)
+ : prepared.error
if (validationError) {
console.warn("[display_diagram] Validation error:", validationError)
+ // Undo the streamed preview, as a failed edit does: the canvas
+ // keeps the diagram from before this failed call
+ if (originalXml) onDisplayChart(originalXml, true)
// Return error to model - sendAutomaticallyWhen will trigger retry
if (DEBUG) {
console.log(
@@ -213,9 +251,12 @@ ${finalXml}
if (
enableVlmValidation &&
captureValidationPng &&
- validateDiagram
+ validateDiagram &&
+ // At most this many checks per user turn, passed or not
+ validationRetryCountRef.current < MAX_VALIDATION_RETRIES
) {
let capturedPngData: string | null = null
+ const stopped = watchStop?.()
try {
// Notify UI that we're starting capture
updateValidationState(toolCall.toolCallId, "capturing")
@@ -225,24 +266,28 @@ ${finalXml}
await new Promise((resolve) => setTimeout(resolve, 100))
capturedPngData = await captureValidationPng()
- if (capturedPngData) {
+ // Stopped while the screenshot was taken: no check. The
+ // chat waits for this handler, so it must end now.
+ if (stopped?.()) {
+ updateValidationState(toolCall.toolCallId, "skipped")
+ } else if (capturedPngData) {
if (DEBUG) {
console.log(
"[display_diagram] Captured PNG for validation",
)
}
- const retryCount =
- validationRetryCountRef.current.get(
- toolCall.toolCallId,
- ) || 0
+ // Each retry is a new tool call, so count attempts
+ // per user turn (the chat resets it when the user sends)
+ const attempt = validationRetryCountRef.current + 1
+ validationRetryCountRef.current = attempt
// Notify UI that we're validating (include the image)
updateValidationState(
toolCall.toolCallId,
"validating",
{
- attempt: retryCount + 1,
+ attempt,
maxAttempts: MAX_VALIDATION_RETRIES,
imageData: capturedPngData,
},
@@ -254,17 +299,12 @@ ${finalXml}
)
if (!result.valid) {
- if (retryCount < MAX_VALIDATION_RETRIES) {
- validationRetryCountRef.current.set(
- toolCall.toolCallId,
- retryCount + 1,
- )
-
+ if (attempt < MAX_VALIDATION_RETRIES) {
const feedback =
formatValidationFeedback(result)
if (DEBUG) {
console.log(
- `[display_diagram] Validation failed (attempt ${retryCount + 1}/${MAX_VALIDATION_RETRIES}):`,
+ `[display_diagram] Validation failed (attempt ${attempt}/${MAX_VALIDATION_RETRIES}):`,
result.issues,
)
}
@@ -274,7 +314,7 @@ ${finalXml}
toolCall.toolCallId,
"failed",
{
- attempt: retryCount + 1,
+ attempt,
maxAttempts: MAX_VALIDATION_RETRIES,
result,
imageData: capturedPngData,
@@ -285,20 +325,16 @@ ${finalXml}
tool: "display_diagram",
toolCallId: toolCall.toolCallId,
state: "output-error",
- errorText: `[Validation attempt ${retryCount + 1}/${MAX_VALIDATION_RETRIES}]\n${feedback}`,
+ errorText: `[Validation attempt ${attempt}/${MAX_VALIDATION_RETRIES}]\n${feedback}`,
})
return
} else {
- // Max retries reached - accept the diagram with warning
+ // Last attempt - accept the diagram with warning
if (DEBUG) {
console.log(
"[display_diagram] Max validation retries reached, accepting diagram",
)
}
- validationRetryCountRef.current.delete(
- toolCall.toolCallId,
- )
-
// Notify UI that we're accepting with issues (include the image)
updateValidationState(
toolCall.toolCallId,
@@ -314,10 +350,6 @@ ${finalXml}
return
}
} else {
- // Validation passed - clean up retry count
- validationRetryCountRef.current.delete(
- toolCall.toolCallId,
- )
if (DEBUG) {
console.log(
"[display_diagram] Validation passed!",
@@ -340,6 +372,16 @@ ${finalXml}
updateValidationState(toolCall.toolCallId, "skipped")
}
} catch (error) {
+ // Cancelled by Stop: the diagram stays, unchecked
+ if ((error as Error)?.name === "AbortError") {
+ updateValidationState(toolCall.toolCallId, "skipped")
+ addToolOutput({
+ tool: "display_diagram",
+ toolCallId: toolCall.toolCallId,
+ output: "Successfully displayed the diagram.",
+ })
+ return
+ }
// VLM validation error - log but don't block the user
console.warn(
"[display_diagram] VLM validation error:",
@@ -373,21 +415,24 @@ ${finalXml}
}
}
+ // originalXml: the diagram before the streamed previews, if any were drawn.
+ // Operations apply to it, the same base XML that streaming used.
const handleEditDiagram = async (
toolCall: ToolCall,
addToolOutput: AddToolOutputFn,
+ originalXml: string | undefined,
) => {
const { operations } = toolCall.input as {
operations: DiagramOperation[]
}
let currentXml = ""
+ // On failure, undo the streaming preview so the canvas matches the XML
+ // reported back to the model
+ const restoreOriginal = () => {
+ if (originalXml) onDisplayChart(originalXml, true)
+ }
try {
- // Use the original XML captured during streaming (shared with chat-message-display)
- // This ensures we apply operations to the same base XML that streaming used
- const originalXml = editDiagramOriginalXmlRef.current.get(
- toolCall.toolCallId,
- )
if (originalXml) {
currentXml = originalXml
} else {
@@ -401,26 +446,19 @@ ${finalXml}
}
}
- const { applyDiagramOperations } = await import("@/lib/utils")
- const { result: editedXml, errors } = applyDiagramOperations(
- currentXml,
- operations,
- )
-
- // Check for operation errors
- if (errors.length > 0) {
- const errorMessages = errors
- .map(
- (e) =>
- `- ${e.type} on cell_id="${e.cellId}": ${e.message}`,
- )
- .join("\n")
-
+ // All or nothing, checked like the MCP server's edit_diagram.
+ // The model sees the first page, so edits target it.
+ const outcome = editDiagram(currentXml, operations, {})
+ if (!outcome.ok) {
+ const reason = outcome.pageError
+ ? outcome.errors[0]
+ : `No changes were made because ${outcome.errors.length} operation(s) failed:\n${outcome.errors.map((e) => `- ${e}`).join("\n")}`
+ restoreOriginal()
addToolOutput({
tool: "edit_diagram",
toolCallId: toolCall.toolCallId,
state: "output-error",
- errorText: `Some operations failed:\n${errorMessages}
+ errorText: `${reason}
Current diagram XML:
\`\`\`xml
@@ -429,49 +467,23 @@ ${currentXml}
Please check the cell IDs and retry.`,
})
- // Clean up the shared original XML ref
- editDiagramOriginalXmlRef.current.delete(toolCall.toolCallId)
return
}
- // loadDiagram validates and returns error if invalid
- const validationError = onDisplayChart(editedXml)
- if (validationError) {
- console.warn(
- "[edit_diagram] Validation error:",
- validationError,
- )
- addToolOutput({
- tool: "edit_diagram",
- toolCallId: toolCall.toolCallId,
- state: "output-error",
- errorText: `Edit produced invalid XML: ${validationError}
-
-Current diagram XML:
-\`\`\`xml
-${currentXml}
-\`\`\`
-
-Please fix the operations to avoid structural issues.`,
- })
- // Clean up the shared original XML ref
- editDiagramOriginalXmlRef.current.delete(toolCall.toolCallId)
- return
- }
+ onDisplayChart(outcome.xml, true)
onExport()
addToolOutput({
tool: "edit_diagram",
toolCallId: toolCall.toolCallId,
- output: `Successfully applied ${operations.length} operation(s) to the diagram.`,
+ output: `Successfully applied ${outcome.applied} operation(s) to the diagram.`,
})
- // Clean up the shared original XML ref
- editDiagramOriginalXmlRef.current.delete(toolCall.toolCallId)
} catch (error) {
console.error("[edit_diagram] Failed:", error)
const errorMessage =
error instanceof Error ? error.message : String(error)
+ restoreOriginal()
addToolOutput({
tool: "edit_diagram",
toolCallId: toolCall.toolCallId,
@@ -485,8 +497,6 @@ ${currentXml || "No XML available"}
Please check cell IDs and retry, or use display_diagram to regenerate.`,
})
- // Clean up the shared original XML ref even on error
- editDiagramOriginalXmlRef.current.delete(toolCall.toolCallId)
}
}
@@ -496,6 +506,19 @@ Please check cell IDs and retry, or use display_diagram to regenerate.`,
) => {
const { xml } = toolCall.input as { xml: string }
+ // Nothing to continue: loading the fragment alone would replace the whole diagram
+ if (!partialXmlRef.current) {
+ addToolOutput({
+ tool: "append_diagram",
+ toolCallId: toolCall.toolCallId,
+ state: "output-error",
+ errorText: `ERROR: There is no truncated diagram to continue, so append_diagram cannot be used now.
+
+Use display_diagram to create the complete diagram, or edit_diagram to change the current one.`,
+ })
+ return
+ }
+
// Detect if LLM incorrectly started fresh instead of continuing
// LLM should only output bare mxCells now, so wrapper tags indicate error
const trimmed = xml.trim()
@@ -534,10 +557,18 @@ Start your continuation with the NEXT character after where it stopped.`,
const finalXml = partialXmlRef.current
partialXmlRef.current = "" // Reset
- const fullXml = wrapWithMxFile(finalXml)
- const validationError = onDisplayChart(fullXml)
+ const prepared = prepareNewDiagram(finalXml, NEW_PAGE)
+ // It draws now: it takes the stored originals, as display_diagram
+ const originalXml = prepared.ok ? takeOriginals() : undefined
+ const validationError = prepared.ok
+ ? onDisplayChart(prepared.xml, true)
+ : prepared.error
if (validationError) {
+ // Loading failed: back to the diagram before the previews
+ if (prepared.ok && originalXml) {
+ onDisplayChart(originalXml, true)
+ }
addToolOutput({
tool: "append_diagram",
toolCallId: toolCall.toolCallId,
diff --git a/hooks/use-model-config.ts b/hooks/use-model-config.ts
index 7c918f05..e05f680f 100644
--- a/hooks/use-model-config.ts
+++ b/hooks/use-model-config.ts
@@ -13,6 +13,7 @@ import {
flattenModels,
type ModelConfig,
type MultiModelConfig,
+ PROVIDER_INFO,
type ProviderConfig,
type ProviderName,
} from "@/lib/types/model-config"
@@ -63,6 +64,28 @@ function migrateOldConfig(): MultiModelConfig | null {
return config
}
+const isKnownProvider = (p: { provider: string }) =>
+ Object.hasOwn(PROVIDER_INFO, p.provider)
+
+/**
+ * The stored config without providers this version does not know (saved
+ * by another version, or edited by hand): they would break every list of
+ * models. They stay in storage (saveConfig keeps them). Throws on bad JSON.
+ */
+function parseStoredConfig(stored: string): MultiModelConfig {
+ const config = JSON.parse(stored) as MultiModelConfig
+ const known = config.providers.filter(isKnownProvider)
+ if (known.length < config.providers.length) {
+ console.warn(
+ "Skipped saved providers this version does not know:",
+ config.providers
+ .filter((p) => !isKnownProvider(p))
+ .map((p) => p.provider),
+ )
+ }
+ return { ...config, providers: known }
+}
+
/**
* Load config from localStorage
*/
@@ -73,7 +96,7 @@ function loadConfig(): MultiModelConfig {
const stored = localStorage.getItem(STORAGE_KEYS.modelConfigs)
if (stored) {
try {
- return JSON.parse(stored) as MultiModelConfig
+ return parseStoredConfig(stored)
} catch {
console.error("Failed to parse model config")
}
@@ -98,7 +121,35 @@ function loadConfig(): MultiModelConfig {
*/
function saveConfig(config: MultiModelConfig): void {
if (typeof window === "undefined") return
- localStorage.setItem(STORAGE_KEYS.modelConfigs, JSON.stringify(config))
+ // Providers this version does not know are not in config: keep them,
+ // with their keys, for the version that saved them
+ let unknown: MultiModelConfig["providers"] = []
+ try {
+ const stored = localStorage.getItem(STORAGE_KEYS.modelConfigs)
+ if (stored) {
+ unknown = (JSON.parse(stored) as MultiModelConfig).providers.filter(
+ (p) => !isKnownProvider(p),
+ )
+ }
+ } catch {
+ // Unreadable: nothing to keep
+ }
+ localStorage.setItem(
+ STORAGE_KEYS.modelConfigs,
+ JSON.stringify({
+ ...config,
+ providers: [...config.providers, ...unknown],
+ }),
+ )
+}
+
+/**
+ * Server model to fall back to: the one marked default, else the first one
+ */
+function defaultServerModelId(
+ serverModels: FlattenedServerModel[],
+): string | undefined {
+ return (serverModels.find((m) => m.isDefault) ?? serverModels[0])?.id
}
export interface UseModelConfigReturn {
@@ -144,10 +195,25 @@ export function useModelConfig(): UseModelConfigReturn {
setIsLoaded(true)
}, [])
- // Load server models on mount (if any)
+ // Pick up config changes saved by other tabs, so this tab neither shows a
+ // stale model nor overwrites their changes on its next save
+ useEffect(() => {
+ const handleStorage = (e: StorageEvent) => {
+ if (e.key === STORAGE_KEYS.modelConfigs) setConfig(loadConfig())
+ }
+ window.addEventListener("storage", handleStorage)
+ return () => window.removeEventListener("storage", handleStorage)
+ }, [])
+
+ // Load server models on mount (if any), and again when the desktop app
+ // restarted its server for another preset
useEffect(() => {
if (typeof window === "undefined") return
+ loadServerModels()
+ return window.electronAPI?.onServerRestarted?.(loadServerModels)
+ }, [])
+ function loadServerModels() {
fetch(getApiEndpoint("/api/server-models"))
.then((res) => {
if (!res.ok) {
@@ -165,24 +231,37 @@ export function useModelConfig(): UseModelConfigReturn {
setServerModels(raw)
setServerLoaded(true)
- // Auto-select default server model if no model is currently selected
+ // Auto-select the default server model if no model is selected,
+ // or if the saved server model is gone (renamed or removed)
setConfig((prev) => {
- if (!prev.selectedModelId && raw.length > 0) {
- const defaultModel = raw.find((m) => m.isDefault)
- if (defaultModel) {
- return { ...prev, selectedModelId: defaultModel.id }
- }
- // If no default marked, use first server model
- return { ...prev, selectedModelId: raw[0].id }
- }
- return prev
+ const id = prev.selectedModelId
+ const isStale =
+ id?.startsWith("server:") &&
+ !raw.some((m) => m.id === id)
+ if (id && !isStale) return prev
+ // Saved before non-ASCII characters in provider names
+ // got into the id: they were dropped from it
+ const renamed = raw.filter(
+ (m) =>
+ `server:${m.providerLabel
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-|-$/g, "")}:${m.modelId}` === id,
+ )
+ const fallback =
+ renamed.length === 1
+ ? renamed[0].id
+ : defaultServerModelId(raw)
+ return fallback === id
+ ? prev
+ : { ...prev, selectedModelId: fallback }
})
})
.catch((error) => {
console.error("Error while loading server models:", error)
setServerLoaded(true)
})
- }, [])
+ }
// Save config whenever it changes (after initial load)
useEffect(() => {
@@ -260,24 +339,31 @@ export function useModelConfig(): UseModelConfigReturn {
[],
)
- const deleteProvider = useCallback((providerId: string) => {
- setConfig((prev) => {
- const provider = prev.providers.find((p) => p.id === providerId)
- const modelIds = provider?.models.map((m) => m.id) || []
+ const deleteProvider = useCallback(
+ (providerId: string) => {
+ setConfig((prev) => {
+ const provider = prev.providers.find((p) => p.id === providerId)
+ const modelIds = provider?.models.map((m) => m.id) || []
- // Clear selected model if it belongs to deleted provider
- const newSelectedId =
- prev.selectedModelId && modelIds.includes(prev.selectedModelId)
- ? undefined
- : prev.selectedModelId
+ // Fall back to the default server model if the selected model
+ // belongs to the deleted provider
+ const newSelectedId =
+ prev.selectedModelId &&
+ modelIds.includes(prev.selectedModelId)
+ ? defaultServerModelId(serverModels)
+ : prev.selectedModelId
- return {
- ...prev,
- providers: prev.providers.filter((p) => p.id !== providerId),
- selectedModelId: newSelectedId,
- }
- })
- }, [])
+ return {
+ ...prev,
+ providers: prev.providers.filter(
+ (p) => p.id !== providerId,
+ ),
+ selectedModelId: newSelectedId,
+ }
+ })
+ },
+ [serverModels],
+ )
const addModel = useCallback(
(providerId: string, modelId: string): ModelConfig => {
@@ -334,14 +420,15 @@ export function useModelConfig(): UseModelConfigReturn {
}
: p,
),
- // Clear selected model if it was deleted
+ // Fall back to the default server model if the selected model
+ // was deleted
selectedModelId:
prev.selectedModelId === modelConfigId
- ? undefined
+ ? defaultServerModelId(serverModels)
: prev.selectedModelId,
}))
},
- [],
+ [serverModels],
)
const resetConfig = useCallback(() => {
@@ -428,7 +515,8 @@ export function getSelectedAIConfig(): {
let config: MultiModelConfig
try {
- config = JSON.parse(stored)
+ // Unknown providers would break the model lookup below
+ config = parseStoredConfig(stored)
} catch {
return { ...empty, accessCode }
}
diff --git a/hooks/use-session-manager.ts b/hooks/use-session-manager.ts
index 9243e2c3..e950644a 100644
--- a/hooks/use-session-manager.ts
+++ b/hooks/use-session-manager.ts
@@ -1,6 +1,8 @@
"use client"
import { useCallback, useEffect, useRef, useState } from "react"
+import { toast } from "sonner"
+import { useDictionary } from "@/hooks/use-dictionary"
import {
type ChatSession,
createEmptySession,
@@ -11,10 +13,12 @@ import {
getSession,
isIndexedDBAvailable,
migrateFromLocalStorage,
+ readSessionCount,
type SessionMetadata,
type StoredMessage,
saveSession,
} from "@/lib/session-storage"
+import { STORAGE_KEYS } from "@/lib/storage"
export interface SessionData {
messages: StoredMessage[]
@@ -24,6 +28,13 @@ export interface SessionData {
diagramHistory?: { svg: string; xml: string }[]
}
+// Taken right before a save's data is read: the chat on screen then, and
+// the order of the reads
+export interface SaveTicket {
+ generation: number
+ seq: number
+}
+
export interface UseSessionManagerReturn {
// State
sessions: SessionMetadata[]
@@ -35,13 +46,27 @@ export interface UseSessionManagerReturn {
// Actions
switchSession: (id: string) => Promise
deleteSession: (id: string) => Promise<{ wasCurrentSession: boolean }>
- // forSessionId: optional session ID to verify save targets correct session (prevents stale debounce writes)
+ // ticket: getSaveTicket() before the data was read (by default, now).
+ // The save is dropped if another chat is on screen when its turn comes,
+ // or if a copy of this chat read later was saved already.
+ // Resolves to false when the save failed (the user was told)
saveCurrentSession: (
data: SessionData,
- forSessionId?: string | null,
- ) => Promise
+ ticket?: SaveTicket,
+ ) => Promise
refreshSessions: () => Promise
clearCurrentSession: () => void
+ getChatGeneration: () => number
+ getSaveTicket: () => SaveTicket
+}
+
+// Reading the session list loads every stored session in full, and window
+// focus also fires each time the user clicks back from the draw.io iframe
+const FOCUS_REFRESH_INTERVAL_MS = 30_000
+
+function notifySaveFailed(message: string) {
+ // Same id, so repeated failures update one toast instead of stacking
+ toast.error(message, { id: "session-save-failed", duration: 8000 })
}
interface UseSessionManagerOptions {
@@ -53,6 +78,7 @@ export function useSessionManager(
options: UseSessionManagerOptions = {},
): UseSessionManagerReturn {
const { initialSessionId } = options
+ const dict = useDictionary()
const [sessions, setSessions] = useState([])
const [currentSessionId, setCurrentSessionId] = useState(
null,
@@ -66,6 +92,23 @@ export function useSessionManager(
const isInitializedRef = useRef(false)
// Sequence guard for URL changes - prevents out-of-order async resolution
const urlChangeSequenceRef = useRef(0)
+ // The chat on screen, read by saves that run after a render or a wait
+ const currentSessionRef = useRef(null)
+ // Goes up each time another chat is put on screen (creating the
+ // session of the chat on screen does not count)
+ const chatGenerationRef = useRef(0)
+ // Saves run one at a time, so two saves of a new chat create it once
+ const saveQueueRef = useRef>(Promise.resolve())
+ // The last ticket number, and that of the newest data saved
+ const saveSeqRef = useRef(0)
+ const savedSeqRef = useRef(0)
+
+ const changeChat = useCallback((session: ChatSession | null) => {
+ chatGenerationRef.current++
+ currentSessionRef.current = session
+ setCurrentSession(session)
+ setCurrentSessionId(session?.id ?? null)
+ }, [])
// Load sessions list
const refreshSessions = useCallback(async () => {
@@ -101,14 +144,36 @@ export function useSessionManager(
// Load sessions list
const metadata = await getAllSessionMetadata()
setSessions(metadata)
+ // The desktop app may try its other port next launch, where
+ // an older version may have saved the chats: only when this
+ // origin surely has none (a failed read is not "none") and
+ // keeps no model settings or keys either
+ if (window.electronAPI?.chatsLoaded) {
+ const count = await readSessionCount()
+ // The app saves an empty config on its first load; the
+ // providers are what holds the keys, besides an access
+ // code
+ let hasSettings = true
+ try {
+ const config = JSON.parse(
+ localStorage.getItem(STORAGE_KEYS.modelConfigs) ??
+ "{}",
+ )
+ hasSettings =
+ (config.providers?.length ?? 0) > 0 ||
+ !!localStorage.getItem(STORAGE_KEYS.accessCode)
+ } catch {
+ // Unreadable: treat as settings, and stay
+ }
+ if (count !== null && !hasSettings) {
+ window.electronAPI.chatsLoaded(count).catch(() => {})
+ }
+ }
// Only load a session if initialSessionId is provided (from URL param)
if (initialSessionId) {
const session = await getSession(initialSessionId)
- if (session) {
- setCurrentSession(session)
- setCurrentSessionId(session.id)
- }
+ if (session) changeChat(session)
// If session not found, stay in blank state (URL has invalid session ID)
}
// If no initialSessionId, start with blank state (no auto-restore)
@@ -120,7 +185,7 @@ export function useSessionManager(
}
init()
- }, [initialSessionId])
+ }, [initialSessionId, changeChat])
// Handle URL session ID changes after initialization
// Note: intentionally NOT including currentSessionId in deps to avoid race conditions
@@ -135,6 +200,7 @@ export function useSessionManager(
async function handleSessionIdChange() {
if (initialSessionId) {
+ const generation = chatGenerationRef.current
// URL has session ID - load it
const session = await getSession(initialSessionId)
@@ -143,16 +209,13 @@ export function useSessionManager(
if (currentSequence !== urlChangeSequenceRef.current) {
return
}
+ // Another chat was put on screen meanwhile (New Chat right
+ // after this one got its session id in the URL): keep it
+ if (generation !== chatGenerationRef.current) return
- if (session) {
- // Only update if the session is different from current
- setCurrentSessionId((current) => {
- if (current !== session.id) {
- setCurrentSession(session)
- return session.id
- }
- return current
- })
+ // Only update if the session is different from current
+ if (session && currentSessionRef.current?.id !== session.id) {
+ changeChat(session)
}
}
// Removed: else clause that clears session
@@ -161,11 +224,17 @@ export function useSessionManager(
}
handleSessionIdChange()
- }, [initialSessionId, isAvailable])
+ }, [initialSessionId, isAvailable, changeChat])
- // Refresh sessions on window focus (multi-tab sync)
+ // Refresh sessions on window focus (multi-tab sync), at most once per interval
+ const lastFocusRefreshRef = useRef(0)
useEffect(() => {
const handleFocus = () => {
+ const now = Date.now()
+ if (now - lastFocusRefreshRef.current < FOCUS_REFRESH_INTERVAL_MS) {
+ return
+ }
+ lastFocusRefreshRef.current = now
refreshSessions()
}
window.addEventListener("focus", handleFocus)
@@ -177,9 +246,11 @@ export function useSessionManager(
async (id: string): Promise => {
if (id === currentSessionId) return null
- // Save current session first if it has messages
- if (currentSession && currentSession.messages.length > 0) {
- await saveSession(currentSession)
+ // Save current session first if it has messages (as saved
+ // last: the caller may have just saved it)
+ const current = currentSessionRef.current
+ if (current && current.messages.length > 0) {
+ await saveSession(current)
}
// Load the target session
@@ -189,9 +260,7 @@ export function useSessionManager(
return null
}
- // Update state
- setCurrentSession(session)
- setCurrentSessionId(session.id)
+ changeChat(session)
return {
messages: session.messages,
@@ -201,7 +270,7 @@ export function useSessionManager(
diagramHistory: session.diagramHistory,
}
},
- [currentSessionId, currentSession],
+ [currentSessionId, changeChat],
)
// Delete a session
@@ -211,101 +280,137 @@ export function useSessionManager(
await deleteSessionFromDB(id)
// If deleting current session, clear state (caller will show new empty session)
- if (wasCurrentSession) {
- setCurrentSession(null)
- setCurrentSessionId(null)
- }
+ if (wasCurrentSession) changeChat(null)
await refreshSessions()
return { wasCurrentSession }
},
- [currentSessionId, refreshSessions],
+ [currentSessionId, refreshSessions, changeChat],
)
// Save current session data (debounced externally by caller)
- // forSessionId: if provided, verify save targets correct session (prevents stale debounce writes)
const saveCurrentSession = useCallback(
- async (
- data: SessionData,
- forSessionId?: string | null,
- ): Promise => {
- // If forSessionId is provided, verify it matches current session
- // This prevents stale debounced saves from overwriting a newly switched session
- if (
- forSessionId !== undefined &&
- forSessionId !== currentSessionId
- ) {
- return
+ (data: SessionData, ticket?: SaveTicket): Promise => {
+ // The data is of the chat on screen when it was read
+ const { generation, seq } = ticket ?? {
+ generation: chatGenerationRef.current,
+ seq: ++saveSeqRef.current,
}
+ const run = async (): Promise => {
+ // That chat is no longer on screen (leaving it saved it)
+ if (generation !== chatGenerationRef.current) return true
+ // A copy read later was saved already (one that waited for
+ // its thumbnail must not undo it)
+ if (seq < savedSeqRef.current) return true
+ // Nothing can be stored without IndexedDB
+ if (!isIndexedDBAvailable()) return true
+ // The user may put another chat on screen while this one is
+ // written; the stored copy is still right, the state is not
+ const stillOnScreen = () =>
+ chatGenerationRef.current === generation
+ const currentSession = currentSessionRef.current
- if (!currentSession) {
- // Create a new session if none exists
- const newSession: ChatSession = {
- ...createEmptySession(),
+ if (!currentSession) {
+ // Create a new session if none exists
+ const newSession: ChatSession = {
+ ...createEmptySession(),
+ messages: data.messages,
+ xmlSnapshots: data.xmlSnapshots,
+ diagramXml: data.diagramXml,
+ thumbnailDataUrl: data.thumbnailDataUrl,
+ diagramHistory: data.diagramHistory,
+ title: extractTitle(data.messages),
+ }
+ // Without a stored session, keep no session id (it would end
+ // up in the URL and point to nothing after a reload)
+ if (!(await saveSession(newSession))) {
+ notifySaveFailed(dict.errors.sessionSaveFailed)
+ return false
+ }
+ savedSeqRef.current = seq
+ await enforceSessionLimit()
+ if (stillOnScreen()) {
+ currentSessionRef.current = newSession
+ setCurrentSession(newSession)
+ setCurrentSessionId(newSession.id)
+ }
+ await refreshSessions()
+ return true
+ }
+
+ // Update existing session
+ const updatedSession: ChatSession = {
+ ...currentSession,
messages: data.messages,
xmlSnapshots: data.xmlSnapshots,
diagramXml: data.diagramXml,
- thumbnailDataUrl: data.thumbnailDataUrl,
- diagramHistory: data.diagramHistory,
- title: extractTitle(data.messages),
+ thumbnailDataUrl:
+ data.thumbnailDataUrl ??
+ currentSession.thumbnailDataUrl,
+ diagramHistory:
+ data.diagramHistory ?? currentSession.diagramHistory,
+ updatedAt: Date.now(),
+ // Update title if it's still default and we have messages
+ title:
+ currentSession.title === "New Chat" &&
+ data.messages.length > 0
+ ? extractTitle(data.messages)
+ : currentSession.title,
}
- await saveSession(newSession)
- await enforceSessionLimit()
- setCurrentSession(newSession)
- setCurrentSessionId(newSession.id)
- await refreshSessions()
- return
+
+ if (!(await saveSession(updatedSession))) {
+ notifySaveFailed(dict.errors.sessionSaveFailed)
+ return false
+ }
+ savedSeqRef.current = seq
+ if (stillOnScreen()) {
+ currentSessionRef.current = updatedSession
+ setCurrentSession(updatedSession)
+ }
+
+ // Update sessions list metadata
+ setSessions((prev) =>
+ prev.map((s) =>
+ s.id === updatedSession.id
+ ? {
+ ...s,
+ title: updatedSession.title,
+ updatedAt: updatedSession.updatedAt,
+ messageCount: updatedSession.messages.length,
+ hasDiagram:
+ !!updatedSession.diagramXml &&
+ updatedSession.diagramXml.trim().length >
+ 0,
+ thumbnailDataUrl:
+ updatedSession.thumbnailDataUrl,
+ }
+ : s,
+ ),
+ )
+ return true
}
-
- // Update existing session
- const updatedSession: ChatSession = {
- ...currentSession,
- messages: data.messages,
- xmlSnapshots: data.xmlSnapshots,
- diagramXml: data.diagramXml,
- thumbnailDataUrl:
- data.thumbnailDataUrl ?? currentSession.thumbnailDataUrl,
- diagramHistory:
- data.diagramHistory ?? currentSession.diagramHistory,
- updatedAt: Date.now(),
- // Update title if it's still default and we have messages
- title:
- currentSession.title === "New Chat" &&
- data.messages.length > 0
- ? extractTitle(data.messages)
- : currentSession.title,
- }
-
- await saveSession(updatedSession)
- setCurrentSession(updatedSession)
-
- // Update sessions list metadata
- setSessions((prev) =>
- prev.map((s) =>
- s.id === updatedSession.id
- ? {
- ...s,
- title: updatedSession.title,
- updatedAt: updatedSession.updatedAt,
- messageCount: updatedSession.messages.length,
- hasDiagram:
- !!updatedSession.diagramXml &&
- updatedSession.diagramXml.trim().length > 0,
- thumbnailDataUrl: updatedSession.thumbnailDataUrl,
- }
- : s,
- ),
- )
+ const result = saveQueueRef.current.then(run)
+ saveQueueRef.current = result.catch(() => {})
+ return result
},
- [currentSession, currentSessionId, refreshSessions],
+ [refreshSessions, dict],
)
// Clear current session state (for starting fresh without loading another session)
const clearCurrentSession = useCallback(() => {
- setCurrentSession(null)
- setCurrentSessionId(null)
- }, [])
+ changeChat(null)
+ }, [changeChat])
+
+ const getChatGeneration = useCallback(() => chatGenerationRef.current, [])
+
+ const getSaveTicket = useCallback(
+ (): SaveTicket => ({
+ generation: chatGenerationRef.current,
+ seq: ++saveSeqRef.current,
+ }),
+ [],
+ )
return {
sessions,
@@ -318,5 +423,7 @@ export function useSessionManager(
saveCurrentSession,
refreshSessions,
clearCurrentSession,
+ getChatGeneration,
+ getSaveTicket,
}
}
diff --git a/hooks/use-validate-diagram.ts b/hooks/use-validate-diagram.ts
index b9cea01b..d3402400 100644
--- a/hooks/use-validate-diagram.ts
+++ b/hooks/use-validate-diagram.ts
@@ -6,6 +6,7 @@
import { experimental_useObject as useObject } from "@ai-sdk/react"
import { useCallback, useRef } from "react"
+import { getSelectedAIConfig } from "@/hooks/use-model-config"
import { getApiEndpoint } from "@/lib/base-path"
import {
type ValidationResult,
@@ -39,6 +40,8 @@ export function useValidateDiagram(options: UseValidateDiagramOptions = {}) {
const { object, submit, isLoading, error, stop } = useObject({
api: getApiEndpoint("/api/validate-diagram"),
schema: ValidationResultSchema,
+ // Resolved per request so a changed access code is picked up
+ headers: () => ({ "x-access-code": getSelectedAIConfig().accessCode }),
onFinish: ({
object,
error: finishError,
@@ -100,9 +103,22 @@ export function useValidateDiagram(options: UseValidateDiagramOptions = {}) {
[submit],
)
+ /**
+ * End a running check (the user pressed Stop): its promise rejects with
+ * an AbortError, so the tool handler can finish at once.
+ */
+ const cancel = useCallback(() => {
+ const pending = pendingValidationRef.current
+ if (!pending) return
+ pendingValidationRef.current = null
+ stop()
+ pending.reject(new DOMException("Validation cancelled", "AbortError"))
+ }, [stop])
+
/**
* Validate with fallback - returns default valid result on error.
* Use this to avoid blocking the user on validation failures.
+ * A cancelled check is passed on as its AbortError.
*/
const validateWithFallback = useCallback(
async (
@@ -112,6 +128,7 @@ export function useValidateDiagram(options: UseValidateDiagramOptions = {}) {
try {
return await validate(imageData, sessionId)
} catch (error) {
+ if ((error as Error)?.name === "AbortError") throw error
console.warn(
"[useValidateDiagram] Validation failed, using fallback:",
error,
@@ -127,6 +144,7 @@ export function useValidateDiagram(options: UseValidateDiagramOptions = {}) {
validate,
validateWithFallback,
stop,
+ cancel,
// State
isValidating: isLoading,
diff --git a/lib/access-code.ts b/lib/access-code.ts
new file mode 100644
index 00000000..ec6cb52c
--- /dev/null
+++ b/lib/access-code.ts
@@ -0,0 +1,50 @@
+/**
+ * Refuse a POST that a page on another website could have sent. A browser
+ * sends a cross-site POST without asking first (CORS preflight) only with a
+ * text or form body, so the routes take JSON only. In the desktop app also
+ * refuse a foreign Host: a site that points its own domain name at
+ * 127.0.0.1 (DNS rebinding) is same-origin with the local server, but its
+ * requests carry that domain. A request the server builds itself has no
+ * Host. Returns the response to send, or null when the request may go on.
+ */
+export function rejectCrossSite(req: Request): Response | null {
+ const contentType = req.headers.get("content-type") ?? ""
+ if (!/^\s*application\/json\b/i.test(contentType)) {
+ return Response.json(
+ { error: "Content-Type must be application/json" },
+ { status: 415 },
+ )
+ }
+ const host = req.headers.get("host")
+ if (
+ process.env.NEXT_AI_DRAWIO_DESKTOP === "1" &&
+ host &&
+ !/^(127\.0\.0\.1|localhost)(:\d+)?$/i.test(host)
+ ) {
+ return Response.json({ error: "Forbidden" }, { status: 403 })
+ }
+ return null
+}
+
+/**
+ * Check the x-access-code header against ACCESS_CODE_LIST.
+ * Returns a 401 response to send back when the check fails, or null when the
+ * request may continue (including when no access codes are configured).
+ */
+export function checkAccessCode(req: Request): Response | null {
+ const accessCodes =
+ process.env.ACCESS_CODE_LIST?.split(",")
+ .map((code) => code.trim())
+ .filter(Boolean) || []
+ if (accessCodes.length === 0) return null
+
+ const accessCodeHeader = req.headers.get("x-access-code")
+ if (accessCodeHeader && accessCodes.includes(accessCodeHeader)) return null
+
+ return Response.json(
+ {
+ error: "Invalid or missing access code. Please configure it in Settings.",
+ },
+ { status: 401 },
+ )
+}
diff --git a/lib/admin/providers.ts b/lib/admin/providers.ts
index 67d50344..39a48cae 100644
--- a/lib/admin/providers.ts
+++ b/lib/admin/providers.ts
@@ -2,6 +2,7 @@ import { z } from "zod"
import {
ProviderNameSchema,
type ServerModelsConfig,
+ slugify,
} from "@/lib/server-model-config"
import {
FIXED_CRED_PROVIDERS,
@@ -182,12 +183,15 @@ export function validateAdminProviders(
return `${PROVIDER_INFO[single].label} is already configured in AI_MODELS_CONFIG / ai-models.json and shares global credentials. Manage it via the environment configuration instead.`
}
}
+ // Server model ids are built from the slugified name, so names must
+ // stay distinct after slugifying ("OpenAI" and "openai" would collide)
const names = list.map((p) => displayName(p))
- if (new Set(names).size !== names.length) {
- return "Provider display names must be unique."
+ const slugs = names.map(slugify)
+ if (new Set(slugs).size !== slugs.length) {
+ return "Provider display names must be unique (ignoring case and punctuation)."
}
- const envNames = new Set(envProviders.map((p) => p.name))
- const clash = names.find((n) => envNames.has(n))
+ const envSlugs = new Set(envProviders.map((p) => slugify(p.name)))
+ const clash = names.find((_, i) => envSlugs.has(slugs[i]))
if (clash) {
return `"${clash}" is already defined in AI_MODELS_CONFIG / ai-models.json. Use a different display name.`
}
@@ -240,10 +244,14 @@ export function deriveEnvUpdates(
indexByProvider.set(p.provider, index + 1)
if (p.provider === "bedrock") {
- if (p.awsAccessKeyId) updates.AWS_ACCESS_KEY_ID = p.awsAccessKeyId
+ // ADMIN_ names keep the standard AWS_* vars untouched, so other
+ // AWS clients (e.g. the DynamoDB quota table) keep their own
+ // credentials instead of picking up the panel's Bedrock keys
+ if (p.awsAccessKeyId)
+ updates.ADMIN_AWS_ACCESS_KEY_ID = p.awsAccessKeyId
if (p.awsSecretAccessKey)
- updates.AWS_SECRET_ACCESS_KEY = p.awsSecretAccessKey
- if (p.awsRegion) updates.AWS_REGION = p.awsRegion
+ updates.ADMIN_AWS_SECRET_ACCESS_KEY = p.awsSecretAccessKey
+ if (p.awsRegion) updates.ADMIN_AWS_REGION = p.awsRegion
} else if (p.provider === "vertexai") {
if (p.vertexApiKey) updates.GOOGLE_VERTEX_API_KEY = p.vertexApiKey
if (p.baseUrl) updates.GOOGLE_VERTEX_BASE_URL = p.baseUrl
@@ -284,6 +292,10 @@ function derivedEnvKeys(list: StoredAdminProvider[]): string[] {
const index = indexByProvider.get(p.provider) ?? 0
indexByProvider.set(p.provider, index + 1)
if (p.provider === "bedrock") {
+ keys.add("ADMIN_AWS_ACCESS_KEY_ID")
+ keys.add("ADMIN_AWS_SECRET_ACCESS_KEY")
+ keys.add("ADMIN_AWS_REGION")
+ // Written by older versions; listed so the next save clears them
keys.add("AWS_ACCESS_KEY_ID")
keys.add("AWS_SECRET_ACCESS_KEY")
keys.add("AWS_REGION")
diff --git a/lib/admin/settings.ts b/lib/admin/settings.ts
index 664c370e..f2ecce40 100644
--- a/lib/admin/settings.ts
+++ b/lib/admin/settings.ts
@@ -10,13 +10,27 @@ interface SettingsFile {
values: Record
}
-// Original env values snapshotted before the first overlay, so removing a
-// key from the settings file restores the env default. null = was unset.
-const originalEnv: Record = {}
-// Keys currently overlaid, so we can restore ones removed from the file.
-let overlaidKeys = new Set()
+interface SettingsState {
+ // Original env values snapshotted before the first overlay, so removing
+ // a key from the settings file restores the env default. null = was unset.
+ originalEnv: Record
+ // Keys currently overlaid, so we can restore ones removed from the file.
+ overlaidKeys: Set
+ cachedSettings: Record | null
+}
-let cachedSettings: Record | null = null
+// Kept on globalThis because the build can load this module more than once
+// (instrumentation.ts and the API routes get separate copies); per-module
+// state would make a route forget what instrumentation overlaid at startup.
+const globalState = globalThis as typeof globalThis & {
+ __adminSettingsState?: SettingsState
+}
+globalState.__adminSettingsState ??= {
+ originalEnv: {},
+ overlaidKeys: new Set(),
+ cachedSettings: null,
+}
+const state = globalState.__adminSettingsState
export function getSettingsPath(): string {
const custom = process.env.SETTINGS_FILE
@@ -25,7 +39,7 @@ export function getSettingsPath(): string {
}
export function loadSettings(): Record {
- if (cachedSettings) return cachedSettings
+ if (state.cachedSettings) return state.cachedSettings
try {
const raw = fs.readFileSync(getSettingsPath(), "utf8")
const parsed = JSON.parse(raw) as SettingsFile
@@ -43,21 +57,22 @@ export function loadSettings(): Record {
for (const [key, value] of Object.entries(rawValues)) {
if (typeof value === "string") values[key] = value
}
- cachedSettings = values
+ state.cachedSettings = values
} catch (err: any) {
if (err?.code !== "ENOENT") {
console.error("[admin-settings] Failed to read settings file:", err)
}
- cachedSettings = {}
+ state.cachedSettings = {}
}
- return cachedSettings
+ return state.cachedSettings
}
export function applyToEnv(): void {
const values = loadSettings()
+ const { originalEnv } = state
// Restore env for keys that were overlaid before but are now gone
- for (const key of overlaidKeys) {
+ for (const key of state.overlaidKeys) {
if (!(key in values)) {
const original = originalEnv[key]
if (original === null) delete process.env[key]
@@ -72,12 +87,12 @@ export function applyToEnv(): void {
process.env[key] = value
}
- overlaidKeys = new Set(Object.keys(values))
+ state.overlaidKeys = new Set(Object.keys(values))
}
// The effective env value if the file entry were removed (for fallback display)
export function getEnvFallback(key: string): string | null {
- if (overlaidKeys.has(key)) return originalEnv[key] ?? null
+ if (state.overlaidKeys.has(key)) return state.originalEnv[key] ?? null
return process.env[key] ?? null
}
@@ -101,7 +116,7 @@ export function saveSettings(updates: Record): void {
fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2), { mode: 0o600 })
fs.renameSync(tmpPath, filePath)
- cachedSettings = current
+ state.cachedSettings = current
applyToEnv()
}
@@ -122,13 +137,13 @@ export function isSettingsWritable(): boolean {
// Test-only: reset module state
export function _resetForTests(): void {
- cachedSettings = null
+ state.cachedSettings = null
writableCache = null
- for (const key of overlaidKeys) {
- const original = originalEnv[key]
+ for (const key of state.overlaidKeys) {
+ const original = state.originalEnv[key]
if (original === null) delete process.env[key]
else if (original !== undefined) process.env[key] = original
}
- overlaidKeys = new Set()
- for (const key of Object.keys(originalEnv)) delete originalEnv[key]
+ state.overlaidKeys = new Set()
+ state.originalEnv = {}
}
diff --git a/lib/ai-providers.ts b/lib/ai-providers.ts
index eb45831a..bf040440 100644
--- a/lib/ai-providers.ts
+++ b/lib/ai-providers.ts
@@ -1,16 +1,33 @@
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock"
import { createAnthropic } from "@ai-sdk/anthropic"
-import { azure, createAzure } from "@ai-sdk/azure"
-import { createDeepSeek, deepseek } from "@ai-sdk/deepseek"
-import { createGateway, gateway } from "@ai-sdk/gateway"
-import { createGoogleGenerativeAI, google } from "@ai-sdk/google"
+import { createAzure } from "@ai-sdk/azure"
+import { createDeepSeek } from "@ai-sdk/deepseek"
+import { createGoogleGenerativeAI } from "@ai-sdk/google"
import { createVertex } from "@ai-sdk/google-vertex"
-import { createOpenAI, openai } from "@ai-sdk/openai"
-import { aihubmix, createAihubmix } from "@aihubmix/ai-sdk-provider"
+import { createOpenAI } from "@ai-sdk/openai"
+import { createOpenAICompatible } from "@ai-sdk/openai-compatible"
+import { createAihubmix } from "@aihubmix/ai-sdk-provider"
import { fromNodeProviderChain } from "@aws-sdk/credential-providers"
import { createOpenRouter } from "@openrouter/ai-sdk-provider"
-import { createOllama, ollama } from "ollama-ai-provider-v2"
-import { PROVIDER_INFO, type ProviderName } from "@/lib/types/model-config"
+import {
+ createGateway,
+ defaultSettingsMiddleware,
+ extractReasoningMiddleware,
+ type LanguageModel,
+ wrapLanguageModel,
+} from "ai"
+import { createOllama } from "ollama-ai-provider-v2"
+import {
+ adminProvidersToConfig,
+ loadAdminProviders,
+} from "@/lib/admin/providers"
+import { getEnvFallback } from "@/lib/admin/settings"
+import { isPrivateUrl, redirectGuardedFetch } from "@/lib/ssrf-protection"
+import {
+ normalizeBaseUrl,
+ PROVIDER_INFO,
+ type ProviderName,
+} from "@/lib/types/model-config"
export type { ProviderName }
@@ -19,7 +36,6 @@ export const AIHUBMIX_APP_CODE = "MSBS9675"
interface ModelConfig {
model: any
providerOptions?: any
- headers?: Record
modelId: string
provider: ProviderName
}
@@ -85,6 +101,9 @@ export interface ClientOverrides {
awsSessionToken?: string | null
// Vertex AI config
vertexApiKey?: string | null // Express Mode API key
+ // baseUrl is the server's own _BASE_URL (the admin panel's Test),
+ // not one a user chose: no redirect guard
+ trustedBaseUrl?: boolean
// Custom headers (e.g., for EdgeOne cookie auth)
headers?: Record
// Custom env var name(s) for server models
@@ -93,34 +112,6 @@ export interface ClientOverrides {
baseUrlEnv?: string
}
-// Providers that can be selected from client settings
-const ALLOWED_CLIENT_PROVIDERS: ProviderName[] = [
- "openai",
- "anthropic",
- "google",
- "vertexai",
- "azure",
- "bedrock",
- "openrouter",
- "aihubmix",
- "deepseek",
- "siliconflow",
- "sglang",
- "gateway",
- "edgeone",
- "ollama",
- "doubao",
- "modelscope",
- "glm",
- "qwen",
- "qiniu",
- "kimi",
- "minimax",
- "novita",
- "mimo",
- "atlascloud",
-]
-
// Bedrock provider options for Anthropic beta features
const BEDROCK_ANTHROPIC_BETA = {
bedrock: {
@@ -128,11 +119,6 @@ const BEDROCK_ANTHROPIC_BETA = {
},
}
-// Direct Anthropic API headers for beta features
-const ANTHROPIC_BETA_HEADERS = {
- "anthropic-beta": "fine-grained-tool-streaming-2025-05-14",
-}
-
/**
* Resolve baseURL based on whether user is providing their own API key.
* When user provides their own API key, we should NOT fall back to server's
@@ -237,13 +223,33 @@ function parseIntSafe(
return parsed
}
+/**
+ * GOOGLE_TOP_K and GOOGLE_TOP_P. They are call settings, so they go on the
+ * model through a middleware: as Google provider options they were dropped.
+ */
+function googleSamplingSettings(): { topK?: number; topP?: number } {
+ const settings: { topK?: number; topP?: number } = {}
+ const topK = parseIntSafe(process.env.GOOGLE_TOP_K, "GOOGLE_TOP_K", 1, 100)
+ if (topK) settings.topK = topK
+ if (process.env.GOOGLE_TOP_P) {
+ const topP = Number.parseFloat(process.env.GOOGLE_TOP_P)
+ if (Number.isNaN(topP) || topP < 0 || topP > 1) {
+ throw new Error(
+ `GOOGLE_TOP_P must be a number between 0 and 1, got: ${process.env.GOOGLE_TOP_P}`,
+ )
+ }
+ settings.topP = topP
+ }
+ return settings
+}
+
/**
* Build provider-specific options from environment variables
* Supports various AI SDK providers with their unique configuration options
*
* Environment variables:
- * - OPENAI_REASONING_EFFORT: OpenAI reasoning effort level (minimal/low/medium/high) - for o1/o3/o4/gpt-5
- * - OPENAI_REASONING_SUMMARY: OpenAI reasoning summary (auto/detailed) - auto-enabled for o1/o3/o4/gpt-5
+ * - OPENAI_REASONING_EFFORT: OpenAI reasoning effort level (minimal/low/medium/high) - for the o-series and gpt-5 or later
+ * - OPENAI_REASONING_SUMMARY: OpenAI reasoning summary (auto/detailed) - auto-enabled for the o-series and gpt-5 or later
* - ANTHROPIC_THINKING_BUDGET_TOKENS: Anthropic thinking budget in tokens (1024-64000)
* - ANTHROPIC_THINKING_TYPE: Anthropic thinking type (enabled)
* - GOOGLE_THINKING_BUDGET: Google Gemini 2.5 thinking budget in tokens (1024-100000)
@@ -267,14 +273,9 @@ function buildProviderOptions(
const reasoningEffort = process.env.OPENAI_REASONING_EFFORT
const reasoningSummary = process.env.OPENAI_REASONING_SUMMARY
- // OpenAI reasoning models (o1, o3, o4, gpt-5) need reasoningSummary to return thoughts
- if (
- modelId &&
- (modelId.includes("o1") ||
- modelId.includes("o3") ||
- modelId.includes("o4") ||
- modelId.includes("gpt-5"))
- ) {
+ // Reasoning models (the o-series, gpt-5 and later) need
+ // reasoningSummary to return thoughts
+ if (modelId && /^(o\d|gpt-([5-9]|[1-9]\d))/.test(modelId)) {
options.openai = {
// Auto-enable reasoning summary for reasoning models
// Use 'auto' as default since not all models support 'detailed'
@@ -331,7 +332,6 @@ function buildProviderOptions(
}
case "google": {
- const reasoningEffort = process.env.GOOGLE_REASONING_EFFORT
const thinkingBudgetVal = parseIntSafe(
process.env.GOOGLE_THINKING_BUDGET,
"GOOGLE_THINKING_BUDGET",
@@ -370,47 +370,6 @@ function buildProviderOptions(
}
options.google = { thinkingConfig }
- } else if (reasoningEffort) {
- options.google = {
- reasoningEffort: reasoningEffort as
- | "low"
- | "medium"
- | "high",
- }
- }
-
- // Keep existing Google options
- const options_obj: Record = {}
- const candidateCount = parseIntSafe(
- process.env.GOOGLE_CANDIDATE_COUNT,
- "GOOGLE_CANDIDATE_COUNT",
- 1,
- 8,
- )
- if (candidateCount) {
- options_obj.candidateCount = candidateCount
- }
- const topK = parseIntSafe(
- process.env.GOOGLE_TOP_K,
- "GOOGLE_TOP_K",
- 1,
- 100,
- )
- if (topK) {
- options_obj.topK = topK
- }
- if (process.env.GOOGLE_TOP_P) {
- const topP = Number.parseFloat(process.env.GOOGLE_TOP_P)
- if (Number.isNaN(topP) || topP < 0 || topP > 1) {
- throw new Error(
- `GOOGLE_TOP_P must be a number between 0 and 1, got: ${process.env.GOOGLE_TOP_P}`,
- )
- }
- options_obj.topP = topP
- }
-
- if (Object.keys(options_obj).length > 0) {
- options.google = { ...options.google, ...options_obj }
}
break
}
@@ -530,27 +489,6 @@ function buildProviderOptions(
break
}
- case "deepseek":
- case "openrouter":
- case "aihubmix":
- case "siliconflow":
- case "sglang":
- case "gateway":
- case "modelscope":
- case "doubao":
- case "minimax":
- case "glm":
- case "qwen":
- case "kimi":
- case "qiniu":
- case "novita":
- case "atlascloud":
- case "mimo": {
- // These providers don't have reasoning configs in AI SDK yet
- // Gateway passes through to underlying providers which handle their own configs
- break
- }
-
default:
break
}
@@ -635,6 +573,7 @@ function detectProvider(): ProviderName | null {
function validateProviderCredentials(
provider: ProviderName,
customApiKeyEnv?: string | string[],
+ customBaseUrlEnv?: string,
): void {
// Handle array of env var names - at least one must be set
if (Array.isArray(customApiKeyEnv)) {
@@ -670,9 +609,12 @@ function validateProviderCredentials(
}
}
- // Azure requires either AZURE_BASE_URL or AZURE_RESOURCE_NAME in addition to API key
+ // Azure requires either AZURE_BASE_URL or AZURE_RESOURCE_NAME in addition
+ // to API key, or a server model's own URL variable (an admin panel entry)
if (provider === "azure") {
- const hasBaseUrl = !!process.env.AZURE_BASE_URL
+ const hasBaseUrl =
+ !!process.env.AZURE_BASE_URL ||
+ !!(customBaseUrlEnv && process.env[customBaseUrlEnv])
const hasResourceName = !!process.env.AZURE_RESOURCE_NAME
if (!hasBaseUrl && !hasResourceName) {
throw new Error(
@@ -683,41 +625,189 @@ function validateProviderCredentials(
}
}
+/** AWS's Bedrock endpoint for a region, as the Bedrock SDK builds it */
+function bedrockRuntimeUrl(region: string): string {
+ const suffix =
+ [
+ ["cn-", "amazonaws.com.cn"],
+ ["us-iso-", "c2s.ic.gov"],
+ ["us-isob-", "sc2s.sgov.gov"],
+ ["eu-isoe-", "cloud.adc-e.uk"],
+ ["us-isof-", "csp.hci.ic.gov"],
+ ["eusc-", "amazonaws.eu"],
+ ].find(([prefix]) => region.startsWith(prefix))?.[1] ?? "amazonaws.com"
+ return `https://bedrock-runtime.${region}.${suffix}`
+}
+
/**
- * Get the AI model based on environment variables
- *
- * Environment variables:
- * - AI_PROVIDER: The provider to use (bedrock, openai, anthropic, google, azure, ollama, openrouter, aihubmix, deepseek, siliconflow, sglang, gateway, modelscope)
- * - AI_MODEL: The model ID/name for the selected provider
- *
- * Provider-specific env vars:
- * - OPENAI_API_KEY: OpenAI API key
- * - OPENAI_BASE_URL: Custom OpenAI-compatible endpoint (optional)
- * - ANTHROPIC_API_KEY: Anthropic API key
- * - GOOGLE_GENERATIVE_AI_API_KEY: Google API key
- * - AZURE_RESOURCE_NAME, AZURE_API_KEY: Azure OpenAI credentials
- * - AWS_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY: AWS Bedrock credentials
- * - OLLAMA_BASE_URL: Ollama server URL (optional, defaults to https://ollama.com/api)
- * - OPENROUTER_API_KEY: OpenRouter API key
- * - AIHUBMIX_API_KEY: AIHubMix API key
- * - DEEPSEEK_API_KEY: DeepSeek API key
- * - DEEPSEEK_BASE_URL: DeepSeek endpoint (optional)
- * - SILICONFLOW_API_KEY: SiliconFlow API key
- * - SILICONFLOW_BASE_URL: SiliconFlow endpoint (optional, defaults to https://api.siliconflow.cn/v1)
- * - SGLANG_API_KEY: SGLang API key
- * - SGLANG_BASE_URL: SGLang endpoint (optional)
- * - MODELSCOPE_API_KEY: ModelScope API key
- * - MODELSCOPE_BASE_URL: ModelScope endpoint (optional)
+ * Providers whose SDK has the official endpoint built in. The others are
+ * OpenAI-compatible APIs (or Anthropic) that are called at
+ * PROVIDER_INFO.defaultBaseUrl unless a base URL is configured.
*/
-export function getAIModel(overrides?: ClientOverrides): ModelConfig {
+const SDK_KNOWS_ENDPOINT = new Set([
+ "openai",
+ "google",
+ "azure",
+ "openrouter",
+ "gateway",
+ "deepseek",
+])
+
+/** Where and how to call a provider, once credentials are resolved */
+interface Endpoint {
+ apiKey?: string
+ baseURL?: string
+ headers?: Record
+ fetch?: typeof fetch
+ authToken?: string // Anthropic Bearer auth
+ resourceName?: string // Azure
+ // baseURL comes from the settings or env, not the provider's default
+ configuredBaseURL?: boolean
+}
+
+/**
+ * An OpenAI-compatible chat model. includeUsage asks for token usage in the
+ * stream, which quota tracking needs. Some of these models write their
+ * reasoning inside tags; that text becomes reasoning, not reply.
+ */
+function compatibleModel(
+ provider: ProviderName,
+ modelId: string,
+ e: Endpoint,
+): LanguageModel {
+ const model = createOpenAICompatible({
+ name: provider,
+ apiKey: e.apiKey,
+ baseURL: e.baseURL ?? "",
+ ...(e.headers && { headers: e.headers }),
+ ...(e.fetch && { fetch: e.fetch }),
+ includeUsage: true,
+ })(modelId)
+ return wrapLanguageModel({
+ model,
+ middleware: extractReasoningMiddleware({ tagName: "think" }),
+ })
+}
+
+/** Create the model for a provider. Credentials are already resolved. */
+function createModel(
+ provider: ProviderName,
+ modelId: string,
+ e: Endpoint,
+): LanguageModel {
+ const opts = {
+ apiKey: e.apiKey,
+ ...(e.baseURL && { baseURL: e.baseURL }),
+ ...(e.fetch && { fetch: e.fetch }),
+ }
+ switch (provider) {
+ case "openai": {
+ const openaiProvider = createOpenAI(opts)
+ // A configured base URL is usually a proxy that only has Chat
+ // Completions; without one the Responses API is used, which
+ // returns reasoning for the o-series and gpt-5 or later
+ return e.configuredBaseURL
+ ? openaiProvider.chat(modelId)
+ : openaiProvider(modelId)
+ }
+ case "anthropic":
+ // The provider streams tool input per tool (eager_input_streaming),
+ // which replaced the fine-grained-tool-streaming beta header
+ return createAnthropic({
+ ...(e.authToken
+ ? { authToken: e.authToken }
+ : { apiKey: e.apiKey }),
+ baseURL: e.baseURL,
+ ...(e.fetch && { fetch: e.fetch }),
+ })(modelId)
+ case "google": {
+ const model = createGoogleGenerativeAI(opts)(modelId)
+ const sampling = googleSamplingSettings()
+ return Object.keys(sampling).length > 0
+ ? wrapLanguageModel({
+ model,
+ middleware: defaultSettingsMiddleware({
+ settings: sampling,
+ }),
+ })
+ : model
+ }
+ case "azure":
+ // baseURL takes precedence over resourceName per SDK behavior
+ return createAzure({
+ ...opts,
+ ...(!e.baseURL &&
+ e.resourceName && { resourceName: e.resourceName }),
+ })(modelId)
+ case "openrouter":
+ return createOpenRouter(opts)(modelId)
+ case "gateway":
+ // Without a key or URL the SDK uses Vercel's endpoint and OIDC
+ return createGateway(opts)(modelId)
+ case "deepseek":
+ case "kimi":
+ case "mimo":
+ // Kimi and MiMo return reasoning_content like DeepSeek and need it
+ // passed back in multi-turn tool calls (MiMo answers 400 otherwise)
+ return createDeepSeek(opts)(modelId)
+ case "doubao": {
+ // DeepSeek and Kimi models on Doubao use reasoning_content too
+ const lower = modelId.toLowerCase()
+ return lower.includes("deepseek") || lower.includes("kimi")
+ ? createDeepSeek(opts)(modelId)
+ : compatibleModel(provider, modelId, e)
+ }
+ case "aihubmix":
+ return isAihubmixStandardBaseURL(e.baseURL)
+ ? createAihubmix({
+ apiKey: e.apiKey,
+ appCode: AIHUBMIX_APP_CODE,
+ })(modelId)
+ : compatibleModel(provider, modelId, e)
+ case "minimax": {
+ const { baseURL, isAnthropicCompatible } = normalizeMiniMaxBaseURL(
+ e.baseURL as string,
+ )
+ return isAnthropicCompatible
+ ? createAnthropic({
+ apiKey: e.apiKey,
+ baseURL,
+ ...(e.fetch && { fetch: e.fetch }),
+ })(modelId)
+ : compatibleModel(provider, modelId, { ...e, baseURL })
+ }
+ default:
+ // siliconflow, sglang, modelscope, glm, qwen, qiniu, novita,
+ // atlascloud, edgeone
+ return compatibleModel(provider, modelId, e)
+ }
+}
+
+/**
+ * Get the AI model for a chat request: the client's own provider and
+ * credentials, or the server's (AI_PROVIDER, AI_MODEL and each provider's
+ * _API_KEY / _BASE_URL, see env.example). The settings test
+ * button uses the same function, so a passing test means the chat works.
+ */
+export function getAIModel(clientOverrides?: ClientOverrides): ModelConfig {
+ // Drop an endpoint path pasted along with the client's base URL
+ const overrides = clientOverrides?.baseUrl
+ ? {
+ ...clientOverrides,
+ baseUrl: normalizeBaseUrl(clientOverrides.baseUrl),
+ }
+ : clientOverrides
// SECURITY: Prevent SSRF attacks (GHSA-9qf7-mprq-9qgm)
// If a custom baseUrl is provided, an API key MUST also be provided.
// This prevents attackers from redirecting server API keys to malicious endpoints.
// Exception: EdgeOne doesn't require API keys.
// Ollama is exempt only when no server OLLAMA_API_KEY is configured;
// when it IS configured, the outer guard also enforces client apiKey for custom baseUrls.
+ // A trusted URL is the server's own (the admin Test of an entry without
+ // one), not a user's
if (
overrides?.baseUrl &&
+ !overrides?.trustedBaseUrl &&
!overrides?.apiKey &&
!(overrides?.provider === "vertexai" && overrides?.vertexApiKey) &&
overrides?.provider !== "edgeone" &&
@@ -756,13 +846,9 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
let provider: ProviderName
if (overrides?.provider) {
// Validate client-provided provider
- if (
- !ALLOWED_CLIENT_PROVIDERS.includes(
- overrides.provider as ProviderName,
- )
- ) {
+ if (!Object.hasOwn(PROVIDER_INFO, overrides.provider)) {
throw new Error(
- `Invalid provider: ${overrides.provider}. Allowed providers: ${ALLOWED_CLIENT_PROVIDERS.join(", ")}`,
+ `Invalid provider: ${overrides.provider}. Allowed providers: ${Object.keys(PROVIDER_INFO).join(", ")}`,
)
}
provider = overrides.provider as ProviderName
@@ -780,52 +866,75 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
.map(([p]) => p)
if (configured.length === 0) {
+ const keys = Object.entries(PROVIDER_ENV_VARS)
+ .filter(([, envVar]) => envVar)
+ .map(([p, envVar]) => `- ${envVar} for ${p}`)
throw new Error(
`No AI provider configured. Please set one of the following API keys in your .env.local file:\n` +
- `- AI_GATEWAY_API_KEY for Vercel AI Gateway\n` +
- `- DEEPSEEK_API_KEY for DeepSeek\n` +
- `- OPENAI_API_KEY for OpenAI\n` +
- `- ANTHROPIC_API_KEY for Anthropic\n` +
- `- GOOGLE_GENERATIVE_AI_API_KEY for Google\n` +
- `- AWS_ACCESS_KEY_ID for Bedrock\n` +
- `- OPENROUTER_API_KEY for OpenRouter\n` +
- `- AIHUBMIX_API_KEY for AIHubMix\n` +
- `- AZURE_API_KEY for Azure\n` +
- `- SILICONFLOW_API_KEY for SiliconFlow\n` +
- `- SGLANG_API_KEY for SGLang\n` +
- `- MODELSCOPE_API_KEY for ModelScope\n` +
+ `${keys.join("\n")}\n` +
+ `- AWS_ACCESS_KEY_ID for bedrock\n` +
`Or set AI_PROVIDER=ollama for local Ollama.`,
)
- } else {
- throw new Error(
- `Multiple AI providers configured (${configured.join(", ")}). ` +
- `Please set AI_PROVIDER to specify which one to use.`,
- )
}
+ throw new Error(
+ `Multiple AI providers configured (${configured.join(", ")}). ` +
+ `Please set AI_PROVIDER to specify which one to use.`,
+ )
}
}
+ if (!Object.hasOwn(PROVIDER_INFO, provider)) {
+ throw new Error(
+ `Unknown AI provider: ${provider}. Supported providers: ${Object.keys(PROVIDER_INFO).join(", ")}`,
+ )
+ }
// Only validate server credentials if client isn't providing their own API key
if (!isClientOverride) {
- validateProviderCredentials(provider, overrides?.apiKeyEnv)
+ validateProviderCredentials(
+ provider,
+ overrides?.apiKeyEnv,
+ overrides?.baseUrlEnv,
+ )
}
console.log(`[AI Provider] Initializing ${provider} with model: ${modelId}`)
- let model: any
- let providerOptions: any
- let headers: Record | undefined
-
+ // Requests to a base URL the client chose must not follow redirects
+ const guardedFetch =
+ overrides?.baseUrl && !overrides.trustedBaseUrl
+ ? redirectGuardedFetch()
+ : undefined
// Build provider-specific options from environment variables
- const customProviderOptions = buildProviderOptions(provider, modelId)
+ let providerOptions = buildProviderOptions(provider, modelId)
+ let model: LanguageModel
switch (provider) {
case "bedrock": {
// Use client-provided credentials if available, otherwise fall back to IAM/env vars
const hasClientCredentials =
overrides?.awsAccessKeyId && overrides?.awsSecretAccessKey
+ // Keys from the admin panel. The ADMIN_ names keep them out of the
+ // default AWS credential chain, which other clients such as the
+ // DynamoDB quota manager use with their own credentials.
+ const adminAccessKeyId = process.env.ADMIN_AWS_ACCESS_KEY_ID
+ const adminSecretAccessKey = process.env.ADMIN_AWS_SECRET_ACCESS_KEY
+ // The region becomes part of the endpoint's host name, so a
+ // request's region must be a region name, or it could send the
+ // server's credentials to another host
+ if (
+ overrides?.awsRegion &&
+ !/^[a-z]{2,4}(-[a-z]+)+-\d{1,2}$/.test(overrides.awsRegion)
+ ) {
+ throw Object.assign(
+ new Error(`Invalid AWS region "${overrides.awsRegion}"`),
+ { statusCode: 400 },
+ )
+ }
const bedrockRegion =
- overrides?.awsRegion || process.env.AWS_REGION || "us-west-2"
+ overrides?.awsRegion ||
+ process.env.ADMIN_AWS_REGION ||
+ process.env.AWS_REGION ||
+ "us-west-2"
const bedrockProvider = hasClientCredentials
? createAmazonBedrock({
@@ -835,11 +944,25 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
...(overrides?.awsSessionToken && {
sessionToken: overrides.awsSessionToken,
}),
+ // Without an apiKey the SDK reads the server's
+ // AWS_BEARER_TOKEN_BEDROCK, which wins over the keys
+ apiKey: "",
+ // Without a baseURL it reads the server's
+ // AWS_ENDPOINT_URL_BEDROCK_RUNTIME / AWS_ENDPOINT_URL
+ baseURL: bedrockRuntimeUrl(bedrockRegion),
})
- : createAmazonBedrock({
- region: bedrockRegion,
- credentialProvider: fromNodeProviderChain(),
- })
+ : adminAccessKeyId && adminSecretAccessKey
+ ? createAmazonBedrock({
+ region: bedrockRegion,
+ accessKeyId: adminAccessKeyId,
+ secretAccessKey: adminSecretAccessKey,
+ // The keys the admin panel's Test button checked
+ apiKey: "",
+ })
+ : createAmazonBedrock({
+ region: bedrockRegion,
+ credentialProvider: fromNodeProviderChain(),
+ })
model = bedrockProvider(modelId)
// Add Anthropic beta options if using Claude models via Bedrock
if (modelId.includes("anthropic.claude")) {
@@ -847,102 +970,20 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
providerOptions = {
bedrock: {
...BEDROCK_ANTHROPIC_BETA.bedrock,
- ...(customProviderOptions?.bedrock || {}),
+ ...(providerOptions?.bedrock || {}),
},
}
- } else if (customProviderOptions) {
- providerOptions = customProviderOptions
}
break
}
- case "openai": {
- const apiKey = resolveApiKey(overrides, "OPENAI_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "OPENAI_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
- if (baseURL) {
- // Custom base URL = third-party proxy, use Chat Completions API
- // for compatibility (most proxies don't support /responses endpoint)
- const customOpenAI = createOpenAI({ apiKey, baseURL })
- model = customOpenAI.chat(modelId)
- } else if (overrides?.apiKey) {
- // Custom API key but official OpenAI endpoint, use Responses API
- // to support reasoning for gpt-5, o1, o3, o4 models
- const customOpenAI = createOpenAI({ apiKey })
- model = customOpenAI(modelId)
- } else {
- model = openai(modelId)
- }
- break
- }
-
- case "anthropic": {
- const apiKey = resolveApiKey(overrides, "ANTHROPIC_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "ANTHROPIC_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- "https://api.anthropic.com/v1",
- )
- // Anthropic supports two auth methods (mutually exclusive):
- // - apiKey: sends as `x-api-key` header
- // - authToken: sends as `Authorization: Bearer ` header
- // Prefer apiKey if present (including client overrides); fall back
- // to ANTHROPIC_AUTH_TOKEN env var only when no apiKey is available.
- const authToken = !apiKey
- ? process.env.ANTHROPIC_AUTH_TOKEN
- : undefined
- const customProvider = createAnthropic({
- ...(authToken ? { authToken } : { apiKey }),
- baseURL,
- headers: ANTHROPIC_BETA_HEADERS,
- })
- model = customProvider(modelId)
- // Add beta headers for fine-grained tool streaming
- headers = ANTHROPIC_BETA_HEADERS
- break
- }
-
- case "google": {
- const apiKey = resolveApiKey(
- overrides,
- "GOOGLE_GENERATIVE_AI_API_KEY",
- )
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "GOOGLE_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
- if (baseURL || overrides?.apiKey) {
- const customGoogle = createGoogleGenerativeAI({
- apiKey,
- ...(baseURL && { baseURL }),
- })
- model = customGoogle(modelId)
- } else {
- model = google(modelId)
- }
- break
- }
case "vertexai": {
// Express Mode: Use API key for authentication
- const vertexApiKey =
- overrides?.vertexApiKey || process.env.GOOGLE_VERTEX_API_KEY
+ // SECURITY: a client base URL only ever gets the client's key, so the
+ // server's GOOGLE_VERTEX_API_KEY is never sent to a client-chosen host
+ const vertexApiKey = overrides?.baseUrl
+ ? overrides.vertexApiKey
+ : overrides?.vertexApiKey || process.env.GOOGLE_VERTEX_API_KEY
if (!vertexApiKey) {
throw new Error(
@@ -951,491 +992,241 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
)
}
- // Support custom base URL from env or client override
- const baseURL =
- overrides?.baseUrl || process.env.GOOGLE_VERTEX_BASE_URL
-
- const vertexProvider = createVertex({
+ // Support custom base URL from env or client override.
+ // A client key only goes to the client's URL or the official one.
+ const baseURL = resolveBaseURL(
+ overrides?.vertexApiKey,
+ overrides?.baseUrl,
+ process.env.GOOGLE_VERTEX_BASE_URL,
+ )
+ model = createVertex({
apiKey: vertexApiKey,
...(baseURL && { baseURL }),
- })
- model = vertexProvider(modelId)
- break
- }
-
- case "azure": {
- const apiKey = resolveApiKey(overrides, "AZURE_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(overrides, "AZURE_BASE_URL")
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
- // Only use server's resourceName if user is NOT providing their own API key
- const resourceName = overrides?.apiKey
- ? undefined
- : process.env.AZURE_RESOURCE_NAME
- // Azure requires either baseURL or resourceName to construct the endpoint
- // resourceName constructs: https://{resourceName}.openai.azure.com/openai/v1{path}
- if (baseURL || resourceName || overrides?.apiKey) {
- const customAzure = createAzure({
- apiKey,
- // baseURL takes precedence over resourceName per SDK behavior
- ...(baseURL && { baseURL }),
- ...(!baseURL && resourceName && { resourceName }),
- })
- model = customAzure(modelId)
- } else {
- model = azure(modelId)
- }
+ ...(guardedFetch && { fetch: guardedFetch }),
+ })(modelId)
break
}
case "ollama": {
- const baseURL = overrides?.baseUrl || process.env.OLLAMA_BASE_URL
// SECURITY: When client provides a custom base URL, only use
// client-provided API key. Never fall back to server OLLAMA_API_KEY
// to prevent leaking server credentials to user-controlled endpoints.
const apiKey = overrides?.baseUrl
? overrides?.apiKey || undefined
: resolveApiKey(overrides, "OLLAMA_API_KEY")
- if (baseURL || apiKey) {
- const customOllama = createOllama({
- ...(baseURL && { baseURL }),
- ...(apiKey && {
- headers: { Authorization: `Bearer ${apiKey}` },
- }),
- })
- model = customOllama(modelId)
- } else {
- model = ollama(modelId)
- }
- break
- }
-
- case "openrouter": {
- const apiKey = resolveApiKey(overrides, "OPENROUTER_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "OPENROUTER_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
- const openrouter = createOpenRouter({
- apiKey,
+ // Like other providers, a user's key never goes to the server's
+ // base URL: without a URL of their own it goes to Ollama Cloud.
+ // The server's key goes to OLLAMA_BASE_URL (or a server model's
+ // own variable), else to the SDK's local default: the desktop
+ // app's "Ollama (Local)" preset puts its key field there too.
+ const baseURL =
+ overrides?.baseUrl ||
+ (overrides?.apiKey
+ ? PROVIDER_INFO.ollama.defaultBaseUrl
+ : resolveBaseUrlEnv(overrides, "OLLAMA_BASE_URL"))
+ model = createOllama({
...(baseURL && { baseURL }),
- })
- model = openrouter(modelId)
+ ...(apiKey && {
+ headers: { Authorization: `Bearer ${apiKey}` },
+ }),
+ ...(guardedFetch && { fetch: guardedFetch }),
+ })(modelId)
break
}
- case "aihubmix": {
- const apiKey = resolveApiKey(overrides, "AIHUBMIX_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
+ case "edgeone":
+ // EdgeOne Pages Edge AI, an OpenAI-compatible API without a key.
+ // The SDK appends /chat/completions to the base URL. Cookies
+ // (eo_token, eo_time) and the access code authenticate the call.
+ model = compatibleModel(provider, modelId, {
+ apiKey: "edgeone",
+ baseURL: overrides?.baseUrl || "/api/edgeai",
+ headers: overrides?.headers,
+ fetch: guardedFetch,
+ })
+ break
+
+ default: {
+ // Every other provider takes an API key and a base URL from
+ // _API_KEY / _BASE_URL (or a server model's apiKeyEnv)
+ const apiKey = resolveApiKey(
overrides,
- "AIHUBMIX_BASE_URL",
+ PROVIDER_ENV_VARS[provider] as string,
)
- const baseURL = resolveBaseURL(
+ const baseUrlEnv =
+ provider === "gateway"
+ ? "AI_GATEWAY_BASE_URL"
+ : `${provider.toUpperCase()}_BASE_URL`
+ // A local default (SGLang's 127.0.0.1) only fills the settings
+ // form; the server must not call its own machine for it. With a
+ // user's key the OpenAI SDK would read the server's
+ // OPENAI_BASE_URL, so name the official endpoint.
+ const defaultUrl = PROVIDER_INFO[provider].defaultBaseUrl
+ const publicDefault = defaultUrl?.startsWith("https://")
+ ? defaultUrl
+ : undefined
+ const configuredBaseURL = resolveBaseURL(
overrides?.apiKey,
overrides?.baseUrl,
- serverBaseUrl,
- PROVIDER_INFO.aihubmix.defaultBaseUrl,
+ resolveBaseUrlEnv(overrides, baseUrlEnv),
)
- const defaultBaseURL = PROVIDER_INFO.aihubmix.defaultBaseUrl
-
+ const baseURL =
+ configuredBaseURL ||
+ (SDK_KNOWS_ENDPOINT.has(provider) &&
+ !(provider === "openai" && overrides?.apiKey)
+ ? undefined
+ : publicDefault)
+ // With a user's Azure key the SDK would read the server's
+ // AZURE_RESOURCE_NAME
if (
- isAihubmixStandardBaseURL(baseURL) ||
- baseURL === defaultBaseURL
+ !baseURL &&
+ (!SDK_KNOWS_ENDPOINT.has(provider) ||
+ (provider === "azure" && overrides?.apiKey))
) {
- const aihubmixProvider =
- overrides?.apiKey || apiKey
- ? createAihubmix({
- apiKey,
- appCode: AIHUBMIX_APP_CODE,
- })
- : aihubmix
- model = aihubmixProvider(modelId)
- } else {
- const aihubmixCompatibleProvider = createOpenAI({
- apiKey,
- baseURL,
- })
- model = aihubmixCompatibleProvider.chat(modelId)
- }
- break
- }
-
- case "deepseek": {
- const apiKey = resolveApiKey(overrides, "DEEPSEEK_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "DEEPSEEK_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
- if (baseURL || overrides?.apiKey) {
- const customDeepSeek = createDeepSeek({
- apiKey,
- ...(baseURL && { baseURL }),
- })
- model = customDeepSeek(modelId)
- } else {
- model = deepseek(modelId)
- }
- break
- }
-
- case "siliconflow": {
- const apiKey = resolveApiKey(overrides, "SILICONFLOW_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "SILICONFLOW_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- "https://api.siliconflow.cn/v1",
- )
- const siliconflowProvider = createOpenAI({
- apiKey,
- baseURL,
- })
- model = siliconflowProvider.chat(modelId)
- break
- }
-
- case "sglang": {
- const apiKey = resolveApiKey(overrides, "SGLANG_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "SGLANG_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
-
- const sglangProvider = createOpenAI({
- apiKey,
- ...(baseURL && { baseURL }),
- // Add a custom fetch wrapper to intercept and fix the stream from sglang
- fetch: async (url, options) => {
- const response = await fetch(url, options)
- if (!response.body) {
- return response
- }
-
- // Create a transform stream to fix the non-compliant sglang stream
- let buffer = ""
- const decoder = new TextDecoder()
-
- const transformStream = new TransformStream({
- transform(chunk, controller) {
- buffer += decoder.decode(chunk, { stream: true })
- // Process all complete messages in the buffer
- let messageEndPos
- while (
- (messageEndPos = buffer.indexOf("\n\n")) !== -1
- ) {
- const message = buffer.substring(
- 0,
- messageEndPos,
- )
- buffer = buffer.substring(messageEndPos + 2) // Move past the '\n\n'
-
- if (message.startsWith("data: ")) {
- const jsonStr = message.substring(6).trim()
- if (jsonStr === "[DONE]") {
- controller.enqueue(
- new TextEncoder().encode(
- message + "\n\n",
- ),
- )
- continue
- }
- try {
- const data = JSON.parse(jsonStr)
- const delta = data.choices?.[0]?.delta
-
- if (delta) {
- // Fix 1: remove invalid empty role
- if (delta.role === "") {
- delete delta.role
- }
- // Fix 2: remove non-standard reasoning_content field
- if ("reasoning_content" in delta) {
- delete delta.reasoning_content
- }
- }
-
- // Re-serialize and forward the corrected data with the correct SSE format
- controller.enqueue(
- new TextEncoder().encode(
- `data: ${JSON.stringify(data)}\n\n`,
- ),
- )
- } catch (_e) {
- // If parsing fails, forward the original message to avoid breaking the stream.
- controller.enqueue(
- new TextEncoder().encode(
- message + "\n\n",
- ),
- )
- }
- } else if (message.trim() !== "") {
- // Pass through other message types (e.g., 'event: ...')
- controller.enqueue(
- new TextEncoder().encode(
- message + "\n\n",
- ),
- )
- }
- }
- },
- flush(controller) {
- // If there's anything left in the buffer, forward it.
- if (buffer.trim()) {
- controller.enqueue(
- new TextEncoder().encode(buffer),
- )
- }
- },
- })
-
- const transformedBody =
- response.body.pipeThrough(transformStream)
-
- // Return a new response with the transformed body
- return new Response(transformedBody, {
- status: response.status,
- statusText: response.statusText,
- headers: response.headers,
- })
- },
- })
- model = sglangProvider.chat(modelId)
- break
- }
-
- case "gateway": {
- // Vercel AI Gateway - unified access to multiple AI providers
- // Model format: "provider/model" e.g., "openai/gpt-4o", "anthropic/claude-sonnet-4-5"
- // See: https://vercel.com/ai-gateway
- const apiKey = resolveApiKey(overrides, "AI_GATEWAY_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "AI_GATEWAY_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- )
- // Only use custom configuration if explicitly set (local dev or custom Gateway)
- // Otherwise undefined → AI SDK uses Vercel default (https://ai-gateway.vercel.sh/v1/ai) + OIDC
- if (baseURL || overrides?.apiKey) {
- const customGateway = createGateway({
- apiKey,
- ...(baseURL && { baseURL }),
- })
- model = customGateway(modelId)
- } else {
- model = gateway(modelId)
- }
- break
- }
-
- case "edgeone": {
- // EdgeOne Pages Edge AI - uses OpenAI-compatible API
- // AI SDK appends /chat/completions to baseURL
- // /api/edgeai + /chat/completions = /api/edgeai/chat/completions
- const baseURL = overrides?.baseUrl || "/api/edgeai"
- const edgeoneProvider = createOpenAI({
- apiKey: "edgeone", // Dummy key - EdgeOne doesn't require API key
- baseURL,
- // Pass cookies for EdgeOne Pages authentication (eo_token, eo_time)
- ...(overrides?.headers && { headers: overrides.headers }),
- })
- model = edgeoneProvider.chat(modelId)
- break
- }
-
- case "doubao": {
- const apiKey = resolveApiKey(overrides, "DOUBAO_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "DOUBAO_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- "https://ark.cn-beijing.volces.com/api/v3",
- )
- const lowerModelId = modelId.toLowerCase()
- // Use DeepSeek provider for DeepSeek/Kimi models, OpenAI for others (multimodal support)
- if (
- lowerModelId.includes("deepseek") ||
- lowerModelId.includes("kimi")
- ) {
- const doubaoProvider = createDeepSeek({
- apiKey,
- baseURL,
- })
- model = doubaoProvider(modelId)
- } else {
- const doubaoProvider = createOpenAI({
- apiKey,
- baseURL,
- })
- model = doubaoProvider.chat(modelId)
- }
- break
- }
-
- case "modelscope": {
- const apiKey = resolveApiKey(overrides, "MODELSCOPE_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "MODELSCOPE_BASE_URL",
- )
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- "https://api-inference.modelscope.cn/v1",
- )
- const modelscopeProvider = createOpenAI({
- apiKey,
- baseURL,
- })
- model = modelscopeProvider.chat(modelId)
- break
- }
-
- case "minimax": {
- const apiKey = resolveApiKey(overrides, "MINIMAX_API_KEY")
- const serverBaseUrl = resolveBaseUrlEnv(
- overrides,
- "MINIMAX_BASE_URL",
- )
- const rawBaseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- serverBaseUrl,
- PROVIDER_INFO.minimax.defaultBaseUrl,
- )
-
- if (!rawBaseURL) {
throw new Error(
- "MiniMax base URL could not be resolved. Set MINIMAX_BASE_URL or configure a base URL in settings.",
+ `${PROVIDER_INFO[provider].label} needs a base URL. Add it in the model settings.`,
)
}
-
- const { baseURL, isAnthropicCompatible } =
- normalizeMiniMaxBaseURL(rawBaseURL)
-
- if (isAnthropicCompatible) {
- const minimax = createAnthropic({ apiKey, baseURL })
- model = minimax.chat(modelId)
- } else {
- const minimax = createOpenAI({ apiKey, baseURL })
- model = minimax.chat(modelId)
- }
- break
- }
-
- case "mimo": {
- const apiKey = resolveApiKey(overrides, "MIMO_API_KEY")
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- resolveBaseUrlEnv(overrides, "MIMO_BASE_URL"),
- PROVIDER_INFO.mimo?.defaultBaseUrl,
- )
- // Use createDeepSeek to properly handle reasoning_content for MiMo
- // thinking models (e.g., mimo-v2.5-pro). MiMo's API requires
- // reasoning_content to be passed back during multi-turn tool calls
- // (returns 400 otherwise), same convention as DeepSeek and Kimi.
- const mimoProvider = createDeepSeek({ apiKey, baseURL })
- model = mimoProvider(modelId)
- break
- }
-
- case "glm":
- case "qwen":
- case "qiniu":
- case "novita":
- case "atlascloud": {
- const envVar = PROVIDER_ENV_VARS[provider]
- if (!envVar) {
- throw new Error(
- `API key environment variable not defined for provider: ${provider}`,
- )
- }
- const apiKey = resolveApiKey(overrides, envVar)
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- resolveBaseUrlEnv(
- overrides,
- `${provider.toUpperCase()}_BASE_URL`,
- ),
- PROVIDER_INFO[provider]?.defaultBaseUrl,
- )
- const customProvider = createOpenAI({
+ model = createModel(provider, modelId, {
apiKey,
baseURL,
+ configuredBaseURL: !!configuredBaseURL,
+ fetch: guardedFetch,
+ // Bearer auth for Anthropic when there is no API key
+ authToken:
+ provider === "anthropic" && !apiKey
+ ? process.env.ANTHROPIC_AUTH_TOKEN
+ : undefined,
+ // Only the server's own resource; a client key needs its URL
+ resourceName:
+ provider === "azure" && !overrides?.apiKey
+ ? process.env.AZURE_RESOURCE_NAME
+ : undefined,
})
- model = customProvider.chat(modelId)
- break
}
-
- case "kimi": {
- const apiKey = resolveApiKey(overrides, "KIMI_API_KEY")
- const baseURL = resolveBaseURL(
- overrides?.apiKey,
- overrides?.baseUrl,
- resolveBaseUrlEnv(overrides, "KIMI_BASE_URL"),
- PROVIDER_INFO.kimi?.defaultBaseUrl,
- )
- // Use createDeepSeek to properly handle reasoning_content for Kimi
- // thinking models (e.g., kimi-k2.6). Kimi's API uses the same
- // reasoning_content field as DeepSeek, so this provider correctly
- // captures and replays reasoning in multi-turn conversations.
- const customProvider = createDeepSeek({ apiKey, baseURL })
- model = customProvider(modelId)
- break
- }
-
- default:
- throw new Error(
- `Unknown AI provider: ${provider}. Supported providers: bedrock, openai, anthropic, google, azure, ollama, openrouter, aihubmix, deepseek, siliconflow, sglang, gateway, edgeone, doubao, modelscope, glm, qwen, qiniu, kimi, minimax, novita, mimo, atlascloud`,
- )
}
- // Apply provider-specific options for all providers except bedrock (which has special handling)
- if (customProviderOptions && provider !== "bedrock" && !providerOptions) {
- providerOptions = customProviderOptions
- }
-
- return { model, providerOptions, headers, modelId, provider }
+ return { model, providerOptions, modelId, provider }
}
/**
- * Check if a model supports prompt caching.
- * Currently only Claude models on Bedrock support prompt caching.
+ * The deployment's EdgeOne Pages function, as an absolute URL (the SDK
+ * needs one). EdgeOne serves functions by their folder from the site root,
+ * so Next's base path does not apply.
+ */
+export function edgeOneEndpoint(req: Request): string {
+ const origin = req.headers.get("origin") || new URL(req.url).origin
+ return `${origin}/api/edgeai`
+}
+
+/**
+ * The server's _BASE_URL for a provider, which getAIModel uses for a
+ * server model without a URL variable of its own (an admin panel entry
+ * without a URL). None for Bedrock and EdgeOne. Ollama and Vertex AI share
+ * one variable with the panel, which writes an entry's URL into it: an
+ * entry without a URL gets the environment's value once saved (before a
+ * save the variable may still hold the entry's previous URL), and Ollama
+ * without one goes to the SDK's local default.
+ */
+export function globalBaseUrl(provider: ProviderName): string | undefined {
+ if (provider === "ollama") {
+ return getEnvFallback("OLLAMA_BASE_URL") || "http://127.0.0.1:11434/api"
+ }
+ if (provider === "vertexai") {
+ return getEnvFallback("GOOGLE_VERTEX_BASE_URL") || undefined
+ }
+ if (provider === "bedrock" || provider === "edgeone") return undefined
+ // Azure set up by resource name only: the URL the SDK builds from it
+ if (
+ provider === "azure" &&
+ !process.env.AZURE_BASE_URL &&
+ process.env.AZURE_RESOURCE_NAME
+ ) {
+ return `https://${process.env.AZURE_RESOURCE_NAME}.openai.azure.com/openai`
+ }
+ const name =
+ provider === "gateway"
+ ? "AI_GATEWAY_BASE_URL"
+ : `${provider.toUpperCase()}_BASE_URL`
+ return process.env[name] || undefined
+}
+
+/** The provider of the server's own config: AI_PROVIDER, or the one with a key */
+export function getServerProvider(): ProviderName | null {
+ return (process.env.AI_PROVIDER as ProviderName) || detectProvider()
+}
+
+/**
+ * Whether a call made with the caller's own settings runs on an endpoint of
+ * the deployment: its EdgeOne function, the server's keyless Ollama, or an
+ * address on the server's network (which ignores a dummy key header).
+ * Bedrock and EdgeOne never use a client base URL. Never in the desktop
+ * app, where every endpoint is the user's. clientBaseUrl: normalized.
+ */
+export async function usesServerEndpoint(
+ provider: ProviderName | null | undefined,
+ clientBaseUrl: string,
+ apiKey: string | null | undefined,
+): Promise {
+ if (process.env.NEXT_AI_DRAWIO_DESKTOP === "1") return false
+ if (provider === "edgeone") return true
+ if (provider === "ollama" && !clientBaseUrl && !apiKey) return true
+ return (
+ provider !== "bedrock" &&
+ !!clientBaseUrl &&
+ (await isPrivateUrl(clientBaseUrl))
+ )
+}
+
+/**
+ * Whether the call is paid for by the server's own credentials (env keys or
+ * IAM role) rather than credentials sent with the request. Mirrors which key
+ * each branch of getAIModel ends up using.
+ */
+export function usesServerCredentials(
+ provider: ProviderName,
+ overrides?: ClientOverrides,
+): boolean {
+ // The desktop app's local server holds the user's own preset keys
+ if (process.env.NEXT_AI_DRAWIO_DESKTOP === "1") return false
+ // Cleaned like getAIModel does: "/" means no base URL
+ const baseUrl = normalizeBaseUrl(overrides?.baseUrl ?? "")
+ switch (provider) {
+ case "bedrock":
+ return !(overrides?.awsAccessKeyId && overrides?.awsSecretAccessKey)
+ case "vertexai":
+ return !overrides?.vertexApiKey
+ case "edgeone":
+ // The platform's own endpoint, no key involved
+ return false
+ case "ollama":
+ // Only a server key costs money; a keyless local server or the
+ // client's own server does not
+ return (
+ !baseUrl &&
+ !overrides?.apiKey &&
+ !!(overrides?.apiKeyEnv || process.env.OLLAMA_API_KEY)
+ )
+ default:
+ return !overrides?.apiKey
+ }
+}
+
+/**
+ * Prompt cache breakpoint for Claude, set on a message's providerOptions.
+ * Each provider reads only its own key; OpenRouter also reads the
+ * anthropic one.
+ */
+export const CACHE_POINT = {
+ bedrock: { cachePoint: { type: "default" } },
+ anthropic: { cacheControl: { type: "ephemeral" } },
+}
+
+/**
+ * Check if a model supports prompt caching: Claude models, on Bedrock,
+ * the Anthropic API or OpenRouter (see CACHE_POINT).
*/
export function supportsPromptCaching(modelId: string): boolean {
- // Bedrock prompt caching is supported for Claude models
return (
modelId.includes("claude") ||
modelId.includes("anthropic") ||
@@ -1464,6 +1255,17 @@ export function getValidationModel(): ReturnType["model"] {
)
}
- const { model } = getAIModel({ modelId })
+ // A default set in the admin panel becomes AI_PROVIDER/AI_MODEL, but its key
+ // lives in an ADMIN_-prefixed env var. Point at it the way the chat route
+ // does for server models, or the standard env var is required instead.
+ const panelDefault = adminProvidersToConfig(
+ loadAdminProviders(),
+ ).providers.find((p) => p.default && p.provider === process.env.AI_PROVIDER)
+
+ const { model } = getAIModel({
+ modelId,
+ apiKeyEnv: panelDefault?.apiKeyEnv,
+ baseUrlEnv: panelDefault?.baseUrlEnv,
+ })
return model
}
diff --git a/lib/aihubmix-models.ts b/lib/aihubmix-models.ts
deleted file mode 100644
index f52bae5c..00000000
--- a/lib/aihubmix-models.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-export const AIHUBMIX_MODELS_ENDPOINT = "https://aihubmix.com/api/v1/models"
-
-const NON_CHAT_MODEL_TYPES = new Set([
- "embedding",
- "image_generation",
- "rerank",
- "transcription",
- "tts",
- "video",
-])
-
-type AihubmixModelListPayload = {
- data?: unknown
-}
-
-type AihubmixModelRecord = {
- model_id?: unknown
- types?: unknown
-}
-
-function getModelTypes(types: unknown): Set {
- if (typeof types !== "string") {
- return new Set()
- }
-
- return new Set(
- types
- .split(",")
- .map((type) => type.trim())
- .filter(Boolean),
- )
-}
-
-function isChatModel(record: AihubmixModelRecord): record is {
- model_id: string
- types: string
-} {
- if (typeof record.model_id !== "string" || !record.model_id.trim()) {
- return false
- }
-
- const types = getModelTypes(record.types)
- if (!types.has("llm")) {
- return false
- }
-
- return !Array.from(NON_CHAT_MODEL_TYPES).some((type) => types.has(type))
-}
-
-export function extractAihubmixModelIds(payload: unknown): string[] {
- const data = (payload as AihubmixModelListPayload)?.data
- if (!Array.isArray(data)) {
- return []
- }
-
- const seen = new Set()
- const modelIds: string[] = []
-
- for (const item of data) {
- if (!item || typeof item !== "object") {
- continue
- }
-
- const record = item as AihubmixModelRecord
- if (!isChatModel(record)) {
- continue
- }
-
- const modelId = record.model_id.trim()
- if (seen.has(modelId)) {
- continue
- }
-
- seen.add(modelId)
- modelIds.push(modelId)
- }
-
- return modelIds
-}
diff --git a/lib/cached-responses.ts b/lib/cached-responses.ts
index 8b8375f3..c2b474f7 100644
--- a/lib/cached-responses.ts
+++ b/lib/cached-responses.ts
@@ -1,6 +1,8 @@
export interface CachedResponse {
promptText: string
hasImage: boolean
+ // Name of the bundled example file the prompt is sent with
+ fileName?: string
xml: string
}
@@ -254,6 +256,7 @@ export const CACHED_EXAMPLE_RESPONSES: CachedResponse[] = [
{
promptText: "Replicate this in aws style",
hasImage: true,
+ fileName: "architecture.png",
xml: `
@@ -318,6 +321,7 @@ export const CACHED_EXAMPLE_RESPONSES: CachedResponse[] = [
{
promptText: "Replicate this flowchart.",
hasImage: true,
+ fileName: "example.png",
xml: `
@@ -379,6 +383,7 @@ export const CACHED_EXAMPLE_RESPONSES: CachedResponse[] = [
{
promptText: "Summarize this paper as a diagram",
hasImage: true,
+ fileName: "chain-of-thought.txt",
xml: `
@@ -879,14 +884,19 @@ export const CACHED_EXAMPLE_RESPONSES: CachedResponse[] = [
},
]
+// Examples that come with a file only match when that exact example file is
+// attached, so a user's own file with the same prompt still goes to the model.
+// Callers that can't tell file names (the server) only get text-only examples.
export function findCachedResponse(
promptText: string,
hasImage: boolean,
+ fileName?: string,
): CachedResponse | undefined {
return CACHED_EXAMPLE_RESPONSES.find(
(c) =>
c.promptText === promptText &&
c.hasImage === hasImage &&
+ (!c.fileName || c.fileName === fileName) &&
c.xml !== "",
)
}
diff --git a/lib/chat-helpers.ts b/lib/chat-helpers.ts
index e9c054f1..ddd8a730 100644
--- a/lib/chat-helpers.ts
+++ b/lib/chat-helpers.ts
@@ -6,25 +6,37 @@ export const MAX_FILE_SIZE = 2 * 1024 * 1024 // 2MB
export const MAX_FILES = 5
// Helper function to validate file parts in messages
+// Checks every message, since history is sent to the model too
export function validateFileParts(messages: any[]): {
valid: boolean
error?: string
} {
- const lastMessage = messages[messages.length - 1]
- const fileParts =
- lastMessage?.parts?.filter((p: any) => p.type === "file") || []
+ for (const message of messages) {
+ const fileParts =
+ message?.parts?.filter((p: any) => p.type === "file") || []
- if (fileParts.length > MAX_FILES) {
- return {
- valid: false,
- error: `Too many files. Maximum ${MAX_FILES} allowed.`,
+ if (fileParts.length > MAX_FILES) {
+ return {
+ valid: false,
+ error: `Too many files. Maximum ${MAX_FILES} allowed.`,
+ }
}
- }
- for (const filePart of fileParts) {
- // Data URLs format: data:image/png;base64,
- // Base64 increases size by ~33%, so we check the decoded size
- if (filePart.url?.startsWith("data:")) {
+ for (const filePart of fileParts) {
+ // The client sends files inline. Any other URL would be downloaded
+ // by the server (AI SDK does that for models without URL support).
+ if (
+ typeof filePart.url !== "string" ||
+ !filePart.url.startsWith("data:")
+ ) {
+ return {
+ valid: false,
+ error: "Files must be uploaded inline as data URLs.",
+ }
+ }
+
+ // Data URLs format: data:image/png;base64,
+ // Base64 increases size by ~33%, so we check the decoded size
const base64Data = filePart.url.split(",")[1]
if (base64Data) {
const sizeInBytes = Math.ceil((base64Data.length * 3) / 4)
@@ -41,49 +53,83 @@ export function validateFileParts(messages: any[]): {
return { valid: true }
}
-// Helper function to check if diagram is minimal/empty
-export function isMinimalDiagram(xml: string): boolean {
- const stripped = xml.replace(/\s/g, "")
- return !stripped.includes('id="2"')
+// A tool-call input providers accept: a non-empty JSON object
+function isValidToolInput(input: unknown): boolean {
+ return !!input && typeof input === "object" && Object.keys(input).length > 0
}
// Helper function to replace historical tool call XML with placeholders
// This reduces token usage and forces LLM to rely on the current diagram XML (source of truth)
-// Also fixes invalid/undefined inputs from interrupted streaming
+// Tool calls with invalid inputs are left for dropInvalidToolCalls to remove
export function replaceHistoricalToolInputs(messages: any[]): any[] {
return messages.map((msg) => {
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
return msg
}
- const replacedContent = msg.content
- .map((part: any) => {
- if (part.type === "tool-call") {
- const toolName = part.toolName
- // Fix invalid/undefined inputs from interrupted streaming
- if (
- !part.input ||
- typeof part.input !== "object" ||
- Object.keys(part.input).length === 0
- ) {
- // Skip tool calls with invalid inputs entirely
- return null
- }
- if (
- toolName === "display_diagram" ||
- toolName === "edit_diagram"
- ) {
- return {
- ...part,
- input: {
- placeholder:
- "[XML content replaced - see current diagram XML in system context]",
- },
- }
- }
+ const replacedContent = msg.content.map((part: any) => {
+ if (
+ part.type === "tool-call" &&
+ isValidToolInput(part.input) &&
+ (part.toolName === "display_diagram" ||
+ part.toolName === "edit_diagram")
+ ) {
+ return {
+ ...part,
+ input: {
+ placeholder:
+ "[XML content replaced - see current diagram XML in system context]",
+ },
}
- return part
- })
- .filter(Boolean) // Remove null entries (invalid tool calls)
+ }
+ return part
+ })
return { ...msg, content: replacedContent }
})
}
+
+// Remove tool-calls with invalid inputs (from failed repair or interrupted streaming),
+// together with their tool-results: providers reject a result whose call is missing.
+// Messages left empty are removed too (Bedrock rejects empty content arrays).
+export function dropInvalidToolCalls(messages: any[]): any[] {
+ const droppedIds = new Set()
+ return messages
+ .map((msg) => {
+ if (!Array.isArray(msg.content)) return msg
+ const content = msg.content.filter((part: any) => {
+ if (
+ msg.role === "assistant" &&
+ part.type === "tool-call" &&
+ !isValidToolInput(part.input)
+ ) {
+ console.warn(
+ `[chat-helpers] Dropping tool-call with invalid input:`,
+ { toolName: part.toolName, input: part.input },
+ )
+ droppedIds.add(part.toolCallId)
+ return false
+ }
+ // Results always come after their call, so the id is known by now
+ return !(
+ part.type === "tool-result" &&
+ droppedIds.has(part.toolCallId)
+ )
+ })
+ return { ...msg, content }
+ })
+ .filter((msg) => !Array.isArray(msg.content) || msg.content.length > 0)
+}
+
+// Fix common LLM JSON mistakes in tool-call input before jsonrepair runs
+export function fixToolInputJson(input: string): string {
+ return (
+ input
+ // Inconsistent quote escaping in XML attributes inside JSON strings:
+ // y="-20\" (opening quote unescaped, closing escaped) becomes y=\"-20\".
+ // Must run before the key fix below, which would rewrite the `="`.
+ .replace(/(\w+)="([^"]*?)\\"/g, '$1=\\"$2\\"')
+ // `:=` instead of `: `
+ .replace(/:=/g, ": ")
+ // `"key"= "` instead of `"key": "`, only for JSON keys
+ .replace(/"(\w+)"\s*=\s*"/g, '"$1": "')
+ )
+}
diff --git a/lib/deprecated-params.ts b/lib/deprecated-params.ts
new file mode 100644
index 00000000..2d9d2fda
--- /dev/null
+++ b/lib/deprecated-params.ts
@@ -0,0 +1,94 @@
+import { wrapLanguageModel } from "ai"
+import { rejectionText } from "@/lib/output-token-limit"
+
+type WrappedModel = ReturnType
+
+/**
+ * Claude 4.7 and later answer a non-default temperature, top_p or top_k,
+ * and the extended thinking budget (thinking type "enabled"), with a 400.
+ * TEMPERATURE and the *_THINKING_BUDGET_TOKENS settings send exactly these.
+ */
+const DEPRECATED_PARAM =
+ /`?(?:temperature|top_p|top_k)`? is deprecated for this model|"?thinking\.type\.enabled"? is not supported/i
+
+interface CallParams {
+ temperature?: number
+ topP?: number
+ topK?: number
+ providerOptions?: Record | undefined>
+}
+
+// What these models take instead of a budget. Without display "summarized"
+// they think but send no thinking text to show.
+const ADAPTIVE_THINKING = { type: "adaptive", display: "summarized" }
+
+/** Turn a thinking config of type "enabled" stored under key into adaptive */
+function adaptiveThinking(
+ options: Record | undefined,
+ key: string,
+): Record | undefined {
+ const config = options?.[key] as { type?: string } | undefined
+ if (config?.type !== "enabled") return options
+ return { ...options, [key]: ADAPTIVE_THINKING }
+}
+
+/**
+ * The params without the settings newer Claude models reject, or null when
+ * the error is about something else or there is nothing to change. The
+ * model then runs with its default sampling, and a thinking budget becomes
+ * adaptive thinking.
+ */
+export function withoutDeprecatedParams(
+ error: unknown,
+ params: T,
+): T | null {
+ const text = rejectionText(error)
+ if (!text || !DEPRECATED_PARAM.test(text)) return null
+
+ const { temperature, topP, topK, ...rest } = params
+ const options = params.providerOptions
+ const anthropic = adaptiveThinking(options?.anthropic, "thinking")
+ const bedrock = adaptiveThinking(options?.bedrock, "reasoningConfig")
+ const changed =
+ temperature !== undefined ||
+ topP !== undefined ||
+ topK !== undefined ||
+ anthropic !== options?.anthropic ||
+ bedrock !== options?.bedrock
+ if (!changed) return null
+
+ return {
+ ...rest,
+ ...(options && {
+ providerOptions: {
+ ...options,
+ ...(anthropic && { anthropic }),
+ ...(bedrock && { bedrock }),
+ },
+ }),
+ } as T
+}
+
+/** Retry the stream once without the settings newer Claude models reject. */
+export function withDeprecatedParamsFallback(
+ model: WrappedModel,
+): WrappedModel {
+ return wrapLanguageModel({
+ model,
+ middleware: {
+ specificationVersion: "v3",
+ async wrapStream({ doStream, params, model: inner }) {
+ try {
+ return await doStream()
+ } catch (error) {
+ const retry = withoutDeprecatedParams(error, params)
+ if (!retry) throw error
+ console.warn(
+ "[model params] Rejected sampling or thinking settings, retrying with default sampling and adaptive thinking",
+ )
+ return await inner.doStream(retry)
+ }
+ },
+ },
+ })
+}
diff --git a/lib/dynamo-quota-manager.ts b/lib/dynamo-quota-manager.ts
index 981868b4..f423c8ed 100644
--- a/lib/dynamo-quota-manager.ts
+++ b/lib/dynamo-quota-manager.ts
@@ -64,10 +64,13 @@ interface QuotaCheckResult {
* Check all quotas and increment request count atomically.
* Uses composite key (PK=user, SK=date) for per-day tracking.
* Each day automatically gets a new item - no explicit reset needed.
+ * A request limit of 0 means none; increment 0 checks the limits without
+ * counting a request (the screenshot check).
*/
export async function checkAndIncrementRequest(
ip: string,
limits: QuotaLimits,
+ increment = 1,
): Promise {
// Skip if quota tracking not enabled
if (!client || !TABLE) {
@@ -99,7 +102,7 @@ export async function checkAndIncrementRequest(
attribute_not_exists(tpmCount) OR tpmCount < :tpmLimit)
`,
ExpressionAttributeValues: {
- ":one": { N: "1" },
+ ":one": { N: String(increment) },
":minute": { S: currentMinute },
":reqLimit": { N: String(limits.requests || 999999) },
":tokenLimit": { N: String(limits.tokens || 999999) },
diff --git a/lib/i18n/dictionaries/en.json b/lib/i18n/dictionaries/en.json
index 61cf73fc..fa5ee712 100644
--- a/lib/i18n/dictionaries/en.json
+++ b/lib/i18n/dictionaries/en.json
@@ -178,9 +178,6 @@
"networkError": "Network error. Please check your connection.",
"retryLimit": "Auto-retry limit reached ({max}). Please try again manually.",
"continuationRetryLimit": "Continuation retry limit reached ({max}). The diagram may be too complex.",
- "validationFailed": "Diagram validation failed. Please try regenerating.",
- "malformedXml": "AI generated invalid diagram XML. Please try regenerating.",
- "failedToProcess": "Failed to process diagram. Please try regenerating.",
"sessionCorrupted": "Session data was corrupted. Starting fresh.",
"failedToSave": "Failed to save messages to localStorage",
"failedToRestore": "Failed to restore from localStorage",
@@ -188,7 +185,25 @@
"failedToExport": "Error fetching chart data",
"failedToLoadExample": "Error loading example image",
"failedToRecordFeedback": "Failed to record your feedback. Please try again.",
- "storageUpdateFailed": "Chat cleared but browser storage could not be updated"
+ "storageUpdateFailed": "Chat cleared but browser storage could not be updated",
+ "sessionSaveFailed": "Could not save this chat. Browser storage may be full: delete old chats from history and try again.",
+ "sessionSaveFailedLeave": "Could not save this chat. Browser storage may be full. You can go on without saving it, then delete old chats from the list in the new chat.",
+ "continueWithoutSaving": "Continue without saving",
+ "llm": {
+ "invalid_api_key": "The provider rejected the API key. Check it in model settings.",
+ "forbidden": "The provider refused the request. The key may not have access to this model or region.",
+ "model_not_found": "The provider does not know this model. Check the model ID in model settings.",
+ "insufficient_quota": "The provider account has no credit or quota left.",
+ "rate_limited": "The provider is limiting requests. Wait a moment and try again.",
+ "context_too_long": "The conversation is too long for this model. Start a new chat or pick a model with a larger context.",
+ "images_unsupported": "This model doesn't support image input.",
+ "tools_unsupported": "This model doesn't support tool calls, which drawing needs. Pick another model.",
+ "output_truncated": "The output was cut off before the diagram was complete. Try a simpler request or raise the output limit in settings.",
+ "provider_unavailable": "The provider is having problems. Try again later.",
+ "cannot_connect": "Could not reach the provider. Check the base URL and your network.",
+ "timeout": "The provider did not answer in time.",
+ "openModelSettings": "Open model settings"
+ }
},
"quota": {
"dailyLimit": "Daily Quota Reached",
@@ -235,6 +250,7 @@
"reasoning": {
"thinking": "Thinking...",
"thoughtFor": "Thought for {duration} seconds",
+ "thoughtForOne": "Thought for 1 second",
"thoughtBrief": "Thought for a few seconds"
},
"dev": {
@@ -371,6 +387,11 @@
"enterSecretKey": "Enter your secret access key",
"baseUrl": "Base URL",
"optional": "(optional)",
+ "getApiKey": "Get API key",
+ "fetchModels": "Fetch models from the provider",
+ "noTools": "no tool calls",
+ "mayNotDraw": "models.dev lists no tool call support for this model, so it may not be able to draw.",
+ "requestUrl": "Requests go to {url}",
"baseUrlWithExample": "Base URL (optional, e.g. {example})",
"customEndpoint": "Custom endpoint URL",
"minimaxBaseUrlHint": "Use /anthropic for Anthropic-compatible API (recommended), or /v1 for OpenAI-compatible API",
diff --git a/lib/i18n/dictionaries/ja.json b/lib/i18n/dictionaries/ja.json
index c4393bb6..97c5c754 100644
--- a/lib/i18n/dictionaries/ja.json
+++ b/lib/i18n/dictionaries/ja.json
@@ -178,9 +178,6 @@
"networkError": "ネットワークエラー。接続を確認してください。",
"retryLimit": "自動再試行制限に達しました({max})。手動で再試行してください。",
"continuationRetryLimit": "継続再試行制限に達しました({max})。ダイアグラムが複雑すぎる可能性があります。",
- "validationFailed": "ダイアグラムの検証に失敗しました。再生成してみてください。",
- "malformedXml": "AI が無効なダイアグラム XML を生成しました。再生成してみてください。",
- "failedToProcess": "ダイアグラムの処理に失敗しました。再生成してみてください。",
"sessionCorrupted": "セッションデータが破損しました。最初からやり直します。",
"failedToSave": "localStorage へのメッセージの保存に失敗しました",
"failedToRestore": "localStorage からの復元に失敗しました",
@@ -188,7 +185,25 @@
"failedToExport": "チャートデータの取得エラー",
"failedToLoadExample": "例の画像の読み込みエラー",
"failedToRecordFeedback": "フィードバックの記録に失敗しました。もう一度お試しください。",
- "storageUpdateFailed": "チャットはクリアされましたが、ブラウザストレージを更新できませんでした"
+ "storageUpdateFailed": "チャットはクリアされましたが、ブラウザストレージを更新できませんでした",
+ "sessionSaveFailed": "このチャットを保存できませんでした。ブラウザのストレージがいっぱいの可能性があります。履歴から古いチャットを削除して、もう一度お試しください。",
+ "sessionSaveFailedLeave": "このチャットを保存できませんでした。ブラウザのストレージがいっぱいの可能性があります。保存せずに続けて、新しいチャットの一覧から古いチャットを削除できます。",
+ "continueWithoutSaving": "保存せずに続ける",
+ "llm": {
+ "invalid_api_key": "プロバイダーが API キーを拒否しました。モデル設定で確認してください。",
+ "forbidden": "プロバイダーがリクエストを拒否しました。このキーにはこのモデルまたはリージョンの利用権限がない可能性があります。",
+ "model_not_found": "プロバイダーがこのモデルを認識できません。モデル設定でモデル ID を確認してください。",
+ "insufficient_quota": "プロバイダーのアカウントの残高または利用枠がなくなりました。",
+ "rate_limited": "プロバイダーがリクエスト数を制限しています。少し待ってから再試行してください。",
+ "context_too_long": "会話がこのモデルで扱える長さを超えています。新しいチャットを始めるか、より長いコンテキストに対応したモデルを選んでください。",
+ "images_unsupported": "このモデルは画像入力に対応していません。",
+ "tools_unsupported": "このモデルはツール呼び出しに対応していません。作図にはツール呼び出しが必要です。別のモデルを選んでください。",
+ "output_truncated": "ダイアグラムが完成する前に出力が途中で切れました。リクエストを簡単にするか、設定で出力上限を上げてください。",
+ "provider_unavailable": "プロバイダーで問題が発生しています。しばらくしてから再試行してください。",
+ "cannot_connect": "プロバイダーに接続できません。Base URL とネットワークを確認してください。",
+ "timeout": "プロバイダーから時間内に応答がありませんでした。",
+ "openModelSettings": "モデル設定を開く"
+ }
},
"quota": {
"dailyLimit": "1日の割当量に達しました",
@@ -235,6 +250,7 @@
"reasoning": {
"thinking": "考え中...",
"thoughtFor": "{duration} 秒考えました",
+ "thoughtForOne": "1 秒考えました",
"thoughtBrief": "数秒考えました"
},
"dev": {
@@ -325,6 +341,11 @@
"enterSecretKey": "シークレットアクセスキーを入力",
"baseUrl": "ベース URL",
"optional": "(オプション)",
+ "getApiKey": "API キーを取得",
+ "fetchModels": "プロバイダーからモデル一覧を取得",
+ "noTools": "ツール呼び出し非対応",
+ "mayNotDraw": "models.dev によると、このモデルはツール呼び出しに対応していないため、作図できない可能性があります。",
+ "requestUrl": "リクエスト先: {url}",
"baseUrlWithExample": "ベース URL(オプション、例: {example})",
"customEndpoint": "カスタムエンドポイント URL",
"minimaxBaseUrlHint": "/anthropic で Anthropic 互換 API(推奨)、または /v1 で OpenAI 互換 API を使用",
diff --git a/lib/i18n/dictionaries/zh-Hant.json b/lib/i18n/dictionaries/zh-Hant.json
index 5de362c2..d3521c80 100644
--- a/lib/i18n/dictionaries/zh-Hant.json
+++ b/lib/i18n/dictionaries/zh-Hant.json
@@ -178,9 +178,6 @@
"networkError": "網路錯誤。請檢查您的連線。",
"retryLimit": "已達自動重試限制({max})。請手動重試。",
"continuationRetryLimit": "已達繼續重試限制({max})。圖表可能過於複雜。",
- "validationFailed": "圖表驗證失敗。請嘗試重新產生。",
- "malformedXml": "AI 產生的圖表 XML 無效。請嘗試重新產生。",
- "failedToProcess": "無法處理圖表。請嘗試重新產生。",
"sessionCorrupted": "工作階段資料已損壞。重新開始。",
"failedToSave": "無法儲存訊息到 localStorage",
"failedToRestore": "無法從 localStorage 還原",
@@ -188,7 +185,25 @@
"failedToExport": "取得圖表資料時出錯",
"failedToLoadExample": "載入範例圖片時出錯",
"failedToRecordFeedback": "記錄您的回饋失敗。請重試。",
- "storageUpdateFailed": "聊天已清除,但無法更新瀏覽器儲存空間"
+ "storageUpdateFailed": "聊天已清除,但無法更新瀏覽器儲存空間",
+ "sessionSaveFailed": "無法儲存這個對話。瀏覽器儲存空間可能已滿,請在歷史紀錄裡刪除舊對話後重試。",
+ "sessionSaveFailedLeave": "無法儲存這個對話,瀏覽器儲存空間可能已滿。可以不儲存它、直接繼續,再在新對話的列表裡刪除舊對話。",
+ "continueWithoutSaving": "不儲存,繼續",
+ "llm": {
+ "invalid_api_key": "服務商拒絕了這個 API Key,請在模型設定中檢查。",
+ "forbidden": "服務商拒絕了這次請求。這個 Key 可能沒有使用該模型或該地區的權限。",
+ "model_not_found": "服務商找不到這個模型,請在模型設定中檢查模型 ID。",
+ "insufficient_quota": "服務商帳戶的餘額或額度已經用完。",
+ "rate_limited": "服務商正在限制請求頻率,請稍候再試。",
+ "context_too_long": "對話內容超過了這個模型能處理的長度。請開啟新的對話,或換一個上下文更長的模型。",
+ "images_unsupported": "這個模型不支援圖片輸入。",
+ "tools_unsupported": "這個模型不支援工具呼叫,而繪圖需要工具呼叫。請換一個模型。",
+ "output_truncated": "輸出在圖表完成之前就被截斷了。請簡化請求,或在設定中調高輸出上限。",
+ "provider_unavailable": "服務商發生問題,請稍後再試。",
+ "cannot_connect": "無法連線到服務商,請檢查 Base URL 和網路。",
+ "timeout": "服務商沒有及時回應。",
+ "openModelSettings": "開啟模型設定"
+ }
},
"quota": {
"dailyLimit": "已達每日配額",
@@ -235,6 +250,7 @@
"reasoning": {
"thinking": "思考中...",
"thoughtFor": "思考了 {duration} 秒",
+ "thoughtForOne": "思考了 1 秒",
"thoughtBrief": "思考了幾秒鐘"
},
"dev": {
@@ -371,6 +387,11 @@
"enterSecretKey": "輸入您的 Secret Key",
"baseUrl": "基礎 URL",
"optional": "(可選)",
+ "getApiKey": "取得 API Key",
+ "fetchModels": "從服務商取得模型清單",
+ "noTools": "不支援工具呼叫",
+ "mayNotDraw": "models.dev 顯示這個模型不支援工具呼叫,可能無法繪圖。",
+ "requestUrl": "請求將傳送至 {url}",
"baseUrlWithExample": "基礎 URL(可選,例如 {example})",
"customEndpoint": "自訂端點 URL",
"minimaxBaseUrlHint": "使用 /anthropic 端點為 Anthropic 相容 API(推薦),或使用 /v1 端點為 OpenAI 相容 API",
diff --git a/lib/i18n/dictionaries/zh.json b/lib/i18n/dictionaries/zh.json
index f46601e9..1d8f9adb 100644
--- a/lib/i18n/dictionaries/zh.json
+++ b/lib/i18n/dictionaries/zh.json
@@ -178,9 +178,6 @@
"networkError": "网络错误。请检查您的连接。",
"retryLimit": "已达到自动重试限制({max})。请手动重试。",
"continuationRetryLimit": "已达到继续重试限制({max})。图表可能过于复杂。",
- "validationFailed": "图表验证失败。请尝试重新生成。",
- "malformedXml": "AI 生成的图表 XML 无效。请尝试重新生成。",
- "failedToProcess": "无法处理图表。请尝试重新生成。",
"sessionCorrupted": "会话数据已损坏。重新开始。",
"failedToSave": "无法保存消息到 localStorage",
"failedToRestore": "无法从 localStorage 恢复",
@@ -188,7 +185,25 @@
"failedToExport": "获取图表数据时出错",
"failedToLoadExample": "加载示例图片时出错",
"failedToRecordFeedback": "记录您的反馈失败。请重试。",
- "storageUpdateFailed": "聊天已清除,但无法更新浏览器存储"
+ "storageUpdateFailed": "聊天已清除,但无法更新浏览器存储",
+ "sessionSaveFailed": "无法保存这个对话。浏览器存储空间可能已满,请在历史记录里删除旧对话后重试。",
+ "sessionSaveFailedLeave": "无法保存这个对话,浏览器存储空间可能已满。可以不保存它、直接继续,再在新对话的列表里删除旧对话。",
+ "continueWithoutSaving": "不保存,继续",
+ "llm": {
+ "invalid_api_key": "服务商拒绝了这个 API Key,请在模型设置里检查。",
+ "forbidden": "服务商拒绝了这次请求。这个 Key 可能没有使用该模型或该地区的权限。",
+ "model_not_found": "服务商找不到这个模型,请在模型设置里检查模型 ID。",
+ "insufficient_quota": "服务商账户的余额或额度已经用完。",
+ "rate_limited": "服务商正在限制请求频率,请稍等片刻再试。",
+ "context_too_long": "对话内容超过了这个模型能处理的长度。请新开一个对话,或换一个上下文更长的模型。",
+ "images_unsupported": "这个模型不支持图片输入。",
+ "tools_unsupported": "这个模型不支持工具调用,而画图需要工具调用。请换一个模型。",
+ "output_truncated": "输出在图画完之前就被截断了。请简化请求,或在设置里调高输出上限。",
+ "provider_unavailable": "服务商出了问题,请稍后再试。",
+ "cannot_connect": "连接不上服务商,请检查 Base URL 和网络。",
+ "timeout": "服务商没有及时响应。",
+ "openModelSettings": "打开模型设置"
+ }
},
"quota": {
"dailyLimit": "已达每日配额",
@@ -235,6 +250,7 @@
"reasoning": {
"thinking": "思考中...",
"thoughtFor": "思考了 {duration} 秒",
+ "thoughtForOne": "思考了 1 秒",
"thoughtBrief": "思考了几秒钟"
},
"dev": {
@@ -371,6 +387,11 @@
"enterSecretKey": "输入您的 Secret Key",
"baseUrl": "基础 URL",
"optional": "(可选)",
+ "getApiKey": "获取 API Key",
+ "fetchModels": "从服务商获取模型列表",
+ "noTools": "不支持工具调用",
+ "mayNotDraw": "models.dev 显示这个模型不支持工具调用,可能无法画图。",
+ "requestUrl": "请求将发往 {url}",
"baseUrlWithExample": "基础 URL(可选,例如 {example})",
"customEndpoint": "自定义端点 URL",
"minimaxBaseUrlHint": "使用 /anthropic 端点为 Anthropic 兼容 API(推荐),或使用 /v1 端点为 OpenAI 兼容 API",
diff --git a/lib/langfuse.ts b/lib/langfuse.ts
index 040a296f..23398c47 100644
--- a/lib/langfuse.ts
+++ b/lib/langfuse.ts
@@ -51,8 +51,15 @@ export function setTraceOutput(output: string) {
if (!isLangfuseEnabled()) return
updateActiveTrace({ output })
+ endTrace()
+}
+
+// End the observe() wrapper span (AI SDK creates its own child spans with usage).
+// It uses endOnExit: false, so every request path has to end it, or the trace
+// is never exported: stream finish, stream error/abort, and early returns.
+export function endTrace() {
+ if (!isLangfuseEnabled()) return
- // End the observe() wrapper span (AI SDK creates its own child spans with usage)
const activeSpan = api.trace.getActiveSpan()
if (activeSpan) {
activeSpan.end()
diff --git a/lib/llm-errors.ts b/lib/llm-errors.ts
new file mode 100644
index 00000000..c587276d
--- /dev/null
+++ b/lib/llm-errors.ts
@@ -0,0 +1,187 @@
+import {
+ APICallError,
+ InvalidToolInputError,
+ LoadAPIKeyError,
+ NoSuchToolError,
+ RetryError,
+ ToolCallRepairError,
+} from "ai"
+
+/**
+ * What went wrong with a model call, for a hint the user can act on. The
+ * provider's own message always goes along, because a guess can be wrong.
+ */
+export type LLMErrorCode =
+ | "invalid_api_key"
+ | "forbidden"
+ | "model_not_found"
+ | "insufficient_quota"
+ | "rate_limited"
+ | "context_too_long"
+ | "images_unsupported"
+ | "tools_unsupported"
+ | "output_truncated"
+ | "provider_unavailable"
+ | "cannot_connect"
+ | "timeout"
+ | "unknown"
+
+export interface LLMError {
+ type: "provider"
+ code: LLMErrorCode
+ message: string
+}
+
+// Texts that name the cause more precisely than the status code: a quota
+// error can come as 403 or 429, a context or image error as a plain 400
+const SPECIFIC_TEXTS: Array<[RegExp, LLMErrorCode]> = [
+ [
+ // Not "too many tokens": that is Bedrock's throttling message
+ /context length|context window|maximum context|prompt is too long|input is too long|too many input tokens/i,
+ "context_too_long",
+ ],
+ [
+ /image content block|image_url|does not support image|image input is not supported/i,
+ "images_unsupported",
+ ],
+ [
+ /does not support tools|tool use is not supported|tools? (?:are|is) not supported|function calling is not supported/i,
+ "tools_unsupported",
+ ],
+ // Bedrock, when the output limit cut the tool call's JSON short
+ [/toolUse\.input is invalid/i, "output_truncated"],
+ // Bedrock, for a model id without the inference profile prefix
+ [/on-demand throughput isn.t supported/i, "model_not_found"],
+ [
+ /insufficient[_ ]quota|insufficient balance|exceeded your current quota|credit balance is too low|余额不足/i,
+ "insufficient_quota",
+ ],
+]
+
+const STATUS_CODES: Record = {
+ 401: "invalid_api_key",
+ 402: "insufficient_quota",
+ // Not "invalid key": a valid key can lack access to a model or region
+ 403: "forbidden",
+ 404: "model_not_found",
+ 408: "timeout",
+ // A retired model
+ 410: "model_not_found",
+ 413: "context_too_long",
+ 429: "rate_limited",
+}
+
+const GENERAL_TEXTS: Array<[RegExp, LLMErrorCode]> = [
+ [
+ /model[_ ]not[_ ]found|model .*does not exist|unknown model|no such model/i,
+ "model_not_found",
+ ],
+ [
+ /invalid[_ ]api[_ ]key|incorrect api key|unauthorized/i,
+ "invalid_api_key",
+ ],
+ // "too many tokens": Bedrock's throttling
+ [/rate limit|too many requests|too many tokens/i, "rate_limited"],
+ [
+ /Cannot connect to API|ECONNREFUSED|ENOTFOUND|ECONNRESET|ETIMEDOUT|fetch failed/i,
+ "cannot_connect",
+ ],
+]
+
+/** Secrets a provider may echo back: API keys, Bearer tokens, key=value */
+function redact(text: string): string {
+ return text
+ .replace(/\b(sk|pk|rk|ak)-[A-Za-z0-9_-]{8,}/g, "$1-[redacted]")
+ .replace(/\bBearer\s+[A-Za-z0-9._~+/-]+=*/gi, "Bearer [redacted]")
+ .replace(/\bAKIA[0-9A-Z]{16}\b/g, "[redacted]")
+ .replace(
+ /\b(api[_-]?key|access[_-]?key|secret|token|password|signature)(["']?\s*[:=]\s*["']?)[^\s"',&}]+/gi,
+ "$1$2[redacted]",
+ )
+}
+
+function problemDetail(body: string): string | undefined {
+ try {
+ const detail = JSON.parse(body)?.detail
+ return typeof detail === "string" ? detail : undefined
+ } catch {
+ return undefined
+ }
+}
+
+/**
+ * The error text for the chat stream: what went wrong with the provider as
+ * JSON for the hint, or the text the model must read to fix a tool call.
+ * On the server's keys the provider's own text stays in the server log:
+ * it can name the server's account, role or internal hosts.
+ */
+export function streamErrorText(error: unknown, hideDetails = false): string {
+ // The SDK passes an invalid tool call's error as a plain string. Other
+ // strings come from providers (DeepSeek's SDK sends stream errors so).
+ if (
+ typeof error === "string" &&
+ /^(Invalid input for tool|Model tried to call unavailable tool)/.test(
+ error,
+ )
+ ) {
+ return error
+ }
+ if (isToolCallError(error)) return (error as Error).message
+ const classified = classifyLLMError(error)
+ if (hideDetails) {
+ console.error("[chat] Provider error:", error)
+ classified.message = "The provider returned an error."
+ }
+ return JSON.stringify(classified)
+}
+
+/**
+ * Model and tool errors the SDK sends back to the model as the tool result,
+ * so it can fix its call. Their text has to stay as it is.
+ */
+export function isToolCallError(error: unknown): boolean {
+ return (
+ InvalidToolInputError.isInstance(error) ||
+ NoSuchToolError.isInstance(error) ||
+ ToolCallRepairError.isInstance(error)
+ )
+}
+
+export function classifyLLMError(error: unknown): LLMError {
+ // After the SDK's retries, the last attempt says what happened
+ const e = RetryError.isInstance(error) ? error.lastError : error
+ // Errors sent inside the stream can be plain objects like OpenRouter's
+ // { code: 503, message }
+ const plain = e as {
+ message?: unknown
+ code?: unknown
+ statusCode?: number
+ }
+ const raw =
+ e instanceof Error
+ ? e.message
+ : typeof plain?.message === "string"
+ ? plain.message
+ : String(e)
+ const body = APICallError.isInstance(e) ? (e.responseBody ?? "") : ""
+ // A problem+json body names the reason the SDK left out (NVIDIA: "Gone")
+ const detail = problemDetail(body)
+ const message = redact(detail ? `${raw}: ${detail}` : raw).slice(0, 500)
+ const text = `${raw} ${body}`
+ const status = APICallError.isInstance(e)
+ ? e.statusCode
+ : (plain?.statusCode ??
+ (typeof plain?.code === "number" ? plain.code : undefined))
+
+ const find = (rules: Array<[RegExp, LLMErrorCode]>) =>
+ rules.find(([pattern]) => pattern.test(text))?.[1]
+ const code =
+ (e instanceof Error && e.name === "TimeoutError" && "timeout") ||
+ (LoadAPIKeyError.isInstance(e) && "invalid_api_key") ||
+ find(SPECIFIC_TEXTS) ||
+ (status && STATUS_CODES[status]) ||
+ (status && status >= 500 && "provider_unavailable") ||
+ find(GENERAL_TEXTS) ||
+ "unknown"
+ return { type: "provider", code, message }
+}
diff --git a/lib/model-catalog.json b/lib/model-catalog.json
new file mode 100644
index 00000000..b505c31e
--- /dev/null
+++ b/lib/model-catalog.json
@@ -0,0 +1,1682 @@
+{
+ "openai": {
+ "chatgpt-image-latest": {"tools":false,"images":true,"reasoning":false},
+ "gpt-4.1": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "gpt-4.1-mini": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "gpt-4o": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "gpt-4o-2024-08-06": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "gpt-4o-2024-11-20": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "gpt-4o-mini": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "gpt-5": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":272000},
+ "gpt-5.1": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.2": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.2-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.3-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.3-codex-spark": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":32000},
+ "gpt-5.4": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.4-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.5": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-daybreak-blue-latest": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-daybreak-red-latest": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-image-1-mini": {"tools":false,"images":true,"reasoning":false},
+ "gpt-image-1.5": {"tools":false,"images":true,"reasoning":false},
+ "gpt-realtime-2.1": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":32000},
+ "o3": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "o3-pro": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "text-embedding-3-large": {"tools":false,"images":false,"reasoning":false,"context":8191,"output":3072},
+ "text-embedding-3-small": {"tools":false,"images":false,"reasoning":false,"context":8191,"output":1536},
+ "text-embedding-ada-002": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536}
+ },
+ "anthropic": {
+ "claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-fable-5-1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-haiku-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-haiku-4-5-20251001": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-5-20251101": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-4-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "claude-sonnet-4-5-20250929": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000}
+ },
+ "google": {
+ "deep-research-max-preview-04-2026": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":65536},
+ "deep-research-preview-04-2026": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":65536},
+ "gemini-2.5-computer-use-preview-10-2025": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":64000},
+ "gemini-2.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-2.5-flash-image": {"tools":false,"images":true,"reasoning":true,"context":32768,"output":32768},
+ "gemini-2.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3-flash-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3-pro-image": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":32768},
+ "gemini-3-pro-image-preview": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "gemini-3.1-flash-image": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "gemini-3.1-flash-image-preview": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":65536},
+ "gemini-3.1-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.1-flash-lite-image": {"tools":true,"images":true,"reasoning":true,"context":65536,"output":4096},
+ "gemini-3.1-flash-live-preview": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":65536},
+ "gemini-3.1-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.1-pro-preview-customtools": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.5-live-translate-preview": {"tools":false,"images":false,"reasoning":false,"context":16384,"output":32768},
+ "gemini-3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-embedding-001": {"tools":false,"images":false,"reasoning":false,"context":2048,"output":1},
+ "gemini-embedding-2": {"tools":false,"images":true,"reasoning":false,"context":8192,"output":1},
+ "gemini-flash-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-flash-lite-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemma-4-26b-a4b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "gemma-4-31b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "lyria-3-clip-preview": {"tools":false,"images":true,"reasoning":false,"context":1048576,"output":65536},
+ "lyria-3-pro-preview": {"tools":false,"images":true,"reasoning":false,"context":1048576,"output":65536}
+ },
+ "vertexai": {
+ "claude-fable-5-1@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-fable-5@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-haiku-4-5@20251001": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-5@20251101": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-6@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-7@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-8@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5-5@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-4-5@20250929": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-sonnet-4-6@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-5-5@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-5@default": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "gemini-2.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-2.5-flash-image": {"tools":false,"images":true,"reasoning":false,"context":32768,"output":32768},
+ "gemini-2.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65535},
+ "gemini-2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3-flash-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3-pro-image": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":32768},
+ "gemini-3.1-flash-image": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "gemini-3.1-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.1-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.1-pro-preview-customtools": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-embedding-001": {"tools":false,"images":false,"reasoning":false,"context":2048,"output":1},
+ "gemini-flash-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-flash-lite-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "meta/llama-4-maverick-17b-128e-instruct-maas": {"tools":true,"images":true,"reasoning":false,"context":524288,"output":8192},
+ "openai/gpt-oss-120b-maas": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":131072},
+ "xai/grok-4.20-non-reasoning": {"tools":true,"images":true,"reasoning":false,"context":2000000,"output":30000},
+ "xai/grok-4.20-reasoning": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":30000},
+ "xai/grok-4.3": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":30000},
+ "xai/grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":524288,"output":500000},
+ "zai-org/glm-5.2-maas": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":64000}
+ },
+ "azure": {
+ "claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-fable-5-1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-haiku-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-mythos-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-1": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "claude-opus-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "codestral-2501": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":256000},
+ "cohere-command-a": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "cohere-embed-v-4-0": {"tools":false,"images":true,"reasoning":false,"context":128000,"output":1536},
+ "cohere-embed-v3-english": {"tools":false,"images":false,"reasoning":false,"context":512,"output":1024},
+ "cohere-embed-v3-multilingual": {"tools":false,"images":false,"reasoning":false,"context":512,"output":1024},
+ "deepseek-v3.2": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":128000},
+ "deepseek-v3.2-speciale": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":128000},
+ "deepseek-v4-flash": {"tools":false,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-pro": {"tools":false,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "gpt-5": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1-codex-max": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1-codex-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.2": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.2-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.3-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.4-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.5": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-chat-latest": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-image-1.5": {"tools":false,"images":true,"reasoning":false},
+ "grok-4-1-fast-non-reasoning": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "grok-4-1-fast-reasoning": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":8192},
+ "grok-4-20-non-reasoning": {"tools":true,"images":false,"reasoning":false,"context":262000,"output":8192},
+ "grok-4-20-reasoning": {"tools":true,"images":false,"reasoning":true,"context":262000,"output":8192},
+ "grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":128000},
+ "kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "llama-3.3-70b-instruct": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":32768},
+ "llama-4-maverick-17b-128e-instruct-fp8": {"tools":true,"images":true,"reasoning":false,"context":1000000,"output":16384},
+ "llama-4-scout-17b-16e-instruct": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "ministral-3b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":8192},
+ "mistral-medium-2505": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":128000},
+ "mistral-small-2503": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":32768},
+ "model-router": {"tools":true,"images":true,"reasoning":false,"context":200000,"output":16384},
+ "o3": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "phi-4": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "phi-4-mini": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "phi-4-mini-reasoning": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":4096},
+ "phi-4-multimodal": {"tools":false,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "phi-4-reasoning": {"tools":false,"images":false,"reasoning":true,"context":32000,"output":4096},
+ "phi-4-reasoning-plus": {"tools":false,"images":false,"reasoning":true,"context":32000,"output":4096},
+ "text-embedding-3-large": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":3072},
+ "text-embedding-3-small": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "text-embedding-ada-002": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536}
+ },
+ "bedrock": {
+ "amazon.nova-2-lite-v1:0": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "amazon.nova-lite-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "amazon.nova-micro-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":10000},
+ "amazon.nova-pro-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "anthropic.claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-fable-5-1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic.claude-opus-4-5-20251101-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic.claude-opus-4-6-v1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-sonnet-4-5-20250929-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic.claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic.claude-sonnet-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "apac.amazon.nova-lite-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "apac.amazon.nova-micro-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":10000},
+ "apac.amazon.nova-pro-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "au.anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "au.anthropic.claude-opus-4-6-v1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "au.anthropic.claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "au.anthropic.claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "au.anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "au.anthropic.claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "au.anthropic.claude-sonnet-4-5-20250929-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "au.anthropic.claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "au.anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "ca.amazon.nova-lite-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "deepseek.r1-v1:0": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32768},
+ "deepseek.v3-v1:0": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":81920},
+ "deepseek.v3.2": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":81920},
+ "eu.amazon.nova-2-lite-v1:0": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "eu.amazon.nova-lite-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "eu.amazon.nova-micro-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":10000},
+ "eu.amazon.nova-pro-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "eu.anthropic.claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "eu.anthropic.claude-opus-4-5-20251101-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "eu.anthropic.claude-opus-4-6-v1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-sonnet-4-5-20250929-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "eu.anthropic.claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.anthropic.claude-sonnet-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "eu.mistral.pixtral-large-2502-v1:0": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "global.amazon.nova-2-lite-v1:0": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "global.anthropic.claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-fable-5-1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "global.anthropic.claude-opus-4-5-20251101-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "global.anthropic.claude-opus-4-6-v1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-sonnet-4-5-20250929-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "global.anthropic.claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.anthropic.claude-sonnet-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "global.moonshotai.kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":128000},
+ "global.openai.gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.openai.gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.openai.gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.openai.gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.openai.gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.openai.gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.openai.gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "global.xai.grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "global.xai.grok-4.7": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "google.gemma-3-12b-it": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "google.gemma-3-27b-it": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "google.gemma-3-4b-it": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":4096},
+ "google.gemma-4-26b-a4b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "google.gemma-4-31b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "google.gemma-4-e2b": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":8192},
+ "in.anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "in.anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "in.anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "in.openai.gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "in.openai.gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "jp.amazon.nova-2-lite-v1:0": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "jp.anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "jp.anthropic.claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "jp.anthropic.claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "jp.anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "jp.anthropic.claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "jp.anthropic.claude-sonnet-4-5-20250929-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "jp.anthropic.claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "jp.anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "meta.llama3-1-70b-instruct-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "meta.llama3-1-8b-instruct-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "meta.llama3-3-70b-instruct-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "meta.llama4-maverick-17b-instruct-v1:0": {"tools":true,"images":true,"reasoning":false,"context":1000000,"output":8192},
+ "meta.llama4-scout-17b-instruct-v1:0": {"tools":true,"images":true,"reasoning":false,"context":10000000,"output":8192},
+ "minimax.minimax-m2": {"tools":true,"images":false,"reasoning":true,"context":204608,"output":128000},
+ "minimax.minimax-m2.1": {"tools":true,"images":false,"reasoning":true,"context":196608,"output":131072},
+ "minimax.minimax-m2.5": {"tools":true,"images":false,"reasoning":true,"context":196608,"output":98304},
+ "mistral.devstral-2-123b": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":8192},
+ "mistral.magistral-small-2509": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":40000},
+ "mistral.ministral-3-14b-instruct": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "mistral.ministral-3-3b-instruct": {"tools":true,"images":true,"reasoning":false,"context":256000,"output":8192},
+ "mistral.ministral-3-8b-instruct": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "mistral.mistral-large-3-675b-instruct": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":8192},
+ "mistral.pixtral-large-2502-v1:0": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "mistral.voxtral-mini-3b-2507": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":4096},
+ "mistral.voxtral-small-24b-2507": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":8192},
+ "moonshot.kimi-k2-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":16000},
+ "moonshotai.kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":16384},
+ "nvidia.nemotron-nano-12b-v2": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "nvidia.nemotron-nano-3-30b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":8192},
+ "nvidia.nemotron-nano-9b-v2": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "nvidia.nemotron-super-3-120b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":131072},
+ "openai.gpt-5.4": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "openai.gpt-5.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "openai.gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai.gpt-oss-120b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":131072},
+ "openai.gpt-oss-120b-1:0": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":128000},
+ "openai.gpt-oss-20b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":131072},
+ "openai.gpt-oss-20b-1:0": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":128000},
+ "openai.gpt-oss-safeguard-120b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16384},
+ "openai.gpt-oss-safeguard-20b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16384},
+ "qwen.qwen3-235b-a22b-2507-v1:0": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":131072},
+ "qwen.qwen3-32b-v1:0": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "qwen.qwen3-coder-30b-a3b-v1:0": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":131072},
+ "qwen.qwen3-coder-480b-a35b-v1:0": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":65536},
+ "qwen.qwen3-coder-next": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen.qwen3-next-80b-a3b": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":262000},
+ "qwen.qwen3-vl-235b-a22b": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":262000},
+ "us-gov.openai.gpt-oss-120b-1:0": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16384},
+ "us-gov.openai.gpt-oss-20b-1:0": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16384},
+ "us.amazon.nova-2-lite-v1:0": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "us.amazon.nova-lite-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "us.amazon.nova-micro-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":10000},
+ "us.amazon.nova-pro-v1:0": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "us.anthropic.claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-fable-5-1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-haiku-4-5-20251001-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "us.anthropic.claude-opus-4-5-20251101-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "us.anthropic.claude-opus-4-6-v1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-sonnet-4-5-20250929-v1:0": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "us.anthropic.claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.anthropic.claude-sonnet-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "us.deepseek.r1-v1:0": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32768},
+ "us.meta.llama3-1-70b-instruct-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "us.meta.llama3-1-8b-instruct-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "us.meta.llama3-3-70b-instruct-v1:0": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "us.meta.llama4-maverick-17b-instruct-v1:0": {"tools":true,"images":true,"reasoning":false,"context":1000000,"output":8192},
+ "us.meta.llama4-scout-17b-instruct-v1:0": {"tools":true,"images":true,"reasoning":false,"context":10000000,"output":8192},
+ "us.mistral.pixtral-large-2502-v1:0": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "us.moonshotai.kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":128000},
+ "us.openai.gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.openai.gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.openai.gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.openai.gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.openai.gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.openai.gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.openai.gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "us.writer.palmyra-x4-v1:0": {"tools":true,"images":false,"reasoning":true,"context":122880,"output":8192},
+ "us.writer.palmyra-x5-v1:0": {"tools":true,"images":false,"reasoning":true,"context":1040000,"output":8192},
+ "us.xai.grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "us.xai.grok-4.7": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "writer.palmyra-x4-v1:0": {"tools":true,"images":false,"reasoning":true,"context":122880,"output":8192},
+ "writer.palmyra-x5-v1:0": {"tools":true,"images":false,"reasoning":true,"context":1040000,"output":8192},
+ "xai.grok-4.3": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "xai.grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "zai.glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":131072},
+ "zai.glm-4.7-flash": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":131072},
+ "zai.glm-5": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":131072}
+ },
+ "ollama": {
+ "deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":1048576},
+ "deepseek-v4-flash:0731": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":1048576},
+ "deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":1048576},
+ "deepseek-v4-pro:0813": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":1048576},
+ "deepseek-v4.1-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":384000},
+ "gemma4:31b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":131072},
+ "glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":976000,"output":131072},
+ "glm-5.3": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "glm-5.3-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "gpt-oss:120b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "gpt-oss:20b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "minimax-m2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax-m2.7": {"tools":true,"images":false,"reasoning":true,"context":196608,"output":196608},
+ "minimax-m3": {"tools":true,"images":true,"reasoning":true,"context":512000,"output":131072},
+ "mistral-large-3:675b": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":262144},
+ "nemotron-3-nano:30b": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "nemotron-3-super": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "nemotron-3-ultra": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":128000},
+ "qwen3.5:397b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536}
+ },
+ "openrouter": {
+ "~anthropic/claude-fable-latest": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "~anthropic/claude-haiku-latest": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "~anthropic/claude-opus-latest": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "~anthropic/claude-sonnet-latest": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "~deepseek/deepseek-flash-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "~deepseek/deepseek-pro-latest": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":393216},
+ "~deepseek/deepseek-v4-flash-latest": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":943718},
+ "~google/gemini-flash-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "~google/gemini-pro-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "~moonshotai/kimi-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "~openai/gpt-astra-latest": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "~openai/gpt-luna-latest": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "~openai/gpt-mini-latest": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "~openai/gpt-sol-latest": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "~openai/gpt-terra-latest": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "~x-ai/grok-latest": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":450000},
+ "~z-ai/glm-flash-latest": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "~z-ai/glm-latest": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":943718},
+ "aion-labs/aion-2.0": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "aion-labs/aion-3.0": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "aion-labs/aion-3.0-mini": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "aion-labs/aion-3.5": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "aion-labs/aion-3.5-mini": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "aion-labs/aion-rp-llama-3.1-8b": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":29491},
+ "amazon/nova-2-lite-v1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "amazon/nova-lite-v1": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":5120},
+ "amazon/nova-micro-v1": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":5120},
+ "amazon/nova-premier-v1": {"tools":true,"images":true,"reasoning":false,"context":1000000,"output":32000},
+ "amazon/nova-pro-v1": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":5120},
+ "anthracite-org/magnum-v4-72b": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":4096},
+ "anthropic/claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-fable-5.1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-haiku-4.5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic/claude-opus-4.1": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "anthropic/claude-opus-4.5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic/claude-opus-4.6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-4.7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-4.8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-5.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-sonnet-4": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic/claude-sonnet-4.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "anthropic/claude-sonnet-4.6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-sonnet-5.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "apodex/apodex-1.1-mini:free": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":235929},
+ "arcee-ai/trinity-large-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":80000},
+ "baidu/ernie-4.5-vl-424b-a47b": {"tools":false,"images":true,"reasoning":true,"context":123000,"output":16000},
+ "bytedance-seed/seed-1.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "bytedance-seed/seed-1.6-flash": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "bytedance-seed/seed-2-1-turbo": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "bytedance-seed/seed-2.0-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "bytedance-seed/seed-2.0-lite": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "bytedance-seed/seed-2.0-mini": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "bytedance/ui-tars-1.5-7b": {"tools":false,"images":true,"reasoning":false,"context":128000,"output":2048},
+ "cognitivecomputations/dolphin-mistral-24b-venice-edition": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":8192},
+ "cohere/command-a": {"tools":false,"images":false,"reasoning":false,"context":256000,"output":8192},
+ "cohere/command-a-plus": {"tools":true,"images":true,"reasoning":true,"context":192000,"output":64000},
+ "cohere/command-r-08-2024": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4000},
+ "cohere/command-r-plus-08-2024": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4000},
+ "cohere/command-r7b-12-2024": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":4000},
+ "cohere/north-mini-code:free": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":64000},
+ "deepseek/deepseek-chat": {"tools":true,"images":false,"reasoning":false,"context":163840,"output":16000},
+ "deepseek/deepseek-chat-v3-0324": {"tools":true,"images":false,"reasoning":false,"context":163840,"output":147456},
+ "deepseek/deepseek-chat-v3.1": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":32768},
+ "deepseek/deepseek-r1": {"tools":true,"images":false,"reasoning":true,"context":64000,"output":16000},
+ "deepseek/deepseek-r1-0528": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":32768},
+ "deepseek/deepseek-v3.1-terminus": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":147456},
+ "deepseek/deepseek-v3.2": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":65536},
+ "deepseek/deepseek-v3.2-exp": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":147456},
+ "deepseek/deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":943718},
+ "deepseek/deepseek-v4-flash-0731": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":943718},
+ "deepseek/deepseek-v4-flash-vision-exp": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":262144},
+ "deepseek/deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":384000},
+ "deepseek/deepseek-v4-pro-0813": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":943718},
+ "deepseek/deepseek-v4.1-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "dots-studio/dots-3-note-preview:free": {"tools":true,"images":true,"reasoning":true,"context":512000,"output":460800},
+ "fireworks/ember-1": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "google/gemini-2.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65535},
+ "google/gemini-2.5-flash-image": {"tools":false,"images":true,"reasoning":false,"context":32768,"output":8192},
+ "google/gemini-2.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65535},
+ "google/gemini-2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-2.5-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3-flash-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3-pro-image": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "google/gemini-3-pro-image-preview": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":32768},
+ "google/gemini-3.1-flash-image": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "google/gemini-3.1-flash-image-preview": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":58982},
+ "google/gemini-3.1-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.1-flash-lite-image": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":58982},
+ "google/gemini-3.1-flash-lite-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.1-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.1-pro-preview-customtools": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemma-2-27b-it": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":2048},
+ "google/gemma-3-12b-it": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":16384},
+ "google/gemma-3-27b-it": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":117964},
+ "google/gemma-3-4b-it": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":16384},
+ "google/gemma-4-26b-a4b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "google/gemma-4-26b-a4b-it:free": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "google/gemma-4-31b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":16384},
+ "google/gemma-4-31b-it:free": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "google/lyria-3-clip-preview": {"tools":false,"images":true,"reasoning":false,"context":1048576,"output":65536},
+ "google/lyria-3-pro-preview": {"tools":false,"images":true,"reasoning":false,"context":1048576,"output":65536},
+ "gryphe/mythomax-l2-13b": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":3686},
+ "ibm-granite/granite-4.0-h-micro": {"tools":false,"images":false,"reasoning":false,"context":131000,"output":117900},
+ "ibm-granite/granite-4.2-8b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":117964},
+ "inception/mercury-2": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":50000},
+ "inception/mercury-2.5": {"tools":true,"images":false,"reasoning":true,"context":260000,"output":65536},
+ "inclusionai/ling-3.0-flash": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "inclusionai/ling-3.0-flash-fin": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "inclusionai/ling-3.0-flash-sante:free": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "inclusionai/ling-3.0-flash-vl": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "inclusionai/ling-3.1-flash": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "inference-net/schematron-v2-small": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "inference-net/schematron-v2-turbo": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":8192},
+ "kwaipilot/kat-coder-pro-v2.5": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":235929},
+ "liquid/lfm-2.5-2.6b:free": {"tools":true,"images":false,"reasoning":true,"context":65536,"output":8192},
+ "mancer/weaver": {"tools":false,"images":false,"reasoning":false,"context":8000,"output":6000},
+ "meituan/longcat-2.0": {"tools":true,"images":false,"reasoning":true,"context":1048756,"output":262144},
+ "meta-llama/llama-3.1-70b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "meta-llama/llama-3.1-8b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":117964},
+ "meta-llama/llama-3.2-1b-instruct": {"tools":false,"images":false,"reasoning":false,"context":60000,"output":54000},
+ "meta-llama/llama-3.2-3b-instruct": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":117964},
+ "meta-llama/llama-3.3-70b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "meta-llama/llama-4-maverick": {"tools":true,"images":true,"reasoning":false,"context":1048576,"output":16384},
+ "meta-llama/llama-4-scout": {"tools":true,"images":true,"reasoning":false,"context":1310720,"output":16384},
+ "meta-llama/llama-guard-4-12b": {"tools":false,"images":true,"reasoning":false,"context":163840,"output":16384},
+ "meta/muse-glimmer-30b": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":117964},
+ "meta/muse-spark-1.1": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "meta/muse-spark-1.2": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "meta/muse-spark-1.2-contributor": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "meta/muse-spark-1.3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "meta/muse-spark-1.3-contributor": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "microsoft/phi-4": {"tools":false,"images":false,"reasoning":false,"context":16384,"output":14745},
+ "microsoft/wizardlm-2-8x22b": {"tools":false,"images":false,"reasoning":false,"context":65535,"output":8000},
+ "minimax/minimax-01": {"tools":false,"images":true,"reasoning":false,"context":1000192,"output":40000},
+ "minimax/minimax-m1": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":40000},
+ "minimax/minimax-m2": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":176947},
+ "minimax/minimax-m2-her": {"tools":false,"images":false,"reasoning":false,"context":65536,"output":2048},
+ "minimax/minimax-m2.1": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128000},
+ "minimax/minimax-m2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":176947},
+ "minimax/minimax-m3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":512000},
+ "mistralai/codestral-2508": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":204800},
+ "mistralai/devstral-2512": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":209715},
+ "mistralai/ministral-14b-2512": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":209715},
+ "mistralai/ministral-3b-2512": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":104857},
+ "mistralai/ministral-8b-2512": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":209715},
+ "mistralai/mistral-large": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":102400},
+ "mistralai/mistral-large-2407": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":104857},
+ "mistralai/mistral-large-2512": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":209715},
+ "mistralai/mistral-medium-3": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":104857},
+ "mistralai/mistral-medium-3-5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":209715},
+ "mistralai/mistral-medium-3.1": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":104857},
+ "mistralai/mistral-nemo": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "mistralai/mistral-saba": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":26214},
+ "mistralai/mistral-small-24b-instruct-2501": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":16384},
+ "mistralai/mistral-small-2603": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":209715},
+ "mistralai/mistral-small-3.1-24b-instruct": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":102400},
+ "mistralai/mistral-small-3.2-24b-instruct": {"tools":true,"images":true,"reasoning":false,"context":256000,"output":16384},
+ "mistralai/mixtral-8x22b-instruct": {"tools":true,"images":false,"reasoning":false,"context":65536,"output":52428},
+ "mistralai/voxtral-small-24b-2507": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":26214},
+ "moonshotai/kimi-k2": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":98304},
+ "moonshotai/kimi-k2-0905": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":98304},
+ "moonshotai/kimi-k2-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":235929},
+ "moonshotai/kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "moonshotai/kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "moonshotai/kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "moonshotai/kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943718},
+ "morph/morph-v3-fast": {"tools":false,"images":false,"reasoning":false,"context":81920,"output":38000},
+ "morph/morph-v3-large": {"tools":false,"images":false,"reasoning":false,"context":262144,"output":131072},
+ "nex-agi/nex-n2.5-mini": {"tools":false,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "nex-agi/nex-n2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "nousresearch/hermes-3-llama-3.1-405b": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "nousresearch/hermes-3-llama-3.1-70b": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "nousresearch/hermes-4-405b": {"tools":false,"images":false,"reasoning":true,"context":131072,"output":117964},
+ "nvidia/nemotron-3-nano-30b-a3b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":235929},
+ "nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":65536},
+ "nvidia/nemotron-3-super-120b-a12b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":235929},
+ "nvidia/nemotron-3-super-120b-a12b:free": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":235929},
+ "nvidia/nemotron-3-ultra-550b-a55b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":16384},
+ "nvidia/nemotron-3-ultra-550b-a55b:free": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":65536},
+ "nvidia/nemotron-3.5-content-safety": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":117964},
+ "nvidia/nemotron-3.5-content-safety:free": {"tools":false,"images":true,"reasoning":true,"context":128000,"output":8192},
+ "nvidia/nemotron-3.5-lightning": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":131072},
+ "nvidia/nemotron-3.5-lightning:free": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":65536},
+ "openai/gpt-3.5-turbo": {"tools":true,"images":false,"reasoning":false,"context":16385,"output":4096},
+ "openai/gpt-3.5-turbo-0613": {"tools":true,"images":false,"reasoning":false,"context":4095,"output":3685},
+ "openai/gpt-3.5-turbo-16k": {"tools":true,"images":false,"reasoning":false,"context":16385,"output":4096},
+ "openai/gpt-3.5-turbo-instruct": {"tools":false,"images":false,"reasoning":false,"context":4095,"output":3685},
+ "openai/gpt-4": {"tools":true,"images":false,"reasoning":false,"context":8191,"output":4096},
+ "openai/gpt-4-turbo": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "openai/gpt-4.1": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4.1-mini": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4.1-nano": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4o": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-2024-05-13": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "openai/gpt-4o-2024-08-06": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-2024-11-20": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-mini": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-mini-2024-07-18": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-5": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-image": {"tools":false,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-image-mini": {"tools":false,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-codex-max": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-codex-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2-chat": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":32000},
+ "openai/gpt-5.2-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.3-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.4-image-2": {"tools":false,"images":true,"reasoning":true,"context":272000,"output":128000},
+ "openai/gpt-5.4-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.5": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-luna-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-sol-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-terra-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-astra-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-luna-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-sol-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6.1-sol-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-audio": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-audio-mini": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-chat-latest": {"tools":true,"images":true,"reasoning":false,"context":400000,"output":128000},
+ "openai/gpt-oss-120b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":117964},
+ "openai/gpt-oss-20b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "openai/gpt-oss-safeguard-20b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":65536},
+ "openai/o1": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o1-pro": {"tools":false,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o3": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o3-mini": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":100000},
+ "openai/o3-mini-high": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":100000},
+ "openai/o3-pro": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o4-mini": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o4-mini-high": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openrouter/auto": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "openrouter/bodybuilder": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":128000},
+ "openrouter/free": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":8000},
+ "openrouter/fusion": {"tools":false,"images":false,"reasoning":false,"context":1000000,"output":128000},
+ "openrouter/pareto-code": {"tools":false,"images":false,"reasoning":false,"context":2000000,"output":200000},
+ "perceptron/perceptron-mk1": {"tools":false,"images":true,"reasoning":true,"context":32768,"output":8192},
+ "perceptron/perceptron-mk1.5": {"tools":true,"images":true,"reasoning":true,"context":36864,"output":8192},
+ "perplexity/sonar": {"tools":false,"images":true,"reasoning":false,"context":127072,"output":114364},
+ "perplexity/sonar-deep-research": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":115200},
+ "perplexity/sonar-pro": {"tools":false,"images":true,"reasoning":false,"context":200000,"output":8000},
+ "perplexity/sonar-pro-search": {"tools":false,"images":true,"reasoning":true,"context":200000,"output":8000},
+ "perplexity/sonar-reasoning-pro": {"tools":false,"images":true,"reasoning":true,"context":128000,"output":115200},
+ "poolside/laguna-s-2.1": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "poolside/laguna-s-2.1:free": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "poolside/laguna-xs-2.1": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "poolside/laguna-xs-2.1:free": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "prism-ml/ternary-bonsai-2-27b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "qwen/qwen-2.5-72b-instruct": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":16384},
+ "qwen/qwen-2.5-7b-instruct": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":29491},
+ "qwen/qwen-2.5-coder-32b-instruct": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":29491},
+ "qwen/qwen-plus": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":32768},
+ "qwen/qwen-plus-2025-07-28": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":32768},
+ "qwen/qwen2.5-vl-72b-instruct": {"tools":false,"images":true,"reasoning":false,"context":128000,"output":115200},
+ "qwen/qwen3-14b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "qwen/qwen3-235b-a22b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "qwen/qwen3-235b-a22b-2507": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":235929},
+ "qwen/qwen3-235b-a22b-thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":117964},
+ "qwen/qwen3-30b-a3b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "qwen/qwen3-30b-a3b-instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":32000},
+ "qwen/qwen3-30b-a3b-thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":81920,"output":32768},
+ "qwen/qwen3-32b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "qwen/qwen3-8b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "qwen/qwen3-coder": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen/qwen3-coder-30b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":235929},
+ "qwen/qwen3-coder-flash": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":65536},
+ "qwen/qwen3-coder-next": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":235929},
+ "qwen/qwen3-coder-plus": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":65536},
+ "qwen/qwen3-max": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen/qwen3-max-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3-next-80b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":235929},
+ "qwen/qwen3-next-80b-a3b-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "qwen/qwen3-vl-235b-a22b-instruct": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":32768},
+ "qwen/qwen3-vl-235b-a22b-thinking": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "qwen/qwen3-vl-30b-a3b-instruct": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":16384},
+ "qwen/qwen3-vl-30b-a3b-thinking": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "qwen/qwen3-vl-32b-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":32768},
+ "qwen/qwen3-vl-8b-instruct": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":32768},
+ "qwen/qwen3-vl-8b-thinking": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "qwen/qwen3.5-122b-a10b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3.5-27b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3.5-35b-a3b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "qwen/qwen3.5-397b-a17b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "qwen/qwen3.5-9b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "qwen/qwen3.5-flash-02-23": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen/qwen3.5-plus-02-15": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen/qwen3.5-plus-20260420": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen/qwen3.6-27b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":81920},
+ "qwen/qwen3.6-35b-a3b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "qwen/qwen3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen/qwen3.6-max-preview": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3.6-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen/qwen3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen/qwen3.7-max": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "qwen/qwen3.7-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen/qwen3.8-2.4t-a95b": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "qwen/qwen3.8-27b": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen/qwen3.8-27b:free": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":235929},
+ "qwen/qwen3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen/qwen3.8-max-0902": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen/qwen3.8-max-prime": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen/qwen3.8-omni-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "rekaai/reka-edge": {"tools":true,"images":true,"reasoning":false,"context":16384,"output":14745},
+ "rekaai/reka-flash-3": {"tools":false,"images":false,"reasoning":true,"context":65536,"output":58982},
+ "relace/relace-apply-3": {"tools":false,"images":false,"reasoning":false,"context":256000,"output":128000},
+ "relace/relace-search": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":128000},
+ "sakana/fugu-max": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "sakana/fugu-ultra": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "sakana/fugu-ultra-v2": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "sakana/sakana-namazu": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "sao10k/l3-lunaris-8b": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":7372},
+ "sao10k/l3.1-euryale-70b": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "sao10k/l3.3-euryale-70b": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "stealth/space-bunny-alpha": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":524288},
+ "stepfun/step-3.5-flash": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "stepfun/step-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":230400},
+ "tencent/hunyuan-a13b-instruct": {"tools":false,"images":false,"reasoning":true,"context":131072,"output":117964},
+ "tencent/hy-mt2-1.8b": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":4096},
+ "tencent/hy-mt2-30b-a3b": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":4096},
+ "tencent/hy-mt2-7b": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":4096},
+ "tencent/hy3": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":128000},
+ "tencent/hy3-preview": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":235929},
+ "tencent/hy4-preview": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":64000},
+ "thedrummer/cydonia-24b-v4.1": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":117964},
+ "thedrummer/skyfall-36b-v2": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":29491},
+ "thedrummer/unslopnemo-12b": {"tools":false,"images":false,"reasoning":false,"context":1024000,"output":819200},
+ "thinkingmachines/inkling": {"tools":true,"images":true,"reasoning":true,"context":524288,"output":262144},
+ "thinkingmachines/inkling-small": {"tools":true,"images":true,"reasoning":true,"context":524288,"output":262144},
+ "thinkingmachines/inkling-small:free": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":262144},
+ "thinkingmachines/inkling:free": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":262144},
+ "unbiased/pareto": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":131072},
+ "unbiased/pareto-26.10-preview": {"tools":true,"images":true,"reasoning":false,"context":1048576,"output":131072},
+ "undi95/remm-slerp-l2-13b": {"tools":false,"images":false,"reasoning":false,"context":6144,"output":5529},
+ "upstage/solar-mini4": {"tools":true,"images":false,"reasoning":true,"context":524288,"output":131072},
+ "upstage/solar-pro-3": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":117964},
+ "upstage/solar-pro4": {"tools":true,"images":false,"reasoning":true,"context":524288,"output":131072},
+ "writer/palmyra-x5": {"tools":false,"images":false,"reasoning":false,"context":1040000,"output":8192},
+ "x-ai/grok-4.20": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":1800000},
+ "x-ai/grok-4.20-multi-agent": {"tools":false,"images":true,"reasoning":true,"context":2000000,"output":1800000},
+ "x-ai/grok-4.3": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":900000},
+ "x-ai/grok-4.5": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":450000},
+ "x-ai/grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":450000},
+ "x-ai/grok-4.7": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":450000},
+ "x-ai/grok-build-0.1": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":230400},
+ "xiaomi/mimo-v2.5": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":131072},
+ "xiaomi/mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1050000,"output":131072},
+ "xiaomi/mimo-v2.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":131072},
+ "xiaomi/mimo-v2.6-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":131072},
+ "xiaomi/mimo-v2.6-pro-ultraspeed": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "z-ai/glm-4.5": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "z-ai/glm-4.5-air": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "z-ai/glm-4.5v": {"tools":true,"images":true,"reasoning":true,"context":65536,"output":16384},
+ "z-ai/glm-4.6": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":16384},
+ "z-ai/glm-4.6v": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "z-ai/glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "z-ai/glm-4.7-flash": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":117964},
+ "z-ai/glm-5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128000},
+ "z-ai/glm-5-turbo": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":131072},
+ "z-ai/glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "z-ai/glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":943718},
+ "z-ai/glm-5.3": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "z-ai/glm-5.3-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":943717},
+ "z-ai/glm-5.3-flashx": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "z-ai/glm-5.3-prime": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "z-ai/glm-5v-turbo": {"tools":true,"images":true,"reasoning":true,"context":202752,"output":131072}
+ },
+ "aihubmix": {
+ "alicloud-deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "alicloud-deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "alicloud-glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000},
+ "claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-fable-5-1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-haiku-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-opus-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-6-think": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-7-think": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-4-8": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "claude-opus-4-8-think": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-opus-5-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "claude-sonnet-4-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "claude-sonnet-4-6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "claude-sonnet-4-6-think": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "coding-glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000},
+ "coding-glm-5.1-free": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000},
+ "coding-minimax-m2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128100},
+ "coding-minimax-m2.7-free": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128100},
+ "coding-minimax-m2.7-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128100},
+ "coding-xiaomi-mimo-v2.5": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "coding-xiaomi-mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "deep-deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deep-deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-flash-0731": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-flash-0731-fast": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-flash-vision-exp": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-pro-0813": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4.1-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":384000},
+ "doubao-seed-2-0-code-preview": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "doubao-seed-2-0-lite-260428": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "doubao-seed-2-0-mini-260428": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "doubao-seed-2-0-pro": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "gemini-2.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-2.5-flash-image": {"tools":false,"images":true,"reasoning":true,"context":32768,"output":32768},
+ "gemini-2.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3-flash-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3-pro-image": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":32768},
+ "gemini-3.1-flash-image": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "gemini-3.1-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.1-flash-lite-image": {"tools":true,"images":true,"reasoning":true,"context":65536,"output":4096},
+ "gemini-3.1-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.1-pro-preview-customtools": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "gemini-3.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "glm-4.5v": {"tools":true,"images":true,"reasoning":true,"context":64000,"output":16384},
+ "glm-4.6": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "glm-4.6v": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":32768},
+ "glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":128000},
+ "glm-5.3": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":128000},
+ "glm-5.3-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "glm-5v-turbo": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":128000},
+ "gpt-5": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.1-codex-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.2": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.2-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.3-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.4-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.4-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "gpt-5.5": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "gpt-image-1.5": {"tools":false,"images":true,"reasoning":false},
+ "grok-4.3": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "grok-4.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "grok-4.7": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "grok-build-0.1": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "hy3": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":128000},
+ "hy3-preview": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":128000},
+ "hy4-preview": {"tools":true,"images":false,"reasoning":true,"context":1024000,"output":64000},
+ "kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "kimi-k2.7-code-highspeed": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "longcat-2.0": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "mimo-v2.5": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.6-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.6-pro-ultraspeed": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "minimax-m2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128000},
+ "minimax-m3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":512000},
+ "muse-spark-1.1": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "muse-spark-1.2": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "muse-spark-1.3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "o3": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "o4-mini": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "ox-alpha": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen3-max": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen3-vl-plus": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "qwen3.5-122b-a10b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen3.5-27b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen3.5-35b-a3b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen3.5-397b-a17b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.5-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.6-35b-a3b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":991000,"output":64000},
+ "qwen3.6-max-preview": {"tools":true,"images":false,"reasoning":true,"context":240000,"output":64000},
+ "qwen3.6-plus": {"tools":true,"images":true,"reasoning":true,"context":991000,"output":64000},
+ "qwen3.7-flash": {"tools":true,"images":false,"reasoning":true,"context":991000,"output":64000},
+ "qwen3.7-max": {"tools":true,"images":false,"reasoning":true,"context":991000,"output":64000},
+ "qwen3.7-plus": {"tools":true,"images":false,"reasoning":true,"context":991000,"output":64000},
+ "qwen3.8-2.4t-a95b": {"tools":true,"images":true,"reasoning":true,"context":262000,"output":262000},
+ "qwen3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen3.8-max": {"tools":true,"images":false,"reasoning":true,"context":991000,"output":128000},
+ "qwen3.8-max-preview": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen3.8-omni-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "step-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "step-5-preview": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "xiaomi-mimo-v2.5": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "xiaomi-mimo-v2.5-free": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "xiaomi-mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "xiaomi-mimo-v2.5-pro-free": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "zai-glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000}
+ },
+ "deepseek": {
+ "deepseek-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":393216},
+ "deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":393216}
+ },
+ "siliconflow": {
+ "ByteDance-Seed/Seed-OSS-36B-Instruct": {"tools":true,"images":false,"reasoning":false,"context":262000,"output":262000},
+ "deepseek-ai/DeepSeek-OCR": {"tools":false,"images":true,"reasoning":false,"context":8192,"output":8192},
+ "deepseek-ai/DeepSeek-R1": {"tools":true,"images":false,"reasoning":true,"context":164000,"output":164000},
+ "deepseek-ai/DeepSeek-V3": {"tools":true,"images":false,"reasoning":false,"context":164000,"output":164000},
+ "deepseek-ai/DeepSeek-V3.1-Terminus": {"tools":true,"images":false,"reasoning":true,"context":164000,"output":164000},
+ "deepseek-ai/DeepSeek-V3.2": {"tools":true,"images":false,"reasoning":true,"context":164000,"output":164000},
+ "deepseek-ai/DeepSeek-V4-Flash": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-ai/DeepSeek-V4-Pro": {"tools":true,"images":false,"reasoning":true,"context":1049000,"output":393000},
+ "inclusionAI/Ling-flash-2.0": {"tools":true,"images":false,"reasoning":false,"context":131000,"output":131000},
+ "PaddlePaddle/PaddleOCR-VL-1.5": {"tools":false,"images":true,"reasoning":false,"context":16384,"output":16384},
+ "Pro/deepseek-ai/DeepSeek-R1": {"tools":true,"images":false,"reasoning":true,"context":164000,"output":164000},
+ "Pro/deepseek-ai/DeepSeek-V3": {"tools":true,"images":false,"reasoning":false,"context":164000,"output":164000},
+ "Pro/deepseek-ai/DeepSeek-V3.1-Terminus": {"tools":true,"images":false,"reasoning":true,"context":164000,"output":164000},
+ "Pro/deepseek-ai/DeepSeek-V3.2": {"tools":true,"images":false,"reasoning":true,"context":164000,"output":164000},
+ "Pro/MiniMaxAI/MiniMax-M2.5": {"tools":true,"images":false,"reasoning":false,"context":192000,"output":131000},
+ "Pro/moonshotai/Kimi-K2.5": {"tools":true,"images":true,"reasoning":true,"context":262000,"output":262000},
+ "Pro/moonshotai/Kimi-K2.6": {"tools":true,"images":true,"reasoning":true,"context":262000,"output":262000},
+ "Pro/zai-org/GLM-5": {"tools":true,"images":false,"reasoning":true,"context":205000,"output":205000},
+ "Pro/zai-org/GLM-5.1": {"tools":true,"images":false,"reasoning":true,"context":205000,"output":205000},
+ "Qwen/Qwen2.5-72B-Instruct": {"tools":true,"images":false,"reasoning":false,"context":33000,"output":4000},
+ "Qwen/Qwen2.5-7B-Instruct": {"tools":true,"images":false,"reasoning":false,"context":33000,"output":4000},
+ "Qwen/Qwen3-14B": {"tools":true,"images":false,"reasoning":true,"context":131000,"output":131000},
+ "Qwen/Qwen3-235B-A22B-Thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":262000,"output":262000},
+ "Qwen/Qwen3-30B-A3B-Instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":262000,"output":262000},
+ "Qwen/Qwen3-32B": {"tools":true,"images":false,"reasoning":true,"context":131000,"output":131000},
+ "Qwen/Qwen3-8B": {"tools":true,"images":false,"reasoning":true,"context":131000,"output":131000},
+ "Qwen/Qwen3-Coder-30B-A3B-Instruct": {"tools":true,"images":false,"reasoning":false,"context":262000,"output":262000},
+ "Qwen/Qwen3-Coder-480B-A35B-Instruct": {"tools":true,"images":false,"reasoning":false,"context":262000,"output":262000},
+ "Qwen/Qwen3-VL-30B-A3B-Instruct": {"tools":true,"images":true,"reasoning":false,"context":262000,"output":262000},
+ "Qwen/Qwen3-VL-30B-A3B-Thinking": {"tools":true,"images":true,"reasoning":true,"context":262000,"output":262000},
+ "Qwen/Qwen3-VL-32B-Instruct": {"tools":true,"images":true,"reasoning":false,"context":262000,"output":262000},
+ "Qwen/Qwen3-VL-32B-Thinking": {"tools":true,"images":true,"reasoning":true,"context":262000,"output":262000},
+ "Qwen/Qwen3-VL-8B-Instruct": {"tools":true,"images":true,"reasoning":false,"context":262000,"output":262000},
+ "Qwen/Qwen3.5-122B-A10B": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "Qwen/Qwen3.5-27B": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "Qwen/Qwen3.5-35B-A3B": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "Qwen/Qwen3.5-397B-A17B": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "Qwen/Qwen3.5-4B": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "Qwen/Qwen3.5-9B": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "Qwen/Qwen3.6-35B-A3B": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":65536},
+ "stepfun-ai/Step-3.5-Flash": {"tools":true,"images":false,"reasoning":true,"context":262000,"output":262000},
+ "tencent/Hunyuan-A13B-Instruct": {"tools":true,"images":false,"reasoning":true,"context":131000,"output":131000},
+ "zai-org/GLM-4.5-Air": {"tools":true,"images":false,"reasoning":false,"context":131000,"output":131000},
+ "zai-org/GLM-5.2": {"tools":true,"images":false,"reasoning":true,"context":1049000,"output":262000}
+ },
+ "gateway": {
+ "alibaba/qwen-3-14b": {"tools":true,"images":false,"reasoning":true,"context":40960,"output":16384},
+ "alibaba/qwen-3-235b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":16384},
+ "alibaba/qwen-3-30b": {"tools":true,"images":false,"reasoning":true,"context":40960,"output":16384},
+ "alibaba/qwen-3-32b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":8192},
+ "alibaba/qwen-3.6-max-preview": {"tools":true,"images":false,"reasoning":true,"context":240000,"output":64000},
+ "alibaba/qwen3-235b-a22b-thinking": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "alibaba/qwen3-coder": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "alibaba/qwen3-coder-30b-a3b": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":8192},
+ "alibaba/qwen3-coder-next": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":256000},
+ "alibaba/qwen3-coder-plus": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":65536},
+ "alibaba/qwen3-embedding-0.6b": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":32768},
+ "alibaba/qwen3-embedding-4b": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":32768},
+ "alibaba/qwen3-embedding-8b": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":32768},
+ "alibaba/qwen3-max": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":32768},
+ "alibaba/qwen3-max-preview": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":32768},
+ "alibaba/qwen3-max-thinking": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":65536},
+ "alibaba/qwen3-next-80b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262114,"output":262114},
+ "alibaba/qwen3-next-80b-a3b-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":262144},
+ "alibaba/qwen3-vl-235b-a22b-instruct": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":129024},
+ "alibaba/qwen3-vl-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":129024},
+ "alibaba/qwen3-vl-thinking": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "alibaba/qwen3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "alibaba/qwen3.5-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "alibaba/qwen3.6-27b": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":65536},
+ "alibaba/qwen3.6-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "alibaba/qwen3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":991000,"output":64000},
+ "alibaba/qwen3.7-max": {"tools":true,"images":false,"reasoning":true,"context":991000,"output":64000},
+ "alibaba/qwen3.7-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "alibaba/qwen3.8-2.4t-a95b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":128000},
+ "alibaba/qwen3.8-27b": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "alibaba/qwen3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":991000,"output":128000},
+ "alibaba/qwen3.8-max": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":128000},
+ "alibaba/qwen3.8-max-0902": {"tools":true,"images":true,"reasoning":true,"context":991000,"output":128000},
+ "alibaba/qwen3.8-max-prime": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "alibaba/qwen3.8-omni-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "amazon/nova-2-lite": {"tools":false,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "amazon/nova-lite": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "amazon/nova-micro": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":10000},
+ "amazon/nova-pro": {"tools":true,"images":true,"reasoning":false,"context":300000,"output":10000},
+ "amazon/titan-embed-text-v2": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "anthropic/claude-3-haiku": {"tools":true,"images":true,"reasoning":false,"context":200000,"output":4096},
+ "anthropic/claude-fable-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-fable-5.1": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-haiku-4.5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic/claude-opus-4": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "anthropic/claude-opus-4.5": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "anthropic/claude-opus-4.6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-4.7": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-4.8": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-4.8-fast": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-5-fast": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-5.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-opus-5.5-fast": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-sonnet-4": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "anthropic/claude-sonnet-4.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "anthropic/claude-sonnet-4.6": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-sonnet-5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "anthropic/claude-sonnet-5.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "arcee-ai/trinity-large-thinking": {"tools":true,"images":false,"reasoning":true,"context":262100,"output":80000},
+ "bytedance/seed-1.6": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "bytedance/seed-1.8": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32768},
+ "bytedance/seed-2.1-turbo": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "cohere/command-a": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":8000},
+ "cohere/embed-v4.0": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":1536},
+ "cohere/rerank-v3.5": {"tools":false,"images":false,"reasoning":false,"context":4096,"output":4096},
+ "cohere/rerank-v4-fast": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "cohere/rerank-v4-pro": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "deepseek/deepseek-r1": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32768},
+ "deepseek/deepseek-v3.1": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":128000},
+ "deepseek/deepseek-v3.1-terminus": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "deepseek/deepseek-v3.2": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":8000},
+ "deepseek/deepseek-v3.2-thinking": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":8000},
+ "deepseek/deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek/deepseek-v4-flash-0731": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek/deepseek-v4-flash-vision-exp": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "deepseek/deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek/deepseek-v4-pro-0813": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek/deepseek-v4.1-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":32768},
+ "deepseek/deepseek-v4.1-flash-fast": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1000000},
+ "fireworks/ember-1": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "fish-audio/transcribe-1": {"tools":false,"images":false,"reasoning":false},
+ "google/gemini-2.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-2.5-flash-image": {"tools":false,"images":true,"reasoning":false,"context":32768,"output":65535},
+ "google/gemini-2.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65535},
+ "google/gemini-2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "google/gemini-3-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65000},
+ "google/gemini-3-pro-image": {"tools":false,"images":true,"reasoning":false,"context":65536,"output":32768},
+ "google/gemini-3.1-flash-image": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "google/gemini-3.1-flash-image-preview": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "google/gemini-3.1-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65000},
+ "google/gemini-3.1-flash-lite-image": {"tools":false,"images":true,"reasoning":true,"context":65536,"output":4096},
+ "google/gemini-3.1-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "google/gemini-3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "google/gemini-3.5-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65000},
+ "google/gemini-3.5-transcribe": {"tools":false,"images":false,"reasoning":false},
+ "google/gemini-3.5-transcribe-live": {"tools":false,"images":false,"reasoning":false},
+ "google/gemini-3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "google/gemini-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "google/gemini-3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65535},
+ "google/gemini-3.8-live": {"tools":false,"images":false,"reasoning":false},
+ "google/gemini-3.8-live-extended-thinking": {"tools":false,"images":false,"reasoning":false},
+ "google/gemini-embedding-001": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "google/gemini-embedding-2": {"tools":false,"images":false,"reasoning":false},
+ "google/gemini-omni-flash-preview": {"tools":false,"images":true,"reasoning":true,"context":1000000,"output":57920},
+ "google/gemma-4-26b-a4b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "google/gemma-4-31b-it": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":131072},
+ "google/text-embedding-005": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "google/text-multilingual-embedding-002": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "inception/mercury-2": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":50000},
+ "inception/mercury-2.5": {"tools":true,"images":false,"reasoning":true,"context":260000,"output":65536},
+ "inception/mercury-coder-small": {"tools":true,"images":false,"reasoning":false,"context":32000,"output":16384},
+ "inclusionai/ling-3.0-flash": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":32000},
+ "inclusionai/ling-3.0-flash-fin": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":32000},
+ "inclusionai/ling-3.0-flash-sante": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":32000},
+ "inclusionai/ling-3.0-flash-sante-free": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":32000},
+ "inclusionai/ling-3.0-flash-vl": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "inclusionai/ling-3.1-flash": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "inclusionai/ling-3.1-flash-free": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "inference-net/schematron-v2-small": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "inference-net/schematron-v2-turbo": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":8192},
+ "interfaze/interfaze-beta": {"tools":false,"images":true,"reasoning":true,"context":1000000,"output":32000},
+ "meituan/longcat-2.5-preview": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "meta/llama-3.1-70b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":8192},
+ "meta/llama-3.1-8b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":8192},
+ "meta/llama-3.3-70b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "meta/llama-4-maverick": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "meta/llama-4-scout": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "meta/muse-glimmer-30b": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":131072},
+ "meta/muse-spark-1.1": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "meta/muse-spark-1.2": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "meta/muse-spark-1.2-contributor": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "meta/muse-spark-1.3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "meta/muse-spark-1.3-contributor": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "microsoft/mai-transcribe-1.5": {"tools":false,"images":false,"reasoning":false},
+ "microsoft/mai-transcribe-2": {"tools":false,"images":false,"reasoning":false},
+ "microsoft/mai-transcribe-2-streaming": {"tools":false,"images":false,"reasoning":false},
+ "minimax/minimax-m2": {"tools":true,"images":false,"reasoning":true,"context":205000,"output":196608},
+ "minimax/minimax-m2.1": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.1-lightning": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131000},
+ "minimax/minimax-m2.5-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131000},
+ "minimax/minimax-m2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131000},
+ "minimax/minimax-m2.7-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131100},
+ "minimax/minimax-m3": {"tools":true,"images":true,"reasoning":true,"context":512000,"output":512000},
+ "mistral/codestral": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":4096},
+ "mistral/codestral-embed": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "mistral/ministral-14b": {"tools":false,"images":true,"reasoning":false,"context":262144,"output":256000},
+ "mistral/ministral-3b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":128000},
+ "mistral/ministral-8b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":128000},
+ "mistral/mistral-embed": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "mistral/mistral-large-3": {"tools":false,"images":true,"reasoning":false,"context":262144,"output":256000},
+ "mistral/mistral-medium-3.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":256000},
+ "mistral/mistral-nemo": {"tools":true,"images":false,"reasoning":false,"context":60288,"output":16000},
+ "mistral/mistral-small": {"tools":true,"images":true,"reasoning":false,"context":262144,"output":4000},
+ "mixedbread/toast-1": {"tools":true,"images":false,"reasoning":false,"context":131000,"output":4000},
+ "moonshotai/kimi-k2": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":131072},
+ "moonshotai/kimi-k2-thinking": {"tools":true,"images":false,"reasoning":true,"context":216144,"output":216144},
+ "moonshotai/kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "moonshotai/kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262000,"output":262000},
+ "moonshotai/kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32768},
+ "moonshotai/kimi-k2.7-code-highspeed": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "moonshotai/kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "moonshotai/kimi-k3-fast": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "morph/morph-v3-fast": {"tools":false,"images":false,"reasoning":false,"context":16000,"output":16000},
+ "morph/morph-v3-large": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "nvidia/nemotron-3-nano-30b-a3b": {"tools":false,"images":false,"reasoning":true,"context":262144,"output":262144},
+ "nvidia/nemotron-3-super-120b-a12b": {"tools":false,"images":false,"reasoning":true,"context":256000,"output":32000},
+ "nvidia/nemotron-3-ultra-550b-a55b": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":65000},
+ "nvidia/nemotron-3.5-lightning": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":131072},
+ "nvidia/nemotron-nano-12b-v2-vl": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":131072},
+ "nvidia/nemotron-nano-9b-v2": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":131072},
+ "openai/gpt-3.5-turbo": {"tools":false,"images":false,"reasoning":false,"context":16385,"output":4096},
+ "openai/gpt-4.1": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4.1-fast": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4.1-mini": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4.1-mini-fast": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4.1-nano-fast": {"tools":true,"images":true,"reasoning":false,"context":1047576,"output":32768},
+ "openai/gpt-4o": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-fast": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-mini": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-mini-fast": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":16384},
+ "openai/gpt-4o-mini-transcribe": {"tools":false,"images":false,"reasoning":false},
+ "openai/gpt-4o-transcribe": {"tools":false,"images":false,"reasoning":false},
+ "openai/gpt-5": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-fast": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-mini-fast": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":272000},
+ "openai/gpt-5.1-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-codex-max": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-codex-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-thinking": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.1-thinking-fast": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2-fast": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.2-pro": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.3-codex": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.3-codex-fast": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.4-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.4-mini": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4-mini-fast": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4-nano": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "openai/gpt-5.4-pro": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.5": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "openai/gpt-5.5-fast": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "openai/gpt-5.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":128000},
+ "openai/gpt-5.6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-luna-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-sol-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-terra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-5.6-terra-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-astra": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-astra-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-luna": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-luna-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6-sol-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6.1-sol": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-6.1-sol-fast": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":128000},
+ "openai/gpt-live-1": {"tools":false,"images":false,"reasoning":false},
+ "openai/gpt-oss-120b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":131072},
+ "openai/gpt-oss-20b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "openai/gpt-oss-safeguard-120b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16000},
+ "openai/gpt-oss-safeguard-20b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16000},
+ "openai/gpt-realtime-1.5": {"tools":false,"images":false,"reasoning":false},
+ "openai/gpt-realtime-2": {"tools":false,"images":false,"reasoning":false},
+ "openai/gpt-realtime-2.1": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "openai/gpt-realtime-mini": {"tools":false,"images":false,"reasoning":false},
+ "openai/gpt-realtime-whisper": {"tools":false,"images":false,"reasoning":false},
+ "openai/o3": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o3-fast": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o3-pro": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/o4-mini-fast": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":100000},
+ "openai/text-embedding-3-large": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "openai/text-embedding-3-small": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "openai/text-embedding-ada-002": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "openai/whisper-1": {"tools":false,"images":false,"reasoning":false},
+ "perplexity/pplx-embed-v1-0.6b": {"tools":false,"images":false,"reasoning":false,"context":32000},
+ "perplexity/pplx-embed-v1-4b": {"tools":false,"images":false,"reasoning":false,"context":32000},
+ "perplexity/sonar": {"tools":true,"images":true,"reasoning":false,"context":127000,"output":8000},
+ "poolside/laguna-s-2.1": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "poolside/laguna-s-2.1-free": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":32768},
+ "quiverai/arrow-2": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":65536},
+ "quiverai/arrow-2-telos": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":65536},
+ "sakana/fugu-max": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "sakana/fugu-ultra": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "sakana/fugu-ultra-v2": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "sakana/namazu": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "spacexai/grok-4.1-fast-non-reasoning": {"tools":true,"images":true,"reasoning":false,"context":1000000,"output":1000000},
+ "spacexai/grok-4.1-fast-reasoning": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "spacexai/grok-4.20-multi-agent": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "spacexai/grok-4.20-multi-agent-beta": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "spacexai/grok-4.20-non-reasoning": {"tools":true,"images":true,"reasoning":false,"context":2000000,"output":2000000},
+ "spacexai/grok-4.20-non-reasoning-beta": {"tools":true,"images":true,"reasoning":false,"context":2000000,"output":2000000},
+ "spacexai/grok-4.20-reasoning": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "spacexai/grok-4.20-reasoning-beta": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "spacexai/grok-4.3": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "spacexai/grok-4.5": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "spacexai/grok-4.6": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "spacexai/grok-4.7": {"tools":true,"images":true,"reasoning":true,"context":500000,"output":500000},
+ "spacexai/grok-build-0.1": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "spacexai/grok-stt": {"tools":false,"images":false,"reasoning":false},
+ "spacexai/grok-voice-think-fast-1.0": {"tools":false,"images":false,"reasoning":false},
+ "spacexai/grok-voice-think-fast-2.0": {"tools":false,"images":false,"reasoning":false},
+ "stepfun/step-3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":262114,"output":262114},
+ "stepfun/step-3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "stepfun/step-5-preview": {"tools":false,"images":true,"reasoning":false,"context":1000000,"output":1000000},
+ "tencent/hy-mt2-lite": {"tools":false,"images":false,"reasoning":false,"context":8000,"output":4000},
+ "tencent/hy-mt2-plus": {"tools":false,"images":false,"reasoning":false,"context":8000,"output":4000},
+ "tencent/hy-mt2-pro": {"tools":false,"images":false,"reasoning":false,"context":8000,"output":4000},
+ "tencent/hy3": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":262144},
+ "tencent/hy4-preview": {"tools":true,"images":false,"reasoning":true,"context":1024000,"output":64000},
+ "thinkingmachines/inkling": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "thinkingmachines/inkling-small": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":1000000},
+ "voyage/rerank-2.5": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "voyage/rerank-2.5-lite": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "voyage/rerank-3": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "voyage/rerank-3-lite": {"tools":false,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "voyage/voyage-3-large": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "voyage/voyage-3.5": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "voyage/voyage-3.5-lite": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "voyage/voyage-4": {"tools":false,"images":false,"reasoning":false,"context":32000},
+ "voyage/voyage-4-large": {"tools":false,"images":false,"reasoning":false,"context":32000},
+ "voyage/voyage-4-lite": {"tools":false,"images":false,"reasoning":false,"context":32000},
+ "voyage/voyage-code-2": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "voyage/voyage-code-3": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "voyage/voyage-finance-2": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "voyage/voyage-law-2": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1536},
+ "xiaomi/mimo-v2.5": {"tools":true,"images":true,"reasoning":true,"context":1050000,"output":131100},
+ "xiaomi/mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1050000,"output":131000},
+ "xiaomi/mimo-v2.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "xiaomi/mimo-v2.6-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "xiaomi/mimo-v2.6-pro-ultraspeed": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "zai/glm-4.5": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":96000},
+ "zai/glm-4.5-air": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":96000},
+ "zai/glm-4.5v": {"tools":true,"images":true,"reasoning":true,"context":66000,"output":16000},
+ "zai/glm-4.6": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":96000},
+ "zai/glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":120000},
+ "zai/glm-4.7-flash": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":131000},
+ "zai/glm-4.7-flashx": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000},
+ "zai/glm-5": {"tools":true,"images":false,"reasoning":true,"context":202800,"output":131100},
+ "zai/glm-5-turbo": {"tools":true,"images":false,"reasoning":true,"context":202800,"output":131072},
+ "zai/glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":202800,"output":64000},
+ "zai/glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":128000},
+ "zai/glm-5.2-fast": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":128000},
+ "zai/glm-5.3": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":1000000},
+ "zai/glm-5.3-fast": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":262144},
+ "zai/glm-5.3-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131000},
+ "zai/glm-5.3-flashx": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "zai/glm-5v-turbo": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":128000}
+ },
+ "doubao": {
+ "deepseek-v4-flash-ga-260731": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-pro-ga-260813": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "doubao-seed-1-6-251015": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":64000},
+ "doubao-seed-1-6-flash-250828": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-1-6-vision-250815": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-1-8-251228": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":64000},
+ "doubao-seed-2-0-code-preview-260215": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "doubao-seed-2-0-lite-260428": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":131072},
+ "doubao-seed-2-0-mini-260428": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":131072},
+ "doubao-seed-2-0-pro-260215": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "doubao-seed-2-1-pro-260628": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "doubao-seed-2-1-turbo-260628": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "doubao-seed-character-260628": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "doubao-seed-evolving": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":256000},
+ "glm-5-2-260617": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "glm-5-3-flash-260828": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072}
+ },
+ "modelscope": {
+ "Qwen/Qwen3-235B-A22B-Instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":131072},
+ "Qwen/Qwen3-235B-A22B-Thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":131072},
+ "Qwen/Qwen3-30B-A3B-Instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":16384},
+ "Qwen/Qwen3-30B-A3B-Thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32768},
+ "Qwen/Qwen3-Coder-30B-A3B-Instruct": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "ZhipuAI/GLM-4.5": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "ZhipuAI/GLM-4.6": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":98304}
+ },
+ "glm": {
+ "glm-4.5": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "glm-4.5-air": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "glm-4.5-flash": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "glm-4.5v": {"tools":true,"images":true,"reasoning":true,"context":64000,"output":16384},
+ "glm-4.6": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "glm-4.6v": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":32768},
+ "glm-4.6v-flash": {"tools":true,"images":true,"reasoning":true,"context":128000,"output":32768},
+ "glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "glm-4.7-flash": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":131072},
+ "glm-4.7-flashx": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":131072},
+ "glm-5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":131072},
+ "glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "glm-5.3": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "glm-5.3-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "glm-5.3-flashx": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "glm-5v-turbo": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":131072}
+ },
+ "qwen": {
+ "deepseek-r1": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "deepseek-r1-0528": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "deepseek-r1-distill-llama-70b": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "deepseek-r1-distill-llama-8b": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "deepseek-r1-distill-qwen-1-5b": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "deepseek-r1-distill-qwen-14b": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "deepseek-r1-distill-qwen-32b": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "deepseek-r1-distill-qwen-7b": {"tools":true,"images":false,"reasoning":true,"context":32768,"output":16384},
+ "deepseek-v3": {"tools":true,"images":false,"reasoning":false,"context":65536,"output":8192},
+ "deepseek-v3-1": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":65536},
+ "deepseek-v3-2-exp": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":65536},
+ "deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":384000},
+ "deepseek-v4.1-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":384000},
+ "glm-5": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":16384},
+ "glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":202752,"output":128000},
+ "glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":128000},
+ "glm-5.3": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":131072},
+ "kimi-k2-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":16384},
+ "kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":16384},
+ "kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "kimi/kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "MiniMax-M2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax/MiniMax-M2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "moonshot-kimi-k2-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qvq-max": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":8192},
+ "qwen-deep-research": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":32768},
+ "qwen-doc-turbo": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen-flash": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":32768},
+ "qwen-long": {"tools":true,"images":false,"reasoning":false,"context":10000000,"output":8192},
+ "qwen-math-plus": {"tools":true,"images":false,"reasoning":false,"context":4096,"output":3072},
+ "qwen-math-turbo": {"tools":true,"images":false,"reasoning":false,"context":4096,"output":3072},
+ "qwen-max": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen-mt-plus": {"tools":false,"images":false,"reasoning":false,"context":16384,"output":8192},
+ "qwen-mt-turbo": {"tools":false,"images":false,"reasoning":false,"context":16384,"output":8192},
+ "qwen-omni-turbo": {"tools":true,"images":true,"reasoning":false,"context":32768,"output":2048},
+ "qwen-omni-turbo-realtime": {"tools":true,"images":true,"reasoning":false,"context":32768,"output":2048},
+ "qwen-plus": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":32768},
+ "qwen-plus-character": {"tools":true,"images":false,"reasoning":false,"context":32768,"output":4096},
+ "qwen-turbo": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":16384},
+ "qwen-vl-max": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "qwen-vl-ocr": {"tools":false,"images":true,"reasoning":false,"context":34096,"output":4096},
+ "qwen-vl-plus": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-14b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-32b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-72b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-7b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-coder-32b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-coder-7b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-math-72b-instruct": {"tools":true,"images":false,"reasoning":false,"context":4096,"output":3072},
+ "qwen2-5-math-7b-instruct": {"tools":true,"images":false,"reasoning":false,"context":4096,"output":3072},
+ "qwen2-5-omni-7b": {"tools":true,"images":true,"reasoning":false,"context":32768,"output":2048},
+ "qwen2-5-vl-72b-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "qwen2-5-vl-7b-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "qwen3-14b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "qwen3-235b-a22b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "qwen3-32b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":16384},
+ "qwen3-8b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "qwen3-asr-flash": {"tools":false,"images":false,"reasoning":false,"context":53248,"output":4096},
+ "qwen3-coder-30b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen3-coder-480b-a35b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen3-coder-flash": {"tools":true,"images":false,"reasoning":false,"context":1000000,"output":65536},
+ "qwen3-coder-plus": {"tools":true,"images":false,"reasoning":false,"context":1048576,"output":65536},
+ "qwen3-max": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "qwen3-next-80b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":32768},
+ "qwen3-next-80b-a3b-thinking": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "qwen3-omni-flash": {"tools":true,"images":true,"reasoning":true,"context":65536,"output":16384},
+ "qwen3-omni-flash-realtime": {"tools":true,"images":true,"reasoning":false,"context":65536,"output":16384},
+ "qwen3-vl-235b-a22b": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "qwen3-vl-30b-a3b": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "qwen3-vl-plus": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":32768},
+ "qwen3.5-397b-a17b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen3.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.5-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.6-max-preview": {"tools":true,"images":false,"reasoning":true,"context":245800,"output":65536},
+ "qwen3.6-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.7-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen3.7-max": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":65536},
+ "qwen3.7-plus": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "qwen3.8-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen3.8-max": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwen3.8-omni-flash": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":131072},
+ "qwq-32b": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "qwq-plus": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":8192},
+ "siliconflow/deepseek-r1-0528": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":32768},
+ "siliconflow/deepseek-v3-0324": {"tools":true,"images":false,"reasoning":false,"context":163840,"output":163840},
+ "siliconflow/deepseek-v3.1-terminus": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":65536},
+ "siliconflow/deepseek-v3.2": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":65536},
+ "tongyi-intent-detect-v3": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":1024}
+ },
+ "qiniu": {
+ "claude-3.5-haiku": {"tools":true,"images":true,"reasoning":false,"context":200000,"output":8192},
+ "claude-3.5-sonnet": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":8200},
+ "claude-3.7-sonnet": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":128000},
+ "claude-4.0-opus": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "claude-4.0-sonnet": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-4.1-opus": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":32000},
+ "claude-4.5-haiku": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "claude-4.5-opus": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":200000},
+ "claude-4.5-sonnet": {"tools":true,"images":true,"reasoning":true,"context":200000,"output":64000},
+ "deepseek-r1": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "deepseek-r1-0528": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "deepseek-v3": {"tools":false,"images":false,"reasoning":false,"context":128000,"output":16000},
+ "deepseek-v3-0324": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":16000},
+ "deepseek-v3.1": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "deepseek/deepseek-math-v2": {"tools":false,"images":false,"reasoning":true,"context":160000,"output":160000},
+ "deepseek/deepseek-v3.1-terminus": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":32000},
+ "deepseek/deepseek-v3.1-terminus-thinking": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "deepseek/deepseek-v3.2-251201": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "deepseek/deepseek-v3.2-exp": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":32000},
+ "deepseek/deepseek-v3.2-exp-thinking": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "doubao-1.5-pro-32k": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":12000},
+ "doubao-1.5-thinking-pro": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":16000},
+ "doubao-1.5-vision-pro": {"tools":false,"images":true,"reasoning":false,"context":128000,"output":16000},
+ "doubao-seed-1.6": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-1.6-flash": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-1.6-thinking": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-2.0-code": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "doubao-seed-2.0-lite": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-2.0-mini": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":32000},
+ "doubao-seed-2.0-pro": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":128000},
+ "gemini-2.0-flash": {"tools":true,"images":true,"reasoning":false,"context":1048576,"output":8192},
+ "gemini-2.0-flash-lite": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":8192},
+ "gemini-2.5-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":64000},
+ "gemini-2.5-flash-lite": {"tools":true,"images":true,"reasoning":false,"context":1048576,"output":64000},
+ "gemini-2.5-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":65536},
+ "gemini-3.0-flash-preview": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "gemini-3.0-pro-image-preview": {"tools":false,"images":true,"reasoning":false,"context":32768,"output":8192},
+ "gemini-3.0-pro-preview": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":64000},
+ "glm-4.5": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "glm-4.5-air": {"tools":true,"images":false,"reasoning":true,"context":131000,"output":4096},
+ "gpt-oss-120b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":4096},
+ "gpt-oss-20b": {"tools":true,"images":false,"reasoning":true,"context":128000,"output":4096},
+ "kimi-k2": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":128000},
+ "meituan/longcat-flash-chat": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":131072},
+ "meituan/longcat-flash-lite": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":320000},
+ "mimo-v2-flash": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":256000},
+ "MiniMax-M1": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":80000},
+ "minimax/minimax-m2": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000},
+ "minimax/minimax-m2.1": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128000},
+ "minimax/minimax-m2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128000},
+ "minimax/minimax-m2.5-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":128000},
+ "moonshotai/kimi-k2-0905": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":100000},
+ "moonshotai/kimi-k2-thinking": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":100000},
+ "moonshotai/kimi-k2.5": {"tools":true,"images":true,"reasoning":false,"context":256000,"output":256000},
+ "openai/gpt-5": {"tools":true,"images":false,"reasoning":false,"context":400000,"output":128000},
+ "openai/gpt-5.2": {"tools":true,"images":true,"reasoning":true,"context":400000,"output":128000},
+ "qwen-max-2025-01-25": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":4096},
+ "qwen-turbo": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":4096},
+ "qwen-vl-max-2025-01-25": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":4096},
+ "qwen2.5-vl-72b-instruct": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "qwen2.5-vl-7b-instruct": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":8192},
+ "qwen3-235b-a22b": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":32000},
+ "qwen3-235b-a22b-instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":64000},
+ "qwen3-235b-a22b-thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":4096},
+ "qwen3-30b-a3b": {"tools":true,"images":false,"reasoning":true,"context":40000,"output":4096},
+ "qwen3-30b-a3b-instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":128000,"output":32000},
+ "qwen3-30b-a3b-thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":126000,"output":32000},
+ "qwen3-32b": {"tools":true,"images":false,"reasoning":true,"context":40000,"output":4096},
+ "qwen3-coder-480b-a35b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262000,"output":4096},
+ "qwen3-max": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen3-max-preview": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":64000},
+ "qwen3-next-80b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":32768},
+ "qwen3-next-80b-a3b-thinking": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "qwen3-vl-30b-a3b-thinking": {"tools":true,"images":true,"reasoning":false,"context":128000,"output":32000},
+ "qwen3.5-397b-a17b": {"tools":true,"images":true,"reasoning":true,"context":256000,"output":64000},
+ "stepfun-ai/gelab-zero-4b-preview": {"tools":true,"images":true,"reasoning":false,"context":8192,"output":4096},
+ "stepfun/step-3.5-flash": {"tools":false,"images":true,"reasoning":false,"context":64000,"output":4096},
+ "x-ai/grok-4-fast": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "x-ai/grok-4-fast-non-reasoning": {"tools":true,"images":true,"reasoning":false,"context":2000000,"output":2000000},
+ "x-ai/grok-4-fast-reasoning": {"tools":true,"images":true,"reasoning":true,"context":2000000,"output":2000000},
+ "x-ai/grok-4.1-fast": {"tools":true,"images":false,"reasoning":true,"context":2000000,"output":2000000},
+ "x-ai/grok-4.1-fast-non-reasoning": {"tools":true,"images":true,"reasoning":false,"context":2000000,"output":2000000},
+ "x-ai/grok-4.1-fast-reasoning": {"tools":true,"images":true,"reasoning":true,"context":20000000,"output":2000000},
+ "x-ai/grok-code-fast-1": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":10000},
+ "xiaomi/mimo-v2-flash": {"tools":true,"images":false,"reasoning":true,"context":256000,"output":256000},
+ "z-ai/autoglm-phone-9b": {"tools":true,"images":true,"reasoning":false,"context":12800,"output":4096},
+ "z-ai/glm-4.6": {"tools":true,"images":false,"reasoning":false,"context":200000,"output":200000},
+ "z-ai/glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":200000},
+ "z-ai/glm-5": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000}
+ },
+ "kimi": {
+ "kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k2.7-code-highspeed": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576}
+ },
+ "minimax": {
+ "MiniMax-M2": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax-M2.1": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax-M2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax-M2.5-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax-M2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax-M2.7-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "MiniMax-M3": {"tools":true,"images":true,"reasoning":true,"context":1000000,"output":512000}
+ },
+ "novita": {
+ "baichuan/baichuan-m2-32b": {"tools":false,"images":false,"reasoning":false,"context":131072,"output":131072},
+ "baidu/ernie-4.5-21B-a3b": {"tools":true,"images":false,"reasoning":false,"context":120000,"output":8000},
+ "baidu/ernie-4.5-21B-a3b-thinking": {"tools":false,"images":false,"reasoning":true,"context":131072,"output":65536},
+ "baidu/ernie-4.5-300b-a47b-paddle": {"tools":false,"images":false,"reasoning":false,"context":123000,"output":12000},
+ "baidu/ernie-4.5-vl-28b-a3b": {"tools":true,"images":true,"reasoning":true,"context":30000,"output":8000},
+ "baidu/ernie-4.5-vl-28b-a3b-thinking": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":65536},
+ "baidu/ernie-4.5-vl-424b-a47b": {"tools":false,"images":true,"reasoning":true,"context":123000,"output":16000},
+ "deepseek/deepseek-ocr": {"tools":false,"images":true,"reasoning":false,"context":8192,"output":8192},
+ "deepseek/deepseek-ocr-2": {"tools":false,"images":true,"reasoning":false,"context":8192,"output":8192},
+ "deepseek/deepseek-prover-v2-671b": {"tools":false,"images":false,"reasoning":false,"context":160000,"output":160000},
+ "deepseek/deepseek-r1-0528": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":32768},
+ "deepseek/deepseek-r1-0528-qwen3-8b": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":32000},
+ "deepseek/deepseek-r1-distill-llama-70b": {"tools":false,"images":false,"reasoning":true,"context":8192,"output":8192},
+ "deepseek/deepseek-r1-distill-qwen-14b": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":16384},
+ "deepseek/deepseek-r1-distill-qwen-32b": {"tools":false,"images":false,"reasoning":false,"context":64000,"output":32000},
+ "deepseek/deepseek-r1-turbo": {"tools":true,"images":false,"reasoning":true,"context":64000,"output":16000},
+ "deepseek/deepseek-v3-0324": {"tools":true,"images":false,"reasoning":false,"context":163840,"output":163840},
+ "deepseek/deepseek-v3-turbo": {"tools":true,"images":false,"reasoning":false,"context":64000,"output":16000},
+ "deepseek/deepseek-v3.1": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "deepseek/deepseek-v3.1-terminus": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "deepseek/deepseek-v3.2": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":65536},
+ "deepseek/deepseek-v3.2-exp": {"tools":true,"images":false,"reasoning":true,"context":163840,"output":65536},
+ "deepseek/deepseek-v4-flash": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":393216},
+ "deepseek/deepseek-v4-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":393216},
+ "google/gemma-3-12b-it": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":8192},
+ "google/gemma-3-27b-it": {"tools":false,"images":true,"reasoning":false,"context":98304,"output":16384},
+ "google/gemma-4-26b-a4b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "google/gemma-4-31b-it": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":131072},
+ "gryphe/mythomax-l2-13b": {"tools":false,"images":false,"reasoning":false,"context":4096,"output":3200},
+ "inclusionai/ling-2.6-1t": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":32768},
+ "inclusionai/ling-2.6-flash": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":32768},
+ "inclusionai/ring-2.6-1t": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":65536},
+ "kwaipilot/kat-coder-pro": {"tools":true,"images":false,"reasoning":false,"context":256000,"output":128000},
+ "meta-llama/llama-3-70b-instruct": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":8000},
+ "meta-llama/llama-3-8b-instruct": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":8192},
+ "meta-llama/llama-3.1-8b-instruct": {"tools":false,"images":false,"reasoning":false,"context":16384,"output":16384},
+ "meta-llama/llama-3.2-3b-instruct": {"tools":false,"images":false,"reasoning":false,"context":32768,"output":32000},
+ "meta-llama/llama-3.3-70b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":120000},
+ "meta-llama/llama-4-maverick-17b-128e-instruct-fp8": {"tools":false,"images":true,"reasoning":false,"context":1048576,"output":8192},
+ "meta-llama/llama-4-scout-17b-16e-instruct": {"tools":false,"images":true,"reasoning":false,"context":131072,"output":131072},
+ "microsoft/wizardlm-2-8x22b": {"tools":false,"images":false,"reasoning":false,"context":65535,"output":8000},
+ "minimax/minimax-m2": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.1": {"tools":true,"images":false,"reasoning":false,"context":204800,"output":131072},
+ "minimax/minimax-m2.5": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.5-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimax/minimax-m2.7-highspeed": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "minimaxai/minimax-m1-80k": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":40000},
+ "mistralai/mistral-nemo": {"tools":false,"images":false,"reasoning":false,"context":60288,"output":16000},
+ "moonshotai/kimi-k2-0905": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":98304},
+ "moonshotai/kimi-k2-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":32768},
+ "moonshotai/kimi-k2-thinking": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":98304},
+ "moonshotai/kimi-k2.5": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "moonshotai/kimi-k2.6": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "moonshotai/kimi-k2.7-code": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":262144},
+ "moonshotai/kimi-k3": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":1048576},
+ "nousresearch/hermes-2-pro-llama-3-8b": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":8192},
+ "openai/gpt-oss-120b": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "openai/gpt-oss-20b": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "paddlepaddle/paddleocr-vl": {"tools":false,"images":true,"reasoning":false,"context":16384,"output":16384},
+ "qwen/qwen-2.5-72b-instruct": {"tools":true,"images":false,"reasoning":false,"context":32000,"output":8192},
+ "qwen/qwen-mt-plus": {"tools":false,"images":false,"reasoning":false,"context":16384,"output":8192},
+ "qwen/qwen2.5-7b-instruct": {"tools":true,"images":false,"reasoning":false,"context":32000,"output":32000},
+ "qwen/qwen2.5-vl-72b-instruct": {"tools":false,"images":true,"reasoning":false,"context":32768,"output":32768},
+ "qwen/qwen3-235b-a22b-fp8": {"tools":false,"images":false,"reasoning":true,"context":40960,"output":20000},
+ "qwen/qwen3-235b-a22b-instruct-2507": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":16384},
+ "qwen/qwen3-235b-a22b-thinking-2507": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "qwen/qwen3-30b-a3b-fp8": {"tools":false,"images":false,"reasoning":true,"context":40960,"output":20000},
+ "qwen/qwen3-32b-fp8": {"tools":false,"images":false,"reasoning":true,"context":40960,"output":20000},
+ "qwen/qwen3-4b-fp8": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":20000},
+ "qwen/qwen3-8b-fp8": {"tools":false,"images":false,"reasoning":true,"context":128000,"output":20000},
+ "qwen/qwen3-coder-30b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":160000,"output":32768},
+ "qwen/qwen3-coder-480b-a35b-instruct": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen/qwen3-coder-next": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen/qwen3-max": {"tools":true,"images":false,"reasoning":false,"context":262144,"output":65536},
+ "qwen/qwen3-next-80b-a3b-instruct": {"tools":true,"images":false,"reasoning":false,"context":131072,"output":32768},
+ "qwen/qwen3-next-80b-a3b-thinking": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":32768},
+ "qwen/qwen3-omni-30b-a3b-instruct": {"tools":true,"images":true,"reasoning":false,"context":65536,"output":16384},
+ "qwen/qwen3-omni-30b-a3b-thinking": {"tools":true,"images":true,"reasoning":true,"context":65536,"output":16384},
+ "qwen/qwen3-vl-235b-a22b-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":32768},
+ "qwen/qwen3-vl-235b-a22b-thinking": {"tools":false,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "qwen/qwen3-vl-30b-a3b-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":32768},
+ "qwen/qwen3-vl-30b-a3b-thinking": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":32768},
+ "qwen/qwen3-vl-8b-instruct": {"tools":true,"images":true,"reasoning":false,"context":131072,"output":32768},
+ "qwen/qwen3.5-122b-a10b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3.5-27b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3.5-35b-a3b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":65536},
+ "qwen/qwen3.5-397b-a17b": {"tools":true,"images":true,"reasoning":true,"context":262144,"output":64000},
+ "qwen/qwen3.7-max": {"tools":true,"images":false,"reasoning":true,"context":1000000,"output":65536},
+ "sao10K/l3-70b-euryale-v2.1": {"tools":true,"images":false,"reasoning":false,"context":8192,"output":8192},
+ "sao10K/l3-8b-lunaris": {"tools":false,"images":false,"reasoning":false,"context":8192,"output":8192},
+ "sao10K/L3-8B-stheno-v3.2": {"tools":true,"images":false,"reasoning":false,"context":8192,"output":32000},
+ "sao10K/l31-70b-euryale-v2.2": {"tools":true,"images":false,"reasoning":false,"context":8192,"output":8192},
+ "xiaomimimo/mimo-v2-flash": {"tools":true,"images":false,"reasoning":true,"context":262144,"output":32000},
+ "xiaomimimo/mimo-v2-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "xiaomimimo/mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "zai-org/autoglm-phone-9b-multilingual": {"tools":false,"images":true,"reasoning":false,"context":65536,"output":65536},
+ "zai-org/glm-4.5": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "zai-org/glm-4.5-air": {"tools":true,"images":false,"reasoning":true,"context":131072,"output":98304},
+ "zai-org/glm-4.5v": {"tools":true,"images":true,"reasoning":true,"context":65536,"output":16384},
+ "zai-org/glm-4.6": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "zai-org/glm-4.6v": {"tools":true,"images":true,"reasoning":true,"context":131072,"output":32768},
+ "zai-org/glm-4.7": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "zai-org/glm-4.7-flash": {"tools":true,"images":false,"reasoning":true,"context":200000,"output":128000},
+ "zai-org/glm-5": {"tools":true,"images":false,"reasoning":true,"context":202800,"output":131072},
+ "zai-org/glm-5.1": {"tools":true,"images":false,"reasoning":true,"context":204800,"output":131072},
+ "zai-org/glm-5.2": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072}
+ },
+ "mimo": {
+ "mimo-v2.5": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.5-pro": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.5-pro-ultraspeed": {"tools":true,"images":false,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.6-flash": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.6-pro": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072},
+ "mimo-v2.6-pro-ultraspeed": {"tools":true,"images":true,"reasoning":true,"context":1048576,"output":131072}
+ }
+}
diff --git a/lib/model-catalog.ts b/lib/model-catalog.ts
new file mode 100644
index 00000000..60f26cf5
--- /dev/null
+++ b/lib/model-catalog.ts
@@ -0,0 +1,45 @@
+import catalog from "@/lib/model-catalog.json"
+import type { ProviderName } from "@/lib/types/model-config"
+
+/**
+ * What models.dev knows about a model (scripts/update-model-catalog.mjs).
+ * Only used for hints: requests are sent the same way either way, since
+ * the data can be wrong or out of date.
+ */
+export interface ModelInfo {
+ tools: boolean
+ images: boolean
+ reasoning: boolean
+ context?: number
+ output?: number
+}
+
+const CATALOG = catalog as Record>
+
+/**
+ * The entry for a model: an exact match ignoring case, else the longest id
+ * the model id starts with, followed by "-", ":" or ".". So
+ * claude-sonnet-4-5-20250929 finds claude-sonnet-4-5, but gpt-4 does not
+ * find gpt-4o.
+ */
+export function getModelInfo(
+ provider: ProviderName,
+ modelId: string,
+): ModelInfo | undefined {
+ const models = CATALOG[provider]
+ if (!models) return undefined
+ const wanted = modelId.trim().toLowerCase()
+ let best: string | undefined
+ for (const id of Object.keys(models)) {
+ const lower = id.toLowerCase()
+ if (lower === wanted) return models[id]
+ if (
+ wanted.startsWith(lower) &&
+ "-:.".includes(wanted[lower.length]) &&
+ lower.length > (best?.length ?? 0)
+ ) {
+ best = id
+ }
+ }
+ return best ? models[best] : undefined
+}
diff --git a/lib/output-token-limit.ts b/lib/output-token-limit.ts
index bd4a9876..28119eea 100644
--- a/lib/output-token-limit.ts
+++ b/lib/output-token-limit.ts
@@ -22,6 +22,12 @@ export const MAX_OUTPUT_TOKENS_LIMIT = 200000
*/
const MIN_USABLE_OUTPUT_TOKENS = 1024
+/**
+ * Retry budget when a rejection names the budget parameter but no number we can
+ * read. It is the default from before 64000, which these providers ran with.
+ */
+const FALLBACK_OUTPUT_TOKENS = 16000
+
/** Status codes that can carry a complaint about the requested budget. */
const BUDGET_REJECTION_STATUSES = new Set([400, 422])
@@ -29,24 +35,8 @@ function usableLimit(value: number): number | null {
return value >= MIN_USABLE_OUTPUT_TOKENS ? value : null
}
-/**
- * A budget this large exceeds what some models accept. Providers reject it with a
- * 400 that names the real limit, so we parse the number out and retry once
- * instead of failing the turn.
- *
- * Formats seen in the wild:
- * - Bedrock: "The maximum tokens you requested exceeds the model limit of 4096."
- * - OpenRouter: "This endpoint's maximum context length is 64000 tokens. However,
- * you requested about 64025 tokens (25 of text input, 64000 in the output)."
- * Note this one is an input+output ceiling, so the input has to be subtracted.
- * - Anthropic: "max_tokens: 200000 > 64000, which is the maximum allowed..."
- * - OpenAI: "This model supports at most 16384 completion tokens"
- *
- * Every pattern names tokens explicitly. A generic one (an earlier draft matched
- * "lower than N") would reinterpret unrelated failures, and retrying on a bogus
- * number turns a readable error into an empty diagram.
- */
-export function parseOutputTokenLimit(error: unknown): number | null {
+/** Message and body of an error that may be about the budget, or null. */
+export function rejectionText(error: unknown): string | null {
const err = error as {
message?: unknown
responseBody?: unknown
@@ -66,24 +56,109 @@ export function parseOutputTokenLimit(error: unknown): number | null {
typeof err?.responseBody === "string" ? err.responseBody : "",
].join(" ")
- if (!text) return null
+ return text.trim() ? text : null
+}
+/**
+ * A budget this large exceeds what some models accept. Providers reject it with a
+ * 400 that names the real limit, so we parse the number out and retry once
+ * instead of failing the turn.
+ *
+ * Formats seen in the wild:
+ * - Bedrock: "The maximum tokens you requested exceeds the model limit of 4096."
+ * - OpenRouter: "This endpoint's maximum context length is 64000 tokens. However,
+ * you requested about 64025 tokens (25 of text input, 64000 in the output)."
+ * Note this one is an input+output ceiling, so the input has to be subtracted.
+ * vLLM and SGLang send the same kind of ceiling, with the input written as
+ * "6000 in the messages", "has 6000 input tokens" or "6000 tokens from the input".
+ * - Anthropic: "max_tokens: 200000 > 64000, which is the maximum allowed..."
+ * - OpenAI: "This model supports at most 16384 completion tokens"
+ * - Volcengine Ark: "The parameter `max_tokens` specified in the request are not
+ * valid: integer above maximum value, expected a value <= 32768, but got 64000"
+ * - DashScope: "Range of max_tokens should be [1, 8192]"
+ *
+ * Every pattern names tokens explicitly. A generic one (an earlier draft matched
+ * "lower than N") would reinterpret unrelated failures, and retrying on a bogus
+ * number turns a readable error into an empty diagram.
+ */
+function readCeiling(text: string): number | null {
// Combined input+output ceiling: subtract the input the provider counted,
// plus a small margin because its estimate is approximate.
- const context = text.match(/maximum context length is (\d+)/i)
+ const context = text.match(/maximum context length (?:is|of) (\d+)/i)
if (context) {
- const input = text.match(/(\d+) of text input/i)
- return usableLimit(
- Number(context[1]) - (input ? Number(input[1]) : 0) - 1024,
- )
+ const input =
+ text.match(/(\d+) of text input/i) ||
+ text.match(/(\d+) in the messages/i) ||
+ text.match(/(\d+) tokens from the input/i) ||
+ text.match(/(\d+) input tokens/i)
+ return Number(context[1]) - (input ? Number(input[1]) : 0) - 1024
}
const output =
text.match(/model limit of (\d+)/i) ||
text.match(/> (\d+), which is the maximum/i) ||
- text.match(/at most (\d+) completion tokens/i)
+ text.match(/at most (\d+) completion tokens/i) ||
+ text.match(/max_\w*tokens.*?expected a value (?:<=|\\u003c=) (\d+)/i) ||
+ text.match(/Range of max_tokens should be \[1,\s*(\d+)\]/i)
- return output ? usableLimit(Number(output[1])) : null
+ return output ? Number(output[1]) : null
+}
+
+/** The usable output ceiling named in a rejection, or null. */
+export function parseOutputTokenLimit(error: unknown): number | null {
+ const text = rejectionText(error)
+ const ceiling = text ? readCeiling(text) : null
+ return ceiling === null ? null : usableLimit(ceiling)
+}
+
+/**
+ * Thinking budget the provider adds on top of maxOutputTokens. Bedrock and
+ * Anthropic send maxOutputTokens + budgetTokens as max_tokens, so a ceiling in
+ * their rejection covers both.
+ */
+function thinkingBudget(providerOptions: unknown): number {
+ const options = providerOptions as
+ | {
+ bedrock?: {
+ reasoningConfig?: { type?: string; budgetTokens?: unknown }
+ }
+ anthropic?: {
+ thinking?: { type?: string; budgetTokens?: unknown }
+ }
+ }
+ | undefined
+ const config =
+ options?.bedrock?.reasoningConfig ?? options?.anthropic?.thinking
+ return config?.type === "enabled" && typeof config.budgetTokens === "number"
+ ? config.budgetTokens
+ : 0
+}
+
+/**
+ * The budget to retry with after a rejection, or null to surface the error.
+ */
+export function retryOutputTokens(
+ error: unknown,
+ params: { maxOutputTokens?: number; providerOptions?: unknown },
+): number | null {
+ const requested = params.maxOutputTokens
+ const text = rejectionText(error)
+ if (!requested || !text) return null
+
+ const ceiling = readCeiling(text)
+ if (ceiling !== null) {
+ // The ceiling applies to what was actually sent, thinking included,
+ // so the retry has to leave room for the thinking too.
+ const thinking = thinkingBudget(params.providerOptions)
+ if (ceiling >= requested + thinking) return null
+ return usableLimit(ceiling - thinking)
+ }
+
+ // Names the budget parameter, but in a format we cannot read a number from
+ if (/max_\w*tokens/i.test(text) && requested > FALLBACK_OUTPUT_TOKENS) {
+ return FALLBACK_OUTPUT_TOKENS
+ }
+ return null
}
/**
@@ -103,17 +178,15 @@ export function withOutputTokenLimitFallback(
try {
return await doStream()
} catch (error) {
- const limit = parseOutputTokenLimit(error)
- const requested = params.maxOutputTokens
-
- if (!limit || !requested || limit >= requested) throw error
+ const retry = retryOutputTokens(error, params)
+ if (!retry) throw error
console.warn(
- `[maxOutputTokens] ${requested} rejected, retrying with ${limit}`,
+ `[maxOutputTokens] ${params.maxOutputTokens} rejected, retrying with ${retry}`,
)
return await inner.doStream({
...params,
- maxOutputTokens: limit,
+ maxOutputTokens: retry,
})
}
},
@@ -135,11 +208,17 @@ function validBudget(value: string | null | undefined): number | null {
* desktop app too), then server env, then the default. Both sources go through
* the same validation, so a typo in either falls back instead of reaching the
* provider.
+ *
+ * On the server's credentials the user setting can only lower the server value,
+ * so MAX_OUTPUT_TOKENS keeps capping what the server pays for.
*/
-export function resolveMaxOutputTokens(headerValue: string | null): number {
- return (
- validBudget(headerValue) ??
- validBudget(process.env.MAX_OUTPUT_TOKENS) ??
- DEFAULT_MAX_OUTPUT_TOKENS
- )
+export function resolveMaxOutputTokens(
+ headerValue: string | null,
+ usesServerCredentials: boolean,
+): number {
+ const header = validBudget(headerValue)
+ const server =
+ validBudget(process.env.MAX_OUTPUT_TOKENS) ?? DEFAULT_MAX_OUTPUT_TOKENS
+ if (header === null) return server
+ return usesServerCredentials ? Math.min(header, server) : header
}
diff --git a/lib/pdf-utils.ts b/lib/pdf-utils.ts
index 2e5c4adb..49db1041 100644
--- a/lib/pdf-utils.ts
+++ b/lib/pdf-utils.ts
@@ -1,4 +1,4 @@
-import { extractText, getDocumentProxy } from "unpdf"
+import { extractText } from "unpdf"
// Maximum characters allowed for extracted text (configurable via env)
const DEFAULT_MAX_EXTRACTED_CHARS = 150000 // 150k chars
@@ -14,6 +14,7 @@ const TEXT_EXTENSIONS = [
".json",
".csv",
".xml",
+ ".svg",
".html",
".css",
".js",
@@ -43,8 +44,10 @@ const TEXT_EXTENSIONS = [
*/
export async function extractPdfText(file: File): Promise {
const buffer = await file.arrayBuffer()
- const pdf = await getDocumentProxy(new Uint8Array(buffer))
- const { text } = await extractText(pdf, { mergePages: true })
+ // Pass raw bytes so unpdf destroys the PDF document when it is done
+ const { text } = await extractText(new Uint8Array(buffer), {
+ mergePages: true,
+ })
return text as string
}
diff --git a/lib/provider-models.ts b/lib/provider-models.ts
new file mode 100644
index 00000000..a809da75
--- /dev/null
+++ b/lib/provider-models.ts
@@ -0,0 +1,255 @@
+import { createGateway } from "ai"
+import { getModelInfo } from "@/lib/model-catalog"
+import { readLimitedBody } from "@/lib/read-limited-body"
+import {
+ normalizeBaseUrl,
+ PROVIDER_INFO,
+ type ProviderName,
+} from "@/lib/types/model-config"
+
+/** A model a provider offers. tools is false when it cannot call tools. */
+export interface ListedModel {
+ id: string
+ tools?: boolean
+}
+
+export const AIHUBMIX_MODELS_ENDPOINT = "https://aihubmix.com/api/v1/models"
+
+export function canListModels(provider: ProviderName): boolean {
+ return (
+ Object.hasOwn(PROVIDER_INFO, provider) &&
+ !!PROVIDER_INFO[provider].modelList
+ )
+}
+
+// Models in OpenAI-style lists that are not for chat
+const NON_CHAT =
+ /(?:^|[-/_])(?:embed(?:ding)?s?|whisper|tts|transcribe|dall-e|moderation|rerank|realtime|sora)(?:$|[-/_])|gpt-image/i
+
+const NON_CHAT_AIHUBMIX_TYPES = new Set([
+ "embedding",
+ "image_generation",
+ "rerank",
+ "transcription",
+ "tts",
+ "video",
+])
+
+/** Chat model ids from AIHubMix's public model list */
+export function extractAihubmixModelIds(payload: unknown): string[] {
+ const data = (payload as { data?: unknown })?.data
+ if (!Array.isArray(data)) return []
+ const ids = new Set()
+ for (const item of data) {
+ const record = item as { model_id?: unknown; types?: unknown }
+ if (typeof record?.model_id !== "string" || !record.model_id.trim()) {
+ continue
+ }
+ const types = new Set(
+ typeof record.types === "string"
+ ? record.types.split(",").map((t) => t.trim())
+ : [],
+ )
+ if (!types.has("llm")) continue
+ if ([...NON_CHAT_AIHUBMIX_TYPES].some((t) => types.has(t))) continue
+ ids.add(record.model_id.trim())
+ }
+ return [...ids]
+}
+
+/**
+ * An error this module wrote itself. Only these texts reach the caller:
+ * the base URL is the caller's and may be an internal address, so anything
+ * else (a parse error quoting the body, a network error naming a host)
+ * stays in the server log.
+ */
+export class ModelListError extends Error {
+ constructor(
+ message: string,
+ readonly statusCode?: number,
+ ) {
+ super(message)
+ this.name = "ModelListError"
+ }
+}
+
+const MAX_LIST_BYTES = 2 * 1024 * 1024
+
+/** A fetch that reads at most MAX_LIST_BYTES of each response */
+function sizeLimitedFetch(fetchFn: typeof fetch): typeof fetch {
+ return async (input, init) => {
+ // Ends a download that is too large (the Gateway SDK passes no
+ // signal of its own)
+ const download = new AbortController()
+ const signal = init?.signal
+ ? AbortSignal.any([init.signal, download.signal])
+ : download.signal
+ const response = await fetchFn(input, { ...init, signal })
+ const body = await readLimitedBody(response, MAX_LIST_BYTES)
+ if (body === null) {
+ download.abort()
+ throw new ModelListError("The model list is too large.")
+ }
+ // The body is already decoded and has its own length now
+ const headers = new Headers(response.headers)
+ headers.delete("content-encoding")
+ headers.delete("content-length")
+ // Some statuses must have no body at all
+ const noBody = [101, 204, 205, 304].includes(response.status)
+ return new Response(noBody ? null : body, {
+ status: response.status,
+ statusText: response.statusText,
+ headers,
+ })
+ }
+}
+
+/** GET a JSON list; a failed request carries its status for the error hint */
+async function getJson(
+ url: string,
+ headers: Record,
+ fetchFn: typeof fetch,
+): Promise {
+ const response = await fetchFn(url, {
+ headers,
+ signal: AbortSignal.timeout(15_000),
+ })
+ if (!response.ok) {
+ throw new ModelListError(
+ `The model list request failed (${response.status})`,
+ response.status,
+ )
+ }
+ const text = await response.text()
+ try {
+ return JSON.parse(text)
+ } catch {
+ throw new ModelListError("The model list was not valid JSON.")
+ }
+}
+
+/**
+ * Where to list from without the user's base URL: where chat goes then. For
+ * Ollama without a key that is the server's Ollama, else the SDK's local
+ * default; a local default in PROVIDER_INFO (SGLang's) only fills the
+ * settings form.
+ */
+function listFallbackUrl(provider: ProviderName, apiKey?: string): string {
+ if (provider === "ollama" && !apiKey) {
+ return process.env.OLLAMA_BASE_URL || "http://127.0.0.1:11434/api"
+ }
+ const url = PROVIDER_INFO[provider].defaultBaseUrl
+ return url?.startsWith("https://") ? url : ""
+}
+
+/**
+ * The provider's chat models, with tool support from the provider's own
+ * data or else models.dev. Only the client's key is used, so the server's
+ * keys never go to a URL the client chose.
+ */
+export async function listProviderModels(
+ provider: ProviderName,
+ { apiKey, baseUrl }: { apiKey?: string; baseUrl?: string },
+ unlimitedFetch: typeof fetch = fetch,
+): Promise {
+ const fetchFn = sizeLimitedFetch(unlimitedFetch)
+ const base = normalizeBaseUrl(baseUrl || listFallbackUrl(provider, apiKey))
+ const bearer: Record = apiKey
+ ? { Authorization: `Bearer ${apiKey}` }
+ : {}
+ let models: ListedModel[]
+
+ // AIHubMix has a public list, unless the user points to another
+ // endpoint, which is OpenAI-compatible
+ const style =
+ provider === "aihubmix" &&
+ baseUrl &&
+ !/^https:\/\/aihubmix\.com(\/v1)?$/.test(base)
+ ? "openai"
+ : PROVIDER_INFO[provider].modelList
+
+ switch (style) {
+ case "anthropic": {
+ const data = await getJson(
+ `${base}/models?limit=1000`,
+ {
+ "x-api-key": apiKey ?? "",
+ "anthropic-version": "2023-06-01",
+ },
+ fetchFn,
+ )
+ models = (data.data ?? []).map((m: { id: string }) => ({
+ id: m.id,
+ }))
+ break
+ }
+ case "google": {
+ // The key goes in a header: in the URL it would end up in logs
+ const data = await getJson(
+ `${base}/models?pageSize=1000`,
+ { "x-goog-api-key": apiKey ?? "" },
+ fetchFn,
+ )
+ models = (data.models ?? [])
+ .filter((m: { supportedGenerationMethods?: string[] }) =>
+ m.supportedGenerationMethods?.includes("generateContent"),
+ )
+ .map((m: { name: string }) => ({
+ id: m.name.replace(/^models\//, ""),
+ }))
+ break
+ }
+ case "ollama": {
+ const api = base.endsWith("/api") ? base : `${base}/api`
+ const data = await getJson(`${api}/tags`, bearer, fetchFn)
+ models = (data.models ?? []).map((m: { name: string }) => ({
+ id: m.name,
+ }))
+ break
+ }
+ case "openrouter": {
+ const data = await getJson(`${base}/models`, bearer, fetchFn)
+ models = (data.data ?? []).map(
+ (m: { id: string; supported_parameters?: string[] }) => ({
+ id: m.id,
+ ...(m.supported_parameters && {
+ tools: m.supported_parameters.includes("tools"),
+ }),
+ }),
+ )
+ break
+ }
+ case "gateway": {
+ const { models: entries } = await createGateway({
+ ...(apiKey && { apiKey }),
+ ...(baseUrl && { baseURL: base }),
+ fetch: fetchFn,
+ }).getAvailableModels()
+ models = entries
+ .filter((m) => !m.modelType || m.modelType === "language")
+ .map((m) => ({ id: m.id }))
+ break
+ }
+ case "aihubmix": {
+ const data = await getJson(AIHUBMIX_MODELS_ENDPOINT, {}, fetchFn)
+ models = extractAihubmixModelIds(data).map((id) => ({ id }))
+ break
+ }
+ default: {
+ if (!base) {
+ throw new ModelListError(
+ `${PROVIDER_INFO[provider].label} needs a base URL to list its models.`,
+ )
+ }
+ const data = await getJson(`${base}/models`, bearer, fetchFn)
+ models = (data.data ?? [])
+ .map((m: { id: string }) => ({ id: m.id }))
+ .filter((m: ListedModel) => !NON_CHAT.test(m.id))
+ }
+ }
+
+ return models.map((m) => ({
+ ...m,
+ tools: m.tools ?? getModelInfo(provider, m.id)?.tools,
+ }))
+}
diff --git a/lib/read-limited-body.ts b/lib/read-limited-body.ts
new file mode 100644
index 00000000..884462f9
--- /dev/null
+++ b/lib/read-limited-body.ts
@@ -0,0 +1,32 @@
+/**
+ * Read a response body, giving up once it passes maxBytes, so a huge
+ * download from a URL the client chose can't exhaust server memory.
+ * Returns null when it is too large; the caller then aborts the request,
+ * which ends the download.
+ */
+export async function readLimitedBody(
+ response: Response,
+ maxBytes: number,
+): Promise {
+ if (Number(response.headers.get("content-length")) > maxBytes) {
+ return null
+ }
+ if (!response.body) return new ArrayBuffer(0)
+
+ const reader = response.body.getReader()
+ const chunks: Uint8Array[] = []
+ let total = 0
+ while (true) {
+ const { done, value } = await reader.read()
+ if (done) break
+ total += value.byteLength
+ if (total > maxBytes) {
+ // Not awaited: a copy of the body that Next.js keeps (its fetch
+ // dedupe) can hold the cancel back until it is read
+ reader.cancel().catch(() => {})
+ return null
+ }
+ chunks.push(value)
+ }
+ return new Blob(chunks as BlobPart[]).arrayBuffer()
+}
diff --git a/lib/server-model-config.ts b/lib/server-model-config.ts
index f00d1485..6b5220f0 100644
--- a/lib/server-model-config.ts
+++ b/lib/server-model-config.ts
@@ -47,11 +47,14 @@ export interface FlattenedServerModel {
/**
* Convert provider name to URL-safe slug for use in model ID
- * e.g., "OpenAI Production" → "openai-production"
+ * e.g., "OpenAI Production" → "openai-production", "主力" → "4e3b-529b"
+ * Non-ASCII characters become their hex code point so CJK names stay
+ * distinct; the id is sent in HTTP headers, which must be ASCII.
*/
-function slugify(name: string): string {
+export function slugify(name: string): string {
return name
.toLowerCase()
+ .replace(/[^\p{ASCII}]/gu, (c) => `-${c.codePointAt(0)?.toString(16)}-`)
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "")
}
@@ -189,6 +192,7 @@ export async function loadFlattenedServerModels(): Promise<
const defaultModelId = process.env.AI_MODEL
const flattened: FlattenedServerModel[] = []
+ const seenIds = new Set()
for (const p of cfg.providers) {
const providerLabel =
@@ -199,6 +203,16 @@ export async function loadFlattenedServerModels(): Promise<
for (const modelId of p.models) {
const id = `server:${nameSlug}:${modelId}`
+ // Names that differ only in case or punctuation share a slug.
+ // A repeated id would always resolve to the first provider's
+ // credentials, so drop it instead.
+ if (seenIds.has(id)) {
+ console.warn(
+ `[server-model-config] Skipping duplicate model id "${id}". Provider names must differ in letters or digits.`,
+ )
+ continue
+ }
+ seenIds.add(id)
// Default model priority:
// 1. From ai-models.json: first model of provider with default: true
diff --git a/lib/session-storage.ts b/lib/session-storage.ts
index 8450375c..33098367 100644
--- a/lib/session-storage.ts
+++ b/lib/session-storage.ts
@@ -1,5 +1,6 @@
import { type DBSchema, type IDBPDatabase, openDB } from "idb"
import { nanoid } from "nanoid"
+import { toast } from "sonner"
import type { Template } from "./template-storage"
// Constants
@@ -61,6 +62,7 @@ let dbPromise: Promise> | null = null
async function getDB(): Promise> {
if (!dbPromise) {
+ // A failed or lost connection is not cached: the next call reopens it
dbPromise = openDB(DB_NAME, DB_VERSION, {
upgrade(db, oldVersion) {
if (oldVersion < 1) {
@@ -88,6 +90,28 @@ async function getDB(): Promise> {
}
}
},
+ blocked() {
+ // An older tab keeps the DB open, so the upgrade has to wait
+ toast.warning(
+ "Please close other tabs of this app to finish updating chat storage.",
+ { id: "idb-upgrade-blocked", duration: 10000 },
+ )
+ },
+ blocking(_currentVersion, _blockedVersion, event) {
+ // Another tab needs to upgrade the DB: close our connection so
+ // it is not stuck, and reopen on the next call
+ const db = event.target as IDBDatabase
+ db.close()
+ dbPromise = null
+ },
+ terminated() {
+ // The browser closed the connection (e.g. Safari after a long
+ // time in the background)
+ dbPromise = null
+ },
+ }).catch((error) => {
+ dbPromise = null
+ throw error
})
}
return dbPromise
@@ -145,36 +169,22 @@ export async function getSession(id: string): Promise {
}
}
+// Returns false on failure (e.g. storage quota exceeded). Other sessions are
+// never deleted automatically; the caller tells the user instead.
export async function saveSession(session: ChatSession): Promise {
if (!isIndexedDBAvailable()) return false
try {
const db = await getDB()
await db.put(STORE_NAME, session)
+ // The desktop app opens this port (this origin's chats) next launch
+ window.electronAPI?.chatSaved?.().catch(() => {})
return true
} catch (error) {
- // Handle quota exceeded
- if (
- error instanceof DOMException &&
- error.name === "QuotaExceededError"
- ) {
- console.warn("Storage quota exceeded, deleting oldest session...")
- await deleteOldestSession()
- // Retry once
- try {
- const db = await getDB()
- await db.put(STORE_NAME, session)
- return true
- } catch (retryError) {
- console.error(
- "Failed to save session after cleanup:",
- retryError,
- )
- return false
- }
- } else {
- console.error("Failed to save session:", error)
- return false
- }
+ console.error("Failed to save session:", error)
+ // Reopen the connection next time in case it was lost (Safari reports
+ // "Connection to Indexed Database server lost" without closing it)
+ dbPromise = null
+ return false
}
}
@@ -189,13 +199,18 @@ export async function deleteSession(id: string): Promise {
}
export async function getSessionCount(): Promise {
- if (!isIndexedDBAvailable()) return 0
+ return (await readSessionCount()) ?? 0
+}
+
+/** The number of saved chats, or null when it could not be read */
+export async function readSessionCount(): Promise {
+ if (!isIndexedDBAvailable()) return null
try {
const db = await getDB()
return await db.count(STORE_NAME)
} catch (error) {
console.error("Failed to get session count:", error)
- return 0
+ return null
}
}
diff --git a/lib/ssrf-protection.ts b/lib/ssrf-protection.ts
index 6b43ddf2..20f2e191 100644
--- a/lib/ssrf-protection.ts
+++ b/lib/ssrf-protection.ts
@@ -115,3 +115,87 @@ export async function isPrivateUrl(urlString: string): Promise {
export function allowPrivateUrls(): boolean {
return process.env.ALLOW_PRIVATE_URLS !== "false"
}
+
+/** A redirect the guard below refused; its text is safe to show */
+export class RedirectRefusedError extends Error {
+ constructor(message = "Redirects are not allowed for custom base URLs") {
+ super(message)
+ this.name = "RedirectRefusedError"
+ }
+}
+
+const MAX_REDIRECTS = 5
+
+// Dropped when a redirect goes to another origin: those fetch drops, and
+// the key headers of providers that do not use Authorization (Anthropic,
+// Google, Azure)
+const CREDENTIAL_HEADERS = [
+ "authorization",
+ "proxy-authorization",
+ "cookie",
+ "x-api-key",
+ "x-goog-api-key",
+ "api-key",
+]
+
+/**
+ * A fetch for requests to a base URL the client chose. With private URLs
+ * blocked, a public URL could still redirect the request to an internal
+ * host, so redirects are refused. With private URLs allowed but the quota
+ * on (DYNAMODB_QUOTA_TABLE), a request to a private address counts as the
+ * server's: a public URL's redirects are followed only to public addresses,
+ * or it could reach the server's own network uncounted. Undefined otherwise.
+ */
+export function redirectGuardedFetch(): typeof fetch | undefined {
+ const blockAll = !allowPrivateUrls()
+ if (!blockAll && !process.env.DYNAMODB_QUOTA_TABLE) return undefined
+ return async (input, init) => {
+ let url = input instanceof Request ? input.url : String(input)
+ let next = init
+ // A request to a private address already counts as the server's
+ let startsPrivate: boolean | undefined
+ for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
+ const response = await fetch(url, { ...next, redirect: "manual" })
+ const location = response.headers.get("location")
+ if (response.status < 300 || response.status >= 400 || !location) {
+ return response
+ }
+ if (blockAll) throw new RedirectRefusedError()
+ startsPrivate ??= await isPrivateUrl(url)
+ const from = new URL(url)
+ url = new URL(location, url).toString()
+ if (!startsPrivate && (await isPrivateUrl(url))) {
+ throw new RedirectRefusedError(
+ "Redirects to private addresses are not allowed",
+ )
+ }
+ // The rest as fetch itself does it. Another origin gets no
+ // credentials (the user's key, EdgeOne's cookies)
+ const headers = new Headers(next?.headers)
+ if (new URL(url).origin !== from.origin) {
+ for (const name of CREDENTIAL_HEADERS) headers.delete(name)
+ }
+ next = { ...next, headers }
+ // 303, and 301 or 302 after a POST, go on as a GET without the
+ // body
+ const method = (next.method ?? "GET").toUpperCase()
+ if (
+ response.status === 303 ||
+ ((response.status === 301 || response.status === 302) &&
+ method === "POST")
+ ) {
+ for (const name of [
+ "content-type",
+ "content-length",
+ "content-encoding",
+ "content-language",
+ "content-location",
+ ]) {
+ headers.delete(name)
+ }
+ next = { ...next, method: "GET", body: undefined }
+ }
+ }
+ throw new RedirectRefusedError("Too many redirects")
+ }
+}
diff --git a/lib/system-prompts.ts b/lib/system-prompts.ts
index aafa52b4..f033bd61 100644
--- a/lib/system-prompts.ts
+++ b/lib/system-prompts.ts
@@ -1,11 +1,14 @@
/**
* System prompts for different AI models
* Extended prompt is used for models with higher cache token minimums (Opus 4.5, Haiku 4.5)
- *
- * Token counting utilities are in a separate file (token-counter.ts) to avoid
- * WebAssembly issues with Next.js server-side rendering.
*/
+import {
+ SWIMLANE_EXAMPLE,
+ TWO_EDGES_EXAMPLE,
+ WAYPOINT_EXAMPLE,
+} from "@/packages/mcp-server/src/xml-examples.ts"
+
// Default system prompt (~1900 tokens) - works with all models
export const DEFAULT_SYSTEM_PROMPT = `
You are an expert diagram creation assistant specializing in draw.io XML generation.
@@ -41,7 +44,7 @@ parameters: {
tool name: edit_diagram
description: Edit specific parts of the EXISTING diagram. Use this when making small targeted changes like adding/removing elements, changing labels, or adjusting properties. This is more efficient than regenerating the entire diagram.
parameters: {
- edits: Array<{search: string, replace: string}>
+ operations: Array<{operation: "update" | "add" | "delete", cell_id: string, new_xml?: string}>
}
---Tool3---
tool name: append_diagram
@@ -241,21 +244,7 @@ const EXTENDED_ADDITIONS = `
**Example with swimlanes and edges** (generate ONLY this - no wrapper tags):
\`\`\`xml
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+${SWIMLANE_EXAMPLE}
\`\`\`
### append_diagram Details
@@ -318,12 +307,7 @@ If cell_id not found, check "Current diagram XML" for correct IDs. Use display_d
### Two edges between same nodes (CORRECT - no overlap):
\`\`\`xml
-
-
-
-
-
-
+${TWO_EDGES_EXAMPLE}
\`\`\`
### Edge with single waypoint (simple detour):
@@ -342,14 +326,7 @@ If cell_id not found, check "Current diagram XML" for correct IDs. Use display_d
**WRONG:** Direct diagonal line crosses over Develop
**CORRECT:** Route around the OUTSIDE (go right first, then up)
\`\`\`xml
-
-
-
-
-
-
-
-
+${WAYPOINT_EXAMPLE}
\`\`\`
This routes the edge to the RIGHT of all shapes (x=750), then enters Main from the right side.
diff --git a/lib/types/model-config.ts b/lib/types/model-config.ts
index a1d9603a..d8acad4f 100644
--- a/lib/types/model-config.ts
+++ b/lib/types/model-config.ts
@@ -32,6 +32,8 @@ export interface ModelConfig {
modelId: string // e.g., "gpt-4o", "claude-sonnet-4-5"
validated?: boolean // Has this model been validated
validationError?: string // Error message if validation failed
+ validationWarning?: string // Passed, but e.g. did not call a tool
+ responseTime?: number // Milliseconds the last test took
}
// Provider configuration
@@ -120,22 +122,45 @@ export const PROVIDER_LOGO_MAP: Record = {
atlascloud: "openai",
}
-// Provider metadata
+/** How a provider lists its models (see lib/provider-models.ts) */
+export type ModelListStyle =
+ | "openai"
+ | "anthropic"
+ | "google"
+ | "ollama"
+ | "openrouter"
+ | "aihubmix"
+ | "gateway"
+
+// Provider metadata. apiKeyUrl is the page where users create a key.
+// modelList is missing where a key alone cannot list the models (Bedrock,
+// Vertex, Azure) or the list is not reliable (Doubao, MiniMax).
export const PROVIDER_INFO: Record<
ProviderName,
- { label: string; defaultBaseUrl?: string }
+ {
+ label: string
+ defaultBaseUrl?: string
+ apiKeyUrl?: string
+ modelList?: ModelListStyle
+ }
> = {
openai: {
label: "OpenAI",
defaultBaseUrl: "https://api.openai.com/v1",
+ apiKeyUrl: "https://platform.openai.com/api-keys",
+ modelList: "openai",
},
anthropic: {
label: "Anthropic",
defaultBaseUrl: "https://api.anthropic.com/v1",
+ apiKeyUrl: "https://platform.claude.com/settings/keys",
+ modelList: "anthropic",
},
google: {
label: "Google",
defaultBaseUrl: "https://generativelanguage.googleapis.com/v1beta",
+ apiKeyUrl: "https://aistudio.google.com/apikey",
+ modelList: "google",
},
vertexai: { label: "Google Vertex AI" },
azure: {
@@ -146,77 +171,114 @@ export const PROVIDER_INFO: Record<
ollama: {
label: "Ollama",
defaultBaseUrl: "https://ollama.com/api",
+ apiKeyUrl: "https://ollama.com/settings/keys",
+ modelList: "ollama",
},
openrouter: {
label: "OpenRouter",
defaultBaseUrl: "https://openrouter.ai/api/v1",
+ apiKeyUrl: "https://openrouter.ai/keys",
+ modelList: "openrouter",
},
aihubmix: {
label: "AIHubMix",
defaultBaseUrl: "https://aihubmix.com/v1",
+ apiKeyUrl: "https://aihubmix.com/token",
+ modelList: "aihubmix",
},
deepseek: {
label: "DeepSeek",
defaultBaseUrl: "https://api.deepseek.com/v1",
+ apiKeyUrl: "https://platform.deepseek.com/api_keys",
+ modelList: "openai",
},
siliconflow: {
label: "SiliconFlow",
defaultBaseUrl: "https://api.siliconflow.cn/v1",
+ apiKeyUrl: "https://cloud.siliconflow.cn/account/ak",
+ modelList: "openai",
},
sglang: {
label: "SGLang",
defaultBaseUrl: "http://127.0.0.1:8000/v1",
+ modelList: "openai",
},
gateway: {
label: "AI Gateway",
defaultBaseUrl: "https://ai-gateway.vercel.sh/v1/ai",
+ apiKeyUrl: "https://vercel.com/ai-gateway",
+ modelList: "gateway",
},
edgeone: { label: "EdgeOne Pages" },
doubao: {
label: "Doubao (ByteDance)",
defaultBaseUrl: "https://ark.cn-beijing.volces.com/api/v3",
+ apiKeyUrl:
+ "https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey",
},
modelscope: {
label: "ModelScope",
defaultBaseUrl: "https://api-inference.modelscope.cn/v1",
+ apiKeyUrl: "https://modelscope.cn/my/myaccesstoken",
+ modelList: "openai",
},
glm: {
label: "GLM (Zhipu)",
defaultBaseUrl: "https://open.bigmodel.cn/api/paas/v4",
+ apiKeyUrl: "https://open.bigmodel.cn/usercenter/proj-mgmt/apikeys",
+ modelList: "openai",
},
qwen: {
label: "Qwen (Alibaba)",
defaultBaseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
+ apiKeyUrl: "https://bailian.console.aliyun.com/?tab=model#/api-key",
+ modelList: "openai",
},
qiniu: {
label: "Qiniu",
defaultBaseUrl: "https://api.qnaigc.com/v1",
+ apiKeyUrl: "https://www.qiniu.com/ai/models",
+ modelList: "openai",
},
kimi: {
label: "Kimi (Moonshot)",
defaultBaseUrl: "https://api.moonshot.cn/v1",
+ apiKeyUrl: "https://platform.moonshot.cn/console/api-keys",
+ modelList: "openai",
},
minimax: {
label: "MiniMax",
defaultBaseUrl: "https://api.minimaxi.com/anthropic",
+ apiKeyUrl:
+ "https://platform.minimaxi.com/user-center/basic-information/interface-key",
},
novita: {
label: "Novita AI",
defaultBaseUrl: "https://api.novita.ai/openai",
+ apiKeyUrl: "https://novita.ai/dashboard/key",
+ modelList: "openai",
},
mimo: {
label: "MiMo (Xiaomi)",
defaultBaseUrl: "https://api.xiaomimimo.com/v1",
+ apiKeyUrl: "https://platform.xiaomimimo.com/#/console/api-keys",
+ modelList: "openai",
},
atlascloud: {
label: "Atlas Cloud",
defaultBaseUrl: "https://api.atlascloud.ai/v1",
+ apiKeyUrl: "https://www.atlascloud.ai/console/api-keys",
+ modelList: "openai",
},
}
// Suggested models per provider for quick add
export const SUGGESTED_MODELS: Partial> = {
openai: [
+ "gpt-6.1-sol",
+ "gpt-6-sol",
+ "gpt-6-luna",
+ "gpt-6-astra",
"gpt-5.5-pro",
"gpt-5.5",
"gpt-5.4-pro",
@@ -230,7 +292,13 @@ export const SUGGESTED_MODELS: Partial> = {
"gpt-4o-mini",
],
anthropic: [
- // Claude 4.8 / 4.7 / 4.6 series (latest, dateless pinned IDs)
+ // Claude 5 series (latest)
+ "claude-opus-5-5",
+ "claude-sonnet-5-5",
+ "claude-fable-5-1",
+ "claude-opus-5",
+ "claude-sonnet-5",
+ // Claude 4.8 / 4.7 / 4.6 series (dateless pinned IDs)
"claude-opus-4-8",
"claude-sonnet-4-6",
"claude-haiku-4-5",
@@ -279,39 +347,47 @@ export const SUGGESTED_MODELS: Partial> = {
"o3",
"o4-mini",
],
+ // Newer models only answer through an inference profile id (the region
+ // prefix). Each id here was called once on 2026-10-04.
bedrock: [
- // Anthropic Claude
- "anthropic.claude-opus-4-8",
- "anthropic.claude-opus-4-7",
- "anthropic.claude-sonnet-4-6",
- "anthropic.claude-opus-4-6-v1",
- "anthropic.claude-opus-4-5-20251101-v1:0",
- "anthropic.claude-sonnet-4-5-20250929-v1:0",
- "anthropic.claude-haiku-4-5-20251001-v1:0",
- "anthropic.claude-opus-4-1-20250805-v1:0",
- "anthropic.claude-opus-4-20250514-v1:0",
- "anthropic.claude-sonnet-4-20250514-v1:0",
- "anthropic.claude-3-5-haiku-20241022-v1:0",
+ // Anthropic Claude ("global." works from any region)
+ "global.anthropic.claude-opus-5-5",
+ "global.anthropic.claude-sonnet-5-5",
+ "global.anthropic.claude-fable-5-1",
+ "global.anthropic.claude-opus-5",
+ "global.anthropic.claude-sonnet-5",
+ "global.anthropic.claude-opus-4-8",
+ "global.anthropic.claude-opus-4-7",
+ "global.anthropic.claude-sonnet-4-6",
+ "global.anthropic.claude-opus-4-6-v1",
+ "global.anthropic.claude-opus-4-5-20251101-v1:0",
+ "global.anthropic.claude-sonnet-4-5-20250929-v1:0",
+ "global.anthropic.claude-haiku-4-5-20251001-v1:0",
+ "global.anthropic.claude-sonnet-4-20250514-v1:0",
// Amazon Nova
- "amazon.nova-2-lite-v1:0",
- "amazon.nova-premier-v1:0",
+ "us.amazon.nova-2-lite-v1:0",
"amazon.nova-pro-v1:0",
"amazon.nova-lite-v1:0",
"amazon.nova-micro-v1:0",
// Meta Llama
- "meta.llama4-maverick-17b-instruct-v1:0",
- "meta.llama4-scout-17b-instruct-v1:0",
- "meta.llama3-3-70b-instruct-v1:0",
+ "us.meta.llama4-maverick-17b-instruct-v1:0",
+ "us.meta.llama4-scout-17b-instruct-v1:0",
+ "us.meta.llama3-3-70b-instruct-v1:0",
// Mistral
"mistral.mistral-large-3-675b-instruct",
- "mistral.pixtral-large-2502-v1:0",
+ "us.mistral.pixtral-large-2502-v1:0",
],
openrouter: [
// Anthropic
+ "anthropic/claude-opus-5.5",
+ "anthropic/claude-sonnet-5.5",
+ "anthropic/claude-fable-5.1",
"anthropic/claude-opus-4.8",
"anthropic/claude-sonnet-4.6",
"anthropic/claude-haiku-4.5",
// OpenAI
+ "openai/gpt-6.1-sol",
+ "openai/gpt-6-luna",
"openai/gpt-5.5",
"openai/gpt-5.4",
"openai/gpt-5.4-mini",
@@ -400,6 +476,10 @@ export const SUGGESTED_MODELS: Partial> = {
"default",
],
gateway: [
+ "anthropic/claude-opus-5.5",
+ "anthropic/claude-sonnet-5.5",
+ "openai/gpt-6.1-sol",
+ "openai/gpt-6-luna",
"openai/gpt-5.5",
"anthropic/claude-opus-4.7",
"google/gemini-3.1-pro-preview",
@@ -529,3 +609,38 @@ export function findModelById(
): FlattenedModel | undefined {
return flattenModels(config).find((m) => m.id === modelId)
}
+
+/**
+ * A base URL the way the SDKs expect it: no spaces, no trailing slash, and
+ * no endpoint path users often paste along (".../v1/chat/completions"),
+ * which the SDK would append a second time.
+ */
+export function normalizeBaseUrl(url: string): string {
+ return url
+ .trim()
+ .replace(/\/+$/, "")
+ .replace(/\/(?:chat\/completions|completions|messages|responses)$/, "")
+}
+
+/** Where a chat request goes for a base URL, or null when the SDK decides */
+export function chatRequestUrl(
+ provider: ProviderName,
+ baseUrl: string,
+): string | null {
+ const url = normalizeBaseUrl(baseUrl)
+ if (!url) return null
+ if (provider === "anthropic") return `${url}/messages`
+ // These SDKs build their own paths (or, for MiniMax, pick the protocol
+ // from the URL)
+ const ownPaths: ProviderName[] = [
+ "google",
+ "vertexai",
+ "azure",
+ "bedrock",
+ "ollama",
+ "gateway",
+ "minimax",
+ "edgeone",
+ ]
+ return ownPaths.includes(provider) ? null : `${url}/chat/completions`
+}
diff --git a/lib/url-utils.ts b/lib/url-utils.ts
index e0f0010b..d0b774f4 100644
--- a/lib/url-utils.ts
+++ b/lib/url-utils.ts
@@ -1,5 +1,6 @@
import { z } from "zod"
import { getApiEndpoint } from "@/lib/base-path"
+import { STORAGE_KEYS } from "@/lib/storage"
export interface UrlData {
url: string
@@ -18,7 +19,11 @@ const UrlResponseSchema = z.object({
export async function extractUrlContent(url: string): Promise {
const response = await fetch(getApiEndpoint("/api/parse-url"), {
method: "POST",
- headers: { "Content-Type": "application/json" },
+ headers: {
+ "Content-Type": "application/json",
+ "x-access-code":
+ localStorage.getItem(STORAGE_KEYS.accessCode) || "",
+ },
body: JSON.stringify({ url }),
})
diff --git a/lib/use-file-processor.tsx b/lib/use-file-processor.tsx
index 5b2622ce..3f86d076 100644
--- a/lib/use-file-processor.tsx
+++ b/lib/use-file-processor.tsx
@@ -27,78 +27,72 @@ export function useFileProcessor() {
const handleFileChange = async (newFiles: File[]) => {
setFiles(newFiles)
- // Extract text immediately for new PDF/text files
- for (const file of newFiles) {
- const needsExtraction =
- (isPdfFile(file) || isTextFile(file)) && !pdfData.has(file)
- if (needsExtraction) {
- // Mark as extracting
- setPdfData((prev) => {
- const next = new Map(prev)
- next.set(file, {
- text: "",
- charCount: 0,
- isExtracting: true,
- })
- return next
- })
+ const pending = newFiles.filter(
+ (file) =>
+ (isPdfFile(file) || isTextFile(file)) && !pdfData.has(file),
+ )
- // Extract text asynchronously
- try {
- let text: string
- if (isPdfFile(file)) {
- text = await extractPdfText(file)
- } else {
- text = await extractTextFileContent(file)
- }
+ // Before any await: drop data for removed files and mark every new
+ // file as extracting, so queued files also block sending
+ setPdfData((prev) => {
+ const next = new Map()
+ for (const file of newFiles) {
+ const existing = prev.get(file)
+ if (existing) next.set(file, existing)
+ }
+ for (const file of pending) {
+ next.set(file, { text: "", charCount: 0, isExtracting: true })
+ }
+ return next
+ })
- // Check character limit
- if (text.length > MAX_EXTRACTED_CHARS) {
- const limitK = MAX_EXTRACTED_CHARS / 1000
- toast.error(
- `${file.name}: Content exceeds ${limitK}k character limit (${(text.length / 1000).toFixed(1)}k chars)`,
- )
- setPdfData((prev) => {
- const next = new Map(prev)
- next.delete(file)
- return next
- })
- // Remove the file from the list
- setFiles((prev) => prev.filter((f) => f !== file))
- continue
- }
+ // Extract one file at a time
+ for (const file of pending) {
+ try {
+ let text: string
+ if (isPdfFile(file)) {
+ text = await extractPdfText(file)
+ } else {
+ text = await extractTextFileContent(file)
+ }
- setPdfData((prev) => {
- const next = new Map(prev)
- next.set(file, {
- text,
- charCount: text.length,
- isExtracting: false,
- })
- return next
- })
- } catch (error) {
- console.error("Failed to extract text:", error)
- toast.error(`Failed to read file: ${file.name}`)
+ // Check character limit
+ if (text.length > MAX_EXTRACTED_CHARS) {
+ const limitK = MAX_EXTRACTED_CHARS / 1000
+ toast.error(
+ `${file.name}: Content exceeds ${limitK}k character limit (${(text.length / 1000).toFixed(1)}k chars)`,
+ )
setPdfData((prev) => {
const next = new Map(prev)
next.delete(file)
return next
})
+ // Remove the file from the list
+ setFiles((prev) => prev.filter((f) => f !== file))
+ continue
}
+
+ setPdfData((prev) => {
+ // The file was removed while extracting
+ if (!prev.has(file)) return prev
+ const next = new Map(prev)
+ next.set(file, {
+ text,
+ charCount: text.length,
+ isExtracting: false,
+ })
+ return next
+ })
+ } catch (error) {
+ console.error("Failed to extract text:", error)
+ toast.error(`Failed to read file: ${file.name}`)
+ setPdfData((prev) => {
+ const next = new Map(prev)
+ next.delete(file)
+ return next
+ })
}
}
-
- // Clean up pdfData for removed files
- setPdfData((prev) => {
- const next = new Map(prev)
- for (const key of prev.keys()) {
- if (!newFiles.includes(key)) {
- next.delete(key)
- }
- }
- return next
- })
}
return {
diff --git a/lib/utils.ts b/lib/utils.ts
index 2c1f8b43..a4749b92 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -1,9 +1,7 @@
import { type ClassValue, clsx } from "clsx"
import * as pako from "pako"
import { twMerge } from "tailwind-merge"
-import type { DiagramOperation } from "@/components/chat/types"
-
-export type { DiagramOperation }
+import { hasCells } from "@/packages/mcp-server/src/pages.ts"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
@@ -20,37 +18,16 @@ export function cn(...inputs: ClassValue[]) {
export const MIN_REAL_DIAGRAM_LENGTH = 300
/**
- * Check if diagram XML represents a real diagram (not just empty template).
+ * Check if diagram XML represents a real diagram (not just empty template):
+ * it has a shape (however short), or is long enough to hold pages worth
+ * keeping.
* @param xml - The diagram XML string to check
* @returns true if the XML is a real diagram with content
*/
export function isRealDiagram(xml: string | undefined | null): boolean {
- return !!xml && xml.length > MIN_REAL_DIAGRAM_LENGTH
+ return !!xml && (hasCells(xml) || xml.length > MIN_REAL_DIAGRAM_LENGTH)
}
-// ============================================================================
-// XML Validation/Fix Constants
-// ============================================================================
-
-/** Maximum XML size to process (1MB) - larger XMLs may cause performance issues */
-const MAX_XML_SIZE = 1_000_000
-
-/** Maximum iterations for aggressive cell dropping to prevent infinite loops */
-const MAX_DROP_ITERATIONS = 10
-
-/** Structural attributes that should not be duplicated in draw.io */
-const STRUCTURAL_ATTRS = [
- "edge",
- "parent",
- "source",
- "target",
- "vertex",
- "connectable",
-]
-
-/** Valid XML entity names */
-const VALID_ENTITIES = new Set(["lt", "gt", "amp", "quot", "apos"])
-
// ============================================================================
// mxCell XML Helpers
// ============================================================================
@@ -76,6 +53,20 @@ export function isMxCellXmlComplete(xml: string | undefined | null): boolean {
// No valid ending found at all
if (lastValidEnd === -1) return false
+ // If the last mxCell has no after it, it must be self-closing.
+ // Otherwise the trailing "/>" belongs to a child such as
+ // and the output was cut off before the cell was closed.
+ const lastCellStart = trimmed.lastIndexOf(" lastMxCellClose &&
+ // (quoted values may hold a raw "<", which the auto-fix escapes)
+ !/^"']|"[^"]*"|'[^']*')*\/>/.test(
+ trimmed.slice(lastCellStart),
+ )
+ ) {
+ return false
+ }
+
// Check what comes after the last valid ending
// For />: add 2 chars, for : add 9 chars
const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
@@ -95,102 +86,12 @@ export function isMxCellXmlComplete(xml: string | undefined | null): boolean {
export function extractCompleteMxCells(xml: string | undefined | null): string {
if (!xml) return ""
- const completeCells: Array<{ index: number; text: string }> = []
+ // Match self-closing or ... , in document order.
+ // The lazy [^>]*? tries "/>" first, so a self-closing cell never swallows
+ // the following cells up to the next .
+ const cellPattern = /]*?(?:\/>|>[\s\S]*?<\/mxCell>)/g
- // Match self-closing mxCell tags:
- // Also match mxCell with nested mxGeometry: ...
- const selfClosingPattern = /]*\/>/g
- const nestedPattern = /]*>[\s\S]*?<\/mxCell>/g
-
- // Find all self-closing mxCell elements
- let match: RegExpExecArray | null
- while ((match = selfClosingPattern.exec(xml)) !== null) {
- completeCells.push({ index: match.index, text: match[0] })
- }
-
- // Find all mxCell elements with nested content (like mxGeometry)
- while ((match = nestedPattern.exec(xml)) !== null) {
- completeCells.push({ index: match.index, text: match[0] })
- }
-
- // Sort by position to maintain order
- completeCells.sort((a, b) => a.index - b.index)
-
- // Remove duplicates (a self-closing match might overlap with nested match)
- const seen = new Set()
- const uniqueCells = completeCells.filter((cell) => {
- if (seen.has(cell.index)) return false
- seen.add(cell.index)
- return true
- })
-
- return uniqueCells.map((c) => c.text).join("\n")
-}
-
-// ============================================================================
-// XML Parsing Helpers
-// ============================================================================
-
-interface ParsedTag {
- tag: string
- tagName: string
- isClosing: boolean
- isSelfClosing: boolean
- startIndex: number
- endIndex: number
-}
-
-/**
- * Parse XML tags while properly handling quoted strings
- * This is a shared utility used by both validation and fixing logic
- */
-function parseXmlTags(xml: string): ParsedTag[] {
- const tags: ParsedTag[] = []
- let i = 0
-
- while (i < xml.length) {
- const tagStart = xml.indexOf("<", i)
- if (tagStart === -1) break
-
- // Find matching > by tracking quotes
- let tagEnd = tagStart + 1
- let inQuote = false
- let quoteChar = ""
-
- while (tagEnd < xml.length) {
- const c = xml[tagEnd]
- if (inQuote) {
- if (c === quoteChar) inQuote = false
- } else {
- if (c === '"' || c === "'") {
- inQuote = true
- quoteChar = c
- } else if (c === ">") {
- break
- }
- }
- tagEnd++
- }
-
- if (tagEnd >= xml.length) break
-
- const tag = xml.substring(tagStart, tagEnd + 1)
- i = tagEnd + 1
-
- const tagMatch = /^<(\/?)([a-zA-Z][a-zA-Z0-9:_-]*)/.exec(tag)
- if (!tagMatch) continue
-
- tags.push({
- tag,
- tagName: tagMatch[2],
- isClosing: tagMatch[1] === "/",
- isSelfClosing: tag.endsWith("/>"),
- startIndex: tagStart,
- endIndex: tagEnd,
- })
- }
-
- return tags
+ return (xml.match(cellPattern) || []).join("\n")
}
/**
@@ -315,61 +216,6 @@ export function convertToLegalXml(xmlString: string): string {
return result
}
-/**
- * Wrap XML content with the full mxfile structure required by draw.io.
- * Always adds root cells (id="0" and id="1") automatically.
- * If input already contains root cells, they are removed to avoid duplication.
- * LLM should only generate mxCell elements starting from id="2".
- * @param xml - The XML string (bare mxCells, , , or full )
- * @returns Full mxfile-wrapped XML string with root cells included
- */
-export function wrapWithMxFile(xml: string): string {
- const ROOT_CELLS = ' '
-
- if (!xml || !xml.trim()) {
- return `${ROOT_CELLS} `
- }
-
- // Already has full structure
- if (xml.includes("${xml} `
- }
-
- // Has wrapper - extract inner content
- let content = xml
- if (xml.includes("")) {
- content = xml.replace(/<\/?root>/g, "").trim()
- }
-
- // Strip trailing LLM wrapper tags (from any provider: Anthropic, DeepSeek, etc.)
- // Find the last valid mxCell ending and remove everything after it
- const lastSelfClose = content.lastIndexOf("/>")
- const lastMxCellClose = content.lastIndexOf(" ")
- const lastValidEnd = Math.max(lastSelfClose, lastMxCellClose)
- if (lastValidEnd !== -1) {
- const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
- const suffix = content.slice(lastValidEnd + endOffset)
- // If suffix is only closing tags (wrapper tags), strip it
- if (/^(\s*<\/[^>]+>)*\s*$/.test(suffix)) {
- content = content.slice(0, lastValidEnd + endOffset)
- }
- }
-
- // Remove any existing root cells from content (LLM shouldn't include them, but handle it gracefully)
- // Use flexible patterns that match both self-closing (/>) and non-self-closing (> ) formats
- content = content
- .replace(/]*\bid=["']0["'][^>]*(?:\/>|><\/mxCell>)/g, "")
- .replace(/]*\bid=["']1["'][^>]*(?:\/>|><\/mxCell>)/g, "")
- .trim()
-
- return `${ROOT_CELLS}${content} `
-}
-
/**
* Replace nodes in a Draw.io XML diagram
* @param currentXML - The original Draw.io XML string
@@ -472,1207 +318,11 @@ export function replaceNodes(currentXML: string, nodes: string): string {
}
}
-// ============================================================================
-// ID-based Diagram Operations
-// ============================================================================
-
-export interface OperationError {
- type: "update" | "add" | "delete"
- cellId: string
- message: string
-}
-
-export interface ApplyOperationsResult {
- result: string
- errors: OperationError[]
-}
-
/**
- * Apply diagram operations (update/add/delete) using ID-based lookup.
- * This replaces the text-matching approach with direct DOM manipulation.
- *
- * @param xmlContent - The full mxfile XML content
- * @param operations - Array of operations to apply
- * @returns Object with result XML and any errors
+ * Decode an xmlsvg export (SVG data URL) into uncompressed diagram XML.
+ * Only the first page is returned; for the full multi-page document use the
+ * autosaved chartXML instead.
*/
-export function applyDiagramOperations(
- xmlContent: string,
- operations: DiagramOperation[],
-): ApplyOperationsResult {
- const errors: OperationError[] = []
-
- // Parse the XML
- const parser = new DOMParser()
- const doc = parser.parseFromString(xmlContent, "text/xml")
-
- // Check for parse errors
- const parseError = doc.querySelector("parsererror")
- if (parseError) {
- return {
- result: xmlContent,
- errors: [
- {
- type: "update",
- cellId: "",
- message: `XML parse error: ${parseError.textContent}`,
- },
- ],
- }
- }
-
- // Find the root element (inside mxGraphModel)
- const root = doc.querySelector("root")
- if (!root) {
- return {
- result: xmlContent,
- errors: [
- {
- type: "update",
- cellId: "",
- message: "Could not find element in XML",
- },
- ],
- }
- }
-
- // Build a map of cell IDs to elements
- const cellMap = new Map()
- root.querySelectorAll("mxCell").forEach((cell) => {
- const id = cell.getAttribute("id")
- if (id) cellMap.set(id, cell)
- })
-
- // Process each operation
- for (const op of operations) {
- if (op.operation === "update") {
- const existingCell = cellMap.get(op.cell_id)
- if (!existingCell) {
- errors.push({
- type: "update",
- cellId: op.cell_id,
- message: `Cell with id="${op.cell_id}" not found`,
- })
- continue
- }
-
- if (!op.new_xml) {
- errors.push({
- type: "update",
- cellId: op.cell_id,
- message: "new_xml is required for update operation",
- })
- continue
- }
-
- // Parse the new XML
- const newDoc = parser.parseFromString(
- `${op.new_xml} `,
- "text/xml",
- )
- const newCell = newDoc.querySelector("mxCell")
- if (!newCell) {
- errors.push({
- type: "update",
- cellId: op.cell_id,
- message: "new_xml must contain an mxCell element",
- })
- continue
- }
-
- // Validate ID matches
- const newCellId = newCell.getAttribute("id")
- if (newCellId !== op.cell_id) {
- errors.push({
- type: "update",
- cellId: op.cell_id,
- message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
- })
- continue
- }
-
- // Import and replace the node
- const importedNode = doc.importNode(newCell, true)
- existingCell.parentNode?.replaceChild(importedNode, existingCell)
-
- // Update the map with the new element
- cellMap.set(op.cell_id, importedNode)
- } else if (op.operation === "add") {
- // Check if ID already exists
- if (cellMap.has(op.cell_id)) {
- errors.push({
- type: "add",
- cellId: op.cell_id,
- message: `Cell with id="${op.cell_id}" already exists`,
- })
- continue
- }
-
- if (!op.new_xml) {
- errors.push({
- type: "add",
- cellId: op.cell_id,
- message: "new_xml is required for add operation",
- })
- continue
- }
-
- // Parse the new XML
- const newDoc = parser.parseFromString(
- `${op.new_xml} `,
- "text/xml",
- )
- const newCell = newDoc.querySelector("mxCell")
- if (!newCell) {
- errors.push({
- type: "add",
- cellId: op.cell_id,
- message: "new_xml must contain an mxCell element",
- })
- continue
- }
-
- // Validate ID matches
- const newCellId = newCell.getAttribute("id")
- if (newCellId !== op.cell_id) {
- errors.push({
- type: "add",
- cellId: op.cell_id,
- message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
- })
- continue
- }
-
- // Import and append the node
- const importedNode = doc.importNode(newCell, true)
- root.appendChild(importedNode)
-
- // Add to map
- cellMap.set(op.cell_id, importedNode)
- } else if (op.operation === "delete") {
- // Protect root cells from deletion
- if (op.cell_id === "0" || op.cell_id === "1") {
- errors.push({
- type: "delete",
- cellId: op.cell_id,
- message: `Cannot delete root cell "${op.cell_id}"`,
- })
- continue
- }
-
- const existingCell = cellMap.get(op.cell_id)
- if (!existingCell) {
- // Cell not found - might have been cascade-deleted by a previous operation
- // Skip silently instead of erroring (AI may redundantly list children/edges)
- continue
- }
-
- // Cascade delete: collect all cells to delete (children + edges + self)
- const cellsToDelete = new Set()
-
- // Recursive function to find all descendants
- const collectDescendants = (cellId: string) => {
- if (cellsToDelete.has(cellId)) return
- cellsToDelete.add(cellId)
-
- // Find children (cells where parent === cellId)
- const children = root.querySelectorAll(
- `mxCell[parent="${cellId}"]`,
- )
- children.forEach((child) => {
- const childId = child.getAttribute("id")
- if (childId && childId !== "0" && childId !== "1") {
- collectDescendants(childId)
- }
- })
- }
-
- // Collect the target cell and all its descendants
- collectDescendants(op.cell_id)
-
- // Find edges referencing any of the cells to be deleted
- // Also recursively collect children of those edges (e.g., edge labels)
- for (const cellId of cellsToDelete) {
- const referencingEdges = root.querySelectorAll(
- `mxCell[source="${cellId}"], mxCell[target="${cellId}"]`,
- )
- referencingEdges.forEach((edge) => {
- const edgeId = edge.getAttribute("id")
- // Protect root cells from being added via edge references
- if (edgeId && edgeId !== "0" && edgeId !== "1") {
- // Recurse to collect edge's children (like labels)
- collectDescendants(edgeId)
- }
- })
- }
-
- // Log what will be deleted
- if (cellsToDelete.size > 1) {
- console.log(
- `[applyDiagramOperations] Cascade delete "${op.cell_id}" → deleting ${cellsToDelete.size} cells: ${Array.from(cellsToDelete).join(", ")}`,
- )
- }
-
- // Delete all collected cells
- for (const cellId of cellsToDelete) {
- const cell = cellMap.get(cellId)
- if (cell) {
- cell.parentNode?.removeChild(cell)
- cellMap.delete(cellId)
- }
- }
- }
- }
-
- // Serialize back to string
- const serializer = new XMLSerializer()
- const result = serializer.serializeToString(doc)
-
- return { result, errors }
-}
-
-// ============================================================================
-// Validation Helper Functions
-// ============================================================================
-
-/** Check for duplicate structural attributes in a tag */
-function checkDuplicateAttributes(xml: string): string | null {
- const structuralSet = new Set(STRUCTURAL_ATTRS)
- const tagPattern = /<[^>]+>/g
- let tagMatch
- while ((tagMatch = tagPattern.exec(xml)) !== null) {
- const tag = tagMatch[0]
- const attrPattern = /\s([a-zA-Z_:][a-zA-Z0-9_:.-]*)\s*=/g
- const attributes = new Map()
- let attrMatch
- while ((attrMatch = attrPattern.exec(tag)) !== null) {
- const attrName = attrMatch[1]
- attributes.set(attrName, (attributes.get(attrName) || 0) + 1)
- }
- const duplicates = Array.from(attributes.entries())
- .filter(([name, count]) => count > 1 && structuralSet.has(name))
- .map(([name]) => name)
- if (duplicates.length > 0) {
- return `Invalid XML: Duplicate structural attribute(s): ${duplicates.join(", ")}. Remove duplicate attributes.`
- }
- }
- return null
-}
-
-/** Check for duplicate IDs in XML */
-function checkDuplicateIds(xml: string): string | null {
- const idPattern = /\bid\s*=\s*["']([^"']+)["']/gi
- const ids = new Map()
- let idMatch
- while ((idMatch = idPattern.exec(xml)) !== null) {
- const id = idMatch[1]
- ids.set(id, (ids.get(id) || 0) + 1)
- }
- const duplicateIds = Array.from(ids.entries())
- .filter(([, count]) => count > 1)
- .map(([id, count]) => `'${id}' (${count}x)`)
- if (duplicateIds.length > 0) {
- return `Invalid XML: Found duplicate ID(s): ${duplicateIds.slice(0, 3).join(", ")}. All id attributes must be unique.`
- }
- return null
-}
-
-/** Check for tag mismatches using parsed tags */
-function checkTagMismatches(xml: string): string | null {
- const xmlWithoutComments = xml.replace(//g, "")
- const tags = parseXmlTags(xmlWithoutComments)
- const tagStack: string[] = []
-
- for (const { tagName, isClosing, isSelfClosing } of tags) {
- if (isClosing) {
- if (tagStack.length === 0) {
- return `Invalid XML: Closing tag ${tagName}> without matching opening tag`
- }
- const expected = tagStack.pop()
- if (expected?.toLowerCase() !== tagName.toLowerCase()) {
- return `Invalid XML: Expected closing tag ${expected}> but found ${tagName}>`
- }
- } else if (!isSelfClosing) {
- tagStack.push(tagName)
- }
- }
- if (tagStack.length > 0) {
- return `Invalid XML: Document has ${tagStack.length} unclosed tag(s): ${tagStack.join(", ")}`
- }
- return null
-}
-
-/** Check for invalid character references */
-function checkCharacterReferences(xml: string): string | null {
- const charRefPattern = /?[^;]+;?/g
- let charMatch
- while ((charMatch = charRefPattern.exec(xml)) !== null) {
- const ref = charMatch[0]
- if (ref.startsWith("")) {
- if (!ref.endsWith(";")) {
- return `Invalid XML: Missing semicolon after hex reference: ${ref}`
- }
- const hexDigits = ref.substring(3, ref.length - 1)
- if (hexDigits.length === 0 || !/^[0-9a-fA-F]+$/.test(hexDigits)) {
- return `Invalid XML: Invalid hex character reference: ${ref}`
- }
- } else if (ref.startsWith("")) {
- if (!ref.endsWith(";")) {
- return `Invalid XML: Missing semicolon after decimal reference: ${ref}`
- }
- const decDigits = ref.substring(2, ref.length - 1)
- if (decDigits.length === 0 || !/^[0-9]+$/.test(decDigits)) {
- return `Invalid XML: Invalid decimal character reference: ${ref}`
- }
- }
- }
- return null
-}
-
-/** Check for invalid entity references */
-function checkEntityReferences(xml: string): string | null {
- const xmlWithoutComments = xml.replace(//g, "")
- const bareAmpPattern = /&(?!(?:lt|gt|amp|quot|apos|#))/g
- if (bareAmpPattern.test(xmlWithoutComments)) {
- return "Invalid XML: Found unescaped & character(s). Replace & with &"
- }
- const invalidEntityPattern = /&([a-zA-Z][a-zA-Z0-9]*);/g
- let entityMatch
- while (
- (entityMatch = invalidEntityPattern.exec(xmlWithoutComments)) !== null
- ) {
- if (!VALID_ENTITIES.has(entityMatch[1])) {
- return `Invalid XML: Invalid entity reference: &${entityMatch[1]}; - use only valid XML entities (lt, gt, amp, quot, apos)`
- }
- }
- return null
-}
-
-/** Check for nested mxCell tags using regex */
-function checkNestedMxCells(xml: string): string | null {
- const cellTagPattern = /<\/?mxCell[^>]*>/g
- const cellStack: number[] = []
- let cellMatch
- while ((cellMatch = cellTagPattern.exec(xml)) !== null) {
- const tag = cellMatch[0]
- if (tag.startsWith(" ")) {
- if (cellStack.length > 0) cellStack.pop()
- } else if (!tag.endsWith("/>")) {
- const isLabelOrGeometry =
- /\sas\s*=\s*["'](valueLabel|geometry)["']/.test(tag)
- if (!isLabelOrGeometry) {
- cellStack.push(cellMatch.index)
- if (cellStack.length > 1) {
- return "Invalid XML: Found nested mxCell tags. Cells should be siblings, not nested inside other mxCell elements."
- }
- }
- }
- }
- return null
-}
-
-/**
- * Validates draw.io XML structure for common issues
- * Uses DOM parsing + additional regex checks for high accuracy
- * @param xml - The XML string to validate
- * @returns null if valid, error message string if invalid
- */
-export function validateMxCellStructure(xml: string): string | null {
- // Size check for performance
- if (xml.length > MAX_XML_SIZE) {
- console.warn(
- `[validateMxCellStructure] XML size (${xml.length}) exceeds ${MAX_XML_SIZE} bytes, may cause performance issues`,
- )
- }
-
- // 0. First use DOM parser to catch syntax errors (most accurate)
- try {
- const parser = new DOMParser()
- const doc = parser.parseFromString(xml, "text/xml")
- const parseError = doc.querySelector("parsererror")
- if (parseError) {
- return `Invalid XML: The XML contains syntax errors (likely unescaped special characters like <, >, & in attribute values). Please escape special characters: use < for <, > for >, & for &, " for ". Regenerate the diagram with properly escaped values.`
- }
-
- // DOM-based checks for nested mxCell
- const allCells = doc.querySelectorAll("mxCell")
- for (const cell of allCells) {
- if (cell.parentElement?.tagName === "mxCell") {
- const id = cell.getAttribute("id") || "unknown"
- return `Invalid XML: Found nested mxCell (id="${id}"). Cells should be siblings, not nested inside other mxCell elements.`
- }
- }
- } catch (error) {
- // Log unexpected DOMParser errors before falling back to regex checks
- console.warn(
- "[validateMxCellStructure] DOMParser threw unexpected error, falling back to regex validation:",
- error,
- )
- }
-
- // 1. Check for CDATA wrapper (invalid at document root)
- if (/^\s* from end"
- }
-
- // 2. Check for duplicate structural attributes
- const dupAttrError = checkDuplicateAttributes(xml)
- if (dupAttrError) {
- return dupAttrError
- }
-
- // 3. Check for unescaped < in attribute values
- const attrValuePattern = /=\s*"([^"]*)"/g
- let attrValMatch
- while ((attrValMatch = attrValuePattern.exec(xml)) !== null) {
- const value = attrValMatch[1]
- if (//g
- let commentMatch
- while ((commentMatch = commentPattern.exec(xml)) !== null) {
- if (/--/.test(commentMatch[1])) {
- return "Invalid XML: Comment contains -- (double hyphen) which is not allowed"
- }
- }
-
- // 8. Check for unescaped entity references and invalid entity names
- const entityError = checkEntityReferences(xml)
- if (entityError) {
- return entityError
- }
-
- // 9. Check for empty id attributes on mxCell
- if (/]*\sid\s*=\s*["']\s*["'][^>]*>/g.test(xml)) {
- return "Invalid XML: Found mxCell element(s) with empty id attribute"
- }
-
- // 10. Check for nested mxCell tags
- const nestedCellError = checkNestedMxCells(xml)
- if (nestedCellError) {
- return nestedCellError
- }
-
- return null
-}
-
-/**
- * Attempts to auto-fix common XML issues in draw.io diagrams
- * @param xml - The XML string to fix
- * @returns Object with fixed XML and list of fixes applied
- */
-export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
- let fixed = xml
- const fixes: string[] = []
-
- // 0. Fix JSON-escaped XML (common when XML is stored in JSON without unescaping)
- // Only apply when we see JSON-escaped attribute patterns like =\"value\"
- // Don't apply to legitimate \n in value attributes (draw.io uses these for line breaks)
- if (/=\\"/.test(fixed)) {
- // Replace literal \" with actual quotes
- fixed = fixed.replace(/\\"/g, '"')
- // Replace literal \n with actual newlines (only after confirming JSON-escaped)
- fixed = fixed.replace(/\\n/g, "\n")
- fixes.push("Fixed JSON-escaped XML")
- }
-
- // 1. Remove CDATA wrapper (MUST be before text-before-root check)
- if (/^\s*\s*$/, "")
- fixes.push("Removed CDATA wrapper")
- }
-
- // 1b. Strip trailing LLM wrapper tags (DeepSeek, Anthropic, etc.)
- // These are closing tags after the last valid mxCell that break XML parsing
- const lastSelfClose = fixed.lastIndexOf("/>")
- const lastMxCellClose = fixed.lastIndexOf(" ")
- const lastValidEnd = Math.max(lastSelfClose, lastMxCellClose)
- if (lastValidEnd !== -1) {
- const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
- const suffix = fixed.slice(lastValidEnd + endOffset)
- // If suffix contains only closing tags (wrapper tags) or whitespace, strip it
- if (/^(\s*<\/[^>]+>)+\s*$/.test(suffix)) {
- fixed = fixed.slice(0, lastValidEnd + endOffset)
- fixes.push("Stripped trailing LLM wrapper tags")
- }
- }
-
- // 2. Remove text before XML declaration or root element (only if it's garbage text, not valid XML)
- const xmlStart = fixed.search(/<(\?xml|mxGraphModel|mxfile)/i)
- if (xmlStart > 0 && !/^<[a-zA-Z]/.test(fixed.trim())) {
- fixed = fixed.substring(xmlStart)
- fixes.push("Removed text before XML root")
- }
-
- // 2. Fix duplicate attributes (keep first occurrence, remove duplicates)
- let dupAttrFixed = false
- fixed = fixed.replace(/<[^>]+>/g, (tag) => {
- let newTag = tag
-
- for (const attr of STRUCTURAL_ATTRS) {
- // Find all occurrences of this attribute
- const attrRegex = new RegExp(
- `\\s${attr}\\s*=\\s*["'][^"']*["']`,
- "gi",
- )
- const matches = tag.match(attrRegex)
-
- if (matches && matches.length > 1) {
- // Keep first, remove others
- let firstKept = false
- newTag = newTag.replace(attrRegex, (m) => {
- if (!firstKept) {
- firstKept = true
- return m
- }
- dupAttrFixed = true
- return ""
- })
- }
- }
- return newTag
- })
- if (dupAttrFixed) {
- fixes.push("Removed duplicate structural attributes")
- }
-
- // 3. Fix unescaped & characters (but not valid entities)
- // Match & not followed by valid entity pattern
- const ampersandPattern =
- /&(?!(?:lt|gt|amp|quot|apos|#[0-9]+|#x[0-9a-fA-F]+);)/g
- if (ampersandPattern.test(fixed)) {
- fixed = fixed.replace(
- /&(?!(?:lt|gt|amp|quot|apos|#[0-9]+|#x[0-9a-fA-F]+);)/g,
- "&",
- )
- fixes.push("Escaped unescaped & characters")
- }
-
- // 3. Fix invalid entity names like &quot; -> "
- // Common mistake: double-escaping
- const invalidEntities = [
- { pattern: /&quot;/g, replacement: """, name: "&quot;" },
- { pattern: /&lt;/g, replacement: "<", name: "&lt;" },
- { pattern: /&gt;/g, replacement: ">", name: "&gt;" },
- { pattern: /&apos;/g, replacement: "'", name: "&apos;" },
- { pattern: /&amp;/g, replacement: "&", name: "&amp;" },
- ]
- for (const { pattern, replacement, name } of invalidEntities) {
- if (pattern.test(fixed)) {
- fixed = fixed.replace(pattern, replacement)
- fixes.push(`Fixed double-escaped entity ${name}`)
- }
- }
-
- // 3b. Fix malformed attribute values where " is used as delimiter instead of actual quotes
- // Pattern: attr="value" should become attr="value" (the " was meant to be the quote delimiter)
- // This commonly happens with dashPattern="1 1;"
- const malformedQuotePattern = /(\s[a-zA-Z][a-zA-Z0-9_:-]*)="/
- if (malformedQuotePattern.test(fixed)) {
- // Replace =" with =" and trailing " before next attribute or tag end with "
- fixed = fixed.replace(
- /(\s[a-zA-Z][a-zA-Z0-9_:-]*)="([^&]*?)"/g,
- '$1="$2"',
- )
- fixes.push(
- 'Fixed malformed attribute quotes (="..." to ="...")',
- )
- }
-
- // 3c. Fix malformed closing tags like ->
- const malformedClosingTag = /<\/([a-zA-Z][a-zA-Z0-9]*)\s*\/>/g
- if (malformedClosingTag.test(fixed)) {
- fixed = fixed.replace(/<\/([a-zA-Z][a-zA-Z0-9]*)\s*\/>/g, "$1>")
- fixes.push("Fixed malformed closing tags ( to )")
- }
-
- // 3d. Fix missing space between attributes like vertex="1"parent="1"
- const missingSpacePattern = /("[^"]*")([a-zA-Z][a-zA-Z0-9_:-]*=)/g
- if (missingSpacePattern.test(fixed)) {
- fixed = fixed.replace(/("[^"]*")([a-zA-Z][a-zA-Z0-9_:-]*=)/g, "$1 $2")
- fixes.push("Added missing space between attributes")
- }
-
- // 3e. Fix unescaped quotes in style color values like fillColor="#fff2e6"
- // The " after Color= prematurely ends the style attribute. Remove it.
- // Pattern: ;fillColor="#fff → ;fillColor=#fff (remove first ", keep second as style closer)
- const quotedColorPattern = /;([a-zA-Z]*[Cc]olor)="#/
- if (quotedColorPattern.test(fixed)) {
- fixed = fixed.replace(/;([a-zA-Z]*[Cc]olor)="#/g, ";$1=#")
- fixes.push("Removed quotes around color values in style")
- }
-
- // 4. Fix unescaped < and > in attribute values
- // < is required to be escaped, > is not strictly required but we escape for consistency
- const attrPattern = /(=\s*")([^"]*?)(<)([^"]*?)(")/g
- let attrMatch
- let hasUnescapedLt = false
- while ((attrMatch = attrPattern.exec(fixed)) !== null) {
- if (!attrMatch[3].startsWith("<")) {
- hasUnescapedLt = true
- break
- }
- }
- if (hasUnescapedLt) {
- // Replace < and > with < and > inside attribute values
- fixed = fixed.replace(/=\s*"([^"]*)"/g, (_match, value) => {
- const escaped = value.replace(//g, ">")
- return `="${escaped}"`
- })
- fixes.push("Escaped <> characters in attribute values")
- }
-
- // 5. Fix invalid character references (remove malformed ones)
- // Pattern: followed by non-hex chars before ;
- const invalidHexRefs: string[] = []
- fixed = fixed.replace(/([^;]*);/g, (match, hex) => {
- if (/^[0-9a-fA-F]+$/.test(hex) && hex.length > 0) {
- return match // Valid hex ref, keep it
- }
- invalidHexRefs.push(match)
- return "" // Remove invalid ref
- })
- if (invalidHexRefs.length > 0) {
- fixes.push(
- `Removed ${invalidHexRefs.length} invalid hex character reference(s)`,
- )
- }
-
- // 6. Fix invalid decimal character references
- const invalidDecRefs: string[] = []
- fixed = fixed.replace(/([^x][^;]*);/g, (match, dec) => {
- if (/^[0-9]+$/.test(dec) && dec.length > 0) {
- return match // Valid decimal ref, keep it
- }
- invalidDecRefs.push(match)
- return "" // Remove invalid ref
- })
- if (invalidDecRefs.length > 0) {
- fixes.push(
- `Removed ${invalidDecRefs.length} invalid decimal character reference(s)`,
- )
- }
-
- // 7. Fix invalid comment syntax (replace -- with - repeatedly until none left)
- fixed = fixed.replace(//g, (match, content) => {
- if (/--/.test(content)) {
- // Keep replacing until no double hyphens remain
- let fixedContent = content
- while (/--/.test(fixedContent)) {
- fixedContent = fixedContent.replace(/--/g, "-")
- }
- fixes.push("Fixed invalid comment syntax (removed double hyphens)")
- return ``
- }
- return match
- })
-
- // 8. Fix tags that should be (common LLM mistake)
- // This handles both opening and closing tags
- const hasCellTags = /<\/?Cell[\s>]/i.test(fixed)
- if (hasCellTags) {
- console.log("[autoFixXml] Step 8: Found tags to fix")
- const beforeFix = fixed
- fixed = fixed.replace(//gi, "")
- fixed = fixed.replace(/<\/Cell>/gi, " ")
- if (beforeFix !== fixed) {
- console.log("[autoFixXml] Step 8: Fixed tags")
- }
- fixes.push("Fixed tags to ")
- }
-
- // 8b. Fix common closing tag typos (MUST run before foreign tag removal)
- const tagTypos = [
- { wrong: /<\/mxElement>/gi, right: " ", name: "" },
- { wrong: /<\/mxcell>/g, right: " | | | | ", name: " | " }, // case sensitivity
- {
- wrong: /<\/mxgeometry>/g,
- right: "",
- name: "",
- },
- { wrong: /<\/mxpoint>/g, right: "", name: "" },
- {
- wrong: /<\/mxgraphmodel>/gi,
- right: "",
- name: "",
- },
- ]
- for (const { wrong, right, name } of tagTypos) {
- const before = fixed
- fixed = fixed.replace(wrong, right)
- if (fixed !== before) {
- fixes.push(`Fixed typo ${name} to ${right}`)
- }
- }
-
- // 8c. Remove non-draw.io tags (after typo fixes so lowercase variants are fixed first)
- // IMPORTANT: Only remove tags at the element level, NOT inside quoted attribute values
- // Tags like , inside value="text " should be preserved (they're HTML content)
- const validDrawioTags = new Set([
- "mxfile",
- "diagram",
- "mxGraphModel",
- "root",
- "mxCell",
- "mxGeometry",
- "mxPoint",
- "Array",
- "Object",
- "mxRectangle",
- ])
-
- // Helper: Check if a position is inside a quoted attribute value
- // by counting unescaped quotes before that position
- const isInsideQuotes = (str: string, pos: number): boolean => {
- let inQuote = false
- let quoteChar = ""
- for (let i = 0; i < pos && i < str.length; i++) {
- const c = str[i]
- if (inQuote) {
- if (c === quoteChar) inQuote = false
- } else if (c === '"' || c === "'") {
- // Check if this quote is part of an attribute (preceded by =)
- // Look back for = sign
- let j = i - 1
- while (j >= 0 && /\s/.test(str[j])) j--
- if (j >= 0 && str[j] === "=") {
- inQuote = true
- quoteChar = c
- }
- }
- }
- return inQuote
- }
-
- const foreignTagPattern = /<\/?([a-zA-Z][a-zA-Z0-9_]*)[^>]*>/g
- let foreignMatch
- const foreignTags = new Set()
- const foreignTagPositions: Array<{
- tag: string
- start: number
- end: number
- }> = []
-
- while ((foreignMatch = foreignTagPattern.exec(fixed)) !== null) {
- const tagName = foreignMatch[1]
- // Skip if this is a valid draw.io tag
- if (validDrawioTags.has(tagName)) continue
- // Skip if this tag is inside a quoted attribute value
- if (isInsideQuotes(fixed, foreignMatch.index)) continue
-
- foreignTags.add(tagName)
- foreignTagPositions.push({
- tag: tagName,
- start: foreignMatch.index,
- end: foreignMatch.index + foreignMatch[0].length,
- })
- }
-
- if (foreignTagPositions.length > 0) {
- // Remove tags from end to start to preserve indices
- foreignTagPositions.sort((a, b) => b.start - a.start)
- for (const { start, end } of foreignTagPositions) {
- fixed = fixed.slice(0, start) + fixed.slice(end)
- }
- fixes.push(
- `Removed foreign tags: ${Array.from(foreignTags).join(", ")}`,
- )
- }
-
- // 10. Fix unclosed tags by appending missing closing tags
- // Use parseXmlTags helper to track open tags
- const tagStack: string[] = []
- const parsedTags = parseXmlTags(fixed)
-
- for (const { tagName, isClosing, isSelfClosing } of parsedTags) {
- if (isClosing) {
- // Find matching opening tag (may not be the last one if there's mismatch)
- const lastIdx = tagStack.lastIndexOf(tagName)
- if (lastIdx !== -1) {
- tagStack.splice(lastIdx, 1)
- }
- } else if (!isSelfClosing) {
- tagStack.push(tagName)
- }
- }
-
- // If there are unclosed tags, append closing tags in reverse order
- // But first verify with simple count that they're actually unclosed
- if (tagStack.length > 0) {
- const tagsToClose: string[] = []
- for (const tagName of tagStack.reverse()) {
- // Simple count check: only close if opens > closes
- const openCount = (
- fixed.match(new RegExp(`<${tagName}[\\s>]`, "gi")) || []
- ).length
- const closeCount = (
- fixed.match(new RegExp(`${tagName}>`, "gi")) || []
- ).length
- if (openCount > closeCount) {
- tagsToClose.push(tagName)
- }
- }
- if (tagsToClose.length > 0) {
- const closingTags = tagsToClose.map((t) => `${t}>`).join("\n")
- fixed = fixed.trimEnd() + "\n" + closingTags
- fixes.push(
- `Closed ${tagsToClose.length} unclosed tag(s): ${tagsToClose.join(", ")}`,
- )
- }
- }
-
- // 10b. Remove extra closing tags (more closes than opens)
- // Need to properly count self-closing tags (they don't need closing tags)
- // IMPORTANT: Only count tags at element level, NOT inside quoted attribute values
- const tagCounts = new Map<
- string,
- { opens: number; closes: number; selfClosing: number }
- >()
- // Match full tags to detect self-closing by checking if ends with />
- const fullTagPattern = /<(\/?[a-zA-Z][a-zA-Z0-9]*)[^>]*>/g
- let tagCountMatch
- while ((tagCountMatch = fullTagPattern.exec(fixed)) !== null) {
- // Skip tags inside quoted attribute values (e.g., value="Title ")
- if (isInsideQuotes(fixed, tagCountMatch.index)) continue
-
- const fullMatch = tagCountMatch[0] // e.g., " " or " "
- const tagPart = tagCountMatch[1] // e.g., "mxCell" or "/mxCell"
- const isClosing = tagPart.startsWith("/")
- const isSelfClosing = fullMatch.endsWith("/>")
- const tagName = isClosing ? tagPart.slice(1) : tagPart
-
- // Only count valid draw.io tags - skip partial/invalid tags like "mx" from streaming
- if (!validDrawioTags.has(tagName)) continue
-
- let counts = tagCounts.get(tagName)
- if (!counts) {
- counts = { opens: 0, closes: 0, selfClosing: 0 }
- tagCounts.set(tagName, counts)
- }
- if (isClosing) {
- counts.closes++
- } else if (isSelfClosing) {
- counts.selfClosing++
- } else {
- counts.opens++
- }
- }
-
- // Log tag counts for debugging
- for (const [tagName, counts] of tagCounts) {
- if (
- tagName === "mxCell" ||
- tagName === "mxGeometry" ||
- counts.opens !== counts.closes
- ) {
- console.log(
- `[autoFixXml] Step 10b: ${tagName} - opens: ${counts.opens}, closes: ${counts.closes}, selfClosing: ${counts.selfClosing}`,
- )
- }
- }
-
- // Find tags with extra closing tags (self-closing tags are balanced, don't need closing)
- for (const [tagName, counts] of tagCounts) {
- const extraCloses = counts.closes - counts.opens // Only compare opens vs closes (self-closing are balanced)
- if (extraCloses > 0) {
- console.log(
- `[autoFixXml] Step 10b: ${tagName} has ${counts.opens} opens, ${counts.closes} closes, removing ${extraCloses} extra`,
- )
- // Remove extra closing tags from the end
- let removed = 0
- const closeTagPattern = new RegExp(`${tagName}>`, "g")
- const matches = [...fixed.matchAll(closeTagPattern)]
- // Remove from the end (last occurrences are likely the extras)
- for (
- let i = matches.length - 1;
- i >= 0 && removed < extraCloses;
- i--
- ) {
- const match = matches[i]
- const idx = match.index ?? 0
- fixed = fixed.slice(0, idx) + fixed.slice(idx + match[0].length)
- removed++
- }
- if (removed > 0) {
- console.log(
- `[autoFixXml] Step 10b: Removed ${removed} extra ${tagName}>`,
- )
- fixes.push(
- `Removed ${removed} extra ${tagName}> closing tag(s)`,
- )
- }
- }
- }
-
- // 10c. Remove trailing garbage after last XML tag (e.g., stray backslashes, text)
- // Find the last valid closing tag or self-closing tag
- const closingTagPattern = /<\/[a-zA-Z][a-zA-Z0-9]*>|\/>/g
- let lastValidTagEnd = -1
- let closingMatch
- while ((closingMatch = closingTagPattern.exec(fixed)) !== null) {
- lastValidTagEnd = closingMatch.index + closingMatch[0].length
- }
- if (lastValidTagEnd > 0 && lastValidTagEnd < fixed.length) {
- const trailing = fixed.slice(lastValidTagEnd).trim()
- if (trailing) {
- fixed = fixed.slice(0, lastValidTagEnd)
- fixes.push("Removed trailing garbage after last XML tag")
- }
- }
-
- // 11. Fix nested mxCell by flattening
- // Pattern A: ...... (duplicate ID)
- // Pattern B: ...... (different ID - true nesting)
- const lines = fixed.split("\n")
- let newLines: string[] = []
- let nestedFixed = 0
- let extraClosingToRemove = 0
-
- // First pass: fix duplicate ID nesting (same as before)
- for (let i = 0; i < lines.length; i++) {
- const line = lines[i]
- const nextLine = lines[i + 1]
-
- // Check if current line and next line are both mxCell opening tags with same ID
- if (
- nextLine &&
- / ") &&
- !nextLine.includes("/>")
- ) {
- const id1 = line.match(/\bid\s*=\s*["']([^"']+)["']/)?.[1]
- const id2 = nextLine.match(/\bid\s*=\s*["']([^"']+)["']/)?.[1]
-
- if (id1 && id1 === id2) {
- nestedFixed++
- extraClosingToRemove++ // Need to remove one later
- continue // Skip this duplicate opening line
- }
- }
-
- // Remove extra if we have pending removals
- if (extraClosingToRemove > 0 && /^\s*<\/mxCell>\s*$/.test(line)) {
- extraClosingToRemove--
- continue // Skip this closing tag
- }
-
- newLines.push(line)
- }
-
- if (nestedFixed > 0) {
- fixed = newLines.join("\n")
- fixes.push(`Flattened ${nestedFixed} duplicate-ID nested mxCell(s)`)
- }
-
- // Second pass: fix true nesting (different IDs)
- // Insert before nested child to close parent
- const lines2 = fixed.split("\n")
- newLines = []
- let trueNestedFixed = 0
- let cellDepth = 0
- let pendingCloseRemoval = 0
-
- for (let i = 0; i < lines2.length; i++) {
- const line = lines2[i]
- const trimmed = line.trim()
-
- // Track mxCell depth
- const isOpenCell = / ")
- const isCloseCell = trimmed === ""
-
- if (isOpenCell) {
- if (cellDepth > 0) {
- // Found nested cell - insert closing tag for parent before this line
- const indent = line.match(/^(\s*)/)?.[1] || ""
- newLines.push(indent + "")
- trueNestedFixed++
- pendingCloseRemoval++ // Need to remove one later
- }
- cellDepth = 1 // Reset to 1 since we just opened a new cell
- newLines.push(line)
- } else if (isCloseCell) {
- if (pendingCloseRemoval > 0) {
- pendingCloseRemoval--
- // Skip this extra closing tag
- } else {
- cellDepth = Math.max(0, cellDepth - 1)
- newLines.push(line)
- }
- } else {
- newLines.push(line)
- }
- }
-
- if (trueNestedFixed > 0) {
- fixed = newLines.join("\n")
- fixes.push(`Fixed ${trueNestedFixed} true nested mxCell(s)`)
- }
-
- // 12. Fix duplicate IDs by appending suffix
- const seenIds = new Map()
- const duplicateIds: string[] = []
-
- // First pass: find duplicates
- const idPattern = /\bid\s*=\s*["']([^"']+)["']/gi
- let idMatch
- while ((idMatch = idPattern.exec(fixed)) !== null) {
- const id = idMatch[1]
- seenIds.set(id, (seenIds.get(id) || 0) + 1)
- }
-
- // Find which IDs are duplicated
- for (const [id, count] of seenIds) {
- if (count > 1) duplicateIds.push(id)
- }
-
- // Second pass: rename duplicates (keep first occurrence, rename others)
- if (duplicateIds.length > 0) {
- const idCounters = new Map()
- fixed = fixed.replace(/\bid\s*=\s*["']([^"']+)["']/gi, (match, id) => {
- if (!duplicateIds.includes(id)) return match
-
- const count = idCounters.get(id) || 0
- idCounters.set(id, count + 1)
-
- if (count === 0) return match // Keep first occurrence
-
- // Rename subsequent occurrences
- const newId = `${id}_dup${count}`
- return match.replace(id, newId)
- })
- fixes.push(`Renamed ${duplicateIds.length} duplicate ID(s)`)
- }
-
- // 9. Fix empty id attributes by generating unique IDs
- let emptyIdCount = 0
- fixed = fixed.replace(
- /]*)\sid\s*=\s*["']\s*["']([^>]*)>/g,
- (_match, before, after) => {
- emptyIdCount++
- const newId = `cell_${Date.now()}_${emptyIdCount}`
- return ``
- },
- )
- if (emptyIdCount > 0) {
- fixes.push(`Generated ${emptyIdCount} missing ID(s)`)
- }
-
- // 13. Aggressive: drop broken mxCell elements that can't be fixed
- // Only do this if DOM parser still finds errors after all other fixes
- if (typeof DOMParser !== "undefined") {
- let droppedCells = 0
- let maxIterations = MAX_DROP_ITERATIONS
- while (maxIterations-- > 0) {
- const parser = new DOMParser()
- const doc = parser.parseFromString(fixed, "text/xml")
- const parseError = doc.querySelector("parsererror")
- if (!parseError) break // Valid now!
-
- const errText = parseError.textContent || ""
- const match = errText.match(/(\d+):\d+:/)
- if (!match) break
-
- const errLine = parseInt(match[1], 10) - 1
- const lines = fixed.split("\n")
-
- // Find the mxCell containing this error line
- let cellStart = errLine
- let cellEnd = errLine
-
- // Go back to find 0 && !lines[cellStart].includes(" or />
- while (cellEnd < lines.length - 1) {
- if (
- lines[cellEnd].includes(" ") ||
- lines[cellEnd].trim().endsWith("/>")
- ) {
- break
- }
- cellEnd++
- }
-
- // Remove these lines
- lines.splice(cellStart, cellEnd - cellStart + 1)
- fixed = lines.join("\n")
- droppedCells++
- }
- if (droppedCells > 0) {
- fixes.push(`Dropped ${droppedCells} unfixable mxCell element(s)`)
- }
- }
-
- return { fixed, fixes }
-}
-
-/**
- * Validates XML and attempts to fix if invalid
- * @param xml - The XML string to validate and potentially fix
- * @returns Object with validation result, fixed XML if applicable, and fixes applied
- */
-export function validateAndFixXml(xml: string): {
- valid: boolean
- error: string | null
- fixed: string | null
- fixes: string[]
-} {
- // First validation attempt
- let error = validateMxCellStructure(xml)
-
- if (!error) {
- return { valid: true, error: null, fixed: null, fixes: [] }
- }
-
- // Try to fix
- const { fixed, fixes } = autoFixXml(xml)
- console.log("[validateAndFixXml] Fixes applied:", fixes)
-
- // Validate the fixed version
- error = validateMxCellStructure(fixed)
- if (error) {
- console.log("[validateAndFixXml] Still invalid after fix:", error)
- }
-
- if (!error) {
- return { valid: true, error: null, fixed, fixes }
- }
-
- // Still invalid after fixes - but return the partially fixed XML
- // so we can see what was fixed and what error remains
- return {
- valid: false,
- error,
- fixed: fixes.length > 0 ? fixed : null,
- fixes,
- }
-}
-
export function extractDiagramXML(xml_svg_string: string): string {
try {
// 1. Parse the SVG string (using built-in DOMParser in a browser-like environment)
diff --git a/next.config.ts b/next.config.ts
index 466596a4..366b1351 100644
--- a/next.config.ts
+++ b/next.config.ts
@@ -10,9 +10,11 @@ const nextConfig: NextConfig = {
env: {
APP_VERSION: packageJson.version,
},
- // Include instrumentation.ts in standalone build for Langfuse telemetry
+ // Include instrumentation.ts in standalone build for Langfuse telemetry,
+ // and the shape library docs get_shape_library reads at run time
outputFileTracingIncludes: {
"*": ["./instrumentation.ts"],
+ "/api/chat": ["./docs/shape-libraries/*.md"],
},
}
diff --git a/package-lock.json b/package-lock.json
index 01260003..4ed8ed73 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,16 +9,16 @@
"version": "0.4.16",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/amazon-bedrock": "^4.0.1",
- "@ai-sdk/anthropic": "^3.0.0",
- "@ai-sdk/azure": "^3.0.0",
- "@ai-sdk/deepseek": "^2.0.0",
- "@ai-sdk/gateway": "^3.0.0",
- "@ai-sdk/google": "^3.0.0",
- "@ai-sdk/google-vertex": "^4.0.16",
- "@ai-sdk/openai": "^3.0.0",
- "@ai-sdk/react": "^3.0.1",
- "@aihubmix/ai-sdk-provider": "^2.1.0",
+ "@ai-sdk/amazon-bedrock": "^4.0.191",
+ "@ai-sdk/anthropic": "^3.0.127",
+ "@ai-sdk/azure": "^3.0.133",
+ "@ai-sdk/deepseek": "^2.0.71",
+ "@ai-sdk/google": "^3.0.130",
+ "@ai-sdk/google-vertex": "^4.0.210",
+ "@ai-sdk/openai": "^3.0.124",
+ "@ai-sdk/openai-compatible": "^2.0.81",
+ "@ai-sdk/react": "^3.0.303",
+ "@aihubmix/ai-sdk-provider": "^2.2.1",
"@aws-sdk/client-dynamodb": "^3.957.0",
"@aws-sdk/credential-providers": "^3.943.0",
"@extractus/article-extractor": "^8.0.18",
@@ -28,7 +28,7 @@
"@langfuse/tracing": "^4.4.9",
"@next/third-parties": "^16.0.6",
"@opennextjs/cloudflare": "^1.17.1",
- "@openrouter/ai-sdk-provider": "^2.0.0",
+ "@openrouter/ai-sdk-provider": "^2.10.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.222.0",
"@opentelemetry/sdk-trace-node": "^2.2.0",
@@ -44,7 +44,7 @@
"@radix-ui/react-tooltip": "^1.1.8",
"@radix-ui/react-use-controllable-state": "^1.2.2",
"@xmldom/xmldom": "^0.9.8",
- "ai": "^6.0.1",
+ "ai": "^6.0.300",
"base-64": "^1.0.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
@@ -56,7 +56,7 @@
"nanoid": "^5.0.0",
"negotiator": "^1.0.0",
"next": "^16.0.7",
- "ollama-ai-provider-v2": "^3.0.0",
+ "ollama-ai-provider-v2": "^3.6.0",
"pako": "^2.1.0",
"prism-react-renderer": "^2.4.1",
"react": "^19.1.2",
@@ -66,6 +66,7 @@
"react-markdown": "^10.1.0",
"react-resizable-panels": "^3.0.6",
"remark-gfm": "^4.0.1",
+ "saxes": "^6.0.0",
"server-only": "^0.0.1",
"sonner": "^2.0.7",
"tailwind-merge": "^3.0.2",
@@ -123,15 +124,15 @@
"license": "MIT"
},
"node_modules/@ai-sdk/amazon-bedrock": {
- "version": "4.0.113",
- "resolved": "https://registry.npmjs.org/@ai-sdk/amazon-bedrock/-/amazon-bedrock-4.0.113.tgz",
- "integrity": "sha512-qoeF2ghkYqHY4u68rasZopYsRuGnRwgqSfe6rhC/jM6W73Z7TU5v9QcfDYKt9dgp19YHY9EqiX3SXVC+uPGkRQ==",
+ "version": "4.0.191",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/amazon-bedrock/-/amazon-bedrock-4.0.191.tgz",
+ "integrity": "sha512-7wgtq8On8oqmofGIGtYl8EOEbawnfiPbIbWpu2ksGNF065J8mbpE6DhkHNv0dpPbvnn4txL4sflUwUPwHw7/SQ==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/anthropic": "3.0.81",
- "@ai-sdk/openai": "3.0.68",
- "@ai-sdk/provider": "3.0.10",
- "@ai-sdk/provider-utils": "4.0.27",
+ "@ai-sdk/anthropic": "3.0.127",
+ "@ai-sdk/openai": "3.0.124",
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57",
"@smithy/eventstream-codec": "^4.0.1",
"@smithy/util-utf8": "^4.0.0",
"aws4fetch": "^1.0.20"
@@ -143,75 +144,14 @@
"zod": "^3.25.76 || ^4.1.8"
}
},
- "node_modules/@ai-sdk/amazon-bedrock/node_modules/@ai-sdk/anthropic": {
- "version": "3.0.81",
- "resolved": "https://registry.npmjs.org/@ai-sdk/anthropic/-/anthropic-3.0.81.tgz",
- "integrity": "sha512-B1JDd9Ugq9R5AgIaW3674lhGCMMYJcPUxnrZh8fzbGojgg4QvHFRv6eZahGQAUsmGHbcf74G9bdSBDLWQGY2GA==",
- "license": "Apache-2.0",
- "dependencies": {
- "@ai-sdk/provider": "3.0.10",
- "@ai-sdk/provider-utils": "4.0.27"
- },
- "engines": {
- "node": ">=18"
- },
- "peerDependencies": {
- "zod": "^3.25.76 || ^4.1.8"
- }
- },
- "node_modules/@ai-sdk/amazon-bedrock/node_modules/@ai-sdk/openai": {
- "version": "3.0.68",
- "resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.68.tgz",
- "integrity": "sha512-FCs/DPr4M95UyZ/ABHJmTmCEYRCka/4J0Bna0nsd78QCdGIS0X/zhn+fVzB7mZJo7464uOWYUjROx9PGNGOb0w==",
- "license": "Apache-2.0",
- "dependencies": {
- "@ai-sdk/provider": "3.0.10",
- "@ai-sdk/provider-utils": "4.0.27"
- },
- "engines": {
- "node": ">=18"
- },
- "peerDependencies": {
- "zod": "^3.25.76 || ^4.1.8"
- }
- },
- "node_modules/@ai-sdk/amazon-bedrock/node_modules/@ai-sdk/provider": {
- "version": "3.0.10",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.10.tgz",
- "integrity": "sha512-Q3BZ27qfpYqnCYGvE3vt+Qi6LGOF9R5Nmzn+9JoM1lCRsD9mYaIhfJLkSunN48nfGXJ6n+XNV0J/XVpqGQl7Dw==",
- "license": "Apache-2.0",
- "dependencies": {
- "json-schema": "^0.4.0"
- },
- "engines": {
- "node": ">=18"
- }
- },
- "node_modules/@ai-sdk/amazon-bedrock/node_modules/@ai-sdk/provider-utils": {
- "version": "4.0.27",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-4.0.27.tgz",
- "integrity": "sha512-ubkAJ+xODouwtmN1tYlvTPphH1hPOBfZaEQe8U7skGvFAnIRs9PPpsq57bC2+Ky/MB4yzhd6YOsxTAx9sGpazw==",
- "license": "Apache-2.0",
- "dependencies": {
- "@ai-sdk/provider": "3.0.10",
- "@standard-schema/spec": "^1.1.0",
- "eventsource-parser": "^3.0.8"
- },
- "engines": {
- "node": ">=18"
- },
- "peerDependencies": {
- "zod": "^3.25.76 || ^4.1.8"
- }
- },
"node_modules/@ai-sdk/anthropic": {
- "version": "3.0.47",
- "resolved": "https://registry.npmjs.org/@ai-sdk/anthropic/-/anthropic-3.0.47.tgz",
- "integrity": "sha512-E6Z3i/xvxGDxRskMMbuX9+xDK4l5LesrP2O7YQ0CcbAkYP25qTo/kYGf/AsJrLkNIY23HeO/kheUWtG1XZllDA==",
+ "version": "3.0.127",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/anthropic/-/anthropic-3.0.127.tgz",
+ "integrity": "sha512-Inff1DmPRVWi6QGr9YL2wiA4maILZuve9I8id/O4keQEoq6fJv6EfvkrSFw/nNvm2jiTcIUfDrpczeHTuhl9mg==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15"
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57"
},
"engines": {
"node": ">=18"
@@ -221,14 +161,15 @@
}
},
"node_modules/@ai-sdk/azure": {
- "version": "3.0.34",
- "resolved": "https://registry.npmjs.org/@ai-sdk/azure/-/azure-3.0.34.tgz",
- "integrity": "sha512-nnOFtgvZYOa6XIeAm18i56NX77Yu4Bd+Tnbt85LGUEqwJFR54kFTlR1nm3BAJCphHrmQteJd1P3QErtyoXig8A==",
+ "version": "3.0.133",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/azure/-/azure-3.0.133.tgz",
+ "integrity": "sha512-iVCT1q6bRPWjzisu7Xy1bc1yWtegd3WSZzmi4+jkiFTKTxir703F5jH8mWPG0hPnqW/OjdkOrSzcQ063j7T/ng==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/openai": "3.0.33",
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15"
+ "@ai-sdk/deepseek": "2.0.71",
+ "@ai-sdk/openai": "3.0.124",
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57"
},
"engines": {
"node": ">=18"
@@ -238,13 +179,13 @@
}
},
"node_modules/@ai-sdk/deepseek": {
- "version": "2.0.20",
- "resolved": "https://registry.npmjs.org/@ai-sdk/deepseek/-/deepseek-2.0.20.tgz",
- "integrity": "sha512-MAL04sDTOWUiBjAGWaVgyeE4bYRb9QpKYRlIeCTZFga6I8yQs50XakhWEssrmvVihdpHGkqpDtCHsFqCydsWLA==",
+ "version": "2.0.71",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/deepseek/-/deepseek-2.0.71.tgz",
+ "integrity": "sha512-2uLtZBgONfzEP7tZOg23sBzi33n1/gcW1zLNNw96/PLt9vdqDYIkGQnKRd6F+/0Wwy77jVulU+gzpiudk3f0UQ==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15"
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57"
},
"engines": {
"node": ">=18"
@@ -254,14 +195,14 @@
}
},
"node_modules/@ai-sdk/gateway": {
- "version": "3.0.55",
- "resolved": "https://registry.npmjs.org/@ai-sdk/gateway/-/gateway-3.0.55.tgz",
- "integrity": "sha512-7xMeTJnCjwRwXKVCiv4Ly4qzWvDuW3+W1WIV0X1EFu6W83d4mEhV9bFArto10MeTw40ewuDjrbrZd21mXKohkw==",
+ "version": "3.0.209",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/gateway/-/gateway-3.0.209.tgz",
+ "integrity": "sha512-CjCBzC35lRZ0LnUYLDfTB92p0Fr1Fu9Wrivc+LI0u3ox/DyZ0wPOHjt8DKBKHD/R4aZ86+JCFFuD9f8MFjAQFg==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15",
- "@vercel/oidc": "3.1.0"
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57",
+ "@vercel/oidc": "3.2.0"
},
"engines": {
"node": ">=18"
@@ -271,13 +212,13 @@
}
},
"node_modules/@ai-sdk/google": {
- "version": "3.0.31",
- "resolved": "https://registry.npmjs.org/@ai-sdk/google/-/google-3.0.31.tgz",
- "integrity": "sha512-RVNz8WFSIRbXbYDBE6JvlE2escWPJimBCs22LzKEYH7DNfl/X7cHNa1LFho4PsY6Ib0JmbzB8s2+i0wHs/wNCg==",
+ "version": "3.0.130",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/google/-/google-3.0.130.tgz",
+ "integrity": "sha512-DOhGfFT667LbopxMZU7kIiT4K8F2RNN8ziwmOxAfMbp3xTvAaxQ4ynPKNWBVXcCkGYDmzPb7QiazN1IRX8y3lw==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15"
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57"
},
"engines": {
"node": ">=18"
@@ -287,15 +228,16 @@
}
},
"node_modules/@ai-sdk/google-vertex": {
- "version": "4.0.63",
- "resolved": "https://registry.npmjs.org/@ai-sdk/google-vertex/-/google-vertex-4.0.63.tgz",
- "integrity": "sha512-/RNi6KSB4162DDYeXHUKQc5jLPmiJMkhTswLwbfPUEPyyFjbxpBWgeAk/vS/u8jxT4IPyYQ8cD/uyvJibmtmww==",
+ "version": "4.0.210",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/google-vertex/-/google-vertex-4.0.210.tgz",
+ "integrity": "sha512-7Jj41iWsTFUcv6pzji5AlW2QaMJt/PEMU/zwqji2H7U52WvZE345kyo+nKRJMFtHgSVqFmzmMUIM9gyeaXKRtw==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/anthropic": "3.0.47",
- "@ai-sdk/google": "3.0.31",
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15",
+ "@ai-sdk/anthropic": "3.0.127",
+ "@ai-sdk/google": "3.0.130",
+ "@ai-sdk/openai-compatible": "2.0.81",
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57",
"google-auth-library": "^10.5.0"
},
"engines": {
@@ -306,13 +248,13 @@
}
},
"node_modules/@ai-sdk/openai": {
- "version": "3.0.33",
- "resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.33.tgz",
- "integrity": "sha512-O/8SVKAiwFHkGAUfBnrLb7L2IjbpP9ySWbmOktOfa0KtzutZkmKNrJ5CtB5dj+lwuENbOuZeRsnsZdOjar7hig==",
+ "version": "3.0.124",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.124.tgz",
+ "integrity": "sha512-7DpRUPXzJ+S6XasEJBojzFCs04J3rNPs0FZFF3PEeix49IggZAwsMrqscUwB/+05+87r01SS/jeopQGOOL4AEQ==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15"
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57"
},
"engines": {
"node": ">=18"
@@ -322,42 +264,13 @@
}
},
"node_modules/@ai-sdk/openai-compatible": {
- "version": "2.0.48",
- "resolved": "https://registry.npmjs.org/@ai-sdk/openai-compatible/-/openai-compatible-2.0.48.tgz",
- "integrity": "sha512-z9MC6M4Oh/yUY/F/eszOtO8wc2nMz99XmZQKd2gWTtyIfe716xTfrKe3aYZKg20NZDtyjqPPKPSR+wqz7q1T7Q==",
+ "version": "2.0.81",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/openai-compatible/-/openai-compatible-2.0.81.tgz",
+ "integrity": "sha512-L14Jd0lAFKNM42l9TxlpwkNiyMlSvSvGySLc9GN4FB0qOo4/WmYOeLyn/CPhghIr0K4O6vH1y0nChxGljq2ccg==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.10",
- "@ai-sdk/provider-utils": "4.0.27"
- },
- "engines": {
- "node": ">=18"
- },
- "peerDependencies": {
- "zod": "^3.25.76 || ^4.1.8"
- }
- },
- "node_modules/@ai-sdk/openai-compatible/node_modules/@ai-sdk/provider": {
- "version": "3.0.10",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.10.tgz",
- "integrity": "sha512-Q3BZ27qfpYqnCYGvE3vt+Qi6LGOF9R5Nmzn+9JoM1lCRsD9mYaIhfJLkSunN48nfGXJ6n+XNV0J/XVpqGQl7Dw==",
- "license": "Apache-2.0",
- "dependencies": {
- "json-schema": "^0.4.0"
- },
- "engines": {
- "node": ">=18"
- }
- },
- "node_modules/@ai-sdk/openai-compatible/node_modules/@ai-sdk/provider-utils": {
- "version": "4.0.27",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-4.0.27.tgz",
- "integrity": "sha512-ubkAJ+xODouwtmN1tYlvTPphH1hPOBfZaEQe8U7skGvFAnIRs9PPpsq57bC2+Ky/MB4yzhd6YOsxTAx9sGpazw==",
- "license": "Apache-2.0",
- "dependencies": {
- "@ai-sdk/provider": "3.0.10",
- "@standard-schema/spec": "^1.1.0",
- "eventsource-parser": "^3.0.8"
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57"
},
"engines": {
"node": ">=18"
@@ -367,9 +280,9 @@
}
},
"node_modules/@ai-sdk/provider": {
- "version": "3.0.8",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.8.tgz",
- "integrity": "sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ==",
+ "version": "3.0.18",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.18.tgz",
+ "integrity": "sha512-IpefF5ssZVOZCD4Wu7zxt6PwZoGT2HLVjxpJ10Bjx6EAmRfLYJPjyrfxb0RYDlwvXnDb9nkQ7Do/oJVDuRFzew==",
"license": "Apache-2.0",
"dependencies": {
"json-schema": "^0.4.0"
@@ -379,30 +292,40 @@
}
},
"node_modules/@ai-sdk/provider-utils": {
- "version": "4.0.15",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-4.0.15.tgz",
- "integrity": "sha512-8XiKWbemmCbvNN0CLR9u3PQiet4gtEVIrX4zzLxnCj06AwsEDJwJVBbKrEI4t6qE8XRSIvU2irka0dcpziKW6w==",
+ "version": "4.0.57",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-4.0.57.tgz",
+ "integrity": "sha512-89a7sPvZqXsP3PigcAQLWntu/duIVEcN/RyChAB+j6p3C7NwW767sVttvL8r0+BetTg63uAXQoYHCZZueQKBSw==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "3.0.8",
+ "@ai-sdk/provider": "3.0.18",
"@standard-schema/spec": "^1.1.0",
- "eventsource-parser": "^3.0.6"
+ "eventsource-parser": "^3.0.8",
+ "undici": "^6.28.0"
},
"engines": {
- "node": ">=18"
+ "node": ">=18.17"
},
"peerDependencies": {
"zod": "^3.25.76 || ^4.1.8"
}
},
+ "node_modules/@ai-sdk/provider-utils/node_modules/undici": {
+ "version": "6.29.0",
+ "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.0.tgz",
+ "integrity": "sha512-R+RODBqp6i2pPflGdq+xIOUkl+RNfGgHwoinecKu/JCuf2uO06cOKoDbI2P7Dn6KcswdKwrczbU6IYJ6K8X+wg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=18.17"
+ }
+ },
"node_modules/@ai-sdk/react": {
- "version": "3.0.102",
- "resolved": "https://registry.npmjs.org/@ai-sdk/react/-/react-3.0.102.tgz",
- "integrity": "sha512-WPSYJxk/HM3SWhhxE+WrLhIMbaRLpDHWWYzUlrptXowwEoyliYBYZAkzip/gV8hybT2NyrWnsUh5KRO5iBdsQA==",
+ "version": "3.0.303",
+ "resolved": "https://registry.npmjs.org/@ai-sdk/react/-/react-3.0.303.tgz",
+ "integrity": "sha512-PeTBn25x8QxfIIpdUDOpWHfiIZxMRJCw04PhWNHI9hl+gtu+zGwAxWU+sy3PH/f/oEBBAp7eDDUDDjiuqwcw6g==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider-utils": "4.0.15",
- "ai": "6.0.100",
+ "@ai-sdk/provider-utils": "4.0.57",
+ "ai": "6.0.300",
"swr": "^2.2.5",
"throttleit": "2.1.0"
},
@@ -414,9 +337,9 @@
}
},
"node_modules/@aihubmix/ai-sdk-provider": {
- "version": "2.1.0",
- "resolved": "https://registry.npmjs.org/@aihubmix/ai-sdk-provider/-/ai-sdk-provider-2.1.0.tgz",
- "integrity": "sha512-AqK10PV5B4zWFBav5PRUhrWGYTHjC0s6cIbd4v9hBo6D9SqKv5o41B+dkl6IeIPrnZrMGLEE9Mn5R+VaEkhbdg==",
+ "version": "2.2.1",
+ "resolved": "https://registry.npmjs.org/@aihubmix/ai-sdk-provider/-/ai-sdk-provider-2.2.1.tgz",
+ "integrity": "sha512-VUkSYFbrigs5ZF44LRSHEI/q1fZyLkExWvrgBG4w0ysGEE/XXt7fi9NgMG9UH1PQVBgnc/PP03CUhTM1nhmGxQ==",
"license": "Apache-2.0",
"dependencies": {
"@ai-sdk/anthropic": "^3.0.0",
@@ -5890,9 +5813,9 @@
}
},
"node_modules/@openrouter/ai-sdk-provider": {
- "version": "2.8.1",
- "resolved": "https://registry.npmjs.org/@openrouter/ai-sdk-provider/-/ai-sdk-provider-2.8.1.tgz",
- "integrity": "sha512-Y6j3yivgoEUf/kutD/k5GX/mzZfioRFoSx0gbQ+mIOzMaH/vJv1rCkztiuvlLw5xRYQil7oxHUZvmSfXqOx1NQ==",
+ "version": "2.10.0",
+ "resolved": "https://registry.npmjs.org/@openrouter/ai-sdk-provider/-/ai-sdk-provider-2.10.0.tgz",
+ "integrity": "sha512-FMsAEjLUt5pWuRE2LDC/LCvVrFjLlrEzUITH5+5SZtfq7KZ2wrOHjQVxzz92sju8S9ltpzW87CLW8/b0oBXVCw==",
"license": "Apache-2.0",
"engines": {
"node": ">=18"
@@ -9326,9 +9249,9 @@
]
},
"node_modules/@vercel/oidc": {
- "version": "3.1.0",
- "resolved": "https://registry.npmjs.org/@vercel/oidc/-/oidc-3.1.0.tgz",
- "integrity": "sha512-Fw28YZpRnA3cAHHDlkt7xQHiJ0fcL+NRcIqsocZQUSmbzeIKRpwttJjik5ZGanXP+vlA4SbTg+AbA3bP363l+w==",
+ "version": "3.2.0",
+ "resolved": "https://registry.npmjs.org/@vercel/oidc/-/oidc-3.2.0.tgz",
+ "integrity": "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug==",
"license": "Apache-2.0",
"engines": {
"node": ">= 20"
@@ -9594,15 +9517,15 @@
}
},
"node_modules/ai": {
- "version": "6.0.100",
- "resolved": "https://registry.npmjs.org/ai/-/ai-6.0.100.tgz",
- "integrity": "sha512-BIxhG7M7wvcWCF+IEnZi7WpkRLOM3jR2vJ0mMuohl2UB2i1R/ZUa1cHFel1xI8nWvyUpOoQXKqsM0BAH50EYSQ==",
+ "version": "6.0.300",
+ "resolved": "https://registry.npmjs.org/ai/-/ai-6.0.300.tgz",
+ "integrity": "sha512-ZBT30eQTy6exP+EWJJxm3DMb34m9td8STy+hfnvKdjhyEQoM8vQN5YfFLTRVVTCONHoM+y5K+Tudb6PTFrpikA==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/gateway": "3.0.55",
- "@ai-sdk/provider": "3.0.8",
- "@ai-sdk/provider-utils": "4.0.15",
- "@opentelemetry/api": "1.9.0"
+ "@ai-sdk/gateway": "3.0.209",
+ "@ai-sdk/provider": "3.0.18",
+ "@ai-sdk/provider-utils": "4.0.57",
+ "@opentelemetry/api": "^1.9.0"
},
"engines": {
"node": ">=18"
@@ -9611,15 +9534,6 @@
"zod": "^3.25.76 || ^4.1.8"
}
},
- "node_modules/ai/node_modules/@opentelemetry/api": {
- "version": "1.9.0",
- "resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",
- "integrity": "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==",
- "license": "Apache-2.0",
- "engines": {
- "node": ">=8.0.0"
- }
- },
"node_modules/ajv": {
"version": "6.14.0",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.14.0.tgz",
@@ -19589,39 +19503,26 @@
"license": "MIT"
},
"node_modules/ollama-ai-provider-v2": {
- "version": "3.5.0",
- "resolved": "https://registry.npmjs.org/ollama-ai-provider-v2/-/ollama-ai-provider-v2-3.5.0.tgz",
- "integrity": "sha512-+s/aYIYa91z2Vk3AkGAz3BaPAQ0flS2eFZD3BN2mD/N6W6YQbcookyu6pc2cbc8SP5VGpNB857WJ0eHDjKXsXw==",
+ "version": "3.6.0",
+ "resolved": "https://registry.npmjs.org/ollama-ai-provider-v2/-/ollama-ai-provider-v2-3.6.0.tgz",
+ "integrity": "sha512-1Om3FVJYhBwkAr5kQ+BX1s/tdVdtVdoFQWrX4PBQHDHPISyGt24CjhtggEjUYpy5ait0YeVfZwEpIYjgD8Ih7Q==",
"license": "Apache-2.0",
"dependencies": {
- "@ai-sdk/provider": "^3.0.8",
- "@ai-sdk/provider-utils": "^4.0.19"
+ "@ai-sdk/provider": "^3.0.10",
+ "@ai-sdk/provider-utils": "^4.0.27"
},
"engines": {
- "node": ">=18"
+ "node": ">=20"
+ },
+ "funding": {
+ "type": "Buy Me a Coffee",
+ "url": "https://buymeacoffee.com/nordwestt"
},
"peerDependencies": {
"ai": "^5.0.0 || ^6.0.0",
"zod": "^4.0.16"
}
},
- "node_modules/ollama-ai-provider-v2/node_modules/@ai-sdk/provider-utils": {
- "version": "4.0.23",
- "resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-4.0.23.tgz",
- "integrity": "sha512-z8GlDaCmRSDlqkMF2f4/RFgWxdarvIbyuk+m6WXT1LYgsnGiXRJGTD2Z1+SDl3LqtFuRtGX1aghYvQLoHL/9pg==",
- "license": "Apache-2.0",
- "dependencies": {
- "@ai-sdk/provider": "3.0.8",
- "@standard-schema/spec": "^1.1.0",
- "eventsource-parser": "^3.0.6"
- },
- "engines": {
- "node": ">=18"
- },
- "peerDependencies": {
- "zod": "^3.25.76 || ^4.1.8"
- }
- },
"node_modules/on-finished": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz",
@@ -21478,7 +21379,6 @@
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/saxes/-/saxes-6.0.0.tgz",
"integrity": "sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==",
- "dev": true,
"license": "ISC",
"dependencies": {
"xmlchars": "^2.2.0"
@@ -25261,7 +25161,6 @@
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz",
"integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==",
- "dev": true,
"license": "MIT"
},
"node_modules/y18n": {
diff --git a/package.json b/package.json
index c131f12e..3db426aa 100644
--- a/package.json
+++ b/package.json
@@ -31,16 +31,16 @@
"test:e2e": "playwright test"
},
"dependencies": {
- "@ai-sdk/amazon-bedrock": "^4.0.1",
- "@ai-sdk/anthropic": "^3.0.0",
- "@ai-sdk/azure": "^3.0.0",
- "@ai-sdk/deepseek": "^2.0.0",
- "@ai-sdk/gateway": "^3.0.0",
- "@ai-sdk/google": "^3.0.0",
- "@ai-sdk/google-vertex": "^4.0.16",
- "@ai-sdk/openai": "^3.0.0",
- "@ai-sdk/react": "^3.0.1",
- "@aihubmix/ai-sdk-provider": "^2.1.0",
+ "@ai-sdk/amazon-bedrock": "^4.0.191",
+ "@ai-sdk/anthropic": "^3.0.127",
+ "@ai-sdk/azure": "^3.0.133",
+ "@ai-sdk/deepseek": "^2.0.71",
+ "@ai-sdk/google": "^3.0.130",
+ "@ai-sdk/google-vertex": "^4.0.210",
+ "@ai-sdk/openai": "^3.0.124",
+ "@ai-sdk/openai-compatible": "^2.0.81",
+ "@ai-sdk/react": "^3.0.303",
+ "@aihubmix/ai-sdk-provider": "^2.2.1",
"@aws-sdk/client-dynamodb": "^3.957.0",
"@aws-sdk/credential-providers": "^3.943.0",
"@extractus/article-extractor": "^8.0.18",
@@ -50,7 +50,7 @@
"@langfuse/tracing": "^4.4.9",
"@next/third-parties": "^16.0.6",
"@opennextjs/cloudflare": "^1.17.1",
- "@openrouter/ai-sdk-provider": "^2.0.0",
+ "@openrouter/ai-sdk-provider": "^2.10.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.222.0",
"@opentelemetry/sdk-trace-node": "^2.2.0",
@@ -66,7 +66,7 @@
"@radix-ui/react-tooltip": "^1.1.8",
"@radix-ui/react-use-controllable-state": "^1.2.2",
"@xmldom/xmldom": "^0.9.8",
- "ai": "^6.0.1",
+ "ai": "^6.0.300",
"base-64": "^1.0.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
@@ -78,7 +78,7 @@
"nanoid": "^5.0.0",
"negotiator": "^1.0.0",
"next": "^16.0.7",
- "ollama-ai-provider-v2": "^3.0.0",
+ "ollama-ai-provider-v2": "^3.6.0",
"pako": "^2.1.0",
"prism-react-renderer": "^2.4.1",
"react": "^19.1.2",
@@ -88,6 +88,7 @@
"react-markdown": "^10.1.0",
"react-resizable-panels": "^3.0.6",
"remark-gfm": "^4.0.1",
+ "saxes": "^6.0.0",
"server-only": "^0.0.1",
"sonner": "^2.0.7",
"tailwind-merge": "^3.0.2",
diff --git a/packages/claude-plugin/README.md b/packages/claude-plugin/README.md
index db4de05b..752fabbc 100644
--- a/packages/claude-plugin/README.md
+++ b/packages/claude-plugin/README.md
@@ -25,10 +25,13 @@ claude mcp add drawio -- npx @next-ai-drawio/mcp-server@latest
## Features
- **Real-time Preview**: Diagrams appear and update in your browser as Claude creates them
-- **Version History**: Restore previous diagram versions with visual thumbnails
+- **Drawing Rules and Shape Libraries**: Claude gets the web app's layout and style rules and the icon docs for AWS, Azure, GCP, Kubernetes and more
+- **Self-check**: Claude can take a screenshot of the rendered diagram and fix what looks wrong
+- **Version History**: Restore one of the last 20 versions from the **History** button, shown as thumbnails
- **Natural Language**: Describe diagrams in plain text - flowcharts, architecture diagrams, etc.
-- **Edit Support**: Modify existing diagrams with natural language instructions
-- **Export**: Save diagrams as `.drawio` files
+- **Edit Support**: Modify existing diagrams with natural language instructions, including your own edits in the browser
+- **Export**: Save diagrams as `.drawio`, `.png`, `.svg`, or `.drawio.svg` files
+- **Auto-save**: Each diagram is saved to `~/.next-ai-drawio/`, so `claude --resume` can pick it up again
- **Self-contained**: Embedded server, no external dependencies required
## Use Case Examples
@@ -72,11 +75,16 @@ between user, client app, auth server, and resource server
| Tool | Description |
|------|-------------|
-| `start_session` | Opens browser with real-time diagram preview |
+| `start_session` | Opens browser with real-time diagram preview; the result includes the drawing rules |
+| `get_drawing_guide` | Return the drawing rules again |
+| `get_shape_library` | Return the shapes and icon styles of a library such as `aws4` |
| `create_new_diagram` | Create a new diagram from XML |
-| `edit_diagram` | Edit diagram by ID-based operations |
+| `load_diagram` | Load a `.drawio` file from disk |
+| `edit_diagram` | Edit diagram by ID-based operations; all or nothing |
| `get_diagram` | Get the current diagram XML |
-| `export_diagram` | Save diagram to a `.drawio` file |
+| `screenshot_diagram` | Return a PNG of a page so Claude can check the result |
+| `export_diagram` | Save diagram to a `.drawio`, `.png`, `.svg`, or `.drawio.svg` file |
+| `list_pages`, `add_page`, `rename_page`, `delete_page` | Work with multi-page diagrams |
## How It Works
@@ -95,6 +103,8 @@ Claude Code <--stdio--> MCP Server <--http--> Browser (draw.io)
|----------|---------|-------------|
| `PORT` | `6002` | Port for the embedded HTTP server |
| `DRAWIO_BASE_URL` | `https://embed.diagrams.net` | Base URL for draw.io (for self-hosted deployments) |
+| `DRAWIO_DATA_DIR` | `~/.next-ai-drawio` | Folder for auto-saved diagrams; `off` turns auto-save off |
+| `DEBUG` | unset | Set to `true` to log debug messages |
## Links
diff --git a/packages/mcp-server/README.md b/packages/mcp-server/README.md
index 9e19b86c..968688a2 100644
--- a/packages/mcp-server/README.md
+++ b/packages/mcp-server/README.md
@@ -104,27 +104,41 @@ Use the standard MCP configuration with:
## Features
- **Real-time Preview**: Diagrams appear and update in your browser as the AI creates them
-- **Version History**: Restore previous diagram versions with visual thumbnails - click the clock button (bottom-right) to browse and restore earlier states
-- **Natural Language**: Describe diagrams in plain text - flowcharts, architecture diagrams, etc.
-- **Edit Support**: Modify existing diagrams with natural language instructions
-- **Export**: Save diagrams as `.drawio` files
+- **Drawing Rules**: The AI gets the same layout, edge and style rules as the web app, plus the shape library docs (AWS, Azure, GCP, Kubernetes, Cisco and more), so it uses real icon names instead of guessing
+- **Self-check**: The AI can take a screenshot of the rendered diagram and fix overlapping shapes or edges that cross shapes
+- **Edit Support**: Modify existing diagrams with natural language instructions. If any change in an edit fails, nothing is written and the AI gets the reason and the current page XML
+- **Your Edits Are Kept**: Changes you make in the browser are read before the AI edits again. If the AI overwrites a change you were still making, your version is saved in History
+- **Version History**: Click **History** at the top right of the preview page to restore one of the last 20 versions, shown as thumbnails
+- **Download and Export**: Save as `.drawio`, `.png`, `.svg`, or `.drawio.svg` (an SVG with the diagram embedded, which draw.io can open and edit again), from the **Download** button or through `export_diagram`
+- **Multi-page**: List, add, rename, and delete pages, and edit any page
+- **Auto-save**: Each session's diagram is saved to `~/.next-ai-drawio/.drawio`, so it survives a restart of the MCP client
+- **Themes and Dark Mode**: Pick a draw.io theme under **Extras > Theme**; the page follows the system dark mode
- **Self-contained**: Embedded server, works offline (except draw.io UI which loads from `embed.diagrams.net` by default, configurable via `DRAWIO_BASE_URL`)
## Available Tools
| Tool | Description |
|------|-------------|
-| `start_session` | Opens browser with real-time diagram preview |
-| `create_new_diagram` | Create a new diagram from XML (requires `xml` argument) |
+| `start_session` | Opens browser with real-time diagram preview; the result includes the drawing rules |
+| `get_drawing_guide` | Return the drawing rules again, for example after a long conversation was compacted |
+| `get_shape_library` | Return the shapes and icon styles of a library such as `aws4`, `azure2`, or `kubernetes` |
+| `create_new_diagram` | Create a new diagram from XML; a plain list of `mxCell` elements is enough |
| `load_diagram` | Load a `.drawio` file from disk into the session (handles compressed files) |
-| `edit_diagram` | Edit diagram by ID-based operations (update/add/delete cells) |
-| `get_diagram` | Get the current diagram XML |
-| `export_diagram` | Save diagram to a `.drawio`, `.png`, or `.svg` file |
+| `edit_diagram` | Edit diagram by ID-based operations (update/add/delete cells); all or nothing |
+| `get_diagram` | Get the current diagram XML, including your edits in the browser |
+| `screenshot_diagram` | Return a PNG of a page so the AI can check the rendered diagram |
+| `export_diagram` | Save diagram to a `.drawio`, `.png`, `.svg`, or `.drawio.svg` file |
| `list_pages` | List every page (tab) with id, name, index, and cell count |
| `add_page` | Append a new page without touching existing ones |
| `rename_page` | Rename a page |
| `delete_page` | Delete a page (refuses to delete the last one) |
+## Continue a Diagram Later
+
+After every change, the diagram is saved as a normal `.drawio` file in `~/.next-ai-drawio/`, and `start_session` tells the AI the file path. When you resume a conversation after restarting your MCP client (for example `claude --resume`), the AI calls `start_session` and then `load_diagram` with that path. You can also open the file in draw.io yourself.
+
+The newest 50 files are kept. Set `DRAWIO_DATA_DIR` to use another folder, or to `off` to turn auto-save off.
+
## How It Works
```
@@ -154,6 +168,8 @@ Use the standard MCP configuration with:
|----------|---------|-------------|
| `PORT` | `6002` | Port for the embedded HTTP server |
| `DRAWIO_BASE_URL` | `https://embed.diagrams.net` | Base URL for the draw.io embed. Set this to use a self-hosted draw.io instance for private deployments. |
+| `DRAWIO_DATA_DIR` | `~/.next-ai-drawio` | Folder for the auto-saved `.drawio` files. Set to `off` to turn auto-save off. |
+| `DEBUG` | unset | Set to `true` to log debug messages to stderr. |
### Private Deployment (Self-hosted draw.io)
@@ -179,7 +195,7 @@ You can deploy your own draw.io instance using the official Docker image:
docker run -d -p 8080:8080 jgraph/drawio
```
-Then set `DRAWIO_BASE_URL=http://localhost:8080` (or your server's URL).
+Then set `DRAWIO_BASE_URL=http://localhost:8080` (or your server's URL). The preview page loads nothing else from the internet, so with a local draw.io it works offline. One exception: shapes from the Material Design library show icons from `fonts.gstatic.com`.
## Troubleshooting
@@ -208,6 +224,10 @@ Call `start_session` first to open the browser window.
Check that the browser URL has the `?mcp=` query parameter. The MCP session ID connects the browser to the server.
+### Screenshot or PNG/SVG export times out
+
+PNG and SVG files are rendered by draw.io in the preview tab. Browsers slow down tabs that stay in the background, so the tab may not answer in time. Bring the preview tab to the front and try again.
+
## License
Apache-2.0
diff --git a/packages/mcp-server/package-lock.json b/packages/mcp-server/package-lock.json
index fc7a3ada..fb2ded77 100644
--- a/packages/mcp-server/package-lock.json
+++ b/packages/mcp-server/package-lock.json
@@ -1,17 +1,19 @@
{
"name": "@next-ai-drawio/mcp-server",
- "version": "0.2.3",
+ "version": "0.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@next-ai-drawio/mcp-server",
- "version": "0.2.3",
+ "version": "0.3.0",
"license": "Apache-2.0",
"dependencies": {
- "@modelcontextprotocol/sdk": "^1.0.4",
+ "@modelcontextprotocol/sdk": "^1.31.0",
"linkedom": "^0.18.0",
"open": "^11.0.0",
+ "pako": "^2.2.0",
+ "saxes": "^6.0.0",
"zod": "^4.0.0"
},
"bin": {
@@ -19,6 +21,7 @@
},
"devDependencies": {
"@types/node": "^24.0.0",
+ "@types/pako": "^2.0.4",
"tsx": "^4.19.0",
"typescript": "^5",
"vitest": "^4.1.8"
@@ -908,6 +911,13 @@
"undici-types": ">=7.24.0 <7.24.7"
}
},
+ "node_modules/@types/pako": {
+ "version": "2.0.4",
+ "resolved": "https://registry.npmjs.org/@types/pako/-/pako-2.0.4.tgz",
+ "integrity": "sha512-VWDCbrLeVXJM9fihYodcLiIv0ku+AlOa/TQ1SvYOaBuyrSKgEcro95LJyIsJ4vSo6BXIxOKxiJAat04CmST9Fw==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@vitest/expect": {
"version": "4.1.11",
"resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-4.1.11.tgz",
@@ -2600,6 +2610,22 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/pako": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/pako/-/pako-2.2.0.tgz",
+ "integrity": "sha512-zJq6RP/5q+TO2OpFV3FHzlPnFjmkb7Nc99a5SNjJE+uu/PkpChs+NIZSSzbBoD+6kjiISXjfYdwj1ZRQ81dz/w==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/puzrin"
+ },
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/nodeca"
+ }
+ ],
+ "license": "(MIT AND Zlib)"
+ },
"node_modules/parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
@@ -2834,6 +2860,18 @@
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"license": "MIT"
},
+ "node_modules/saxes": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/saxes/-/saxes-6.0.0.tgz",
+ "integrity": "sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==",
+ "license": "ISC",
+ "dependencies": {
+ "xmlchars": "^2.2.0"
+ },
+ "engines": {
+ "node": ">=v12.22.7"
+ }
+ },
"node_modules/send": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/send/-/send-1.2.1.tgz",
@@ -3379,6 +3417,12 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/xmlchars": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz",
+ "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==",
+ "license": "MIT"
+ },
"node_modules/zod": {
"version": "4.6.5",
"resolved": "https://registry.npmjs.org/zod/-/zod-4.6.5.tgz",
diff --git a/packages/mcp-server/package.json b/packages/mcp-server/package.json
index 3143eb2e..9349eb6f 100644
--- a/packages/mcp-server/package.json
+++ b/packages/mcp-server/package.json
@@ -1,6 +1,6 @@
{
"name": "@next-ai-drawio/mcp-server",
- "version": "0.2.3",
+ "version": "0.3.0",
"description": "MCP server for Next AI Draw.io - AI-powered diagram generation with real-time browser preview",
"type": "module",
"main": "dist/index.js",
@@ -8,7 +8,8 @@
"next-ai-drawio-mcp": "./dist/index.js"
},
"scripts": {
- "build": "tsc",
+ "build": "tsc && node scripts/copy-assets.mjs",
+ "check-package": "node scripts/check-package.mjs",
"dev": "tsx watch src/index.ts",
"start": "node dist/index.js",
"test": "vitest run",
@@ -38,13 +39,16 @@
"access": "public"
},
"dependencies": {
- "@modelcontextprotocol/sdk": "^1.0.4",
+ "@modelcontextprotocol/sdk": "^1.31.0",
"linkedom": "^0.18.0",
"open": "^11.0.0",
+ "pako": "^2.2.0",
+ "saxes": "^6.0.0",
"zod": "^4.0.0"
},
"devDependencies": {
"@types/node": "^24.0.0",
+ "@types/pako": "^2.0.4",
"tsx": "^4.19.0",
"typescript": "^5",
"vitest": "^4.1.8"
diff --git a/packages/mcp-server/scripts/check-package.mjs b/packages/mcp-server/scripts/check-package.mjs
new file mode 100644
index 00000000..133cbdf2
--- /dev/null
+++ b/packages/mcp-server/scripts/check-package.mjs
@@ -0,0 +1,23 @@
+// Fail if the npm package would miss files the server reads at runtime.
+// Tests run from src/ (tsx) and cannot notice a broken dist/ copy step.
+// Run after `npm run build`.
+import { execSync } from "node:child_process"
+
+const REQUIRED = [
+ "dist/index.js",
+ "dist/shape-libraries/aws4.md",
+ "dist/preview/index.html",
+ "dist/preview/preview.css",
+ "dist/preview/preview.js",
+]
+
+const [pack] = JSON.parse(
+ execSync("npm pack --dry-run --json", { encoding: "utf8" }),
+)
+const files = new Set(pack.files.map((f) => f.path))
+const missing = REQUIRED.filter((f) => !files.has(f))
+if (missing.length > 0) {
+ console.error(`npm package is missing: ${missing.join(", ")}`)
+ process.exit(1)
+}
+console.log(`npm package OK (${files.size} files)`)
diff --git a/packages/mcp-server/scripts/copy-assets.mjs b/packages/mcp-server/scripts/copy-assets.mjs
new file mode 100644
index 00000000..67f8e0e8
--- /dev/null
+++ b/packages/mcp-server/scripts/copy-assets.mjs
@@ -0,0 +1,22 @@
+// Copy non-TypeScript assets into dist/ after tsc, so they ship in the npm
+// package ("files": ["dist"]).
+import { cpSync, mkdirSync, readdirSync } from "node:fs"
+import { dirname, join } from "node:path"
+import { fileURLToPath } from "node:url"
+
+const pkg = join(dirname(fileURLToPath(import.meta.url)), "..")
+
+// Shape library docs live at the repository root, shared with the web app
+const libSrc = join(pkg, "../../docs/shape-libraries")
+const libDest = join(pkg, "dist/shape-libraries")
+mkdirSync(libDest, { recursive: true })
+for (const file of readdirSync(libSrc)) {
+ if (file.endsWith(".md") && file !== "README.md") {
+ cpSync(join(libSrc, file), join(libDest, file))
+ }
+}
+
+// Browser preview page (HTML, CSS and script)
+cpSync(join(pkg, "src/preview"), join(pkg, "dist/preview"), {
+ recursive: true,
+})
diff --git a/packages/mcp-server/src/diagram-operations.ts b/packages/mcp-server/src/diagram-operations.ts
index 4711de7b..5315f191 100644
--- a/packages/mcp-server/src/diagram-operations.ts
+++ b/packages/mcp-server/src/diagram-operations.ts
@@ -7,7 +7,10 @@
* first page is targeted (the "active page by convention" — see pages.ts).
*/
-import { findPageElement, hasPageSelector, type PageSelector } from "./pages.js"
+import { decompressPageContent } from "./load-diagram.ts"
+import { log } from "./logger.ts"
+import { findPageElement, hasPageSelector, type PageSelector } from "./pages.ts"
+import { getXmlSyntaxError } from "./xml-syntax.ts"
export interface DiagramOperation {
operation: "update" | "add" | "delete"
@@ -26,6 +29,56 @@ export interface ApplyOperationsResult {
errors: OperationError[]
}
+// Cells with links, tooltips or custom data are stored as
+// (or ): the id sits
+// on the wrapper, so the wrapper is treated as the cell.
+const CELL_SELECTOR = "mxCell, UserObject, object"
+
+/**
+ * Return the of a page, creating it when missing. An empty
+ * page gets a blank model with the "0" and "1" root cells; a page whose text
+ * is draw.io's compressed format is decompressed in place. Returns null if
+ * the text is neither empty nor decompressible.
+ */
+function ensurePageRoot(doc: Document, page: Element): Element | null {
+ const existing = page.querySelector("root")
+ if (existing) return existing
+
+ let model = page.querySelector("mxGraphModel")
+ if (!model) {
+ const text = page.textContent?.trim() ?? ""
+ if (text) {
+ const xml = decompressPageContent(text)
+ if (!xml || getXmlSyntaxError(xml)) return null
+ const parsed = new DOMParser().parseFromString(xml, "text/xml")
+ if (parsed.documentElement?.tagName !== "mxGraphModel") return null
+ page.textContent = ""
+ model = page.appendChild(
+ doc.importNode(parsed.documentElement, true),
+ ) as Element
+ const decompressedRoot = model.querySelector("root")
+ if (decompressedRoot) return decompressedRoot
+ } else {
+ model = page.appendChild(doc.createElement("mxGraphModel"))
+ }
+ }
+
+ const blank = new DOMParser().parseFromString(
+ ` `,
+ "text/xml",
+ )
+ return model.appendChild(
+ doc.importNode(blank.documentElement, true),
+ ) as Element
+}
+
+/** Read parent/source/target, which a wrapped cell keeps on its inner mxCell. */
+function cellAttr(cell: Element, name: string): string | null {
+ const inner =
+ cell.tagName === "mxCell" ? cell : cell.querySelector("mxCell")
+ return inner?.getAttribute(name) ?? null
+}
+
/**
* Apply diagram operations (update/add/delete) using ID-based lookup.
*
@@ -43,12 +96,8 @@ export function applyDiagramOperations(
): ApplyOperationsResult {
const errors: OperationError[] = []
- // Parse the XML
- const parser = new DOMParser()
- const doc = parser.parseFromString(xmlContent, "text/xml")
-
- // Check for parse errors
- const parseError = doc.querySelector("parsererror")
+ // Check for syntax errors, then parse the XML
+ const parseError = getXmlSyntaxError(xmlContent)
if (parseError) {
return {
result: xmlContent,
@@ -56,11 +105,13 @@ export function applyDiagramOperations(
{
type: "update",
cellId: "",
- message: `XML parse error: ${parseError.textContent}`,
+ message: `XML parse error: ${parseError}`,
},
],
}
}
+ const parser = new DOMParser()
+ const doc = parser.parseFromString(xmlContent, "text/xml")
// Locate the element to operate on.
//
@@ -86,7 +137,7 @@ export function applyDiagramOperations(
],
}
}
- root = found.element.querySelector("root")
+ root = ensurePageRoot(doc as unknown as Document, found.element)
if (!root) {
const pageId =
found.element.getAttribute("id") || `(index ${found.index})`
@@ -96,7 +147,7 @@ export function applyDiagramOperations(
{
type: "update",
cellId: "",
- message: `Page "${pageId}" has no element`,
+ message: `Page "${pageId}" has no element and its content could not be decompressed`,
},
],
}
@@ -132,10 +183,16 @@ export function applyDiagramOperations(
// Build a map of cell IDs to elements (scoped to the resolved page).
const cellMap = new Map()
- root.querySelectorAll("mxCell").forEach((cell) => {
+ root.querySelectorAll(CELL_SELECTOR).forEach((cell) => {
const id = cell.getAttribute("id")
- if (id) cellMap.set(id, cell)
+ // A wrapped mxCell may repeat its wrapper's id; the wrapper is the cell
+ const wrapped =
+ cell.tagName === "mxCell" &&
+ /^(UserObject|object)$/.test(cell.parentElement?.tagName ?? "")
+ if (id && !wrapped) cellMap.set(id, cell)
})
+ // Ids deleted so far in this batch; deleting one again is a no-op
+ const deletedIds = new Set()
// Process each operation
for (const op of operations) {
@@ -164,7 +221,7 @@ export function applyDiagramOperations(
`${op.new_xml} `,
"text/xml",
)
- const newCell = newDoc.querySelector("mxCell")
+ const newCell = newDoc.querySelector(CELL_SELECTOR)
if (!newCell) {
errors.push({
type: "update",
@@ -216,7 +273,7 @@ export function applyDiagramOperations(
`${op.new_xml} `,
"text/xml",
)
- const newCell = newDoc.querySelector("mxCell")
+ const newCell = newDoc.querySelector(CELL_SELECTOR)
if (!newCell) {
errors.push({
type: "add",
@@ -256,8 +313,15 @@ export function applyDiagramOperations(
const existingCell = cellMap.get(op.cell_id)
if (!existingCell) {
- // Cell not found - might have been cascade-deleted by a previous operation
- // Skip silently instead of erroring (AI may redundantly list children/edges)
+ // Skip cells already cascade-deleted by a previous operation
+ // (AI may redundantly list children/edges); warn otherwise
+ if (!deletedIds.has(op.cell_id)) {
+ errors.push({
+ type: "delete",
+ cellId: op.cell_id,
+ message: `Cell with id="${op.cell_id}" not found`,
+ })
+ }
continue
}
@@ -270,17 +334,17 @@ export function applyDiagramOperations(
cellsToDelete.add(cellId)
// Find children (cells where parent === cellId)
- // Scoped to `root` so other pages' cells with the same parent id
- // (notably "1") are never touched.
- const children = root!.querySelectorAll(
- `mxCell[parent="${cellId}"]`,
- )
- children.forEach((child) => {
- const childId = child.getAttribute("id")
- if (childId && childId !== "0" && childId !== "1") {
+ // cellMap only holds this page's cells, so other pages' cells
+ // with the same parent id (notably "1") are never touched.
+ for (const [childId, child] of cellMap) {
+ if (
+ childId !== "0" &&
+ childId !== "1" &&
+ cellAttr(child, "parent") === cellId
+ ) {
collectDescendants(childId)
}
- })
+ }
}
// Collect the target cell and all its descendants
@@ -289,23 +353,23 @@ export function applyDiagramOperations(
// Find edges referencing any of the cells to be deleted
// Also recursively collect children of those edges (e.g., edge labels)
for (const cellId of cellsToDelete) {
- const referencingEdges = root.querySelectorAll(
- `mxCell[source="${cellId}"], mxCell[target="${cellId}"]`,
- )
- referencingEdges.forEach((edge) => {
- const edgeId = edge.getAttribute("id")
+ for (const [edgeId, edge] of cellMap) {
// Protect root cells from being added via edge references
- if (edgeId && edgeId !== "0" && edgeId !== "1") {
+ if (edgeId === "0" || edgeId === "1") continue
+ if (
+ cellAttr(edge, "source") === cellId ||
+ cellAttr(edge, "target") === cellId
+ ) {
// Recurse to collect edge's children (like labels)
collectDescendants(edgeId)
}
- })
+ }
}
- // Log what will be deleted
+ // Log what will be deleted (stderr: stdout carries JSON-RPC)
if (cellsToDelete.size > 1) {
- console.log(
- `[applyDiagramOperations] Cascade delete "${op.cell_id}" → deleting ${cellsToDelete.size} cells: ${Array.from(cellsToDelete).join(", ")}`,
+ log.debug(
+ `Cascade delete "${op.cell_id}" → deleting ${cellsToDelete.size} cells: ${Array.from(cellsToDelete).join(", ")}`,
)
}
@@ -316,6 +380,7 @@ export function applyDiagramOperations(
cell.parentNode?.removeChild(cell)
cellMap.delete(cellId)
}
+ deletedIds.add(cellId)
}
}
}
diff --git a/packages/mcp-server/src/dom.ts b/packages/mcp-server/src/dom.ts
new file mode 100644
index 00000000..13972b3a
--- /dev/null
+++ b/packages/mcp-server/src/dom.ts
@@ -0,0 +1,98 @@
+/**
+ * DOM setup for Node. The XML helpers use the global DOMParser and
+ * XMLSerializer, which the browser has and Node gets from here.
+ *
+ * linkedom gives us a DOM with querySelector, but it is lenient: it never
+ * reports syntax errors (no ; xml-syntax.ts checks them), and
+ * its serializer writes raw newlines inside attribute values, which the
+ * browser reads back as spaces. serializeXml writes attribute values safely.
+ */
+import { DOMParser } from "linkedom"
+
+const ESCAPES: Record = {
+ "&": "&",
+ "<": "<",
+ ">": ">",
+ '"': """,
+ "\t": " ",
+ "\n": "
",
+ "\r": "
",
+}
+const escapeChars = (text: string, chars: RegExp) =>
+ text.replace(chars, (c) => ESCAPES[c])
+
+/**
+ * Serialize a linkedom node as XML. Attribute values escape tabs and line
+ * breaks too, so multi-line labels (value="a
b") survive a round trip.
+ */
+export function serializeXml(node: Node): string {
+ switch (node.nodeType) {
+ case 9: {
+ // Document
+ const root = (node as Document).documentElement
+ return root ? serializeXml(root) : ""
+ }
+ case 1: {
+ // Element
+ const el = node as Element
+ let out = `<${el.tagName}`
+ for (const attr of Array.from(el.attributes)) {
+ out += ` ${attr.name}="${escapeChars(attr.value, /[&<>"\t\n\r]/g)}"`
+ }
+ if (el.childNodes.length === 0) return `${out}/>`
+ out += ">"
+ for (const child of Array.from(el.childNodes)) {
+ out += serializeXml(child)
+ }
+ return `${out}${el.tagName}>`
+ }
+ case 3:
+ // Text
+ return escapeChars(node.textContent ?? "", /[&<>]/g)
+ case 4:
+ // CDATA
+ return ``
+ case 8:
+ // Comment
+ return ``
+ default:
+ return ""
+ }
+}
+
+/**
+ * XML parsers read a literal tab or line break inside an attribute value as
+ * a space (a line break written as
stays one). linkedom keeps it, and
+ * serializeXml would then write it as a real line break, so an edit would
+ * change labels it never touched. Applied to the text before linkedom.
+ */
+function normalizeAttributeWhitespace(xml: string): string {
+ return xml.replace(
+ /<[A-Za-z][^"'<>]*(?:(?:"[^"]*"|'[^']*')[^"'<>]*)*>/g,
+ (tag) =>
+ tag.replace(/"[^"]*"|'[^']*'/g, (value) =>
+ value.replace(/\r\n|[\t\n\r]/g, " "),
+ ),
+ )
+}
+
+class XmlDomParser extends DOMParser {
+ parseFromString(text: string, type: string) {
+ return super.parseFromString(
+ type.includes("xml") ? normalizeAttributeWhitespace(text) : text,
+ type as any,
+ )
+ }
+}
+
+class XMLSerializerPolyfill {
+ serializeToString(node: Node): string {
+ return serializeXml(node)
+ }
+}
+
+/** Install the DOMParser and XMLSerializer globals the XML helpers use. */
+export function installDomPolyfill(): void {
+ ;(globalThis as any).DOMParser = XmlDomParser
+ ;(globalThis as any).XMLSerializer = XMLSerializerPolyfill
+}
diff --git a/packages/mcp-server/src/drawing-guide.ts b/packages/mcp-server/src/drawing-guide.ts
new file mode 100644
index 00000000..4265a64e
--- /dev/null
+++ b/packages/mcp-server/src/drawing-guide.ts
@@ -0,0 +1,107 @@
+/**
+ * Drawing guide for the model, returned by start_session, get_drawing_guide
+ * and the diagram-workflow prompt.
+ *
+ * Adapted from the web app's system prompt (lib/system-prompts.ts) and its
+ * tool descriptions (app/api/chat/route.ts). When drawing rules change there,
+ * update this file too.
+ */
+
+import {
+ indent,
+ SWIMLANE_EXAMPLE,
+ TWO_EDGES_EXAMPLE,
+ WAYPOINT_EXAMPLE,
+} from "./xml-examples.ts"
+
+export const DRAWING_GUIDE = `# Draw.io drawing guide
+
+## Workflow
+- create_new_diagram draws a new diagram and REPLACES the whole document. add_page adds another tab. edit_diagram changes cells of an existing page. load_diagram opens a .drawio file (the server reads the file itself). get_diagram returns the current XML, including the user's manual edits. export_diagram saves to a file.
+- Before drawing, describe your layout plan in 2-3 sentences, so shapes do not overlap and edges do not cross shapes.
+- Send XML only through tool calls, never in chat text. Never draw a box just to send the user a message.
+- Before using any icon library (AWS, Azure, GCP, Kubernetes, Cisco, BPMN, Material Design, web icons...), call get_shape_library and use the exact style names it returns. NEVER guess icon style names. For AWS, use the AWS 2025 icons (library aws4).
+- After drawing or heavily editing a complex diagram, call screenshot_diagram once to see the result, and fix overlapping shapes and edges that cross shapes.
+- When replicating a diagram from an image, match its style and layout closely: straight or curved lines, rounded or square shapes.
+- The preview page has History (it saves a snapshot before every AI change and can restore any of the last 20 versions) and Download. You can make changes freely; nothing is lost.
+
+## The XML you send
+Single page (create_new_diagram, add_page): send ONLY the mxCell elements. The server adds , , and the root cells id="0" and id="1".
+
+
+
+
+
+
+
+
+Several pages at once (create_new_diagram only): send a full with one per page. Every page's must start with .
+
+Rules (XML that breaks them is rejected):
+1. All mxCell elements are siblings. NEVER nest an mxCell inside another mxCell.
+2. Ids are unique within a page and start from "2" ("0" and "1" are the root cells).
+3. parent="1" for top-level shapes, parent="" for shapes inside a container.
+4. Edge source and target must reference existing cell ids.
+5. Escape special characters in attribute values: < for <, > for >, & for &, " for ".
+6. NEVER include XML comments (). draw.io strips them.
+7. In tool arguments (JSON), every " inside the XML must be escaped as \\".
+
+Containers and swimlanes: children use the container id as parent and coordinates relative to the container.
+
+${indent(SWIMLANE_EXAMPLE)}
+
+## Layout
+- Keep every element of a page within x 0 to 800 and y 0 to 600, so the whole diagram fits one view without a page break.
+- Containers (for example AWS cloud boxes) are at most 700 pixels wide and 550 pixels tall.
+- Start near x=40, y=40 and keep elements grouped closely.
+- For large diagrams, stack vertically or use a grid instead of spreading wide.
+
+## Edge routing rules
+Rule 1: Never let two edges share a path. Two edges between the same nodes exit and enter at different points (exitY=0.3 for the first, exitY=0.7 for the second, not both 0.5).
+Rule 2: For bidirectional connections (A to B and B to A), use opposite sides: A exits right (exitX=1) into the left of B (entryX=0); B exits left (exitX=0) into the right of A (entryX=1).
+Rule 3: Always set exitX, exitY, entryX and entryY in the edge style, e.g. style="edgeStyle=orthogonalEdgeStyle;exitX=1;exitY=0.3;entryX=0;entryY=0.3;endArrow=classic;".
+Rule 4: Route edges AROUND shapes in the way. Before drawing an edge, find every shape between source and target; if one is in the path, add waypoints. Route diagonal connections along the outside of the diagram, not through the middle. Keep 20-30px clearance from shapes. An edge must never cross another shape's box.
+Rule 5: Plan the layout first. Organize shapes into rows or columns following the flow, space them 150-200px apart so edges have room, and prefer one flow direction (left to right or top to bottom).
+Rule 6: Use 2-3 waypoints for L-shaped or U-shaped paths. Each change of direction needs a waypoint, and segments should be horizontal or vertical.
+Rule 7: Use natural connection points. Never connect at corners (both X and Y 0 or 1). Top-to-bottom flow: exitY=1 into entryY=0. Left-to-right flow: exitX=1 into entryX=0. Diagonal: the side closest to the target.
+
+Before sending XML, check:
+1. Does any edge cross a shape that is not its source or target? Add waypoints.
+2. Do two edges share a path? Change their exit or entry points.
+3. Is any connection point at a corner? Use the middle of a side.
+4. Could moving shapes remove edge crossings? Revise the layout.
+
+Two edges between the same nodes:
+
+${indent(TWO_EDGES_EXAMPLE)}
+
+Waypoints go inside in the edge geometry. Example: Hotfix (right, bottom) connects to Main (center, top) while Develop (center, middle) is in between, so the edge goes right to x=750 first, then up, and enters Main from the right:
+
+${indent(WAYPOINT_EXAMPLE)}
+
+## Styles
+- Shapes: rounded=1, fillColor=#hex, strokeColor=#hex, whiteSpace=wrap;html=1;
+- Edges: endArrow=classic, block, open or none; startArrow=none or classic; curved=1; edgeStyle=orthogonalEdgeStyle
+- Text: fontSize=14, fontStyle=1 (bold), align=center, left or right
+- Animated connectors: add flowAnimation=1 to the edge style.
+
+## Minimal style
+When the user asks for a minimal, plain, black-and-white or unstyled diagram, use these rules instead of the styles above:
+- No fillColor, strokeColor, rounded, fontSize, fontStyle or hex colors.
+- Shapes use style "whiteSpace=wrap;html=1;", edges use "html=1;endArrow=classic;".
+- Containers that hold other shapes use "whiteSpace=wrap;html=1;fillColor=none;" so they do not cover their children.
+- Keep at least 50px between elements, and follow all edge routing rules strictly.
+
+## Editing with edit_diagram
+- update replaces a cell: send the complete mxCell including mxGeometry, with the same id as cell_id.
+- add inserts a new cell with a new id. One cell per operation.
+- delete removes a cell. Its children and every edge connected to it are deleted too, so give only the container's id.
+- All-or-nothing: if any operation fails, nothing is applied. A rejected call includes the current XML of the page; rebuild your operations on it and retry.
+- If the diagram is large, change it with edit_diagram instead of redrawing it.
+
+ {"operations": [{"operation": "update", "cell_id": "3", "new_xml": " "}]}
+ {"page_name": "CNN", "operations": [{"operation": "add", "cell_id": "conv-1", "new_xml": " "}]}
+ {"page_index": 1, "operations": [{"operation": "delete", "cell_id": "5"}]}
+
+Pages: list_pages shows every page's id, name and index. edit_diagram, get_diagram and export_diagram take an optional page_id, page_name or page_index; without one they use the first page.
+`
diff --git a/packages/mcp-server/src/edit-diagram.ts b/packages/mcp-server/src/edit-diagram.ts
new file mode 100644
index 00000000..a77e8e92
--- /dev/null
+++ b/packages/mcp-server/src/edit-diagram.ts
@@ -0,0 +1,109 @@
+/**
+ * Core of the edit_diagram tool, kept free of session state so it can be
+ * unit tested.
+ *
+ * All-or-nothing, like the web app (hooks/use-diagram-tool-handlers.ts):
+ * if any operation fails, nothing is written and every failure is reported,
+ * so the model never builds on a half-applied edit.
+ */
+
+import {
+ applyDiagramOperations,
+ type DiagramOperation,
+} from "./diagram-operations.ts"
+import { type PageSelector, projectPage } from "./pages.ts"
+import { validateAndFixXml, validateMxCellStructure } from "./xml-validation.ts"
+
+export type EditOutcome =
+ | { ok: true; xml: string; applied: number; fixes: string[] }
+ | { ok: false; errors: string[]; pageError: boolean }
+
+/** Number of top-level elements in an XML fragment. */
+function countTopLevelElements(fragment: string): number {
+ const doc = new DOMParser().parseFromString(
+ `${fragment} `,
+ "text/xml",
+ )
+ // On a syntax error the browser adds a element (Chrome
+ // next to the cells, Firefox as the root); the syntax is checked later
+ const root = doc.documentElement
+ if (!root || root.tagName === "parsererror") return 1
+ return Array.from(root.children).filter(
+ (el) => el.tagName !== "parsererror",
+ ).length
+}
+
+/** The target page as a one-page , or the whole document. */
+export function targetPageXml(xml: string, selector: PageSelector): string {
+ const projection = projectPage(xml, selector)
+ return projection.ok ? projection.xml : xml
+}
+
+export function editDiagram(
+ xml: string,
+ operations: DiagramOperation[],
+ selector: PageSelector,
+): EditOutcome {
+ const errors: string[] = []
+ const fixes: string[] = []
+ const prepared: DiagramOperation[] = []
+
+ for (const op of operations) {
+ if (op.operation === "delete" || !op.new_xml) {
+ prepared.push(op)
+ continue
+ }
+ // Checked before validation: several cells fail the strict parser
+ // with a misleading "only one root" syntax error.
+ if (countTopLevelElements(op.new_xml) > 1) {
+ errors.push(
+ `${op.operation} ${op.cell_id}: new_xml must contain exactly one cell; use one add operation per cell`,
+ )
+ continue
+ }
+ const check = validateAndFixXml(op.new_xml)
+ if (!check.valid) {
+ errors.push(
+ `${op.operation} ${op.cell_id}: invalid new_xml: ${check.error}`,
+ )
+ continue
+ }
+ if (check.fixed) {
+ fixes.push(`${op.cell_id}: ${check.fixes.join(", ")}`)
+ }
+ prepared.push({ ...op, new_xml: check.fixed ?? op.new_xml })
+ }
+ if (errors.length > 0) return { ok: false, errors, pageError: false }
+
+ const { result, errors: opErrors } = applyDiagramOperations(
+ xml,
+ prepared,
+ selector,
+ )
+ // An empty cellId means the page itself could not be edited
+ const pageLevel = opErrors.find((e) => e.cellId === "")
+ if (pageLevel) {
+ return { ok: false, errors: [pageLevel.message], pageError: true }
+ }
+ if (opErrors.length > 0) {
+ return {
+ ok: false,
+ errors: opErrors.map((e) => `${e.type} ${e.cellId}: ${e.message}`),
+ pageError: false,
+ }
+ }
+
+ // Validate only the target page, and reject only errors this edit
+ // introduced: problems already in other pages or in a loaded file must
+ // not block every edit.
+ const after = validateMxCellStructure(targetPageXml(result, selector))
+ if (after && !validateMxCellStructure(targetPageXml(xml, selector))) {
+ return {
+ ok: false,
+ errors: [`the edit would make the page invalid: ${after}`],
+ pageError: false,
+ }
+ }
+
+ return { ok: true, xml: result, applied: operations.length, fixes }
+}
diff --git a/packages/mcp-server/src/edit-gate.ts b/packages/mcp-server/src/edit-gate.ts
index b3b75ee9..f80240b2 100644
--- a/packages/mcp-server/src/edit-gate.ts
+++ b/packages/mcp-server/src/edit-gate.ts
@@ -17,7 +17,14 @@
* change: the set of pages, each page's name, and each page's cell tree
* (tags + sorted attributes + text). Byte equality is kept as a fast path.
*/
-import { isMxGraphModel, normalizeToMxfile, parseMxfile } from "./pages.js"
+import {
+ findPageElement,
+ isMxGraphModel,
+ normalizeToMxfile,
+ type PageSelector,
+ parseMxfile,
+ serializeMxfile,
+} from "./pages.ts"
export type EditGateResult =
| { ok: true }
@@ -100,3 +107,38 @@ export function checkEditGate(
}
return { ok: true }
}
+
+/**
+ * The model was shown only the selected page of liveXml (get_diagram with a
+ * page selector, or a rejected edit's error). It has seen the whole document
+ * if the other pages are as it last saw them, or if it saw nothing before
+ * (it then has no old copy of them to edit from). Returns the new
+ * lastSeenXml: liveXml, or lastSeenXml unchanged.
+ */
+export function markPageSeen(
+ lastSeenXml: string,
+ liveXml: string,
+ selector: PageSelector,
+): string {
+ const parse = (xml: string) => {
+ const normalized = normalizeToMxfile(xml)
+ return normalized ? parseMxfile(normalized) : null
+ }
+ // An empty record also follows load_diagram and page tools that wrote
+ // over unseen changes, when the model may remember older pages: only
+ // a one-page document is then fully seen
+ if (!lastSeenXml) {
+ const pages = parse(liveXml)?.querySelectorAll("diagram").length ?? 1
+ return pages <= 1 ? liveXml : lastSeenXml
+ }
+ const otherPages = (xml: string) => {
+ const doc = parse(xml)
+ if (!doc) return null
+ findPageElement(doc, selector)?.element.remove()
+ return contentFingerprint(serializeMxfile(doc))
+ }
+ const before = otherPages(lastSeenXml)
+ return before !== null && before === otherPages(liveXml)
+ ? liveXml
+ : lastSeenXml
+}
diff --git a/packages/mcp-server/src/exclusive.ts b/packages/mcp-server/src/exclusive.ts
new file mode 100644
index 00000000..364f1c15
--- /dev/null
+++ b/packages/mcp-server/src/exclusive.ts
@@ -0,0 +1,25 @@
+/**
+ * A queue for tool handlers: each call waits until the previous one ended.
+ * A tool call the client cancelled while it waited (the MCP SDK aborts its
+ * extra.signal, the handler's last argument) is skipped.
+ */
+export function createExclusive() {
+ let tail: Promise = Promise.resolve()
+ return function exclusive Promise>(
+ handler: T,
+ ): T {
+ return ((...args: unknown[]) => {
+ const extra = args.at(-1) as { signal?: AbortSignal } | undefined
+ const run = tail.then(() =>
+ extra?.signal?.aborted
+ ? {
+ content: [{ type: "text", text: "Cancelled." }],
+ isError: true,
+ }
+ : handler(...args),
+ )
+ tail = run.catch(() => {})
+ return run
+ }) as T
+ }
+}
diff --git a/packages/mcp-server/src/history.ts b/packages/mcp-server/src/history.ts
index 492eda1b..7fff09a8 100644
--- a/packages/mcp-server/src/history.ts
+++ b/packages/mcp-server/src/history.ts
@@ -3,10 +3,18 @@
* Stores {xml, svg} entries in a circular buffer
*/
-import { log } from "./logger.js"
+import { log } from "./logger.ts"
const MAX_HISTORY = 20
-const historyStore = new Map>()
+
+interface HistoryEntry {
+ id: number // Stable across shifts of the circular buffer
+ xml: string
+ svg: string
+}
+
+let nextEntryId = 0
+const historyStore = new Map()
export function addHistory(sessionId: string, xml: string, svg = ""): number {
let history = historyStore.get(sessionId)
@@ -15,13 +23,16 @@ export function addHistory(sessionId: string, xml: string, svg = ""): number {
historyStore.set(sessionId, history)
}
- // Dedupe: skip if same as last entry
+ // Dedupe: skip if same as last entry (any other change, also of page
+ // settings only, is a new version; keepInHistory leaves out draw.io's
+ // copy of a server write)
const last = history[history.length - 1]
- if (last?.xml === xml) {
+ if (last && last.xml === xml) {
+ if (svg && !last.svg) last.svg = svg
return history.length - 1
}
- history.push({ xml, svg })
+ history.push({ id: nextEntryId++, xml, svg })
// Circular buffer
if (history.length > MAX_HISTORY) {
@@ -32,29 +43,35 @@ export function addHistory(sessionId: string, xml: string, svg = ""): number {
return history.length - 1
}
-export function getHistory(
- sessionId: string,
-): Array<{ xml: string; svg: string }> {
+export function getHistory(sessionId: string): HistoryEntry[] {
return historyStore.get(sessionId) || []
}
+/** Look up an entry by its id; the array index shifts as old entries drop. */
export function getHistoryEntry(
sessionId: string,
- index: number,
-): { xml: string; svg: string } | undefined {
- const history = historyStore.get(sessionId)
- return history?.[index]
+ id: number,
+): HistoryEntry | undefined {
+ return historyStore.get(sessionId)?.find((entry) => entry.id === id)
}
export function clearHistory(sessionId: string): void {
historyStore.delete(sessionId)
}
-export function updateLastHistorySvg(sessionId: string, svg: string): boolean {
+/**
+ * Give the last entry the image the browser took of shownXml, the diagram
+ * it just loaded, when that entry is this diagram
+ */
+export function updateLastHistorySvg(
+ sessionId: string,
+ svg: string,
+ shownXml: string,
+): boolean {
const history = historyStore.get(sessionId)
if (!history || history.length === 0) return false
const last = history[history.length - 1]
- if (!last.svg) {
+ if (!last.svg && last.xml === shownXml) {
last.svg = svg
return true
}
diff --git a/packages/mcp-server/src/http-server.ts b/packages/mcp-server/src/http-server.ts
index f23a2455..26170052 100644
--- a/packages/mcp-server/src/http-server.ts
+++ b/packages/mcp-server/src/http-server.ts
@@ -3,7 +3,11 @@
* Serves draw.io embed with state sync and history UI
*/
+import { randomUUID } from "node:crypto"
+import { readFileSync } from "node:fs"
import http from "node:http"
+import { dirname, join } from "node:path"
+import { fileURLToPath } from "node:url"
const MAX_BODY_BYTES = 10 * 1024 * 1024 // 10 MiB
@@ -12,29 +16,44 @@ function readBody(
res: http.ServerResponse,
cb: (body: string) => void,
): void {
- let body = ""
+ // Decode once at the end: a multi-byte UTF-8 character can be split
+ // across two chunks.
+ const chunks: Buffer[] = []
let size = 0
+ let tooLarge = false
req.on("data", (chunk: Buffer) => {
+ if (tooLarge) return
size += chunk.length
if (size > MAX_BODY_BYTES) {
- res.writeHead(413, { "Content-Type": "application/json" })
- res.end(JSON.stringify({ error: "Payload too large" }))
- req.destroy()
+ // Read the rest without keeping it and answer at the end: a
+ // connection closed mid-upload reaches the browser as a network
+ // error, without this answer
+ tooLarge = true
+ chunks.length = 0
return
}
- body += chunk
+ chunks.push(chunk)
+ })
+ req.on("end", () => {
+ if (tooLarge) {
+ res.writeHead(413, { "Content-Type": "application/json" })
+ res.end(JSON.stringify({ error: "Payload too large" }))
+ return
+ }
+ cb(Buffer.concat(chunks).toString("utf8"))
})
- req.on("end", () => cb(body))
}
+import { contentFingerprint } from "./edit-gate.ts"
import {
addHistory,
clearHistory,
getHistory,
getHistoryEntry,
updateLastHistorySvg,
-} from "./history.js"
-import { log } from "./logger.js"
+} from "./history.ts"
+import { log } from "./logger.ts"
+import { BLANK_MXFILE } from "./pages.ts"
// Configurable draw.io embed URL for private deployments
const DRAWIO_BASE_URL =
@@ -52,19 +71,17 @@ function getOrigin(url: string): string {
const DRAWIO_ORIGIN = getOrigin(DRAWIO_BASE_URL)
-// Minimal blank diagram used to bootstrap new sessions.
-// This avoids the draw.io embed spinner (spin=1) getting stuck when no `load(xml)` is ever sent.
-const DEFAULT_DIAGRAM_XML = ` `
-
// Normalize URL for iframe src - ensure no double slashes
function normalizeUrl(url: string): string {
// Remove trailing slash to avoid double slashes
return url.replace(/\/$/, "")
}
-function isLikelyMcpSessionId(sessionId: string): boolean {
- // Keep this cheap and conservative to avoid creating state for arbitrary IDs.
- return sessionId.startsWith("mcp-") && sessionId.length <= 128
+// Session ids look like "mcp--" (start_session).
+// Only this charset is accepted, because ids are written into the page's
+// HTML and script and into the redirect Location header.
+function isValidSessionId(sessionId: string): boolean {
+ return /^mcp-[a-z0-9-]{1,64}$/.test(sessionId)
}
// Find the most recent active session (for auto-redirect when no sessionId provided)
@@ -78,25 +95,75 @@ function getMostRecentSessionId(): string | null {
return mostRecent?.id || null
}
+/**
+ * Give a session whose state is gone (it expired, or the MCP process
+ * restarted) its auto-saved diagram back. The MCP tools call this before
+ * they read the state, so they never build on an older copy and then
+ * overwrite the file. Not a change worth saving again.
+ */
+export function restoreSavedSession(sessionId: string): void {
+ if (stateStore.has(sessionId) || !isValidSessionId(sessionId)) return
+ const saved = savedStateLoader?.(sessionId)
+ if (saved) setState(sessionId, saved, undefined, false, false)
+}
+
function ensureSessionStateInitialized(sessionId: string): void {
if (!sessionId) return
- if (!isLikelyMcpSessionId(sessionId)) return
+ if (!isValidSessionId(sessionId)) return
+ restoreSavedSession(sessionId)
if (stateStore.has(sessionId)) return
- setState(sessionId, DEFAULT_DIAGRAM_XML)
+ // Not a change worth saving: the browser fills it on its next push
+ // A blank diagram keeps the draw.io spinner (spin=1) from waiting
+ // forever when no load(xml) is ever sent
+ setState(sessionId, BLANK_MXFILE, undefined, false, false)
+ // Nothing is known about this session: a tab that still shows it keeps
+ // its diagram
+ const state = stateStore.get(sessionId)
+ if (state) state.blank = true
}
interface SessionState {
xml: string
version: number
+ // Made when the state is created (first use, or again after it expired
+ // or the MCP process restarted) and kept by every write. A tab tells by
+ // it that the server lost what it knew, and every push names the state
+ // it was based on, so one based on a lost state is refused.
+ stateId: string
+ // Created blank because nothing was saved; cleared by the first write
+ blank?: boolean
+ // Version of the last write the browser did not make itself (AI edit,
+ // restore). A browser push based on an older version is rejected.
+ serverVersion?: number
+ // The XML of that write: what a thumbnail taken after loading it shows
+ serverXml?: string
+ // The browser saved a change of the user's since that write (a sync
+ // reply is no change)
+ userEdited?: boolean
lastUpdated: Date
+ lastPolled?: number // Last browser poll; an open tab keeps the session alive
svg?: string // Cached SVG from last browser save
syncRequested?: number // Timestamp when sync requested, cleared when browser responds
- exportFormat?: "png" | "svg" // Set by MCP tool to request browser export
+ exportFormat?: ExportFormat // Set by MCP tool to request browser export
exportXml?: string // Single-page projection to load before a page-targeted export
+ exportOptions?: ExportOptions // Extra draw.io export parameters (PNG only)
+ exportId?: number // Number of the pending export, echoed with its result
exportData?: string // Base64/SVG data returned by browser after export
}
+/** draw.io export formats; xmlsvg is an SVG with the diagram embedded */
+export type ExportFormat = "png" | "svg" | "xmlsvg"
+
+/**
+ * draw.io's PNG export takes these directly: width caps the image size
+ * (never upscales), pageId renders a page other than the one on screen.
+ */
+export interface ExportOptions {
+ width?: number
+ pageId?: string
+}
+
export const stateStore = new Map()
let server: http.Server | null = null
@@ -108,23 +175,76 @@ export function getState(sessionId: string): SessionState | undefined {
return stateStore.get(sessionId)
}
-export function setState(sessionId: string, xml: string, svg?: string): number {
+// Called after every state change (AI write, browser push, restore)
+let stateListener: ((sessionId: string, xml: string) => void) | null = null
+
+export function onStateChange(
+ listener: (sessionId: string, xml: string) => void,
+): void {
+ stateListener = listener
+}
+
+// Reads a session's saved diagram when its state is created again (it
+// expired, or the MCP process restarted)
+let savedStateLoader: ((sessionId: string) => string | null) | null = null
+
+export function onSessionRecreate(
+ loader: (sessionId: string) => string | null,
+): void {
+ savedStateLoader = loader
+}
+
+export function setState(
+ sessionId: string,
+ xml: string,
+ svg?: string,
+ fromBrowser = false,
+ notify = true,
+): number {
const existing = stateStore.get(sessionId)
const newVersion = (existing?.version || 0) + 1
stateStore.set(sessionId, {
xml,
version: newVersion,
+ stateId: existing?.stateId ?? randomUUID(),
+ serverVersion: fromBrowser ? existing?.serverVersion : newVersion,
+ serverXml: fromBrowser ? existing?.serverXml : xml,
+ userEdited: fromBrowser ? existing?.userEdited : false,
lastUpdated: new Date(),
- svg: svg || existing?.svg, // Preserve cached SVG if not provided
+ lastPolled: existing?.lastPolled,
+ // The image of this XML, never an older one's: a write without an
+ // image (AI write, sync reply) leaves none until the browser sends
+ // it, unless it is the same XML
+ svg:
+ svg ||
+ (existing && existing.xml === xml ? existing.svg : undefined),
syncRequested: undefined, // Clear sync request when browser pushes state
exportFormat: existing?.exportFormat, // Preserve pending export request
exportXml: existing?.exportXml, // Preserve pending projection
+ exportOptions: existing?.exportOptions,
+ exportId: existing?.exportId,
exportData: existing?.exportData, // Preserve export result
})
log.debug(`State updated: session=${sessionId}, version=${newVersion}`)
+ if (notify) stateListener?.(sessionId, xml)
return newVersion
}
+/**
+ * Keep the session's diagram in History before a write replaces it.
+ * Nothing to keep when the browser saved no change of the user's since the
+ * last server write and History ends with that write: the state is that
+ * write, or draw.io's own copy of it from a sync (other text, same diagram).
+ */
+export function keepInHistory(sessionId: string, xml: string, svg = ""): void {
+ const state = stateStore.get(sessionId)
+ const last = getHistory(sessionId).at(-1)
+ if (state && !state.userEdited && last && last.xml === state.serverXml) {
+ return
+ }
+ addHistory(sessionId, xml, svg)
+}
+
/**
* Ask the browser bridge to export the current diagram as png/svg.
*
@@ -138,17 +258,24 @@ export function setState(sessionId: string, xml: string, svg?: string): number {
*/
export function requestExport(
sessionId: string,
- format: "png" | "svg",
+ format: ExportFormat,
projectionXml?: string,
+ options?: ExportOptions,
): boolean {
const state = stateStore.get(sessionId)
if (!state) return false
state.exportData = undefined
state.exportXml = projectionXml
+ state.exportOptions = options
state.exportFormat = format
+ // The browser sends this back with the result, so a late result of an
+ // export that timed out is not taken for this one
+ state.exportId = ++lastExportId
return true
}
+let lastExportId = 0
+
export function requestSync(sessionId: string): boolean {
const state = stateStore.get(sessionId)
if (state) {
@@ -222,7 +349,11 @@ export function stopHttpServer(): void {
function cleanupExpiredSessions(): void {
const now = Date.now()
for (const [sessionId, state] of stateStore) {
- if (now - state.lastUpdated.getTime() > SESSION_TTL) {
+ const lastActive = Math.max(
+ state.lastUpdated.getTime(),
+ state.lastPolled ?? 0,
+ )
+ if (now - lastActive > SESSION_TTL) {
stateStore.delete(sessionId)
clearHistory(sessionId)
log.info(`Cleaned up expired session: ${sessionId}`)
@@ -245,7 +376,49 @@ function handleRequest(
req: http.IncomingMessage,
res: http.ServerResponse,
): void {
- const url = new URL(req.url || "/", `http://localhost:${serverPort}`)
+ // A bad request must never take down the MCP process
+ try {
+ routeRequest(req, res)
+ } catch (err) {
+ log.error("HTTP request failed:", err)
+ if (!res.headersSent) res.writeHead(500)
+ res.end()
+ }
+}
+
+// Serve only requests addressed to localhost, sent by the preview page
+// itself (Origin is the address it was opened at, the Host) or by a
+// non-browser client (no Origin header). This blocks DNS rebinding, other
+// websites, and pages on other localhost ports, whose plain text POSTs need
+// no CORS preflight.
+function isLocalRequest(req: http.IncomingMessage): boolean {
+ const host = req.headers.host ?? ""
+ const origin = req.headers.origin
+ return (
+ /^(localhost|127\.0\.0\.1)(:\d+)?$/.test(host) &&
+ (origin === undefined || origin === `http://${host}`)
+ )
+}
+
+function routeRequest(
+ req: http.IncomingMessage,
+ res: http.ServerResponse,
+): void {
+ let url: URL
+ try {
+ url = new URL(req.url || "/", `http://localhost:${serverPort}`)
+ } catch {
+ // e.g. "//" is not a valid URL path
+ res.writeHead(400)
+ res.end("Bad Request")
+ return
+ }
+
+ if (!isLocalRequest(req)) {
+ res.writeHead(403)
+ res.end("Forbidden")
+ return
+ }
const requestOrigin = req.headers.origin
if (requestOrigin === `http://localhost:${serverPort}`) {
@@ -262,12 +435,19 @@ function handleRequest(
if (url.pathname === "/" || url.pathname === "/index.html") {
const sessionId = url.searchParams.get("mcp") || ""
+ if (sessionId && !isValidSessionId(sessionId)) {
+ res.writeHead(400)
+ res.end("Invalid session id")
+ return
+ }
// Auto-redirect to most recent session if no sessionId provided
if (!sessionId) {
const recentSessionId = getMostRecentSessionId()
if (recentSessionId) {
- res.writeHead(302, { Location: `/?mcp=${recentSessionId}` })
+ res.writeHead(302, {
+ Location: `/?mcp=${encodeURIComponent(recentSessionId)}`,
+ })
res.end()
return
}
@@ -305,14 +485,21 @@ function handleStateApi(
}
ensureSessionStateInitialized(sessionId)
const state = stateStore.get(sessionId)
+ // Polling counts as activity, so a session stays alive while its
+ // tab is open
+ if (state) state.lastPolled = Date.now()
res.writeHead(200, { "Content-Type": "application/json" })
res.end(
JSON.stringify({
xml: state?.xml || null,
version: state?.version || 0,
+ stateId: state?.stateId ?? null,
+ blank: !!state?.blank,
syncRequested: !!state?.syncRequested,
exportFormat: state?.exportFormat || null,
exportXml: state?.exportXml || null,
+ exportOptions: state?.exportOptions || null,
+ exportId: state?.exportId ?? null,
}),
)
} else if (req.method === "POST") {
@@ -320,29 +507,124 @@ function handleStateApi(
try {
const data = JSON.parse(body)
const { sessionId } = data
- if (!sessionId) {
+ if (!sessionId || !isValidSessionId(sessionId)) {
res.writeHead(400, { "Content-Type": "application/json" })
- res.end(JSON.stringify({ error: "sessionId required" }))
+ res.end(
+ JSON.stringify({ error: "valid sessionId required" }),
+ )
return
}
// Browser is returning export data (png/svg)
if (data.exportData !== undefined) {
const state = stateStore.get(sessionId)
- if (state) {
+ if (state && data.exportId === state.exportId) {
state.exportData = data.exportData
state.exportFormat = undefined
state.exportXml = undefined
+ state.exportOptions = undefined
+ state.exportId = undefined
log.debug(
`Export data received for session=${sessionId}`,
)
+ } else if (state) {
+ log.debug(
+ `Ignored a late export result for session=${sessionId}`,
+ )
}
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ success: true }))
return
}
- const version = setState(sessionId, data.xml, data.svg)
+ // A push can come before the tab's first poll after a
+ // restart: recover the saved file first, so it is compared
+ // with that and never overwrites it unseen
+ ensureSessionStateInitialized(sessionId)
+ const current = stateStore.get(sessionId)
+
+ // A tab of this version names the state its push is based
+ // on. Another state (the server lost the one it knew, or
+ // the tab has not polled yet): refused, and the tab's next
+ // poll decides whose diagram wins.
+ if (current && "stateId" in data) {
+ if (data.stateId !== current.stateId) {
+ res.writeHead(409, {
+ "Content-Type": "application/json",
+ })
+ res.end(
+ JSON.stringify({
+ error: "Session was recreated",
+ stateChanged: true,
+ version: current.version,
+ }),
+ )
+ return
+ }
+ // What a recovering tab showed: kept in history only
+ if (data.source === "recover") {
+ const saved =
+ typeof data.xml === "string" &&
+ !!data.xml &&
+ data.xml !== current.xml
+ if (saved) {
+ addHistory(sessionId, data.xml, data.svg || "")
+ }
+ res.writeHead(409, {
+ "Content-Type": "application/json",
+ })
+ res.end(
+ JSON.stringify({
+ error: "Diagram changed on the server",
+ version: current.version,
+ savedToHistory: saved,
+ }),
+ )
+ return
+ }
+ }
+
+ // The browser edited a version older than the latest AI write
+ // (it has not loaded that write yet). Keep the AI write; the
+ // browser loads it on its next poll. A sync reply is also
+ // stale after a newer write of the browser's own (a user
+ // edit saved while the export ran).
+ if (
+ typeof data.baseVersion === "number" &&
+ (data.baseVersion < (current?.serverVersion ?? 0) ||
+ (data.source === "sync" &&
+ data.baseVersion < (current?.version ?? 0)))
+ ) {
+ let savedToHistory = false
+ if (data.source === "sync") {
+ // A stale sync reply: the store already holds the
+ // newer AI write, so the sync is done.
+ if (current) current.syncRequested = undefined
+ } else if (typeof data.xml === "string" && data.xml) {
+ // A user edit lost the race with an AI write. Keep
+ // it in history so the user can restore it.
+ addHistory(sessionId, data.xml, data.svg || "")
+ savedToHistory = true
+ }
+ res.writeHead(409, { "Content-Type": "application/json" })
+ res.end(
+ JSON.stringify({
+ error: "Diagram changed on the server",
+ version: current?.version,
+ savedToHistory,
+ }),
+ )
+ return
+ }
+
+ if (typeof data.xml !== "string") {
+ res.writeHead(400, { "Content-Type": "application/json" })
+ res.end(JSON.stringify({ error: "xml must be a string" }))
+ return
+ }
+ const version = setState(sessionId, data.xml, data.svg, true)
+ const saved = stateStore.get(sessionId)
+ if (saved && data.source !== "sync") saved.userEdited = true
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ success: true, version }))
} catch {
@@ -378,7 +660,11 @@ function handleHistoryApi(
res.writeHead(200, { "Content-Type": "application/json" })
res.end(
JSON.stringify({
- entries: history.map((entry, i) => ({ index: i, svg: entry.svg })),
+ entries: history.map((entry, i) => ({
+ index: i,
+ id: entry.id,
+ svg: entry.svg,
+ })),
count: history.length,
}),
)
@@ -396,26 +682,36 @@ function handleRestoreApi(
readBody(req, res, (body) => {
try {
- const { sessionId, index } = JSON.parse(body)
- if (!sessionId || index === undefined) {
+ const { sessionId, id } = JSON.parse(body)
+ if (!sessionId || typeof id !== "number") {
res.writeHead(400, { "Content-Type": "application/json" })
- res.end(
- JSON.stringify({ error: "sessionId and index required" }),
- )
+ res.end(JSON.stringify({ error: "sessionId and id required" }))
return
}
- const entry = getHistoryEntry(sessionId, index)
+ const entry = getHistoryEntry(sessionId, id)
if (!entry) {
res.writeHead(404, { "Content-Type": "application/json" })
res.end(JSON.stringify({ error: "Entry not found" }))
return
}
+ // Edits in the browser since the last entry are not in history
+ // yet: keep them, so the restore can be undone
+ // (any state besides a blank page; a cleared document with its
+ // own pages counts)
+ const current = stateStore.get(sessionId)
+ if (
+ current &&
+ contentFingerprint(current.xml) !==
+ contentFingerprint(BLANK_MXFILE)
+ ) {
+ keepInHistory(sessionId, current.xml, current.svg)
+ }
const newVersion = setState(sessionId, entry.xml)
addHistory(sessionId, entry.xml, entry.svg)
- log.info(`Restored session ${sessionId} to index ${index}`)
+ log.info(`Restored session ${sessionId} to history entry ${id}`)
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ success: true, newVersion }))
@@ -438,14 +734,27 @@ function handleHistorySvgApi(
readBody(req, res, (body) => {
try {
- const { sessionId, svg } = JSON.parse(body)
+ const { sessionId, svg, stateId, version } = JSON.parse(body)
if (!sessionId || !svg) {
res.writeHead(400, { "Content-Type": "application/json" })
res.end(JSON.stringify({ error: "sessionId and svg required" }))
return
}
- updateLastHistorySvg(sessionId, svg)
+ // The browser took it of the server write it loaded, named by
+ // the state and version. One that arrives after the next server
+ // write, or for a state since lost, is dropped; a browser write
+ // since (a sync reply) leaves that write's image valid.
+ const state = stateStore.get(sessionId)
+ if (
+ state &&
+ state.stateId === stateId &&
+ state.serverVersion === version &&
+ state.serverXml !== undefined
+ ) {
+ updateLastHistorySvg(sessionId, svg, state.serverXml)
+ if (state.xml === state.serverXml) state.svg = svg
+ }
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ success: true }))
} catch {
@@ -455,571 +764,34 @@ function handleHistorySvgApi(
})
}
-function getHtmlPage(sessionId: string): string {
- return `
-
-
-
-
- Next AI Draw.io
-
-
-
-
-
-
-
-
-
-
-
-
-
- Format
-
- Draw.io (.drawio)
- PNG Image (.png)
- SVG Vector (.svg)
-
-
-
-
-
-
-
-
-
-`
+function loadPreviewTemplate(): string {
+ if (!previewTemplate) {
+ const read = (file: string) =>
+ readFileSync(join(PREVIEW_DIR, file), "utf-8")
+ previewTemplate = read("index.html")
+ .replace("{{CSS}}", () => read("preview.css"))
+ .replace("{{SCRIPT}}", () => read("preview.js"))
+ }
+ return previewTemplate
+}
+
+/** A JSON string literal that is safe inside a
+
+