@ai-zen/agents-sdk 1.2.0-alpha.0 → 1.2.0-alpha.1
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,25 @@ export declare class ConfigManager {
|
|
|
7
14
|
readonly basePath: string;
|
|
8
15
|
/** 当前配置里的模型 id 集合(bootstrap 时填充,供默认 Agent 校验 modelId) */
|
|
9
16
|
private knownModelIds;
|
|
10
|
-
|
|
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
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* 读磁盘原文(不做任何迁移/同步),供 `ensureDefaultConfig()` 对比写盘用。
|
|
31
|
+
*
|
|
32
|
+
* 文件在但读不成配置(JSON 解析不了、或者解析出来不是对象)就是一档「坏」:当场用出厂
|
|
33
|
+
* 配置写回磁盘,再回调一次,返回出厂配置。写回是为了让下次启动不再遇到同一个坏文件 ——
|
|
34
|
+
* 只返回默认值不落盘,用户每次开客户端都会再撞一次。
|
|
35
|
+
*/
|
|
20
36
|
private readRaw;
|
|
21
37
|
write(config: AppConfig): Promise<void>;
|
|
22
38
|
/**
|
|
@@ -12,15 +12,19 @@ export class ConfigManager {
|
|
|
12
12
|
basePath;
|
|
13
13
|
/** 当前配置里的模型 id 集合(bootstrap 时填充,供默认 Agent 校验 modelId) */
|
|
14
14
|
knownModelIds = new Set();
|
|
15
|
-
|
|
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,13 @@ export class ConfigManager {
|
|
|
30
34
|
const raw = await this.readRaw();
|
|
31
35
|
return raw ? migrateConfig(raw).config : { ...DEFAULT_APP_CONFIG };
|
|
32
36
|
}
|
|
33
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* 读磁盘原文(不做任何迁移/同步),供 `ensureDefaultConfig()` 对比写盘用。
|
|
39
|
+
*
|
|
40
|
+
* 文件在但读不成配置(JSON 解析不了、或者解析出来不是对象)就是一档「坏」:当场用出厂
|
|
41
|
+
* 配置写回磁盘,再回调一次,返回出厂配置。写回是为了让下次启动不再遇到同一个坏文件 ——
|
|
42
|
+
* 只返回默认值不落盘,用户每次开客户端都会再撞一次。
|
|
43
|
+
*/
|
|
34
44
|
async readRaw() {
|
|
35
45
|
try {
|
|
36
46
|
await fs.access(this.configPath);
|
|
@@ -39,7 +49,20 @@ export class ConfigManager {
|
|
|
39
49
|
return null;
|
|
40
50
|
}
|
|
41
51
|
const raw = await fs.readFile(this.configPath, "utf-8");
|
|
42
|
-
|
|
52
|
+
let parsed;
|
|
53
|
+
try {
|
|
54
|
+
parsed = JSON.parse(raw);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// 解析不了与「解析出来不是对象」是同一档坏
|
|
58
|
+
parsed = null;
|
|
59
|
+
}
|
|
60
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
61
|
+
await this.write(DEFAULT_APP_CONFIG);
|
|
62
|
+
await this.onRecovered?.(this.configPath);
|
|
63
|
+
return { ...DEFAULT_APP_CONFIG };
|
|
64
|
+
}
|
|
65
|
+
return parsed;
|
|
43
66
|
}
|
|
44
67
|
async write(config) {
|
|
45
68
|
const dir = dirname(this.configPath);
|
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";
|