@amaster.ai/client 1.1.0-beta.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,291 @@
1
+ /**
2
+ * ============================================================================
3
+ * Workflow Module - Type Definitions
4
+ * ============================================================================
5
+ *
6
+ * This module provides workflow execution capabilities using Dify-style
7
+ * workflow engine.
8
+ *
9
+ * ## Key Features
10
+ * - Execute workflows with inputs
11
+ * - Blocking or streaming execution modes
12
+ * - File upload support
13
+ * - Trace ID for debugging
14
+ * - Auto-inject app_id from URL
15
+ *
16
+ * @module workflow
17
+ */
18
+
19
+ import type { ClientResult } from './common';
20
+
21
+ /**
22
+ * Workflow execution response mode
23
+ * - `blocking`: Wait for workflow completion (default)
24
+ * - `streaming`: Stream workflow execution progress
25
+ */
26
+ export type WorkflowResponseMode = 'blocking' | 'streaming';
27
+
28
+ /**
29
+ * Workflow input value (can be any JSON-serializable type)
30
+ */
31
+ export type WorkflowInputValue =
32
+ | string
33
+ | number
34
+ | boolean
35
+ | null
36
+ | undefined
37
+ | Record<string, unknown>
38
+ | unknown[];
39
+
40
+ /**
41
+ * Workflow file attachment
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const file: WorkflowFile = {
46
+ * name: 'document.pdf',
47
+ * type: 'application/pdf',
48
+ * url: 'https://cdn.example.com/document.pdf'
49
+ * };
50
+ * ```
51
+ */
52
+ export interface WorkflowFile {
53
+ /** File name */
54
+ name?: string;
55
+ /** MIME type */
56
+ type?: string;
57
+ /** File URL */
58
+ url?: string;
59
+ /** Additional metadata */
60
+ [key: string]: unknown;
61
+ }
62
+
63
+ /**
64
+ * Workflow execution request parameters
65
+ *
66
+ * @example
67
+ * Simple execution:
68
+ * ```typescript
69
+ * const request: WorkflowRunRequest = {
70
+ * inputs: {
71
+ * query: 'What is the weather?',
72
+ * location: 'New York'
73
+ * }
74
+ * };
75
+ * ```
76
+ *
77
+ * @example
78
+ * With files:
79
+ * ```typescript
80
+ * const request: WorkflowRunRequest = {
81
+ * inputs: { action: 'analyze' },
82
+ * files: [{
83
+ * name: 'data.csv',
84
+ * type: 'text/csv',
85
+ * url: 'https://example.com/data.csv'
86
+ * }],
87
+ * user: 'user-123',
88
+ * trace_id: 'trace-abc-456'
89
+ * };
90
+ * ```
91
+ */
92
+ export interface WorkflowRunRequest {
93
+ /** Input variables for the workflow */
94
+ inputs?: Record<string, WorkflowInputValue>;
95
+ /**
96
+ * Response mode
97
+ * @default 'blocking'
98
+ */
99
+ response_mode?: WorkflowResponseMode;
100
+ /**
101
+ * User identifier
102
+ * @default 'anonymous'
103
+ */
104
+ user?: string;
105
+ /** File attachments */
106
+ files?: WorkflowFile[];
107
+ /** Trace ID for debugging */
108
+ trace_id?: string;
109
+ }
110
+
111
+ /**
112
+ * Workflow execution response
113
+ *
114
+ * @template TOutput - The type of workflow outputs
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * interface AnalysisOutput {
119
+ * summary: string;
120
+ * sentiment: 'positive' | 'negative' | 'neutral';
121
+ * keywords: string[];
122
+ * }
123
+ *
124
+ * const result = await client.workflow.run<AnalysisOutput>('text-analysis', {
125
+ * text: 'This is a great product!'
126
+ * });
127
+ *
128
+ * console.log(result.data.outputs.summary);
129
+ * console.log(result.data.outputs.sentiment); // 'positive'
130
+ * ```
131
+ */
132
+ export interface WorkflowRunResponse<TOutput = Record<string, unknown>> {
133
+ /** Task ID */
134
+ task_id: string;
135
+ /** Workflow run ID */
136
+ workflow_run_id: string;
137
+ /**
138
+ * Execution status
139
+ * @example 'succeeded', 'failed', 'running'
140
+ */
141
+ status: string;
142
+ /** Workflow output data */
143
+ outputs: TOutput;
144
+ /** Error message (if failed) */
145
+ error: string | null;
146
+ /** Execution time in milliseconds */
147
+ elapsed_time: number;
148
+ /** Total tokens consumed (for LLM workflows) */
149
+ total_tokens: number;
150
+ /** Total workflow steps executed */
151
+ total_steps: number;
152
+ /** Creation timestamp (Unix time in milliseconds) */
153
+ created_at: number;
154
+ /** Completion timestamp (Unix time in milliseconds) */
155
+ finished_at: number;
156
+ }
157
+
158
+ // ==================== Workflow Client API ====================
159
+
160
+ /**
161
+ * Workflow Execution Client API
162
+ *
163
+ * Provides methods for executing Dify-style workflows.
164
+ *
165
+ * @example
166
+ * Complete workflow execution:
167
+ * ```typescript
168
+ * const client = createClient({ baseURL: 'https://api.amaster.ai' });
169
+ *
170
+ * // 1. Simple text processing workflow
171
+ * const result = await client.workflow.run('text-summarizer', {
172
+ * text: 'Long article content here...',
173
+ * max_words: 100
174
+ * });
175
+ *
176
+ * if (result.data) {
177
+ * console.log('Summary:', result.data.outputs.summary);
178
+ * console.log('Time taken:', result.data.elapsed_time, 'ms');
179
+ * console.log('Tokens used:', result.data.total_tokens);
180
+ * }
181
+ *
182
+ * // 2. Data analysis workflow with type safety
183
+ * interface AnalysisOutput {
184
+ * insights: string[];
185
+ * score: number;
186
+ * recommendations: string[];
187
+ * }
188
+ *
189
+ * const analysis = await client.workflow.run<AnalysisOutput>('data-analyzer', {
190
+ * dataset: 'sales-2024',
191
+ * metric: 'revenue'
192
+ * });
193
+ *
194
+ * console.log('Insights:', analysis.data.outputs.insights);
195
+ * console.log('Score:', analysis.data.outputs.score);
196
+ * ```
197
+ */
198
+ export interface WorkflowClientAPI {
199
+ /**
200
+ * Execute a workflow
201
+ *
202
+ * Runs a workflow with the provided inputs and returns the result.
203
+ * Automatically injects `app_id` from URL if in browser environment.
204
+ *
205
+ * @template TOutput - The type of workflow outputs
206
+ * @param workflowName - Workflow identifier (key/name)
207
+ * @param inputs - Workflow inputs (can be simple object or WorkflowRunRequest)
208
+ * @returns Workflow execution result
209
+ *
210
+ * @example
211
+ * Simple execution with inputs:
212
+ * ```typescript
213
+ * const result = await client.workflow.run('greeting', {
214
+ * name: 'Alice',
215
+ * language: 'en'
216
+ * });
217
+ *
218
+ * console.log(result.data.outputs.message); // "Hello, Alice!"
219
+ * ```
220
+ *
221
+ * @example
222
+ * With full request configuration:
223
+ * ```typescript
224
+ * const result = await client.workflow.run('document-processor', {
225
+ * inputs: {
226
+ * action: 'extract',
227
+ * format: 'json'
228
+ * },
229
+ * files: [{
230
+ * name: 'invoice.pdf',
231
+ * url: 'https://example.com/invoice.pdf'
232
+ * }],
233
+ * user: 'user-123',
234
+ * trace_id: 'req-abc-456'
235
+ * });
236
+ * ```
237
+ *
238
+ * @example
239
+ * Type-safe outputs:
240
+ * ```typescript
241
+ * interface SentimentOutput {
242
+ * sentiment: 'positive' | 'negative' | 'neutral';
243
+ * confidence: number;
244
+ * keywords: string[];
245
+ * }
246
+ *
247
+ * const result = await client.workflow.run<SentimentOutput>('sentiment-analysis', {
248
+ * text: 'I love this product! It works great.'
249
+ * });
250
+ *
251
+ * if (result.data) {
252
+ * // TypeScript knows the structure of outputs
253
+ * console.log(result.data.outputs.sentiment); // Type: 'positive' | 'negative' | 'neutral'
254
+ * console.log(result.data.outputs.confidence); // Type: number
255
+ * console.log(result.data.outputs.keywords); // Type: string[]
256
+ * }
257
+ * ```
258
+ *
259
+ * @example
260
+ * Error handling:
261
+ * ```typescript
262
+ * const result = await client.workflow.run('risky-workflow', { input: 'test' });
263
+ *
264
+ * if (result.error) {
265
+ * console.error('Workflow failed:', result.error.message);
266
+ * } else if (result.data.error) {
267
+ * console.error('Workflow error:', result.data.error);
268
+ * } else {
269
+ * console.log('Success:', result.data.outputs);
270
+ * }
271
+ * ```
272
+ *
273
+ * @example
274
+ * Performance monitoring:
275
+ * ```typescript
276
+ * const startTime = Date.now();
277
+ * const result = await client.workflow.run('heavy-task', { data: largeDataset });
278
+ *
279
+ * if (result.data) {
280
+ * console.log(`Network time: ${Date.now() - startTime}ms`);
281
+ * console.log(`Execution time: ${result.data.elapsed_time}ms`);
282
+ * console.log(`Steps: ${result.data.total_steps}`);
283
+ * console.log(`Tokens: ${result.data.total_tokens}`);
284
+ * }
285
+ * ```
286
+ */
287
+ run<TOutput = Record<string, unknown>>(
288
+ workflowName: string,
289
+ inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest
290
+ ): Promise<ClientResult<WorkflowRunResponse<TOutput>>>;
291
+ }