@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.
Files changed (284) hide show
  1. package/bin/agent/AgentContext.d.ts.map +1 -1
  2. package/bin/agent/AgentContext.js +1 -0
  3. package/bin/agent/AgentContext.js.map +1 -1
  4. package/bin/agent/AgentContextTypes.d.ts +5 -1
  5. package/bin/agent/AgentContextTypes.d.ts.map +1 -1
  6. package/bin/agent/AgentRuntime.d.ts +6 -1
  7. package/bin/agent/AgentRuntime.d.ts.map +1 -1
  8. package/bin/agent/AgentRuntime.js +22 -3
  9. package/bin/agent/AgentRuntime.js.map +1 -1
  10. package/bin/agent/AgentRuntimeState.d.ts.map +1 -1
  11. package/bin/agent/AgentRuntimeState.js +58 -2
  12. package/bin/agent/AgentRuntimeState.js.map +1 -1
  13. package/bin/agent/AgentRuntimeTypes.d.ts +5 -1
  14. package/bin/agent/AgentRuntimeTypes.d.ts.map +1 -1
  15. package/bin/agent/project/AgentInitializer.d.ts +3 -2
  16. package/bin/agent/project/AgentInitializer.d.ts.map +1 -1
  17. package/bin/agent/project/AgentInitializer.js +34 -44
  18. package/bin/agent/project/AgentInitializer.js.map +1 -1
  19. package/bin/config/Config.d.ts.map +1 -1
  20. package/bin/config/Config.js +2 -21
  21. package/bin/config/Config.js.map +1 -1
  22. package/bin/config/Paths.d.ts +1 -5
  23. package/bin/config/Paths.d.ts.map +1 -1
  24. package/bin/config/Paths.js +2 -8
  25. package/bin/config/Paths.js.map +1 -1
  26. package/bin/host/daemon/ProjectSetup.d.ts +2 -1
  27. package/bin/host/daemon/ProjectSetup.d.ts.map +1 -1
  28. package/bin/host/daemon/ProjectSetup.js +14 -21
  29. package/bin/host/daemon/ProjectSetup.js.map +1 -1
  30. package/bin/host/runtime/AgentHostRuntime.d.ts.map +1 -1
  31. package/bin/host/runtime/AgentHostRuntime.js +1 -2
  32. package/bin/host/runtime/AgentHostRuntime.js.map +1 -1
  33. package/bin/host/runtime/CityPaths.d.ts +0 -3
  34. package/bin/host/runtime/CityPaths.d.ts.map +1 -1
  35. package/bin/host/runtime/CityPaths.js +0 -3
  36. package/bin/host/runtime/CityPaths.js.map +1 -1
  37. package/bin/host/sdk/Agent.d.ts +4 -0
  38. package/bin/host/sdk/Agent.d.ts.map +1 -1
  39. package/bin/host/sdk/Agent.js +71 -2
  40. package/bin/host/sdk/Agent.js.map +1 -1
  41. package/bin/host/sdk/AgentSdkTypes.d.ts +9 -0
  42. package/bin/host/sdk/AgentSdkTypes.d.ts.map +1 -1
  43. package/bin/host/sdk/SdkSessionSystemComposer.d.ts +11 -2
  44. package/bin/host/sdk/SdkSessionSystemComposer.d.ts.map +1 -1
  45. package/bin/host/sdk/SdkSessionSystemComposer.js +19 -2
  46. package/bin/host/sdk/SdkSessionSystemComposer.js.map +1 -1
  47. package/bin/host/sdk/Session.d.ts +10 -0
  48. package/bin/host/sdk/Session.d.ts.map +1 -1
  49. package/bin/host/sdk/Session.js +8 -0
  50. package/bin/host/sdk/Session.js.map +1 -1
  51. package/bin/http/Server.d.ts.map +1 -1
  52. package/bin/http/Server.js +1 -11
  53. package/bin/http/Server.js.map +1 -1
  54. package/bin/http/auth/AuthEnv.d.ts +0 -9
  55. package/bin/http/auth/AuthEnv.d.ts.map +1 -1
  56. package/bin/http/auth/AuthEnv.js +0 -9
  57. package/bin/http/auth/AuthEnv.js.map +1 -1
  58. package/bin/http/auth/CliAuthStateStore.d.ts +0 -4
  59. package/bin/http/auth/CliAuthStateStore.d.ts.map +1 -1
  60. package/bin/http/auth/CliAuthStateStore.js +0 -4
  61. package/bin/http/auth/CliAuthStateStore.js.map +1 -1
  62. package/bin/http/control/ModelRoutes.d.ts.map +1 -1
  63. package/bin/http/control/ModelRoutes.js +3 -8
  64. package/bin/http/control/ModelRoutes.js.map +1 -1
  65. package/bin/index.d.ts +2 -6
  66. package/bin/index.d.ts.map +1 -1
  67. package/bin/index.js +2 -6
  68. package/bin/index.js.map +1 -1
  69. package/bin/model/CreateModel.d.ts +2 -2
  70. package/bin/model/CreateModel.d.ts.map +1 -1
  71. package/bin/model/CreateModel.js +13 -12
  72. package/bin/model/CreateModel.js.map +1 -1
  73. package/bin/plugin/Activation.d.ts +4 -0
  74. package/bin/plugin/Activation.d.ts.map +1 -1
  75. package/bin/plugin/Activation.js +2 -2
  76. package/bin/plugin/Activation.js.map +1 -1
  77. package/bin/plugin/LocalExecution.d.ts.map +1 -1
  78. package/bin/plugin/LocalExecution.js +23 -2
  79. package/bin/plugin/LocalExecution.js.map +1 -1
  80. package/bin/plugin/PluginRegistry.js +2 -2
  81. package/bin/plugin/PluginRegistry.js.map +1 -1
  82. package/bin/plugins/asr/Plugin.d.ts.map +1 -1
  83. package/bin/plugins/asr/Plugin.js +4 -5
  84. package/bin/plugins/asr/Plugin.js.map +1 -1
  85. package/bin/plugins/auth/Plugin.d.ts.map +1 -1
  86. package/bin/plugins/auth/Plugin.js +1 -0
  87. package/bin/plugins/auth/Plugin.js.map +1 -1
  88. package/bin/plugins/auth/runtime/AuthorizationConfig.d.ts +4 -4
  89. package/bin/plugins/auth/runtime/AuthorizationConfig.d.ts.map +1 -1
  90. package/bin/plugins/auth/runtime/AuthorizationConfig.js +28 -26
  91. package/bin/plugins/auth/runtime/AuthorizationConfig.js.map +1 -1
  92. package/bin/plugins/auth/runtime/AuthorizationPolicy.d.ts +2 -0
  93. package/bin/plugins/auth/runtime/AuthorizationPolicy.d.ts.map +1 -1
  94. package/bin/plugins/auth/runtime/AuthorizationPolicy.js +3 -2
  95. package/bin/plugins/auth/runtime/AuthorizationPolicy.js.map +1 -1
  96. package/bin/plugins/auth/runtime/AuthorizationStore.d.ts +1 -1
  97. package/bin/plugins/auth/runtime/AuthorizationStore.d.ts.map +1 -1
  98. package/bin/plugins/auth/runtime/AuthorizationStore.js +3 -4
  99. package/bin/plugins/auth/runtime/AuthorizationStore.js.map +1 -1
  100. package/bin/plugins/skill/Plugin.js +2 -2
  101. package/bin/plugins/skill/Plugin.js.map +1 -1
  102. package/bin/plugins/tts/Plugin.d.ts.map +1 -1
  103. package/bin/plugins/tts/Plugin.js +4 -5
  104. package/bin/plugins/tts/Plugin.js.map +1 -1
  105. package/bin/plugins/web/Plugin.d.ts.map +1 -1
  106. package/bin/plugins/web/Plugin.js +4 -5
  107. package/bin/plugins/web/Plugin.js.map +1 -1
  108. package/bin/plugins/workboard/Plugin.js +2 -2
  109. package/bin/plugins/workboard/Plugin.js.map +1 -1
  110. package/bin/service/builtins/chat/accounts/ChannelAccountService.d.ts +4 -1
  111. package/bin/service/builtins/chat/accounts/ChannelAccountService.d.ts.map +1 -1
  112. package/bin/service/builtins/chat/accounts/ChannelAccountService.js +64 -91
  113. package/bin/service/builtins/chat/accounts/ChannelAccountService.js.map +1 -1
  114. package/bin/service/builtins/chat/runtime/ChatChannelActions.d.ts.map +1 -1
  115. package/bin/service/builtins/chat/runtime/ChatChannelActions.js +11 -18
  116. package/bin/service/builtins/chat/runtime/ChatChannelActions.js.map +1 -1
  117. package/bin/service/builtins/chat/runtime/ChatChannelCore.d.ts +1 -1
  118. package/bin/service/builtins/chat/runtime/ChatChannelCore.d.ts.map +1 -1
  119. package/bin/service/builtins/chat/runtime/ChatChannelCore.js +9 -17
  120. package/bin/service/builtins/chat/runtime/ChatChannelCore.js.map +1 -1
  121. package/bin/service/builtins/memory/Action.d.ts +1 -5
  122. package/bin/service/builtins/memory/Action.d.ts.map +1 -1
  123. package/bin/service/builtins/memory/Action.js +4 -42
  124. package/bin/service/builtins/memory/Action.js.map +1 -1
  125. package/bin/service/builtins/memory/MemoryService.d.ts.map +1 -1
  126. package/bin/service/builtins/memory/MemoryService.js +2 -32
  127. package/bin/service/builtins/memory/MemoryService.js.map +1 -1
  128. package/bin/service/builtins/memory/runtime/Search.d.ts +7 -3
  129. package/bin/service/builtins/memory/runtime/Search.d.ts.map +1 -1
  130. package/bin/service/builtins/memory/runtime/Search.js +220 -16
  131. package/bin/service/builtins/memory/runtime/Search.js.map +1 -1
  132. package/bin/service/builtins/memory/runtime/Store.d.ts +9 -50
  133. package/bin/service/builtins/memory/runtime/Store.d.ts.map +1 -1
  134. package/bin/service/builtins/memory/runtime/Store.js +10 -130
  135. package/bin/service/builtins/memory/runtime/Store.js.map +1 -1
  136. package/bin/service/builtins/memory/runtime/Writer.d.ts.map +1 -1
  137. package/bin/service/builtins/memory/runtime/Writer.js +1 -2
  138. package/bin/service/builtins/memory/runtime/Writer.js.map +1 -1
  139. package/bin/service/builtins/memory/types/Memory.d.ts +3 -57
  140. package/bin/service/builtins/memory/types/Memory.d.ts.map +1 -1
  141. package/bin/service/schedule/Store.d.ts +22 -25
  142. package/bin/service/schedule/Store.d.ts.map +1 -1
  143. package/bin/service/schedule/Store.js +172 -154
  144. package/bin/service/schedule/Store.js.map +1 -1
  145. package/bin/session/composer/system/default/SystemDomain.d.ts.map +1 -1
  146. package/bin/session/composer/system/default/SystemDomain.js +1 -0
  147. package/bin/session/composer/system/default/SystemDomain.js.map +1 -1
  148. package/bin/shared/types/AgentHost.d.ts +120 -4
  149. package/bin/shared/types/AgentHost.d.ts.map +1 -1
  150. package/bin/shared/types/Plugin.d.ts +5 -1
  151. package/bin/shared/types/Plugin.d.ts.map +1 -1
  152. package/package.json +1 -4
  153. package/src/agent/AgentContext.ts +1 -0
  154. package/src/agent/AgentContextTypes.ts +5 -0
  155. package/src/agent/AgentRuntime.ts +32 -3
  156. package/src/agent/AgentRuntimeState.ts +66 -2
  157. package/src/agent/AgentRuntimeTypes.ts +5 -0
  158. package/src/agent/project/AgentInitializer.ts +40 -42
  159. package/src/config/Config.ts +2 -17
  160. package/src/config/Paths.ts +2 -9
  161. package/src/host/daemon/ProjectSetup.ts +19 -21
  162. package/src/host/runtime/AgentHostRuntime.ts +0 -2
  163. package/src/host/runtime/CityPaths.ts +0 -3
  164. package/src/host/sdk/Agent.ts +67 -2
  165. package/src/host/sdk/AgentSdkTypes.ts +10 -0
  166. package/src/host/sdk/SdkSessionSystemComposer.ts +39 -2
  167. package/src/host/sdk/Session.ts +18 -0
  168. package/src/http/Server.ts +0 -13
  169. package/src/http/auth/AuthEnv.ts +0 -9
  170. package/src/http/auth/CliAuthStateStore.ts +0 -4
  171. package/src/http/control/ModelRoutes.ts +3 -9
  172. package/src/index.ts +2 -12
  173. package/src/model/CreateModel.ts +15 -13
  174. package/src/plugin/Activation.ts +6 -2
  175. package/src/plugin/LocalExecution.ts +24 -2
  176. package/src/plugin/PluginRegistry.ts +2 -2
  177. package/src/plugins/asr/Plugin.ts +4 -5
  178. package/src/plugins/auth/Plugin.ts +1 -0
  179. package/src/plugins/auth/runtime/AuthorizationConfig.ts +47 -37
  180. package/src/plugins/auth/runtime/AuthorizationPolicy.ts +5 -2
  181. package/src/plugins/auth/runtime/AuthorizationStore.ts +6 -5
  182. package/src/plugins/skill/Plugin.ts +2 -2
  183. package/src/plugins/tts/Plugin.ts +4 -5
  184. package/src/plugins/web/Plugin.ts +4 -5
  185. package/src/plugins/workboard/Plugin.ts +2 -2
  186. package/src/service/builtins/chat/accounts/ChannelAccountService.ts +42 -62
  187. package/src/service/builtins/chat/runtime/ChatChannelActions.ts +12 -18
  188. package/src/service/builtins/chat/runtime/ChatChannelCore.ts +9 -14
  189. package/src/service/builtins/memory/Action.ts +6 -47
  190. package/src/service/builtins/memory/MemoryService.ts +1 -33
  191. package/src/service/builtins/memory/runtime/Search.ts +256 -16
  192. package/src/service/builtins/memory/runtime/Store.ts +13 -185
  193. package/src/service/builtins/memory/runtime/Writer.ts +1 -2
  194. package/src/service/builtins/memory/types/Memory.ts +2 -59
  195. package/src/service/schedule/Store.ts +215 -175
  196. package/src/session/composer/system/default/SystemDomain.ts +1 -0
  197. package/src/shared/types/AgentHost.ts +138 -4
  198. package/src/shared/types/Plugin.ts +5 -0
  199. package/tsconfig.tsbuildinfo +1 -1
  200. package/bin/http/auth/AuthMiddleware.d.ts +0 -36
  201. package/bin/http/auth/AuthMiddleware.d.ts.map +0 -1
  202. package/bin/http/auth/AuthMiddleware.js +0 -37
  203. package/bin/http/auth/AuthMiddleware.js.map +0 -1
  204. package/bin/http/auth/AuthRoutes.d.ts +0 -17
  205. package/bin/http/auth/AuthRoutes.d.ts.map +0 -1
  206. package/bin/http/auth/AuthRoutes.js +0 -78
  207. package/bin/http/auth/AuthRoutes.js.map +0 -1
  208. package/bin/http/auth/AuthService.d.ts +0 -119
  209. package/bin/http/auth/AuthService.d.ts.map +0 -1
  210. package/bin/http/auth/AuthService.js +0 -307
  211. package/bin/http/auth/AuthService.js.map +0 -1
  212. package/bin/http/auth/AuthStore.d.ts +0 -165
  213. package/bin/http/auth/AuthStore.d.ts.map +0 -1
  214. package/bin/http/auth/AuthStore.js +0 -442
  215. package/bin/http/auth/AuthStore.js.map +0 -1
  216. package/bin/http/auth/RoutePolicy.d.ts +0 -30
  217. package/bin/http/auth/RoutePolicy.d.ts.map +0 -1
  218. package/bin/http/auth/RoutePolicy.js +0 -229
  219. package/bin/http/auth/RoutePolicy.js.map +0 -1
  220. package/bin/plugin/Lifecycle.d.ts +0 -33
  221. package/bin/plugin/Lifecycle.d.ts.map +0 -1
  222. package/bin/plugin/Lifecycle.js +0 -102
  223. package/bin/plugin/Lifecycle.js.map +0 -1
  224. package/bin/service/builtins/memory/runtime/Indexer.d.ts +0 -71
  225. package/bin/service/builtins/memory/runtime/Indexer.d.ts.map +0 -1
  226. package/bin/service/builtins/memory/runtime/Indexer.js +0 -345
  227. package/bin/service/builtins/memory/runtime/Indexer.js.map +0 -1
  228. package/bin/service/schedule/Schema.d.ts +0 -171
  229. package/bin/service/schedule/Schema.d.ts.map +0 -1
  230. package/bin/service/schedule/Schema.js +0 -26
  231. package/bin/service/schedule/Schema.js.map +0 -1
  232. package/bin/shared/utils/store/StoreChannelAccountRepository.d.ts +0 -34
  233. package/bin/shared/utils/store/StoreChannelAccountRepository.d.ts.map +0 -1
  234. package/bin/shared/utils/store/StoreChannelAccountRepository.js +0 -198
  235. package/bin/shared/utils/store/StoreChannelAccountRepository.js.map +0 -1
  236. package/bin/shared/utils/store/StoreEnvRepository.d.ts +0 -98
  237. package/bin/shared/utils/store/StoreEnvRepository.d.ts.map +0 -1
  238. package/bin/shared/utils/store/StoreEnvRepository.js +0 -334
  239. package/bin/shared/utils/store/StoreEnvRepository.js.map +0 -1
  240. package/bin/shared/utils/store/StoreModelRepository.d.ts +0 -61
  241. package/bin/shared/utils/store/StoreModelRepository.d.ts.map +0 -1
  242. package/bin/shared/utils/store/StoreModelRepository.js +0 -278
  243. package/bin/shared/utils/store/StoreModelRepository.js.map +0 -1
  244. package/bin/shared/utils/store/StoreSchema.d.ts +0 -13
  245. package/bin/shared/utils/store/StoreSchema.d.ts.map +0 -1
  246. package/bin/shared/utils/store/StoreSchema.js +0 -319
  247. package/bin/shared/utils/store/StoreSchema.js.map +0 -1
  248. package/bin/shared/utils/store/StoreSecureSettings.d.ts +0 -33
  249. package/bin/shared/utils/store/StoreSecureSettings.d.ts.map +0 -1
  250. package/bin/shared/utils/store/StoreSecureSettings.js +0 -91
  251. package/bin/shared/utils/store/StoreSecureSettings.js.map +0 -1
  252. package/bin/shared/utils/store/StoreShared.d.ts +0 -44
  253. package/bin/shared/utils/store/StoreShared.d.ts.map +0 -1
  254. package/bin/shared/utils/store/StoreShared.js +0 -40
  255. package/bin/shared/utils/store/StoreShared.js.map +0 -1
  256. package/bin/shared/utils/store/crypto.d.ts +0 -24
  257. package/bin/shared/utils/store/crypto.d.ts.map +0 -1
  258. package/bin/shared/utils/store/crypto.js +0 -101
  259. package/bin/shared/utils/store/crypto.js.map +0 -1
  260. package/bin/shared/utils/store/index.d.ts +0 -230
  261. package/bin/shared/utils/store/index.d.ts.map +0 -1
  262. package/bin/shared/utils/store/index.js +0 -360
  263. package/bin/shared/utils/store/index.js.map +0 -1
  264. package/bin/shared/utils/store/schema.d.ts +0 -690
  265. package/bin/shared/utils/store/schema.d.ts.map +0 -1
  266. package/bin/shared/utils/store/schema.js +0 -81
  267. package/bin/shared/utils/store/schema.js.map +0 -1
  268. package/src/http/auth/AuthMiddleware.ts +0 -61
  269. package/src/http/auth/AuthRoutes.ts +0 -100
  270. package/src/http/auth/AuthService.ts +0 -367
  271. package/src/http/auth/AuthStore.ts +0 -572
  272. package/src/http/auth/RoutePolicy.ts +0 -255
  273. package/src/plugin/Lifecycle.ts +0 -116
  274. package/src/service/builtins/memory/runtime/Indexer.ts +0 -466
  275. package/src/service/schedule/Schema.ts +0 -34
  276. package/src/shared/utils/store/StoreChannelAccountRepository.ts +0 -269
  277. package/src/shared/utils/store/StoreEnvRepository.ts +0 -452
  278. package/src/shared/utils/store/StoreModelRepository.ts +0 -324
  279. package/src/shared/utils/store/StoreSchema.ts +0 -344
  280. package/src/shared/utils/store/StoreSecureSettings.ts +0 -126
  281. package/src/shared/utils/store/StoreShared.ts +0 -67
  282. package/src/shared/utils/store/crypto.ts +0 -112
  283. package/src/shared/utils/store/index.ts +0 -497
  284. package/src/shared/utils/store/schema.ts +0 -103
@@ -1,269 +0,0 @@
1
- /**
2
- * PlatformStore 渠道账号仓储。
3
- *
4
- * 关键点(中文)
5
- * - 统一管理 `channel_accounts` 表。
6
- * - 负责敏感字段解密/加密与 channel account 的语义化组装。
7
- */
8
-
9
- import type {
10
- StoredChannelAccount,
11
- UpsertChannelAccountInput,
12
- } from "@/shared/types/Store.js";
13
- import { decryptText, decryptTextSync, encryptText } from "./crypto.js";
14
- import type { PlatformStoreContext } from "./StoreShared.js";
15
- import {
16
- normalizeChannelAccountChannel,
17
- normalizeNonEmptyText,
18
- nowIso,
19
- optionalTrimmedText,
20
- } from "./StoreShared.js";
21
-
22
- /**
23
- * 同步列出 channel accounts。
24
- */
25
- export function listChannelAccountsSync(
26
- context: PlatformStoreContext,
27
- channelInput?: string,
28
- ): StoredChannelAccount[] {
29
- const maybeChannel = optionalTrimmedText(channelInput);
30
- const rows = maybeChannel
31
- ? context.sqlite.prepare(
32
- `
33
- SELECT
34
- id, channel, name, identity, owner, creator,
35
- bot_token_encrypted, app_id_encrypted, app_secret_encrypted,
36
- domain, sandbox, created_at, updated_at
37
- FROM channel_accounts
38
- WHERE channel = ?
39
- ORDER BY name ASC, id ASC;
40
- `,
41
- ).all(maybeChannel)
42
- : context.sqlite.prepare(
43
- `
44
- SELECT
45
- id, channel, name, identity, owner, creator,
46
- bot_token_encrypted, app_id_encrypted, app_secret_encrypted,
47
- domain, sandbox, created_at, updated_at
48
- FROM channel_accounts
49
- ORDER BY channel ASC, name ASC, id ASC;
50
- `,
51
- ).all();
52
- const out: StoredChannelAccount[] = [];
53
- for (const row of rows as Array<Record<string, unknown>>) {
54
- const entry = buildChannelAccountFromRowSync(row);
55
- if (entry) out.push(entry);
56
- }
57
- return out;
58
- }
59
-
60
- /**
61
- * 同步按 ID 获取 channel account。
62
- */
63
- export function getChannelAccountSync(
64
- context: PlatformStoreContext,
65
- accountIdInput: string,
66
- ): StoredChannelAccount | null {
67
- const accountId = normalizeNonEmptyText(accountIdInput, "channel account id");
68
- const rows = listChannelAccountsSync(context);
69
- return rows.find((item) => item.id === accountId) || null;
70
- }
71
-
72
- /**
73
- * 异步列出 channel accounts。
74
- */
75
- export async function listChannelAccounts(
76
- context: PlatformStoreContext,
77
- channelInput?: string,
78
- ): Promise<StoredChannelAccount[]> {
79
- const maybeChannel = optionalTrimmedText(channelInput);
80
- const rows = maybeChannel
81
- ? context.sqlite.prepare(
82
- `
83
- SELECT
84
- id, channel, name, identity, owner, creator,
85
- bot_token_encrypted, app_id_encrypted, app_secret_encrypted,
86
- domain, sandbox, created_at, updated_at
87
- FROM channel_accounts
88
- WHERE channel = ?
89
- ORDER BY name ASC, id ASC;
90
- `,
91
- ).all(maybeChannel)
92
- : context.sqlite.prepare(
93
- `
94
- SELECT
95
- id, channel, name, identity, owner, creator,
96
- bot_token_encrypted, app_id_encrypted, app_secret_encrypted,
97
- domain, sandbox, created_at, updated_at
98
- FROM channel_accounts
99
- ORDER BY channel ASC, name ASC, id ASC;
100
- `,
101
- ).all();
102
- const out: StoredChannelAccount[] = [];
103
- for (const row of rows as Array<Record<string, unknown>>) {
104
- const entry = await buildChannelAccountFromRow(row);
105
- if (entry) out.push(entry);
106
- }
107
- return out;
108
- }
109
-
110
- /**
111
- * 异步按 ID 获取 channel account。
112
- */
113
- export async function getChannelAccount(
114
- context: PlatformStoreContext,
115
- accountIdInput: string,
116
- ): Promise<StoredChannelAccount | null> {
117
- const accountId = normalizeNonEmptyText(accountIdInput, "channel account id");
118
- const rows = await listChannelAccounts(context);
119
- return rows.find((item) => item.id === accountId) || null;
120
- }
121
-
122
- /**
123
- * 新增或更新 channel account。
124
- */
125
- export async function upsertChannelAccount(
126
- context: PlatformStoreContext,
127
- input: UpsertChannelAccountInput,
128
- ): Promise<void> {
129
- const id = normalizeNonEmptyText(input.id, "channel account id");
130
- const channel = normalizeChannelAccountChannel(input.channel);
131
- const name = normalizeNonEmptyText(input.name, "channel account name");
132
- const existing = await getChannelAccount(context, id);
133
- const createdAt = existing?.createdAt || nowIso();
134
- const updatedAt = nowIso();
135
-
136
- const nextBotToken =
137
- Object.prototype.hasOwnProperty.call(input, "botToken")
138
- ? optionalTrimmedText(input.botToken)
139
- : existing?.botToken;
140
- const nextAppId =
141
- Object.prototype.hasOwnProperty.call(input, "appId")
142
- ? optionalTrimmedText(input.appId)
143
- : existing?.appId;
144
- const nextAppSecret =
145
- Object.prototype.hasOwnProperty.call(input, "appSecret")
146
- ? optionalTrimmedText(input.appSecret)
147
- : existing?.appSecret;
148
- const botTokenEncrypted = nextBotToken ? await encryptText(nextBotToken) : null;
149
- const appIdEncrypted = nextAppId ? await encryptText(nextAppId) : null;
150
- const appSecretEncrypted = nextAppSecret ? await encryptText(nextAppSecret) : null;
151
-
152
- context.sqlite.prepare(
153
- `
154
- INSERT INTO channel_accounts (
155
- id, channel, name, identity, owner, creator,
156
- bot_token_encrypted, app_id_encrypted, app_secret_encrypted,
157
- domain, sandbox, created_at, updated_at
158
- )
159
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
160
- ON CONFLICT(id) DO UPDATE SET
161
- channel = excluded.channel,
162
- name = excluded.name,
163
- identity = excluded.identity,
164
- owner = excluded.owner,
165
- creator = excluded.creator,
166
- bot_token_encrypted = excluded.bot_token_encrypted,
167
- app_id_encrypted = excluded.app_id_encrypted,
168
- app_secret_encrypted = excluded.app_secret_encrypted,
169
- domain = excluded.domain,
170
- sandbox = excluded.sandbox,
171
- updated_at = excluded.updated_at;
172
- `,
173
- ).run(
174
- id,
175
- channel,
176
- name,
177
- optionalTrimmedText(input.identity) || null,
178
- optionalTrimmedText(input.owner) || null,
179
- optionalTrimmedText(input.creator) || null,
180
- botTokenEncrypted,
181
- appIdEncrypted,
182
- appSecretEncrypted,
183
- optionalTrimmedText(input.domain) || null,
184
- input.sandbox === true ? 1 : 0,
185
- createdAt,
186
- updatedAt,
187
- );
188
- }
189
-
190
- /**
191
- * 删除 channel account。
192
- */
193
- export function removeChannelAccount(
194
- context: PlatformStoreContext,
195
- accountIdInput: string,
196
- ): void {
197
- const accountId = normalizeNonEmptyText(accountIdInput, "channel account id");
198
- context.sqlite.prepare("DELETE FROM channel_accounts WHERE id = ?;").run(accountId);
199
- }
200
-
201
- /**
202
- * 同步构造 channel account。
203
- */
204
- function buildChannelAccountFromRowSync(
205
- row: Record<string, unknown>,
206
- ): StoredChannelAccount | null {
207
- const id = String(row.id || "").trim();
208
- if (!id) return null;
209
- const channel = normalizeChannelAccountChannel(String(row.channel || ""));
210
- const botTokenEncrypted =
211
- typeof row.bot_token_encrypted === "string" ? row.bot_token_encrypted : "";
212
- const appIdEncrypted =
213
- typeof row.app_id_encrypted === "string" ? row.app_id_encrypted : "";
214
- const appSecretEncrypted =
215
- typeof row.app_secret_encrypted === "string" ? row.app_secret_encrypted : "";
216
- const botToken = botTokenEncrypted ? decryptTextSync(botTokenEncrypted) : undefined;
217
- const appId = appIdEncrypted ? decryptTextSync(appIdEncrypted) : undefined;
218
- const appSecret = appSecretEncrypted ? decryptTextSync(appSecretEncrypted) : undefined;
219
- return {
220
- id,
221
- channel,
222
- name: String(row.name || "").trim() || id,
223
- identity: optionalTrimmedText(String(row.identity || "")),
224
- owner: optionalTrimmedText(String(row.owner || "")),
225
- creator: optionalTrimmedText(String(row.creator || "")),
226
- botToken: optionalTrimmedText(botToken),
227
- appId: optionalTrimmedText(appId),
228
- appSecret: optionalTrimmedText(appSecret),
229
- domain: optionalTrimmedText(String(row.domain || "")),
230
- sandbox: Number(row.sandbox || 0) === 1,
231
- createdAt: String(row.created_at || ""),
232
- updatedAt: String(row.updated_at || ""),
233
- };
234
- }
235
-
236
- /**
237
- * 异步构造 channel account。
238
- */
239
- async function buildChannelAccountFromRow(
240
- row: Record<string, unknown>,
241
- ): Promise<StoredChannelAccount | null> {
242
- const id = String(row.id || "").trim();
243
- if (!id) return null;
244
- const channel = normalizeChannelAccountChannel(String(row.channel || ""));
245
- const botTokenEncrypted =
246
- typeof row.bot_token_encrypted === "string" ? row.bot_token_encrypted : "";
247
- const appIdEncrypted =
248
- typeof row.app_id_encrypted === "string" ? row.app_id_encrypted : "";
249
- const appSecretEncrypted =
250
- typeof row.app_secret_encrypted === "string" ? row.app_secret_encrypted : "";
251
- const botToken = botTokenEncrypted ? await decryptText(botTokenEncrypted) : undefined;
252
- const appId = appIdEncrypted ? await decryptText(appIdEncrypted) : undefined;
253
- const appSecret = appSecretEncrypted ? await decryptText(appSecretEncrypted) : undefined;
254
- return {
255
- id,
256
- channel,
257
- name: String(row.name || "").trim() || id,
258
- identity: optionalTrimmedText(String(row.identity || "")),
259
- owner: optionalTrimmedText(String(row.owner || "")),
260
- creator: optionalTrimmedText(String(row.creator || "")),
261
- botToken: optionalTrimmedText(botToken),
262
- appId: optionalTrimmedText(appId),
263
- appSecret: optionalTrimmedText(appSecret),
264
- domain: optionalTrimmedText(String(row.domain || "")),
265
- sandbox: Number(row.sandbox || 0) === 1,
266
- createdAt: String(row.created_at || ""),
267
- updatedAt: String(row.updated_at || ""),
268
- };
269
- }
@@ -1,452 +0,0 @@
1
- /**
2
- * PlatformStore 环境变量仓储。
3
- *
4
- * 关键点(中文)
5
- * - 统一管理 `env_entries` 单表。
6
- * - 同时暴露 global / agent 两层语义化包装。
7
- */
8
-
9
- import type {
10
- StoredAgentEnvEntry,
11
- StoredEnvEntry,
12
- StoredEnvScope,
13
- StoredGlobalEnvEntry,
14
- UpsertAgentEnvEntryInput,
15
- UpsertEnvEntryInput,
16
- UpsertGlobalEnvEntryInput,
17
- } from "@/shared/types/Store.js";
18
- import { decryptText, decryptTextSync, encryptText } from "./crypto.js";
19
- import type { PlatformStoreContext } from "./StoreShared.js";
20
- import { normalizeNonEmptyText, nowIso } from "./StoreShared.js";
21
-
22
- /**
23
- * 规范化 env scope。
24
- */
25
- export function normalizeEnvScope(input: string): StoredEnvScope {
26
- const scope = String(input || "").trim().toLowerCase();
27
- if (scope === "agent") return "agent";
28
- return "global";
29
- }
30
-
31
- /**
32
- * 规范化 env 的 agent 目标。
33
- */
34
- export function normalizeEnvAgentTarget(
35
- scope: StoredEnvScope,
36
- agentIdInput?: string,
37
- ): string {
38
- if (scope === "global") return "";
39
- return normalizeNonEmptyText(agentIdInput || "", "agentId");
40
- }
41
-
42
- /**
43
- * 同步构造 env 条目。
44
- */
45
- function buildEnvEntryFromRowSync(row: {
46
- scope?: unknown;
47
- agent_id?: unknown;
48
- key?: unknown;
49
- description?: unknown;
50
- value_encrypted?: unknown;
51
- created_at?: unknown;
52
- updated_at?: unknown;
53
- }): StoredEnvEntry | null {
54
- const scope = normalizeEnvScope(String(row.scope || ""));
55
- const agentId = String(row.agent_id || "").trim();
56
- const key = String(row.key || "").trim();
57
- const encrypted = typeof row.value_encrypted === "string" ? row.value_encrypted : "";
58
- if (!key || !encrypted) return null;
59
- return {
60
- scope,
61
- agentId: scope === "agent" ? agentId : undefined,
62
- key,
63
- description: String(row.description || "").trim() || undefined,
64
- value: decryptTextSync(encrypted),
65
- createdAt: String(row.created_at || ""),
66
- updatedAt: String(row.updated_at || ""),
67
- };
68
- }
69
-
70
- /**
71
- * 异步构造 env 条目。
72
- */
73
- async function buildEnvEntryFromRow(row: {
74
- scope?: unknown;
75
- agent_id?: unknown;
76
- key?: unknown;
77
- description?: unknown;
78
- value_encrypted?: unknown;
79
- created_at?: unknown;
80
- updated_at?: unknown;
81
- }): Promise<StoredEnvEntry | null> {
82
- const scope = normalizeEnvScope(String(row.scope || ""));
83
- const agentId = String(row.agent_id || "").trim();
84
- const key = String(row.key || "").trim();
85
- const encrypted = typeof row.value_encrypted === "string" ? row.value_encrypted : "";
86
- if (!key || !encrypted) return null;
87
- return {
88
- scope,
89
- agentId: scope === "agent" ? agentId : undefined,
90
- key,
91
- description: String(row.description || "").trim() || undefined,
92
- value: await decryptText(encrypted),
93
- createdAt: String(row.created_at || ""),
94
- updatedAt: String(row.updated_at || ""),
95
- };
96
- }
97
-
98
- /**
99
- * 同步列出 env 条目。
100
- */
101
- export function listEnvEntriesSync(
102
- context: PlatformStoreContext,
103
- scopeInput?: StoredEnvScope,
104
- agentIdInput?: string,
105
- ): StoredEnvEntry[] {
106
- const hasScope = Boolean(scopeInput);
107
- const scope = hasScope ? normalizeEnvScope(scopeInput || "global") : undefined;
108
- const hasAgentFilter = scope === "agent" && Boolean(String(agentIdInput || "").trim());
109
- const agentId = hasAgentFilter
110
- ? normalizeEnvAgentTarget(scope, agentIdInput)
111
- : undefined;
112
- const rows = hasAgentFilter
113
- ? context.sqlite.prepare(
114
- `
115
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
116
- FROM env_entries
117
- WHERE scope = 'agent' AND agent_id = ?
118
- ORDER BY key ASC;
119
- `,
120
- ).all(agentId)
121
- : scope === "agent"
122
- ? context.sqlite.prepare(
123
- `
124
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
125
- FROM env_entries
126
- WHERE scope = 'agent'
127
- ORDER BY agent_id ASC, key ASC;
128
- `,
129
- ).all()
130
- : scope === "global"
131
- ? context.sqlite.prepare(
132
- `
133
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
134
- FROM env_entries
135
- WHERE scope = 'global'
136
- ORDER BY key ASC;
137
- `,
138
- ).all()
139
- : context.sqlite.prepare(
140
- `
141
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
142
- FROM env_entries
143
- ORDER BY scope ASC, agent_id ASC, key ASC;
144
- `,
145
- ).all();
146
- const out: StoredEnvEntry[] = [];
147
- for (const row of rows as Array<Record<string, unknown>>) {
148
- const entry = buildEnvEntryFromRowSync(row);
149
- if (entry) out.push(entry);
150
- }
151
- return out;
152
- }
153
-
154
- /**
155
- * 异步列出 env 条目。
156
- */
157
- export async function listEnvEntries(
158
- context: PlatformStoreContext,
159
- scopeInput?: StoredEnvScope,
160
- agentIdInput?: string,
161
- ): Promise<StoredEnvEntry[]> {
162
- const hasScope = Boolean(scopeInput);
163
- const scope = hasScope ? normalizeEnvScope(scopeInput || "global") : undefined;
164
- const hasAgentFilter = scope === "agent" && Boolean(String(agentIdInput || "").trim());
165
- const agentId = hasAgentFilter
166
- ? normalizeEnvAgentTarget(scope, agentIdInput)
167
- : undefined;
168
- const rows = hasAgentFilter
169
- ? context.sqlite.prepare(
170
- `
171
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
172
- FROM env_entries
173
- WHERE scope = 'agent' AND agent_id = ?
174
- ORDER BY key ASC;
175
- `,
176
- ).all(agentId)
177
- : scope === "agent"
178
- ? context.sqlite.prepare(
179
- `
180
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
181
- FROM env_entries
182
- WHERE scope = 'agent'
183
- ORDER BY agent_id ASC, key ASC;
184
- `,
185
- ).all()
186
- : scope === "global"
187
- ? context.sqlite.prepare(
188
- `
189
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
190
- FROM env_entries
191
- WHERE scope = 'global'
192
- ORDER BY key ASC;
193
- `,
194
- ).all()
195
- : context.sqlite.prepare(
196
- `
197
- SELECT scope, agent_id, key, description, value_encrypted, created_at, updated_at
198
- FROM env_entries
199
- ORDER BY scope ASC, agent_id ASC, key ASC;
200
- `,
201
- ).all();
202
- const out: StoredEnvEntry[] = [];
203
- for (const row of rows as Array<Record<string, unknown>>) {
204
- const entry = await buildEnvEntryFromRow(row);
205
- if (entry) out.push(entry);
206
- }
207
- return out;
208
- }
209
-
210
- /**
211
- * 新增或更新 env 条目。
212
- */
213
- export async function upsertEnvEntry(
214
- context: PlatformStoreContext,
215
- input: UpsertEnvEntryInput,
216
- ): Promise<void> {
217
- const scope = normalizeEnvScope(input.scope);
218
- const agentId = normalizeEnvAgentTarget(scope, input.agentId);
219
- const key = normalizeNonEmptyText(input.key, `${scope} env key`);
220
- const description = String(input.description || "").trim();
221
- const value = String(input.value ?? "");
222
- const existing = context.sqlite
223
- .prepare(
224
- `
225
- SELECT created_at
226
- FROM env_entries
227
- WHERE scope = ? AND agent_id = ? AND key = ?
228
- LIMIT 1;
229
- `,
230
- )
231
- .get(scope, agentId, key) as { created_at?: unknown } | undefined;
232
- const createdAt = String(existing?.created_at || nowIso());
233
- const updatedAt = nowIso();
234
- const encrypted = await encryptText(value);
235
- context.sqlite
236
- .prepare(
237
- `
238
- INSERT INTO env_entries (
239
- scope,
240
- agent_id,
241
- key,
242
- description,
243
- value_encrypted,
244
- created_at,
245
- updated_at
246
- )
247
- VALUES (?, ?, ?, ?, ?, ?, ?)
248
- ON CONFLICT(scope, agent_id, key) DO UPDATE SET
249
- description = excluded.description,
250
- value_encrypted = excluded.value_encrypted,
251
- updated_at = excluded.updated_at;
252
- `,
253
- )
254
- .run(scope, agentId, key, description || null, encrypted, createdAt, updatedAt);
255
- }
256
-
257
- /**
258
- * 删除单个 env 条目。
259
- */
260
- export function removeEnvEntry(
261
- context: PlatformStoreContext,
262
- input: { scope: StoredEnvScope; agentId?: string; key: string },
263
- ): void {
264
- const scope = normalizeEnvScope(input.scope);
265
- const agentId = normalizeEnvAgentTarget(scope, input.agentId);
266
- const key = normalizeNonEmptyText(input.key, `${scope} env key`);
267
- context.sqlite
268
- .prepare("DELETE FROM env_entries WHERE scope = ? AND agent_id = ? AND key = ?;")
269
- .run(scope, agentId, key);
270
- }
271
-
272
- /**
273
- * 同步列出全局环境变量。
274
- */
275
- export function listGlobalEnvEntriesSync(
276
- context: PlatformStoreContext,
277
- ): StoredGlobalEnvEntry[] {
278
- return listEnvEntriesSync(context, "global");
279
- }
280
-
281
- /**
282
- * 同步读取全局环境变量映射。
283
- */
284
- export function getGlobalEnvMapSync(
285
- context: PlatformStoreContext,
286
- ): Record<string, string> {
287
- const entries = listGlobalEnvEntriesSync(context);
288
- const map: Record<string, string> = {};
289
- for (const item of entries) {
290
- map[item.key] = item.value;
291
- }
292
- return map;
293
- }
294
-
295
- /**
296
- * 异步列出全局环境变量。
297
- */
298
- export async function listGlobalEnvEntries(
299
- context: PlatformStoreContext,
300
- ): Promise<StoredGlobalEnvEntry[]> {
301
- return listEnvEntries(context, "global");
302
- }
303
-
304
- /**
305
- * 异步读取全局环境变量映射。
306
- */
307
- export async function getGlobalEnvMap(
308
- context: PlatformStoreContext,
309
- ): Promise<Record<string, string>> {
310
- const entries = await listGlobalEnvEntries(context);
311
- const map: Record<string, string> = {};
312
- for (const item of entries) {
313
- map[item.key] = item.value;
314
- }
315
- return map;
316
- }
317
-
318
- /**
319
- * 新增或更新全局环境变量。
320
- */
321
- export async function upsertGlobalEnvEntry(
322
- context: PlatformStoreContext,
323
- input: UpsertGlobalEnvEntryInput,
324
- ): Promise<void> {
325
- await upsertEnvEntry(context, {
326
- scope: "global",
327
- key: input.key,
328
- value: input.value,
329
- });
330
- }
331
-
332
- /**
333
- * 删除单个全局环境变量。
334
- */
335
- export function removeGlobalEnvEntry(
336
- context: PlatformStoreContext,
337
- keyInput: string,
338
- ): void {
339
- removeEnvEntry(context, {
340
- scope: "global",
341
- key: keyInput,
342
- });
343
- }
344
-
345
- /**
346
- * 清空全局环境变量。
347
- */
348
- export function clearGlobalEnvEntries(context: PlatformStoreContext): void {
349
- context.sqlite.prepare("DELETE FROM env_entries WHERE scope = 'global';").run();
350
- }
351
-
352
- /**
353
- * 同步列出指定 agent 的私有环境变量。
354
- */
355
- export function listAgentEnvEntriesSync(
356
- context: PlatformStoreContext,
357
- agentIdInput: string,
358
- ): StoredAgentEnvEntry[] {
359
- return listEnvEntriesSync(context, "agent", agentIdInput);
360
- }
361
-
362
- /**
363
- * 同步读取 agent 环境变量映射。
364
- */
365
- export function getAgentEnvMapSync(
366
- context: PlatformStoreContext,
367
- agentIdInput: string,
368
- ): Record<string, string> {
369
- const entries = listAgentEnvEntriesSync(context, agentIdInput);
370
- const map: Record<string, string> = {};
371
- for (const item of entries) {
372
- map[item.key] = item.value;
373
- }
374
- return map;
375
- }
376
-
377
- /**
378
- * 异步列出指定 agent 的私有环境变量。
379
- */
380
- export async function listAgentEnvEntries(
381
- context: PlatformStoreContext,
382
- agentIdInput: string,
383
- ): Promise<StoredAgentEnvEntry[]> {
384
- return listEnvEntries(context, "agent", agentIdInput);
385
- }
386
-
387
- /**
388
- * 异步列出全部 agent 私有环境变量。
389
- */
390
- export async function listAllAgentEnvEntries(
391
- context: PlatformStoreContext,
392
- ): Promise<StoredAgentEnvEntry[]> {
393
- return listEnvEntries(context, "agent");
394
- }
395
-
396
- /**
397
- * 异步读取 agent 环境变量映射。
398
- */
399
- export async function getAgentEnvMap(
400
- context: PlatformStoreContext,
401
- agentIdInput: string,
402
- ): Promise<Record<string, string>> {
403
- const entries = await listAgentEnvEntries(context, agentIdInput);
404
- const map: Record<string, string> = {};
405
- for (const item of entries) {
406
- map[item.key] = item.value;
407
- }
408
- return map;
409
- }
410
-
411
- /**
412
- * 新增或更新 agent 私有环境变量。
413
- */
414
- export async function upsertAgentEnvEntry(
415
- context: PlatformStoreContext,
416
- input: UpsertAgentEnvEntryInput,
417
- ): Promise<void> {
418
- await upsertEnvEntry(context, {
419
- scope: "agent",
420
- agentId: input.agentId,
421
- key: input.key,
422
- value: input.value,
423
- });
424
- }
425
-
426
- /**
427
- * 删除指定 agent 的单个环境变量。
428
- */
429
- export function removeAgentEnvEntry(
430
- context: PlatformStoreContext,
431
- agentIdInput: string,
432
- keyInput: string,
433
- ): void {
434
- removeEnvEntry(context, {
435
- scope: "agent",
436
- agentId: agentIdInput,
437
- key: keyInput,
438
- });
439
- }
440
-
441
- /**
442
- * 清空指定 agent 的私有环境变量。
443
- */
444
- export function clearAgentEnvEntries(
445
- context: PlatformStoreContext,
446
- agentIdInput: string,
447
- ): void {
448
- const agentId = normalizeNonEmptyText(agentIdInput, "agentId");
449
- context.sqlite
450
- .prepare("DELETE FROM env_entries WHERE scope = 'agent' AND agent_id = ?;")
451
- .run(agentId);
452
- }