nolo-cli 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/agent-runtime/hostAdapter.ts +53 -0
- package/agent-runtime/index.ts +28 -0
- package/agent-runtime/localLoop.ts +62 -0
- package/agent-runtime/runtimeDecision.ts +70 -0
- package/agent-runtime/types.ts +87 -0
- package/agentRunCommand.ts +104 -0
- package/agentRuntimeCommands.ts +139 -22
- package/agentRuntimeLocal.ts +7 -0
- package/ai/agent/_executeModel.ts +118 -0
- package/ai/agent/agentSlice.ts +544 -1
- package/ai/agent/appWorkingMemory.ts +126 -0
- package/ai/agent/avatarUtils.ts +24 -0
- package/ai/agent/buildEditingContext.ts +373 -0
- package/ai/agent/buildSystemPrompt.ts +532 -0
- package/ai/agent/cleanAgentMessages.ts +140 -0
- package/ai/agent/cliChatClient.ts +119 -0
- package/ai/agent/contextCompiler.ts +107 -0
- package/ai/agent/contextLayerContract.ts +44 -0
- package/ai/agent/createAgentSchema.ts +234 -0
- package/ai/agent/executeToolCall.ts +58 -0
- package/ai/agent/fetchAgentContexts.ts +42 -0
- package/ai/agent/generatePrompt.ts +3 -0
- package/ai/agent/getFullChatContextKeys.ts +168 -0
- package/ai/agent/hooks/fetchPublicAgents.ts +133 -0
- package/ai/agent/hooks/useAgentConfig.ts +61 -0
- package/ai/agent/hooks/useAgentDialog.ts +35 -0
- package/ai/agent/hooks/useAgentFormValidation.ts +202 -0
- package/ai/agent/hooks/usePublicAgents.ts +473 -0
- package/ai/agent/persistMessageWithFixedId.ts +37 -0
- package/ai/agent/planSlice.ts +259 -0
- package/ai/agent/referenceUtils.ts +229 -0
- package/ai/agent/runAgentBackground.ts +238 -0
- package/ai/agent/runAgentClientLoop.ts +138 -0
- package/ai/agent/runtimeGuidance.ts +97 -0
- package/ai/agent/runtimeServerBase.ts +37 -0
- package/ai/agent/server/fetchPublicAgents.ts +128 -0
- package/ai/agent/startParallelAgentStreams.ts +424 -0
- package/ai/agent/startupProtocol.ts +53 -0
- package/ai/agent/streamAgentChatTurn.ts +1299 -0
- package/ai/agent/streamAgentChatTurnUtils.ts +738 -0
- package/ai/agent/types.ts +71 -0
- package/ai/agent/utils/imageOutput.ts +39 -0
- package/ai/agent/utils/publicImageAgentMode.ts +26 -0
- package/ai/agent/utils/sortUtils.ts +250 -0
- package/ai/agent/web/referencePickerUtils.ts +146 -0
- package/ai/ai.locale.ts +1083 -0
- package/ai/chat/accumulateToolCallChunks.ts +95 -0
- package/ai/chat/fetchUtils.native.ts +276 -0
- package/ai/chat/fetchUtils.ts +153 -0
- package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
- package/ai/chat/parseApiError.ts +64 -0
- package/ai/chat/parseMultilineSSE.ts +95 -0
- package/ai/chat/sendOpenAICompletionsRequest.native.ts +682 -0
- package/ai/chat/sendOpenAICompletionsRequest.ts +712 -0
- package/ai/chat/sendOpenAIResponseRequest.ts +512 -0
- package/ai/chat/shouldUseServerProxy.ts +18 -0
- package/ai/chat/sseClient.native.ts +91 -0
- package/ai/chat/sseClient.ts +67 -0
- package/ai/chat/streamReader.native.ts +31 -0
- package/ai/chat/streamReader.ts +62 -0
- package/ai/chat/updateTotalUsage.ts +72 -0
- package/ai/context/buildReferenceContext.ts +437 -0
- package/ai/context/calculateContextUsage.ts +133 -0
- package/ai/context/retention.ts +165 -0
- package/ai/context/tokenUtils.ts +78 -0
- package/ai/index.ts +1 -1
- package/ai/llm/agentCapabilities.ts +74 -0
- package/ai/llm/calculateGeminiImageTokens.ts +57 -0
- package/ai/llm/deepinfra.ts +28 -0
- package/ai/llm/fireworks.ts +68 -0
- package/ai/llm/generateRequestBody.ts +165 -0
- package/ai/llm/getModelContextWindow.ts +84 -0
- package/ai/llm/getNoloKey.ts +37 -0
- package/ai/llm/getPricing.ts +232 -0
- package/ai/llm/hooks/useModelPricing.ts +75 -0
- package/ai/llm/imagePricing.ts +66 -0
- package/ai/llm/isResponseAPIModel.ts +13 -0
- package/ai/llm/kimi.ts +18 -0
- package/ai/llm/mimo.ts +71 -0
- package/ai/llm/mistral.ts +22 -0
- package/ai/llm/modelAvatar.ts +427 -0
- package/ai/llm/models.ts +45 -0
- package/ai/llm/openrouterModels.ts +141 -0
- package/ai/llm/providers.ts +307 -0
- package/ai/llm/reasoningModels.ts +28 -0
- package/ai/llm/types.ts +59 -0
- package/ai/llm/usageRequestOptions.ts +59 -0
- package/ai/memory/capture.ts +148 -0
- package/ai/memory/consolidate.ts +104 -0
- package/ai/memory/delete.ts +147 -0
- package/ai/memory/overlay.ts +84 -0
- package/ai/memory/query.ts +38 -0
- package/ai/memory/queryShared.ts +160 -0
- package/ai/memory/rank.ts +105 -0
- package/ai/memory/recentRelationshipRecap.ts +247 -0
- package/ai/memory/remember.ts +167 -0
- package/ai/memory/runtime.ts +76 -0
- package/ai/memory/store.ts +20 -0
- package/ai/memory/storeShared.ts +76 -0
- package/ai/memory/types.ts +46 -0
- package/ai/memory/understanding.ts +349 -0
- package/ai/memory/understandingGreeting.ts +264 -0
- package/ai/messages/type.ts +20 -0
- package/ai/policy/personalizationDialog.ts +333 -0
- package/ai/policy/runtimePolicy.ts +440 -0
- package/ai/policy/selfUpdateFields.ts +48 -0
- package/ai/policy/types.ts +64 -0
- package/ai/skills/referenceRuntime.ts +274 -0
- package/ai/skills/skillDiagnostics.ts +251 -0
- package/ai/skills/skillDocBuilder.ts +139 -0
- package/ai/skills/skillDocProtocol.ts +434 -0
- package/ai/skills/skillReferenceSummary.ts +63 -0
- package/ai/skills/skillSummaryMarker.ts +26 -0
- package/ai/token/calculatePrice.ts +546 -0
- package/ai/token/db.ts +98 -0
- package/ai/token/externalToolCost.ts +321 -0
- package/ai/token/hooks/useRecords.ts +65 -0
- package/ai/token/missingUsageEstimate.ts +42 -0
- package/ai/token/modelUsageQuery.ts +252 -0
- package/ai/token/normalizeUsage.ts +84 -0
- package/ai/token/openaiImageGenerationUsage.ts +56 -0
- package/ai/token/prepareTokenUsageData.ts +88 -0
- package/ai/token/query.ts +88 -0
- package/ai/token/queryUserTokens.ts +59 -0
- package/ai/token/resolveBillingTarget.ts +52 -0
- package/ai/token/saveTokenRecord.ts +53 -0
- package/ai/token/serverDialogProjection.ts +78 -0
- package/ai/token/serverTokenWriter.ts +143 -0
- package/ai/token/stats.ts +21 -0
- package/ai/token/tokenThunks.ts +24 -0
- package/ai/token/types.ts +93 -0
- package/ai/tools/agent/agentTools.ts +176 -0
- package/ai/tools/agent/agentUpdateShared.ts +311 -0
- package/ai/tools/agent/callAgentTool.ts +139 -0
- package/ai/tools/agent/createAgentTool.ts +512 -0
- package/ai/tools/agent/createDialogTool.ts +69 -0
- package/ai/tools/agent/createSkillAgentTool.ts +62 -0
- package/ai/tools/agent/parallelBudget.ts +221 -0
- package/ai/tools/agent/presets/appBuilderPreset.ts +147 -0
- package/ai/tools/agent/runLlmTool.ts +96 -0
- package/ai/tools/agent/runStreamingAgentTool.ts +73 -0
- package/ai/tools/agent/skillAgentArgs.ts +106 -0
- package/ai/tools/agent/skillAgentPreset.ts +89 -0
- package/ai/tools/agent/streamParallelAgentsTool.ts +122 -0
- package/ai/tools/agent/updateAgentTool.ts +96 -0
- package/ai/tools/agent/updateSelfTool.ts +113 -0
- package/ai/tools/amazonProductScraperTool.ts +86 -0
- package/ai/tools/apifyActorClient.ts +45 -0
- package/ai/tools/appEditGuard.ts +372 -0
- package/ai/tools/appReadSnapshot.ts +153 -0
- package/ai/tools/appTools.ts +1549 -0
- package/ai/tools/applyEditTool.ts +256 -0
- package/ai/tools/applyLineEditsTool.ts +312 -0
- package/ai/tools/browserTools/click.ts +33 -0
- package/ai/tools/browserTools/closeSession.ts +29 -0
- package/ai/tools/browserTools/common.ts +27 -0
- package/ai/tools/browserTools/openSession.ts +48 -0
- package/ai/tools/browserTools/readContent.ts +38 -0
- package/ai/tools/browserTools/selectOption.ts +46 -0
- package/ai/tools/browserTools/typeText.ts +42 -0
- package/ai/tools/category/createCategoryTool.ts +66 -0
- package/ai/tools/category/queryContentsByCategoryTool.ts +69 -0
- package/ai/tools/category/updateContentCategoryTool.ts +75 -0
- package/ai/tools/cfBrowserTools.ts +319 -0
- package/ai/tools/cfSpeechToTextTool.ts +49 -0
- package/ai/tools/checkEnvTool.ts +65 -0
- package/ai/tools/cloudflareCrawlTool.ts +289 -0
- package/ai/tools/codeSearchTool.ts +111 -0
- package/ai/tools/codeTools.ts +101 -0
- package/ai/tools/createDocTool.ts +132 -0
- package/ai/tools/createPlanTool.ts +999 -0
- package/ai/tools/createSkillDocTool.ts +155 -0
- package/ai/tools/createWorkflowTool.ts +154 -0
- package/ai/tools/deepseekOcrTool.ts +34 -0
- package/ai/tools/delayTool.ts +31 -0
- package/ai/tools/deleteSpacesTool.ts +325 -0
- package/ai/tools/deleteSpacesToolModel.ts +159 -0
- package/ai/tools/devReloadUtils.ts +29 -0
- package/ai/tools/dialogMessageSearch.ts +137 -0
- package/ai/tools/doctorSkillTool.ts +72 -0
- package/ai/tools/ecommerceScraperTool.ts +86 -0
- package/ai/tools/emailTools.ts +549 -0
- package/ai/tools/evalSkillTool.ts +92 -0
- package/ai/tools/exaSearchTool.ts +64 -0
- package/ai/tools/execBashTool.ts +379 -0
- package/ai/tools/executeSqlTool.ts +192 -0
- package/ai/tools/fetchWebpageSupport.ts +309 -0
- package/ai/tools/fetchWebpageTool.ts +84 -0
- package/ai/tools/geminiImagePreviewTool.ts +361 -0
- package/ai/tools/generateDocxTool.ts +215 -0
- package/ai/tools/googleSearchScraperTool.ts +106 -0
- package/ai/tools/importDataTool.ts +133 -0
- package/ai/tools/importSkillTool.ts +162 -0
- package/ai/tools/index.ts +1927 -0
- package/ai/tools/listFilesTool.ts +82 -0
- package/ai/tools/listUserSpacesTool.ts +113 -0
- package/ai/tools/modelUsageTools.ts +199 -0
- package/ai/tools/olmOcrTool.ts +34 -0
- package/ai/tools/openaiImageTool.ts +267 -0
- package/ai/tools/prepareTools.ts +23 -0
- package/ai/tools/readDocTool.ts +84 -0
- package/ai/tools/readFileTool.ts +211 -0
- package/ai/tools/readTool.ts +163 -0
- package/ai/tools/readXPostTool.ts +233 -0
- package/ai/tools/rememberMemoryTool.ts +84 -0
- package/ai/tools/remotionVideoTool.ts +151 -0
- package/ai/tools/searchDialogMessagesTool.ts +222 -0
- package/ai/tools/searchRepoTool.ts +115 -0
- package/ai/tools/searchWorkspaceTool.ts +259 -0
- package/ai/tools/skillFollowup.ts +86 -0
- package/ai/tools/surfWeatherTool.ts +169 -0
- package/ai/tools/table/addTableRowTool.ts +217 -0
- package/ai/tools/table/createTableTool.ts +315 -0
- package/ai/tools/table/rowTools.ts +366 -0
- package/ai/tools/table/schemaTools.ts +244 -0
- package/ai/tools/table/shareTableTool.ts +148 -0
- package/ai/tools/table/toolShared.ts +129 -0
- package/ai/tools/toolApiClient.ts +198 -0
- package/ai/tools/toolNameAliases.ts +57 -0
- package/ai/tools/toolResultError.ts +42 -0
- package/ai/tools/toolRunSlice.ts +303 -0
- package/ai/tools/toolSchemaCompatibility.ts +53 -0
- package/ai/tools/toolVisibility.ts +4 -0
- package/ai/tools/types.ts +20 -0
- package/ai/tools/uiAskChoiceTool.ts +104 -0
- package/ai/tools/updateContentTitleTool.ts +84 -0
- package/ai/tools/updateDocTool.ts +105 -0
- package/ai/tools/updateUserPreferenceProfileTool.ts +145 -0
- package/ai/tools/whisperTool.ts +77 -0
- package/ai/tools/writeFileTool.ts +210 -0
- package/ai/tools/youtubeScraperTool.ts +116 -0
- package/ai/tools/ziweiChartTool.ts +678 -0
- package/ai/types.ts +55 -0
- package/ai/workflow/workflowExecutor.ts +323 -0
- package/ai/workflow/workflowSlice.ts +73 -0
- package/ai/workflow/workflowTypes.ts +106 -0
- package/client/agentRun.test.ts +240 -0
- package/client/agentRun.ts +182 -19
- package/client/compactDialog.test.ts +238 -0
- package/client/localRuntimeAdapter.test.ts +135 -0
- package/client/localRuntimeAdapter.ts +244 -0
- package/client/profileConfig.test.ts +40 -0
- package/client/streamingOutput.test.ts +22 -0
- package/client/streamingOutput.ts +38 -0
- package/commandRegistry.ts +11 -2
- package/connector-experimental/index.ts +5 -0
- package/database/actions/cacheMergedUserData.ts +64 -0
- package/database/actions/common.ts +242 -0
- package/database/actions/deleteFile.ts +40 -0
- package/database/actions/fetchUserData.ts +16 -0
- package/database/actions/fileContent.ts +125 -0
- package/database/actions/patch.ts +155 -0
- package/database/actions/read.ts +337 -0
- package/database/actions/readAndWait.ts +224 -0
- package/database/actions/readRequestManager.ts +120 -0
- package/database/actions/remove.ts +94 -0
- package/database/actions/replication.ts +366 -0
- package/database/actions/upload.ts +174 -0
- package/database/actions/upsert.ts +56 -0
- package/database/actions/write.ts +126 -0
- package/database/client/db.native.ts +73 -0
- package/database/client/db.ts +51 -0
- package/database/client/fetchUserData.ts +61 -0
- package/database/client/handleError.ts +19 -0
- package/database/client/queryRequest.ts +21 -0
- package/database/config.ts +21 -0
- package/database/dbActionThunks.ts +1 -0
- package/database/dbSlice.ts +149 -0
- package/database/email.ts +42 -0
- package/database/fileRing.ts +51 -0
- package/database/fileSharding.ts +70 -0
- package/database/fileStorage.native.ts +92 -0
- package/database/fileStorage.ts +232 -0
- package/database/fileUrl.ts +34 -0
- package/database/hooks/useUserData.ts +489 -0
- package/database/index.ts +1 -0
- package/database/keys.ts +765 -0
- package/database/queryPrefixes.ts +14 -0
- package/database/requests.ts +443 -0
- package/database/runtimeServerContext.ts +35 -0
- package/database/server/MemoryDB.ts +76 -0
- package/database/server/actorAccess.ts +76 -0
- package/database/server/agentDelegation.ts +124 -0
- package/database/server/coreDataOwnership.ts +13 -0
- package/database/server/coreDataProxy.ts +76 -0
- package/database/server/cybotReadonly.ts +18 -0
- package/database/server/dataHandlers.ts +111 -0
- package/database/server/db.ts +118 -0
- package/database/server/dbPath.ts +20 -0
- package/database/server/delete.ts +499 -0
- package/database/server/emailRepository.ts +1480 -0
- package/database/server/ensureDbOpen.ts +12 -0
- package/database/server/fileRead.ts +337 -0
- package/database/server/fileService.ts +436 -0
- package/database/server/handleTransaction.ts +86 -0
- package/database/server/patch.ts +282 -0
- package/database/server/query.ts +138 -0
- package/database/server/read.ts +325 -0
- package/database/server/resourceAccess.ts +211 -0
- package/database/server/routes.ts +110 -0
- package/database/server/spaceMemberAuthority.ts +67 -0
- package/database/server/upload.ts +159 -0
- package/database/server/write.ts +494 -0
- package/database/server/writeAuthority.ts +133 -0
- package/database/sqliteDb.ts +46 -0
- package/database/table/deleteTable.ts +120 -0
- package/database/tenantPlacement.ts +57 -0
- package/database/tombstones.ts +52 -0
- package/database/userDataLoadDecision.ts +17 -0
- package/database/userDataMerge.ts +95 -0
- package/database/userPreferenceRegister.ts +108 -0
- package/database/utils/dbPath.ts +47 -0
- package/database/utils/ulid.native.ts +6 -0
- package/database/utils/ulid.ts +1 -0
- package/index.ts +37 -19
- package/localRuntimeDb.ts +28 -0
- package/package.json +17 -4
- package/runtimeModeArgs.ts +33 -0
- package/tui/readlineWorkspace.ts +1 -0
- package/tui/session.ts +22 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// 文件路径: ai/chat/streamReader.native.ts
|
|
2
|
+
// React Native 版流式读取器 - 使用 react-native-sse
|
|
3
|
+
|
|
4
|
+
import EventSource from 'react-native-sse';
|
|
5
|
+
|
|
6
|
+
export interface StreamReaderOptions {
|
|
7
|
+
response: Response;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
onChunk: (chunk: string) => void;
|
|
10
|
+
onError: (error: Error) => void;
|
|
11
|
+
onComplete: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* React Native 版流式读取器
|
|
16
|
+
* 注意:在 RN 中,我们实际上不使用 Response,而是直接走 SSE
|
|
17
|
+
* 这个文件只是为了类型兼容,实际的 SSE 逻辑在 performFetchRequestNative 中
|
|
18
|
+
*/
|
|
19
|
+
export async function readStream(options: StreamReaderOptions): Promise<void> {
|
|
20
|
+
// 在 React Native 中,这个函数不会被调用
|
|
21
|
+
// SSE 处理在 performFetchRequestNative 中完成
|
|
22
|
+
options.onError(new Error('readStream should not be called in React Native'));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 检查当前环境是否支持 ReadableStream
|
|
27
|
+
* React Native 返回 false,需要使用 SSE 方式
|
|
28
|
+
*/
|
|
29
|
+
export function supportsReadableStream(): boolean {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// 文件路径: ai/chat/streamReader.ts
|
|
2
|
+
// Web 版流式读取器 - 使用 fetch Response + ReadableStream
|
|
3
|
+
|
|
4
|
+
import type { SSEClientOptions } from './sseClient';
|
|
5
|
+
|
|
6
|
+
export interface StreamReaderOptions {
|
|
7
|
+
response: Response;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
onChunk: (chunk: string) => void;
|
|
10
|
+
onError: (error: Error) => void;
|
|
11
|
+
onComplete: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Web 版流式读取器
|
|
16
|
+
* 从 fetch Response 的 ReadableStream 读取数据
|
|
17
|
+
*/
|
|
18
|
+
export async function readStream(options: StreamReaderOptions): Promise<void> {
|
|
19
|
+
const { response, onChunk, onError, onComplete } = options;
|
|
20
|
+
|
|
21
|
+
const reader = response.body?.getReader();
|
|
22
|
+
if (!reader) {
|
|
23
|
+
onError(new Error('Response body is not readable'));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const decoder = new TextDecoder();
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
while (true) {
|
|
31
|
+
const { done, value } = await reader.read();
|
|
32
|
+
|
|
33
|
+
if (done) {
|
|
34
|
+
onComplete();
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
39
|
+
onChunk(chunk);
|
|
40
|
+
}
|
|
41
|
+
} catch (error: any) {
|
|
42
|
+
if (error?.name === 'AbortError') {
|
|
43
|
+
onComplete();
|
|
44
|
+
} else {
|
|
45
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
46
|
+
}
|
|
47
|
+
} finally {
|
|
48
|
+
try {
|
|
49
|
+
await reader.cancel();
|
|
50
|
+
} catch (_e) {
|
|
51
|
+
// ignore
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 检查当前环境是否支持 ReadableStream
|
|
58
|
+
* Web 环境返回 true,React Native 返回 false
|
|
59
|
+
*/
|
|
60
|
+
export function supportsReadableStream(): boolean {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// 文件路径: ai/chat/updateTotalUsage.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ✨ 新增辅助函数 ✨
|
|
5
|
+
* 根据新的数据块更新累积的 token 使用量。
|
|
6
|
+
* @param currentUsage - 当前的累积 usage 对象,可能为 null。
|
|
7
|
+
* @param newUsageChunk - 从流中收到的新 usage 数据块。
|
|
8
|
+
* @returns 更新后的 usage 对象。
|
|
9
|
+
*/
|
|
10
|
+
export function updateTotalUsage(currentUsage: any, newUsageChunk: any): any {
|
|
11
|
+
if (!newUsageChunk) {
|
|
12
|
+
return currentUsage;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 如果是第一次接收,直接克隆新数据块
|
|
16
|
+
if (!currentUsage) {
|
|
17
|
+
return { ...newUsageChunk };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 否则,在现有基础上进行累加或更新
|
|
21
|
+
const updatedUsage = { ...currentUsage };
|
|
22
|
+
|
|
23
|
+
// === token 相关 ===
|
|
24
|
+
updatedUsage.completion_tokens =
|
|
25
|
+
newUsageChunk.completion_tokens ?? updatedUsage.completion_tokens;
|
|
26
|
+
updatedUsage.prompt_tokens =
|
|
27
|
+
newUsageChunk.prompt_tokens ?? updatedUsage.prompt_tokens;
|
|
28
|
+
updatedUsage.total_tokens =
|
|
29
|
+
newUsageChunk.total_tokens ?? updatedUsage.total_tokens;
|
|
30
|
+
|
|
31
|
+
if (newUsageChunk.prompt_tokens_details) {
|
|
32
|
+
updatedUsage.prompt_tokens_details = {
|
|
33
|
+
...(updatedUsage.prompt_tokens_details || {}),
|
|
34
|
+
...newUsageChunk.prompt_tokens_details,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (newUsageChunk.completion_tokens_details) {
|
|
38
|
+
updatedUsage.completion_tokens_details = {
|
|
39
|
+
...(updatedUsage.completion_tokens_details || {}),
|
|
40
|
+
...newUsageChunk.completion_tokens_details,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// === OpenRouter usage.cost 相关 ===
|
|
45
|
+
if (typeof newUsageChunk.cost === "number") {
|
|
46
|
+
// 对于 OpenRouter,通常只有最后一条 chunk 带 cost,这里直接覆盖即可
|
|
47
|
+
updatedUsage.cost = newUsageChunk.cost;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (newUsageChunk.cost_details) {
|
|
51
|
+
updatedUsage.cost_details = {
|
|
52
|
+
...(updatedUsage.cost_details || {}),
|
|
53
|
+
...newUsageChunk.cost_details,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
typeof newUsageChunk.billing_provider === "string" &&
|
|
59
|
+
newUsageChunk.billing_provider.trim()
|
|
60
|
+
) {
|
|
61
|
+
updatedUsage.billing_provider = newUsageChunk.billing_provider.trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
typeof newUsageChunk.billing_model === "string" &&
|
|
66
|
+
newUsageChunk.billing_model.trim()
|
|
67
|
+
) {
|
|
68
|
+
updatedUsage.billing_model = newUsageChunk.billing_model.trim();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return updatedUsage;
|
|
72
|
+
}
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
// ai/context/fetchReferenceContents.ts
|
|
2
|
+
|
|
3
|
+
import { read } from "database/dbSlice";
|
|
4
|
+
import { AppDispatch } from "app/store";
|
|
5
|
+
import { slateToText } from "create/editor/transforms/slateToText";
|
|
6
|
+
import { slateToSimplifiedMarkdown } from "create/editor/transforms/slateToSimplifiedMarkdown";
|
|
7
|
+
import { extractCategorizedMentions } from "create/editor/utils/slateUtils";
|
|
8
|
+
import { DialogConfig } from "app/types";
|
|
9
|
+
import { DataType } from "create/types";
|
|
10
|
+
import { extractCustomId } from "core/prefix";
|
|
11
|
+
import { getRuntimeServerContext } from "database/runtimeServerContext";
|
|
12
|
+
import { fetchAndCacheMessages } from "chat/messages/fetchAndCacheMessages";
|
|
13
|
+
import { createSpaceKey } from "create/space/spaceKeys";
|
|
14
|
+
import { TableMeta } from "render/table/types";
|
|
15
|
+
import { fetchAndSerializeTable } from "render/table/utils/tableSerialization";
|
|
16
|
+
import { estimateTokenCount } from "ai/context/tokenUtils";
|
|
17
|
+
|
|
18
|
+
interface FetchOptions {
|
|
19
|
+
format?: "json" | "text" | "simplified_markdown";
|
|
20
|
+
inlineMentionMeta?: boolean;
|
|
21
|
+
preloaded?: Map<string, any>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type MentionMeta = {
|
|
25
|
+
displayType?: string;
|
|
26
|
+
title?: string;
|
|
27
|
+
metaParts: string[] | readonly string[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const MAX_META_TEXT_LENGTH = 80;
|
|
31
|
+
const DIALOG_REFERENCE_MESSAGE_LIMIT = 20;
|
|
32
|
+
const DIALOG_REFERENCE_SNIPPET_CHARS = 1200;
|
|
33
|
+
const DIALOG_HANDOFF_SNIPPET_CHARS = 360;
|
|
34
|
+
|
|
35
|
+
const truncateMetaText = (value: string | undefined, max = MAX_META_TEXT_LENGTH) => {
|
|
36
|
+
if (!value) return "";
|
|
37
|
+
if (value.length <= max) return value;
|
|
38
|
+
return `${value.slice(0, max - 3)}...`;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const readSafe = async (dispatch: AppDispatch, dbKey: string) => {
|
|
42
|
+
try {
|
|
43
|
+
return await dispatch(read({
|
|
44
|
+
dbKey: dbKey
|
|
45
|
+
})).unwrap();
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const buildMentionMetaMap = async (
|
|
52
|
+
slateData: any,
|
|
53
|
+
dispatch: AppDispatch
|
|
54
|
+
): Promise<Map<string, MentionMeta>> => {
|
|
55
|
+
const metaMap = new Map<string, MentionMeta>();
|
|
56
|
+
const mentions = extractCategorizedMentions(slateData);
|
|
57
|
+
|
|
58
|
+
if (!mentions) return metaMap;
|
|
59
|
+
|
|
60
|
+
const pageEntries = await Promise.all(
|
|
61
|
+
(mentions.pages || []).map(async (pageId: string) => {
|
|
62
|
+
const pageData = await readSafe(dispatch, pageId);
|
|
63
|
+
if (!pageData) {
|
|
64
|
+
return [`page:${pageId}`, { displayType: "page", metaParts: [] }] as const;
|
|
65
|
+
}
|
|
66
|
+
if (pageData.type === DataType.DIALOG) {
|
|
67
|
+
const cybotCount = Array.isArray(pageData.cybots)
|
|
68
|
+
? pageData.cybots.length
|
|
69
|
+
: 0;
|
|
70
|
+
return [
|
|
71
|
+
`page:${pageId}`,
|
|
72
|
+
{
|
|
73
|
+
displayType: "dialog",
|
|
74
|
+
title: pageData.title,
|
|
75
|
+
metaParts: [
|
|
76
|
+
`cybots=${cybotCount}`,
|
|
77
|
+
`updated=${pageData.updatedAt || pageData.updated_at || pageData.updated || "Unknown"}`,
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
] as const;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const tags =
|
|
84
|
+
pageData.tags && pageData.tags.length > 0
|
|
85
|
+
? pageData.tags.slice(0, 5).join(", ")
|
|
86
|
+
: "";
|
|
87
|
+
return [
|
|
88
|
+
`page:${pageId}`,
|
|
89
|
+
{
|
|
90
|
+
displayType: "page",
|
|
91
|
+
title: pageData.title,
|
|
92
|
+
metaParts: [
|
|
93
|
+
pageData.spaceId ? `space=${pageData.spaceId}` : "",
|
|
94
|
+
tags ? `tags=${tags}` : "",
|
|
95
|
+
`updated=${pageData.updatedAt || pageData.updated_at || pageData.updated || "Unknown"}`,
|
|
96
|
+
].filter(Boolean),
|
|
97
|
+
},
|
|
98
|
+
] as const;
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
pageEntries.forEach((entry: any) => metaMap.set(entry[0], entry[1]));
|
|
103
|
+
|
|
104
|
+
const agentEntries = await Promise.all(
|
|
105
|
+
(mentions.agents || []).map(async (agentId: string) => {
|
|
106
|
+
const agentData = await readSafe(dispatch, agentId);
|
|
107
|
+
if (!agentData) {
|
|
108
|
+
return [`agent:${agentId}`, { displayType: "agent", metaParts: [] }] as const;
|
|
109
|
+
}
|
|
110
|
+
const desc = truncateMetaText(
|
|
111
|
+
agentData.description || agentData.introduction || ""
|
|
112
|
+
);
|
|
113
|
+
return [
|
|
114
|
+
`agent:${agentId}`,
|
|
115
|
+
{
|
|
116
|
+
displayType: "agent",
|
|
117
|
+
title: agentData.name,
|
|
118
|
+
metaParts: [
|
|
119
|
+
`public=${agentData.isPublic ? "yes" : "no"}`,
|
|
120
|
+
desc ? `desc=${desc}` : "",
|
|
121
|
+
].filter(Boolean),
|
|
122
|
+
},
|
|
123
|
+
] as const;
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
agentEntries.forEach((entry: any) => metaMap.set(entry[0], entry[1]));
|
|
128
|
+
|
|
129
|
+
const spaceEntries = await Promise.all(
|
|
130
|
+
(mentions.spaces || []).map(async (spaceId: string) => {
|
|
131
|
+
const spaceKey = createSpaceKey.space(spaceId);
|
|
132
|
+
const spaceData = await readSafe(dispatch, spaceKey);
|
|
133
|
+
if (!spaceData) {
|
|
134
|
+
return [`space:${spaceId}`, { displayType: "space", metaParts: [] }] as const;
|
|
135
|
+
}
|
|
136
|
+
const desc = truncateMetaText(spaceData.description || "");
|
|
137
|
+
const categoriesCount = Object.keys(spaceData.categories || {}).length;
|
|
138
|
+
const contentsCount = Object.keys(spaceData.contents || {}).length;
|
|
139
|
+
return [
|
|
140
|
+
`space:${spaceId}`,
|
|
141
|
+
{
|
|
142
|
+
displayType: "space",
|
|
143
|
+
title: spaceData.name,
|
|
144
|
+
metaParts: [
|
|
145
|
+
desc ? `desc=${desc}` : "",
|
|
146
|
+
`categories=${categoriesCount}`,
|
|
147
|
+
`contents=${contentsCount}`,
|
|
148
|
+
].filter(Boolean),
|
|
149
|
+
},
|
|
150
|
+
] as const;
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
spaceEntries.forEach((entry: any) => metaMap.set(entry[0], entry[1]));
|
|
155
|
+
|
|
156
|
+
return metaMap;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const buildInlineMention = (
|
|
160
|
+
node: any,
|
|
161
|
+
metaMap: Map<string, MentionMeta>
|
|
162
|
+
): string => {
|
|
163
|
+
const resourceType = node.resourceType || "unknown";
|
|
164
|
+
const resourceId = node.resourceId || "unknown";
|
|
165
|
+
const key = `${resourceType}:${resourceId}`;
|
|
166
|
+
const meta = metaMap.get(key);
|
|
167
|
+
const label = meta?.title || node.label || resourceId || "mention";
|
|
168
|
+
const displayType = meta?.displayType || resourceType;
|
|
169
|
+
const metaParts = (meta?.metaParts as string[]) ?? [];
|
|
170
|
+
const idPart =
|
|
171
|
+
resourceType === "tool" ? `tool=${resourceId}` : `dbkey=${resourceId}`;
|
|
172
|
+
const metaSuffix = metaParts.length ? ` | ${metaParts.join(" | ")}` : "";
|
|
173
|
+
return `@${label}(${displayType} | ${idPart}${metaSuffix})`;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const clipReferenceText = (value: unknown, max: number) => {
|
|
177
|
+
const raw =
|
|
178
|
+
typeof value === "string"
|
|
179
|
+
? value
|
|
180
|
+
: value == null
|
|
181
|
+
? ""
|
|
182
|
+
: JSON.stringify(value);
|
|
183
|
+
const trimmed = raw.trim();
|
|
184
|
+
if (trimmed.length <= max) return trimmed;
|
|
185
|
+
return `${trimmed.slice(0, max)}\n...[truncated ${trimmed.length - max} chars]`;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// --- Sub-handlers for specific types ---
|
|
189
|
+
|
|
190
|
+
const fetchDialogReference = async (
|
|
191
|
+
dbKey: string,
|
|
192
|
+
refContent: any,
|
|
193
|
+
dispatch: AppDispatch
|
|
194
|
+
): Promise<string> => {
|
|
195
|
+
const dialogConfig = refContent as DialogConfig;
|
|
196
|
+
const dialogTitle = dialogConfig.title || `Untitled Dialog (${dbKey})`;
|
|
197
|
+
const dialogId = extractCustomId(dbKey) || dbKey.replace(/^dialog-/, "");
|
|
198
|
+
const checkpoint = (dialogConfig as any).runtimeCheckpoint || null;
|
|
199
|
+
const summary = typeof (dialogConfig as any).summary === "string"
|
|
200
|
+
? (dialogConfig as any).summary.trim()
|
|
201
|
+
: "";
|
|
202
|
+
const proactiveSummary = typeof (dialogConfig as any).proactiveSummary === "string"
|
|
203
|
+
? (dialogConfig as any).proactiveSummary.trim()
|
|
204
|
+
: "";
|
|
205
|
+
|
|
206
|
+
const messages = await dispatch(
|
|
207
|
+
async (_dispatch: any, getState: any, { db }: any) => {
|
|
208
|
+
const state = getState();
|
|
209
|
+
const { currentToken: token, remoteServers } =
|
|
210
|
+
getRuntimeServerContext(state);
|
|
211
|
+
|
|
212
|
+
return await fetchAndCacheMessages({
|
|
213
|
+
db,
|
|
214
|
+
dialogId,
|
|
215
|
+
limit: DIALOG_REFERENCE_MESSAGE_LIMIT,
|
|
216
|
+
token,
|
|
217
|
+
remoteServers: remoteServers.length > 0 ? remoteServers : undefined,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const sortedMessages = [...messages].reverse();
|
|
223
|
+
const formatContent = (content: unknown) =>
|
|
224
|
+
clipReferenceText(content, DIALOG_REFERENCE_SNIPPET_CHARS);
|
|
225
|
+
const transcript = sortedMessages
|
|
226
|
+
.map((msg: any, index) => {
|
|
227
|
+
const toolLine = msg.toolName ? ` tool=${msg.toolName}` : "";
|
|
228
|
+
const agentLine = msg.cybotKey ? ` agent=${msg.cybotKey}` : "";
|
|
229
|
+
const createdLine = msg.createdAt ? ` at=${msg.createdAt}` : "";
|
|
230
|
+
return [
|
|
231
|
+
`### Message ${index + 1}: ${msg.role || "unknown"} id=${msg.id || "unknown"}${toolLine}${agentLine}${createdLine}`,
|
|
232
|
+
formatContent(msg.content) || "[empty]",
|
|
233
|
+
].join("\n");
|
|
234
|
+
})
|
|
235
|
+
.join("\n\n");
|
|
236
|
+
|
|
237
|
+
const checkpointLines: string[] = [];
|
|
238
|
+
if (checkpoint && typeof checkpoint === "object") {
|
|
239
|
+
if (checkpoint.status) checkpointLines.push(`- status: ${checkpoint.status}`);
|
|
240
|
+
if (checkpoint.lastUserInput) checkpointLines.push(`- lastUserInput: ${formatContent(checkpoint.lastUserInput)}`);
|
|
241
|
+
if (checkpoint.lastAssistantText) checkpointLines.push(`- lastAssistantText: ${formatContent(checkpoint.lastAssistantText)}`);
|
|
242
|
+
if (Array.isArray(checkpoint.lastToolNames) && checkpoint.lastToolNames.length) {
|
|
243
|
+
checkpointLines.push(`- lastToolNames: ${checkpoint.lastToolNames.join(", ")}`);
|
|
244
|
+
}
|
|
245
|
+
if (Array.isArray(checkpoint.availableToolNames) && checkpoint.availableToolNames.length) {
|
|
246
|
+
checkpointLines.push(`- availableToolNames: ${checkpoint.availableToolNames.join(", ")}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const recentToolEvidence = sortedMessages
|
|
251
|
+
.filter((msg: any) => msg.role === "tool" || msg.toolName)
|
|
252
|
+
.slice(-3)
|
|
253
|
+
.map((msg: any) => {
|
|
254
|
+
const label = msg.toolName ? `${msg.toolName}` : "tool";
|
|
255
|
+
return `- ${label} id=${msg.id || "unknown"}: ${clipReferenceText(msg.content, DIALOG_HANDOFF_SNIPPET_CHARS) || "[empty]"}`;
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const handoffLines = [
|
|
259
|
+
`- Use this when continuing work, transferring to another Agent, comparing with the current task, or preparing a document/plan from the prior discussion.`,
|
|
260
|
+
`- Current state source: ${checkpointLines.length ? "Runtime Checkpoint" : "summaries and recent transcript"}.`,
|
|
261
|
+
summary ? `- Compressed background: passive summary is available below; treat it as lossy, not original wording.` : "",
|
|
262
|
+
proactiveSummary ? `- Recent work: proactive summary is available below; use it for current direction, not exact evidence.` : "",
|
|
263
|
+
recentToolEvidence.length
|
|
264
|
+
? `- Recent tool evidence:\n${recentToolEvidence.join("\n")}`
|
|
265
|
+
: `- Recent tool evidence: none loaded in the latest ${DIALOG_REFERENCE_MESSAGE_LIMIT} messages.`,
|
|
266
|
+
`- For exact claims, old decisions, original wording, files/tools mentioned earlier, or anything not visible in the recent transcript, call searchDialogMessages with DB Key ${dbKey}.`,
|
|
267
|
+
].filter(Boolean);
|
|
268
|
+
|
|
269
|
+
const referenceBody = [
|
|
270
|
+
`Conversation Reference:`,
|
|
271
|
+
`DB Key: ${dbKey}`,
|
|
272
|
+
`Title: ${dialogTitle}`,
|
|
273
|
+
`Status: ${(dialogConfig as any).status || "unknown"}`,
|
|
274
|
+
`Loaded Recent Messages: ${sortedMessages.length}`,
|
|
275
|
+
`Conversation Handoff:\n${handoffLines.join("\n")}`,
|
|
276
|
+
checkpointLines.length ? `Runtime Checkpoint:\n${checkpointLines.join("\n")}` : "",
|
|
277
|
+
summary ? `Passive Summary (compressed history, not original wording):\n${summary}` : "",
|
|
278
|
+
proactiveSummary ? `Proactive Summary (recent work summary, not original wording):\n${proactiveSummary}` : "",
|
|
279
|
+
`Recent Transcript (original message excerpts, oldest to newest):\n${transcript || "[no recent messages loaded]"}`,
|
|
280
|
+
[
|
|
281
|
+
`Coverage Note: This reference intentionally loads only the latest ${DIALOG_REFERENCE_MESSAGE_LIMIT} messages plus summaries/checkpoint to control token load.`,
|
|
282
|
+
`Original Message Lookup Policy: If the user asks for an exact old message, original wording, who said what, why a decision was made, early-history detail, file/tool evidence, failed attempts, or a comparison with prior work, use searchDialogMessages({ dialogKey: "${dbKey}", query: "..." }) before making a factual claim from this referenced conversation.`,
|
|
283
|
+
].join("\n"),
|
|
284
|
+
].filter(Boolean).join("\n\n");
|
|
285
|
+
|
|
286
|
+
const tokenEstimate = estimateTokenCount(referenceBody);
|
|
287
|
+
|
|
288
|
+
return (
|
|
289
|
+
`${referenceBody}\n\n` +
|
|
290
|
+
`Token Load Estimate: ${tokenEstimate} tokens for this conversation reference.\n` +
|
|
291
|
+
`---\n\n`
|
|
292
|
+
);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const fetchTableReference = async (
|
|
296
|
+
dbKey: string,
|
|
297
|
+
refContent: any,
|
|
298
|
+
dispatch: AppDispatch
|
|
299
|
+
): Promise<string> => {
|
|
300
|
+
const tableMeta = refContent as TableMeta;
|
|
301
|
+
const title = tableMeta.displayName || tableMeta.description || `Untitled Table (${dbKey})`;
|
|
302
|
+
|
|
303
|
+
const { markdown: tableMd } = await dispatch(
|
|
304
|
+
async (_dispatch: any, getState: any, { db }: any) => {
|
|
305
|
+
const state = getState();
|
|
306
|
+
const { currentToken: token, remoteServers } =
|
|
307
|
+
getRuntimeServerContext(state);
|
|
308
|
+
|
|
309
|
+
return await fetchAndSerializeTable(tableMeta, db, {
|
|
310
|
+
token,
|
|
311
|
+
remoteServers,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
const tags = tableMeta.tags?.length ? tableMeta.tags.join(", ") : "None";
|
|
317
|
+
const description = tableMeta.description || "No description provided.";
|
|
318
|
+
|
|
319
|
+
return (
|
|
320
|
+
`Reference Item (Table):\n` +
|
|
321
|
+
`DB Key: ${dbKey}\n` +
|
|
322
|
+
`Title: ${title}\n` +
|
|
323
|
+
`Description: ${description}\n` +
|
|
324
|
+
`Tags: ${tags}\n` +
|
|
325
|
+
`Content (Markdown Table):\n\n${tableMd}\n` +
|
|
326
|
+
`---\n\n`
|
|
327
|
+
);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const fetchSlateReference = async (
|
|
331
|
+
dbKey: string,
|
|
332
|
+
refContent: any,
|
|
333
|
+
dispatch: AppDispatch,
|
|
334
|
+
options: FetchOptions
|
|
335
|
+
): Promise<string | null> => {
|
|
336
|
+
if (!refContent?.slateData) return null;
|
|
337
|
+
|
|
338
|
+
const title = refContent.title || `Untitled (${dbKey})`;
|
|
339
|
+
let contentString: string;
|
|
340
|
+
let contentType: string;
|
|
341
|
+
const inlineMentionMeta =
|
|
342
|
+
options.inlineMentionMeta ?? options.format === "simplified_markdown";
|
|
343
|
+
|
|
344
|
+
switch (options.format) {
|
|
345
|
+
case "text":
|
|
346
|
+
contentType = "Plain Text";
|
|
347
|
+
contentString = slateToText(refContent.slateData);
|
|
348
|
+
break;
|
|
349
|
+
case "simplified_markdown":
|
|
350
|
+
contentType = "Simplified Markdown";
|
|
351
|
+
if (inlineMentionMeta) {
|
|
352
|
+
const metaMap = await buildMentionMetaMap(refContent.slateData, dispatch);
|
|
353
|
+
contentString = slateToSimplifiedMarkdown(refContent.slateData, {
|
|
354
|
+
mentionResolver: (node) => buildInlineMention(node, metaMap),
|
|
355
|
+
});
|
|
356
|
+
} else {
|
|
357
|
+
contentString = slateToSimplifiedMarkdown(refContent.slateData);
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
case "json":
|
|
361
|
+
default:
|
|
362
|
+
contentType = "Slate JSON";
|
|
363
|
+
contentString = JSON.stringify(refContent.slateData, null, 2);
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (
|
|
368
|
+
!contentString ||
|
|
369
|
+
(typeof contentString === "string" && !contentString.trim()) ||
|
|
370
|
+
contentString === "[]"
|
|
371
|
+
) {
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const tags = (refContent.tags || []).length > 0 ? refContent.tags.join(", ") : "None";
|
|
376
|
+
const createdAt = refContent.created || "Unknown Creation Date";
|
|
377
|
+
const updatedAt = refContent.updated || "Unknown Update Date";
|
|
378
|
+
|
|
379
|
+
return (
|
|
380
|
+
`Reference Item:\n` +
|
|
381
|
+
`DB Key: ${dbKey}\n` +
|
|
382
|
+
`Title: ${title}\n` +
|
|
383
|
+
`Content (${contentType}):\n${contentString}\n` +
|
|
384
|
+
`Tags: ${tags}\n` +
|
|
385
|
+
`Created At: ${createdAt}\n` +
|
|
386
|
+
`Updated At: ${updatedAt}\n` +
|
|
387
|
+
`---\n\n`
|
|
388
|
+
);
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* 智能地获取并格式化参考内容。
|
|
393
|
+
*/
|
|
394
|
+
export const fetchReferenceContents = async (
|
|
395
|
+
references: string[],
|
|
396
|
+
dispatch: AppDispatch,
|
|
397
|
+
options: FetchOptions = { format: "simplified_markdown" }
|
|
398
|
+
): Promise<Map<string, string>> => {
|
|
399
|
+
const result = new Map<string, string>();
|
|
400
|
+
if (!references || references.length === 0) return result;
|
|
401
|
+
|
|
402
|
+
const referencePromises = references.map(async (dbKey) => {
|
|
403
|
+
try {
|
|
404
|
+
const hasPreloaded = options.preloaded?.has(dbKey);
|
|
405
|
+
const refContent = hasPreloaded
|
|
406
|
+
? options.preloaded?.get(dbKey)
|
|
407
|
+
: await dispatch(read({
|
|
408
|
+
dbKey: dbKey
|
|
409
|
+
})).unwrap();
|
|
410
|
+
|
|
411
|
+
if (!refContent) return null;
|
|
412
|
+
|
|
413
|
+
let formatted: string | null = null;
|
|
414
|
+
|
|
415
|
+
if (refContent.type === DataType.DIALOG) {
|
|
416
|
+
formatted = await fetchDialogReference(dbKey, refContent, dispatch);
|
|
417
|
+
} else if (refContent.type === DataType.TABLE) {
|
|
418
|
+
formatted = await fetchTableReference(dbKey, refContent, dispatch);
|
|
419
|
+
} else {
|
|
420
|
+
formatted = await fetchSlateReference(dbKey, refContent, dispatch, options);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (formatted) return [dbKey, formatted] as [string, string];
|
|
424
|
+
return null;
|
|
425
|
+
} catch (error: any) {
|
|
426
|
+
console.error(`Error fetching reference ${dbKey}:`, error);
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
const resolved = await Promise.all(referencePromises);
|
|
432
|
+
resolved.forEach((item) => {
|
|
433
|
+
if (item) result.set(item[0], item[1]);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
return result;
|
|
437
|
+
};
|