"use strict"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { StringEnum } from "@earendil-works/pi-ai"; import { getConfig } from "../config"; import { processImage } from "../media"; import { visionCompletion } from "../client"; import { DIAGRAM_UNDERSTANDING } from "../prompts"; import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, truncateHead, } from "@earendil-works/pi-coding-agent"; export function registerUnderstandDiagramTool(pi: ExtensionAPI) { pi.registerTool({ name: "understand_technical_diagram", label: "Understand Diagram", description: "Analyze system architecture diagrams, UML class/sequence diagrams, flowcharts, ER diagrams, and network diagrams. Provides structured component breakdowns, relationship mappings, design pattern analysis, and potential issues identification. Supports local files (png, jpg) and HTTP(S) URLs.", promptSnippet: "Analyze architecture diagrams, UML, flowcharts, ER diagrams — structured interpretation", promptGuidelines: [ "Use understand_technical_diagram when the user provides a technical diagram (architecture, UML, ER, flowchart, network) and wants it explained. Use the optional diagramType hint ('uml_class', 'sequence', 'flowchart', 'er', 'architecture', 'network', 'component') for better accuracy.", ], parameters: Type.Object({ imagePath: Type.String({ description: "Path to the diagram image (relative to cwd, supports @ prefix) or HTTP(S) URL.", }), prompt: Type.Optional( Type.String({ description: "What should the AI explain about this diagram? Defaults to a comprehensive analysis.", }) ), diagramType: Type.Optional( StringEnum([ "uml_class", "sequence", "flowchart", "er", "architecture", "network", "component", ] as const) ), }), async execute(_toolCallId, params, signal, onUpdate, ctx) { const config = getConfig(); onUpdate?.({ content: [{ type: "text", text: "正在调用 GLM-4.6V 解读图表..." }], }); const image = processImage(params.imagePath, ctx.cwd, config.maxImageSizeMB); let userPrompt = params.prompt || "Explain this diagram comprehensively, covering its structure, components, relationships, and design patterns."; if (params.diagramType) { userPrompt = `Diagram type: ${params.diagramType}\n\n${userPrompt}`; } const result = await visionCompletion( config, DIAGRAM_UNDERSTANDING, [image], userPrompt, signal ); const truncated = truncateHead(result, { maxLines: DEFAULT_MAX_LINES, maxBytes: DEFAULT_MAX_BYTES, }); let text = truncated.content; if (truncated.truncated) { text += `\n\n[输出已截断: ${truncated.outputLines}/${truncated.totalLines} 行]`; } return { content: [{ type: "text", text }], details: { raw: result }, }; }, }); }