Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(userMemories): workflow id build issue (#10998)

Neko0b110b601240
diff --git a/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/[workflowId]/route.ts b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/[workflowId]/route.ts
new file mode 100644
index 00000000000..fcf38351a38
--- /dev/null
+++ b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/[workflowId]/route.ts
@@ -0,0 +1,10 @@
+import { serveMany } from '@upstash/workflow/nextjs';
+
+import { TOPIC_WORKFLOW_NAMES } from '@/server/services/memory/userMemory/extract';
+import { cepWorkflow, identityWorkflow, orchestratorWorkflow } from '../workflows';
+
+export const { POST } = serveMany({
+ [TOPIC_WORKFLOW_NAMES.orchestrator]: orchestratorWorkflow,
+ [TOPIC_WORKFLOW_NAMES.cep]: cepWorkflow,
+ [TOPIC_WORKFLOW_NAMES.identity]: identityWorkflow,
+});
diff --git a/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/batch/route.ts b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/batch/route.ts
new file mode 100644
index 00000000000..95277086c34
--- /dev/null
+++ b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/batch/route.ts
@@ -0,0 +1,22 @@
+import { serve } from '@upstash/workflow/nextjs';
+
+import {
+ type MemoryExtractionPayloadInput,
+ normalizeMemoryExtractionPayload,
+} from '@/server/services/memory/userMemory/extract';
+
+import { orchestratorWorkflow } from '../workflows';
+
+export const { POST } = serve<MemoryExtractionPayloadInput>(async (context) => {
+ const payload = normalizeMemoryExtractionPayload(context.requestPayload || {});
+
+ await context.invoke('memory:user-memory:extract:topics:batch', {
+ body: context.requestPayload,
+ workflow: orchestratorWorkflow,
+ });
+
+ return {
+ processedTopics: payload.topicIds.length,
+ processedUsers: payload.userIds.length,
+ };
+});
diff --git a/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/route.ts b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/workflows.ts
similarity index 94%
rename from src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/route.ts
rename to src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/workflows.ts
index 4ab7efafd3a..790fea71908 100644
--- a/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/route.ts
+++ b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/workflows.ts
@@ -1,5 +1,5 @@
import { LayersEnum, MemorySourceType } from '@lobechat/types';
-import { createWorkflow, serveMany } from '@upstash/workflow/nextjs';
+import { createWorkflow } from '@upstash/workflow/nextjs';
import {
MemoryExtractionExecutor,
@@ -176,8 +176,7 @@ export const orchestratorWorkflow = createWorkflow<
};
});
-export const { POST } = serveMany({
- [TOPIC_WORKFLOW_NAMES.orchestrator]: orchestratorWorkflow,
- [TOPIC_WORKFLOW_NAMES.cep]: cepWorkflow,
- [TOPIC_WORKFLOW_NAMES.identity]: identityWorkflow,
-});
+// Explicitly set workflow ids to keep context.invoke working even before serveMany assigns them
+cepWorkflow.workflowId = TOPIC_WORKFLOW_NAMES.cep;
+identityWorkflow.workflowId = TOPIC_WORKFLOW_NAMES.identity;
+orchestratorWorkflow.workflowId = TOPIC_WORKFLOW_NAMES.orchestrator;
diff --git a/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-user-topics/route.ts b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-user-topics/route.ts
index 4d6479cbba0..0ab28844c72 100644
--- a/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-user-topics/route.ts
+++ b/src/app/(backend)/api/workflows/memory-user-memory/pipelines/chat-topic/process-user-topics/route.ts
@@ -10,7 +10,6 @@ import {
normalizeMemoryExtractionPayload,
} from '@/server/services/memory/userMemory/extract';
import { MemorySourceType } from '@lobechat/types';
-import { orchestratorWorkflow } from '../process-topics/route';
const TOPIC_PAGE_SIZE = 50;
const TOPIC_BATCH_SIZE = 10;
@@ -89,20 +88,21 @@ export const { POST } = serve<MemoryExtractionPayloadInput>(async (context) => {
const cursor = 'cursor' in topicBatch ? topicBatch.cursor : undefined;
const batches = chunk(ids, TOPIC_BATCH_SIZE);
+ // NOTICE: We trigger via QStash instead of context.invoke because invoke only swaps the last path
+ // segment with the workflowId. If we invoked directly from /process-user-topics, child workflow
+ // URLs would inherit that base and lose the desired /process-topics/workflows prefix.
await Promise.all(
batches.map((topicIds, index) =>
- context.invoke(
+ context.run(
`memory:user-memory:extract:users:${userId}:process-topics-batch:${index}`,
- {
- body: {
+ () =>
+ MemoryExtractionWorkflowService.triggerProcessTopics({
...buildWorkflowPayloadInput(params),
topicCursor: undefined,
topicIds,
userId,
userIds: [userId],
- },
- workflow: orchestratorWorkflow,
- }
+ }),
),
),
);
diff --git a/src/server/services/memory/userMemory/extract.ts b/src/server/services/memory/userMemory/extract.ts
index 82b0195e184..8d2d242199b 100644
--- a/src/server/services/memory/userMemory/extract.ts
+++ b/src/server/services/memory/userMemory/extract.ts
@@ -1762,15 +1762,16 @@ export class MemoryExtractionExecutor {
}
const WORKFLOW_PATHS = {
- topicBatch: '/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics',
+ topicBatch:
+ '/api/workflows/memory-user-memory/pipelines/chat-topic/process-topics/workflows/batch',
userTopics: '/api/workflows/memory-user-memory/pipelines/chat-topic/process-user-topics',
users: '/api/workflows/memory-user-memory/pipelines/chat-topic/process-users',
} as const;
export const TOPIC_WORKFLOW_NAMES = {
- cep: 'process-topics/extract-layers/cep',
- identity: 'process-topics/extract-layers/identity',
- orchestrator: 'process-topics/extract-layers/orchestrator',
+ cep: 'process-topics-extract-layers-cep',
+ identity: 'process-topics-extract-layers-identity',
+ orchestrator: 'process-topics-extract-layers-orchestrator',
} as const;
const getWorkflowUrl = (path: string, baseUrl: string) => {
@@ -1820,4 +1821,13 @@ export class MemoryExtractionWorkflowService {
const url = getWorkflowUrl(WORKFLOW_PATHS.userTopics, payload.baseUrl);
return this.getClient().trigger({ body: payload, url });
}
+
+ static triggerProcessTopics(payload: MemoryExtractionPayloadInput) {
+ if (!payload.baseUrl) {
+ throw new Error('Missing baseUrl for workflow trigger');
+ }
+
+ const url = getWorkflowUrl(WORKFLOW_PATHS.topicBatch, payload.baseUrl);
+ return this.getClient().trigger({ body: payload, url });
+ }
}