| 63 | } |
| 64 | |
| 65 | function createAgentStreamError(error: unknown): Error { |
| 66 | if (error instanceof Error) return error |
| 67 | if (typeof error === 'string' && error) return new Error(error) |
| 68 | if (typeof error === 'object' && error !== null) { |
| 69 | const record = error as { name?: unknown; message?: unknown; stack?: unknown } |
| 70 | const streamError = new Error( |
| 71 | typeof record.message === 'string' && record.message ? record.message : 'Agent stream failed' |
| 72 | ) |
| 73 | if (typeof record.name === 'string' && record.name) streamError.name = record.name |
| 74 | if (typeof record.stack === 'string' && record.stack) streamError.stack = record.stack |
| 75 | return streamError |
| 76 | } |
| 77 | return new Error('Agent stream failed') |
| 78 | } |
| 79 | |
| 80 | function isRecord(value: unknown): value is Record<string, unknown> { |
| 81 | return typeof value === 'object' && value !== null && !Array.isArray(value) |