@zhin.js/gateway 0.0.1-bootstrap.0 → 1.1.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/LICENSE +21 -0
- package/README.md +61 -0
- package/lib/config/ai-config-loader.d.ts +22 -0
- package/lib/config/ai-config-loader.js +96 -0
- package/lib/config/configuration-chat-policy.d.ts +57 -0
- package/lib/config/configuration-chat-policy.js +423 -0
- package/lib/config/configuration-patch.d.ts +16 -0
- package/lib/config/configuration-patch.js +105 -0
- package/lib/config/configuration-path.d.ts +6 -0
- package/lib/config/configuration-path.js +40 -0
- package/lib/config/configuration-projection.d.ts +6 -0
- package/lib/config/configuration-projection.js +28 -0
- package/lib/config/configuration-redaction.d.ts +18 -0
- package/lib/config/configuration-redaction.js +75 -0
- package/lib/config/configuration.d.ts +81 -0
- package/lib/config/configuration.js +686 -0
- package/lib/config/console-schema.d.ts +11 -0
- package/lib/config/console-schema.js +223 -0
- package/lib/config/environment-files.d.ts +3 -0
- package/lib/config/environment-files.js +14 -0
- package/lib/config/literal-secrets.d.ts +34 -0
- package/lib/config/literal-secrets.js +136 -0
- package/lib/config/plugin-package-map.d.ts +2 -0
- package/lib/config/plugin-package-map.js +14 -0
- package/lib/config/plugin-schema-catalog.d.ts +22 -0
- package/lib/config/plugin-schema-catalog.js +262 -0
- package/lib/config.d.ts +13 -0
- package/lib/config.js +13 -0
- package/lib/database/config.d.ts +22 -0
- package/lib/database/config.js +159 -0
- package/lib/database/index.d.ts +2 -0
- package/lib/database/index.js +2 -0
- package/lib/database/project-database.d.ts +31 -0
- package/lib/database/project-database.js +130 -0
- package/lib/database.d.ts +2 -0
- package/lib/database.js +2 -0
- package/lib/http/console-rpc.d.ts +27 -0
- package/lib/http/console-rpc.js +51 -0
- package/lib/http/gateway-console.d.ts +63 -0
- package/lib/http/gateway-console.js +309 -0
- package/lib/http/gateway-events.d.ts +40 -0
- package/lib/http/gateway-events.js +185 -0
- package/lib/http/gateway-http-server.d.ts +95 -0
- package/lib/http/gateway-http-server.js +522 -0
- package/lib/http/local-principal.d.ts +2 -0
- package/lib/http/local-principal.js +16 -0
- package/lib/http/plugin-switch-rpc.d.ts +19 -0
- package/lib/http/plugin-switch-rpc.js +214 -0
- package/lib/http/run-gateway.d.ts +43 -0
- package/lib/http/run-gateway.js +268 -0
- package/lib/http/start-gateway.d.ts +114 -0
- package/lib/http/start-gateway.js +226 -0
- package/lib/http-response.d.ts +6 -0
- package/lib/http-response.js +25 -0
- package/lib/http.d.ts +6 -0
- package/lib/http.js +6 -0
- package/lib/iam/crypto.d.ts +12 -0
- package/lib/iam/crypto.js +58 -0
- package/lib/iam/index.d.ts +8 -0
- package/lib/iam/index.js +8 -0
- package/lib/iam/install.d.ts +36 -0
- package/lib/iam/install.js +74 -0
- package/lib/iam/local-admin.d.ts +27 -0
- package/lib/iam/local-admin.js +102 -0
- package/lib/iam/rate-limit.d.ts +15 -0
- package/lib/iam/rate-limit.js +52 -0
- package/lib/iam/routes.d.ts +23 -0
- package/lib/iam/routes.js +157 -0
- package/lib/iam/rpc.d.ts +14 -0
- package/lib/iam/rpc.js +100 -0
- package/lib/iam/service.d.ts +203 -0
- package/lib/iam/service.js +784 -0
- package/lib/iam/setup-service.d.ts +83 -0
- package/lib/iam/setup-service.js +206 -0
- package/lib/iam/tables.d.ts +20 -0
- package/lib/iam/tables.js +82 -0
- package/lib/iam.d.ts +2 -0
- package/lib/iam.js +2 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.js +22 -0
- package/lib/local/client.d.ts +71 -0
- package/lib/local/client.js +296 -0
- package/lib/local/handler.d.ts +30 -0
- package/lib/local/handler.js +86 -0
- package/lib/local/local-socket.d.ts +55 -0
- package/lib/local/local-socket.js +232 -0
- package/lib/local/paths.d.ts +34 -0
- package/lib/local/paths.js +58 -0
- package/lib/local/protocol.d.ts +140 -0
- package/lib/local/protocol.js +90 -0
- package/lib/local/state-file.d.ts +8 -0
- package/lib/local/state-file.js +45 -0
- package/lib/local.d.ts +10 -0
- package/lib/local.js +10 -0
- package/lib/onboarding/allowlist.d.ts +54 -0
- package/lib/onboarding/allowlist.js +106 -0
- package/lib/onboarding/errors.d.ts +7 -0
- package/lib/onboarding/errors.js +17 -0
- package/lib/onboarding/index.d.ts +6 -0
- package/lib/onboarding/index.js +6 -0
- package/lib/onboarding/install.d.ts +99 -0
- package/lib/onboarding/install.js +132 -0
- package/lib/onboarding/package-manager.d.ts +47 -0
- package/lib/onboarding/package-manager.js +179 -0
- package/lib/onboarding/service.d.ts +53 -0
- package/lib/onboarding/service.js +144 -0
- package/lib/onboarding/state-store.d.ts +26 -0
- package/lib/onboarding/state-store.js +97 -0
- package/lib/onboarding.d.ts +2 -0
- package/lib/onboarding.js +2 -0
- package/lib/packages/authorization.d.ts +24 -0
- package/lib/packages/authorization.js +128 -0
- package/lib/packages/errors.d.ts +11 -0
- package/lib/packages/errors.js +26 -0
- package/lib/packages/index.d.ts +5 -0
- package/lib/packages/index.js +5 -0
- package/lib/packages/rpc.d.ts +30 -0
- package/lib/packages/rpc.js +160 -0
- package/lib/packages/service.d.ts +139 -0
- package/lib/packages/service.js +504 -0
- package/lib/packages/snapshot.d.ts +14 -0
- package/lib/packages/snapshot.js +73 -0
- package/lib/packages.d.ts +2 -0
- package/lib/packages.js +2 -0
- package/lib/plugin-management/activity.d.ts +6 -0
- package/lib/plugin-management/activity.js +14 -0
- package/lib/plugin-management/config-explanation.d.ts +6 -0
- package/lib/plugin-management/config-explanation.js +113 -0
- package/lib/plugin-management/use-cases.d.ts +15 -0
- package/lib/plugin-management/use-cases.js +120 -0
- package/lib/plugin-management/workflow.d.ts +136 -0
- package/lib/plugin-management/workflow.js +878 -0
- package/lib/plugin-management.d.ts +5 -0
- package/lib/plugin-management.js +5 -0
- package/lib/project/config-file.d.ts +6 -0
- package/lib/project/config-file.js +58 -0
- package/lib/project/environment.d.ts +13 -0
- package/lib/project/environment.js +52 -0
- package/lib/project/instance-declarations.d.ts +55 -0
- package/lib/project/instance-declarations.js +542 -0
- package/lib/project/legacy-config.d.ts +25 -0
- package/lib/project/legacy-config.js +70 -0
- package/lib/project/node-requirements.d.ts +7 -0
- package/lib/project/node-requirements.js +20 -0
- package/lib/project/tsx-module-loader.d.ts +7 -0
- package/lib/project/tsx-module-loader.js +29 -0
- package/lib/project.d.ts +7 -0
- package/lib/project.js +7 -0
- package/lib/server.d.ts +11 -0
- package/lib/server.js +11 -0
- package/lib/supervisor/instance-logs.d.ts +39 -0
- package/lib/supervisor/instance-logs.js +152 -0
- package/lib/supervisor/instance-supervisor.d.ts +140 -0
- package/lib/supervisor/instance-supervisor.js +408 -0
- package/lib/supervisor/run-instance.d.ts +35 -0
- package/lib/supervisor/run-instance.js +161 -0
- package/lib/supervisor/terminal-state.d.ts +30 -0
- package/lib/supervisor/terminal-state.js +57 -0
- package/lib/supervisor.d.ts +4 -0
- package/lib/supervisor.js +4 -0
- package/lib/terminal/backends.d.ts +38 -0
- package/lib/terminal/backends.js +193 -0
- package/lib/terminal/config.d.ts +63 -0
- package/lib/terminal/config.js +237 -0
- package/lib/terminal/host.d.ts +26 -0
- package/lib/terminal/host.js +76 -0
- package/lib/terminal/index.d.ts +60 -0
- package/lib/terminal/index.js +135 -0
- package/lib/terminal/modules.d.ts +87 -0
- package/lib/terminal/modules.js +44 -0
- package/lib/terminal/service.d.ts +101 -0
- package/lib/terminal/service.js +455 -0
- package/lib/terminal/tickets.d.ts +46 -0
- package/lib/terminal/tickets.js +76 -0
- package/lib/terminal.d.ts +3 -0
- package/lib/terminal.js +3 -0
- package/lib/usage/aggregate.d.ts +87 -0
- package/lib/usage/aggregate.js +212 -0
- package/lib/usage.d.ts +2 -0
- package/lib/usage.js +2 -0
- package/package.json +117 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 凉菜
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# @zhin.js/gateway
|
|
2
|
+
|
|
3
|
+
Zhin.js 管理网关([ADR 0054](../../../docs/adr/0054-management-gateway.md))。网关负责宿主层面的事:Console 账号与权限、终端、
|
|
4
|
+
项目配置与 `.env`、依赖事务、实例监管。它**不依赖 `zhin.js` 与 IM 层**,不加载插件代码(`pnpm check:architecture` 强制)。
|
|
5
|
+
|
|
6
|
+
网关进程(`zhin start`;前台运行用 `zhin start --foreground`)自己打开项目数据库并持有 `console_*` 表:登录、初始化、`iam.*`、`onboarding.update`、终端与审计
|
|
7
|
+
都在网关里,没有实例时 Console 照样可用;受管实例只按转发的主体做授权。`--standalone`(开发 / 测试,无网关)的实例仍在进程内
|
|
8
|
+
装配 `installConsoleIam` / `installHostConsoleTerminal`。
|
|
9
|
+
|
|
10
|
+
## 使用
|
|
11
|
+
|
|
12
|
+
项目不直接调用本包:`@zhin.js/cli` 的 `zhin start` 启动网关,网关再拉起项目里的 `zhin.js/instance`。新项目由 `create-zhin-app` 生成;已有项目与同批的 `zhin.js`、`@zhin.js/cli` 一起安装(网关与实例之间的控制通道带协议版本,必须一起升级):
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add zhin.js@latest @zhin.js/gateway@latest
|
|
16
|
+
pnpm add -D @zhin.js/cli@latest # 生产安装别用 --prod 剔除它:zhin start 需要 CLI
|
|
17
|
+
pnpm dev # zhin start --foreground --watch source config
|
|
18
|
+
zhin start --mode production --no-watch # 后台;zhin status / logs -f / restart / stop
|
|
19
|
+
zhin start --foreground --mode production --no-watch # systemd / Docker / PM2(pnpm start)
|
|
20
|
+
zhin console init # 首次创建 Console 所有者;之后 http.token 失效
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
运行时文件:`.zhin/gateway.sock`(本地控制套接字,`0600`)、`.zhin/gateway.json`、`.zhin/logs/instance.log`、`.zhin/logs/gateway.log`。
|
|
24
|
+
命令与退出码见 [zhin start](../../../docs/cli/runtime.md),升级步骤见[升级到管理网关](../../../docs/migrations/management-gateway.md)。
|
|
25
|
+
|
|
26
|
+
## 子路径
|
|
27
|
+
|
|
28
|
+
| 子路径 | 内容 |
|
|
29
|
+
|---|---|
|
|
30
|
+
| `@zhin.js/gateway/iam` | Console 账号、角色、会话、API 令牌、审计与初始化(ADR 0049):`ConsoleIamService`、`ConsoleSetupService`、`installConsoleIam`、`registerConsoleAuthRoutes`、`dispatchIamRpc` |
|
|
31
|
+
| `@zhin.js/gateway/terminal` | Console 终端(ADR 0051):`TerminalService`、`installHostConsoleTerminal`、`parseTerminalConfig` |
|
|
32
|
+
| `@zhin.js/gateway/config` | 原始配置文件与 `.env` 读写:`ConsoleConfigurationStore`、`readEnvFile` / `writeEnvFile`、配置路径与补丁、Console Schema |
|
|
33
|
+
| `@zhin.js/gateway/plugin-management` | 项目级依赖事务:`PluginManagementWorkflow`、`PluginManagementActivity`、插件用例 |
|
|
34
|
+
| `@zhin.js/gateway/project` | 项目文件工具:`findConfigFile`、`loadRuntimeEnvironmentLayers`、`createTsxModuleLoader`、实例声明迁移、旧配置检测、Node 版本要求 |
|
|
35
|
+
| `@zhin.js/gateway/supervisor` | 实例进程监管 `InstanceSupervisor`:IPC 控制通道(`ready` / `shutdown` / `restart-request`)、崩溃重启与风暴保护、日志采集(环形缓冲 + `.zhin/logs/instance.log` 按大小轮转);`runSupervisedInstance` 供 `zhin start` 使用 |
|
|
36
|
+
| `@zhin.js/gateway/usage` | 用量统计聚合 |
|
|
37
|
+
| `@zhin.js/gateway/onboarding` | Console 引导(ADR 0053):`.zhin/onboarding.json` 状态(`OnboardingStateStore`、自动识别已配置步骤)、`OnboardingService`、安装白名单与 `authorizeInstall`、流式 `installPackages` |
|
|
38
|
+
| `@zhin.js/gateway/http` | HTML / SSE 响应工具(实例也导入,不加载 Node HTTP 栈) |
|
|
39
|
+
| `@zhin.js/gateway/server` | 网关 HTTP 服务器(`GatewayHttpServer`:自有路由、`{base}` 认证后转发、其他路由原样转发、实例离线 503 / 超时 504 / 其他 502;`useConsoleRpc` 登记更多网关 RPC)、`gateway.*` Console RPC 路由、`startGatewayConsole`(项目数据库、本地认证、`/auth/*`、`iam.*`、`onboarding.update`、初始化向导、终端、审计)、`registerGatewayEventRoutes`(合并 `{base}/events`、`{base}/gateway/packages/progress`)、`startGateway`(服务器 + Console + 实例监管 + `@zhin.js/host-http/bridge` 实例传输)、`runGateway`(`zhin start` 的监管 + 网关入口) |
|
|
40
|
+
| `@zhin.js/gateway/database` | 网关自己的项目数据库连接(ADR 0054 §7):`loadProjectDatabaseConfig` / `resolveProjectDatabaseConfig`(与实例 `resolveDatabaseConfig` 同一规则)、`ProjectDatabase`(SQLite WAL;不用同步的 `busy_timeout`,`SQLITE_BUSY` 以异步退避重试,默认 5 秒预算,超时返回 `503 database_busy` 与 `Retry-After`) |
|
|
41
|
+
| `@zhin.js/gateway` | 以上全部 |
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { ConsoleIamService } from '@zhin.js/gateway/iam';
|
|
45
|
+
import { findConfigFile } from '@zhin.js/gateway/project';
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
只导入需要的子路径:根入口会加载全部区域(数据库驱动定义、终端、包管理器调用等)。
|
|
49
|
+
|
|
50
|
+
## 依赖边界
|
|
51
|
+
|
|
52
|
+
依赖 `@zhin.js/host-http`、`@zhin.js/database`、`@zhin.js/console-protocol`、`@zhin.js/runtime`(仅配置与项目文件工具)、
|
|
53
|
+
`@zhin.js/plugin-runtime`(契约)与 `@zhin.js/scaffold-wizard`。需要 IM 类型时在本包内定义窄接口,不导入 IM 包。
|
|
54
|
+
终端后端 `node-pty` / `ssh2` 是可选依赖,首次使用时才加载。
|
|
55
|
+
|
|
56
|
+
## 开发
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pnpm --filter @zhin.js/gateway build
|
|
60
|
+
pnpm vitest run packages/host/gateway
|
|
61
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type AiConfigUtils = {
|
|
2
|
+
/** Startup AI consumer validation and retired-authority rejection; no execution or credential IO. */
|
|
3
|
+
validateModelRuntimeConfig?: (ai: unknown, hostOwnerId: string) => string[];
|
|
4
|
+
applyAiConfigFixes: (ai: unknown) => {
|
|
5
|
+
ai: Record<string, unknown> | undefined;
|
|
6
|
+
fixes: string[];
|
|
7
|
+
};
|
|
8
|
+
/** Retired `ai.agent.*` switches (#735); absent on older agent packages. */
|
|
9
|
+
findDeprecatedAgentConfigKeys?: (agent: unknown) => Array<{
|
|
10
|
+
key: string;
|
|
11
|
+
reason: string;
|
|
12
|
+
}>;
|
|
13
|
+
findDeprecatedAgentBindingKeys?: (agents: unknown) => Array<{
|
|
14
|
+
agent: string;
|
|
15
|
+
key: string;
|
|
16
|
+
reason: string;
|
|
17
|
+
}>;
|
|
18
|
+
/** `ai.multimodal.perception` validation; absent on older agent packages. */
|
|
19
|
+
resolvePerceptionConfig?: (raw: unknown, providerAliases?: readonly string[]) => unknown;
|
|
20
|
+
};
|
|
21
|
+
/** 从项目 cwd 向上解析;monorepo 开发时回退到仓库根与源码 import。 */
|
|
22
|
+
export declare function loadAiConfigUtils(cwd: string): AiConfigUtils | null;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
const MODULE_ID = '@zhin.js/agent/config';
|
|
6
|
+
const requireFromCli = createRequire(import.meta.url);
|
|
7
|
+
async function loadMonorepoDevUtils() {
|
|
8
|
+
try {
|
|
9
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const repoRoot = path.resolve(here, '../../../../../');
|
|
11
|
+
const srcPath = path.join(repoRoot, 'packages/im/agent/src/config/index.ts');
|
|
12
|
+
if (!fs.existsSync(srcPath))
|
|
13
|
+
return null;
|
|
14
|
+
return (await import(pathToFileURL(srcPath).href));
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Vitest 下 createRequire 会因 development 条件解析到 .ts;用 ESM import 预热 monorepo 源码。 */
|
|
21
|
+
const monorepoDevUtils = await loadMonorepoDevUtils();
|
|
22
|
+
function loadFromAgentPackageJson(agentPkgJson) {
|
|
23
|
+
const agentDir = path.dirname(agentPkgJson);
|
|
24
|
+
if (monorepoDevUtils && fs.existsSync(path.join(agentDir, 'src/config/index.ts'))) {
|
|
25
|
+
return monorepoDevUtils;
|
|
26
|
+
}
|
|
27
|
+
const libPath = path.join(agentDir, 'lib/config/index.js');
|
|
28
|
+
if (!fs.existsSync(libPath))
|
|
29
|
+
return null;
|
|
30
|
+
try {
|
|
31
|
+
return requireFromCli(libPath);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function findAgentPackageJson(dir) {
|
|
38
|
+
const candidates = [
|
|
39
|
+
path.join(dir, 'node_modules/@zhin.js/agent/package.json'),
|
|
40
|
+
path.join(dir, 'node_modules/zhin.js/node_modules/@zhin.js/agent/package.json'),
|
|
41
|
+
path.join(dir, 'packages/im/agent/package.json'),
|
|
42
|
+
];
|
|
43
|
+
for (const candidate of candidates) {
|
|
44
|
+
if (fs.existsSync(candidate))
|
|
45
|
+
return candidate;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function tryRequireFromProject(pkgJson) {
|
|
50
|
+
try {
|
|
51
|
+
const req = createRequire(pkgJson);
|
|
52
|
+
return req(MODULE_ID);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
const dir = path.dirname(pkgJson);
|
|
56
|
+
const agentPkg = findAgentPackageJson(dir);
|
|
57
|
+
if (agentPkg)
|
|
58
|
+
return loadFromAgentPackageJson(agentPkg);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function walkUpForAgentConfig(startDir, maxDepth = 14) {
|
|
63
|
+
let dir = path.resolve(startDir);
|
|
64
|
+
for (let i = 0; i < maxDepth; i++) {
|
|
65
|
+
const pkgJson = path.join(dir, 'package.json');
|
|
66
|
+
if (fs.existsSync(pkgJson)) {
|
|
67
|
+
const loaded = tryRequireFromProject(pkgJson);
|
|
68
|
+
if (loaded)
|
|
69
|
+
return loaded;
|
|
70
|
+
}
|
|
71
|
+
const parent = path.dirname(dir);
|
|
72
|
+
if (parent === dir)
|
|
73
|
+
break;
|
|
74
|
+
dir = parent;
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
/** 从项目 cwd 向上解析;monorepo 开发时回退到仓库根与源码 import。 */
|
|
79
|
+
export function loadAiConfigUtils(cwd) {
|
|
80
|
+
const fromProject = walkUpForAgentConfig(cwd);
|
|
81
|
+
if (fromProject)
|
|
82
|
+
return fromProject;
|
|
83
|
+
if (monorepoDevUtils)
|
|
84
|
+
return monorepoDevUtils;
|
|
85
|
+
try {
|
|
86
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
87
|
+
const repoRoot = path.resolve(here, '../../../../../');
|
|
88
|
+
const fromRepo = walkUpForAgentConfig(repoRoot, 6);
|
|
89
|
+
if (fromRepo)
|
|
90
|
+
return fromRepo;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
/* ignore */
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fixed reason codes; a denial never carries a stored value or schema detail.
|
|
3
|
+
* - `permission`: role/approval fields (C4), never manageable from chat.
|
|
4
|
+
* - `scope`: outside the chat scope (C3: Host keys beyond `HOST_CONFIG_PATCH_PATHS`, declarations).
|
|
5
|
+
* - `leaf`: not a single scalar / scalar-array leaf (no object overwrite or deletion).
|
|
6
|
+
* - `required`: a required leaf cannot be unset.
|
|
7
|
+
*/
|
|
8
|
+
export type ConfigChatAccessKind = 'sensitive' | 'unknown' | 'missing' | 'permission' | 'scope' | 'leaf' | 'required';
|
|
9
|
+
export declare class ConfigChatAccessError extends Error {
|
|
10
|
+
readonly kind: ConfigChatAccessKind;
|
|
11
|
+
constructor(kind: ConfigChatAccessKind);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Role, approval and allow/deny-list vocabulary (C4): `master`, `trusted`,
|
|
15
|
+
* `endpoints[*].master/trusted`, `ai.trigger.masters/trusted`, `toolHardGate.*`, and
|
|
16
|
+
* compound names such as `adminIds`, `ownerId`, `masterId`, `trustedUsers`,
|
|
17
|
+
* `superUsers`, `whitelist`, `allowlist`, `blacklist` or `bypassMasters`.
|
|
18
|
+
* Matched by contains/prefix/suffix after normalization (like `sensitiveConfigKey`) so
|
|
19
|
+
* casing, separators or compounds cannot bypass it. Deliberately fail-closed: an
|
|
20
|
+
* ordinary key that merely contains a role word (e.g. `masterVolume`) is CLI-only.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isChatPermissionSegment(segment: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Indirect role binding (M2): `endpoints[*].id|name` is the key that binds leaf
|
|
25
|
+
* `master`/`trusted` to an account, so remapping it from chat is a permission change.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isChatIdentityRemapPath(path: readonly string[]): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* M1/B1: a chat `set` never plants a new `${…}` or `env://` reference (even with a
|
|
30
|
+
* non-credential-looking name), since a later `get` or runtime echo would expand it.
|
|
31
|
+
* Existing references are untouched by unrelated operations.
|
|
32
|
+
*/
|
|
33
|
+
export declare function assertChatValueHasNoEnvironmentReference(value: unknown, depth?: number): void;
|
|
34
|
+
export interface ChatConfigurationScope {
|
|
35
|
+
/** Host-owned top-level keys (Runtime `HOST_CONFIG_KEYS`). */
|
|
36
|
+
readonly hostKeys: readonly string[];
|
|
37
|
+
/** Host chat whitelist; v1 is exactly `HOST_CONFIG_PATCH_PATHS`. */
|
|
38
|
+
readonly hostPaths: readonly (readonly string[])[];
|
|
39
|
+
/** Declaration keys owned by source/plugin management. */
|
|
40
|
+
readonly declarationKeys: ReadonlySet<string>;
|
|
41
|
+
}
|
|
42
|
+
/** C3/C4 scope gate, checked before any schema proof or value read. */
|
|
43
|
+
export declare function assertChatConfigurationScope(path: readonly string[], scope: ChatConfigurationScope): void;
|
|
44
|
+
/** Whitelisted Host roots reachable from a (possibly shorter) `fields` scope. */
|
|
45
|
+
export declare function chatHostRoots(prefix: readonly string[], scope: ChatConfigurationScope): readonly (readonly string[])[];
|
|
46
|
+
/** Raw JSON Schema only. Unknown branches never confer access. No values are expanded. */
|
|
47
|
+
export declare function assertChatConfigurationAccess(schema: unknown, path: readonly string[], values: readonly unknown[], deep?: boolean, absolutePath?: readonly string[]): void;
|
|
48
|
+
/**
|
|
49
|
+
* Leaf-only chat writes: the target must resolve to scalars or arrays of scalars in
|
|
50
|
+
* every schema branch. Object targets would let a parent overwrite/removal bypass
|
|
51
|
+
* per-child checks, so they are rejected as `leaf`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function chatLeafTypes(schema: unknown, path: readonly string[], values?: readonly unknown[]): readonly string[];
|
|
54
|
+
/** `unset` removes only optional leaves; any branch listing the key as required refuses. */
|
|
55
|
+
export declare function assertChatOptionalLeaf(schema: unknown, path: readonly string[]): void;
|
|
56
|
+
/** Declared leaf paths below `path` (properties only), each still re-checked by the store. */
|
|
57
|
+
export declare function chatConfigurationLeaves(schema: unknown, path: readonly string[], limit?: number): readonly (readonly string[])[];
|