Files
t3code/apps/mobile/src/lib/threadActivityInspector.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

310 lines
9.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { V2ItemSupport } from "@t3tools/client-runtime/state/item-support";
import { toolItemForDisplay } from "@t3tools/client-runtime/work-log/presentation";
import type { ThreadId } from "@t3tools/contracts";
import { formatDuration } from "@t3tools/shared/orchestrationTiming";
import * as DateTime from "effect/DateTime";
import type { ThreadFeedActivity } from "./threadActivity";
export interface ThreadActivityInspectorField {
readonly label: string;
readonly value: string;
}
export interface ThreadActivityInspectorBlock {
readonly label: string;
readonly value: string;
readonly monospaced: boolean;
}
export interface ThreadActivityFileLink {
readonly label: string;
readonly path: string;
readonly line?: number;
}
export interface ThreadActivityWebLink {
readonly label: string;
readonly url: string;
}
export interface ThreadActivityInspectorModel {
readonly fields: ReadonlyArray<ThreadActivityInspectorField>;
readonly blocks: ReadonlyArray<ThreadActivityInspectorBlock>;
readonly fileLinks: ReadonlyArray<ThreadActivityFileLink>;
readonly webLinks: ReadonlyArray<ThreadActivityWebLink>;
readonly canRollback: boolean;
readonly rollbackTarget: {
readonly threadId: ThreadId;
readonly checkpointId: string;
readonly scopeId: string;
} | null;
readonly structuredDetails: string;
}
function formatStructured(value: unknown): string {
if (typeof value === "string") return value;
try {
return JSON.stringify(value, null, 2) ?? String(value);
} catch {
return String(value);
}
}
function durationLabel(
startedAt: DateTime.Utc | null,
completedAt: DateTime.Utc | null,
): string | null {
if (startedAt === null) return null;
const start = DateTime.toEpochMillis(startedAt);
const end = completedAt === null ? Date.now() : DateTime.toEpochMillis(completedAt);
return formatDuration(Math.max(0, end - start));
}
function addBlock(
blocks: ThreadActivityInspectorBlock[],
label: string,
value: unknown,
monospaced = true,
): void {
if (value === undefined || value === null || value === "") return;
blocks.push({ label, value: formatStructured(value), monospaced });
}
export function buildThreadActivityInspector(
activity: Pick<ThreadFeedActivity, "projectedItem">,
support: V2ItemSupport,
currentThreadId: ThreadId,
): ThreadActivityInspectorModel {
const row = activity.projectedItem;
const item = row.item;
const fields: ThreadActivityInspectorField[] = [
{ label: "Item", value: item.type.replaceAll("_", " ") },
{ label: "Status", value: item.status.replaceAll("_", " ") },
];
const duration = durationLabel(item.startedAt, item.completedAt);
if (duration) fields.push({ label: "Duration", value: duration });
if (row.visibility !== "local") fields.push({ label: "Visibility", value: row.visibility });
if (support.run) fields.push({ label: "Run", value: support.run.status });
const latestAttempt = support.attempts.at(-1);
if (latestAttempt) {
fields.push({
label: "Attempt",
value: `${latestAttempt.attemptOrdinal} · ${latestAttempt.status} · ${latestAttempt.reason.replaceAll("_", " ")}`,
});
}
if (support.node) {
fields.push({
label: "Node",
value: `${support.node.kind.replaceAll("_", " ")} · ${support.node.status}`,
});
}
if (support.providerThread) {
fields.push({
label: "Provider thread",
value: `${support.providerThread.providerInstanceId} · ${support.providerThread.status}`,
});
}
if (support.providerTurn) {
fields.push({ label: "Provider turn", value: support.providerTurn.status });
}
if (support.providerSession) {
fields.push({
label: "Session",
value: `${support.providerSession.status} · ${support.providerSession.model ?? "default model"}`,
});
fields.push({ label: "Working directory", value: support.providerSession.cwd });
}
if (support.runtimeRequest) {
fields.push({
label: "Request",
value: `${support.runtimeRequest.status} · ${support.runtimeRequest.responseCapability.type.replaceAll("_", " ")}`,
});
}
const blocks: ThreadActivityInspectorBlock[] = [];
const fileLinks: ThreadActivityFileLink[] = [];
const webLinks: ThreadActivityWebLink[] = [];
if (support.attempts.length > 1) {
addBlock(
blocks,
"Attempt history",
support.attempts
.map(
(attempt) =>
`Attempt ${attempt.attemptOrdinal} · ${attempt.status} · ${attempt.reason.replaceAll("_", " ")}`,
)
.join("\n"),
);
}
switch (item.type) {
case "reasoning":
addBlock(blocks, "Reasoning", item.text, false);
break;
case "command_execution":
addBlock(blocks, "Command", item.input);
if (item.exitCode !== undefined) {
addBlock(blocks, "Exit", `Process exited with code ${item.exitCode}`);
}
break;
case "file_change":
for (const change of item.changes ?? [{ path: item.fileName, operation: "modify" }]) {
fileLinks.push({
label: `${change.operation} ${change.oldPath ? `${change.oldPath} → ` : ""}${change.path}`,
path: change.path,
});
}
if (item.additions !== undefined || item.deletions !== undefined) {
fields.push({
label: "Changes",
value: `+${item.additions ?? 0} −${item.deletions ?? 0}`,
});
}
break;
case "file_search":
addBlock(blocks, "Query", item.pattern);
for (const result of item.results ?? []) {
fileLinks.push({
label: result.preview ? `${result.fileName} — ${result.preview}` : result.fileName,
path: result.fileName,
...(result.line === undefined ? {} : { line: result.line }),
});
}
break;
case "web_search":
addBlock(blocks, "Queries", item.patterns?.join("\n"));
for (const result of item.results ?? []) {
if (result.url) {
webLinks.push({
label: result.title ?? result.url,
url: result.url,
});
}
if (result.snippet)
addBlock(blocks, result.title ?? "Search result", result.snippet, false);
}
break;
case "dynamic_tool":
addBlock(blocks, "Input", item.input);
break;
case "approval_request":
addBlock(blocks, "Prompt", item.prompt, false);
break;
case "user_input_request":
addBlock(
blocks,
"Questions",
item.questions.map((question) => question.question).join("\n"),
false,
);
break;
case "checkpoint":
addBlock(
blocks,
"Files",
item.files
.map((file) => `${file.path} +${file.additions} −${file.deletions} ${file.kind}`)
.join("\n"),
);
break;
case "subagent":
addBlock(blocks, "Prompt", item.prompt, false);
addBlock(blocks, "Progress", support.subagent?.progress ?? item.progress, false);
addBlock(blocks, "Result", support.subagent?.result ?? item.result, false);
if (support.subagent) {
fields.push({
label: "Delegated task",
value: `${support.subagent.origin.replaceAll("_", " ")} · ${support.subagent.status}`,
});
}
break;
case "handoff":
addBlock(blocks, "Summary", item.summary, false);
fields.push({
label: "Handoff",
value: `${item.strategy.replaceAll("_", " ")} · ${support.contextHandoff?.status ?? item.status}`,
});
if (support.contextTransfer) {
fields.push({
label: "Transfer",
value: `${support.contextTransfer.type.replaceAll("_", " ")} · ${support.contextTransfer.status}`,
});
if (support.contextTransfer.resolution) {
fields.push({
label: "Context",
value: support.contextTransfer.resolution.strategy.replaceAll("_", " "),
});
}
}
break;
case "error":
addBlock(blocks, "Error", item.failure.message, false);
if (item.failure.code) fields.push({ label: "Code", value: item.failure.code });
if (item.failure.retryable !== null) {
fields.push({ label: "Retryable", value: item.failure.retryable ? "yes" : "no" });
}
break;
case "proposed_plan":
addBlock(blocks, "Plan", item.markdown, false);
break;
case "todo_list":
addBlock(
blocks,
"Tasks",
item.steps
.map((step) => `${step.status === "completed" ? "✓" : "○"} ${step.text}`)
.join("\n"),
false,
);
addBlock(blocks, "Explanation", item.explanation, false);
break;
case "compaction":
addBlock(blocks, "Summary", item.summary, false);
if (item.beforeTokenCount !== undefined || item.afterTokenCount !== undefined) {
fields.push({
label: "Context tokens",
value: `${item.beforeTokenCount ?? "?"} → ${item.afterTokenCount ?? "?"}`,
});
}
break;
case "run_interrupt_request":
case "run_interrupt_result":
addBlock(blocks, "Message", item.message, false);
break;
case "fork":
case "thread_created":
case "user_message":
case "assistant_message":
break;
}
const checkpoint =
item.type === "checkpoint" && row.sourceThreadId === currentThreadId
? support.checkpoint
: null;
return {
fields,
blocks,
fileLinks,
webLinks,
canRollback: checkpoint?.status === "ready",
rollbackTarget:
checkpoint?.status === "ready"
? {
threadId: row.sourceThreadId,
checkpointId: checkpoint.id,
scopeId: checkpoint.scopeId,
}
: null,
structuredDetails: formatStructured({
visibility: row.visibility,
sourceThreadId: row.sourceThreadId,
sourceItemId: row.sourceItemId,
item: toolItemForDisplay(item),
}),
};
}