import { Editor } from 'tldraw' import { AgentRequest } from '../../shared/types/AgentRequest' import { BasePromptPart } from '../../shared/types/BasePromptPart' import { PromptPart } from '../../shared/types/PromptPart' import { TldrawAgent } from '../agent/TldrawAgent' import { AgentHelpers } from '../AgentHelpers' // ============================================================================ // Registry // ============================================================================ const registry = new Map>() /** * Register a prompt part util class. Call this after defining each util class. */ export function registerPromptPartUtil>( util: T ): T { if (registry.has(util.type)) { throw new Error(`Prompt part util already registered: ${util.type}`) } registry.set(util.type, util) return util } /** * Get all registered prompt part util classes. */ export function getAllPromptPartUtils(): PromptPartUtilConstructor[] { return Array.from(registry.values()) as PromptPartUtilConstructor[] } /** * Get an object containing instantiated prompt part utils for an agent. */ export function getPromptPartUtilsRecord(agent: TldrawAgent) { const object = {} as Record> for (const util of registry.values()) { object[util.type as PromptPart['type']] = new util(agent) as PromptPartUtil } return object } // ============================================================================ // Base Class // ============================================================================ export abstract class PromptPartUtil { static type: string agent: TldrawAgent editor: Editor constructor(agent: TldrawAgent) { this.agent = agent this.editor = agent?.editor } /** * Get some data to add to the prompt. * @returns The prompt part. */ abstract getPart(request: AgentRequest, helpers: AgentHelpers): Promise | T } export interface PromptPartUtilConstructor { new (agent: TldrawAgent): PromptPartUtil type: T['type'] }