nolo-cli 0.1.39 → 0.1.41
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 +4 -4
- package/agent-runtime/dialogWritePlan.ts +39 -0
- package/agent-runtime/hostAdapter.ts +4 -0
- package/agent-runtime/hybridRecordStore.ts +5 -3
- package/agent-runtime/index.ts +15 -0
- package/agent-runtime/localLoop.ts +87 -7
- package/agent-runtime/localToolPolicy.ts +2 -13
- package/agent-runtime/localWorkspaceTools.ts +42 -110
- package/agent-runtime/runtimeToolPolicy.ts +2 -11
- package/agent-runtime/runtimeToolSurface.ts +198 -0
- package/agent-runtime/types.ts +1 -0
- package/agentAliases.ts +2 -1
- package/agentRunCommand.ts +10 -32
- package/agentRuntimeLocal.ts +1 -0
- package/ai/agent/_executeModel.ts +146 -0
- package/ai/agent/agentSlice.ts +556 -1
- package/ai/agent/appWorkingMemory.ts +126 -0
- package/ai/agent/buildEditingContext.ts +378 -0
- package/ai/agent/buildSystemPrompt.ts +585 -0
- package/ai/agent/canvasEditingContext.ts +49 -0
- package/ai/agent/cleanAgentMessages.ts +140 -0
- package/ai/agent/cliChatClient.ts +153 -0
- package/ai/agent/cliExecutor.ts +314 -40
- package/ai/agent/contextCompiler.ts +107 -0
- package/ai/agent/contextLayerContract.ts +44 -0
- package/ai/agent/createAgentSchema.ts +272 -0
- package/ai/agent/currentTimeContext.ts +65 -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/guidedCreation/capabilities.ts +84 -0
- package/ai/agent/guidedCreation/types.ts +38 -0
- package/ai/agent/isLiveAudioOnlyAgent.ts +26 -0
- package/ai/agent/liveAudioModel.ts +2 -0
- package/ai/agent/pageBuilderHandoffRules.ts +75 -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 +310 -0
- package/ai/agent/runAgentClientLoop.ts +164 -0
- package/ai/agent/runtimeGuidance.ts +116 -0
- package/ai/agent/serverOrigin.ts +7 -0
- package/ai/agent/startParallelAgentStreams.ts +424 -0
- package/ai/agent/startupProtocol.ts +53 -0
- package/ai/agent/streamAgentChatTurn.ts +2001 -0
- package/ai/agent/streamAgentChatTurnUtils.ts +860 -0
- package/ai/agent/toolFailureGuard.ts +99 -0
- package/ai/agent/types.ts +73 -0
- package/ai/agent/utils/imageOutput.ts +37 -0
- package/ai/agent/utils/publicImageAgentMode.ts +26 -0
- package/ai/ai.locale.ts +1095 -0
- package/ai/chat/accumulateToolCallChunks.ts +126 -0
- package/ai/chat/fetchUtils.ts +143 -0
- package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
- package/ai/chat/parseApiError.ts +67 -0
- package/ai/chat/parseMultilineSSE.ts +95 -0
- package/ai/chat/sendOpenAICompletionsRequest.ts +897 -0
- package/ai/chat/sendOpenAIResponseRequest.ts +720 -0
- package/ai/chat/serverProxyRetry.ts +102 -0
- package/ai/chat/shouldUseServerProxy.ts +18 -0
- package/ai/chat/streamRetry.ts +46 -0
- package/ai/chat/updateTotalUsage.ts +89 -0
- package/ai/context/buildReferenceContext.ts +437 -0
- package/ai/context/retention.ts +165 -0
- package/ai/context/tokenUtils.ts +78 -0
- package/ai/llm/agentCapabilities.ts +74 -0
- package/ai/llm/deepinfra.ts +79 -0
- package/ai/llm/fireworks.ts +68 -0
- package/ai/llm/generateRequestBody.ts +165 -0
- package/ai/llm/getModelContextWindow.ts +93 -0
- package/ai/llm/getPricing.ts +252 -0
- package/ai/llm/imagePricing.ts +66 -0
- package/ai/llm/isResponseAPIModel.ts +13 -0
- package/ai/llm/kimi.ts +23 -0
- package/ai/llm/mimo.ts +99 -0
- package/ai/llm/mistral.ts +21 -0
- package/ai/llm/models.ts +45 -0
- package/ai/llm/openrouterModels.ts +6 -0
- package/ai/llm/providers.ts +313 -0
- package/ai/llm/reasoningModels.ts +27 -0
- package/ai/llm/types.ts +82 -0
- package/ai/llm/usageRequestOptions.ts +59 -0
- package/ai/memory/queryShared.ts +161 -0
- package/ai/memory/recentRelationshipRecap.ts +247 -0
- package/ai/memory/remember.ts +167 -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/policy/personalizationDialog.ts +333 -0
- package/ai/policy/runtimePolicy.ts +454 -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/skillSummaryMarker.ts +26 -0
- package/ai/token/calculatePrice.ts +576 -0
- package/ai/token/missingUsageEstimate.ts +42 -0
- package/ai/token/normalizeUsage.ts +124 -0
- package/ai/token/openaiImageGenerationUsage.ts +56 -0
- package/ai/token/prepareTokenUsageData.ts +93 -0
- package/ai/token/resolveBillingTarget.ts +52 -0
- package/ai/token/saveTokenRecord.ts +53 -0
- package/ai/token/types.ts +103 -0
- package/ai/tools/agent/agentTools.ts +235 -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/prepareAgentDraftTool.ts +159 -0
- package/ai/tools/agent/presets/appBuilderPreset.ts +154 -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/startAgentDialogTool.ts +53 -0
- package/ai/tools/agent/streamParallelAgentsTool.ts +123 -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 +259 -0
- package/ai/tools/applyLineEditsTool.ts +315 -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 +102 -0
- package/ai/tools/cloudflareCrawlTool.ts +289 -0
- package/ai/tools/codeSearchTool.ts +123 -0
- package/ai/tools/codeTools.ts +101 -0
- package/ai/tools/convertMarxistsBookTool.ts +103 -0
- package/ai/tools/createDocTool.ts +132 -0
- package/ai/tools/createSkillDocTool.ts +155 -0
- package/ai/tools/createWorkflowTool.ts +154 -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 +63 -0
- package/ai/tools/execShellTool.ts +266 -0
- package/ai/tools/executeSqlTool.ts +192 -0
- package/ai/tools/fetchWebpageSupport.ts +333 -0
- package/ai/tools/fetchWebpageTool.ts +97 -0
- package/ai/tools/geminiImagePreviewTool.ts +386 -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 +2308 -0
- package/ai/tools/jdProductScraperTool.ts +821 -0
- package/ai/tools/listUserSpacesTool.ts +113 -0
- package/ai/tools/localFilesTool.ts +276 -0
- package/ai/tools/marxistsOfflineBook.ts +253 -0
- package/ai/tools/modelUsageTools.ts +206 -0
- package/ai/tools/noloWorkspaceReadTools.ts +467 -0
- package/ai/tools/olmOcrTool.ts +33 -0
- package/ai/tools/openaiImageTool.ts +267 -0
- package/ai/tools/prepareTools.ts +23 -0
- package/ai/tools/readDocTool.ts +88 -0
- package/ai/tools/readFileTool.ts +214 -0
- package/ai/tools/readTool.ts +194 -0
- package/ai/tools/readXPostTool.ts +229 -0
- package/ai/tools/readXhsProfileTool.ts +627 -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/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 +386 -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/taobaoTmallProductScraperTool.ts +49 -0
- package/ai/tools/toolApiClient.ts +279 -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/uiAskChoiceTool.ts +104 -0
- package/ai/tools/updateContentTitleTool.ts +84 -0
- package/ai/tools/updateDocTool.ts +105 -0
- package/ai/tools/updateUserPreferenceProfileTool.ts +153 -0
- package/ai/tools/wereadGatewayTool.ts +152 -0
- package/ai/tools/whisperTool.ts +76 -0
- package/ai/tools/writeFileTool.ts +213 -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/app/actions/syncAppRecord.ts +73 -0
- package/app/constants/animationSets.ts +21 -0
- package/app/constants/appEditor.ts +99 -0
- package/app/favorite/favoriteSlice.ts +690 -0
- package/app/favorite/type.ts +43 -0
- package/app/hooks/deleteDbKey.ts +233 -0
- package/app/i18n/client.ts +13 -0
- package/app/i18n/i18n.base.ts +14 -0
- package/app/i18n/i18n.config.ts +77 -0
- package/app/i18n/index.ts +15 -0
- package/app/i18n/mapLanguage.ts +11 -0
- package/app/i18n/translations/appBuilder.locale.ts +453 -0
- package/app/i18n/translations/error.locale.ts +246 -0
- package/app/i18n/translations/interface.locale.ts +2174 -0
- package/app/i18n/translations/pricing.locale.ts +397 -0
- package/app/i18n/translations/privacy.locale.ts +168 -0
- package/app/i18n/translations/seo.locale.ts +129 -0
- package/app/i18n/translations/terms.locale.ts +124 -0
- package/app/i18n/types.ts +6 -0
- package/app/notifications/model.ts +77 -0
- package/app/notifications/notificationSlice.ts +141 -0
- package/app/reducer.ts +36 -0
- package/app/settings/dbActionThunks.ts +34 -0
- package/app/settings/defaultSpacePreference.ts +87 -0
- package/app/settings/settingSlice.tsx +1212 -0
- package/app/settings/settingsRecord.ts +8 -0
- package/app/stateViews/runtime.ts +90 -0
- package/app/store.ts +55 -0
- package/app/theme/colors.ts +280 -0
- package/app/theme/fontPreference.ts +77 -0
- package/app/theme/theme.config.ts +120 -0
- package/app/theme/themeModeBootstrap.ts +123 -0
- package/app/types/appSummary.ts +114 -0
- package/app/types.ts +358 -0
- package/app/utils/appKeys.ts +55 -0
- package/app/utils/async.ts +28 -0
- package/app/utils/desktopAgentRuntimeTurnClient.ts +73 -0
- package/app/utils/env.ts +17 -0
- package/app/utils/fileUtils.ts +229 -0
- package/app/utils/imageUtils.ts +274 -0
- package/app/utils/myContentItems.ts +289 -0
- package/app/utils/retryAfter.ts +36 -0
- package/app/utils/retryFetch.ts +61 -0
- package/app/utils/toast.ts +3 -0
- package/auth/action/signUpAction.ts +227 -0
- package/auth/adminPermissions.ts +18 -0
- package/auth/authSlice.ts +531 -0
- package/auth/client/loginRequest.ts +14 -0
- package/auth/resetAuthScopedClientState.ts +27 -0
- package/auth/routes.ts +178 -0
- package/auth/token.ts +63 -0
- package/auth/types.ts +40 -0
- package/chat/chat.locale.ts +813 -0
- package/chat/dialog/actions/addCybotAction.ts +52 -0
- package/chat/dialog/actions/addReferenceKeysAction.ts +62 -0
- package/chat/dialog/actions/builtinDialogLlm.ts +35 -0
- package/chat/dialog/actions/cleanupCliSession.ts +40 -0
- package/chat/dialog/actions/createDialogAction.ts +379 -0
- package/chat/dialog/actions/createScheduledTaskAction.ts +78 -0
- package/chat/dialog/actions/handleSendMessageAction.ts +197 -0
- package/chat/dialog/actions/handleSendMessageResolver.ts +27 -0
- package/chat/dialog/actions/removeCybotAction.ts +6 -0
- package/chat/dialog/actions/removeDialogAgentAction.ts +44 -0
- package/chat/dialog/actions/replacePrimaryCybotAction.ts +6 -0
- package/chat/dialog/actions/setPrimaryDialogAgentAction.ts +52 -0
- package/chat/dialog/actions/updateDialogSummaryAction.ts +430 -0
- package/chat/dialog/actions/updateDialogTitleAction.ts +151 -0
- package/chat/dialog/actions/updateDialogTitlePolicy.ts +30 -0
- package/chat/dialog/actions/updateTokensAction.ts +233 -0
- package/chat/dialog/dialogAgents.ts +72 -0
- package/chat/dialog/dialogSlice.ts +872 -0
- package/chat/dialog/dialogTitle.ts +81 -0
- package/chat/dialog/dialogTokenStats.ts +12 -0
- package/chat/dialog/dialogUrl.ts +6 -0
- package/chat/dialog/goal.ts +108 -0
- package/chat/dialog/objectAssistantRegistry.ts +286 -0
- package/chat/dialog/schedule.ts +17 -0
- package/chat/messages/fetchAndCacheMessages.ts +119 -0
- package/chat/messages/fetchConvMsgs.ts +51 -0
- package/chat/messages/fileUrl.ts +13 -0
- package/chat/messages/messageAgent.ts +8 -0
- package/chat/messages/messageContent.ts +199 -0
- package/chat/messages/messageContract.ts +88 -0
- package/chat/messages/messageSlice.ts +1367 -0
- package/chat/messages/toolThunks.ts +625 -0
- package/cli/agentAliases.ts +2 -1
- package/cli/agentRunCommand.ts +10 -32
- package/cli/agentRuntimeLocal.ts +1 -0
- package/cli/client/agentRun.ts +67 -95
- package/cli/client/localRuntimeAdapter.ts +323 -54
- package/cli/commandRegistry.ts +2 -2
- package/cli/machineWsRunDispatch.ts +51 -6
- package/cli/offlineMarxistsAgentCommand.ts +6 -4
- package/client/agentConfigResolver.test.ts +2 -2
- package/client/agentRun.test.ts +175 -57
- package/client/agentRun.ts +67 -95
- package/client/localRuntimeAdapter.test.ts +682 -37
- package/client/localRuntimeAdapter.ts +323 -54
- package/client/localRuntimeDryRun.test.ts +23 -9
- package/client/localToolPolicy.test.ts +4 -4
- package/commandRegistry.ts +2 -2
- package/core/builtinAgents.ts +209 -0
- package/core/config.ts +15 -0
- package/core/crypto.ts +51 -0
- package/core/generateKeyPairFromSeedV1.ts +15 -0
- package/core/generateMainKey.ts +22 -0
- package/core/init.ts +41 -0
- package/core/localOrigins.ts +8 -0
- package/core/password.ts +13 -0
- package/core/prefix.ts +23 -0
- package/create/editor/transforms/fromMarkdown/index.ts +260 -0
- package/create/editor/transforms/fromMarkdown/inline.ts +285 -0
- package/create/editor/transforms/fromMarkdown/table.ts +130 -0
- package/create/editor/transforms/markdownToSlate.ts +24 -0
- package/create/editor/transforms/slateToRenderMarkdown.ts +218 -0
- package/create/editor/transforms/slateToSimplifiedMarkdown.ts +224 -0
- package/create/editor/transforms/slateToText.ts +33 -0
- package/create/editor/utils/slateUtils.ts +233 -0
- package/create/space/addSpaceAction.ts +193 -0
- package/create/space/category/categoryActions.ts +427 -0
- package/create/space/changeSpaceAction.ts +36 -0
- package/create/space/constants.ts +8 -0
- package/create/space/content/addContentAction.ts +116 -0
- package/create/space/content/contentThunks.ts +90 -0
- package/create/space/content/deleteContentFromSpaceAction.ts +187 -0
- package/create/space/content/deleteMultipleContentAction.ts +112 -0
- package/create/space/content/moveContentAction.ts +285 -0
- package/create/space/content/updateContentCategoryAction.ts +154 -0
- package/create/space/content/updateContentTitleAction.ts +151 -0
- package/create/space/content/uploadAndAddFileToSpaceAction.ts +98 -0
- package/create/space/contentKeyUtils.ts +117 -0
- package/create/space/deleteSpaceAction.ts +276 -0
- package/create/space/fetchSpaceAction.ts +70 -0
- package/create/space/fetchSpaceSidebarStateAction.ts +26 -0
- package/create/space/loadDefaultSpaceAction.ts +57 -0
- package/create/space/member/addMemberAction.ts +79 -0
- package/create/space/member/fetchUserSpaceMembershipsAction.ts +386 -0
- package/create/space/member/memberThunks.ts +47 -0
- package/create/space/member/removeMemberAction.ts +67 -0
- package/create/space/resolvePreferredSpaceId.ts +113 -0
- package/create/space/sidebarVisibleTypes.ts +241 -0
- package/create/space/space.locale.ts +1087 -0
- package/create/space/spaceAccess.ts +182 -0
- package/create/space/spaceCollapsedState.ts +54 -0
- package/create/space/spaceKeys.ts +65 -0
- package/create/space/spaceSlice.ts +328 -0
- package/create/space/spaceThunks.ts +410 -0
- package/create/space/types.ts +27 -0
- package/create/space/updateSpaceAction.ts +107 -0
- package/create/space/utils/permissions.ts +47 -0
- package/create/types.ts +21 -0
- package/database/actions/agentReadResolution.ts +33 -0
- package/database/actions/common.ts +243 -0
- package/database/actions/deleteFile.ts +40 -0
- package/database/actions/fileContent.ts +125 -0
- package/database/actions/patch.ts +155 -0
- package/database/actions/read.ts +358 -0
- package/database/actions/readAndWait.ts +260 -0
- package/database/actions/readRequestManager.ts +120 -0
- package/database/actions/readResolution.ts +125 -0
- package/database/actions/remove.ts +94 -0
- package/database/actions/replication.ts +477 -0
- package/database/actions/upload.ts +186 -0
- package/database/actions/upsert.ts +56 -0
- package/database/actions/write.ts +146 -0
- package/database/authority/ownerKey.ts +80 -0
- package/database/authority/recordAuthority.ts +67 -0
- package/database/authority/userAuthorityRegistry.ts +54 -0
- package/database/client/fetchUserData.ts +61 -0
- package/database/dbActionThunks.ts +1 -0
- package/database/dbSlice.ts +149 -0
- package/database/fileRing.ts +51 -0
- package/database/fileStorage.ts +232 -0
- package/database/fileUrl.ts +34 -0
- package/database/keys.ts +767 -0
- package/database/queryPrefixes.ts +14 -0
- package/database/requests.ts +443 -0
- package/database/runtimeServerContext.ts +54 -0
- package/database/server/levelAuthorityStore.ts +20 -1
- package/database/tenantPlacement.ts +57 -0
- package/database/userPreferenceRegister.ts +131 -0
- package/database/utils/ulid.ts +1 -0
- package/integrations/anthropic/anthropicModels.ts +47 -0
- package/integrations/deepseek/models.ts +37 -0
- package/integrations/google/models.ts +341 -0
- package/integrations/openai/chatCompletionStreamMode.ts +10 -0
- package/integrations/openai/filterAndCleanMessages.ts +284 -0
- package/integrations/openai/flexTier.ts +102 -0
- package/integrations/openai/generateOpenAIRequestBody.ts +274 -0
- package/integrations/openai/generateResponseRequestBody.ts +77 -0
- package/integrations/openai/models.ts +271 -0
- package/integrations/openai/providerBodyCompatibility.ts +23 -0
- package/integrations/openai/responsesHelpers.ts +297 -0
- package/integrations/x-reader/types.ts +109 -0
- package/integrations/xai/models.ts +13 -0
- package/integrations/xhs-reader/analyze/profileAnalyzer.ts +205 -0
- package/integrations/xhs-reader/backends/playwrightProfileCollector.ts +624 -0
- package/integrations/xhs-reader/bridge/readXhsProfileWithBridge.ts +227 -0
- package/integrations/xhs-reader/client/xhsWebApiClient.ts +229 -0
- package/integrations/xhs-reader/normalize.ts +390 -0
- package/integrations/xhs-reader/orchestrator.ts +178 -0
- package/integrations/xhs-reader/redaction.ts +68 -0
- package/integrations/xhs-reader/types.ts +244 -0
- package/integrations/xhs-reader/url.ts +57 -0
- package/machineWsRunDispatch.ts +51 -6
- package/offlineMarxistsAgentCommand.ts +6 -4
- package/package.json +430 -3
- package/render/page/createPageAction.ts +183 -0
- package/render/page/docSlice.ts +468 -0
- package/render/page/types.ts +17 -0
- package/render/table/createTableAction.ts +270 -0
- package/render/table/deleteTableAction.ts +193 -0
- package/render/table/fetchAndCacheTableRows.ts +174 -0
- package/render/table/tableSlice.ts +1106 -0
- package/render/table/toolValueUtils.ts +363 -0
- package/render/table/types.ts +252 -0
- package/render/table/utils/tableSerialization.ts +50 -0
- package/server/handlers/agentRun/types.ts +104 -0
- package/share/action.ts +225 -0
- package/share/helpers.ts +302 -0
- package/share/keys.ts +243 -0
- package/share/publicSharePolicy.ts +27 -0
- package/share/shareSlice.ts +53 -0
- package/share/types.ts +100 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const RETRYABLE_SERVER_PROXY_STATUSES = new Set([502, 503, 504]);
|
|
2
|
+
const DEFAULT_SERVER_PROXY_RETRY_AFTER_MS = 1_500;
|
|
3
|
+
const MAX_SERVER_PROXY_RETRIES = 1;
|
|
4
|
+
|
|
5
|
+
const normalizeServerProxyRetryAfterMs = (value: unknown) => {
|
|
6
|
+
const parsed = Number(value);
|
|
7
|
+
return Number.isFinite(parsed) && parsed >= 0
|
|
8
|
+
? Math.round(parsed)
|
|
9
|
+
: DEFAULT_SERVER_PROXY_RETRY_AFTER_MS;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const resolveServerProxyRetryAfterMs = (response: Response) => {
|
|
13
|
+
const retryAfterHeader = response.headers.get("Retry-After");
|
|
14
|
+
if (!retryAfterHeader) {
|
|
15
|
+
return DEFAULT_SERVER_PROXY_RETRY_AFTER_MS;
|
|
16
|
+
}
|
|
17
|
+
const seconds = Number(retryAfterHeader);
|
|
18
|
+
if (Number.isFinite(seconds) && seconds >= 0) {
|
|
19
|
+
return Math.round(seconds * 1_000);
|
|
20
|
+
}
|
|
21
|
+
return DEFAULT_SERVER_PROXY_RETRY_AFTER_MS;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const isRetryableServerProxyFetchError = (error: unknown) => {
|
|
25
|
+
if (!error || (error as any)?.name === "AbortError") return false;
|
|
26
|
+
const message =
|
|
27
|
+
typeof (error as any)?.message === "string"
|
|
28
|
+
? (error as any).message
|
|
29
|
+
: String(error);
|
|
30
|
+
return /ECONNREFUSED|ECONNRESET|EPIPE|socket hang up|network error|failed to fetch|fetch failed|connection closed|load failed|502|503|504/i.test(
|
|
31
|
+
message
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const waitForServerProxyRetry = async (
|
|
36
|
+
retryAfterMs: number,
|
|
37
|
+
signal?: AbortSignal
|
|
38
|
+
) => {
|
|
39
|
+
if (retryAfterMs <= 0) return;
|
|
40
|
+
if (signal?.aborted) {
|
|
41
|
+
const abortError = new Error("Aborted");
|
|
42
|
+
abortError.name = "AbortError";
|
|
43
|
+
throw abortError;
|
|
44
|
+
}
|
|
45
|
+
await new Promise<void>((resolve, reject) => {
|
|
46
|
+
const onAbort = () => {
|
|
47
|
+
clearTimeout(timeoutId);
|
|
48
|
+
signal?.removeEventListener("abort", onAbort);
|
|
49
|
+
const abortError = new Error("Aborted");
|
|
50
|
+
abortError.name = "AbortError";
|
|
51
|
+
reject(abortError);
|
|
52
|
+
};
|
|
53
|
+
const timeoutId = setTimeout(() => {
|
|
54
|
+
signal?.removeEventListener("abort", onAbort);
|
|
55
|
+
resolve();
|
|
56
|
+
}, normalizeServerProxyRetryAfterMs(retryAfterMs));
|
|
57
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const performServerProxyFetchWithRetry = async ({
|
|
62
|
+
execute,
|
|
63
|
+
signal,
|
|
64
|
+
logPrefix = "[fetchWithServerProxy]",
|
|
65
|
+
}: {
|
|
66
|
+
execute: () => Promise<Response>;
|
|
67
|
+
signal?: AbortSignal;
|
|
68
|
+
logPrefix?: string;
|
|
69
|
+
}): Promise<Response> => {
|
|
70
|
+
for (let attempt = 0; attempt <= MAX_SERVER_PROXY_RETRIES; attempt += 1) {
|
|
71
|
+
try {
|
|
72
|
+
const response = await execute();
|
|
73
|
+
if (
|
|
74
|
+
attempt < MAX_SERVER_PROXY_RETRIES &&
|
|
75
|
+
RETRYABLE_SERVER_PROXY_STATUSES.has(response.status)
|
|
76
|
+
) {
|
|
77
|
+
const retryAfterMs = resolveServerProxyRetryAfterMs(response);
|
|
78
|
+
console.warn(
|
|
79
|
+
`${logPrefix} 检测到${response.status}状态,${retryAfterMs}ms后重试一次...`
|
|
80
|
+
);
|
|
81
|
+
await waitForServerProxyRetry(retryAfterMs, signal);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
return response;
|
|
85
|
+
} catch (error: any) {
|
|
86
|
+
if (
|
|
87
|
+
attempt < MAX_SERVER_PROXY_RETRIES &&
|
|
88
|
+
isRetryableServerProxyFetchError(error)
|
|
89
|
+
) {
|
|
90
|
+
console.warn(
|
|
91
|
+
`${logPrefix} 检测到网络瞬断,${DEFAULT_SERVER_PROXY_RETRY_AFTER_MS}ms后重试一次...`,
|
|
92
|
+
error
|
|
93
|
+
);
|
|
94
|
+
await waitForServerProxyRetry(DEFAULT_SERVER_PROXY_RETRY_AFTER_MS, signal);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
throw new Error("server proxy retry exhausted unexpectedly");
|
|
102
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Agent } from "../../app/types";
|
|
2
|
+
|
|
3
|
+
export const shouldUseServerProxy = (
|
|
4
|
+
agentConfig: Pick<Agent, "provider" | "useServerProxy">,
|
|
5
|
+
requestProvider?: string
|
|
6
|
+
): boolean => {
|
|
7
|
+
const effectiveProvider = (requestProvider || agentConfig.provider || "").toLowerCase();
|
|
8
|
+
|
|
9
|
+
// Google requests are forced through the server proxy for now because the
|
|
10
|
+
// native Gemini image bridge, provider fallback, and request translation live
|
|
11
|
+
// on the server. Keep this centralized so we can later add direct/custom-url
|
|
12
|
+
// opt-out for user-managed keys without having to update web/native twice.
|
|
13
|
+
if (effectiveProvider === "google") {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return !!agentConfig.useServerProxy;
|
|
18
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export const MAX_INITIAL_STREAM_RETRIES = 1;
|
|
2
|
+
export const DEFAULT_INITIAL_STREAM_RETRY_AFTER_MS = 1_500;
|
|
3
|
+
|
|
4
|
+
const normalizeInitialStreamRetryAfterMs = (value: unknown) => {
|
|
5
|
+
const parsed = Number(value);
|
|
6
|
+
return Number.isFinite(parsed) && parsed >= 0
|
|
7
|
+
? Math.round(parsed)
|
|
8
|
+
: DEFAULT_INITIAL_STREAM_RETRY_AFTER_MS;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const isRetryableInitialStreamError = (error: unknown) => {
|
|
12
|
+
if (!error || (error as any)?.name === "AbortError") return false;
|
|
13
|
+
const message =
|
|
14
|
+
typeof (error as any)?.message === "string"
|
|
15
|
+
? (error as any).message
|
|
16
|
+
: String(error);
|
|
17
|
+
return /ECONNRESET|ECONNREFUSED|EPIPE|socket hang up|network error|failed to fetch|fetch failed|load failed|connection closed|stream ended before first visible delta|response stream ended before first visible delta|模型响应流 .* 秒内没有返回新内容/i.test(
|
|
18
|
+
message
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const waitForInitialStreamRetry = async (
|
|
23
|
+
retryAfterMs: number,
|
|
24
|
+
signal?: AbortSignal
|
|
25
|
+
) => {
|
|
26
|
+
if (signal?.aborted) {
|
|
27
|
+
const abortError = new Error("Aborted");
|
|
28
|
+
abortError.name = "AbortError";
|
|
29
|
+
throw abortError;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await new Promise<void>((resolve, reject) => {
|
|
33
|
+
const onAbort = () => {
|
|
34
|
+
clearTimeout(timeoutId);
|
|
35
|
+
signal?.removeEventListener("abort", onAbort);
|
|
36
|
+
const abortError = new Error("Aborted");
|
|
37
|
+
abortError.name = "AbortError";
|
|
38
|
+
reject(abortError);
|
|
39
|
+
};
|
|
40
|
+
const timeoutId = setTimeout(() => {
|
|
41
|
+
signal?.removeEventListener("abort", onAbort);
|
|
42
|
+
resolve();
|
|
43
|
+
}, normalizeInitialStreamRetryAfterMs(retryAfterMs));
|
|
44
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
45
|
+
});
|
|
46
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// 文件路径: ai/chat/updateTotalUsage.ts
|
|
2
|
+
|
|
3
|
+
import { CompletionUsage } from "../../chat/messages/types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ✨ 新增辅助函数 ✨
|
|
7
|
+
* 根据新的数据块更新累积的 token 使用量。
|
|
8
|
+
* @param currentUsage - 当前的累积 usage 对象,可能为 null。
|
|
9
|
+
* @param newUsageChunk - 从流中收到的新 usage 数据块。
|
|
10
|
+
* @returns 更新后的 usage 对象。
|
|
11
|
+
*/
|
|
12
|
+
export function updateTotalUsage(
|
|
13
|
+
currentUsage: CompletionUsage | null,
|
|
14
|
+
newUsageChunk: Partial<CompletionUsage>
|
|
15
|
+
): CompletionUsage | null {
|
|
16
|
+
if (!newUsageChunk) {
|
|
17
|
+
return currentUsage;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 如果是第一次接收,直接克隆新数据块
|
|
21
|
+
if (!currentUsage) {
|
|
22
|
+
return {
|
|
23
|
+
completion_tokens: 0,
|
|
24
|
+
prompt_tokens: 0,
|
|
25
|
+
total_tokens: 0,
|
|
26
|
+
...newUsageChunk,
|
|
27
|
+
} as CompletionUsage;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 否则,在现有基础上进行累加或更新
|
|
31
|
+
const updatedUsage: CompletionUsage = { ...currentUsage };
|
|
32
|
+
|
|
33
|
+
// === token 相关 ===
|
|
34
|
+
// 这里假设 usage 是累积的还是单次的?
|
|
35
|
+
// 如果是 OpenAI 流式 output,usage 通常只在最后一次 chunk 发送完整的统计值 (除了 Azure 等可能变体)。
|
|
36
|
+
// 如果是这种情况,我们直接覆盖即可。
|
|
37
|
+
// 但如果服务端分片发送增量(比较少见但有可能),则需要累加。
|
|
38
|
+
// 原代码逻辑是:
|
|
39
|
+
// updatedUsage.completion_tokens = newUsageChunk.completion_tokens ?? updatedUsage.completion_tokens;
|
|
40
|
+
// 这意味着如果有新值就覆盖,没新值保持原样。这适合 "最后一次发送完整值" 的场景。
|
|
41
|
+
|
|
42
|
+
// 保持原逻辑:覆盖
|
|
43
|
+
if (newUsageChunk.completion_tokens !== undefined) updatedUsage.completion_tokens = newUsageChunk.completion_tokens;
|
|
44
|
+
if (newUsageChunk.prompt_tokens !== undefined) updatedUsage.prompt_tokens = newUsageChunk.prompt_tokens;
|
|
45
|
+
if (newUsageChunk.total_tokens !== undefined) updatedUsage.total_tokens = newUsageChunk.total_tokens;
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if (newUsageChunk.prompt_tokens_details) {
|
|
49
|
+
updatedUsage.prompt_tokens_details = {
|
|
50
|
+
...(updatedUsage.prompt_tokens_details || {}),
|
|
51
|
+
...newUsageChunk.prompt_tokens_details,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (newUsageChunk.completion_tokens_details) {
|
|
55
|
+
updatedUsage.completion_tokens_details = {
|
|
56
|
+
...(updatedUsage.completion_tokens_details || {}),
|
|
57
|
+
...newUsageChunk.completion_tokens_details,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// === OpenRouter usage.cost 相关 ===
|
|
62
|
+
if (typeof newUsageChunk.cost === "number") {
|
|
63
|
+
// 对于 OpenRouter,通常只有最后一条 chunk 带 cost,这里直接覆盖即可
|
|
64
|
+
updatedUsage.cost = newUsageChunk.cost;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (newUsageChunk.cost_details) {
|
|
68
|
+
updatedUsage.cost_details = {
|
|
69
|
+
...(updatedUsage.cost_details || {}),
|
|
70
|
+
...newUsageChunk.cost_details,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
typeof newUsageChunk.billing_provider === "string" &&
|
|
76
|
+
newUsageChunk.billing_provider.trim()
|
|
77
|
+
) {
|
|
78
|
+
updatedUsage.billing_provider = newUsageChunk.billing_provider.trim();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (
|
|
82
|
+
typeof newUsageChunk.billing_model === "string" &&
|
|
83
|
+
newUsageChunk.billing_model.trim()
|
|
84
|
+
) {
|
|
85
|
+
updatedUsage.billing_model = newUsageChunk.billing_model.trim();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return updatedUsage;
|
|
89
|
+
}
|
|
@@ -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 "../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
|
+
};
|