@lobehub/lobehub 2.0.0-next.294 → 2.0.0-next.295
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/CHANGELOG.md +25 -0
- package/apps/desktop/src/main/__mocks__/node-mac-permissions.ts +0 -1
- package/apps/desktop/src/main/__mocks__/setup.ts +0 -1
- package/apps/desktop/src/main/controllers/__tests__/SystemCtr.test.ts +1 -4
- package/apps/desktop/tsconfig.json +4 -10
- package/changelog/v1.json +9 -0
- package/e2e/scripts/setup.ts +45 -32
- package/package.json +1 -1
- package/packages/database/src/models/__tests__/knowledgeBase.test.ts +1 -1
- package/packages/database/src/repositories/knowledge/index.ts +1 -4
- package/packages/types/src/discover/assistants.ts +2 -2
- package/scripts/migrate-spa-navigation.ts +129 -0
- package/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/route.ts +112 -109
- package/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-user-topics/route.ts +125 -113
- package/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-users/route.ts +74 -65
- package/src/app/[variants]/(auth)/auth-error/page.tsx +1 -1
- package/src/app/[variants]/(auth)/login/[[...login]]/page.tsx +1 -1
- package/src/app/[variants]/(auth)/next-auth/error/AuthErrorPage.tsx +1 -1
- package/src/app/[variants]/(auth)/next-auth/signin/AuthSignInBox.tsx +1 -1
- package/src/app/[variants]/(auth)/oauth/callback/error/page.tsx +1 -1
- package/src/app/[variants]/(auth)/oauth/callback/success/page.tsx +1 -1
- package/src/app/[variants]/(auth)/oauth/consent/[uid]/page.tsx +1 -1
- package/src/app/[variants]/(auth)/reset-password/layout.tsx +1 -1
- package/src/app/[variants]/(auth)/reset-password/page.tsx +2 -2
- package/src/app/[variants]/(auth)/signin/layout.tsx +1 -1
- package/src/app/[variants]/(auth)/signin/useSignIn.ts +1 -1
- package/src/app/[variants]/(auth)/signup/[[...signup]]/BetterAuthSignUpForm.tsx +2 -2
- package/src/app/[variants]/(auth)/signup/[[...signup]]/page.tsx +1 -1
- package/src/app/[variants]/(auth)/signup/[[...signup]]/useSignUp.tsx +1 -1
- package/src/app/[variants]/(auth)/verify-email/layout.tsx +1 -1
- package/src/app/[variants]/(auth)/verify-email/page.tsx +2 -2
- package/src/app/[variants]/(main)/_layout/index.tsx +1 -1
- package/src/app/[variants]/(main)/agent/_layout/Sidebar/Cron/CronTopicGroup.tsx +1 -1
- package/src/app/[variants]/(main)/agent/_layout/Sidebar/Header/AddTopicButon.tsx +1 -1
- package/src/app/[variants]/(main)/agent/_layout/Sidebar/Header/Nav.tsx +1 -1
- package/src/app/[variants]/(main)/agent/_layout/Sidebar/Topic/AllTopicsDrawer/index.tsx +1 -1
- package/src/app/[variants]/(main)/agent/_layout/Sidebar/Topic/hooks/useThreadNavigation.ts +1 -1
- package/src/app/[variants]/(main)/agent/_layout/Sidebar/Topic/hooks/useTopicNavigation.ts +1 -1
- package/src/app/[variants]/(main)/agent/features/TelemetryNotification.tsx +2 -3
- package/src/app/[variants]/(main)/community/(detail)/assistant/features/Details/Nav.tsx +9 -9
- package/src/app/[variants]/(main)/community/(detail)/assistant/features/Details/Versions/index.tsx +2 -3
- package/src/app/[variants]/(main)/community/(detail)/features/MakedownRender.tsx +1 -2
- package/src/app/[variants]/(main)/community/(detail)/features/ShareButton.tsx +2 -3
- package/src/app/[variants]/(main)/community/(detail)/features/Toc/Heading.tsx +2 -3
- package/src/app/[variants]/(main)/community/(detail)/mcp/features/Details/Versions/index.tsx +2 -2
- package/src/app/[variants]/(main)/community/(detail)/model/features/Details/Nav.tsx +12 -11
- package/src/app/[variants]/(main)/community/(detail)/model/features/Details/Parameter/ParameterItem.tsx +2 -3
- package/src/app/[variants]/(main)/community/(detail)/provider/features/Details/Nav.tsx +11 -10
- package/src/app/[variants]/(main)/community/(detail)/provider/features/Header.tsx +10 -9
- package/src/app/[variants]/(main)/community/(list)/(home)/index.tsx +1 -1
- package/src/app/[variants]/(main)/community/(list)/assistant/features/Category/useCategory.tsx +1 -1
- package/src/app/[variants]/(main)/community/(list)/features/SortButton/index.tsx +2 -3
- package/src/app/[variants]/(main)/community/_layout/Sidebar/Header/Nav.tsx +1 -1
- package/src/app/[variants]/(main)/community/features/CreateButton/Inner.tsx +1 -1
- package/src/app/[variants]/(main)/community/features/CreateButton/index.tsx +1 -1
- package/src/app/[variants]/(main)/community/features/Search.tsx +1 -2
- package/src/app/[variants]/(main)/community/features/Title.tsx +5 -5
- package/src/app/[variants]/(main)/group/_layout/Sidebar/Header/Nav.tsx +1 -1
- package/src/app/[variants]/(main)/group/_layout/Sidebar/Topic/AllTopicsDrawer/index.tsx +1 -1
- package/src/app/[variants]/(main)/group/_layout/Sidebar/Topic/hooks/useThreadNavigation.ts +1 -1
- package/src/app/[variants]/(main)/group/features/Conversation/Header/ShareButton/index.tsx +1 -1
- package/src/app/[variants]/(main)/group/features/TelemetryNotification.tsx +2 -3
- package/src/app/[variants]/(main)/home/_layout/Body/Agent/AllAgentsDrawer/index.tsx +1 -1
- package/src/app/[variants]/(main)/hooks/useActiveTabKey.ts +6 -11
- package/src/app/[variants]/(main)/image/NotSupportClient.tsx +4 -3
- package/src/app/[variants]/(main)/image/_layout/ConfigPanel/components/ImageUpload.tsx +1 -1
- package/src/app/[variants]/(main)/image/_layout/ConfigPanel/components/MultiImagesUpload/ImageManageModal.tsx +1 -1
- package/src/app/[variants]/(main)/image/_layout/ConfigPanel/components/MultiImagesUpload/index.tsx +1 -1
- package/src/app/[variants]/(main)/memory/(home)/features/RoleTagCloud/index.tsx +1 -1
- package/src/app/[variants]/(main)/memory/_layout/Sidebar/Header/Nav.tsx +1 -1
- package/src/app/[variants]/(main)/memory/features/SourceLink.tsx +1 -1
- package/src/app/[variants]/(main)/page/_layout/Body/AllPagesDrawer/index.tsx +1 -1
- package/src/app/[variants]/(main)/settings/about/features/ItemCard.tsx +2 -3
- package/src/app/[variants]/(main)/settings/about/features/ItemLink.tsx +2 -3
- package/src/app/[variants]/(main)/settings/about/features/Version.tsx +6 -7
- package/src/app/[variants]/(main)/settings/features/SettingsContent.tsx +1 -1
- package/src/app/[variants]/(main)/settings/features/UpgradeAlert.tsx +4 -4
- package/src/app/[variants]/(main)/settings/provider/(list)/Footer.tsx +2 -2
- package/src/app/[variants]/(main)/settings/provider/detail/index.tsx +1 -1
- package/src/app/[variants]/(main)/settings/provider/detail/ollama/CheckError.tsx +1 -1
- package/src/app/[variants]/(main)/settings/provider/features/ProviderConfig/index.tsx +12 -6
- package/src/app/[variants]/(main)/settings/security/index.tsx +1 -1
- package/src/app/[variants]/(main)/settings/stats/features/overview/ShareButton/ShareModal.tsx +1 -1
- package/src/app/[variants]/(main)/settings/stats/features/rankings/AssistantsRank.tsx +1 -1
- package/src/app/[variants]/(main)/settings/stats/features/rankings/TopicsRank.tsx +1 -1
- package/src/app/[variants]/(mobile)/_layout/index.tsx +1 -1
- package/src/app/[variants]/(mobile)/chat/settings/features/AgentInfoDescription/index.tsx +1 -1
- package/src/app/[variants]/(mobile)/chat/settings/features/SettingButton.tsx +1 -1
- package/src/app/[variants]/(mobile)/router/index.tsx +1 -1
- package/src/app/[variants]/page.tsx +1 -1
- package/src/app/[variants]/router/index.tsx +1 -1
- package/src/components/404/index.tsx +4 -4
- package/src/components/Analytics/index.tsx +1 -1
- package/src/components/BrandWatermark/index.tsx +4 -4
- package/src/components/Branding/ProductLogo/Custom.tsx +1 -1
- package/src/components/Error/index.tsx +3 -4
- package/src/components/GoBack/index.tsx +2 -2
- package/src/components/LabsModal/LabCard.tsx +1 -1
- package/src/components/Link.tsx +25 -5
- package/src/components/OllamaSetupGuide/index.tsx +5 -4
- package/src/components/WebFavicon/index.tsx +1 -1
- package/src/components/client/ClientResponsiveContent/index.tsx +1 -1
- package/src/components/client/ClientResponsiveLayout.tsx +1 -1
- package/src/components/mdx/Image.tsx +1 -1
- package/src/components/mdx/Link.tsx +26 -9
- package/src/features/AlertBanner/CloudBanner.tsx +2 -3
- package/src/features/ChatInput/ActionBar/Model/ControlsForm.tsx +8 -7
- package/src/features/ChatInput/ActionBar/Params/Controls.tsx +1 -3
- package/src/features/ChatInput/ActionBar/Token/index.tsx +1 -1
- package/src/features/ChatInput/Mobile/index.tsx +1 -1
- package/src/features/Conversation/ChatItem/components/MessageContent/index.tsx +1 -1
- package/src/features/Conversation/Error/OllamaBizError/index.tsx +1 -1
- package/src/features/Conversation/Error/OllamaSetupGuide/Desktop.tsx +1 -2
- package/src/features/Conversation/Error/index.tsx +1 -1
- package/src/features/Conversation/Messages/AssistantGroup/Tool/Actions/Settings.tsx +1 -1
- package/src/features/Conversation/Messages/AssistantGroup/Tool/index.tsx +1 -1
- package/src/features/Conversation/Messages/AssistantGroup/index.tsx +1 -1
- package/src/features/Conversation/Messages/Tool/Tool/index.tsx +1 -1
- package/src/features/Conversation/Messages/components/SearchGrounding.tsx +1 -1
- package/src/features/DataImporter/Error.tsx +3 -3
- package/src/features/DevPanel/CacheViewer/cacheProvider.tsx +2 -1
- package/src/features/DevPanel/MetadataViewer/Og.tsx +1 -1
- package/src/features/DevPanel/features/FloatPanel.tsx +1 -1
- package/src/features/DevPanel/features/Table/TooltipContent.tsx +3 -4
- package/src/features/DevPanel/index.tsx +1 -1
- package/src/features/EditorCanvas/InlineToolbar.tsx +1 -6
- package/src/features/FileViewer/NotSupport/index.tsx +2 -3
- package/src/features/Follow/index.tsx +8 -9
- package/src/features/LibraryModal/AddFilesToKnowledgeBase/SelectForm.tsx +2 -2
- package/src/features/MCP/Scores.tsx +1 -1
- package/src/features/MCPPluginDetail/Nav.tsx +8 -8
- package/src/features/MCPPluginDetail/Overview/TagList.tsx +1 -1
- package/src/features/MobileTabBar/index.tsx +2 -1
- package/src/features/OllamaSetupGuide/Desktop.tsx +1 -2
- package/src/features/PWAInstall/Install.tsx +1 -1
- package/src/features/PWAInstall/index.tsx +1 -1
- package/src/features/PluginStore/McpList/index.tsx +1 -1
- package/src/features/PluginStore/PluginList/Detail/Header.tsx +6 -6
- package/src/features/PluginsUI/Render/DefaultType/index.tsx +1 -1
- package/src/features/Portal/Artifacts/Body/Renderer/index.tsx +1 -1
- package/src/features/ResourceManager/components/ChunkDrawer/index.tsx +1 -1
- package/src/features/ResourceManager/components/Explorer/ListView/Skeleton.tsx +26 -26
- package/src/features/ResourceManager/components/Explorer/ToolBar/BatchActionsDropdown.tsx +147 -149
- package/src/features/ResourceManager/index.tsx +1 -1
- package/src/features/Setting/Footer.tsx +4 -5
- package/src/features/User/UserPanel/PanelContent.tsx +1 -1
- package/src/hooks/useActiveTabKey.ts +6 -3
- package/src/hooks/useIsSingleMode.test.ts +10 -24
- package/src/hooks/useIsSingleMode.ts +4 -2
- package/src/hooks/useIsSubSlug.ts +2 -1
- package/src/hooks/useQuery.ts +5 -5
- package/src/layout/GlobalProvider/AppTheme.tsx +2 -2
- package/src/layout/GlobalProvider/StyleRegistry.tsx +1 -1
- package/src/layout/GlobalProvider/useUserStateRedirect.ts +13 -25
- package/src/libs/next/Image.tsx +13 -0
- package/src/libs/next/Link.tsx +13 -0
- package/src/libs/next/dynamic.tsx +13 -0
- package/src/libs/next/index.ts +22 -0
- package/src/libs/next/navigation.ts +22 -0
- package/src/libs/router/Link.tsx +30 -0
- package/src/libs/router/index.ts +18 -0
- package/src/libs/router/navigation.ts +72 -0
- package/src/server/modules/AgentRuntime/AgentStateManager.ts +5 -1
- package/src/store/chat/slices/portal/selectors.test.ts +5 -15
- package/src/store/page/index.ts +1 -1
- package/src/store/page/slices/crud/index.ts +1 -1
- package/src/app/[variants]/(main)/hooks/usePathname.ts +0 -10
- package/src/app/[variants]/(main)/hooks/useQuery.ts +0 -12
- package/src/app/[variants]/(main)/hooks/useRouter.ts +0 -22
- package/src/app/[variants]/(main)/hooks/useSearchParams.ts +0 -11
|
@@ -4,143 +4,145 @@ import {
|
|
|
4
4
|
tracer as upstashWorkflowTracer,
|
|
5
5
|
} from '@lobechat/observability-otel/modules/upstash-workflow';
|
|
6
6
|
import { LayersEnum, MemorySourceType } from '@lobechat/types';
|
|
7
|
-
import {
|
|
7
|
+
import { Client } from '@upstash/qstash';
|
|
8
8
|
import { WorkflowAbort } from '@upstash/workflow';
|
|
9
|
+
import { serve } from '@upstash/workflow/nextjs';
|
|
9
10
|
|
|
11
|
+
import { parseMemoryExtractionConfig } from '@/server/globalConfig/parseMemoryExtractionConfig';
|
|
10
12
|
import {
|
|
11
13
|
MemoryExtractionExecutor,
|
|
12
14
|
type MemoryExtractionPayloadInput,
|
|
13
15
|
normalizeMemoryExtractionPayload,
|
|
14
16
|
} from '@/server/services/memory/userMemory/extract';
|
|
15
|
-
import { Client } from '@upstash/qstash'
|
|
16
|
-
import { parseMemoryExtractionConfig } from '@/server/globalConfig/parseMemoryExtractionConfig';
|
|
17
17
|
|
|
18
18
|
const { upstashWorkflowExtraHeaders } = parseMemoryExtractionConfig();
|
|
19
19
|
|
|
20
20
|
const CEP_LAYERS: LayersEnum[] = [LayersEnum.Context, LayersEnum.Experience, LayersEnum.Preference];
|
|
21
21
|
const IDENTITY_LAYERS: LayersEnum[] = [LayersEnum.Identity];
|
|
22
22
|
|
|
23
|
-
export const { POST } = serve<MemoryExtractionPayloadInput>(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
console.log('[chat-topic][batch] Starting batch topic processing workflow', {
|
|
42
|
-
topicIds: payload.topicIds,
|
|
43
|
-
userIds: payload.userIds,
|
|
23
|
+
export const { POST } = serve<MemoryExtractionPayloadInput>(
|
|
24
|
+
(context) =>
|
|
25
|
+
upstashWorkflowTracer.startActiveSpan(
|
|
26
|
+
'workflow:memory-user-memory:process-topics',
|
|
27
|
+
async (span) => {
|
|
28
|
+
const payload = normalizeMemoryExtractionPayload(context.requestPayload || {});
|
|
29
|
+
|
|
30
|
+
span.setAttributes({
|
|
31
|
+
...buildUpstashWorkflowAttributes(context),
|
|
32
|
+
'workflow.memory_user_memory.force_all': payload.forceAll,
|
|
33
|
+
'workflow.memory_user_memory.force_topics': payload.forceTopics,
|
|
34
|
+
'workflow.memory_user_memory.layers': payload.layers.join(','),
|
|
35
|
+
'workflow.memory_user_memory.source': payload.sources.join(','),
|
|
36
|
+
'workflow.memory_user_memory.topic_count': payload.topicIds.length,
|
|
37
|
+
'workflow.memory_user_memory.user_count': payload.userIds.length,
|
|
38
|
+
'workflow.name': 'memory-user-memory:process-topics',
|
|
44
39
|
});
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
41
|
+
try {
|
|
42
|
+
console.log('[chat-topic][batch] Starting batch topic processing workflow', {
|
|
43
|
+
topicIds: payload.topicIds,
|
|
44
|
+
userIds: payload.userIds,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (!payload.userIds.length) {
|
|
48
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
message: 'No user id provided for topic batch.',
|
|
52
|
+
processedTopics: 0,
|
|
53
|
+
processedUsers: 0,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (!payload.topicIds.length) {
|
|
57
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
message: 'No topic ids provided for extraction.',
|
|
61
|
+
processedTopics: 0,
|
|
62
|
+
processedUsers: 0,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (!payload.sources.includes(MemorySourceType.ChatTopic)) {
|
|
66
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
message: 'Source not supported in topic batch.',
|
|
70
|
+
processedTopics: 0,
|
|
71
|
+
processedUsers: 0,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const userId = payload.userIds[0];
|
|
76
|
+
const executor = await MemoryExtractionExecutor.create();
|
|
77
|
+
|
|
78
|
+
// CEP: run in parallel across the batch
|
|
79
|
+
await Promise.all(
|
|
80
|
+
payload.topicIds.map((topicId, index) =>
|
|
81
|
+
context.run(
|
|
82
|
+
`memory:user-memory:extract:users:${userId}:topics:${topicId}:cep:${index}`,
|
|
83
|
+
() =>
|
|
84
|
+
executor.extractTopic({
|
|
85
|
+
forceAll: payload.forceAll,
|
|
86
|
+
forceTopics: payload.forceTopics,
|
|
87
|
+
from: payload.from,
|
|
88
|
+
layers: CEP_LAYERS,
|
|
89
|
+
source: MemorySourceType.ChatTopic,
|
|
90
|
+
to: payload.to,
|
|
91
|
+
topicId,
|
|
92
|
+
userId,
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
95
|
+
),
|
|
96
|
+
);
|
|
76
97
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
`memory:user-memory:extract:users:${userId}:topics:${topicId}:cep:${index}`,
|
|
98
|
+
// Identity: run sequentially for the batch
|
|
99
|
+
for (const [index, topicId] of payload.topicIds.entries()) {
|
|
100
|
+
await context.run(
|
|
101
|
+
`memory:user-memory:extract:users:${userId}:topics:${topicId}:identity:${index}`,
|
|
82
102
|
() =>
|
|
83
103
|
executor.extractTopic({
|
|
84
104
|
forceAll: payload.forceAll,
|
|
85
105
|
forceTopics: payload.forceTopics,
|
|
86
106
|
from: payload.from,
|
|
87
|
-
layers:
|
|
107
|
+
layers: IDENTITY_LAYERS,
|
|
88
108
|
source: MemorySourceType.ChatTopic,
|
|
89
109
|
to: payload.to,
|
|
90
110
|
topicId,
|
|
91
111
|
userId,
|
|
92
112
|
}),
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
// Identity: run sequentially for the batch
|
|
98
|
-
for (const [index, topicId] of payload.topicIds.entries()) {
|
|
99
|
-
await context.run(
|
|
100
|
-
`memory:user-memory:extract:users:${userId}:topics:${topicId}:identity:${index}`,
|
|
101
|
-
() =>
|
|
102
|
-
executor.extractTopic({
|
|
103
|
-
forceAll: payload.forceAll,
|
|
104
|
-
forceTopics: payload.forceTopics,
|
|
105
|
-
from: payload.from,
|
|
106
|
-
layers: IDENTITY_LAYERS,
|
|
107
|
-
source: MemorySourceType.ChatTopic,
|
|
108
|
-
to: payload.to,
|
|
109
|
-
topicId,
|
|
110
|
-
userId,
|
|
111
|
-
}),
|
|
112
|
-
);
|
|
113
|
-
}
|
|
113
|
+
);
|
|
114
|
+
}
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
console.log('[chat-topic][batch] Batch topic processing workflow completed', {
|
|
117
|
+
processedTopics: payload.topicIds.length,
|
|
118
|
+
});
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
processedTopics: payload.topicIds.length,
|
|
124
|
+
processedUsers: payload.userIds.length,
|
|
125
|
+
};
|
|
126
|
+
} catch (error) {
|
|
127
|
+
// NOTICE: Let WorkflowAbort bubble up (used internally by Upstash); record others
|
|
128
|
+
if (error instanceof WorkflowAbort) {
|
|
129
|
+
console.warn('workflow aborted:', error.message);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
span.recordException(error as Error);
|
|
134
|
+
span.setStatus({
|
|
135
|
+
code: SpanStatusCode.ERROR,
|
|
136
|
+
message: error instanceof Error ? error.message : 'process-topics workflow failed',
|
|
137
|
+
});
|
|
120
138
|
|
|
121
|
-
return {
|
|
122
|
-
processedTopics: payload.topicIds.length,
|
|
123
|
-
processedUsers: payload.userIds.length,
|
|
124
|
-
};
|
|
125
|
-
} catch (error) {
|
|
126
|
-
// NOTICE: Let WorkflowAbort bubble up (used internally by Upstash); record others
|
|
127
|
-
if (error instanceof WorkflowAbort) {
|
|
128
|
-
console.warn('workflow aborted:', error.message);
|
|
129
139
|
throw error;
|
|
140
|
+
} finally {
|
|
141
|
+
span.end();
|
|
130
142
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
code: SpanStatusCode.ERROR,
|
|
135
|
-
message: error instanceof Error ? error.message : 'process-topics workflow failed',
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
throw error;
|
|
139
|
-
} finally {
|
|
140
|
-
span.end();
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
), {
|
|
143
|
+
},
|
|
144
|
+
),
|
|
145
|
+
{
|
|
144
146
|
// NOTICE(@nekomeowww): Here as scenarios like Vercel Deployment Protection,
|
|
145
147
|
// intermediate context.run(...) won't offer customizable headers like context.trigger(...) / client.trigger(...)
|
|
146
148
|
// for passing additional headers, we have to provide a custom QStash client with the required headers here.
|
|
@@ -151,6 +153,7 @@ export const { POST } = serve<MemoryExtractionPayloadInput>((context) =>
|
|
|
151
153
|
headers: {
|
|
152
154
|
...upstashWorkflowExtraHeaders,
|
|
153
155
|
},
|
|
154
|
-
token: process.env.QSTASH_TOKEN
|
|
155
|
-
})
|
|
156
|
-
}
|
|
156
|
+
token: process.env.QSTASH_TOKEN!,
|
|
157
|
+
}),
|
|
158
|
+
},
|
|
159
|
+
);
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { MemorySourceType } from '@lobechat/types';
|
|
2
|
+
import { Client } from '@upstash/qstash';
|
|
1
3
|
import { serve } from '@upstash/workflow/nextjs';
|
|
4
|
+
|
|
2
5
|
import type { ListTopicsForMemoryExtractorCursor } from '@/database/models/topic';
|
|
6
|
+
import { parseMemoryExtractionConfig } from '@/server/globalConfig/parseMemoryExtractionConfig';
|
|
3
7
|
import {
|
|
4
8
|
MemoryExtractionExecutor,
|
|
5
9
|
type MemoryExtractionPayloadInput,
|
|
@@ -8,135 +12,143 @@ import {
|
|
|
8
12
|
normalizeMemoryExtractionPayload,
|
|
9
13
|
} from '@/server/services/memory/userMemory/extract';
|
|
10
14
|
import { forEachBatchSequential } from '@/server/services/memory/userMemory/topicBatching';
|
|
11
|
-
import { MemorySourceType } from '@lobechat/types';
|
|
12
|
-
import { parseMemoryExtractionConfig } from '@/server/globalConfig/parseMemoryExtractionConfig';
|
|
13
|
-
import { Client } from '@upstash/qstash'
|
|
14
15
|
|
|
15
16
|
const TOPIC_PAGE_SIZE = 50;
|
|
16
17
|
const TOPIC_BATCH_SIZE = 4;
|
|
17
18
|
|
|
18
19
|
const { upstashWorkflowExtraHeaders } = parseMemoryExtractionConfig();
|
|
19
20
|
|
|
20
|
-
export const { POST } = serve<MemoryExtractionPayloadInput>(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const executor = await MemoryExtractionExecutor.create();
|
|
30
|
-
|
|
31
|
-
const scheduleNextPage = async (userId: string, cursorCreatedAt: Date, cursorId: string) => {
|
|
32
|
-
await MemoryExtractionWorkflowService.triggerProcessUserTopics({
|
|
33
|
-
...buildWorkflowPayloadInput({
|
|
34
|
-
...params,
|
|
35
|
-
topicCursor: {
|
|
36
|
-
createdAt: cursorCreatedAt.toISOString(),
|
|
37
|
-
id: cursorId,
|
|
38
|
-
userId,
|
|
39
|
-
},
|
|
40
|
-
topicIds: [],
|
|
41
|
-
userId,
|
|
42
|
-
userIds: [userId],
|
|
43
|
-
}),
|
|
44
|
-
}, { extraHeaders: upstashWorkflowExtraHeaders });
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
for (const userId of params.userIds) {
|
|
48
|
-
const topicCursor =
|
|
49
|
-
params.topicCursor && params.topicCursor.userId === userId
|
|
50
|
-
? {
|
|
51
|
-
createdAt: new Date(params.topicCursor.createdAt),
|
|
52
|
-
id: params.topicCursor.id,
|
|
53
|
-
}
|
|
54
|
-
: undefined;
|
|
21
|
+
export const { POST } = serve<MemoryExtractionPayloadInput>(
|
|
22
|
+
async (context) => {
|
|
23
|
+
const params = normalizeMemoryExtractionPayload(context.requestPayload || {});
|
|
24
|
+
if (!params.userIds.length) {
|
|
25
|
+
return { message: 'No user ids provided for topic processing.' };
|
|
26
|
+
}
|
|
27
|
+
if (!params.sources.includes(MemorySourceType.ChatTopic)) {
|
|
28
|
+
return { message: 'No supported sources requested, skip topic processing.' };
|
|
29
|
+
}
|
|
55
30
|
|
|
56
|
-
const
|
|
57
|
-
params.topicIds && params.topicIds.length > 0
|
|
58
|
-
? await context.run(
|
|
59
|
-
`memory:user-memory:extract:users:${userId}:filter-topic-ids`,
|
|
60
|
-
async () => {
|
|
61
|
-
const filtered = await executor.filterTopicIdsForUser(userId, params.topicIds);
|
|
62
|
-
return filtered.length > 0 ? filtered : undefined;
|
|
63
|
-
},
|
|
64
|
-
)
|
|
65
|
-
: undefined;
|
|
31
|
+
const executor = await MemoryExtractionExecutor.create();
|
|
66
32
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
cursor: topicCursor,
|
|
76
|
-
forceAll: params.forceAll,
|
|
77
|
-
forceTopics: params.forceTopics,
|
|
78
|
-
from: params.from,
|
|
79
|
-
to: params.to,
|
|
33
|
+
const scheduleNextPage = async (userId: string, cursorCreatedAt: Date, cursorId: string) => {
|
|
34
|
+
await MemoryExtractionWorkflowService.triggerProcessUserTopics(
|
|
35
|
+
{
|
|
36
|
+
...buildWorkflowPayloadInput({
|
|
37
|
+
...params,
|
|
38
|
+
topicCursor: {
|
|
39
|
+
createdAt: cursorCreatedAt.toISOString(),
|
|
40
|
+
id: cursorId,
|
|
80
41
|
userId,
|
|
81
42
|
},
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
43
|
+
topicIds: [],
|
|
44
|
+
userId,
|
|
45
|
+
userIds: [userId],
|
|
46
|
+
}),
|
|
47
|
+
},
|
|
48
|
+
{ extraHeaders: upstashWorkflowExtraHeaders },
|
|
49
|
+
);
|
|
50
|
+
};
|
|
85
51
|
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
52
|
+
for (const userId of params.userIds) {
|
|
53
|
+
const topicCursor =
|
|
54
|
+
params.topicCursor && params.topicCursor.userId === userId
|
|
55
|
+
? {
|
|
56
|
+
createdAt: new Date(params.topicCursor.createdAt),
|
|
57
|
+
id: params.topicCursor.id,
|
|
58
|
+
}
|
|
59
|
+
: undefined;
|
|
90
60
|
|
|
91
|
-
|
|
61
|
+
const topicsFromPayload =
|
|
62
|
+
params.topicIds && params.topicIds.length > 0
|
|
63
|
+
? await context.run(
|
|
64
|
+
`memory:user-memory:extract:users:${userId}:filter-topic-ids`,
|
|
65
|
+
async () => {
|
|
66
|
+
const filtered = await executor.filterTopicIdsForUser(userId, params.topicIds);
|
|
67
|
+
return filtered.length > 0 ? filtered : undefined;
|
|
68
|
+
},
|
|
69
|
+
)
|
|
70
|
+
: undefined;
|
|
92
71
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`memory:user-memory:extract:users:${userId}:process-topics-batch:${batchIndex}`,
|
|
72
|
+
const topicBatch = await context.run<{
|
|
73
|
+
cursor?: ListTopicsForMemoryExtractorCursor;
|
|
74
|
+
ids: string[];
|
|
75
|
+
}>(
|
|
76
|
+
`memory:user-memory:extract:users:${userId}:list-topics:${topicCursor?.id || 'root'}`,
|
|
99
77
|
() =>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
78
|
+
topicsFromPayload && topicsFromPayload.length > 0
|
|
79
|
+
? Promise.resolve({ ids: topicsFromPayload })
|
|
80
|
+
: executor.getTopicsForUser(
|
|
81
|
+
{
|
|
82
|
+
cursor: topicCursor,
|
|
83
|
+
forceAll: params.forceAll,
|
|
84
|
+
forceTopics: params.forceTopics,
|
|
85
|
+
from: params.from,
|
|
86
|
+
to: params.to,
|
|
87
|
+
userId,
|
|
88
|
+
},
|
|
89
|
+
TOPIC_PAGE_SIZE,
|
|
90
|
+
),
|
|
107
91
|
);
|
|
108
|
-
});
|
|
109
92
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// NOTICE: Upstash Workflow only supports serializable data into plain JSON,
|
|
115
|
-
// this causes the Date object to be converted into string when passed as parameter from
|
|
116
|
-
// context to child workflow. So we need to convert it back to Date object here.
|
|
117
|
-
const createdAt = new Date(cursor.createdAt);
|
|
118
|
-
if (Number.isNaN(createdAt.getTime())) {
|
|
119
|
-
throw new Error('Invalid cursor date when scheduling next topic page');
|
|
120
|
-
}
|
|
93
|
+
const ids = topicBatch.ids;
|
|
94
|
+
if (!ids.length) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
121
97
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
)
|
|
98
|
+
const cursor = 'cursor' in topicBatch ? topicBatch.cursor : undefined;
|
|
99
|
+
|
|
100
|
+
await forEachBatchSequential(ids, TOPIC_BATCH_SIZE, async (topicIds, batchIndex) => {
|
|
101
|
+
// NOTICE: We trigger via QStash instead of context.invoke because invoke only swaps the last path
|
|
102
|
+
// segment with the workflowId. If we invoked directly from /process-user-topics, child workflow
|
|
103
|
+
// URLs would inherit that base and lose the desired /process-topics/workflows prefix.
|
|
104
|
+
await context.run(
|
|
105
|
+
`memory:user-memory:extract:users:${userId}:process-topics-batch:${batchIndex}`,
|
|
106
|
+
() =>
|
|
107
|
+
MemoryExtractionWorkflowService.triggerProcessTopics(
|
|
108
|
+
{
|
|
109
|
+
...buildWorkflowPayloadInput(params),
|
|
110
|
+
topicCursor: undefined,
|
|
111
|
+
topicIds,
|
|
112
|
+
userId,
|
|
113
|
+
userIds: [userId],
|
|
114
|
+
},
|
|
115
|
+
{ extraHeaders: upstashWorkflowExtraHeaders },
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (!topicsFromPayload && cursor) {
|
|
121
|
+
await context.run(
|
|
122
|
+
`memory:user-memory:extract:users:${userId}:topics:${cursor.id}:schedule-next-batch`,
|
|
123
|
+
() => {
|
|
124
|
+
// NOTICE: Upstash Workflow only supports serializable data into plain JSON,
|
|
125
|
+
// this causes the Date object to be converted into string when passed as parameter from
|
|
126
|
+
// context to child workflow. So we need to convert it back to Date object here.
|
|
127
|
+
const createdAt = new Date(cursor.createdAt);
|
|
128
|
+
if (Number.isNaN(createdAt.getTime())) {
|
|
129
|
+
throw new Error('Invalid cursor date when scheduling next topic page');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
scheduleNextPage(userId, createdAt, cursor.id);
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
}
|
|
125
136
|
}
|
|
126
|
-
}
|
|
127
137
|
|
|
128
|
-
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
})
|
|
138
|
+
return { processedUsers: params.userIds.length };
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
// NOTICE(@nekomeowww): Here as scenarios like Vercel Deployment Protection,
|
|
142
|
+
// intermediate context.run(...) won't offer customizable headers like context.trigger(...) / client.trigger(...)
|
|
143
|
+
// for passing additional headers, we have to provide a custom QStash client with the required headers here.
|
|
144
|
+
//
|
|
145
|
+
// Refer to the doc for more details:
|
|
146
|
+
// https://upstash.com/docs/workflow/troubleshooting/vercel#step-2-pass-header-when-triggering
|
|
147
|
+
qstashClient: new Client({
|
|
148
|
+
headers: {
|
|
149
|
+
...upstashWorkflowExtraHeaders,
|
|
150
|
+
},
|
|
151
|
+
token: process.env.QSTASH_TOKEN!,
|
|
152
|
+
}),
|
|
153
|
+
},
|
|
154
|
+
);
|