feat(ai-chat): agent roles (admin-defined persona + optional model)

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>
This commit is contained in:
claude code agent 227
2026-06-20 06:30:06 +03:00
parent c8af637654
commit 30c3189220
30 changed files with 1674 additions and 40 deletions

View File

@@ -0,0 +1,70 @@
import { type Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
// Reusable, workspace-scoped agent roles (admin-owned). A role REPLACES the
// persona layer of the system prompt (instructions) and may optionally
// override the chat model. The non-removable SAFETY_FRAMEWORK is always still
// appended downstream — a role only shapes the persona, never the safety rules.
await db.schema
.createTable('ai_agent_roles')
.ifNotExists()
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
)
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
// Who created the role (audit). The role is shared and outlives its author,
// so SET NULL on user deletion (unlike ai_chats.creator_id which is NOT NULL).
.addColumn('creator_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
// Display name, e.g. 'Proofreader'.
.addColumn('name', 'varchar', (col) => col.notNull())
// Optional presentation emoji for the role badge.
.addColumn('emoji', 'varchar', (col) => col)
// Optional short description shown in the management UI.
.addColumn('description', 'text', (col) => col)
// The persona fragment injected into the system prompt (replaces the admin
// persona / DEFAULT_PROMPT). Required.
.addColumn('instructions', 'text', (col) => col.notNull())
// Optional model override: { chatModel } or { driver, chatModel }. NULL =>
// use the workspace default model. Driver creds come from the matching
// provider in ai_provider_credentials (no per-role creds).
.addColumn('model_config', 'jsonb', (col) => col)
.addColumn('enabled', 'boolean', (col) => col.notNull().defaultTo(true))
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
// Soft delete (consistent with ai_chats): the role disappears from the
// picker but lookups can still resolve it for already-bound chats.
.addColumn('deleted_at', 'timestamptz', (col) => col)
.execute();
// Scoped lookups (listByWorkspace) hit workspace_id first.
await db.schema
.createIndex('idx_ai_agent_roles_workspace_id')
.ifNotExists()
.on('ai_agent_roles')
.column('workspace_id')
.execute();
// Bind a chat to a role. ON DELETE SET NULL: a hard-deleted role degrades the
// chat to the universal assistant instead of breaking it. The role is read
// from this column on every turn — the client only sends roleId on chat
// creation (first message).
await db.schema
.alterTable('ai_chats')
.addColumn('role_id', 'uuid', (col) =>
col.references('ai_agent_roles.id').onDelete('set null'),
)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.alterTable('ai_chats').dropColumn('role_id').execute();
await db.schema.dropTable('ai_agent_roles').execute();
}