micro-models-agent 0.57.0 → 0.57.1
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/CHANGELOG.md +481 -476
- package/README.md +358 -358
- package/dist/certification/certifications.json +493 -493
- 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/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 +219 -219
- 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 +51 -51
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { semverGt } from "../updater/checker";
|
|
2
|
+
export class PluginManager {
|
|
3
|
+
plugins = [];
|
|
4
|
+
hostBridge = null;
|
|
5
|
+
/**
|
|
6
|
+
* Register the host bridge (set once by the REPL). It is merged into every
|
|
7
|
+
* hook context as `ctx.hostBridge` unless the caller already provided one.
|
|
8
|
+
*/
|
|
9
|
+
setHostBridge(bridge) {
|
|
10
|
+
this.hostBridge = bridge;
|
|
11
|
+
}
|
|
12
|
+
/** Merge the host bridge into a hook context (caller-provided value wins). */
|
|
13
|
+
withHost(ctx) {
|
|
14
|
+
if (!ctx || typeof ctx !== "object")
|
|
15
|
+
return ctx;
|
|
16
|
+
if (!this.hostBridge || ctx.hostBridge !== undefined) {
|
|
17
|
+
return ctx;
|
|
18
|
+
}
|
|
19
|
+
return { ...ctx, hostBridge: this.hostBridge };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Register a plugin. Plugins with the same `name` are deduplicated — the
|
|
23
|
+
* higher `version` wins (unversioned plugins count as "0.0.0"). The loser is
|
|
24
|
+
* discarded; `skipped_older` lets the caller warn about the dropped copy.
|
|
25
|
+
*/
|
|
26
|
+
register(plugin, source) {
|
|
27
|
+
if (source)
|
|
28
|
+
plugin.source = source;
|
|
29
|
+
const existing = this.plugins.find((p) => p.name === plugin.name);
|
|
30
|
+
if (existing) {
|
|
31
|
+
if (semverGt(plugin.version || "0.0.0", existing.version || "0.0.0")) {
|
|
32
|
+
this.plugins = this.plugins.filter((p) => p.name !== plugin.name);
|
|
33
|
+
this.plugins.push(plugin);
|
|
34
|
+
this.sort();
|
|
35
|
+
return { status: "registered" };
|
|
36
|
+
}
|
|
37
|
+
return { status: "skipped_older", existing };
|
|
38
|
+
}
|
|
39
|
+
this.plugins.push(plugin);
|
|
40
|
+
this.sort();
|
|
41
|
+
return { status: "registered" };
|
|
42
|
+
}
|
|
43
|
+
sort() {
|
|
44
|
+
this.plugins.sort((a, b) => (b.priority || 0) - (a.priority || 0));
|
|
45
|
+
}
|
|
46
|
+
getAllPlugins() {
|
|
47
|
+
return [...this.plugins];
|
|
48
|
+
}
|
|
49
|
+
/** Detailed plugin list (name, version, source) for REPL / CLI display. */
|
|
50
|
+
getPluginInfos() {
|
|
51
|
+
return [...this.plugins].map((plugin) => ({ plugin, source: plugin.source }));
|
|
52
|
+
}
|
|
53
|
+
/** Find a registered plugin by name, or null. */
|
|
54
|
+
getPlugin(name) {
|
|
55
|
+
return this.plugins.find((p) => p.name === name) ?? null;
|
|
56
|
+
}
|
|
57
|
+
runOnBuildPrompt() {
|
|
58
|
+
return this.plugins.map((p) => {
|
|
59
|
+
try {
|
|
60
|
+
return p.onBuildPrompt?.() ?? null;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async runOnBeforeTool(rawCtx, call) {
|
|
68
|
+
const ctx = this.withHost(rawCtx);
|
|
69
|
+
for (const plugin of this.plugins) {
|
|
70
|
+
if (plugin.onBeforeTool) {
|
|
71
|
+
try {
|
|
72
|
+
const result = await plugin.onBeforeTool(ctx, call);
|
|
73
|
+
if (result === false)
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
/* error isolation */
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
async runOnAfterTool(rawCtx, call, result) {
|
|
84
|
+
const ctx = this.withHost(rawCtx);
|
|
85
|
+
for (const plugin of this.plugins) {
|
|
86
|
+
if (plugin.onAfterTool) {
|
|
87
|
+
try {
|
|
88
|
+
await plugin.onAfterTool(ctx, call, result);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
/* error isolation */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async runOnError(rawCtx, error) {
|
|
97
|
+
const ctx = this.withHost(rawCtx);
|
|
98
|
+
for (const plugin of this.plugins) {
|
|
99
|
+
if (plugin.onError) {
|
|
100
|
+
try {
|
|
101
|
+
await plugin.onError(ctx, error);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
/* error isolation */
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
runOnSessionStart(rawCtx) {
|
|
110
|
+
const ctx = this.withHost(rawCtx);
|
|
111
|
+
for (const plugin of this.plugins) {
|
|
112
|
+
if (plugin.onSessionStart) {
|
|
113
|
+
try {
|
|
114
|
+
plugin.onSessionStart(ctx);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
/* error isolation */
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
runOnBeforeThink(rawCtx) {
|
|
123
|
+
const ctx = this.withHost(rawCtx);
|
|
124
|
+
for (const plugin of this.plugins) {
|
|
125
|
+
if (plugin.onBeforeThink) {
|
|
126
|
+
try {
|
|
127
|
+
plugin.onBeforeThink(ctx);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
/* error isolation */
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
runOnAfterThink(rawCtx, response) {
|
|
136
|
+
const ctx = this.withHost(rawCtx);
|
|
137
|
+
for (const plugin of this.plugins) {
|
|
138
|
+
if (plugin.onAfterThink) {
|
|
139
|
+
try {
|
|
140
|
+
plugin.onAfterThink(ctx, response);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
/* error isolation */
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
runOnSessionEnd(rawCtx) {
|
|
149
|
+
const ctx = this.withHost(rawCtx);
|
|
150
|
+
for (const plugin of this.plugins) {
|
|
151
|
+
if (plugin.onSessionEnd) {
|
|
152
|
+
try {
|
|
153
|
+
plugin.onSessionEnd(ctx);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
/* error isolation */
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
runOnToolCall(rawCtx) {
|
|
162
|
+
const ctx = this.withHost(rawCtx);
|
|
163
|
+
for (const plugin of this.plugins) {
|
|
164
|
+
if (plugin.onToolCall) {
|
|
165
|
+
try {
|
|
166
|
+
plugin.onToolCall(ctx);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
/* error isolation */
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
runOnPhase(rawCtx, phase) {
|
|
175
|
+
const ctx = this.withHost(rawCtx);
|
|
176
|
+
for (const plugin of this.plugins) {
|
|
177
|
+
if (plugin.onPhase) {
|
|
178
|
+
try {
|
|
179
|
+
plugin.onPhase(ctx, phase);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
/* error isolation */
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
runOnToolStart(rawCtx, call) {
|
|
188
|
+
const ctx = this.withHost(rawCtx);
|
|
189
|
+
for (const plugin of this.plugins) {
|
|
190
|
+
if (plugin.onToolStart) {
|
|
191
|
+
try {
|
|
192
|
+
plugin.onToolStart(ctx, call);
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
/* error isolation */
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
runOnToolEnd(rawCtx, call, result, durationMs) {
|
|
201
|
+
const ctx = this.withHost(rawCtx);
|
|
202
|
+
for (const plugin of this.plugins) {
|
|
203
|
+
if (plugin.onToolEnd) {
|
|
204
|
+
try {
|
|
205
|
+
plugin.onToolEnd(ctx, call, result, durationMs);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
/* error isolation */
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** Pipe a text chunk through plugin transforms; returns the (possibly replaced) chunk. */
|
|
214
|
+
runOnText(rawCtx, chunk) {
|
|
215
|
+
const ctx = this.withHost(rawCtx);
|
|
216
|
+
for (const plugin of this.plugins) {
|
|
217
|
+
if (plugin.onText) {
|
|
218
|
+
try {
|
|
219
|
+
const out = plugin.onText(ctx, chunk);
|
|
220
|
+
if (typeof out === "string")
|
|
221
|
+
chunk = out;
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
/* error isolation */
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return chunk;
|
|
229
|
+
}
|
|
230
|
+
/** Pipe a meta chunk through plugin transforms; returns the (possibly replaced) chunk. */
|
|
231
|
+
runOnMeta(rawCtx, chunk) {
|
|
232
|
+
const ctx = this.withHost(rawCtx);
|
|
233
|
+
for (const plugin of this.plugins) {
|
|
234
|
+
if (plugin.onMeta) {
|
|
235
|
+
try {
|
|
236
|
+
const out = plugin.onMeta(ctx, chunk);
|
|
237
|
+
if (typeof out === "string")
|
|
238
|
+
chunk = out;
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
/* error isolation */
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return chunk;
|
|
246
|
+
}
|
|
247
|
+
/** Dispatch a completed-run summary to all plugins (once per Agent.run()). */
|
|
248
|
+
runOnTurnEnd(rawCtx, summary) {
|
|
249
|
+
const ctx = this.withHost(rawCtx);
|
|
250
|
+
for (const plugin of this.plugins) {
|
|
251
|
+
if (plugin.onTurnEnd) {
|
|
252
|
+
try {
|
|
253
|
+
plugin.onTurnEnd(ctx, summary);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
/* error isolation */
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { calculateCost, formatCost } from "./prices";
|
|
2
|
+
/**
|
|
3
|
+
* Accumulates per-call LLM cost for a session, attributed to the provider
|
|
4
|
+
* that served the call. Call `record()` after each LLM call with the
|
|
5
|
+
* (estimated or API) token usage and the provider name; read `total` for the
|
|
6
|
+
* running total, `totalFor(provider)` / `breakdown()` for per-provider
|
|
7
|
+
* attribution. When the model has no known price, `record` is a no-op.
|
|
8
|
+
*/
|
|
9
|
+
export class CostTracker {
|
|
10
|
+
config;
|
|
11
|
+
model;
|
|
12
|
+
_total = 0;
|
|
13
|
+
_known = false;
|
|
14
|
+
byProvider = new Map();
|
|
15
|
+
constructor(model, config) {
|
|
16
|
+
this.model = model;
|
|
17
|
+
this.config = config;
|
|
18
|
+
}
|
|
19
|
+
/** Update the model (on /model switch). Later records use the new price. */
|
|
20
|
+
setModel(model) {
|
|
21
|
+
this.model = model;
|
|
22
|
+
}
|
|
23
|
+
/** Record a single LLM call's token usage, attributed to `provider`. */
|
|
24
|
+
record(promptTokens, completionTokens, provider) {
|
|
25
|
+
const key = provider || "unknown";
|
|
26
|
+
const cost = calculateCost(this.model, promptTokens, completionTokens, this.config);
|
|
27
|
+
if (cost === undefined)
|
|
28
|
+
return;
|
|
29
|
+
this._known = true;
|
|
30
|
+
this._total += cost;
|
|
31
|
+
let entry = this.byProvider.get(key);
|
|
32
|
+
if (!entry) {
|
|
33
|
+
entry = { total: 0, known: false };
|
|
34
|
+
this.byProvider.set(key, entry);
|
|
35
|
+
}
|
|
36
|
+
entry.known = true;
|
|
37
|
+
entry.total += cost;
|
|
38
|
+
}
|
|
39
|
+
/** Running total cost in USD, or undefined if no model price is known. */
|
|
40
|
+
get total() {
|
|
41
|
+
return this._known ? this._total : undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Total for one provider, or undefined when nothing was priced for it. */
|
|
44
|
+
totalFor(provider) {
|
|
45
|
+
const entry = this.byProvider.get(provider);
|
|
46
|
+
return entry && entry.known ? entry.total : undefined;
|
|
47
|
+
}
|
|
48
|
+
/** Per-provider totals sorted by cost descending (priced providers only). */
|
|
49
|
+
breakdown() {
|
|
50
|
+
const entries = [];
|
|
51
|
+
for (const [provider, entry] of this.byProvider) {
|
|
52
|
+
if (entry.known)
|
|
53
|
+
entries.push({ provider, cost: entry.total });
|
|
54
|
+
}
|
|
55
|
+
return entries.sort((a, b) => b.cost - a.cost);
|
|
56
|
+
}
|
|
57
|
+
/** Human-readable total, or undefined when pricing is unavailable. */
|
|
58
|
+
get formatted() {
|
|
59
|
+
return this._known ? formatCost(this._total) : undefined;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in price table (USD per 1M tokens) for OpenCode Zen / Go models.
|
|
3
|
+
* Values from https://opencode.ai/docs/zen/ and /docs/go/ (August 2026).
|
|
4
|
+
* Model ids are stored WITHOUT the `opencode/` or `opencode-go/` prefix so a
|
|
5
|
+
* single table serves both providers; lookup strips the prefix.
|
|
6
|
+
*/
|
|
7
|
+
const ZEN_PRICES = {
|
|
8
|
+
// Free models
|
|
9
|
+
"big-pickle": { input: 0, output: 0 },
|
|
10
|
+
"mimo-v2.5-free": { input: 0, output: 0 },
|
|
11
|
+
"hy3-free": { input: 0, output: 0 },
|
|
12
|
+
"nemotron-3-ultra-free": { input: 0, output: 0 },
|
|
13
|
+
"nemotron-3.5-lightning-free": { input: 0, output: 0 },
|
|
14
|
+
"muse-spark-1.2-contributor-free": { input: 0, output: 0 },
|
|
15
|
+
// MiniMax
|
|
16
|
+
"minimax-m3": { input: 0.3, output: 1.2 },
|
|
17
|
+
"minimax-m2.7": { input: 0.3, output: 1.2 },
|
|
18
|
+
"minimax-m2.5": { input: 0.3, output: 1.2 },
|
|
19
|
+
// GLM
|
|
20
|
+
"glm-5.2": { input: 1.4, output: 4.4 },
|
|
21
|
+
"glm-5.1": { input: 1.4, output: 4.4 },
|
|
22
|
+
"glm-5": { input: 1.0, output: 3.2 },
|
|
23
|
+
"glm-5.3": { input: 1.4, output: 4.4 },
|
|
24
|
+
// Kimi
|
|
25
|
+
"kimi-k2.7-code": { input: 0.95, output: 4.0 },
|
|
26
|
+
"kimi-k3": { input: 3.0, output: 15.0 },
|
|
27
|
+
"kimi-k2.6": { input: 0.95, output: 4.0 },
|
|
28
|
+
"kimi-k2.5": { input: 0.6, output: 3.0 },
|
|
29
|
+
// Qwen
|
|
30
|
+
"qwen3.7-max": { input: 2.5, output: 7.5 },
|
|
31
|
+
"qwen3.7-plus": { input: 0.4, output: 1.6 },
|
|
32
|
+
"qwen3.6-plus": { input: 0.5, output: 3.0 },
|
|
33
|
+
"qwen3.5-plus": { input: 0.2, output: 1.2 },
|
|
34
|
+
"qwen3.8-max": { input: 2.5, output: 7.5 },
|
|
35
|
+
// DeepSeek (off-peak)
|
|
36
|
+
"deepseek-v4-pro": { input: 0.66, output: 1.98 },
|
|
37
|
+
"deepseek-v4-flash": { input: 0.22, output: 0.66 },
|
|
38
|
+
// Claude
|
|
39
|
+
"claude-fable-5": { input: 10, output: 50 },
|
|
40
|
+
"claude-opus-5": { input: 5, output: 25 },
|
|
41
|
+
"claude-opus-4-8": { input: 5, output: 25 },
|
|
42
|
+
"claude-opus-4-7": { input: 5, output: 25 },
|
|
43
|
+
"claude-opus-4-6": { input: 5, output: 25 },
|
|
44
|
+
"claude-opus-4-5": { input: 5, output: 25 },
|
|
45
|
+
"claude-sonnet-5": { input: 2, output: 10 },
|
|
46
|
+
"claude-sonnet-4-6": { input: 3, output: 15 },
|
|
47
|
+
"claude-sonnet-4-5": { input: 3, output: 15 },
|
|
48
|
+
"claude-haiku-4-5": { input: 1, output: 5 },
|
|
49
|
+
// Gemini
|
|
50
|
+
"gemini-3.7-flash": { input: 1.5, output: 7.5 },
|
|
51
|
+
"gemini-3.6-flash": { input: 1.5, output: 7.5 },
|
|
52
|
+
"gemini-3.5-flash": { input: 1.5, output: 9.0 },
|
|
53
|
+
"gemini-3.5-flash-lite": { input: 0.3, output: 2.5 },
|
|
54
|
+
"gemini-3.1-pro": { input: 2, output: 12 },
|
|
55
|
+
"gemini-3-flash": { input: 0.5, output: 3.0 },
|
|
56
|
+
// Grok
|
|
57
|
+
"grok-4.6": { input: 2, output: 6 },
|
|
58
|
+
"grok-4.5": { input: 2, output: 6 },
|
|
59
|
+
"grok-build-0.1": { input: 1, output: 2 },
|
|
60
|
+
// Muse Spark
|
|
61
|
+
"muse-spark-1.2": { input: 1.25, output: 4.25 },
|
|
62
|
+
// GPT-5.x (Zen pricing; Go may differ)
|
|
63
|
+
"gpt-5.6-sol": { input: 5, output: 30 },
|
|
64
|
+
"gpt-5.6-terra": { input: 2, output: 12 },
|
|
65
|
+
"gpt-5.6-luna": { input: 0.2, output: 1.2 },
|
|
66
|
+
"gpt-5.5": { input: 5, output: 30 },
|
|
67
|
+
"gpt-5.5-pro": { input: 30, output: 180 },
|
|
68
|
+
"gpt-5.4": { input: 2.5, output: 15 },
|
|
69
|
+
"gpt-5.4-pro": { input: 30, output: 180 },
|
|
70
|
+
"gpt-5.4-mini": { input: 0.75, output: 4.5 },
|
|
71
|
+
"gpt-5.4-nano": { input: 0.2, output: 1.25 },
|
|
72
|
+
"gpt-5.3-codex": { input: 1.75, output: 14 },
|
|
73
|
+
"gpt-5.3-codex-spark": { input: 1.75, output: 14 },
|
|
74
|
+
"gpt-5.2": { input: 1.75, output: 14 },
|
|
75
|
+
"gpt-5.2-codex": { input: 1.75, output: 14 },
|
|
76
|
+
"gpt-5.1": { input: 1.07, output: 8.5 },
|
|
77
|
+
"gpt-5.1-codex": { input: 1.07, output: 8.5 },
|
|
78
|
+
"gpt-5.1-codex-max": { input: 1.25, output: 10 },
|
|
79
|
+
"gpt-5.1-codex-mini": { input: 0.25, output: 2 },
|
|
80
|
+
"gpt-5": { input: 1.07, output: 8.5 },
|
|
81
|
+
"gpt-5-codex": { input: 1.07, output: 8.5 },
|
|
82
|
+
"gpt-5-nano": { input: 0.05, output: 0.4 },
|
|
83
|
+
};
|
|
84
|
+
/** Provider prefixes stripped before a price lookup. */
|
|
85
|
+
const PROVIDER_PREFIXES = ["opencode-go/", "opencode/", "openai/", "anthropic/"];
|
|
86
|
+
/** Normalize a model id to its bare form for the price table lookup. */
|
|
87
|
+
export function normalizeModelId(model) {
|
|
88
|
+
let id = model.trim();
|
|
89
|
+
for (const prefix of PROVIDER_PREFIXES) {
|
|
90
|
+
if (id.startsWith(prefix)) {
|
|
91
|
+
id = id.slice(prefix.length);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Strip a vendor path like qwen/qwen3.5-9b -> qwen3.5-9b (keeps bare ids).
|
|
96
|
+
const parts = id.split("/");
|
|
97
|
+
return parts[parts.length - 1];
|
|
98
|
+
}
|
|
99
|
+
/** Resolve the price for a model id. Overrides win, then the builtin table. */
|
|
100
|
+
export function resolvePrice(model, config) {
|
|
101
|
+
if (!config?.enabled)
|
|
102
|
+
return undefined;
|
|
103
|
+
const bare = normalizeModelId(model);
|
|
104
|
+
const overrides = config.overrides ?? {};
|
|
105
|
+
const direct = overrides[model] ?? overrides[bare];
|
|
106
|
+
if (direct)
|
|
107
|
+
return direct;
|
|
108
|
+
return ZEN_PRICES[bare];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Compute the cost in USD of a single call given its token usage and the
|
|
112
|
+
* model price. Returns undefined when the model has no known price.
|
|
113
|
+
*/
|
|
114
|
+
export function calculateCost(model, promptTokens, completionTokens, config) {
|
|
115
|
+
const price = resolvePrice(model, config);
|
|
116
|
+
if (!price)
|
|
117
|
+
return undefined;
|
|
118
|
+
return (promptTokens / 1_000_000) * price.input + (completionTokens / 1_000_000) * price.output;
|
|
119
|
+
}
|
|
120
|
+
/** Format a USD cost for display. Zero and very small amounts stay readable. */
|
|
121
|
+
export function formatCost(cost) {
|
|
122
|
+
if (cost === 0)
|
|
123
|
+
return "$0.00";
|
|
124
|
+
if (cost >= 0.01)
|
|
125
|
+
return `$${cost.toFixed(2)}`;
|
|
126
|
+
if (cost >= 0.0001)
|
|
127
|
+
return `$${cost.toFixed(4)}`;
|
|
128
|
+
return `$${cost.toFixed(6)}`;
|
|
129
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Heuristic detection of long-running (server/watch) commands.
|
|
3
|
+
*
|
|
4
|
+
* The explicit `background: true` bash parameter always wins; this list is a
|
|
5
|
+
* convenience so dev servers don't block the agent. It only inspects the raw
|
|
6
|
+
* shell command (a technical process-type decision, not task classification).
|
|
7
|
+
*/
|
|
8
|
+
const LONG_RUNNING_PATTERNS = [
|
|
9
|
+
/\b(npm|pnpm|yarn|bun|npx)\s+(run\s+)?(dev|start|serve|preview|watch|server)\b/i,
|
|
10
|
+
/\b(deno|node)\s+.*\b(run\s+)?(dev|serve|watch|server)\b/i,
|
|
11
|
+
/(^|\s)--watch\b/i,
|
|
12
|
+
/(^|\s)-w\b/i,
|
|
13
|
+
/\bnodemon\b/i,
|
|
14
|
+
/\btsx watch\b/i,
|
|
15
|
+
/\b(ts-node|tsc)\s+.*--watch\b/i,
|
|
16
|
+
/\bvite(?!\s+(build|create))\b/i,
|
|
17
|
+
/\bwebpack(-dev-server|\s+serve)\b/i,
|
|
18
|
+
/\bastro dev\b/i,
|
|
19
|
+
/\bnext (dev|start)\b/i,
|
|
20
|
+
/\bnuxt (dev|start)\b/i,
|
|
21
|
+
/\bsvelte-kit (dev|preview)\b/i,
|
|
22
|
+
/\bgatsby develop\b/i,
|
|
23
|
+
/\bdocker(-compose)?\s+.*\bup\b/i,
|
|
24
|
+
/\buvicorn\b|\bgunicorn\b/i,
|
|
25
|
+
/\bpython\s+-m\s+http\.server\b/i,
|
|
26
|
+
/\bflask run\b/i,
|
|
27
|
+
/\bphp artisan serve\b/i,
|
|
28
|
+
/\brails (server|s)\b/i,
|
|
29
|
+
/\bvitest watch\b/i,
|
|
30
|
+
/\bjest --watch\b/i,
|
|
31
|
+
];
|
|
32
|
+
export function isLongRunningCommand(command) {
|
|
33
|
+
return LONG_RUNNING_PATTERNS.some((pattern) => pattern.test(command));
|
|
34
|
+
}
|