|
| 1 | +import type { RuntimeToolSchemaDiagnostic } from "./tool-schema-projection.js"; |
| 2 | +import { projectRuntimeToolInputSchema } from "./tool-schema-projection.js"; |
| 3 | + |
| 4 | +type ToolProjectionField = "name" | "description" | "parameters"; |
| 5 | +type AnthropicToolChoiceMode = "auto" | "any" | "none"; |
| 6 | +type AnthropicToolChoiceWithDisable = { |
| 7 | + readonly disable_parallel_tool_use?: boolean; |
| 8 | +}; |
| 9 | +export type AnthropicResolvedToolChoice = |
| 10 | + | AnthropicToolChoiceMode |
| 11 | + | ({ readonly type: "auto" } & AnthropicToolChoiceWithDisable) |
| 12 | + | ({ readonly type: "any" } & AnthropicToolChoiceWithDisable) |
| 13 | + | { readonly type: "none" } |
| 14 | + | ({ readonly type: "tool"; readonly name: string } & AnthropicToolChoiceWithDisable); |
| 15 | + |
| 16 | +type AnthropicToolProjectionDescriptor = { |
| 17 | + readonly [key in ToolProjectionField]?: unknown; |
| 18 | +}; |
| 19 | + |
| 20 | +export type AnthropicProjectedTool = { |
| 21 | + readonly originalIndex: number; |
| 22 | + readonly name: string; |
| 23 | + readonly description?: string; |
| 24 | + readonly parameters: Record<string, unknown> & { required?: string[] }; |
| 25 | +}; |
| 26 | + |
| 27 | +export type AnthropicToolProjectionSnapshot = { |
| 28 | + readonly tools: readonly AnthropicProjectedTool[]; |
| 29 | + readonly diagnostics: readonly RuntimeToolSchemaDiagnostic[]; |
| 30 | +}; |
| 31 | + |
| 32 | +function readObjectStringField(value: unknown, field: string): string | undefined { |
| 33 | + return isRecord(value) && typeof value[field] === "string" ? value[field] : undefined; |
| 34 | +} |
| 35 | + |
| 36 | +function readDisableParallelToolUse(value: unknown): AnthropicToolChoiceWithDisable { |
| 37 | + return isRecord(value) && typeof value.disable_parallel_tool_use === "boolean" |
| 38 | + ? { disable_parallel_tool_use: value.disable_parallel_tool_use } |
| 39 | + : {}; |
| 40 | +} |
| 41 | + |
| 42 | +function isAnthropicToolChoiceMode(value: string): value is AnthropicToolChoiceMode { |
| 43 | + return value === "auto" || value === "any" || value === "none"; |
| 44 | +} |
| 45 | + |
| 46 | +function readToolField( |
| 47 | + tool: object, |
| 48 | + field: ToolProjectionField, |
| 49 | +): { ok: true; value: unknown } | { ok: false } { |
| 50 | + try { |
| 51 | + return { ok: true, value: Reflect.get(tool, field) }; |
| 52 | + } catch { |
| 53 | + return { ok: false }; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function readToolEntry( |
| 58 | + tools: readonly AnthropicToolProjectionDescriptor[], |
| 59 | + toolIndex: number, |
| 60 | +): { ok: true; tool: unknown } | { ok: false } { |
| 61 | + try { |
| 62 | + return { ok: true, tool: Reflect.get(tools, String(toolIndex)) }; |
| 63 | + } catch { |
| 64 | + return { ok: false }; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 69 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 70 | +} |
| 71 | + |
| 72 | +function unreadableDiagnostic(toolIndex: number): RuntimeToolSchemaDiagnostic { |
| 73 | + return { |
| 74 | + toolName: `tool[${toolIndex}]`, |
| 75 | + toolIndex, |
| 76 | + violations: [`tool[${toolIndex}] is unreadable`], |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +function normalizeRequired(value: unknown): string[] | undefined { |
| 81 | + return Array.isArray(value) |
| 82 | + ? value.filter((entry): entry is string => typeof entry === "string") |
| 83 | + : undefined; |
| 84 | +} |
| 85 | + |
| 86 | +export function snapshotAnthropicToolProjectionInputs( |
| 87 | + tools: readonly AnthropicToolProjectionDescriptor[] | undefined, |
| 88 | +): AnthropicToolProjectionSnapshot { |
| 89 | + if (!tools) { |
| 90 | + return { tools: [], diagnostics: [] }; |
| 91 | + } |
| 92 | + |
| 93 | + let length: number; |
| 94 | + try { |
| 95 | + length = tools.length; |
| 96 | + } catch { |
| 97 | + return { tools: [], diagnostics: [unreadableDiagnostic(0)] }; |
| 98 | + } |
| 99 | + |
| 100 | + const projectedTools: AnthropicProjectedTool[] = []; |
| 101 | + const diagnostics: RuntimeToolSchemaDiagnostic[] = []; |
| 102 | + for (let toolIndex = 0; toolIndex < length; toolIndex += 1) { |
| 103 | + const entry = readToolEntry(tools, toolIndex); |
| 104 | + if (!entry.ok || !isRecord(entry.tool)) { |
| 105 | + diagnostics.push(unreadableDiagnostic(toolIndex)); |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + const name = readToolField(entry.tool, "name"); |
| 110 | + const toolName = |
| 111 | + name.ok && typeof name.value === "string" && name.value ? name.value : `tool[${toolIndex}]`; |
| 112 | + const descriptorViolations = name.ok ? [] : [`${toolName}.name is unreadable`]; |
| 113 | + if (!name.ok || typeof name.value !== "string" || !name.value) { |
| 114 | + diagnostics.push({ |
| 115 | + toolName, |
| 116 | + toolIndex, |
| 117 | + violations: |
| 118 | + descriptorViolations.length > 0 |
| 119 | + ? descriptorViolations |
| 120 | + : [`${toolName}.name must be a non-empty string`], |
| 121 | + }); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + const parameters = readToolField(entry.tool, "parameters"); |
| 126 | + if (!parameters.ok) { |
| 127 | + diagnostics.push({ |
| 128 | + toolName, |
| 129 | + toolIndex, |
| 130 | + violations: [`${toolName}.parameters is unreadable`], |
| 131 | + }); |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + const schemaProjection = projectRuntimeToolInputSchema( |
| 136 | + parameters.value, |
| 137 | + `${toolName}.parameters`, |
| 138 | + ); |
| 139 | + if (schemaProjection.violations.length > 0 || !isRecord(schemaProjection.schema)) { |
| 140 | + diagnostics.push({ |
| 141 | + toolName, |
| 142 | + toolIndex, |
| 143 | + violations: schemaProjection.violations, |
| 144 | + }); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + const description = readToolField(entry.tool, "description"); |
| 149 | + const required = normalizeRequired(schemaProjection.schema.required); |
| 150 | + projectedTools.push({ |
| 151 | + originalIndex: toolIndex, |
| 152 | + name: name.value, |
| 153 | + ...(description.ok && typeof description.value === "string" |
| 154 | + ? { description: description.value } |
| 155 | + : {}), |
| 156 | + parameters: { |
| 157 | + ...schemaProjection.schema, |
| 158 | + ...(required ? { required } : {}), |
| 159 | + }, |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + return { tools: projectedTools, diagnostics }; |
| 164 | +} |
| 165 | + |
| 166 | +export function resolveAnthropicToolChoiceForProjectedTools( |
| 167 | + toolChoice: unknown, |
| 168 | + toolNames: readonly string[], |
| 169 | +): AnthropicResolvedToolChoice | undefined { |
| 170 | + if (typeof toolChoice === "string") { |
| 171 | + return isAnthropicToolChoiceMode(toolChoice) && (toolChoice === "none" || toolNames.length > 0) |
| 172 | + ? toolChoice |
| 173 | + : undefined; |
| 174 | + } |
| 175 | + if (!isRecord(toolChoice)) { |
| 176 | + return undefined; |
| 177 | + } |
| 178 | + const type = readObjectStringField(toolChoice, "type"); |
| 179 | + if (type === "none") { |
| 180 | + return { type }; |
| 181 | + } |
| 182 | + if ((type === "auto" || type === "any") && toolNames.length > 0) { |
| 183 | + return { type, ...readDisableParallelToolUse(toolChoice) }; |
| 184 | + } |
| 185 | + if (type === "tool") { |
| 186 | + const name = readObjectStringField(toolChoice, "name"); |
| 187 | + return name && toolNames.includes(name) |
| 188 | + ? { type, name, ...readDisableParallelToolUse(toolChoice) } |
| 189 | + : { type: "none" }; |
| 190 | + } |
| 191 | + return undefined; |
| 192 | +} |
0 commit comments