micro-models-agent 0.51.2 → 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +358 -358
- package/dist/cli/commands.js +447 -0
- package/dist/cli/completer.js +167 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/main.js +153 -0
- package/dist/cli/plugin-commands.js +36 -0
- package/dist/cli/repl-commands.js +761 -0
- package/dist/cli/repl.js +702 -0
- package/dist/cli/run-result.js +33 -0
- package/dist/cli/security-commands.js +164 -0
- package/dist/cli/setup.js +237 -0
- package/dist/config/config.js +276 -0
- package/dist/config/defaults.js +141 -0
- package/dist/config/domains.js +179 -0
- package/dist/config/experts.js +15 -0
- package/dist/config/index.js +4 -0
- package/dist/config/security.js +213 -0
- package/dist/config/types.js +1 -0
- package/dist/core/agent-moe.js +102 -0
- package/dist/core/agent.js +1018 -0
- package/dist/core/bootstrap.js +481 -0
- package/dist/core/crash-handler.js +51 -0
- package/dist/core/environment.js +199 -0
- package/dist/core/index.js +2 -0
- package/dist/core/prompt-builder.js +76 -0
- package/dist/core/session-logger.js +251 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.js +26 -0
- package/dist/core/workspace.js +76 -0
- package/dist/i18n/en.json +679 -0
- package/dist/i18n/index.js +46 -0
- package/dist/i18n/ru.json +679 -0
- package/dist/index.js +22 -0
- package/dist/llm/image-utils.js +143 -0
- package/dist/llm/index.js +4 -0
- package/dist/llm/model-loader.js +78 -0
- package/dist/llm/openai-compat.js +497 -0
- package/dist/llm/orchestrator.js +200 -0
- package/dist/llm/provider.js +10 -0
- package/dist/llm/response.js +39 -0
- package/dist/llm/token-counter.js +39 -0
- package/dist/llm/types.js +1 -0
- package/dist/logger/app-logger.js +189 -0
- package/dist/logger/file-log.js +151 -0
- package/dist/logger/index.js +1 -0
- package/dist/main.js +509 -100
- package/dist/migration/backup.js +45 -0
- package/dist/migration/detect.js +50 -0
- package/dist/migration/index.js +2 -0
- package/dist/modules/artifacts/store.js +61 -0
- package/dist/modules/browser/actions.js +76 -0
- package/dist/modules/browser/bridge-client.js +199 -0
- package/dist/modules/browser/bridge-path.js +10 -0
- package/dist/modules/browser/bridge-server.mjs +202 -202
- package/dist/modules/browser/cookie-store.js +24 -0
- package/dist/modules/browser/driver.js +136 -0
- package/dist/modules/browser/index.js +7 -0
- package/dist/modules/browser/module.js +29 -0
- package/dist/modules/browser/session.js +342 -0
- package/dist/modules/browser/snapshot.js +148 -0
- package/dist/modules/browser/types.js +12 -0
- package/dist/modules/certification/cli.js +213 -0
- package/dist/modules/certification/fact-checker.js +82 -0
- package/dist/modules/certification/loader.js +106 -0
- package/dist/modules/certification/manifest.js +58 -0
- package/dist/modules/certification/runner.js +245 -0
- package/dist/modules/certification/scenarios.js +407 -0
- package/dist/modules/certification/types.js +1 -0
- package/dist/modules/context/chunk-query.js +100 -0
- package/dist/modules/context/fact-extractor.js +168 -0
- package/dist/modules/context/history.js +15 -0
- package/dist/modules/context/index.js +1 -0
- package/dist/modules/context/manager.js +440 -0
- package/dist/modules/execution/audit-runners.js +206 -0
- package/dist/modules/execution/auditor.js +218 -0
- package/dist/modules/execution/execution-plugin.js +431 -0
- package/dist/modules/execution/index.js +8 -0
- package/dist/modules/execution/module.js +625 -0
- package/dist/modules/execution/moe-executor.js +304 -0
- package/dist/modules/execution/plan-coverage.js +68 -0
- package/dist/modules/execution/plan-persister.js +46 -0
- package/dist/modules/execution/plan-store.js +196 -0
- package/dist/modules/execution/plan-tool.js +677 -0
- package/dist/modules/execution/plan-validator.js +153 -0
- package/dist/modules/execution/planner.js +94 -0
- package/dist/modules/execution/stuck-detector.js +746 -0
- package/dist/modules/execution/tracker.js +69 -0
- package/dist/modules/execution/types.js +1 -0
- package/dist/modules/execution/verifier.js +235 -0
- package/dist/modules/execution/windows-commands.js +41 -0
- package/dist/modules/hallucination/confidence.js +66 -0
- package/dist/modules/hallucination/consistency.js +26 -0
- package/dist/modules/hallucination/detector.js +47 -0
- package/dist/modules/hallucination/factual.js +169 -0
- package/dist/modules/hallucination/index.js +5 -0
- package/dist/modules/hallucination/js-identifiers.js +262 -0
- package/dist/modules/hallucination/llm-judge.js +101 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/indexer/cache.js +40 -0
- package/dist/modules/indexer/index.js +3 -0
- package/dist/modules/indexer/module.js +246 -0
- package/dist/modules/indexer/project-profile.js +183 -0
- package/dist/modules/indexer/walker.js +101 -0
- package/dist/modules/lsp/check-tool.js +58 -0
- package/dist/modules/lsp/client.js +389 -0
- package/dist/modules/lsp/command.js +60 -0
- package/dist/modules/lsp/config.js +135 -0
- package/dist/modules/lsp/index.js +3 -0
- package/dist/modules/lsp/module.js +260 -0
- package/dist/modules/lsp/probe.js +86 -0
- package/dist/modules/lsp/project-root.js +32 -0
- package/dist/modules/lsp/startup-check.js +144 -0
- package/dist/modules/lsp/types.js +1 -0
- package/dist/modules/mcp/client.js +399 -0
- package/dist/modules/mcp/index.js +3 -0
- package/dist/modules/mcp/module.js +142 -0
- package/dist/modules/mcp/registry.js +15 -0
- package/dist/modules/memory/index.js +1 -0
- package/dist/modules/memory/module.js +96 -0
- package/dist/modules/memory/search.js +42 -0
- package/dist/modules/memory/store.js +69 -0
- package/dist/modules/pipelines/engine.js +60 -0
- package/dist/modules/pipelines/index.js +3 -0
- package/dist/modules/pipelines/parser.js +56 -0
- package/dist/modules/pipelines/template.js +14 -0
- package/dist/modules/plugins/builtin/lint-on-write.js +334 -0
- package/dist/modules/plugins/builtin/notify.js +9 -0
- package/dist/modules/plugins/index.js +1 -0
- package/dist/modules/plugins/loader.js +70 -0
- package/dist/modules/plugins/manager.js +261 -0
- package/dist/modules/plugins/types.js +1 -0
- package/dist/modules/pricing/index.js +61 -0
- package/dist/modules/pricing/prices.js +129 -0
- package/dist/modules/processes/detect.js +34 -0
- package/dist/modules/processes/index.js +2 -0
- package/dist/modules/processes/registry.js +327 -0
- package/dist/modules/processes/runner.js +23 -0
- package/dist/modules/providers/create.js +22 -0
- package/dist/modules/providers/fallback.js +79 -0
- package/dist/modules/providers/health.js +46 -0
- package/dist/modules/providers/index.js +5 -0
- package/dist/modules/providers/manager.js +161 -0
- package/dist/modules/providers/presets.js +128 -0
- package/dist/modules/providers/registry.js +22 -0
- package/dist/modules/providers/types.js +1 -0
- package/dist/modules/registry.js +48 -0
- package/dist/modules/security/audit-log.js +136 -0
- package/dist/modules/security/audit-notifier.js +292 -0
- package/dist/modules/security/command-validator.js +219 -0
- package/dist/modules/security/content-scanner.js +53 -0
- package/dist/modules/security/data-sanitizer.js +89 -0
- package/dist/modules/security/encryption.js +242 -0
- package/dist/modules/security/index.js +14 -0
- package/dist/modules/security/network-validator.js +88 -0
- package/dist/modules/security/path-validator.js +203 -0
- package/dist/modules/security/rate-limiter.js +119 -0
- package/dist/modules/security/security-policies.js +531 -0
- package/dist/modules/security/session-encryption.js +210 -0
- package/dist/modules/security/session-isolation.js +95 -0
- package/dist/modules/session/index.js +3 -0
- package/dist/modules/session/manager.js +172 -0
- package/dist/modules/session/module.js +24 -0
- package/dist/modules/session/store.js +222 -0
- package/dist/modules/session/types.js +1 -0
- package/dist/modules/skills/index.js +2 -0
- package/dist/modules/skills/loader.js +72 -0
- package/dist/modules/skills/matcher.js +27 -0
- package/dist/modules/skills/module.js +129 -0
- package/dist/modules/types.js +1 -0
- package/dist/modules/updater/checker.js +96 -0
- package/dist/modules/updater/index.js +2 -0
- package/dist/modules/updater/module.js +116 -0
- package/dist/modules/user-profile/compressor.js +16 -0
- package/dist/modules/user-profile/index.js +1 -0
- package/dist/modules/user-profile/profile.js +68 -0
- package/dist/skills/builtin/git.md +36 -36
- package/dist/skills/builtin/typescript.md +35 -35
- package/dist/tools/approve.js +33 -0
- package/dist/tools/attach-image.js +101 -0
- package/dist/tools/bash.js +519 -0
- package/dist/tools/browser.js +115 -0
- package/dist/tools/chunk-query.js +100 -0
- package/dist/tools/create-dir.js +56 -0
- package/dist/tools/delete-file.js +63 -0
- package/dist/tools/download-file.js +117 -0
- package/dist/tools/edit-file.js +80 -0
- package/dist/tools/enable-tools.js +59 -0
- package/dist/tools/executor.js +154 -0
- package/dist/tools/file-info.js +47 -0
- package/dist/tools/filter-tools.js +17 -0
- package/dist/tools/glob-tool.js +27 -0
- package/dist/tools/grep-tool.js +125 -0
- package/dist/tools/hidden-tools-block.js +37 -0
- package/dist/tools/index.js +78 -0
- package/dist/tools/list-dir.js +49 -0
- package/dist/tools/load-skill.js +43 -0
- package/dist/tools/mcp-call.js +69 -0
- package/dist/tools/move-file.js +86 -0
- package/dist/tools/path-utils.js +101 -0
- package/dist/tools/pipeline-run.js +145 -0
- package/dist/tools/preview.js +2 -0
- package/dist/tools/process-kill.js +40 -0
- package/dist/tools/process-list.js +37 -0
- package/dist/tools/process-log.js +54 -0
- package/dist/tools/question.js +141 -0
- package/dist/tools/read-file.js +179 -0
- package/dist/tools/recall.js +118 -0
- package/dist/tools/registry.js +47 -0
- package/dist/tools/remember.js +68 -0
- package/dist/tools/scope-check.js +32 -0
- package/dist/tools/search-history.js +85 -0
- package/dist/tools/subagent.js +196 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/user-input.js +123 -0
- package/dist/tools/web-browse.js +87 -0
- package/dist/tools/web-fetch.js +119 -0
- package/dist/tools/web-search.js +105 -0
- package/dist/tools/write-file.js +82 -0
- package/dist/ui/box.js +77 -0
- package/dist/ui/colors.js +4 -0
- package/dist/ui/diff.js +178 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/line-editor.js +822 -0
- package/dist/ui/line-math.js +73 -0
- package/dist/ui/md-formatter.js +212 -0
- package/dist/ui/output.js +13 -0
- package/dist/ui/plan-view.js +103 -0
- package/dist/ui/renderer.js +259 -0
- package/dist/ui/spinner.js +70 -0
- package/dist/ui/table.js +144 -0
- package/package.json +6 -4
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/llm/provider.ts
|
|
2
|
+
/** Helper: extract plain text from message content (for token counting, logging, etc.) */
|
|
3
|
+
export function getMessageText(content) {
|
|
4
|
+
if (typeof content === "string")
|
|
5
|
+
return content;
|
|
6
|
+
return content
|
|
7
|
+
.filter((p) => p.type === "text")
|
|
8
|
+
.map((p) => p.text)
|
|
9
|
+
.join("");
|
|
10
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function parseChunks(chunks) {
|
|
2
|
+
let text = "";
|
|
3
|
+
let reasoning;
|
|
4
|
+
const toolCalls = [];
|
|
5
|
+
let hasToolCalls = false;
|
|
6
|
+
for (const chunk of chunks) {
|
|
7
|
+
if (chunk.type === "text" && chunk.content) {
|
|
8
|
+
text += chunk.content;
|
|
9
|
+
}
|
|
10
|
+
if (chunk.type === "reasoning" && chunk.content) {
|
|
11
|
+
reasoning = (reasoning || "") + chunk.content;
|
|
12
|
+
}
|
|
13
|
+
if (chunk.type === "tool_call" && chunk.toolCall) {
|
|
14
|
+
hasToolCalls = true;
|
|
15
|
+
let parsedArgs;
|
|
16
|
+
try {
|
|
17
|
+
parsedArgs = JSON.parse(chunk.toolCall.arguments);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
parsedArgs = {};
|
|
21
|
+
}
|
|
22
|
+
toolCalls.push({
|
|
23
|
+
id: chunk.toolCall.id,
|
|
24
|
+
name: chunk.toolCall.name,
|
|
25
|
+
arguments: parsedArgs,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (hasToolCalls) {
|
|
30
|
+
return { type: "tool_call", calls: toolCalls, reasoning };
|
|
31
|
+
}
|
|
32
|
+
if (reasoning && !text) {
|
|
33
|
+
return { type: "reasoning", content: reasoning };
|
|
34
|
+
}
|
|
35
|
+
if (!text && !reasoning) {
|
|
36
|
+
return { type: "empty" };
|
|
37
|
+
}
|
|
38
|
+
return { type: "text", content: text || "", reasoning };
|
|
39
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/llm/token-counter.ts
|
|
2
|
+
import { encodingForModel, getEncoding } from "js-tiktoken";
|
|
3
|
+
import { getMessageText } from "./provider";
|
|
4
|
+
export class TokenCounter {
|
|
5
|
+
encoder;
|
|
6
|
+
constructor(model = "gpt-4o") {
|
|
7
|
+
try {
|
|
8
|
+
this.encoder = encodingForModel(model);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
// Fallback to cl100k_base for unknown models
|
|
12
|
+
this.encoder = getEncoding("cl100k_base");
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
count(text) {
|
|
16
|
+
if (!text)
|
|
17
|
+
return 0;
|
|
18
|
+
return this.encoder.encode(text).length;
|
|
19
|
+
}
|
|
20
|
+
countMessages(messages) {
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (const msg of messages) {
|
|
23
|
+
// ~4 tokens per message overhead (role, boundaries)
|
|
24
|
+
total += 4;
|
|
25
|
+
const text = getMessageText(msg.content);
|
|
26
|
+
total += this.count(text);
|
|
27
|
+
if (msg.role === "tool")
|
|
28
|
+
total += 2; // tool_call_id
|
|
29
|
+
}
|
|
30
|
+
total += 2; // assistant priming
|
|
31
|
+
return total;
|
|
32
|
+
}
|
|
33
|
+
encode(text) {
|
|
34
|
+
return this.encoder.encode(text);
|
|
35
|
+
}
|
|
36
|
+
decode(tokens) {
|
|
37
|
+
return this.encoder.decode(tokens);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, existsSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { sanitizeLogMessage } from "../modules/security/data-sanitizer";
|
|
4
|
+
import { FileLogWriter } from "./file-log";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
7
|
+
const LEVEL_COLORS = {
|
|
8
|
+
debug: (s) => pc.dim(s),
|
|
9
|
+
info: (s) => s,
|
|
10
|
+
warn: (s) => pc.yellow(s),
|
|
11
|
+
error: (s) => pc.red(s),
|
|
12
|
+
};
|
|
13
|
+
function isColorEnabled() {
|
|
14
|
+
return !process.env.NO_COLOR && !process.env.CI && process.stdout.isTTY === true;
|
|
15
|
+
}
|
|
16
|
+
export class Logger {
|
|
17
|
+
level;
|
|
18
|
+
prefix;
|
|
19
|
+
logDir = null;
|
|
20
|
+
sessionDir = null;
|
|
21
|
+
fileLog;
|
|
22
|
+
constructor(level = "info", prefix = "") {
|
|
23
|
+
this.level = level;
|
|
24
|
+
this.prefix = prefix;
|
|
25
|
+
this.fileLog = new FileLogWriter();
|
|
26
|
+
}
|
|
27
|
+
setLevel(level) {
|
|
28
|
+
this.level = level;
|
|
29
|
+
}
|
|
30
|
+
setLogDir(dir) {
|
|
31
|
+
this.logDir = dir;
|
|
32
|
+
if (!existsSync(dir)) {
|
|
33
|
+
mkdirSync(dir, { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
this.fileLog.setLogDir(dir);
|
|
36
|
+
}
|
|
37
|
+
setSessionDir(dir) {
|
|
38
|
+
this.sessionDir = dir;
|
|
39
|
+
if (!existsSync(dir)) {
|
|
40
|
+
mkdirSync(dir, { recursive: true });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
clearSessionDir() {
|
|
44
|
+
this.sessionDir = null;
|
|
45
|
+
}
|
|
46
|
+
/** Open a per-session `.log` file (legacy v1 behavior). */
|
|
47
|
+
initSessionLog(sessionId) {
|
|
48
|
+
this.fileLog.initSessionLog(sessionId);
|
|
49
|
+
}
|
|
50
|
+
/** Fall back to daily log files when no session is active. */
|
|
51
|
+
closeSessionLog() {
|
|
52
|
+
this.fileLog.closeSessionLog();
|
|
53
|
+
}
|
|
54
|
+
getLogPath() {
|
|
55
|
+
return this.fileLog.getLogPath();
|
|
56
|
+
}
|
|
57
|
+
cleanupOldLogs(maxDays, maxFiles) {
|
|
58
|
+
this.fileLog.cleanupOldLogs(maxDays, maxFiles);
|
|
59
|
+
}
|
|
60
|
+
// --- Tagged helpers (legacy v1 API) ---
|
|
61
|
+
logLLMRequest(model, messagesCount, promptPreview, caller) {
|
|
62
|
+
this.fileLog.logLLMRequest(model, messagesCount, sanitizeLogMessage(promptPreview), caller);
|
|
63
|
+
}
|
|
64
|
+
logLLMResponse(model, responseLength, genTimeMs, error, caller) {
|
|
65
|
+
this.fileLog.logLLMResponse(model, responseLength, genTimeMs, error, caller);
|
|
66
|
+
}
|
|
67
|
+
logToolCall(tool, preview, result) {
|
|
68
|
+
this.fileLog.logToolCall(tool, sanitizeLogMessage(preview), result !== undefined ? sanitizeLogMessage(result) : undefined);
|
|
69
|
+
}
|
|
70
|
+
logToolOutput(tool, output, exitCode) {
|
|
71
|
+
this.fileLog.logToolOutput(tool, sanitizeLogMessage(output), exitCode);
|
|
72
|
+
}
|
|
73
|
+
logREPL(tag, content) {
|
|
74
|
+
this.fileLog.logREPL(tag, sanitizeLogMessage(content));
|
|
75
|
+
}
|
|
76
|
+
child(prefix) {
|
|
77
|
+
const childLogger = new Logger(this.level, this.prefix ? `${this.prefix}:${prefix}` : prefix);
|
|
78
|
+
if (this.logDir)
|
|
79
|
+
childLogger.setLogDir(this.logDir);
|
|
80
|
+
if (this.sessionDir)
|
|
81
|
+
childLogger.setSessionDir(this.sessionDir);
|
|
82
|
+
return childLogger;
|
|
83
|
+
}
|
|
84
|
+
debug(msg, meta) {
|
|
85
|
+
this.log("debug", msg, meta);
|
|
86
|
+
}
|
|
87
|
+
info(msg, meta) {
|
|
88
|
+
this.log("info", msg, meta);
|
|
89
|
+
}
|
|
90
|
+
warn(msg, meta) {
|
|
91
|
+
this.log("warn", msg, meta);
|
|
92
|
+
}
|
|
93
|
+
error(msg, meta) {
|
|
94
|
+
this.log("error", msg, meta);
|
|
95
|
+
}
|
|
96
|
+
log(level, msg, meta) {
|
|
97
|
+
if (LEVELS[level] < LEVELS[this.level])
|
|
98
|
+
return;
|
|
99
|
+
// Sanitize log message to remove sensitive data
|
|
100
|
+
const sanitizedMsg = sanitizeLogMessage(msg);
|
|
101
|
+
const sanitizedMeta = meta ? this.sanitizeMeta(meta) : undefined;
|
|
102
|
+
const ts = new Date().toISOString();
|
|
103
|
+
const prefix = this.prefix ? ` [${this.prefix}]` : "";
|
|
104
|
+
const metaStr = sanitizedMeta ? ` ${JSON.stringify(sanitizedMeta)}` : "";
|
|
105
|
+
const line = `[${level.toUpperCase()}]${prefix} ${ts} — ${sanitizedMsg}${metaStr}`;
|
|
106
|
+
console.log(isColorEnabled() ? LEVEL_COLORS[level](line) : line);
|
|
107
|
+
// Legacy v1 behavior: also append to the tagged `.log` file (session or daily).
|
|
108
|
+
this.fileLog.log(level.toUpperCase(), this.prefix || "MMA", `${ts} — ${sanitizedMsg}${metaStr}`);
|
|
109
|
+
const logTarget = this.sessionDir ?? this.logDir;
|
|
110
|
+
if (logTarget) {
|
|
111
|
+
try {
|
|
112
|
+
appendFileSync(join(logTarget, "app.jsonl"), JSON.stringify({
|
|
113
|
+
level,
|
|
114
|
+
ts,
|
|
115
|
+
prefix: this.prefix,
|
|
116
|
+
msg: sanitizedMsg,
|
|
117
|
+
meta: sanitizedMeta ?? null,
|
|
118
|
+
}) + "\n", "utf-8");
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
/* file logging is best-effort */
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Sanitize metadata object to remove sensitive data
|
|
127
|
+
*/
|
|
128
|
+
sanitizeMeta(meta) {
|
|
129
|
+
const sanitized = {};
|
|
130
|
+
for (const [key, value] of Object.entries(meta)) {
|
|
131
|
+
if (typeof value === "string") {
|
|
132
|
+
sanitized[key] = sanitizeLogMessage(value);
|
|
133
|
+
}
|
|
134
|
+
else if (typeof value === "object" && value !== null) {
|
|
135
|
+
sanitized[key] = this.sanitizeMeta(value);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
sanitized[key] = value;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return sanitized;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Write a structured record to app.jsonl only (no console / .log output).
|
|
145
|
+
* Used for machine-readable reports (environment, etc.) that would be too
|
|
146
|
+
* noisy on the terminal but are valuable for cross-device diagnosis.
|
|
147
|
+
*/
|
|
148
|
+
logStructured(type, data) {
|
|
149
|
+
const logTarget = this.sessionDir ?? this.logDir;
|
|
150
|
+
if (!logTarget)
|
|
151
|
+
return;
|
|
152
|
+
try {
|
|
153
|
+
appendFileSync(join(logTarget, "app.jsonl"), JSON.stringify({
|
|
154
|
+
level: "info",
|
|
155
|
+
ts: new Date().toISOString(),
|
|
156
|
+
type,
|
|
157
|
+
meta: this.sanitizeMeta(data),
|
|
158
|
+
}) + "\n", "utf-8");
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
/* file logging is best-effort */
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Write a log entry to app.jsonl only — no console output, no .log file.
|
|
166
|
+
* Use this for noisy subsystems (LSP, probes) whose warnings should be
|
|
167
|
+
* diagnosable from session files but not pollute the user's terminal.
|
|
168
|
+
*/
|
|
169
|
+
logSilent(level, msg, meta) {
|
|
170
|
+
const logTarget = this.sessionDir ?? this.logDir;
|
|
171
|
+
if (!logTarget)
|
|
172
|
+
return;
|
|
173
|
+
const ts = new Date().toISOString();
|
|
174
|
+
const sanitizedMsg = sanitizeLogMessage(msg);
|
|
175
|
+
const sanitizedMeta = meta ? this.sanitizeMeta(meta) : undefined;
|
|
176
|
+
try {
|
|
177
|
+
appendFileSync(join(logTarget, "app.jsonl"), JSON.stringify({
|
|
178
|
+
level,
|
|
179
|
+
ts,
|
|
180
|
+
prefix: this.prefix,
|
|
181
|
+
msg: sanitizedMsg,
|
|
182
|
+
meta: sanitizedMeta ?? null,
|
|
183
|
+
}) + "\n", "utf-8");
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
/* file logging is best-effort */
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, existsSync, readdirSync, renameSync, statSync, unlinkSync, } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
const MAX_LOG_SIZE = 5 * 1024 * 1024;
|
|
4
|
+
const MAX_LOG_FILES = 30;
|
|
5
|
+
const DEFAULT_MAX_DAYS = 30;
|
|
6
|
+
const TOOL_OUTPUT_LIMIT = 2000;
|
|
7
|
+
/**
|
|
8
|
+
* Tagged file logger (legacy MMA v1 behavior).
|
|
9
|
+
*
|
|
10
|
+
* Writes human-readable log lines to `~/.mma/logs/`:
|
|
11
|
+
* - `<sessionId>.log` while a session is active
|
|
12
|
+
* - `YYYY-MM-DD.log` for everything outside a session
|
|
13
|
+
*
|
|
14
|
+
* Features restored from v1:
|
|
15
|
+
* - size-based rotation (5 MB → file.log.1, file.log.2, ...)
|
|
16
|
+
* - cleanup of old logs (age + max file count)
|
|
17
|
+
* - tagged helpers: LLM request/response, tool calls, REPL
|
|
18
|
+
*/
|
|
19
|
+
export class FileLogWriter {
|
|
20
|
+
logDir = null;
|
|
21
|
+
sessionLogPath = null;
|
|
22
|
+
setLogDir(dir) {
|
|
23
|
+
this.logDir = dir;
|
|
24
|
+
if (!existsSync(dir)) {
|
|
25
|
+
mkdirSync(dir, { recursive: true });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Open a per-session log file. Triggers cleanup of old logs. */
|
|
29
|
+
initSessionLog(sessionId) {
|
|
30
|
+
if (!this.logDir)
|
|
31
|
+
return;
|
|
32
|
+
if (!existsSync(this.logDir)) {
|
|
33
|
+
mkdirSync(this.logDir, { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
this.sessionLogPath = join(this.logDir, `${sessionId}.log`);
|
|
36
|
+
this.cleanupOldLogs();
|
|
37
|
+
}
|
|
38
|
+
/** Fall back to daily log files when no session is active. */
|
|
39
|
+
closeSessionLog() {
|
|
40
|
+
this.sessionLogPath = null;
|
|
41
|
+
}
|
|
42
|
+
getLogPath() {
|
|
43
|
+
if (this.sessionLogPath)
|
|
44
|
+
return this.sessionLogPath;
|
|
45
|
+
if (!this.logDir)
|
|
46
|
+
return "";
|
|
47
|
+
const date = new Date().toISOString().slice(0, 10);
|
|
48
|
+
return join(this.logDir, `${date}.log`);
|
|
49
|
+
}
|
|
50
|
+
/** Rotate the file if it exceeds the size limit. */
|
|
51
|
+
rotateIfNeeded(filePath) {
|
|
52
|
+
if (!existsSync(filePath))
|
|
53
|
+
return;
|
|
54
|
+
const size = statSync(filePath).size;
|
|
55
|
+
if (size < MAX_LOG_SIZE)
|
|
56
|
+
return;
|
|
57
|
+
let idx = 1;
|
|
58
|
+
while (existsSync(`${filePath}.${idx}`))
|
|
59
|
+
idx++;
|
|
60
|
+
renameSync(filePath, `${filePath}.${idx}`);
|
|
61
|
+
}
|
|
62
|
+
log(level, tag, message) {
|
|
63
|
+
const fp = this.getLogPath();
|
|
64
|
+
if (!fp)
|
|
65
|
+
return;
|
|
66
|
+
if (this.logDir && !existsSync(this.logDir)) {
|
|
67
|
+
mkdirSync(this.logDir, { recursive: true });
|
|
68
|
+
}
|
|
69
|
+
this.rotateIfNeeded(fp);
|
|
70
|
+
const line = `[${new Date().toISOString()}] [${level}] [${tag}] ${message}\n`;
|
|
71
|
+
try {
|
|
72
|
+
appendFileSync(fp, line, "utf-8");
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
/* file logging is best-effort */
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
logLLMRequest(model, messagesCount, promptPreview, caller) {
|
|
79
|
+
const tag = caller ? `LLM/${caller}` : "LLM";
|
|
80
|
+
this.log("INFO", tag, `→ ${model} | ${messagesCount} messages | "${promptPreview.slice(0, 100)}"`);
|
|
81
|
+
}
|
|
82
|
+
logLLMResponse(model, responseLength, genTimeMs, error, caller) {
|
|
83
|
+
const tag = caller ? `LLM/${caller}` : "LLM";
|
|
84
|
+
if (error) {
|
|
85
|
+
this.log("ERROR", tag, `← ${model} | ${responseLength} chars | ${genTimeMs}ms | ERROR: ${error}`);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.log("INFO", tag, `← ${model} | ${responseLength} chars | ${genTimeMs}ms`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
logToolCall(tool, preview, result) {
|
|
92
|
+
const previewTrimmed = preview.length > 200 ? preview.slice(0, 200) + "..." : preview;
|
|
93
|
+
if (result !== undefined) {
|
|
94
|
+
const resultTrimmed = result.length > 200 ? result.slice(0, 200) + "..." : result;
|
|
95
|
+
this.log("INFO", "TOOL", `${tool} | ${previewTrimmed} → ${resultTrimmed}`);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.log("INFO", "TOOL", `${tool} | ${previewTrimmed}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
logToolOutput(tool, output, exitCode) {
|
|
102
|
+
const trimmed = output.length > TOOL_OUTPUT_LIMIT
|
|
103
|
+
? output.slice(0, TOOL_OUTPUT_LIMIT) +
|
|
104
|
+
`\n... (truncated ${output.length - TOOL_OUTPUT_LIMIT} chars)`
|
|
105
|
+
: output;
|
|
106
|
+
this.log("DEBUG", "TOOL_OUTPUT", `${tool} exit:${exitCode}\n${trimmed}`);
|
|
107
|
+
}
|
|
108
|
+
logREPL(tag, content) {
|
|
109
|
+
const trimmed = content.length > 400 ? content.slice(0, 400) + "..." : content;
|
|
110
|
+
this.log("INFO", `REPL/${tag}`, trimmed);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Clean up old log files.
|
|
114
|
+
* - Deletes files older than maxDays
|
|
115
|
+
* - Keeps at most maxFiles most recent .log files
|
|
116
|
+
*/
|
|
117
|
+
cleanupOldLogs(maxDays = DEFAULT_MAX_DAYS, maxFiles = MAX_LOG_FILES) {
|
|
118
|
+
if (!this.logDir || !existsSync(this.logDir))
|
|
119
|
+
return;
|
|
120
|
+
const now = Date.now();
|
|
121
|
+
const list = () => readdirSync(this.logDir)
|
|
122
|
+
.filter((f) => f.endsWith(".log") || /\.log\.\d+$/.test(f))
|
|
123
|
+
.map((f) => ({
|
|
124
|
+
name: f,
|
|
125
|
+
path: join(this.logDir, f),
|
|
126
|
+
mtime: statSync(join(this.logDir, f)).mtimeMs,
|
|
127
|
+
}))
|
|
128
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
129
|
+
// 1. Delete by age.
|
|
130
|
+
for (const f of list()) {
|
|
131
|
+
if (now - f.mtime > maxDays * 24 * 60 * 60 * 1000) {
|
|
132
|
+
try {
|
|
133
|
+
unlinkSync(f.path);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
/* ignore */
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// 2. Delete excess (fresh but more than maxFiles).
|
|
141
|
+
const remaining = list();
|
|
142
|
+
for (let i = maxFiles; i < remaining.length; i++) {
|
|
143
|
+
try {
|
|
144
|
+
unlinkSync(remaining[i].path);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
/* ignore */
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Logger } from "./app-logger";
|