Open History to see the last commit
1import type { OrchestrationV2TurnItem, OrchestrationV2TurnItemStatus } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3
4import type { MuseItem } from "./protocol.ts";
5
6const decodeToolArgs = Schema.decodeUnknownSync(
7 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
8);
9
10type Fields<
11 T extends OrchestrationV2TurnItem["type"],
12 K extends keyof Extract<OrchestrationV2TurnItem, { type: T }>,
13> = Pick<Extract<OrchestrationV2TurnItem, { type: T }>, K>;
14type MuseToolPresentation = {
15 readonly status: OrchestrationV2TurnItemStatus;
16 readonly title: string | null;
17} & (
18 | Fields<"command_execution", "type" | "input" | "output" | "exitCode">
19 | Fields<"file_change", "type" | "fileName" | "oldStr" | "newStr" | "diffStr">
20 | Fields<"web_search", "type" | "patterns" | "results">
21 | Fields<"dynamic_tool", "type" | "toolName" | "input" | "output">
22);
23
24/** Unrecognized native terminal states must not appear as successful work. */
25export function museItemStatus(item: MuseItem): OrchestrationV2TurnItemStatus {
26 if (item.status === "inProgress") return "running";
27 if (item.status === "cancelled") return "cancelled";
28 if (
29 item.status === "completed" &&
30 !(item.kind === "compaction" && item.outcome && item.outcome !== "compacted")
31 )
32 return "completed";
33 return "failed";
34}
35
36/** Converts native tool details into the shared timeline fields used by every client. */
37export function museToolPresentation(
38 item: MuseItem,
39 status = museItemStatus(item),
40): MuseToolPresentation {
41 let args: Record<string, unknown> | undefined;
42 if (item.args) {
43 try {
44 args = decodeToolArgs(item.args);
45 } catch {
46 // Streaming model-authored arguments may not be valid JSON yet.
47 }
48 }
49 const stringArg = (...keys: string[]) => {
50 for (const key of keys) {
51 const value = args?.[key];
52 if (typeof value === "string") return value;
53 }
54 return undefined;
55 };
56 const toolName = item.tool ?? item.kind;
57 const title =
58 item.kind === "subagent"
59 ? item.objective || item.role || "Muse agent"
60 : item.kind === "workflow"
61 ? item.entryId || "Workflow"
62 : toolName;
63 const detail =
64 status === "failed" ? item.failureReason || `Muse reported ${item.status}.` : undefined;
65 const shared = { status, title: detail ? `${title}: ${detail}` : title };
66 const text =
67 item.visibleOutput ??
68 item.result?.summary ??
69 item.text ??
70 item.summary?.join("\n") ??
71 item.fallbackText ??
72 item.objective ??
73 "";
74 const output = detail ? [detail, text].filter(Boolean).join("\n") : text;
75
76 if (item.kind === "userShell" || /shell|bash|exec_command/i.test(item.tool ?? "")) {
77 return {
78 ...shared,
79 type: "command_execution",
80 input: item.commandText ?? stringArg("command", "cmd") ?? item.args ?? "",
81 output,
82 ...(item.exitCode === undefined ? {} : { exitCode: item.exitCode }),
83 };
84 }
85 if (item.kind === "toolCall" && /write|edit|patch/i.test(toolName)) {
86 const fileName = stringArg("file_path", "path", "filePath")?.trim();
87 if (fileName) {
88 const oldStr = stringArg("old_string", "old_str", "oldStr", "oldText");
89 const newStr = stringArg("new_string", "new_str", "newStr", "newText", "content");
90 const diffStr = stringArg("diff", "patch", "diffStr");
91 return {
92 ...shared,
93 type: "file_change",
94 fileName,
95 ...(oldStr === undefined ? {} : { oldStr }),
96 ...(newStr === undefined ? {} : { newStr }),
97 ...(diffStr === undefined ? {} : { diffStr }),
98 };
99 }
100 }
101 if (item.kind === "toolCall" && /web|browse|fetch/i.test(toolName)) {
102 const query = stringArg("query", "url", "pattern")?.trim();
103 return {
104 ...shared,
105 type: "web_search",
106 ...(query ? { patterns: [query] } : {}),
107 ...(output ? { results: [{ snippet: output }] } : {}),
108 };
109 }
110 return {
111 ...shared,
112 type: "dynamic_tool",
113 toolName,
114 input: args ?? item.args ?? "",
115 output,
116 };
117}