/** * kernel-triggers.ts - Triggers API methods for KernelClient * * Installed onto KernelClient.prototype via installTriggerMethods(). */ import { invoke } from '@tauri-apps/api/core'; import type { KernelClient } from './kernel-client'; /** Trigger shape shared across trigger operations. */ type TriggerItem = { id: string; name: string; handId: string; triggerType: string; enabled: boolean; createdAt: string; modifiedAt: string; description?: string; tags: string[]; }; /** Trigger type definition for create/update operations. */ type TriggerTypeSpec = { type: string; cron?: string; pattern?: string; path?: string; secret?: string; events?: string[]; }; export function installTriggerMethods(ClientClass: { prototype: KernelClient }): void { const proto = ClientClass.prototype as any; // ─── Triggers API ─── /** * List all triggers * Returns empty array on error for graceful degradation */ proto.listTriggers = async function (this: KernelClient): Promise<{ triggers?: TriggerItem[] }> { try { const triggers = await invoke('trigger_list'); return { triggers }; } catch (error) { this.log('error', `[TriggersAPI] listTriggers failed: ${this.formatError(error)}`); return { triggers: [] }; } }; /** * Get a single trigger by ID * Returns null on error for graceful degradation */ proto.getTrigger = async function (this: KernelClient, id: string): Promise { try { return await invoke('trigger_get', { id }); } catch (error) { this.log('error', `[TriggersAPI] getTrigger(${id}) failed: ${this.formatError(error)}`); return null; } }; /** * Create a new trigger * Returns null on error for graceful degradation */ proto.createTrigger = async function (this: KernelClient, trigger: { id: string; name: string; handId: string; triggerType: TriggerTypeSpec; enabled?: boolean; description?: string; tags?: string[]; }): Promise { try { return await invoke('trigger_create', { request: trigger }); } catch (error) { this.log('error', `[TriggersAPI] createTrigger(${trigger.id}) failed: ${this.formatError(error)}`); return null; } }; /** * Update an existing trigger * Throws on error as this is a mutation operation that callers need to handle */ proto.updateTrigger = async function (this: KernelClient, id: string, updates: { name?: string; enabled?: boolean; handId?: string; triggerType?: TriggerTypeSpec; }): Promise { try { return await invoke('trigger_update', { id, name: updates.name, enabled: updates.enabled, handId: updates.handId, }); } catch (error) { this.log('error', `[TriggersAPI] updateTrigger(${id}) failed: ${this.formatError(error)}`); throw error; } }; /** * Delete a trigger * Throws on error as this is a destructive operation that callers need to handle */ proto.deleteTrigger = async function (this: KernelClient, id: string): Promise { try { await invoke('trigger_delete', { id }); } catch (error) { this.log('error', `[TriggersAPI] deleteTrigger(${id}) failed: ${this.formatError(error)}`); throw error; } }; /** * Execute a trigger * Throws on error as callers need to know if execution failed */ proto.executeTrigger = async function (this: KernelClient, id: string, input?: Record): Promise> { try { return await invoke>('trigger_execute', { id, input: input || {} }); } catch (error) { this.log('error', `[TriggersAPI] executeTrigger(${id}) failed: ${this.formatError(error)}`); throw error; } }; }