Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

extensions.ts

40 lines1.2 KB

1/**
2 * A list one plugin reads and other plugins add to, in any order. For
3 * example, a permissions plugin reads rules that tool plugins contribute.
4 *
5 * @example
6 * ```ts
7 * export const PermissionRules = createExtensionPoint<PermissionRule>('acme/permission-rules')
8 * // a tool plugin: contribute: [PermissionRules.item({ tool: 'write_file', decision: 'ask' })]
9 * // the permissions plugin, at run time: ctx.collect(PermissionRules)
10 * ```
11 */
12export interface ExtensionPoint<T> {
13 readonly name: string
14 item: (value: T) => ExtensionItem<T>
16​
17/** One contribution to an extension point. */
18export interface ExtensionItem<T = unknown> {
19 readonly point: string
20 readonly value: T
22​
23export function createExtensionPoint<T>(name: string): ExtensionPoint<T> {
24 return { name, item: (value) => ({ point: name, value }) }
26​
27/**
28 * A typed plugin event. `ctx.emit(event, value)` sends it to every
29 * `ctx.on(event, handler)` in the session, and clients see it as a
30 * `harness.plugin.event` CUSTOM event.
31 */
32export interface PluginEvent<T> {
33 readonly name: string
34 /** Type only. Never read at runtime. */
35 readonly __type?: T
37​
38export function createPluginEvent<T>(name: string): PluginEvent<T> {
39 return { name }