@nemesis-oss/ollama-sdk 1.8.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +182 -0
- package/README.md +247 -11
- package/dist/{anthropic-B_UXplNF.d.ts → anthropic-CPaoxeLX.d.ts} +4 -2
- package/dist/{anthropic-CVDFy_a0.d.cts → anthropic-DuE8M6Tz.d.cts} +4 -2
- package/dist/{compose-DLStUfaF.d.ts → compose-BQYXdpar.d.ts} +1 -1
- package/dist/{compose-DS-UWFx8.d.cts → compose-lt-Qz3lI.d.cts} +1 -1
- package/dist/generated-api.cjs +42 -3
- package/dist/generated-api.cjs.map +1 -1
- package/dist/generated-api.d.cts +96 -6
- package/dist/generated-api.d.ts +96 -6
- package/dist/generated-api.js +41 -4
- package/dist/generated-api.js.map +1 -1
- package/dist/generated-models-schemas.cjs +62 -2
- package/dist/generated-models-schemas.cjs.map +1 -1
- package/dist/generated-models-schemas.d.cts +108 -3
- package/dist/generated-models-schemas.d.ts +108 -3
- package/dist/generated-models-schemas.js +56 -3
- package/dist/generated-models-schemas.js.map +1 -1
- package/dist/generated-runtime.cjs +105 -5
- package/dist/generated-runtime.cjs.map +1 -1
- package/dist/generated-runtime.d.cts +2 -2
- package/dist/generated-runtime.d.ts +2 -2
- package/dist/generated-runtime.js +105 -5
- package/dist/generated-runtime.js.map +1 -1
- package/dist/index.cjs +1644 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1506 -14
- package/dist/index.d.ts +1506 -14
- package/dist/index.js +1607 -55
- package/dist/index.js.map +1 -1
- package/dist/mcp/tools.json +43 -24
- package/dist/mcp-generated.cjs +43 -1
- package/dist/mcp-generated.cjs.map +1 -1
- package/dist/mcp-generated.d.cts +2 -2
- package/dist/mcp-generated.d.ts +2 -2
- package/dist/mcp-generated.js +43 -1
- package/dist/mcp-generated.js.map +1 -1
- package/dist/{runtime-C6UYtBRB.d.ts → runtime-BV9SK_cf.d.ts} +69 -2
- package/dist/{runtime-DTmUeU6K.d.cts → runtime-BsdBbuOv.d.cts} +69 -2
- package/dist/skills.cjs.map +1 -1
- package/dist/skills.d.cts +3 -3
- package/dist/skills.d.ts +3 -3
- package/dist/skills.js.map +1 -1
- package/dist/{types-5PAjLoCp.d.cts → types-BtQexFpT.d.cts} +113 -10
- package/dist/{types-5PAjLoCp.d.ts → types-BtQexFpT.d.ts} +113 -10
- package/package.json +11 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,127 @@
|
|
|
1
|
-
import { F as FetchLike, M as Middleware, L as Logger, R as RequestLifecycleHook, H as HttpClient, c as OllamaStreamEvent, A as AbortableAsyncIterable, d as OllamaStreamEventType, P as ProgressStreamResult, B as BinaryBody, O as OllamaRuntime, C as ChatStreamResult, G as GenerateStreamResult, e as RawUsageSource, T as TokenUsage } from './runtime-
|
|
2
|
-
export { D as DoneEvent, E as ErrorEvent, f as HttpBody, g as HttpClientOptions, h as HttpRequestOptions, i as LifecycleErrorEvent, j as LifecycleRetryEvent, k as LifecycleStartEvent, l as LifecycleSuccessEvent, m as LogFn, n as MessageEvent, o as MiddlewareContext, N as NANOS_PER_MS, p as NANOS_PER_SECOND, q as NOOP_LOGGER, r as NextFunction, s as OllamaAbortError, t as OllamaAgentMaxIterationsError, u as OllamaAgentMaxToolCallsError, v as OllamaAuthError, w as
|
|
3
|
-
import { T as ThinkingMetadata, M as ModelResponse, S as ShowRequestOptions, a as ShowResponse, P as PullRequestOptions, b as ProgressResponse, c as PushRequestOptions, C as CreateRequestOptions, D as DeleteRequestOptions, d as StatusResponse, e as CopyRequestOptions, f as PsResponse, V as VersionResponse,
|
|
4
|
-
export {
|
|
1
|
+
import { F as FetchLike, M as Middleware, L as Logger, R as RequestLifecycleHook, H as HttpClient, c as OllamaStreamEvent, A as AbortableAsyncIterable, d as OllamaStreamEventType, P as ProgressStreamResult, B as BinaryBody, O as OllamaRuntime, C as ChatStreamResult, G as GenerateStreamResult, e as RawUsageSource, T as TokenUsage } from './runtime-BV9SK_cf.js';
|
|
2
|
+
export { D as DoneEvent, E as ErrorEvent, f as HttpBody, g as HttpClientOptions, h as HttpRequestOptions, i as LifecycleErrorEvent, j as LifecycleRetryEvent, k as LifecycleStartEvent, l as LifecycleSuccessEvent, m as LogFn, n as MessageEvent, o as MiddlewareContext, N as NANOS_PER_MS, p as NANOS_PER_SECOND, q as NOOP_LOGGER, r as NextFunction, s as OllamaAbortError, t as OllamaAgentMaxIterationsError, u as OllamaAgentMaxToolCallsError, v as OllamaAuthError, w as OllamaBadGatewayError, x as OllamaClientError, y as OllamaClientErrorOptions, z as OllamaErrorRequestContext, J as OllamaErrorResponseContext, K as OllamaGenericClientError, Q as OllamaIncompatibleModelError, U as OllamaMcpError, V as OllamaModelRoutingError, W as OllamaNetworkError, X as OllamaNotFoundError, Y as OllamaOverloadedError, Z as OllamaQuotaExceededError, _ as OllamaRateLimitError, $ as OllamaRequestTooLargeError, a0 as OllamaRequestValidationError, a1 as OllamaResponseValidationError, a2 as OllamaServerError, a3 as OllamaServerVersionUnknownError, a4 as OllamaSkillInvalidError, a5 as OllamaSkillNotFoundError, a6 as OllamaStreamError, a7 as OllamaTimeoutError, a8 as OllamaToolTimeoutError, a9 as OllamaToolValidationError, aa as OllamaUnsupportedCapabilityError, ab as RequestContext, ac as RequestLifecycleEvent, ad as ResponseContext, ae as SseEvent, af as ThinkingEvent, ag as TokenEvent, ah as ToolCallEvent, ai as composeMiddleware, aj as createConsoleLogger, ak as extractUsage, al as mapError, am as parseSseStream } from './runtime-BV9SK_cf.js';
|
|
3
|
+
import { T as ThinkingMetadata, M as ModelResponse, S as ShowRequestOptions, a as ShowResponse, P as PullRequestOptions, b as ProgressResponse, c as PushRequestOptions, C as CreateRequestOptions, D as DeleteRequestOptions, d as StatusResponse, e as CopyRequestOptions, f as PsResponse, V as VersionResponse, R as RequestCancellationOptions, g as ToolParameters, h as ThinkValue, i as ToolCall, j as ChatRequestOptions, k as ModelOptions, l as ToolDefinition, F as FormatOption, m as ChatResponse, n as Message, G as GenerateRequestOptions, o as GenerateResponse, E as EmbedRequestOptions, p as EmbedResponse, q as EmbeddingsRequestOptions, r as EmbeddingsResponse, W as WebSearchRequestOptions, s as WebSearchResponse, t as WebFetchRequestOptions, u as WebFetchResponse, U as UsageRequestOptions, B as BalanceRequestOptions, v as VisionInput } from './types-BtQexFpT.js';
|
|
4
|
+
export { L as ListResponse, w as Logprob, x as LogprobToken, y as ModelDetails, z as Role, A as ToolCallFunction, H as ToolFunctionDefinition, I as ToolProperty, J as UsageRange, K as UsageScope, N as WebSearchResult, O as imageStringNeedsResolution, Q as resolveImageInput } from './types-BtQexFpT.js';
|
|
5
5
|
import { z } from 'zod';
|
|
6
|
-
import { R as RequestRunner, S as SystemOneChoiceAnswer, o as SystemOneNoulAnswer, p as SystemOneScoreAnswer, q as SystemOneChoiceQuestion, r as SystemOneNoulQuestion, s as SystemOneScoreQuestion, t as SystemOneRequest$1, u as SystemOneResponse$1, v as SystemOneContent, N as NativeApi, w as OpenAICompatClient, x as AnthropicCompatClient } from './anthropic-
|
|
7
|
-
export { y as AnthropicCacheControl, z as AnthropicContentBlock, B as AnthropicImageContentBlock, C as AnthropicMessage, n as AnthropicMessageStreamEvent, A as AnthropicMessagesRequest, m as AnthropicMessagesResponse, D as AnthropicMessagesStream, E as AnthropicMetadata, F as AnthropicOutputConfig, G as AnthropicRedactedThinkingContentBlock, H as AnthropicSystem, I as AnthropicSystemTextBlock, J as AnthropicTextContentBlock, K as AnthropicThinkingConfig, L as AnthropicThinkingContentBlock, M as AnthropicTool, P as AnthropicToolChoice, Q as AnthropicToolResultContentBlock, T as AnthropicToolUseContentBlock, U as AnthropicUnknownStreamEvent, V as OllamaAnthropicContentBlock, W as OllamaAnthropicMessage, X as OllamaAnthropicMessagesRequest, Y as OllamaAnthropicSystem, Z as OllamaAnthropicSystemTextBlock, _ as OllamaAnthropicTextContentBlock, $ as OllamaAnthropicThinkingConfig, a0 as OllamaAnthropicTool, a1 as OllamaAnthropicToolResultContentBlock, a2 as OllamaOpenAIChatCompletionRequest, a3 as OllamaOpenAICompletionRequest, a4 as OllamaOpenAIEmbeddingRequest, a5 as OllamaOpenAIReasoningEffort, a6 as OllamaOpenAIResponsesRequest, a7 as OpenAIChatCompletionChoice, b as OpenAIChatCompletionChunk, a8 as OpenAIChatCompletionChunkChoice, a9 as OpenAIChatCompletionDelta, O as OpenAIChatCompletionRequest, a as OpenAIChatCompletionResponse, aa as OpenAIChatCompletionStream, ab as OpenAICompletionChoice, e as OpenAICompletionChunk, c as OpenAICompletionRequest, d as OpenAICompletionResponse, ac as OpenAICompletionStream, ad as OpenAIContentPart, ae as OpenAIEmbeddingInput, af as OpenAIEmbeddingItem, f as OpenAIEmbeddingRequest, g as OpenAIEmbeddingResponse, ag as OpenAIFunctionDefinition, ah as OpenAIImageUrlContentPart, h as OpenAIListModelsResponse, ai as OpenAIMessage, i as OpenAIModelItem, aj as OpenAIReasoningEffort, ak as OpenAIResponseFormat, al as OpenAIResponsesCompletedEvent, am as OpenAIResponsesContentPartAddedEvent, an as OpenAIResponsesContentPartDoneEvent, ao as OpenAIResponsesCreatedEvent, ap as OpenAIResponsesError, aq as OpenAIResponsesEventBase, ar as OpenAIResponsesFailedEvent, as as OpenAIResponsesFunctionCallArgumentsDeltaEvent, at as OpenAIResponsesFunctionCallArgumentsDoneEvent, au as OpenAIResponsesInProgressEvent, av as OpenAIResponsesIncompleteDetails, aw as OpenAIResponsesIncompleteEvent, ax as OpenAIResponsesOutputContent, ay as OpenAIResponsesOutputFunctionCall, az as OpenAIResponsesOutputItem, aA as OpenAIResponsesOutputItemAddedEvent, aB as OpenAIResponsesOutputItemDoneEvent, aC as OpenAIResponsesOutputMessage, aD as OpenAIResponsesOutputReasoning, aE as OpenAIResponsesOutputRefusal, aF as OpenAIResponsesOutputTextContent, aG as OpenAIResponsesOutputTextDeltaEvent, aH as OpenAIResponsesOutputTextDoneEvent, aI as OpenAIResponsesQueuedEvent, aJ as OpenAIResponsesReasoningSummaryPartAddedEvent, aK as OpenAIResponsesReasoningSummaryPartDoneEvent, aL as OpenAIResponsesReasoningSummaryTextDeltaEvent, aM as OpenAIResponsesReasoningSummaryTextDoneEvent, aN as OpenAIResponsesReasoningTextDeltaEvent, aO as OpenAIResponsesReasoningTextDoneEvent, aP as OpenAIResponsesRefusalDeltaEvent, aQ as OpenAIResponsesRefusalDoneEvent, j as OpenAIResponsesRequest, k as OpenAIResponsesResponse, aR as OpenAIResponsesStatus, aS as OpenAIResponsesStream, aT as OpenAIResponsesStreamError, l as OpenAIResponsesStreamEvent, aU as OpenAIResponsesUsage, aV as OpenAIStreamOptions, aW as OpenAITextContentPart, aX as OpenAITool, aY as OpenAIToolCall, aZ as OpenAIToolCallDelta, a_ as OpenAIUsage, a$ as SystemOneConfidence, b0 as SystemOneProbabilities } from './anthropic-
|
|
8
|
-
import { T as ToolHandler, a as Tool, A as AnyTool, b as ToolRegistry, c as ToolExecutionResult } from './compose-
|
|
9
|
-
export { d as AppliedSkillResult, e as ApplySkillOptions, P as ParsedFrontmatter, S as Skill, f as SkillFrontmatter, g as SkillSummary, h as ToolExecutionContext, i as ToolExecutionFailure, j as ToolExecutionSuccess, k as ToolRegistryOptions, l as applySkill, p as parseFrontmatter } from './compose-
|
|
6
|
+
import { R as RequestRunner, S as SystemOneChoiceAnswer, o as SystemOneNoulAnswer, p as SystemOneScoreAnswer, q as SystemOneChoiceQuestion, r as SystemOneNoulQuestion, s as SystemOneScoreQuestion, t as SystemOneRequest$1, u as SystemOneResponse$1, v as SystemOneContent, N as NativeApi, w as OpenAICompatClient, x as AnthropicCompatClient } from './anthropic-CPaoxeLX.js';
|
|
7
|
+
export { y as AnthropicCacheControl, z as AnthropicContentBlock, B as AnthropicImageContentBlock, C as AnthropicMessage, n as AnthropicMessageStreamEvent, A as AnthropicMessagesRequest, m as AnthropicMessagesResponse, D as AnthropicMessagesStream, E as AnthropicMetadata, F as AnthropicOutputConfig, G as AnthropicRedactedThinkingContentBlock, H as AnthropicSystem, I as AnthropicSystemTextBlock, J as AnthropicTextContentBlock, K as AnthropicThinkingConfig, L as AnthropicThinkingContentBlock, M as AnthropicTool, P as AnthropicToolChoice, Q as AnthropicToolResultContentBlock, T as AnthropicToolUseContentBlock, U as AnthropicUnknownStreamEvent, V as OllamaAnthropicContentBlock, W as OllamaAnthropicMessage, X as OllamaAnthropicMessagesRequest, Y as OllamaAnthropicSystem, Z as OllamaAnthropicSystemTextBlock, _ as OllamaAnthropicTextContentBlock, $ as OllamaAnthropicThinkingConfig, a0 as OllamaAnthropicTool, a1 as OllamaAnthropicToolResultContentBlock, a2 as OllamaOpenAIChatCompletionRequest, a3 as OllamaOpenAICompletionRequest, a4 as OllamaOpenAIEmbeddingRequest, a5 as OllamaOpenAIReasoningEffort, a6 as OllamaOpenAIResponsesRequest, a7 as OpenAIChatCompletionChoice, b as OpenAIChatCompletionChunk, a8 as OpenAIChatCompletionChunkChoice, a9 as OpenAIChatCompletionDelta, O as OpenAIChatCompletionRequest, a as OpenAIChatCompletionResponse, aa as OpenAIChatCompletionStream, ab as OpenAICompletionChoice, e as OpenAICompletionChunk, c as OpenAICompletionRequest, d as OpenAICompletionResponse, ac as OpenAICompletionStream, ad as OpenAIContentPart, ae as OpenAIEmbeddingInput, af as OpenAIEmbeddingItem, f as OpenAIEmbeddingRequest, g as OpenAIEmbeddingResponse, ag as OpenAIFunctionDefinition, ah as OpenAIImageUrlContentPart, h as OpenAIListModelsResponse, ai as OpenAIMessage, i as OpenAIModelItem, aj as OpenAIReasoningEffort, ak as OpenAIResponseFormat, al as OpenAIResponsesCompletedEvent, am as OpenAIResponsesContentPartAddedEvent, an as OpenAIResponsesContentPartDoneEvent, ao as OpenAIResponsesCreatedEvent, ap as OpenAIResponsesError, aq as OpenAIResponsesEventBase, ar as OpenAIResponsesFailedEvent, as as OpenAIResponsesFunctionCallArgumentsDeltaEvent, at as OpenAIResponsesFunctionCallArgumentsDoneEvent, au as OpenAIResponsesInProgressEvent, av as OpenAIResponsesIncompleteDetails, aw as OpenAIResponsesIncompleteEvent, ax as OpenAIResponsesOutputContent, ay as OpenAIResponsesOutputFunctionCall, az as OpenAIResponsesOutputItem, aA as OpenAIResponsesOutputItemAddedEvent, aB as OpenAIResponsesOutputItemDoneEvent, aC as OpenAIResponsesOutputMessage, aD as OpenAIResponsesOutputReasoning, aE as OpenAIResponsesOutputRefusal, aF as OpenAIResponsesOutputTextContent, aG as OpenAIResponsesOutputTextDeltaEvent, aH as OpenAIResponsesOutputTextDoneEvent, aI as OpenAIResponsesQueuedEvent, aJ as OpenAIResponsesReasoningSummaryPartAddedEvent, aK as OpenAIResponsesReasoningSummaryPartDoneEvent, aL as OpenAIResponsesReasoningSummaryTextDeltaEvent, aM as OpenAIResponsesReasoningSummaryTextDoneEvent, aN as OpenAIResponsesReasoningTextDeltaEvent, aO as OpenAIResponsesReasoningTextDoneEvent, aP as OpenAIResponsesRefusalDeltaEvent, aQ as OpenAIResponsesRefusalDoneEvent, j as OpenAIResponsesRequest, k as OpenAIResponsesResponse, aR as OpenAIResponsesStatus, aS as OpenAIResponsesStream, aT as OpenAIResponsesStreamError, l as OpenAIResponsesStreamEvent, aU as OpenAIResponsesUsage, aV as OpenAIStreamOptions, aW as OpenAITextContentPart, aX as OpenAITool, aY as OpenAIToolCall, aZ as OpenAIToolCallDelta, a_ as OpenAIUsage, a$ as SystemOneConfidence, b0 as SystemOneProbabilities } from './anthropic-CPaoxeLX.js';
|
|
8
|
+
import { T as ToolHandler, a as Tool, A as AnyTool, b as ToolRegistry, c as ToolExecutionResult } from './compose-BQYXdpar.js';
|
|
9
|
+
export { d as AppliedSkillResult, e as ApplySkillOptions, P as ParsedFrontmatter, S as Skill, f as SkillFrontmatter, g as SkillSummary, h as ToolExecutionContext, i as ToolExecutionFailure, j as ToolExecutionSuccess, k as ToolRegistryOptions, l as applySkill, p as parseFrontmatter } from './compose-BQYXdpar.js';
|
|
10
10
|
import { M as McpClientLike, a as McpToolDescriptor, b as McpTask, c as McpCallToolResult } from './types-CI-gM2u5.js';
|
|
11
11
|
export { d as McpContentBlock, e as McpCreateTaskResult, f as McpElicitationFormRequest, g as McpElicitationHandlers, h as McpElicitationRequest, i as McpElicitationResult, j as McpElicitationUrlRequest, k as McpIcon, l as McpInputRequiredResult, m as McpListToolsParams, n as McpListToolsResult, o as McpRequestOptions, p as McpServerCapabilities, q as McpTaskMethod, r as McpTaskRequest, s as McpTaskStatus, t as McpToolAnnotations, u as McpToolCallResult, v as McpToolExecution } from './types-CI-gM2u5.js';
|
|
12
12
|
import * as Otel from '@opentelemetry/api';
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
16
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
17
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface BalanceResponse {
|
|
21
|
+
readonly included: IncludedBalance | LegacyIncludedBalance;
|
|
22
|
+
readonly purchased: {
|
|
23
|
+
/** Remaining unexpired purchased credits in USD. */
|
|
24
|
+
readonly balance_usd: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
30
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
31
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
32
|
+
*/
|
|
33
|
+
interface IncludedBalance {
|
|
34
|
+
/** Remaining included credits in USD. */
|
|
35
|
+
readonly balance_usd: number;
|
|
36
|
+
/** Included credits available for the full period in USD. */
|
|
37
|
+
readonly allowance_usd: number;
|
|
38
|
+
readonly period: {
|
|
39
|
+
/** Start of the included period in UTC, inclusive. */
|
|
40
|
+
readonly from: string;
|
|
41
|
+
/** End of the included period in UTC, exclusive. */
|
|
42
|
+
readonly until: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
48
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
49
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
50
|
+
*/
|
|
51
|
+
interface LegacyBalanceLimit {
|
|
52
|
+
/** Percentage of the limit remaining. */
|
|
53
|
+
readonly remaining_percent: number;
|
|
54
|
+
/** Next reset time in UTC. */
|
|
55
|
+
readonly resets_at: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
60
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
61
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
interface LegacyIncludedBalance {
|
|
65
|
+
readonly session: LegacyBalanceLimit;
|
|
66
|
+
readonly weekly: LegacyBalanceLimit;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
71
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
72
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
type UsageBucket = UsageMetrics & {
|
|
76
|
+
/** Start of the bucket in UTC, inclusive. */
|
|
77
|
+
readonly from: string;
|
|
78
|
+
/** End of the bucket in UTC, exclusive. */
|
|
79
|
+
readonly until: string;
|
|
80
|
+
/** Present and true for the current hour or day, which is still in progress. */
|
|
81
|
+
readonly partial?: boolean | undefined;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
86
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
87
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
88
|
+
*/
|
|
89
|
+
interface UsageMetrics {
|
|
90
|
+
/** Number of recorded requests. */
|
|
91
|
+
readonly request_count: number;
|
|
92
|
+
/** Total value of the requests in USD, including usage covered by your plan and usage paid from purchased credits. */
|
|
93
|
+
readonly usage_usd?: number | undefined;
|
|
94
|
+
/** Recorded input tokens, including cached input tokens. */
|
|
95
|
+
readonly input_tokens?: number | undefined;
|
|
96
|
+
/** Recorded input tokens read from cache, already included in input_tokens. */
|
|
97
|
+
readonly cached_input_tokens?: number | undefined;
|
|
98
|
+
/** Recorded output tokens. */
|
|
99
|
+
readonly output_tokens?: number | undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* AUTO-GENERATED by `npm run contract:generate`. DO NOT EDIT.
|
|
104
|
+
* Source: contracts/ir/ollama.ir.json (canonical Ollama IR).
|
|
105
|
+
* See ADR 0013 and ADR 0014 for the contract-first architecture.
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
interface UsageResponse {
|
|
109
|
+
/** Selected time range. */
|
|
110
|
+
readonly range: '24h' | '7d' | '30d';
|
|
111
|
+
/** Selected usage scope. */
|
|
112
|
+
readonly scope: 'self' | 'team';
|
|
113
|
+
/** Set automatically - hour for 24h, day for 7d and 30d. */
|
|
114
|
+
readonly granularity: 'hour' | 'day';
|
|
115
|
+
/** Start of the range in UTC, inclusive. */
|
|
116
|
+
readonly from: string;
|
|
117
|
+
/** End of the range in UTC, exclusive. */
|
|
118
|
+
readonly until: string;
|
|
119
|
+
/** Usage across the entire range. */
|
|
120
|
+
readonly totals: UsageMetrics;
|
|
121
|
+
/** Usage in chronological order, including buckets with no recorded requests. */
|
|
122
|
+
readonly buckets: readonly UsageBucket[];
|
|
123
|
+
}
|
|
124
|
+
|
|
14
125
|
/**
|
|
15
126
|
* Exponential backoff with full jitter calculation.
|
|
16
127
|
*/
|
|
@@ -291,6 +402,31 @@ interface OllamaClientConfig {
|
|
|
291
402
|
readonly logger?: Logger;
|
|
292
403
|
/** Enables console debug logger if true. */
|
|
293
404
|
readonly debug?: boolean;
|
|
405
|
+
/**
|
|
406
|
+
* Default context window (`num_ctx`) applied to every `chat`/`generate`
|
|
407
|
+
* request that doesn't set `options.num_ctx` itself. Injected as
|
|
408
|
+
* `options.num_ctx` on the wire, making the effective window explicit
|
|
409
|
+
* instead of leaving it to Ollama's server default (commonly 2048–4096,
|
|
410
|
+
* model-dependent) — which **silently truncates** prompts that don't fit,
|
|
411
|
+
* with no error and no warning. See `src/context-safety.ts`.
|
|
412
|
+
*/
|
|
413
|
+
readonly defaultContextLength?: number;
|
|
414
|
+
/**
|
|
415
|
+
* Fraction of the effective context window at which the client warns that
|
|
416
|
+
* a request's estimated prompt size is approaching truncation (default
|
|
417
|
+
* `0.9` — see `DEFAULT_CONTEXT_WARNING_THRESHOLD`).
|
|
418
|
+
*/
|
|
419
|
+
readonly contextWarningThreshold?: number;
|
|
420
|
+
/**
|
|
421
|
+
* What happens when the pre-flight context check (see
|
|
422
|
+
* `src/context-safety.ts`) concludes a request will likely be truncated:
|
|
423
|
+
* `'warn'` (default) logs a warning through the configured logger and sends
|
|
424
|
+
* the request anyway; `'throw'` rejects the request client-side with
|
|
425
|
+
* `OllamaClientError` before anything hits the wire. `'throw'` is the
|
|
426
|
+
* safety-critical choice — use it when silent history truncation would
|
|
427
|
+
* corrupt application behavior (e.g. long-context RAG, agents).
|
|
428
|
+
*/
|
|
429
|
+
readonly onContextOverflow?: 'warn' | 'throw';
|
|
294
430
|
/** Request lifecycle hook for telemetry/metrics. */
|
|
295
431
|
readonly onLifecycleEvent?: RequestLifecycleHook;
|
|
296
432
|
}
|
|
@@ -314,6 +450,20 @@ declare function checkEndpointHealth(endpoint: OllamaEndpoint, fetchImpl?: Fetch
|
|
|
314
450
|
*/
|
|
315
451
|
|
|
316
452
|
type RuntimeMode = 'local' | 'cloud' | 'unknown';
|
|
453
|
+
/**
|
|
454
|
+
* Best-effort classification of a model's parallel tool-call emission
|
|
455
|
+
* behavior. Used by agent loops that need to know whether the model
|
|
456
|
+
* is likely to emit multiple `tool_calls` in a single assistant
|
|
457
|
+
* message (parallel) or one-at-a-time (sequential).
|
|
458
|
+
*
|
|
459
|
+
* This is a **heuristic** derived from runtime mode + model family
|
|
460
|
+
* pattern matching against `/api/show`'s reported model_info. The
|
|
461
|
+
* SDK does NOT normalize between cloud and local — it reports the
|
|
462
|
+
* best assessment it can and lets the caller decide how to act.
|
|
463
|
+
*
|
|
464
|
+
* See ADR 0023 for the rationale and the recommended caller pattern.
|
|
465
|
+
*/
|
|
466
|
+
type ParallelToolCallBehavior = 'yes' | 'no' | 'unknown';
|
|
317
467
|
interface ModelCapabilities {
|
|
318
468
|
readonly model: string;
|
|
319
469
|
readonly reported: readonly string[];
|
|
@@ -338,6 +488,44 @@ interface ModelCapabilities {
|
|
|
338
488
|
* making a network call that Ollama Cloud is known to reject.
|
|
339
489
|
*/
|
|
340
490
|
readonly supportsStructuredOutputRequest: boolean;
|
|
491
|
+
/**
|
|
492
|
+
* Best-effort inference of whether the model is likely to emit
|
|
493
|
+
* **multiple `tool_calls` in a single assistant message** (parallel)
|
|
494
|
+
* vs **one tool call per assistant turn** (sequential).
|
|
495
|
+
*
|
|
496
|
+
* The SDK does NOT normalize between cloud and local — it reports
|
|
497
|
+
* the best assessment it can and lets the caller decide. Agent
|
|
498
|
+
* authors who need hard sequential execution should set
|
|
499
|
+
* `ToolRegistry`'s `maxConcurrency: 1` regardless of this field's
|
|
500
|
+
* value, since the model's behavior is not deterministic across
|
|
501
|
+
* prompts.
|
|
502
|
+
*
|
|
503
|
+
* Inference matrix (see ADR 0023):
|
|
504
|
+
*
|
|
505
|
+
* - **`'yes'`** — the model is known to emit parallel tool calls:
|
|
506
|
+
* - Cloud-mode endpoints (the OpenAI/Anthropic compat bridges
|
|
507
|
+
* proxy to proprietary models like GPT-4o, Claude, which
|
|
508
|
+
* freely emit parallel tool calls).
|
|
509
|
+
* - Local models from families that ship with parallel-tool-call
|
|
510
|
+
* training (Qwen 2.5+, Llama 3.1+ tool-use variants, Mistral
|
|
511
|
+
* function-calling fine-tunes, Hermes 2/3, Command R+).
|
|
512
|
+
*
|
|
513
|
+
* - **`'no'`** — the model is known to emit at most one tool call
|
|
514
|
+
* per assistant turn. Currently no model family is classified
|
|
515
|
+
* as `'no'` by default; the field is reserved for future
|
|
516
|
+
* narrowing as more is learned about specific models.
|
|
517
|
+
*
|
|
518
|
+
* - **`'unknown'`** — the model family is not in the known-yes or
|
|
519
|
+
* known-no list, OR `/api/show` didn't return enough
|
|
520
|
+
* information to make an assessment. Callers should treat
|
|
521
|
+
* `'unknown'` as "ask the model and see what it does" — the
|
|
522
|
+
* `ToolRegistry` will execute whatever calls the model emits,
|
|
523
|
+
* in parallel via `Promise.all` by default.
|
|
524
|
+
*
|
|
525
|
+
* See: `src/tools/registry.ts` for `maxConcurrency` enforcement,
|
|
526
|
+
* and ADR 0023 for the full design rationale.
|
|
527
|
+
*/
|
|
528
|
+
readonly parallelToolCalls: ParallelToolCallBehavior;
|
|
341
529
|
}
|
|
342
530
|
declare function inferRuntimeMode(baseUrl: string): RuntimeMode;
|
|
343
531
|
declare function detectModelCapabilities(http: HttpClient, model: string, signal?: AbortSignal): Promise<ModelCapabilities>;
|
|
@@ -375,6 +563,13 @@ declare class OllamaStream<TChunk, TFinal> implements AsyncIterable<OllamaStream
|
|
|
375
563
|
* Model and blob management operations.
|
|
376
564
|
*/
|
|
377
565
|
|
|
566
|
+
/** Result of a convenience blob upload ({@link ModelsClient.createBlobFromData}). */
|
|
567
|
+
interface BlobUploadResult {
|
|
568
|
+
/** Content digest (`sha256:<hex>`) of the uploaded payload. */
|
|
569
|
+
readonly digest: string;
|
|
570
|
+
/** True when the server already had this blob (HTTP 200) and skipped writing. */
|
|
571
|
+
readonly alreadyExisted: boolean;
|
|
572
|
+
}
|
|
378
573
|
/**
|
|
379
574
|
* Every operation here targets one specific Ollama server's local model catalog or blob
|
|
380
575
|
* store — unlike `chat`/`generate`/`embed`, a different endpoint isn't an interchangeable
|
|
@@ -413,10 +608,504 @@ declare class ModelsClient {
|
|
|
413
608
|
copy(request: CopyRequestOptions): Promise<StatusResponse>;
|
|
414
609
|
ps(): Promise<PsResponse>;
|
|
415
610
|
version(): Promise<VersionResponse>;
|
|
611
|
+
/**
|
|
612
|
+
* Immediately unloads `model` from VRAM/RAM by issuing an empty
|
|
613
|
+
* `/api/generate` request with `keep_alive: 0`. The server
|
|
614
|
+
* finalizes any in-flight generation, releases the model's
|
|
615
|
+
* weights from GPU memory, and frees the slot for subsequent
|
|
616
|
+
* pipelines (e.g. swap a vision encoder out before loading an
|
|
617
|
+
* LLM).
|
|
618
|
+
*
|
|
619
|
+
* This is the ergonomic equivalent of:
|
|
620
|
+
*
|
|
621
|
+
* ```ts
|
|
622
|
+
* client.generate({ model, prompt: '', keep_alive: 0 });
|
|
623
|
+
* ```
|
|
624
|
+
*
|
|
625
|
+
* Use {@link pin} for the opposite lifecycle — pinning a model
|
|
626
|
+
* indefinitely in VRAM for hot-loop inference.
|
|
627
|
+
*
|
|
628
|
+
* Resolves once the server acknowledges the unload. The promise
|
|
629
|
+
* rejects on transport errors, model-not-found (404), or auth
|
|
630
|
+
* failures — callers handling transient issues can retry via the
|
|
631
|
+
* standard {@link withRetry} policy.
|
|
632
|
+
*
|
|
633
|
+
* See: https://github.com/ollama/ollama/blob/main/docs/faq.md
|
|
634
|
+
* #how-do-i-keep-a-model-loaded-in-memory-or-make-it-unload-immediately
|
|
635
|
+
*/
|
|
636
|
+
unload(model: string): Promise<void>;
|
|
637
|
+
/**
|
|
638
|
+
* Pre-loads and pins `model` into VRAM indefinitely by issuing an
|
|
639
|
+
* empty `/api/generate` request with `keep_alive: -1`. The server
|
|
640
|
+
* loads the model into GPU memory (paying the cold-load latency
|
|
641
|
+
* once) and keeps it resident until either:
|
|
642
|
+
*
|
|
643
|
+
* - an explicit {@link unload} call is made,
|
|
644
|
+
* - the server process is restarted, or
|
|
645
|
+
* - the host runs out of VRAM and the server's own eviction
|
|
646
|
+
* policy kicks in.
|
|
647
|
+
*
|
|
648
|
+
* This is the ergonomic equivalent of:
|
|
649
|
+
*
|
|
650
|
+
* ```ts
|
|
651
|
+
* client.generate({ model, prompt: '', keep_alive: -1 });
|
|
652
|
+
* ```
|
|
653
|
+
*
|
|
654
|
+
* Use {@link unload} to release the pinned slot when the hot loop
|
|
655
|
+
* is done — leaving models pinned indefinitely exhausts VRAM and
|
|
656
|
+
* starves subsequent pipelines.
|
|
657
|
+
*
|
|
658
|
+
* Resolves once the server acknowledges the pin (i.e. the model
|
|
659
|
+
* is fully loaded). The promise rejects on transport errors,
|
|
660
|
+
* model-not-found (404), or VRAM-exhaustion failures.
|
|
661
|
+
*/
|
|
662
|
+
pin(model: string): Promise<void>;
|
|
416
663
|
createBlob(digest: string, data: BinaryBody): Promise<void>;
|
|
417
664
|
checkBlob(digest: string): Promise<boolean>;
|
|
665
|
+
/**
|
|
666
|
+
* Computes a blob digest the way Ollama's content-addressed store expects:
|
|
667
|
+
* SHA-256 over the raw bytes, formatted as `sha256:<64 lowercase hex>`.
|
|
668
|
+
*
|
|
669
|
+
* Uses the Web Crypto API (`crypto.subtle.digest`), which exists in every
|
|
670
|
+
* supported runtime — Node.js ≥ 19 exposes it as a global (this SDK
|
|
671
|
+
* requires Node ≥ 20), and browsers/edge runtimes have always had it.
|
|
672
|
+
*/
|
|
673
|
+
computeBlobDigest(data: Uint8Array): Promise<string>;
|
|
674
|
+
/**
|
|
675
|
+
* Computes the SHA-256 digest of `data` and pushes it as a blob in one
|
|
676
|
+
* step — the content-addressed upload Ollama's own tooling performs
|
|
677
|
+
* (`curl -T file http://…/api/blobs/sha256:…`). Returns the digest, which
|
|
678
|
+
* `/api/create`'s `files` field references.
|
|
679
|
+
*
|
|
680
|
+
* Pass `digest` to skip recomputation when the caller already knows it —
|
|
681
|
+
* the server verifies the content against the digest and rejects mismatches.
|
|
682
|
+
*/
|
|
683
|
+
createBlobFromData(data: Uint8Array, opts?: {
|
|
684
|
+
readonly digest?: string | undefined;
|
|
685
|
+
readonly signal?: AbortSignal | undefined;
|
|
686
|
+
}): Promise<BlobUploadResult>;
|
|
687
|
+
/**
|
|
688
|
+
* Reads a local file and uploads it as a blob (Node.js only — dynamic
|
|
689
|
+
* `node:fs` import, so browser bundles never pull it in). The file name
|
|
690
|
+
* (basename) is what `/api/create`'s `files` map expects as the key, so
|
|
691
|
+
* this returns it alongside the digest.
|
|
692
|
+
*/
|
|
693
|
+
createBlobFromFile(path: string, opts?: {
|
|
694
|
+
readonly signal?: AbortSignal | undefined;
|
|
695
|
+
}): Promise<BlobUploadResult & {
|
|
696
|
+
readonly fileName: string;
|
|
697
|
+
}>;
|
|
698
|
+
/**
|
|
699
|
+
* One-shot custom-model publishing from raw GGUF weights, following the
|
|
700
|
+
* protocol Ollama's API reference documents: push a blob for the GGUF
|
|
701
|
+
* bytes, then `POST /api/create` with `files: { <fileName>: <digest> }`.
|
|
702
|
+
* Split GGUFs are supported via `files` — pass every shard's path.
|
|
703
|
+
*
|
|
704
|
+
* ```ts
|
|
705
|
+
* await client.models.createModelFromGguf('my-model', '/path/to/model.gguf', {
|
|
706
|
+
* template: '{{ .Prompt }}',
|
|
707
|
+
* });
|
|
708
|
+
* ```
|
|
709
|
+
*
|
|
710
|
+
* @param model Name for the new model (e.g. `'my-model:latest'`).
|
|
711
|
+
* @param gguf Path(s) to GGUF file(s) on the local filesystem.
|
|
712
|
+
* @param opts Everything else `/api/create` accepts (`template`, `system`,
|
|
713
|
+
* `parameters`, `license`, `quantize`, …) except `files` and `from`.
|
|
714
|
+
*/
|
|
715
|
+
createModelFromGguf(model: string, gguf: string | readonly string[], opts?: Omit<CreateRequestOptions, 'model' | 'files' | 'from' | 'stream'>): Promise<ProgressResponse>;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Ergonomic OpenAI Responses API bridge — `client.responses.create()`.
|
|
720
|
+
*
|
|
721
|
+
* Ollama (v0.13.3+) exposes OpenAI's Responses API at `POST /v1/responses` as
|
|
722
|
+
* a **stateless** compatibility endpoint: it accepts `model`, `input`,
|
|
723
|
+
* `instructions`, `tools`, `stream`, `temperature`, `top_p`,
|
|
724
|
+
* `max_output_tokens`, `reasoning.effort`, `truncation`, and the Ollama
|
|
725
|
+
* extension `think` — and rejects OpenAI's stateful `previous_response_id` /
|
|
726
|
+
* `conversation` fields, because every call is independent. Responses deliver
|
|
727
|
+
* text inside `output[] → message → content[] → output_text` blocks rather
|
|
728
|
+
* than chat-completion `choices`.
|
|
729
|
+
*
|
|
730
|
+
* This module wraps that endpoint with a small, OpenAI-SDK-shaped surface for
|
|
731
|
+
* teams migrating from `openai.responses.create()` — and adds a safety net the
|
|
732
|
+
* raw compat layer doesn't have: **dual-mode transport**.
|
|
733
|
+
*
|
|
734
|
+
* 1. **Native** — POST `/v1/responses` directly. Used when the server
|
|
735
|
+
* supports it (Ollama ≥ v0.13.3, or Ollama Cloud).
|
|
736
|
+
* 2. **Chat adapter** — on `404 Not Found` (older Ollama builds without the
|
|
737
|
+
* endpoint), the same request is transparently re-issued against
|
|
738
|
+
* `/api/chat` with `instructions` mapped to a system message,
|
|
739
|
+
* `max_output_tokens` to `options.num_predict`, and tools mapped to the
|
|
740
|
+
* native tool-definition shape. The response is normalized back into the
|
|
741
|
+
* Responses form (`output_text`, `usage.input_tokens`/`output_tokens`),
|
|
742
|
+
* so migrating code works unchanged against old servers.
|
|
743
|
+
*
|
|
744
|
+
* The `transport` field on the result records which path served the request.
|
|
745
|
+
*
|
|
746
|
+
* For the full OpenAI-shaped surface (stateful fields accepted-but-ignored,
|
|
747
|
+
* raw `output` items, SSE event objects), use `client.openai.responses()`
|
|
748
|
+
* instead — this module is the ergonomic subset.
|
|
749
|
+
*/
|
|
750
|
+
|
|
751
|
+
/** Flat tool definition, matching the `openai` npm SDK's `tools[]` shape. */
|
|
752
|
+
interface ResponsesToolDefinition {
|
|
753
|
+
readonly name: string;
|
|
754
|
+
readonly description?: string | undefined;
|
|
755
|
+
readonly parameters?: ToolParameters | undefined;
|
|
756
|
+
}
|
|
757
|
+
/** Reasoning effort levels Ollama's `/v1/responses` accepts under `reasoning.effort`. */
|
|
758
|
+
type ResponsesReasoningEffort = 'low' | 'medium' | 'high';
|
|
759
|
+
/**
|
|
760
|
+
* Ergonomic request for {@link ResponsesModule.create} — the subset of
|
|
761
|
+
* OpenAI's Responses API that Ollama supports, minus the stateful fields
|
|
762
|
+
* (`previous_response_id`, `conversation`) Ollama rejects.
|
|
763
|
+
*/
|
|
764
|
+
interface ResponsesCreateRequest extends RequestCancellationOptions {
|
|
765
|
+
readonly model: string;
|
|
766
|
+
/** The user prompt. Send the full conversation here — Ollama is stateless. */
|
|
767
|
+
readonly input: string;
|
|
768
|
+
/** System/developer instructions, mapped to a system message. */
|
|
769
|
+
readonly instructions?: string | undefined;
|
|
770
|
+
readonly tools?: readonly ResponsesToolDefinition[] | undefined;
|
|
771
|
+
readonly temperature?: number | undefined;
|
|
772
|
+
readonly top_p?: number | undefined;
|
|
773
|
+
readonly max_output_tokens?: number | undefined;
|
|
774
|
+
/** Maps to `reasoning.effort` on the native endpoint; ignored by the chat adapter. */
|
|
775
|
+
readonly reasoning_effort?: ResponsesReasoningEffort | undefined;
|
|
776
|
+
/** Ollama-specific extension: thinking output control (boolean or model-defined level). */
|
|
777
|
+
readonly think?: ThinkValue | undefined;
|
|
778
|
+
}
|
|
779
|
+
/** Token usage in OpenAI Responses naming. */
|
|
780
|
+
interface ResponsesUsage {
|
|
781
|
+
readonly input_tokens: number;
|
|
782
|
+
readonly output_tokens: number;
|
|
783
|
+
readonly total_tokens: number;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Normalized, non-stateful Responses result — `output_text` is the joined
|
|
787
|
+
* text of all `output_text` content blocks, so consumers never touch the
|
|
788
|
+
* nested `output[]` structure.
|
|
789
|
+
*/
|
|
790
|
+
interface ResponsesCreateResponse {
|
|
791
|
+
readonly id: string;
|
|
792
|
+
readonly model: string;
|
|
793
|
+
readonly created_at: string;
|
|
794
|
+
readonly output_text: string;
|
|
795
|
+
/** Reasoning-model thinking trace, when produced with `think` enabled. */
|
|
796
|
+
readonly thinking?: string | undefined;
|
|
797
|
+
/** Parsed function calls the model requested, when `tools` were provided. */
|
|
798
|
+
readonly tool_calls?: readonly ToolCall[] | undefined;
|
|
799
|
+
readonly usage?: ResponsesUsage | undefined;
|
|
800
|
+
/** Which transport served the request — see the module doc on dual-mode. */
|
|
801
|
+
readonly transport: 'native' | 'chat-adapter';
|
|
802
|
+
}
|
|
803
|
+
/** Simplified streaming events from {@link ResponsesModule.stream}. */
|
|
804
|
+
type ResponsesStreamEvent = {
|
|
805
|
+
readonly type: 'text_delta';
|
|
806
|
+
readonly delta: string;
|
|
807
|
+
} | {
|
|
808
|
+
readonly type: 'thinking_delta';
|
|
809
|
+
readonly delta: string;
|
|
810
|
+
} | {
|
|
811
|
+
readonly type: 'done';
|
|
812
|
+
readonly response: ResponsesCreateResponse;
|
|
813
|
+
};
|
|
814
|
+
/**
|
|
815
|
+
* Dual-mode Responses API bridge. Access via `client.responses` — see the
|
|
816
|
+
* module doc for the transport selection rules.
|
|
817
|
+
*/
|
|
818
|
+
declare class ResponsesModule {
|
|
819
|
+
private readonly runner;
|
|
820
|
+
constructor(runner: RequestRunner);
|
|
821
|
+
/** The request body sent to the native endpoint (also reused for streaming). */
|
|
822
|
+
private nativeBody;
|
|
823
|
+
/**
|
|
824
|
+
* Runs one Responses request, preferring the native `POST /v1/responses`
|
|
825
|
+
* endpoint and transparently re-issuing via `/api/chat` when the server
|
|
826
|
+
* answers `404` (pre-v0.13.3 Ollama without the compatibility endpoint).
|
|
827
|
+
*/
|
|
828
|
+
create(req: ResponsesCreateRequest): Promise<ResponsesCreateResponse>;
|
|
829
|
+
/** Convenience: `create()` returning only the `output_text` string. */
|
|
830
|
+
createText(req: ResponsesCreateRequest): Promise<string>;
|
|
831
|
+
private createViaChatAdapter;
|
|
832
|
+
/**
|
|
833
|
+
* Streams a Responses request as simplified
|
|
834
|
+
* `{type: 'text_delta' | 'thinking_delta' | 'done'}` events. Dual-mode like
|
|
835
|
+
* {@link create}: native SSE from `/v1/responses` when available, otherwise
|
|
836
|
+
* the normalized `/api/chat` token stream. Tool calls are not delta'd —
|
|
837
|
+
* collect them from the final `done` event's `response.tool_calls`.
|
|
838
|
+
*
|
|
839
|
+
* This is a lazy async generator: nothing is sent until the first
|
|
840
|
+
* iteration, matching `for await (... of client.responses.stream(req))`
|
|
841
|
+
* intuition. The generator is also cancellable through `req.signal`.
|
|
842
|
+
*/
|
|
843
|
+
stream(req: ResponsesCreateRequest): AsyncGenerator<ResponsesStreamEvent, void, void>;
|
|
418
844
|
}
|
|
419
845
|
|
|
846
|
+
/**
|
|
847
|
+
* KV-prefix-preserving conversation sessions.
|
|
848
|
+
*
|
|
849
|
+
* Ollama reuses the model's KV cache across turns of a conversation by
|
|
850
|
+
* **prompt prefix matching**: if the new prompt's leading tokens are
|
|
851
|
+
* byte-identical to the previous turn's, the cached prefix is reused and
|
|
852
|
+
* only the new suffix is evaluated — surfaced by the server as
|
|
853
|
+
* `prompt_eval_cached_count`. Anything that mutates the prefix invalidates
|
|
854
|
+
* the whole cache and forces a full re-evaluation (`prompt_eval_count`
|
|
855
|
+
* spikes, latency jumps): changing the system prompt, prepending dynamic
|
|
856
|
+
* timestamps or request IDs to the conversation, or reordering past turns.
|
|
857
|
+
*
|
|
858
|
+
* `ConversationSession` makes the cache-friendly structure the default:
|
|
859
|
+
*
|
|
860
|
+
* - The system prompt (if any) is fixed at construction and always the
|
|
861
|
+
* first message. There is deliberately **no API to mutate it later** —
|
|
862
|
+
* a mutable system prompt is the #1 silent KV-cache killer.
|
|
863
|
+
* - History is append-only. Messages are never reordered, rewritten, or
|
|
864
|
+
* re-serialized between turns — `getMessages()` copies, so external
|
|
865
|
+
* mutation can't leak in either.
|
|
866
|
+
* - Per-turn options (`options`, `think`, `tools`, …) ride on the request,
|
|
867
|
+
* not the history, so changing them never disturbs the prefix.
|
|
868
|
+
*
|
|
869
|
+
* Each turn records what the server reported about the cache, so degrading
|
|
870
|
+
* hit rates (a symptom of prefix churn somewhere upstream, or a context
|
|
871
|
+
* window that no longer holds the whole history) are visible in
|
|
872
|
+
* {@link ConversationSession.cacheStats} instead of hiding in raw responses.
|
|
873
|
+
*/
|
|
874
|
+
|
|
875
|
+
/** What one turn's response reported about KV-cache reuse and evaluation. */
|
|
876
|
+
interface TurnCacheStats {
|
|
877
|
+
/** Prompt tokens served from the KV cache this turn. */
|
|
878
|
+
readonly cachedTokens: number;
|
|
879
|
+
/** Prompt tokens freshly evaluated this turn. */
|
|
880
|
+
readonly evaluatedTokens: number;
|
|
881
|
+
/**
|
|
882
|
+
* Cache hit rate for this turn: `cached / (cached + evaluated)`.
|
|
883
|
+
*
|
|
884
|
+
* Note the denominator adds both counters — when the *entire* prompt hits
|
|
885
|
+
* the cache, Ollama reports `prompt_eval_count: 0`, so dividing by
|
|
886
|
+
* `prompt_eval_count` alone would falsely yield a 0% hit rate on the
|
|
887
|
+
* best-case turn.
|
|
888
|
+
*/
|
|
889
|
+
readonly hitRate: number;
|
|
890
|
+
}
|
|
891
|
+
/** Cumulative cache statistics across every completed turn so far. */
|
|
892
|
+
interface CumulativeCacheStats extends TurnCacheStats {
|
|
893
|
+
/** Number of completed turns (user+assistant pairs). */
|
|
894
|
+
readonly turns: number;
|
|
895
|
+
/** Total prompt tokens across turns, cached + evaluated. */
|
|
896
|
+
readonly totalPromptTokens: number;
|
|
897
|
+
}
|
|
898
|
+
/** Everything one {@link ConversationSession.sendTurn} call knows. */
|
|
899
|
+
interface SessionTurn {
|
|
900
|
+
/** The assistant's text reply (same value `send()` returns). */
|
|
901
|
+
readonly content: string;
|
|
902
|
+
/** What the server reported about KV-cache reuse for this turn. */
|
|
903
|
+
readonly cache: TurnCacheStats;
|
|
904
|
+
/** The full underlying `/api/chat` response (usage, durations, thinking, …). */
|
|
905
|
+
readonly response: ChatResponse;
|
|
906
|
+
}
|
|
907
|
+
/** Per-turn overrides — none of these touch the history prefix, so they are KV-safe. */
|
|
908
|
+
interface ConversationSendOptions extends Pick<ChatRequestOptions, 'signal' | 'timeoutMs'> {
|
|
909
|
+
readonly options?: ModelOptions | undefined;
|
|
910
|
+
readonly think?: ThinkValue | undefined;
|
|
911
|
+
readonly keep_alive?: string | number | undefined;
|
|
912
|
+
readonly tools?: readonly ToolDefinition[] | undefined;
|
|
913
|
+
readonly format?: FormatOption | undefined;
|
|
914
|
+
}
|
|
915
|
+
/** Static configuration for a session, fixed at construction (KV-prefix safety). */
|
|
916
|
+
interface ConversationSessionOptions {
|
|
917
|
+
readonly model: string;
|
|
918
|
+
/**
|
|
919
|
+
* System prompt, pinned as the first history message for the session's
|
|
920
|
+
* whole lifetime. Avoid dynamic content here (timestamps, request IDs) —
|
|
921
|
+
* a per-call system prompt defeats prefix caching entirely.
|
|
922
|
+
*/
|
|
923
|
+
readonly systemPrompt?: string | undefined;
|
|
924
|
+
/** Default `options` (sampler settings, `num_ctx`, …) applied to every turn. */
|
|
925
|
+
readonly options?: ModelOptions | undefined;
|
|
926
|
+
/** Default `think` control applied to every turn. */
|
|
927
|
+
readonly think?: ThinkValue | undefined;
|
|
928
|
+
/** Default `keep_alive` applied to every turn. */
|
|
929
|
+
readonly keep_alive?: string | number | undefined;
|
|
930
|
+
/** Default `tools` applied to every turn. */
|
|
931
|
+
readonly tools?: readonly ToolDefinition[] | undefined;
|
|
932
|
+
/** Default `format` (structured output) applied to every turn. */
|
|
933
|
+
readonly format?: FormatOption | undefined;
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* A multi-turn chat session with an immutable, cache-friendly message
|
|
937
|
+
* history. Create via `client.session(...)` or construct directly:
|
|
938
|
+
*
|
|
939
|
+
* ```ts
|
|
940
|
+
* const session = client.session('llama3.1', 'You are a concise assistant.');
|
|
941
|
+
* await session.send('Hi!'); // turn 1 — cold cache
|
|
942
|
+
* await session.send('Why is the sky blue?'); // turn 2 — prefix reused
|
|
943
|
+
* session.cacheStats; // cumulative hit rate
|
|
944
|
+
* ```
|
|
945
|
+
*/
|
|
946
|
+
declare class ConversationSession {
|
|
947
|
+
private readonly client;
|
|
948
|
+
private readonly config;
|
|
949
|
+
/** Ordered, append-only history. Message objects are frozen on push. */
|
|
950
|
+
private readonly history;
|
|
951
|
+
/** Mutable tallies behind the read-only {@link cacheStats} view. */
|
|
952
|
+
private readonly tallies;
|
|
953
|
+
constructor(client: OllamaClient, config: ConversationSessionOptions);
|
|
954
|
+
/** The model name this session talks to. */
|
|
955
|
+
get model(): string;
|
|
956
|
+
/** Number of completed turns. */
|
|
957
|
+
get turnCount(): number;
|
|
958
|
+
/** Cumulative KV-cache statistics across completed turns. */
|
|
959
|
+
get cacheStats(): CumulativeCacheStats;
|
|
960
|
+
/**
|
|
961
|
+
* Sends the user's message and returns the assistant's reply text —
|
|
962
|
+
* the ergonomic path. Use {@link sendTurn} when you also need the
|
|
963
|
+
* per-turn cache statistics or the raw response.
|
|
964
|
+
*/
|
|
965
|
+
send(content: string, overrides?: ConversationSendOptions): Promise<string>;
|
|
966
|
+
/**
|
|
967
|
+
* Sends the user's message and returns the full turn record: reply text,
|
|
968
|
+
* per-turn KV-cache statistics, and the raw `/api/chat` response.
|
|
969
|
+
*/
|
|
970
|
+
sendTurn(content: string, overrides?: ConversationSendOptions): Promise<SessionTurn>;
|
|
971
|
+
/**
|
|
972
|
+
* A defensive copy of the session history — mutating the returned array or
|
|
973
|
+
* its frozen messages has no effect on the session.
|
|
974
|
+
*/
|
|
975
|
+
getMessages(): readonly Message[];
|
|
976
|
+
/**
|
|
977
|
+
* Clears the conversation back to its initial state (system prompt, zero
|
|
978
|
+
* turns, zeroed cache statistics). The next turn starts with a cold cache
|
|
979
|
+
* by definition — there is nothing upstream of it to reuse.
|
|
980
|
+
*/
|
|
981
|
+
reset(): void;
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/**
|
|
985
|
+
* Batch-constrained embedding generation for high-volume ingestion pipelines.
|
|
986
|
+
*
|
|
987
|
+
* Ollama's `/api/embed` accepts an array of inputs, but two independent limits
|
|
988
|
+
* make naive usage fragile at corpus scale:
|
|
989
|
+
*
|
|
990
|
+
* 1. **Concurrency.** `Promise.all(inputs.map(...))` fires every request at
|
|
991
|
+
* once; against a local Ollama daemon this contends sockets, saturates the
|
|
992
|
+
* request queue (`OLLAMA_MAX_QUEUE`, surfacing as 503 `overloaded`), and —
|
|
993
|
+
* worst case — exhausts GPU VRAM as the model instance serves N prompts
|
|
994
|
+
* simultaneously.
|
|
995
|
+
* 2. **Context window.** Ollama truncates any input string longer than the
|
|
996
|
+
* model's context window (`num_ctx`) *silently* unless `truncate: false` is
|
|
997
|
+
* set — degraded vectors with no error, no warning, nothing on the wire.
|
|
998
|
+
*
|
|
999
|
+
* {@link batchEmbed} addresses the first with a fixed-size worker pool over
|
|
1000
|
+
* `client.embed()` (so every batch still rides the client's full pipeline:
|
|
1001
|
+
* failover, retry, auth, logging, telemetry). The client-level
|
|
1002
|
+
* `embedBatch()` wrapper addresses the second with a pre-flight per-string
|
|
1003
|
+
* token estimate against the resolved `num_ctx` window (see
|
|
1004
|
+
* {@link findOversizedEmbedInputs}).
|
|
1005
|
+
*
|
|
1006
|
+
* Semantics:
|
|
1007
|
+
* - **Order-preserving** — result index `i` is always the embedding of
|
|
1008
|
+
* `input[i]`, regardless of which batch completed first.
|
|
1009
|
+
* - **Fail-fast** — the first batch error aborts every sibling in-flight
|
|
1010
|
+
* batch (surfaces the *original* error, not the derived aborts) and rejects
|
|
1011
|
+
* the whole operation. Vector indexing that half-succeeds is a corrupted
|
|
1012
|
+
* index; callers re-running an ingestion prefer all-or-nothing.
|
|
1013
|
+
* - **Abortable** — the caller's `signal` cancels queued and in-flight batches
|
|
1014
|
+
* alike and rejects with `code: 'aborted'`.
|
|
1015
|
+
*/
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* Default number of input strings per `/api/embed` request. 32 keeps a single
|
|
1019
|
+
* request's prompt comfortably inside typical embedding-model context windows
|
|
1020
|
+
* (2048+) while amortizing HTTP overhead across the corpus.
|
|
1021
|
+
*/
|
|
1022
|
+
declare const DEFAULT_EMBED_BATCH_SIZE = 32;
|
|
1023
|
+
/**
|
|
1024
|
+
* Default number of batches kept in flight concurrently. 3 is deliberately
|
|
1025
|
+
* conservative: a local Ollama daemon serves one model instance per GPU, so
|
|
1026
|
+
* 3 concurrent embedding prompts bound queue depth and VRAM pressure without
|
|
1027
|
+
* leaving the hardware idle between batches. Raise for multi-endpoint
|
|
1028
|
+
* clients or remote daemons; lower (to 1) for constrained devices.
|
|
1029
|
+
*/
|
|
1030
|
+
declare const DEFAULT_EMBED_CONCURRENCY = 3;
|
|
1031
|
+
interface EmbedBatchOptions {
|
|
1032
|
+
/** Model to embed with (e.g. `'nomic-embed-text:latest'`). */
|
|
1033
|
+
readonly model: string;
|
|
1034
|
+
/** Input strings, in corpus order. Result index `i` matches `input[i]`. */
|
|
1035
|
+
readonly input: readonly string[];
|
|
1036
|
+
/**
|
|
1037
|
+
* Inputs per `/api/embed` request. Default {@link DEFAULT_EMBED_BATCH_SIZE}
|
|
1038
|
+
* (32). Must be a positive integer.
|
|
1039
|
+
*/
|
|
1040
|
+
readonly batchSize?: number | undefined;
|
|
1041
|
+
/**
|
|
1042
|
+
* Batches kept in flight simultaneously. Default
|
|
1043
|
+
* {@link DEFAULT_EMBED_CONCURRENCY} (3). Must be a positive integer.
|
|
1044
|
+
*/
|
|
1045
|
+
readonly concurrency?: number | undefined;
|
|
1046
|
+
/** Passed through to each batch request — `false` makes oversized inputs
|
|
1047
|
+
* a server-side error instead of a silent truncation. */
|
|
1048
|
+
readonly truncate?: boolean | undefined;
|
|
1049
|
+
/** Passed through to each batch request (dimensionality truncation). */
|
|
1050
|
+
readonly dimensions?: number | undefined;
|
|
1051
|
+
/** Passed through to each batch request (model unload policy). */
|
|
1052
|
+
readonly keep_alive?: string | number | undefined;
|
|
1053
|
+
/** Ollama model options (`num_ctx`, …) passed through to each batch request. */
|
|
1054
|
+
readonly options?: ModelOptions | undefined;
|
|
1055
|
+
/** Aborts the whole batch operation — queued and in-flight alike. */
|
|
1056
|
+
readonly signal?: AbortSignal | undefined;
|
|
1057
|
+
/** Per-request timeout applied to each individual batch request. */
|
|
1058
|
+
readonly timeoutMs?: number | undefined;
|
|
1059
|
+
/**
|
|
1060
|
+
* Progress callback invoked after each successfully embedded batch as
|
|
1061
|
+
* `onBatchComplete(completedBatches, totalBatches)`. Never called after the
|
|
1062
|
+
* operation rejects.
|
|
1063
|
+
*/
|
|
1064
|
+
readonly onBatchComplete?: ((completedBatches: number, totalBatches: number) => void) | undefined;
|
|
1065
|
+
}
|
|
1066
|
+
interface EmbedBatchResult {
|
|
1067
|
+
readonly model: string;
|
|
1068
|
+
/**
|
|
1069
|
+
* Embeddings in input order — `embeddings[i]` corresponds to
|
|
1070
|
+
* `options.input[i]`, independent of batch completion order.
|
|
1071
|
+
*/
|
|
1072
|
+
readonly embeddings: readonly (readonly number[])[];
|
|
1073
|
+
/** Number of `/api/embed` requests the corpus was split into. */
|
|
1074
|
+
readonly batchCount: number;
|
|
1075
|
+
}
|
|
1076
|
+
/** An input string whose estimated token count exceeds the context window. */
|
|
1077
|
+
interface OversizedEmbedInput {
|
|
1078
|
+
/** Index into the input array. */
|
|
1079
|
+
readonly index: number;
|
|
1080
|
+
/** Estimated token count for `input[index]`. */
|
|
1081
|
+
readonly estimate: number;
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Finds input strings whose heuristic token estimate exceeds `contextLength`
|
|
1085
|
+
* (typically the resolved `num_ctx`). Unlike chat/generate pre-flight there is
|
|
1086
|
+
* no warning margin and no output-token headroom: an embedding request's
|
|
1087
|
+
* prompt IS the whole input, so the exact window is the trip point, and
|
|
1088
|
+
* crossing it means silent truncation (or, with `truncate: false`, a
|
|
1089
|
+
* server-side rejection).
|
|
1090
|
+
*/
|
|
1091
|
+
declare function findOversizedEmbedInputs(input: readonly string[], contextLength: number): OversizedEmbedInput[];
|
|
1092
|
+
/**
|
|
1093
|
+
* Builds the warn/throw message for oversized embed inputs (mirrors
|
|
1094
|
+
* `contextWarningMessage` for chat/generate).
|
|
1095
|
+
*/
|
|
1096
|
+
declare function embedBatchOverflowMessage(oversized: readonly OversizedEmbedInput[], totalInputs: number, contextLength: number, truncateFalse: boolean): string;
|
|
1097
|
+
/**
|
|
1098
|
+
* Splits `input` into `batchSize` slices and embeds them through `client.embed()`
|
|
1099
|
+
* with at most `concurrency` batches in flight. Order-preserving and fail-fast —
|
|
1100
|
+
* see the module doc for the full semantics.
|
|
1101
|
+
*
|
|
1102
|
+
* Prefer the {@link OllamaClient.embedBatch} wrapper, which additionally
|
|
1103
|
+
* injects `defaultContextLength` as `num_ctx` and enforces the client's
|
|
1104
|
+
* `onContextOverflow` policy against per-string estimates before any request
|
|
1105
|
+
* is sent.
|
|
1106
|
+
*/
|
|
1107
|
+
declare function batchEmbed(client: OllamaClient, options: EmbedBatchOptions): Promise<EmbedBatchResult>;
|
|
1108
|
+
|
|
420
1109
|
/**
|
|
421
1110
|
* Ergonomic System One types — key-safe generics on top of the generated
|
|
422
1111
|
* contract models.
|
|
@@ -453,6 +1142,28 @@ declare class ModelsClient {
|
|
|
453
1142
|
* Wave 13.
|
|
454
1143
|
*/
|
|
455
1144
|
|
|
1145
|
+
/**
|
|
1146
|
+
* Maximum total request size, in bytes, accepted by `/v1/systemone`
|
|
1147
|
+
* when the request body carries a non-empty `images` array.
|
|
1148
|
+
*
|
|
1149
|
+
* The Ollama server enforces this limit server-side and rejects
|
|
1150
|
+
* oversized payloads with HTTP 413. The SDK's runtime also enforces
|
|
1151
|
+
* it client-side (see `OllamaRuntime`'s `maxRequestBytesWithImages`
|
|
1152
|
+
* handling) so the round-trip is avoided.
|
|
1153
|
+
*
|
|
1154
|
+
* Without images, the limit is the much smaller
|
|
1155
|
+
* {@link MAX_SYSTEM_ONE_REQUEST_BYTES} (64 KiB) — the bumped
|
|
1156
|
+
* images-cap exists specifically to accommodate base64-encoded image
|
|
1157
|
+
* bytes shared across all questions in a single System One call.
|
|
1158
|
+
*/
|
|
1159
|
+
declare const MAX_SYSTEM_ONE_IMAGES_BYTES: number;
|
|
1160
|
+
/**
|
|
1161
|
+
* Maximum total request size, in bytes, for `/v1/systemone` when no
|
|
1162
|
+
* images are attached. The Ollama server caps the JSON-only payload
|
|
1163
|
+
* at 64 KiB; the runtime enforces this client-side (see
|
|
1164
|
+
* `OllamaRuntime`'s `maxRequestBytes`).
|
|
1165
|
+
*/
|
|
1166
|
+
declare const MAX_SYSTEM_ONE_REQUEST_BYTES: number;
|
|
456
1167
|
/**
|
|
457
1168
|
* Discriminated union of the three question kinds. The upstream OpenAPI
|
|
458
1169
|
* defines this inline as `oneOf` within SystemOneRequest.questions; we
|
|
@@ -513,6 +1224,23 @@ type SystemOneAnswers<Q extends SystemOneQuestions = SystemOneQuestions> = {
|
|
|
513
1224
|
*
|
|
514
1225
|
* The `model`, `state`, `images`, and `keep_alive` fields are inherited
|
|
515
1226
|
* from the generated base type unchanged.
|
|
1227
|
+
*
|
|
1228
|
+
* ## Image support
|
|
1229
|
+
*
|
|
1230
|
+
* The `images` field accepts up to N base64-encoded images shared by
|
|
1231
|
+
* all questions in the request. Total request payload (JSON + images)
|
|
1232
|
+
* must remain within {@link MAX_SYSTEM_ONE_IMAGES_BYTES} (32 MiB);
|
|
1233
|
+
* requests without images are capped at
|
|
1234
|
+
* {@link MAX_SYSTEM_ONE_REQUEST_BYTES} (64 KiB).
|
|
1235
|
+
*
|
|
1236
|
+
* Use {@link estimateSystemOneRequestBytes} for a client-side pre-flight
|
|
1237
|
+
* check before sending a large multi-image batch — the runtime also
|
|
1238
|
+
* enforces the limit, but the early check avoids the round-trip when
|
|
1239
|
+
* the caller already knows the payload is too large.
|
|
1240
|
+
*
|
|
1241
|
+
* Requires Clef or Clef Flash with vision weights. URLs and data URLs
|
|
1242
|
+
* are NOT supported — pre-encode the bytes to base64 before adding to
|
|
1243
|
+
* the array.
|
|
516
1244
|
*/
|
|
517
1245
|
interface SystemOneRequest<Q extends SystemOneQuestions = SystemOneQuestions> extends Omit<SystemOneRequest$1, 'questions'> {
|
|
518
1246
|
readonly questions: Q;
|
|
@@ -526,6 +1254,27 @@ interface SystemOneResponse<Q extends SystemOneQuestions = SystemOneQuestions> e
|
|
|
526
1254
|
readonly answers: SystemOneAnswers<Q>;
|
|
527
1255
|
readonly usage: SystemOneUsage;
|
|
528
1256
|
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Estimate the on-the-wire byte size of a System One request body, for
|
|
1259
|
+
* pre-flight size validation against {@link MAX_SYSTEM_ONE_REQUEST_BYTES}
|
|
1260
|
+
* (no images) or {@link MAX_SYSTEM_ONE_IMAGES_BYTES} (with images).
|
|
1261
|
+
*
|
|
1262
|
+
* The estimate is conservative: it uses `JSON.stringify` length and
|
|
1263
|
+
* assumes UTF-8 encoding. The actual wire size may differ slightly due
|
|
1264
|
+
* to server-side JSON normalization (key ordering, whitespace), but the
|
|
1265
|
+
* estimate is always within a few percent of the real payload size and
|
|
1266
|
+
* is suitable for "fail fast before sending" checks.
|
|
1267
|
+
*
|
|
1268
|
+
* @example
|
|
1269
|
+
* ```ts
|
|
1270
|
+
* const request: SystemOneRequest = { ... };
|
|
1271
|
+
* const bytes = estimateSystemOneRequestBytes(request);
|
|
1272
|
+
* const cap = request.images?.length ? MAX_SYSTEM_ONE_IMAGES_BYTES : MAX_SYSTEM_ONE_REQUEST_BYTES;
|
|
1273
|
+
* if (bytes > cap) throw new Error(`payload ${bytes}B exceeds cap ${cap}B`);
|
|
1274
|
+
* await client.systemOne(request);
|
|
1275
|
+
* ```
|
|
1276
|
+
*/
|
|
1277
|
+
declare function estimateSystemOneRequestBytes(request: SystemOneRequest | SystemOneRequest$1): number;
|
|
529
1278
|
|
|
530
1279
|
/**
|
|
531
1280
|
* Higher-level System One decision helpers.
|
|
@@ -795,6 +1544,20 @@ declare class OllamaClient {
|
|
|
795
1544
|
* `OLLAMA_API_KEY`), independent of whether `config.endpoints` was used instead.
|
|
796
1545
|
*/
|
|
797
1546
|
private readonly cloudApiKey;
|
|
1547
|
+
/** Configured `defaultContextLength` — injected as `num_ctx` when a request omits it. */
|
|
1548
|
+
private readonly defaultContextLength;
|
|
1549
|
+
/** Configured `contextWarningThreshold` (default 0.9). */
|
|
1550
|
+
private readonly contextWarningThreshold;
|
|
1551
|
+
/** Configured overflow policy — `'warn'` (default) or `'throw'`. */
|
|
1552
|
+
private readonly onContextOverflow;
|
|
1553
|
+
/**
|
|
1554
|
+
* Controllers for every in-flight request and active stream managed by this
|
|
1555
|
+
* client — see {@link OllamaClient.destroy} and {@link createRequestScope}.
|
|
1556
|
+
* Entries live for exactly as long as the underlying work does: until the
|
|
1557
|
+
* response settles (requests) or the stream's `finalResult` settles
|
|
1558
|
+
* (streams, via `executeWithFailover`'s `holdUntil` release path).
|
|
1559
|
+
*/
|
|
1560
|
+
private readonly activeRequests;
|
|
798
1561
|
constructor(config?: OllamaClientConfig);
|
|
799
1562
|
get modelsClient(): ModelsClient;
|
|
800
1563
|
/**
|
|
@@ -851,6 +1614,59 @@ declare class OllamaClient {
|
|
|
851
1614
|
*/
|
|
852
1615
|
get decision(): Decision;
|
|
853
1616
|
private _decision;
|
|
1617
|
+
/**
|
|
1618
|
+
* Ergonomic OpenAI Responses API bridge (`client.responses.create()` /
|
|
1619
|
+
* `.stream()`). Dual-mode transport: prefers the native `POST
|
|
1620
|
+
* /v1/responses` endpoint (Ollama ≥ v0.13.3) and transparently re-issues via
|
|
1621
|
+
* `/api/chat` when the server answers 404, so migrating OpenAI code works
|
|
1622
|
+
* unchanged against older servers. See `src/responses.ts`.
|
|
1623
|
+
*/
|
|
1624
|
+
get responses(): ResponsesModule;
|
|
1625
|
+
private _responses;
|
|
1626
|
+
/**
|
|
1627
|
+
* Starts a KV-prefix-preserving multi-turn conversation session.
|
|
1628
|
+
*
|
|
1629
|
+
* ```ts
|
|
1630
|
+
* const session = client.session('llama3.1', 'You are a concise assistant.');
|
|
1631
|
+
* const reply = await session.send('Hi!');
|
|
1632
|
+
* const detail = await session.sendTurn('Why is the sky blue?');
|
|
1633
|
+
* detail.cache.hitRate; // per-turn KV-cache hit rate
|
|
1634
|
+
* session.cacheStats; // cumulative stats across turns
|
|
1635
|
+
* ```
|
|
1636
|
+
*
|
|
1637
|
+
* Accepts either `(model, systemPrompt?)` or a full options object
|
|
1638
|
+
* (`{ model, systemPrompt, options, think, keep_alive, tools }`). See
|
|
1639
|
+
* `src/conversation.ts` for why the system prompt is pinned for the
|
|
1640
|
+
* session's lifetime.
|
|
1641
|
+
*/
|
|
1642
|
+
session(model: string, systemPrompt?: string): ConversationSession;
|
|
1643
|
+
session(options: ConversationSessionOptions): ConversationSession;
|
|
1644
|
+
/**
|
|
1645
|
+
* Context-window pre-flight enforcement — see `src/context-safety.ts` and
|
|
1646
|
+
* the `defaultContextLength` / `onContextOverflow` config docs. Logs a
|
|
1647
|
+
* warning (default) or throws client-side when the estimated prompt size
|
|
1648
|
+
* is close to / beyond the effective context window.
|
|
1649
|
+
*/
|
|
1650
|
+
private enforceContextSafety;
|
|
1651
|
+
/**
|
|
1652
|
+
* Returns `options` with `num_ctx` injected from the client's
|
|
1653
|
+
* `defaultContextLength` when the request didn't set one — making the
|
|
1654
|
+
* effective context window explicit on the wire instead of relying on
|
|
1655
|
+
* Ollama's silently-truncating server default. Returns the original
|
|
1656
|
+
* reference when there's nothing to inject (the common case).
|
|
1657
|
+
*/
|
|
1658
|
+
private injectDefaultContextLength;
|
|
1659
|
+
/**
|
|
1660
|
+
* Creates the per-request abort scope registered with {@link destroy}:
|
|
1661
|
+
* a controller that fires when the caller's `signal` aborts OR when
|
|
1662
|
+
* `destroy()` runs, whichever comes first. `dispose()` removes the
|
|
1663
|
+
* registration when the work it guards settles — for plain requests when
|
|
1664
|
+
* the response settles, for streams when `finalResult` settles (the same
|
|
1665
|
+
* moment `executeWithFailover`'s `holdUntil` releases the endpoint slot),
|
|
1666
|
+
* so a destroy() can tear down a stream mid-consumption but never outlives
|
|
1667
|
+
* one that already finished.
|
|
1668
|
+
*/
|
|
1669
|
+
private createRequestScope;
|
|
854
1670
|
/**
|
|
855
1671
|
* Fail-fast guard for `format` (structured output) requests: throws before any network
|
|
856
1672
|
* call if the candidate endpoint is inferred as Ollama Cloud, which does not currently
|
|
@@ -913,6 +1729,36 @@ declare class OllamaClient {
|
|
|
913
1729
|
generateWithSchema<T>(req: Omit<GenerateRequestOptions, 'stream' | 'format'>, schema: z.ZodType<T>): Promise<T>;
|
|
914
1730
|
embed(req: EmbedRequestOptions): Promise<EmbedResponse>;
|
|
915
1731
|
embedText(model: string, input: string | readonly string[]): Promise<readonly (readonly number[])[]>;
|
|
1732
|
+
/**
|
|
1733
|
+
* Batch-constrained, order-preserving embedding generation for large
|
|
1734
|
+
* corpora — the safe building block for RAG/vector-index ingestion at
|
|
1735
|
+
* scale. Splits `input` into `batchSize` slices and embeds them through
|
|
1736
|
+
* {@link embed} with at most `concurrency` batches in flight (defaults:
|
|
1737
|
+
* 32 × 3), so `Promise.all`-style floods that OOM local daemons and
|
|
1738
|
+
* saturate `OLLAMA_MAX_QUEUE` can't happen by accident. Fail-fast: the
|
|
1739
|
+
* first batch error aborts every sibling in-flight batch and rejects the
|
|
1740
|
+
* whole operation with the original error.
|
|
1741
|
+
*
|
|
1742
|
+
* Before any request is sent, `defaultContextLength` is injected as
|
|
1743
|
+
* `num_ctx` (same as chat/generate) and each input string's token estimate
|
|
1744
|
+
* is checked against the resolved window: Ollama silently truncates
|
|
1745
|
+
* oversized embedding inputs by default, so the client warns (or throws,
|
|
1746
|
+
* under `onContextOverflow: 'throw'`) with the offending indexes — see
|
|
1747
|
+
* `src/embed-batch.ts` for the exact trip point (no margin: an embedding
|
|
1748
|
+
* prompt IS the whole input).
|
|
1749
|
+
*
|
|
1750
|
+
* ```ts
|
|
1751
|
+
* const { embeddings, batchCount } = await client.embedBatch({
|
|
1752
|
+
* model: 'nomic-embed-text:latest',
|
|
1753
|
+
* input: corpus, // e.g. 50k chunks
|
|
1754
|
+
* batchSize: 32, // inputs per /api/embed request
|
|
1755
|
+
* concurrency: 3, // batches in flight
|
|
1756
|
+
* onBatchComplete: (done, total) => progress.log(`${done}/${total}`),
|
|
1757
|
+
* });
|
|
1758
|
+
* // embeddings[i] always corresponds to corpus[i]
|
|
1759
|
+
* ```
|
|
1760
|
+
*/
|
|
1761
|
+
embedBatch(options: EmbedBatchOptions): Promise<EmbedBatchResult>;
|
|
916
1762
|
/**
|
|
917
1763
|
* @deprecated Ollama's `/api/embeddings` endpoint has been superseded by `/api/embed`
|
|
918
1764
|
* (exposed here as {@link OllamaClient.embed}), which additionally supports batch
|
|
@@ -930,6 +1776,18 @@ declare class OllamaClient {
|
|
|
930
1776
|
readonly version: () => Promise<VersionResponse>;
|
|
931
1777
|
readonly createBlob: (digest: string, data: BinaryBody) => Promise<void>;
|
|
932
1778
|
readonly checkBlob: (digest: string) => Promise<boolean>;
|
|
1779
|
+
/**
|
|
1780
|
+
* Immediately unloads `model` from VRAM. Convenience alias for
|
|
1781
|
+
* {@link ModelsClient.unload} — see that method for the full
|
|
1782
|
+
* lifecycle contract.
|
|
1783
|
+
*/
|
|
1784
|
+
readonly unloadModel: (model: string) => Promise<void>;
|
|
1785
|
+
/**
|
|
1786
|
+
* Pre-loads and pins `model` into VRAM indefinitely. Convenience
|
|
1787
|
+
* alias for {@link ModelsClient.pin} — see that method for the
|
|
1788
|
+
* full lifecycle contract.
|
|
1789
|
+
*/
|
|
1790
|
+
readonly pinModel: (model: string) => Promise<void>;
|
|
933
1791
|
/**
|
|
934
1792
|
* Ollama's System One decision layer (`POST /v1/systemone`) — evaluates
|
|
935
1793
|
* 1–64 typed questions (choice, noul, or score) against a provided state
|
|
@@ -977,6 +1835,45 @@ declare class OllamaClient {
|
|
|
977
1835
|
* which applies identically here.
|
|
978
1836
|
*/
|
|
979
1837
|
webFetch(req: WebFetchRequestOptions): Promise<WebFetchResponse>;
|
|
1838
|
+
/**
|
|
1839
|
+
* Ollama Cloud usage statistics (`GET https://ollama.com/api/usage`) — request counts,
|
|
1840
|
+
* USD spend, and token totals (including cached input tokens) for cloud inference,
|
|
1841
|
+
* web search, and web fetch, bucketed by hour (`range: '24h'`) or day (`'7d'`/`'30d'`).
|
|
1842
|
+
*
|
|
1843
|
+
* See {@link OllamaClient.webSearch} for the cloud-endpoint/auth/timeout/retry
|
|
1844
|
+
* behavior, which applies identically here. Query parameters mirror the official
|
|
1845
|
+
* endpoint: `range` (`'24h' | '7d' | '30d'`, server default `'7d'`) and `scope`
|
|
1846
|
+
* (`'self' | 'team'`, server default `'self'`; team scope requires a team admin).
|
|
1847
|
+
* Omitted options are left unset so the server applies its own defaults.
|
|
1848
|
+
*
|
|
1849
|
+
* ```ts
|
|
1850
|
+
* const usage = await client.usage({ range: '24h' });
|
|
1851
|
+
* console.log(usage.totals.request_count, usage.totals.usage_usd);
|
|
1852
|
+
* for (const bucket of usage.buckets) {
|
|
1853
|
+
* if (bucket.partial) continue; // current hour, still in progress
|
|
1854
|
+
* // ...
|
|
1855
|
+
* }
|
|
1856
|
+
* ```
|
|
1857
|
+
*/
|
|
1858
|
+
usage(req?: UsageRequestOptions): Promise<UsageResponse>;
|
|
1859
|
+
/**
|
|
1860
|
+
* Ollama Cloud balance (`GET https://ollama.com/api/balance`) — remaining included
|
|
1861
|
+
* and purchased usage credits. The included balance is either a plan-period credit
|
|
1862
|
+
* object (`balance_usd`/`allowance_usd`/`period`) or, on legacy plans, session/weekly
|
|
1863
|
+
* percentage limits; the shape is discriminated at runtime by the server response.
|
|
1864
|
+
* See {@link OllamaClient.webSearch} for the cloud-endpoint/auth/timeout/retry
|
|
1865
|
+
* behavior, which applies identically here.
|
|
1866
|
+
*
|
|
1867
|
+
* ```ts
|
|
1868
|
+
* const balance = await client.balance();
|
|
1869
|
+
* if ('balance_usd' in balance.included) {
|
|
1870
|
+
* console.log(balance.included.balance_usd, balance.purchased.balance_usd);
|
|
1871
|
+
* } else {
|
|
1872
|
+
* console.log(balance.included.session.remaining_percent, '% of session limit left');
|
|
1873
|
+
* }
|
|
1874
|
+
* ```
|
|
1875
|
+
*/
|
|
1876
|
+
balance(req?: BalanceRequestOptions): Promise<BalanceResponse>;
|
|
980
1877
|
/**
|
|
981
1878
|
* Runs `operation` against the fixed Ollama Cloud host with this client's default
|
|
982
1879
|
* timeout and retry policy — the same primitives `executeWithFailover` uses, minus the
|
|
@@ -987,6 +1884,26 @@ declare class OllamaClient {
|
|
|
987
1884
|
runtimeMode(): RuntimeMode;
|
|
988
1885
|
healthCheck(): Promise<EndpointHealthCheckResult[]>;
|
|
989
1886
|
endpointStatus(): EndpointHealth[];
|
|
1887
|
+
/**
|
|
1888
|
+
* Immediately aborts every in-flight request and active stream managed by
|
|
1889
|
+
* this client — the clean teardown path for short-lived processes, CLI
|
|
1890
|
+
* runners, and `node:worker_threads` tasks where dangling fetch bodies,
|
|
1891
|
+
* unconsumed stream readers, or queued capacity waiters would otherwise
|
|
1892
|
+
* keep the event loop (and thus the thread/process) alive.
|
|
1893
|
+
*
|
|
1894
|
+
* Every aborted operation rejects with an {@link OllamaAbortError}
|
|
1895
|
+
* (`code: 'aborted'`) carrying `reason` as its message — including requests
|
|
1896
|
+
* still queued behind `maxConcurrentPerEndpoint`, not just dispatched ones.
|
|
1897
|
+
* Streams reject their `finalResult` and release their endpoint slots.
|
|
1898
|
+
*
|
|
1899
|
+
* Idempotent: calling it again (or on a client with nothing in flight)
|
|
1900
|
+
* is a no-op that returns `0`. The client remains usable afterward —
|
|
1901
|
+
* `destroy()` is a drain, not a permanent disable; callers that want a
|
|
1902
|
+
* permanently-dead client should discard the instance.
|
|
1903
|
+
*
|
|
1904
|
+
* @returns the number of in-flight operations that were aborted.
|
|
1905
|
+
*/
|
|
1906
|
+
destroy(reason?: string): number;
|
|
990
1907
|
get openai(): OpenAICompatClient;
|
|
991
1908
|
get anthropic(): AnthropicCompatClient;
|
|
992
1909
|
}
|
|
@@ -1003,6 +1920,269 @@ interface TimeoutSignal {
|
|
|
1003
1920
|
*/
|
|
1004
1921
|
declare function createTimeoutSignal(timeoutMs?: number, userSignal?: AbortSignal): TimeoutSignal;
|
|
1005
1922
|
|
|
1923
|
+
/**
|
|
1924
|
+
* Standalone fetch-with-backoff helper.
|
|
1925
|
+
*
|
|
1926
|
+
* Ollama's server caps in-flight work via `OLLAMA_MAX_QUEUE` (default
|
|
1927
|
+
* 512); when the queue saturates, excess requests are rejected with
|
|
1928
|
+
* HTTP **503 Service Unavailable**. The same retry discipline applies
|
|
1929
|
+
* to **429 Too Many Requests** for rate-limited cloud endpoints.
|
|
1930
|
+
*
|
|
1931
|
+
* Callers running parallel agentic swarms (multi-turn tool loops,
|
|
1932
|
+
* fan-out embed batches, speculative-decoding pipelines) can't always
|
|
1933
|
+
* predict saturation ahead of time, so the SDK exposes this helper —
|
|
1934
|
+
* a thin wrapper around `fetch` that retries 503/429 responses with
|
|
1935
|
+
* the same jittered exponential backoff used by the SDK's internal
|
|
1936
|
+
* transport (see `./backoff.ts` and `./retry.ts`).
|
|
1937
|
+
*
|
|
1938
|
+
* The helper is intentionally standalone: it works with any
|
|
1939
|
+
* `fetch`-shaped function (browser `globalThis.fetch`, `undici`, an
|
|
1940
|
+
* Edge runtime polyfill, or a `vi.fn()` mock) and does not depend on
|
|
1941
|
+
* the wider `OllamaClient` / `HttpClient` machinery. Use it when you
|
|
1942
|
+
* want the SDK's retry ergonomics on raw HTTP traffic that doesn't go
|
|
1943
|
+
* through `OllamaClient` — for example, when calling the OpenAI / Anthropic
|
|
1944
|
+
* compatibility bridges directly, or when talking to a sibling Ollama
|
|
1945
|
+
* instance outside the configured endpoint registry.
|
|
1946
|
+
*
|
|
1947
|
+
* For SDK-routed traffic (`client.chat`, `client.generate`, …),
|
|
1948
|
+
* retry/backoff is already applied automatically by the transport
|
|
1949
|
+
* layer; you do NOT need to wrap those calls in `fetchWithBackoff`.
|
|
1950
|
+
*/
|
|
1951
|
+
|
|
1952
|
+
/**
|
|
1953
|
+
* Configuration for {@link fetchWithBackoff}.
|
|
1954
|
+
*
|
|
1955
|
+
* All fields optional; sensible defaults are provided.
|
|
1956
|
+
*/
|
|
1957
|
+
interface FetchWithBackoffConfig {
|
|
1958
|
+
/**
|
|
1959
|
+
* Maximum number of retry attempts after the initial request.
|
|
1960
|
+
* Defaults to `3` (i.e. up to 4 total HTTP requests).
|
|
1961
|
+
*/
|
|
1962
|
+
readonly maxRetries?: number | undefined;
|
|
1963
|
+
/**
|
|
1964
|
+
* Backoff tuning. Defaults to {@link DEFAULT_BACKOFF}
|
|
1965
|
+
* (`initialDelayMs: 500`, `maxDelayMs: 30_000`, `backoffFactor: 2`).
|
|
1966
|
+
*/
|
|
1967
|
+
readonly backoff?: BackoffOptions | undefined;
|
|
1968
|
+
/**
|
|
1969
|
+
* Optional callback fired before each retry sleep. Useful for
|
|
1970
|
+
* logging, OpenTelemetry span events, or surfacing the
|
|
1971
|
+
* backoff in observability dashboards.
|
|
1972
|
+
*
|
|
1973
|
+
* Receives the attempt index (0-based for the original request,
|
|
1974
|
+
* 1+ for retries), the HTTP status that triggered the retry
|
|
1975
|
+
* (or `0` for network-level failures), and the calculated delay
|
|
1976
|
+
* in milliseconds (already jittered).
|
|
1977
|
+
*/
|
|
1978
|
+
readonly onRetry?: ((attempt: number, status: number, delayMs: number) => void) | undefined;
|
|
1979
|
+
/**
|
|
1980
|
+
* Optional `AbortSignal` forwarded to the underlying `fetch`.
|
|
1981
|
+
* Aborting mid-backoff cancels the sleep and rejects with an
|
|
1982
|
+
* `AbortError`-shaped error (the same shape `fetch` itself
|
|
1983
|
+
* rejects with on abort).
|
|
1984
|
+
*/
|
|
1985
|
+
readonly signal?: AbortSignal | undefined;
|
|
1986
|
+
/**
|
|
1987
|
+
* Optional `fetch` implementation to use. Defaults to
|
|
1988
|
+
* `globalThis.fetch`. Pass an explicit implementation when
|
|
1989
|
+
* running in environments without a global `fetch` (legacy
|
|
1990
|
+
* Node.js, custom sandboxes), or to inject a mock for testing.
|
|
1991
|
+
*/
|
|
1992
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* HTTP statuses that trigger a retry by default. Both indicate
|
|
1996
|
+
* transient, server-side saturation — the queue is full and the
|
|
1997
|
+
* request is rejected without consuming inference resources.
|
|
1998
|
+
*
|
|
1999
|
+
* - `503 Service Unavailable` — Ollama queue saturated
|
|
2000
|
+
* (`OLLAMA_MAX_QUEUE` exceeded).
|
|
2001
|
+
* - `429 Too Many Requests` — rate limit hit (typically Ollama
|
|
2002
|
+
* Cloud endpoints).
|
|
2003
|
+
*/
|
|
2004
|
+
declare const RETRYABLE_STATUS_CODES: ReadonlySet<number>;
|
|
2005
|
+
/**
|
|
2006
|
+
* Default retry configuration used when {@link FetchWithBackoffConfig}
|
|
2007
|
+
* is omitted entirely. Matches the SDK's internal transport defaults
|
|
2008
|
+
* so consumers see identical retry behavior whether they go through
|
|
2009
|
+
* `OllamaClient` or call `fetchWithBackoff` directly.
|
|
2010
|
+
*/
|
|
2011
|
+
declare const DEFAULT_FETCH_BACKOFF_CONFIG: {
|
|
2012
|
+
readonly maxRetries: 3;
|
|
2013
|
+
};
|
|
2014
|
+
/**
|
|
2015
|
+
* Wrapper around any `fetch`-shaped function that retries HTTP 429 / 503
|
|
2016
|
+
* responses with jittered exponential backoff (full-jitter strategy —
|
|
2017
|
+
* see `./backoff.ts` for the rationale and AWS Architecture Blog
|
|
2018
|
+
* citation).
|
|
2019
|
+
*
|
|
2020
|
+
* The wrapper:
|
|
2021
|
+
* - forwards `input`/`init` unchanged to every attempt,
|
|
2022
|
+
* - sleeps before each retry using the configured backoff,
|
|
2023
|
+
* - retries up to `maxRetries` times (default 3), then returns the
|
|
2024
|
+
* final `Response` (whatever its status) so the caller can
|
|
2025
|
+
* branch on it,
|
|
2026
|
+
* - rethrows network errors (TypeError under WHATWG fetch) only
|
|
2027
|
+
* after exhausting retries — intermediate attempts also sleep,
|
|
2028
|
+
* - honors `AbortSignal` for both the underlying `fetch` and the
|
|
2029
|
+
* sleep between retries.
|
|
2030
|
+
*
|
|
2031
|
+
* @example
|
|
2032
|
+
* ```ts
|
|
2033
|
+
* const res = await fetchWithBackoff(
|
|
2034
|
+
* 'http://localhost:11434/api/chat',
|
|
2035
|
+
* { method: 'POST', body: JSON.stringify(payload) },
|
|
2036
|
+
* {
|
|
2037
|
+
* maxRetries: 5,
|
|
2038
|
+
* onRetry: (attempt, status, delayMs) =>
|
|
2039
|
+
* console.warn(`retry ${attempt} (HTTP ${status}) in ${delayMs}ms`),
|
|
2040
|
+
* },
|
|
2041
|
+
* );
|
|
2042
|
+
* if (!res.ok) throw new Error(`chat failed: HTTP ${res.status}`);
|
|
2043
|
+
* ```
|
|
2044
|
+
*/
|
|
2045
|
+
declare function fetchWithBackoff(input: string | URL | Request, init?: RequestInit | undefined, config?: FetchWithBackoffConfig): Promise<Response>;
|
|
2046
|
+
|
|
2047
|
+
/**
|
|
2048
|
+
* VRAM lifecycle helpers — ergonomic `keep_alive` primitives.
|
|
2049
|
+
*
|
|
2050
|
+
* Ollama's `keep_alive` parameter (accepted by `/api/generate`,
|
|
2051
|
+
* `/api/chat`, `/api/embed`, `/api/embeddings`, and `/v1/systemone`)
|
|
2052
|
+
* controls how long a model remains resident in VRAM after a request
|
|
2053
|
+
* completes. The upstream spec defines four operational semantics:
|
|
2054
|
+
*
|
|
2055
|
+
* - **Duration string** (`"5m"`, `"10s"`, `"24h"`) — explicit window.
|
|
2056
|
+
* - **Integer seconds** (`3600`, `300`) — explicit window in seconds.
|
|
2057
|
+
* - **Indefinite pinning** (`-1` or `"-1m"`) — keep the model
|
|
2058
|
+
* permanently resident in VRAM until the server is restarted or
|
|
2059
|
+
* an explicit `0` request is made. Useful for hot-loop inference
|
|
2060
|
+
* where load latency dominates.
|
|
2061
|
+
* - **Immediate eviction** (`0` or `"0s"`) — unload the model from
|
|
2062
|
+
* VRAM as soon as the response is finalized. Frees GPU memory
|
|
2063
|
+
* for the next pipeline (e.g. swap a vision model out before
|
|
2064
|
+
* loading an LLM).
|
|
2065
|
+
*
|
|
2066
|
+
* These primitives let callers express VRAM-lifecycle intent with
|
|
2067
|
+
* readable names instead of magic integers, while preserving full
|
|
2068
|
+
* backward compatibility with raw `string | number` values.
|
|
2069
|
+
*
|
|
2070
|
+
* See: https://github.com/ollama/ollama/blob/main/docs/faq.md
|
|
2071
|
+
* #how-do-i-keep-a-model-loaded-in-memory-or-make-it-unload-immediately
|
|
2072
|
+
*/
|
|
2073
|
+
/**
|
|
2074
|
+
* Sentinel value that, when passed as `keep_alive`, instructs the
|
|
2075
|
+
* server to evict the model from VRAM as soon as the in-flight
|
|
2076
|
+
* request finalizes its response. Equivalent to passing `0` or
|
|
2077
|
+
* `"0s"`.
|
|
2078
|
+
*/
|
|
2079
|
+
declare const KEEP_ALIVE_UNLOAD: 0;
|
|
2080
|
+
/**
|
|
2081
|
+
* Sentinel value that, when passed as `keep_alive`, instructs the
|
|
2082
|
+
* server to keep the model resident in VRAM indefinitely (until
|
|
2083
|
+
* the server is restarted or an explicit `0` request arrives).
|
|
2084
|
+
* Equivalent to passing `-1` or `"-1m"`.
|
|
2085
|
+
*/
|
|
2086
|
+
declare const KEEP_ALIVE_INDEFINITE: -1;
|
|
2087
|
+
/**
|
|
2088
|
+
* Ergonomic union for the `keep_alive` field on chat/generate/embed/
|
|
2089
|
+
* embeddings/System One requests.
|
|
2090
|
+
*
|
|
2091
|
+
* Accepts every upstream-supported shape plus two SDK-level sugar
|
|
2092
|
+
* literals:
|
|
2093
|
+
*
|
|
2094
|
+
* - `'unload'` — same as `0`; frees GPU VRAM after this request.
|
|
2095
|
+
* - `'indefinite'` — same as `-1`; pins the model in VRAM until
|
|
2096
|
+
* an explicit unload.
|
|
2097
|
+
*
|
|
2098
|
+
* Use {@link normalizeKeepAlive} to convert this into the raw
|
|
2099
|
+
* `string | number` shape the wire expects.
|
|
2100
|
+
*/
|
|
2101
|
+
type KeepAlive = string | number | 'unload' | 'indefinite';
|
|
2102
|
+
/**
|
|
2103
|
+
* Convert a {@link KeepAlive} value into the raw `string | number`
|
|
2104
|
+
* shape Ollama's REST API expects.
|
|
2105
|
+
*
|
|
2106
|
+
* - `'unload'` → `0` (immediate eviction)
|
|
2107
|
+
* - `'indefinite'` → `-1` (perpetual pinning)
|
|
2108
|
+
* - everything else is passed through unchanged
|
|
2109
|
+
*
|
|
2110
|
+
* Returns `undefined` for `undefined` input so callers can pipe
|
|
2111
|
+
* optional fields straight through without conditional spreads.
|
|
2112
|
+
*
|
|
2113
|
+
* @example
|
|
2114
|
+
* normalizeKeepAlive('unload') // → 0
|
|
2115
|
+
* normalizeKeepAlive('indefinite') // → -1
|
|
2116
|
+
* normalizeKeepAlive('5m') // → '5m'
|
|
2117
|
+
* normalizeKeepAlive(3600) // → 3600
|
|
2118
|
+
* normalizeKeepAlive(undefined) // → undefined
|
|
2119
|
+
*/
|
|
2120
|
+
declare function normalizeKeepAlive(keepAlive: KeepAlive | undefined): string | number | undefined;
|
|
2121
|
+
/**
|
|
2122
|
+
* Type guard: `true` when the value is one of the SDK-level sugar
|
|
2123
|
+
* literals (`'unload'` or `'indefinite'`). Useful for callers that
|
|
2124
|
+
* want to branch on intent rather than magic integers.
|
|
2125
|
+
*/
|
|
2126
|
+
declare function isKeepAliveSugar(value: KeepAlive | unknown): value is 'unload' | 'indefinite';
|
|
2127
|
+
|
|
2128
|
+
/**
|
|
2129
|
+
* `done_reason` decoder helpers — graceful handling of upstream
|
|
2130
|
+
* variance in the `done_reason` field returned by `/api/chat` and
|
|
2131
|
+
* `/api/generate`.
|
|
2132
|
+
*
|
|
2133
|
+
* The Ollama OpenAPI documents `done_reason` as a free-form string,
|
|
2134
|
+
* but the values actually observed on the wire fall into a small,
|
|
2135
|
+
* stable set:
|
|
2136
|
+
*
|
|
2137
|
+
* - `"stop"` — natural stop token hit or end-of-text.
|
|
2138
|
+
* - `"length"` — `num_predict` ceiling reached mid-generation.
|
|
2139
|
+
* - `"load"` — model load event (seen in streaming chunks
|
|
2140
|
+
* before the first token is generated; the
|
|
2141
|
+
* server reports `done: true` with `done_reason:
|
|
2142
|
+
* 'load'` to indicate the load phase completed).
|
|
2143
|
+
* - `"unload"` — model unloaded (seen after a `keep_alive: 0`
|
|
2144
|
+
* request finalizes the unload).
|
|
2145
|
+
*
|
|
2146
|
+
* The SDK accepts any string for `done_reason` (so unknown values
|
|
2147
|
+
* don't cause runtime errors — the conformance-test failure mode
|
|
2148
|
+
* the Wave 13 CI run flagged). These helpers give callers who want
|
|
2149
|
+
* to narrow on the known set a type-safe way to do so.
|
|
2150
|
+
*
|
|
2151
|
+
* See: https://github.com/ollama/ollama/blob/main/docs/api.md
|
|
2152
|
+
*/
|
|
2153
|
+
/**
|
|
2154
|
+
* The set of `done_reason` values Ollama documents and emits in
|
|
2155
|
+
* practice. Unknown upstream additions pass through as plain
|
|
2156
|
+
* strings — use {@link isKnownDoneReason} to narrow.
|
|
2157
|
+
*/
|
|
2158
|
+
type KnownDoneReason = 'stop' | 'length' | 'load' | 'unload';
|
|
2159
|
+
/**
|
|
2160
|
+
* Set of known `done_reason` literals, exposed for callers building
|
|
2161
|
+
* exhaustive `switch` statements or runtime validation tables.
|
|
2162
|
+
*/
|
|
2163
|
+
declare const KNOWN_DONE_REASONS: ReadonlySet<KnownDoneReason>;
|
|
2164
|
+
/**
|
|
2165
|
+
* Type guard: `true` when `value` is one of the documented
|
|
2166
|
+
* `done_reason` literals. Use this to safely narrow from
|
|
2167
|
+
* `string | undefined` to {@link KnownDoneReason} before a
|
|
2168
|
+
* `switch` block.
|
|
2169
|
+
*
|
|
2170
|
+
* @example
|
|
2171
|
+
* ```ts
|
|
2172
|
+
* if (isKnownDoneReason(res.done_reason)) {
|
|
2173
|
+
* switch (res.done_reason) {
|
|
2174
|
+
* case 'stop': // …
|
|
2175
|
+
* case 'length': // …
|
|
2176
|
+
* case 'load': // …
|
|
2177
|
+
* case 'unload': // …
|
|
2178
|
+
* }
|
|
2179
|
+
* } else {
|
|
2180
|
+
* // forward-compat: an unknown reason we didn't list
|
|
2181
|
+
* }
|
|
2182
|
+
* ```
|
|
2183
|
+
*/
|
|
2184
|
+
declare function isKnownDoneReason(value: string | undefined | null): value is KnownDoneReason;
|
|
2185
|
+
|
|
1006
2186
|
/**
|
|
1007
2187
|
* Normalization mappers and aggregators for Ollama streaming responses.
|
|
1008
2188
|
*/
|
|
@@ -1013,9 +2193,27 @@ declare function normalizeProgressStream(source: AbortableAsyncIterable<Progress
|
|
|
1013
2193
|
|
|
1014
2194
|
/**
|
|
1015
2195
|
* NDJSON (newline-delimited JSON) stream reader for native fetch Response bodies.
|
|
2196
|
+
*
|
|
2197
|
+
* Ollama's streaming endpoints (chat / generate / pull / push / create)
|
|
2198
|
+
* emit newline-delimited JSON objects. Each line is a complete JSON
|
|
2199
|
+
* document. The transport is chunked HTTP with status 200 OK — meaning
|
|
2200
|
+
* the HTTP status never changes mid-stream even when the server
|
|
2201
|
+
* encounters an error after generation has started.
|
|
2202
|
+
*
|
|
2203
|
+
* Instead, Ollama emits a final JSON frame of the form
|
|
2204
|
+
* `{"error": "..."}` and closes the stream. This reader detects such
|
|
2205
|
+
* in-band error frames and throws {@link OllamaStreamError} immediately,
|
|
2206
|
+
* so callers don't see the error message bleed into the assistant's
|
|
2207
|
+
* content stream as garbage tokens.
|
|
1016
2208
|
*/
|
|
1017
2209
|
/**
|
|
1018
2210
|
* Parses a byte stream from fetch body into an async generator of typed JSON chunks.
|
|
2211
|
+
*
|
|
2212
|
+
* Throws {@link OllamaStreamError} immediately upon encountering an
|
|
2213
|
+
* in-band `{"error": "..."}` frame. The thrown error carries an empty
|
|
2214
|
+
* `partialContent` (this low-level parser does not accumulate content;
|
|
2215
|
+
* the higher-level aggregator in `streaming/normalize.ts` is the
|
|
2216
|
+
* right place to surface partial content if needed).
|
|
1019
2217
|
*/
|
|
1020
2218
|
declare function parseNdjsonStream<T>(stream: ReadableStream<Uint8Array>): AsyncGenerator<T, void, undefined>;
|
|
1021
2219
|
|
|
@@ -1157,6 +2355,184 @@ interface DefineToolOptions<TParams, TResult> {
|
|
|
1157
2355
|
*/
|
|
1158
2356
|
declare function defineTool<TParams, TResult>(options: DefineToolOptions<TParams, TResult>): Tool<TParams, TResult>;
|
|
1159
2357
|
|
|
2358
|
+
/**
|
|
2359
|
+
* Tool-call streaming accumulator — defensive merge logic for
|
|
2360
|
+
* `message.tool_calls` arrays that arrive across multiple NDJSON
|
|
2361
|
+
* chunks.
|
|
2362
|
+
*
|
|
2363
|
+
* ## When this matters
|
|
2364
|
+
*
|
|
2365
|
+
* Ollama's **native `/api/chat`** streaming protocol emits
|
|
2366
|
+
* `message.tool_calls` as a complete JSON array (with parsed-object
|
|
2367
|
+
* `arguments`, not stringified deltas) in a single `done: true` chunk.
|
|
2368
|
+
* For native streaming, the accumulator here is mostly a no-op —
|
|
2369
|
+
* one chunk carries the full array, the next chunk has nothing.
|
|
2370
|
+
*
|
|
2371
|
+
* The accumulator becomes load-bearing in three scenarios:
|
|
2372
|
+
*
|
|
2373
|
+
* 1. **OpenAI-compat streaming** (`/v1/chat/completions` with
|
|
2374
|
+
* `stream: true`): tool call arguments come as string deltas
|
|
2375
|
+
* spread across multiple chunks, identified by `index`. The
|
|
2376
|
+
* compat layer at `src/integrations/openai.ts` already does
|
|
2377
|
+
* its own per-index accumulation; this utility exists for
|
|
2378
|
+
* callers who want the same logic on raw NDJSON chunks.
|
|
2379
|
+
*
|
|
2380
|
+
* 2. **Defensive native streaming**: although the documented
|
|
2381
|
+
* Ollama behavior is "tool_calls arrive in the final chunk",
|
|
2382
|
+
* a future server version could legitimately stream them
|
|
2383
|
+
* incrementally. The SDK shouldn't crash or produce
|
|
2384
|
+
* duplicates if that happens.
|
|
2385
|
+
*
|
|
2386
|
+
* 3. **Caller-side buffering**: agent loops that consume
|
|
2387
|
+
* `OllamaStream` events directly may want to buffer tool-call
|
|
2388
|
+
* deltas themselves rather than trust the SDK's aggregator —
|
|
2389
|
+
* this utility gives them a typed, tested building block.
|
|
2390
|
+
*
|
|
2391
|
+
* ## Merge semantics
|
|
2392
|
+
*
|
|
2393
|
+
* Two `ToolCall` entries are considered "the same call" when they
|
|
2394
|
+
* share the same `id` (the SDK-synthesized stable identifier — see
|
|
2395
|
+
* `src/tools/tool-call-id.ts`), OR when they're at the same array
|
|
2396
|
+
* position and neither has an `id`. When two same-call entries are
|
|
2397
|
+
* merged:
|
|
2398
|
+
*
|
|
2399
|
+
* - `function.name` — the later chunk's name wins (the model may
|
|
2400
|
+
* emit the name in a later chunk after an initial empty one).
|
|
2401
|
+
* - `function.arguments` — the two objects are merged with
|
|
2402
|
+
* spread semantics: `{...earlier, ...later}`. This handles
|
|
2403
|
+
* the case where the model emits arguments incrementally as
|
|
2404
|
+
* partial JSON objects (which is non-standard but seen in some
|
|
2405
|
+
* open-weights fine-tunes).
|
|
2406
|
+
*
|
|
2407
|
+
* For arguments-as-string deltas (the OpenAI-compat case), the
|
|
2408
|
+
* accumulator concatenates the strings rather than merging — see
|
|
2409
|
+
* {@link mergeToolCallArgumentsString}.
|
|
2410
|
+
*/
|
|
2411
|
+
|
|
2412
|
+
/**
|
|
2413
|
+
* Determine whether two `ToolCall` entries refer to the same logical
|
|
2414
|
+
* call (and should therefore be merged rather than appended).
|
|
2415
|
+
*
|
|
2416
|
+
* Returns `true` when:
|
|
2417
|
+
* - both have an `id` and the ids match, OR
|
|
2418
|
+
* - neither has an `id` and they're at the same array index
|
|
2419
|
+
* (caller is responsible for the index check — this helper
|
|
2420
|
+
* only handles the id case).
|
|
2421
|
+
*/
|
|
2422
|
+
declare function isSameToolCall(a: ToolCall, b: ToolCall, aIndex?: number, bIndex?: number): boolean;
|
|
2423
|
+
/**
|
|
2424
|
+
* Merge two `ToolCall` entries that refer to the same logical call.
|
|
2425
|
+
*
|
|
2426
|
+
* The later chunk's `function.name` wins when present; otherwise the
|
|
2427
|
+
* earlier one's name is preserved. The two `arguments` objects are
|
|
2428
|
+
* merged with spread semantics (`{...earlier, ...later}`), which
|
|
2429
|
+
* handles the (non-standard but observed) case where the model
|
|
2430
|
+
* emits arguments as partial JSON objects across chunks.
|
|
2431
|
+
*
|
|
2432
|
+
* For arguments-as-string deltas (OpenAI-compat streaming), use
|
|
2433
|
+
* {@link mergeToolCallArgumentsString} instead.
|
|
2434
|
+
*/
|
|
2435
|
+
declare function mergeToolCall(earlier: ToolCall, later: ToolCall): ToolCall;
|
|
2436
|
+
/**
|
|
2437
|
+
* Merge two `ToolCall` entries where `function.arguments` is a JSON
|
|
2438
|
+
* string (the OpenAI-compat streaming format), concatenating the
|
|
2439
|
+
* string deltas rather than spreading objects.
|
|
2440
|
+
*
|
|
2441
|
+
* Returns a `ToolCall` whose `function.arguments` is the
|
|
2442
|
+
* concatenated string. The caller is responsible for JSON-parsing
|
|
2443
|
+
* the final string when the stream completes.
|
|
2444
|
+
*/
|
|
2445
|
+
declare function mergeToolCallArgumentsString(earlier: ToolCall, later: ToolCall): ToolCall;
|
|
2446
|
+
/**
|
|
2447
|
+
* Defensive accumulator for `message.tool_calls` arrays that arrive
|
|
2448
|
+
* across multiple NDJSON chunks. Used internally by
|
|
2449
|
+
* `aggregateChat` in `src/streaming/normalize.ts` so the SDK doesn't
|
|
2450
|
+
* produce duplicate tool_call entries if a future server version
|
|
2451
|
+
* starts streaming them incrementally.
|
|
2452
|
+
*
|
|
2453
|
+
* The merge strategy is:
|
|
2454
|
+
*
|
|
2455
|
+
* - For each tool call in the new chunk, check whether an existing
|
|
2456
|
+
* entry matches (by `id` if both have one, by array position
|
|
2457
|
+
* otherwise).
|
|
2458
|
+
* - If a match is found, merge the two entries via
|
|
2459
|
+
* {@link mergeToolCall}.
|
|
2460
|
+
* - If no match is found, append the new entry.
|
|
2461
|
+
*
|
|
2462
|
+
* This preserves the documented native Ollama behavior (tool_calls
|
|
2463
|
+
* arrive complete in one chunk → no merge happens, just one append)
|
|
2464
|
+
* while being defensive against incremental streaming.
|
|
2465
|
+
*
|
|
2466
|
+
* @example
|
|
2467
|
+
* ```ts
|
|
2468
|
+
* const acc = new ToolCallAccumulator();
|
|
2469
|
+
* acc.process([
|
|
2470
|
+
* { function: { name: 'get_weather', arguments: { city: 'Tokyo' } } },
|
|
2471
|
+
* ]);
|
|
2472
|
+
* acc.process([
|
|
2473
|
+
* { function: { name: 'get_weather', arguments: { unit: 'celsius' } } },
|
|
2474
|
+
* ]);
|
|
2475
|
+
* const result = acc.finalize();
|
|
2476
|
+
* // → [{ function: { name: 'get_weather', arguments: { city: 'Tokyo', unit: 'celsius' } } }]
|
|
2477
|
+
* ```
|
|
2478
|
+
*/
|
|
2479
|
+
declare class ToolCallAccumulator {
|
|
2480
|
+
private readonly entries;
|
|
2481
|
+
/**
|
|
2482
|
+
* Process a chunk's `tool_calls` array. Entries that match an
|
|
2483
|
+
* existing accumulated entry (by id or array position) are merged;
|
|
2484
|
+
* new entries are appended.
|
|
2485
|
+
*
|
|
2486
|
+
* Returns the current accumulated state (a shallow copy — entries
|
|
2487
|
+
* themselves are NOT deep-cloned, so callers that mutate
|
|
2488
|
+
* `function.arguments` on a returned entry will mutate the
|
|
2489
|
+
* accumulator's internal state. Use {@link snapshot} for an
|
|
2490
|
+
* explicitly read-only view, or deep-clone the result if you need
|
|
2491
|
+
* full isolation).
|
|
2492
|
+
*/
|
|
2493
|
+
process(toolCalls: readonly ToolCall[] | undefined): readonly ToolCall[];
|
|
2494
|
+
/**
|
|
2495
|
+
* Returns the final accumulated tool calls. Identical to calling
|
|
2496
|
+
* `process(undefined)` — included for ergonomics so callers can
|
|
2497
|
+
* express "I'm done, give me the result" without a no-op call.
|
|
2498
|
+
*
|
|
2499
|
+
* The returned array is a shallow copy; entries themselves are
|
|
2500
|
+
* the same object references the accumulator holds internally.
|
|
2501
|
+
*/
|
|
2502
|
+
finalize(): readonly ToolCall[];
|
|
2503
|
+
/**
|
|
2504
|
+
* Returns the current accumulated state without modifying the
|
|
2505
|
+
* accumulator. Useful for mid-stream inspection.
|
|
2506
|
+
*
|
|
2507
|
+
* The returned array is a shallow copy; mutating it (e.g.
|
|
2508
|
+
* `result.push(...)`) does not affect the accumulator, but
|
|
2509
|
+
* mutating an entry's `function.arguments` object DOES affect
|
|
2510
|
+
* the accumulator's internal state because the entry reference
|
|
2511
|
+
* is shared. Deep-clone the result if you need full isolation.
|
|
2512
|
+
*/
|
|
2513
|
+
snapshot(): readonly ToolCall[];
|
|
2514
|
+
/**
|
|
2515
|
+
* Returns the number of accumulated tool calls so far.
|
|
2516
|
+
*/
|
|
2517
|
+
get length(): number;
|
|
2518
|
+
}
|
|
2519
|
+
/**
|
|
2520
|
+
* Convenience function: merge two `tool_calls` arrays using the
|
|
2521
|
+
* default accumulator semantics, returning the merged array.
|
|
2522
|
+
*
|
|
2523
|
+
* Stateless equivalent of:
|
|
2524
|
+
*
|
|
2525
|
+
* ```ts
|
|
2526
|
+
* const acc = new ToolCallAccumulator();
|
|
2527
|
+
* acc.process(earlier);
|
|
2528
|
+
* acc.process(later);
|
|
2529
|
+
* return acc.finalize();
|
|
2530
|
+
* ```
|
|
2531
|
+
*
|
|
2532
|
+
* Useful for one-off merges without instantiating an accumulator.
|
|
2533
|
+
*/
|
|
2534
|
+
declare function mergeToolCallArrays(earlier: readonly ToolCall[] | undefined, later: readonly ToolCall[] | undefined): readonly ToolCall[];
|
|
2535
|
+
|
|
1160
2536
|
/**
|
|
1161
2537
|
* Converts MCP tools into Ollama-compatible tools.
|
|
1162
2538
|
*/
|
|
@@ -1348,15 +2724,131 @@ declare const GEN_AI_SYSTEM_OLLAMA = "ollama";
|
|
|
1348
2724
|
* Cross-runtime helpers with no Node.js API dependency, so they stay safe to import from
|
|
1349
2725
|
* the core client bundle (see ADR 0006 — Edge Runtime CI).
|
|
1350
2726
|
*/
|
|
2727
|
+
|
|
1351
2728
|
/**
|
|
1352
2729
|
* Encodes an image to a base64 string, matching the wire format Ollama's `/api/chat` and
|
|
1353
2730
|
* `/api/generate` `images` arrays expect.
|
|
1354
2731
|
*
|
|
1355
|
-
* - A `string` is assumed to already be base64-encoded and is returned unchanged.
|
|
1356
|
-
*
|
|
1357
|
-
*
|
|
1358
|
-
*
|
|
2732
|
+
* - A `string` is assumed to already be base64-encoded and is returned unchanged. For
|
|
2733
|
+
* data URIs, URLs, and local file paths use {@link resolveImageInput}, which this SDK's
|
|
2734
|
+
* request pipeline applies automatically (see {@link withEncodedImages}).
|
|
2735
|
+
* - A `Uint8Array` (or Node `Buffer`) is base64-encoded using whichever universal primitive
|
|
2736
|
+
* the runtime exposes: `Buffer` on Node.js, `btoa` on browsers/Edge runtimes. Chunked so it
|
|
2737
|
+
* doesn't blow the engine's max call-stack/argument-count limits on large images.
|
|
2738
|
+
*/
|
|
2739
|
+
declare function encodeImage(image: VisionInput): Promise<string>;
|
|
2740
|
+
|
|
2741
|
+
/**
|
|
2742
|
+
* Context-window safety: client-side token estimation and pre-flight overflow
|
|
2743
|
+
* warnings for `/api/chat` and `/api/generate`.
|
|
2744
|
+
*
|
|
2745
|
+
* Why this exists: when `options.num_ctx` is left unset, Ollama loads the
|
|
2746
|
+
* model with a conservative default context window (commonly 2048–4096
|
|
2747
|
+
* tokens, model-dependent) and **silently truncates** any prompt that doesn't
|
|
2748
|
+
* fit — the model just loses the top of the conversation with no error, no
|
|
2749
|
+
* warning, and often no visible symptom beyond degraded answers. This module
|
|
2750
|
+
* estimates the prompt size client-side (a fast heuristic — no tokenizer
|
|
2751
|
+
* download, no model dependency) and surfaces a warning *before* the request
|
|
2752
|
+
* is sent, when the estimate approaches the effective context window.
|
|
2753
|
+
*
|
|
2754
|
+
* The estimator is deliberately simple and documented as approximate
|
|
2755
|
+
* (±20–30% vs a real BPE tokenizer):
|
|
2756
|
+
*
|
|
2757
|
+
* - CJK code points (Chinese/Japanese/Korean) count as ~1 token each —
|
|
2758
|
+
* modern tokenizers emit roughly one token per CJK character.
|
|
2759
|
+
* - Everything else estimates at ~4 characters per token, the classic
|
|
2760
|
+
* English GPT-style average.
|
|
2761
|
+
* - Each attached image adds a fixed {@link IMAGE_TOKEN_ESTIMATE} —
|
|
2762
|
+
* vision encoders consume a roughly predictable tile budget per image
|
|
2763
|
+
* (e.g. LLaVA-family encoders emit a few hundred tokens per image).
|
|
2764
|
+
*
|
|
2765
|
+
* It is used in three places:
|
|
2766
|
+
*
|
|
2767
|
+
* 1. `OllamaClient.chat()`/`generate()` pre-flight — logs a warning (or
|
|
2768
|
+
* throws, per `onContextOverflow`) when the estimate crosses
|
|
2769
|
+
* {@link DEFAULT_CONTEXT_WARNING_THRESHOLD} of the effective window.
|
|
2770
|
+
* 2. The `defaultContextLength` client config — injects `num_ctx` when the
|
|
2771
|
+
* caller didn't set one, making the effective window explicit instead
|
|
2772
|
+
* of server-default guesswork.
|
|
2773
|
+
* 3. `ConversationSession` — sessions grow monotonically, so they surface
|
|
2774
|
+
* cache and context stats per turn.
|
|
2775
|
+
*/
|
|
2776
|
+
|
|
2777
|
+
/**
|
|
2778
|
+
* Fraction of the effective context window at which a pre-flight warning
|
|
2779
|
+
* fires. 0.9 = warn when the estimated prompt reaches 90% of `num_ctx`.
|
|
2780
|
+
* Above this, the response has little room left and the next turn's history
|
|
2781
|
+
* will almost certainly overflow.
|
|
2782
|
+
*/
|
|
2783
|
+
declare const DEFAULT_CONTEXT_WARNING_THRESHOLD = 0.9;
|
|
2784
|
+
/**
|
|
2785
|
+
* Rough token budget consumed per attached image. Vision encoders emit a few
|
|
2786
|
+
* hundred embedding tokens per image tile; 300 sits between LLaVA-1.5's ~258
|
|
2787
|
+
* and qwen-vl's higher tile counts. This is an estimate for warning purposes
|
|
2788
|
+
* only — it never gates a request on its own unless images push the total
|
|
2789
|
+
* past the threshold.
|
|
2790
|
+
*/
|
|
2791
|
+
declare const IMAGE_TOKEN_ESTIMATE = 300;
|
|
2792
|
+
/**
|
|
2793
|
+
* Heuristic token estimate for a single text string: ~1 token per CJK
|
|
2794
|
+
* character + ~1 token per 4 non-CJK characters. Approximate by design —
|
|
2795
|
+
* accurate enough to warn against silent truncation without shipping a
|
|
2796
|
+
* model-specific tokenizer.
|
|
2797
|
+
*/
|
|
2798
|
+
declare function estimateTokens(text: string): number;
|
|
2799
|
+
/**
|
|
2800
|
+
* Estimated prompt tokens for a `/api/chat` request: every message plus
|
|
2801
|
+
* tool-schema overhead (each tool definition's JSON schema serializes into
|
|
2802
|
+
* the prompt on Ollama's side).
|
|
2803
|
+
*/
|
|
2804
|
+
declare function estimateChatRequestTokens(req: ChatRequestOptions): number;
|
|
2805
|
+
/** Estimated prompt tokens for a `/api/generate` request (prompt + system + images). */
|
|
2806
|
+
declare function estimateGenerateRequestTokens(req: GenerateRequestOptions): number;
|
|
2807
|
+
/** Where the context window used by a pre-flight check came from. */
|
|
2808
|
+
type ContextWindowSource =
|
|
2809
|
+
/** Explicit `options.num_ctx` on the request — exact, server-enforced. */
|
|
2810
|
+
'request'
|
|
2811
|
+
/** Client `defaultContextLength` config — injected as `num_ctx` by the pipeline. */
|
|
2812
|
+
| 'client-default'
|
|
2813
|
+
/** Neither set; Ollama's server default applies (2048–4096, model-dependent). */
|
|
2814
|
+
| 'server-fallback';
|
|
2815
|
+
/** What the pre-flight check concluded about one request. */
|
|
2816
|
+
interface ContextCheck {
|
|
2817
|
+
/** Estimated prompt tokens (heuristic — see {@link estimateTokens}). */
|
|
2818
|
+
readonly estimatedPromptTokens: number;
|
|
2819
|
+
/** The context window the estimate was compared against. */
|
|
2820
|
+
readonly effectiveContextLength: number;
|
|
2821
|
+
/** Where that window came from. */
|
|
2822
|
+
readonly windowSource: ContextWindowSource;
|
|
2823
|
+
/** True when the estimate crossed the warning threshold of the effective window. */
|
|
2824
|
+
readonly exceedsThreshold: boolean;
|
|
2825
|
+
}
|
|
2826
|
+
interface ContextCheckOptions {
|
|
2827
|
+
/** Client `defaultContextLength`, applied when the request doesn't set `num_ctx`. */
|
|
2828
|
+
readonly defaultContextLength?: number | undefined;
|
|
2829
|
+
/** Warning threshold; defaults to {@link DEFAULT_CONTEXT_WARNING_THRESHOLD}. */
|
|
2830
|
+
readonly threshold?: number | undefined;
|
|
2831
|
+
}
|
|
2832
|
+
/**
|
|
2833
|
+
* Runs the pre-flight context check for a chat request. Pure — callers decide
|
|
2834
|
+
* what to do with the result (log, throw, ignore).
|
|
2835
|
+
*/
|
|
2836
|
+
declare function checkChatContext(req: ChatRequestOptions, options?: ContextCheckOptions): ContextCheck;
|
|
2837
|
+
/**
|
|
2838
|
+
* Runs the pre-flight context check for a generate request. Pure — callers
|
|
2839
|
+
* decide what to do with the result (log, throw, ignore).
|
|
2840
|
+
*/
|
|
2841
|
+
declare function checkGenerateContext(req: GenerateRequestOptions, options?: ContextCheckOptions): ContextCheck;
|
|
2842
|
+
/**
|
|
2843
|
+
* The conservative server-side default (2048) Ollama most commonly applies
|
|
2844
|
+
* when `num_ctx` is unset — used to warn about *likely* truncation even when
|
|
2845
|
+
* no effective window is known client-side.
|
|
2846
|
+
*/
|
|
2847
|
+
declare const OLLAMA_FALLBACK_CONTEXT_LENGTH = 2048;
|
|
2848
|
+
/**
|
|
2849
|
+
* Builds the human-readable warning text for a failed pre-flight check.
|
|
2850
|
+
* Exported for reuse by `ConversationSession` and for tests.
|
|
1359
2851
|
*/
|
|
1360
|
-
declare function
|
|
2852
|
+
declare function contextWarningMessage(check: ContextCheck, kind: 'chat' | 'generate'): string;
|
|
1361
2853
|
|
|
1362
|
-
export { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_SYSTEM, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_OLLAMA_AGENT_ITERATION, ATTR_OLLAMA_AGENT_MAX_ITERATIONS, ATTR_OLLAMA_ENDPOINT_ATTEMPT, ATTR_OLLAMA_ENDPOINT_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL, AbortableAsyncIterable, Agent, type AgentChatClient, type AgentConfig, type AgentHooks, type AgentResult, type AgentRunInput, type AgentTurn, type AnswerFor, AnthropicCompatClient, AnyTool, type BackoffOptions, ChatRequestOptions, ChatResponse, ChatStreamResult, type ChoiceDecision, CopyRequestOptions, CreateRequestOptions, DEFAULT_BACKOFF, DEFAULT_BASE_URL, DEFAULT_FAILOVER_CODES, DEFAULT_RETRY_CONFIG, DEFAULT_TIMEOUT_MS, type Decision, type DefineToolOptions, DeleteRequestOptions, EmbedRequestOptions, EmbedResponse, EmbeddingsRequestOptions, EmbeddingsResponse, type EndpointHealth, type EndpointHealthCheckResult, EndpointRegistry, type EndpointRegistryOptions, FetchLike, GEN_AI_SYSTEM_OLLAMA, GenerateRequestOptions, GenerateResponse, GenerateStreamResult, HttpClient, type LoadMcpToolsOptions, Logger, McpBridge, type McpBridgeOptions, McpCallToolResult, McpClientLike, McpTask, McpToolDescriptor, Message, Middleware, type ModelCapabilities, ModelOptions, ModelResponse, ModelsClient, type NoulDecision, OLLAMA_CLOUD_BASE_URL, OllamaClient, type OllamaClientConfig, type OllamaCloudFreeTierQuotaBudgets, type OllamaCredentialConfig, type OllamaEndpoint, OllamaStream, OllamaStreamEvent, OllamaStreamEventType, OpenAICompatClient, ProgressResponse, ProgressStreamResult, PsResponse, PullRequestOptions, PushRequestOptions, QuotaManager, type QuotaManagerOptions, type QuotaWindowConfig, type QuotaWindowStatus, type RankResult, RawUsageSource, RequestLifecycleHook, RequestRunner, type RetryConfig, type RuntimeMode, type ScoreDecision, ShowRequestOptions, ShowResponse, type SpanAttributeValue, type SpanAttributes, StatusResponse, type SupportedSchema, type SystemOneAnswer, type SystemOneAnswers, SystemOneChoiceAnswer, SystemOneChoiceQuestion, SystemOneContent, SystemOneNoulAnswer, SystemOneNoulQuestion, type SystemOneQuestion, type SystemOneQuestions, type SystemOneRequest, type SystemOneResponse, SystemOneScoreAnswer, SystemOneScoreQuestion, type SystemOneUsage, ThinkValue, ThinkingMetadata, type TimeoutSignal, TokenUsage, Tool, ToolCall, ToolDefinition, ToolExecutionResult, ToolHandler, ToolRegistry, VersionResponse, WebFetchRequestOptions, WebFetchResponse, WebSearchRequestOptions, WebSearchResponse, addSpanEvent, calculateBackoff, checkEndpointHealth, createDecision, createOllamaCloudFreeTierQuota, createTimeoutSignal, defineTool, detectModelCapabilities, encodeImage, inferRuntimeMode, listAllMcpTools, listAvailableModels, loadMcpTools, normalizeChatStream, normalizeGenerateStream, normalizeProgressStream, parseNdjsonStream, parseStructuredOutput, registerMcpTools, resolveCredentialEndpoints, setSpanError, toDataStream, toResponse, toTextStream, withRetry, withSpan, zodToJsonSchema };
|
|
2854
|
+
export { ATTR_GEN_AI_OPERATION_NAME, ATTR_GEN_AI_REQUEST_MODEL, ATTR_GEN_AI_RESPONSE_MODEL, ATTR_GEN_AI_SYSTEM, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_NAME, ATTR_GEN_AI_USAGE_INPUT_TOKENS, ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_OLLAMA_AGENT_ITERATION, ATTR_OLLAMA_AGENT_MAX_ITERATIONS, ATTR_OLLAMA_ENDPOINT_ATTEMPT, ATTR_OLLAMA_ENDPOINT_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL, AbortableAsyncIterable, Agent, type AgentChatClient, type AgentConfig, type AgentHooks, type AgentResult, type AgentRunInput, type AgentTurn, type AnswerFor, AnthropicCompatClient, AnyTool, type BackoffOptions, BalanceRequestOptions, type BalanceResponse, type BlobUploadResult, ChatRequestOptions, ChatResponse, ChatStreamResult, type ChoiceDecision, type ContextCheck, type ContextCheckOptions, type ContextWindowSource, type ConversationSendOptions, ConversationSession, type ConversationSessionOptions, CopyRequestOptions, CreateRequestOptions, type CumulativeCacheStats, DEFAULT_BACKOFF, DEFAULT_BASE_URL, DEFAULT_CONTEXT_WARNING_THRESHOLD, DEFAULT_EMBED_BATCH_SIZE, DEFAULT_EMBED_CONCURRENCY, DEFAULT_FAILOVER_CODES, DEFAULT_FETCH_BACKOFF_CONFIG, DEFAULT_RETRY_CONFIG, DEFAULT_TIMEOUT_MS, type Decision, type DefineToolOptions, DeleteRequestOptions, type EmbedBatchOptions, type EmbedBatchResult, EmbedRequestOptions, EmbedResponse, EmbeddingsRequestOptions, EmbeddingsResponse, type EndpointHealth, type EndpointHealthCheckResult, EndpointRegistry, type EndpointRegistryOptions, FetchLike, type FetchWithBackoffConfig, FormatOption, GEN_AI_SYSTEM_OLLAMA, GenerateRequestOptions, GenerateResponse, GenerateStreamResult, HttpClient, IMAGE_TOKEN_ESTIMATE, type IncludedBalance, KEEP_ALIVE_INDEFINITE, KEEP_ALIVE_UNLOAD, KNOWN_DONE_REASONS, type KeepAlive, type KnownDoneReason, type LegacyBalanceLimit, type LegacyIncludedBalance, type LoadMcpToolsOptions, Logger, MAX_SYSTEM_ONE_IMAGES_BYTES, MAX_SYSTEM_ONE_REQUEST_BYTES, McpBridge, type McpBridgeOptions, McpCallToolResult, McpClientLike, McpTask, McpToolDescriptor, Message, Middleware, type ModelCapabilities, ModelOptions, ModelResponse, ModelsClient, type NoulDecision, OLLAMA_CLOUD_BASE_URL, OLLAMA_FALLBACK_CONTEXT_LENGTH, OllamaClient, type OllamaClientConfig, type OllamaCloudFreeTierQuotaBudgets, type OllamaCredentialConfig, type OllamaEndpoint, OllamaStream, OllamaStreamEvent, OllamaStreamEventType, OpenAICompatClient, type OversizedEmbedInput, type ParallelToolCallBehavior, ProgressResponse, ProgressStreamResult, PsResponse, PullRequestOptions, PushRequestOptions, QuotaManager, type QuotaManagerOptions, type QuotaWindowConfig, type QuotaWindowStatus, RETRYABLE_STATUS_CODES, type RankResult, RawUsageSource, RequestCancellationOptions, RequestLifecycleHook, RequestRunner, type ResponsesCreateRequest, type ResponsesCreateResponse, ResponsesModule, type ResponsesReasoningEffort, type ResponsesStreamEvent, type ResponsesToolDefinition, type ResponsesUsage, type RetryConfig, type RuntimeMode, type ScoreDecision, type SessionTurn, ShowRequestOptions, ShowResponse, type SpanAttributeValue, type SpanAttributes, StatusResponse, type SupportedSchema, type SystemOneAnswer, type SystemOneAnswers, SystemOneChoiceAnswer, SystemOneChoiceQuestion, SystemOneContent, SystemOneNoulAnswer, SystemOneNoulQuestion, type SystemOneQuestion, type SystemOneQuestions, type SystemOneRequest, type SystemOneResponse, SystemOneScoreAnswer, SystemOneScoreQuestion, type SystemOneUsage, ThinkValue, ThinkingMetadata, type TimeoutSignal, TokenUsage, Tool, ToolCall, ToolCallAccumulator, ToolDefinition, ToolExecutionResult, ToolHandler, ToolParameters, ToolRegistry, type TurnCacheStats, type UsageBucket, type UsageMetrics, UsageRequestOptions, type UsageResponse, VersionResponse, VisionInput, WebFetchRequestOptions, WebFetchResponse, WebSearchRequestOptions, WebSearchResponse, addSpanEvent, batchEmbed, calculateBackoff, checkChatContext, checkEndpointHealth, checkGenerateContext, contextWarningMessage, createDecision, createOllamaCloudFreeTierQuota, createTimeoutSignal, defineTool, detectModelCapabilities, embedBatchOverflowMessage, encodeImage, estimateChatRequestTokens, estimateGenerateRequestTokens, estimateSystemOneRequestBytes, estimateTokens, fetchWithBackoff, findOversizedEmbedInputs, inferRuntimeMode, isKeepAliveSugar, isKnownDoneReason, isSameToolCall, listAllMcpTools, listAvailableModels, loadMcpTools, mergeToolCall, mergeToolCallArgumentsString, mergeToolCallArrays, normalizeChatStream, normalizeGenerateStream, normalizeKeepAlive, normalizeProgressStream, parseNdjsonStream, parseStructuredOutput, registerMcpTools, resolveCredentialEndpoints, setSpanError, toDataStream, toResponse, toTextStream, withRetry, withSpan, zodToJsonSchema };
|