(parts: AgentTraceChunk[])
| 882 | } |
| 883 | |
| 884 | function deriveAgentSteps(parts: AgentTraceChunk[]): AgentDerivedStep[] { |
| 885 | if (!parts.length) { |
| 886 | return []; |
| 887 | } |
| 888 | |
| 889 | interface Snapshot { |
| 890 | id?: string; |
| 891 | hasReasoning: boolean; |
| 892 | step: AgentDerivedStep; |
| 893 | } |
| 894 | |
| 895 | const snapshots: Snapshot[] = []; |
| 896 | const snapshotById = new Map<string, Snapshot>(); |
| 897 | const steps: AgentDerivedStep[] = []; |
| 898 | |
| 899 | const ensureDateMs = (iso?: string) => { |
| 900 | if (!iso) return undefined; |
| 901 | const value = new Date(iso).getTime(); |
| 902 | return Number.isNaN(value) ? undefined : value; |
| 903 | }; |
| 904 | |
| 905 | const createSnapshotStep = ({ |
| 906 | toolCallId, |
| 907 | toolName, |
| 908 | input, |
| 909 | timestamp, |
| 910 | sequence, |
| 911 | }: { |
| 912 | toolCallId?: string; |
| 913 | toolName?: string; |
| 914 | input?: unknown; |
| 915 | timestamp: string; |
| 916 | sequence: number; |
| 917 | }): Snapshot => { |
| 918 | const step: AgentDerivedStep = { |
| 919 | key: toolCallId ? `tool-${toolCallId}` : `tool-${sequence}`, |
| 920 | actions: [], |
| 921 | observations: [], |
| 922 | toolCallId, |
| 923 | toolName, |
| 924 | toolInput: input ?? null, |
| 925 | toolOutput: undefined, |
| 926 | timestamp, |
| 927 | sequence, |
| 928 | startedAt: timestamp, |
| 929 | isComplete: false, |
| 930 | }; |
| 931 | const snapshot: Snapshot = { id: toolCallId, step, hasReasoning: false }; |
| 932 | snapshots.push(snapshot); |
| 933 | if (toolCallId) { |
| 934 | snapshotById.set(toolCallId, snapshot); |
| 935 | } |
| 936 | steps.push(step); |
| 937 | return snapshot; |
| 938 | }; |
| 939 | |
| 940 | const findFallbackSnapshot = () => snapshots.find((candidate) => !candidate.hasReasoning); |
| 941 |
no test coverage detected