UNPKG

@mastra/core

Version:
1 lines 6.66 kB
{"version":3,"file":"hooks-JAto1d0E.cjs","names":[],"sources":["../src/hooks/mitt.ts","../src/hooks/index.ts"],"sourcesContent":["// copied from https://github.com/developit/mitt\n\nexport type EventType = string | symbol;\n\n// An event handler can take an optional event argument\n// and should not return a value\nexport type Handler<T = unknown> = (event: T) => void;\nexport type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;\n\n// An array of all currently registered event handlers for a type\nexport type EventHandlerList<T = unknown> = Array<Handler<T>>;\nexport type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;\n\n// A map of event types and their corresponding event handlers.\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | '*',\n EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>;\n\nexport interface Emitter<Events extends Record<EventType, unknown>> {\n all: EventHandlerMap<Events>;\n\n on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;\n on(type: '*', handler: WildcardHandler<Events>): void;\n\n off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;\n off(type: '*', handler: WildcardHandler<Events>): void;\n\n emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;\n emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;\n}\n\n/**\n * Mitt: Tiny (~200b) functional event emitter / pubsub.\n * @name mitt\n * @returns {Mitt}\n */\nexport default function mitt<Events extends Record<EventType, unknown>>(\n all?: EventHandlerMap<Events>,\n): Emitter<Events> {\n type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>;\n all = all || new Map();\n\n return {\n /**\n * A Map of event names to registered handler functions.\n */\n all,\n\n /**\n * Register an event handler for the given type.\n * @param {string|symbol} type Type of event to listen for, or `'*'` for all events\n * @param {Function} handler Function to call in response to given event\n * @memberOf mitt\n */\n on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {\n const handlers: Array<GenericEventHandler> | undefined = all.get(type);\n if (handlers) {\n handlers.push(handler);\n } else {\n all.set(type, [handler] as EventHandlerList<Events[keyof Events]>);\n }\n },\n\n /**\n * Remove an event handler for the given type.\n * If `handler` is omitted, all handlers of the given type are removed.\n * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)\n * @param {Function} [handler] Handler function to remove\n * @memberOf mitt\n */\n off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {\n const handlers: Array<GenericEventHandler> | undefined = all.get(type);\n if (handlers) {\n if (handler) {\n handlers.splice(handlers.indexOf(handler) >>> 0, 1);\n } else {\n all.set(type, []);\n }\n }\n },\n\n /**\n * Invoke all handlers for the given type.\n * If present, `'*'` handlers are invoked after type-matched handlers.\n *\n * Note: Manually firing '*' handlers is not supported.\n *\n * @param {string|symbol} type The event type to invoke\n * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler\n * @memberOf mitt\n */\n emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {\n let handlers = all.get(type);\n if (handlers) {\n (handlers as EventHandlerList<Events[keyof Events]>).slice().map(handler => {\n handler(evt!);\n });\n }\n\n handlers = all.get('*');\n if (handlers) {\n (handlers as WildCardEventHandlerList<Events>).slice().map(handler => {\n handler(type, evt!);\n });\n }\n },\n };\n}\n","import type { ScoringHookInput } from '../evals';\n\nimport mitt from './mitt';\nimport type { Handler } from './mitt';\n\nexport enum AvailableHooks {\n ON_EVALUATION = 'onEvaluation',\n ON_GENERATION = 'onGeneration',\n ON_SCORER_RUN = 'onScorerRun',\n}\n\nconst hooks = mitt();\n\nexport function registerHook(hook: AvailableHooks.ON_SCORER_RUN, action: Handler<ScoringHookInput>): void;\nexport function registerHook(hook: `${AvailableHooks}`, action: Handler<any>): void {\n hooks.on(hook, action);\n}\n\nexport function deregisterHook(hook: AvailableHooks.ON_SCORER_RUN, action: Handler<ScoringHookInput>): void;\nexport function deregisterHook(hook: `${AvailableHooks}`, action: Handler<any>): void {\n hooks.off(hook, action);\n}\n\nexport function executeHook(hook: AvailableHooks.ON_SCORER_RUN, action: ScoringHookInput): void;\nexport function executeHook(hook: `${AvailableHooks}`, data: unknown): void {\n // do not block the main thread\n setImmediate(() => {\n hooks.emit(hook, data);\n });\n}\n\n/**\n * Number of handlers currently registered for a hook on the module-level\n * emitter. The emitter never drops handlers on its own, so leak regression\n * tests use this to assert that short-lived instances (e.g. a standalone\n * Agent's ephemeral Mastra) don't accumulate handlers (#19404).\n *\n * @internal test-only\n */\nexport function __hookHandlerCount(hook: `${AvailableHooks}`): number {\n return hooks.all.get(hook)?.length ?? 0;\n}\n"],"mappings":";;;;;;AAqCA,SAAwB,KACtB,KACiB;CAEjB,MAAM,uBAAO,IAAI,IAAI;CAErB,OAAO;;;;EAIL;;;;;;;EAQA,GAA6B,MAAW,SAA8B;GACpE,MAAM,WAAmD,IAAI,IAAI,IAAI;GACrE,IAAI,UACF,SAAS,KAAK,OAAO;QAErB,IAAI,IAAI,MAAM,CAAC,OAAO,CAA2C;EAErE;;;;;;;;EASA,IAA8B,MAAW,SAA+B;GACtE,MAAM,WAAmD,IAAI,IAAI,IAAI;GACrE,IAAI,UACF,IAAI,SACF,SAAS,OAAO,SAAS,QAAQ,OAAO,MAAM,GAAG,CAAC;QAElD,IAAI,IAAI,MAAM,CAAC,CAAC;EAGtB;;;;;;;;;;;EAYA,KAA+B,MAAW,KAAmB;GAC3D,IAAI,WAAW,IAAI,IAAI,IAAI;GAC3B,IAAI,UACF,SAAqD,MAAM,CAAC,CAAC,KAAI,YAAW;IAC1E,QAAQ,GAAI;GACd,CAAC;GAGH,WAAW,IAAI,IAAI,GAAG;GACtB,IAAI,UACF,SAA+C,MAAM,CAAC,CAAC,KAAI,YAAW;IACpE,QAAQ,MAAM,GAAI;GACpB,CAAC;EAEL;CACF;AACF;;;ACvGA,IAAY,iBAAL,yBAAA,gBAAA;CACL,eAAA,mBAAA;CACA,eAAA,mBAAA;CACA,eAAA,mBAAA;;AACF,EAAA,CAAA,CAAA;AAEA,MAAM,QAAQ,KAAK;AAGnB,SAAgB,aAAa,MAA2B,QAA4B;CAClF,MAAM,GAAG,MAAM,MAAM;AACvB;AAGA,SAAgB,eAAe,MAA2B,QAA4B;CACpF,MAAM,IAAI,MAAM,MAAM;AACxB;AAGA,SAAgB,YAAY,MAA2B,MAAqB;CAE1E,mBAAmB;EACjB,MAAM,KAAK,MAAM,IAAI;CACvB,CAAC;AACH;;;;;;;;;AAUA,SAAgB,mBAAmB,MAAmC;CACpE,OAAO,MAAM,IAAI,IAAI,IAAI,CAAC,EAAE,UAAU;AACxC"}