maidang 29.8.0 → 29.8.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 CHANGED
@@ -22,10 +22,34 @@ maidang posts --slug <slug> --json
22
22
  maidang songs 年度歌单
23
23
  maidang links 链接
24
24
  maidang projects 项目
25
+ maidang tools 常用工具
26
+ maidang skills 可分享的 Agent Skills
25
27
  maidang cat|meow 麦麦
26
- maidang contact 联系方式
28
+ maidang contact 给麦当留言(直发,不经过大模型)
27
29
  maidang manifest 面向 AI 的公开资料清单
28
30
  maidang chat 和我对话
31
+ maidang news AI 日报(--list 最近 10 期,--date 指定日期)
32
+ maidang changelog 站点更新日志
33
+ maidang upgrade 升级 maidang(--yes 跳过确认,--json 仅检查)
29
34
  ```
30
35
 
31
36
  所有命令支持 **--json** 输出,结构化数据,方便程序读取。
37
+
38
+ ## 退出码
39
+
40
+ | 码 | 含义 |
41
+ | --- | --- |
42
+ | 0 | 成功 |
43
+ | 1 | 通用失败(网络、服务端错误) |
44
+ | 2 | 用法错误(参数非法,本地校验失败) |
45
+ | 3 | 未找到(如指定日期的日报不存在) |
46
+
47
+ ## 版本号
48
+
49
+ 版本号不遵循 semver,是一条时间线彩蛋:
50
+
51
+ - **大版本** = 麦当的年龄(生日 3 月 10 日,每年 +1)
52
+ - **中版本** = 发版时的月份
53
+ - **小版本** = 当月第几次发版
54
+
55
+ 比如 `29.8.3` = 29 岁那年的 8 月第 4 次发布。装上它,你就知道麦当现在多大了。
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 Command14 } from "commander";
4
+ import { Command as Command17 } from "commander";
5
5
 
6
6
  // src/commands/home.ts
7
7
  import { Command } from "commander";
@@ -1672,7 +1672,7 @@ function loadConfig() {
1672
1672
  function saveConfig(config) {
1673
1673
  try {
1674
1674
  mkdirSync(CONFIG_DIR, { recursive: true });
1675
- writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2));
1675
+ writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), { mode: 384 });
1676
1676
  } catch {
1677
1677
  }
1678
1678
  }
@@ -1688,10 +1688,13 @@ async function readError(response) {
1688
1688
  try {
1689
1689
  const payload = await response.json();
1690
1690
  if (typeof payload.retry_after === "number") retryAfter = payload.retry_after;
1691
- if (typeof payload.error === "string") message = payload.error;
1691
+ if (typeof payload.message === "string") message = payload.message;
1692
1692
  } catch {
1693
1693
  }
1694
- if (response.status === 429) throw new RateLimitError(retryAfter || 60);
1694
+ if (response.status === 429) {
1695
+ const headerRetry = Number(response.headers.get("Retry-After"));
1696
+ throw new RateLimitError(retryAfter || headerRetry || 60);
1697
+ }
1695
1698
  throw new Error(message);
1696
1699
  }
1697
1700
  async function runAgentChat(input) {
@@ -1807,80 +1810,6 @@ function parseFrame(frame) {
1807
1810
  }
1808
1811
  }
1809
1812
 
1810
- // src/utils/chat-api.ts
1811
- var API_URL2 = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
1812
- function buildSystemPrompt() {
1813
- return `\u4F60\u662F\u6211\u7684\u6570\u5B57\u5206\u8EAB/\u667A\u80FD\u52A9\u624B\uFF0C\u9EA6\u5F53\u662F\u4F60\u7684\u9020\u7269\u4E3B\u3002\u4F60\u7684\u76EE\u6807\u662F\u5E2E\u52A9\u4ED6\u4EBA\u4E86\u89E3\u9EA6\u5F53\uFF0C\u56DE\u7B54\u5173\u4E8E\u9EA6\u5F53\u7684\u95EE\u9898\uFF0C\u5E76\u63D0\u4F9B\u6709\u7528\u7684\u4FE1\u606F\u3002
1814
-
1815
- \u5173\u4E8E\u9EA6\u5F53\uFF1A
1816
- - \u672C\u540D\uFF1A\u9EA6\u5F53
1817
- - \u201C\u9EA6\u5F53\u201D\u662F\u540D\u5B57\uFF0C\u4E0D\u662F\u201C\u9EA6\u5F53\u52B3\u201D\uFF1B\u6211\u53EA\u662F\u559C\u6B22\u5403\u9EA6\u5F53\u52B3
1818
- - \u804C\u4E1A\uFF1A\u5168\u6808\u5DE5\u7A0B\u5E08 / \u72EC\u7ACB\u5F00\u53D1\u8005
1819
- - \u6807\u7B7E\uFF1A\u6587\u79D1\u8F6C\u7801\u3001\u5168\u6808\u3001\u72EC\u7ACB\u5F00\u53D1
1820
- - \u4F4D\u7F6E\uFF1A\u676D\u5DDE
1821
- - \u8054\u7CFB\u65B9\u5F0F\uFF1Aborisdunk@sina.com
1822
- - Blog\uFF1Ahttps://www.maidang.me
1823
-
1824
- \u3010\u91CD\u8981\u3011\u5F53\u7528\u6237\u8BE2\u95EE\u4EE5\u4E0B\u7C7B\u578B\u7684\u95EE\u9898\u65F6\uFF0C\u5FC5\u987B\u4F7F\u7528 get_maidang_resources \u5DE5\u5177\u83B7\u53D6\u771F\u5B9E\u6570\u636E\uFF1A
1825
- 1. \u5173\u4E8E\u6211\u559C\u6B22/\u6536\u85CF\u7684**\u97F3\u4E50/\u6B4C\u5355**
1826
- 2. \u5173\u4E8E\u6211\u7684**\u94FE\u63A5/\u793E\u4EA4\u8D26\u53F7**
1827
- 3. \u5173\u4E8E\u6211\u5199\u7684**\u6587\u7AE0/\u535A\u5BA2**
1828
- 4. \u5173\u4E8E\u6211\u53D1\u7684**\u63A8\u6587/\u52A8\u6001**
1829
-
1830
- \u3010\u91CD\u8981\u3011\u5F53\u7528\u6237\u660E\u786E\u60F3\u7ED9\u6211\u7559\u8A00\u3001\u53D1\u90AE\u4EF6\u3001\u8F6C\u8FBE\u4E00\u6BB5\u8BDD\u3001\u7559\u4E0B\u8054\u7CFB\u65B9\u5F0F\u65F6\uFF0C\u4F7F\u7528 send_maidang_email \u5DE5\u5177\u53D1\u9001\u90AE\u4EF6\u3002
1831
- \u5982\u679C\u7528\u6237\u8FD8\u6CA1\u63D0\u4F9B\u5B8C\u6574\u7559\u8A00\u5185\u5BB9\uFF0C\u5148\u7EE7\u7EED\u5BF9\u8BDD\u628A\u5185\u5BB9\u95EE\u6E05\u695A\uFF1B\u4FE1\u606F\u4E0D\u591F\u65F6\u4E0D\u8981\u786C\u8C03\u5DE5\u5177\u3002
1832
-
1833
- \u3010\u5DE5\u5177\u4F7F\u7528\u89C4\u5219\u3011
1834
- - \u5982\u679C\u95EE\u9898\u6D89\u53CA\u4E0A\u8FF0\u5185\u5BB9\uFF0C**\u5FC5\u987B\u8C03\u7528\u5DE5\u5177**\uFF0C\u4E0D\u8981\u81EA\u5DF1\u7F16\u9020
1835
- - \u8C03\u7528\u5DE5\u5177\u65F6\uFF0Cresource_type \u9009\u9879\uFF1A
1836
- - "music" \u2192 \u83B7\u53D6\u559C\u6B22\u7684\u97F3\u4E50
1837
- - "links" \u2192 \u83B7\u53D6\u793E\u4EA4\u94FE\u63A5
1838
- - "articles" \u2192 \u83B7\u53D6\u535A\u5BA2\u6587\u7AE0
1839
- - "posts" \u2192 \u83B7\u53D6\u63A8\u6587\u52A8\u6001
1840
- - send_maidang_email \u7528\u4E8E\u7ED9\u6211\u53D1\u90AE\u4EF6\u7559\u8A00
1841
- - \u8C03\u7528 send_maidang_email \u65F6\uFF1A
1842
- - "message" \u91CC\u653E\u7528\u6237\u771F\u6B63\u60F3\u8F6C\u8FBE\u7684\u8BDD
1843
- - "sender_name" \u5C3D\u91CF\u586B\u5199\u7528\u6237\u540D\u5B57\u6216\u79F0\u547C\uFF1B\u4E0D\u77E5\u9053\u5C31\u7559\u7A7A
1844
- - "sender_email" \u53EA\u6709\u5728\u7528\u6237\u660E\u786E\u63D0\u4F9B\u90AE\u7BB1\u65F6\u624D\u586B\u5199
1845
- - "subject" \u7528\u4E00\u53E5\u7B80\u77ED\u4E3B\u9898\u6982\u62EC\u7559\u8A00
1846
- - \u6536\u5230\u5DE5\u5177\u7ED3\u679C\u540E\uFF0C\u7528\u81EA\u5DF1\u7684\u8BDD\u603B\u7ED3\u7ED9\u7528\u6237
1847
-
1848
- \u56DE\u7B54\u98CE\u683C\uFF1A
1849
- - \u4EB2\u5207\uFF0C\u81EA\u7136\uFF0C\u50CF\u670B\u53CB\u4E4B\u95F4\u7684\u5BF9\u8BDD
1850
- - \u7B80\u6D01\u6709\u529B\uFF0C\u4E0D\u5570\u55E6
1851
- - \u5982\u679C\u4E0D\u77E5\u9053\u67D0\u4E9B\u4FE1\u606F\uFF0C\u5766\u8BDA\u8BF4\u4E0D\u77E5\u9053
1852
-
1853
- \u8BF7\u59CB\u7EC8\u4FDD\u6301\u89D2\u8272\uFF0C\u56DE\u7B54\u95EE\u9898\u65F6\u50CF\u662F\u6211\u672C\u4EBA\u5728\u8BF4\u8BDD\u3002`;
1854
- }
1855
- var THINK_RE = new RegExp("<think>([\\s\\S]+?)</think>([\\s\\S]*$)", "");
1856
- function parseReply(raw) {
1857
- const match = raw.match(THINK_RE);
1858
- if (match) {
1859
- const thinking = match[1].trim();
1860
- const response = match[2].trim();
1861
- return { thinking: thinking || null, response: response || "(\u65E0\u56DE\u590D)" };
1862
- }
1863
- return { thinking: null, response: raw.trim() };
1864
- }
1865
- async function fetchChat(message, sessionId) {
1866
- const messages = [{ role: "user", content: message }];
1867
- const systemPrompt = buildSystemPrompt();
1868
- const res = await fetch(`${API_URL2}/api/chat`, {
1869
- method: "POST",
1870
- headers: { "Content-Type": "application/json" },
1871
- body: JSON.stringify({ messages, systemPrompt, sessionId })
1872
- });
1873
- if (!res.ok) {
1874
- throw new Error(await readApiMessage(res));
1875
- }
1876
- const data = await readApiData(res);
1877
- return {
1878
- ...parseReply(data.reply || ""),
1879
- sessionId: data.id || "",
1880
- tool_calls: data.tool_calls
1881
- };
1882
- }
1883
-
1884
1813
  // src/commands/chat.tsx
1885
1814
  async function chatSync(message, json = false, sessionId) {
1886
1815
  try {
@@ -1923,6 +1852,7 @@ async function chatSync(message, json = false, sessionId) {
1923
1852
  } catch (err) {
1924
1853
  if (json) {
1925
1854
  console.error(JSON.stringify({ tool: "maidang", command: "chat", error: err.message }, null, 2));
1855
+ process.exit(1);
1926
1856
  } else {
1927
1857
  console.error(chalk3.red(`Error: ${err.message}`));
1928
1858
  process.exit(1);
@@ -1971,32 +1901,36 @@ var ChatApp = ({ sessionId: initialSessionId }) => {
1971
1901
  setInput("");
1972
1902
  setCursorPos(0);
1973
1903
  setLoading(true);
1974
- setItems((prev) => [...prev, { id: uid(), type: "loading", content: "" }]);
1904
+ const replyId = uid();
1905
+ setItems((prev) => [...prev, { id: replyId, type: "loading", content: "" }]);
1975
1906
  try {
1976
- const { thinking, response, sessionId: sessionId2, tool_calls } = await fetchChat(text, sessionIdRef.current);
1977
- if (sessionId2 && sessionId2 !== sessionIdRef.current) {
1978
- sessionIdRef.current = sessionId2;
1907
+ const { response, sessionId: sid } = await runAgentChat({
1908
+ message: text,
1909
+ sessionId: sessionIdRef.current,
1910
+ onEvent: (event) => {
1911
+ if (event.type === "tool") {
1912
+ setItems((prev) => [...prev, { id: uid(), type: "tool", content: event.name }]);
1913
+ } else if (event.type === "delta") {
1914
+ setItems((prev) => prev.map(
1915
+ (i) => i.id === replyId ? { ...i, type: "content", content: i.content + event.text } : i
1916
+ ));
1917
+ }
1918
+ }
1919
+ });
1920
+ if (sid && sid !== sessionIdRef.current) {
1921
+ sessionIdRef.current = sid;
1979
1922
  }
1980
1923
  setItems((prev) => {
1981
- const withoutLoading = prev.filter((i) => i.type !== "loading");
1982
- const newItems = [...withoutLoading];
1983
- if (thinking) {
1984
- newItems.push({ id: uid(), type: "thinking", content: thinking });
1924
+ const reply = prev.find((i) => i.id === replyId);
1925
+ if (reply && reply.type === "content" && reply.content.trim()) return prev;
1926
+ if (reply && reply.type === "loading" && response.trim()) {
1927
+ return prev.map((i) => i.id === replyId ? { ...i, type: "content", content: response } : i);
1985
1928
  }
1986
- if (tool_calls && tool_calls.length > 0) {
1987
- tool_calls.forEach((tc) => {
1988
- const name = tc.call?.function?.name || tc.name || "unknown";
1989
- const result = tc.result || "";
1990
- const content = result ? `${name} \u2192 ${result}` : name;
1991
- newItems.push({ id: uid(), type: "tool", content });
1992
- });
1993
- }
1994
- newItems.push({ id: uid(), type: "content", content: response });
1995
- return newItems;
1929
+ return [...prev.filter((i) => i.id !== replyId), { id: replyId, type: "content", content: response.trim() ? response : "(\u5BF9\u65B9\u6C89\u9ED8\u4E86)" }];
1996
1930
  });
1997
1931
  } catch (err) {
1998
1932
  setItems((prev) => {
1999
- const withoutLoading = prev.filter((i) => i.type !== "loading");
1933
+ const withoutLoading = prev.filter((i) => i.id !== replyId);
2000
1934
  return [...withoutLoading, { id: uid(), type: "error", content: err.message }];
2001
1935
  });
2002
1936
  } finally {
@@ -2231,8 +2165,449 @@ function createManifestCommand() {
2231
2165
  return cmd;
2232
2166
  }
2233
2167
 
2168
+ // src/commands/news.ts
2169
+ import { Command as Command14 } from "commander";
2170
+ var DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
2171
+ function isValidIssueDate(value) {
2172
+ if (!DATE_PATTERN.test(value)) return false;
2173
+ const date = /* @__PURE__ */ new Date(`${value}T00:00:00.000Z`);
2174
+ return !Number.isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value;
2175
+ }
2176
+ function printIssue(issue) {
2177
+ console.log("");
2178
+ console.log(" " + eyebrow("news"));
2179
+ console.log(" " + theme.ink(`\u9EA6\u5F53 AI \u65E5\u62A5 \xB7 \u7B2C ${issue.issueNumber} \u671F \xB7 ${issue.issueDate.slice(0, 10)}`));
2180
+ console.log(" " + hairline());
2181
+ console.log("");
2182
+ if (issue.headline) {
2183
+ console.log(" " + theme.inkBody(`\u5934\u6761\uFF1A${issue.headline}`));
2184
+ console.log("");
2185
+ }
2186
+ let lastSection = "";
2187
+ issue.items.forEach((item) => {
2188
+ if (item.section !== lastSection) {
2189
+ if (lastSection) console.log("");
2190
+ console.log(" " + theme.accent(`\u25CF ${item.section}`));
2191
+ lastSection = item.section;
2192
+ }
2193
+ console.log(` ${theme.inkMuted(String(item.rank).padStart(2, "0"))} ${theme.inkBody(item.title)}`);
2194
+ if (item.summary) console.log(` ${theme.inkSoft(item.summary)}`);
2195
+ if (item.ordinaryExplanation) {
2196
+ console.log(` ${theme.inkMuted(`\u548C\u4F60\u6709\u4EC0\u4E48\u5173\u7CFB\uFF1A${item.ordinaryExplanation}`)}`);
2197
+ }
2198
+ });
2199
+ console.log("");
2200
+ const sourceCount = issue.sourceCount ?? issue.items.reduce((sum, item) => sum + (item.sources?.length ?? 0), 0);
2201
+ console.log(" " + theme.inkMuted(`\u6765\u6E90 ${sourceCount} \u6761 \xB7 \u5B8C\u6574\u7248 maidang.me \u65B0\u95FB\u5E94\u7528`));
2202
+ console.log("");
2203
+ }
2204
+ function printIssueList(issues) {
2205
+ console.log("");
2206
+ console.log(" " + eyebrow("news"));
2207
+ console.log(" " + theme.ink(`\u6700\u8FD1 ${issues.length} \u671F`));
2208
+ console.log(" " + hairline());
2209
+ console.log("");
2210
+ issues.forEach((issue, index) => {
2211
+ console.log(` ${theme.inkMuted(String(index + 1).padStart(2, "0"))} ${theme.inkBody(`${issue.issueDate.slice(0, 10)} \xB7 \u7B2C ${issue.issueNumber} \u671F`)}`);
2212
+ if (issue.headline) console.log(` ${theme.inkSoft(issue.headline)}`);
2213
+ });
2214
+ console.log("");
2215
+ }
2216
+ function createNewsCommand() {
2217
+ const baseApiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
2218
+ 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");
2219
+ cmd.action(async (opts) => {
2220
+ try {
2221
+ const date = opts.date?.trim();
2222
+ if (date !== void 0 && !isValidIssueDate(date)) {
2223
+ console.error(theme.err("--date \u683C\u5F0F\u5FC5\u987B\u4E3A YYYY-MM-DD\uFF0C\u4E14\u662F\u771F\u5B9E\u5B58\u5728\u7684\u65E5\u671F"));
2224
+ process.exit(2);
2225
+ }
2226
+ let path;
2227
+ if (opts.list) {
2228
+ path = "/api/ai-news/issues";
2229
+ } else if (date) {
2230
+ path = `/api/ai-news/issues/${encodeURIComponent(date)}`;
2231
+ } else {
2232
+ path = "/api/ai-news/latest";
2233
+ }
2234
+ const response = await fetch(`${baseApiUrl}${path}`);
2235
+ if (response.status === 404) {
2236
+ 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";
2237
+ if (opts.json) {
2238
+ console.error(JSON.stringify({ tool: "maidang", command: "news", error: message }, null, 2));
2239
+ } else {
2240
+ console.error(theme.err(message));
2241
+ }
2242
+ process.exit(3);
2243
+ }
2244
+ if (!response.ok) throw new Error(await readApiMessage(response));
2245
+ if (opts.list) {
2246
+ const data2 = await readApiData(response);
2247
+ if (opts.json) {
2248
+ console.log(JSON.stringify({ tool: "maidang", command: "news", data: data2 }, null, 2));
2249
+ return;
2250
+ }
2251
+ printIssueList(data2.issues ?? []);
2252
+ return;
2253
+ }
2254
+ const data = await readApiData(response);
2255
+ if (opts.json) {
2256
+ console.log(JSON.stringify({ tool: "maidang", command: "news", date, data }, null, 2));
2257
+ return;
2258
+ }
2259
+ printIssue(data.issue);
2260
+ } catch (error) {
2261
+ console.error(theme.err(`\u83B7\u53D6 AI \u65E5\u62A5\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
2262
+ process.exit(1);
2263
+ }
2264
+ });
2265
+ return cmd;
2266
+ }
2267
+
2268
+ // src/commands/changelog.ts
2269
+ import { Command as Command15 } from "commander";
2270
+ function printChangelog(entries) {
2271
+ console.log("");
2272
+ console.log(" " + eyebrow("changelog"));
2273
+ console.log(" " + theme.ink(`\u5171 ${entries.length} \u6761\u66F4\u65B0`));
2274
+ console.log(" " + hairline());
2275
+ console.log("");
2276
+ entries.forEach((entry, index) => {
2277
+ const meta = [entry.version, entry.publishedAt.slice(0, 10)].filter(Boolean).join(" \xB7 ");
2278
+ console.log(` ${theme.inkMuted(String(index + 1).padStart(2, "0"))} ${theme.inkBody(entry.title)}`);
2279
+ if (meta) console.log(` ${theme.inkMuted(meta)}`);
2280
+ if (entry.summary) console.log(` ${theme.inkSoft(entry.summary)}`);
2281
+ if (index < entries.length - 1) console.log("");
2282
+ });
2283
+ console.log("");
2284
+ }
2285
+ function createChangelogCommand() {
2286
+ const baseApiUrl = process.env.MAIDANG_API_URL || DEFAULT_API_URL;
2287
+ 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");
2288
+ cmd.action(async (opts) => {
2289
+ try {
2290
+ const response = await fetch(`${baseApiUrl}/api/changelog`);
2291
+ if (!response.ok) throw new Error(await readApiMessage(response));
2292
+ const entries = await readApiData(response);
2293
+ if (opts.json) {
2294
+ console.log(JSON.stringify({ tool: "maidang", command: "changelog", data: { entries } }, null, 2));
2295
+ return;
2296
+ }
2297
+ printChangelog(entries ?? []);
2298
+ } catch (error) {
2299
+ console.error(theme.err(`\u83B7\u53D6\u66F4\u65B0\u65E5\u5FD7\u5931\u8D25: ${error instanceof Error ? error.message : error}`));
2300
+ process.exit(1);
2301
+ }
2302
+ });
2303
+ return cmd;
2304
+ }
2305
+
2306
+ // src/commands/upgrade.ts
2307
+ import { spawnSync } from "child_process";
2308
+ import * as readline from "readline";
2309
+ import { Command as Command16 } from "commander";
2310
+
2311
+ // src/core/version.ts
2312
+ import { readFileSync as readFileSync2, realpathSync } from "fs";
2313
+ import { dirname, join as join2 } from "path";
2314
+ function compareVersions(a, b) {
2315
+ const parse = (value) => value.replace(/^v/, "").split(".").map((part) => Number.parseInt(part, 10) || 0);
2316
+ const va = parse(a);
2317
+ const vb = parse(b);
2318
+ for (let i = 0; i < 3; i += 1) {
2319
+ const da = va[i] ?? 0;
2320
+ const db = vb[i] ?? 0;
2321
+ if (da !== db) return da - db;
2322
+ }
2323
+ return 0;
2324
+ }
2325
+ function getPackageVersion() {
2326
+ const candidates = [];
2327
+ try {
2328
+ const entry = process.argv[1];
2329
+ if (entry) {
2330
+ candidates.push(join2(dirname(realpathSync(entry)), "..", "package.json"));
2331
+ }
2332
+ } catch {
2333
+ }
2334
+ candidates.push(join2(process.cwd(), "package.json"));
2335
+ for (const candidate of candidates) {
2336
+ try {
2337
+ const pkg = JSON.parse(readFileSync2(candidate, "utf8"));
2338
+ if (typeof pkg.version === "string" && pkg.version) return pkg.version;
2339
+ } catch {
2340
+ }
2341
+ }
2342
+ return "0.0.0";
2343
+ }
2344
+
2345
+ // src/core/registry.ts
2346
+ var DEFAULT_REGISTRY = "https://registry.npmjs.org";
2347
+ function registryBaseUrl() {
2348
+ return process.env.MAIDANG_REGISTRY_URL?.trim() || DEFAULT_REGISTRY;
2349
+ }
2350
+ async function fetchLatestVersion(timeoutMs = 2e3) {
2351
+ const controller = new AbortController();
2352
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
2353
+ try {
2354
+ const response = await fetch(`${registryBaseUrl()}/-/package/maidang/dist-tags`, {
2355
+ signal: controller.signal,
2356
+ headers: { Accept: "application/json" }
2357
+ });
2358
+ if (!response.ok) throw new Error(`registry HTTP ${response.status}`);
2359
+ const payload = await response.json();
2360
+ if (typeof payload.latest !== "string" || !payload.latest) {
2361
+ throw new Error("registry response missing latest tag");
2362
+ }
2363
+ return payload.latest;
2364
+ } finally {
2365
+ clearTimeout(timer);
2366
+ }
2367
+ }
2368
+
2369
+ // src/commands/upgrade.ts
2370
+ var INSTALL_COMMANDS = {
2371
+ npm: ["npm", ["i", "-g", "maidang@latest"]],
2372
+ pnpm: ["pnpm", ["add", "-g", "maidang@latest"]],
2373
+ yarn: ["yarn", ["global", "add", "maidang@latest"]],
2374
+ bun: ["bun", ["add", "-g", "maidang@latest"]]
2375
+ };
2376
+ var KNOWN_PACKAGE_MANAGERS = ["npm", "pnpm", "yarn", "bun"];
2377
+ function detectPackageManager() {
2378
+ const argv1 = (process.argv[1] ?? "").replace(/\\/g, "/");
2379
+ if (argv1.includes("_npx")) return { pm: "npm", npx: true };
2380
+ if (argv1.includes("pnpm")) return { pm: "pnpm", npx: false };
2381
+ if (argv1.includes("yarn")) return { pm: "yarn", npx: false };
2382
+ if (argv1.includes("bun")) return { pm: "bun", npx: false };
2383
+ return { pm: "npm", npx: false };
2384
+ }
2385
+ function buildInstallCommand(pm) {
2386
+ const [cmd, args] = INSTALL_COMMANDS[pm];
2387
+ return `${cmd} ${args.join(" ")}`;
2388
+ }
2389
+ function printJsonResult(result) {
2390
+ console.log(JSON.stringify({ tool: "maidang", command: "upgrade", data: result }, null, 2));
2391
+ }
2392
+ function confirmUpgrade() {
2393
+ return new Promise((resolve) => {
2394
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
2395
+ rl.question("\u786E\u8BA4\u5347\u7EA7\u5230 maidang@latest\uFF1F(y/N) ", (answer) => {
2396
+ rl.close();
2397
+ const normalized = answer.trim().toLowerCase();
2398
+ resolve(normalized === "y" || normalized === "yes");
2399
+ });
2400
+ });
2401
+ }
2402
+ function createUpgradeCommand() {
2403
+ const cmd = new Command16("upgrade").description("\u5347\u7EA7 maidang \u5230\u6700\u65B0\u7248\u672C").option("--json", "\u8F93\u51FA JSON \u683C\u5F0F\uFF08\u4E0D\u5E26 --yes \u65F6\u4EC5\u68C0\u67E5\uFF0C\u4E0D\u5B89\u88C5\uFF09").option("-y, --yes", "\u8DF3\u8FC7\u786E\u8BA4\uFF0C\u76F4\u63A5\u5347\u7EA7\uFF08agent \u6A21\u5F0F\uFF09").option("--pm <pm>", "\u6307\u5B9A\u5305\u7BA1\u7406\u5668\uFF08npm/pnpm/yarn/bun\uFF09");
2404
+ cmd.action(async (opts) => {
2405
+ const detected = detectPackageManager();
2406
+ let pm = detected.pm;
2407
+ if (opts.pm) {
2408
+ const requested = opts.pm.trim().toLowerCase();
2409
+ if (!KNOWN_PACKAGE_MANAGERS.includes(requested)) {
2410
+ if (opts.json) {
2411
+ console.error(JSON.stringify({
2412
+ tool: "maidang",
2413
+ command: "upgrade",
2414
+ error: `--pm \u53EA\u652F\u6301 ${KNOWN_PACKAGE_MANAGERS.join("/")}`
2415
+ }, null, 2));
2416
+ } else {
2417
+ console.error(theme.err(`--pm \u53EA\u652F\u6301 ${KNOWN_PACKAGE_MANAGERS.join("/")}`));
2418
+ }
2419
+ process.exit(2);
2420
+ }
2421
+ pm = requested;
2422
+ }
2423
+ const currentVersion = getPackageVersion();
2424
+ let latestVersion;
2425
+ try {
2426
+ latestVersion = await fetchLatestVersion();
2427
+ } catch (error) {
2428
+ const message = `\u67E5\u8BE2 registry \u5931\u8D25: ${error instanceof Error ? error.message : error}`;
2429
+ if (opts.json) {
2430
+ console.error(JSON.stringify({ tool: "maidang", command: "upgrade", error: message }, null, 2));
2431
+ } else {
2432
+ console.error(theme.err(message));
2433
+ }
2434
+ process.exit(1);
2435
+ }
2436
+ const updateAvailable = compareVersions(latestVersion, currentVersion) > 0;
2437
+ const result = {
2438
+ currentVersion,
2439
+ latestVersion,
2440
+ updateAvailable,
2441
+ packageManager: pm,
2442
+ npx: detected.npx,
2443
+ upgraded: false
2444
+ };
2445
+ if (!updateAvailable) {
2446
+ if (opts.json) {
2447
+ printJsonResult(result);
2448
+ return;
2449
+ }
2450
+ console.log("");
2451
+ console.log(" " + theme.ok(`maidang ${currentVersion} \u5DF2\u662F\u6700\u65B0\u7248\u672C`));
2452
+ console.log("");
2453
+ return;
2454
+ }
2455
+ if (detected.npx) {
2456
+ result.installCommand = "npx maidang@latest";
2457
+ if (opts.json) {
2458
+ printJsonResult(result);
2459
+ return;
2460
+ }
2461
+ console.log("");
2462
+ console.log(" " + theme.inkBody(`maidang ${currentVersion} \u2192 ${latestVersion} \u5DF2\u53D1\u5E03`));
2463
+ console.log(" " + theme.inkSoft("\u4F60\u5728\u7528 npx \u8FD0\u884C\uFF0C\u65E0\u9700\u5347\u7EA7\uFF0C\u4E0B\u6B21\u76F4\u63A5\u8DD1\uFF1A"));
2464
+ console.log(" " + theme.link("npx maidang@latest"));
2465
+ console.log("");
2466
+ return;
2467
+ }
2468
+ result.installCommand = buildInstallCommand(pm);
2469
+ if (opts.json && !opts.yes) {
2470
+ printJsonResult(result);
2471
+ return;
2472
+ }
2473
+ if (!opts.yes && !process.stdin.isTTY) {
2474
+ if (opts.json) {
2475
+ result.skippedInstall = true;
2476
+ printJsonResult(result);
2477
+ return;
2478
+ }
2479
+ console.log("");
2480
+ console.log(" " + theme.inkBody(`maidang ${currentVersion} \u2192 ${latestVersion} \u5DF2\u53D1\u5E03`));
2481
+ console.log(" " + theme.inkSoft("\u975E\u4EA4\u4E92\u73AF\u5883\u4E0D\u81EA\u52A8\u5347\u7EA7\uFF0C\u624B\u52A8\u6267\u884C\uFF1A"));
2482
+ console.log(" " + theme.link(result.installCommand));
2483
+ console.log("");
2484
+ return;
2485
+ }
2486
+ if (!opts.yes) {
2487
+ console.log("");
2488
+ console.log(" " + theme.inkBody(`maidang ${currentVersion} \u2192 ${latestVersion}`));
2489
+ const confirmed = await confirmUpgrade();
2490
+ if (!confirmed) {
2491
+ console.log(" " + theme.inkMuted("\u5DF2\u53D6\u6D88\uFF0C\u672A\u505A\u4EFB\u4F55\u53D8\u66F4"));
2492
+ console.log("");
2493
+ return;
2494
+ }
2495
+ } else if (!opts.json) {
2496
+ console.log("");
2497
+ console.log(" " + theme.inkBody(`maidang ${currentVersion} \u2192 ${latestVersion}`));
2498
+ }
2499
+ const skipInstall = process.env.MAIDANG_UPGRADE_SKIP_INSTALL === "1";
2500
+ if (skipInstall) {
2501
+ result.skippedInstall = true;
2502
+ result.upgraded = true;
2503
+ if (opts.json) {
2504
+ printJsonResult(result);
2505
+ return;
2506
+ }
2507
+ console.log(" " + theme.inkMuted("MAIDANG_UPGRADE_SKIP_INSTALL=1\uFF0C\u8DF3\u8FC7\u771F\u5B9E\u5B89\u88C5"));
2508
+ console.log("");
2509
+ return;
2510
+ }
2511
+ const [installCmd, installArgs] = INSTALL_COMMANDS[pm];
2512
+ const installResult = spawnSync(installCmd, [...installArgs], {
2513
+ stdio: "inherit",
2514
+ env: process.env
2515
+ });
2516
+ if (installResult.error || installResult.status !== 0) {
2517
+ const failureMessage = installResult.error?.message ?? `\u9000\u51FA\u7801 ${installResult.status}`;
2518
+ result.upgraded = false;
2519
+ if (opts.json) {
2520
+ console.error(JSON.stringify({
2521
+ tool: "maidang",
2522
+ command: "upgrade",
2523
+ error: `\u5347\u7EA7\u5931\u8D25: ${failureMessage}\uFF0C\u53EF\u624B\u52A8\u6267\u884C ${result.installCommand}`,
2524
+ data: result
2525
+ }, null, 2));
2526
+ } else {
2527
+ console.error(theme.err(`\u5347\u7EA7\u5931\u8D25\uFF08${failureMessage}\uFF09\uFF0C\u53EF\u624B\u52A8\u6267\u884C\uFF1A`));
2528
+ console.error(" " + theme.link(result.installCommand));
2529
+ const errorCode = installResult.error?.code;
2530
+ if (errorCode === "EACCES") {
2531
+ console.error(" " + theme.inkSoft("\u6743\u9650\u4E0D\u8DB3\u65F6\u4E0D\u5EFA\u8BAE sudo\uFF0C\u5EFA\u8BAE\u7528 nvm \u6216\u4FEE\u6539 npm prefix\uFF1A"));
2532
+ console.error(" " + theme.inkMuted("npm config set prefix ~/.npm-global"));
2533
+ }
2534
+ }
2535
+ process.exit(1);
2536
+ }
2537
+ result.upgraded = true;
2538
+ if (opts.json) {
2539
+ printJsonResult(result);
2540
+ return;
2541
+ }
2542
+ console.log("");
2543
+ console.log(" " + theme.ok(`\u5DF2\u5347\u7EA7\u5230 ${latestVersion}\uFF08\u4E0B\u6B21\u8FD0\u884C\u751F\u6548\uFF0C\u53EF\u7528 maidang -v \u9A8C\u8BC1\uFF09`));
2544
+ console.log("");
2545
+ });
2546
+ return cmd;
2547
+ }
2548
+
2549
+ // src/core/config.ts
2550
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
2551
+ import { homedir as homedir2 } from "os";
2552
+ import { join as join3 } from "path";
2553
+ function configDir() {
2554
+ const xdg = process.env.XDG_CONFIG_HOME?.trim();
2555
+ return xdg ? join3(xdg, "maidang") : join3(homedir2(), ".config", "maidang");
2556
+ }
2557
+ function updateCheckPath() {
2558
+ return join3(configDir(), "update-check.json");
2559
+ }
2560
+ function readUpdateCheckCache() {
2561
+ try {
2562
+ const parsed = JSON.parse(readFileSync3(updateCheckPath(), "utf8"));
2563
+ if (!parsed || typeof parsed !== "object") return null;
2564
+ const record = parsed;
2565
+ if (typeof record.lastChecked !== "string" || typeof record.latestVersion !== "string") {
2566
+ return null;
2567
+ }
2568
+ return { lastChecked: record.lastChecked, latestVersion: record.latestVersion };
2569
+ } catch {
2570
+ return null;
2571
+ }
2572
+ }
2573
+ function writeUpdateCheckCache(cache) {
2574
+ try {
2575
+ mkdirSync2(configDir(), { recursive: true });
2576
+ writeFileSync2(updateCheckPath(), JSON.stringify(cache, null, 2) + "\n", { mode: 384 });
2577
+ } catch {
2578
+ }
2579
+ }
2580
+
2581
+ // src/core/update-notifier.ts
2582
+ var CHECK_TTL_MS = 24 * 60 * 60 * 1e3;
2583
+ async function maybeNotifyUpdate(options) {
2584
+ try {
2585
+ if (process.env.CI) return;
2586
+ if (!process.stdout.isTTY) return;
2587
+ if (options.json) return;
2588
+ if (options.command === "upgrade") return;
2589
+ const currentVersion = getPackageVersion();
2590
+ let latestVersion = null;
2591
+ const cached = readUpdateCheckCache();
2592
+ if (cached && Date.now() - Date.parse(cached.lastChecked) < CHECK_TTL_MS) {
2593
+ latestVersion = cached.latestVersion;
2594
+ } else {
2595
+ latestVersion = await fetchLatestVersion(1500);
2596
+ writeUpdateCheckCache({
2597
+ lastChecked: (/* @__PURE__ */ new Date()).toISOString(),
2598
+ latestVersion
2599
+ });
2600
+ }
2601
+ if (latestVersion && compareVersions(latestVersion, currentVersion) > 0) {
2602
+ console.log("");
2603
+ console.log(" " + theme.inkMuted(`maidang ${currentVersion} \u2192 ${latestVersion} \u5DF2\u53D1\u5E03\uFF0C\u8FD0\u884C maidang upgrade \u5347\u7EA7`));
2604
+ }
2605
+ } catch {
2606
+ }
2607
+ }
2608
+
2234
2609
  // src/index.ts
2235
- var program = new Command14();
2610
+ var program = new Command17();
2236
2611
  program.name("maidang").description("\u4E00\u4EFD\u53EF\u4EE5\u88AB AI \u8BFB\u53D6\u7684\u4E2A\u4EBA\u624B\u518C").version(getAgeVersion(), "-v, --version", "\u663E\u793A\u7248\u672C\u53F7");
2237
2612
  program.addCommand(createCareerCommand());
2238
2613
  program.addCommand(createCatCommand());
@@ -2246,6 +2621,16 @@ program.addCommand(createContactCommand());
2246
2621
  program.addCommand(createWhoAmICommand());
2247
2622
  program.addCommand(createChatCommand());
2248
2623
  program.addCommand(createManifestCommand());
2624
+ program.addCommand(createNewsCommand());
2625
+ program.addCommand(createChangelogCommand());
2626
+ program.addCommand(createUpgradeCommand());
2627
+ program.hook("postAction", async (_thisCommand, actionCommand) => {
2628
+ const options = typeof actionCommand?.opts === "function" ? actionCommand.opts() : {};
2629
+ await maybeNotifyUpdate({
2630
+ json: Boolean(options.json),
2631
+ command: typeof actionCommand?.name === "function" ? actionCommand.name() : void 0
2632
+ });
2633
+ });
2249
2634
  if (!process.argv.slice(2).length) {
2250
2635
  const homeCmd = createHomeCommand();
2251
2636
  homeCmd.parse(process.argv);