* Performs a non-streaming chat completion and returns the full response text. * * Anthropic-format models are completed via the `/v1/messages` endpoint; * all other models use the OpenAI-compatible chat completions endpoint. * * @param prompt - The user prompt to send as a single user mes
(prompt: string)
| 486 | * @throws Error with an Opencode Go-specific prefix if the request fails. |
| 487 | */ |
| 488 | async completePrompt(prompt: string): Promise<string> { |
| 489 | const { id: modelId, format, temperature, reasoningEffort, maxTokens } = await this.resolveModel() |
| 490 | |
| 491 | if (format === "anthropic") { |
| 492 | try { |
| 493 | const message = await this.anthropicClient.messages.create({ |
| 494 | model: modelId, |
| 495 | // Honour the same includeMaxTokens/modelMaxTokens override |
| 496 | // logic as the streaming path so non-streaming completions |
| 497 | // respect the user's max-output slider instead of always |
| 498 | // falling back to the model default. |
| 499 | max_tokens: |
| 500 | this.options.includeMaxTokens === true |
| 501 | ? this.options.modelMaxTokens || maxTokens || 16_384 |
| 502 | : (maxTokens ?? 16_384), |
| 503 | temperature: this.supportsTemperature(modelId) ? (temperature ?? 1.0) : undefined, |
| 504 | messages: [{ role: "user", content: prompt }], |
| 505 | stream: false, |
| 506 | }) |
| 507 | |
| 508 | const content = message.content.find(({ type }) => type === "text") |
| 509 | return content?.type === "text" ? content.text : "" |
| 510 | } catch (error) { |
| 511 | if (error instanceof Error) { |
| 512 | throw new Error(`Opencode Go completion error: ${error.message}`) |
| 513 | } |
| 514 | throw error |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | try { |
| 519 | const requestOptions: OpenAI.Chat.ChatCompletionCreateParams = { |
| 520 | model: modelId, |
| 521 | messages: [{ role: "user", content: prompt }], |
| 522 | stream: false, |
| 523 | } |
| 524 | |
| 525 | if (this.supportsTemperature(modelId)) { |
| 526 | requestOptions.temperature = temperature |
| 527 | } |
| 528 | |
| 529 | requestOptions.max_completion_tokens = |
| 530 | this.options.includeMaxTokens === true ? this.options.modelMaxTokens || maxTokens : maxTokens |
| 531 | |
| 532 | if (reasoningEffort) { |
| 533 | requestOptions.reasoning_effort = |
| 534 | reasoningEffort as OpenAI.Chat.ChatCompletionCreateParams["reasoning_effort"] |
| 535 | } |
| 536 | |
| 537 | const response = await this.client.chat.completions.create(requestOptions) |
| 538 | return response.choices[0]?.message.content || "" |
| 539 | } catch (error) { |
| 540 | if (error instanceof Error) { |
| 541 | throw new Error(`Opencode Go completion error: ${error.message}`) |
| 542 | } |
| 543 | throw error |
| 544 | } |
| 545 | } |
nothing calls this directly
no test coverage detected