UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

148 lines • 6.08 kB
import { registerToolArgumentValidator } from "@earendil-works/pi-ai"; import { Type } from "typebox"; import { compileModernSchema } from "../modern/tool-schema.js"; import { buildMcpToolNames } from "./naming.js"; import { collectAllPages } from "./pagination.js"; export { buildMcpToolNames, collectAllPages }; const PERMISSIVE_OBJECT_SCHEMA = { type: "object", properties: {} }; export function convertJsonSchemaToTypeBox(schema, modern = false) { if (modern && (typeof schema === "boolean" || isRecord(schema))) { const validate = compileModernSchema(schema); // TypeBox/tool APIs require an object container. allOf preserves the exact // boolean constraint; provider-specific object adaptation happens in pi-ai. const parameters = Type.Unsafe(typeof schema === "boolean" ? { allOf: [schema] } : structuredClone(schema)); registerToolArgumentValidator(parameters, validate); return { schema: parameters, warnings: [] }; } const warnings = []; const resolved = resolveRefs(stripTopLevelSchemaKeys(schema), schema, new Set(), warnings); if (warnings.length > 0) { return { schema: Type.Unsafe({ ...PERMISSIVE_OBJECT_SCHEMA }), warnings }; } if (!isRecord(resolved)) { return { schema: Type.Unsafe({ ...PERMISSIVE_OBJECT_SCHEMA }), warnings: ["MCP schema is not an object; using permissive object schema."], }; } return { schema: Type.Unsafe(resolved), warnings }; } export function prepareOutputSchemaRetry(schema) { const warnings = []; const record = isRecord(schema) ? { ...schema } : { ...PERMISSIVE_OBJECT_SCHEMA }; if (hasOwn(record, "$schema")) { delete record.$schema; warnings.push("Stripped top-level $schema from MCP outputSchema retry."); } if (hasOwn(record, "additionalProperties")) { delete record.additionalProperties; warnings.push("Stripped top-level additionalProperties from MCP outputSchema retry."); } return { schema: record, warnings }; } export function mapMcpToolResult(result) { const content = mapMcpContent(result); if (result.isError === true) { return { ok: false, error: { message: contentToErrorMessage(content), content } }; } return { ok: true, content }; } function mapMcpContent(result) { const content = []; for (const block of result.content ?? []) { content.push(mapContentBlock(block)); } if (result.structuredContent !== undefined) { content.push({ type: "text", text: stringifyJson(result.structuredContent) }); } if (content.length === 0) { content.push({ type: "text", text: "(empty result)" }); } return content; } function mapContentBlock(block) { if (block.type === "text") { return { type: "text", text: typeof block.text === "string" ? block.text : stringifyJson(block.text) }; } if (block.type === "image" && typeof block.data === "string" && typeof block.mimeType === "string") { return { type: "image", data: block.data, mimeType: block.mimeType }; } if (block.type === "audio" && typeof block.data === "string" && typeof block.mimeType === "string") { return { type: "audio", data: block.data, mimeType: block.mimeType }; } if (block.type === "resource" && hasOwn(block, "resource")) { return { type: "resource", resource: block.resource }; } if (block.type === "resource_link" && typeof block.uri === "string") { return { type: "resource_link", uri: block.uri, ...(typeof block.name === "string" ? { name: block.name } : {}), ...(typeof block.description === "string" ? { description: block.description } : {}), ...(typeof block.mimeType === "string" ? { mimeType: block.mimeType } : {}), }; } return { type: "text", text: stringifyJson(block) }; } function contentToErrorMessage(content) { const firstText = content.find((block) => block.type === "text"); return firstText?.text.trim() || "MCP tool returned an error result."; } function stripTopLevelSchemaKeys(schema) { if (!isRecord(schema)) return schema; const result = { ...schema }; delete result.$schema; delete result.additionalProperties; return result; } function resolveRefs(value, root, seenRefs, warnings) { if (Array.isArray(value)) { return value.map((entry) => resolveRefs(entry, root, seenRefs, warnings)); } if (!isRecord(value)) return value; if (typeof value.$ref === "string") { const ref = value.$ref; const resolved = resolveLocalRef(root, ref); if (resolved === undefined || seenRefs.has(ref)) { warnings.push(`MCP schema contains unresolvable $ref '${ref}'; using permissive object schema.`); return { ...PERMISSIVE_OBJECT_SCHEMA }; } const nextSeenRefs = new Set(seenRefs); nextSeenRefs.add(ref); return resolveRefs(resolved, root, nextSeenRefs, warnings); } const result = {}; for (const [key, child] of Object.entries(value)) { if (key === "type" && child === null) continue; result[key] = resolveRefs(child, root, seenRefs, warnings); } return result; } function resolveLocalRef(root, ref) { if (!ref.startsWith("#/")) return undefined; let current = root; for (const part of ref .slice(2) .split("/") .map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"))) { if (!isRecord(current) || !(part in current)) return undefined; current = current[part]; } return current; } function stringifyJson(value) { const text = JSON.stringify(value); return text ?? String(value); } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function hasOwn(value, key) { return Object.getOwnPropertyDescriptor(value, key) !== undefined; } //# sourceMappingURL=schema-compat.js.map