@pencil-agent/nano-pencil 1.10.8 → 1.11.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.
- package/dist/builtin-extensions.d.ts +34 -0
- package/dist/builtin-extensions.js +153 -0
- package/dist/cli/config-selector.d.ts +1 -1
- package/dist/cli/file-processor.js +1 -1
- package/dist/cli/session-picker.d.ts +1 -1
- package/dist/core/bash-executor.js +1 -1
- package/dist/core/{auth-storage.js → config/auth-storage.js} +1 -1
- package/dist/core/{resource-loader.d.ts → config/resource-loader.d.ts} +8 -8
- package/dist/core/{resource-loader.js → config/resource-loader.js} +7 -7
- package/dist/core/{settings-manager.js → config/settings-manager.js} +1 -1
- package/dist/core/export-html/index.d.ts +1 -1
- package/dist/core/export-html/index.js +1 -1
- package/dist/core/extensions/loader.d.ts +1 -1
- package/dist/core/extensions/loader.js +1 -1
- package/dist/core/extensions/runner.d.ts +1 -1
- package/dist/core/extensions/types.d.ts +5 -3
- package/dist/core/index.d.ts +3 -3
- package/dist/core/index.js +2 -2
- package/dist/core/model/index.d.ts +5 -0
- package/dist/core/model/index.js +5 -0
- package/dist/core/model/switcher.d.ts +80 -0
- package/dist/core/model/switcher.js +133 -0
- package/dist/core/model-registry.d.ts +2 -2
- package/dist/core/model-registry.js +1 -1
- package/dist/core/package-manager.d.ts +1 -1
- package/dist/core/{prompt-templates.js → prompt/prompt-templates.js} +2 -2
- package/dist/core/{system-prompt.d.ts → prompt/system-prompt.d.ts} +3 -1
- package/dist/core/{system-prompt.js → prompt/system-prompt.js} +15 -15
- package/dist/core/{agent-session.d.ts → runtime/agent-session.d.ts} +39 -10
- package/dist/core/{agent-session.js → runtime/agent-session.js} +82 -15
- package/dist/core/{sdk.d.ts → runtime/sdk.d.ts} +11 -11
- package/dist/core/{sdk.js → runtime/sdk.js} +13 -13
- package/dist/core/{compaction → session/compaction}/branch-summarization.d.ts +1 -1
- package/dist/core/{compaction → session/compaction}/branch-summarization.js +1 -1
- package/dist/core/session/compaction/compaction-coordinator.d.ts +65 -0
- package/dist/core/session/compaction/compaction-coordinator.js +67 -0
- package/dist/core/{compaction → session/compaction}/compaction.d.ts +1 -1
- package/dist/core/{compaction → session/compaction}/compaction.js +1 -1
- package/dist/core/{session-manager.d.ts → session/session-manager.d.ts} +1 -1
- package/dist/core/{session-manager.js → session/session-manager.js} +2 -2
- package/dist/core/soul-integration.d.ts +5 -4
- package/dist/core/soul-integration.js +42 -11
- package/dist/core/tools/bash.js +1 -1
- package/dist/core/tools/find.js +1 -1
- package/dist/core/tools/grep.js +1 -1
- package/dist/core/tools/index.d.ts +13 -0
- package/dist/core/tools/index.js +35 -0
- package/dist/core/tools/orchestrator.d.ts +73 -0
- package/dist/core/tools/orchestrator.js +92 -0
- package/dist/core/tools/read.js +1 -1
- package/dist/core/tools/source.d.ts +79 -0
- package/dist/core/tools/source.js +104 -0
- package/dist/{utils → core/utils}/shell.js +2 -2
- package/dist/{utils → core/utils}/tools-manager.js +1 -1
- package/dist/extensions/{link-world → defaults/link-world}/index.d.ts +1 -1
- package/dist/extensions/{link-world → defaults/link-world}/index.js +1 -1
- package/dist/extensions/defaults/mcp/index.d.ts +18 -0
- package/dist/extensions/defaults/mcp/index.js +45 -0
- package/dist/extensions/{security-audit → defaults/security-audit}/engine/logger.js +1 -1
- package/dist/extensions/{security-audit → defaults/security-audit}/index.d.ts +1 -1
- package/dist/extensions/defaults/soul/index.d.ts +17 -0
- package/dist/extensions/defaults/soul/index.js +335 -0
- package/dist/extensions/optional/export-html/index.d.ts +36 -0
- package/dist/extensions/optional/export-html/index.js +251 -0
- package/dist/extensions/optional/index.d.ts +9 -0
- package/dist/extensions/optional/index.js +10 -0
- package/dist/extensions/{simplify → optional/simplify}/index.d.ts +1 -1
- package/dist/extensions/{simplify → optional/simplify}/index.js +2 -2
- package/dist/index.d.ts +11 -11
- package/dist/index.js +10 -10
- package/dist/main.js +7 -7
- package/dist/modes/interactive/components/config-selector.d.ts +1 -1
- package/dist/modes/interactive/components/footer.d.ts +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts +1 -1
- package/dist/modes/interactive/components/session-selector-search.d.ts +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +1 -1
- package/dist/modes/interactive/components/soul-stats.d.ts +1 -1
- package/dist/modes/interactive/components/tool-execution.js +2 -2
- package/dist/modes/interactive/components/tree-selector.d.ts +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -1
- package/dist/modes/interactive/interactive-mode.js +5 -5
- package/dist/modes/print-mode.d.ts +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +2 -2
- package/dist/modes/rpc/rpc-mode.d.ts +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +2 -2
- package/dist/{utils → modes/utils}/clipboard-image.js +1 -1
- package/dist/{utils → modes/utils}/image-convert.js +1 -1
- package/dist/{utils → modes/utils}/image-resize.js +1 -1
- package/dist/nanopencil-defaults.d.ts +1 -1
- package/dist/packages/{nano-mem → mem-core}/cli.js +21 -0
- package/dist/packages/mem-core/dedup.d.ts +17 -0
- package/dist/packages/mem-core/dedup.js +84 -0
- package/dist/packages/{nano-mem → mem-core}/engine.d.ts +9 -0
- package/dist/packages/{nano-mem → mem-core}/engine.js +79 -8
- package/dist/packages/{nano-mem → mem-core}/extension.js +3 -3
- package/dist/packages/{nano-mem → mem-core}/i18n.js +134 -102
- package/dist/packages/{nano-mem → mem-core}/package.json +1 -1
- package/dist/packages/mem-core/update.d.ts +34 -0
- package/dist/packages/{nano-mem → mem-core}/update.js +35 -2
- package/dist/packages/{nanosoul → soul-core}/manager.d.ts +1 -1
- package/dist/packages/{nanosoul → soul-core}/manager.js +1 -3
- package/dist/packages/{nanosoul → soul-core}/package.json +1 -1
- package/docs/ARCHITECTURE_EVOLUTION.md +228 -0
- package/docs/ARCHITECTURE_OPTIMIZATION.md +404 -0
- package/docs/REFACTORING_PLAN.md +592 -0
- package/docs/REFACTORING_TASKS.md +181 -0
- package/docs/long_text_2F8185A2-88D9-4971-A476-A64C9E0A2282.txt +399 -0
- package/docs/plan.md +44 -0
- package/docs/v2.md +199 -0
- package/package.json +4 -4
- package/dist/packages/nano-mem/update.d.ts +0 -14
- package/dist/packages/nanosoul/src/config.d.ts +0 -20
- package/dist/packages/nanosoul/src/config.js +0 -103
- package/dist/packages/nanosoul/src/evolution.d.ts +0 -82
- package/dist/packages/nanosoul/src/evolution.js +0 -335
- package/dist/packages/nanosoul/src/index.d.ts +0 -13
- package/dist/packages/nanosoul/src/index.js +0 -13
- package/dist/packages/nanosoul/src/injection.d.ts +0 -28
- package/dist/packages/nanosoul/src/injection.js +0 -407
- package/dist/packages/nanosoul/src/manager.d.ts +0 -105
- package/dist/packages/nanosoul/src/manager.js +0 -459
- package/dist/packages/nanosoul/src/store.js +0 -229
- package/dist/packages/nanosoul/src/types.d.ts +0 -369
- package/dist/packages/nanosoul/store.d.ts +0 -62
- package/dist/packages/nanosoul/types.js +0 -7
- package/dist/pencil-mem-integration.d.ts +0 -10
- package/dist/pencil-mem-integration.js +0 -102
- /package/dist/core/{auth-storage.d.ts → config/auth-storage.d.ts} +0 -0
- /package/dist/core/{resolve-config-value.d.ts → config/resolve-config-value.d.ts} +0 -0
- /package/dist/core/{resolve-config-value.js → config/resolve-config-value.js} +0 -0
- /package/dist/core/{settings-manager.d.ts → config/settings-manager.d.ts} +0 -0
- /package/dist/core/{prompt-templates.d.ts → prompt/prompt-templates.d.ts} +0 -0
- /package/dist/core/{event-bus.d.ts → runtime/event-bus.d.ts} +0 -0
- /package/dist/core/{event-bus.js → runtime/event-bus.js} +0 -0
- /package/dist/core/{compaction → session/compaction}/index.d.ts +0 -0
- /package/dist/core/{compaction → session/compaction}/index.js +0 -0
- /package/dist/core/{compaction → session/compaction}/utils.d.ts +0 -0
- /package/dist/core/{compaction → session/compaction}/utils.js +0 -0
- /package/dist/{utils → core/utils}/shell.d.ts +0 -0
- /package/dist/{utils → core/utils}/sleep.d.ts +0 -0
- /package/dist/{utils → core/utils}/sleep.js +0 -0
- /package/dist/{utils → core/utils}/tools-manager.d.ts +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/engine/detector.d.ts +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/engine/detector.js +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/engine/interceptor.d.ts +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/engine/interceptor.js +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/engine/logger.d.ts +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/index.js +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/interface.d.ts +0 -0
- /package/dist/extensions/{security-audit → defaults/security-audit}/interface.js +0 -0
- /package/dist/{utils → modes/utils}/clipboard-image.d.ts +0 -0
- /package/dist/{utils → modes/utils}/clipboard-native.d.ts +0 -0
- /package/dist/{utils → modes/utils}/clipboard-native.js +0 -0
- /package/dist/{utils → modes/utils}/clipboard.d.ts +0 -0
- /package/dist/{utils → modes/utils}/clipboard.js +0 -0
- /package/dist/{utils → modes/utils}/image-convert.d.ts +0 -0
- /package/dist/{utils → modes/utils}/image-resize.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/cli.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/config.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/config.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/consolidation.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/consolidation.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/eviction.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/eviction.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/extension.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/extraction.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/extraction.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/full-insights-html.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/full-insights-html.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/full-insights.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/full-insights.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/i18n.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/index.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/index.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/insights-html.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/insights-html.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/linking.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/linking.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/privacy.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/privacy.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/scoring.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/scoring.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/store.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/store.js +0 -0
- /package/dist/packages/{nano-mem → mem-core}/types.d.ts +0 -0
- /package/dist/packages/{nano-mem → mem-core}/types.js +0 -0
- /package/dist/packages/{nanosoul → soul-core}/config.d.ts +0 -0
- /package/dist/packages/{nanosoul → soul-core}/config.js +0 -0
- /package/dist/packages/{nanosoul → soul-core}/evolution.d.ts +0 -0
- /package/dist/packages/{nanosoul → soul-core}/evolution.js +0 -0
- /package/dist/packages/{nanosoul → soul-core}/index.d.ts +0 -0
- /package/dist/packages/{nanosoul → soul-core}/index.js +0 -0
- /package/dist/packages/{nanosoul → soul-core}/injection.d.ts +0 -0
- /package/dist/packages/{nanosoul → soul-core}/injection.js +0 -0
- /package/dist/packages/{nanosoul/src → soul-core}/store.d.ts +0 -0
- /package/dist/packages/{nanosoul → soul-core}/store.js +0 -0
- /package/dist/packages/{nanosoul → soul-core}/types.d.ts +0 -0
- /package/dist/packages/{nanosoul/src → soul-core}/types.js +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NanoPencil 内置扩展注册表
|
|
3
|
+
*
|
|
4
|
+
* 管理 NanoPencil 默认加载的内置扩展路径:
|
|
5
|
+
* - NanoMem: 持久化记忆扩展(packages/mem-core/)
|
|
6
|
+
* - Soul: AI 人格进化扩展(extensions/defaults/soul/)
|
|
7
|
+
* - MCP: MCP 工具扩展(extensions/defaults/mcp/)
|
|
8
|
+
* - LinkWorld: 互联网访问扩展(extensions/defaults/link-world/)
|
|
9
|
+
* - SecurityAudit: 安全审计扩展(extensions/defaults/security-audit/)
|
|
10
|
+
*
|
|
11
|
+
* 可选扩展(通过配置启用):
|
|
12
|
+
* - Simplify: 代码简化扩展(extensions/optional/simplify/)
|
|
13
|
+
* - export-html: HTML 导出扩展(extensions/optional/export-html/)
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* 获取 NanoPencil 默认加载的内置扩展路径列表
|
|
17
|
+
*
|
|
18
|
+
* 返回所有默认启用的扩展路径(defaults/):
|
|
19
|
+
* - NanoMem (持久化记忆)
|
|
20
|
+
* - Soul (AI 人格进化)
|
|
21
|
+
* - LinkWorld (联网搜索)
|
|
22
|
+
* - SecurityAudit (安全审计)
|
|
23
|
+
* - MCP (MCP 协议适配)
|
|
24
|
+
*
|
|
25
|
+
* 可选扩展需要通过配置启用:
|
|
26
|
+
* - Simplify (代码简化) - extensions/optional/simplify/
|
|
27
|
+
* - export-html (HTML 导出) - extensions/optional/export-html/
|
|
28
|
+
*/
|
|
29
|
+
export declare function getBuiltinExtensionPaths(): string[];
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated 请使用 getBuiltinExtensionPaths()
|
|
32
|
+
*/
|
|
33
|
+
export declare function getNanopencilDefaultExtensionPaths(): string[];
|
|
34
|
+
//# sourceMappingURL=builtin-extensions.d.ts.map
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NanoPencil 内置扩展注册表
|
|
3
|
+
*
|
|
4
|
+
* 管理 NanoPencil 默认加载的内置扩展路径:
|
|
5
|
+
* - NanoMem: 持久化记忆扩展(packages/mem-core/)
|
|
6
|
+
* - Soul: AI 人格进化扩展(extensions/defaults/soul/)
|
|
7
|
+
* - MCP: MCP 工具扩展(extensions/defaults/mcp/)
|
|
8
|
+
* - LinkWorld: 互联网访问扩展(extensions/defaults/link-world/)
|
|
9
|
+
* - SecurityAudit: 安全审计扩展(extensions/defaults/security-audit/)
|
|
10
|
+
*
|
|
11
|
+
* 可选扩展(通过配置启用):
|
|
12
|
+
* - Simplify: 代码简化扩展(extensions/optional/simplify/)
|
|
13
|
+
* - export-html: HTML 导出扩展(extensions/optional/export-html/)
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import { dirname, join } from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
const require = createRequire(import.meta.url);
|
|
21
|
+
/** 内置扩展路径 */
|
|
22
|
+
const BUNDLED_NANOMEM_EXTENSION_PACKAGES = join(__dirname, "packages", "mem-core", "extension.js");
|
|
23
|
+
const BUNDLED_SIMPLIFY_EXTENSION = join(__dirname, "extensions", "optional", "simplify", "index.js");
|
|
24
|
+
const BUNDLED_LINK_WORLD_EXTENSION = join(__dirname, "extensions", "defaults", "link-world", "index.js");
|
|
25
|
+
const BUNDLED_SECURITY_AUDIT_EXTENSION = join(__dirname, "extensions", "defaults", "security-audit", "index.js");
|
|
26
|
+
const BUNDLED_SOUL_EXTENSION = join(__dirname, "extensions", "defaults", "soul", "index.js");
|
|
27
|
+
const BUNDLED_MCP_EXTENSION = join(__dirname, "extensions", "defaults", "mcp", "index.js");
|
|
28
|
+
const BUNDLED_EXPORT_HTML_EXTENSION = join(__dirname, "extensions", "optional", "export-html", "index.js");
|
|
29
|
+
/** 从当前模块位置向上查找包根(含 package.json 且 name 为 nano-pencil 相关) */
|
|
30
|
+
function findPackageRoot(startDir) {
|
|
31
|
+
let dir = startDir;
|
|
32
|
+
for (let i = 0; i < 20; i++) {
|
|
33
|
+
try {
|
|
34
|
+
const pkgPath = join(dir, "package.json");
|
|
35
|
+
if (existsSync(pkgPath)) {
|
|
36
|
+
const raw = readFileSync(pkgPath, "utf-8");
|
|
37
|
+
const pkg = JSON.parse(raw);
|
|
38
|
+
if (pkg.name === "@pencil-agent/nano-pencil" || pkg.name === "nanopencil")
|
|
39
|
+
return dir;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// ignore
|
|
44
|
+
}
|
|
45
|
+
const parent = dirname(dir);
|
|
46
|
+
if (parent === dir)
|
|
47
|
+
break;
|
|
48
|
+
dir = parent;
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 获取 NanoPencil 默认加载的内置扩展路径列表
|
|
54
|
+
*
|
|
55
|
+
* 返回所有默认启用的扩展路径(defaults/):
|
|
56
|
+
* - NanoMem (持久化记忆)
|
|
57
|
+
* - Soul (AI 人格进化)
|
|
58
|
+
* - LinkWorld (联网搜索)
|
|
59
|
+
* - SecurityAudit (安全审计)
|
|
60
|
+
* - MCP (MCP 协议适配)
|
|
61
|
+
*
|
|
62
|
+
* 可选扩展需要通过配置启用:
|
|
63
|
+
* - Simplify (代码简化) - extensions/optional/simplify/
|
|
64
|
+
* - export-html (HTML 导出) - extensions/optional/export-html/
|
|
65
|
+
*/
|
|
66
|
+
export function getBuiltinExtensionPaths() {
|
|
67
|
+
const paths = [];
|
|
68
|
+
// === NanoMem 扩展 ===
|
|
69
|
+
// 1) 优先使用 build 时打包到 dist/packages 的扩展
|
|
70
|
+
if (existsSync(BUNDLED_NANOMEM_EXTENSION_PACKAGES)) {
|
|
71
|
+
paths.push(BUNDLED_NANOMEM_EXTENSION_PACKAGES);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// 2) require.resolve:开发/本地安装时 node_modules 中的 mem-core
|
|
75
|
+
try {
|
|
76
|
+
const extPath = require.resolve("@pencil-agent/mem-core/extension.js");
|
|
77
|
+
if (existsSync(extPath))
|
|
78
|
+
paths.push(extPath);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// 3) 按包根 + node_modules/@pencil-agent/mem-core/dist/extension.js 查找
|
|
82
|
+
const packageRoot = findPackageRoot(__dirname);
|
|
83
|
+
if (packageRoot) {
|
|
84
|
+
const candidate = join(packageRoot, "node_modules", "@pencil-agent", "mem-core", "dist", "extension.js");
|
|
85
|
+
if (existsSync(candidate))
|
|
86
|
+
paths.push(candidate);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// === Simplify 扩展(可选源码,编译到 dist/extensions/optional/simplify) ===
|
|
91
|
+
if (existsSync(BUNDLED_SIMPLIFY_EXTENSION)) {
|
|
92
|
+
paths.push(BUNDLED_SIMPLIFY_EXTENSION);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
// 开发模式:尝试 .ts 源文件
|
|
96
|
+
const simplifyTs = join(__dirname, "extensions", "optional", "simplify", "index.ts");
|
|
97
|
+
if (existsSync(simplifyTs))
|
|
98
|
+
paths.push(simplifyTs);
|
|
99
|
+
}
|
|
100
|
+
// === link-world 扩展(内置源码,编译到 dist/extensions/defaults/link-world) ===
|
|
101
|
+
if (existsSync(BUNDLED_LINK_WORLD_EXTENSION)) {
|
|
102
|
+
paths.push(BUNDLED_LINK_WORLD_EXTENSION);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const linkWorldTs = join(__dirname, "extensions", "defaults", "link-world", "index.ts");
|
|
106
|
+
if (existsSync(linkWorldTs))
|
|
107
|
+
paths.push(linkWorldTs);
|
|
108
|
+
}
|
|
109
|
+
// === Security Audit 扩展(内置源码,编译到 dist/extensions/defaults/security-audit) ===
|
|
110
|
+
if (existsSync(BUNDLED_SECURITY_AUDIT_EXTENSION)) {
|
|
111
|
+
paths.push(BUNDLED_SECURITY_AUDIT_EXTENSION);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const securityAuditTs = join(__dirname, "extensions", "defaults", "security-audit", "index.ts");
|
|
115
|
+
if (existsSync(securityAuditTs))
|
|
116
|
+
paths.push(securityAuditTs);
|
|
117
|
+
}
|
|
118
|
+
// === Soul 扩展(人格进化系统) ===
|
|
119
|
+
if (existsSync(BUNDLED_SOUL_EXTENSION)) {
|
|
120
|
+
paths.push(BUNDLED_SOUL_EXTENSION);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const soulTs = join(__dirname, "extensions", "defaults", "soul", "index.ts");
|
|
124
|
+
if (existsSync(soulTs))
|
|
125
|
+
paths.push(soulTs);
|
|
126
|
+
}
|
|
127
|
+
// === MCP 扩展(MCP 工具协议适配) ===
|
|
128
|
+
if (existsSync(BUNDLED_MCP_EXTENSION)) {
|
|
129
|
+
paths.push(BUNDLED_MCP_EXTENSION);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const mcpTs = join(__dirname, "extensions", "defaults", "mcp", "index.ts");
|
|
133
|
+
if (existsSync(mcpTs))
|
|
134
|
+
paths.push(mcpTs);
|
|
135
|
+
}
|
|
136
|
+
// === export-html 扩展(可选,HTML 导出功能) ===
|
|
137
|
+
if (existsSync(BUNDLED_EXPORT_HTML_EXTENSION)) {
|
|
138
|
+
paths.push(BUNDLED_EXPORT_HTML_EXTENSION);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const exportHtmlTs = join(__dirname, "extensions", "optional", "export-html", "index.ts");
|
|
142
|
+
if (existsSync(exportHtmlTs))
|
|
143
|
+
paths.push(exportHtmlTs);
|
|
144
|
+
}
|
|
145
|
+
return paths;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated 请使用 getBuiltinExtensionPaths()
|
|
149
|
+
*/
|
|
150
|
+
export function getNanopencilDefaultExtensionPaths() {
|
|
151
|
+
return getBuiltinExtensionPaths();
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=builtin-extensions.js.map
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* TUI config selector for `pi config` command
|
|
3
3
|
*/
|
|
4
4
|
import type { ResolvedPaths } from "../core/package-manager.js";
|
|
5
|
-
import type { SettingsManager } from "../core/settings-manager.js";
|
|
5
|
+
import type { SettingsManager } from "../core/config/settings-manager.js";
|
|
6
6
|
export interface ConfigSelectorOptions {
|
|
7
7
|
resolvedPaths: ResolvedPaths;
|
|
8
8
|
settingsManager: SettingsManager;
|
|
@@ -5,7 +5,7 @@ import { access, readFile, stat } from "node:fs/promises";
|
|
|
5
5
|
import chalk from "chalk";
|
|
6
6
|
import { resolve } from "path";
|
|
7
7
|
import { resolveReadPath } from "../core/tools/path-utils.js";
|
|
8
|
-
import { formatDimensionNote, resizeImage } from "../utils/image-resize.js";
|
|
8
|
+
import { formatDimensionNote, resizeImage } from "../modes/utils/image-resize.js";
|
|
9
9
|
import { detectSupportedImageMimeTypeFromFile } from "../utils/mime.js";
|
|
10
10
|
/** Process @file arguments into text content and image attachments */
|
|
11
11
|
export async function processFileArguments(fileArgs, options) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* TUI session selector for --resume flag
|
|
3
3
|
*/
|
|
4
|
-
import type { SessionInfo, SessionListProgress } from "../core/session-manager.js";
|
|
4
|
+
import type { SessionInfo, SessionListProgress } from "../core/session/session-manager.js";
|
|
5
5
|
type SessionsLoader = (onProgress?: SessionListProgress) => Promise<SessionInfo[]>;
|
|
6
6
|
/** Show TUI session selector and return selected session path or null if cancelled */
|
|
7
7
|
export declare function selectSession(currentSessionsLoader: SessionsLoader, allSessionsLoader: SessionsLoader): Promise<string | null>;
|
|
@@ -11,7 +11,7 @@ import { tmpdir } from "node:os";
|
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { spawn } from "child_process";
|
|
13
13
|
import stripAnsi from "strip-ansi";
|
|
14
|
-
import { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "
|
|
14
|
+
import { getShellConfig, getShellEnv, killProcessTree, sanitizeBinaryOutput } from "./utils/shell.js";
|
|
15
15
|
import { DEFAULT_MAX_BYTES, truncateTail } from "./tools/truncate.js";
|
|
16
16
|
// ============================================================================
|
|
17
17
|
// Implementation
|
|
@@ -9,7 +9,7 @@ import { getEnvApiKey, getOAuthApiKey, getOAuthProvider, getOAuthProviders, } fr
|
|
|
9
9
|
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
10
10
|
import { dirname, join } from "path";
|
|
11
11
|
import lockfile from "proper-lockfile";
|
|
12
|
-
import { getAgentDir } from "
|
|
12
|
+
import { getAgentDir } from "../../config.js";
|
|
13
13
|
import { resolveConfigValue } from "./resolve-config-value.js";
|
|
14
14
|
export class FileAuthStorageBackend {
|
|
15
15
|
authPath;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { type Theme } from "
|
|
2
|
-
import type { ResourceDiagnostic } from "
|
|
3
|
-
export type { ResourceCollision, ResourceDiagnostic } from "
|
|
4
|
-
import { type EventBus } from "
|
|
5
|
-
import type { ExtensionFactory, LoadExtensionsResult } from "
|
|
6
|
-
import { type PathMetadata } from "
|
|
7
|
-
import type { PromptTemplate } from "
|
|
1
|
+
import { type Theme } from "../../modes/interactive/theme/theme.js";
|
|
2
|
+
import type { ResourceDiagnostic } from "../diagnostics.js";
|
|
3
|
+
export type { ResourceCollision, ResourceDiagnostic } from "../diagnostics.js";
|
|
4
|
+
import { type EventBus } from "../runtime/event-bus.js";
|
|
5
|
+
import type { ExtensionFactory, LoadExtensionsResult } from "../extensions/types.js";
|
|
6
|
+
import { type PathMetadata } from "../package-manager.js";
|
|
7
|
+
import type { PromptTemplate } from "../prompt/prompt-templates.js";
|
|
8
8
|
import { SettingsManager } from "./settings-manager.js";
|
|
9
|
-
import type { Skill } from "
|
|
9
|
+
import type { Skill } from "../skills.js";
|
|
10
10
|
export interface ResourceExtensionPaths {
|
|
11
11
|
skillPaths?: Array<{
|
|
12
12
|
path: string;
|
|
@@ -2,14 +2,14 @@ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join, resolve, sep } from "node:path";
|
|
4
4
|
import chalk from "chalk";
|
|
5
|
-
import { CONFIG_DIR_NAME, getAgentDir } from "
|
|
6
|
-
import { loadThemeFromPath } from "
|
|
7
|
-
import { createEventBus } from "
|
|
8
|
-
import { createExtensionRuntime, loadExtensionFromFactory, loadExtensions } from "
|
|
9
|
-
import { DefaultPackageManager } from "
|
|
10
|
-
import { loadPromptTemplates } from "
|
|
5
|
+
import { CONFIG_DIR_NAME, getAgentDir } from "../../config.js";
|
|
6
|
+
import { loadThemeFromPath } from "../../modes/interactive/theme/theme.js";
|
|
7
|
+
import { createEventBus } from "../runtime/event-bus.js";
|
|
8
|
+
import { createExtensionRuntime, loadExtensionFromFactory, loadExtensions } from "../extensions/loader.js";
|
|
9
|
+
import { DefaultPackageManager } from "../package-manager.js";
|
|
10
|
+
import { loadPromptTemplates } from "../prompt/prompt-templates.js";
|
|
11
11
|
import { SettingsManager } from "./settings-manager.js";
|
|
12
|
-
import { loadSkills } from "
|
|
12
|
+
import { loadSkills } from "../skills.js";
|
|
13
13
|
function resolvePromptInput(input, description) {
|
|
14
14
|
if (!input) {
|
|
15
15
|
return undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
2
|
import { dirname, join } from "path";
|
|
3
3
|
import lockfile from "proper-lockfile";
|
|
4
|
-
import { APP_NAME, CONFIG_DIR_NAME, getAgentDir } from "
|
|
4
|
+
import { APP_NAME, CONFIG_DIR_NAME, getAgentDir } from "../../config.js";
|
|
5
5
|
/** Deep merge settings: project/overrides take precedence, nested objects merge recursively */
|
|
6
6
|
function deepMergeSettings(base, overrides) {
|
|
7
7
|
const result = { ...base };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AgentState } from "@pencil-agent/agent-core";
|
|
2
|
-
import { SessionManager } from "../session-manager.js";
|
|
2
|
+
import { SessionManager } from "../session/session-manager.js";
|
|
3
3
|
/**
|
|
4
4
|
* Interface for rendering custom tools to HTML.
|
|
5
5
|
* Used by agent-session to pre-render extension tool output.
|
|
@@ -2,7 +2,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
|
2
2
|
import { basename, join } from "path";
|
|
3
3
|
import { APP_NAME, getExportTemplateDir } from "../../config.js";
|
|
4
4
|
import { getResolvedThemeColors, getThemeExportColors } from "../../modes/interactive/theme/theme.js";
|
|
5
|
-
import { SessionManager } from "../session-manager.js";
|
|
5
|
+
import { SessionManager } from "../session/session-manager.js";
|
|
6
6
|
/** Parse a color string to RGB values. Supports hex (#RRGGBB) and rgb(r,g,b) formats. */
|
|
7
7
|
function parseColor(color) {
|
|
8
8
|
const hexMatch = color.match(/^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/);
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Uses @mariozechner/jiti fork with virtualModules support for compiled Bun binaries.
|
|
5
5
|
*/
|
|
6
|
-
import { type EventBus } from "../event-bus.js";
|
|
6
|
+
import { type EventBus } from "../runtime/event-bus.js";
|
|
7
7
|
import type { Extension, ExtensionFactory, ExtensionRuntime, LoadExtensionsResult } from "./types.js";
|
|
8
8
|
/**
|
|
9
9
|
* Create a runtime with throwing stubs for action methods.
|
|
@@ -20,7 +20,7 @@ import { CONFIG_DIR_NAME, getAgentDir, isBunBinary } from "../../config.js";
|
|
|
20
20
|
// NOTE: This import works because loader.ts exports are NOT re-exported from index.ts,
|
|
21
21
|
// avoiding a circular dependency. Extensions can import from @pencil-agent/nano-pencil.
|
|
22
22
|
import * as _bundledPiCodingAgent from "../../index.js";
|
|
23
|
-
import { createEventBus } from "../event-bus.js";
|
|
23
|
+
import { createEventBus } from "../runtime/event-bus.js";
|
|
24
24
|
import { execCommand } from "../exec.js";
|
|
25
25
|
/** Modules available to extensions via virtualModules (for compiled Bun binary) */
|
|
26
26
|
const VIRTUAL_MODULES = {
|
|
@@ -7,7 +7,7 @@ import type { KeyId } from "@pencil-agent/tui";
|
|
|
7
7
|
import type { ResourceDiagnostic } from "../diagnostics.js";
|
|
8
8
|
import type { KeybindingsConfig } from "../keybindings.js";
|
|
9
9
|
import type { ModelRegistry } from "../model-registry.js";
|
|
10
|
-
import type { SessionManager } from "../session-manager.js";
|
|
10
|
+
import type { SessionManager } from "../session/session-manager.js";
|
|
11
11
|
import type { BeforeAgentStartEvent, BeforeAgentStartEventResult, ContextEvent, Extension, ExtensionActions, ExtensionCommandContext, ExtensionCommandContextActions, ExtensionContext, ExtensionContextActions, ExtensionError, ExtensionEvent, ExtensionFlag, ExtensionRuntime, ExtensionShortcut, ExtensionUIContext, InputEvent, InputEventResult, InputSource, MessageRenderer, RegisteredCommand, RegisteredTool, ResourcesDiscoverEvent, SessionBeforeCompactResult, SessionBeforeForkResult, SessionBeforeSwitchResult, SessionBeforeTreeResult, ToolCallEvent, ToolCallEventResult, ToolResultEvent, ToolResultEventResult, UserBashEvent, UserBashEventResult } from "./types.js";
|
|
12
12
|
/** Combined result from all before_agent_start handlers */
|
|
13
13
|
interface BeforeAgentStartCombinedResult {
|
|
@@ -13,14 +13,14 @@ import type { AutocompleteItem, Component, EditorComponent, EditorTheme, KeyId,
|
|
|
13
13
|
import type { Static, TSchema } from "@sinclair/typebox";
|
|
14
14
|
import type { Theme } from "../../modes/interactive/theme/theme.js";
|
|
15
15
|
import type { BashResult } from "../bash-executor.js";
|
|
16
|
-
import type { CompactionPreparation, CompactionResult } from "../compaction/index.js";
|
|
17
|
-
import type { EventBus } from "../event-bus.js";
|
|
16
|
+
import type { CompactionPreparation, CompactionResult } from "../session/compaction/index.js";
|
|
17
|
+
import type { EventBus } from "../runtime/event-bus.js";
|
|
18
18
|
import type { ExecOptions, ExecResult } from "../exec.js";
|
|
19
19
|
import type { ReadonlyFooterDataProvider } from "../footer-data-provider.js";
|
|
20
20
|
import type { KeybindingsManager } from "../keybindings.js";
|
|
21
21
|
import type { CustomMessage } from "../messages.js";
|
|
22
22
|
import type { ModelRegistry } from "../model-registry.js";
|
|
23
|
-
import type { BranchSummaryEntry, CompactionEntry, ReadonlySessionManager, SessionEntry, SessionManager } from "../session-manager.js";
|
|
23
|
+
import type { BranchSummaryEntry, CompactionEntry, ReadonlySessionManager, SessionEntry, SessionManager } from "../session/session-manager.js";
|
|
24
24
|
import type { SlashCommandInfo } from "../slash-commands.js";
|
|
25
25
|
import type { BashOperations } from "../tools/bash.js";
|
|
26
26
|
import type { EditToolDetails } from "../tools/edit.js";
|
|
@@ -257,6 +257,8 @@ export interface ToolDefinition<TParams extends TSchema = TSchema, TDetails = un
|
|
|
257
257
|
description: string;
|
|
258
258
|
/** Parameter schema (TypeBox) */
|
|
259
259
|
parameters: TParams;
|
|
260
|
+
/** Usage guidance for system prompt (optional) */
|
|
261
|
+
guidance?: string;
|
|
260
262
|
/** Execute the tool. */
|
|
261
263
|
execute(toolCallId: string, params: Static<TParams>, signal: AbortSignal | undefined, onUpdate: AgentToolUpdateCallback<TDetails> | undefined, ctx: ExtensionContext): Promise<AgentToolResult<TDetails>>;
|
|
262
264
|
/** Custom rendering for tool call display */
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Core modules shared between all run modes.
|
|
3
3
|
*/
|
|
4
|
-
export { AgentSession, type AgentSessionConfig, type AgentSessionEvent, type AgentSessionEventListener, type ModelCycleResult, type PromptOptions, type SessionStats, } from "./agent-session.js";
|
|
4
|
+
export { AgentSession, type AgentSessionConfig, type AgentSessionEvent, type AgentSessionEventListener, type ModelCycleResult, type PromptOptions, type SessionStats, } from "./runtime/agent-session.js";
|
|
5
5
|
export { type BashExecutorOptions, type BashResult, executeBash, executeBashWithOperations, } from "./bash-executor.js";
|
|
6
|
-
export type { CompactionResult } from "./compaction/index.js";
|
|
7
|
-
export { createEventBus, type EventBus, type EventBusController, } from "./event-bus.js";
|
|
6
|
+
export type { CompactionResult } from "./session/compaction/index.js";
|
|
7
|
+
export { createEventBus, type EventBus, type EventBusController, } from "./runtime/event-bus.js";
|
|
8
8
|
export { type AgentEndEvent, type AgentStartEvent, type AgentToolResult, type AgentToolUpdateCallback, type BeforeAgentStartEvent, type ContextEvent, discoverAndLoadExtensions, type ExecOptions, type ExecResult, type Extension, type ExtensionAPI, type ExtensionCommandContext, type ExtensionContext, type ExtensionError, type ExtensionEvent, type ExtensionFactory, type ExtensionFlag, type ExtensionHandler, ExtensionRunner, type ExtensionShortcut, type ExtensionUIContext, type LoadExtensionsResult, type MessageRenderer, type RegisteredCommand, type SessionBeforeCompactEvent, type SessionBeforeForkEvent, type SessionBeforeSwitchEvent, type SessionBeforeTreeEvent, type SessionCompactEvent, type SessionForkEvent, type SessionShutdownEvent, type SessionStartEvent, type SessionSwitchEvent, type SessionTreeEvent, type ToolCallEvent, type ToolDefinition, type ToolRenderResultOptions, type ToolResultEvent, type TurnEndEvent, type TurnStartEvent, wrapToolsWithExtensions, } from "./extensions/index.js";
|
|
9
9
|
export { MCPManager } from "./mcp-manager.js";
|
|
10
10
|
export type { MCPServerConfig, MCPTool, MCPToolResult, } from "./mcp/mcp-client.js";
|
package/dist/core/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Core modules shared between all run modes.
|
|
3
3
|
*/
|
|
4
|
-
export { AgentSession, } from "./agent-session.js";
|
|
4
|
+
export { AgentSession, } from "./runtime/agent-session.js";
|
|
5
5
|
export { executeBash, executeBashWithOperations, } from "./bash-executor.js";
|
|
6
|
-
export { createEventBus, } from "./event-bus.js";
|
|
6
|
+
export { createEventBus, } from "./runtime/event-bus.js";
|
|
7
7
|
// Extensions system
|
|
8
8
|
export { discoverAndLoadExtensions, ExtensionRunner, wrapToolsWithExtensions, } from "./extensions/index.js";
|
|
9
9
|
// MCP (Model Context Protocol) support
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model Switcher
|
|
3
|
+
*
|
|
4
|
+
* Encapsulates model selection, cycling, and fallback logic.
|
|
5
|
+
* This coordinator manages model switching operations.
|
|
6
|
+
*/
|
|
7
|
+
import type { Model } from "@pencil-agent/ai";
|
|
8
|
+
import type { ThinkingLevel } from "@pencil-agent/agent-core";
|
|
9
|
+
export interface ModelCycleResult {
|
|
10
|
+
model: Model<any>;
|
|
11
|
+
previousModel: Model<any> | undefined;
|
|
12
|
+
}
|
|
13
|
+
export interface ModelSwitcherOptions {
|
|
14
|
+
/** Get API key for a model */
|
|
15
|
+
getApiKey: (model: Model<any>) => Promise<string | undefined>;
|
|
16
|
+
/** Get API key for a provider */
|
|
17
|
+
getApiKeyForProvider: (provider: string) => Promise<string | undefined>;
|
|
18
|
+
/** Get all available models */
|
|
19
|
+
getAvailableModels: () => Model<any>[];
|
|
20
|
+
/** Set the model on the agent */
|
|
21
|
+
setModelOnAgent: (model: Model<any>) => void;
|
|
22
|
+
/** Get current model */
|
|
23
|
+
getCurrentModel: () => Model<any> | undefined;
|
|
24
|
+
/** Scoped models from --models flag */
|
|
25
|
+
scopedModels?: Array<{
|
|
26
|
+
model: Model<any>;
|
|
27
|
+
thinkingLevel: ThinkingLevel;
|
|
28
|
+
}>;
|
|
29
|
+
}
|
|
30
|
+
export declare class ModelSwitcher {
|
|
31
|
+
private options;
|
|
32
|
+
private _scopedModels;
|
|
33
|
+
constructor(options: ModelSwitcherOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Get current model
|
|
36
|
+
*/
|
|
37
|
+
getModel(): Model<any> | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a model has an API key
|
|
40
|
+
*/
|
|
41
|
+
hasApiKey(model: Model<any>): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Get models with API keys
|
|
44
|
+
*/
|
|
45
|
+
getModelsWithApiKey(): Promise<Model<any>[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Get scoped models with API keys
|
|
48
|
+
*/
|
|
49
|
+
getScopedModelsWithApiKey(): Promise<Array<{
|
|
50
|
+
model: Model<any>;
|
|
51
|
+
thinkingLevel: ThinkingLevel;
|
|
52
|
+
}>>;
|
|
53
|
+
/**
|
|
54
|
+
* Cycle to next/previous model
|
|
55
|
+
*/
|
|
56
|
+
cycleModel(direction?: "forward" | "backward"): Promise<ModelCycleResult | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* Cycle through scoped models
|
|
59
|
+
*/
|
|
60
|
+
private cycleScopedModel;
|
|
61
|
+
/**
|
|
62
|
+
* Cycle through all available models
|
|
63
|
+
*/
|
|
64
|
+
private cycleAvailableModel;
|
|
65
|
+
/**
|
|
66
|
+
* Set scoped models
|
|
67
|
+
*/
|
|
68
|
+
setScopedModels(models: Array<{
|
|
69
|
+
model: Model<any>;
|
|
70
|
+
thinkingLevel: ThinkingLevel;
|
|
71
|
+
}>): void;
|
|
72
|
+
/**
|
|
73
|
+
* Get scoped models
|
|
74
|
+
*/
|
|
75
|
+
getScopedModels(): Array<{
|
|
76
|
+
model: Model<any>;
|
|
77
|
+
thinkingLevel: ThinkingLevel;
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=switcher.d.ts.map
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model Switcher
|
|
3
|
+
*
|
|
4
|
+
* Encapsulates model selection, cycling, and fallback logic.
|
|
5
|
+
* This coordinator manages model switching operations.
|
|
6
|
+
*/
|
|
7
|
+
import { modelsAreEqual } from "@pencil-agent/ai";
|
|
8
|
+
export class ModelSwitcher {
|
|
9
|
+
options;
|
|
10
|
+
_scopedModels;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this._scopedModels = options.scopedModels || [];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Get current model
|
|
17
|
+
*/
|
|
18
|
+
getModel() {
|
|
19
|
+
return this.options.getCurrentModel();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Check if a model has an API key
|
|
23
|
+
*/
|
|
24
|
+
async hasApiKey(model) {
|
|
25
|
+
const apiKey = await this.options.getApiKey(model);
|
|
26
|
+
return !!apiKey;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get models with API keys
|
|
30
|
+
*/
|
|
31
|
+
async getModelsWithApiKey() {
|
|
32
|
+
const available = this.options.getAvailableModels();
|
|
33
|
+
const result = [];
|
|
34
|
+
for (const model of available) {
|
|
35
|
+
if (await this.hasApiKey(model)) {
|
|
36
|
+
result.push(model);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get scoped models with API keys
|
|
43
|
+
*/
|
|
44
|
+
async getScopedModelsWithApiKey() {
|
|
45
|
+
const apiKeysByProvider = new Map();
|
|
46
|
+
const result = [];
|
|
47
|
+
for (const scoped of this._scopedModels) {
|
|
48
|
+
const provider = scoped.model.provider;
|
|
49
|
+
let apiKey;
|
|
50
|
+
if (apiKeysByProvider.has(provider)) {
|
|
51
|
+
apiKey = apiKeysByProvider.get(provider);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
apiKey = await this.options.getApiKeyForProvider(provider);
|
|
55
|
+
apiKeysByProvider.set(provider, apiKey);
|
|
56
|
+
}
|
|
57
|
+
if (apiKey) {
|
|
58
|
+
result.push(scoped);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Cycle to next/previous model
|
|
65
|
+
*/
|
|
66
|
+
async cycleModel(direction = "forward") {
|
|
67
|
+
if (this._scopedModels.length > 0) {
|
|
68
|
+
return this.cycleScopedModel(direction);
|
|
69
|
+
}
|
|
70
|
+
return this.cycleAvailableModel(direction);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Cycle through scoped models
|
|
74
|
+
*/
|
|
75
|
+
async cycleScopedModel(direction) {
|
|
76
|
+
const scopedModels = await this.getScopedModelsWithApiKey();
|
|
77
|
+
if (scopedModels.length <= 1)
|
|
78
|
+
return undefined;
|
|
79
|
+
const currentModel = this.getModel();
|
|
80
|
+
let currentIndex = scopedModels.findIndex((sm) => modelsAreEqual(sm.model, currentModel));
|
|
81
|
+
if (currentIndex === -1)
|
|
82
|
+
currentIndex = 0;
|
|
83
|
+
const len = scopedModels.length;
|
|
84
|
+
const nextIndex = direction === "forward"
|
|
85
|
+
? (currentIndex + 1) % len
|
|
86
|
+
: (currentIndex - 1 + len) % len;
|
|
87
|
+
const next = scopedModels[nextIndex];
|
|
88
|
+
const previousModel = currentModel;
|
|
89
|
+
// Set the new model
|
|
90
|
+
this.options.setModelOnAgent(next.model);
|
|
91
|
+
return {
|
|
92
|
+
model: next.model,
|
|
93
|
+
previousModel,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Cycle through all available models
|
|
98
|
+
*/
|
|
99
|
+
async cycleAvailableModel(direction) {
|
|
100
|
+
const available = await this.getModelsWithApiKey();
|
|
101
|
+
if (available.length <= 1)
|
|
102
|
+
return undefined;
|
|
103
|
+
const currentModel = this.getModel();
|
|
104
|
+
let currentIndex = available.findIndex((m) => modelsAreEqual(m, currentModel));
|
|
105
|
+
if (currentIndex === -1)
|
|
106
|
+
currentIndex = 0;
|
|
107
|
+
const len = available.length;
|
|
108
|
+
const nextIndex = direction === "forward"
|
|
109
|
+
? (currentIndex + 1) % len
|
|
110
|
+
: (currentIndex - 1 + len) % len;
|
|
111
|
+
const next = available[nextIndex];
|
|
112
|
+
const previousModel = currentModel;
|
|
113
|
+
// Set the new model
|
|
114
|
+
this.options.setModelOnAgent(next);
|
|
115
|
+
return {
|
|
116
|
+
model: next,
|
|
117
|
+
previousModel,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Set scoped models
|
|
122
|
+
*/
|
|
123
|
+
setScopedModels(models) {
|
|
124
|
+
this._scopedModels = models;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get scoped models
|
|
128
|
+
*/
|
|
129
|
+
getScopedModels() {
|
|
130
|
+
return this._scopedModels;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=switcher.js.map
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Model registry - manages built-in and custom models, provides API key resolution.
|
|
3
3
|
*/
|
|
4
4
|
import { type Api, type AssistantMessageEventStream, type Context, type Model, type OAuthProviderInterface, type SimpleStreamOptions } from "@pencil-agent/ai";
|
|
5
|
-
import type { AuthStorage } from "./auth-storage.js";
|
|
6
|
-
import { clearConfigValueCache } from "./resolve-config-value.js";
|
|
5
|
+
import type { AuthStorage } from "./config/auth-storage.js";
|
|
6
|
+
import { clearConfigValueCache } from "./config/resolve-config-value.js";
|
|
7
7
|
/** Clear the config value command cache. Exported for testing. */
|
|
8
8
|
export declare const clearApiKeyCache: typeof clearConfigValueCache;
|
|
9
9
|
/**
|
|
@@ -7,7 +7,7 @@ import AjvModule from "ajv";
|
|
|
7
7
|
import { existsSync, readFileSync } from "fs";
|
|
8
8
|
import { join } from "path";
|
|
9
9
|
import { getAgentDir } from "../config.js";
|
|
10
|
-
import { clearConfigValueCache, resolveConfigValue, resolveHeaders } from "./resolve-config-value.js";
|
|
10
|
+
import { clearConfigValueCache, resolveConfigValue, resolveHeaders } from "./config/resolve-config-value.js";
|
|
11
11
|
const Ajv = AjvModule.default || AjvModule;
|
|
12
12
|
// Schema for OpenRouter routing preferences
|
|
13
13
|
const OpenRouterRoutingSchema = Type.Object({
|