|
1 |
| -import type { ContentToolResult, ContentToolUse, Message } from "./models"; |
| 1 | +import * as v from "valibot"; |
| 2 | +import { |
| 3 | + artifacts as artifactModels, |
| 4 | + repl, |
| 5 | + type ContentToolResult, |
| 6 | + type ContentToolUse, |
| 7 | + type Message, |
| 8 | +} from "./models"; |
2 | 9 |
|
3 | 10 | interface RenderContext {
|
4 | 11 | artifacts: Map<
|
@@ -45,20 +52,27 @@ function renderToolUse(ctx: RenderContext, content: ContentToolUse): boolean {
|
45 | 52 | switch (name) {
|
46 | 53 | case "artifacts":
|
47 | 54 | return renderToolUseArtifact(ctx, content);
|
48 |
| - case "repl": |
| 55 | + case "repl": { |
| 56 | + const { input } = v.parse(repl.toolUseSchema, content); |
49 | 57 | ctx.markdown.push(`## Tool use: ${name}`);
|
50 | 58 | ctx.markdown.push(`\`\`\`js\n${input.code.trim()}\n\`\`\``);
|
51 | 59 | return true;
|
| 60 | + } |
| 61 | + default: { |
| 62 | + const str = |
| 63 | + typeof input === "string" ? input : JSON.stringify(input, null, 2); |
| 64 | + ctx.markdown.push(`## Tool use: ${name}`); |
| 65 | + ctx.markdown.push(`\`\`\`\n${str.trim()}\n\`\`\``); |
| 66 | + return true; |
| 67 | + } |
52 | 68 | }
|
53 | 69 | }
|
54 | 70 |
|
55 | 71 | function renderToolUseArtifact(
|
56 | 72 | ctx: RenderContext,
|
57 | 73 | content: ContentToolUse,
|
58 | 74 | ): boolean {
|
59 |
| - const { name, input } = content; |
60 |
| - if (name !== "artifacts") return false; |
61 |
| - |
| 75 | + const { input } = v.parse(artifactModels.toolUseSchema, content); |
62 | 76 | const { artifacts, markdown } = ctx;
|
63 | 77 |
|
64 | 78 | switch (input.command) {
|
@@ -110,16 +124,18 @@ function renderToolResult(
|
110 | 124 | ctx: RenderContext,
|
111 | 125 | content: ContentToolResult,
|
112 | 126 | ): boolean {
|
113 |
| - const { name, content: result } = content; |
114 |
| - switch (name) { |
115 |
| - case "artifacts": |
116 |
| - return false; |
117 |
| - case "repl": |
118 |
| - for (const item of result) { |
119 |
| - ctx.markdown.push( |
120 |
| - `\`\`\`json\n${JSON.stringify(item.text, null, 2)}\n\`\`\``, |
121 |
| - ); |
122 |
| - } |
123 |
| - return true; |
| 127 | + const { name } = content; |
| 128 | + if (name === "artifacts") { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + const { content: result } = v.parse(repl.toolResultSchema, content); |
| 133 | + for (const item of result) { |
| 134 | + ctx.markdown.push("<details><summary>Tool result</summary>"); |
| 135 | + ctx.markdown.push( |
| 136 | + `\`\`\`json\n${JSON.stringify(item.text, null, 2)}\n\`\`\``, |
| 137 | + ); |
| 138 | + ctx.markdown.push("</details>\n\n"); |
124 | 139 | }
|
| 140 | + return true; |
125 | 141 | }
|
0 commit comments