ai-sdk-ollama 4.1.0 → 4.3.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.
Files changed (69) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +40 -13
  3. package/dist/functions/generate-text.d.ts +63 -0
  4. package/dist/functions/generate-text.d.ts.map +1 -0
  5. package/dist/functions/stream-text.d.ts +25 -0
  6. package/dist/functions/stream-text.d.ts.map +1 -0
  7. package/dist/index.browser.cjs +6290 -1904
  8. package/dist/index.browser.cjs.map +1 -1
  9. package/dist/index.browser.d.ts +12 -1130
  10. package/dist/index.browser.d.ts.map +1 -0
  11. package/dist/index.browser.js +6290 -1904
  12. package/dist/index.browser.js.map +1 -1
  13. package/dist/index.cjs +6319 -1933
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.ts +19 -1240
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +6319 -1933
  18. package/dist/index.js.map +1 -1
  19. package/dist/models/chat-language-model.d.ts +33 -0
  20. package/dist/models/chat-language-model.d.ts.map +1 -0
  21. package/dist/models/chat-language-model.test-helpers.d.ts +28 -0
  22. package/dist/models/chat-language-model.test-helpers.d.ts.map +1 -0
  23. package/dist/models/chat-request.d.ts +85 -0
  24. package/dist/models/chat-request.d.ts.map +1 -0
  25. package/dist/models/chat-result.d.ts +78 -0
  26. package/dist/models/chat-result.d.ts.map +1 -0
  27. package/dist/models/chat-stream.d.ts +15 -0
  28. package/dist/models/chat-stream.d.ts.map +1 -0
  29. package/dist/models/embedding-model.d.ts +35 -0
  30. package/dist/models/embedding-model.d.ts.map +1 -0
  31. package/dist/models/embedding-reranking-model.d.ts +84 -0
  32. package/dist/models/embedding-reranking-model.d.ts.map +1 -0
  33. package/dist/models/image-model.d.ts +34 -0
  34. package/dist/models/image-model.d.ts.map +1 -0
  35. package/dist/models/reranking-model.d.ts +83 -0
  36. package/dist/models/reranking-model.d.ts.map +1 -0
  37. package/dist/ollama-client.d.ts +40 -0
  38. package/dist/ollama-client.d.ts.map +1 -0
  39. package/dist/ollama-tools.d.ts +51 -0
  40. package/dist/ollama-tools.d.ts.map +1 -0
  41. package/dist/provider.browser.d.ts +11 -0
  42. package/dist/provider.browser.d.ts.map +1 -0
  43. package/dist/provider.d.ts +213 -0
  44. package/dist/provider.d.ts.map +1 -0
  45. package/dist/test-setup.d.ts +6 -0
  46. package/dist/test-setup.d.ts.map +1 -0
  47. package/dist/tool/web-fetch.d.ts +274 -0
  48. package/dist/tool/web-fetch.d.ts.map +1 -0
  49. package/dist/tool/web-search.d.ts +282 -0
  50. package/dist/tool/web-search.d.ts.map +1 -0
  51. package/dist/utils/convert-to-ollama-messages.d.ts +8 -0
  52. package/dist/utils/convert-to-ollama-messages.d.ts.map +1 -0
  53. package/dist/utils/cosine-similarity.d.ts +18 -0
  54. package/dist/utils/cosine-similarity.d.ts.map +1 -0
  55. package/dist/utils/json-schema-coercion.d.ts +38 -0
  56. package/dist/utils/json-schema-coercion.d.ts.map +1 -0
  57. package/dist/utils/json-text-repair.d.ts +63 -0
  58. package/dist/utils/json-text-repair.d.ts.map +1 -0
  59. package/dist/utils/map-ollama-finish-reason.d.ts +10 -0
  60. package/dist/utils/map-ollama-finish-reason.d.ts.map +1 -0
  61. package/dist/utils/object-generation-reliability.d.ts +82 -0
  62. package/dist/utils/object-generation-reliability.d.ts.map +1 -0
  63. package/dist/utils/ollama-error.d.ts +16 -0
  64. package/dist/utils/ollama-error.d.ts.map +1 -0
  65. package/dist/utils/tool-calling-reliability.d.ts +162 -0
  66. package/dist/utils/tool-calling-reliability.d.ts.map +1 -0
  67. package/package.json +17 -20
  68. package/dist/index.browser.d.cts +0 -1130
  69. package/dist/index.d.cts +0 -1240
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 371f515: Track AI SDK 7.0.95 and open the provider up to custom Ollama clients.
8
+
9
+ - The `ai` peer range moves to `^7.0.95`, with `@ai-sdk/provider` and `@ai-sdk/provider-utils` updated to match.
10
+ - `OllamaClient` is a structural contract, so the official client, a maintained fork, or a custom adapter can be injected in both Node.js and browser builds. Streaming methods use the newly exported `AbortableStream` type.
11
+ - Per-request `AbortSignal`s are forwarded to the client, and an aborted request settles as a rejection across the reliability retry and fallback paths.
12
+ - Returned `message.thinking` is surfaced as AI SDK reasoning independently of the request's `think` setting, including on terminal stream chunks, and streamed text and reasoning blocks close symmetrically.
13
+ - Output usage carries the total token count Ollama reports and leaves the text/reasoning split unset, matching what the API provides.
14
+
15
+ ## 4.2.0
16
+
17
+ ### Minor Changes
18
+
19
+ - a48c0e1: Add MCP Apps examples demonstrating the host flow for ui:// resources
20
+
21
+ - Node example: connect with mcpAppClientCapabilities, split tools by visibility, generateText with model-visible tools, read and fingerprint ui:// HTML resource, simulate iframe proxy allow/deny
22
+ - Browser example: Vite dev server hosts MCP Apps sandbox, proxies read-resource and call-tool requests, renders interactive dashboard via experimental_MCPAppRenderer
23
+ - Shared stdio MCP server with dashboard resource, model+app tool, and app-only refresh tool
24
+
3
25
  ## 4.1.0
4
26
 
5
27
  ### Minor Changes
package/README.md CHANGED
@@ -740,7 +740,10 @@ const ollama = createOllama({ apiKey: process.env.OLLAMA_API_KEY });
740
740
 
741
741
  ### Using Existing Ollama Client
742
742
 
743
- You can also pass an existing Ollama client instance to reuse your configuration:
743
+ You can also pass an existing Ollama client instance to reuse your configuration.
744
+ The provider accepts the exported structural `OllamaClient` interface, so the
745
+ official client, the maintained compatibility fork, or a custom adapter can be
746
+ injected in Node.js and browser builds.
744
747
 
745
748
  ```typescript
746
749
  import { Ollama } from 'ollama';
@@ -756,13 +759,36 @@ const existingClient = new Ollama({
756
759
  const ollamaSdk = createOllama({ client: existingClient });
757
760
 
758
761
  // Use both clients as needed
759
- await ollamaRaw.list(); // Direct Ollama operations
762
+ await existingClient.list(); // Direct Ollama operations
760
763
  const { text } = await generateText({
761
764
  model: ollamaSdk('llama3.2'),
762
765
  prompt: 'Hello!',
763
766
  });
764
767
  ```
765
768
 
769
+ #### Custom client adapters
770
+
771
+ `OllamaClient` is structural, so any object with the right shape works — no
772
+ `Ollama` instance required. Streaming methods return an `AbortableStream`, an
773
+ async iterable with an `abort()`:
774
+
775
+ ```typescript
776
+ import type { AbortableStream, OllamaClient } from 'ai-sdk-ollama';
777
+ import { createOllama } from 'ai-sdk-ollama';
778
+
779
+ const client: OllamaClient = {
780
+ chat: myChat, // returns a ChatResponse, or an AbortableStream of them
781
+ embed: myEmbed,
782
+ webSearch: myWebSearch,
783
+ webFetch: myWebFetch,
784
+ };
785
+
786
+ const ollama = createOllama({ client });
787
+ ```
788
+
789
+ Per-request cancellation is forwarded to the client as an `AbortSignal`, and an
790
+ aborted request rejects rather than resolving with a retried or fallback result.
791
+
766
792
  ### Structured Output
767
793
 
768
794
  ```typescript
@@ -918,39 +944,38 @@ const { output: custom } = await generateText({
918
944
 
919
945
  ### Reasoning Support
920
946
 
921
- Some models like DeepSeek-R1 support reasoning (chain-of-thought) output. Enable this feature to see the model's thinking process:
947
+ Use `think` to request reasoning generation from models that support it. Returned `message.thinking` is exposed as AI SDK reasoning, separately from final text, in both generation results and streams (including terminal chunks).
922
948
 
923
949
  ```typescript
924
950
  import { ollama } from 'ai-sdk-ollama';
925
951
  import { generateText } from 'ai';
926
952
 
927
953
  // Enable reasoning for models that support it (e.g., deepseek-r1:7b)
928
- const model = ollama('deepseek-r1:7b', { reasoning: true });
954
+ const model = ollama('deepseek-r1:7b', { think: true });
929
955
 
930
956
  // Generate text with reasoning
931
- const { text } = await generateText({
957
+ const { text, reasoningText } = await generateText({
932
958
  model,
933
959
  prompt:
934
960
  'Solve: If I have 3 boxes, each with 4 smaller boxes, and each smaller box has 5 items, how many items total?',
935
961
  });
936
962
 
937
963
  console.log('Answer:', text);
938
- // DeepSeek-R1 includes reasoning in the output with <think> tags:
939
- // <think>
940
- // First, I'll calculate the number of smaller boxes: 3 × 4 = 12
941
- // Then, the total items: 12 × 5 = 60
942
- // </think>
943
- // You have 60 items in total.
964
+ console.log('Reasoning:', reasoningText);
944
965
 
945
966
  // Compare with reasoning disabled
946
- const modelNoReasoning = ollama('deepseek-r1:7b', { reasoning: false });
967
+ const modelNoReasoning = ollama('deepseek-r1:7b', { think: false });
947
968
  const { text: noReasoningText } = await generateText({
948
969
  model: modelNoReasoning,
949
970
  prompt: 'Calculate 3 × 4 × 5',
950
971
  });
951
- // Output: 60 (without showing the thinking process)
972
+ console.log('Answer:', noReasoningText);
952
973
  ```
953
974
 
975
+ `think` controls the request, not filtering of the response. If the server returns thinking with `think` omitted or `false`, the adapter still preserves it as reasoning; it does not turn it into final text. If no thinking is returned, no reasoning content is added.
976
+
977
+ Ollama supplies a combined output token count. The adapter preserves that total and the raw counters, but leaves the text/reasoning token breakdown unknown instead of attributing all output tokens to text.
978
+
954
979
  **Recommended Reasoning Models**:
955
980
 
956
981
  - `deepseek-r1:7b` - Balanced performance and reasoning capability (5GB)
@@ -1029,6 +1054,8 @@ For detailed testing information, see [Integration Tests Documentation](./src/in
1029
1054
 
1030
1055
  🔗 **[MCP Tools Example](../../examples/node/src/mcp-tools-example.ts)** - Model Context Protocol integration
1031
1056
 
1057
+ 🧩 **[MCP Apps Example](../../examples/node/src/mcp-apps-example.ts)** - MCP Apps host flow (ui:// resources, split tools, iframe proxy)
1058
+
1032
1059
  🔧 **[JSON repair example](../../examples/node/src/json-repair-example.ts)** - Object generation with repair options and custom repair
1033
1060
 
1034
1061
  🔀 **[Cascade repair example](../../examples/node/src/test-cascade-repair.ts)** - `cascadeRepairText` and `enhancedRepairText` (jsonrepair + Ollama-specific)
@@ -0,0 +1,63 @@
1
+ /**
2
+ * generateText - Enhanced generateText with Ollama-specific reliability
3
+ *
4
+ * This wrapper provides response synthesis and enhanced tool calling reliability
5
+ * that addresses the core Ollama limitation: tools execute but no final text is generated.
6
+ */
7
+ import { generateText as _generateText } from 'ai';
8
+ /**
9
+ * Enhanced options for Ollama-specific reliability features
10
+ */
11
+ export interface EnhancedOptions {
12
+ /**
13
+ * Enable response synthesis when tools are called but no text is generated
14
+ * @default true
15
+ */
16
+ enableSynthesis?: boolean;
17
+ /**
18
+ * Custom synthesis prompt template
19
+ */
20
+ synthesisPrompt?: string;
21
+ /**
22
+ * Maximum attempts for synthesis
23
+ * @default 2
24
+ */
25
+ maxSynthesisAttempts?: number;
26
+ /**
27
+ * Minimum response length to consider valid
28
+ * @default 10
29
+ */
30
+ minResponseLength?: number;
31
+ /**
32
+ * EXPERIMENTAL: Enable tool calling with structured output (output)
33
+ *
34
+ * The official AI SDK doesn't support combining toolChoice: 'required' with output.
35
+ * When enabled, this uses a two-phase approach:
36
+ * 1. Execute tools first (without output)
37
+ * 2. Generate structured output with tool results injected as context
38
+ *
39
+ * This is NOT standard AI SDK behavior - only enable if you need both features together.
40
+ *
41
+ * @default false
42
+ */
43
+ enableToolsWithStructuredOutput?: boolean;
44
+ }
45
+ /**
46
+ * Enhanced generateText options that extend the official AI SDK options
47
+ */
48
+ export type GenerateTextOptions = Parameters<typeof _generateText>[0] & {
49
+ /**
50
+ * Enhanced options for Ollama-specific reliability features
51
+ */
52
+ enhancedOptions?: EnhancedOptions;
53
+ };
54
+ /**
55
+ * Enhanced generateText function with Ollama-specific reliability improvements
56
+ *
57
+ * This function applies synthesis by default when tools execute but return empty responses.
58
+ * The enhancement preserves the original response prototype and all methods/getters.
59
+ *
60
+ * Type parameters are inferred from the options, preserving AI SDK's type inference.
61
+ */
62
+ export declare function generateText(options: GenerateTextOptions): Promise<Awaited<ReturnType<typeof _generateText>>>;
63
+ //# sourceMappingURL=generate-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-text.d.ts","sourceRoot":"","sources":["../../src/functions/generate-text.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,IAAI,aAAa,EAAe,MAAM,IAAI,CAAC;AAqBhE;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAE3B;;;;;;;;;;;OAWG;IACH,+BAA+B,CAAC,EAAE,OAAO,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG;IACtE;;OAEG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;CACnC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC,CAAC,CA4NpD"}
@@ -0,0 +1,25 @@
1
+ import { streamText as _streamText } from 'ai';
2
+ type AIStreamTextOptions = Parameters<typeof _streamText>[0];
3
+ export type StreamTextOptions = AIStreamTextOptions & {
4
+ enhancedOptions?: {
5
+ /**
6
+ * @default true
7
+ */
8
+ enableToolLogging?: boolean;
9
+ /**
10
+ * @default true
11
+ */
12
+ enableStreamingSynthesis?: boolean;
13
+ /**
14
+ * @default 10
15
+ */
16
+ minStreamLength?: number;
17
+ /**
18
+ * @default 3000
19
+ */
20
+ synthesisTimeout?: number;
21
+ };
22
+ };
23
+ export declare function streamText(options: StreamTextOptions): Promise<Awaited<ReturnType<typeof _streamText>>>;
24
+ export {};
25
+ //# sourceMappingURL=stream-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-text.d.ts","sourceRoot":"","sources":["../../src/functions/stream-text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,WAAW,EAAe,MAAM,IAAI,CAAC;AAE5D,KAAK,mBAAmB,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAsC7D,MAAM,MAAM,iBAAiB,GAAG,mBAAmB,GAAG;IACpD,eAAe,CAAC,EAAE;QAChB;;WAEG;QACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;QAC5B;;WAEG;QACH,wBAAwB,CAAC,EAAE,OAAO,CAAC;QACnC;;WAEG;QACH,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB;;WAEG;QACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;KAC3B,CAAC;CACH,CAAC;AAUF,wBAAsB,UAAU,CAC9B,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAsclD"}