(prompt: string)
| 575 | } |
| 576 | |
| 577 | async completePrompt(prompt: string) { |
| 578 | const { id: modelId, maxTokens, temperature, reasoning } = await this.fetchModel() |
| 579 | |
| 580 | const completionParams: OpenRouterChatCompletionParams = { |
| 581 | model: modelId, |
| 582 | max_tokens: maxTokens, |
| 583 | temperature, |
| 584 | messages: [{ role: "user", content: prompt }], |
| 585 | stream: false, |
| 586 | // Only include provider if openRouterSpecificProvider is not "[default]". |
| 587 | ...(this.options.openRouterSpecificProvider && |
| 588 | this.options.openRouterSpecificProvider !== OPENROUTER_DEFAULT_PROVIDER_NAME && { |
| 589 | provider: { |
| 590 | order: [this.options.openRouterSpecificProvider], |
| 591 | only: [this.options.openRouterSpecificProvider], |
| 592 | allow_fallbacks: false, |
| 593 | }, |
| 594 | }), |
| 595 | ...(reasoning && { reasoning }), |
| 596 | } |
| 597 | |
| 598 | // Add Anthropic beta header for fine-grained tool streaming when using Anthropic models |
| 599 | const requestOptions = modelId.startsWith("anthropic/") |
| 600 | ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } |
| 601 | : undefined |
| 602 | |
| 603 | let response |
| 604 | |
| 605 | try { |
| 606 | response = await this.client.chat.completions.create(completionParams, requestOptions) |
| 607 | } catch (error) { |
| 608 | // Try to parse as OpenRouter error structure using Zod |
| 609 | const parseResult = OpenRouterErrorResponseSchema.safeParse(error) |
| 610 | |
| 611 | if (parseResult.success && parseResult.data.error) { |
| 612 | const openRouterError = parseResult.data |
| 613 | const rawString = openRouterError.error?.metadata?.raw |
| 614 | const parsedError = extractErrorFromMetadataRaw(rawString) |
| 615 | const rawErrorMessage = parsedError || openRouterError.error?.message || "Unknown error" |
| 616 | |
| 617 | const apiError = Object.assign( |
| 618 | new ApiProviderError( |
| 619 | rawErrorMessage, |
| 620 | this.providerName, |
| 621 | modelId, |
| 622 | "completePrompt", |
| 623 | openRouterError.error?.code, |
| 624 | ), |
| 625 | { |
| 626 | status: openRouterError.error?.code, |
| 627 | error: openRouterError.error, |
| 628 | }, |
| 629 | ) |
| 630 | |
| 631 | TelemetryService.instance.captureException(apiError) |
| 632 | throw handleOpenAIError(error, this.providerName) |
| 633 | } else { |
| 634 | // Fallback for non-OpenRouter errors |
nothing calls this directly
no test coverage detected