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.

🔨 chore(userMemories): fetch user persona (#12206)

Nekobbc1dfcc23e7
diff --git a/src/server/routers/lambda/userMemory.ts b/src/server/routers/lambda/userMemory.ts
index be230ed7817..c54ca668c3e 100644
--- a/src/server/routers/lambda/userMemory.ts
+++ b/src/server/routers/lambda/userMemory.ts
@@ -21,9 +21,10 @@ import {
UserMemoryIdentityModel,
UserMemoryPreferenceModel,
} from '@/database/models/userMemory/index';
+import { UserPersonaModel } from '@/database/models/userMemory/persona';
+import { appEnv } from '@/envs/app';
import { authedProcedure, router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
-import { appEnv } from '@/envs/app';
import { parseMemoryExtractionConfig } from '@/server/globalConfig/parseMemoryExtractionConfig';
import {
MemoryExtractionWorkflowService,
@@ -41,6 +42,7 @@ const userMemoryProcedure = authedProcedure.use(serverDatabase).use(async (opts)
contextModel: new UserMemoryContextModel(ctx.serverDB, ctx.userId),
experienceModel: new UserMemoryExperienceModel(ctx.serverDB, ctx.userId),
identityModel: new UserMemoryIdentityModel(ctx.serverDB, ctx.userId),
+ personaModel: new UserPersonaModel(ctx.serverDB, ctx.userId),
preferenceModel: new UserMemoryPreferenceModel(ctx.serverDB, ctx.userId),
topicModel: new TopicModel(ctx.serverDB, ctx.userId),
userMemoryModel: new UserMemoryModel(ctx.serverDB, ctx.userId),
@@ -77,7 +79,6 @@ export const userMemoryRouter = router({
});
}),
-
// ============ Activity CRUD ============
deleteActivity: userMemoryProcedure
.input(z.object({ id: z.string() }))
@@ -99,14 +100,12 @@ export const userMemoryRouter = router({
return ctx.experienceModel.delete(input.id);
}),
-
deleteIdentity: userMemoryProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
return ctx.userMemoryModel.removeIdentityEntry(input.id);
}),
-
// ============ Preference CRUD ============
deletePreference: userMemoryProcedure
.input(z.object({ id: z.string() }))
@@ -114,7 +113,6 @@ export const userMemoryRouter = router({
return ctx.preferenceModel.delete(input.id);
}),
-
getActivities: userMemoryProcedure.query(async ({ ctx }) => {
return ctx.userMemoryModel.searchActivities({});
}),
@@ -145,16 +143,24 @@ export const userMemoryRouter = router({
return {
error: task.error,
id: task.id,
- metadata: initUserMemoryExtractionMetadata(task.metadata as UserMemoryExtractionMetadata | undefined),
+ metadata: initUserMemoryExtractionMetadata(
+ task.metadata as UserMemoryExtractionMetadata | undefined,
+ ),
status: task.status as AsyncTaskStatus,
};
}),
// ============ Persona ============
-getPersona: userMemoryProcedure.query(async () => {
- return { content: '', summary: '' };
- }),
+ getPersona: userMemoryProcedure.query(async ({ ctx }) => {
+ const latest = await ctx.personaModel.getLatestPersonaDocument();
+ if (!latest) return null;
+
+ return {
+ content: latest.persona ?? '',
+ summary: latest.tagline ?? '',
+ };
+ }),
getPreferences: userMemoryProcedure.query(async ({ ctx }) => {
return ctx.userMemoryModel.searchPreferences({});
@@ -199,8 +205,7 @@ getPersona: userMemoryProcedure.query(async () => {
source: 'chat_topic',
});
- const initialStatus =
- totalTopics === 0 ? AsyncTaskStatus.Success : AsyncTaskStatus.Pending;
+ const initialStatus = totalTopics === 0 ? AsyncTaskStatus.Success : AsyncTaskStatus.Pending;
const taskId = await ctx.asyncTaskModel.create({
metadata,
status: initialStatus,
diff --git a/src/store/userMemory/slices/home/action.ts b/src/store/userMemory/slices/home/action.ts
index 35db278563c..f1bd8e34e86 100644
--- a/src/store/userMemory/slices/home/action.ts
+++ b/src/store/userMemory/slices/home/action.ts
@@ -13,7 +13,7 @@ const FETCH_PERSONA_KEY = 'useFetchPersona';
const n = (namespace: string) => namespace;
export interface HomeAction {
- useFetchPersona: () => SWRResponse<PersonaData>;
+ useFetchPersona: () => SWRResponse<PersonaData | null>;
useFetchTags: () => SWRResponse<QueryIdentityRolesResult>;
}
@@ -25,10 +25,10 @@ export const createHomeSlice: StateCreator<
> = (set) => ({
useFetchPersona: () =>
useClientDataSWR(FETCH_PERSONA_KEY, () => userMemoryService.getPersona(), {
- onSuccess: (data: PersonaData | undefined) => {
+ onSuccess: (data: PersonaData | null | undefined) => {
set(
{
- persona: data,
+ persona: data ?? undefined,
personaInit: true,
},
false,