Files
archie/ui/src/chat/ChatToolCall.vue
T

111 lines
4.0 KiB
Vue

<script setup lang="ts">
import { CircleCheck, CircleX, LoaderCircle } from "@lucide/vue";
import { computed } from "vue";
import { CodeBlock, CodeBlockContent } from "@/components/ui/code-block";
import {
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/components/ui/trace";
import ChatNavChip from "./ChatNavChip.vue";
import type { ChatToolCall } from "./state";
/**
* One tool call: the name, and what came back once it is opened.
*
* A quiet line rather than the point of the reply: it is evidence for the
* answer under it, so it is dim, and a failed call is the only thing here that
* raises its voice. The parameters it was given and its outcome are folded away
* until asked for. Rendered inside a Trace, which an empty list never creates,
* so a turn that called no tools looks untouched.
*/
const props = defineProps<{
tool: ChatToolCall;
/** The turn is still arriving, so a call with no outcome yet is running. */
streaming?: boolean;
}>();
const name = computed(
() => props.tool.name || props.tool.Name || props.tool.tool || "tool",
);
const parameters = computed(
() => props.tool.parameters || props.tool.Parameters || "",
);
const failed = computed(() =>
Boolean(props.tool.err || props.tool.Err || props.tool.failed),
);
// A recorded turn reports its outcome in summary, a live frame in text, and a
// failure may only carry the error.
const outcome = computed(
() =>
props.tool.summary ||
props.tool.Summary ||
props.tool.text ||
props.tool.err ||
props.tool.Err ||
"",
);
const running = computed(() => !outcome.value && !failed.value && !!props.streaming);
const duration = computed(() => {
const ms = props.tool.duration_ms;
if (!ms) return "";
return ms < 1000 ? `${ms}ms` : `${(ms / 1000).toFixed(ms < 10000 ? 1 : 0)}s`;
});
// The row shows the call's main value, as "git diff --stat" rather than its
// JSON: the first string argument, else the parameters as sent.
const argument = computed(() => {
try {
const args: unknown = JSON.parse(parameters.value);
if (args && typeof args === "object" && !Array.isArray(args)) {
const first = Object.values(args).find((v) => typeof v === "string" && v);
if (typeof first === "string") return first.split("\n", 1)[0];
if (Object.keys(args).length === 0) return "";
}
} catch {
// Truncated or not JSON: show it as it came.
}
return parameters.value.split("\n", 1)[0];
});
const detail = computed(() =>
[parameters.value && `> ${parameters.value}`, outcome.value].filter(Boolean).join("\n\n"),
);
</script>
<template>
<ChatNavChip v-if="tool.path" :path="tool.path" :label="tool.label" />
<TraceCollapsible v-else-if="detail" :default-open="failed">
<TraceCollapsibleTrigger>
<TraceLine :status="failed ? 'error' : running ? 'running' : undefined">
<TraceIcon>
<CircleX v-if="failed" />
<LoaderCircle v-else-if="running" class="animate-spin" />
<CircleCheck v-else class="text-success" />
</TraceIcon>
<span class="font-mono text-foreground">{{ name }}</span>
<TraceTitle class="font-mono text-xs">{{ argument }}</TraceTitle>
<span v-if="duration" class="shrink-0 text-xs tabular-nums">{{ duration }}</span>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<CodeBlock size="sm">
<CodeBlockContent wrap class="max-h-64">{{ detail }}</CodeBlockContent>
</CodeBlock>
</TraceCollapsibleContent>
</TraceCollapsible>
<TraceLine v-else :status="failed ? 'error' : running ? 'running' : undefined">
<TraceIcon>
<CircleX v-if="failed" />
<LoaderCircle v-else-if="running" class="animate-spin" />
<CircleCheck v-else class="text-success" />
</TraceIcon>
<span class="font-mono text-foreground">{{ name }}</span>
<TraceTitle class="font-mono text-xs">{{ argument }}</TraceTitle>
<span v-if="duration" class="shrink-0 text-xs tabular-nums">{{ duration }}</span>
</TraceLine>
</template>