(opts: CreateBotOptions<TStateSchema>)
| 252 | } |
| 253 | |
| 254 | export function createBot< |
| 255 | TStateSchema extends StandardSchemaV1 | undefined = undefined, |
| 256 | >(opts: CreateBotOptions<TStateSchema>): Bot<ThreadStateOf<TStateSchema>> { |
| 257 | const cfg = opts.store ?? {}; |
| 258 | if ( |
| 259 | (cfg.identity && !cfg.transcripts) || |
| 260 | (!cfg.identity && cfg.transcripts) |
| 261 | ) { |
| 262 | throw new Error( |
| 263 | "createBot: `identity` and `transcripts` must be configured together.", |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | const backend: StateStore = cfg.adapter ?? new MemoryStore(); |
| 268 | const telemetry = new BotTelemetry({ |
| 269 | backend, |
| 270 | packageName: pkg.name, |
| 271 | packageVersion: pkg.version, |
| 272 | }); |
| 273 | const transcripts = new Transcripts(backend, cfg.transcripts ?? {}); |
| 274 | const registry = new ActionRegistry({ |
| 275 | store: opts.actionStore ?? kvActionStore(backend), |
| 276 | }); |
| 277 | |
| 278 | for (const c of opts.components ?? []) { |
| 279 | if (!c.name) { |
| 280 | console.warn( |
| 281 | "[bot] createBot: skipping anonymous component — give it a name to enable durable actions after restart.", |
| 282 | ); |
| 283 | continue; |
| 284 | } |
| 285 | registry.registerComponent(c.name, c as unknown as ComponentFn); |
| 286 | } |
| 287 | |
| 288 | const agentFactory: (threadId: string) => AbstractAgent = (() => { |
| 289 | const a = opts.agent; |
| 290 | if (typeof a === "function") |
| 291 | return a as (threadId: string) => AbstractAgent; |
| 292 | if (a) return () => a; |
| 293 | return () => { |
| 294 | throw new Error( |
| 295 | "createBot: no agent configured (pass `agent` to use runAgent)", |
| 296 | ); |
| 297 | }; |
| 298 | })(); |
| 299 | |
| 300 | const toolMap = new Map<string, BotTool>(); |
| 301 | for (const t of opts.tools ?? []) toolMap.set(t.name, t); |
| 302 | const context = opts.context ?? []; |
| 303 | |
| 304 | const mentionHandlers: BotHandler[] = []; |
| 305 | const messageHandlers: BotHandler[] = []; |
| 306 | const threadStartedHandlers: ThreadStartHandler[] = []; |
| 307 | const interactionHandlers = new Map< |
| 308 | string, |
| 309 | (ctx: InteractionContext) => void | Promise<void> |
| 310 | >(); |
| 311 | const interruptHandlers = new Map< |
searching dependent graphs…