Skip to content

nimgent/providers/anthropic

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

Anthropic Messages API adapter.

Default Anthropic Messages endpoint.

defaultAnthropicEndpoint = "https://api.anthropic.com/v1/messages"

View source

Explicit known families; unknown models retain manual thinking support.

proc anthropicEfforts(model: string): seq[string] {.raises: [], tags: [],
forbids: [].}

Returns: seq[string].

Name Type Default
model string

View source

Convert a thinking level to Anthropic request options.

proc anthropicThinkingOptions(model, level: string): JsonNode {.
raises: [ValueError], tags: [], forbids: [].}

Returns: JsonNode.

Name Type Default
model string
level string

View source

Encode base64 image data as an Anthropic content block.

proc anthropicImageBlock(mimeType, data: string): JsonNode {.raises: [],
tags: [], forbids: [].}

Returns: JsonNode.

Name Type Default
mimeType string
data string

View source

Encode file content as an Anthropic document block.

proc anthropicDocument(f: FileContent): JsonNode {.raises: [], tags: [],
forbids: [].}

Returns: JsonNode.

Name Type Default
f FileContent

View source

Parse an Anthropic Messages response.

proc parseAnthropicOutput(data: JsonNode): ProviderResponse {.
raises: [ProviderError, KeyError], tags: [], forbids: [].}

Returns: ProviderResponse.

Name Type Default
data JsonNode

View source

Create an Anthropic provider.

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

Returns: AnthropicProvider.

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

View source

Build an Anthropic Messages request body.

proc buildAnthropicBody(request: ProviderRequest): JsonNode {.
raises: [ProviderError, KeyError], tags: [ReadIOEffect, WriteIOEffect],
forbids: [].}

Returns: JsonNode.

Name Type Default
request ProviderRequest

View source

Consume one Anthropic Messages SSE event.

proc handleAnthropicEvent(message: var JsonNode; args: var seq[string];
data: JsonNode; onEvent: StreamCallback): SseAction {.
raises: [KeyError, Exception, ProviderError],
tags: [RootEffect, ReadIOEffect, WriteIOEffect], forbids: [].}

Returns: SseAction.

Name Type Default
message var JsonNode
args var seq[string]
data JsonNode
onEvent StreamCallback

View source

Serialize Anthropic-specific request options.

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

Returns: JsonNode.

Name Type Default
value AnthropicOptions

View source

Anthropic Messages provider configuration.

AnthropicProvider = ref object of Provider
endpoint*: string
userAgent*: string ## Header carrying a stable conversation id; empty sends none. OpenCode Zen's
## gateway requires `x-opencode-session` on its Messages endpoint.
sessionHeader*: string

View source

Anthropic thinking mode.

AnthropicThinking = enum
DisabledThinking = "disabled", AdaptiveThinking = "adaptive",
EnabledThinking = "enabled"

View source

Anthropic-specific structured and thinking settings.

AnthropicOptions = object
thinking*: Option[AnthropicThinking]
budgetTokens*: Option[int] ## Required with EnabledThinking.

View source

Return Anthropic native structured-output options.

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

Returns: JsonNode.

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

View source

Send a request through the Anthropic Messages API.

method generateAsync(provider: AnthropicProvider; 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 AnthropicProvider
request ProviderRequest

View source

Stream a request through the Anthropic Messages API.

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

Returns: Future[ProviderResponse].

Name Type Default
provider AnthropicProvider
request ProviderRequest
onEvent StreamCallback

View source