micro-models-agent 0.57.0 → 0.57.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 (233) hide show
  1. package/CHANGELOG.md +518 -476
  2. package/README.md +358 -358
  3. package/dist/certification/certifications.json +493 -493
  4. package/dist/cli/commands.js +447 -0
  5. package/dist/cli/completer.js +167 -0
  6. package/dist/cli/index.js +2 -0
  7. package/dist/cli/main.js +153 -0
  8. package/dist/cli/plugin-commands.js +36 -0
  9. package/dist/cli/repl-commands.js +761 -0
  10. package/dist/cli/repl.js +702 -0
  11. package/dist/cli/run-result.js +33 -0
  12. package/dist/cli/security-commands.js +164 -0
  13. package/dist/cli/setup.js +237 -0
  14. package/dist/config/config.js +276 -0
  15. package/dist/config/defaults.js +141 -0
  16. package/dist/config/domains.js +179 -0
  17. package/dist/config/experts.js +15 -0
  18. package/dist/config/index.js +4 -0
  19. package/dist/config/security.js +213 -0
  20. package/dist/config/types.js +1 -0
  21. package/dist/core/agent-moe.js +102 -0
  22. package/dist/core/agent.js +1018 -0
  23. package/dist/core/bootstrap.js +481 -0
  24. package/dist/core/crash-handler.js +51 -0
  25. package/dist/core/environment.js +199 -0
  26. package/dist/core/index.js +2 -0
  27. package/dist/core/prompt-builder.js +76 -0
  28. package/dist/core/session-logger.js +251 -0
  29. package/dist/core/types.js +1 -0
  30. package/dist/core/version.js +26 -0
  31. package/dist/core/workspace.js +76 -0
  32. package/dist/i18n/en.json +679 -0
  33. package/dist/i18n/index.js +46 -0
  34. package/dist/i18n/ru.json +679 -0
  35. package/dist/index.js +22 -0
  36. package/dist/llm/image-utils.js +143 -0
  37. package/dist/llm/index.js +4 -0
  38. package/dist/llm/model-loader.js +78 -0
  39. package/dist/llm/openai-compat.js +497 -0
  40. package/dist/llm/orchestrator.js +200 -0
  41. package/dist/llm/provider.js +10 -0
  42. package/dist/llm/response.js +39 -0
  43. package/dist/llm/token-counter.js +39 -0
  44. package/dist/llm/types.js +1 -0
  45. package/dist/logger/app-logger.js +189 -0
  46. package/dist/logger/file-log.js +151 -0
  47. package/dist/logger/index.js +1 -0
  48. package/dist/main.js +2351 -1640
  49. package/dist/migration/backup.js +45 -0
  50. package/dist/migration/detect.js +50 -0
  51. package/dist/migration/index.js +2 -0
  52. package/dist/modules/artifacts/store.js +61 -0
  53. package/dist/modules/browser/actions.js +76 -0
  54. package/dist/modules/browser/bridge-client.js +199 -0
  55. package/dist/modules/browser/bridge-path.js +10 -0
  56. package/dist/modules/browser/bridge-server.mjs +219 -219
  57. package/dist/modules/browser/cookie-store.js +24 -0
  58. package/dist/modules/browser/driver.js +136 -0
  59. package/dist/modules/browser/index.js +7 -0
  60. package/dist/modules/browser/module.js +29 -0
  61. package/dist/modules/browser/session.js +342 -0
  62. package/dist/modules/browser/snapshot.js +148 -0
  63. package/dist/modules/browser/types.js +12 -0
  64. package/dist/modules/certification/cli.js +213 -0
  65. package/dist/modules/certification/fact-checker.js +82 -0
  66. package/dist/modules/certification/loader.js +106 -0
  67. package/dist/modules/certification/manifest.js +58 -0
  68. package/dist/modules/certification/runner.js +245 -0
  69. package/dist/modules/certification/scenarios.js +407 -0
  70. package/dist/modules/certification/types.js +1 -0
  71. package/dist/modules/context/chunk-query.js +100 -0
  72. package/dist/modules/context/fact-extractor.js +168 -0
  73. package/dist/modules/context/history.js +15 -0
  74. package/dist/modules/context/index.js +1 -0
  75. package/dist/modules/context/manager.js +440 -0
  76. package/dist/modules/execution/audit-runners.js +206 -0
  77. package/dist/modules/execution/auditor.js +218 -0
  78. package/dist/modules/execution/execution-plugin.js +431 -0
  79. package/dist/modules/execution/index.js +8 -0
  80. package/dist/modules/execution/module.js +625 -0
  81. package/dist/modules/execution/moe-executor.js +304 -0
  82. package/dist/modules/execution/plan-coverage.js +68 -0
  83. package/dist/modules/execution/plan-persister.js +46 -0
  84. package/dist/modules/execution/plan-store.js +196 -0
  85. package/dist/modules/execution/plan-tool.js +677 -0
  86. package/dist/modules/execution/plan-validator.js +153 -0
  87. package/dist/modules/execution/planner.js +94 -0
  88. package/dist/modules/execution/stuck-detector.js +746 -0
  89. package/dist/modules/execution/tracker.js +69 -0
  90. package/dist/modules/execution/types.js +1 -0
  91. package/dist/modules/execution/verifier.js +235 -0
  92. package/dist/modules/execution/windows-commands.js +41 -0
  93. package/dist/modules/hallucination/confidence.js +66 -0
  94. package/dist/modules/hallucination/consistency.js +26 -0
  95. package/dist/modules/hallucination/detector.js +47 -0
  96. package/dist/modules/hallucination/factual.js +169 -0
  97. package/dist/modules/hallucination/index.js +5 -0
  98. package/dist/modules/hallucination/js-identifiers.js +262 -0
  99. package/dist/modules/hallucination/llm-judge.js +101 -0
  100. package/dist/modules/index.js +5 -0
  101. package/dist/modules/indexer/cache.js +40 -0
  102. package/dist/modules/indexer/index.js +3 -0
  103. package/dist/modules/indexer/module.js +246 -0
  104. package/dist/modules/indexer/project-profile.js +183 -0
  105. package/dist/modules/indexer/walker.js +101 -0
  106. package/dist/modules/lsp/check-tool.js +58 -0
  107. package/dist/modules/lsp/client.js +389 -0
  108. package/dist/modules/lsp/command.js +60 -0
  109. package/dist/modules/lsp/config.js +135 -0
  110. package/dist/modules/lsp/index.js +3 -0
  111. package/dist/modules/lsp/module.js +260 -0
  112. package/dist/modules/lsp/probe.js +86 -0
  113. package/dist/modules/lsp/project-root.js +32 -0
  114. package/dist/modules/lsp/startup-check.js +144 -0
  115. package/dist/modules/lsp/types.js +1 -0
  116. package/dist/modules/mcp/client.js +399 -0
  117. package/dist/modules/mcp/index.js +3 -0
  118. package/dist/modules/mcp/module.js +142 -0
  119. package/dist/modules/mcp/registry.js +15 -0
  120. package/dist/modules/memory/index.js +1 -0
  121. package/dist/modules/memory/module.js +96 -0
  122. package/dist/modules/memory/search.js +42 -0
  123. package/dist/modules/memory/store.js +69 -0
  124. package/dist/modules/pipelines/engine.js +60 -0
  125. package/dist/modules/pipelines/index.js +3 -0
  126. package/dist/modules/pipelines/parser.js +56 -0
  127. package/dist/modules/pipelines/template.js +14 -0
  128. package/dist/modules/plugins/builtin/lint-on-write.js +334 -0
  129. package/dist/modules/plugins/builtin/notify.js +9 -0
  130. package/dist/modules/plugins/index.js +1 -0
  131. package/dist/modules/plugins/loader.js +70 -0
  132. package/dist/modules/plugins/manager.js +261 -0
  133. package/dist/modules/plugins/types.js +1 -0
  134. package/dist/modules/pricing/index.js +61 -0
  135. package/dist/modules/pricing/prices.js +129 -0
  136. package/dist/modules/processes/detect.js +34 -0
  137. package/dist/modules/processes/index.js +2 -0
  138. package/dist/modules/processes/registry.js +327 -0
  139. package/dist/modules/processes/runner.js +23 -0
  140. package/dist/modules/providers/create.js +22 -0
  141. package/dist/modules/providers/fallback.js +79 -0
  142. package/dist/modules/providers/health.js +46 -0
  143. package/dist/modules/providers/index.js +5 -0
  144. package/dist/modules/providers/manager.js +161 -0
  145. package/dist/modules/providers/presets.js +128 -0
  146. package/dist/modules/providers/registry.js +22 -0
  147. package/dist/modules/providers/types.js +1 -0
  148. package/dist/modules/registry.js +48 -0
  149. package/dist/modules/security/audit-log.js +136 -0
  150. package/dist/modules/security/audit-notifier.js +292 -0
  151. package/dist/modules/security/command-validator.js +219 -0
  152. package/dist/modules/security/content-scanner.js +53 -0
  153. package/dist/modules/security/data-sanitizer.js +89 -0
  154. package/dist/modules/security/encryption.js +242 -0
  155. package/dist/modules/security/index.js +14 -0
  156. package/dist/modules/security/network-validator.js +88 -0
  157. package/dist/modules/security/path-validator.js +203 -0
  158. package/dist/modules/security/rate-limiter.js +119 -0
  159. package/dist/modules/security/security-policies.js +531 -0
  160. package/dist/modules/security/session-encryption.js +210 -0
  161. package/dist/modules/security/session-isolation.js +95 -0
  162. package/dist/modules/session/index.js +3 -0
  163. package/dist/modules/session/manager.js +172 -0
  164. package/dist/modules/session/module.js +24 -0
  165. package/dist/modules/session/store.js +222 -0
  166. package/dist/modules/session/types.js +1 -0
  167. package/dist/modules/skills/index.js +2 -0
  168. package/dist/modules/skills/loader.js +72 -0
  169. package/dist/modules/skills/matcher.js +27 -0
  170. package/dist/modules/skills/module.js +129 -0
  171. package/dist/modules/types.js +1 -0
  172. package/dist/modules/updater/checker.js +96 -0
  173. package/dist/modules/updater/index.js +2 -0
  174. package/dist/modules/updater/module.js +116 -0
  175. package/dist/modules/user-profile/compressor.js +16 -0
  176. package/dist/modules/user-profile/index.js +1 -0
  177. package/dist/modules/user-profile/profile.js +68 -0
  178. package/dist/skills/builtin/git.md +36 -36
  179. package/dist/skills/builtin/typescript.md +35 -35
  180. package/dist/tools/approve.js +33 -0
  181. package/dist/tools/attach-image.js +101 -0
  182. package/dist/tools/bash.js +519 -0
  183. package/dist/tools/browser.js +115 -0
  184. package/dist/tools/chunk-query.js +100 -0
  185. package/dist/tools/create-dir.js +56 -0
  186. package/dist/tools/delete-file.js +63 -0
  187. package/dist/tools/download-file.js +117 -0
  188. package/dist/tools/edit-file.js +80 -0
  189. package/dist/tools/enable-tools.js +59 -0
  190. package/dist/tools/executor.js +154 -0
  191. package/dist/tools/file-info.js +47 -0
  192. package/dist/tools/filter-tools.js +17 -0
  193. package/dist/tools/glob-tool.js +27 -0
  194. package/dist/tools/grep-tool.js +125 -0
  195. package/dist/tools/hidden-tools-block.js +37 -0
  196. package/dist/tools/index.js +78 -0
  197. package/dist/tools/list-dir.js +49 -0
  198. package/dist/tools/load-skill.js +43 -0
  199. package/dist/tools/mcp-call.js +69 -0
  200. package/dist/tools/move-file.js +86 -0
  201. package/dist/tools/path-utils.js +101 -0
  202. package/dist/tools/pipeline-run.js +145 -0
  203. package/dist/tools/preview.js +2 -0
  204. package/dist/tools/process-kill.js +40 -0
  205. package/dist/tools/process-list.js +37 -0
  206. package/dist/tools/process-log.js +54 -0
  207. package/dist/tools/question.js +141 -0
  208. package/dist/tools/read-file.js +179 -0
  209. package/dist/tools/recall.js +118 -0
  210. package/dist/tools/registry.js +47 -0
  211. package/dist/tools/remember.js +68 -0
  212. package/dist/tools/scope-check.js +32 -0
  213. package/dist/tools/search-history.js +85 -0
  214. package/dist/tools/subagent.js +196 -0
  215. package/dist/tools/types.js +1 -0
  216. package/dist/tools/user-input.js +123 -0
  217. package/dist/tools/web-browse.js +87 -0
  218. package/dist/tools/web-fetch.js +119 -0
  219. package/dist/tools/web-search.js +105 -0
  220. package/dist/tools/write-file.js +82 -0
  221. package/dist/ui/box.js +77 -0
  222. package/dist/ui/colors.js +4 -0
  223. package/dist/ui/diff.js +178 -0
  224. package/dist/ui/index.js +6 -0
  225. package/dist/ui/line-editor.js +822 -0
  226. package/dist/ui/line-math.js +73 -0
  227. package/dist/ui/md-formatter.js +212 -0
  228. package/dist/ui/output.js +13 -0
  229. package/dist/ui/plan-view.js +103 -0
  230. package/dist/ui/renderer.js +259 -0
  231. package/dist/ui/spinner.js +70 -0
  232. package/dist/ui/table.js +144 -0
  233. package/package.json +51 -51
@@ -0,0 +1,276 @@
1
+ import { existsSync, readFileSync, unlinkSync, writeFileSync, mkdirSync } from "fs";
2
+ import { join, dirname } from "path";
3
+ import { homedir } from "os";
4
+ import { DEFAULTS } from "./defaults";
5
+ import { DEFAULT_SECURITY_CONFIG } from "./security";
6
+ import { DEFAULT_LSP_CONFIG } from "../modules/lsp/config";
7
+ import { t } from "../i18n/index";
8
+ import { MigrationDetector } from "../migration/detect";
9
+ import { BackupManager } from "../migration/backup";
10
+ import { validateExpertConfig } from "./experts";
11
+ import { ConfigEncryptor } from "../modules/security/encryption";
12
+ import { loadDomainFiles, saveDomainFiles } from "./domains";
13
+ // ── Centralized path constants ──────────────────────────────────────────
14
+ export const CONFIG_DIR = join(homedir(), ".mma");
15
+ export const CONFIG_FILE = join(CONFIG_DIR, "config.json");
16
+ export const CONFIG_DOMAIN_DIR = join(CONFIG_DIR, "config");
17
+ // ── Internal helpers ────────────────────────────────────────────────────
18
+ /**
19
+ * Restore RegExp instances in dangerousPatterns that were serialized as {}
20
+ * (pre-0.8.0 configs) or as {__regex, source, flags} (new format).
21
+ * Falls back to default patterns for any entry that is not a real RegExp.
22
+ */
23
+ function restoreDangerousPatterns(patterns, defaults) {
24
+ const fallback = (Array.isArray(defaults) ? defaults : []);
25
+ if (!Array.isArray(patterns) || patterns.length === 0) {
26
+ return fallback;
27
+ }
28
+ return patterns.map((p, i) => {
29
+ if (p instanceof RegExp)
30
+ return p;
31
+ if (p && typeof p === "object") {
32
+ const { source, flags } = p;
33
+ if (source && typeof source === "string") {
34
+ try {
35
+ return new RegExp(source, flags || "");
36
+ }
37
+ catch {
38
+ // fall through
39
+ }
40
+ }
41
+ }
42
+ return fallback[i] || fallback[0] || p;
43
+ });
44
+ }
45
+ function deepMerge(target, source) {
46
+ const result = { ...target };
47
+ for (const key of Object.keys(source)) {
48
+ const srcVal = source[key];
49
+ const tgtVal = target[key];
50
+ if (srcVal !== null &&
51
+ srcVal !== undefined &&
52
+ typeof srcVal === "object" &&
53
+ !Array.isArray(srcVal) &&
54
+ typeof tgtVal === "object" &&
55
+ !Array.isArray(tgtVal)) {
56
+ result[key] = deepMerge(tgtVal, srcVal);
57
+ }
58
+ else if (srcVal !== undefined) {
59
+ result[key] = srcVal;
60
+ }
61
+ }
62
+ return result;
63
+ }
64
+ /**
65
+ * JSON replacer that serializes RegExp objects as {__regex, source, flags}
66
+ * so they survive JSON.stringify/parse round-trips.
67
+ */
68
+ function regexReplacer(_key, value) {
69
+ if (value instanceof RegExp) {
70
+ return { __regex: true, source: value.source, flags: value.flags };
71
+ }
72
+ return value;
73
+ }
74
+ /**
75
+ * JSON reviver that restores RegExp objects serialized by regexReplacer.
76
+ */
77
+ function regexReviver(_key, value) {
78
+ if (value && typeof value === "object" && value.__regex === true) {
79
+ const { source, flags } = value;
80
+ try {
81
+ return new RegExp(source, flags);
82
+ }
83
+ catch {
84
+ return value;
85
+ }
86
+ }
87
+ return value;
88
+ }
89
+ /**
90
+ * Legacy npx `args` for LSP servers that predate the fixed invocation.
91
+ */
92
+ const LEGACY_LSP_SERVER_ARGS = {
93
+ typescript: { args: ["typescript-language-server", "--stdio"], timeout: 30000 },
94
+ javascript: { args: ["typescript-language-server", "--stdio"], timeout: 30000 },
95
+ json: { args: ["vscode-json-languageserver", "--stdio"], timeout: 30000 },
96
+ html: { args: ["vscode-html-languageserver", "--stdio"], timeout: 60000 },
97
+ css: { args: ["vscode-css-languageserver", "--stdio"], timeout: 60000 },
98
+ };
99
+ /**
100
+ * Replace stale pre-0.41.1 LSP server invocations with current defaults.
101
+ */
102
+ function normalizeLspServerArgs(config) {
103
+ const servers = config.lsp?.servers;
104
+ if (!servers)
105
+ return;
106
+ for (const [lang, legacy] of Object.entries(LEGACY_LSP_SERVER_ARGS)) {
107
+ const server = servers[lang];
108
+ const def = DEFAULT_LSP_CONFIG.servers[lang];
109
+ if (!server || server.command !== "npx" || !def)
110
+ continue;
111
+ const storedArgs = Array.isArray(server.args) ? server.args : null;
112
+ const isLegacy = storedArgs === null || JSON.stringify(storedArgs) === JSON.stringify(legacy.args);
113
+ if (isLegacy) {
114
+ server.args = def.args;
115
+ server.timeout = def.timeout;
116
+ }
117
+ }
118
+ }
119
+ function loadJSON(path) {
120
+ try {
121
+ if (existsSync(path)) {
122
+ return JSON.parse(readFileSync(path, "utf-8"), regexReviver);
123
+ }
124
+ }
125
+ catch {
126
+ /* ignore malformed files */
127
+ }
128
+ return null;
129
+ }
130
+ function applyEnvVars(config) {
131
+ const env = process.env;
132
+ const result = { ...config };
133
+ if (env.MMA_MODEL)
134
+ result.model = env.MMA_MODEL;
135
+ if (env.MMA_PROVIDER_TYPE)
136
+ result.provider = { ...result.provider, type: env.MMA_PROVIDER_TYPE };
137
+ if (env.MMA_PROVIDER_BASEURL)
138
+ result.provider = { ...result.provider, baseUrl: env.MMA_PROVIDER_BASEURL };
139
+ if (env.MMA_PROVIDER_APIKEY)
140
+ result.provider = { ...result.provider, apiKey: env.MMA_PROVIDER_APIKEY };
141
+ if (env.MMA_LOG_LEVEL)
142
+ result.logLevel = env.MMA_LOG_LEVEL;
143
+ if (env.MMA_LOCALE)
144
+ result.locale = env.MMA_LOCALE;
145
+ if (env.MMA_CONTEXT_WINDOW) {
146
+ const v = parseInt(env.MMA_CONTEXT_WINDOW, 10);
147
+ if (!isNaN(v))
148
+ result.contextWindow = v;
149
+ }
150
+ if (env.MMA_MAX_TOOL_ITERATIONS) {
151
+ const v = parseInt(env.MMA_MAX_TOOL_ITERATIONS, 10);
152
+ if (!isNaN(v))
153
+ result.maxToolIterations = v;
154
+ }
155
+ if (env.MMA_STUCK_THRESHOLD) {
156
+ const v = parseInt(env.MMA_STUCK_THRESHOLD, 10);
157
+ if (!isNaN(v))
158
+ result.stuckThreshold = v;
159
+ }
160
+ if (env.MMA_AUTO_PLAN)
161
+ result.autoPlan = env.MMA_AUTO_PLAN === "true";
162
+ if (env.MMA_MOE_ENABLED)
163
+ result.moe = { ...result.moe, enabled: env.MMA_MOE_ENABLED === "true" };
164
+ return result;
165
+ }
166
+ /**
167
+ * Load config from domain files (preferred) or legacy config.json (fallback).
168
+ *
169
+ * Pipeline:
170
+ * 1. DEFAULTS
171
+ * 2. deepMerge(legacy config.json) — if exists
172
+ * 3. deepMerge(domain files) — if exist, override legacy
173
+ * 4. deepMerge(project .mmrc) — overrides everything
174
+ * 5. Post-load fixups (RegExp, LSP args)
175
+ * 6. Env vars
176
+ * 7. Decrypt
177
+ *
178
+ * On first run (no config.json, no domain files), auto-creates domain files.
179
+ */
180
+ export function loadConfig(options) {
181
+ const globalPath = join(options.configDir, "config.json");
182
+ mkdirSync(options.configDir, { recursive: true });
183
+ // v1 → v2 migration check (reads config.json directly, before domain logic)
184
+ const detector = new MigrationDetector(options.configDir);
185
+ if (detector.needsMigration()) {
186
+ const backup = new BackupManager(options.configDir);
187
+ backup.backupConfig();
188
+ const summary = backup.getBackupSummary();
189
+ console.log(t("migration.detected", { summary }));
190
+ try {
191
+ unlinkSync(globalPath);
192
+ }
193
+ catch { }
194
+ }
195
+ let config = { ...DEFAULTS };
196
+ let legacyDetected = false;
197
+ // Step 1: legacy config.json (fallback)
198
+ const globalData = loadJSON(globalPath);
199
+ if (globalData) {
200
+ config = deepMerge(config, globalData);
201
+ legacyDetected = true;
202
+ }
203
+ // Step 2: domain files (override legacy)
204
+ const domainConfig = loadDomainFiles(options.configDir);
205
+ if (domainConfig) {
206
+ config = deepMerge(config, domainConfig);
207
+ legacyDetected = false;
208
+ }
209
+ // Step 3: first run — auto-create domain files instead of config.json
210
+ if (!globalData && !domainConfig) {
211
+ saveDomainFiles(config, options.configDir);
212
+ }
213
+ // Step 4: project .mmrc (overrides everything)
214
+ const projectData = loadJSON(options.projectConfigPath);
215
+ if (projectData) {
216
+ config = deepMerge(config, projectData);
217
+ }
218
+ // Step 5: post-load fixups — AFTER all merges
219
+ if (config.security?.contentScan?.dangerousPatterns) {
220
+ config.security.contentScan.dangerousPatterns = restoreDangerousPatterns(config.security.contentScan.dangerousPatterns, DEFAULT_SECURITY_CONFIG.contentScan.dangerousPatterns);
221
+ }
222
+ normalizeLspServerArgs(config);
223
+ // Step 6: env vars
224
+ config = applyEnvVars(config);
225
+ // Step 7: decrypt
226
+ try {
227
+ const encryptor = new ConfigEncryptor();
228
+ const decrypted = encryptor.decrypt(config);
229
+ for (const key of Object.keys(decrypted)) {
230
+ config[key] = decrypted[key];
231
+ }
232
+ }
233
+ catch (e) {
234
+ console.warn(t("config.decryption_warning", { error: e.message }));
235
+ }
236
+ return { config, legacyDetected };
237
+ }
238
+ export function validateConfig(config, allToolTags) {
239
+ const errors = validateExpertConfig(config, allToolTags);
240
+ const knownTags = new Set(allToolTags);
241
+ for (const tag of config.tools?.defaultTags ?? []) {
242
+ if (!knownTags.has(tag)) {
243
+ errors.push(`tools.defaultTags references unknown tag "${tag}". Known tags: ${Array.from(knownTags).join(", ")}`);
244
+ }
245
+ }
246
+ if (errors.length > 0) {
247
+ throw new Error(`Config validation failed:\n${errors.join("\n")}`);
248
+ }
249
+ }
250
+ /**
251
+ * Save config to domain files (preferred) or legacy single file (fallback).
252
+ *
253
+ * @param config Full AppConfig to save
254
+ * @param configPath Legacy path (e.g. ~/.mma/config.json) — used when configDir is absent
255
+ * @param configDir Optional config directory (e.g. ~/.mma). When provided, writes domain files.
256
+ */
257
+ export function saveConfig(config, configPath, configDir) {
258
+ if (configDir) {
259
+ saveDomainFiles(config, configDir);
260
+ return;
261
+ }
262
+ // Legacy single-file write (backward compat for tests / edge cases)
263
+ const dir = dirname(configPath);
264
+ mkdirSync(dir, { recursive: true });
265
+ try {
266
+ const encryptor = new ConfigEncryptor();
267
+ const encryptedConfig = encryptor.encrypt({
268
+ ...config,
269
+ });
270
+ writeFileSync(configPath, JSON.stringify(encryptedConfig, regexReplacer, 2), "utf-8");
271
+ }
272
+ catch (e) {
273
+ console.warn(t("config.encryption_warning", { error: e.message }));
274
+ writeFileSync(configPath, JSON.stringify(config, regexReplacer, 2), "utf-8");
275
+ }
276
+ }
@@ -0,0 +1,141 @@
1
+ import { DEFAULT_SECURITY_CONFIG } from "./security";
2
+ import { DEFAULT_LSP_CONFIG } from "../modules/lsp/config";
3
+ export const DEFAULTS = {
4
+ version: "2.0.0",
5
+ model: "qwen/qwen3.5-9b",
6
+ provider: {
7
+ type: "openai-compat",
8
+ baseUrl: "http://localhost:1234/v1",
9
+ apiKey: "not-needed",
10
+ },
11
+ orchestrator: {
12
+ model: undefined,
13
+ provider: undefined,
14
+ },
15
+ moe: {
16
+ enabled: false,
17
+ },
18
+ tools: {
19
+ defaultTags: ["file", "code"],
20
+ enableOnDemand: true,
21
+ },
22
+ experts: {
23
+ code: {
24
+ model: "qwen/qwen3.5-9b",
25
+ tool_tags: ["file", "code", "shell"],
26
+ max_attempts: 3,
27
+ },
28
+ research: {
29
+ model: "qwen/qwen3.5-9b",
30
+ tool_tags: ["research"],
31
+ max_attempts: 3,
32
+ },
33
+ browser: {
34
+ model: "qwen/qwen3.5-9b",
35
+ tool_tags: ["browser", "vision"],
36
+ max_attempts: 3,
37
+ },
38
+ vision: {
39
+ model: "qwen/qwen3.5-9b",
40
+ tool_tags: ["browser", "vision", "file"],
41
+ max_attempts: 2,
42
+ },
43
+ },
44
+ contextWindow: 32768,
45
+ contextBudget: {
46
+ systemPrompt: 0.1,
47
+ responseReserve: 0.15,
48
+ compactionThreshold: 0.75,
49
+ },
50
+ modelLoad: {
51
+ autoLoad: false,
52
+ flashAttention: true,
53
+ offloadKvCacheToGpu: true,
54
+ },
55
+ retry: {
56
+ maxRetries: 3,
57
+ baseDelay: 1000,
58
+ maxDelay: 30000,
59
+ maxStreamRetries: 2,
60
+ noDataTimeoutMs: 180000,
61
+ },
62
+ maxToolIterations: 1000,
63
+ stuckThreshold: 6,
64
+ autoPlan: true,
65
+ showReasoning: false,
66
+ logLevel: "info",
67
+ locale: "en",
68
+ session: {
69
+ autoSave: true,
70
+ maxSessions: 50,
71
+ isolateMemory: false,
72
+ },
73
+ browser: {
74
+ enabled: true,
75
+ headless: true,
76
+ maxElements: 30,
77
+ maxContentChars: 2500,
78
+ maxConsoleEntries: 40,
79
+ maxConsoleLineChars: 400,
80
+ viewportWidth: 1280,
81
+ viewportHeight: 720,
82
+ navigationTimeout: 15000,
83
+ },
84
+ webSearch: {
85
+ enabled: true,
86
+ },
87
+ errorWebSearch: {
88
+ enabled: true,
89
+ threshold: 5,
90
+ maxResults: 5,
91
+ maxQueryChars: 200,
92
+ maxSearchesPerSession: 3,
93
+ requestTimeoutMs: 10000,
94
+ },
95
+ pricing: {
96
+ enabled: true,
97
+ overrides: {},
98
+ },
99
+ ui: {
100
+ spinner: true,
101
+ toolStyle: "inline",
102
+ toolComments: true,
103
+ showContextStats: false,
104
+ showCompaction: true,
105
+ },
106
+ mcpServers: {
107
+ context7: {
108
+ name: "context7",
109
+ transport: "http",
110
+ url: "https://mcp.context7.com/mcp",
111
+ headers: {
112
+ CONTEXT7_API_KEY: process.env.CONTEXT7_API_KEY || "",
113
+ },
114
+ enabled: true,
115
+ timeout: 30000,
116
+ },
117
+ },
118
+ security: DEFAULT_SECURITY_CONFIG,
119
+ sessionIsolation: {
120
+ enabled: false,
121
+ isolatePlugins: true,
122
+ isolateTempFiles: true,
123
+ },
124
+ skills: {
125
+ budget: 0.15,
126
+ },
127
+ subagent: {
128
+ resultMode: "text",
129
+ maxSummaryChars: 2000,
130
+ artifactsDir: ".mma/artifacts",
131
+ stableSystemPrompt: true,
132
+ maxParallelChunkQueries: 4,
133
+ },
134
+ lsp: DEFAULT_LSP_CONFIG,
135
+ updater: {
136
+ enabled: true,
137
+ checkOnStart: true,
138
+ autoInstall: true,
139
+ intervalMs: 0,
140
+ },
141
+ };
@@ -0,0 +1,179 @@
1
+ import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync } from "fs";
2
+ import { join } from "path";
3
+ /**
4
+ * Mapping: domain name → AppConfig keys that belong to it.
5
+ * Each domain becomes one JSON file in ~/.mma/config/<domain>.json.
6
+ */
7
+ export const CONFIG_DOMAINS = {
8
+ core: [
9
+ "version",
10
+ "model",
11
+ "contextWindow",
12
+ "contextBudget",
13
+ "modelLoad",
14
+ "maxToolIterations",
15
+ "stuckThreshold",
16
+ "autoPlan",
17
+ "showReasoning",
18
+ "logLevel",
19
+ "locale",
20
+ ],
21
+ provider: ["provider", "orchestrator", "retry", "pricing"],
22
+ moe: ["moe", "experts"],
23
+ security: ["security"],
24
+ browser: ["browser"],
25
+ search: ["webSearch", "errorWebSearch"],
26
+ tools: ["tools", "subagent"],
27
+ session: ["session", "sessionIsolation"],
28
+ ui: ["ui"],
29
+ mcp: ["mcpServers"],
30
+ lsp: ["lsp"],
31
+ skills: ["skills"],
32
+ updater: ["updater"],
33
+ };
34
+ /**
35
+ * JSON replacer that serializes RegExp objects as {__regex, source, flags}
36
+ * so they survive JSON.stringify/parse round-trips.
37
+ */
38
+ function regexReplacer(_key, value) {
39
+ if (value instanceof RegExp) {
40
+ return { __regex: true, source: value.source, flags: value.flags };
41
+ }
42
+ return value;
43
+ }
44
+ /**
45
+ * JSON reviver that restores RegExp objects serialized by regexReplacer.
46
+ */
47
+ function regexReviver(_key, value) {
48
+ if (value &&
49
+ typeof value === "object" &&
50
+ value.__regex === true) {
51
+ const { source, flags } = value;
52
+ try {
53
+ return new RegExp(source, flags);
54
+ }
55
+ catch {
56
+ return value;
57
+ }
58
+ }
59
+ return value;
60
+ }
61
+ /**
62
+ * Deep merge two objects. Arrays and scalars in source replace target.
63
+ */
64
+ function deepMerge(target, source) {
65
+ const result = { ...target };
66
+ for (const key of Object.keys(source)) {
67
+ const srcVal = source[key];
68
+ const tgtVal = target[key];
69
+ if (srcVal !== null &&
70
+ srcVal !== undefined &&
71
+ typeof srcVal === "object" &&
72
+ !Array.isArray(srcVal) &&
73
+ typeof tgtVal === "object" &&
74
+ !Array.isArray(tgtVal)) {
75
+ result[key] = deepMerge(tgtVal, srcVal);
76
+ }
77
+ else if (srcVal !== undefined) {
78
+ result[key] = srcVal;
79
+ }
80
+ }
81
+ return result;
82
+ }
83
+ /**
84
+ * Extract a domain subset from a full AppConfig.
85
+ */
86
+ export function extractDomain(config, domain) {
87
+ const keys = CONFIG_DOMAINS[domain];
88
+ if (!keys)
89
+ return {};
90
+ const subset = {};
91
+ for (const key of keys) {
92
+ if (config[key] !== undefined) {
93
+ subset[key] = config[key];
94
+ }
95
+ }
96
+ return subset;
97
+ }
98
+ /**
99
+ * Get the file path for a domain config file.
100
+ */
101
+ export function getDomainFilePath(configDir, domain) {
102
+ return join(configDir, "config", `${domain}.json`);
103
+ }
104
+ /**
105
+ * Check if domain files exist in config/ directory.
106
+ */
107
+ export function hasDomainFiles(configDir) {
108
+ const configDirPath = join(configDir, "config");
109
+ if (!existsSync(configDirPath))
110
+ return false;
111
+ try {
112
+ const files = readdirSync(configDirPath);
113
+ return files.some((f) => f.endsWith(".json"));
114
+ }
115
+ catch {
116
+ return false;
117
+ }
118
+ }
119
+ /**
120
+ * Load all domain files from ~/.mma/config/ and merge into a partial AppConfig.
121
+ * Each file is parsed with regexReviver to restore RegExp objects.
122
+ * Returns null if no domain files exist.
123
+ */
124
+ export function loadDomainFiles(configDir) {
125
+ const configDirPath = join(configDir, "config");
126
+ if (!existsSync(configDirPath))
127
+ return null;
128
+ let result = {};
129
+ let anyLoaded = false;
130
+ for (const domain of Object.keys(CONFIG_DOMAINS)) {
131
+ const filePath = getDomainFilePath(configDir, domain);
132
+ if (!existsSync(filePath))
133
+ continue;
134
+ try {
135
+ const raw = readFileSync(filePath, "utf-8");
136
+ const data = JSON.parse(raw, regexReviver);
137
+ result = deepMerge(result, data);
138
+ anyLoaded = true;
139
+ }
140
+ catch {
141
+ // Skip malformed domain files
142
+ }
143
+ }
144
+ return anyLoaded ? result : null;
145
+ }
146
+ /**
147
+ * Save a full AppConfig to domain files in ~/.mma/config/.
148
+ * Each domain's keys are extracted and written to config/<domain>.json.
149
+ * RegExp objects are serialized via regexReplacer.
150
+ */
151
+ export function saveDomainFiles(config, configDir) {
152
+ const configDirPath = join(configDir, "config");
153
+ mkdirSync(configDirPath, { recursive: true });
154
+ for (const [domain, keys] of Object.entries(CONFIG_DOMAINS)) {
155
+ const subset = {};
156
+ let hasContent = false;
157
+ for (const key of keys) {
158
+ if (config[key] !== undefined) {
159
+ subset[key] = config[key];
160
+ hasContent = true;
161
+ }
162
+ }
163
+ if (!hasContent)
164
+ continue;
165
+ const filePath = getDomainFilePath(configDir, domain);
166
+ writeFileSync(filePath, JSON.stringify(subset, regexReplacer, 2), "utf-8");
167
+ }
168
+ }
169
+ /**
170
+ * Determine which domain a config key belongs to.
171
+ * Returns the domain name or null if not found.
172
+ */
173
+ export function domainForKey(key) {
174
+ for (const [domain, keys] of Object.entries(CONFIG_DOMAINS)) {
175
+ if (keys.includes(key))
176
+ return domain;
177
+ }
178
+ return null;
179
+ }
@@ -0,0 +1,15 @@
1
+ export function getExpertConfig(config, tag) {
2
+ return config.experts?.[tag];
3
+ }
4
+ export function validateExpertConfig(config, allToolTags) {
5
+ const errors = [];
6
+ const knownTags = new Set(allToolTags);
7
+ for (const [name, expert] of Object.entries(config.experts || {})) {
8
+ for (const tag of expert.tool_tags) {
9
+ if (!knownTags.has(tag)) {
10
+ errors.push(`Expert "${name}" references unknown tool_tag "${tag}". Known tags: ${Array.from(knownTags).join(", ")}`);
11
+ }
12
+ }
13
+ }
14
+ return errors;
15
+ }
@@ -0,0 +1,4 @@
1
+ export { DEFAULTS } from "./defaults";
2
+ export { loadConfig, saveConfig, validateConfig, CONFIG_DIR, CONFIG_FILE, CONFIG_DOMAIN_DIR } from "./config";
3
+ export { getExpertConfig, validateExpertConfig } from "./experts";
4
+ export { CONFIG_DOMAINS, extractDomain, hasDomainFiles, loadDomainFiles, saveDomainFiles, getDomainFilePath, domainForKey, } from "./domains";