char-slop/berry

multipurpose self-modifiable agentic harness

git clone https://git.t4t.associates/char-slop/berry

Charlotte Somrefactor: use justin instead of ad-hoc type validation760c3b5

main
950 B26 linesraw
1import type { ImageContent, ToolResultMessage } from "@earendil-works/pi-ai";
2import type { ToolCall } from "@berry/api/web.ts";
3import { imageResult } from "@berry/api/content.ts";
4
5const EMPTY: readonly ImageContent[] = [];
6
7export function isImageResult(value: unknown): value is ImageContent {
8  return imageResult(value).errors === undefined;
9}
10
11/** The text and images of a model-facing result, including output produced before a failure. */
12export function toolResult(
13  message: ToolResultMessage | null,
14): Pick<ToolCall, "result" | "error" | "images"> {
15  if (message === null) return { result: null, error: null, images: EMPTY };
16  const text = message.content.flatMap((block) =>
17    block.type === "text" && block.text ? [block.text] : []
18  ).join(
19    "\n",
20  );
21  return {
22    result: message.isError ? null : text,
23    error: message.isError ? text : null,
24    images: message.content.filter((block) => block.type === "image"),
25  };
26}