The main agent is meant to know the user, but nothing could read or change the user model the foundation added. Onboarding pasted the user's answers into the first bot's instructions as a "User Context" block, and the user had no way to see or remove what their bots knew about them. This makes the user model a user-controlled resource: - db: a profile repository scoped by user. One atomic update writes the account name, profile fields, new facts (deduplicated by normalized text, capped per user), fact edits including per-space visibility, and confirmations. Health and values facts wait for confirmation unless the user wrote them. Forgetting hard-deletes facts by id, category or all, clears the summary that may restate them and marks earlier chats as consolidated so they cannot teach them again; forgetting everything also clears the profile fields. Writes take a per-user advisory lock. - api/contracts: profile.get, profile.update and profile.forget. Unknown fact ids are a 404 on update and ignored on forget; spaces outside the user's memberships are rejected. - adapters: an update_user_profile tool for the main bot only, hidden from specialists and shared messaging channels and refused if called anyway. Facts carry source "conversation" and the bot, thread and message they came from. The scripted runtime records "learn about me: [category:] statement" for tests. - web: onboarding writes the name, time zone (browser default, now prefilled and editable), locale, chosen language, communication style and the hobby and work-style answers to the profile instead of overwriting the bot's instructions, and the harvested memory section no longer repeats them. A new "About you" settings section lists profile fields and facts by category with their origin, confirms pending facts, and forgets one fact or everything. Tests cover the contracts, the repository (unit and PostgreSQL), tool selection and refusal, the scripted trigger, the onboarding mapping and view helpers, an API suite that runs the tool through the executor with a real main bot, authorization for the new procedures, and an E2E spec. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MRtGG9ARfqqhma3t85Ro2u
83 lines
2.7 KiB
TypeScript
83 lines
2.7 KiB
TypeScript
import {
|
|
LanguageTagSchema,
|
|
TimeZoneSchema,
|
|
USER_COMMUNICATION_STYLE_MAX_LENGTH,
|
|
UserFactCategorySchema,
|
|
UserFactValueSchema,
|
|
} from "@rakazo/contracts";
|
|
import type { PrismaClient } from "@rakazo/db";
|
|
import { InvalidUserFactError, updateUserProfile } from "@rakazo/db";
|
|
import { z } from "zod";
|
|
|
|
const UpdateUserProfileArgs = z.object({
|
|
facts: z
|
|
.array(z.object({ category: UserFactCategorySchema, value: UserFactValueSchema }))
|
|
.max(20)
|
|
.optional(),
|
|
time_zone: TimeZoneSchema.optional(),
|
|
language: LanguageTagSchema.optional(),
|
|
communication_style: z.string().trim().min(1).max(USER_COMMUNICATION_STYLE_MAX_LENGTH).optional(),
|
|
});
|
|
|
|
export interface UserProfileToolContext {
|
|
userId: string;
|
|
bot: { id: string; kind: string };
|
|
threadId: string;
|
|
/** The message the run answers, so a fact can be traced to where it was learned. */
|
|
sourceMessageId: string | null;
|
|
}
|
|
|
|
/** Records what the main bot learned about the user, attributed to the conversation. */
|
|
export async function updateUserProfileFromTool(
|
|
prisma: PrismaClient,
|
|
context: UserProfileToolContext,
|
|
rawArgs: Record<string, unknown>,
|
|
) {
|
|
if (context.bot.kind !== "main") {
|
|
return { error: "Only the user's main bot can update the user profile." };
|
|
}
|
|
const parsed = UpdateUserProfileArgs.safeParse(rawArgs);
|
|
if (!parsed.success) {
|
|
const issues = parsed.error.issues
|
|
.map((issue) => `${issue.path.join(".") || "arguments"}: ${issue.message}`)
|
|
.join("; ");
|
|
return { error: `Invalid profile update: ${issues}` };
|
|
}
|
|
const args = parsed.data;
|
|
const facts = args.facts ?? [];
|
|
if (
|
|
facts.length === 0 &&
|
|
args.time_zone === undefined &&
|
|
args.language === undefined &&
|
|
args.communication_style === undefined
|
|
) {
|
|
return { error: "Nothing to save. Pass facts or a profile field." };
|
|
}
|
|
try {
|
|
const added = await updateUserProfile(prisma, context.userId, {
|
|
timeZone: args.time_zone,
|
|
language: args.language,
|
|
communicationStyle: args.communication_style,
|
|
addFacts: facts.map((fact) => ({
|
|
...fact,
|
|
source: "conversation" as const,
|
|
sourceBotId: context.bot.id,
|
|
sourceThreadId: context.threadId,
|
|
sourceMessageId: context.sourceMessageId,
|
|
})),
|
|
});
|
|
const pending = added.filter((fact) => fact.status === "pending").length;
|
|
return pending > 0
|
|
? {
|
|
ok: true,
|
|
saved: added.length - pending,
|
|
pendingConfirmation: pending,
|
|
note: "Health and values facts stay hidden from bots until the user confirms them in their settings.",
|
|
}
|
|
: { ok: true, saved: added.length };
|
|
} catch (error) {
|
|
if (error instanceof InvalidUserFactError) return { error: error.message };
|
|
throw error;
|
|
}
|
|
}
|