(response: Response)
| 454 | |
| 455 | |
| 456 | async function parseDirectCallResponse(response: Response): Promise<ToolCallResult> { |
| 457 | const contentType = response.headers.get('content-type')?.toLowerCase() ?? ''; |
| 458 | const bodyText = await response.text(); |
| 459 | const message = contentType.includes('text/event-stream') |
| 460 | ? parseSseJsonRpcPayload(bodyText) |
| 461 | : parseJsonRpcPayload(bodyText); |
| 462 | |
| 463 | if (!response.ok) { |
| 464 | throw new OperationError(readJsonRpcErrorMessage(message, `HTTP ${response.status}`)); |
| 465 | } |
| 466 | |
| 467 | if (!message || typeof message !== 'object') { |
| 468 | throw new OperationError('Direct Learn MCP call returned an unexpected payload.'); |
| 469 | } |
| 470 | |
| 471 | if ('error' in message && message.error) { |
| 472 | throw new OperationError(readJsonRpcErrorMessage(message)); |
| 473 | } |
| 474 | |
| 475 | if (!('result' in message) || !message.result) { |
| 476 | throw new OperationError('Direct Learn MCP call did not include a tool result.'); |
| 477 | } |
| 478 | |
| 479 | return message.result as ToolCallResult; |
| 480 | } |
| 481 | |
| 482 | function parseSseJsonRpcPayload(bodyText: string): JsonRpcResponsePayload { |
| 483 | const events = bodyText |
no test coverage detected