diff --git a/index.d.ts b/index.d.ts index b5453b0..b743902 100644 --- a/index.d.ts +++ b/index.d.ts @@ -198,8 +198,30 @@ namespace WebMCP { annotations?: ToolAnnotations; } + /** + * Initialization options for a `ToolActivatedEvent`. + */ + type ToolActivatedEventInit = globalThis.ToolActivatedEventInit; + + /** + * Event dispatched at a `ModelContext` when the execution of a tool begins. + */ + type ToolActivatedEvent = globalThis.ToolActivatedEvent; + + /** + * Initialization options for a `ToolCancelEvent`. + */ + type ToolCancelEventInit = globalThis.ToolCancelEventInit; + + /** + * Event dispatched at a `ModelContext` when the execution of a tool is cancelled. + */ + type ToolCancelEvent = globalThis.ToolCancelEvent; + interface ModelContextEventMap { "toolchange": Event; + "toolactivated": ToolActivatedEvent; + "toolcancel": ToolCancelEvent; } /** @@ -234,6 +256,14 @@ namespace WebMCP { * Event handler for the toolchange event. */ ontoolchange: ((this: ModelContext, ev: Event) => any) | null; + /** + * Event handler for the toolactivated event. + */ + ontoolactivated: ((this: ModelContext, ev: ToolActivatedEvent) => any) | null; + /** + * Event handler for the toolcancel event. + */ + ontoolcancel: ((this: ModelContext, ev: ToolCancelEvent) => any) | null; addEventListener(type: K, listener: (this: ModelContext, ev: ModelContextEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; @@ -249,6 +279,50 @@ interface Document { */ readonly modelContext?: WebMCP.ModelContext; } + +interface ToolActivatedEventInit extends EventInit { + /** + * The name of the tool whose execution has started. + */ + toolName?: string; +} + +/** + * The event dispatched at a `ModelContext` object when the execution of a tool begins. + */ +interface ToolActivatedEvent extends Event { + /** + * The name of the tool whose execution has started. + */ + readonly toolName: string; +} + +var ToolActivatedEvent: { + prototype: ToolActivatedEvent; + new(type: string, eventInitDict?: ToolActivatedEventInit): ToolActivatedEvent; +}; + +interface ToolCancelEventInit extends EventInit { + /** + * The name of the tool whose execution was cancelled. + */ + toolName?: string; +} + +/** + * The event dispatched at a `ModelContext` object when the execution of a tool is cancelled. + */ +interface ToolCancelEvent extends Event { + /** + * The name of the tool whose execution was cancelled. + */ + readonly toolName: string; +} + +var ToolCancelEvent: { + prototype: ToolCancelEvent; + new(type: string, eventInitDict?: ToolCancelEventInit): ToolCancelEvent; +}; } export type { WebMCP }; diff --git a/index.test-d.ts b/index.test-d.ts index 3c11c54..56d0505 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -189,3 +189,47 @@ test('supports executeTool', async () => { }>(); }); +test('supports the toolchange, toolactivated, and toolcancel events', () => { + document.modelContext?.addEventListener('toolchange', (event) => { + expectTypeOf(event).toEqualTypeOf(); + }); + + document.modelContext?.addEventListener('toolactivated', (event) => { + expectTypeOf(event).toEqualTypeOf(); + expectTypeOf(event.toolName).toEqualTypeOf(); + }); + + document.modelContext?.addEventListener('toolcancel', (event) => { + expectTypeOf(event).toEqualTypeOf(); + expectTypeOf(event.toolName).toEqualTypeOf(); + }); + + if (document.modelContext) { + document.modelContext.ontoolchange = (event) => { + expectTypeOf(event).toEqualTypeOf(); + }; + document.modelContext.ontoolactivated = (event) => { + expectTypeOf(event).toEqualTypeOf(); + }; + document.modelContext.ontoolcancel = (event) => { + expectTypeOf(event).toEqualTypeOf(); + }; + document.modelContext.ontoolchange = null; + document.modelContext.ontoolactivated = null; + document.modelContext.ontoolcancel = null; + } + + expectTypeOf(new ToolActivatedEvent('toolactivated')).toEqualTypeOf(); + expectTypeOf(new ToolActivatedEvent('toolactivated', { toolName: 'search', bubbles: true })) + .toEqualTypeOf(); + expectTypeOf(new ToolCancelEvent('toolcancel', { toolName: 'search' })).toEqualTypeOf(); + + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); +});