|
| 1 | +/** |
| 2 | + * BotConfigTool — clawtalk_bot_config: Read, update bot configuration, or browse voices. |
| 3 | + */ |
| 4 | + |
| 5 | +import { Type } from '@sinclair/typebox'; |
| 6 | +import type { ClawTalkClient } from '../lib/clawtalk-sdk/index.js'; |
| 7 | +import type { Voice } from '../lib/clawtalk-sdk/types.js'; |
| 8 | +import type { Logger } from '../types/plugin.js'; |
| 9 | +import { ToolError } from '../utils/errors.js'; |
| 10 | + |
| 11 | +// ── Schema ────────────────────────────────────────────────── |
| 12 | + |
| 13 | +export const BotConfigToolSchema = Type.Object({ |
| 14 | + action: Type.Union([Type.Literal('get'), Type.Literal('update'), Type.Literal('list_voices')]), |
| 15 | + agent_name: Type.Optional(Type.String({ description: 'Bot name (e.g. Daisy)' })), |
| 16 | + bot_role: Type.Optional(Type.String({ description: 'Bot role (e.g. live phone voice for Smokies Motels)' })), |
| 17 | + custom_instructions: Type.Optional( |
| 18 | + Type.String({ description: 'Custom behaviour instructions, business rules, pricing, etc.' }), |
| 19 | + ), |
| 20 | + greeting: Type.Optional(Type.String({ description: 'Greeting spoken when a call connects' })), |
| 21 | + voice_preference: Type.Optional(Type.String({ description: 'Voice ID (e.g. Rime.ArcanaV3.astra)' })), |
| 22 | + // list_voices filters |
| 23 | + provider: Type.Optional( |
| 24 | + Type.String({ |
| 25 | + description: |
| 26 | + 'Voice provider. Required for list_voices (default: "rime"). Options: rime, minimax, telnyx, inworld, resemble, aws, azure', |
| 27 | + }), |
| 28 | + ), |
| 29 | + language: Type.Optional(Type.String({ description: 'Filter by language code (e.g. "en", "es", "fr-FR")' })), |
| 30 | + gender: Type.Optional(Type.String({ description: 'Filter by gender ("Male" or "Female")' })), |
| 31 | + accent: Type.Optional(Type.String({ description: 'Filter by accent (e.g. "British", "Southern American")' })), |
| 32 | + search: Type.Optional(Type.String({ description: 'Search voice name or label' })), |
| 33 | +}); |
| 34 | + |
| 35 | +// ── Voice Cache ───────────────────────────────────────────── |
| 36 | + |
| 37 | +interface CacheEntry { |
| 38 | + voices: Voice[]; |
| 39 | + providers: string[]; |
| 40 | + defaultVoice: string; |
| 41 | + fetchedAt: number; |
| 42 | +} |
| 43 | + |
| 44 | +const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes |
| 45 | +const voiceCache = new Map<string, CacheEntry>(); |
| 46 | + |
| 47 | +// ── Helpers ────────────────────────────────────────────────── |
| 48 | + |
| 49 | +function formatResult(payload: unknown) { |
| 50 | + return { |
| 51 | + content: [{ type: 'text' as const, text: JSON.stringify(payload, null, 2) }], |
| 52 | + details: payload, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function truncate(str: string | null, max: number): string | null { |
| 57 | + if (!str) return null; |
| 58 | + return str.length > max ? `${str.slice(0, max - 1)}_` : str; |
| 59 | +} |
| 60 | + |
| 61 | +// ── Tool ───────────────────────────────────────────────────── |
| 62 | + |
| 63 | +export class BotConfigTool { |
| 64 | + readonly name = 'clawtalk_bot_config'; |
| 65 | + readonly label = 'ClawTalk Bot Config'; |
| 66 | + readonly description = |
| 67 | + 'Read or update the bot configuration (name, role, custom instructions, greeting, voice). Use action "get" to read current config, "update" to change fields, "list_voices" to browse available voices by provider.'; |
| 68 | + readonly parameters = BotConfigToolSchema; |
| 69 | + |
| 70 | + private readonly client: ClawTalkClient; |
| 71 | + private readonly logger: Logger; |
| 72 | + |
| 73 | + constructor(params: { client: ClawTalkClient; logger: Logger }) { |
| 74 | + this.client = params.client; |
| 75 | + this.logger = params.logger; |
| 76 | + } |
| 77 | + |
| 78 | + async execute(_toolCallId: string, raw: Record<string, unknown>) { |
| 79 | + const { action } = raw as { action: string }; |
| 80 | + |
| 81 | + if (action === 'get') { |
| 82 | + return this.handleGet(); |
| 83 | + } |
| 84 | + |
| 85 | + if (action === 'update') { |
| 86 | + return this.handleUpdate(raw); |
| 87 | + } |
| 88 | + |
| 89 | + if (action === 'list_voices') { |
| 90 | + return this.handleListVoices(raw); |
| 91 | + } |
| 92 | + |
| 93 | + throw new ToolError('clawtalk_bot_config', `Unknown action: ${action}`); |
| 94 | + } |
| 95 | + |
| 96 | + private async handleGet() { |
| 97 | + this.logger.info('Getting bot config'); |
| 98 | + try { |
| 99 | + const me = await this.client.user.me(); |
| 100 | + const config = { |
| 101 | + agent_name: me.agent_name ?? null, |
| 102 | + display_name: me.display_name ?? null, |
| 103 | + bot_role: me.bot_role ?? 'personal AI assistant', |
| 104 | + custom_instructions: me.custom_instructions ?? null, |
| 105 | + greeting: me.greeting ?? null, |
| 106 | + voice_preference: me.voice_preference ?? null, |
| 107 | + }; |
| 108 | + return formatResult(config); |
| 109 | + } catch (err) { |
| 110 | + throw ToolError.fromError('clawtalk_bot_config', err); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private async handleUpdate(raw: Record<string, unknown>) { |
| 115 | + this.logger.info('Updating bot config'); |
| 116 | + const fields: Record<string, unknown> = {}; |
| 117 | + for (const key of ['agent_name', 'bot_role', 'custom_instructions', 'greeting', 'voice_preference']) { |
| 118 | + if (raw[key] !== undefined) fields[key] = raw[key]; |
| 119 | + } |
| 120 | + if (Object.keys(fields).length === 0) { |
| 121 | + throw new ToolError('clawtalk_bot_config', 'No fields provided for update'); |
| 122 | + } |
| 123 | + try { |
| 124 | + await this.client.user.updateMe(fields); |
| 125 | + const me = await this.client.user.me(); |
| 126 | + const config = { |
| 127 | + agent_name: me.agent_name ?? null, |
| 128 | + display_name: me.display_name ?? null, |
| 129 | + bot_role: me.bot_role ?? 'personal AI assistant', |
| 130 | + custom_instructions: me.custom_instructions ?? null, |
| 131 | + greeting: me.greeting ?? null, |
| 132 | + voice_preference: me.voice_preference ?? null, |
| 133 | + message: 'Bot config updated. Changes take effect on the next call.', |
| 134 | + }; |
| 135 | + return formatResult(config); |
| 136 | + } catch (err) { |
| 137 | + throw ToolError.fromError('clawtalk_bot_config', err); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private async handleListVoices(raw: Record<string, unknown>) { |
| 142 | + const provider = (raw.provider as string) || 'rime'; |
| 143 | + const language = raw.language as string | undefined; |
| 144 | + const gender = raw.gender as string | undefined; |
| 145 | + const accent = raw.accent as string | undefined; |
| 146 | + const search = raw.search as string | undefined; |
| 147 | + |
| 148 | + this.logger.info(`Listing voices for provider: ${provider}`); |
| 149 | + |
| 150 | + try { |
| 151 | + // Check cache |
| 152 | + const cached = voiceCache.get(provider); |
| 153 | + let voices: Voice[]; |
| 154 | + let defaultVoice: string; |
| 155 | + let allProviders: string[]; |
| 156 | + |
| 157 | + if (cached && Date.now() - cached.fetchedAt < CACHE_TTL_MS) { |
| 158 | + voices = cached.voices; |
| 159 | + defaultVoice = cached.defaultVoice; |
| 160 | + allProviders = cached.providers; |
| 161 | + } else { |
| 162 | + const result = await this.client.voices.list(provider); |
| 163 | + voices = result.voices; |
| 164 | + defaultVoice = result.default_voice; |
| 165 | + allProviders = result.providers; |
| 166 | + voiceCache.set(provider, { |
| 167 | + voices, |
| 168 | + providers: allProviders, |
| 169 | + defaultVoice, |
| 170 | + fetchedAt: Date.now(), |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + // Apply client-side filters |
| 175 | + let filtered = voices; |
| 176 | + |
| 177 | + if (language) { |
| 178 | + const lang = language.toLowerCase(); |
| 179 | + filtered = filtered.filter((v) => v.language.toLowerCase().startsWith(lang)); |
| 180 | + } |
| 181 | + |
| 182 | + if (gender) { |
| 183 | + const g = gender.toLowerCase(); |
| 184 | + filtered = filtered.filter((v) => v.gender?.toLowerCase() === g); |
| 185 | + } |
| 186 | + |
| 187 | + if (accent) { |
| 188 | + const a = accent.toLowerCase(); |
| 189 | + filtered = filtered.filter((v) => v.accent?.toLowerCase().includes(a)); |
| 190 | + } |
| 191 | + |
| 192 | + if (search) { |
| 193 | + const s = search.toLowerCase(); |
| 194 | + filtered = filtered.filter( |
| 195 | + (v) => |
| 196 | + v.name.toLowerCase().includes(s) || |
| 197 | + v.id.toLowerCase().includes(s) || |
| 198 | + (v.label?.toLowerCase().includes(s) ?? false), |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + const totalMatching = filtered.length; |
| 203 | + const capped = filtered.slice(0, 20); |
| 204 | + |
| 205 | + return formatResult({ |
| 206 | + default_voice: defaultVoice, |
| 207 | + provider, |
| 208 | + providers: allProviders, |
| 209 | + total_matching: totalMatching, |
| 210 | + showing: capped.length, |
| 211 | + voices: capped.map((v) => ({ |
| 212 | + id: v.id, |
| 213 | + name: v.name, |
| 214 | + provider: v.provider, |
| 215 | + language: v.language, |
| 216 | + gender: v.gender, |
| 217 | + label: truncate(v.label, 80), |
| 218 | + })), |
| 219 | + }); |
| 220 | + } catch (err) { |
| 221 | + throw ToolError.fromError('clawtalk_bot_config', err); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments