@ai-zen/agents-sdk 1.2.0-alpha.0 → 1.2.0-alpha.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.
@@ -1,4 +1,11 @@
1
1
  import type { AppConfig, AgentDefinition, ReasoningPresets } from "../types/index.js";
2
+ /**
3
+ * 配置坏了、已经按出厂恢复之后的回调(参数是坏掉的那个文件路径)。
4
+ *
5
+ * 恢复本身在读的那一刻就做完了(写回出厂配置 + 返回出厂配置);这个回调只负责让上层
6
+ * 能告诉用户「你的配置被重置过一次」。
7
+ */
8
+ export type ConfigRecoveredHandler = (path: string) => void | Promise<void>;
2
9
  /**
3
10
  * 配置管理器 — 负责配置文件的读写、目录初始化、默认实体创建。
4
11
  */
@@ -7,16 +14,28 @@ export declare class ConfigManager {
7
14
  readonly basePath: string;
8
15
  /** 当前配置里的模型 id 集合(bootstrap 时填充,供默认 Agent 校验 modelId) */
9
16
  private knownModelIds;
10
- constructor(configPath: string);
17
+ /** 配置坏了被恢复出厂之后要通知的那一下(不注入就只是恢复,不告诉任何人) */
18
+ private readonly onRecovered?;
19
+ constructor(configPath: string, onRecovered?: ConfigRecoveredHandler);
11
20
  /**
12
- * 读取配置。文件不存在时返回出厂默认配置。
21
+ * 读取配置。文件不存在时返回出厂默认配置;文件坏了(解析不了 / 不是对象)时
22
+ * 恢复出厂并写回磁盘,也返回出厂默认配置(见 `readRaw`)。
13
23
  *
14
24
  * 读到即**迁移**(`config/migrate.ts`):上层拿到的永远是当前字段形状,
15
25
  * 旧字段(`maxContextTokens` / `maxContextChars`)不会漏到调用方。
16
26
  * 迁移结果不在这里落盘——落盘只在 `ensureDefaultConfig()` 做一次。
17
27
  */
18
28
  read(): Promise<AppConfig>;
19
- /** 读磁盘原文(不做任何迁移/同步),供 `ensureDefaultConfig()` 对比写盘用 */
29
+ /**
30
+ * 读磁盘原文(不做任何迁移/同步),供 `ensureDefaultConfig()` 对比写盘用。
31
+ *
32
+ * 文件在但读不成配置(JSON 解析不了、或者解析出来不是对象)就是一档「坏」:先把坏文件
33
+ * 改名留档,再用出厂配置写回磁盘,再回调一次,返回出厂配置。写回是为了让下次启动不再
34
+ * 遇到同一个坏文件 —— 只返回默认值不落盘,用户每次开客户端都会再撞一次。
35
+ *
36
+ * 留档不是为了兜底,是为了数据:`config.json` 一恢复,用户的端点与密钥就全没了,
37
+ * 残骸是唯一能查回「我原来配了什么」的地方。
38
+ */
20
39
  private readRaw;
21
40
  write(config: AppConfig): Promise<void>;
22
41
  /**
@@ -104,23 +123,25 @@ export declare class ConfigManager {
104
123
  /** mcp.json 路径:{basePath}/mcp.json */
105
124
  get mcpPath(): string;
106
125
  /**
107
- * 确保 basePath/mcp.json 存在。不存在时写入出厂默认 MCP 配置(含 socket-pty 与 chrome-devtools)。
108
- * 已有文件不被覆盖,尊重用户已配置的 MCP 服务器。
109
- * @returns 返回当前生效的 MCP 配置原始内容
126
+ * 确保 basePath/mcp.json 存在,三种情况分开(与 `config.json` / `host.json` 同一口径):
127
+ *
128
+ * - **不存在** → 写入出厂默认 MCP 配置(含 socket-pty 与 chrome-devtools)。这是正常首启,
129
+ * 不回调;
130
+ * - **存在且解析得成对象** → 原样返回,不动用户的文件(哪怕里面全是用户自己的服务器);
131
+ * - **存在但坏了**(读不了 / 不是 JSON / 不是对象 / 是数组)→ 恢复出厂:先把坏文件改名
132
+ * 留档为 `mcp.json.broken`,再写回出厂默认配置,然后回调一次。
133
+ *
134
+ * @returns 当前生效的 MCP 配置原始内容
110
135
  */
111
136
  ensureDefaultMcpConfig(): Promise<Record<string, unknown>>;
112
137
  /**
113
- * 读取 basePath/mcp.json,不存在时返回空结构。
114
- */
115
- readMcpConfig(): Promise<{
116
- mcpServers: Record<string, unknown>;
117
- }>;
118
- /**
119
- * 原子写入 mcp.json。
138
+ * 把坏掉的配置文件改名留档为 `<文件名>.broken`(`config.json` 与 `mcp.json` 同一口径)。
139
+ *
140
+ * 同名残骸已经在就带上时间戳(同一毫秒再撞上就补序号),免得把上一份残骸覆盖掉。
120
141
  */
121
- writeMcpConfig(config: {
122
- mcpServers: Record<string, unknown>;
123
- }): Promise<void>;
142
+ private archiveBroken;
143
+ /** 这个路径在不在(`access` 不抛就是在) */
144
+ private exists;
124
145
  /** 原子写 JSON 文件(先写 .tmp 再 rename,避免半成品)。 */
125
146
  private writeJson;
126
147
  }
@@ -12,15 +12,19 @@ export class ConfigManager {
12
12
  basePath;
13
13
  /** 当前配置里的模型 id 集合(bootstrap 时填充,供默认 Agent 校验 modelId) */
14
14
  knownModelIds = new Set();
15
- constructor(configPath) {
15
+ /** 配置坏了被恢复出厂之后要通知的那一下(不注入就只是恢复,不告诉任何人) */
16
+ onRecovered;
17
+ constructor(configPath, onRecovered) {
16
18
  this.configPath = configPath;
17
19
  this.basePath = dirname(configPath);
20
+ this.onRecovered = onRecovered;
18
21
  }
19
22
  // -----------------------------------------------------------------------
20
23
  // config.json
21
24
  // -----------------------------------------------------------------------
22
25
  /**
23
- * 读取配置。文件不存在时返回出厂默认配置。
26
+ * 读取配置。文件不存在时返回出厂默认配置;文件坏了(解析不了 / 不是对象)时
27
+ * 恢复出厂并写回磁盘,也返回出厂默认配置(见 `readRaw`)。
24
28
  *
25
29
  * 读到即**迁移**(`config/migrate.ts`):上层拿到的永远是当前字段形状,
26
30
  * 旧字段(`maxContextTokens` / `maxContextChars`)不会漏到调用方。
@@ -30,7 +34,16 @@ export class ConfigManager {
30
34
  const raw = await this.readRaw();
31
35
  return raw ? migrateConfig(raw).config : { ...DEFAULT_APP_CONFIG };
32
36
  }
33
- /** 读磁盘原文(不做任何迁移/同步),供 `ensureDefaultConfig()` 对比写盘用 */
37
+ /**
38
+ * 读磁盘原文(不做任何迁移/同步),供 `ensureDefaultConfig()` 对比写盘用。
39
+ *
40
+ * 文件在但读不成配置(JSON 解析不了、或者解析出来不是对象)就是一档「坏」:先把坏文件
41
+ * 改名留档,再用出厂配置写回磁盘,再回调一次,返回出厂配置。写回是为了让下次启动不再
42
+ * 遇到同一个坏文件 —— 只返回默认值不落盘,用户每次开客户端都会再撞一次。
43
+ *
44
+ * 留档不是为了兜底,是为了数据:`config.json` 一恢复,用户的端点与密钥就全没了,
45
+ * 残骸是唯一能查回「我原来配了什么」的地方。
46
+ */
34
47
  async readRaw() {
35
48
  try {
36
49
  await fs.access(this.configPath);
@@ -39,7 +52,21 @@ export class ConfigManager {
39
52
  return null;
40
53
  }
41
54
  const raw = await fs.readFile(this.configPath, "utf-8");
42
- return JSON.parse(raw);
55
+ let parsed;
56
+ try {
57
+ parsed = JSON.parse(raw);
58
+ }
59
+ catch {
60
+ // 解析不了与「解析出来不是对象」是同一档坏
61
+ parsed = null;
62
+ }
63
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
64
+ await this.archiveBroken(this.configPath);
65
+ await this.write(DEFAULT_APP_CONFIG);
66
+ await this.onRecovered?.(this.configPath);
67
+ return { ...DEFAULT_APP_CONFIG };
68
+ }
69
+ return parsed;
43
70
  }
44
71
  async write(config) {
45
72
  const dir = dirname(this.configPath);
@@ -328,9 +355,15 @@ export class ConfigManager {
328
355
  return join(this.basePath, "mcp.json");
329
356
  }
330
357
  /**
331
- * 确保 basePath/mcp.json 存在。不存在时写入出厂默认 MCP 配置(含 socket-pty 与 chrome-devtools)。
332
- * 已有文件不被覆盖,尊重用户已配置的 MCP 服务器。
333
- * @returns 返回当前生效的 MCP 配置原始内容
358
+ * 确保 basePath/mcp.json 存在,三种情况分开(与 `config.json` / `host.json` 同一口径):
359
+ *
360
+ * - **不存在** → 写入出厂默认 MCP 配置(含 socket-pty 与 chrome-devtools)。这是正常首启,
361
+ * 不回调;
362
+ * - **存在且解析得成对象** → 原样返回,不动用户的文件(哪怕里面全是用户自己的服务器);
363
+ * - **存在但坏了**(读不了 / 不是 JSON / 不是对象 / 是数组)→ 恢复出厂:先把坏文件改名
364
+ * 留档为 `mcp.json.broken`,再写回出厂默认配置,然后回调一次。
365
+ *
366
+ * @returns 当前生效的 MCP 配置原始内容
334
367
  */
335
368
  async ensureDefaultMcpConfig() {
336
369
  const mcpPath = this.mcpPath;
@@ -343,33 +376,45 @@ export class ConfigManager {
343
376
  await this.writeJson(mcpPath, DEFAULT_MCP_CONFIG);
344
377
  return { ...DEFAULT_MCP_CONFIG };
345
378
  }
346
- // 已存在 → 不覆盖,读取返回
347
- return JSON.parse(await fs.readFile(mcpPath, "utf-8"));
348
- }
349
- /**
350
- * 读取 basePath/mcp.json,不存在时返回空结构。
351
- */
352
- async readMcpConfig() {
353
- const mcpPath = this.mcpPath;
379
+ // 存在:读不出来、解析不了、解析出来不是对象(含数组)都是同一档「坏」
380
+ let parsed = null;
354
381
  try {
355
- await fs.access(mcpPath);
382
+ parsed = JSON.parse(await fs.readFile(mcpPath, "utf-8"));
356
383
  }
357
384
  catch {
358
- return { mcpServers: {} };
385
+ parsed = null;
359
386
  }
360
- try {
361
- return JSON.parse(await fs.readFile(mcpPath, "utf-8"));
362
- }
363
- catch {
364
- return { mcpServers: {} };
387
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
388
+ return parsed;
365
389
  }
390
+ // 恢复出厂:先留一份残骸(用户会问「我原来那些服务器去哪了」),再写回出厂配置
391
+ await this.archiveBroken(mcpPath);
392
+ await this.writeJson(mcpPath, DEFAULT_MCP_CONFIG);
393
+ await this.onRecovered?.(mcpPath);
394
+ return { ...DEFAULT_MCP_CONFIG };
366
395
  }
367
396
  /**
368
- * 原子写入 mcp.json。
397
+ * 把坏掉的配置文件改名留档为 `<文件名>.broken`(`config.json` 与 `mcp.json` 同一口径)。
398
+ *
399
+ * 同名残骸已经在就带上时间戳(同一毫秒再撞上就补序号),免得把上一份残骸覆盖掉。
369
400
  */
370
- async writeMcpConfig(config) {
371
- await this.ensureDirs();
372
- await this.writeJson(this.mcpPath, config);
401
+ async archiveBroken(path) {
402
+ const base = `${path}.broken`;
403
+ let target = base;
404
+ for (let index = 0; await this.exists(target); index += 1) {
405
+ target = index === 0 ? `${base}.${Date.now()}` : `${base}.${Date.now()}-${index}`;
406
+ }
407
+ await fs.rename(path, target);
408
+ }
409
+ /** 这个路径在不在(`access` 不抛就是在) */
410
+ async exists(path) {
411
+ try {
412
+ await fs.access(path);
413
+ return true;
414
+ }
415
+ catch {
416
+ return false;
417
+ }
373
418
  }
374
419
  /** 原子写 JSON 文件(先写 .tmp 再 rename,避免半成品)。 */
375
420
  async writeJson(path, data) {
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export { SubAgentsScopePlugin } from "./scope/plugins/subagents/SubAgentsScopePl
21
21
  export { allInOne } from "./scope/plugins/allInOne.js";
22
22
  export type { AllInOneOptions } from "./scope/plugins/allInOne.js";
23
23
  export { ConfigManager, DEFAULT_AGENT_ID, DEFAULT_AGENT_DEFINITION, DEFAULT_SUBAGENT_ID, DEFAULT_SUBAGENT_DEFINITION, DEFAULT_APP_CONFIG, DEFAULT_MCP_CONFIG, CONFIG_SUB_DIRS, } from "./config/ConfigManager.js";
24
+ export type { ConfigRecoveredHandler } from "./config/ConfigManager.js";
24
25
  export { FACTORY_SKILLS, HANDOFF_SKILL_ID, FALLBACK_HANDOFF_TEMPLATE, isCustomSkill, } from "./config/factory-skills.js";
25
26
  export { FACTORY_REASONING_PRESETS, REASONING_PRESETS_FILE } from "./config/reasoning-presets.js";
26
27
  export { EntityRepository } from "./shared/EntityRepository.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-zen/agents-sdk",
3
- "version": "1.2.0-alpha.0",
3
+ "version": "1.2.0-alpha.2",
4
4
  "description": "AI-Zen Agents SDK — shared business logic for CLI and Desktop",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",