nolo-cli 0.1.8 → 0.1.9

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 (239) hide show
  1. package/README.md +32 -0
  2. package/agentRuntimeCommands.ts +3 -3
  3. package/ai/agent/_executeModel.ts +118 -0
  4. package/ai/agent/agentSlice.ts +525 -0
  5. package/ai/agent/appWorkingMemory.ts +126 -0
  6. package/ai/agent/avatarUtils.ts +24 -0
  7. package/ai/agent/buildEditingContext.ts +373 -0
  8. package/ai/agent/buildSystemPrompt.ts +532 -0
  9. package/ai/agent/cleanAgentMessages.ts +140 -0
  10. package/ai/agent/cliChatClient.ts +119 -0
  11. package/ai/agent/cliExecutor.ts +733 -0
  12. package/ai/agent/cliPrompt.ts +10 -0
  13. package/ai/agent/contextCompiler.ts +107 -0
  14. package/ai/agent/contextLayerContract.ts +44 -0
  15. package/ai/agent/createAgentSchema.ts +234 -0
  16. package/ai/agent/executeToolCall.ts +58 -0
  17. package/ai/agent/fetchAgentContexts.ts +42 -0
  18. package/ai/agent/generatePrompt.ts +3 -0
  19. package/ai/agent/getFullChatContextKeys.ts +168 -0
  20. package/ai/agent/hooks/fetchPublicAgents.ts +133 -0
  21. package/ai/agent/hooks/useAgentConfig.ts +61 -0
  22. package/ai/agent/hooks/useAgentDialog.ts +35 -0
  23. package/ai/agent/hooks/useAgentFormValidation.ts +202 -0
  24. package/ai/agent/hooks/usePublicAgents.ts +473 -0
  25. package/ai/agent/machineRunPermissions.ts +95 -0
  26. package/ai/agent/persistMessageWithFixedId.ts +37 -0
  27. package/ai/agent/planSlice.ts +259 -0
  28. package/ai/agent/referenceUtils.ts +229 -0
  29. package/ai/agent/runAgentBackground.ts +238 -0
  30. package/ai/agent/runAgentClientLoop.ts +138 -0
  31. package/ai/agent/runtimeGuidance.ts +97 -0
  32. package/ai/agent/runtimeServerBase.ts +37 -0
  33. package/ai/agent/server/fetchPublicAgents.ts +128 -0
  34. package/ai/agent/startParallelAgentStreams.ts +424 -0
  35. package/ai/agent/startupProtocol.ts +53 -0
  36. package/ai/agent/streamAgentChatTurn.ts +1278 -0
  37. package/ai/agent/streamAgentChatTurnUtils.ts +738 -0
  38. package/ai/agent/types.ts +71 -0
  39. package/ai/agent/utils/imageOutput.ts +33 -0
  40. package/ai/agent/utils/sortUtils.ts +250 -0
  41. package/ai/agent/web/referencePickerUtils.ts +146 -0
  42. package/ai/ai.locale.ts +1075 -0
  43. package/ai/chat/accumulateToolCallChunks.ts +95 -0
  44. package/ai/chat/fetchUtils.native.ts +276 -0
  45. package/ai/chat/fetchUtils.ts +153 -0
  46. package/ai/chat/parseApiError.ts +64 -0
  47. package/ai/chat/parseMultilineSSE.ts +95 -0
  48. package/ai/chat/sendOpenAICompletionsRequest.native.ts +682 -0
  49. package/ai/chat/sendOpenAICompletionsRequest.ts +703 -0
  50. package/ai/chat/sendOpenAIResponseRequest.ts +491 -0
  51. package/ai/chat/shouldUseServerProxy.ts +18 -0
  52. package/ai/chat/sseClient.native.ts +91 -0
  53. package/ai/chat/sseClient.ts +67 -0
  54. package/ai/chat/streamReader.native.ts +31 -0
  55. package/ai/chat/streamReader.ts +62 -0
  56. package/ai/chat/updateTotalUsage.ts +72 -0
  57. package/ai/context/buildReferenceContext.ts +437 -0
  58. package/ai/context/calculateContextUsage.ts +133 -0
  59. package/ai/context/retention.ts +165 -0
  60. package/ai/context/tokenUtils.ts +78 -0
  61. package/ai/index.ts +1 -0
  62. package/ai/llm/calculateGeminiImageTokens.ts +57 -0
  63. package/ai/llm/deepinfra.ts +28 -0
  64. package/ai/llm/fireworks.ts +50 -0
  65. package/ai/llm/generateRequestBody.ts +165 -0
  66. package/ai/llm/getModelContextWindow.ts +84 -0
  67. package/ai/llm/getNoloKey.ts +31 -0
  68. package/ai/llm/getPricing.ts +199 -0
  69. package/ai/llm/hooks/useModelPricing.ts +75 -0
  70. package/ai/llm/imagePricing.ts +40 -0
  71. package/ai/llm/isResponseAPIModel.ts +13 -0
  72. package/ai/llm/mimo.ts +71 -0
  73. package/ai/llm/mistral.ts +22 -0
  74. package/ai/llm/modelAvatar.ts +427 -0
  75. package/ai/llm/models.ts +45 -0
  76. package/ai/llm/openrouterModels.ts +269 -0
  77. package/ai/llm/providers.ts +306 -0
  78. package/ai/llm/reasoningModels.ts +28 -0
  79. package/ai/llm/types.ts +59 -0
  80. package/ai/llm/usageRequestOptions.ts +59 -0
  81. package/ai/memory/capture.ts +148 -0
  82. package/ai/memory/consolidate.ts +104 -0
  83. package/ai/memory/delete.ts +147 -0
  84. package/ai/memory/overlay.ts +84 -0
  85. package/ai/memory/query.ts +38 -0
  86. package/ai/memory/queryShared.ts +160 -0
  87. package/ai/memory/rank.ts +105 -0
  88. package/ai/memory/recentRelationshipRecap.ts +249 -0
  89. package/ai/memory/remember.ts +167 -0
  90. package/ai/memory/runtime.ts +76 -0
  91. package/ai/memory/store.ts +20 -0
  92. package/ai/memory/storeShared.ts +76 -0
  93. package/ai/memory/types.ts +46 -0
  94. package/ai/memory/understanding.ts +349 -0
  95. package/ai/memory/understandingGreeting.ts +264 -0
  96. package/ai/messages/type.ts +20 -0
  97. package/ai/policy/personalizationDialog.ts +333 -0
  98. package/ai/policy/runtimePolicy.ts +440 -0
  99. package/ai/policy/selfUpdateFields.ts +48 -0
  100. package/ai/policy/types.ts +64 -0
  101. package/ai/skills/referenceRuntime.ts +274 -0
  102. package/ai/skills/skillDiagnostics.ts +251 -0
  103. package/ai/skills/skillDocBuilder.ts +139 -0
  104. package/ai/skills/skillDocProtocol.ts +434 -0
  105. package/ai/skills/skillReferenceSummary.ts +63 -0
  106. package/ai/skills/skillSummaryMarker.ts +26 -0
  107. package/ai/token/calculatePrice.ts +544 -0
  108. package/ai/token/db.ts +98 -0
  109. package/ai/token/externalToolCost.ts +330 -0
  110. package/ai/token/hooks/useRecords.ts +65 -0
  111. package/ai/token/missingUsageEstimate.ts +42 -0
  112. package/ai/token/modelUsageQuery.ts +252 -0
  113. package/ai/token/normalizeUsage.ts +84 -0
  114. package/ai/token/openaiImageGenerationUsage.ts +56 -0
  115. package/ai/token/prepareTokenUsageData.ts +88 -0
  116. package/ai/token/query.ts +88 -0
  117. package/ai/token/queryUserTokens.ts +59 -0
  118. package/ai/token/resolveBillingTarget.ts +52 -0
  119. package/ai/token/saveTokenRecord.ts +53 -0
  120. package/ai/token/serverDialogProjection.ts +78 -0
  121. package/ai/token/serverTokenWriter.ts +143 -0
  122. package/ai/token/stats.ts +21 -0
  123. package/ai/token/tokenThunks.ts +24 -0
  124. package/ai/token/types.ts +93 -0
  125. package/ai/tools/agent/agentTools.ts +176 -0
  126. package/ai/tools/agent/agentUpdateShared.ts +311 -0
  127. package/ai/tools/agent/callAgentTool.ts +139 -0
  128. package/ai/tools/agent/createAgentTool.ts +512 -0
  129. package/ai/tools/agent/createDialogTool.ts +69 -0
  130. package/ai/tools/agent/createSkillAgentTool.ts +62 -0
  131. package/ai/tools/agent/parallelBudget.ts +221 -0
  132. package/ai/tools/agent/presets/appBuilderPreset.ts +145 -0
  133. package/ai/tools/agent/runLlmTool.ts +96 -0
  134. package/ai/tools/agent/runStreamingAgentTool.ts +73 -0
  135. package/ai/tools/agent/skillAgentArgs.ts +106 -0
  136. package/ai/tools/agent/skillAgentPreset.ts +89 -0
  137. package/ai/tools/agent/streamParallelAgentsTool.ts +122 -0
  138. package/ai/tools/agent/updateAgentTool.ts +96 -0
  139. package/ai/tools/agent/updateSelfTool.ts +113 -0
  140. package/ai/tools/amazonProductScraperTool.ts +86 -0
  141. package/ai/tools/apifyActorClient.ts +45 -0
  142. package/ai/tools/appEditGuard.ts +372 -0
  143. package/ai/tools/appReadSnapshot.ts +153 -0
  144. package/ai/tools/appTools.ts +1549 -0
  145. package/ai/tools/applyEditTool.ts +256 -0
  146. package/ai/tools/applyLineEditsTool.ts +312 -0
  147. package/ai/tools/browserTools/click.ts +33 -0
  148. package/ai/tools/browserTools/closeSession.ts +29 -0
  149. package/ai/tools/browserTools/common.ts +27 -0
  150. package/ai/tools/browserTools/openSession.ts +48 -0
  151. package/ai/tools/browserTools/readContent.ts +38 -0
  152. package/ai/tools/browserTools/selectOption.ts +46 -0
  153. package/ai/tools/browserTools/typeText.ts +42 -0
  154. package/ai/tools/category/createCategoryTool.ts +66 -0
  155. package/ai/tools/category/queryContentsByCategoryTool.ts +69 -0
  156. package/ai/tools/category/updateContentCategoryTool.ts +75 -0
  157. package/ai/tools/cfBrowserTools.ts +319 -0
  158. package/ai/tools/cfSpeechToTextTool.ts +49 -0
  159. package/ai/tools/checkEnvTool.ts +65 -0
  160. package/ai/tools/cloudflareCrawlTool.ts +289 -0
  161. package/ai/tools/codeSearchTool.ts +111 -0
  162. package/ai/tools/codeTools.ts +101 -0
  163. package/ai/tools/createDocTool.ts +132 -0
  164. package/ai/tools/createPlanTool.ts +999 -0
  165. package/ai/tools/createSkillDocTool.ts +155 -0
  166. package/ai/tools/createWorkflowTool.ts +154 -0
  167. package/ai/tools/deepseekOcrTool.ts +34 -0
  168. package/ai/tools/delayTool.ts +31 -0
  169. package/ai/tools/deleteSpacesTool.ts +325 -0
  170. package/ai/tools/deleteSpacesToolModel.ts +159 -0
  171. package/ai/tools/devReloadUtils.ts +29 -0
  172. package/ai/tools/dialogMessageSearch.ts +137 -0
  173. package/ai/tools/doctorSkillTool.ts +72 -0
  174. package/ai/tools/ecommerceScraperTool.ts +86 -0
  175. package/ai/tools/emailTools.ts +549 -0
  176. package/ai/tools/evalSkillTool.ts +92 -0
  177. package/ai/tools/exaSearchTool.ts +64 -0
  178. package/ai/tools/execBashTool.ts +379 -0
  179. package/ai/tools/executeSqlTool.ts +192 -0
  180. package/ai/tools/fetchWebpageSupport.ts +309 -0
  181. package/ai/tools/fetchWebpageTool.ts +84 -0
  182. package/ai/tools/geminiImagePreviewTool.ts +361 -0
  183. package/ai/tools/generateDocxTool.ts +215 -0
  184. package/ai/tools/googleSearchScraperTool.ts +106 -0
  185. package/ai/tools/importDataTool.ts +133 -0
  186. package/ai/tools/importSkillTool.ts +162 -0
  187. package/ai/tools/index.ts +1858 -0
  188. package/ai/tools/listFilesTool.ts +82 -0
  189. package/ai/tools/listUserSpacesTool.ts +113 -0
  190. package/ai/tools/modelUsageTools.ts +142 -0
  191. package/ai/tools/olmOcrTool.ts +34 -0
  192. package/ai/tools/openaiImageTool.ts +218 -0
  193. package/ai/tools/paddleOcrTool.ts +34 -0
  194. package/ai/tools/prepareTools.ts +23 -0
  195. package/ai/tools/readDocTool.ts +84 -0
  196. package/ai/tools/readFileTool.ts +211 -0
  197. package/ai/tools/readTool.ts +163 -0
  198. package/ai/tools/readXPostTool.ts +233 -0
  199. package/ai/tools/rememberMemoryTool.ts +84 -0
  200. package/ai/tools/remotionVideoTool.ts +151 -0
  201. package/ai/tools/searchDialogMessagesTool.ts +222 -0
  202. package/ai/tools/searchRepoTool.ts +115 -0
  203. package/ai/tools/searchWorkspaceTool.ts +259 -0
  204. package/ai/tools/skillFollowup.ts +86 -0
  205. package/ai/tools/surfWeatherTool.ts +169 -0
  206. package/ai/tools/table/addTableRowTool.ts +217 -0
  207. package/ai/tools/table/createTableTool.ts +315 -0
  208. package/ai/tools/table/rowTools.ts +366 -0
  209. package/ai/tools/table/schemaTools.ts +244 -0
  210. package/ai/tools/table/shareTableTool.ts +148 -0
  211. package/ai/tools/table/toolShared.ts +129 -0
  212. package/ai/tools/toolApiClient.ts +198 -0
  213. package/ai/tools/toolNameAliases.ts +57 -0
  214. package/ai/tools/toolResultError.ts +42 -0
  215. package/ai/tools/toolRunSlice.ts +303 -0
  216. package/ai/tools/toolSchemaCompatibility.ts +53 -0
  217. package/ai/tools/toolVisibility.ts +4 -0
  218. package/ai/tools/types.ts +20 -0
  219. package/ai/tools/uiAskChoiceTool.ts +104 -0
  220. package/ai/tools/updateContentTitleTool.ts +84 -0
  221. package/ai/tools/updateDocTool.ts +105 -0
  222. package/ai/tools/updateUserPreferenceProfileTool.ts +145 -0
  223. package/ai/tools/whisperTool.ts +77 -0
  224. package/ai/tools/writeFileTool.ts +210 -0
  225. package/ai/tools/youtubeScraperTool.ts +116 -0
  226. package/ai/tools/ziweiChartTool.ts +678 -0
  227. package/ai/types.ts +55 -0
  228. package/ai/workflow/workflowExecutor.ts +323 -0
  229. package/ai/workflow/workflowSlice.ts +73 -0
  230. package/ai/workflow/workflowTypes.ts +106 -0
  231. package/client/compactDialog.ts +222 -0
  232. package/connector-experimental/capabilities.ts +73 -0
  233. package/connector-experimental/codexBinary.ts +41 -0
  234. package/connector-experimental/heartbeatLoop.ts +22 -0
  235. package/connector-experimental/index.ts +5 -0
  236. package/connector-experimental/machineInfo.ts +46 -0
  237. package/connector-experimental/protocol.ts +54 -0
  238. package/machineCommands.ts +4 -4
  239. package/package.json +8 -6
@@ -0,0 +1,440 @@
1
+ import type { Agent } from "app/types";
2
+ import type {
3
+ AgentBasePolicy,
4
+ DialogPolicyState,
5
+ KnowledgeCaptureLevel,
6
+ SelfEvolutionMode,
7
+ SpaceContextLevel,
8
+ TonePreset,
9
+ ToneResolutionMode,
10
+ UserPreferenceProfile,
11
+ } from "./types";
12
+ import {
13
+ DEFAULT_AGENT_BASE_POLICY,
14
+ DEFAULT_USER_PREFERENCE_PROFILE,
15
+ } from "./types";
16
+
17
+ export type CaptureIntent = "none" | "possible" | "strong";
18
+ export type SpaceNeed = "none" | "light" | "deep";
19
+ export type KnowledgeCaptureMode = "blocked" | "ask" | "suggest" | "auto";
20
+
21
+ export interface ResolvedRuntimePolicy {
22
+ tonePreset: TonePreset;
23
+ toneResolutionMode: ToneResolutionMode;
24
+ knowledgeCapture: {
25
+ level: KnowledgeCaptureLevel;
26
+ mode: KnowledgeCaptureMode;
27
+ explicitRequest: boolean;
28
+ maxAutoCreatesPerDialog: number;
29
+ currentAutoCreates: number;
30
+ captureIntent: CaptureIntent;
31
+ };
32
+ spaceContext: {
33
+ level: SpaceContextLevel;
34
+ explicitRequest: boolean;
35
+ need: SpaceNeed;
36
+ maxReadCallsPerTurn: number;
37
+ maxReadCallsPerDialog: number;
38
+ currentAutoReads: number;
39
+ preloadSummaryCount: number;
40
+ preloadBudgetRatio: number;
41
+ };
42
+ selfEvolutionMode: SelfEvolutionMode;
43
+ }
44
+
45
+ const clampLevel = <T extends 1 | 2 | 3 | 4>(
46
+ value: unknown,
47
+ fallback: T,
48
+ ): T => {
49
+ const n = Number(value);
50
+ if (n === 1 || n === 2 || n === 3 || n === 4) {
51
+ return n as T;
52
+ }
53
+ return fallback;
54
+ };
55
+
56
+ const normalizeTonePreset = (value: unknown, fallback: TonePreset): TonePreset => {
57
+ switch (value) {
58
+ case "professional":
59
+ case "friendly":
60
+ case "direct":
61
+ case "pragmatic":
62
+ case "default":
63
+ return value;
64
+ default:
65
+ return fallback;
66
+ }
67
+ };
68
+
69
+ const normalizeToneResolutionMode = (
70
+ value: unknown,
71
+ fallback: ToneResolutionMode,
72
+ ): ToneResolutionMode => {
73
+ switch (value) {
74
+ case "agent_first":
75
+ case "user_first":
76
+ case "blend":
77
+ return value;
78
+ default:
79
+ return fallback;
80
+ }
81
+ };
82
+
83
+ const normalizeSelfEvolutionMode = (
84
+ value: unknown,
85
+ fallback: SelfEvolutionMode,
86
+ ): SelfEvolutionMode => {
87
+ switch (value) {
88
+ case "none":
89
+ case "knowledge_only":
90
+ case "prompt_and_refs":
91
+ case "full":
92
+ return value;
93
+ default:
94
+ return fallback;
95
+ }
96
+ };
97
+
98
+ export const resolveAgentBasePolicy = (agentConfig?: Agent | any): AgentBasePolicy => {
99
+ const raw = agentConfig?.basePolicy ?? {};
100
+
101
+ return {
102
+ version: 1,
103
+ tone: {
104
+ preset: normalizeTonePreset(
105
+ raw?.tone?.preset,
106
+ DEFAULT_AGENT_BASE_POLICY.tone?.preset ?? "default",
107
+ ),
108
+ resolutionMode: normalizeToneResolutionMode(
109
+ raw?.tone?.resolutionMode,
110
+ DEFAULT_AGENT_BASE_POLICY.tone?.resolutionMode ?? "blend",
111
+ ),
112
+ },
113
+ knowledgeCaptureMaxLevel: clampLevel(
114
+ raw?.knowledgeCaptureMaxLevel,
115
+ DEFAULT_AGENT_BASE_POLICY.knowledgeCaptureMaxLevel,
116
+ ),
117
+ spaceContextMaxLevel: clampLevel(
118
+ raw?.spaceContextMaxLevel,
119
+ DEFAULT_AGENT_BASE_POLICY.spaceContextMaxLevel,
120
+ ),
121
+ selfEvolutionMode: normalizeSelfEvolutionMode(
122
+ raw?.selfEvolutionMode,
123
+ DEFAULT_AGENT_BASE_POLICY.selfEvolutionMode,
124
+ ),
125
+ };
126
+ };
127
+
128
+ export const resolveUserPreferenceProfile = (
129
+ settingsRecord?: Record<string, any> | null,
130
+ ): UserPreferenceProfile => {
131
+ const fallbackSpaceLevel =
132
+ settingsRecord?.enableReadCurrentSpace === false
133
+ ? 1
134
+ : DEFAULT_USER_PREFERENCE_PROFILE.spaceContextLevel;
135
+
136
+ return {
137
+ version: 1,
138
+ tone: {
139
+ preset: normalizeTonePreset(
140
+ settingsRecord?.userTonePreset,
141
+ DEFAULT_USER_PREFERENCE_PROFILE.tone?.preset ?? "default",
142
+ ),
143
+ },
144
+ knowledgeCaptureLevel: clampLevel(
145
+ settingsRecord?.knowledgeCaptureLevel,
146
+ DEFAULT_USER_PREFERENCE_PROFILE.knowledgeCaptureLevel,
147
+ ),
148
+ spaceContextLevel: clampLevel(
149
+ settingsRecord?.spaceContextLevel,
150
+ fallbackSpaceLevel,
151
+ ),
152
+ };
153
+ };
154
+
155
+ const containsAny = (text: string, patterns: string[]): boolean =>
156
+ patterns.some((pattern) => text.includes(pattern));
157
+
158
+ export const inferCaptureIntent = (userInput: string): CaptureIntent => {
159
+ const normalized = userInput.toLowerCase();
160
+
161
+ if (
162
+ containsAny(normalized, [
163
+ "保存",
164
+ "存一下",
165
+ "沉淀",
166
+ "记住",
167
+ "写入文档",
168
+ "建文档",
169
+ "创建文档",
170
+ "导入 skill",
171
+ "导入这个 skill",
172
+ "确认导入",
173
+ "确认导入这个 skill",
174
+ "导入这个技能",
175
+ "确认导入这个技能",
176
+ "创建 skill",
177
+ "创建这个 skill",
178
+ "skill 文档",
179
+ "创建表格",
180
+ "建表",
181
+ "建个表",
182
+ "做个表",
183
+ "做一张表",
184
+ "整理成表",
185
+ "整理成一张表",
186
+ "建立一个表",
187
+ "建立一张表",
188
+ "建立成表",
189
+ "建立成一张表",
190
+ "创建这个表格",
191
+ "确认创建",
192
+ "确认创建这张表格",
193
+ "create doc",
194
+ "import skill",
195
+ "confirm import",
196
+ "create skill",
197
+ "create table",
198
+ "save this",
199
+ "remember this",
200
+ "document this",
201
+ ])
202
+ ) {
203
+ return "strong";
204
+ }
205
+
206
+ if (
207
+ containsAny(normalized, [
208
+ "总结",
209
+ "方案",
210
+ "调研",
211
+ "规范",
212
+ "会议纪要",
213
+ "总结一下",
214
+ "research",
215
+ "proposal",
216
+ "spec",
217
+ "summary",
218
+ ])
219
+ ) {
220
+ return "possible";
221
+ }
222
+
223
+ return "none";
224
+ };
225
+
226
+ export const inferSpaceNeed = (userInput: string): SpaceNeed => {
227
+ const normalized = userInput.toLowerCase();
228
+
229
+ if (
230
+ containsAny(normalized, [
231
+ "当前空间",
232
+ "这个空间",
233
+ "这些文档",
234
+ "根据空间",
235
+ "workspace",
236
+ "current space",
237
+ "these docs",
238
+ "based on the docs",
239
+ ])
240
+ ) {
241
+ return "deep";
242
+ }
243
+
244
+ if (
245
+ containsAny(normalized, [
246
+ "文档",
247
+ "文件",
248
+ "页面",
249
+ "目录",
250
+ "page",
251
+ "pages",
252
+ "docs",
253
+ "files",
254
+ "目录结构",
255
+ ])
256
+ ) {
257
+ return "light";
258
+ }
259
+
260
+ return "none";
261
+ };
262
+
263
+ const resolveTonePreset = (
264
+ agentTone: TonePreset,
265
+ userTone: TonePreset,
266
+ mode: ToneResolutionMode,
267
+ ): TonePreset => {
268
+ if (mode === "agent_first") {
269
+ return agentTone === "default" ? userTone : agentTone;
270
+ }
271
+ if (mode === "user_first") {
272
+ return userTone === "default" ? agentTone : userTone;
273
+ }
274
+ if (userTone !== "default") return userTone;
275
+ return agentTone;
276
+ };
277
+
278
+ const maxReadCallsPerTurnByLevel = (
279
+ level: SpaceContextLevel,
280
+ need: SpaceNeed,
281
+ ): number => {
282
+ if (need === "deep") {
283
+ return { 1: 2, 2: 3, 3: 4, 4: 6 }[level];
284
+ }
285
+ if (need === "light") {
286
+ return { 1: 1, 2: 2, 3: 3, 4: 4 }[level];
287
+ }
288
+ return { 1: 0, 2: 0, 3: 1, 4: 2 }[level];
289
+ };
290
+
291
+ const maxReadCallsPerDialogByLevel = (
292
+ level: SpaceContextLevel,
293
+ need: SpaceNeed,
294
+ ): number => {
295
+ if (need === "deep") {
296
+ return { 1: 3, 2: 4, 3: 6, 4: 8 }[level];
297
+ }
298
+ if (need === "light") {
299
+ return { 1: 2, 2: 3, 3: 4, 4: 6 }[level];
300
+ }
301
+ return { 1: 0, 2: 0, 3: 2, 4: 4 }[level];
302
+ };
303
+
304
+ const preloadSummaryCountByLevel = (level: SpaceContextLevel): number =>
305
+ ({ 1: 0, 2: 0, 3: 8, 4: 16 }[level]);
306
+
307
+ const preloadBudgetRatioByLevel = (level: SpaceContextLevel): number =>
308
+ ({ 1: 0, 2: 0.01, 3: 0.04, 4: 0.08 }[level]);
309
+
310
+ export const resolveRuntimePolicy = (params: {
311
+ agentConfig?: Agent | any;
312
+ settingsRecord?: Record<string, any> | null;
313
+ userInput: string;
314
+ dialogPolicyState?: DialogPolicyState | null;
315
+ }): ResolvedRuntimePolicy => {
316
+ const { agentConfig, settingsRecord, userInput, dialogPolicyState } = params;
317
+
318
+ const agentBasePolicy = resolveAgentBasePolicy(agentConfig);
319
+ const userPreferenceProfile = resolveUserPreferenceProfile(settingsRecord);
320
+
321
+ const knowledgeLevel = Math.min(
322
+ agentBasePolicy.knowledgeCaptureMaxLevel,
323
+ userPreferenceProfile.knowledgeCaptureLevel,
324
+ ) as KnowledgeCaptureLevel;
325
+ const spaceLevel = Math.min(
326
+ agentBasePolicy.spaceContextMaxLevel,
327
+ userPreferenceProfile.spaceContextLevel,
328
+ ) as SpaceContextLevel;
329
+
330
+ const captureIntent = inferCaptureIntent(userInput);
331
+ const spaceNeed = inferSpaceNeed(userInput);
332
+ const explicitCaptureRequest = captureIntent === "strong";
333
+ const explicitSpaceRequest = spaceNeed !== "none";
334
+
335
+ const currentAutoCreates = Math.max(
336
+ 0,
337
+ Number(dialogPolicyState?.autoKnowledgeCaptureCount ?? 0),
338
+ );
339
+ const currentAutoReads = Math.max(
340
+ 0,
341
+ Number(dialogPolicyState?.autoSpaceReadCount ?? 0),
342
+ );
343
+
344
+ let knowledgeMode: KnowledgeCaptureMode = "blocked";
345
+ if (explicitCaptureRequest) {
346
+ knowledgeMode = "auto";
347
+ } else if (knowledgeLevel === 2) {
348
+ knowledgeMode = "ask";
349
+ } else if (knowledgeLevel === 3) {
350
+ knowledgeMode = "suggest";
351
+ } else if (knowledgeLevel >= 4 && captureIntent !== "none") {
352
+ knowledgeMode = "auto";
353
+ }
354
+
355
+ return {
356
+ tonePreset: resolveTonePreset(
357
+ agentBasePolicy.tone?.preset ?? "default",
358
+ userPreferenceProfile.tone?.preset ?? "default",
359
+ agentBasePolicy.tone?.resolutionMode ?? "blend",
360
+ ),
361
+ toneResolutionMode: agentBasePolicy.tone?.resolutionMode ?? "blend",
362
+ knowledgeCapture: {
363
+ level: knowledgeLevel,
364
+ mode: knowledgeMode,
365
+ explicitRequest: explicitCaptureRequest,
366
+ maxAutoCreatesPerDialog: explicitCaptureRequest ? 99 : 1,
367
+ currentAutoCreates,
368
+ captureIntent,
369
+ },
370
+ spaceContext: {
371
+ level: spaceLevel,
372
+ explicitRequest: explicitSpaceRequest,
373
+ need: spaceNeed,
374
+ maxReadCallsPerTurn: maxReadCallsPerTurnByLevel(spaceLevel, spaceNeed),
375
+ maxReadCallsPerDialog: maxReadCallsPerDialogByLevel(spaceLevel, spaceNeed),
376
+ currentAutoReads,
377
+ preloadSummaryCount: preloadSummaryCountByLevel(spaceLevel),
378
+ preloadBudgetRatio: preloadBudgetRatioByLevel(spaceLevel),
379
+ },
380
+ selfEvolutionMode: agentBasePolicy.selfEvolutionMode,
381
+ };
382
+ };
383
+
384
+ export const buildUserPolicyContext = (
385
+ policy: ResolvedRuntimePolicy,
386
+ ): string => {
387
+ const lines = [
388
+ `用户语气偏好:${policy.tonePreset}`,
389
+ `知识沉淀级别:${policy.knowledgeCapture.level}(当前模式:${policy.knowledgeCapture.mode})`,
390
+ `空间上下文级别:${policy.spaceContext.level}(本轮最多自动 read ${policy.spaceContext.maxReadCallsPerTurn} 次)`,
391
+ "如果工具返回 policy limit,不要重试同一路径,应改为解释限制、先给答案或向用户确认。",
392
+ ];
393
+
394
+ if (policy.knowledgeCapture.mode === "ask") {
395
+ lines.push("当前用户偏好要求:创建知识文档前先询问用户。");
396
+ } else if (policy.knowledgeCapture.mode === "suggest") {
397
+ lines.push("当前用户偏好要求:先完成回答,再建议是否沉淀成文档/表格。");
398
+ }
399
+
400
+ return lines.join("\n");
401
+ };
402
+
403
+ export const buildStaticUserPolicyContext = (params: {
404
+ agentConfig?: Agent | any;
405
+ settingsRecord?: Record<string, any> | null;
406
+ }): string => {
407
+ const agentBasePolicy = resolveAgentBasePolicy(params.agentConfig);
408
+ const userPreferenceProfile = resolveUserPreferenceProfile(params.settingsRecord);
409
+ const tonePreset = resolveTonePreset(
410
+ agentBasePolicy.tone?.preset ?? "default",
411
+ userPreferenceProfile.tone?.preset ?? "default",
412
+ agentBasePolicy.tone?.resolutionMode ?? "blend",
413
+ );
414
+
415
+ return [
416
+ `用户语气偏好:${tonePreset}`,
417
+ `知识沉淀级别:${Math.min(agentBasePolicy.knowledgeCaptureMaxLevel, userPreferenceProfile.knowledgeCaptureLevel)}`,
418
+ `空间上下文级别:${Math.min(agentBasePolicy.spaceContextMaxLevel, userPreferenceProfile.spaceContextLevel)}`,
419
+ "请尽量保留 agent 自身角色设定,同时按用户偏好调整表达和自动化程度。",
420
+ ].join("\n");
421
+ };
422
+
423
+ export const resolveSpaceContextPreloadPlan = (level: SpaceContextLevel) => ({
424
+ preloadSummaryCount: preloadSummaryCountByLevel(level),
425
+ preloadBudgetRatio: preloadBudgetRatioByLevel(level),
426
+ includeRecentContent: level >= 3,
427
+ });
428
+
429
+ export const isBudgetedWorkspaceReadKey = (dbKey: string): boolean => {
430
+ const normalized = dbKey.toLowerCase();
431
+ return (
432
+ normalized.startsWith("page-") ||
433
+ normalized.startsWith("dialog-") ||
434
+ normalized.startsWith("space-") ||
435
+ normalized.startsWith("table-") ||
436
+ normalized.startsWith("table_row-") ||
437
+ normalized.startsWith("row-") ||
438
+ normalized.startsWith("meta-")
439
+ );
440
+ };
@@ -0,0 +1,48 @@
1
+ export const AGENT_UPDATE_FIELD_NAMES = [
2
+ "name",
3
+ "model",
4
+ "provider",
5
+ "prompt",
6
+ "introduction",
7
+ "greeting",
8
+ "isPublic",
9
+ "tags",
10
+ "tools",
11
+ "references",
12
+ "temperature",
13
+ "top_p",
14
+ "frequency_penalty",
15
+ "presence_penalty",
16
+ "max_tokens",
17
+ "reasoning_effort",
18
+ ] as const;
19
+
20
+ export type AgentUpdateField = (typeof AGENT_UPDATE_FIELD_NAMES)[number];
21
+
22
+ export const DEFAULT_AUTO_APPROVED_SELF_UPDATE_FIELDS: AgentUpdateField[] = [
23
+ "greeting",
24
+ "introduction",
25
+ "tags",
26
+ ];
27
+
28
+ const VALID_AGENT_UPDATE_FIELDS = new Set<string>(AGENT_UPDATE_FIELD_NAMES);
29
+
30
+ export const normalizeAgentUpdateFieldList = (
31
+ value: unknown,
32
+ fallback: readonly AgentUpdateField[] = DEFAULT_AUTO_APPROVED_SELF_UPDATE_FIELDS,
33
+ ): AgentUpdateField[] => {
34
+ if (!Array.isArray(value)) {
35
+ return [...fallback];
36
+ }
37
+
38
+ const normalized = Array.from(
39
+ new Set(
40
+ value.filter(
41
+ (item): item is AgentUpdateField =>
42
+ typeof item === "string" && VALID_AGENT_UPDATE_FIELDS.has(item),
43
+ ),
44
+ ),
45
+ );
46
+
47
+ return normalized;
48
+ };
@@ -0,0 +1,64 @@
1
+ export type TonePreset =
2
+ | "default"
3
+ | "professional"
4
+ | "friendly"
5
+ | "direct"
6
+ | "pragmatic";
7
+
8
+ export type ToneResolutionMode = "agent_first" | "user_first" | "blend";
9
+
10
+ export type KnowledgeCaptureLevel = 1 | 2 | 3 | 4;
11
+ export type SpaceContextLevel = 1 | 2 | 3 | 4;
12
+
13
+ export type SelfEvolutionMode =
14
+ | "none"
15
+ | "knowledge_only"
16
+ | "prompt_and_refs"
17
+ | "full";
18
+
19
+ export interface AgentBasePolicy {
20
+ version: 1;
21
+ tone?: {
22
+ preset?: TonePreset;
23
+ resolutionMode?: ToneResolutionMode;
24
+ };
25
+ knowledgeCaptureMaxLevel: KnowledgeCaptureLevel;
26
+ spaceContextMaxLevel: SpaceContextLevel;
27
+ selfEvolutionMode: SelfEvolutionMode;
28
+ }
29
+
30
+ export interface UserPreferenceProfile {
31
+ version: 1;
32
+ tone?: {
33
+ preset?: TonePreset;
34
+ };
35
+ knowledgeCaptureLevel: KnowledgeCaptureLevel;
36
+ spaceContextLevel: SpaceContextLevel;
37
+ }
38
+
39
+ export interface DialogPolicyState {
40
+ autoKnowledgeCaptureCount?: number;
41
+ autoSpaceReadCount?: number;
42
+ updatedAt?: number;
43
+ }
44
+
45
+ export const DEFAULT_AGENT_BASE_POLICY: AgentBasePolicy = {
46
+ version: 1,
47
+ tone: {
48
+ preset: "default",
49
+ resolutionMode: "blend",
50
+ },
51
+ knowledgeCaptureMaxLevel: 4,
52
+ spaceContextMaxLevel: 4,
53
+ selfEvolutionMode: "knowledge_only",
54
+ };
55
+
56
+ export const DEFAULT_USER_PREFERENCE_PROFILE: UserPreferenceProfile = {
57
+ version: 1,
58
+ tone: {
59
+ preset: "default",
60
+ },
61
+ knowledgeCaptureLevel: 2,
62
+ spaceContextLevel: 3,
63
+ };
64
+