nolo-cli 0.6.1 → 0.8.0-alpha.10

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 (175) hide show
  1. package/_executeModel-CPDNUUQV.js +198 -0
  2. package/action-PAXRPJJF.js +27 -0
  3. package/addContentAction-CZVWF3QZ.js +25 -0
  4. package/addDialogAgentAction-CCIDTPHC.js +67 -0
  5. package/agentDeleteCommand-OSUMJMZM.js +323 -0
  6. package/agentEmailCommands-OQTJIPQU.js +621 -0
  7. package/agentListCommands-FATQEUYT.js +219 -0
  8. package/agentMachineCommands-6LZWF4GL.js +421 -0
  9. package/agentPullCommand-O2MTUAFR.js +122 -0
  10. package/agentRecordCommands-TIHFU65Z.js +89 -0
  11. package/agentRunCommand-R64Y3HCH.js +102 -0
  12. package/agentRunControl-K4VKNEHF.js +57 -0
  13. package/agentRuntimeLocal-6PIWAHLQ.js +99 -0
  14. package/agentSlice-TP4VARJF.js +41 -0
  15. package/authCommand-WXMD3IPR.js +574 -0
  16. package/authCommands-X74AFDTC.js +26 -0
  17. package/chunk-2EISRLRC.js +238 -0
  18. package/chunk-2FTTDCD4.js +675 -0
  19. package/chunk-2MALU52L.js +51 -0
  20. package/chunk-2TPUKEU7.js +1078 -0
  21. package/chunk-3ASDTTEI.js +1774 -0
  22. package/chunk-3EUW3WEO.js +8 -0
  23. package/chunk-3LY4F7SV.js +9293 -0
  24. package/chunk-4CQLVGRV.js +50 -0
  25. package/chunk-4IJYA27C.js +342 -0
  26. package/chunk-4SN6J65J.js +38 -0
  27. package/chunk-4VNS5WPM.js +42 -0
  28. package/chunk-572EKYLF.js +792 -0
  29. package/chunk-5MIDPSSK.js +206 -0
  30. package/chunk-5MMAK75Q.js +15 -0
  31. package/chunk-5VV37AT4.js +222 -0
  32. package/chunk-63W5N6P3.js +150 -0
  33. package/chunk-6A5EF6IR.js +929 -0
  34. package/chunk-6HYVJV7B.js +102 -0
  35. package/chunk-6P2WX5IA.js +3631 -0
  36. package/chunk-6WD3OOJS.js +20 -0
  37. package/chunk-ATNTHIZI.js +13 -0
  38. package/chunk-AUGHVUFE.js +69 -0
  39. package/chunk-AVMKZHBC.js +85 -0
  40. package/chunk-AZPCASRE.js +36 -0
  41. package/chunk-BNMT7YY5.js +34 -0
  42. package/chunk-BR2WP3DF.js +548 -0
  43. package/chunk-BSTNFPHD.js +231 -0
  44. package/chunk-BU4OPIXC.js +171 -0
  45. package/chunk-CF2WZF4N.js +134 -0
  46. package/chunk-CF7PE763.js +10 -0
  47. package/chunk-CKOJQDKR.js +198 -0
  48. package/chunk-CM54RSSA.js +206 -0
  49. package/chunk-CWMSB67P.js +34 -0
  50. package/chunk-DA2OEKS6.js +27 -0
  51. package/chunk-DFFIZ25G.js +30 -0
  52. package/chunk-DWLEOOGR.js +8 -0
  53. package/chunk-DZ3XJVET.js +231 -0
  54. package/chunk-EHLHSX7R.js +1433 -0
  55. package/chunk-ELRRLDWI.js +478 -0
  56. package/chunk-EYEEHCU4.js +3066 -0
  57. package/chunk-FQZWM5YX.js +628 -0
  58. package/chunk-G3SUCT4W.js +8 -0
  59. package/chunk-G5H7RNFL.js +807 -0
  60. package/chunk-GAKEBQD6.js +30 -0
  61. package/chunk-GBRG75BQ.js +86 -0
  62. package/chunk-H2RMRZJE.js +113 -0
  63. package/chunk-H33743XH.js +25 -0
  64. package/chunk-HD63YREL.js +19 -0
  65. package/chunk-HEIIIZBX.js +104 -0
  66. package/chunk-HNMXYYJR.js +12 -0
  67. package/chunk-HOMYP7GZ.js +2152 -0
  68. package/chunk-HZFOMJRA.js +4300 -0
  69. package/chunk-IAXBCTTE.js +175 -0
  70. package/chunk-IDZTNRP2.js +85 -0
  71. package/chunk-IHHA5Y2S.js +9013 -0
  72. package/chunk-ISNCLGUZ.js +146 -0
  73. package/chunk-IYQV26RA.js +138 -0
  74. package/chunk-J4576WMJ.js +91 -0
  75. package/chunk-JBTJAPUF.js +35 -0
  76. package/chunk-JZPBVBIY.js +94 -0
  77. package/chunk-KQHGZWSV.js +63 -0
  78. package/chunk-KUG52IAP.js +8 -0
  79. package/chunk-KX6YBIHG.js +8 -0
  80. package/chunk-LCBAMY2M.js +60 -0
  81. package/chunk-LM25IZBJ.js +902 -0
  82. package/chunk-LM6IYQ3F.js +16 -0
  83. package/chunk-M76ECQRD.js +13658 -0
  84. package/chunk-MRC4Q34M.js +188 -0
  85. package/chunk-NEMRK4EK.js +944 -0
  86. package/chunk-NXZGF7F7.js +2878 -0
  87. package/chunk-OF5H3QWS.js +300 -0
  88. package/chunk-OLZBQWZU.js +6 -0
  89. package/chunk-OOYVBFM7.js +191 -0
  90. package/chunk-OWOHNGCN.js +50 -0
  91. package/chunk-PN7AFLOK.js +3401 -0
  92. package/chunk-POCFIDGQ.js +562 -0
  93. package/chunk-Q6WACBKB.js +1949 -0
  94. package/chunk-QFV2OAIM.js +421 -0
  95. package/chunk-QJ6V2NIN.js +1447 -0
  96. package/chunk-QSJ3HFEF.js +8 -0
  97. package/chunk-QUWDTOUP.js +190 -0
  98. package/chunk-QYXLLLGL.js +4519 -0
  99. package/chunk-ROWQEJRP.js +139 -0
  100. package/chunk-S7Z2HMCN.js +25 -0
  101. package/chunk-TDIQFT2F.js +301 -0
  102. package/chunk-TILFE6LV.js +381 -0
  103. package/chunk-TKOUBWBO.js +24674 -0
  104. package/chunk-TYH5JE6N.js +27158 -0
  105. package/chunk-U4TRT5V4.js +246 -0
  106. package/chunk-U6VC4MU4.js +8 -0
  107. package/chunk-UAD2WT54.js +64 -0
  108. package/chunk-USZMK5UT.js +105 -0
  109. package/chunk-VEJLAVTC.js +2052 -0
  110. package/chunk-VF4K2QE6.js +21 -0
  111. package/chunk-VHBWHNVT.js +59 -0
  112. package/chunk-VIDPVFP3.js +20 -0
  113. package/chunk-VP4WJWKW.js +8882 -0
  114. package/chunk-VQ2AJRGD.js +148 -0
  115. package/chunk-W4NVOSFF.js +37 -0
  116. package/chunk-WWC7I6TA.js +8 -0
  117. package/chunk-X7W4TNB4.js +1550 -0
  118. package/chunk-XDVAYP26.js +537 -0
  119. package/chunk-XKIMRZO2.js +221 -0
  120. package/chunk-Y5GCAMGP.js +9 -0
  121. package/chunk-YWPXYBY7.js +360 -0
  122. package/chunk-Z4NR32XR.js +50 -0
  123. package/chunk-ZNFJWEN4.js +56 -0
  124. package/cliExecutor-DNE7TCC4.js +36 -0
  125. package/clipboardy-BVE3LVEH.js +12001 -0
  126. package/createAgentAutomationAction-KLKS2HGU.js +864 -0
  127. package/createDialogAction-Z33JMBF5.js +52 -0
  128. package/createPageAction-AQVNOAAI.js +169 -0
  129. package/db-HCJ5PRB5.js +271 -0
  130. package/dbSlice-C4UJLRZP.js +59 -0
  131. package/deleteFile-4SKGPHOZ.js +41 -0
  132. package/dialogCommands-W5PD6MZU.js +59 -0
  133. package/dialogSlice-RBJYVSAR.js +146 -0
  134. package/docCreateCommands-MM34S4Z3.js +215 -0
  135. package/docDeleteCommands-JJTU722V.js +176 -0
  136. package/docReadCommands-GLYDMCTU.js +129 -0
  137. package/docStore-5TYME4DO.js +96 -0
  138. package/docUpdateCommands-ZCGQ7URR.js +313 -0
  139. package/fetchMessages-JOCYIFKL.js +16 -0
  140. package/getPricing-PWPJ6MNV.js +30 -0
  141. package/handleSendMessageAction-KA2MRW6E.js +183 -0
  142. package/index.js +184 -112254
  143. package/lib-FYYBQIXS.js +19839 -0
  144. package/localDialogRead-D3BELSTF.js +26 -0
  145. package/localFirstLog-DHNOY2NA.js +19 -0
  146. package/localRuntimeAuthority-TEGCOWX4.js +471 -0
  147. package/localRuntimeDb-LGDF6FQP.js +12 -0
  148. package/machineCommands-ZB5WUSPF.js +774 -0
  149. package/machineInfo-M6KB6HZN.js +10 -0
  150. package/machineWsRunDispatch-GWCAPPCM.js +105 -0
  151. package/memoryCommands-O4MIPIWQ.js +476 -0
  152. package/models-XLEOET3E.js +9 -0
  153. package/offlineMarxistsAgentCommand-BCPJITJL.js +242 -0
  154. package/package.json +3 -25
  155. package/read-RIJCIVKX.js +17 -0
  156. package/readDocTool-GZD2N3LO.js +16 -0
  157. package/readlineWorkspace-FEDY44AA.js +5357 -0
  158. package/removeDialogAgentAction-CXLST3N6.js +72 -0
  159. package/runAgentClientLoop-W4NKE64V.js +288 -0
  160. package/runtimeDoctorCommands-LRHLPDRC.js +289 -0
  161. package/setDialogExtraReferencesAction-LP6VYGU7.js +59 -0
  162. package/setPrimaryDialogAgentAction-D3GGH23T.js +79 -0
  163. package/settingSlice-J4I3Z5H7.js +198 -0
  164. package/skillSummaryMarker-42HKSXYY.js +9 -0
  165. package/slateToRenderMarkdown-Y5IYN73U.js +7 -0
  166. package/slateUtils-2M7MUTWX.js +19 -0
  167. package/spaceCommands-LC4FDAKE.js +450 -0
  168. package/spaceSlice-32XOGBEF.js +145 -0
  169. package/streamAgentChatTurn-YQW32AQH.js +4503 -0
  170. package/tableCommands-GOFP7QKA.js +1286 -0
  171. package/toolApiClient-QI3YBNWB.js +26 -0
  172. package/tools-XGTFRYD7.js +83 -0
  173. package/understanding-HCSFPE7R.js +318 -0
  174. package/updateCommands-VOSPKXFF.js +31 -0
  175. package/updateDialogTitleAction-4LHQDNHQ.js +185 -0
@@ -0,0 +1,562 @@
1
+ import {
2
+ applyAgentSecretMigrationUpdates,
3
+ createFileCredentialBroker,
4
+ migrateAgentSecrets,
5
+ normalizeAgentRuntimeToolPolicy
6
+ } from "./chunk-TDIQFT2F.js";
7
+ import {
8
+ patch,
9
+ remove,
10
+ write
11
+ } from "./chunk-G5H7RNFL.js";
12
+ import {
13
+ asRecordOrEmpty
14
+ } from "./chunk-KX6YBIHG.js";
15
+ import {
16
+ createAgentKey,
17
+ require_index_umd,
18
+ toTrimmedString
19
+ } from "./chunk-PN7AFLOK.js";
20
+ import {
21
+ asyncThunkCreator,
22
+ buildCreateSlice,
23
+ getRuntimeServerContext
24
+ } from "./chunk-EYEEHCU4.js";
25
+ import {
26
+ asTrimmedString
27
+ } from "./chunk-3EUW3WEO.js";
28
+ import {
29
+ asOptionalTrimmedString
30
+ } from "./chunk-G3SUCT4W.js";
31
+ import {
32
+ __toESM
33
+ } from "./chunk-4VNS5WPM.js";
34
+
35
+ // packages/ai/chat/agentCredentialSyncClient.ts
36
+ function buildUrl(currentServer, credentialRef) {
37
+ return `${currentServer}/api/agent-credentials/${encodeURIComponent(credentialRef)}`;
38
+ }
39
+ async function fetchServerSyncedCredential(ctx, credentialRef) {
40
+ try {
41
+ const res = await fetch(buildUrl(ctx.currentServer, credentialRef), {
42
+ method: "GET",
43
+ headers: { Authorization: `Bearer ${ctx.authToken}` }
44
+ });
45
+ if (!res.ok) return null;
46
+ const data = await res.json();
47
+ return data.apiKey ?? null;
48
+ } catch {
49
+ return null;
50
+ }
51
+ }
52
+ async function deleteServerSyncedCredential(ctx, credentialRef) {
53
+ try {
54
+ const res = await fetch(buildUrl(ctx.currentServer, credentialRef), {
55
+ method: "DELETE",
56
+ headers: { Authorization: `Bearer ${ctx.authToken}` }
57
+ });
58
+ return res.ok;
59
+ } catch {
60
+ return false;
61
+ }
62
+ }
63
+ async function pushServerSyncedCredential(ctx, credentialRef, apiKey) {
64
+ try {
65
+ const res = await fetch(buildUrl(ctx.currentServer, credentialRef), {
66
+ method: "POST",
67
+ headers: {
68
+ "Content-Type": "application/json",
69
+ Authorization: `Bearer ${ctx.authToken}`
70
+ },
71
+ body: JSON.stringify({ apiKey })
72
+ });
73
+ return res.ok;
74
+ } catch {
75
+ return false;
76
+ }
77
+ }
78
+
79
+ // packages/ai/agent/agentSlice.ts
80
+ var import_ulid = __toESM(require_index_umd());
81
+ var createAgentCredentialBroker = createFileCredentialBroker;
82
+ function setAgentCredentialBrokerFactoryForTests(factory) {
83
+ createAgentCredentialBroker = factory ?? createFileCredentialBroker;
84
+ }
85
+ var readNonEmptyApiKey = (value) => asTrimmedString(value);
86
+ var migrateRawApiKeyForAgent = async (args) => {
87
+ const broker = createAgentCredentialBroker();
88
+ const result = await migrateAgentSecrets({
89
+ agent: {
90
+ key: args.agentKey,
91
+ apiKey: args.apiKey,
92
+ apiKeyRef: args.apiKeyRef,
93
+ credentialRef: args.credentialRef
94
+ },
95
+ broker
96
+ });
97
+ const next = applyAgentSecretMigrationUpdates(args.record, result.updates);
98
+ if (readNonEmptyApiKey(next.apiKey)) {
99
+ throw new Error(
100
+ "Failed to migrate agent API key into the local credential broker. The raw key was not stripped."
101
+ );
102
+ }
103
+ return next;
104
+ };
105
+ var createSliceWithThunks = buildCreateSlice({
106
+ creators: { asyncThunk: asyncThunkCreator }
107
+ });
108
+ var initialState = {};
109
+ var normalizeAgentReferences = (references) => {
110
+ if (!Array.isArray(references)) return [];
111
+ return references.map((ref) => ({
112
+ dbKey: ref.dbKey || "",
113
+ title: ref.title || "",
114
+ type: ref.type === "page" ? "knowledge" : ref.type || "knowledge"
115
+ }));
116
+ };
117
+ var normalizeRuntimeToolPolicy = (value) => {
118
+ const policy = normalizeAgentRuntimeToolPolicy(value);
119
+ if (!policy) return void 0;
120
+ const hasPolicyContent = Boolean(
121
+ policy.agentTools?.length || policy.runtimeTools?.length || policy.workspace || policy.shell || policy.isolation || policy.git || policy.budget || policy.audit
122
+ );
123
+ return hasPolicyContent ? policy : void 0;
124
+ };
125
+ var processAgentCreateForm = (formData, userId) => {
126
+ const isPublic = !!formData.isPublic;
127
+ const machineId = asTrimmedString(formData.machineId);
128
+ const result = {
129
+ ...formData,
130
+ // tags: "a, b" -> ["a", "b"]
131
+ tags: formData.tags ? formData.tags.split(",").map((s) => s.trim()).filter(Boolean) : [],
132
+ // 归一化 references
133
+ references: normalizeAgentReferences(formData.references || []),
134
+ // 非公开时,强制清空白名单,避免脏数据
135
+ whitelist: isPublic ? formData.whitelist || [] : [],
136
+ // allowFork 独立于公开:Space 内的私有 agent 也可被空间成员复制
137
+ allowFork: !!formData.allowFork
138
+ };
139
+ delete result.machineId;
140
+ result.runtimeToolPolicy = normalizeRuntimeToolPolicy(
141
+ formData.runtimeToolPolicy
142
+ );
143
+ if (!result.runtimeToolPolicy) {
144
+ delete result.runtimeToolPolicy;
145
+ }
146
+ if (formData.apiSource === "cli" && machineId) {
147
+ const binding = {
148
+ ...asRecordOrEmpty(result.runtimeBinding),
149
+ machineId,
150
+ ownerUserId: userId
151
+ };
152
+ result.runtimeBinding = binding;
153
+ }
154
+ return result;
155
+ };
156
+ var processAgentUpdateChanges = (data, userId, previousAgent) => {
157
+ const changes = {};
158
+ if ("name" in data) {
159
+ changes.name = String(data.name ?? "").trim();
160
+ }
161
+ if ("model" in data) {
162
+ changes.model = (data.model ?? "").trim();
163
+ }
164
+ if ("provider" in data) {
165
+ changes.provider = (data.provider ?? "").trim();
166
+ }
167
+ if ("prompt" in data) {
168
+ changes.prompt = (data.prompt ?? "").trim();
169
+ }
170
+ if ("introduction" in data) {
171
+ changes.introduction = (data.introduction ?? "").trim();
172
+ }
173
+ if ("customProviderUrl" in data) {
174
+ changes.customProviderUrl = (data.customProviderUrl ?? "").trim();
175
+ }
176
+ if ("apiKey" in data) {
177
+ changes.apiKey = (data.apiKey ?? "").trim();
178
+ }
179
+ if ("hasVision" in data && data.hasVision !== void 0) {
180
+ changes.hasVision = !!data.hasVision;
181
+ }
182
+ if ("apiSource" in data && data.apiSource) {
183
+ changes.apiSource = data.apiSource;
184
+ }
185
+ if ("cliProvider" in data) {
186
+ changes.cliProvider = data.cliProvider || "";
187
+ }
188
+ if ("machineId" in data) {
189
+ const machineId = String(data.machineId ?? "").trim();
190
+ const effectiveApiSource = data.apiSource ?? previousAgent?.apiSource;
191
+ if (machineId) {
192
+ if (effectiveApiSource === "cli" || effectiveApiSource === void 0) {
193
+ const binding = {
194
+ ...asRecordOrEmpty(changes.runtimeBinding),
195
+ machineId,
196
+ ownerUserId: userId
197
+ };
198
+ changes.runtimeBinding = binding;
199
+ }
200
+ } else {
201
+ if (effectiveApiSource === "cli" || effectiveApiSource === void 0) {
202
+ changes.runtimeBinding = null;
203
+ }
204
+ }
205
+ }
206
+ if ("useServerProxy" in data && data.useServerProxy !== void 0) {
207
+ changes.useServerProxy = !!data.useServerProxy;
208
+ }
209
+ if ("sharingLevel" in data) {
210
+ const sharingLevel = data.sharingLevel;
211
+ if (sharingLevel === "default" || sharingLevel === "split" || sharingLevel === "full") {
212
+ changes.sharingLevel = sharingLevel;
213
+ }
214
+ }
215
+ if ("greeting" in data) {
216
+ changes.greeting = data.greeting;
217
+ }
218
+ if ("tools" in data) {
219
+ changes.tools = Array.isArray(data.tools) ? data.tools.slice() : [];
220
+ }
221
+ if ("runtimeToolPolicy" in data) {
222
+ const rawRuntimeToolPolicy = data.runtimeToolPolicy;
223
+ changes.runtimeToolPolicy = rawRuntimeToolPolicy === null ? null : normalizeRuntimeToolPolicy(rawRuntimeToolPolicy) ?? null;
224
+ }
225
+ const numericKeys = [
226
+ "inputPrice",
227
+ "outputPrice",
228
+ "temperature",
229
+ "top_p",
230
+ "frequency_penalty",
231
+ "presence_penalty",
232
+ "max_tokens"
233
+ ];
234
+ numericKeys.forEach((key) => {
235
+ if (key in data) {
236
+ const raw = data[key];
237
+ if (raw === void 0 || raw === null) {
238
+ changes[key] = raw;
239
+ } else {
240
+ const num = Number(raw);
241
+ changes[key] = Number.isNaN(num) ? raw : num;
242
+ }
243
+ }
244
+ });
245
+ if ("reasoning_effort" in data) {
246
+ changes.reasoning_effort = data.reasoning_effort;
247
+ }
248
+ if ("tags" in data) {
249
+ const raw = data.tags;
250
+ let arr = [];
251
+ if (Array.isArray(raw)) {
252
+ arr = raw.map((s) => toTrimmedString(s)).filter(Boolean);
253
+ } else if (typeof raw === "string") {
254
+ arr = raw.split(",").map((s) => s.trim()).filter(Boolean);
255
+ }
256
+ changes.tags = arr;
257
+ }
258
+ if ("references" in data) {
259
+ changes.references = normalizeAgentReferences(
260
+ data.references || []
261
+ );
262
+ }
263
+ if ("whitelist" in data) {
264
+ changes.whitelist = data.whitelist || [];
265
+ }
266
+ if ("allowFork" in data) {
267
+ changes.allowFork = !!data.allowFork;
268
+ }
269
+ if ("avatarFileId" in data) {
270
+ const raw = data.avatarFileId;
271
+ changes.avatarFileId = raw ? String(raw).trim() : null;
272
+ }
273
+ if ("isPublic" in data) {
274
+ changes.isPublic = !!data.isPublic;
275
+ if (!changes.isPublic) {
276
+ changes.whitelist = [];
277
+ }
278
+ }
279
+ return changes;
280
+ };
281
+ var slice = createSliceWithThunks({
282
+ name: "agent",
283
+ initialState,
284
+ reducers: (create) => ({
285
+ /**
286
+ * 通用 LLM 调用(不带 Agent 上下文 / 历史)
287
+ */
288
+ runLlm: create.asyncThunk(async (args, thunkApi) => {
289
+ const overrides = {};
290
+ if (args.systemPromptOverride !== void 0) overrides.prompt = args.systemPromptOverride;
291
+ if (args.toolsOverride !== void 0) overrides.tools = args.toolsOverride;
292
+ const { _executeModel } = await import("./_executeModel-CPDNUUQV.js");
293
+ return _executeModel(
294
+ {
295
+ isStreaming: args.isStreaming ?? false,
296
+ withAgentContext: false,
297
+ withChatHistory: false,
298
+ agentConfigOverrides: Object.keys(overrides).length ? overrides : void 0
299
+ },
300
+ args,
301
+ thunkApi
302
+ );
303
+ }),
304
+ /**
305
+ * 通用 Agent 调用(带 Agent 上下文,多轮工具循环)
306
+ *
307
+ * 使用客户端 runAgentClientLoop:
308
+ * - 每轮调用 LLM(非流式)
309
+ * - 遇到 tool_calls 时通过 findToolExecutor 本地执行工具
310
+ * - 循环直到无工具调用或触发其他运行时停止条件
311
+ */
312
+ runAgent: create.asyncThunk(async (args, thunkApi) => {
313
+ const { runAgentClientLoop } = await import("./runAgentClientLoop-W4NKE64V.js");
314
+ const { content: loopContent, toolCallCount } = await runAgentClientLoop(
315
+ {
316
+ agentKey: args.agentKey,
317
+ content: args.content,
318
+ parentMessageId: args.parentMessageId,
319
+ billingDialogKey: args.billingDialogKey
320
+ },
321
+ thunkApi
322
+ );
323
+ return loopContent;
324
+ }),
325
+ /**
326
+ * 聊天轮次流式 Agent 调用
327
+ */
328
+ streamAgentChatTurn: create.asyncThunk(async (args, thunkApi) => {
329
+ const { streamAgentChatTurnHandler } = await import("./streamAgentChatTurn-YQW32AQH.js");
330
+ return streamAgentChatTurnHandler(args, thunkApi);
331
+ }),
332
+ /**
333
+ * 创建 Agent:
334
+ * - 写入用户私有路径
335
+ * - 如 isPublic=true,则同时写入公共路径
336
+ * - 返回完整 Agent 对象(包含 id / meta 字段)
337
+ */
338
+ createAgent: create.asyncThunk(
339
+ async ({ userId, formData, spaceId }, thunkApi) => {
340
+ const effectiveUserId = asOptionalTrimmedString(userId) ?? "local";
341
+ const processed = processAgentCreateForm(formData, effectiveUserId);
342
+ const now = Date.now();
343
+ const id = (0, import_ulid.ulid)();
344
+ const privateKey = createAgentKey.private(effectiveUserId, id);
345
+ const publicKey = createAgentKey.public(id);
346
+ let agent = {
347
+ ...processed,
348
+ id,
349
+ type: "agent" /* AGENT */,
350
+ userId: effectiveUserId,
351
+ createdAt: now,
352
+ updatedAt: now,
353
+ dialogCount: 0,
354
+ messageCount: 0,
355
+ tokenCount: 0,
356
+ spaceId
357
+ // 记录 spaceId
358
+ };
359
+ if (effectiveUserId === "local") {
360
+ agent.isPublic = false;
361
+ }
362
+ try {
363
+ const { localFirstLog } = await import("./localFirstLog-DHNOY2NA.js");
364
+ localFirstLog("agent.create.start", {
365
+ owner: effectiveUserId,
366
+ hasRawApiKey: Boolean(readNonEmptyApiKey(agent.apiKey)),
367
+ hasSpace: Boolean(spaceId)
368
+ });
369
+ } catch {
370
+ }
371
+ const createRawApiKey = readNonEmptyApiKey(agent.apiKey);
372
+ if (createRawApiKey) {
373
+ agent = await migrateRawApiKeyForAgent({
374
+ record: agent,
375
+ agentKey: privateKey,
376
+ apiKey: createRawApiKey,
377
+ apiKeyRef: agent.apiKeyRef,
378
+ credentialRef: agent.credentialRef
379
+ });
380
+ }
381
+ await thunkApi.dispatch(
382
+ write({
383
+ data: agent,
384
+ customKey: privateKey,
385
+ userId: effectiveUserId
386
+ })
387
+ ).unwrap();
388
+ if (agent.isPublic) {
389
+ await thunkApi.dispatch(
390
+ write({
391
+ data: agent,
392
+ customKey: publicKey,
393
+ userId: effectiveUserId
394
+ })
395
+ ).unwrap();
396
+ }
397
+ if (agent.credentialSynced && createRawApiKey) {
398
+ const ctx = getRuntimeServerContext(thunkApi.getState());
399
+ const credentialRef = agent.credentialRef;
400
+ if (ctx.currentToken && credentialRef) {
401
+ try {
402
+ await pushServerSyncedCredential(
403
+ { currentServer: ctx.currentServer ?? "", authToken: ctx.currentToken },
404
+ credentialRef,
405
+ createRawApiKey
406
+ );
407
+ } catch {
408
+ }
409
+ }
410
+ }
411
+ try {
412
+ const { localFirstLog } = await import("./localFirstLog-DHNOY2NA.js");
413
+ localFirstLog("agent.create.done", {
414
+ owner: effectiveUserId,
415
+ key: privateKey,
416
+ hasCredentialRef: Boolean(
417
+ typeof agent.credentialRef === "string" && agent.credentialRef.trim()
418
+ ),
419
+ hasRawApiKey: Boolean(readNonEmptyApiKey(agent.apiKey)),
420
+ isPublic: Boolean(agent.isPublic)
421
+ });
422
+ } catch {
423
+ }
424
+ return agent;
425
+ }
426
+ ),
427
+ /**
428
+ * 更新 Agent(支持局部字段 patch):
429
+ * - patch 私有副本
430
+ * - 如提供 previousAgent,则同步更新 / 删除公共副本
431
+ *
432
+ * 注意:
433
+ * - Tool 场景下一般不提供 previousAgent,此时只保证私有副本被更新;
434
+ * 公共副本(应用市场)不做强一致保证。
435
+ */
436
+ updateAgent: create.asyncThunk(
437
+ async ({ userId, agentId, formData, previousAgent }, thunkApi) => {
438
+ const effectiveUserId = asOptionalTrimmedString(userId) ?? "local";
439
+ const normalizedAgentId = (() => {
440
+ const raw = agentId.trim();
441
+ if (raw.startsWith("agent-")) {
442
+ const parts = raw.split("-");
443
+ if (parts.length >= 3) return parts.slice(2).join("-");
444
+ }
445
+ return raw;
446
+ })();
447
+ const privateKey = createAgentKey.private(effectiveUserId, normalizedAgentId);
448
+ const publicKey = createAgentKey.public(normalizedAgentId);
449
+ let changes = processAgentUpdateChanges(formData || {}, effectiveUserId, previousAgent);
450
+ const updateRawApiKey = readNonEmptyApiKey(changes.apiKey);
451
+ if (updateRawApiKey) {
452
+ const formAny = formData || {};
453
+ const migrated = await migrateRawApiKeyForAgent({
454
+ record: { ...changes },
455
+ agentKey: privateKey,
456
+ apiKey: updateRawApiKey,
457
+ apiKeyRef: formAny.apiKeyRef ?? previousAgent?.apiKeyRef ?? changes.apiKeyRef,
458
+ credentialRef: formAny.credentialRef ?? previousAgent?.credentialRef ?? changes.credentialRef
459
+ });
460
+ changes = migrated;
461
+ if (!readNonEmptyApiKey(changes.apiKey)) {
462
+ changes.apiKey = null;
463
+ }
464
+ }
465
+ let localExists = false;
466
+ try {
467
+ const { db } = thunkApi.extra;
468
+ const localData = await db.get(privateKey);
469
+ localExists = !!localData;
470
+ } catch (e) {
471
+ }
472
+ if (localExists) {
473
+ await thunkApi.dispatch(
474
+ patch({
475
+ dbKey: privateKey,
476
+ changes
477
+ })
478
+ ).unwrap();
479
+ } else if (previousAgent) {
480
+ const merged = {
481
+ ...previousAgent,
482
+ ...changes,
483
+ id: normalizedAgentId,
484
+ type: previousAgent.type || "agent" /* AGENT */,
485
+ userId: effectiveUserId
486
+ };
487
+ await thunkApi.dispatch(
488
+ write({
489
+ data: merged,
490
+ customKey: privateKey,
491
+ userId: effectiveUserId
492
+ })
493
+ ).unwrap();
494
+ } else {
495
+ await thunkApi.dispatch(
496
+ patch({
497
+ dbKey: privateKey,
498
+ changes
499
+ })
500
+ ).unwrap();
501
+ }
502
+ if (previousAgent && effectiveUserId !== "local") {
503
+ const wasPublic = !!previousAgent.isPublic;
504
+ const hasIsPublicChange = Object.prototype.hasOwnProperty.call(
505
+ changes,
506
+ "isPublic"
507
+ );
508
+ const nowPublic = hasIsPublicChange ? !!changes.isPublic : wasPublic;
509
+ if (nowPublic) {
510
+ const mergedPublic = {
511
+ ...previousAgent,
512
+ ...changes,
513
+ id: normalizedAgentId,
514
+ type: previousAgent.type || "agent" /* AGENT */,
515
+ userId: effectiveUserId
516
+ };
517
+ await thunkApi.dispatch(
518
+ write({
519
+ data: mergedPublic,
520
+ customKey: publicKey,
521
+ userId: effectiveUserId
522
+ })
523
+ ).unwrap();
524
+ } else if (wasPublic && !nowPublic) {
525
+ await thunkApi.dispatch(remove(publicKey)).unwrap();
526
+ }
527
+ }
528
+ const base = previousAgent ?? {};
529
+ const mergedPrivate = {
530
+ ...base,
531
+ ...changes,
532
+ id: normalizedAgentId,
533
+ type: base.type || "agent" /* AGENT */,
534
+ userId: effectiveUserId
535
+ };
536
+ return mergedPrivate;
537
+ }
538
+ )
539
+ })
540
+ });
541
+ var {
542
+ runLlm,
543
+ runAgent,
544
+ streamAgentChatTurn,
545
+ createAgent,
546
+ updateAgent
547
+ } = slice.actions;
548
+ var agentReducer = slice.reducer;
549
+ var agentSlice_default = agentReducer;
550
+
551
+ export {
552
+ fetchServerSyncedCredential,
553
+ deleteServerSyncedCredential,
554
+ setAgentCredentialBrokerFactoryForTests,
555
+ slice,
556
+ runLlm,
557
+ runAgent,
558
+ streamAgentChatTurn,
559
+ createAgent,
560
+ updateAgent,
561
+ agentSlice_default
562
+ };