nandi/oripublic Fork 0
7895c1d1c9bb1048807dc04f7246dc456c47e025
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

Add copy-paste functionality for code blocks and completion widgets 7895c1d · on 7895c1d1c9bb1048807dc04f7246dc456c47e025 · k33g · yesterday
ToolCallCard.tsx · 150 lines · 4.1 KBTypeScript Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * ToolCallCard shows one tool call the agent is running, in the spirit of
 * Zed's agent panel: a collapsible card with the tool kind, a live status,
 * touched locations, and the call's output (text, or a file diff).
 *
 * @example
 * <ToolCallCard call={{ toolCallId: "c1", title: "Reading main.go",
 *   toolKind: "read", status: "in_progress", contents: [], locations: [] }} />
 */

import type { ToolCall } from "../reducer";
import { InlineText } from "./InlineText";
import type { AcpToolCallContent } from "../protocol";
import { Markdown } from "./Markdown";
import { CopyButton } from "./CopyButton";

const kindIcons: Record<string, string> = {
	read: "📖",
	edit: "✏️",
	delete: "🗑️",
	move: "📦",
	search: "🔍",
	execute: "⚡",
	think: "💭",
	fetch: "🌐",
	switch_mode: "🔀",
	other: "🔧",
};

const statusLabels: Record<string, string> = {
	pending: "pending",
	in_progress: "running",
	completed: "done",
	failed: "failed",
};

export function ToolCallCard({ call }: { call: ToolCall }) {
	// Extract all textual content for copy button
	const getToolContent = (): string => {
		return call.contents
			.map((content) => {
				if (content.type === "content" && content.content?.text) {
					return content.content.text;
				}
				if (content.type === "diff") {
					const parts = [];
					if (content.path) parts.push(`Path: ${content.path}`);
					if (content.oldText) parts.push(`Old:\n${content.oldText}`);
					if (content.newText) parts.push(`New:\n${content.newText}`);
					return parts.join("\n\n");
				}
				return "";
			})
			.filter(Boolean)
			.join("\n\n");
	};

	return (
		<details className={`tool tool-${call.status}`}>
			<summary className="tool-summary">
				<span className="tool-icon" aria-hidden>
					{kindIcons[call.toolKind] ?? kindIcons.other}
				</span>
				<span className="tool-title">
					<InlineText text={call.title} />
				</span>
				<span className={`tool-status tool-status-${call.status}`}>
					{statusLabels[call.status] ?? call.status}
				</span>
				{getToolContent() && (
					<CopyButton content={getToolContent()} label="Copy tool output" />
				)}
			</summary>
			<div className="tool-body">
				{call.locations.length > 0 && (
					<ul className="tool-locations">
						{call.locations.map((location, index) => (
							// biome-ignore lint/suspicious/noArrayIndexKey: tool-call locations carry no id and the list is replaced wholesale on every update.
							<li key={index}>
								<code>
									{location.path}
									{location.line != null ? `:${location.line}` : ""}
								</code>
							</li>
						))}
					</ul>
				)}
				{call.contents.map((content, index) => (
					// biome-ignore lint/suspicious/noArrayIndexKey: tool-call contents carry no id and the list is replaced wholesale on every update.
					<ToolContent key={index} content={content} toolKind={call.toolKind} />
				))}
			</div>
		</details>
	);
}

function ToolContent({
	content,
	toolKind,
}: { content: AcpToolCallContent; toolKind: string }) {
	switch (content.type) {
		case "content":
			return (
				<TextToolContent text={content.content?.text} toolKind={toolKind} />
			);
		case "diff":
			return (
				<div className="tool-diff">
					<code className="tool-diff-path">{content.path}</code>
					{content.oldText != null && (
						<pre className="tool-diff-old">
							{prefixLines(content.oldText, "- ")}
						</pre>
					)}
					<pre className="tool-diff-new">
						{prefixLines(content.newText ?? "", "+ ")}
					</pre>
				</div>
			);
		case "terminal":
			return <p className="tool-terminal">terminal: {content.terminalId}</p>;
		default:
			return null;
	}
}

/**
 * TextToolContent renders a tool call's textual output. System-command
 * output (kind "execute") is verbatim text: it goes into a monospace block,
 * never through the markdown pipeline.
 */
function TextToolContent({
	text,
	toolKind,
}: { text: string | undefined; toolKind: string }) {
	if (!text) {
		return null;
	}
	if (toolKind === "execute") {
		return <pre className="tool-output">{text}</pre>;
	}
	return <Markdown text={text} />;
}

function prefixLines(text: string, prefix: string): string {
	return text
		.split("\n")
		.map((line) => prefix + line)
		.join("\n");
}