MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / completePrompt

Method completePrompt

src/api/providers/openrouter.ts:577–648  ·  view source on GitHub ↗
(prompt: string)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

fetchModelMethod · 0.95
handleStreamingErrorMethod · 0.95
handleOpenAIErrorFunction · 0.90
captureExceptionMethod · 0.65
StringInterface · 0.50
createMethod · 0.45

Tested by

no test coverage detected