(messages, modelId, apiKey, onChunk, onTokenUpdate, files = [], searchEnabled = false, abortController = null, onStreamOpen = null, onReasoningChunk = null, reasoningEnabled = true, reasoningEffort = DEFAULT_REASONING_EFFORT)
| 454 | { |
| 455 | context, |
| 456 | maxAttempts, |
| 457 | timeoutMs, |
| 458 | signal |
| 459 | } |
| 460 | ); |
| 461 | |
| 462 | if (window.networkLogger) { |
| 463 | window.networkLogger.logRequest({ |
| 464 | type: 'openrouter', |
| 465 | method: 'POST', |
| 466 | url: url, |
| 467 | status: response.status, |
| 468 | request: { |
| 469 | headers: window.networkLogger.sanitizeHeaders(headers), |
| 470 | body: body |
| 471 | }, |
| 472 | response: data |
| 473 | }); |
| 474 | } |
| 475 | |
| 476 | if (!response.ok) { |
| 477 | const error = new Error(`HTTP error! status: ${response.status}`); |
| 478 | error.status = response.status; |
| 479 | error.responseText = text || ''; |
| 480 | error.data = data; |
| 481 | error.responseData = data; |
| 482 | throw error; |
| 483 | } |
| 484 | |
| 485 | const content = data?.choices?.[0]?.message?.content; |
| 486 | if (typeof content !== 'string') { |
| 487 | const error = new Error('Invalid completion response'); |
| 488 | error.responseData = data; |
| 489 | throw error; |
| 490 | } |
| 491 | |
| 492 | return { |
| 493 | content, |
| 494 | data, |
| 495 | response |
| 496 | }; |
| 497 | } catch (error) { |
| 498 | console.error('Error sending completion:', { |
| 499 | name: typeof error?.name === 'string' |
| 500 | ? error.name.replace(/[^A-Za-z0-9_.-]/g, '').slice(0, 80) |
| 501 | : 'Error', |
| 502 | code: typeof error?.code === 'string' |
| 503 | ? error.code.replace(/[^A-Za-z0-9_.-]/g, '').slice(0, 80) |
| 504 | : null, |
| 505 | status: Number.isFinite(Number(error?.status)) ? Number(error.status) : null |
| 506 | }); |
| 507 | |
| 508 | if (window.networkLogger) { |
| 509 | window.networkLogger.logRequest({ |
| 510 | type: 'openrouter', |
| 511 | method: 'POST', |
| 512 | url: url, |
| 513 | status: Number.isFinite(error?.status) ? error.status : 0, |
no test coverage detected