botinabox 2.5.1 → 2.5.2

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 (277) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +190 -190
  3. package/bin/botinabox.mjs +1 -1
  4. package/dist/channels/discord/adapter.d.ts +32 -0
  5. package/dist/channels/discord/adapter.js +70 -0
  6. package/dist/channels/discord/inbound.d.ts +25 -0
  7. package/dist/channels/discord/inbound.js +24 -0
  8. package/dist/channels/discord/models.d.ts +8 -0
  9. package/dist/channels/discord/models.js +5 -0
  10. package/dist/channels/discord/outbound.d.ts +14 -0
  11. package/dist/channels/discord/outbound.js +38 -0
  12. package/dist/channels/slack/adapter.d.ts +33 -0
  13. package/dist/channels/slack/adapter.js +74 -0
  14. package/dist/channels/slack/inbound.d.ts +59 -0
  15. package/dist/channels/slack/inbound.js +96 -0
  16. package/dist/channels/slack/index.d.ts +1 -1
  17. package/dist/channels/slack/models.d.ts +9 -0
  18. package/dist/channels/slack/models.js +5 -0
  19. package/dist/channels/slack/outbound.d.ts +12 -0
  20. package/dist/channels/slack/outbound.js +18 -0
  21. package/dist/channels/slack/transcribe.d.ts +41 -0
  22. package/dist/channels/slack/transcribe.js +106 -0
  23. package/dist/channels/webhook/adapter.d.ts +23 -0
  24. package/dist/channels/webhook/adapter.js +86 -0
  25. package/dist/channels/webhook/hmac.d.ts +13 -0
  26. package/dist/channels/webhook/hmac.js +26 -0
  27. package/dist/channels/webhook/models.d.ts +9 -0
  28. package/dist/channels/webhook/models.js +5 -0
  29. package/dist/channels/webhook/server.d.ts +20 -0
  30. package/dist/channels/webhook/server.js +91 -0
  31. package/dist/chat-pipeline-C-XlLGNl.d.ts +648 -0
  32. package/dist/chat-pipeline-CR1KF6eX.d.ts +652 -0
  33. package/dist/chat-pipeline-DisuC8SB.d.ts +643 -0
  34. package/dist/chat-pipeline-DuNX5WoL.d.ts +655 -0
  35. package/dist/chunk-2LGXQPEA.js +41 -0
  36. package/dist/chunk-3X3YKI4T.js +357 -0
  37. package/dist/chunk-D47AIFOD.js +351 -0
  38. package/dist/chunk-DSNJKNEW.js +328 -0
  39. package/dist/chunk-GS2JFL6I.js +144 -0
  40. package/dist/chunk-J6S6QMUY.js +144 -0
  41. package/dist/chunk-QLA6YOFN.js +22 -0
  42. package/dist/chunk-UACT2WXX.js +381 -0
  43. package/dist/cli/templates/config.yml.d.ts +7 -0
  44. package/dist/cli/templates/config.yml.js +61 -0
  45. package/dist/cli/templates/env.d.ts +1 -0
  46. package/dist/cli/templates/env.js +30 -0
  47. package/dist/cli/templates/index.ts.d.ts +2 -0
  48. package/dist/cli/templates/index.ts.js +30 -0
  49. package/dist/cli/templates/package.json.d.ts +5 -0
  50. package/dist/cli/templates/package.json.js +28 -0
  51. package/dist/cli.js +0 -0
  52. package/dist/connector-DDahQw-2.d.ts +63 -0
  53. package/dist/connectors/google/calendar-connector.d.ts +40 -0
  54. package/dist/connectors/google/calendar-connector.js +243 -0
  55. package/dist/connectors/google/gmail-connector.d.ts +42 -0
  56. package/dist/connectors/google/gmail-connector.js +345 -0
  57. package/dist/connectors/google/oauth.d.ts +48 -0
  58. package/dist/connectors/google/oauth.js +112 -0
  59. package/dist/connectors/google/types.d.ts +78 -0
  60. package/dist/connectors/google/types.js +2 -0
  61. package/dist/core/chat/auto-discovery.d.ts +16 -0
  62. package/dist/core/chat/auto-discovery.js +54 -0
  63. package/dist/core/chat/channel-registry.d.ts +45 -0
  64. package/dist/core/chat/channel-registry.js +96 -0
  65. package/dist/core/chat/chat-pipeline.d.ts +113 -0
  66. package/dist/core/chat/chat-pipeline.js +395 -0
  67. package/dist/core/chat/chat-responder.d.ts +90 -0
  68. package/dist/core/chat/chat-responder.js +185 -0
  69. package/dist/core/chat/formatter.d.ts +11 -0
  70. package/dist/core/chat/formatter.js +60 -0
  71. package/dist/core/chat/index.d.ts +24 -0
  72. package/dist/core/chat/index.js +18 -0
  73. package/dist/core/chat/message-interpreter.d.ts +91 -0
  74. package/dist/core/chat/message-interpreter.js +166 -0
  75. package/dist/core/chat/message-store.d.ts +66 -0
  76. package/dist/core/chat/message-store.js +131 -0
  77. package/dist/core/chat/notification-queue.d.ts +34 -0
  78. package/dist/core/chat/notification-queue.js +111 -0
  79. package/dist/core/chat/pipeline.d.ts +38 -0
  80. package/dist/core/chat/pipeline.js +89 -0
  81. package/dist/core/chat/policies.d.ts +16 -0
  82. package/dist/core/chat/policies.js +25 -0
  83. package/dist/core/chat/routing.d.ts +17 -0
  84. package/dist/core/chat/routing.js +36 -0
  85. package/dist/core/chat/session-key.d.ts +30 -0
  86. package/dist/core/chat/session-key.js +65 -0
  87. package/dist/core/chat/session-manager.d.ts +17 -0
  88. package/dist/core/chat/session-manager.js +23 -0
  89. package/dist/core/chat/text-chunker.d.ts +9 -0
  90. package/dist/core/chat/text-chunker.js +48 -0
  91. package/dist/core/chat/triage-router.d.ts +75 -0
  92. package/dist/core/chat/triage-router.js +142 -0
  93. package/dist/core/chat/types.d.ts +5 -0
  94. package/dist/core/chat/types.js +5 -0
  95. package/dist/core/config/defaults.d.ts +2 -0
  96. package/dist/core/config/defaults.js +38 -0
  97. package/dist/core/config/index.d.ts +6 -0
  98. package/dist/core/config/index.js +4 -0
  99. package/dist/core/config/interpolate.d.ts +5 -0
  100. package/dist/core/config/interpolate.js +27 -0
  101. package/dist/core/config/loader.d.ts +24 -0
  102. package/dist/core/config/loader.js +59 -0
  103. package/dist/core/config/schema.d.ts +5 -0
  104. package/dist/core/config/schema.js +119 -0
  105. package/dist/core/data/core-entity-contexts.d.ts +14 -0
  106. package/dist/core/data/core-entity-contexts.js +197 -0
  107. package/dist/core/data/core-migrations.d.ts +5 -0
  108. package/dist/core/data/core-migrations.js +45 -0
  109. package/dist/core/data/core-schema.d.ts +6 -0
  110. package/dist/core/data/core-schema.js +454 -0
  111. package/dist/core/data/data-store.d.ts +67 -0
  112. package/dist/core/data/data-store.js +218 -0
  113. package/dist/core/data/domain-entity-contexts.d.ts +29 -0
  114. package/dist/core/data/domain-entity-contexts.js +321 -0
  115. package/dist/core/data/domain-schema.d.ts +36 -0
  116. package/dist/core/data/domain-schema.js +323 -0
  117. package/dist/core/data/index.d.ts +7 -0
  118. package/dist/core/data/index.js +7 -0
  119. package/dist/core/data/types.d.ts +111 -0
  120. package/dist/core/data/types.js +1 -0
  121. package/dist/core/hooks/hook-bus.d.ts +18 -0
  122. package/dist/core/hooks/hook-bus.js +120 -0
  123. package/dist/core/hooks/index.d.ts +2 -0
  124. package/dist/core/hooks/index.js +1 -0
  125. package/dist/core/hooks/types.d.ts +19 -0
  126. package/dist/core/hooks/types.js +1 -0
  127. package/dist/core/index.d.ts +4 -0
  128. package/dist/core/index.js +4 -0
  129. package/dist/core/llm/auto-discovery.d.ts +11 -0
  130. package/dist/core/llm/auto-discovery.js +49 -0
  131. package/dist/core/llm/cost-tracker.d.ts +6 -0
  132. package/dist/core/llm/cost-tracker.js +38 -0
  133. package/dist/core/llm/index.d.ts +4 -0
  134. package/dist/core/llm/index.js +3 -0
  135. package/dist/core/llm/model-router.d.ts +25 -0
  136. package/dist/core/llm/model-router.js +49 -0
  137. package/dist/core/llm/provider-registry.d.ts +9 -0
  138. package/dist/core/llm/provider-registry.js +25 -0
  139. package/dist/core/llm/types.d.ts +2 -0
  140. package/dist/core/llm/types.js +2 -0
  141. package/dist/core/orchestrator/adapters/api-adapter.d.ts +34 -0
  142. package/dist/core/orchestrator/adapters/api-adapter.js +88 -0
  143. package/dist/core/orchestrator/adapters/cli-adapter.d.ts +22 -0
  144. package/dist/core/orchestrator/adapters/cli-adapter.js +69 -0
  145. package/dist/core/orchestrator/adapters/deterministic-adapter.d.ts +35 -0
  146. package/dist/core/orchestrator/adapters/deterministic-adapter.js +75 -0
  147. package/dist/core/orchestrator/adapters/env-whitelist.d.ts +4 -0
  148. package/dist/core/orchestrator/adapters/env-whitelist.js +27 -0
  149. package/dist/core/orchestrator/adapters/output-extractor.d.ts +11 -0
  150. package/dist/core/orchestrator/adapters/output-extractor.js +59 -0
  151. package/dist/core/orchestrator/adapters/process-manager.d.ts +15 -0
  152. package/dist/core/orchestrator/adapters/process-manager.js +26 -0
  153. package/dist/core/orchestrator/adapters/tool-loop.d.ts +22 -0
  154. package/dist/core/orchestrator/adapters/tool-loop.js +66 -0
  155. package/dist/core/orchestrator/agent-registry.d.ts +31 -0
  156. package/dist/core/orchestrator/agent-registry.js +135 -0
  157. package/dist/core/orchestrator/budget-controller.d.ts +19 -0
  158. package/dist/core/orchestrator/budget-controller.js +73 -0
  159. package/dist/core/orchestrator/chain-guard.d.ts +14 -0
  160. package/dist/core/orchestrator/chain-guard.js +23 -0
  161. package/dist/core/orchestrator/circuit-breaker.d.ts +65 -0
  162. package/dist/core/orchestrator/circuit-breaker.js +159 -0
  163. package/dist/core/orchestrator/claude-stream-parser.d.ts +31 -0
  164. package/dist/core/orchestrator/claude-stream-parser.js +99 -0
  165. package/dist/core/orchestrator/config-revisions.d.ts +6 -0
  166. package/dist/core/orchestrator/config-revisions.js +17 -0
  167. package/dist/core/orchestrator/dependency-resolver.d.ts +20 -0
  168. package/dist/core/orchestrator/dependency-resolver.js +78 -0
  169. package/dist/core/orchestrator/governance-gate.d.ts +110 -0
  170. package/dist/core/orchestrator/governance-gate.js +170 -0
  171. package/dist/core/orchestrator/learning-pipeline.d.ts +109 -0
  172. package/dist/core/orchestrator/learning-pipeline.js +249 -0
  173. package/dist/core/orchestrator/loop-detector.d.ts +51 -0
  174. package/dist/core/orchestrator/loop-detector.js +133 -0
  175. package/dist/core/orchestrator/ndjson-logger.d.ts +6 -0
  176. package/dist/core/orchestrator/ndjson-logger.js +18 -0
  177. package/dist/core/orchestrator/permission-relay.d.ts +72 -0
  178. package/dist/core/orchestrator/permission-relay.js +164 -0
  179. package/dist/core/orchestrator/run-manager.d.ts +31 -0
  180. package/dist/core/orchestrator/run-manager.js +178 -0
  181. package/dist/core/orchestrator/scheduler.d.ts +70 -0
  182. package/dist/core/orchestrator/scheduler.js +198 -0
  183. package/dist/core/orchestrator/secret-store.d.ts +57 -0
  184. package/dist/core/orchestrator/secret-store.js +171 -0
  185. package/dist/core/orchestrator/session-manager.d.ts +13 -0
  186. package/dist/core/orchestrator/session-manager.js +66 -0
  187. package/dist/core/orchestrator/task-queue.d.ts +34 -0
  188. package/dist/core/orchestrator/task-queue.js +83 -0
  189. package/dist/core/orchestrator/template-interpolate.d.ts +5 -0
  190. package/dist/core/orchestrator/template-interpolate.js +18 -0
  191. package/dist/core/orchestrator/user-registry.d.ts +47 -0
  192. package/dist/core/orchestrator/user-registry.js +76 -0
  193. package/dist/core/orchestrator/wakeup-queue.d.ts +9 -0
  194. package/dist/core/orchestrator/wakeup-queue.js +45 -0
  195. package/dist/core/orchestrator/workflow-engine.d.ts +47 -0
  196. package/dist/core/orchestrator/workflow-engine.js +204 -0
  197. package/dist/core/security/audit.d.ts +20 -0
  198. package/dist/core/security/audit.js +33 -0
  199. package/dist/core/security/column-validator.d.ts +20 -0
  200. package/dist/core/security/column-validator.js +37 -0
  201. package/dist/core/security/index.d.ts +5 -0
  202. package/dist/core/security/index.js +5 -0
  203. package/dist/core/security/process-env.d.ts +13 -0
  204. package/dist/core/security/process-env.js +49 -0
  205. package/dist/core/security/sanitizer.d.ts +11 -0
  206. package/dist/core/security/sanitizer.js +39 -0
  207. package/dist/core/security/types.d.ts +11 -0
  208. package/dist/core/security/types.js +1 -0
  209. package/dist/core/update/auto-update.d.ts +21 -0
  210. package/dist/core/update/auto-update.js +102 -0
  211. package/dist/core/update/backup-manager.d.ts +7 -0
  212. package/dist/core/update/backup-manager.js +24 -0
  213. package/dist/core/update/index.d.ts +8 -0
  214. package/dist/core/update/index.js +6 -0
  215. package/dist/core/update/migration-hooks.d.ts +11 -0
  216. package/dist/core/update/migration-hooks.js +10 -0
  217. package/dist/core/update/types.d.ts +11 -0
  218. package/dist/core/update/types.js +1 -0
  219. package/dist/core/update/update-checker.d.ts +11 -0
  220. package/dist/core/update/update-checker.js +63 -0
  221. package/dist/core/update/update-manager.d.ts +25 -0
  222. package/dist/core/update/update-manager.js +101 -0
  223. package/dist/core/update/version-utils.d.ts +6 -0
  224. package/dist/core/update/version-utils.js +34 -0
  225. package/dist/gmail-connector-2FVYTQJH.js +6 -0
  226. package/dist/gmail-connector-MNUBRNFM.js +6 -0
  227. package/dist/gmail-connector-PS2VLGNE.js +6 -0
  228. package/dist/gmail-connector-ULSMN6X2.js +6 -0
  229. package/dist/gmail-connector-URRFX6A3.js +6 -0
  230. package/dist/inbound-AFBUPSPG.js +10 -0
  231. package/dist/inbound-AFOHYNUY.js +6 -0
  232. package/dist/inbound-CGIXRXGC.js +8 -0
  233. package/dist/inbound-MCOLRH6U.js +10 -0
  234. package/dist/inbound-SNEMBLGA.js +6 -0
  235. package/dist/inbound-ZJHAYVMF.js +10 -0
  236. package/dist/index.d.ts +16 -3
  237. package/dist/index.js +67 -28
  238. package/dist/provider-qqJYv9nv.d.ts +75 -0
  239. package/dist/providers/anthropic/models.d.ts +2 -0
  240. package/dist/providers/anthropic/models.js +29 -0
  241. package/dist/providers/anthropic/provider.d.ts +13 -0
  242. package/dist/providers/anthropic/provider.js +119 -0
  243. package/dist/providers/anthropic/tool-converter.d.ts +10 -0
  244. package/dist/providers/anthropic/tool-converter.js +7 -0
  245. package/dist/providers/ollama/provider.d.ts +17 -0
  246. package/dist/providers/ollama/provider.js +185 -0
  247. package/dist/providers/openai/models.d.ts +2 -0
  248. package/dist/providers/openai/models.js +29 -0
  249. package/dist/providers/openai/provider.d.ts +13 -0
  250. package/dist/providers/openai/provider.js +163 -0
  251. package/dist/providers/openai/tool-converter.d.ts +10 -0
  252. package/dist/providers/openai/tool-converter.js +10 -0
  253. package/dist/shared/constants.d.ts +50 -0
  254. package/dist/shared/constants.js +64 -0
  255. package/dist/shared/index.d.ts +14 -0
  256. package/dist/shared/index.js +14 -0
  257. package/dist/shared/types/agent.d.ts +36 -0
  258. package/dist/shared/types/agent.js +2 -0
  259. package/dist/shared/types/channel.d.ts +70 -0
  260. package/dist/shared/types/channel.js +2 -0
  261. package/dist/shared/types/config.d.ts +111 -0
  262. package/dist/shared/types/config.js +2 -0
  263. package/dist/shared/types/connector.d.ts +77 -0
  264. package/dist/shared/types/connector.js +2 -0
  265. package/dist/shared/types/execution.d.ts +29 -0
  266. package/dist/shared/types/execution.js +2 -0
  267. package/dist/shared/types/provider.d.ts +73 -0
  268. package/dist/shared/types/provider.js +2 -0
  269. package/dist/shared/types/task.d.ts +47 -0
  270. package/dist/shared/types/task.js +2 -0
  271. package/dist/shared/types/workflow.d.ts +39 -0
  272. package/dist/shared/types/workflow.js +2 -0
  273. package/dist/shared/utils.d.ts +6 -0
  274. package/dist/shared/utils.js +13 -0
  275. package/dist/update-check.d.ts +5 -0
  276. package/dist/update-check.js +56 -0
  277. package/package.json +100 -100
@@ -0,0 +1,75 @@
1
+ /** LLM provider types — Story 1.5 / 2.1 */
2
+ interface ToolDefinition {
3
+ name: string;
4
+ description: string;
5
+ parameters: Record<string, unknown>;
6
+ }
7
+ interface ChatMessage {
8
+ role: "user" | "assistant" | "system";
9
+ content: string | ContentBlock[];
10
+ }
11
+ type ContentBlock = {
12
+ type: "text";
13
+ text: string;
14
+ } | {
15
+ type: "tool_use";
16
+ id: string;
17
+ name: string;
18
+ input: unknown;
19
+ } | {
20
+ type: "tool_result";
21
+ tool_use_id: string;
22
+ content: string;
23
+ };
24
+ interface ChatParams {
25
+ messages: ChatMessage[];
26
+ system?: string;
27
+ tools?: ToolDefinition[];
28
+ maxTokens?: number;
29
+ temperature?: number;
30
+ model: string;
31
+ abortSignal?: AbortSignal;
32
+ }
33
+ interface TokenUsage {
34
+ inputTokens: number;
35
+ outputTokens: number;
36
+ cacheReadTokens?: number;
37
+ cacheWriteTokens?: number;
38
+ }
39
+ interface ChatResult {
40
+ content: string;
41
+ toolUses?: ToolUse[];
42
+ usage: TokenUsage;
43
+ model: string;
44
+ stopReason: "end_turn" | "tool_use" | "max_tokens" | "stop_sequence";
45
+ }
46
+ interface ToolUse {
47
+ id: string;
48
+ name: string;
49
+ input: unknown;
50
+ }
51
+ interface ModelInfo {
52
+ id: string;
53
+ displayName: string;
54
+ contextWindow: number;
55
+ maxOutputTokens: number;
56
+ capabilities: Array<"chat" | "tools" | "vision" | "streaming">;
57
+ /** Cost in micro-cents per 1M tokens */
58
+ inputCostPerMToken?: number;
59
+ outputCostPerMToken?: number;
60
+ }
61
+ interface ResolvedModel {
62
+ provider: string;
63
+ model: string;
64
+ }
65
+ interface LLMProvider {
66
+ id: string;
67
+ displayName: string;
68
+ models: ModelInfo[];
69
+ chat(params: ChatParams): Promise<ChatResult>;
70
+ chatStream(params: ChatParams): AsyncGenerator<string, ChatResult, unknown>;
71
+ /** Convert ToolDefinition[] to provider-native format */
72
+ serializeTools(tools: ToolDefinition[]): unknown;
73
+ }
74
+
75
+ export type { ChatMessage as C, LLMProvider as L, ModelInfo as M, ResolvedModel as R, TokenUsage as T, ChatParams as a, ChatResult as b, ContentBlock as c, ToolDefinition as d, ToolUse as e };
@@ -0,0 +1,2 @@
1
+ import type { ModelInfo } from "../../shared/index.js";
2
+ export declare const MODELS: ModelInfo[];
@@ -0,0 +1,29 @@
1
+ export const MODELS = [
2
+ {
3
+ id: 'claude-opus-4-6',
4
+ displayName: 'Claude Opus 4.6',
5
+ contextWindow: 200000,
6
+ maxOutputTokens: 32000,
7
+ inputCostPerMToken: 15,
8
+ outputCostPerMToken: 75,
9
+ capabilities: ['chat', 'tools', 'vision', 'streaming'],
10
+ },
11
+ {
12
+ id: 'claude-sonnet-4-6',
13
+ displayName: 'Claude Sonnet 4.6',
14
+ contextWindow: 200000,
15
+ maxOutputTokens: 16000,
16
+ inputCostPerMToken: 3,
17
+ outputCostPerMToken: 15,
18
+ capabilities: ['chat', 'tools', 'vision', 'streaming'],
19
+ },
20
+ {
21
+ id: 'claude-haiku-4-5',
22
+ displayName: 'Claude Haiku 4.5',
23
+ contextWindow: 200000,
24
+ maxOutputTokens: 8192,
25
+ inputCostPerMToken: 0.8,
26
+ outputCostPerMToken: 4,
27
+ capabilities: ['chat', 'tools', 'vision', 'streaming'],
28
+ },
29
+ ];
@@ -0,0 +1,13 @@
1
+ import type { LLMProvider, ChatParams, ChatResult, ModelInfo, ToolDefinition } from "../../shared/index.js";
2
+ export declare class AnthropicProvider implements LLMProvider {
3
+ readonly id = "anthropic";
4
+ readonly displayName = "Anthropic";
5
+ readonly models: ModelInfo[];
6
+ private client;
7
+ constructor({ apiKey }: {
8
+ apiKey: string;
9
+ });
10
+ serializeTools(tools: ToolDefinition[]): unknown;
11
+ chat(params: ChatParams): Promise<ChatResult>;
12
+ chatStream(params: ChatParams): AsyncGenerator<string, ChatResult, unknown>;
13
+ }
@@ -0,0 +1,119 @@
1
+ import Anthropic from '@anthropic-ai/sdk';
2
+ import { MODELS } from './models.js';
3
+ import { convertTools } from './tool-converter.js';
4
+ export class AnthropicProvider {
5
+ id = 'anthropic';
6
+ displayName = 'Anthropic';
7
+ models = MODELS;
8
+ client;
9
+ constructor({ apiKey }) {
10
+ this.client = new Anthropic({ apiKey });
11
+ }
12
+ serializeTools(tools) {
13
+ return convertTools(tools);
14
+ }
15
+ async chat(params) {
16
+ const { messages, system, tools, maxTokens, temperature, model, abortSignal } = params;
17
+ const anthropicMessages = messages
18
+ .filter((m) => m.role !== 'system')
19
+ .map((m) => ({
20
+ role: m.role,
21
+ content: typeof m.content === 'string' ? m.content : m.content,
22
+ }));
23
+ const response = await this.client.messages.create({
24
+ model,
25
+ max_tokens: maxTokens ?? 4096,
26
+ messages: anthropicMessages,
27
+ ...(system ? { system } : {}),
28
+ ...(tools && tools.length > 0 ? { tools: convertTools(tools) } : {}),
29
+ ...(temperature !== undefined ? { temperature } : {}),
30
+ }, { signal: abortSignal });
31
+ let content = '';
32
+ const toolUses = [];
33
+ for (const block of response.content) {
34
+ if (block.type === 'text') {
35
+ content += block.text;
36
+ }
37
+ else if (block.type === 'tool_use') {
38
+ toolUses.push({
39
+ id: block.id,
40
+ name: block.name,
41
+ input: block.input,
42
+ });
43
+ }
44
+ }
45
+ const stopReason = mapStopReason(response.stop_reason);
46
+ return {
47
+ content,
48
+ toolUses: toolUses.length > 0 ? toolUses : undefined,
49
+ usage: {
50
+ inputTokens: response.usage.input_tokens,
51
+ outputTokens: response.usage.output_tokens,
52
+ },
53
+ model: response.model,
54
+ stopReason,
55
+ };
56
+ }
57
+ async *chatStream(params) {
58
+ const { messages, system, tools, maxTokens, temperature, model, abortSignal } = params;
59
+ const anthropicMessages = messages
60
+ .filter((m) => m.role !== 'system')
61
+ .map((m) => ({
62
+ role: m.role,
63
+ content: typeof m.content === 'string' ? m.content : m.content,
64
+ }));
65
+ const stream = this.client.messages.stream({
66
+ model,
67
+ max_tokens: maxTokens ?? 4096,
68
+ messages: anthropicMessages,
69
+ ...(system ? { system } : {}),
70
+ ...(tools && tools.length > 0 ? { tools: convertTools(tools) } : {}),
71
+ ...(temperature !== undefined ? { temperature } : {}),
72
+ }, { signal: abortSignal });
73
+ for await (const event of stream) {
74
+ if (event.type === 'content_block_delta' &&
75
+ event.delta.type === 'text_delta') {
76
+ yield event.delta.text;
77
+ }
78
+ }
79
+ const finalMessage = await stream.finalMessage();
80
+ let content = '';
81
+ const toolUses = [];
82
+ for (const block of finalMessage.content) {
83
+ if (block.type === 'text') {
84
+ content += block.text;
85
+ }
86
+ else if (block.type === 'tool_use') {
87
+ toolUses.push({
88
+ id: block.id,
89
+ name: block.name,
90
+ input: block.input,
91
+ });
92
+ }
93
+ }
94
+ return {
95
+ content,
96
+ toolUses: toolUses.length > 0 ? toolUses : undefined,
97
+ usage: {
98
+ inputTokens: finalMessage.usage.input_tokens,
99
+ outputTokens: finalMessage.usage.output_tokens,
100
+ },
101
+ model: finalMessage.model,
102
+ stopReason: mapStopReason(finalMessage.stop_reason),
103
+ };
104
+ }
105
+ }
106
+ function mapStopReason(reason) {
107
+ switch (reason) {
108
+ case 'end_turn':
109
+ return 'end_turn';
110
+ case 'tool_use':
111
+ return 'tool_use';
112
+ case 'max_tokens':
113
+ return 'max_tokens';
114
+ case 'stop_sequence':
115
+ return 'stop_sequence';
116
+ default:
117
+ return 'end_turn';
118
+ }
119
+ }
@@ -0,0 +1,10 @@
1
+ import type { ToolDefinition } from "../../shared/index.js";
2
+ export interface AnthropicTool {
3
+ name: string;
4
+ description: string;
5
+ input_schema: {
6
+ type: 'object';
7
+ [key: string]: unknown;
8
+ };
9
+ }
10
+ export declare function convertTools(tools: ToolDefinition[]): AnthropicTool[];
@@ -0,0 +1,7 @@
1
+ export function convertTools(tools) {
2
+ return tools.map((tool) => ({
3
+ name: tool.name,
4
+ description: tool.description,
5
+ input_schema: { type: 'object', ...tool.parameters },
6
+ }));
7
+ }
@@ -0,0 +1,17 @@
1
+ import type { LLMProvider, ChatParams, ChatResult, ModelInfo, ToolDefinition } from "../../shared/index.js";
2
+ export declare class OllamaProvider implements LLMProvider {
3
+ readonly id = "ollama";
4
+ readonly displayName = "Ollama";
5
+ private baseUrl;
6
+ private cachedModels;
7
+ private cacheTimestamp;
8
+ private readonly cacheTtlMs;
9
+ constructor({ baseUrl }?: {
10
+ baseUrl?: string;
11
+ });
12
+ get models(): ModelInfo[];
13
+ serializeTools(_tools: ToolDefinition[]): unknown;
14
+ getModels(): Promise<ModelInfo[]>;
15
+ chat(params: ChatParams): Promise<ChatResult>;
16
+ chatStream(params: ChatParams): AsyncGenerator<string, ChatResult, unknown>;
17
+ }
@@ -0,0 +1,185 @@
1
+ export class OllamaProvider {
2
+ id = 'ollama';
3
+ displayName = 'Ollama';
4
+ baseUrl;
5
+ cachedModels = [];
6
+ cacheTimestamp = 0;
7
+ cacheTtlMs = 5 * 60 * 1000; // 5 minutes
8
+ constructor({ baseUrl = 'http://localhost:11434' } = {}) {
9
+ this.baseUrl = baseUrl.replace(/\/$/, '');
10
+ }
11
+ get models() {
12
+ return this.cachedModels;
13
+ }
14
+ serializeTools(_tools) {
15
+ // Ollama uses a similar format to OpenAI for tools
16
+ return _tools.map((tool) => ({
17
+ type: 'function',
18
+ function: {
19
+ name: tool.name,
20
+ description: tool.description,
21
+ parameters: tool.parameters,
22
+ },
23
+ }));
24
+ }
25
+ async getModels() {
26
+ const now = Date.now();
27
+ if (this.cachedModels.length > 0 && now - this.cacheTimestamp < this.cacheTtlMs) {
28
+ return this.cachedModels;
29
+ }
30
+ try {
31
+ const response = await fetch(`${this.baseUrl}/api/tags`);
32
+ if (!response.ok) {
33
+ return [];
34
+ }
35
+ const data = (await response.json());
36
+ this.cachedModels = data.models.map((m) => ({
37
+ id: m.name,
38
+ displayName: m.name,
39
+ contextWindow: 128000,
40
+ maxOutputTokens: 4096,
41
+ capabilities: ['chat', 'streaming'],
42
+ }));
43
+ this.cacheTimestamp = now;
44
+ return this.cachedModels;
45
+ }
46
+ catch {
47
+ return [];
48
+ }
49
+ }
50
+ async chat(params) {
51
+ const { messages, system, model, maxTokens, temperature } = params;
52
+ const ollamaMessages = [];
53
+ if (system) {
54
+ ollamaMessages.push({ role: 'system', content: system });
55
+ }
56
+ for (const msg of messages) {
57
+ const content = typeof msg.content === 'string'
58
+ ? msg.content
59
+ : msg.content
60
+ .filter((b) => b.type === 'text')
61
+ .map((b) => (b.type === 'text' ? b.text : ''))
62
+ .join('');
63
+ ollamaMessages.push({
64
+ role: msg.role,
65
+ content,
66
+ });
67
+ }
68
+ const body = {
69
+ model,
70
+ messages: ollamaMessages,
71
+ stream: false,
72
+ };
73
+ if (maxTokens !== undefined) {
74
+ body['options'] = { ...(body['options'] ?? {}), num_predict: maxTokens };
75
+ }
76
+ if (temperature !== undefined) {
77
+ body['options'] = { ...(body['options'] ?? {}), temperature };
78
+ }
79
+ const response = await fetch(`${this.baseUrl}/api/chat`, {
80
+ method: 'POST',
81
+ headers: { 'Content-Type': 'application/json' },
82
+ body: JSON.stringify(body),
83
+ signal: params.abortSignal,
84
+ });
85
+ if (!response.ok) {
86
+ throw new Error(`Ollama API error: ${response.status} ${response.statusText}`);
87
+ }
88
+ const data = (await response.json());
89
+ return {
90
+ content: data.message.content,
91
+ usage: {
92
+ inputTokens: data.prompt_eval_count ?? 0,
93
+ outputTokens: data.eval_count ?? 0,
94
+ },
95
+ model: data.model,
96
+ stopReason: 'end_turn',
97
+ };
98
+ }
99
+ async *chatStream(params) {
100
+ const { messages, system, model, maxTokens, temperature } = params;
101
+ const ollamaMessages = [];
102
+ if (system) {
103
+ ollamaMessages.push({ role: 'system', content: system });
104
+ }
105
+ for (const msg of messages) {
106
+ const content = typeof msg.content === 'string'
107
+ ? msg.content
108
+ : msg.content
109
+ .filter((b) => b.type === 'text')
110
+ .map((b) => (b.type === 'text' ? b.text : ''))
111
+ .join('');
112
+ ollamaMessages.push({
113
+ role: msg.role,
114
+ content,
115
+ });
116
+ }
117
+ const body = {
118
+ model,
119
+ messages: ollamaMessages,
120
+ stream: true,
121
+ };
122
+ if (maxTokens !== undefined) {
123
+ body['options'] = { ...(body['options'] ?? {}), num_predict: maxTokens };
124
+ }
125
+ if (temperature !== undefined) {
126
+ body['options'] = { ...(body['options'] ?? {}), temperature };
127
+ }
128
+ const response = await fetch(`${this.baseUrl}/api/chat`, {
129
+ method: 'POST',
130
+ headers: { 'Content-Type': 'application/json' },
131
+ body: JSON.stringify(body),
132
+ signal: params.abortSignal,
133
+ });
134
+ if (!response.ok) {
135
+ throw new Error(`Ollama API error: ${response.status} ${response.statusText}`);
136
+ }
137
+ if (!response.body) {
138
+ throw new Error('No response body for streaming');
139
+ }
140
+ const reader = response.body.getReader();
141
+ const decoder = new TextDecoder();
142
+ let buffer = '';
143
+ let fullContent = '';
144
+ let lastChunk;
145
+ try {
146
+ while (true) {
147
+ const { done, value } = await reader.read();
148
+ if (done)
149
+ break;
150
+ buffer += decoder.decode(value, { stream: true });
151
+ const lines = buffer.split('\n');
152
+ buffer = lines.pop() ?? '';
153
+ for (const line of lines) {
154
+ if (!line.trim())
155
+ continue;
156
+ try {
157
+ const chunk = JSON.parse(line);
158
+ if (chunk.message?.content) {
159
+ fullContent += chunk.message.content;
160
+ yield chunk.message.content;
161
+ }
162
+ if (chunk.done) {
163
+ lastChunk = chunk;
164
+ }
165
+ }
166
+ catch {
167
+ // skip malformed lines
168
+ }
169
+ }
170
+ }
171
+ }
172
+ finally {
173
+ reader.releaseLock();
174
+ }
175
+ return {
176
+ content: fullContent,
177
+ usage: {
178
+ inputTokens: lastChunk?.prompt_eval_count ?? 0,
179
+ outputTokens: lastChunk?.eval_count ?? 0,
180
+ },
181
+ model,
182
+ stopReason: 'end_turn',
183
+ };
184
+ }
185
+ }
@@ -0,0 +1,2 @@
1
+ import type { ModelInfo } from "../../shared/index.js";
2
+ export declare const MODELS: ModelInfo[];
@@ -0,0 +1,29 @@
1
+ export const MODELS = [
2
+ {
3
+ id: 'gpt-4o',
4
+ displayName: 'GPT-4o',
5
+ contextWindow: 128000,
6
+ maxOutputTokens: 16384,
7
+ inputCostPerMToken: 2.5,
8
+ outputCostPerMToken: 10,
9
+ capabilities: ['chat', 'tools', 'vision', 'streaming'],
10
+ },
11
+ {
12
+ id: 'gpt-4o-mini',
13
+ displayName: 'GPT-4o Mini',
14
+ contextWindow: 128000,
15
+ maxOutputTokens: 16384,
16
+ inputCostPerMToken: 0.15,
17
+ outputCostPerMToken: 0.6,
18
+ capabilities: ['chat', 'tools', 'vision', 'streaming'],
19
+ },
20
+ {
21
+ id: 'o3-mini',
22
+ displayName: 'o3 Mini',
23
+ contextWindow: 200000,
24
+ maxOutputTokens: 100000,
25
+ inputCostPerMToken: 1.1,
26
+ outputCostPerMToken: 4.4,
27
+ capabilities: ['chat', 'tools', 'streaming'],
28
+ },
29
+ ];
@@ -0,0 +1,13 @@
1
+ import type { LLMProvider, ChatParams, ChatResult, ModelInfo, ToolDefinition } from "../../shared/index.js";
2
+ export declare class OpenAIProvider implements LLMProvider {
3
+ readonly id = "openai";
4
+ readonly displayName = "OpenAI";
5
+ readonly models: ModelInfo[];
6
+ private client;
7
+ constructor({ apiKey }: {
8
+ apiKey: string;
9
+ });
10
+ serializeTools(tools: ToolDefinition[]): unknown;
11
+ chat(params: ChatParams): Promise<ChatResult>;
12
+ chatStream(params: ChatParams): AsyncGenerator<string, ChatResult, unknown>;
13
+ }
@@ -0,0 +1,163 @@
1
+ import OpenAI from 'openai';
2
+ import { MODELS } from './models.js';
3
+ import { convertTools } from './tool-converter.js';
4
+ export class OpenAIProvider {
5
+ id = 'openai';
6
+ displayName = 'OpenAI';
7
+ models = MODELS;
8
+ client;
9
+ constructor({ apiKey }) {
10
+ this.client = new OpenAI({ apiKey });
11
+ }
12
+ serializeTools(tools) {
13
+ return convertTools(tools);
14
+ }
15
+ async chat(params) {
16
+ const { messages, system, tools, maxTokens, temperature, model, abortSignal } = params;
17
+ const openaiMessages = [];
18
+ if (system) {
19
+ openaiMessages.push({ role: 'system', content: system });
20
+ }
21
+ for (const msg of messages) {
22
+ if (typeof msg.content === 'string') {
23
+ openaiMessages.push({
24
+ role: msg.role,
25
+ content: msg.content,
26
+ });
27
+ }
28
+ else {
29
+ // content blocks — convert text blocks to string
30
+ const textContent = msg.content
31
+ .filter((b) => b.type === 'text')
32
+ .map((b) => (b.type === 'text' ? b.text : ''))
33
+ .join('');
34
+ openaiMessages.push({
35
+ role: msg.role,
36
+ content: textContent,
37
+ });
38
+ }
39
+ }
40
+ const response = await this.client.chat.completions.create({
41
+ model,
42
+ messages: openaiMessages,
43
+ ...(maxTokens !== undefined ? { max_tokens: maxTokens } : {}),
44
+ ...(temperature !== undefined ? { temperature } : {}),
45
+ ...(tools && tools.length > 0 ? { tools: convertTools(tools) } : {}),
46
+ }, { signal: abortSignal });
47
+ const choice = response.choices[0];
48
+ if (!choice) {
49
+ throw new Error('No choices returned from OpenAI');
50
+ }
51
+ const messageContent = choice.message.content ?? '';
52
+ const toolUses = [];
53
+ if (choice.message.tool_calls) {
54
+ for (const tc of choice.message.tool_calls) {
55
+ let input;
56
+ try {
57
+ input = JSON.parse(tc.function.arguments);
58
+ }
59
+ catch {
60
+ input = tc.function.arguments;
61
+ }
62
+ toolUses.push({
63
+ id: tc.id,
64
+ name: tc.function.name,
65
+ input,
66
+ });
67
+ }
68
+ }
69
+ const stopReason = mapFinishReason(choice.finish_reason);
70
+ return {
71
+ content: messageContent,
72
+ toolUses: toolUses.length > 0 ? toolUses : undefined,
73
+ usage: {
74
+ inputTokens: response.usage?.prompt_tokens ?? 0,
75
+ outputTokens: response.usage?.completion_tokens ?? 0,
76
+ },
77
+ model: response.model,
78
+ stopReason,
79
+ };
80
+ }
81
+ async *chatStream(params) {
82
+ const { messages, system, tools, maxTokens, temperature, model, abortSignal } = params;
83
+ const openaiMessages = [];
84
+ if (system) {
85
+ openaiMessages.push({ role: 'system', content: system });
86
+ }
87
+ for (const msg of messages) {
88
+ if (typeof msg.content === 'string') {
89
+ openaiMessages.push({
90
+ role: msg.role,
91
+ content: msg.content,
92
+ });
93
+ }
94
+ else {
95
+ const textContent = msg.content
96
+ .filter((b) => b.type === 'text')
97
+ .map((b) => (b.type === 'text' ? b.text : ''))
98
+ .join('');
99
+ openaiMessages.push({
100
+ role: msg.role,
101
+ content: textContent,
102
+ });
103
+ }
104
+ }
105
+ const stream = this.client.chat.completions.stream({
106
+ model,
107
+ messages: openaiMessages,
108
+ ...(maxTokens !== undefined ? { max_tokens: maxTokens } : {}),
109
+ ...(temperature !== undefined ? { temperature } : {}),
110
+ ...(tools && tools.length > 0 ? { tools: convertTools(tools) } : {}),
111
+ }, { signal: abortSignal });
112
+ let accumulatedContent = '';
113
+ for await (const chunk of stream) {
114
+ const delta = chunk.choices[0]?.delta?.content;
115
+ if (delta) {
116
+ accumulatedContent += delta;
117
+ yield delta;
118
+ }
119
+ }
120
+ const finalCompletion = await stream.finalChatCompletion();
121
+ const choice = finalCompletion.choices[0];
122
+ if (!choice) {
123
+ throw new Error('No choices in final completion');
124
+ }
125
+ const toolUses = [];
126
+ if (choice.message.tool_calls) {
127
+ for (const tc of choice.message.tool_calls) {
128
+ let input;
129
+ try {
130
+ input = JSON.parse(tc.function.arguments);
131
+ }
132
+ catch {
133
+ input = tc.function.arguments;
134
+ }
135
+ toolUses.push({ id: tc.id, name: tc.function.name, input });
136
+ }
137
+ }
138
+ return {
139
+ content: choice.message.content ?? accumulatedContent,
140
+ toolUses: toolUses.length > 0 ? toolUses : undefined,
141
+ usage: {
142
+ inputTokens: finalCompletion.usage?.prompt_tokens ?? 0,
143
+ outputTokens: finalCompletion.usage?.completion_tokens ?? 0,
144
+ },
145
+ model: finalCompletion.model,
146
+ stopReason: mapFinishReason(choice.finish_reason),
147
+ };
148
+ }
149
+ }
150
+ function mapFinishReason(reason) {
151
+ switch (reason) {
152
+ case 'stop':
153
+ return 'end_turn';
154
+ case 'tool_calls':
155
+ return 'tool_use';
156
+ case 'length':
157
+ return 'max_tokens';
158
+ case 'content_filter':
159
+ return 'stop_sequence';
160
+ default:
161
+ return 'end_turn';
162
+ }
163
+ }
@@ -0,0 +1,10 @@
1
+ import type { ToolDefinition } from "../../shared/index.js";
2
+ export interface OpenAITool {
3
+ type: 'function';
4
+ function: {
5
+ name: string;
6
+ description: string;
7
+ parameters: Record<string, unknown>;
8
+ };
9
+ }
10
+ export declare function convertTools(tools: ToolDefinition[]): OpenAITool[];