@downcity/agent 1.1.6 → 1.1.8
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/bin/agent/AgentContext.d.ts.map +1 -1
- package/bin/agent/AgentContext.js +1 -0
- package/bin/agent/AgentContext.js.map +1 -1
- package/bin/agent/AgentContextTypes.d.ts +5 -1
- package/bin/agent/AgentContextTypes.d.ts.map +1 -1
- package/bin/agent/AgentRuntime.d.ts +6 -1
- package/bin/agent/AgentRuntime.d.ts.map +1 -1
- package/bin/agent/AgentRuntime.js +22 -3
- package/bin/agent/AgentRuntime.js.map +1 -1
- package/bin/agent/AgentRuntimeState.d.ts.map +1 -1
- package/bin/agent/AgentRuntimeState.js +58 -2
- package/bin/agent/AgentRuntimeState.js.map +1 -1
- package/bin/agent/AgentRuntimeTypes.d.ts +5 -1
- package/bin/agent/AgentRuntimeTypes.d.ts.map +1 -1
- package/bin/agent/project/AgentInitializer.d.ts +3 -2
- package/bin/agent/project/AgentInitializer.d.ts.map +1 -1
- package/bin/agent/project/AgentInitializer.js +34 -44
- package/bin/agent/project/AgentInitializer.js.map +1 -1
- package/bin/config/Config.d.ts.map +1 -1
- package/bin/config/Config.js +2 -21
- package/bin/config/Config.js.map +1 -1
- package/bin/config/Paths.d.ts +1 -5
- package/bin/config/Paths.d.ts.map +1 -1
- package/bin/config/Paths.js +2 -8
- package/bin/config/Paths.js.map +1 -1
- package/bin/host/daemon/ProjectSetup.d.ts +2 -1
- package/bin/host/daemon/ProjectSetup.d.ts.map +1 -1
- package/bin/host/daemon/ProjectSetup.js +14 -21
- package/bin/host/daemon/ProjectSetup.js.map +1 -1
- package/bin/host/runtime/AgentHostRuntime.d.ts.map +1 -1
- package/bin/host/runtime/AgentHostRuntime.js +1 -2
- package/bin/host/runtime/AgentHostRuntime.js.map +1 -1
- package/bin/host/runtime/CityPaths.d.ts +0 -3
- package/bin/host/runtime/CityPaths.d.ts.map +1 -1
- package/bin/host/runtime/CityPaths.js +0 -3
- package/bin/host/runtime/CityPaths.js.map +1 -1
- package/bin/host/sdk/Agent.d.ts +4 -0
- package/bin/host/sdk/Agent.d.ts.map +1 -1
- package/bin/host/sdk/Agent.js +71 -2
- package/bin/host/sdk/Agent.js.map +1 -1
- package/bin/host/sdk/AgentSdkTypes.d.ts +9 -0
- package/bin/host/sdk/AgentSdkTypes.d.ts.map +1 -1
- package/bin/host/sdk/SdkSessionSystemComposer.d.ts +11 -2
- package/bin/host/sdk/SdkSessionSystemComposer.d.ts.map +1 -1
- package/bin/host/sdk/SdkSessionSystemComposer.js +19 -2
- package/bin/host/sdk/SdkSessionSystemComposer.js.map +1 -1
- package/bin/host/sdk/Session.d.ts +10 -0
- package/bin/host/sdk/Session.d.ts.map +1 -1
- package/bin/host/sdk/Session.js +8 -0
- package/bin/host/sdk/Session.js.map +1 -1
- package/bin/http/Server.d.ts.map +1 -1
- package/bin/http/Server.js +1 -11
- package/bin/http/Server.js.map +1 -1
- package/bin/http/auth/AuthEnv.d.ts +0 -9
- package/bin/http/auth/AuthEnv.d.ts.map +1 -1
- package/bin/http/auth/AuthEnv.js +0 -9
- package/bin/http/auth/AuthEnv.js.map +1 -1
- package/bin/http/auth/CliAuthStateStore.d.ts +0 -4
- package/bin/http/auth/CliAuthStateStore.d.ts.map +1 -1
- package/bin/http/auth/CliAuthStateStore.js +0 -4
- package/bin/http/auth/CliAuthStateStore.js.map +1 -1
- package/bin/http/control/ModelRoutes.d.ts.map +1 -1
- package/bin/http/control/ModelRoutes.js +3 -8
- package/bin/http/control/ModelRoutes.js.map +1 -1
- package/bin/index.d.ts +2 -6
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +2 -6
- package/bin/index.js.map +1 -1
- package/bin/model/CreateModel.d.ts +2 -2
- package/bin/model/CreateModel.d.ts.map +1 -1
- package/bin/model/CreateModel.js +13 -12
- package/bin/model/CreateModel.js.map +1 -1
- package/bin/plugin/Activation.d.ts +4 -0
- package/bin/plugin/Activation.d.ts.map +1 -1
- package/bin/plugin/Activation.js +2 -2
- package/bin/plugin/Activation.js.map +1 -1
- package/bin/plugin/LocalExecution.d.ts.map +1 -1
- package/bin/plugin/LocalExecution.js +23 -2
- package/bin/plugin/LocalExecution.js.map +1 -1
- package/bin/plugin/PluginRegistry.js +2 -2
- package/bin/plugin/PluginRegistry.js.map +1 -1
- package/bin/plugins/asr/Plugin.d.ts.map +1 -1
- package/bin/plugins/asr/Plugin.js +4 -5
- package/bin/plugins/asr/Plugin.js.map +1 -1
- package/bin/plugins/auth/Plugin.d.ts.map +1 -1
- package/bin/plugins/auth/Plugin.js +1 -0
- package/bin/plugins/auth/Plugin.js.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationConfig.d.ts +4 -4
- package/bin/plugins/auth/runtime/AuthorizationConfig.d.ts.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationConfig.js +28 -26
- package/bin/plugins/auth/runtime/AuthorizationConfig.js.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationPolicy.d.ts +2 -0
- package/bin/plugins/auth/runtime/AuthorizationPolicy.d.ts.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationPolicy.js +3 -2
- package/bin/plugins/auth/runtime/AuthorizationPolicy.js.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationStore.d.ts +1 -1
- package/bin/plugins/auth/runtime/AuthorizationStore.d.ts.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationStore.js +3 -4
- package/bin/plugins/auth/runtime/AuthorizationStore.js.map +1 -1
- package/bin/plugins/skill/Plugin.js +2 -2
- package/bin/plugins/skill/Plugin.js.map +1 -1
- package/bin/plugins/tts/Plugin.d.ts.map +1 -1
- package/bin/plugins/tts/Plugin.js +4 -5
- package/bin/plugins/tts/Plugin.js.map +1 -1
- package/bin/plugins/web/Plugin.d.ts.map +1 -1
- package/bin/plugins/web/Plugin.js +4 -5
- package/bin/plugins/web/Plugin.js.map +1 -1
- package/bin/plugins/workboard/Plugin.js +2 -2
- package/bin/plugins/workboard/Plugin.js.map +1 -1
- package/bin/service/builtins/chat/accounts/ChannelAccountService.d.ts +4 -1
- package/bin/service/builtins/chat/accounts/ChannelAccountService.d.ts.map +1 -1
- package/bin/service/builtins/chat/accounts/ChannelAccountService.js +64 -91
- package/bin/service/builtins/chat/accounts/ChannelAccountService.js.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelActions.d.ts.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelActions.js +11 -18
- package/bin/service/builtins/chat/runtime/ChatChannelActions.js.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelCore.d.ts +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelCore.d.ts.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelCore.js +9 -17
- package/bin/service/builtins/chat/runtime/ChatChannelCore.js.map +1 -1
- package/bin/service/builtins/memory/Action.d.ts +1 -5
- package/bin/service/builtins/memory/Action.d.ts.map +1 -1
- package/bin/service/builtins/memory/Action.js +4 -42
- package/bin/service/builtins/memory/Action.js.map +1 -1
- package/bin/service/builtins/memory/MemoryService.d.ts.map +1 -1
- package/bin/service/builtins/memory/MemoryService.js +2 -32
- package/bin/service/builtins/memory/MemoryService.js.map +1 -1
- package/bin/service/builtins/memory/runtime/Search.d.ts +7 -3
- package/bin/service/builtins/memory/runtime/Search.d.ts.map +1 -1
- package/bin/service/builtins/memory/runtime/Search.js +220 -16
- package/bin/service/builtins/memory/runtime/Search.js.map +1 -1
- package/bin/service/builtins/memory/runtime/Store.d.ts +9 -50
- package/bin/service/builtins/memory/runtime/Store.d.ts.map +1 -1
- package/bin/service/builtins/memory/runtime/Store.js +10 -130
- package/bin/service/builtins/memory/runtime/Store.js.map +1 -1
- package/bin/service/builtins/memory/runtime/Writer.d.ts.map +1 -1
- package/bin/service/builtins/memory/runtime/Writer.js +1 -2
- package/bin/service/builtins/memory/runtime/Writer.js.map +1 -1
- package/bin/service/builtins/memory/types/Memory.d.ts +3 -57
- package/bin/service/builtins/memory/types/Memory.d.ts.map +1 -1
- package/bin/service/schedule/Store.d.ts +22 -25
- package/bin/service/schedule/Store.d.ts.map +1 -1
- package/bin/service/schedule/Store.js +172 -154
- package/bin/service/schedule/Store.js.map +1 -1
- package/bin/session/composer/system/default/SystemDomain.d.ts.map +1 -1
- package/bin/session/composer/system/default/SystemDomain.js +1 -0
- package/bin/session/composer/system/default/SystemDomain.js.map +1 -1
- package/bin/shared/types/AgentHost.d.ts +120 -4
- package/bin/shared/types/AgentHost.d.ts.map +1 -1
- package/bin/shared/types/Plugin.d.ts +5 -1
- package/bin/shared/types/Plugin.d.ts.map +1 -1
- package/package.json +1 -4
- package/src/agent/AgentContext.ts +1 -0
- package/src/agent/AgentContextTypes.ts +5 -0
- package/src/agent/AgentRuntime.ts +32 -3
- package/src/agent/AgentRuntimeState.ts +66 -2
- package/src/agent/AgentRuntimeTypes.ts +5 -0
- package/src/agent/project/AgentInitializer.ts +40 -42
- package/src/config/Config.ts +2 -17
- package/src/config/Paths.ts +2 -9
- package/src/host/daemon/ProjectSetup.ts +19 -21
- package/src/host/runtime/AgentHostRuntime.ts +0 -2
- package/src/host/runtime/CityPaths.ts +0 -3
- package/src/host/sdk/Agent.ts +67 -2
- package/src/host/sdk/AgentSdkTypes.ts +10 -0
- package/src/host/sdk/SdkSessionSystemComposer.ts +39 -2
- package/src/host/sdk/Session.ts +18 -0
- package/src/http/Server.ts +0 -13
- package/src/http/auth/AuthEnv.ts +0 -9
- package/src/http/auth/CliAuthStateStore.ts +0 -4
- package/src/http/control/ModelRoutes.ts +3 -9
- package/src/index.ts +2 -12
- package/src/model/CreateModel.ts +15 -13
- package/src/plugin/Activation.ts +6 -2
- package/src/plugin/LocalExecution.ts +24 -2
- package/src/plugin/PluginRegistry.ts +2 -2
- package/src/plugins/asr/Plugin.ts +4 -5
- package/src/plugins/auth/Plugin.ts +1 -0
- package/src/plugins/auth/runtime/AuthorizationConfig.ts +47 -37
- package/src/plugins/auth/runtime/AuthorizationPolicy.ts +5 -2
- package/src/plugins/auth/runtime/AuthorizationStore.ts +6 -5
- package/src/plugins/skill/Plugin.ts +2 -2
- package/src/plugins/tts/Plugin.ts +4 -5
- package/src/plugins/web/Plugin.ts +4 -5
- package/src/plugins/workboard/Plugin.ts +2 -2
- package/src/service/builtins/chat/accounts/ChannelAccountService.ts +42 -62
- package/src/service/builtins/chat/runtime/ChatChannelActions.ts +12 -18
- package/src/service/builtins/chat/runtime/ChatChannelCore.ts +9 -14
- package/src/service/builtins/memory/Action.ts +6 -47
- package/src/service/builtins/memory/MemoryService.ts +1 -33
- package/src/service/builtins/memory/runtime/Search.ts +256 -16
- package/src/service/builtins/memory/runtime/Store.ts +13 -185
- package/src/service/builtins/memory/runtime/Writer.ts +1 -2
- package/src/service/builtins/memory/types/Memory.ts +2 -59
- package/src/service/schedule/Store.ts +215 -175
- package/src/session/composer/system/default/SystemDomain.ts +1 -0
- package/src/shared/types/AgentHost.ts +138 -4
- package/src/shared/types/Plugin.ts +5 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/http/auth/AuthMiddleware.d.ts +0 -36
- package/bin/http/auth/AuthMiddleware.d.ts.map +0 -1
- package/bin/http/auth/AuthMiddleware.js +0 -37
- package/bin/http/auth/AuthMiddleware.js.map +0 -1
- package/bin/http/auth/AuthRoutes.d.ts +0 -17
- package/bin/http/auth/AuthRoutes.d.ts.map +0 -1
- package/bin/http/auth/AuthRoutes.js +0 -78
- package/bin/http/auth/AuthRoutes.js.map +0 -1
- package/bin/http/auth/AuthService.d.ts +0 -119
- package/bin/http/auth/AuthService.d.ts.map +0 -1
- package/bin/http/auth/AuthService.js +0 -307
- package/bin/http/auth/AuthService.js.map +0 -1
- package/bin/http/auth/AuthStore.d.ts +0 -165
- package/bin/http/auth/AuthStore.d.ts.map +0 -1
- package/bin/http/auth/AuthStore.js +0 -442
- package/bin/http/auth/AuthStore.js.map +0 -1
- package/bin/http/auth/RoutePolicy.d.ts +0 -30
- package/bin/http/auth/RoutePolicy.d.ts.map +0 -1
- package/bin/http/auth/RoutePolicy.js +0 -229
- package/bin/http/auth/RoutePolicy.js.map +0 -1
- package/bin/plugin/Lifecycle.d.ts +0 -33
- package/bin/plugin/Lifecycle.d.ts.map +0 -1
- package/bin/plugin/Lifecycle.js +0 -102
- package/bin/plugin/Lifecycle.js.map +0 -1
- package/bin/service/builtins/memory/runtime/Indexer.d.ts +0 -71
- package/bin/service/builtins/memory/runtime/Indexer.d.ts.map +0 -1
- package/bin/service/builtins/memory/runtime/Indexer.js +0 -345
- package/bin/service/builtins/memory/runtime/Indexer.js.map +0 -1
- package/bin/service/schedule/Schema.d.ts +0 -171
- package/bin/service/schedule/Schema.d.ts.map +0 -1
- package/bin/service/schedule/Schema.js +0 -26
- package/bin/service/schedule/Schema.js.map +0 -1
- package/bin/shared/utils/store/StoreChannelAccountRepository.d.ts +0 -34
- package/bin/shared/utils/store/StoreChannelAccountRepository.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreChannelAccountRepository.js +0 -198
- package/bin/shared/utils/store/StoreChannelAccountRepository.js.map +0 -1
- package/bin/shared/utils/store/StoreEnvRepository.d.ts +0 -98
- package/bin/shared/utils/store/StoreEnvRepository.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreEnvRepository.js +0 -334
- package/bin/shared/utils/store/StoreEnvRepository.js.map +0 -1
- package/bin/shared/utils/store/StoreModelRepository.d.ts +0 -61
- package/bin/shared/utils/store/StoreModelRepository.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreModelRepository.js +0 -278
- package/bin/shared/utils/store/StoreModelRepository.js.map +0 -1
- package/bin/shared/utils/store/StoreSchema.d.ts +0 -13
- package/bin/shared/utils/store/StoreSchema.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreSchema.js +0 -319
- package/bin/shared/utils/store/StoreSchema.js.map +0 -1
- package/bin/shared/utils/store/StoreSecureSettings.d.ts +0 -33
- package/bin/shared/utils/store/StoreSecureSettings.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreSecureSettings.js +0 -91
- package/bin/shared/utils/store/StoreSecureSettings.js.map +0 -1
- package/bin/shared/utils/store/StoreShared.d.ts +0 -44
- package/bin/shared/utils/store/StoreShared.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreShared.js +0 -40
- package/bin/shared/utils/store/StoreShared.js.map +0 -1
- package/bin/shared/utils/store/crypto.d.ts +0 -24
- package/bin/shared/utils/store/crypto.d.ts.map +0 -1
- package/bin/shared/utils/store/crypto.js +0 -101
- package/bin/shared/utils/store/crypto.js.map +0 -1
- package/bin/shared/utils/store/index.d.ts +0 -230
- package/bin/shared/utils/store/index.d.ts.map +0 -1
- package/bin/shared/utils/store/index.js +0 -360
- package/bin/shared/utils/store/index.js.map +0 -1
- package/bin/shared/utils/store/schema.d.ts +0 -690
- package/bin/shared/utils/store/schema.d.ts.map +0 -1
- package/bin/shared/utils/store/schema.js +0 -81
- package/bin/shared/utils/store/schema.js.map +0 -1
- package/src/http/auth/AuthMiddleware.ts +0 -61
- package/src/http/auth/AuthRoutes.ts +0 -100
- package/src/http/auth/AuthService.ts +0 -367
- package/src/http/auth/AuthStore.ts +0 -572
- package/src/http/auth/RoutePolicy.ts +0 -255
- package/src/plugin/Lifecycle.ts +0 -116
- package/src/service/builtins/memory/runtime/Indexer.ts +0 -466
- package/src/service/schedule/Schema.ts +0 -34
- package/src/shared/utils/store/StoreChannelAccountRepository.ts +0 -269
- package/src/shared/utils/store/StoreEnvRepository.ts +0 -452
- package/src/shared/utils/store/StoreModelRepository.ts +0 -324
- package/src/shared/utils/store/StoreSchema.ts +0 -344
- package/src/shared/utils/store/StoreSecureSettings.ts +0 -126
- package/src/shared/utils/store/StoreShared.ts +0 -67
- package/src/shared/utils/store/crypto.ts +0 -112
- package/src/shared/utils/store/index.ts +0 -497
- package/src/shared/utils/store/schema.ts +0 -103
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PlatformStore 加密配置仓储。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 管理 `platform_secure_settings` 表。
|
|
6
|
-
* - 平台级与 agent 级敏感配置都复用这套存储。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { decryptText, decryptTextSync, encryptText, encryptTextSync } from "./crypto.js";
|
|
10
|
-
import type { PlatformStoreContext } from "./StoreShared.js";
|
|
11
|
-
import { normalizeNonEmptyText, nowIso } from "./StoreShared.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 同步读取加密 JSON 配置。
|
|
15
|
-
*/
|
|
16
|
-
export function getSecureSettingJsonSync<T>(
|
|
17
|
-
context: PlatformStoreContext,
|
|
18
|
-
key: string,
|
|
19
|
-
): T | null {
|
|
20
|
-
const settingKey = normalizeNonEmptyText(key, "setting key");
|
|
21
|
-
const row = context.sqlite
|
|
22
|
-
.prepare(
|
|
23
|
-
"SELECT value_encrypted FROM platform_secure_settings WHERE key = ? LIMIT 1;",
|
|
24
|
-
)
|
|
25
|
-
.get(settingKey) as { value_encrypted?: unknown } | undefined;
|
|
26
|
-
if (!row || typeof row.value_encrypted !== "string" || !row.value_encrypted) {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
const raw = decryptTextSync(row.value_encrypted);
|
|
30
|
-
return JSON.parse(raw) as T;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 同步写入加密 JSON 配置。
|
|
35
|
-
*/
|
|
36
|
-
export function setSecureSettingJsonSync(
|
|
37
|
-
context: PlatformStoreContext,
|
|
38
|
-
key: string,
|
|
39
|
-
value: unknown,
|
|
40
|
-
): void {
|
|
41
|
-
const settingKey = normalizeNonEmptyText(key, "setting key");
|
|
42
|
-
const raw = JSON.stringify(value ?? null);
|
|
43
|
-
const encrypted = encryptTextSync(raw);
|
|
44
|
-
const now = nowIso();
|
|
45
|
-
context.sqlite
|
|
46
|
-
.prepare(
|
|
47
|
-
`
|
|
48
|
-
INSERT INTO platform_secure_settings (key, value_encrypted, created_at, updated_at)
|
|
49
|
-
VALUES (?, ?, ?, ?)
|
|
50
|
-
ON CONFLICT(key) DO UPDATE SET
|
|
51
|
-
value_encrypted = excluded.value_encrypted,
|
|
52
|
-
updated_at = excluded.updated_at;
|
|
53
|
-
`,
|
|
54
|
-
)
|
|
55
|
-
.run(settingKey, encrypted, now, now);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 删除加密配置。
|
|
60
|
-
*/
|
|
61
|
-
export function removeSecureSetting(
|
|
62
|
-
context: PlatformStoreContext,
|
|
63
|
-
key: string,
|
|
64
|
-
): void {
|
|
65
|
-
const settingKey = normalizeNonEmptyText(key, "setting key");
|
|
66
|
-
context.sqlite
|
|
67
|
-
.prepare("DELETE FROM platform_secure_settings WHERE key = ?;")
|
|
68
|
-
.run(settingKey);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 异步读取加密 JSON 配置。
|
|
73
|
-
*/
|
|
74
|
-
export async function getSecureSettingJson<T>(
|
|
75
|
-
context: PlatformStoreContext,
|
|
76
|
-
key: string,
|
|
77
|
-
): Promise<T | null> {
|
|
78
|
-
const settingKey = normalizeNonEmptyText(key, "setting key");
|
|
79
|
-
const row = context.sqlite
|
|
80
|
-
.prepare(
|
|
81
|
-
"SELECT value_encrypted FROM platform_secure_settings WHERE key = ? LIMIT 1;",
|
|
82
|
-
)
|
|
83
|
-
.get(settingKey) as { value_encrypted?: unknown } | undefined;
|
|
84
|
-
if (!row || typeof row.value_encrypted !== "string" || !row.value_encrypted) {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
const raw = await decryptText(row.value_encrypted);
|
|
88
|
-
return JSON.parse(raw) as T;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 异步写入加密 JSON 配置。
|
|
93
|
-
*/
|
|
94
|
-
export async function setSecureSettingJson(
|
|
95
|
-
context: PlatformStoreContext,
|
|
96
|
-
key: string,
|
|
97
|
-
value: unknown,
|
|
98
|
-
): Promise<void> {
|
|
99
|
-
const settingKey = normalizeNonEmptyText(key, "setting key");
|
|
100
|
-
const raw = JSON.stringify(value ?? null);
|
|
101
|
-
const encrypted = await encryptText(raw);
|
|
102
|
-
const now = nowIso();
|
|
103
|
-
context.sqlite
|
|
104
|
-
.prepare(
|
|
105
|
-
`
|
|
106
|
-
INSERT INTO platform_secure_settings (key, value_encrypted, created_at, updated_at)
|
|
107
|
-
VALUES (?, ?, ?, ?)
|
|
108
|
-
ON CONFLICT(key) DO UPDATE SET
|
|
109
|
-
value_encrypted = excluded.value_encrypted,
|
|
110
|
-
updated_at = excluded.updated_at;
|
|
111
|
-
`,
|
|
112
|
-
)
|
|
113
|
-
.run(settingKey, encrypted, now, now);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* 构造 agent 级加密配置 key。
|
|
118
|
-
*/
|
|
119
|
-
export function buildAgentSecureSettingKey(
|
|
120
|
-
agentIdInput: string,
|
|
121
|
-
keyInput: string,
|
|
122
|
-
): string {
|
|
123
|
-
const agentId = normalizeNonEmptyText(agentIdInput, "agentId");
|
|
124
|
-
const key = normalizeNonEmptyText(keyInput, "agent secure setting key");
|
|
125
|
-
return `agent:${agentId}:${key}`;
|
|
126
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PlatformStore 共享内部工具。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 这里只放 `PlatformStore` 内部多个子模块共用的类型与纯函数。
|
|
6
|
-
* - 对外不暴露业务语义,只服务 `utils/store/*` 内部实现。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import Database from "better-sqlite3";
|
|
10
|
-
import { drizzle } from "drizzle-orm/better-sqlite3";
|
|
11
|
-
import type { StoredChannelAccountChannel } from "@/shared/types/Store.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Drizzle SQLite 实例类型。
|
|
15
|
-
*/
|
|
16
|
-
export type PlatformDrizzleDb = ReturnType<typeof drizzle>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* PlatformStore 子模块上下文。
|
|
20
|
-
*/
|
|
21
|
-
export interface PlatformStoreContext {
|
|
22
|
-
/**
|
|
23
|
-
* 原始 SQLite 连接。
|
|
24
|
-
*/
|
|
25
|
-
sqlite: Database.Database;
|
|
26
|
-
/**
|
|
27
|
-
* Drizzle 查询实例。
|
|
28
|
-
*/
|
|
29
|
-
db: PlatformDrizzleDb;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 返回当前时间的 ISO 字符串。
|
|
34
|
-
*/
|
|
35
|
-
export function nowIso(): string {
|
|
36
|
-
return new Date().toISOString();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 归一化非空文本。
|
|
41
|
-
*/
|
|
42
|
-
export function normalizeNonEmptyText(value: string, fieldName: string): string {
|
|
43
|
-
const normalized = String(value || "").trim();
|
|
44
|
-
if (!normalized) throw new Error(`${fieldName} cannot be empty`);
|
|
45
|
-
return normalized;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 把字符串裁剪为可选文本。
|
|
50
|
-
*/
|
|
51
|
-
export function optionalTrimmedText(value: string | undefined): string | undefined {
|
|
52
|
-
const normalized = String(value || "").trim();
|
|
53
|
-
return normalized || undefined;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 规范化 channel account 的 channel 字段。
|
|
58
|
-
*/
|
|
59
|
-
export function normalizeChannelAccountChannel(
|
|
60
|
-
input: string,
|
|
61
|
-
): StoredChannelAccountChannel {
|
|
62
|
-
const channel = String(input || "").trim().toLowerCase();
|
|
63
|
-
if (channel === "telegram" || channel === "feishu" || channel === "qq") {
|
|
64
|
-
return channel;
|
|
65
|
-
}
|
|
66
|
-
throw new Error(`Unsupported channel account type: ${input}`);
|
|
67
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 模型存储加密工具。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 使用 AES-256-GCM 对敏感字段(apiKey)做加密落盘。
|
|
6
|
-
* - 默认从 `~/.downcity/main/model-db.key` 加载或自动生成密钥。
|
|
7
|
-
*/
|
|
8
|
-
import crypto from "node:crypto";
|
|
9
|
-
import fs from "fs-extra";
|
|
10
|
-
import path from "node:path";
|
|
11
|
-
import { getPlatformStoreKeyPath } from "@/host/runtime/CityPaths.js";
|
|
12
|
-
|
|
13
|
-
const MODEL_DB_KEY_PATH = "model-db.key";
|
|
14
|
-
const ENCRYPTION_ALGO = "aes-256-gcm";
|
|
15
|
-
|
|
16
|
-
let cachedKey: Buffer | null = null;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 重置缓存密钥。
|
|
20
|
-
*
|
|
21
|
-
* 关键点(中文)
|
|
22
|
-
* - 仅在迁移阶段替换 key 文件后调用,确保后续解密重新从磁盘加载最新 key。
|
|
23
|
-
*/
|
|
24
|
-
export function resetModelDbKeyCache(): void {
|
|
25
|
-
cachedKey = null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function resolveKeyFilePathSync(): string {
|
|
29
|
-
const keyPath = getPlatformStoreKeyPath();
|
|
30
|
-
fs.ensureDirSync(path.dirname(keyPath));
|
|
31
|
-
return keyPath;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async function resolveKeyFilePath(): Promise<string> {
|
|
35
|
-
return resolveKeyFilePathSync();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function loadOrCreateKeySync(): Buffer {
|
|
39
|
-
if (cachedKey) return cachedKey;
|
|
40
|
-
const envKey = String(process.env.DC_MODEL_DB_KEY || "").trim();
|
|
41
|
-
if (envKey) {
|
|
42
|
-
cachedKey = crypto.createHash("sha256").update(envKey, "utf8").digest();
|
|
43
|
-
return cachedKey;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const keyPath = resolveKeyFilePathSync();
|
|
47
|
-
if (fs.existsSync(keyPath)) {
|
|
48
|
-
const raw = String(fs.readFileSync(keyPath, "utf8")).trim();
|
|
49
|
-
if (raw) {
|
|
50
|
-
const parsed = Buffer.from(raw, "base64");
|
|
51
|
-
if (parsed.length === 32) {
|
|
52
|
-
cachedKey = parsed;
|
|
53
|
-
return cachedKey;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const next = crypto.randomBytes(32);
|
|
59
|
-
fs.writeFileSync(keyPath, next.toString("base64"), { mode: 0o600 });
|
|
60
|
-
cachedKey = next;
|
|
61
|
-
return cachedKey;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async function loadOrCreateKey(): Promise<Buffer> {
|
|
65
|
-
return loadOrCreateKeySync();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 同步加密字符串(用于同步配置读取链路)。
|
|
70
|
-
*/
|
|
71
|
-
export function encryptTextSync(plainText: string): string {
|
|
72
|
-
const key = loadOrCreateKeySync();
|
|
73
|
-
const iv = crypto.randomBytes(12);
|
|
74
|
-
const cipher = crypto.createCipheriv(ENCRYPTION_ALGO, key, iv);
|
|
75
|
-
const encrypted = Buffer.concat([cipher.update(plainText, "utf8"), cipher.final()]);
|
|
76
|
-
const tag = cipher.getAuthTag();
|
|
77
|
-
return Buffer.concat([iv, tag, encrypted]).toString("base64");
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 同步解密字符串(用于同步配置读取链路)。
|
|
82
|
-
*/
|
|
83
|
-
export function decryptTextSync(cipherText: string): string {
|
|
84
|
-
const key = loadOrCreateKeySync();
|
|
85
|
-
const packed = Buffer.from(cipherText, "base64");
|
|
86
|
-
if (packed.length < 28) {
|
|
87
|
-
throw new Error("Invalid encrypted payload");
|
|
88
|
-
}
|
|
89
|
-
const iv = packed.subarray(0, 12);
|
|
90
|
-
const tag = packed.subarray(12, 28);
|
|
91
|
-
const body = packed.subarray(28);
|
|
92
|
-
const decipher = crypto.createDecipheriv(ENCRYPTION_ALGO, key, iv);
|
|
93
|
-
decipher.setAuthTag(tag);
|
|
94
|
-
const plain = Buffer.concat([decipher.update(body), decipher.final()]);
|
|
95
|
-
return plain.toString("utf8");
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 加密字符串。
|
|
100
|
-
*/
|
|
101
|
-
export async function encryptText(plainText: string): Promise<string> {
|
|
102
|
-
await loadOrCreateKey();
|
|
103
|
-
return encryptTextSync(plainText);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* 解密字符串。
|
|
108
|
-
*/
|
|
109
|
-
export async function decryptText(cipherText: string): Promise<string> {
|
|
110
|
-
await loadOrCreateKey();
|
|
111
|
-
return decryptTextSync(cipherText);
|
|
112
|
-
}
|