(messages: Array<UIMessage>)
| 45 | } |
| 46 | |
| 47 | export function computeMetrics(messages: Array<UIMessage>): ComputedMetrics { |
| 48 | const toolCallLookup = new Map<string, { name: string; arguments: string }>() |
| 49 | const toolResultLookup = new Map< |
| 50 | string, |
| 51 | { content: string; state?: string; error?: string } |
| 52 | >() |
| 53 | |
| 54 | let totalToolCalls = 0 |
| 55 | let totalExecuteCalls = 0 |
| 56 | let redundantSchemaChecks = 0 |
| 57 | let hasSeenSchemaCheck = false |
| 58 | |
| 59 | for (const message of messages) { |
| 60 | for (const part of message.parts) { |
| 61 | if (part.type === 'tool-call') { |
| 62 | totalToolCalls += 1 |
| 63 | toolCallLookup.set(part.id, { |
| 64 | name: part.name, |
| 65 | arguments: part.arguments, |
| 66 | }) |
| 67 | |
| 68 | if (part.name === 'getSchemaInfo') { |
| 69 | if (hasSeenSchemaCheck) redundantSchemaChecks += 1 |
| 70 | hasSeenSchemaCheck = true |
| 71 | } |
| 72 | if (part.name === 'execute_typescript') { |
| 73 | totalExecuteCalls += 1 |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (part.type === 'tool-result') { |
| 78 | toolResultLookup.set(part.toolCallId, { |
| 79 | // `content` may be multimodal (`Array<ContentPart>`) since tool |
| 80 | // results can now carry images/etc. The eval only inspects the JSON |
| 81 | // string `execute_typescript` returns, so coerce non-string content |
| 82 | // to a serialized form that `safeJsonParse` can handle. |
| 83 | content: |
| 84 | typeof part.content === 'string' |
| 85 | ? part.content |
| 86 | : JSON.stringify(part.content), |
| 87 | state: part.state, |
| 88 | error: part.error, |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | const typeScriptAttempts: Array<TypeScriptAttempt> = [] |
| 95 | let compilationFailures = 0 |
| 96 | let runtimeFailures = 0 |
| 97 | |
| 98 | for (const [toolCallId, call] of toolCallLookup.entries()) { |
| 99 | if (call.name !== 'execute_typescript') continue |
| 100 | |
| 101 | const args = safeJsonParse(call.arguments) as |
| 102 | | { typescriptCode?: string } |
| 103 | | undefined |
| 104 | const result = toolResultLookup.get(toolCallId) |
no test coverage detected