@modelprofile.com/flexharness-providers 7.0.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 (130) hide show
  1. package/dist_ts_media_openai/image.d.ts +46 -0
  2. package/dist_ts_media_openai/image.js +110 -0
  3. package/dist_ts_media_openai/index.d.ts +10 -0
  4. package/dist_ts_media_openai/index.js +16 -0
  5. package/dist_ts_media_openai/plugins.d.ts +3 -0
  6. package/dist_ts_media_openai/plugins.js +4 -0
  7. package/dist_ts_openai_account/index.d.ts +6 -0
  8. package/dist_ts_openai_account/index.js +7 -0
  9. package/dist_ts_openai_account/plugins.d.ts +7 -0
  10. package/dist_ts_openai_account/plugins.js +6 -0
  11. package/dist_ts_openai_account/smartai.providers.credentials.d.ts +7 -0
  12. package/dist_ts_openai_account/smartai.providers.credentials.js +126 -0
  13. package/dist_ts_openai_account/smartai.providers.error.d.ts +11 -0
  14. package/dist_ts_openai_account/smartai.providers.error.js +30 -0
  15. package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +161 -0
  16. package/dist_ts_openai_account/smartai.providers.interfaces.js +2 -0
  17. package/dist_ts_openai_account/smartai.providers.openai.d.ts +29 -0
  18. package/dist_ts_openai_account/smartai.providers.openai.js +1075 -0
  19. package/dist_ts_openai_account/smartai.providers.registry.d.ts +11 -0
  20. package/dist_ts_openai_account/smartai.providers.registry.js +53 -0
  21. package/dist_ts_openai_auth/index.d.ts +6 -0
  22. package/dist_ts_openai_auth/index.js +10 -0
  23. package/dist_ts_openai_auth/interfaces.d.ts +71 -0
  24. package/dist_ts_openai_auth/interfaces.js +2 -0
  25. package/dist_ts_openai_auth/smartai.auth.openai.d.ts +34 -0
  26. package/dist_ts_openai_auth/smartai.auth.openai.js +603 -0
  27. package/dist_ts_openai_auth/smartai.middleware.openai.d.ts +6 -0
  28. package/dist_ts_openai_auth/smartai.middleware.openai.js +47 -0
  29. package/dist_ts_openai_auth_files/index.d.ts +43 -0
  30. package/dist_ts_openai_auth_files/index.js +377 -0
  31. package/dist_ts_openai_auth_files/plugins.d.ts +9 -0
  32. package/dist_ts_openai_auth_files/plugins.js +10 -0
  33. package/dist_ts_providers/anthropic/index.d.ts +8 -0
  34. package/dist_ts_providers/anthropic/index.js +13 -0
  35. package/dist_ts_providers/anthropic/plugins.d.ts +4 -0
  36. package/dist_ts_providers/anthropic/plugins.js +4 -0
  37. package/dist_ts_providers/anthropic/smartai.middleware.anthropic.d.ts +8 -0
  38. package/dist_ts_providers/anthropic/smartai.middleware.anthropic.js +10 -0
  39. package/dist_ts_providers/google/index.d.ts +5 -0
  40. package/dist_ts_providers/google/index.js +6 -0
  41. package/dist_ts_providers/google/plugins.d.ts +3 -0
  42. package/dist_ts_providers/google/plugins.js +3 -0
  43. package/dist_ts_providers/groq/index.d.ts +5 -0
  44. package/dist_ts_providers/groq/index.js +6 -0
  45. package/dist_ts_providers/groq/plugins.d.ts +3 -0
  46. package/dist_ts_providers/groq/plugins.js +3 -0
  47. package/dist_ts_providers/index.d.ts +8 -0
  48. package/dist_ts_providers/index.js +9 -0
  49. package/dist_ts_providers/mistral/index.d.ts +5 -0
  50. package/dist_ts_providers/mistral/index.js +6 -0
  51. package/dist_ts_providers/mistral/plugins.d.ts +3 -0
  52. package/dist_ts_providers/mistral/plugins.js +3 -0
  53. package/dist_ts_providers/ollama/index.d.ts +6 -0
  54. package/dist_ts_providers/ollama/index.js +7 -0
  55. package/dist_ts_providers/ollama/interfaces.d.ts +26 -0
  56. package/dist_ts_providers/ollama/interfaces.js +2 -0
  57. package/dist_ts_providers/ollama/smartai.provider.ollama.d.ts +8 -0
  58. package/dist_ts_providers/ollama/smartai.provider.ollama.js +378 -0
  59. package/dist_ts_providers/openai/index.d.ts +15 -0
  60. package/dist_ts_providers/openai/index.js +14 -0
  61. package/dist_ts_providers/openai/plugins.d.ts +6 -0
  62. package/dist_ts_providers/openai/plugins.js +4 -0
  63. package/dist_ts_providers/perplexity/index.d.ts +5 -0
  64. package/dist_ts_providers/perplexity/index.js +6 -0
  65. package/dist_ts_providers/perplexity/plugins.d.ts +3 -0
  66. package/dist_ts_providers/perplexity/plugins.js +3 -0
  67. package/dist_ts_providers/xai/index.d.ts +5 -0
  68. package/dist_ts_providers/xai/index.js +6 -0
  69. package/dist_ts_providers/xai/plugins.d.ts +3 -0
  70. package/dist_ts_providers/xai/plugins.js +3 -0
  71. package/dist_ts_research/index.d.ts +19 -0
  72. package/dist_ts_research/index.js +98 -0
  73. package/dist_ts_research/plugins.d.ts +2 -0
  74. package/dist_ts_research/plugins.js +3 -0
  75. package/license.md +21 -0
  76. package/openai-codex-license.txt +201 -0
  77. package/package.json +105 -0
  78. package/readme.md +62 -0
  79. package/third-party-notices.md +19 -0
  80. package/ts_media_openai/image.ts +147 -0
  81. package/ts_media_openai/index.ts +26 -0
  82. package/ts_media_openai/plugins.ts +3 -0
  83. package/ts_media_openai/readme.md +37 -0
  84. package/ts_openai_account/index.ts +11 -0
  85. package/ts_openai_account/plugins.ts +9 -0
  86. package/ts_openai_account/readme.md +37 -0
  87. package/ts_openai_account/smartai.providers.credentials.ts +163 -0
  88. package/ts_openai_account/smartai.providers.error.ts +49 -0
  89. package/ts_openai_account/smartai.providers.interfaces.ts +222 -0
  90. package/ts_openai_account/smartai.providers.openai.ts +1209 -0
  91. package/ts_openai_account/smartai.providers.registry.ts +57 -0
  92. package/ts_openai_auth/index.ts +12 -0
  93. package/ts_openai_auth/interfaces.ts +89 -0
  94. package/ts_openai_auth/readme.md +37 -0
  95. package/ts_openai_auth/smartai.auth.openai.ts +789 -0
  96. package/ts_openai_auth/smartai.middleware.openai.ts +55 -0
  97. package/ts_openai_auth_files/index.ts +499 -0
  98. package/ts_openai_auth_files/plugins.ts +13 -0
  99. package/ts_openai_auth_files/readme.md +37 -0
  100. package/ts_providers/anthropic/index.ts +20 -0
  101. package/ts_providers/anthropic/plugins.ts +4 -0
  102. package/ts_providers/anthropic/readme.md +37 -0
  103. package/ts_providers/anthropic/smartai.middleware.anthropic.ts +12 -0
  104. package/ts_providers/google/index.ts +8 -0
  105. package/ts_providers/google/plugins.ts +3 -0
  106. package/ts_providers/google/readme.md +37 -0
  107. package/ts_providers/groq/index.ts +8 -0
  108. package/ts_providers/groq/plugins.ts +3 -0
  109. package/ts_providers/groq/readme.md +37 -0
  110. package/ts_providers/index.ts +8 -0
  111. package/ts_providers/mistral/index.ts +8 -0
  112. package/ts_providers/mistral/plugins.ts +3 -0
  113. package/ts_providers/mistral/readme.md +37 -0
  114. package/ts_providers/ollama/index.ts +8 -0
  115. package/ts_providers/ollama/interfaces.ts +28 -0
  116. package/ts_providers/ollama/readme.md +37 -0
  117. package/ts_providers/ollama/smartai.provider.ollama.ts +426 -0
  118. package/ts_providers/openai/index.ts +29 -0
  119. package/ts_providers/openai/plugins.ts +6 -0
  120. package/ts_providers/openai/readme.md +37 -0
  121. package/ts_providers/perplexity/index.ts +8 -0
  122. package/ts_providers/perplexity/plugins.ts +3 -0
  123. package/ts_providers/perplexity/readme.md +37 -0
  124. package/ts_providers/readme.md +62 -0
  125. package/ts_providers/xai/index.ts +8 -0
  126. package/ts_providers/xai/plugins.ts +3 -0
  127. package/ts_providers/xai/readme.md +37 -0
  128. package/ts_research/index.ts +120 -0
  129. package/ts_research/plugins.ts +2 -0
  130. package/ts_research/readme.md +37 -0
@@ -0,0 +1,8 @@
1
+ import * as plugins from './plugins.js';
2
+
3
+ export interface IGoogleModelOptions extends plugins.ISmartAiOptions { provider: 'google'; }
4
+
5
+ export const createGoogleModelProvider = (): plugins.IModelProvider<IGoogleModelOptions> => ({
6
+ id: 'google',
7
+ getModel: (options) => plugins.createGoogle({ apiKey: options.apiKey })(options.model),
8
+ });
@@ -0,0 +1,3 @@
1
+ import { createGoogle } from '@ai-sdk/google';
2
+ export { createGoogle };
3
+ export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
@@ -0,0 +1,37 @@
1
+ Explicit google model provider for FlexHarness models and the AI SDK.
2
+
3
+ Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
4
+ Install the components your project uses. Provider adapters are registered explicitly;
5
+ the agent and harness do not install or register vendor SDKs.
6
+
7
+ ```sh
8
+ pnpm add @modelprofile.com/flexharness-providers
9
+ ```
10
+
11
+ See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
12
+ for composition examples and the SmartAI/SmartAgent migration map.
13
+
14
+ ## Issue Reporting and Security
15
+
16
+ Report issues and security concerns through [community.foss.global](https://community.foss.global).
17
+
18
+ ## License and Legal Information
19
+
20
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
21
+
22
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
23
+
24
+ ### Trademarks
25
+
26
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
27
+
28
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
29
+
30
+ ### Company Information
31
+
32
+ Task Venture Capital GmbH<br>
33
+ Registered at District Court Bremen HRB 35230 HB, Germany
34
+
35
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
36
+
37
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -0,0 +1,8 @@
1
+ import * as plugins from './plugins.js';
2
+
3
+ export interface IGroqModelOptions extends plugins.ISmartAiOptions { provider: 'groq'; }
4
+
5
+ export const createGroqModelProvider = (): plugins.IModelProvider<IGroqModelOptions> => ({
6
+ id: 'groq',
7
+ getModel: (options) => plugins.createGroq({ apiKey: options.apiKey })(options.model),
8
+ });
@@ -0,0 +1,3 @@
1
+ import { createGroq } from '@ai-sdk/groq';
2
+ export { createGroq };
3
+ export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
@@ -0,0 +1,37 @@
1
+ Explicit groq model provider for FlexHarness models and the AI SDK.
2
+
3
+ Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
4
+ Install the components your project uses. Provider adapters are registered explicitly;
5
+ the agent and harness do not install or register vendor SDKs.
6
+
7
+ ```sh
8
+ pnpm add @modelprofile.com/flexharness-providers
9
+ ```
10
+
11
+ See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
12
+ for composition examples and the SmartAI/SmartAgent migration map.
13
+
14
+ ## Issue Reporting and Security
15
+
16
+ Report issues and security concerns through [community.foss.global](https://community.foss.global).
17
+
18
+ ## License and Legal Information
19
+
20
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
21
+
22
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
23
+
24
+ ### Trademarks
25
+
26
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
27
+
28
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
29
+
30
+ ### Company Information
31
+
32
+ Task Venture Capital GmbH<br>
33
+ Registered at District Court Bremen HRB 35230 HB, Germany
34
+
35
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
36
+
37
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -0,0 +1,8 @@
1
+ export * from './anthropic/index.js';
2
+ export * from './openai/index.js';
3
+ export * from './google/index.js';
4
+ export * from './groq/index.js';
5
+ export * from './mistral/index.js';
6
+ export * from './xai/index.js';
7
+ export * from './perplexity/index.js';
8
+ export * from './ollama/index.js';
@@ -0,0 +1,8 @@
1
+ import * as plugins from './plugins.js';
2
+
3
+ export interface IMistralModelOptions extends plugins.ISmartAiOptions { provider: 'mistral'; }
4
+
5
+ export const createMistralModelProvider = (): plugins.IModelProvider<IMistralModelOptions> => ({
6
+ id: 'mistral',
7
+ getModel: (options) => plugins.createMistral({ apiKey: options.apiKey })(options.model),
8
+ });
@@ -0,0 +1,3 @@
1
+ import { createMistral } from '@ai-sdk/mistral';
2
+ export { createMistral };
3
+ export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
@@ -0,0 +1,37 @@
1
+ Explicit mistral model provider for FlexHarness models and the AI SDK.
2
+
3
+ Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
4
+ Install the components your project uses. Provider adapters are registered explicitly;
5
+ the agent and harness do not install or register vendor SDKs.
6
+
7
+ ```sh
8
+ pnpm add @modelprofile.com/flexharness-providers
9
+ ```
10
+
11
+ See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
12
+ for composition examples and the SmartAI/SmartAgent migration map.
13
+
14
+ ## Issue Reporting and Security
15
+
16
+ Report issues and security concerns through [community.foss.global](https://community.foss.global).
17
+
18
+ ## License and Legal Information
19
+
20
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
21
+
22
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
23
+
24
+ ### Trademarks
25
+
26
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
27
+
28
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
29
+
30
+ ### Company Information
31
+
32
+ Task Venture Capital GmbH<br>
33
+ Registered at District Court Bremen HRB 35230 HB, Germany
34
+
35
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
36
+
37
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -0,0 +1,8 @@
1
+ import type { IModelProvider } from '@modelprofile.com/flexharness-models';
2
+ import type { IOllamaModelRequest } from './interfaces.js';
3
+ import { createOllamaModel } from './smartai.provider.ollama.js';
4
+ export const createOllamaModelProvider = (): IModelProvider<IOllamaModelRequest> => ({
5
+ id: 'ollama', getModel: createOllamaModel,
6
+ });
7
+ export { createOllamaModel };
8
+ export * from './interfaces.js';
@@ -0,0 +1,28 @@
1
+ import type { ISmartAiOptions } from '@modelprofile.com/flexharness-models';
2
+
3
+ export interface IOllamaModelRequest extends ISmartAiOptions {
4
+ provider: 'ollama';
5
+ ollamaOptions?: IOllamaModelOptions;
6
+ }
7
+
8
+ /**
9
+ * Ollama model runtime options passed in the request body `options` field.
10
+ * @see https://github.com/ollama/ollama/blob/main/docs/modelfile.md
11
+ */
12
+ export interface IOllamaModelOptions {
13
+ /** Context window size. Default: 2048. */
14
+ num_ctx?: number;
15
+ /** 0 = deterministic. Default: 0.8. For Qwen models use 0.55. */
16
+ temperature?: number;
17
+ top_k?: number;
18
+ top_p?: number;
19
+ repeat_penalty?: number;
20
+ num_predict?: number;
21
+ stop?: string[];
22
+ seed?: number;
23
+ /**
24
+ * Enable thinking/reasoning mode (Qwen3, QwQ, DeepSeek-R1 etc.).
25
+ * The custom Ollama provider handles this directly.
26
+ */
27
+ think?: boolean;
28
+ }
@@ -0,0 +1,37 @@
1
+ Explicit ollama model provider for FlexHarness models and the AI SDK.
2
+
3
+ Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
4
+ Install the components your project uses. Provider adapters are registered explicitly;
5
+ the agent and harness do not install or register vendor SDKs.
6
+
7
+ ```sh
8
+ pnpm add @modelprofile.com/flexharness-providers
9
+ ```
10
+
11
+ See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
12
+ for composition examples and the SmartAI/SmartAgent migration map.
13
+
14
+ ## Issue Reporting and Security
15
+
16
+ Report issues and security concerns through [community.foss.global](https://community.foss.global).
17
+
18
+ ## License and Legal Information
19
+
20
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
21
+
22
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
23
+
24
+ ### Trademarks
25
+
26
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
27
+
28
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
29
+
30
+ ### Company Information
31
+
32
+ Task Venture Capital GmbH<br>
33
+ Registered at District Court Bremen HRB 35230 HB, Germany
34
+
35
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
36
+
37
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -0,0 +1,426 @@
1
+ import type {
2
+ LanguageModelV3,
3
+ LanguageModelV3CallOptions,
4
+ LanguageModelV3GenerateResult,
5
+ LanguageModelV3StreamResult,
6
+ LanguageModelV3StreamPart,
7
+ LanguageModelV3Prompt,
8
+ LanguageModelV3Content,
9
+ LanguageModelV3Usage,
10
+ LanguageModelV3FinishReason,
11
+ } from '@ai-sdk/provider';
12
+ import type { IOllamaModelRequest, IOllamaModelOptions } from './interfaces.js';
13
+
14
+ interface IOllamaMessage {
15
+ role: string;
16
+ content: string;
17
+ images?: string[];
18
+ tool_calls?: Array<{
19
+ function: { name: string; arguments: Record<string, unknown> };
20
+ }>;
21
+ thinking?: string;
22
+ }
23
+
24
+ interface IOllamaTool {
25
+ type: 'function';
26
+ function: {
27
+ name: string;
28
+ description: string;
29
+ parameters: Record<string, unknown>;
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Convert AI SDK V3 prompt messages to Ollama's message format.
35
+ */
36
+ function convertPromptToOllamaMessages(prompt: LanguageModelV3Prompt): IOllamaMessage[] {
37
+ const messages: IOllamaMessage[] = [];
38
+
39
+ for (const msg of prompt) {
40
+ if (msg.role === 'system') {
41
+ // System message content is a plain string in V3
42
+ messages.push({ role: 'system', content: msg.content });
43
+ } else if (msg.role === 'user') {
44
+ let text = '';
45
+ const images: string[] = [];
46
+ for (const part of msg.content) {
47
+ if (part.type === 'text') {
48
+ text += part.text;
49
+ } else if (part.type === 'file' && part.mediaType?.startsWith('image/')) {
50
+ // Handle image files — Ollama expects base64 images
51
+ if (typeof part.data === 'string') {
52
+ images.push(part.data);
53
+ } else if (part.data instanceof Uint8Array) {
54
+ images.push(Buffer.from(part.data).toString('base64'));
55
+ }
56
+ }
57
+ }
58
+ const m: IOllamaMessage = { role: 'user', content: text };
59
+ if (images.length > 0) m.images = images;
60
+ messages.push(m);
61
+ } else if (msg.role === 'assistant') {
62
+ let text = '';
63
+ let thinking = '';
64
+ const toolCalls: IOllamaMessage['tool_calls'] = [];
65
+ for (const part of msg.content) {
66
+ if (part.type === 'text') {
67
+ text += part.text;
68
+ } else if (part.type === 'reasoning') {
69
+ thinking += part.text;
70
+ } else if (part.type === 'tool-call') {
71
+ const args = typeof part.input === 'string'
72
+ ? JSON.parse(part.input as string)
73
+ : (part.input as Record<string, unknown>);
74
+ toolCalls.push({
75
+ function: {
76
+ name: part.toolName,
77
+ arguments: args,
78
+ },
79
+ });
80
+ }
81
+ }
82
+ const m: IOllamaMessage = { role: 'assistant', content: text };
83
+ if (toolCalls.length > 0) m.tool_calls = toolCalls;
84
+ if (thinking) m.thinking = thinking;
85
+ messages.push(m);
86
+ } else if (msg.role === 'tool') {
87
+ for (const part of msg.content) {
88
+ if (part.type === 'tool-result') {
89
+ let resultContent = '';
90
+ if (part.output) {
91
+ if (part.output.type === 'text') {
92
+ resultContent = part.output.value;
93
+ } else if (part.output.type === 'json') {
94
+ resultContent = JSON.stringify(part.output.value);
95
+ }
96
+ }
97
+ messages.push({ role: 'tool', content: resultContent });
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ return messages;
104
+ }
105
+
106
+ /**
107
+ * Convert AI SDK V3 tools to Ollama's tool format.
108
+ */
109
+ function convertToolsToOllamaTools(tools: LanguageModelV3CallOptions['tools']): IOllamaTool[] | undefined {
110
+ if (!tools || tools.length === 0) return undefined;
111
+
112
+ return tools
113
+ .filter((t): t is Extract<typeof t, { type: 'function' }> => t.type === 'function')
114
+ .map(t => ({
115
+ type: 'function' as const,
116
+ function: {
117
+ name: t.name,
118
+ description: t.description ?? '',
119
+ parameters: t.inputSchema as Record<string, unknown>,
120
+ },
121
+ }));
122
+ }
123
+
124
+ function makeUsage(promptTokens?: number, completionTokens?: number): LanguageModelV3Usage {
125
+ return {
126
+ inputTokens: {
127
+ total: promptTokens,
128
+ noCache: undefined,
129
+ cacheRead: undefined,
130
+ cacheWrite: undefined,
131
+ },
132
+ outputTokens: {
133
+ total: completionTokens,
134
+ text: completionTokens,
135
+ reasoning: undefined,
136
+ },
137
+ };
138
+ }
139
+
140
+ function makeFinishReason(reason?: string): LanguageModelV3FinishReason {
141
+ if (reason === 'tool_calls' || reason === 'tool-calls') {
142
+ return { unified: 'tool-calls', raw: reason };
143
+ }
144
+ return { unified: 'stop', raw: reason ?? 'stop' };
145
+ }
146
+
147
+ let idCounter = 0;
148
+ function generateId(): string {
149
+ return `ollama-${Date.now()}-${idCounter++}`;
150
+ }
151
+
152
+ /**
153
+ * Custom LanguageModelV3 implementation for Ollama.
154
+ * Calls Ollama's native /api/chat endpoint directly to support
155
+ * think, num_ctx, temperature, and other model options.
156
+ */
157
+ export function createOllamaModel(options: IOllamaModelRequest): LanguageModelV3 {
158
+ const baseUrl = options.baseUrl ?? 'http://localhost:11434';
159
+ const modelId = options.model;
160
+ const ollamaOpts: IOllamaModelOptions = { ...options.ollamaOptions };
161
+
162
+ // Apply default temperature of 0.55 for Qwen models
163
+ if (modelId.toLowerCase().includes('qwen') && ollamaOpts.temperature === undefined) {
164
+ ollamaOpts.temperature = 0.55;
165
+ }
166
+
167
+ const model: LanguageModelV3 = {
168
+ specificationVersion: 'v3',
169
+ provider: 'ollama',
170
+ modelId,
171
+ supportedUrls: {},
172
+
173
+ async doGenerate(callOptions: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult> {
174
+ const messages = convertPromptToOllamaMessages(callOptions.prompt);
175
+ const tools = convertToolsToOllamaTools(callOptions.tools);
176
+
177
+ const ollamaModelOptions: Record<string, unknown> = { ...ollamaOpts };
178
+ // Override with call-level options if provided
179
+ if (callOptions.temperature !== undefined) ollamaModelOptions.temperature = callOptions.temperature;
180
+ if (callOptions.topP !== undefined) ollamaModelOptions.top_p = callOptions.topP;
181
+ if (callOptions.topK !== undefined) ollamaModelOptions.top_k = callOptions.topK;
182
+ if (callOptions.maxOutputTokens !== undefined) ollamaModelOptions.num_predict = callOptions.maxOutputTokens;
183
+ if (callOptions.seed !== undefined) ollamaModelOptions.seed = callOptions.seed;
184
+ if (callOptions.stopSequences) ollamaModelOptions.stop = callOptions.stopSequences;
185
+ // Remove think from options — it goes at the top level
186
+ const { think, ...modelOpts } = ollamaModelOptions;
187
+
188
+ const requestBody: Record<string, unknown> = {
189
+ model: modelId,
190
+ messages,
191
+ stream: false,
192
+ options: modelOpts,
193
+ };
194
+
195
+ // Add think parameter at the top level (Ollama API requirement)
196
+ if (ollamaOpts.think !== undefined) {
197
+ requestBody.think = ollamaOpts.think;
198
+ }
199
+
200
+ if (tools) requestBody.tools = tools;
201
+
202
+ const response = await fetch(`${baseUrl}/api/chat`, {
203
+ method: 'POST',
204
+ headers: { 'Content-Type': 'application/json' },
205
+ body: JSON.stringify(requestBody),
206
+ signal: callOptions.abortSignal,
207
+ });
208
+
209
+ if (!response.ok) {
210
+ const body = await response.text();
211
+ throw new Error(`Ollama API error ${response.status}: ${body}`);
212
+ }
213
+
214
+ const result = await response.json() as Record<string, unknown>;
215
+ const message = result.message as Record<string, unknown>;
216
+
217
+ // Build content array
218
+ const content: LanguageModelV3Content[] = [];
219
+
220
+ // Add reasoning if present
221
+ if (message.thinking && typeof message.thinking === 'string') {
222
+ content.push({ type: 'reasoning', text: message.thinking });
223
+ }
224
+
225
+ // Add text content
226
+ if (message.content && typeof message.content === 'string') {
227
+ content.push({ type: 'text', text: message.content });
228
+ }
229
+
230
+ // Add tool calls if present
231
+ if (Array.isArray(message.tool_calls)) {
232
+ for (const tc of message.tool_calls as Array<Record<string, unknown>>) {
233
+ const fn = tc.function as Record<string, unknown>;
234
+ content.push({
235
+ type: 'tool-call',
236
+ toolCallId: generateId(),
237
+ toolName: fn.name as string,
238
+ input: JSON.stringify(fn.arguments),
239
+ });
240
+ }
241
+ }
242
+
243
+ const finishReason = Array.isArray(message.tool_calls) && (message.tool_calls as unknown[]).length > 0
244
+ ? makeFinishReason('tool_calls')
245
+ : makeFinishReason('stop');
246
+
247
+ return {
248
+ content,
249
+ finishReason,
250
+ usage: makeUsage(
251
+ (result.prompt_eval_count as number) ?? undefined,
252
+ (result.eval_count as number) ?? undefined,
253
+ ),
254
+ warnings: [],
255
+ request: { body: requestBody },
256
+ };
257
+ },
258
+
259
+ async doStream(callOptions: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult> {
260
+ const messages = convertPromptToOllamaMessages(callOptions.prompt);
261
+ const tools = convertToolsToOllamaTools(callOptions.tools);
262
+
263
+ const ollamaModelOptions: Record<string, unknown> = { ...ollamaOpts };
264
+ if (callOptions.temperature !== undefined) ollamaModelOptions.temperature = callOptions.temperature;
265
+ if (callOptions.topP !== undefined) ollamaModelOptions.top_p = callOptions.topP;
266
+ if (callOptions.topK !== undefined) ollamaModelOptions.top_k = callOptions.topK;
267
+ if (callOptions.maxOutputTokens !== undefined) ollamaModelOptions.num_predict = callOptions.maxOutputTokens;
268
+ if (callOptions.seed !== undefined) ollamaModelOptions.seed = callOptions.seed;
269
+ if (callOptions.stopSequences) ollamaModelOptions.stop = callOptions.stopSequences;
270
+ const { think, ...modelOpts } = ollamaModelOptions;
271
+
272
+ const requestBody: Record<string, unknown> = {
273
+ model: modelId,
274
+ messages,
275
+ stream: true,
276
+ options: modelOpts,
277
+ };
278
+
279
+ if (ollamaOpts.think !== undefined) {
280
+ requestBody.think = ollamaOpts.think;
281
+ }
282
+
283
+ if (tools) requestBody.tools = tools;
284
+
285
+ const response = await fetch(`${baseUrl}/api/chat`, {
286
+ method: 'POST',
287
+ headers: { 'Content-Type': 'application/json' },
288
+ body: JSON.stringify(requestBody),
289
+ signal: callOptions.abortSignal,
290
+ });
291
+
292
+ if (!response.ok) {
293
+ const body = await response.text();
294
+ throw new Error(`Ollama API error ${response.status}: ${body}`);
295
+ }
296
+
297
+ const reader = response.body!.getReader();
298
+ const decoder = new TextDecoder();
299
+
300
+ const textId = generateId();
301
+ const reasoningId = generateId();
302
+ let textStarted = false;
303
+ let reasoningStarted = false;
304
+ let hasToolCalls = false;
305
+ let closed = false;
306
+
307
+ const stream = new ReadableStream<LanguageModelV3StreamPart>({
308
+ async pull(controller) {
309
+ if (closed) return;
310
+
311
+ const processLine = (line: string) => {
312
+ if (!line.trim()) return;
313
+ let json: Record<string, unknown>;
314
+ try {
315
+ json = JSON.parse(line);
316
+ } catch {
317
+ return;
318
+ }
319
+
320
+ const msg = json.message as Record<string, unknown> | undefined;
321
+
322
+ // Handle thinking/reasoning content
323
+ if (msg?.thinking && typeof msg.thinking === 'string') {
324
+ if (!reasoningStarted) {
325
+ reasoningStarted = true;
326
+ controller.enqueue({ type: 'reasoning-start', id: reasoningId });
327
+ }
328
+ controller.enqueue({ type: 'reasoning-delta', id: reasoningId, delta: msg.thinking });
329
+ }
330
+
331
+ // Handle text content
332
+ if (msg?.content && typeof msg.content === 'string') {
333
+ if (reasoningStarted && !textStarted) {
334
+ controller.enqueue({ type: 'reasoning-end', id: reasoningId });
335
+ }
336
+ if (!textStarted) {
337
+ textStarted = true;
338
+ controller.enqueue({ type: 'text-start', id: textId });
339
+ }
340
+ controller.enqueue({ type: 'text-delta', id: textId, delta: msg.content });
341
+ }
342
+
343
+ // Handle tool calls
344
+ if (Array.isArray(msg?.tool_calls)) {
345
+ hasToolCalls = true;
346
+ for (const tc of msg!.tool_calls as Array<Record<string, unknown>>) {
347
+ const fn = tc.function as Record<string, unknown>;
348
+ const callId = generateId();
349
+ controller.enqueue({
350
+ type: 'tool-call',
351
+ toolCallId: callId,
352
+ toolName: fn.name as string,
353
+ input: JSON.stringify(fn.arguments),
354
+ });
355
+ }
356
+ }
357
+
358
+ // Handle done
359
+ if (json.done) {
360
+ if (reasoningStarted && !textStarted) {
361
+ controller.enqueue({ type: 'reasoning-end', id: reasoningId });
362
+ }
363
+ if (textStarted) {
364
+ controller.enqueue({ type: 'text-end', id: textId });
365
+ }
366
+ controller.enqueue({
367
+ type: 'finish',
368
+ finishReason: hasToolCalls
369
+ ? makeFinishReason('tool_calls')
370
+ : makeFinishReason('stop'),
371
+ usage: makeUsage(
372
+ (json.prompt_eval_count as number) ?? undefined,
373
+ (json.eval_count as number) ?? undefined,
374
+ ),
375
+ });
376
+ closed = true;
377
+ controller.close();
378
+ }
379
+ };
380
+
381
+ try {
382
+ let buffer = '';
383
+ while (true) {
384
+ const { done, value } = await reader.read();
385
+ if (done) {
386
+ if (buffer.trim()) processLine(buffer);
387
+ if (!closed) {
388
+ controller.enqueue({
389
+ type: 'finish',
390
+ finishReason: makeFinishReason('stop'),
391
+ usage: makeUsage(undefined, undefined),
392
+ });
393
+ closed = true;
394
+ controller.close();
395
+ }
396
+ return;
397
+ }
398
+
399
+ buffer += decoder.decode(value, { stream: true });
400
+ const lines = buffer.split('\n');
401
+ buffer = lines.pop() || '';
402
+ for (const line of lines) {
403
+ processLine(line);
404
+ if (closed) return;
405
+ }
406
+ }
407
+ } catch (error) {
408
+ if (!closed) {
409
+ controller.error(error);
410
+ closed = true;
411
+ }
412
+ } finally {
413
+ reader.releaseLock();
414
+ }
415
+ },
416
+ });
417
+
418
+ return {
419
+ stream,
420
+ request: { body: requestBody },
421
+ };
422
+ },
423
+ };
424
+
425
+ return model;
426
+ }