maidang 29.9.6 → 29.11.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/dist/index.js +249 -80
- package/dist/index.js.map +4 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -124,9 +124,6 @@ function createHomeCommand() {
|
|
|
124
124
|
// src/commands/career.ts
|
|
125
125
|
import { Command as Command2 } from "commander";
|
|
126
126
|
|
|
127
|
-
// src/constants/api.ts
|
|
128
|
-
var DEFAULT_API_URL = "https://www.maidang.me";
|
|
129
|
-
|
|
130
127
|
// src/data/career.json
|
|
131
128
|
var career_default = {
|
|
132
129
|
career: {
|
|
@@ -184,6 +181,9 @@ var career_default = {
|
|
|
184
181
|
}
|
|
185
182
|
};
|
|
186
183
|
|
|
184
|
+
// src/constants/api.ts
|
|
185
|
+
var DEFAULT_API_URL = "https://api.maidang.me";
|
|
186
|
+
|
|
187
187
|
// src/utils/api-response.ts
|
|
188
188
|
var ApiRequestError = class extends Error {
|
|
189
189
|
constructor(message, status) {
|
|
@@ -200,7 +200,16 @@ function isApiEnvelope(value) {
|
|
|
200
200
|
return typeof record.code === "number" && typeof record.message === "string" && Object.prototype.hasOwnProperty.call(record, "data");
|
|
201
201
|
}
|
|
202
202
|
async function readApiData(response) {
|
|
203
|
-
const
|
|
203
|
+
const text = await response.text();
|
|
204
|
+
let payload;
|
|
205
|
+
try {
|
|
206
|
+
payload = text ? JSON.parse(text) : null;
|
|
207
|
+
} catch {
|
|
208
|
+
if (!response.ok) {
|
|
209
|
+
throw new ApiRequestError(text.trim().slice(0, 200) || `HTTP ${response.status}`, response.status);
|
|
210
|
+
}
|
|
211
|
+
throw new Error("Invalid API response shape");
|
|
212
|
+
}
|
|
204
213
|
if (isApiEnvelope(payload)) {
|
|
205
214
|
if (!response.ok || payload.code !== 0) {
|
|
206
215
|
throw new ApiRequestError(payload.message || `HTTP ${response.status}`, response.status);
|
|
@@ -215,29 +224,153 @@ async function readApiData(response) {
|
|
|
215
224
|
return payload;
|
|
216
225
|
}
|
|
217
226
|
|
|
227
|
+
// src/utils/api-client.ts
|
|
228
|
+
var TICKET_ERROR = "\u9886\u53D6\u8BBF\u5BA2\u7968\u5931\u8D25\uFF0C\u6682\u65F6\u6CA1\u6CD5\u5F00\u59CB\u5BF9\u8BDD";
|
|
229
|
+
function apiBaseUrl() {
|
|
230
|
+
return (process.env.MAIDANG_API_URL || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
231
|
+
}
|
|
232
|
+
function resolveUrl(path) {
|
|
233
|
+
if (path.startsWith("http://") || path.startsWith("https://")) return path;
|
|
234
|
+
return `${apiBaseUrl()}${path.startsWith("/") ? path : `/${path}`}`;
|
|
235
|
+
}
|
|
236
|
+
async function apiFetch(path, init) {
|
|
237
|
+
const headers = new Headers(init?.headers);
|
|
238
|
+
if (!headers.has("accept")) headers.set("accept", "application/json");
|
|
239
|
+
return fetch(resolveUrl(path), { ...init, headers });
|
|
240
|
+
}
|
|
241
|
+
function readableTicketFailure(message) {
|
|
242
|
+
const text = message.trim();
|
|
243
|
+
if (!text || /^[A-Za-z0-9_ .-]+$/.test(text)) return TICKET_ERROR;
|
|
244
|
+
return text;
|
|
245
|
+
}
|
|
246
|
+
async function issueVisitorTicket() {
|
|
247
|
+
let response;
|
|
248
|
+
try {
|
|
249
|
+
response = await fetch(`${apiBaseUrl()}/v1/auth/tickets`, {
|
|
250
|
+
method: "POST",
|
|
251
|
+
headers: {
|
|
252
|
+
accept: "application/json",
|
|
253
|
+
"content-type": "application/json"
|
|
254
|
+
},
|
|
255
|
+
body: "{}"
|
|
256
|
+
});
|
|
257
|
+
} catch {
|
|
258
|
+
throw new ApiRequestError(TICKET_ERROR, 401);
|
|
259
|
+
}
|
|
260
|
+
let message = "";
|
|
261
|
+
let code;
|
|
262
|
+
let ticket;
|
|
263
|
+
try {
|
|
264
|
+
const payload = await response.json();
|
|
265
|
+
code = payload.code;
|
|
266
|
+
if (typeof payload.message === "string") message = payload.message;
|
|
267
|
+
ticket = payload.data?.ticket;
|
|
268
|
+
} catch {
|
|
269
|
+
ticket = void 0;
|
|
270
|
+
}
|
|
271
|
+
const accepted = response.ok && (typeof code !== "number" || code === 0);
|
|
272
|
+
if (accepted && typeof ticket === "string" && ticket.startsWith("tkt_") && ticket.length > "tkt_".length) {
|
|
273
|
+
return ticket;
|
|
274
|
+
}
|
|
275
|
+
throw new ApiRequestError(readableTicketFailure(message), 401);
|
|
276
|
+
}
|
|
277
|
+
|
|
218
278
|
// src/utils/os-agent-api.ts
|
|
219
|
-
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
279
|
+
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
220
280
|
import { homedir } from "os";
|
|
221
281
|
import { join } from "path";
|
|
222
|
-
var API_URL = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
223
282
|
var AGENT_HEADER = "x-maidang-agent";
|
|
224
283
|
var CONFIG_DIR = process.env.MAIDANG_CLI_HOME || join(homedir(), ".maidang");
|
|
225
284
|
var CONFIG_PATH = join(CONFIG_DIR, "cli.json");
|
|
226
285
|
var RUN_TIMEOUT_MS = 18e4;
|
|
227
|
-
|
|
286
|
+
var RESUME_MISS = "\u8FD9\u4E2A\u4F1A\u8BDD\u4E0D\u5C5E\u4E8E\u5F53\u524D\u8BBF\u5BA2\u7968\u636E\uFF0C\u65E0\u6CD5\u7EED\u63A5";
|
|
287
|
+
function readConfigString(value) {
|
|
288
|
+
return typeof value === "string" && value.trim() ? value : void 0;
|
|
289
|
+
}
|
|
290
|
+
function usableVisitorTicket(value) {
|
|
291
|
+
if (typeof value !== "string") return void 0;
|
|
292
|
+
const ticket = value.trim();
|
|
293
|
+
if (!ticket.startsWith("tkt_") || ticket.length <= "tkt_".length) return void 0;
|
|
294
|
+
return ticket;
|
|
295
|
+
}
|
|
296
|
+
function readConfigFile() {
|
|
228
297
|
try {
|
|
229
|
-
|
|
298
|
+
const raw = JSON.parse(readFileSync(CONFIG_PATH, "utf8"));
|
|
299
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
|
|
300
|
+
return { ...raw };
|
|
230
301
|
} catch {
|
|
231
302
|
return {};
|
|
232
303
|
}
|
|
233
304
|
}
|
|
234
|
-
function
|
|
305
|
+
function loadConfig() {
|
|
306
|
+
const record = readConfigFile();
|
|
307
|
+
const config = {};
|
|
308
|
+
const agentToken = readConfigString(record.agentToken);
|
|
309
|
+
const sessionId = readConfigString(record.sessionId);
|
|
310
|
+
const visitorTicket = usableVisitorTicket(record.visitorTicket);
|
|
311
|
+
if (agentToken) config.agentToken = agentToken;
|
|
312
|
+
if (sessionId) config.sessionId = sessionId;
|
|
313
|
+
if (visitorTicket) config.visitorTicket = visitorTicket;
|
|
314
|
+
return config;
|
|
315
|
+
}
|
|
316
|
+
function writeConfig(next) {
|
|
235
317
|
try {
|
|
236
318
|
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
237
|
-
writeFileSync(CONFIG_PATH, JSON.stringify(
|
|
319
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(next, null, 2), { mode: 384 });
|
|
320
|
+
chmodSync(CONFIG_PATH, 384);
|
|
238
321
|
} catch {
|
|
239
322
|
}
|
|
240
323
|
}
|
|
324
|
+
function saveConfig(patch) {
|
|
325
|
+
const next = readConfigFile();
|
|
326
|
+
if (patch.agentToken) next.agentToken = patch.agentToken;
|
|
327
|
+
if (patch.sessionId) next.sessionId = patch.sessionId;
|
|
328
|
+
if (patch.visitorTicket) next.visitorTicket = patch.visitorTicket;
|
|
329
|
+
writeConfig(next);
|
|
330
|
+
}
|
|
331
|
+
function clearStoredVisitorTicket() {
|
|
332
|
+
const next = readConfigFile();
|
|
333
|
+
if (!Object.prototype.hasOwnProperty.call(next, "visitorTicket")) return;
|
|
334
|
+
delete next.visitorTicket;
|
|
335
|
+
writeConfig(next);
|
|
336
|
+
}
|
|
337
|
+
function saveVisitorTicket(ticket) {
|
|
338
|
+
const current = loadConfig();
|
|
339
|
+
saveConfig({
|
|
340
|
+
visitorTicket: ticket,
|
|
341
|
+
...current.sessionId ? { sessionId: current.sessionId } : {}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
async function ensureVisitorTicket(config) {
|
|
345
|
+
if (config.visitorTicket) return config.visitorTicket;
|
|
346
|
+
const ticket = await issueVisitorTicket();
|
|
347
|
+
saveVisitorTicket(ticket);
|
|
348
|
+
return ticket;
|
|
349
|
+
}
|
|
350
|
+
async function refreshVisitorTicket(auth) {
|
|
351
|
+
auth.refreshed = true;
|
|
352
|
+
clearStoredVisitorTicket();
|
|
353
|
+
auth.ticket = await issueVisitorTicket();
|
|
354
|
+
saveVisitorTicket(auth.ticket);
|
|
355
|
+
}
|
|
356
|
+
async function chatFetch(auth, path, init) {
|
|
357
|
+
const send = () => {
|
|
358
|
+
const headers = new Headers(init.headers);
|
|
359
|
+
headers.set("authorization", `Bearer ${auth.ticket}`);
|
|
360
|
+
return apiFetch(path, { ...init, headers });
|
|
361
|
+
};
|
|
362
|
+
const first = await send();
|
|
363
|
+
if (first.status !== 401 || auth.refreshed) return first;
|
|
364
|
+
await first.body?.cancel().catch(() => void 0);
|
|
365
|
+
await refreshVisitorTicket(auth);
|
|
366
|
+
return send();
|
|
367
|
+
}
|
|
368
|
+
async function rejectChat(response, sessionId) {
|
|
369
|
+
if (sessionId && response.status === 404) {
|
|
370
|
+
throw new ApiRequestError(RESUME_MISS, response.status);
|
|
371
|
+
}
|
|
372
|
+
return readError(response);
|
|
373
|
+
}
|
|
241
374
|
function readLastSessionId() {
|
|
242
375
|
return loadConfig().sessionId;
|
|
243
376
|
}
|
|
@@ -260,13 +393,17 @@ async function readError(response) {
|
|
|
260
393
|
const headerRetry = Number(response.headers.get("Retry-After"));
|
|
261
394
|
throw new RateLimitError(retryAfter || headerRetry || 60);
|
|
262
395
|
}
|
|
263
|
-
throw new
|
|
396
|
+
throw new ApiRequestError(message, response.status);
|
|
264
397
|
}
|
|
265
398
|
async function runAgentChat(input) {
|
|
266
399
|
const config = loadConfig();
|
|
400
|
+
const auth = {
|
|
401
|
+
ticket: await ensureVisitorTicket(config),
|
|
402
|
+
refreshed: false
|
|
403
|
+
};
|
|
267
404
|
const token = config.agentToken;
|
|
268
405
|
const sessionId = input.sessionId;
|
|
269
|
-
const runsResponse = await
|
|
406
|
+
const runsResponse = await chatFetch(auth, "/v1/hat/runs", {
|
|
270
407
|
method: "POST",
|
|
271
408
|
headers: {
|
|
272
409
|
"Content-Type": "application/json",
|
|
@@ -281,24 +418,26 @@ async function runAgentChat(input) {
|
|
|
281
418
|
}
|
|
282
419
|
})
|
|
283
420
|
});
|
|
284
|
-
if (!runsResponse.ok) await
|
|
421
|
+
if (!runsResponse.ok) await rejectChat(runsResponse, sessionId);
|
|
285
422
|
const issuedToken = runsResponse.headers.get(AGENT_HEADER) ?? token;
|
|
286
423
|
const queued = (await runsResponse.json()).data ?? {};
|
|
287
|
-
const runId = queued.runId;
|
|
424
|
+
const runId = queued.runId ?? queued.id;
|
|
288
425
|
const newSessionId = queued.sessionId ?? "";
|
|
289
426
|
if (!runId) throw new Error("\u670D\u52A1\u7AEF\u672A\u8FD4\u56DE runId");
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
const eventsResponse = await fetch(`${API_URL}/api/os-agent/runs/${runId}/events`, {
|
|
427
|
+
saveConfig({
|
|
428
|
+
...issuedToken ? { agentToken: issuedToken } : {},
|
|
429
|
+
...newSessionId ? { sessionId: newSessionId } : {},
|
|
430
|
+
visitorTicket: auth.ticket
|
|
431
|
+
});
|
|
432
|
+
const eventsResponse = await chatFetch(auth, `/v1/hat/runs/${runId}/events`, {
|
|
297
433
|
method: "POST",
|
|
298
|
-
headers:
|
|
434
|
+
headers: {
|
|
435
|
+
accept: "text/event-stream",
|
|
436
|
+
...issuedToken ? { [AGENT_HEADER]: issuedToken } : {}
|
|
437
|
+
},
|
|
299
438
|
signal: AbortSignal.timeout(RUN_TIMEOUT_MS)
|
|
300
439
|
});
|
|
301
|
-
if (!eventsResponse.ok) await
|
|
440
|
+
if (!eventsResponse.ok) await rejectChat(eventsResponse, sessionId);
|
|
302
441
|
if (!eventsResponse.body) throw new Error("\u4E8B\u4EF6\u6D41\u4E3A\u7A7A");
|
|
303
442
|
let response = "";
|
|
304
443
|
let deltaText = "";
|
|
@@ -329,12 +468,11 @@ async function runAgentChat(input) {
|
|
|
329
468
|
return {
|
|
330
469
|
response: response || deltaText,
|
|
331
470
|
sessionId: newSessionId,
|
|
332
|
-
runId
|
|
333
|
-
silent: terminal === "run.silent"
|
|
471
|
+
runId
|
|
334
472
|
};
|
|
335
473
|
}
|
|
336
474
|
async function consumeSse(body, onEvent) {
|
|
337
|
-
const terminalEvents = /* @__PURE__ */ new Set(["run.completed", "run.
|
|
475
|
+
const terminalEvents = /* @__PURE__ */ new Set(["run.completed", "run.failed", "run.cancelled", "run.superseded"]);
|
|
338
476
|
const reader = body.getReader();
|
|
339
477
|
const decoder = new TextDecoder();
|
|
340
478
|
let buffer = "";
|
|
@@ -429,9 +567,8 @@ function classifyError(error) {
|
|
|
429
567
|
// src/commands/career.ts
|
|
430
568
|
function createCareerCommand() {
|
|
431
569
|
const cmd = new Command2("career").description("\u5DE5\u4F5C\u7ECF\u5386\uFF08\u4E0E\u7AD9\u70B9 career \u9875\u540C\u6E90\uFF0C--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").action(async (opts) => {
|
|
432
|
-
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
433
570
|
try {
|
|
434
|
-
const response = await
|
|
571
|
+
const response = await apiFetch("/v1/career");
|
|
435
572
|
const experiences = await readApiData(response);
|
|
436
573
|
const data = {
|
|
437
574
|
career: {
|
|
@@ -476,8 +613,7 @@ function createCareerCommand() {
|
|
|
476
613
|
// src/commands/cat.ts
|
|
477
614
|
import { Command as Command3 } from "commander";
|
|
478
615
|
var getCatMoments = async () => {
|
|
479
|
-
const
|
|
480
|
-
const res = await fetch(`${apiUrl}/api/cat`);
|
|
616
|
+
const res = await apiFetch("/v1/images");
|
|
481
617
|
const data = await readApiData(res);
|
|
482
618
|
return Array.isArray(data) ? data : [];
|
|
483
619
|
};
|
|
@@ -547,13 +683,12 @@ function buildPaginationUrl(baseUrl, options, extra = {}) {
|
|
|
547
683
|
|
|
548
684
|
// src/commands/posts.ts
|
|
549
685
|
function createPostsCommand() {
|
|
550
|
-
const baseApiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
551
686
|
const cmd = new Command4("posts").description("\u52A8\u6001\u5217\u8868\uFF0C\u9ED8\u8BA4\u663E\u793A\u5168\u90E8\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").option("-s, --slug <slug>", "\u6309 slug \u83B7\u53D6\u5355\u7BC7\u6587\u7AE0/\u52A8\u6001").option("--page <page>", "\u9875\u7801\uFF0C\u4ECE 1 \u5F00\u59CB").option("--page-size <size>", "\u6BCF\u9875\u6761\u6570");
|
|
552
687
|
cmd.action(async (opts) => {
|
|
553
688
|
const slug = opts.slug?.trim();
|
|
554
689
|
try {
|
|
555
690
|
if (slug) {
|
|
556
|
-
const response2 = await
|
|
691
|
+
const response2 = await apiFetch(`/v1/posts/${encodeURIComponent(slug)}`);
|
|
557
692
|
const data2 = await readApiData(response2);
|
|
558
693
|
if (opts.json) {
|
|
559
694
|
printJsonOk("posts", { slug, post: data2 });
|
|
@@ -567,8 +702,8 @@ function createPostsCommand() {
|
|
|
567
702
|
return;
|
|
568
703
|
}
|
|
569
704
|
const usesPagination = opts.page !== void 0 || opts.pageSize !== void 0;
|
|
570
|
-
const url = usesPagination ? buildPaginationUrl(`${
|
|
571
|
-
const response = await
|
|
705
|
+
const url = usesPagination ? buildPaginationUrl(`${apiBaseUrl()}/v1/posts`, opts) : "/v1/posts";
|
|
706
|
+
const response = await apiFetch(url);
|
|
572
707
|
const responseData = await readApiData(response);
|
|
573
708
|
const data = Array.isArray(responseData) ? {
|
|
574
709
|
items: responseData,
|
|
@@ -1494,9 +1629,18 @@ var songs_default = {
|
|
|
1494
1629
|
};
|
|
1495
1630
|
|
|
1496
1631
|
// src/utils/clipboard.ts
|
|
1497
|
-
import {
|
|
1632
|
+
import { execFileSync } from "child_process";
|
|
1633
|
+
import { platform } from "os";
|
|
1498
1634
|
function copyToClipboard(text) {
|
|
1499
|
-
|
|
1635
|
+
if (platform() === "darwin") {
|
|
1636
|
+
execFileSync("pbcopy", { input: text, stdio: ["pipe", "ignore", "ignore"] });
|
|
1637
|
+
return;
|
|
1638
|
+
}
|
|
1639
|
+
try {
|
|
1640
|
+
execFileSync("wl-copy", { input: text, stdio: ["pipe", "ignore", "ignore"] });
|
|
1641
|
+
} catch {
|
|
1642
|
+
execFileSync("xclip", ["-selection", "clipboard"], { input: text, stdio: ["pipe", "ignore", "ignore"] });
|
|
1643
|
+
}
|
|
1500
1644
|
}
|
|
1501
1645
|
|
|
1502
1646
|
// src/commands/songs.ts
|
|
@@ -1573,9 +1717,8 @@ ${entry.year} \u5E74\u5EA6\u6B4C\u5355 \xB7 ${entry.songs.length} \u9996
|
|
|
1573
1717
|
import { Command as Command6 } from "commander";
|
|
1574
1718
|
function createLinksCommand() {
|
|
1575
1719
|
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) => {
|
|
1576
|
-
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1577
1720
|
try {
|
|
1578
|
-
const res = await
|
|
1721
|
+
const res = await apiFetch("/v1/links");
|
|
1579
1722
|
const links = await readApiData(res);
|
|
1580
1723
|
if (opts.json) {
|
|
1581
1724
|
printJsonOk("links", links);
|
|
@@ -1609,9 +1752,8 @@ function createLinksCommand() {
|
|
|
1609
1752
|
import { Command as Command7 } from "commander";
|
|
1610
1753
|
function createProjectsCommand() {
|
|
1611
1754
|
return new Command7("projects").description("\u9879\u76EE\u5217\u8868\uFF0C\u9ED8\u8BA4\u6BCF\u9875 10 \u6761\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").option("--page <page>", "\u9875\u7801\uFF0C\u4ECE 1 \u5F00\u59CB", "1").option("--page-size <size>", "\u6BCF\u9875\u6761\u6570\uFF0C\u9ED8\u8BA4 10", "10").action(async (opts) => {
|
|
1612
|
-
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1613
1755
|
try {
|
|
1614
|
-
const response = await
|
|
1756
|
+
const response = await apiFetch(buildPaginationUrl(`${apiBaseUrl()}/v1/projects`, opts));
|
|
1615
1757
|
const data = await readApiData(response);
|
|
1616
1758
|
if (opts.json) {
|
|
1617
1759
|
printJsonOk("projects", data);
|
|
@@ -1649,9 +1791,8 @@ var categoryNames = {
|
|
|
1649
1791
|
};
|
|
1650
1792
|
function createToolsCommand() {
|
|
1651
1793
|
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) => {
|
|
1652
|
-
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1653
1794
|
try {
|
|
1654
|
-
const res = await
|
|
1795
|
+
const res = await apiFetch("/v1/tools");
|
|
1655
1796
|
const tools = await readApiData(res);
|
|
1656
1797
|
if (opts.json) {
|
|
1657
1798
|
printJsonOk("tools", tools);
|
|
@@ -1693,10 +1834,9 @@ function createToolsCommand() {
|
|
|
1693
1834
|
import { Command as Command9 } from "commander";
|
|
1694
1835
|
function createSkillsCommand() {
|
|
1695
1836
|
return new Command9("skills").description("\u53EF\u5206\u4EAB\u7684 Agent Skills\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--slug <slug>", "\u67E5\u8BE2\u4E00\u4E2A Skill \u7684\u5B8C\u6574\u5185\u5BB9").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").action(async (opts) => {
|
|
1696
|
-
const apiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
1697
1837
|
try {
|
|
1698
1838
|
if (opts.slug) {
|
|
1699
|
-
const response2 = await
|
|
1839
|
+
const response2 = await apiFetch(`/v1/skills/${encodeURIComponent(opts.slug)}`);
|
|
1700
1840
|
const skill = await readApiData(response2);
|
|
1701
1841
|
if (opts.json) {
|
|
1702
1842
|
printJsonOk("skills", { slug: opts.slug, skill });
|
|
@@ -1715,7 +1855,7 @@ function createSkillsCommand() {
|
|
|
1715
1855
|
console.log(skill.content);
|
|
1716
1856
|
return;
|
|
1717
1857
|
}
|
|
1718
|
-
const response = await
|
|
1858
|
+
const response = await apiFetch("/v1/skills");
|
|
1719
1859
|
const skills = await readApiData(response);
|
|
1720
1860
|
if (opts.json) {
|
|
1721
1861
|
printJsonOk("skills", skills);
|
|
@@ -1747,16 +1887,28 @@ import { Command as Command10 } from "commander";
|
|
|
1747
1887
|
import chalk2 from "chalk";
|
|
1748
1888
|
|
|
1749
1889
|
// src/utils/contact-api.ts
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
const res = await fetch(`${apiUrl}/api/contact`, {
|
|
1890
|
+
function postContact(input, ticket) {
|
|
1891
|
+
return apiFetch("/v1/contact", {
|
|
1753
1892
|
method: "POST",
|
|
1754
1893
|
headers: {
|
|
1755
|
-
"Content-Type": "application/json"
|
|
1894
|
+
"Content-Type": "application/json",
|
|
1895
|
+
...ticket ? { authorization: `Bearer ${ticket}` } : {}
|
|
1756
1896
|
},
|
|
1757
1897
|
body: JSON.stringify(input)
|
|
1758
1898
|
});
|
|
1759
|
-
|
|
1899
|
+
}
|
|
1900
|
+
async function sendContactMessage(input) {
|
|
1901
|
+
const first = await postContact(input);
|
|
1902
|
+
if (first.status !== 401) return readApiData(first);
|
|
1903
|
+
let ticket = null;
|
|
1904
|
+
try {
|
|
1905
|
+
ticket = await issueVisitorTicket();
|
|
1906
|
+
} catch {
|
|
1907
|
+
ticket = null;
|
|
1908
|
+
}
|
|
1909
|
+
if (!ticket) return readApiData(first);
|
|
1910
|
+
await first.body?.cancel().catch(() => void 0);
|
|
1911
|
+
return readApiData(await postContact(input, ticket));
|
|
1760
1912
|
}
|
|
1761
1913
|
|
|
1762
1914
|
// src/commands/contact.ts
|
|
@@ -2157,7 +2309,7 @@ async function chatSync(message, json = false, sessionId) {
|
|
|
2157
2309
|
if (json) {
|
|
2158
2310
|
printJsonOk("chat", {
|
|
2159
2311
|
message,
|
|
2160
|
-
response: result.
|
|
2312
|
+
response: result.response,
|
|
2161
2313
|
sessionId: result.sessionId,
|
|
2162
2314
|
runId: result.runId
|
|
2163
2315
|
});
|
|
@@ -2412,7 +2564,7 @@ function getAgeVersion() {
|
|
|
2412
2564
|
// src/commands/manifest.ts
|
|
2413
2565
|
async function fetchOrEmpty(url) {
|
|
2414
2566
|
try {
|
|
2415
|
-
const res = await
|
|
2567
|
+
const res = await apiFetch(url);
|
|
2416
2568
|
return await readApiData(res);
|
|
2417
2569
|
} catch {
|
|
2418
2570
|
return null;
|
|
@@ -2420,13 +2572,13 @@ async function fetchOrEmpty(url) {
|
|
|
2420
2572
|
}
|
|
2421
2573
|
function createManifestCommand() {
|
|
2422
2574
|
const cmd = new Command13("manifest").description("\u4E00\u6B21\u6027\u8F93\u51FA\u5168\u90E8\u516C\u5F00\u6570\u636E\uFF0C\u7ED9 AI agent \u7528").option("--json", "\u8F93\u51FA JSON\uFF08\u9ED8\u8BA4\u5C31\u662F JSON\uFF0C\u52A0\u4E0D\u52A0\u90FD\u4E00\u6837\uFF09").option("--pretty", "\u4EBA\u7C7B\u53EF\u8BFB\u7684\u7B80\u7248\u6982\u89C8\uFF08\u4E0D\u542B\u6B63\u6587\uFF09").action(async (opts) => {
|
|
2423
|
-
const apiUrl =
|
|
2575
|
+
const apiUrl = apiBaseUrl();
|
|
2424
2576
|
const [careerExperiences, projects, tools, skills, links] = await Promise.all([
|
|
2425
|
-
fetchOrEmpty(
|
|
2426
|
-
fetchOrEmpty(
|
|
2427
|
-
fetchOrEmpty(
|
|
2428
|
-
fetchOrEmpty(
|
|
2429
|
-
fetchOrEmpty(
|
|
2577
|
+
fetchOrEmpty("/v1/career"),
|
|
2578
|
+
fetchOrEmpty("/v1/projects"),
|
|
2579
|
+
fetchOrEmpty("/v1/tools"),
|
|
2580
|
+
fetchOrEmpty("/v1/skills"),
|
|
2581
|
+
fetchOrEmpty("/v1/links")
|
|
2430
2582
|
]);
|
|
2431
2583
|
const manifest = {
|
|
2432
2584
|
version: getAgeVersion(),
|
|
@@ -2500,18 +2652,27 @@ function isValidIssueDate(value) {
|
|
|
2500
2652
|
const date = /* @__PURE__ */ new Date(`${value}T00:00:00.000Z`);
|
|
2501
2653
|
return !Number.isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value;
|
|
2502
2654
|
}
|
|
2655
|
+
function issueLabel(issue) {
|
|
2656
|
+
const day = issue.issueDate.slice(0, 10);
|
|
2657
|
+
return issue.issueNumber != null ? `\u7B2C ${issue.issueNumber} \u671F \xB7 ${day}` : day;
|
|
2658
|
+
}
|
|
2503
2659
|
function printIssue(issue) {
|
|
2660
|
+
const items = issue.items ?? [];
|
|
2504
2661
|
console.log("");
|
|
2505
2662
|
console.log(" " + eyebrow("news"));
|
|
2506
|
-
console.log(" " + theme.ink(`\u9EA6\u5F53 AI \u65E5\u62A5 \xB7
|
|
2663
|
+
console.log(" " + theme.ink(`\u9EA6\u5F53 AI \u65E5\u62A5 \xB7 ${issueLabel(issue)}`));
|
|
2507
2664
|
console.log(" " + hairline());
|
|
2508
2665
|
console.log("");
|
|
2509
2666
|
if (issue.headline) {
|
|
2510
2667
|
console.log(" " + theme.inkBody("\u5934\u6761\uFF1A") + renderMarkdownAnsi(issue.headline).trimEnd());
|
|
2511
2668
|
console.log("");
|
|
2512
2669
|
}
|
|
2670
|
+
if (items.length === 0 && issue.dek) {
|
|
2671
|
+
printMarkdownAnsi(issue.dek, { indent: " " });
|
|
2672
|
+
console.log("");
|
|
2673
|
+
}
|
|
2513
2674
|
let lastSection = "";
|
|
2514
|
-
|
|
2675
|
+
items.forEach((item) => {
|
|
2515
2676
|
if (item.section !== lastSection) {
|
|
2516
2677
|
if (lastSection) console.log("");
|
|
2517
2678
|
console.log(" " + theme.accent(`\u25CF ${item.section}`));
|
|
@@ -2526,7 +2687,7 @@ function printIssue(issue) {
|
|
|
2526
2687
|
}
|
|
2527
2688
|
});
|
|
2528
2689
|
console.log("");
|
|
2529
|
-
const sourceCount = issue.sourceCount ??
|
|
2690
|
+
const sourceCount = issue.sourceCount ?? items.reduce((sum, item) => sum + (item.sources?.length ?? 0), 0);
|
|
2530
2691
|
console.log(" " + theme.inkMuted(`\u6765\u6E90 ${sourceCount} \u6761 \xB7 \u5B8C\u6574\u7248 maidang.me \u65B0\u95FB\u5E94\u7528`));
|
|
2531
2692
|
console.log("");
|
|
2532
2693
|
}
|
|
@@ -2537,13 +2698,23 @@ function printIssueList(issues) {
|
|
|
2537
2698
|
console.log(" " + hairline());
|
|
2538
2699
|
console.log("");
|
|
2539
2700
|
issues.forEach((issue, index) => {
|
|
2540
|
-
console.log(` ${theme.inkMuted(String(index + 1).padStart(2, "0"))} ${theme.inkBody(
|
|
2701
|
+
console.log(` ${theme.inkMuted(String(index + 1).padStart(2, "0"))} ${theme.inkBody(issueLabel(issue))}`);
|
|
2541
2702
|
if (issue.headline) printMarkdownAnsi(issue.headline, { indent: " " });
|
|
2542
2703
|
});
|
|
2543
2704
|
console.log("");
|
|
2544
2705
|
}
|
|
2706
|
+
function normalizeIssues(data) {
|
|
2707
|
+
if (Array.isArray(data)) return data;
|
|
2708
|
+
if (!data || typeof data !== "object") return [];
|
|
2709
|
+
const record = data;
|
|
2710
|
+
if (Array.isArray(record.issues)) return record.issues;
|
|
2711
|
+
if (record.issue && typeof record.issue === "object") return [record.issue];
|
|
2712
|
+
return [];
|
|
2713
|
+
}
|
|
2714
|
+
function issueDay(issue) {
|
|
2715
|
+
return issue.issueDate.slice(0, 10);
|
|
2716
|
+
}
|
|
2545
2717
|
function createNewsCommand() {
|
|
2546
|
-
const baseApiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
2547
2718
|
const cmd = new Command14("news").description("AI \u65E5\u62A5\uFF0C\u6BCF\u5929\u4E00\u671F\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F").option("--list", "\u6700\u8FD1 10 \u671F\u76EE\u5F55").option("--date <date>", "\u6307\u5B9A\u65E5\u671F\u4E00\u671F\uFF0C\u683C\u5F0F YYYY-MM-DD");
|
|
2548
2719
|
cmd.action(async (opts) => {
|
|
2549
2720
|
try {
|
|
@@ -2554,36 +2725,35 @@ function createNewsCommand() {
|
|
|
2554
2725
|
console.error(theme.err(message));
|
|
2555
2726
|
process.exit(2);
|
|
2556
2727
|
}
|
|
2557
|
-
|
|
2558
|
-
if (opts.list) {
|
|
2559
|
-
path = "/api/ai-news/issues";
|
|
2560
|
-
} else if (date) {
|
|
2561
|
-
path = `/api/ai-news/issues/${encodeURIComponent(date)}`;
|
|
2562
|
-
} else {
|
|
2563
|
-
path = "/api/ai-news/latest";
|
|
2564
|
-
}
|
|
2565
|
-
const response = await fetch(`${baseApiUrl}${path}`);
|
|
2728
|
+
const response = await apiFetch("/v1/news");
|
|
2566
2729
|
if (response.status === 404) {
|
|
2567
2730
|
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";
|
|
2568
2731
|
if (opts.json) printJsonErr("news", { type: "not_found", message, hint: "\u7528 --list \u67E5\u770B\u5DF2\u51FA\u76EE\u5F55" }, 3);
|
|
2569
2732
|
console.error(theme.err(message));
|
|
2570
2733
|
process.exit(3);
|
|
2571
2734
|
}
|
|
2735
|
+
const issues = normalizeIssues(await readApiData(response));
|
|
2572
2736
|
if (opts.list) {
|
|
2573
|
-
const
|
|
2737
|
+
const catalog = issues.slice(0, 10);
|
|
2574
2738
|
if (opts.json) {
|
|
2575
|
-
printJsonOk("news",
|
|
2739
|
+
printJsonOk("news", { issues: catalog });
|
|
2576
2740
|
return;
|
|
2577
2741
|
}
|
|
2578
|
-
printIssueList(
|
|
2742
|
+
printIssueList(catalog);
|
|
2579
2743
|
return;
|
|
2580
2744
|
}
|
|
2581
|
-
const
|
|
2745
|
+
const issue = date ? issues.find((item) => issueDay(item) === date) : issues[0];
|
|
2746
|
+
if (!issue) {
|
|
2747
|
+
const message = date ? "\u8FD9\u4E00\u5929\u7684\u62A5\u7EB8\u8FD8\u4E0D\u5B58\u5728" : "\u4ECA\u5929\u8FD8\u6CA1\u51FA\u62A5\uFF0C\u7A0D\u540E\u518D\u6765\u770B\u770B";
|
|
2748
|
+
if (opts.json) printJsonErr("news", { type: "not_found", message, hint: "\u7528 --list \u67E5\u770B\u5DF2\u51FA\u76EE\u5F55" }, 3);
|
|
2749
|
+
console.error(theme.err(message));
|
|
2750
|
+
process.exit(3);
|
|
2751
|
+
}
|
|
2582
2752
|
if (opts.json) {
|
|
2583
|
-
printJsonOk("news", { date, issue
|
|
2753
|
+
printJsonOk("news", { date, issue });
|
|
2584
2754
|
return;
|
|
2585
2755
|
}
|
|
2586
|
-
printIssue(
|
|
2756
|
+
printIssue(issue);
|
|
2587
2757
|
} catch (error) {
|
|
2588
2758
|
if (opts.json) printJsonErr("news", classifyError(error));
|
|
2589
2759
|
console.error(theme.err(`\u83B7\u53D6 AI \u65E5\u62A5\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
|
|
@@ -2611,11 +2781,10 @@ function printChangelog(entries) {
|
|
|
2611
2781
|
console.log("");
|
|
2612
2782
|
}
|
|
2613
2783
|
function createChangelogCommand() {
|
|
2614
|
-
const baseApiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
|
|
2615
2784
|
const cmd = new Command15("changelog").description("\u7AD9\u70B9\u66F4\u65B0\u65E5\u5FD7\uFF08--json \u8F93\u51FA\u7ED3\u6784\u5316\u6570\u636E\uFF09").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F");
|
|
2616
2785
|
cmd.action(async (opts) => {
|
|
2617
2786
|
try {
|
|
2618
|
-
const response = await
|
|
2787
|
+
const response = await apiFetch("/v1/changelog");
|
|
2619
2788
|
const entries = await readApiData(response);
|
|
2620
2789
|
if (opts.json) {
|
|
2621
2790
|
printJsonOk("changelog", { entries });
|