Skip to content

nimgent/providers/openai

This page is generated from the module’s exported API and ## documentation comments.

OpenAI-family HTTP provider.

Native OpenAI uses the Responses API (store: false, reasoning replay). OpenRouter, Hyper, Mistral, OpenCode’s Chat protocol, and any */chat/completions URL use Chat Completions. Provider wire fields such as session_id, cache_control, and HTTP-Referer stay optional on this type.

Default OpenAI Responses endpoint.

defaultOpenAiEndpoint = "https://api.openai.com/v1/responses"

View source

Default OpenAI Chat Completions endpoint.

defaultOpenAiChatEndpoint = "https://api.openai.com/v1/chat/completions"

View source

Default OpenRouter endpoint.

defaultOpenRouterEndpoint = "https://openrouter.ai/api/v1/chat/completions"

View source

Default Hyper endpoint.

defaultHyperEndpoint = "https://hyper.charm.land/v1/chat/completions"

View source

Default Mistral endpoint.

defaultMistralEndpoint = "https://api.mistral.ai/v1/chat/completions"

View source

Default OpenCode endpoint.

defaultOpenCodeEndpoint = "https://opencode.ai/zen/go/v1/chat/completions"

View source

Default OpenCode Zen endpoint.

defaultOpenCodeZenEndpoint = "https://opencode.ai/zen/v1/chat/completions"

View source

The gateway root the protocol paths hang off, or “” when url is not a recognized endpoint. https://host/v1/chat/completionshttps://host/v1, which is also where Gemini’s /models/<id>:generateContent lives.

proc gatewayBase(url: string): string {.raises: [], tags: [], forbids: [].}

Returns: string.

Name Type Default
url string

View source

A gateway endpoint’s sibling path, or “” when no known path is present.

proc siblingEndpoint(url, sibling: string): string {.raises: [], tags: [],
forbids: [].}

Returns: string.

Name Type Default
url string
sibling string

View source

Responses API by default. A */chat/completions URL stays on that wire format.

proc openAI(apiKey: string; endpoint = ""; timeoutSeconds = 300; userAgent = ""): OpenAIProvider {.
raises: [], tags: [], forbids: [].}

Returns: OpenAIProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred ""

View source

Create an OpenRouter provider.

proc openRouter(apiKey: string; endpoint = ""; timeoutSeconds = 300;
siteUrl = ""; siteName = ""; userAgent = ""): OpenRouterProvider {.
raises: [], tags: [], forbids: [].}

Returns: OpenRouterProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
siteUrl inferred ""
siteName inferred ""
userAgent inferred ""

View source

Hyper’s documented agent API is Chat Completions. Their /v1/responses pass-through 400s OpenAI input items, so this stays on chat.

proc hyper(apiKey: string; endpoint = ""; timeoutSeconds = 300; userAgent = ""): HyperProvider {.
raises: [], tags: [], forbids: [].}

Returns: HyperProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred ""

View source

Create an OpenCode provider using Chat Completions.

proc openCodeChat(apiKey: string; endpoint = ""; timeoutSeconds = 300;
userAgent = "nimgent"): OpenCodeProvider {.raises: [],
tags: [], forbids: [].}

Returns: OpenCodeProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred "nimgent"

View source

Create an OpenCode provider using the Responses API.

proc openCodeResponses(apiKey: string; endpoint = ""; timeoutSeconds = 300;
userAgent = "nimgent"): OpenCodeProvider {.raises: [],
tags: [], forbids: [].}

Returns: OpenCodeProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred "nimgent"

View source

Create an OpenCode provider using the Anthropic Messages API.

proc openCodeMessages(apiKey: string; endpoint = ""; timeoutSeconds = 300;
userAgent = "nimgent"): AnthropicProvider {.raises: [],
tags: [], forbids: [].}

Returns: AnthropicProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred "nimgent"

View source

The gateway’s Gemini models on the native Google surface: the same key and gateway root, /models/<id>:generateContent instead of an OpenAI path.

proc openCodeGoogle(apiKey: string; endpoint = ""; timeoutSeconds = 300;
userAgent = "nimgent"): GoogleProvider {.raises: [],
tags: [], forbids: [].}

Returns: GoogleProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred "nimgent"

View source

OpenCode’s gateways expose separate wire protocols. nimgent keeps that choice explicit; Nimlet may add catalog-aware routing above these adapters.

proc openCode(apiKey: string; endpoint = ""; timeoutSeconds = 300;
userAgent = "nimgent"; protocol = ocChat): Provider {.
raises: [ProviderError], tags: [], forbids: [].}

Returns: Provider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred "nimgent"
protocol inferred ocChat

View source

OpenCode Zen’s full catalog: the same protocols as the Go subscription, on the plain /zen/v1 gateway.

proc openCodeZen(apiKey: string; endpoint = ""; timeoutSeconds = 300;
userAgent = "nimgent"; protocol = ocChat): Provider {.
raises: [ProviderError], tags: [], forbids: [].}

Returns: Provider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred "nimgent"
protocol inferred ocChat

View source

Create a Mistral provider.

proc mistral(apiKey: string; endpoint = ""; timeoutSeconds = 300; userAgent = ""): MistralProvider {.
raises: [], tags: [], forbids: [].}

Returns: MistralProvider.

Name Type Default
apiKey string
endpoint inferred ""
timeoutSeconds inferred 300
userAgent inferred ""

View source

Endpoint this model goes to. A per-model route wins over endpoint.

proc endpointFor(provider: OpenAIProvider; model: string): string {.
raises: [Exception], tags: [RootEffect], forbids: [].}

Returns: string.

Name Type Default
provider OpenAIProvider
model string

View source

Whether this model speaks the Responses wire format on that endpoint.

proc usesResponsesFor(provider: OpenAIProvider; model: string): bool {.
raises: [Exception], tags: [RootEffect], forbids: [].}

Returns: bool.

Name Type Default
provider OpenAIProvider
model string

View source

Serialize OpenAI-specific request options.

proc toProviderJson(value: OpenAIOptions): JsonNode {.raises: [], tags: [],
forbids: [].}

Returns: JsonNode.

Name Type Default
value OpenAIOptions

View source

Select an endpoint for a model.

OpenAiRoute = proc (model: string): string {.closure.}

View source

Wire protocol used by an OpenCode gateway.

OpenCodeProtocol = enum
ocChat, ocResponses, ocMessages, ocGoogle

View source

OpenAI-compatible HTTP provider configuration.

OpenAIProvider = ref object of Provider
apiKey*: string
endpoint*: string
timeoutSeconds*: int
siteUrl*: string
siteName*: string ## OpenRouter continuation hint; omitted for native OpenAI.
includeConversationId*: bool ## Anthropic-style cache breakpoints; OpenAI caches stable prefixes itself.
applyCache*: bool ## Mistral prompt caching key derived from the request session ID.
promptCacheKey*: bool ## "max_tokens" (OpenRouter/Mistral) or "max_completion_tokens" (Chat Completions).
maxTokensField*: string
displayName*: string ## True: POST /v1/responses. False: Chat Completions (OpenRouter / compat).
useResponses*: bool
embeddingsEndpoint*: string ## Header carrying a stable conversation id. Empty: the id stays in the body
## (`includeConversationId`) or is not sent. OpenCode Zen wants `x-opencode-session`.
sessionHeader*: string ## Gateway-facing client identity; empty keeps the HTTP library default.
userAgent*: string ## Models this gateway only serves on another surface.
route*: OpenAiRoute ## Replayed-thinking flavor of this gateway's Chat Completions wire.
chatReasoning*: ChatReasoning

View source

OpenRouter provider configuration.

OpenRouterProvider = OpenAIProvider

View source

Hyper provider configuration.

HyperProvider = OpenAIProvider

View source

Mistral provider configuration.

MistralProvider = OpenAIProvider

View source

OpenCode provider configuration.

OpenCodeProvider = OpenAIProvider

View source

Optional OpenAI-specific request settings.

OpenAIOptions = object
parallelToolCalls*: Option[bool]
store*: Option[bool]
user*: Option[string]
dimensions*: Option[int] ## Embedding requests only.

View source

Return native structured-output options for the model’s wire format.

method nativeObjectOptions(provider: OpenAIProvider;
model, name, description: string; schema: JsonNode): JsonNode {.
raises: [Exception], tags: [RootEffect], forbids: [].}

Returns: JsonNode.

Name Type Default
provider OpenAIProvider
model string
name string
description string
schema JsonNode

View source

Return schema issues for the model’s native structured-output format.

method nativeObjectSchemaIssues(provider: OpenAIProvider; model: string;
schema: JsonNode): seq[string] {.
raises: [KeyError], tags: [], forbids: [].}

Returns: seq[string].

Name Type Default
provider OpenAIProvider
model string
schema JsonNode

View source

Send a generation request through an OpenAI-compatible endpoint.

method generateAsync(provider: OpenAIProvider; request: ProviderRequest): Future[
ProviderResponse] {.stackTrace: false, raises: [Exception, ValueError,
ProviderError, KeyError, LibraryError, SslError, IOError], tags: [
RootEffect, ReadIOEffect, WriteIOEffect, ReadDirEffect, ReadEnvEffect,
TimeEffect], forbids: [].}

Returns: Future[ProviderResponse].

Name Type Default
provider OpenAIProvider
request ProviderRequest

View source

Send an embedding request through an OpenAI-compatible endpoint.

method embedAsync(provider: OpenAIProvider; request: EmbeddingRequest): Future[
EmbeddingResponse] {.stackTrace: false, raises: [Exception, ValueError,
ProviderError, LibraryError, SslError, IOError, KeyError], tags: [
RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect, ReadIOEffect,
WriteIOEffect], forbids: [].}

Returns: Future[EmbeddingResponse].

Name Type Default
provider OpenAIProvider
request EmbeddingRequest

View source

Stream a generation request through an OpenAI-compatible endpoint.

method generateStreamAsync(provider: OpenAIProvider; request: ProviderRequest;
onEvent: StreamCallback): Future[ProviderResponse] {.
stackTrace: false, raises: [Exception, ValueError, ProviderError,
LibraryError, SslError, IOError, KeyError,
OSError], tags: [RootEffect, ReadDirEffect,
ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect], forbids: [].}

Returns: Future[ProviderResponse].

Name Type Default
provider OpenAIProvider
request ProviderRequest
onEvent StreamCallback

View source