micro-models-agent 0.46.1 → 0.46.3

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 (218) hide show
  1. package/README.md +312 -312
  2. package/dist/main.js +724 -530
  3. package/dist/modules/browser/bridge-server.mjs +202 -202
  4. package/dist/skills/builtin/git.md +36 -36
  5. package/dist/skills/builtin/typescript.md +35 -35
  6. package/package.json +48 -48
  7. package/dist/cli/commands.js +0 -323
  8. package/dist/cli/completer.js +0 -167
  9. package/dist/cli/index.js +0 -2
  10. package/dist/cli/main.js +0 -165
  11. package/dist/cli/plugin-commands.js +0 -36
  12. package/dist/cli/repl-commands.js +0 -661
  13. package/dist/cli/repl.js +0 -616
  14. package/dist/cli/run-result.js +0 -22
  15. package/dist/cli/security-commands.js +0 -164
  16. package/dist/cli/setup.js +0 -231
  17. package/dist/config/config.js +0 -249
  18. package/dist/config/defaults.js +0 -124
  19. package/dist/config/experts.js +0 -15
  20. package/dist/config/index.js +0 -3
  21. package/dist/config/security.js +0 -193
  22. package/dist/config/types.js +0 -1
  23. package/dist/core/agent-moe.js +0 -102
  24. package/dist/core/agent.js +0 -886
  25. package/dist/core/bootstrap.js +0 -404
  26. package/dist/core/index.js +0 -2
  27. package/dist/core/prompt-builder.js +0 -76
  28. package/dist/core/session-logger.js +0 -197
  29. package/dist/core/types.js +0 -1
  30. package/dist/core/version.js +0 -24
  31. package/dist/core/workspace.js +0 -76
  32. package/dist/i18n/en.json +0 -598
  33. package/dist/i18n/index.js +0 -46
  34. package/dist/i18n/ru.json +0 -598
  35. package/dist/index.js +0 -22
  36. package/dist/llm/image-utils.js +0 -143
  37. package/dist/llm/index.js +0 -4
  38. package/dist/llm/model-loader.js +0 -78
  39. package/dist/llm/openai-compat.js +0 -359
  40. package/dist/llm/orchestrator.js +0 -198
  41. package/dist/llm/provider.js +0 -10
  42. package/dist/llm/response.js +0 -39
  43. package/dist/llm/token-counter.js +0 -39
  44. package/dist/llm/types.js +0 -1
  45. package/dist/logger/app-logger.js +0 -143
  46. package/dist/logger/file-log.js +0 -151
  47. package/dist/logger/index.js +0 -1
  48. package/dist/migration/backup.js +0 -45
  49. package/dist/migration/detect.js +0 -50
  50. package/dist/migration/index.js +0 -2
  51. package/dist/modules/artifacts/store.js +0 -61
  52. package/dist/modules/browser/actions.js +0 -76
  53. package/dist/modules/browser/bridge-client.js +0 -199
  54. package/dist/modules/browser/bridge-path.js +0 -10
  55. package/dist/modules/browser/cookie-store.js +0 -24
  56. package/dist/modules/browser/driver.js +0 -136
  57. package/dist/modules/browser/index.js +0 -7
  58. package/dist/modules/browser/module.js +0 -29
  59. package/dist/modules/browser/session.js +0 -338
  60. package/dist/modules/browser/snapshot.js +0 -148
  61. package/dist/modules/browser/types.js +0 -12
  62. package/dist/modules/certification/cli.js +0 -174
  63. package/dist/modules/certification/fact-checker.js +0 -82
  64. package/dist/modules/certification/loader.js +0 -105
  65. package/dist/modules/certification/manifest.js +0 -50
  66. package/dist/modules/certification/runner.js +0 -159
  67. package/dist/modules/certification/scenarios.js +0 -124
  68. package/dist/modules/certification/types.js +0 -1
  69. package/dist/modules/context/chunk-query.js +0 -100
  70. package/dist/modules/context/fact-extractor.js +0 -162
  71. package/dist/modules/context/history.js +0 -15
  72. package/dist/modules/context/index.js +0 -1
  73. package/dist/modules/context/manager.js +0 -423
  74. package/dist/modules/execution/audit-runners.js +0 -152
  75. package/dist/modules/execution/auditor.js +0 -218
  76. package/dist/modules/execution/execution-plugin.js +0 -272
  77. package/dist/modules/execution/index.js +0 -8
  78. package/dist/modules/execution/module.js +0 -436
  79. package/dist/modules/execution/moe-executor.js +0 -291
  80. package/dist/modules/execution/plan-coverage.js +0 -68
  81. package/dist/modules/execution/plan-persister.js +0 -46
  82. package/dist/modules/execution/plan-store.js +0 -157
  83. package/dist/modules/execution/plan-tool.js +0 -508
  84. package/dist/modules/execution/plan-validator.js +0 -153
  85. package/dist/modules/execution/planner.js +0 -90
  86. package/dist/modules/execution/stuck-detector.js +0 -510
  87. package/dist/modules/execution/tracker.js +0 -67
  88. package/dist/modules/execution/types.js +0 -1
  89. package/dist/modules/execution/verifier.js +0 -222
  90. package/dist/modules/execution/windows-commands.js +0 -41
  91. package/dist/modules/hallucination/confidence.js +0 -66
  92. package/dist/modules/hallucination/consistency.js +0 -26
  93. package/dist/modules/hallucination/detector.js +0 -43
  94. package/dist/modules/hallucination/factual.js +0 -129
  95. package/dist/modules/hallucination/index.js +0 -5
  96. package/dist/modules/hallucination/js-identifiers.js +0 -262
  97. package/dist/modules/hallucination/llm-judge.js +0 -101
  98. package/dist/modules/index.js +0 -5
  99. package/dist/modules/indexer/cache.js +0 -40
  100. package/dist/modules/indexer/index.js +0 -3
  101. package/dist/modules/indexer/module.js +0 -245
  102. package/dist/modules/indexer/project-profile.js +0 -183
  103. package/dist/modules/indexer/walker.js +0 -101
  104. package/dist/modules/lsp/check-tool.js +0 -58
  105. package/dist/modules/lsp/client.js +0 -278
  106. package/dist/modules/lsp/command.js +0 -60
  107. package/dist/modules/lsp/config.js +0 -135
  108. package/dist/modules/lsp/index.js +0 -3
  109. package/dist/modules/lsp/module.js +0 -232
  110. package/dist/modules/lsp/probe.js +0 -76
  111. package/dist/modules/lsp/project-root.js +0 -32
  112. package/dist/modules/lsp/startup-check.js +0 -141
  113. package/dist/modules/lsp/types.js +0 -1
  114. package/dist/modules/mcp/client.js +0 -399
  115. package/dist/modules/mcp/index.js +0 -3
  116. package/dist/modules/mcp/module.js +0 -142
  117. package/dist/modules/mcp/registry.js +0 -15
  118. package/dist/modules/memory/index.js +0 -1
  119. package/dist/modules/memory/module.js +0 -96
  120. package/dist/modules/memory/search.js +0 -42
  121. package/dist/modules/memory/store.js +0 -69
  122. package/dist/modules/pipelines/engine.js +0 -60
  123. package/dist/modules/pipelines/index.js +0 -3
  124. package/dist/modules/pipelines/parser.js +0 -56
  125. package/dist/modules/pipelines/template.js +0 -14
  126. package/dist/modules/plugins/builtin/lint-on-write.js +0 -231
  127. package/dist/modules/plugins/builtin/notify.js +0 -9
  128. package/dist/modules/plugins/index.js +0 -1
  129. package/dist/modules/plugins/loader.js +0 -70
  130. package/dist/modules/plugins/manager.js +0 -217
  131. package/dist/modules/plugins/types.js +0 -1
  132. package/dist/modules/processes/detect.js +0 -34
  133. package/dist/modules/processes/index.js +0 -2
  134. package/dist/modules/processes/registry.js +0 -327
  135. package/dist/modules/processes/runner.js +0 -23
  136. package/dist/modules/registry.js +0 -47
  137. package/dist/modules/security/audit-log.js +0 -136
  138. package/dist/modules/security/audit-notifier.js +0 -292
  139. package/dist/modules/security/command-validator.js +0 -205
  140. package/dist/modules/security/content-scanner.js +0 -53
  141. package/dist/modules/security/data-sanitizer.js +0 -89
  142. package/dist/modules/security/encryption.js +0 -242
  143. package/dist/modules/security/index.js +0 -14
  144. package/dist/modules/security/network-validator.js +0 -71
  145. package/dist/modules/security/path-validator.js +0 -207
  146. package/dist/modules/security/rate-limiter.js +0 -119
  147. package/dist/modules/security/security-policies.js +0 -531
  148. package/dist/modules/security/session-encryption.js +0 -210
  149. package/dist/modules/security/session-isolation.js +0 -95
  150. package/dist/modules/session/index.js +0 -3
  151. package/dist/modules/session/manager.js +0 -172
  152. package/dist/modules/session/module.js +0 -24
  153. package/dist/modules/session/store.js +0 -222
  154. package/dist/modules/session/types.js +0 -1
  155. package/dist/modules/skills/index.js +0 -2
  156. package/dist/modules/skills/loader.js +0 -72
  157. package/dist/modules/skills/matcher.js +0 -27
  158. package/dist/modules/skills/module.js +0 -129
  159. package/dist/modules/types.js +0 -1
  160. package/dist/modules/updater/checker.js +0 -96
  161. package/dist/modules/updater/index.js +0 -2
  162. package/dist/modules/updater/module.js +0 -116
  163. package/dist/modules/user-profile/compressor.js +0 -16
  164. package/dist/modules/user-profile/index.js +0 -1
  165. package/dist/modules/user-profile/profile.js +0 -68
  166. package/dist/tools/approve.js +0 -32
  167. package/dist/tools/attach-image.js +0 -89
  168. package/dist/tools/bash.js +0 -496
  169. package/dist/tools/browser.js +0 -114
  170. package/dist/tools/chunk-query.js +0 -99
  171. package/dist/tools/create-dir.js +0 -55
  172. package/dist/tools/delete-file.js +0 -62
  173. package/dist/tools/download-file.js +0 -116
  174. package/dist/tools/edit-file.js +0 -79
  175. package/dist/tools/enable-tools.js +0 -58
  176. package/dist/tools/executor.js +0 -144
  177. package/dist/tools/file-info.js +0 -46
  178. package/dist/tools/filter-tools.js +0 -17
  179. package/dist/tools/glob-tool.js +0 -26
  180. package/dist/tools/grep-tool.js +0 -84
  181. package/dist/tools/hidden-tools-block.js +0 -37
  182. package/dist/tools/index.js +0 -78
  183. package/dist/tools/list-dir.js +0 -48
  184. package/dist/tools/load-skill.js +0 -42
  185. package/dist/tools/mcp-call.js +0 -68
  186. package/dist/tools/move-file.js +0 -85
  187. package/dist/tools/path-utils.js +0 -51
  188. package/dist/tools/pipeline-run.js +0 -144
  189. package/dist/tools/preview.js +0 -2
  190. package/dist/tools/process-kill.js +0 -29
  191. package/dist/tools/process-list.js +0 -36
  192. package/dist/tools/process-log.js +0 -45
  193. package/dist/tools/question.js +0 -140
  194. package/dist/tools/read-file.js +0 -91
  195. package/dist/tools/recall.js +0 -117
  196. package/dist/tools/registry.js +0 -47
  197. package/dist/tools/remember.js +0 -67
  198. package/dist/tools/scope-check.js +0 -30
  199. package/dist/tools/search-history.js +0 -84
  200. package/dist/tools/subagent.js +0 -196
  201. package/dist/tools/types.js +0 -1
  202. package/dist/tools/user-input.js +0 -123
  203. package/dist/tools/web-browse.js +0 -86
  204. package/dist/tools/web-fetch.js +0 -98
  205. package/dist/tools/web-search.js +0 -78
  206. package/dist/tools/write-file.js +0 -81
  207. package/dist/ui/box.js +0 -77
  208. package/dist/ui/colors.js +0 -4
  209. package/dist/ui/diff.js +0 -178
  210. package/dist/ui/index.js +0 -6
  211. package/dist/ui/line-editor.js +0 -703
  212. package/dist/ui/line-math.js +0 -69
  213. package/dist/ui/md-formatter.js +0 -212
  214. package/dist/ui/output.js +0 -13
  215. package/dist/ui/plan-view.js +0 -103
  216. package/dist/ui/renderer.js +0 -209
  217. package/dist/ui/spinner.js +0 -70
  218. package/dist/ui/table.js +0 -144
package/dist/index.js DELETED
@@ -1,22 +0,0 @@
1
- export { DEFAULTS, loadConfig } from "./config/index";
2
- export { Logger } from "./logger/index";
3
- export { PromptBuilder } from "./core/index";
4
- export { OpenAICompatProvider, TokenCounter, parseChunks, OrchestratorClient } from "./llm/index";
5
- export { ToolRegistry, ToolExecutor, readFileTool, writeFileTool, editFileTool, globTool, grepTool, listDirTool, createDirTool, deleteFileTool, moveFileTool, fileInfoTool, bashTool, subagentTool, webSearchTool, webFetchTool, webBrowseTool, createLoadSkillTool, pipelineRunTool, mcpCallTool, searchHistoryTool, registerAllTools, } from "./tools/index";
6
- export { MigrationDetector, BackupManager } from "./migration/index";
7
- import { MigrationDetector } from "./migration/detect";
8
- import { BackupManager } from "./migration/backup";
9
- import { t } from "./i18n/index";
10
- import { homedir } from "os";
11
- import { join } from "path";
12
- export function checkMigration(configDir) {
13
- const dir = configDir || join(homedir(), ".mma");
14
- const detector = new MigrationDetector(dir);
15
- if (detector.needsMigration()) {
16
- const backup = new BackupManager(dir);
17
- backup.backupConfig();
18
- backup.backupAll();
19
- const summary = backup.getBackupSummary();
20
- console.log(t("migration.summary", { summary }));
21
- }
22
- }
@@ -1,143 +0,0 @@
1
- import { readFileSync } from "fs";
2
- import { extname } from "path";
3
- const MIME_MAP = {
4
- ".png": "image/png",
5
- ".jpg": "image/jpeg",
6
- ".jpeg": "image/jpeg",
7
- ".gif": "image/gif",
8
- ".webp": "image/webp",
9
- ".bmp": "image/bmp",
10
- ".svg": "image/svg+xml",
11
- };
12
- /** Detect MIME type from file extension. */
13
- export function detectMime(filePath) {
14
- const ext = extname(filePath).toLowerCase();
15
- return MIME_MAP[ext] ?? "image/png";
16
- }
17
- /**
18
- * Read image from system clipboard using Bun.Image (macOS/Windows).
19
- * Falls back to platform-specific commands on Linux.
20
- * Returns null if clipboard has no image.
21
- */
22
- export async function readClipboardImage() {
23
- // Bun.Image.fromClipboard() works on macOS and Windows
24
- if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
25
- try {
26
- const img = Bun.Image.fromClipboard();
27
- if (img) {
28
- const buf = await img.resize(800, 800, { fit: "inside" }).jpeg({ quality: 60 }).buffer();
29
- return Buffer.from(buf);
30
- }
31
- }
32
- catch {
33
- // Bun.Image.fromClipboard() failed — fall through to platform fallback
34
- }
35
- }
36
- // Linux fallback: xclip/wl-paste → temp file → read
37
- return readClipboardFallback();
38
- }
39
- async function readClipboardFallback() {
40
- const { platform } = await import("os");
41
- const { execSync } = await import("child_process");
42
- const { readFileSync, unlinkSync } = await import("fs");
43
- const { join } = await import("path");
44
- const tmpPath = join(process.env.TEMP || process.env.TMP || "/tmp", `mma-clip-${Date.now()}.png`);
45
- try {
46
- if (platform() === "linux") {
47
- execSync(`xclip -selection clipboard -t image/png -o > "${tmpPath}" 2>/dev/null`, {
48
- timeout: 5000,
49
- });
50
- }
51
- else {
52
- return null; // macOS/Windows should use Bun.Image
53
- }
54
- const buf = readFileSync(tmpPath);
55
- unlinkSync(tmpPath);
56
- return buf.length > 0 ? buf : null;
57
- }
58
- catch {
59
- try {
60
- unlinkSync(tmpPath);
61
- }
62
- catch { }
63
- return null;
64
- }
65
- }
66
- /**
67
- * Load an image from a file path, resize via Bun.Image, return as JPEG data URL.
68
- * Target: ~800px wide, JPEG quality 60 — typically 5-15KB (~2-4K tokens).
69
- */
70
- export async function loadFileAsDataUrl(filePath) {
71
- const buf = readFileSync(filePath);
72
- // Use Bun.Image if available — resize + convert to JPEG
73
- if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
74
- try {
75
- const img = new Bun.Image(buf);
76
- const { width } = await img.metadata();
77
- const targetWidth = Math.min(800, width || 800);
78
- const dataUrl = await img
79
- .resize(targetWidth, 800, { fit: "inside" })
80
- .jpeg({ quality: 60 })
81
- .dataurl();
82
- return { dataUrl };
83
- }
84
- catch {
85
- // Bun.Image failed — fall through to raw base64
86
- }
87
- }
88
- // Fallback: raw base64 (no resize)
89
- const b64 = buf.toString("base64");
90
- const ext = filePath.split(".").pop()?.toLowerCase();
91
- const mime = ext === "jpg" || ext === "jpeg" ? "image/jpeg" : ext === "webp" ? "image/webp" : "image/png";
92
- return { dataUrl: `data:${mime};base64,${b64}` };
93
- }
94
- /**
95
- * Load an image from a URL, resize, return as JPEG data URL.
96
- */
97
- export async function loadUrlAsDataUrl(url) {
98
- const resp = await fetch(url);
99
- if (!resp.ok) {
100
- throw new Error(`Failed to fetch image: ${resp.status} ${resp.statusText}`);
101
- }
102
- const buf = Buffer.from(await resp.arrayBuffer());
103
- if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
104
- try {
105
- const img = new Bun.Image(buf);
106
- const { width } = await img.metadata();
107
- const targetWidth = Math.min(800, width || 800);
108
- const dataUrl = await img
109
- .resize(targetWidth, 800, { fit: "inside" })
110
- .jpeg({ quality: 60 })
111
- .dataurl();
112
- return { dataUrl };
113
- }
114
- catch {
115
- // fall through
116
- }
117
- }
118
- const b64 = buf.toString("base64");
119
- return { dataUrl: `data:image/png;base64,${b64}` };
120
- }
121
- /**
122
- * Process a clipboard buffer (already resized by readClipboardImage)
123
- * into a data URL. Used by Ctrl+V handler.
124
- */
125
- export async function bufferToDataUrl(buf) {
126
- if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
127
- try {
128
- const img = new Bun.Image(buf);
129
- const { width } = await img.metadata();
130
- const targetWidth = Math.min(800, width || 800);
131
- const dataUrl = await img
132
- .resize(targetWidth, 800, { fit: "inside" })
133
- .jpeg({ quality: 60 })
134
- .dataurl();
135
- return { dataUrl };
136
- }
137
- catch {
138
- // fall through
139
- }
140
- }
141
- const b64 = buf.toString("base64");
142
- return { dataUrl: `data:image/png;base64,${b64}` };
143
- }
package/dist/llm/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { OpenAICompatProvider } from "./openai-compat";
2
- export { TokenCounter } from "./token-counter";
3
- export { parseChunks } from "./response";
4
- export { OrchestratorClient } from "./orchestrator";
@@ -1,78 +0,0 @@
1
- export class ModelLoader {
2
- baseUrl;
3
- logger;
4
- constructor(baseUrl, logger) {
5
- const url = baseUrl.replace(/\/$/, "");
6
- this.baseUrl = url.replace(/\/v1\/?$/, "");
7
- this.logger = logger;
8
- }
9
- async ensureModelLoaded(config) {
10
- if (config.autoLoad === false) {
11
- return { success: true, alreadyLoaded: true };
12
- }
13
- try {
14
- const isLoaded = await this.isModelLoaded(config.model, config.contextLength);
15
- if (isLoaded) {
16
- this.logger.debug(`Model ${config.model} already loaded with correct context`);
17
- return { success: true, alreadyLoaded: true };
18
- }
19
- return await this.loadModel(config);
20
- }
21
- catch (err) {
22
- this.logger.error(`Model loader failed: ${err.message}`);
23
- return { success: false, error: err.message };
24
- }
25
- }
26
- async isModelLoaded(model, contextLength) {
27
- try {
28
- const response = await fetch(`${this.baseUrl}/api/v1/models`, {
29
- signal: AbortSignal.timeout(3000),
30
- });
31
- if (!response.ok)
32
- return false;
33
- const data = (await response.json());
34
- const models = data.models || [];
35
- const found = models.find((m) => m.key === model || m.name === model);
36
- if (!found)
37
- return false;
38
- const loadedInstances = found.loaded_instances || [];
39
- return loadedInstances.some((inst) => inst.config?.context_length === contextLength);
40
- }
41
- catch {
42
- return false;
43
- }
44
- }
45
- async loadModel(config) {
46
- const body = {
47
- model: config.model,
48
- context_length: config.contextLength,
49
- };
50
- if (config.flashAttention !== undefined)
51
- body.flash_attention = config.flashAttention;
52
- if (config.evalBatchSize !== undefined)
53
- body.eval_batch_size = config.evalBatchSize;
54
- if (config.offloadKvCacheToGpu !== undefined)
55
- body.offload_kv_cache_to_gpu = config.offloadKvCacheToGpu;
56
- this.logger.info(`Loading model ${config.model} with context_length=${config.contextLength}`);
57
- this.logger.debug(`POST ${this.baseUrl}/api/v1/models/load`);
58
- const response = await fetch(`${this.baseUrl}/api/v1/models/load`, {
59
- method: "POST",
60
- headers: { "Content-Type": "application/json" },
61
- body: JSON.stringify(body),
62
- signal: AbortSignal.timeout(120000),
63
- });
64
- if (!response.ok) {
65
- const error = await response.text();
66
- this.logger.error(`Model load failed: HTTP ${response.status}: ${error}`);
67
- return { success: false, error: `HTTP ${response.status}: ${error}` };
68
- }
69
- const result = (await response.json());
70
- this.logger.debug(`Model load response: ${JSON.stringify(result)}`);
71
- const loadTime = result.load_time_seconds ?? result.loadTime;
72
- return {
73
- success: true,
74
- alreadyLoaded: false,
75
- loadTime,
76
- };
77
- }
78
- }
@@ -1,359 +0,0 @@
1
- import { TokenCounter } from "./token-counter";
2
- import { t } from "../i18n/index";
3
- import { createRateLimiter } from "../modules/security/rate-limiter";
4
- function buildRequestBody(opts) {
5
- const body = {
6
- model: opts.model,
7
- messages: opts.messages,
8
- stream: opts.stream,
9
- };
10
- if (opts.maxTokens !== undefined)
11
- body.max_tokens = opts.maxTokens;
12
- if (opts.reasoningEffort) {
13
- body.reasoning_effort = opts.reasoningEffort;
14
- }
15
- if (opts.tools && opts.tools.length > 0) {
16
- body.tools = opts.tools.map((t) => ({
17
- type: "function",
18
- function: {
19
- name: t.name,
20
- description: t.description,
21
- parameters: t.parameters,
22
- },
23
- }));
24
- body.tool_choice = "auto";
25
- }
26
- return body;
27
- }
28
- export class OpenAICompatProvider {
29
- model;
30
- contextWindow;
31
- config;
32
- tokenCounter;
33
- retryConfig;
34
- rateLimiter;
35
- constructor(config) {
36
- this.config = config;
37
- this.model = config.model;
38
- this.contextWindow = config.contextWindow ?? 32768;
39
- this.tokenCounter = new TokenCounter();
40
- this.retryConfig = config.retry ?? {
41
- maxRetries: 3,
42
- baseDelay: 1000,
43
- maxDelay: 30000,
44
- };
45
- this.rateLimiter = createRateLimiter(config.rateLimits);
46
- }
47
- async *chat(messages, tools, signal, options) {
48
- // Check rate limit before making request
49
- if (!this.rateLimiter.canMakeRequest()) {
50
- throw new Error(`Rate limit exceeded: ${this.rateLimiter.getConfig().maxRequestsPerMinute} requests per minute`);
51
- }
52
- // Record this request
53
- this.rateLimiter.recordRequest();
54
- const streamResult = this.doStream(messages, tools, signal, options);
55
- let hasToolCall = false;
56
- let hasText = false;
57
- let reasoningAcc = "";
58
- for await (const chunk of streamResult) {
59
- if (chunk.type === "tool_call")
60
- hasToolCall = true;
61
- if (chunk.type === "text")
62
- hasText = true;
63
- if (chunk.type === "reasoning" && chunk.content) {
64
- reasoningAcc += chunk.content;
65
- }
66
- yield chunk;
67
- }
68
- if (!hasToolCall && !hasText) {
69
- const fallback = await this.doNonStreaming(messages, tools, signal, options);
70
- for (const chunk of fallback) {
71
- yield chunk;
72
- }
73
- }
74
- }
75
- async *doStream(messages, tools, signal, options) {
76
- const body = buildRequestBody({
77
- model: this.model,
78
- messages,
79
- tools,
80
- stream: true,
81
- maxTokens: options?.maxTokens ?? this.config.maxCompletionTokens ?? 4096,
82
- reasoningEffort: options?.reasoningEffort,
83
- });
84
- const headers = {
85
- "Content-Type": "application/json",
86
- };
87
- if (this.config.apiKey && this.config.apiKey !== "not-needed") {
88
- headers["Authorization"] = `Bearer ${this.config.apiKey}`;
89
- }
90
- const controller = new AbortController();
91
- const totalTimeoutMs = 120000;
92
- const timeoutId = setTimeout(() => controller.abort(), totalTimeoutMs);
93
- const abortSignal = (() => {
94
- if (!signal)
95
- return controller.signal;
96
- try {
97
- return AbortSignal.any([controller.signal, signal]);
98
- }
99
- catch {
100
- signal.addEventListener("abort", () => controller.abort(), {
101
- once: true,
102
- });
103
- return controller.signal;
104
- }
105
- })();
106
- const response = await this.fetchWithRetry(`${this.config.baseUrl}/chat/completions`, {
107
- method: "POST",
108
- headers,
109
- body: JSON.stringify(body),
110
- signal: abortSignal,
111
- });
112
- if (!response.ok) {
113
- clearTimeout(timeoutId);
114
- const errorText = await response.text();
115
- throw new Error(t("error.llm_api", {
116
- status: response.status,
117
- statusText: response.statusText,
118
- errorText,
119
- }));
120
- }
121
- const reader = response.body?.getReader();
122
- if (!reader) {
123
- clearTimeout(timeoutId);
124
- throw new Error(t("error.no_response_body"));
125
- }
126
- const decoder = new TextDecoder();
127
- let buffer = "";
128
- const toolCallAccs = new Map();
129
- let usage;
130
- try {
131
- while (true) {
132
- const { done, value } = await reader.read();
133
- if (done)
134
- break;
135
- buffer += decoder.decode(value, { stream: true });
136
- const lines = buffer.split("\n");
137
- buffer = lines.pop() || "";
138
- for (const line of lines) {
139
- const trimmed = line.trim();
140
- if (!trimmed || !trimmed.startsWith("data: "))
141
- continue;
142
- const data = trimmed.slice(6);
143
- if (data === "[DONE]")
144
- continue;
145
- try {
146
- const parsed = JSON.parse(data);
147
- const choice = parsed.choices?.[0];
148
- if (!choice) {
149
- // Usage comes in the last chunk with empty choices
150
- if (parsed.usage) {
151
- usage = {
152
- promptTokens: parsed.usage.prompt_tokens ?? 0,
153
- completionTokens: parsed.usage.completion_tokens ?? 0,
154
- totalTokens: parsed.usage.total_tokens ?? 0,
155
- };
156
- }
157
- continue;
158
- }
159
- const delta = choice.delta || {};
160
- const finishReason = choice.finish_reason;
161
- if (delta.reasoning_content) {
162
- yield { type: "reasoning", content: delta.reasoning_content };
163
- }
164
- if (delta.tool_calls) {
165
- for (const tc of delta.tool_calls) {
166
- const idx = tc.index ?? 0;
167
- if (!toolCallAccs.has(idx)) {
168
- toolCallAccs.set(idx, { id: "", name: "", arguments: "" });
169
- }
170
- const acc = toolCallAccs.get(idx);
171
- if (tc.id)
172
- acc.id = tc.id;
173
- if (tc.function?.name)
174
- acc.name = tc.function.name;
175
- if (tc.function?.arguments) {
176
- acc.arguments += tc.function.arguments;
177
- }
178
- }
179
- }
180
- if (delta.content) {
181
- yield { type: "text", content: delta.content };
182
- }
183
- if (finishReason === "tool_calls" && toolCallAccs.size > 0) {
184
- for (const [, acc] of toolCallAccs) {
185
- if (acc.name) {
186
- yield {
187
- type: "tool_call",
188
- toolCall: {
189
- id: acc.id,
190
- name: acc.name,
191
- arguments: acc.arguments || "{}",
192
- },
193
- };
194
- }
195
- }
196
- toolCallAccs.clear();
197
- }
198
- }
199
- catch {
200
- // Skip malformed JSON lines
201
- }
202
- }
203
- }
204
- if (usage) {
205
- yield { type: "done", usage };
206
- }
207
- }
208
- finally {
209
- clearTimeout(timeoutId);
210
- reader.releaseLock();
211
- }
212
- }
213
- async doNonStreaming(messages, tools, signal, options) {
214
- const body = buildRequestBody({
215
- model: this.model,
216
- messages,
217
- tools,
218
- stream: false,
219
- maxTokens: options?.maxTokens ?? this.config.maxCompletionTokens ?? 4096,
220
- reasoningEffort: options?.reasoningEffort,
221
- });
222
- const headers = {
223
- "Content-Type": "application/json",
224
- };
225
- if (this.config.apiKey && this.config.apiKey !== "not-needed") {
226
- headers["Authorization"] = `Bearer ${this.config.apiKey}`;
227
- }
228
- try {
229
- const response = await this.fetchWithRetry(`${this.config.baseUrl}/chat/completions`, {
230
- method: "POST",
231
- headers,
232
- body: JSON.stringify(body),
233
- signal,
234
- });
235
- if (!response.ok) {
236
- const errorText = await response.text();
237
- throw new Error(t("error.llm_api", {
238
- status: response.status,
239
- statusText: response.statusText,
240
- errorText: errorText.slice(0, 500),
241
- }));
242
- }
243
- const data = await response.json();
244
- const choice = data.choices?.[0];
245
- if (!choice) {
246
- return [];
247
- }
248
- const msg = choice.message || {};
249
- const chunks = [];
250
- if (msg.reasoning_content) {
251
- chunks.push({ type: "reasoning", content: msg.reasoning_content });
252
- }
253
- if (msg.content) {
254
- chunks.push({ type: "text", content: msg.content });
255
- }
256
- if (msg.tool_calls) {
257
- for (const tc of msg.tool_calls) {
258
- chunks.push({
259
- type: "tool_call",
260
- toolCall: {
261
- id: tc.id || "",
262
- name: tc.function?.name || "",
263
- arguments: tc.function?.arguments || "{}",
264
- },
265
- });
266
- }
267
- }
268
- // Append usage from API response
269
- if (data.usage) {
270
- chunks.push({
271
- type: "done",
272
- usage: {
273
- promptTokens: data.usage.prompt_tokens ?? 0,
274
- completionTokens: data.usage.completion_tokens ?? 0,
275
- totalTokens: data.usage.total_tokens ?? 0,
276
- },
277
- });
278
- }
279
- return chunks;
280
- }
281
- catch (err) {
282
- throw err instanceof Error ? err : new Error(String(err));
283
- }
284
- }
285
- countTokens(text) {
286
- return this.tokenCounter.count(text);
287
- }
288
- async listModels() {
289
- try {
290
- const url = `${this.config.baseUrl.replace(/\/+$/, "")}/models`;
291
- const headers = {
292
- "Content-Type": "application/json",
293
- };
294
- if (this.config.apiKey && this.config.apiKey !== "not-needed") {
295
- headers["Authorization"] = `Bearer ${this.config.apiKey}`;
296
- }
297
- const response = await fetch(url, {
298
- method: "GET",
299
- headers,
300
- });
301
- if (!response.ok) {
302
- return [];
303
- }
304
- const data = (await response.json());
305
- const models = (data.data || data || [])
306
- .map((m) => m.id || m.name || m.model || "")
307
- .filter(Boolean);
308
- return models;
309
- }
310
- catch {
311
- return [];
312
- }
313
- }
314
- async fetchWithRetry(url, init) {
315
- const { maxRetries, baseDelay, maxDelay } = this.retryConfig;
316
- let lastError = null;
317
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
318
- try {
319
- const response = await fetch(url, init);
320
- if (!this.isRetryable(response.status))
321
- return response;
322
- lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);
323
- }
324
- catch (err) {
325
- if (err.name === "AbortError") {
326
- throw err;
327
- }
328
- lastError = err;
329
- }
330
- if (attempt < maxRetries) {
331
- const delay = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
332
- const jitter = Math.random() * baseDelay * 0.1;
333
- await this.sleep(delay + jitter, init.signal ?? undefined);
334
- }
335
- }
336
- throw lastError ?? new Error(t("error.llm_retries"));
337
- }
338
- isRetryable(status) {
339
- return status === 429 || status >= 500;
340
- }
341
- sleep(ms, signal) {
342
- return new Promise((resolve, reject) => {
343
- if (signal?.aborted) {
344
- reject(new DOMException("Aborted", "AbortError"));
345
- return;
346
- }
347
- let timer;
348
- const onAbort = () => {
349
- clearTimeout(timer);
350
- reject(new DOMException("Aborted", "AbortError"));
351
- };
352
- timer = setTimeout(() => {
353
- signal?.removeEventListener("abort", onAbort);
354
- resolve();
355
- }, ms);
356
- signal?.addEventListener("abort", onAbort, { once: true });
357
- });
358
- }
359
- }