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,33 @@
|
|
|
1
|
+
import { t } from "../i18n/index";
|
|
2
|
+
import { pc } from "../ui/colors";
|
|
3
|
+
import { formatCost } from "../modules/pricing/prices";
|
|
4
|
+
/**
|
|
5
|
+
* Render a single-run AgentResult to the terminal (non-JSON mode) and return
|
|
6
|
+
* the process exit code.
|
|
7
|
+
*
|
|
8
|
+
* On failure only the error is printed. The model's `text` (which is the last
|
|
9
|
+
* *accepted* message, i.e. possibly stale after an audit/hallucination error)
|
|
10
|
+
* was already streamed live during the run — re-printing it here misleadingly
|
|
11
|
+
* suggests the task succeeded.
|
|
12
|
+
*/
|
|
13
|
+
export function printRunResult(result, flush) {
|
|
14
|
+
flush();
|
|
15
|
+
if (result.success) {
|
|
16
|
+
if (result.totalCost !== undefined && result.totalCost > 0) {
|
|
17
|
+
console.log(pc.dim(`${t("repl.cost", { cost: formatCost(result.totalCost) })}`));
|
|
18
|
+
if (result.costBreakdown && result.costBreakdown.length > 1) {
|
|
19
|
+
const parts = result.costBreakdown.map((e) => `${e.provider} ${formatCost(e.cost)}`);
|
|
20
|
+
console.log(pc.dim(` ${t("repl.cost_breakdown", { breakdown: parts.join(", ") })}`));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else if (result.totalTokens !== undefined && result.totalTokens > 0) {
|
|
24
|
+
console.log(pc.dim(`${t("repl.tokens", { tokens: result.totalTokens })}`));
|
|
25
|
+
}
|
|
26
|
+
if (!result.text) {
|
|
27
|
+
console.log(pc.yellow(t("cli.no_output")));
|
|
28
|
+
}
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
console.error(`${t("error.prefix")}${result.error}`);
|
|
32
|
+
return 1;
|
|
33
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { bootstrap } from "../core/bootstrap";
|
|
2
|
+
import { saveConfig } from "../config/config";
|
|
3
|
+
import { join, dirname } from "path";
|
|
4
|
+
import { homedir } from "os";
|
|
5
|
+
import { SECURITY_POLICIES, applySecurityPolicy, getSecurityPolicy, } from "../modules/security/security-policies";
|
|
6
|
+
import { globalAuditNotifier } from "../modules/security/audit-notifier";
|
|
7
|
+
import { t } from "../i18n/index";
|
|
8
|
+
/**
|
|
9
|
+
* Create security subcommand
|
|
10
|
+
*/
|
|
11
|
+
export function createSecurityCommand(program) {
|
|
12
|
+
const securityCmd = program.command("security").description(t("cli.security.description"));
|
|
13
|
+
// security status - show current security configuration
|
|
14
|
+
securityCmd
|
|
15
|
+
.command("status")
|
|
16
|
+
.description(t("cli.security.status"))
|
|
17
|
+
.action(async () => {
|
|
18
|
+
const { config } = await bootstrap();
|
|
19
|
+
const security = config.security || {};
|
|
20
|
+
const bash = security.bash || {};
|
|
21
|
+
const paths = security.paths || {};
|
|
22
|
+
const network = security.network || {};
|
|
23
|
+
const contentScan = security.contentScan || {};
|
|
24
|
+
const rateLimits = security.rateLimits || {};
|
|
25
|
+
const sessionEncryption = security.sessionEncryption || {};
|
|
26
|
+
const auditNotifier = security.auditNotifier || {};
|
|
27
|
+
console.log(t("cli.security.current_policy"));
|
|
28
|
+
console.log(` ${t("cli.security.bash_enabled")}: ${bash.blacklist?.length > 0 ? t("cli.yes") : t("cli.no")}`);
|
|
29
|
+
console.log(` ${t("cli.security.path_validation")}: ${paths.denied?.length > 0 ? t("cli.yes") : t("cli.no")}`);
|
|
30
|
+
console.log(` ${t("cli.security.network_validation")}: ${network.deniedDomains?.length > 0 || network.allowedDomains?.length > 0 ? t("cli.yes") : t("cli.no")}`);
|
|
31
|
+
console.log(` ${t("cli.security.content_scanning")}: ${contentScan.enabled ? t("cli.yes") : t("cli.no")}`);
|
|
32
|
+
console.log(` ${t("cli.security.max_recursion")}: ${security.maxRecursionDepth ?? 3}`);
|
|
33
|
+
console.log(` ${t("cli.security.max_file_ops")}: ${security.maxFileOperations ?? 100}`);
|
|
34
|
+
console.log(` ${t("cli.security.rate_limit")}: ${rateLimits.maxRequestsPerMinute ?? 60}/min`);
|
|
35
|
+
console.log(` ${t("cli.security.session_encryption")}: ${sessionEncryption.enabled ? t("cli.yes") : t("cli.no")}`);
|
|
36
|
+
console.log(` ${t("cli.security.audit_notifier")}: ${auditNotifier.enabled ? t("cli.yes") : t("cli.no")}`);
|
|
37
|
+
});
|
|
38
|
+
// security policies - manage security policies
|
|
39
|
+
securityCmd
|
|
40
|
+
.command("policies")
|
|
41
|
+
.description(t("cli.security.policies"))
|
|
42
|
+
.action(async () => {
|
|
43
|
+
console.log(t("cli.security.available_policies"));
|
|
44
|
+
console.log("");
|
|
45
|
+
for (const [preset, policy] of Object.entries(SECURITY_POLICIES)) {
|
|
46
|
+
if (preset === "custom")
|
|
47
|
+
continue;
|
|
48
|
+
const marker = " ";
|
|
49
|
+
console.log(` ${marker}${preset.padEnd(12)} ${policy.name}`);
|
|
50
|
+
console.log(` ${policy.description}`);
|
|
51
|
+
console.log(` ${t("cli.security.recommended_for")}: ${policy.recommendedFor.join(", ")}`);
|
|
52
|
+
console.log("");
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
// security set-policy - apply a security policy
|
|
56
|
+
securityCmd
|
|
57
|
+
.command("set-policy")
|
|
58
|
+
.argument("<preset>", t("cli.security.preset"))
|
|
59
|
+
.description(t("cli.security.set_policy"))
|
|
60
|
+
.action(async (preset) => {
|
|
61
|
+
const configPath = join(homedir(), ".mma", "config.json");
|
|
62
|
+
const { config: appConfig } = await bootstrap();
|
|
63
|
+
const validPresets = ["strict", "balanced", "permissive"];
|
|
64
|
+
if (!validPresets.includes(preset)) {
|
|
65
|
+
console.log(t("cli.security.invalid_preset", { presets: validPresets.join(", ") }));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const policy = getSecurityPolicy(preset);
|
|
69
|
+
const newSecurityConfig = applySecurityPolicy(preset);
|
|
70
|
+
// Merge with existing config
|
|
71
|
+
appConfig.security = newSecurityConfig;
|
|
72
|
+
saveConfig(appConfig, configPath, dirname(configPath));
|
|
73
|
+
console.log(t("cli.security.policy_applied", { name: policy.name }));
|
|
74
|
+
console.log(t("cli.security.policy_description", { description: policy.description }));
|
|
75
|
+
});
|
|
76
|
+
// security enable-encryption - enable session file encryption
|
|
77
|
+
securityCmd
|
|
78
|
+
.command("enable-encryption")
|
|
79
|
+
.description(t("cli.security.enable_encryption"))
|
|
80
|
+
.action(async () => {
|
|
81
|
+
const configPath = join(homedir(), ".mma", "config.json");
|
|
82
|
+
const { config: appConfig } = await bootstrap();
|
|
83
|
+
appConfig.security = appConfig.security || {};
|
|
84
|
+
appConfig.security.sessionEncryption = {
|
|
85
|
+
enabled: true,
|
|
86
|
+
encryptHistory: true,
|
|
87
|
+
encryptSessionLog: true,
|
|
88
|
+
};
|
|
89
|
+
saveConfig(appConfig, configPath, dirname(configPath));
|
|
90
|
+
console.log(t("cli.security.encryption_enabled"));
|
|
91
|
+
});
|
|
92
|
+
// security disable-encryption - disable session file encryption
|
|
93
|
+
securityCmd
|
|
94
|
+
.command("disable-encryption")
|
|
95
|
+
.description(t("cli.security.disable_encryption"))
|
|
96
|
+
.action(async () => {
|
|
97
|
+
const configPath = join(homedir(), ".mma", "config.json");
|
|
98
|
+
const { config: appConfig } = await bootstrap();
|
|
99
|
+
appConfig.security = appConfig.security || {};
|
|
100
|
+
appConfig.security.sessionEncryption = {
|
|
101
|
+
enabled: false,
|
|
102
|
+
encryptHistory: false,
|
|
103
|
+
encryptSessionLog: false,
|
|
104
|
+
};
|
|
105
|
+
saveConfig(appConfig, configPath, dirname(configPath));
|
|
106
|
+
console.log(t("cli.security.encryption_disabled"));
|
|
107
|
+
});
|
|
108
|
+
// security enable-audit - enable audit notifications
|
|
109
|
+
securityCmd
|
|
110
|
+
.command("enable-audit")
|
|
111
|
+
.description(t("cli.security.enable_audit"))
|
|
112
|
+
.action(async () => {
|
|
113
|
+
const configPath = join(homedir(), ".mma", "config.json");
|
|
114
|
+
const { config: appConfig } = await bootstrap();
|
|
115
|
+
appConfig.security = appConfig.security || {};
|
|
116
|
+
appConfig.security.auditNotifier = {
|
|
117
|
+
enabled: true,
|
|
118
|
+
minSeverity: "medium",
|
|
119
|
+
eventTypes: ["security_block", "bash_command", "file_operation", "network_request"],
|
|
120
|
+
maxRetries: 3,
|
|
121
|
+
webhookTimeout: 5000,
|
|
122
|
+
};
|
|
123
|
+
saveConfig(appConfig, configPath, dirname(configPath));
|
|
124
|
+
console.log(t("cli.security.audit_enabled"));
|
|
125
|
+
});
|
|
126
|
+
// security disable-audit - disable audit notifications
|
|
127
|
+
securityCmd
|
|
128
|
+
.command("disable-audit")
|
|
129
|
+
.description(t("cli.security.disable_audit"))
|
|
130
|
+
.action(async () => {
|
|
131
|
+
const configPath = join(homedir(), ".mma", "config.json");
|
|
132
|
+
const { config: appConfig } = await bootstrap();
|
|
133
|
+
appConfig.security = appConfig.security || {};
|
|
134
|
+
appConfig.security.auditNotifier = {
|
|
135
|
+
enabled: false,
|
|
136
|
+
maxRetries: 3,
|
|
137
|
+
webhookTimeout: 5000,
|
|
138
|
+
};
|
|
139
|
+
saveConfig(appConfig, configPath, dirname(configPath));
|
|
140
|
+
console.log(t("cli.security.audit_disabled"));
|
|
141
|
+
});
|
|
142
|
+
// security audit-stats - show audit notification statistics
|
|
143
|
+
securityCmd
|
|
144
|
+
.command("audit-stats")
|
|
145
|
+
.description(t("cli.security.audit_stats"))
|
|
146
|
+
.action(async () => {
|
|
147
|
+
const stats = globalAuditNotifier.getStats();
|
|
148
|
+
console.log(t("cli.security.audit_stats_title"));
|
|
149
|
+
console.log(` ${t("cli.security.total_notifications")}: ${stats.total}`);
|
|
150
|
+
console.log("");
|
|
151
|
+
console.log(t("cli.security.by_severity"));
|
|
152
|
+
console.log(` ${t("cli.security.low")}: ${stats.bySeverity.low}`);
|
|
153
|
+
console.log(` ${t("cli.security.medium")}: ${stats.bySeverity.medium}`);
|
|
154
|
+
console.log(` ${t("cli.security.high")}: ${stats.bySeverity.high}`);
|
|
155
|
+
console.log(` ${t("cli.security.critical")}: ${stats.bySeverity.critical}`);
|
|
156
|
+
console.log("");
|
|
157
|
+
console.log(t("cli.security.by_type"));
|
|
158
|
+
for (const [type, count] of Object.entries(stats.byType)) {
|
|
159
|
+
if (count > 0) {
|
|
160
|
+
console.log(` ${type}: ${count}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import * as readline from "readline";
|
|
2
|
+
import { pc } from "../ui/colors";
|
|
3
|
+
import { t, setLocale } from "../i18n/index";
|
|
4
|
+
import { Spinner } from "../ui/spinner";
|
|
5
|
+
import { BUILTIN_PROVIDERS, HOSTED_BASE_URLS } from "../modules/providers/presets";
|
|
6
|
+
import { box } from "../ui/box";
|
|
7
|
+
import { renderTable } from "../ui/table";
|
|
8
|
+
async function withSpinner(message, fn) {
|
|
9
|
+
const spinner = new Spinner();
|
|
10
|
+
spinner.start(message);
|
|
11
|
+
try {
|
|
12
|
+
return await fn();
|
|
13
|
+
}
|
|
14
|
+
finally {
|
|
15
|
+
spinner.stop();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Render a numbered list inside a box. */
|
|
19
|
+
function menuList(items) {
|
|
20
|
+
const lines = items.map((it, i) => ` ${pc.cyan(`[${i + 1}]`)} ${it.label}${it.url ? ` ${pc.dim(it.url)}` : ""}`);
|
|
21
|
+
for (const l of box(lines, { width: 72 }))
|
|
22
|
+
console.log(l);
|
|
23
|
+
}
|
|
24
|
+
function ask(rl, question, defaultValue) {
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
const prompt = defaultValue ? `${question} [${defaultValue}]: ` : `${question}: `;
|
|
27
|
+
rl.question(prompt, (answer) => {
|
|
28
|
+
resolve(answer.trim() || defaultValue || "");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const PROVIDER_TYPES = BUILTIN_PROVIDERS.map((spec) => ({
|
|
33
|
+
value: spec.type,
|
|
34
|
+
label: spec.label,
|
|
35
|
+
}));
|
|
36
|
+
const KNOWN_PORTS = [
|
|
37
|
+
{ port: 1234, label: "LM Studio" },
|
|
38
|
+
{ port: 11434, label: "Ollama" },
|
|
39
|
+
{ port: 8080, label: "vLLM / custom" },
|
|
40
|
+
{ port: 4891, label: "GPT4All" },
|
|
41
|
+
];
|
|
42
|
+
async function scanPorts() {
|
|
43
|
+
const found = [];
|
|
44
|
+
for (const { port, label } of KNOWN_PORTS) {
|
|
45
|
+
try {
|
|
46
|
+
const controller = new AbortController();
|
|
47
|
+
const timeout = setTimeout(() => controller.abort(), 2000);
|
|
48
|
+
const resp = await fetch(`http://localhost:${port}/v1/models`, {
|
|
49
|
+
signal: controller.signal,
|
|
50
|
+
});
|
|
51
|
+
clearTimeout(timeout);
|
|
52
|
+
if (resp.ok) {
|
|
53
|
+
found.push({ label, url: `http://localhost:${port}/v1` });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
/* port not available */
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
async function fetchModels(apiBase, apiKey) {
|
|
63
|
+
try {
|
|
64
|
+
const url = `${apiBase.replace(/\/+$/, "")}/models`;
|
|
65
|
+
const controller = new AbortController();
|
|
66
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
67
|
+
const resp = await fetch(url, {
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: `Bearer ${apiKey}`,
|
|
70
|
+
"Content-Type": "application/json",
|
|
71
|
+
},
|
|
72
|
+
signal: controller.signal,
|
|
73
|
+
});
|
|
74
|
+
clearTimeout(timeout);
|
|
75
|
+
if (!resp.ok)
|
|
76
|
+
return [];
|
|
77
|
+
const data = (await resp.json());
|
|
78
|
+
const models = (data.data || data || [])
|
|
79
|
+
.map((m) => m.id || m.name || m.model || "")
|
|
80
|
+
.filter(Boolean);
|
|
81
|
+
return models;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async function testChat(apiBase, apiKey, model) {
|
|
88
|
+
try {
|
|
89
|
+
const url = `${apiBase.replace(/\/+$/, "")}/chat/completions`;
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
const timeout = setTimeout(() => controller.abort(), 10000);
|
|
92
|
+
const resp = await fetch(url, {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: {
|
|
95
|
+
Authorization: `Bearer ${apiKey}`,
|
|
96
|
+
"Content-Type": "application/json",
|
|
97
|
+
},
|
|
98
|
+
body: JSON.stringify({
|
|
99
|
+
model,
|
|
100
|
+
messages: [{ role: "user", content: 'Say "ok"' }],
|
|
101
|
+
max_tokens: 20,
|
|
102
|
+
}),
|
|
103
|
+
signal: controller.signal,
|
|
104
|
+
});
|
|
105
|
+
clearTimeout(timeout);
|
|
106
|
+
if (!resp.ok)
|
|
107
|
+
return false;
|
|
108
|
+
const data = (await resp.json());
|
|
109
|
+
const msg = data.choices?.[0]?.message;
|
|
110
|
+
const text = msg?.content || msg?.reasoning_content || "";
|
|
111
|
+
return text.length > 0;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export async function runSetup(externalRl) {
|
|
118
|
+
console.log(t("setup.title"));
|
|
119
|
+
const rl = externalRl ||
|
|
120
|
+
readline.createInterface({
|
|
121
|
+
input: process.stdin,
|
|
122
|
+
output: process.stdout,
|
|
123
|
+
});
|
|
124
|
+
const ownRl = !externalRl;
|
|
125
|
+
console.log(t("setup.language"));
|
|
126
|
+
const locale = await ask(rl, t("setup.ui_lang"), "en");
|
|
127
|
+
setLocale(locale);
|
|
128
|
+
console.log(t("setup.select_provider"));
|
|
129
|
+
menuList(PROVIDER_TYPES);
|
|
130
|
+
const providerChoice = await ask(rl, t("setup.select_provider_num", { max: PROVIDER_TYPES.length }), "1");
|
|
131
|
+
const providerIdx = Math.max(0, Math.min(PROVIDER_TYPES.length - 1, (parseInt(providerChoice) || 1) - 1));
|
|
132
|
+
const provider = PROVIDER_TYPES[providerIdx].value;
|
|
133
|
+
let apiBase = "";
|
|
134
|
+
if (provider === "openai-compat") {
|
|
135
|
+
const found = await withSpinner(t("setup.scanning_spinner"), async () => scanPorts());
|
|
136
|
+
if (found.length > 0) {
|
|
137
|
+
console.log(t("setup.found_servers", { count: found.length }));
|
|
138
|
+
menuList([
|
|
139
|
+
...found.map((f) => ({ label: f.label, url: f.url })),
|
|
140
|
+
{ label: t("setup.custom_url") },
|
|
141
|
+
]);
|
|
142
|
+
const choice = await ask(rl, t("setup.select_server", { max: found.length + 1 }), "1");
|
|
143
|
+
const idx = (parseInt(choice) || 1) - 1;
|
|
144
|
+
if (idx >= found.length) {
|
|
145
|
+
apiBase = await ask(rl, t("setup.api_base"), "http://localhost:1234/v1");
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
apiBase = found[Math.max(0, Math.min(found.length - 1, idx))].url;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
console.log(t("setup.no_servers"));
|
|
153
|
+
apiBase = await ask(rl, t("setup.api_base"), "http://localhost:1234/v1");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
apiBase = HOSTED_BASE_URLS[provider] ?? "";
|
|
158
|
+
}
|
|
159
|
+
const defaultKey = provider === "openai-compat" ? "not-needed" : "";
|
|
160
|
+
const apiKey = await ask(rl, t("setup.api_key"), defaultKey);
|
|
161
|
+
const models = await withSpinner(t("setup.fetching_spinner"), () => fetchModels(apiBase, apiKey));
|
|
162
|
+
let model = "";
|
|
163
|
+
if (models.length > 0) {
|
|
164
|
+
console.log(t("setup.available_models"));
|
|
165
|
+
menuList(models.slice(0, 10).map((m) => ({ label: m })));
|
|
166
|
+
const choice = await ask(rl, t("setup.select_model", { max: Math.min(models.length, 10) }), "1");
|
|
167
|
+
const idx = Math.max(0, Math.min(models.length - 1, (parseInt(choice) || 1) - 1));
|
|
168
|
+
model = models[idx];
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
model = await ask(rl, t("setup.model_name"), "qwen/qwen3.5-9b");
|
|
172
|
+
}
|
|
173
|
+
const connected = await withSpinner(t("setup.testing_spinner", { model }), () => testChat(apiBase, apiKey, model));
|
|
174
|
+
if (connected) {
|
|
175
|
+
console.log(pc.green(t("setup.ok")));
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
console.log(pc.yellow(t("setup.warning")));
|
|
179
|
+
}
|
|
180
|
+
console.log(t("setup.agent_settings"));
|
|
181
|
+
const contextWindow = parseInt(await ask(rl, t("setup.context_window"), "32768"));
|
|
182
|
+
const maxIterations = parseInt(await ask(rl, t("setup.max_iters"), "1000"));
|
|
183
|
+
// Security settings
|
|
184
|
+
console.log(t("setup.security_header"));
|
|
185
|
+
console.log(pc.dim(t("setup.security_status_on")));
|
|
186
|
+
const configureSecurity = await ask(rl, t("setup.security_configure"), "n");
|
|
187
|
+
let securityBashBlock = false;
|
|
188
|
+
let securityFlagsBlock = false;
|
|
189
|
+
let securityPathsDeny = true;
|
|
190
|
+
if (configureSecurity.toLowerCase() === "y" || configureSecurity.toLowerCase() === "yes") {
|
|
191
|
+
securityBashBlock = (await ask(rl, t("setup.security_bash_block"), "n")).toLowerCase() === "y";
|
|
192
|
+
securityFlagsBlock =
|
|
193
|
+
(await ask(rl, t("setup.security_flags_block"), "n")).toLowerCase() === "y";
|
|
194
|
+
securityPathsDeny = (await ask(rl, t("setup.security_paths_deny"), "Y")).toLowerCase() !== "n";
|
|
195
|
+
}
|
|
196
|
+
if (ownRl) {
|
|
197
|
+
rl.close();
|
|
198
|
+
// Bun: closed readline can leave stdin in a state where a later
|
|
199
|
+
// raw-mode LineEditor never receives keypress events. Force-reset:
|
|
200
|
+
process.stdin.removeAllListeners("data");
|
|
201
|
+
process.stdin.removeAllListeners("keypress");
|
|
202
|
+
if (typeof process.stdin.setRawMode === "function") {
|
|
203
|
+
process.stdin.setRawMode(false);
|
|
204
|
+
}
|
|
205
|
+
// Pause + resume forces Bun to flush its internal buffer and
|
|
206
|
+
// release the stream so LineEditor can attach cleanly.
|
|
207
|
+
process.stdin.pause();
|
|
208
|
+
process.stdin.resume();
|
|
209
|
+
}
|
|
210
|
+
const answers = {
|
|
211
|
+
provider,
|
|
212
|
+
apiBase,
|
|
213
|
+
apiKey: apiKey || "not-needed",
|
|
214
|
+
model,
|
|
215
|
+
contextWindow,
|
|
216
|
+
maxToolIterations: maxIterations,
|
|
217
|
+
locale,
|
|
218
|
+
securityBashBlock,
|
|
219
|
+
securityFlagsBlock,
|
|
220
|
+
securityPathsDeny,
|
|
221
|
+
};
|
|
222
|
+
console.log(pc.green(pc.bold(t("setup.complete"))));
|
|
223
|
+
const summary = renderTable([
|
|
224
|
+
t("setup.summary_setting"),
|
|
225
|
+
t("setup.summary_value"),
|
|
226
|
+
t("setup.summary_setting"),
|
|
227
|
+
t("setup.summary_value"),
|
|
228
|
+
], [
|
|
229
|
+
[t("setup.provider_type"), provider, t("setup.model_name"), model],
|
|
230
|
+
["API Base URL", apiBase, t("setup.context_window"), String(contextWindow)],
|
|
231
|
+
[t("setup.api_key"), apiKey || "not-needed", t("setup.max_iters"), String(maxIterations)],
|
|
232
|
+
[t("setup.ui_lang"), locale, "", ""],
|
|
233
|
+
], { maxColumns: 4 });
|
|
234
|
+
for (const l of summary)
|
|
235
|
+
console.log(l);
|
|
236
|
+
return answers;
|
|
237
|
+
}
|