@wave-av/workflow-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,141 @@
1
+ import { EventEmitter } from 'eventemitter3';
2
+ import { AnyWorkflowEvent, WorkflowDefinition, ExecuteWorkflowRequest, WorkflowExecution, ListExecutionsRequest, ListExecutionsResponse, ExecutionLog } from './types.mjs';
3
+ import 'zod';
4
+
5
+ /**
6
+ * WAVE Workflow SDK Client
7
+ *
8
+ * HTTP client for interacting with the WAVE Workflow API.
9
+ */
10
+
11
+ /**
12
+ * Client configuration options
13
+ */
14
+ interface WaveWorkflowClientConfig {
15
+ /** API key for authentication */
16
+ apiKey: string;
17
+ /** Organization ID for tenant isolation */
18
+ organizationId: string;
19
+ /** Base URL for the API (default: https://api.wave.online) */
20
+ baseUrl?: string;
21
+ /** Request timeout in milliseconds (default: 30000) */
22
+ timeout?: number;
23
+ /** Enable debug logging */
24
+ debug?: boolean;
25
+ }
26
+ /**
27
+ * Workflow event types for the event emitter
28
+ */
29
+ interface WorkflowClientEvents {
30
+ 'execution.started': (event: AnyWorkflowEvent) => void;
31
+ 'execution.completed': (event: AnyWorkflowEvent) => void;
32
+ 'execution.failed': (event: AnyWorkflowEvent) => void;
33
+ 'phase.started': (event: AnyWorkflowEvent) => void;
34
+ 'phase.completed': (event: AnyWorkflowEvent) => void;
35
+ 'error': (error: Error) => void;
36
+ }
37
+ /**
38
+ * WAVE Workflow API Client
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const client = new WaveWorkflowClient({
43
+ * apiKey: process.env.WAVE_API_KEY,
44
+ * organizationId: 'org_123',
45
+ * });
46
+ *
47
+ * // Execute a workflow
48
+ * const execution = await client.execute('my-workflow', {
49
+ * input_params: { foo: 'bar' },
50
+ * });
51
+ *
52
+ * // Wait for completion
53
+ * const result = await client.waitForCompletion(execution.id);
54
+ * ```
55
+ */
56
+ declare class WaveWorkflowClient extends EventEmitter<WorkflowClientEvents> {
57
+ private readonly config;
58
+ private readonly headers;
59
+ constructor(config: WaveWorkflowClientConfig);
60
+ /**
61
+ * Get a workflow definition by slug
62
+ */
63
+ getWorkflow(slug: string): Promise<WorkflowDefinition>;
64
+ /**
65
+ * List all workflows
66
+ */
67
+ listWorkflows(options?: {
68
+ category?: string;
69
+ status?: string;
70
+ limit?: number;
71
+ offset?: number;
72
+ }): Promise<{
73
+ workflows: WorkflowDefinition[];
74
+ total: number;
75
+ }>;
76
+ /**
77
+ * Execute a workflow
78
+ */
79
+ execute(workflowSlug: string, request?: ExecuteWorkflowRequest): Promise<WorkflowExecution>;
80
+ /**
81
+ * Get execution status
82
+ */
83
+ getExecution(executionId: string): Promise<WorkflowExecution>;
84
+ /**
85
+ * List executions
86
+ */
87
+ listExecutions(request?: ListExecutionsRequest): Promise<ListExecutionsResponse>;
88
+ /**
89
+ * Cancel a running execution
90
+ */
91
+ cancelExecution(executionId: string): Promise<WorkflowExecution>;
92
+ /**
93
+ * Pause a running execution
94
+ */
95
+ pauseExecution(executionId: string): Promise<WorkflowExecution>;
96
+ /**
97
+ * Resume a paused execution
98
+ */
99
+ resumeExecution(executionId: string): Promise<WorkflowExecution>;
100
+ /**
101
+ * Retry a failed execution
102
+ */
103
+ retryExecution(executionId: string, options?: {
104
+ from_checkpoint?: boolean;
105
+ }): Promise<WorkflowExecution>;
106
+ /**
107
+ * Get execution logs
108
+ */
109
+ getLogs(executionId: string, options?: {
110
+ level?: 'debug' | 'info' | 'warn' | 'error';
111
+ limit?: number;
112
+ offset?: number;
113
+ }): Promise<{
114
+ logs: ExecutionLog[];
115
+ total: number;
116
+ }>;
117
+ /**
118
+ * Wait for an execution to complete
119
+ */
120
+ waitForCompletion(executionId: string, options?: {
121
+ pollInterval?: number;
122
+ timeout?: number;
123
+ onProgress?: (execution: WorkflowExecution) => void;
124
+ }): Promise<WorkflowExecution>;
125
+ /**
126
+ * Execute a workflow and wait for completion
127
+ */
128
+ executeAndWait(workflowSlug: string, request?: ExecuteWorkflowRequest, waitOptions?: Parameters<typeof this.waitForCompletion>[1]): Promise<WorkflowExecution>;
129
+ /**
130
+ * Subscribe to real-time execution events
131
+ */
132
+ subscribeToExecution(executionId: string): () => void;
133
+ private request;
134
+ private sleep;
135
+ }
136
+ /**
137
+ * Create a new workflow client instance
138
+ */
139
+ declare function createClient(config: WaveWorkflowClientConfig): WaveWorkflowClient;
140
+
141
+ export { WaveWorkflowClient, type WaveWorkflowClientConfig, type WorkflowClientEvents, createClient };
@@ -0,0 +1,141 @@
1
+ import { EventEmitter } from 'eventemitter3';
2
+ import { AnyWorkflowEvent, WorkflowDefinition, ExecuteWorkflowRequest, WorkflowExecution, ListExecutionsRequest, ListExecutionsResponse, ExecutionLog } from './types.js';
3
+ import 'zod';
4
+
5
+ /**
6
+ * WAVE Workflow SDK Client
7
+ *
8
+ * HTTP client for interacting with the WAVE Workflow API.
9
+ */
10
+
11
+ /**
12
+ * Client configuration options
13
+ */
14
+ interface WaveWorkflowClientConfig {
15
+ /** API key for authentication */
16
+ apiKey: string;
17
+ /** Organization ID for tenant isolation */
18
+ organizationId: string;
19
+ /** Base URL for the API (default: https://api.wave.online) */
20
+ baseUrl?: string;
21
+ /** Request timeout in milliseconds (default: 30000) */
22
+ timeout?: number;
23
+ /** Enable debug logging */
24
+ debug?: boolean;
25
+ }
26
+ /**
27
+ * Workflow event types for the event emitter
28
+ */
29
+ interface WorkflowClientEvents {
30
+ 'execution.started': (event: AnyWorkflowEvent) => void;
31
+ 'execution.completed': (event: AnyWorkflowEvent) => void;
32
+ 'execution.failed': (event: AnyWorkflowEvent) => void;
33
+ 'phase.started': (event: AnyWorkflowEvent) => void;
34
+ 'phase.completed': (event: AnyWorkflowEvent) => void;
35
+ 'error': (error: Error) => void;
36
+ }
37
+ /**
38
+ * WAVE Workflow API Client
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const client = new WaveWorkflowClient({
43
+ * apiKey: process.env.WAVE_API_KEY,
44
+ * organizationId: 'org_123',
45
+ * });
46
+ *
47
+ * // Execute a workflow
48
+ * const execution = await client.execute('my-workflow', {
49
+ * input_params: { foo: 'bar' },
50
+ * });
51
+ *
52
+ * // Wait for completion
53
+ * const result = await client.waitForCompletion(execution.id);
54
+ * ```
55
+ */
56
+ declare class WaveWorkflowClient extends EventEmitter<WorkflowClientEvents> {
57
+ private readonly config;
58
+ private readonly headers;
59
+ constructor(config: WaveWorkflowClientConfig);
60
+ /**
61
+ * Get a workflow definition by slug
62
+ */
63
+ getWorkflow(slug: string): Promise<WorkflowDefinition>;
64
+ /**
65
+ * List all workflows
66
+ */
67
+ listWorkflows(options?: {
68
+ category?: string;
69
+ status?: string;
70
+ limit?: number;
71
+ offset?: number;
72
+ }): Promise<{
73
+ workflows: WorkflowDefinition[];
74
+ total: number;
75
+ }>;
76
+ /**
77
+ * Execute a workflow
78
+ */
79
+ execute(workflowSlug: string, request?: ExecuteWorkflowRequest): Promise<WorkflowExecution>;
80
+ /**
81
+ * Get execution status
82
+ */
83
+ getExecution(executionId: string): Promise<WorkflowExecution>;
84
+ /**
85
+ * List executions
86
+ */
87
+ listExecutions(request?: ListExecutionsRequest): Promise<ListExecutionsResponse>;
88
+ /**
89
+ * Cancel a running execution
90
+ */
91
+ cancelExecution(executionId: string): Promise<WorkflowExecution>;
92
+ /**
93
+ * Pause a running execution
94
+ */
95
+ pauseExecution(executionId: string): Promise<WorkflowExecution>;
96
+ /**
97
+ * Resume a paused execution
98
+ */
99
+ resumeExecution(executionId: string): Promise<WorkflowExecution>;
100
+ /**
101
+ * Retry a failed execution
102
+ */
103
+ retryExecution(executionId: string, options?: {
104
+ from_checkpoint?: boolean;
105
+ }): Promise<WorkflowExecution>;
106
+ /**
107
+ * Get execution logs
108
+ */
109
+ getLogs(executionId: string, options?: {
110
+ level?: 'debug' | 'info' | 'warn' | 'error';
111
+ limit?: number;
112
+ offset?: number;
113
+ }): Promise<{
114
+ logs: ExecutionLog[];
115
+ total: number;
116
+ }>;
117
+ /**
118
+ * Wait for an execution to complete
119
+ */
120
+ waitForCompletion(executionId: string, options?: {
121
+ pollInterval?: number;
122
+ timeout?: number;
123
+ onProgress?: (execution: WorkflowExecution) => void;
124
+ }): Promise<WorkflowExecution>;
125
+ /**
126
+ * Execute a workflow and wait for completion
127
+ */
128
+ executeAndWait(workflowSlug: string, request?: ExecuteWorkflowRequest, waitOptions?: Parameters<typeof this.waitForCompletion>[1]): Promise<WorkflowExecution>;
129
+ /**
130
+ * Subscribe to real-time execution events
131
+ */
132
+ subscribeToExecution(executionId: string): () => void;
133
+ private request;
134
+ private sleep;
135
+ }
136
+ /**
137
+ * Create a new workflow client instance
138
+ */
139
+ declare function createClient(config: WaveWorkflowClientConfig): WaveWorkflowClient;
140
+
141
+ export { WaveWorkflowClient, type WaveWorkflowClientConfig, type WorkflowClientEvents, createClient };
package/dist/client.js ADDED
@@ -0,0 +1,262 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client.ts
21
+ var client_exports = {};
22
+ __export(client_exports, {
23
+ WaveWorkflowClient: () => WaveWorkflowClient,
24
+ createClient: () => createClient
25
+ });
26
+ module.exports = __toCommonJS(client_exports);
27
+ var import_eventemitter3 = require("eventemitter3");
28
+ var WaveWorkflowClient = class extends import_eventemitter3.EventEmitter {
29
+ config;
30
+ headers;
31
+ constructor(config) {
32
+ super();
33
+ this.config = {
34
+ baseUrl: "https://api.wave.online",
35
+ timeout: 3e4,
36
+ debug: false,
37
+ ...config
38
+ };
39
+ this.headers = {
40
+ "Authorization": `Bearer ${this.config.apiKey}`,
41
+ "Content-Type": "application/json",
42
+ "X-Organization-Id": this.config.organizationId
43
+ };
44
+ }
45
+ // ==========================================================================
46
+ // Workflow Definitions
47
+ // ==========================================================================
48
+ /**
49
+ * Get a workflow definition by slug
50
+ */
51
+ async getWorkflow(slug) {
52
+ return this.request(`/v1/workflows/${slug}`);
53
+ }
54
+ /**
55
+ * List all workflows
56
+ */
57
+ async listWorkflows(options) {
58
+ const params = new URLSearchParams();
59
+ if (options?.category) params.set("category", options.category);
60
+ if (options?.status) params.set("status", options.status);
61
+ if (options?.limit) params.set("limit", String(options.limit));
62
+ if (options?.offset) params.set("offset", String(options.offset));
63
+ return this.request(`/v1/workflows?${params.toString()}`);
64
+ }
65
+ // ==========================================================================
66
+ // Workflow Executions
67
+ // ==========================================================================
68
+ /**
69
+ * Execute a workflow
70
+ */
71
+ async execute(workflowSlug, request) {
72
+ const response = await this.request(
73
+ `/v1/workflows/${workflowSlug}/execute`,
74
+ {
75
+ method: "POST",
76
+ body: JSON.stringify(request || {})
77
+ }
78
+ );
79
+ return response.execution;
80
+ }
81
+ /**
82
+ * Get execution status
83
+ */
84
+ async getExecution(executionId) {
85
+ return this.request(`/v1/executions/${executionId}`);
86
+ }
87
+ /**
88
+ * List executions
89
+ */
90
+ async listExecutions(request) {
91
+ const params = new URLSearchParams();
92
+ if (request?.workflow_id) params.set("workflow_id", request.workflow_id);
93
+ if (request?.status) params.set("status", request.status);
94
+ if (request?.limit) params.set("limit", String(request.limit));
95
+ if (request?.offset) params.set("offset", String(request.offset));
96
+ if (request?.order_by) params.set("order_by", request.order_by);
97
+ if (request?.order) params.set("order", request.order);
98
+ return this.request(`/v1/executions?${params.toString()}`);
99
+ }
100
+ /**
101
+ * Cancel a running execution
102
+ */
103
+ async cancelExecution(executionId) {
104
+ return this.request(
105
+ `/v1/executions/${executionId}/cancel`,
106
+ { method: "POST" }
107
+ );
108
+ }
109
+ /**
110
+ * Pause a running execution
111
+ */
112
+ async pauseExecution(executionId) {
113
+ return this.request(
114
+ `/v1/executions/${executionId}/pause`,
115
+ { method: "POST" }
116
+ );
117
+ }
118
+ /**
119
+ * Resume a paused execution
120
+ */
121
+ async resumeExecution(executionId) {
122
+ return this.request(
123
+ `/v1/executions/${executionId}/resume`,
124
+ { method: "POST" }
125
+ );
126
+ }
127
+ /**
128
+ * Retry a failed execution
129
+ */
130
+ async retryExecution(executionId, options) {
131
+ return this.request(
132
+ `/v1/executions/${executionId}/retry`,
133
+ {
134
+ method: "POST",
135
+ body: JSON.stringify(options || {})
136
+ }
137
+ );
138
+ }
139
+ // ==========================================================================
140
+ // Execution Logs
141
+ // ==========================================================================
142
+ /**
143
+ * Get execution logs
144
+ */
145
+ async getLogs(executionId, options) {
146
+ const params = new URLSearchParams();
147
+ if (options?.level) params.set("level", options.level);
148
+ if (options?.limit) params.set("limit", String(options.limit));
149
+ if (options?.offset) params.set("offset", String(options.offset));
150
+ return this.request(`/v1/executions/${executionId}/logs?${params.toString()}`);
151
+ }
152
+ // ==========================================================================
153
+ // Convenience Methods
154
+ // ==========================================================================
155
+ /**
156
+ * Wait for an execution to complete
157
+ */
158
+ async waitForCompletion(executionId, options) {
159
+ const pollInterval = options?.pollInterval || 2e3;
160
+ const timeout = options?.timeout || 36e5;
161
+ const startTime = Date.now();
162
+ const terminalStatuses = [
163
+ "completed",
164
+ "failed",
165
+ "cancelled",
166
+ "timeout"
167
+ ];
168
+ while (Date.now() - startTime < timeout) {
169
+ const execution = await this.getExecution(executionId);
170
+ if (options?.onProgress) {
171
+ options.onProgress(execution);
172
+ }
173
+ if (terminalStatuses.includes(execution.status)) {
174
+ return execution;
175
+ }
176
+ await this.sleep(pollInterval);
177
+ }
178
+ throw new Error(`Execution ${executionId} timed out after ${timeout}ms`);
179
+ }
180
+ /**
181
+ * Execute a workflow and wait for completion
182
+ */
183
+ async executeAndWait(workflowSlug, request, waitOptions) {
184
+ const execution = await this.execute(workflowSlug, request);
185
+ return this.waitForCompletion(execution.id, waitOptions);
186
+ }
187
+ // ==========================================================================
188
+ // Real-time Events (WebSocket)
189
+ // ==========================================================================
190
+ /**
191
+ * Subscribe to real-time execution events
192
+ */
193
+ subscribeToExecution(executionId) {
194
+ const wsUrl = this.config.baseUrl.replace("https://", "wss://").replace("http://", "ws://");
195
+ const ws = new WebSocket(
196
+ `${wsUrl}/v1/executions/${executionId}/events?token=${this.config.apiKey}`
197
+ );
198
+ ws.onmessage = (event) => {
199
+ try {
200
+ const data = JSON.parse(event.data);
201
+ this.emit(data.type, data);
202
+ } catch (error) {
203
+ this.emit("error", error);
204
+ }
205
+ };
206
+ ws.onerror = (error) => {
207
+ this.emit("error", new Error(`WebSocket error: ${error}`));
208
+ };
209
+ return () => {
210
+ if (ws.readyState === WebSocket.OPEN) {
211
+ ws.close();
212
+ }
213
+ };
214
+ }
215
+ // ==========================================================================
216
+ // Private Helpers
217
+ // ==========================================================================
218
+ async request(path, options) {
219
+ const url = `${this.config.baseUrl}${path}`;
220
+ if (this.config.debug) {
221
+ console.log(`[WaveWorkflowClient] ${options?.method || "GET"} ${url}`);
222
+ }
223
+ const controller = new AbortController();
224
+ const timeoutId = setTimeout(
225
+ () => controller.abort(),
226
+ this.config.timeout
227
+ );
228
+ try {
229
+ const response = await fetch(url, {
230
+ ...options,
231
+ headers: {
232
+ ...this.headers,
233
+ ...options?.headers
234
+ },
235
+ signal: controller.signal
236
+ });
237
+ clearTimeout(timeoutId);
238
+ if (!response.ok) {
239
+ const error = await response.text();
240
+ throw new Error(`API error (${response.status}): ${error}`);
241
+ }
242
+ return response.json();
243
+ } catch (error) {
244
+ clearTimeout(timeoutId);
245
+ if (error instanceof Error && error.name === "AbortError") {
246
+ throw new Error(`Request timeout after ${this.config.timeout}ms`);
247
+ }
248
+ throw error;
249
+ }
250
+ }
251
+ sleep(ms) {
252
+ return new Promise((resolve) => setTimeout(resolve, ms));
253
+ }
254
+ };
255
+ function createClient(config) {
256
+ return new WaveWorkflowClient(config);
257
+ }
258
+ // Annotate the CommonJS export names for ESM import in node:
259
+ 0 && (module.exports = {
260
+ WaveWorkflowClient,
261
+ createClient
262
+ });
@@ -0,0 +1,8 @@
1
+ import {
2
+ WaveWorkflowClient,
3
+ createClient
4
+ } from "./chunk-M3UAADWT.mjs";
5
+ export {
6
+ WaveWorkflowClient,
7
+ createClient
8
+ };