maidang 29.9.3 → 29.9.4
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/dist/index.js +314 -297
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
-
import { Command as
|
|
4
|
+
import { Command as Command18 } from "commander";
|
|
5
5
|
|
|
6
6
|
// src/commands/home.ts
|
|
7
7
|
import { Command } from "commander";
|
|
@@ -58,7 +58,7 @@ var commands = [
|
|
|
58
58
|
{ name: "links", desc: "\u793E\u4EA4\u4E0E\u5916\u94FE" },
|
|
59
59
|
{ name: "cat", desc: "\u9EA6\u9EA6" },
|
|
60
60
|
{ name: "contact", desc: "\u7ED9\u6211\u7559\u8A00" },
|
|
61
|
-
{ name: "manifest", desc: "\u4E00\u6B21\u6027\u7ED9 AI \u7684\u5168\u90E8\u6570\u636E
|
|
61
|
+
{ name: "manifest", desc: "\u4E00\u6B21\u6027\u7ED9 AI \u7684\u5168\u90E8\u6570\u636E\uFF08JSON\uFF09" },
|
|
62
62
|
{ name: "upgrade", desc: "\u5347\u7EA7 maidang \u5230\u6700\u65B0\u7248" }
|
|
63
63
|
];
|
|
64
64
|
function createHomeCommand() {
|
|
@@ -151,6 +151,13 @@ var career_default = {
|
|
|
151
151
|
};
|
|
152
152
|
|
|
153
153
|
// src/utils/api-response.ts
|
|
154
|
+
var ApiRequestError = class extends Error {
|
|
155
|
+
constructor(message, status) {
|
|
156
|
+
super(message);
|
|
157
|
+
this.status = status;
|
|
158
|
+
this.name = "ApiRequestError";
|
|
159
|
+
}
|
|
160
|
+
};
|
|
154
161
|
function isApiEnvelope(value) {
|
|
155
162
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
156
163
|
return false;
|
|
@@ -162,36 +169,227 @@ async function readApiData(response) {
|
|
|
162
169
|
const payload = await response.json();
|
|
163
170
|
if (isApiEnvelope(payload)) {
|
|
164
171
|
if (!response.ok || payload.code !== 0) {
|
|
165
|
-
throw new
|
|
172
|
+
throw new ApiRequestError(payload.message || `HTTP ${response.status}`, response.status);
|
|
166
173
|
}
|
|
167
174
|
return payload.data;
|
|
168
175
|
}
|
|
169
176
|
if (!response.ok) {
|
|
170
177
|
const record = payload;
|
|
171
178
|
const message = record && typeof record.error === "string" ? record.error : `HTTP ${response.status}`;
|
|
172
|
-
throw new
|
|
179
|
+
throw new ApiRequestError(message, response.status);
|
|
173
180
|
}
|
|
174
181
|
return payload;
|
|
175
182
|
}
|
|
176
|
-
|
|
183
|
+
|
|
184
|
+
// src/utils/os-agent-api.ts
|
|
185
|
+
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
186
|
+
import { homedir } from "os";
|
|
187
|
+
import { join } from "path";
|
|
188
|
+
var API_URL = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
189
|
+
var AGENT_HEADER = "x-maidang-agent";
|
|
190
|
+
var CONFIG_DIR = process.env.MAIDANG_CLI_HOME || join(homedir(), ".maidang");
|
|
191
|
+
var CONFIG_PATH = join(CONFIG_DIR, "cli.json");
|
|
192
|
+
var RUN_TIMEOUT_MS = 18e4;
|
|
193
|
+
function loadConfig() {
|
|
194
|
+
try {
|
|
195
|
+
return JSON.parse(readFileSync(CONFIG_PATH, "utf8"));
|
|
196
|
+
} catch {
|
|
197
|
+
return {};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function saveConfig(config) {
|
|
201
|
+
try {
|
|
202
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
203
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), { mode: 384 });
|
|
204
|
+
} catch {
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function readLastSessionId() {
|
|
208
|
+
return loadConfig().sessionId;
|
|
209
|
+
}
|
|
210
|
+
var RateLimitError = class extends Error {
|
|
211
|
+
constructor(retryAfter) {
|
|
212
|
+
super(`\u8BF7\u6C42\u592A\u9891\u7E41\uFF0C\u8BF7\u5728 ${retryAfter} \u79D2\u540E\u518D\u8BD5`);
|
|
213
|
+
this.retryAfter = retryAfter;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
async function readError(response) {
|
|
217
|
+
let retryAfter = 0;
|
|
218
|
+
let message = `HTTP ${response.status}`;
|
|
177
219
|
try {
|
|
178
220
|
const payload = await response.json();
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
221
|
+
if (typeof payload.retry_after === "number") retryAfter = payload.retry_after;
|
|
222
|
+
if (typeof payload.message === "string") message = payload.message;
|
|
223
|
+
} catch {
|
|
224
|
+
}
|
|
225
|
+
if (response.status === 429) {
|
|
226
|
+
const headerRetry = Number(response.headers.get("Retry-After"));
|
|
227
|
+
throw new RateLimitError(retryAfter || headerRetry || 60);
|
|
228
|
+
}
|
|
229
|
+
throw new Error(message);
|
|
230
|
+
}
|
|
231
|
+
async function runAgentChat(input) {
|
|
232
|
+
const config = loadConfig();
|
|
233
|
+
const token = config.agentToken;
|
|
234
|
+
const sessionId = input.sessionId;
|
|
235
|
+
const runsResponse = await fetch(`${API_URL}/api/os-agent/runs`, {
|
|
236
|
+
method: "POST",
|
|
237
|
+
headers: {
|
|
238
|
+
"Content-Type": "application/json",
|
|
239
|
+
...token ? { [AGENT_HEADER]: token } : {}
|
|
240
|
+
},
|
|
241
|
+
body: JSON.stringify({
|
|
242
|
+
message: input.message,
|
|
243
|
+
...sessionId ? { sessionId } : {},
|
|
244
|
+
context: {
|
|
245
|
+
surface: "cli",
|
|
246
|
+
...process.stdout.columns ? { columns: process.stdout.columns } : {}
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
});
|
|
250
|
+
if (!runsResponse.ok) await readError(runsResponse);
|
|
251
|
+
const issuedToken = runsResponse.headers.get(AGENT_HEADER) ?? token;
|
|
252
|
+
const queued = (await runsResponse.json()).data ?? {};
|
|
253
|
+
const runId = queued.runId;
|
|
254
|
+
const newSessionId = queued.sessionId ?? "";
|
|
255
|
+
if (!runId) throw new Error("\u670D\u52A1\u7AEF\u672A\u8FD4\u56DE runId");
|
|
256
|
+
if (issuedToken || newSessionId) {
|
|
257
|
+
saveConfig({
|
|
258
|
+
...issuedToken ? { agentToken: issuedToken } : {},
|
|
259
|
+
...newSessionId ? { sessionId: newSessionId } : {}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
const eventsResponse = await fetch(`${API_URL}/api/os-agent/runs/${runId}/events`, {
|
|
263
|
+
method: "POST",
|
|
264
|
+
headers: issuedToken ? { [AGENT_HEADER]: issuedToken } : {},
|
|
265
|
+
signal: AbortSignal.timeout(RUN_TIMEOUT_MS)
|
|
266
|
+
});
|
|
267
|
+
if (!eventsResponse.ok) await readError(eventsResponse);
|
|
268
|
+
if (!eventsResponse.body) throw new Error("\u4E8B\u4EF6\u6D41\u4E3A\u7A7A");
|
|
269
|
+
let response = "";
|
|
270
|
+
let deltaText = "";
|
|
271
|
+
const terminal = await consumeSse(eventsResponse.body, (event, payload) => {
|
|
272
|
+
if (event === "message.delta") {
|
|
273
|
+
const text = typeof payload?.text === "string" ? payload.text : "";
|
|
274
|
+
if (text) {
|
|
275
|
+
deltaText += text;
|
|
276
|
+
input.onEvent?.({ type: "delta", text });
|
|
186
277
|
}
|
|
187
|
-
|
|
188
|
-
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (event === "tool.started" && typeof payload?.name === "string") {
|
|
281
|
+
input.onEvent?.({ type: "tool", name: payload.name });
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (event === "run.completed") {
|
|
285
|
+
response = typeof payload?.message === "string" ? payload.message : deltaText;
|
|
286
|
+
}
|
|
287
|
+
if (event === "run.failed") {
|
|
288
|
+
throw new Error(typeof payload?.message === "string" ? payload.message : "agent_run_failed");
|
|
289
|
+
}
|
|
290
|
+
if (event === "run.cancelled" || event === "run.superseded") {
|
|
291
|
+
throw new Error("agent_run_cancelled");
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
if (!terminal) throw new Error("\u4E8B\u4EF6\u6D41\u63D0\u524D\u7ED3\u675F");
|
|
295
|
+
return {
|
|
296
|
+
response: response || deltaText,
|
|
297
|
+
sessionId: newSessionId,
|
|
298
|
+
runId,
|
|
299
|
+
silent: terminal === "run.silent"
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
async function consumeSse(body, onEvent) {
|
|
303
|
+
const terminalEvents = /* @__PURE__ */ new Set(["run.completed", "run.silent", "run.failed", "run.cancelled", "run.superseded"]);
|
|
304
|
+
const reader = body.getReader();
|
|
305
|
+
const decoder = new TextDecoder();
|
|
306
|
+
let buffer = "";
|
|
307
|
+
while (true) {
|
|
308
|
+
const { done, value } = await reader.read();
|
|
309
|
+
if (done) break;
|
|
310
|
+
buffer += decoder.decode(value, { stream: true });
|
|
311
|
+
let separator = buffer.indexOf("\n\n");
|
|
312
|
+
while (separator !== -1) {
|
|
313
|
+
const frame = buffer.slice(0, separator);
|
|
314
|
+
buffer = buffer.slice(separator + 2);
|
|
315
|
+
separator = buffer.indexOf("\n\n");
|
|
316
|
+
const parsed = parseFrame(frame);
|
|
317
|
+
if (!parsed) continue;
|
|
318
|
+
onEvent(parsed.event, parsed.payload);
|
|
319
|
+
if (terminalEvents.has(parsed.event)) {
|
|
320
|
+
await reader.cancel().catch(() => void 0);
|
|
321
|
+
return parsed.event;
|
|
189
322
|
}
|
|
190
323
|
}
|
|
191
|
-
|
|
324
|
+
}
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
function parseFrame(frame) {
|
|
328
|
+
let event = "message";
|
|
329
|
+
let data = "";
|
|
330
|
+
for (const line of frame.split("\n")) {
|
|
331
|
+
if (line.startsWith("event: ")) event = line.slice(7).trim();
|
|
332
|
+
else if (line.startsWith("data: ")) data += line.slice(6);
|
|
333
|
+
}
|
|
334
|
+
if (!data) return null;
|
|
335
|
+
try {
|
|
336
|
+
const parsed = JSON.parse(data);
|
|
337
|
+
const payload = parsed.payload ?? parsed;
|
|
338
|
+
return { event, payload };
|
|
192
339
|
} catch {
|
|
193
|
-
return
|
|
340
|
+
return { event, payload: null };
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// src/core/json-output.ts
|
|
345
|
+
var TOOL_NAME = "maidang";
|
|
346
|
+
function printJsonOk(command, data) {
|
|
347
|
+
process.stdout.write(JSON.stringify({ ok: true, tool: TOOL_NAME, command, data }, null, 2) + "\n");
|
|
348
|
+
}
|
|
349
|
+
function printJsonErr(command, error, exitCode = 1) {
|
|
350
|
+
process.stderr.write(
|
|
351
|
+
JSON.stringify({ ok: false, tool: TOOL_NAME, command, error }, null, 2) + "\n"
|
|
352
|
+
);
|
|
353
|
+
process.exit(exitCode);
|
|
354
|
+
}
|
|
355
|
+
var ERROR_HINTS = {
|
|
356
|
+
auth: "\u8EAB\u4EFD\u51ED\u8BC1\u7F3A\u5931\u6216\u5DF2\u8FC7\u671F\uFF0C\u91CD\u65B0\u83B7\u53D6\u540E\u518D\u8BD5",
|
|
357
|
+
quota: "\u8BF7\u6C42\u592A\u9891\u7E41\uFF0C\u7B49\u4E00\u4F1A\u513F\u518D\u8BD5",
|
|
358
|
+
not_found: "\u786E\u8BA4\u8D44\u6E90\u6807\u8BC6\uFF08slug\u3001\u65E5\u671F\u3001\u5E74\u4EFD\u7B49\uFF09\u662F\u5426\u5B58\u5728",
|
|
359
|
+
invalid_args: "\u68C0\u67E5\u547D\u4EE4\u53C2\u6570\uFF0C\u7528 --help \u67E5\u770B\u7528\u6CD5",
|
|
360
|
+
network: "\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\uFF0C\u4EE5\u53CA MAIDANG_API_URL \u6307\u5411\u7684\u670D\u52A1\u662F\u5426\u53EF\u8FBE",
|
|
361
|
+
server: "\u670D\u52A1\u7AEF\u5F02\u5E38\uFF0C\u7A0D\u540E\u91CD\u8BD5\uFF1B\u6301\u7EED\u5931\u8D25\u8BF7\u901A\u8FC7 maidang contact \u53CD\u9988",
|
|
362
|
+
unknown: "\u672A\u5F52\u7C7B\u7684\u9519\u8BEF\uFF0C\u628A\u8FD9\u6761\u8F93\u51FA\u901A\u8FC7 maidang contact \u53CD\u9988\u7ED9\u9EA6\u5F53"
|
|
363
|
+
};
|
|
364
|
+
var typeFromHttpStatus = (status) => {
|
|
365
|
+
if (status === 401 || status === 403) return "auth";
|
|
366
|
+
if (status === 429) return "quota";
|
|
367
|
+
if (status === 404) return "not_found";
|
|
368
|
+
if (status === 400) return "invalid_args";
|
|
369
|
+
if (status >= 500) return "server";
|
|
370
|
+
if (status >= 200 && status < 300) return "server";
|
|
371
|
+
return "unknown";
|
|
372
|
+
};
|
|
373
|
+
var NETWORK_ERROR_PATTERN = /fetch failed|network|econn|enotfound|socket|超时|timed?\s*out/i;
|
|
374
|
+
var isNetworkError = (error) => error.name === "AbortError" || error.name === "TimeoutError" || NETWORK_ERROR_PATTERN.test(error.message);
|
|
375
|
+
function classifyError(error) {
|
|
376
|
+
if (error instanceof RateLimitError) {
|
|
377
|
+
return {
|
|
378
|
+
type: "quota",
|
|
379
|
+
code: "429",
|
|
380
|
+
message: error.message,
|
|
381
|
+
hint: `\u7B49 ${error.retryAfter} \u79D2\u540E\u518D\u8BD5`
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
if (error instanceof ApiRequestError) {
|
|
385
|
+
const type = typeFromHttpStatus(error.status);
|
|
386
|
+
return { type, code: String(error.status), message: error.message, hint: ERROR_HINTS[type] };
|
|
194
387
|
}
|
|
388
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
389
|
+
if (error instanceof Error && isNetworkError(error)) {
|
|
390
|
+
return { type: "network", message, hint: ERROR_HINTS.network };
|
|
391
|
+
}
|
|
392
|
+
return { type: "unknown", message, hint: ERROR_HINTS.unknown };
|
|
195
393
|
}
|
|
196
394
|
|
|
197
395
|
// src/commands/career.ts
|
|
@@ -200,7 +398,6 @@ function createCareerCommand() {
|
|
|
200
398
|
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
201
399
|
try {
|
|
202
400
|
const response = await fetch(`${apiUrl}/api/career`);
|
|
203
|
-
if (!response.ok) throw new Error(await readApiMessage(response));
|
|
204
401
|
const experiences = await readApiData(response);
|
|
205
402
|
const data = {
|
|
206
403
|
career: {
|
|
@@ -209,7 +406,7 @@ function createCareerCommand() {
|
|
|
209
406
|
}
|
|
210
407
|
};
|
|
211
408
|
if (opts.json) {
|
|
212
|
-
|
|
409
|
+
printJsonOk("career", data);
|
|
213
410
|
return;
|
|
214
411
|
}
|
|
215
412
|
const { intro } = data.career;
|
|
@@ -234,6 +431,7 @@ function createCareerCommand() {
|
|
|
234
431
|
}
|
|
235
432
|
});
|
|
236
433
|
} catch (error) {
|
|
434
|
+
if (opts.json) printJsonErr("career", classifyError(error));
|
|
237
435
|
console.error(theme.err(`\u83B7\u53D6\u5DE5\u4F5C\u7ECF\u5386\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
238
436
|
process.exit(1);
|
|
239
437
|
}
|
|
@@ -246,23 +444,15 @@ import { Command as Command3 } from "commander";
|
|
|
246
444
|
var getCatMoments = async () => {
|
|
247
445
|
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
248
446
|
const res = await fetch(`${apiUrl}/api/cat`);
|
|
249
|
-
if (!res.ok) {
|
|
250
|
-
throw new Error(await readApiMessage(res));
|
|
251
|
-
}
|
|
252
447
|
const data = await readApiData(res);
|
|
253
448
|
return Array.isArray(data) ? data : [];
|
|
254
449
|
};
|
|
255
450
|
function createCatCommand() {
|
|
256
|
-
const cmd = new Command3("cat").alias("meow").description("\u9EA6\u9EA6\uFF0C\u4E00\u53EA\u5728\u5DE1\u89C6\u3001\u53D1\u5446\u3001\u8FFD\u5149\u7684\u732B
|
|
257
|
-
const printJson = (data, error) => {
|
|
258
|
-
const payload = { tool: "maidang", command: "cat", data };
|
|
259
|
-
if (error) payload.error = error;
|
|
260
|
-
console.log(JSON.stringify(payload, null, 2));
|
|
261
|
-
};
|
|
451
|
+
const cmd = new Command3("cat").alias("meow").description("\u9EA6\u9EA6\uFF0C\u4E00\u53EA\u5728\u5DE1\u89C6\u3001\u53D1\u5446\u3001\u8FFD\u5149\u7684\u732B\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").action(async (opts) => {
|
|
262
452
|
try {
|
|
263
453
|
const moments = await getCatMoments();
|
|
264
454
|
if (opts.json) {
|
|
265
|
-
|
|
455
|
+
printJsonOk("cat", moments);
|
|
266
456
|
return;
|
|
267
457
|
}
|
|
268
458
|
console.log("");
|
|
@@ -289,11 +479,8 @@ function createCatCommand() {
|
|
|
289
479
|
});
|
|
290
480
|
console.log("");
|
|
291
481
|
} catch (err) {
|
|
482
|
+
if (opts.json) printJsonErr("cat", classifyError(err));
|
|
292
483
|
const message = err instanceof Error ? err.message : String(err);
|
|
293
|
-
if (opts.json) {
|
|
294
|
-
printJson([], message);
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
484
|
console.error(theme.err(`\u83B7\u53D6\u732B\u732B\u52A8\u6001\u5931\u8D25: ${message}`));
|
|
298
485
|
process.exit(1);
|
|
299
486
|
}
|
|
@@ -333,10 +520,9 @@ function createPostsCommand() {
|
|
|
333
520
|
try {
|
|
334
521
|
if (slug) {
|
|
335
522
|
const response2 = await fetch(`${baseApiUrl}/api/posts/${encodeURIComponent(slug)}`);
|
|
336
|
-
if (!response2.ok) throw new Error(await readApiMessage(response2));
|
|
337
523
|
const data2 = await readApiData(response2);
|
|
338
524
|
if (opts.json) {
|
|
339
|
-
|
|
525
|
+
printJsonOk("posts", { slug, post: data2 });
|
|
340
526
|
return;
|
|
341
527
|
}
|
|
342
528
|
console.log("");
|
|
@@ -349,7 +535,6 @@ function createPostsCommand() {
|
|
|
349
535
|
const usesPagination = opts.page !== void 0 || opts.pageSize !== void 0;
|
|
350
536
|
const url = usesPagination ? buildPaginationUrl(`${baseApiUrl}/api/posts`, opts) : `${baseApiUrl}/api/posts`;
|
|
351
537
|
const response = await fetch(url);
|
|
352
|
-
if (!response.ok) throw new Error(await readApiMessage(response));
|
|
353
538
|
const responseData = await readApiData(response);
|
|
354
539
|
const data = Array.isArray(responseData) ? {
|
|
355
540
|
items: responseData,
|
|
@@ -363,7 +548,7 @@ function createPostsCommand() {
|
|
|
363
548
|
}
|
|
364
549
|
} : responseData;
|
|
365
550
|
if (opts.json) {
|
|
366
|
-
|
|
551
|
+
printJsonOk("posts", data);
|
|
367
552
|
return;
|
|
368
553
|
}
|
|
369
554
|
console.log("");
|
|
@@ -382,6 +567,7 @@ function createPostsCommand() {
|
|
|
382
567
|
});
|
|
383
568
|
console.log("");
|
|
384
569
|
} catch (error) {
|
|
570
|
+
if (opts.json) printJsonErr("posts", classifyError(error));
|
|
385
571
|
console.error(theme.err(`\u83B7\u53D6\u52A8\u6001\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
386
572
|
process.exit(1);
|
|
387
573
|
}
|
|
@@ -1292,27 +1478,31 @@ function printSong(song, index) {
|
|
|
1292
1478
|
console.log(` ${song.artists.join(", ")} \xB7 ${formatDuration(song.duration)} \xB7 ${song.album}`);
|
|
1293
1479
|
}
|
|
1294
1480
|
function createSongsCommand() {
|
|
1295
|
-
|
|
1481
|
+
const yearRange = `${Math.min(...availableYears)}\u2014${Math.max(...availableYears)}`;
|
|
1482
|
+
return new Command5("songs").description(`\u5E74\u5EA6\u6B4C\u5355\uFF08${yearRange}\uFF0C--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09`).option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").option("--year <year>", `\u8F93\u51FA\u6307\u5B9A\u5E74\u4EFD\u7684\u6B4C\u66F2\u5217\u8868\uFF08\u53EF\u9009\uFF1A${availableYears.join("\u3001")}\uFF09`).option("--copy <song-id>", "\u590D\u5236\u6307\u5B9A\u6B4C\u66F2\u540D\u79F0").action((opts) => {
|
|
1296
1483
|
const year = opts.year ? Number(opts.year) : void 0;
|
|
1297
1484
|
const entry = year === void 0 ? void 0 : allYears.find((item) => item.year === year);
|
|
1298
1485
|
if (year !== void 0 && !entry) {
|
|
1299
1486
|
const message = `\u6CA1\u6709 ${year} \u5E74\u7684\u6B4C\u5355`;
|
|
1300
1487
|
if (opts.json) {
|
|
1301
|
-
|
|
1302
|
-
} else {
|
|
1303
|
-
console.error(message);
|
|
1488
|
+
printJsonErr("songs", { type: "not_found", message, hint: `\u53EF\u7528\u5E74\u4EFD\uFF1A${availableYears.join("\u3001")}` });
|
|
1304
1489
|
}
|
|
1490
|
+
console.error(message);
|
|
1305
1491
|
process.exit(1);
|
|
1306
1492
|
}
|
|
1307
1493
|
if (opts.copy) {
|
|
1308
1494
|
const song = allYears.flatMap((item) => item.songs).find((item) => item.id === opts.copy);
|
|
1309
1495
|
if (!song) {
|
|
1310
|
-
|
|
1496
|
+
const message = `\u6CA1\u6709\u627E\u5230\u6B4C\u66F2 ${opts.copy}`;
|
|
1497
|
+
if (opts.json) {
|
|
1498
|
+
printJsonErr("songs", { type: "not_found", message, hint: "\u7528 maidang songs --json \u67E5\u770B\u6B4C\u66F2 id" });
|
|
1499
|
+
}
|
|
1500
|
+
console.error(message);
|
|
1311
1501
|
process.exit(1);
|
|
1312
1502
|
}
|
|
1313
1503
|
copyToClipboard(song.name);
|
|
1314
1504
|
if (opts.json) {
|
|
1315
|
-
|
|
1505
|
+
printJsonOk("songs", { copied: song.name, song });
|
|
1316
1506
|
} else {
|
|
1317
1507
|
console.log(`\u5DF2\u590D\u5236\uFF1A${song.name}`);
|
|
1318
1508
|
}
|
|
@@ -1321,9 +1511,9 @@ function createSongsCommand() {
|
|
|
1321
1511
|
if (opts.json) {
|
|
1322
1512
|
if (entry) {
|
|
1323
1513
|
const meta = allMeta.find((item) => item.year === entry.year);
|
|
1324
|
-
|
|
1514
|
+
printJsonOk("songs", { meta, songs: entry.songs });
|
|
1325
1515
|
} else {
|
|
1326
|
-
|
|
1516
|
+
printJsonOk("songs", { years: availableYears, meta: allMeta, byYear: allYears });
|
|
1327
1517
|
}
|
|
1328
1518
|
return;
|
|
1329
1519
|
}
|
|
@@ -1348,14 +1538,13 @@ ${entry.year} \u5E74\u5EA6\u6B4C\u5355 \xB7 ${entry.songs.length} \u9996
|
|
|
1348
1538
|
// src/commands/links.ts
|
|
1349
1539
|
import { Command as Command6 } from "commander";
|
|
1350
1540
|
function createLinksCommand() {
|
|
1351
|
-
const cmd = new Command6("links").description("\u793E\u4EA4\u4E0E\u5916\u94FE
|
|
1541
|
+
const cmd = new Command6("links").description("\u793E\u4EA4\u4E0E\u5916\u94FE\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").action(async (opts) => {
|
|
1352
1542
|
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1353
1543
|
try {
|
|
1354
1544
|
const res = await fetch(`${apiUrl}/api/links`);
|
|
1355
|
-
if (!res.ok) throw new Error(await readApiMessage(res));
|
|
1356
1545
|
const links = await readApiData(res);
|
|
1357
1546
|
if (opts.json) {
|
|
1358
|
-
|
|
1547
|
+
printJsonOk("links", links);
|
|
1359
1548
|
return;
|
|
1360
1549
|
}
|
|
1361
1550
|
console.log("");
|
|
@@ -1374,6 +1563,7 @@ function createLinksCommand() {
|
|
|
1374
1563
|
});
|
|
1375
1564
|
console.log("");
|
|
1376
1565
|
} catch (err) {
|
|
1566
|
+
if (opts.json) printJsonErr("links", classifyError(err));
|
|
1377
1567
|
console.error(theme.err(`\u83B7\u53D6\u94FE\u63A5\u5931\u8D25: ${err instanceof Error ? err.message : err}`));
|
|
1378
1568
|
process.exit(1);
|
|
1379
1569
|
}
|
|
@@ -1388,10 +1578,9 @@ function createProjectsCommand() {
|
|
|
1388
1578
|
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1389
1579
|
try {
|
|
1390
1580
|
const response = await fetch(buildPaginationUrl(`${apiUrl}/api/projects`, opts));
|
|
1391
|
-
if (!response.ok) throw new Error(await readApiMessage(response));
|
|
1392
1581
|
const data = await readApiData(response);
|
|
1393
1582
|
if (opts.json) {
|
|
1394
|
-
|
|
1583
|
+
printJsonOk("projects", data);
|
|
1395
1584
|
return;
|
|
1396
1585
|
}
|
|
1397
1586
|
console.log("");
|
|
@@ -1410,6 +1599,7 @@ function createProjectsCommand() {
|
|
|
1410
1599
|
});
|
|
1411
1600
|
console.log("");
|
|
1412
1601
|
} catch (error) {
|
|
1602
|
+
if (opts.json) printJsonErr("projects", classifyError(error));
|
|
1413
1603
|
console.error(theme.err(`\u83B7\u53D6\u9879\u76EE\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
1414
1604
|
process.exit(1);
|
|
1415
1605
|
}
|
|
@@ -1424,14 +1614,13 @@ var categoryNames = {
|
|
|
1424
1614
|
desktop: "\u684C\u9762"
|
|
1425
1615
|
};
|
|
1426
1616
|
function createToolsCommand() {
|
|
1427
|
-
const cmd = new Command8("tools").description("\u6700\u8FD1\u5728\u7528\u7684\u597D\u7269
|
|
1617
|
+
const cmd = new Command8("tools").description("\u6700\u8FD1\u5728\u7528\u7684\u597D\u7269\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").action(async (opts) => {
|
|
1428
1618
|
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1429
1619
|
try {
|
|
1430
1620
|
const res = await fetch(`${apiUrl}/api/tools`);
|
|
1431
|
-
if (!res.ok) throw new Error(await readApiMessage(res));
|
|
1432
1621
|
const tools = await readApiData(res);
|
|
1433
1622
|
if (opts.json) {
|
|
1434
|
-
|
|
1623
|
+
printJsonOk("tools", tools);
|
|
1435
1624
|
return;
|
|
1436
1625
|
}
|
|
1437
1626
|
const byCategory = tools.reduce((acc, tool) => {
|
|
@@ -1458,6 +1647,7 @@ function createToolsCommand() {
|
|
|
1458
1647
|
});
|
|
1459
1648
|
console.log("");
|
|
1460
1649
|
} catch (err) {
|
|
1650
|
+
if (opts.json) printJsonErr("tools", classifyError(err));
|
|
1461
1651
|
console.error(theme.err(`\u83B7\u53D6\u5DE5\u5177\u5931\u8D25: ${err instanceof Error ? err.message : err}`));
|
|
1462
1652
|
process.exit(1);
|
|
1463
1653
|
}
|
|
@@ -1473,10 +1663,9 @@ function createSkillsCommand() {
|
|
|
1473
1663
|
try {
|
|
1474
1664
|
if (opts.slug) {
|
|
1475
1665
|
const response2 = await fetch(`${apiUrl}/api/skills/${encodeURIComponent(opts.slug)}`);
|
|
1476
|
-
if (!response2.ok) throw new Error(await readApiMessage(response2));
|
|
1477
1666
|
const skill = await readApiData(response2);
|
|
1478
1667
|
if (opts.json) {
|
|
1479
|
-
|
|
1668
|
+
printJsonOk("skills", { slug: opts.slug, skill });
|
|
1480
1669
|
return;
|
|
1481
1670
|
}
|
|
1482
1671
|
console.log("");
|
|
@@ -1493,10 +1682,9 @@ function createSkillsCommand() {
|
|
|
1493
1682
|
return;
|
|
1494
1683
|
}
|
|
1495
1684
|
const response = await fetch(`${apiUrl}/api/skills`);
|
|
1496
|
-
if (!response.ok) throw new Error(await readApiMessage(response));
|
|
1497
1685
|
const skills = await readApiData(response);
|
|
1498
1686
|
if (opts.json) {
|
|
1499
|
-
|
|
1687
|
+
printJsonOk("skills", skills);
|
|
1500
1688
|
return;
|
|
1501
1689
|
}
|
|
1502
1690
|
console.log("");
|
|
@@ -1513,6 +1701,7 @@ function createSkillsCommand() {
|
|
|
1513
1701
|
}
|
|
1514
1702
|
console.log("");
|
|
1515
1703
|
} catch (error) {
|
|
1704
|
+
if (opts.json) printJsonErr("skills", classifyError(error));
|
|
1516
1705
|
console.error(theme.err(`\u83B7\u53D6 Skills \u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
1517
1706
|
process.exit(1);
|
|
1518
1707
|
}
|
|
@@ -1533,9 +1722,6 @@ async function sendContactMessage(input) {
|
|
|
1533
1722
|
},
|
|
1534
1723
|
body: JSON.stringify(input)
|
|
1535
1724
|
});
|
|
1536
|
-
if (!res.ok) {
|
|
1537
|
-
throw new Error(await readApiMessage(res));
|
|
1538
|
-
}
|
|
1539
1725
|
return readApiData(res);
|
|
1540
1726
|
}
|
|
1541
1727
|
|
|
@@ -1548,24 +1734,18 @@ async function contactSync(message, options) {
|
|
|
1548
1734
|
email: options?.email
|
|
1549
1735
|
});
|
|
1550
1736
|
if (options?.json) {
|
|
1551
|
-
|
|
1552
|
-
tool: "maidang",
|
|
1553
|
-
command: "contact",
|
|
1737
|
+
printJsonOk("contact", {
|
|
1554
1738
|
message,
|
|
1555
1739
|
name: options?.name,
|
|
1556
1740
|
email: options?.email,
|
|
1557
|
-
result
|
|
1558
|
-
|
|
1559
|
-
}, null, 2));
|
|
1741
|
+
result
|
|
1742
|
+
});
|
|
1560
1743
|
return;
|
|
1561
1744
|
}
|
|
1562
1745
|
console.log(result.message);
|
|
1563
1746
|
} catch (error) {
|
|
1747
|
+
if (options?.json) printJsonErr("contact", classifyError(error));
|
|
1564
1748
|
const messageText = error instanceof Error ? error.message : String(error);
|
|
1565
|
-
if (options?.json) {
|
|
1566
|
-
console.error(JSON.stringify({ tool: "maidang", command: "contact", error: messageText }, null, 2));
|
|
1567
|
-
return;
|
|
1568
|
-
}
|
|
1569
1749
|
console.error(chalk2.red(`Error: ${messageText}`));
|
|
1570
1750
|
process.exit(1);
|
|
1571
1751
|
}
|
|
@@ -1574,7 +1754,7 @@ function createContactCommand() {
|
|
|
1574
1754
|
return new Command10("contact").description("\u7ED9\u9EA6\u5F53\u7559\u8A00\uFF08-m \u76F4\u63A5\u53D1\u9001\uFF0C--json \u8F93\u51FA\u7ED3\u6784\u5316\u7ED3\u679C\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").option("-m, --message <message>", "\u76F4\u63A5\u53D1\u9001\u7559\u8A00").option("--name <name>", "\u7559\u8A00\u4EBA\u79F0\u547C").option("--email <email>", "\u7559\u8A00\u4EBA\u90AE\u7BB1").action(async (opts) => {
|
|
1575
1755
|
const data = { email: "borisdunk@sina.com", note: "\u76F4\u63A5\u53D1\u9001\u539F\u6587\u7559\u8A00\uFF0C\u4E0D\u7ECF\u8FC7 chat \u6216\u5927\u6A21\u578B" };
|
|
1576
1756
|
if (opts.json && !opts.message) {
|
|
1577
|
-
|
|
1757
|
+
printJsonOk("contact", data);
|
|
1578
1758
|
return;
|
|
1579
1759
|
}
|
|
1580
1760
|
if (opts.message) {
|
|
@@ -1585,10 +1765,6 @@ function createContactCommand() {
|
|
|
1585
1765
|
});
|
|
1586
1766
|
return;
|
|
1587
1767
|
}
|
|
1588
|
-
if (opts.json) {
|
|
1589
|
-
console.log(JSON.stringify({ tool: "maidang", command: "contact", data }, null, 2));
|
|
1590
|
-
return;
|
|
1591
|
-
}
|
|
1592
1768
|
console.log("");
|
|
1593
1769
|
console.log("contact");
|
|
1594
1770
|
console.log("\u7ED9\u9EA6\u5F53\u7559\u8A00\uFF1A");
|
|
@@ -1625,7 +1801,7 @@ function createWhoAmICommand() {
|
|
|
1625
1801
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1626
1802
|
};
|
|
1627
1803
|
if (opts.json) {
|
|
1628
|
-
|
|
1804
|
+
printJsonOk("whoami", whoami);
|
|
1629
1805
|
return;
|
|
1630
1806
|
}
|
|
1631
1807
|
console.log("");
|
|
@@ -1910,166 +2086,6 @@ var printMarkdownAnsi = (source, options = {}) => {
|
|
|
1910
2086
|
console.log(text);
|
|
1911
2087
|
};
|
|
1912
2088
|
|
|
1913
|
-
// src/utils/os-agent-api.ts
|
|
1914
|
-
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
1915
|
-
import { homedir } from "os";
|
|
1916
|
-
import { join } from "path";
|
|
1917
|
-
var API_URL = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1918
|
-
var AGENT_HEADER = "x-maidang-agent";
|
|
1919
|
-
var CONFIG_DIR = process.env.MAIDANG_CLI_HOME || join(homedir(), ".maidang");
|
|
1920
|
-
var CONFIG_PATH = join(CONFIG_DIR, "cli.json");
|
|
1921
|
-
var RUN_TIMEOUT_MS = 18e4;
|
|
1922
|
-
function loadConfig() {
|
|
1923
|
-
try {
|
|
1924
|
-
return JSON.parse(readFileSync(CONFIG_PATH, "utf8"));
|
|
1925
|
-
} catch {
|
|
1926
|
-
return {};
|
|
1927
|
-
}
|
|
1928
|
-
}
|
|
1929
|
-
function saveConfig(config) {
|
|
1930
|
-
try {
|
|
1931
|
-
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
1932
|
-
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), { mode: 384 });
|
|
1933
|
-
} catch {
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
function readLastSessionId() {
|
|
1937
|
-
return loadConfig().sessionId;
|
|
1938
|
-
}
|
|
1939
|
-
var RateLimitError = class extends Error {
|
|
1940
|
-
constructor(retryAfter) {
|
|
1941
|
-
super(`\u8BF7\u6C42\u592A\u9891\u7E41\uFF0C\u8BF7\u5728 ${retryAfter} \u79D2\u540E\u518D\u8BD5`);
|
|
1942
|
-
this.retryAfter = retryAfter;
|
|
1943
|
-
}
|
|
1944
|
-
};
|
|
1945
|
-
async function readError(response) {
|
|
1946
|
-
let retryAfter = 0;
|
|
1947
|
-
let message = `HTTP ${response.status}`;
|
|
1948
|
-
try {
|
|
1949
|
-
const payload = await response.json();
|
|
1950
|
-
if (typeof payload.retry_after === "number") retryAfter = payload.retry_after;
|
|
1951
|
-
if (typeof payload.message === "string") message = payload.message;
|
|
1952
|
-
} catch {
|
|
1953
|
-
}
|
|
1954
|
-
if (response.status === 429) {
|
|
1955
|
-
const headerRetry = Number(response.headers.get("Retry-After"));
|
|
1956
|
-
throw new RateLimitError(retryAfter || headerRetry || 60);
|
|
1957
|
-
}
|
|
1958
|
-
throw new Error(message);
|
|
1959
|
-
}
|
|
1960
|
-
async function runAgentChat(input) {
|
|
1961
|
-
const config = loadConfig();
|
|
1962
|
-
const token = config.agentToken;
|
|
1963
|
-
const sessionId = input.sessionId;
|
|
1964
|
-
const runsResponse = await fetch(`${API_URL}/api/os-agent/runs`, {
|
|
1965
|
-
method: "POST",
|
|
1966
|
-
headers: {
|
|
1967
|
-
"Content-Type": "application/json",
|
|
1968
|
-
...token ? { [AGENT_HEADER]: token } : {}
|
|
1969
|
-
},
|
|
1970
|
-
body: JSON.stringify({
|
|
1971
|
-
message: input.message,
|
|
1972
|
-
...sessionId ? { sessionId } : {},
|
|
1973
|
-
context: {
|
|
1974
|
-
surface: "cli",
|
|
1975
|
-
...process.stdout.columns ? { columns: process.stdout.columns } : {}
|
|
1976
|
-
}
|
|
1977
|
-
})
|
|
1978
|
-
});
|
|
1979
|
-
if (!runsResponse.ok) await readError(runsResponse);
|
|
1980
|
-
const issuedToken = runsResponse.headers.get(AGENT_HEADER) ?? token;
|
|
1981
|
-
const queued = (await runsResponse.json()).data ?? {};
|
|
1982
|
-
const runId = queued.runId;
|
|
1983
|
-
const newSessionId = queued.sessionId ?? "";
|
|
1984
|
-
if (!runId) throw new Error("\u670D\u52A1\u7AEF\u672A\u8FD4\u56DE runId");
|
|
1985
|
-
if (issuedToken || newSessionId) {
|
|
1986
|
-
saveConfig({
|
|
1987
|
-
...issuedToken ? { agentToken: issuedToken } : {},
|
|
1988
|
-
...newSessionId ? { sessionId: newSessionId } : {}
|
|
1989
|
-
});
|
|
1990
|
-
}
|
|
1991
|
-
const eventsResponse = await fetch(`${API_URL}/api/os-agent/runs/${runId}/events`, {
|
|
1992
|
-
method: "POST",
|
|
1993
|
-
headers: issuedToken ? { [AGENT_HEADER]: issuedToken } : {},
|
|
1994
|
-
signal: AbortSignal.timeout(RUN_TIMEOUT_MS)
|
|
1995
|
-
});
|
|
1996
|
-
if (!eventsResponse.ok) await readError(eventsResponse);
|
|
1997
|
-
if (!eventsResponse.body) throw new Error("\u4E8B\u4EF6\u6D41\u4E3A\u7A7A");
|
|
1998
|
-
let response = "";
|
|
1999
|
-
let deltaText = "";
|
|
2000
|
-
const terminal = await consumeSse(eventsResponse.body, (event, payload) => {
|
|
2001
|
-
if (event === "message.delta") {
|
|
2002
|
-
const text = typeof payload?.text === "string" ? payload.text : "";
|
|
2003
|
-
if (text) {
|
|
2004
|
-
deltaText += text;
|
|
2005
|
-
input.onEvent?.({ type: "delta", text });
|
|
2006
|
-
}
|
|
2007
|
-
return;
|
|
2008
|
-
}
|
|
2009
|
-
if (event === "tool.started" && typeof payload?.name === "string") {
|
|
2010
|
-
input.onEvent?.({ type: "tool", name: payload.name });
|
|
2011
|
-
return;
|
|
2012
|
-
}
|
|
2013
|
-
if (event === "run.completed") {
|
|
2014
|
-
response = typeof payload?.message === "string" ? payload.message : deltaText;
|
|
2015
|
-
}
|
|
2016
|
-
if (event === "run.failed") {
|
|
2017
|
-
throw new Error(typeof payload?.message === "string" ? payload.message : "agent_run_failed");
|
|
2018
|
-
}
|
|
2019
|
-
if (event === "run.cancelled" || event === "run.superseded") {
|
|
2020
|
-
throw new Error("agent_run_cancelled");
|
|
2021
|
-
}
|
|
2022
|
-
});
|
|
2023
|
-
if (!terminal) throw new Error("\u4E8B\u4EF6\u6D41\u63D0\u524D\u7ED3\u675F");
|
|
2024
|
-
return {
|
|
2025
|
-
response: response || deltaText,
|
|
2026
|
-
sessionId: newSessionId,
|
|
2027
|
-
runId,
|
|
2028
|
-
silent: terminal === "run.silent"
|
|
2029
|
-
};
|
|
2030
|
-
}
|
|
2031
|
-
async function consumeSse(body, onEvent) {
|
|
2032
|
-
const terminalEvents = /* @__PURE__ */ new Set(["run.completed", "run.silent", "run.failed", "run.cancelled", "run.superseded"]);
|
|
2033
|
-
const reader = body.getReader();
|
|
2034
|
-
const decoder = new TextDecoder();
|
|
2035
|
-
let buffer = "";
|
|
2036
|
-
while (true) {
|
|
2037
|
-
const { done, value } = await reader.read();
|
|
2038
|
-
if (done) break;
|
|
2039
|
-
buffer += decoder.decode(value, { stream: true });
|
|
2040
|
-
let separator = buffer.indexOf("\n\n");
|
|
2041
|
-
while (separator !== -1) {
|
|
2042
|
-
const frame = buffer.slice(0, separator);
|
|
2043
|
-
buffer = buffer.slice(separator + 2);
|
|
2044
|
-
separator = buffer.indexOf("\n\n");
|
|
2045
|
-
const parsed = parseFrame(frame);
|
|
2046
|
-
if (!parsed) continue;
|
|
2047
|
-
onEvent(parsed.event, parsed.payload);
|
|
2048
|
-
if (terminalEvents.has(parsed.event)) {
|
|
2049
|
-
await reader.cancel().catch(() => void 0);
|
|
2050
|
-
return parsed.event;
|
|
2051
|
-
}
|
|
2052
|
-
}
|
|
2053
|
-
}
|
|
2054
|
-
return null;
|
|
2055
|
-
}
|
|
2056
|
-
function parseFrame(frame) {
|
|
2057
|
-
let event = "message";
|
|
2058
|
-
let data = "";
|
|
2059
|
-
for (const line of frame.split("\n")) {
|
|
2060
|
-
if (line.startsWith("event: ")) event = line.slice(7).trim();
|
|
2061
|
-
else if (line.startsWith("data: ")) data += line.slice(6);
|
|
2062
|
-
}
|
|
2063
|
-
if (!data) return null;
|
|
2064
|
-
try {
|
|
2065
|
-
const parsed = JSON.parse(data);
|
|
2066
|
-
const payload = parsed.payload ?? parsed;
|
|
2067
|
-
return { event, payload };
|
|
2068
|
-
} catch {
|
|
2069
|
-
return { event, payload: null };
|
|
2070
|
-
}
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
2089
|
// src/commands/chat.tsx
|
|
2074
2090
|
var writeHumanMarkdown = (source) => {
|
|
2075
2091
|
const rendered = renderMarkdownAnsi(source);
|
|
@@ -2105,14 +2121,12 @@ async function chatSync(message, json = false, sessionId) {
|
|
|
2105
2121
|
}
|
|
2106
2122
|
});
|
|
2107
2123
|
if (json) {
|
|
2108
|
-
|
|
2109
|
-
tool: "maidang",
|
|
2110
|
-
command: "chat",
|
|
2124
|
+
printJsonOk("chat", {
|
|
2111
2125
|
message,
|
|
2112
2126
|
response: result.silent ? "" : result.response,
|
|
2113
2127
|
sessionId: result.sessionId,
|
|
2114
2128
|
runId: result.runId
|
|
2115
|
-
}
|
|
2129
|
+
});
|
|
2116
2130
|
return;
|
|
2117
2131
|
}
|
|
2118
2132
|
flushPending();
|
|
@@ -2124,13 +2138,9 @@ async function chatSync(message, json = false, sessionId) {
|
|
|
2124
2138
|
console.log(chalk4.dim("\u4F7F\u7528 -c \u7EED\u63A5\uFF0C\u6216 --session-id <id> \u6307\u5B9A\u4F1A\u8BDD"));
|
|
2125
2139
|
}
|
|
2126
2140
|
} catch (err) {
|
|
2127
|
-
if (json)
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
} else {
|
|
2131
|
-
console.error(chalk4.red(`Error: ${err.message}`));
|
|
2132
|
-
process.exit(1);
|
|
2133
|
-
}
|
|
2141
|
+
if (json) printJsonErr("chat", classifyError(err));
|
|
2142
|
+
console.error(chalk4.red(`Error: ${err.message}`));
|
|
2143
|
+
process.exit(1);
|
|
2134
2144
|
}
|
|
2135
2145
|
}
|
|
2136
2146
|
var _id = 0;
|
|
@@ -2320,10 +2330,9 @@ var resolveSessionId = (opts) => {
|
|
|
2320
2330
|
if (opts.sessionId && opts.continue) {
|
|
2321
2331
|
const message = "--session-id \u4E0E -c \u4E0D\u80FD\u540C\u65F6\u4F7F\u7528";
|
|
2322
2332
|
if (opts.json) {
|
|
2323
|
-
|
|
2324
|
-
} else {
|
|
2325
|
-
console.error(chalk4.red(`Error: ${message}`));
|
|
2333
|
+
printJsonErr("chat", { type: "invalid_args", message, hint: "\u4E8C\u9009\u4E00\uFF1A--session-id \u7EED\u63A5\u6307\u5B9A\u5BF9\u8BDD\uFF0C-c \u7EED\u63A5\u4E0A\u4E00\u6B21" });
|
|
2326
2334
|
}
|
|
2335
|
+
console.error(chalk4.red(`Error: ${message}`));
|
|
2327
2336
|
process.exit(1);
|
|
2328
2337
|
}
|
|
2329
2338
|
if (opts.sessionId) return opts.sessionId;
|
|
@@ -2336,10 +2345,9 @@ function createChatCommand() {
|
|
|
2336
2345
|
if (opts.json || opts.message) {
|
|
2337
2346
|
if (!opts.message) {
|
|
2338
2347
|
if (opts.json) {
|
|
2339
|
-
|
|
2340
|
-
} else {
|
|
2341
|
-
console.error('\u8BF7\u63D0\u4F9B\u6D88\u606F\uFF1Amaidang chat -m "\u4F60\u597D"');
|
|
2348
|
+
printJsonErr("chat", { type: "invalid_args", message: "\u7F3A\u5C11\u6D88\u606F\u5185\u5BB9", hint: '\u7528\u6CD5\uFF1Amaidang chat -m "\u4F60\u597D"' });
|
|
2342
2349
|
}
|
|
2350
|
+
console.error('\u8BF7\u63D0\u4F9B\u6D88\u606F\uFF1Amaidang chat -m "\u4F60\u597D"');
|
|
2343
2351
|
process.exit(1);
|
|
2344
2352
|
}
|
|
2345
2353
|
await chatSync(opts.message, !!opts.json, resolveSessionId(opts));
|
|
@@ -2371,9 +2379,6 @@ function getAgeVersion() {
|
|
|
2371
2379
|
async function fetchOrEmpty(url) {
|
|
2372
2380
|
try {
|
|
2373
2381
|
const res = await fetch(url);
|
|
2374
|
-
if (!res.ok) {
|
|
2375
|
-
throw new Error(await readApiMessage(res));
|
|
2376
|
-
}
|
|
2377
2382
|
return await readApiData(res);
|
|
2378
2383
|
} catch {
|
|
2379
2384
|
return null;
|
|
@@ -2390,8 +2395,6 @@ function createManifestCommand() {
|
|
|
2390
2395
|
fetchOrEmpty(`${apiUrl}/api/links`)
|
|
2391
2396
|
]);
|
|
2392
2397
|
const manifest = {
|
|
2393
|
-
tool: "maidang",
|
|
2394
|
-
command: "manifest",
|
|
2395
2398
|
version: getAgeVersion(),
|
|
2396
2399
|
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2397
2400
|
source: apiUrl,
|
|
@@ -2450,7 +2453,7 @@ function createManifestCommand() {
|
|
|
2450
2453
|
console.log("");
|
|
2451
2454
|
return;
|
|
2452
2455
|
}
|
|
2453
|
-
|
|
2456
|
+
printJsonOk("manifest", manifest);
|
|
2454
2457
|
});
|
|
2455
2458
|
return cmd;
|
|
2456
2459
|
}
|
|
@@ -2512,7 +2515,9 @@ function createNewsCommand() {
|
|
|
2512
2515
|
try {
|
|
2513
2516
|
const date = opts.date?.trim();
|
|
2514
2517
|
if (date !== void 0 && !isValidIssueDate(date)) {
|
|
2515
|
-
|
|
2518
|
+
const message = "--date \u683C\u5F0F\u5FC5\u987B\u4E3A YYYY-MM-DD\uFF0C\u4E14\u662F\u771F\u5B9E\u5B58\u5728\u7684\u65E5\u671F";
|
|
2519
|
+
if (opts.json) printJsonErr("news", { type: "invalid_args", message, hint: "\u4F8B\u5982 2026-08-23" }, 2);
|
|
2520
|
+
console.error(theme.err(message));
|
|
2516
2521
|
process.exit(2);
|
|
2517
2522
|
}
|
|
2518
2523
|
let path;
|
|
@@ -2526,18 +2531,14 @@ function createNewsCommand() {
|
|
|
2526
2531
|
const response = await fetch(`${baseApiUrl}${path}`);
|
|
2527
2532
|
if (response.status === 404) {
|
|
2528
2533
|
const message = opts.list ? "\u76EE\u5F55\u8FD8\u4E0D\u5B58\u5728" : date ? "\u8FD9\u4E00\u5929\u7684\u62A5\u7EB8\u8FD8\u4E0D\u5B58\u5728" : "\u4ECA\u5929\u8FD8\u6CA1\u51FA\u62A5\uFF0C\u7A0D\u540E\u518D\u6765\u770B\u770B";
|
|
2529
|
-
if (opts.json) {
|
|
2530
|
-
|
|
2531
|
-
} else {
|
|
2532
|
-
console.error(theme.err(message));
|
|
2533
|
-
}
|
|
2534
|
+
if (opts.json) printJsonErr("news", { type: "not_found", message, hint: "\u7528 --list \u67E5\u770B\u5DF2\u51FA\u76EE\u5F55" }, 3);
|
|
2535
|
+
console.error(theme.err(message));
|
|
2534
2536
|
process.exit(3);
|
|
2535
2537
|
}
|
|
2536
|
-
if (!response.ok) throw new Error(await readApiMessage(response));
|
|
2537
2538
|
if (opts.list) {
|
|
2538
2539
|
const data2 = await readApiData(response);
|
|
2539
2540
|
if (opts.json) {
|
|
2540
|
-
|
|
2541
|
+
printJsonOk("news", data2);
|
|
2541
2542
|
return;
|
|
2542
2543
|
}
|
|
2543
2544
|
printIssueList(data2.issues ?? []);
|
|
@@ -2545,11 +2546,12 @@ function createNewsCommand() {
|
|
|
2545
2546
|
}
|
|
2546
2547
|
const data = await readApiData(response);
|
|
2547
2548
|
if (opts.json) {
|
|
2548
|
-
|
|
2549
|
+
printJsonOk("news", { date, issue: data.issue });
|
|
2549
2550
|
return;
|
|
2550
2551
|
}
|
|
2551
2552
|
printIssue(data.issue);
|
|
2552
2553
|
} catch (error) {
|
|
2554
|
+
if (opts.json) printJsonErr("news", classifyError(error));
|
|
2553
2555
|
console.error(theme.err(`\u83B7\u53D6 AI \u65E5\u62A5\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
2554
2556
|
process.exit(1);
|
|
2555
2557
|
}
|
|
@@ -2580,14 +2582,14 @@ function createChangelogCommand() {
|
|
|
2580
2582
|
cmd.action(async (opts) => {
|
|
2581
2583
|
try {
|
|
2582
2584
|
const response = await fetch(`${baseApiUrl}/api/changelog`);
|
|
2583
|
-
if (!response.ok) throw new Error(await readApiMessage(response));
|
|
2584
2585
|
const entries = await readApiData(response);
|
|
2585
2586
|
if (opts.json) {
|
|
2586
|
-
|
|
2587
|
+
printJsonOk("changelog", { entries });
|
|
2587
2588
|
return;
|
|
2588
2589
|
}
|
|
2589
2590
|
printChangelog(entries ?? []);
|
|
2590
2591
|
} catch (error) {
|
|
2592
|
+
if (opts.json) printJsonErr("changelog", classifyError(error));
|
|
2591
2593
|
console.error(theme.err(`\u83B7\u53D6\u66F4\u65B0\u65E5\u5FD7\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
2592
2594
|
process.exit(1);
|
|
2593
2595
|
}
|
|
@@ -2680,7 +2682,7 @@ async function fetchLatestVersion(options) {
|
|
|
2680
2682
|
signal: controller.signal,
|
|
2681
2683
|
headers: { Accept: "application/json" }
|
|
2682
2684
|
});
|
|
2683
|
-
if (!response.ok) throw new
|
|
2685
|
+
if (!response.ok) throw new ApiRequestError(`registry HTTP ${response.status}`, response.status);
|
|
2684
2686
|
const payload = await response.json();
|
|
2685
2687
|
if (typeof payload.latest !== "string" || !payload.latest) {
|
|
2686
2688
|
throw new Error("registry response missing latest tag");
|
|
@@ -2715,7 +2717,7 @@ function buildInstallCommand(pm) {
|
|
|
2715
2717
|
return `${cmd} ${args.join(" ")}`;
|
|
2716
2718
|
}
|
|
2717
2719
|
function printJsonResult(result) {
|
|
2718
|
-
|
|
2720
|
+
printJsonOk("upgrade", result);
|
|
2719
2721
|
}
|
|
2720
2722
|
function confirmUpgrade() {
|
|
2721
2723
|
return new Promise((resolve) => {
|
|
@@ -2735,15 +2737,11 @@ function createUpgradeCommand() {
|
|
|
2735
2737
|
if (opts.pm) {
|
|
2736
2738
|
const requested = opts.pm.trim().toLowerCase();
|
|
2737
2739
|
if (!KNOWN_PACKAGE_MANAGERS.includes(requested)) {
|
|
2740
|
+
const message = `--pm \u53EA\u652F\u6301 ${KNOWN_PACKAGE_MANAGERS.join("/")}`;
|
|
2738
2741
|
if (opts.json) {
|
|
2739
|
-
|
|
2740
|
-
tool: "maidang",
|
|
2741
|
-
command: "upgrade",
|
|
2742
|
-
error: `--pm \u53EA\u652F\u6301 ${KNOWN_PACKAGE_MANAGERS.join("/")}`
|
|
2743
|
-
}, null, 2));
|
|
2744
|
-
} else {
|
|
2745
|
-
console.error(theme.err(`--pm \u53EA\u652F\u6301 ${KNOWN_PACKAGE_MANAGERS.join("/")}`));
|
|
2742
|
+
printJsonErr("upgrade", { type: "invalid_args", message, hint: "\u4F8B\u5982 maidang upgrade --pm pnpm" }, 2);
|
|
2746
2743
|
}
|
|
2744
|
+
console.error(theme.err(message));
|
|
2747
2745
|
process.exit(2);
|
|
2748
2746
|
}
|
|
2749
2747
|
pm = requested;
|
|
@@ -2754,12 +2752,11 @@ function createUpgradeCommand() {
|
|
|
2754
2752
|
latestVersion = await fetchLatestVersion({ timeoutMs: 1e4, allowNpmConfig: true });
|
|
2755
2753
|
} catch (error) {
|
|
2756
2754
|
const reason = error instanceof Error ? error.message : String(error);
|
|
2757
|
-
const
|
|
2755
|
+
const installHint = `\u53EF\u624B\u52A8\u6267\u884C: ${buildInstallCommand(pm)}`;
|
|
2758
2756
|
if (opts.json) {
|
|
2759
|
-
|
|
2760
|
-
} else {
|
|
2761
|
-
console.error(theme.err(message));
|
|
2757
|
+
printJsonErr("upgrade", { ...classifyError(error), message: `\u67E5\u8BE2 registry \u5931\u8D25: ${reason}`, hint: installHint });
|
|
2762
2758
|
}
|
|
2759
|
+
console.error(theme.err(`\u67E5\u8BE2 registry \u5931\u8D25: ${reason}\uFF0C${installHint}`));
|
|
2763
2760
|
process.exit(1);
|
|
2764
2761
|
}
|
|
2765
2762
|
const updateAvailable = compareVersions(latestVersion, currentVersion) > 0;
|
|
@@ -2846,20 +2843,18 @@ function createUpgradeCommand() {
|
|
|
2846
2843
|
const failureMessage = installResult.error?.message ?? `\u9000\u51FA\u7801 ${installResult.status}`;
|
|
2847
2844
|
result.upgraded = false;
|
|
2848
2845
|
if (opts.json) {
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
}
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
console.error(" " + theme.inkMuted("npm config set prefix ~/.npm-global"));
|
|
2862
|
-
}
|
|
2846
|
+
printJsonErr("upgrade", {
|
|
2847
|
+
type: "unknown",
|
|
2848
|
+
message: `\u5347\u7EA7\u5931\u8D25: ${failureMessage}`,
|
|
2849
|
+
hint: `\u53EF\u624B\u52A8\u6267\u884C ${result.installCommand}`
|
|
2850
|
+
});
|
|
2851
|
+
}
|
|
2852
|
+
console.error(theme.err(`\u5347\u7EA7\u5931\u8D25\uFF08${failureMessage}\uFF09\uFF0C\u53EF\u624B\u52A8\u6267\u884C\uFF1A`));
|
|
2853
|
+
console.error(" " + theme.link(result.installCommand));
|
|
2854
|
+
const errorCode = installResult.error?.code;
|
|
2855
|
+
if (errorCode === "EACCES") {
|
|
2856
|
+
console.error(" " + theme.inkSoft("\u6743\u9650\u4E0D\u8DB3\u65F6\u4E0D\u5EFA\u8BAE sudo\uFF0C\u5EFA\u8BAE\u7528 nvm \u6216\u4FEE\u6539 npm prefix\uFF1A"));
|
|
2857
|
+
console.error(" " + theme.inkMuted("npm config set prefix ~/.npm-global"));
|
|
2863
2858
|
}
|
|
2864
2859
|
process.exit(1);
|
|
2865
2860
|
}
|
|
@@ -2875,6 +2870,26 @@ function createUpgradeCommand() {
|
|
|
2875
2870
|
return cmd;
|
|
2876
2871
|
}
|
|
2877
2872
|
|
|
2873
|
+
// src/core/help.ts
|
|
2874
|
+
import { Help } from "commander";
|
|
2875
|
+
var SECTION_TITLES = {
|
|
2876
|
+
"Usage:": "\u7528\u6CD5:",
|
|
2877
|
+
"Arguments:": "\u53C2\u6570:",
|
|
2878
|
+
"Options:": "\u9009\u9879:",
|
|
2879
|
+
"Global Options:": "\u5168\u5C40\u9009\u9879:",
|
|
2880
|
+
"Commands:": "\u547D\u4EE4:"
|
|
2881
|
+
};
|
|
2882
|
+
var ChineseHelp = class extends Help {
|
|
2883
|
+
styleTitle(title) {
|
|
2884
|
+
return SECTION_TITLES[title] ?? title;
|
|
2885
|
+
}
|
|
2886
|
+
};
|
|
2887
|
+
function localizeHelp(root) {
|
|
2888
|
+
root.createHelp = () => Object.assign(new ChineseHelp(), root.configureHelp());
|
|
2889
|
+
root.helpOption("-h, --help", "\u663E\u793A\u5E2E\u52A9\u4FE1\u606F");
|
|
2890
|
+
root.commands.forEach(localizeHelp);
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2878
2893
|
// src/core/config.ts
|
|
2879
2894
|
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
2880
2895
|
import { homedir as homedir2 } from "os";
|
|
@@ -2936,7 +2951,7 @@ async function maybeNotifyUpdate(options) {
|
|
|
2936
2951
|
}
|
|
2937
2952
|
|
|
2938
2953
|
// src/index.ts
|
|
2939
|
-
var program = new
|
|
2954
|
+
var program = new Command18();
|
|
2940
2955
|
program.name("maidang").description("\u4E00\u4EFD\u53EF\u4EE5\u88AB AI \u8BFB\u53D6\u7684\u4E2A\u4EBA\u624B\u518C").version(getPackageVersion(), "-v, --version", "\u663E\u793A\u7248\u672C\u53F7");
|
|
2941
2956
|
program.addCommand(createCareerCommand());
|
|
2942
2957
|
program.addCommand(createCatCommand());
|
|
@@ -2953,6 +2968,8 @@ program.addCommand(createManifestCommand());
|
|
|
2953
2968
|
program.addCommand(createNewsCommand());
|
|
2954
2969
|
program.addCommand(createChangelogCommand());
|
|
2955
2970
|
program.addCommand(createUpgradeCommand());
|
|
2971
|
+
localizeHelp(program);
|
|
2972
|
+
program.helpCommand("help [command]", "\u663E\u793A\u547D\u4EE4\u5E2E\u52A9");
|
|
2956
2973
|
program.hook("postAction", async (_thisCommand, actionCommand) => {
|
|
2957
2974
|
const options = typeof actionCommand?.opts === "function" ? actionCommand.opts() : {};
|
|
2958
2975
|
await maybeNotifyUpdate({
|