import type { Message, ToolCall } from "../types"; import { ANTHROPIC_THINKING_TAG_PREFIXES, AnthropicInbandScanner, type AnthropicInbandScannerConfig, } from "./anthropic"; import { buildArgShapes, type ToolArgShape } from "./coercion"; import dialectPrompt from "./minimax.md" with { type: "text" }; import { escapeXmlAttr, escapeXmlText, renderDelimitedThinking, renderLegacyTextTranscript, stringifyJson, } from "./rendering"; import type { DialectDefinition, DialectRenderOptions, DialectToolResult } from "./types"; const MINIMAX_WRAPPER_TAGS: Readonly> = { tool_call: true }; const MINIMAX_BASE_TAG_PREFIXES = [ "\n${renderInvokes(calls, options.tools ?? [])}\n`; } function renderToolResults(results: readonly DialectToolResult[]): string { const body = results .map(result => { const tag = result.isError ? "error" : "result"; const streamTag = result.isError ? "stderr" : "stdout"; return `<${tag}>\n${escapeXmlText(result.name)}\n<${streamTag}>${result.text}\n`; }) .join("\n"); return `\n${body}\n`; } function renderThinking(text: string): string { return renderDelimitedThinking("", "", text); } function renderTranscript(messages: readonly Message[], options: DialectRenderOptions = {}): string { return renderLegacyTextTranscript(messages, options, { renderThinking, renderCalls: renderAssistantToolCalls, renderResults: renderToolResults, }); } function renderInvoke(call: ToolCall, shape: ToolArgShape | undefined): string { let body = ``; for (const key in call.arguments) { const value = call.arguments[key]; const isString = shape?.stringArgs.has(key) === true; const rendered = isString && typeof value === "string" ? value : stringifyJson(value); body += `${rendered}`; } return `${body}`; } function renderInvokes(calls: readonly ToolCall[], tools: NonNullable): string { const shapes = buildArgShapes(tools); return calls.map(call => renderInvoke(call, shapes.get(call.name))).join("\n"); } const definition: DialectDefinition = { dialect: "minimax", prompt: dialectPrompt, createScanner: options => new AnthropicInbandScanner(options, MINIMAX_SCANNER_CONFIG), renderToolCall, renderAssistantToolCalls, renderToolResults, renderThinking, renderTranscript, }; export default definition;