@volc-emr/emr-cli 0.1.0-beta.1 → 0.1.0-beta.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.
- package/README.md +81 -269
- package/dist/agent/executor.js +39 -3
- package/dist/agent/llmPlanner.js +4 -17
- package/dist/commands/config.js +57 -0
- package/dist/commands/init.js +102 -0
- package/dist/commands/run.js +41 -0
- package/dist/core/agent.js +30 -0
- package/dist/core/executor.js +36 -0
- package/dist/core/llm-planner.js +131 -0
- package/dist/core/planner.js +12 -0
- package/dist/core/registry.js +11 -0
- package/dist/core/tool.js +2 -0
- package/dist/core/types.js +2 -0
- package/dist/index.js +8 -183
- package/dist/integrations/volc/client.js +53 -0
- package/dist/integrations/volc/createClusterMemory.js +56 -0
- package/dist/integrations/volc/emr.js +177 -0
- package/dist/integrations/volc/tools/createCluster.js +335 -0
- package/dist/integrations/volc/tools/deleteCluster.js +15 -0
- package/dist/integrations/volc/tools/findClustersToCleanup.js +18 -0
- package/dist/integrations/volc/tools/index.js +15 -0
- package/dist/integrations/volc/tools/listClusters.js +68 -0
- package/dist/services/ecsApi.js +159 -0
- package/dist/services/emrApi.js +0 -18
- package/dist/shared/config.js +73 -0
- package/dist/shared/confirm.js +92 -0
- package/dist/shared/llm.js +64 -0
- package/dist/shared/logger.js +122 -0
- package/dist/shared/memory.js +4 -0
- package/dist/shared/prompt.js +9 -0
- package/dist/tools/ecs/createCluster.js +335 -0
- package/dist/tools/ecs/deleteCluster.js +127 -0
- package/dist/tools/ecs/findClustersToCleanup.js +32 -0
- package/dist/tools/ecs/index.js +13 -0
- package/dist/tools/ecs/listClusters.js +68 -0
- package/dist/tools/emr/deleteCluster.js +12 -3
- package/dist/tools/emr/findClustersToCleanup.js +16 -2
- package/dist/tools/emr/index.js +0 -2
- package/dist/tools/registry.js +3 -3
- package/package.json +2 -2
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emrApi = exports.CLUSTER_STATES = exports.EmrClusterState = void 0;
|
|
4
|
+
exports.listClusters = listClusters;
|
|
5
|
+
exports.listAllClusters = listAllClusters;
|
|
6
|
+
exports.releaseCluster = releaseCluster;
|
|
7
|
+
exports.createCluster = createCluster;
|
|
8
|
+
const volcApi_1 = require("./volcApi");
|
|
9
|
+
var EmrClusterState;
|
|
10
|
+
(function (EmrClusterState) {
|
|
11
|
+
EmrClusterState["PENDING_FOR_PAYMENT"] = "PENDING_FOR_PAYMENT";
|
|
12
|
+
EmrClusterState["CREATING"] = "CREATING";
|
|
13
|
+
EmrClusterState["RUNNING"] = "RUNNING";
|
|
14
|
+
EmrClusterState["WARNING"] = "WARNING";
|
|
15
|
+
EmrClusterState["EXCEPTION"] = "EXCEPTION";
|
|
16
|
+
EmrClusterState["RESTORING"] = "RESTORING";
|
|
17
|
+
EmrClusterState["PAUSING"] = "PAUSING";
|
|
18
|
+
EmrClusterState["PAUSED"] = "PAUSED";
|
|
19
|
+
EmrClusterState["TERMINATING"] = "TERMINATING";
|
|
20
|
+
EmrClusterState["TERMINATED"] = "TERMINATED";
|
|
21
|
+
EmrClusterState["TERMINATED_WITH_ERROR"] = "TERMINATED_WITH_ERROR";
|
|
22
|
+
EmrClusterState["FAILED"] = "FAILED";
|
|
23
|
+
EmrClusterState["SHUTDOWN"] = "SHUTDOWN";
|
|
24
|
+
})(EmrClusterState || (exports.EmrClusterState = EmrClusterState = {}));
|
|
25
|
+
exports.CLUSTER_STATES = Object.values(EmrClusterState);
|
|
26
|
+
function compactBody(input) {
|
|
27
|
+
const out = {};
|
|
28
|
+
for (const [k, v] of Object.entries(input)) {
|
|
29
|
+
if (v === undefined || v === null || v === "")
|
|
30
|
+
continue;
|
|
31
|
+
if (Array.isArray(v) && v.length === 0)
|
|
32
|
+
continue;
|
|
33
|
+
out[k] = v;
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function toMs(v) {
|
|
38
|
+
if (v === undefined || v === null || v === "")
|
|
39
|
+
return undefined;
|
|
40
|
+
const n = typeof v === "number" ? v : Number(v);
|
|
41
|
+
if (!Number.isFinite(n))
|
|
42
|
+
return undefined;
|
|
43
|
+
// 10 位 = 秒级,13 位 = 毫秒级,统一转毫秒
|
|
44
|
+
return n < 1e12 ? Math.trunc(n) * 1000 : Math.trunc(n);
|
|
45
|
+
}
|
|
46
|
+
function toMsString(v) {
|
|
47
|
+
const ms = toMs(v);
|
|
48
|
+
return ms === undefined ? undefined : String(ms);
|
|
49
|
+
}
|
|
50
|
+
async function listClusters(params = {}) {
|
|
51
|
+
const body = compactBody({
|
|
52
|
+
ClusterName: params.ClusterName,
|
|
53
|
+
ClusterId: params.ClusterId,
|
|
54
|
+
ReleaseVersion: params.ReleaseVersion,
|
|
55
|
+
ProjectName: params.ProjectName,
|
|
56
|
+
CreateTimeBefore: toMsString(params.CreateTimeBefore),
|
|
57
|
+
CreateTimeAfter: toMsString(params.CreateTimeAfter),
|
|
58
|
+
ClusterIds: params.ClusterIds,
|
|
59
|
+
ClusterTypes: params.ClusterTypes,
|
|
60
|
+
ClusterStates: params.ClusterStates,
|
|
61
|
+
ChargeTypes: params.ChargeTypes,
|
|
62
|
+
Tags: params.Tags,
|
|
63
|
+
MaxResults: params.MaxResults,
|
|
64
|
+
NextToken: params.NextToken
|
|
65
|
+
});
|
|
66
|
+
const result = await (0, volcApi_1.callEmr)("ListClusters", body);
|
|
67
|
+
return {
|
|
68
|
+
Items: Array.isArray(result?.Items) ? result.Items : [],
|
|
69
|
+
TotalCount: typeof result?.TotalCount === "number" ? result.TotalCount : 0,
|
|
70
|
+
MaxResults: typeof result?.MaxResults === "number" ? result.MaxResults : 0,
|
|
71
|
+
NextToken: result?.NextToken || undefined
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async function listAllClusters(params = {}) {
|
|
75
|
+
const { hardLimit, NextToken: initialToken, MaxResults, ...rest } = params;
|
|
76
|
+
const pageSize = Math.min(Math.max(MaxResults ?? 50, 1), 100);
|
|
77
|
+
const all = [];
|
|
78
|
+
let token = initialToken;
|
|
79
|
+
let safety = 0;
|
|
80
|
+
do {
|
|
81
|
+
const page = await listClusters({
|
|
82
|
+
...rest,
|
|
83
|
+
MaxResults: pageSize,
|
|
84
|
+
NextToken: token
|
|
85
|
+
});
|
|
86
|
+
all.push(...page.Items);
|
|
87
|
+
if (hardLimit && all.length >= hardLimit) {
|
|
88
|
+
return all.slice(0, hardLimit);
|
|
89
|
+
}
|
|
90
|
+
token = page.NextToken;
|
|
91
|
+
safety++;
|
|
92
|
+
if (safety > 1000) {
|
|
93
|
+
throw new Error("[listAllClusters] safety break: more than 1000 pages, check NextToken loop");
|
|
94
|
+
}
|
|
95
|
+
} while (token);
|
|
96
|
+
return all;
|
|
97
|
+
}
|
|
98
|
+
function matchArchived(msg) {
|
|
99
|
+
return /marked\s+archived|could not operate before fixed/i.test(msg);
|
|
100
|
+
}
|
|
101
|
+
function matchNotFound(msg) {
|
|
102
|
+
return /not\s*found|does\s*not\s*exist|不存在/i.test(msg);
|
|
103
|
+
}
|
|
104
|
+
function matchAlreadyReleased(msg) {
|
|
105
|
+
return /already\s+released|已释放|已删除/i.test(msg);
|
|
106
|
+
}
|
|
107
|
+
async function releaseCluster(params) {
|
|
108
|
+
try {
|
|
109
|
+
const resp = await (0, volcApi_1.callEmr)("ReleaseCluster", params);
|
|
110
|
+
return {
|
|
111
|
+
ClusterId: params.ClusterId,
|
|
112
|
+
OperationId: resp?.OperationId,
|
|
113
|
+
OperateId: resp?.OperateId,
|
|
114
|
+
Skipped: false
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
const msg = String(e?.message || e);
|
|
119
|
+
if (matchArchived(msg)) {
|
|
120
|
+
return {
|
|
121
|
+
ClusterId: params.ClusterId,
|
|
122
|
+
Skipped: true,
|
|
123
|
+
Reason: "ARCHIVED",
|
|
124
|
+
Message: "集群已被归档(archived),无法再操作;需要联系火山侧解除归档后再释放。"
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (matchNotFound(msg)) {
|
|
128
|
+
return {
|
|
129
|
+
ClusterId: params.ClusterId,
|
|
130
|
+
Skipped: true,
|
|
131
|
+
Reason: "NOT_FOUND",
|
|
132
|
+
Message: "集群不存在(可能已被清理)。"
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (matchAlreadyReleased(msg)) {
|
|
136
|
+
return {
|
|
137
|
+
ClusterId: params.ClusterId,
|
|
138
|
+
Skipped: true,
|
|
139
|
+
Reason: "ALREADY_RELEASED",
|
|
140
|
+
Message: "集群已释放,无需重复操作。"
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
throw e;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function createCluster(params) {
|
|
147
|
+
const body = compactBody({ ...params });
|
|
148
|
+
const resp = await (0, volcApi_1.callEmr)("CreateCluster", body);
|
|
149
|
+
return {
|
|
150
|
+
ClusterId: resp?.ClusterId || "",
|
|
151
|
+
OperationId: resp?.OperationId
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
exports.emrApi = {
|
|
155
|
+
listClusters,
|
|
156
|
+
listAllClusters,
|
|
157
|
+
releaseCluster,
|
|
158
|
+
createCluster
|
|
159
|
+
};
|
package/dist/services/emrApi.js
CHANGED
|
@@ -5,7 +5,6 @@ exports.listClusters = listClusters;
|
|
|
5
5
|
exports.listAllClusters = listAllClusters;
|
|
6
6
|
exports.releaseCluster = releaseCluster;
|
|
7
7
|
exports.createCluster = createCluster;
|
|
8
|
-
exports.findClustersToCleanup = findClustersToCleanup;
|
|
9
8
|
const volcApi_1 = require("./volcApi");
|
|
10
9
|
var EmrClusterState;
|
|
11
10
|
(function (EmrClusterState) {
|
|
@@ -152,26 +151,9 @@ async function createCluster(params) {
|
|
|
152
151
|
OperationId: resp?.OperationId
|
|
153
152
|
};
|
|
154
153
|
}
|
|
155
|
-
const DAY = 86400000;
|
|
156
|
-
const SHUTDOWN_STATES = [
|
|
157
|
-
EmrClusterState.TERMINATED,
|
|
158
|
-
EmrClusterState.TERMINATED_WITH_ERROR,
|
|
159
|
-
EmrClusterState.FAILED,
|
|
160
|
-
EmrClusterState.SHUTDOWN
|
|
161
|
-
];
|
|
162
|
-
async function findClustersToCleanup({ olderThanDays = 7, states } = {}) {
|
|
163
|
-
const threshold = Date.now() - olderThanDays * DAY;
|
|
164
|
-
const targetStates = states && states.length ? states : SHUTDOWN_STATES;
|
|
165
|
-
const items = await listAllClusters({
|
|
166
|
-
ClusterStates: targetStates,
|
|
167
|
-
CreateTimeBefore: threshold
|
|
168
|
-
});
|
|
169
|
-
return items;
|
|
170
|
-
}
|
|
171
154
|
exports.emrApi = {
|
|
172
155
|
listClusters,
|
|
173
156
|
listAllClusters,
|
|
174
157
|
releaseCluster,
|
|
175
|
-
findClustersToCleanup,
|
|
176
158
|
createCluster
|
|
177
159
|
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.readLocalConfig = readLocalConfig;
|
|
7
|
+
exports.writeLocalConfig = writeLocalConfig;
|
|
8
|
+
exports.clearLlmConfig = clearLlmConfig;
|
|
9
|
+
exports.resolveCredentials = resolveCredentials;
|
|
10
|
+
exports.resolveLlmConfig = resolveLlmConfig;
|
|
11
|
+
exports.configFilePath = configFilePath;
|
|
12
|
+
const fs_1 = __importDefault(require("fs"));
|
|
13
|
+
const os_1 = __importDefault(require("os"));
|
|
14
|
+
const path_1 = __importDefault(require("path"));
|
|
15
|
+
const CONFIG_DIR = process.env.VOLC_EMR_CONFIG_DIR || path_1.default.join(os_1.default.homedir(), ".volc-emr");
|
|
16
|
+
const CONFIG_FILE = path_1.default.join(CONFIG_DIR, "config.json");
|
|
17
|
+
function readLocalConfig() {
|
|
18
|
+
try {
|
|
19
|
+
if (!fs_1.default.existsSync(CONFIG_FILE))
|
|
20
|
+
return {};
|
|
21
|
+
return JSON.parse(fs_1.default.readFileSync(CONFIG_FILE, "utf-8"));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function writeLocalConfig(cfg) {
|
|
28
|
+
fs_1.default.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
29
|
+
const prev = readLocalConfig();
|
|
30
|
+
let mergedLlm;
|
|
31
|
+
if (cfg.llm === null) {
|
|
32
|
+
mergedLlm = undefined;
|
|
33
|
+
}
|
|
34
|
+
else if (cfg.llm || prev.llm) {
|
|
35
|
+
mergedLlm = { ...(prev.llm || {}), ...(cfg.llm || {}) };
|
|
36
|
+
}
|
|
37
|
+
const merged = { ...prev, ...cfg };
|
|
38
|
+
if (mergedLlm && Object.keys(mergedLlm).length > 0) {
|
|
39
|
+
merged.llm = mergedLlm;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
delete merged.llm;
|
|
43
|
+
}
|
|
44
|
+
fs_1.default.writeFileSync(CONFIG_FILE, JSON.stringify(merged, null, 2), {
|
|
45
|
+
mode: 0o600
|
|
46
|
+
});
|
|
47
|
+
return CONFIG_FILE;
|
|
48
|
+
}
|
|
49
|
+
function clearLlmConfig() {
|
|
50
|
+
return writeLocalConfig({ llm: null });
|
|
51
|
+
}
|
|
52
|
+
function resolveCredentials(cli = {}) {
|
|
53
|
+
const local = readLocalConfig();
|
|
54
|
+
const accessKey = cli.accessKey || process.env.VOLC_ACCESSKEY || local.accessKey;
|
|
55
|
+
const secretKey = cli.secretKey || process.env.VOLC_SECRETKEY || local.secretKey;
|
|
56
|
+
const region = cli.region || process.env.VOLC_REGION || local.region || "cn-beijing";
|
|
57
|
+
if (!accessKey || !secretKey) {
|
|
58
|
+
throw new Error("未找到 Volcengine 凭证。请通过 CLI 参数、环境变量(VOLC_ACCESSKEY/VOLC_SECRETKEY)或本地配置(~/.volc-emr/config.json)提供。");
|
|
59
|
+
}
|
|
60
|
+
return { accessKey, secretKey, region };
|
|
61
|
+
}
|
|
62
|
+
function resolveLlmConfig(cli = {}) {
|
|
63
|
+
const local = readLocalConfig().llm || {};
|
|
64
|
+
const endpoint = cli.endpoint || process.env.VOLC_LLM_ENDPOINT || local.endpoint;
|
|
65
|
+
const apiKey = cli.apiKey || process.env.VOLC_LLM_API_KEY || local.apiKey;
|
|
66
|
+
const model = cli.model || process.env.VOLC_LLM_MODEL || local.model;
|
|
67
|
+
if (!endpoint)
|
|
68
|
+
return null;
|
|
69
|
+
return { endpoint, apiKey, model };
|
|
70
|
+
}
|
|
71
|
+
function configFilePath() {
|
|
72
|
+
return CONFIG_FILE;
|
|
73
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.confirm = confirm;
|
|
7
|
+
exports.createPromptSession = createPromptSession;
|
|
8
|
+
const readline_1 = __importDefault(require("readline"));
|
|
9
|
+
function createLineSource() {
|
|
10
|
+
const stdin = process.stdin;
|
|
11
|
+
stdin.setEncoding("utf8");
|
|
12
|
+
let buffer = "";
|
|
13
|
+
const queue = [];
|
|
14
|
+
const waiters = [];
|
|
15
|
+
let ended = false;
|
|
16
|
+
const flush = () => {
|
|
17
|
+
let idx;
|
|
18
|
+
while ((idx = buffer.indexOf("\n")) !== -1) {
|
|
19
|
+
const line = buffer.slice(0, idx).replace(/\r$/, "");
|
|
20
|
+
buffer = buffer.slice(idx + 1);
|
|
21
|
+
const w = waiters.shift();
|
|
22
|
+
if (w)
|
|
23
|
+
w(line);
|
|
24
|
+
else
|
|
25
|
+
queue.push(line);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
stdin.on("data", (chunk) => {
|
|
29
|
+
buffer += chunk;
|
|
30
|
+
flush();
|
|
31
|
+
});
|
|
32
|
+
stdin.on("end", () => {
|
|
33
|
+
ended = true;
|
|
34
|
+
if (buffer.length) {
|
|
35
|
+
const w = waiters.shift();
|
|
36
|
+
if (w)
|
|
37
|
+
w(buffer);
|
|
38
|
+
else
|
|
39
|
+
queue.push(buffer);
|
|
40
|
+
buffer = "";
|
|
41
|
+
}
|
|
42
|
+
while (waiters.length) {
|
|
43
|
+
const w = waiters.shift();
|
|
44
|
+
w && w("");
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
nextLine() {
|
|
49
|
+
if (queue.length)
|
|
50
|
+
return Promise.resolve(queue.shift());
|
|
51
|
+
if (ended)
|
|
52
|
+
return Promise.resolve("");
|
|
53
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
54
|
+
},
|
|
55
|
+
stop() {
|
|
56
|
+
stdin.pause();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function confirm(question) {
|
|
61
|
+
const rl = readline_1.default.createInterface({
|
|
62
|
+
input: process.stdin,
|
|
63
|
+
output: process.stdout
|
|
64
|
+
});
|
|
65
|
+
return new Promise((resolve) => {
|
|
66
|
+
rl.question(`${question} (y/n): `, (answer) => {
|
|
67
|
+
rl.close();
|
|
68
|
+
resolve(answer.toLowerCase() === "y");
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function createPromptSession() {
|
|
73
|
+
const source = createLineSource();
|
|
74
|
+
process.stdin.resume();
|
|
75
|
+
return {
|
|
76
|
+
async ask(question, options = {}) {
|
|
77
|
+
const { defaultValue, silent } = options;
|
|
78
|
+
const label = defaultValue
|
|
79
|
+
? `${question} [${defaultValue}]: `
|
|
80
|
+
: `${question}: `;
|
|
81
|
+
process.stdout.write(label);
|
|
82
|
+
const line = await source.nextLine();
|
|
83
|
+
const value = (line || "").trim();
|
|
84
|
+
if (silent)
|
|
85
|
+
process.stdout.write("\n");
|
|
86
|
+
return value || defaultValue || "";
|
|
87
|
+
},
|
|
88
|
+
close() {
|
|
89
|
+
source.stop();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.chatCompletion = chatCompletion;
|
|
7
|
+
const https_1 = __importDefault(require("https"));
|
|
8
|
+
const http_1 = __importDefault(require("http"));
|
|
9
|
+
const url_1 = require("url");
|
|
10
|
+
async function chatCompletion(llm, options) {
|
|
11
|
+
if (!llm.endpoint)
|
|
12
|
+
throw new Error("LLM endpoint is not configured");
|
|
13
|
+
const body = {
|
|
14
|
+
model: llm.model || "default",
|
|
15
|
+
messages: options.messages,
|
|
16
|
+
temperature: options.temperature ?? 0
|
|
17
|
+
};
|
|
18
|
+
if (options.jsonMode) {
|
|
19
|
+
body.response_format = { type: "json_object" };
|
|
20
|
+
}
|
|
21
|
+
const payload = JSON.stringify(body);
|
|
22
|
+
const url = new url_1.URL(llm.endpoint);
|
|
23
|
+
const headers = {
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
"Content-Length": Buffer.byteLength(payload).toString()
|
|
26
|
+
};
|
|
27
|
+
if (llm.apiKey)
|
|
28
|
+
headers["Authorization"] = `Bearer ${llm.apiKey}`;
|
|
29
|
+
const client = url.protocol === "https:" ? https_1.default : http_1.default;
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const req = client.request({
|
|
32
|
+
method: "POST",
|
|
33
|
+
hostname: url.hostname,
|
|
34
|
+
port: url.port || (url.protocol === "https:" ? 443 : 80),
|
|
35
|
+
path: `${url.pathname}${url.search}`,
|
|
36
|
+
headers
|
|
37
|
+
}, (res) => {
|
|
38
|
+
const chunks = [];
|
|
39
|
+
res.on("data", (c) => chunks.push(c));
|
|
40
|
+
res.on("end", () => {
|
|
41
|
+
const raw = Buffer.concat(chunks).toString("utf-8");
|
|
42
|
+
if ((res.statusCode || 500) >= 400) {
|
|
43
|
+
reject(new Error(`LLM HTTP ${res.statusCode}: ${raw}`));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const json = JSON.parse(raw);
|
|
48
|
+
const content = json?.choices?.[0]?.message?.content;
|
|
49
|
+
if (typeof content !== "string") {
|
|
50
|
+
reject(new Error(`Unexpected LLM response shape: ${raw.slice(0, 200)}`));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
resolve(content);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
reject(new Error(`Parse LLM response failed: ${e.message}`));
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
req.on("error", reject);
|
|
61
|
+
req.write(payload);
|
|
62
|
+
req.end();
|
|
63
|
+
});
|
|
64
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ui = void 0;
|
|
4
|
+
exports.logStep = logStep;
|
|
5
|
+
const useColor = process.stdout.isTTY && process.env.NO_COLOR === undefined && process.env.TERM !== "dumb";
|
|
6
|
+
const c = (code) => (s) => (useColor ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
7
|
+
const color = {
|
|
8
|
+
gray: c("90"),
|
|
9
|
+
red: c("31"),
|
|
10
|
+
green: c("32"),
|
|
11
|
+
yellow: c("33"),
|
|
12
|
+
blue: c("34"),
|
|
13
|
+
magenta: c("35"),
|
|
14
|
+
cyan: c("36"),
|
|
15
|
+
bold: c("1"),
|
|
16
|
+
dim: c("2")
|
|
17
|
+
};
|
|
18
|
+
const SYM = {
|
|
19
|
+
info: "ℹ",
|
|
20
|
+
success: "✔",
|
|
21
|
+
warn: "⚠",
|
|
22
|
+
error: "✘",
|
|
23
|
+
skip: "⊘",
|
|
24
|
+
arrow: "→",
|
|
25
|
+
bullet: "•"
|
|
26
|
+
};
|
|
27
|
+
function write(line) {
|
|
28
|
+
process.stdout.write(line.endsWith("\n") ? line : line + "\n");
|
|
29
|
+
}
|
|
30
|
+
function formatInline(val) {
|
|
31
|
+
if (val === undefined)
|
|
32
|
+
return "";
|
|
33
|
+
if (typeof val === "string")
|
|
34
|
+
return val;
|
|
35
|
+
try {
|
|
36
|
+
return JSON.stringify(val);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return String(val);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function formatFull(val) {
|
|
43
|
+
if (val === undefined)
|
|
44
|
+
return "";
|
|
45
|
+
if (typeof val === "string")
|
|
46
|
+
return val;
|
|
47
|
+
try {
|
|
48
|
+
return JSON.stringify(val, null, 2);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return String(val);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.ui = {
|
|
55
|
+
info(msg) {
|
|
56
|
+
write(`${color.cyan(SYM.info)} ${msg}`);
|
|
57
|
+
},
|
|
58
|
+
success(msg) {
|
|
59
|
+
write(`${color.green(SYM.success)} ${msg}`);
|
|
60
|
+
},
|
|
61
|
+
warn(msg) {
|
|
62
|
+
write(`${color.yellow(SYM.warn)} ${msg}`);
|
|
63
|
+
},
|
|
64
|
+
error(msg) {
|
|
65
|
+
write(`${color.red(SYM.error)} ${msg}`);
|
|
66
|
+
},
|
|
67
|
+
skip(msg) {
|
|
68
|
+
write(`${color.yellow(SYM.skip)} ${msg}`);
|
|
69
|
+
},
|
|
70
|
+
dim(msg) {
|
|
71
|
+
write(color.dim(msg));
|
|
72
|
+
},
|
|
73
|
+
kv(key, value) {
|
|
74
|
+
write(` ${color.gray(key + ":")} ${formatInline(value)}`);
|
|
75
|
+
},
|
|
76
|
+
section(title) {
|
|
77
|
+
write("");
|
|
78
|
+
write(color.bold(title));
|
|
79
|
+
},
|
|
80
|
+
divider() {
|
|
81
|
+
write(color.dim("─".repeat(48)));
|
|
82
|
+
},
|
|
83
|
+
raw(msg) {
|
|
84
|
+
write(msg);
|
|
85
|
+
},
|
|
86
|
+
json(value) {
|
|
87
|
+
write(typeof value === "string" ? value : JSON.stringify(value, null, 2));
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
function logStep(i, status, toolName, extra) {
|
|
91
|
+
const idx = color.gray(`[${i + 1}]`);
|
|
92
|
+
if (status === "run") {
|
|
93
|
+
const suffix = extra?.input ? color.dim(` ${formatInline(extra.input)}`) : "";
|
|
94
|
+
write(`${idx} ${color.blue(SYM.arrow)} ${color.bold(toolName)}${suffix}`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (status === "done") {
|
|
98
|
+
write(`${idx} ${color.green(SYM.success)} ${toolName}`);
|
|
99
|
+
if (extra?.output !== undefined) {
|
|
100
|
+
const out = extra.output;
|
|
101
|
+
if (out && typeof out === "object" && out.Skipped) {
|
|
102
|
+
exports.ui.kv("skipped", out.Reason || "true");
|
|
103
|
+
if (out.Message)
|
|
104
|
+
exports.ui.kv("message", out.Message);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
write(color.dim(formatFull(extra.output)));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (status === "skip") {
|
|
113
|
+
const reason = extra?.reason ? ` (${extra.reason})` : "";
|
|
114
|
+
write(`${idx} ${color.yellow(SYM.skip)} ${toolName}${color.dim(reason)}`);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (status === "fail") {
|
|
118
|
+
write(`${idx} ${color.red(SYM.error)} ${toolName}`);
|
|
119
|
+
if (extra?.error)
|
|
120
|
+
exports.ui.kv("error", extra.error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.systemPrompt = void 0;
|
|
4
|
+
exports.systemPrompt = `
|
|
5
|
+
You are a CLI Agent planner.
|
|
6
|
+
Given a natural language task and a list of tools,
|
|
7
|
+
output a JSON array of { tool, input } steps.
|
|
8
|
+
Never call tools yourself. Only produce plans.
|
|
9
|
+
`.trim();
|