|
1 | 1 | import chalk from 'chalk'; |
2 | 2 | import ora from 'ora'; |
3 | 3 |
|
4 | | -import type { CopilotSession } from '@github/copilot-sdk'; |
| 4 | +import type { CopilotSession, JsonValue } from '@github/copilot-sdk'; |
5 | 5 |
|
6 | 6 | import { tryFormatMessage } from './try-format-message.js'; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * Extracts the first string-valued property found among `keys` on a tool-call's |
| 10 | + * JSON arguments, narrowing the untyped `JsonValue` union to its object branch first. |
| 11 | + * @param value - The tool-call arguments to inspect, as reported by the Copilot SDK |
| 12 | + * @param keys - Property names to check, in priority order |
| 13 | + * @returns The first matching string value, or `undefined` if none match |
| 14 | + */ |
| 15 | +function firstStringProp(value: JsonValue | undefined, keys: string[]): string | undefined { |
| 16 | + // Primitives and arrays have no named properties to read |
| 17 | + if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 18 | + return undefined; |
| 19 | + } |
| 20 | + // Return the first key present with a string value, in caller-specified priority order |
| 21 | + for (const key of keys) { |
| 22 | + const prop = value[key]; |
| 23 | + // Skip non-string values (numbers, nested objects, etc.) rather than stringifying them |
| 24 | + if (typeof prop === 'string') return prop; |
| 25 | + } |
| 26 | + return undefined; |
| 27 | +} |
| 28 | + |
8 | 29 | const TOOL_ICONS: Record<string, string> = { |
9 | 30 | browser_screenshot: '📷', |
10 | 31 | browser_navigate: '🌐', |
@@ -69,7 +90,7 @@ export function attachSessionLogger( |
69 | 90 | switch (event.type) { |
70 | 91 | case 'tool.execution_start': { |
71 | 92 | const { toolCallId, toolName, arguments: args } = event.data; |
72 | | - const detail = args?.url ?? args?.path ?? args?.load ?? args?.selector; |
| 93 | + const detail = firstStringProp(args, ['url', 'path', 'load', 'selector']); |
73 | 94 | const icon = TOOL_ICONS[toolName] ?? '🔧'; |
74 | 95 | const label = |
75 | 96 | typeof detail === 'string' ? `${icon} ${toolName} (${detail})` : `${icon} ${toolName}`; |
|
0 commit comments