Reusable, workspace-shared agent roles for the built-in AI chat. A role is a named persona (system-prompt instructions) + optional model override; a chat is bound to a role at creation and applies it every turn. Backend: - migration 20260620T120000: ai_agent_roles table + ai_chats.role_id (FK ON DELETE SET NULL); hand-merged types into db.d.ts/entity.types.ts (db.d.ts is hand-curated here, full codegen would clobber it). - core/ai-chat/roles: CRUD module. list = any workspace member; create/ update/delete = admin (Manage Settings ability, like ai-settings/mcp). All repo queries scoped by workspace_id; soft-delete (deleted_at). - buildSystemPrompt gains roleInstructions: role REPLACES the persona base (admin prompt / DEFAULT_PROMPT) but SAFETY_FRAMEWORK + context are always still appended. - stream(): role resolved from ai_chats.role_id for existing chats (never the request body -> no per-turn role swap); body.roleId only on creation. Disabled (enabled=false) and soft-deleted roles fall back to universal. - getChatModel(workspaceId, override): role model_config can swap model id / driver; a driver without configured creds throws 503 with a clear message naming the driver+role, resolved BEFORE response hijack. Client: - new-chat role picker (enabled roles only, default Universal assistant), roleId sent only on the first message; role badge (emoji+name) in the chat header and conversation list; admin Agent-roles management section in Settings -> AI (add/edit/delete, MCP-form pattern). Tests: ai-chat.prompt.spec (role layering + safety always present, incl. jailbreak); ai.service.spec (override on unconfigured driver -> 503). Implements docs/ai-agent-roles-plan.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
31 lines
1.5 KiB
TypeScript
31 lines
1.5 KiB
TypeScript
import { atom } from "jotai";
|
|
|
|
/**
|
|
* The currently selected chat id. `null` means a fresh (not-yet-created) chat:
|
|
* the server creates the chat row on the first streamed message and echoes its
|
|
* id, which the panel then adopts.
|
|
*/
|
|
// Note: declare via a cast default rather than `atom<string | null>(null)`,
|
|
// which mis-resolves the jotai useAtom overload to the read-only signature
|
|
// under this TS/jotai version (the setter would type as `never`).
|
|
export const activeAiChatIdAtom = atom(null as string | null);
|
|
|
|
// Whether the floating AI chat window is open. Non-persistent (resets per session).
|
|
export const aiChatWindowOpenAtom = atom<boolean>(false);
|
|
|
|
/**
|
|
* The agent role selected for the NEXT new chat. `null` = "Universal assistant"
|
|
* (no role). Consulted ONLY when creating a chat (its first message): the server
|
|
* persists it to ai_chats.role_id and the role is immutable afterwards. Reset to
|
|
* null when starting a new chat. It does NOT affect already-created chats.
|
|
*/
|
|
// Cast default for the same jotai overload reason as activeAiChatIdAtom above.
|
|
export const selectedAiRoleIdAtom = atom(null as string | null);
|
|
|
|
// The AI chat composer draft (text typed but not yet sent). Held here — OUTSIDE
|
|
// ChatThread — so it survives the thread remount that happens when a brand-new
|
|
// chat adopts its freshly created id after the first turn finishes. If it lived
|
|
// in ChatInput's local state, that remount would wipe text the user typed while
|
|
// the agent was still streaming. Reset on deliberate chat switches.
|
|
export const aiChatDraftAtom = atom<string>("");
|