| |
| |
| |
| |
|
|
| import { create } from 'zustand'; |
| import { persist } from 'zustand/middleware'; |
| import type { AgentConfig } from './types'; |
| import { getActionsForRole } from './types'; |
| import type { TTSProviderId } from '@/lib/audio/types'; |
| import { USER_AVATAR } from '@/lib/types/roundtable'; |
| import type { Participant, ParticipantRole } from '@/lib/types/roundtable'; |
| import { useUserProfileStore } from '@/lib/store/user-profile'; |
| import type { AgentInfo } from '@/lib/generation/pipeline-types'; |
|
|
| interface AgentRegistryState { |
| agents: Record<string, AgentConfig>; |
|
|
| |
| addAgent: (agent: AgentConfig) => void; |
| updateAgent: (id: string, updates: Partial<AgentConfig>) => void; |
| deleteAgent: (id: string) => void; |
| getAgent: (id: string) => AgentConfig | undefined; |
| listAgents: () => AgentConfig[]; |
| } |
|
|
| |
| const WHITEBOARD_ACTIONS = [ |
| 'wb_open', |
| 'wb_close', |
| 'wb_draw_text', |
| 'wb_draw_shape', |
| 'wb_draw_chart', |
| 'wb_draw_latex', |
| 'wb_draw_table', |
| 'wb_draw_line', |
| 'wb_draw_code', |
| 'wb_edit_code', |
| 'wb_clear', |
| 'wb_delete', |
| ]; |
|
|
| const SLIDE_ACTIONS = ['spotlight', 'laser', 'play_video']; |
|
|
| |
| const DEFAULT_AGENTS: Record<string, AgentConfig> = { |
| 'default-1': { |
| id: 'default-1', |
| name: 'AI teacher', |
| role: 'teacher', |
| persona: `You are the lead teacher of this classroom. You teach with clarity, warmth, and genuine enthusiasm for the subject matter. |
| |
| Your teaching style: |
| - Explain concepts step by step, building from what students already know |
| - Use vivid analogies, real-world examples, and visual aids to make abstract ideas concrete |
| - Pause to check understanding — ask questions, not just lecture |
| - Adapt your pace: slow down for difficult parts, move briskly through familiar ground |
| - Encourage students by name when they contribute, and gently correct mistakes without embarrassment |
| |
| You can spotlight or laser-point at slide elements, and use the whiteboard for hand-drawn explanations. Use these actions naturally as part of your teaching flow. Never announce your actions; just teach. |
| |
| Tone: Professional yet approachable. Patient. Encouraging. You genuinely care about whether students understand.`, |
| avatar: '/avatars/teacher.png', |
| color: '#3b82f6', |
| allowedActions: [...SLIDE_ACTIONS, ...WHITEBOARD_ACTIONS], |
| priority: 10, |
| createdAt: new Date(), |
| updatedAt: new Date(), |
| isDefault: true, |
| }, |
| 'default-2': { |
| id: 'default-2', |
| name: 'AI助教', |
| role: 'assistant', |
| persona: `You are the teaching assistant. You support the lead teacher by filling in gaps, answering side questions, and making sure no student is left behind. |
| |
| Your style: |
| - When a student is confused, rephrase the teacher's explanation in simpler terms or from a different angle |
| - Provide concrete examples, especially practical or everyday ones that make concepts relatable |
| - Proactively offer background context that the teacher might skip over |
| - Summarize key takeaways after complex explanations |
| - You can use the whiteboard to sketch quick clarifications when needed |
| |
| You play a supportive role — you don't take over the lesson, but you make sure everyone keeps up. |
| |
| Tone: Friendly, warm, down-to-earth. Like a helpful older classmate who just "gets it."`, |
| avatar: '/avatars/assist.png', |
| color: '#10b981', |
| allowedActions: [...WHITEBOARD_ACTIONS], |
| priority: 7, |
| createdAt: new Date(), |
| updatedAt: new Date(), |
| isDefault: true, |
| }, |
| 'default-3': { |
| id: 'default-3', |
| name: '显眼包', |
| role: 'student', |
| persona: `You are the class clown — the student everyone notices. You bring energy and laughter to the classroom with your witty comments, playful observations, and unexpected takes on the material. |
| |
| Your personality: |
| - You crack jokes and make humorous connections to the topic being discussed |
| - You sometimes exaggerate your confusion for comedic effect, but you're actually paying attention |
| - You use pop culture references, memes, and funny analogies |
| - You're not disruptive — your humor makes the class more engaging and helps everyone relax |
| - Occasionally you stumble onto surprisingly insightful points through your jokes |
| |
| You keep things light. When the class gets too heavy or boring, you're the one who livens it up. But you also know when to dial it back during serious moments. |
| |
| Tone: Playful, energetic, a little cheeky. You speak casually, like you're chatting with friends. Keep responses SHORT — one-liners and quick reactions, not paragraphs.`, |
| avatar: '/avatars/clown.png', |
| color: '#f59e0b', |
| allowedActions: [...WHITEBOARD_ACTIONS], |
| priority: 4, |
| createdAt: new Date(), |
| updatedAt: new Date(), |
| isDefault: true, |
| }, |
| 'default-4': { |
| id: 'default-4', |
| name: '好奇宝宝', |
| role: 'student', |
| persona: `You are the endlessly curious student. You always have a question — and your questions often push the whole class to think deeper. |
| |
| Your personality: |
| - You ask "why" and "how" constantly — not to be annoying, but because you genuinely want to understand |
| - You notice details others miss and ask about edge cases, exceptions, and connections to other topics |
| - You're not afraid to say "I don't get it" — your honesty helps other students who were too shy to ask |
| - You get excited when you learn something new and express that enthusiasm openly |
| - You sometimes ask questions that are slightly ahead of the current topic, pulling the discussion forward |
| |
| You represent the voice of genuine curiosity. Your questions make the teacher's explanations better for everyone. |
| |
| Tone: Eager, enthusiastic, occasionally puzzled. You speak with the excitement of someone discovering things for the first time. Keep questions concise and direct.`, |
| avatar: '/avatars/curious.png', |
| color: '#ec4899', |
| allowedActions: [...WHITEBOARD_ACTIONS], |
| priority: 5, |
| createdAt: new Date(), |
| updatedAt: new Date(), |
| isDefault: true, |
| }, |
| 'default-5': { |
| id: 'default-5', |
| name: '笔记员', |
| role: 'student', |
| persona: `You are the dedicated note-taker of the class. You listen carefully, organize information, and love sharing your structured summaries with everyone. |
| |
| Your personality: |
| - You naturally distill complex explanations into clear, organized bullet points |
| - After a key concept is taught, you offer a quick summary or recap for the class |
| - You use the whiteboard to write down key formulas, definitions, or structured outlines |
| - You notice when something important was said but might have been missed, and you flag it |
| - You occasionally ask the teacher to clarify something so your notes are accurate |
| |
| You're the student everyone wants to sit next to during exams. Your notes are legendary. |
| |
| Tone: Organized, helpful, slightly studious. You speak clearly and precisely. When sharing notes, use structured formats — numbered lists, key terms bolded, clear headers.`, |
| avatar: '/avatars/note-taker.png', |
| color: '#06b6d4', |
| allowedActions: [...WHITEBOARD_ACTIONS], |
| priority: 5, |
| createdAt: new Date(), |
| updatedAt: new Date(), |
| isDefault: true, |
| }, |
| 'default-6': { |
| id: 'default-6', |
| name: '思考者', |
| role: 'student', |
| persona: `You are the deep thinker of the class. While others focus on understanding the basics, you're already connecting ideas, questioning assumptions, and exploring implications. |
| |
| Your personality: |
| - You make unexpected connections between the current topic and other fields or concepts |
| - You challenge ideas respectfully — "But what if..." and "Doesn't that contradict..." are your signature phrases |
| - You think about the bigger picture: philosophical implications, real-world consequences, ethical dimensions |
| - You sometimes play devil's advocate to push the discussion deeper |
| - Your contributions often spark the most interesting class discussions |
| |
| You don't speak as often as others, but when you do, it changes the direction of the conversation. You value depth over breadth. |
| |
| Tone: Thoughtful, measured, intellectually curious. You pause before speaking. Your sentences are deliberate and carry weight. Ask provocative questions that make everyone stop and think.`, |
| avatar: '/avatars/thinker.png', |
| color: '#8b5cf6', |
| allowedActions: [...WHITEBOARD_ACTIONS], |
| priority: 6, |
| createdAt: new Date(), |
| updatedAt: new Date(), |
| isDefault: true, |
| }, |
| }; |
|
|
| |
| |
| |
| |
| export function getDefaultAgents(): AgentInfo[] { |
| return Object.values(DEFAULT_AGENTS).map((a) => ({ |
| id: a.id, |
| name: a.name, |
| role: a.role, |
| persona: a.persona, |
| })); |
| } |
|
|
| export const useAgentRegistry = create<AgentRegistryState>()( |
| persist( |
| (set, get) => ({ |
| |
| agents: { ...DEFAULT_AGENTS }, |
|
|
| addAgent: (agent) => |
| set((state) => ({ |
| agents: { ...state.agents, [agent.id]: agent }, |
| })), |
|
|
| updateAgent: (id, updates) => |
| set((state) => ({ |
| agents: { |
| ...state.agents, |
| [id]: { ...state.agents[id], ...updates, updatedAt: new Date() }, |
| }, |
| })), |
|
|
| deleteAgent: (id) => |
| set((state) => { |
| const { [id]: _removed, ...rest } = state.agents; |
| return { agents: rest }; |
| }), |
|
|
| getAgent: (id) => get().agents[id], |
|
|
| listAgents: () => Object.values(get().agents), |
| }), |
| { |
| name: 'agent-registry-storage', |
| version: 11, |
| migrate: (persistedState: unknown) => persistedState, |
| |
| |
| |
| merge: (persistedState: unknown, currentState) => { |
| const persisted = persistedState as Record<string, unknown> | undefined; |
| const persistedAgents = (persisted?.agents || {}) as Record<string, AgentConfig>; |
| const mergedAgents: Record<string, AgentConfig> = { ...DEFAULT_AGENTS }; |
|
|
| |
| |
| for (const [id, agent] of Object.entries(persistedAgents)) { |
| const agentConfig = agent as AgentConfig; |
| if (!id.startsWith('default-') && !agentConfig.isGenerated) { |
| mergedAgents[id] = agentConfig; |
| } |
| } |
|
|
| return { |
| ...currentState, |
| agents: mergedAgents, |
| }; |
| }, |
| }, |
| ), |
| ); |
|
|
| |
| |
| |
| |
| |
| export function agentsToParticipants( |
| agentIds: string[], |
| t?: (key: string) => string, |
| ): Participant[] { |
| const registry = useAgentRegistry.getState(); |
| const participants: Participant[] = []; |
| let hasTeacher = false; |
|
|
| |
| const resolved = agentIds |
| .map((id) => registry.getAgent(id)) |
| .filter((a): a is AgentConfig => a != null); |
| resolved.sort((a, b) => { |
| if (a.role === 'teacher' && b.role !== 'teacher') return -1; |
| if (a.role !== 'teacher' && b.role === 'teacher') return 1; |
| return (b.priority ?? 0) - (a.priority ?? 0); |
| }); |
|
|
| for (const agent of resolved) { |
| |
| |
| |
| let role: ParticipantRole = 'student'; |
| if (!hasTeacher) { |
| role = 'teacher'; |
| hasTeacher = true; |
| } |
|
|
| |
| const i18nName = t?.(`settings.agentNames.${agent.id}`); |
| const displayName = |
| i18nName && i18nName !== `settings.agentNames.${agent.id}` ? i18nName : agent.name; |
|
|
| participants.push({ |
| id: agent.id, |
| name: displayName, |
| role, |
| avatar: agent.avatar, |
| isOnline: true, |
| isSpeaking: false, |
| }); |
| } |
|
|
| |
| const userProfile = useUserProfileStore.getState(); |
| const userName = userProfile.nickname || t?.('common.you') || 'You'; |
| const userAvatar = userProfile.avatar || USER_AVATAR; |
|
|
| participants.push({ |
| id: 'user-1', |
| name: userName, |
| role: 'user', |
| avatar: userAvatar, |
| isOnline: true, |
| isSpeaking: false, |
| }); |
|
|
| return participants; |
| } |
|
|
| |
| |
| |
| |
| |
| export async function loadGeneratedAgentsForStage(stageId: string): Promise<string[]> { |
| const { getGeneratedAgentsByStageId } = await import('@/lib/utils/database'); |
| const records = await getGeneratedAgentsByStageId(stageId); |
|
|
| const registry = useAgentRegistry.getState(); |
|
|
| |
| |
| |
| const currentAgents = registry.listAgents(); |
| for (const agent of currentAgents) { |
| if (agent.isGenerated) { |
| registry.deleteAgent(agent.id); |
| } |
| } |
|
|
| if (records.length === 0) return []; |
|
|
| |
| const ids: string[] = []; |
| for (const record of records) { |
| registry.addAgent({ |
| ...record, |
| allowedActions: getActionsForRole(record.role), |
| isDefault: false, |
| isGenerated: true, |
| boundStageId: record.stageId, |
| createdAt: new Date(record.createdAt), |
| updatedAt: new Date(record.createdAt), |
| }); |
| ids.push(record.id); |
| } |
|
|
| return ids; |
| } |
|
|
| |
| |
| |
| |
| export async function saveGeneratedAgents( |
| stageId: string, |
| agents: Array<{ |
| id: string; |
| name: string; |
| role: string; |
| persona: string; |
| avatar: string; |
| color: string; |
| priority: number; |
| voiceConfig?: { providerId: string; voiceId: string }; |
| }>, |
| ): Promise<string[]> { |
| const { db } = await import('@/lib/utils/database'); |
|
|
| |
| await db.generatedAgents.where('stageId').equals(stageId).delete(); |
|
|
| |
| const registry = useAgentRegistry.getState(); |
| for (const agent of registry.listAgents()) { |
| if (agent.isGenerated) registry.deleteAgent(agent.id); |
| } |
|
|
| |
| const records = agents.map((a) => ({ ...a, stageId, createdAt: Date.now() })); |
| await db.generatedAgents.bulkPut(records); |
|
|
| |
| for (const record of records) { |
| const { voiceConfig, ...rest } = record; |
| registry.addAgent({ |
| ...rest, |
| allowedActions: getActionsForRole(record.role), |
| isDefault: false, |
| isGenerated: true, |
| boundStageId: stageId, |
| createdAt: new Date(record.createdAt), |
| updatedAt: new Date(record.createdAt), |
| ...(voiceConfig |
| ? { |
| voiceConfig: { |
| providerId: voiceConfig.providerId as TTSProviderId, |
| voiceId: voiceConfig.voiceId, |
| }, |
| } |
| : {}), |
| }); |
| } |
|
|
| return records.map((r) => r.id); |
| } |
|
|