Agents: fix pi-tools typing
This commit is contained in:
@@ -1,27 +1,13 @@
|
|||||||
import type { AgentTool } from "@mariozechner/pi-ai";
|
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-ai";
|
||||||
import { codingTools, readTool } from "@mariozechner/pi-coding-agent";
|
import { codingTools, readTool } from "@mariozechner/pi-coding-agent";
|
||||||
|
|
||||||
import { detectMime } from "../media/mime.js";
|
import { detectMime } from "../media/mime.js";
|
||||||
|
|
||||||
// TODO(steipete): Remove this wrapper once pi-mono ships file-magic MIME detection
|
// TODO(steipete): Remove this wrapper once pi-mono ships file-magic MIME detection
|
||||||
// for `read` image payloads in `@mariozechner/pi-coding-agent` (then switch back to `codingTools` directly).
|
// for `read` image payloads in `@mariozechner/pi-coding-agent` (then switch back to `codingTools` directly).
|
||||||
type ImageContentBlock = {
|
type ToolContentBlock = AgentToolResult<unknown>["content"][number];
|
||||||
type: "image";
|
type ImageContentBlock = Extract<ToolContentBlock, { type: "image" }>;
|
||||||
data: string;
|
type TextContentBlock = Extract<ToolContentBlock, { type: "text" }>;
|
||||||
mimeType: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type TextContentBlock = {
|
|
||||||
type: "text";
|
|
||||||
text: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type ToolResult = {
|
|
||||||
content: Array<
|
|
||||||
ImageContentBlock | TextContentBlock | Record<string, unknown>
|
|
||||||
>;
|
|
||||||
details?: unknown;
|
|
||||||
};
|
|
||||||
|
|
||||||
function sniffMimeFromBase64(base64: string): string | undefined {
|
function sniffMimeFromBase64(base64: string): string | undefined {
|
||||||
const trimmed = base64.trim();
|
const trimmed = base64.trim();
|
||||||
@@ -48,18 +34,18 @@ function rewriteReadImageHeader(text: string, mimeType: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function normalizeReadImageResult(
|
function normalizeReadImageResult(
|
||||||
result: ToolResult,
|
result: AgentToolResult<unknown>,
|
||||||
filePath: string,
|
filePath: string,
|
||||||
): ToolResult {
|
): AgentToolResult<unknown> {
|
||||||
const content = Array.isArray(result.content) ? result.content : [];
|
const content = Array.isArray(result.content) ? result.content : [];
|
||||||
|
|
||||||
const image = content.find(
|
const image = content.find(
|
||||||
(b): b is ImageContentBlock =>
|
(b): b is ImageContentBlock =>
|
||||||
!!b &&
|
!!b &&
|
||||||
typeof b === "object" &&
|
typeof b === "object" &&
|
||||||
(b as ImageContentBlock).type === "image" &&
|
(b as { type?: unknown }).type === "image" &&
|
||||||
typeof (b as ImageContentBlock).data === "string" &&
|
typeof (b as { data?: unknown }).data === "string" &&
|
||||||
typeof (b as ImageContentBlock).mimeType === "string",
|
typeof (b as { mimeType?: unknown }).mimeType === "string",
|
||||||
);
|
);
|
||||||
if (!image) return result;
|
if (!image) return result;
|
||||||
|
|
||||||
@@ -82,19 +68,19 @@ function normalizeReadImageResult(
|
|||||||
if (
|
if (
|
||||||
block &&
|
block &&
|
||||||
typeof block === "object" &&
|
typeof block === "object" &&
|
||||||
(block as ImageContentBlock).type === "image"
|
(block as { type?: unknown }).type === "image"
|
||||||
) {
|
) {
|
||||||
const b = block as ImageContentBlock;
|
const b = block as ImageContentBlock & { mimeType: string };
|
||||||
return { ...b, mimeType: sniffed };
|
return { ...b, mimeType: sniffed } satisfies ImageContentBlock;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
block &&
|
block &&
|
||||||
typeof block === "object" &&
|
typeof block === "object" &&
|
||||||
(block as TextContentBlock).type === "text" &&
|
(block as { type?: unknown }).type === "text" &&
|
||||||
typeof (block as TextContentBlock).text === "string"
|
typeof (block as { text?: unknown }).text === "string"
|
||||||
) {
|
) {
|
||||||
const b = block as TextContentBlock;
|
const b = block as TextContentBlock & { text: string };
|
||||||
return { ...b, text: rewriteReadImageHeader(b.text, sniffed) };
|
return { ...b, text: rewriteReadImageHeader(b.text, sniffed) } satisfies TextContentBlock;
|
||||||
}
|
}
|
||||||
return block;
|
return block;
|
||||||
});
|
});
|
||||||
@@ -102,15 +88,13 @@ function normalizeReadImageResult(
|
|||||||
return { ...result, content: nextContent };
|
return { ...result, content: nextContent };
|
||||||
}
|
}
|
||||||
|
|
||||||
function createClawdisReadTool(base: AgentTool): AgentTool {
|
type AnyAgentTool = AgentTool<any, any>;
|
||||||
|
|
||||||
|
function createClawdisReadTool(base: AnyAgentTool): AnyAgentTool {
|
||||||
return {
|
return {
|
||||||
...base,
|
...base,
|
||||||
execute: async (toolCallId, params, signal) => {
|
execute: async (toolCallId, params, signal) => {
|
||||||
const result = (await base.execute(
|
const result = (await base.execute(toolCallId, params as any, signal)) as AgentToolResult<unknown>;
|
||||||
toolCallId,
|
|
||||||
params,
|
|
||||||
signal,
|
|
||||||
)) as ToolResult;
|
|
||||||
const record =
|
const record =
|
||||||
params && typeof params === "object"
|
params && typeof params === "object"
|
||||||
? (params as Record<string, unknown>)
|
? (params as Record<string, unknown>)
|
||||||
@@ -122,8 +106,8 @@ function createClawdisReadTool(base: AgentTool): AgentTool {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createClawdisCodingTools(): AgentTool[] {
|
export function createClawdisCodingTools(): AnyAgentTool[] {
|
||||||
return codingTools.map((tool) =>
|
return (codingTools as unknown as AnyAgentTool[]).map((tool) =>
|
||||||
tool.name === readTool.name ? createClawdisReadTool(tool) : tool,
|
tool.name === readTool.name ? createClawdisReadTool(tool) : (tool as AnyAgentTool),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user