human-in-the-loop 0.1.0 → 2.0.2

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.
package/src/types.ts ADDED
@@ -0,0 +1,431 @@
1
+ /**
2
+ * Core types for human-in-the-loop workflows
3
+ */
4
+
5
+ import type { AIFunctionDefinition, JSONSchema } from 'ai-functions'
6
+
7
+ /**
8
+ * Status of a human interaction request
9
+ */
10
+ export type HumanRequestStatus =
11
+ | 'pending' // Waiting for human response
12
+ | 'in_progress' // Being handled by a human
13
+ | 'completed' // Completed successfully
14
+ | 'rejected' // Rejected or declined
15
+ | 'timeout' // Timed out waiting for response
16
+ | 'escalated' // Escalated to higher authority
17
+ | 'cancelled' // Cancelled before completion
18
+
19
+ /**
20
+ * Priority level for human requests
21
+ */
22
+ export type Priority = 'low' | 'normal' | 'high' | 'critical'
23
+
24
+ /**
25
+ * Human role definition
26
+ */
27
+ export interface Role {
28
+ /** Unique role identifier */
29
+ id: string
30
+ /** Role name */
31
+ name: string
32
+ /** Role description */
33
+ description?: string
34
+ /** Role capabilities/permissions */
35
+ capabilities?: string[]
36
+ /** Default escalation role */
37
+ escalatesTo?: string
38
+ }
39
+
40
+ /**
41
+ * Team composition
42
+ */
43
+ export interface Team {
44
+ /** Team identifier */
45
+ id: string
46
+ /** Team name */
47
+ name: string
48
+ /** Team description */
49
+ description?: string
50
+ /** Team members (role IDs or user IDs) */
51
+ members: string[]
52
+ /** Team lead */
53
+ lead?: string
54
+ }
55
+
56
+ /**
57
+ * Human worker/assignee
58
+ */
59
+ export interface Human {
60
+ /** Unique identifier */
61
+ id: string
62
+ /** Display name */
63
+ name: string
64
+ /** Email address */
65
+ email?: string
66
+ /** Assigned roles */
67
+ roles?: string[]
68
+ /** Teams the human belongs to */
69
+ teams?: string[]
70
+ /** Contact channels */
71
+ channels?: {
72
+ slack?: string
73
+ email?: string
74
+ sms?: string
75
+ web?: boolean
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Goals and objectives
81
+ */
82
+ export interface Goals {
83
+ /** Goal identifier */
84
+ id: string
85
+ /** Goals/objectives */
86
+ objectives: string[]
87
+ /** Success criteria */
88
+ successCriteria?: string[]
89
+ /** Key results */
90
+ keyResults?: string[]
91
+ /** Target date */
92
+ targetDate?: Date
93
+ }
94
+
95
+ /**
96
+ * Key Performance Indicators
97
+ */
98
+ export interface KPIs {
99
+ /** KPI identifier */
100
+ id: string
101
+ /** Metric name */
102
+ name: string
103
+ /** Current value */
104
+ value: number
105
+ /** Target value */
106
+ target?: number
107
+ /** Unit of measurement */
108
+ unit?: string
109
+ /** Trend direction */
110
+ trend?: 'up' | 'down' | 'stable'
111
+ }
112
+
113
+ /**
114
+ * Objectives and Key Results
115
+ */
116
+ export interface OKRs {
117
+ /** OKR identifier */
118
+ id: string
119
+ /** Objective statement */
120
+ objective: string
121
+ /** Key results */
122
+ keyResults: Array<{
123
+ /** Key result description */
124
+ description: string
125
+ /** Current progress (0-1) */
126
+ progress: number
127
+ /** Target value */
128
+ target?: number
129
+ /** Current value */
130
+ current?: number
131
+ }>
132
+ /** Quarter or time period */
133
+ period?: string
134
+ /** Owner */
135
+ owner?: string
136
+ }
137
+
138
+ /**
139
+ * Base human request
140
+ */
141
+ export interface HumanRequest<TInput = unknown, TOutput = unknown> {
142
+ /** Request ID */
143
+ id: string
144
+ /** Request type */
145
+ type: 'approval' | 'question' | 'task' | 'decision' | 'review' | 'notification'
146
+ /** Request status */
147
+ status: HumanRequestStatus
148
+ /** Priority level */
149
+ priority: Priority
150
+ /** Title or summary */
151
+ title: string
152
+ /** Detailed description */
153
+ description: string
154
+ /** Input data for the request */
155
+ input: TInput
156
+ /** Expected output schema */
157
+ outputSchema?: JSONSchema
158
+ /** Who should handle this */
159
+ assignee?: string | string[]
160
+ /** Assigned role */
161
+ role?: string
162
+ /** Assigned team */
163
+ team?: string
164
+ /** Escalation path */
165
+ escalatesTo?: string | string[]
166
+ /** Timeout in milliseconds */
167
+ timeout?: number
168
+ /** When the request was created */
169
+ createdAt: Date
170
+ /** When it was last updated */
171
+ updatedAt: Date
172
+ /** When it was completed */
173
+ completedAt?: Date
174
+ /** Who responded */
175
+ respondedBy?: string
176
+ /** The response */
177
+ response?: TOutput
178
+ /** Rejection reason */
179
+ rejectionReason?: string
180
+ /** Additional metadata */
181
+ metadata?: Record<string, unknown>
182
+ }
183
+
184
+ /**
185
+ * Approval request
186
+ */
187
+ export interface ApprovalRequest<TData = unknown> extends HumanRequest<TData, ApprovalResponse> {
188
+ type: 'approval'
189
+ /** What is being approved */
190
+ subject: string
191
+ /** Default is approve required */
192
+ requiresApproval?: boolean
193
+ /** Approval chain (multiple approvers) */
194
+ approvers?: string[]
195
+ /** Current approver index */
196
+ currentApproverIndex?: number
197
+ }
198
+
199
+ /**
200
+ * Approval response
201
+ */
202
+ export interface ApprovalResponse {
203
+ /** Whether approved */
204
+ approved: boolean
205
+ /** Comments from approver */
206
+ comments?: string
207
+ /** Conditions or requirements */
208
+ conditions?: string[]
209
+ }
210
+
211
+ /**
212
+ * Question request
213
+ */
214
+ export interface QuestionRequest extends HumanRequest<{ question: string; context?: unknown }, string> {
215
+ type: 'question'
216
+ /** The question */
217
+ question: string
218
+ /** Additional context */
219
+ context?: unknown
220
+ /** Suggested answers */
221
+ suggestions?: string[]
222
+ }
223
+
224
+ /**
225
+ * Task request
226
+ */
227
+ export interface TaskRequest<TInput = unknown, TOutput = unknown> extends HumanRequest<TInput, TOutput> {
228
+ type: 'task'
229
+ /** Task instructions */
230
+ instructions: string
231
+ /** Required tools/resources */
232
+ tools?: AIFunctionDefinition[]
233
+ /** Estimated effort */
234
+ estimatedEffort?: string
235
+ }
236
+
237
+ /**
238
+ * Decision request
239
+ */
240
+ export interface DecisionRequest<TOptions extends string = string>
241
+ extends HumanRequest<{ options: TOptions[]; context?: unknown }, TOptions> {
242
+ type: 'decision'
243
+ /** Available options */
244
+ options: TOptions[]
245
+ /** Decision context */
246
+ context?: unknown
247
+ /** Criteria for decision */
248
+ criteria?: string[]
249
+ }
250
+
251
+ /**
252
+ * Review request
253
+ */
254
+ export interface ReviewRequest<TContent = unknown>
255
+ extends HumanRequest<TContent, ReviewResponse> {
256
+ type: 'review'
257
+ /** Content to review */
258
+ content: TContent
259
+ /** Review criteria/checklist */
260
+ criteria?: string[]
261
+ /** Review type */
262
+ reviewType?: 'code' | 'content' | 'design' | 'data' | 'other'
263
+ }
264
+
265
+ /**
266
+ * Review response
267
+ */
268
+ export interface ReviewResponse {
269
+ /** Whether approved */
270
+ approved: boolean
271
+ /** Review comments */
272
+ comments: string
273
+ /** Required changes */
274
+ changes?: string[]
275
+ /** Rating (1-5) */
276
+ rating?: number
277
+ }
278
+
279
+ /**
280
+ * Notification
281
+ */
282
+ export interface Notification {
283
+ /** Notification ID */
284
+ id: string
285
+ /** Notification type */
286
+ type: 'info' | 'warning' | 'error' | 'success'
287
+ /** Title */
288
+ title: string
289
+ /** Message */
290
+ message: string
291
+ /** Recipient(s) */
292
+ recipient: string | string[]
293
+ /** Channel(s) to use */
294
+ channels?: ('slack' | 'email' | 'sms' | 'web')[]
295
+ /** Priority */
296
+ priority?: Priority
297
+ /** Additional data */
298
+ data?: unknown
299
+ /** When created */
300
+ createdAt: Date
301
+ /** When delivered */
302
+ deliveredAt?: Date
303
+ /** Whether read */
304
+ read?: boolean
305
+ }
306
+
307
+ /**
308
+ * Review queue for managing pending human requests
309
+ */
310
+ export interface ReviewQueue<T extends HumanRequest = HumanRequest> {
311
+ /** Queue identifier */
312
+ id: string
313
+ /** Queue name */
314
+ name: string
315
+ /** Queue description */
316
+ description?: string
317
+ /** Items in the queue */
318
+ items: T[]
319
+ /** Queue filters */
320
+ filters?: {
321
+ status?: HumanRequestStatus[]
322
+ priority?: Priority[]
323
+ assignee?: string[]
324
+ role?: string[]
325
+ team?: string[]
326
+ }
327
+ /** Sort order */
328
+ sortBy?: 'createdAt' | 'priority' | 'updatedAt'
329
+ /** Sort direction */
330
+ sortDirection?: 'asc' | 'desc'
331
+ }
332
+
333
+ /**
334
+ * Escalation policy
335
+ */
336
+ export interface EscalationPolicy {
337
+ /** Policy ID */
338
+ id: string
339
+ /** Policy name */
340
+ name: string
341
+ /** When to escalate */
342
+ conditions: {
343
+ /** Escalate if no response after N milliseconds */
344
+ timeout?: number
345
+ /** Escalate on rejection */
346
+ onRejection?: boolean
347
+ /** Escalate if priority is at or above */
348
+ minPriority?: Priority
349
+ }
350
+ /** Escalation path */
351
+ escalationPath: Array<{
352
+ /** Who to escalate to */
353
+ assignee: string
354
+ /** After how long (cumulative) */
355
+ afterMs: number
356
+ /** Notify via these channels */
357
+ notifyVia?: ('slack' | 'email' | 'sms' | 'web')[]
358
+ }>
359
+ }
360
+
361
+ /**
362
+ * Approval workflow configuration
363
+ */
364
+ export interface ApprovalWorkflow {
365
+ /** Workflow ID */
366
+ id: string
367
+ /** Workflow name */
368
+ name: string
369
+ /** Workflow steps */
370
+ steps: Array<{
371
+ /** Step name */
372
+ name: string
373
+ /** Required role */
374
+ role?: string
375
+ /** Required approvers */
376
+ approvers: string[]
377
+ /** All must approve or just one */
378
+ requireAll?: boolean
379
+ /** Auto-approve after timeout */
380
+ autoApproveAfter?: number
381
+ }>
382
+ /** Current step index */
383
+ currentStep?: number
384
+ /** Workflow status */
385
+ status?: 'pending' | 'in_progress' | 'completed' | 'rejected'
386
+ }
387
+
388
+ /**
389
+ * Human-in-the-loop store interface
390
+ */
391
+ export interface HumanStore {
392
+ /** Create a new request */
393
+ create<T extends HumanRequest>(request: Omit<T, 'id' | 'createdAt' | 'updatedAt'>): Promise<T>
394
+
395
+ /** Get a request by ID */
396
+ get<T extends HumanRequest>(id: string): Promise<T | null>
397
+
398
+ /** Update a request */
399
+ update<T extends HumanRequest>(id: string, updates: Partial<T>): Promise<T>
400
+
401
+ /** List requests with filters */
402
+ list<T extends HumanRequest>(filters?: ReviewQueue['filters'], limit?: number): Promise<T[]>
403
+
404
+ /** Complete a request */
405
+ complete<T extends HumanRequest>(id: string, response: T['response']): Promise<T>
406
+
407
+ /** Reject a request */
408
+ reject(id: string, reason: string): Promise<HumanRequest>
409
+
410
+ /** Escalate a request */
411
+ escalate(id: string, to: string): Promise<HumanRequest>
412
+
413
+ /** Cancel a request */
414
+ cancel(id: string): Promise<HumanRequest>
415
+ }
416
+
417
+ /**
418
+ * Options for human interactions
419
+ */
420
+ export interface HumanOptions {
421
+ /** Storage backend */
422
+ store?: HumanStore
423
+ /** Default timeout in milliseconds */
424
+ defaultTimeout?: number
425
+ /** Default priority */
426
+ defaultPriority?: Priority
427
+ /** Escalation policies */
428
+ escalationPolicies?: EscalationPolicy[]
429
+ /** Auto-escalate on timeout */
430
+ autoEscalate?: boolean
431
+ }
package/tsconfig.json CHANGED
@@ -1,14 +1,9 @@
1
1
  {
2
- "extends": "../../tsconfig.json",
3
- "include": ["src"],
4
- "exclude": ["node_modules", "dist"],
2
+ "extends": "../../tsconfig.base.json",
5
3
  "compilerOptions": {
6
- "outDir": "dist",
7
- "declaration": true,
8
- "jsx": "react-jsx",
9
- "incremental": false,
10
- "paths": {
11
- "@/*": ["./src/*"]
12
- }
13
- }
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
14
9
  }
package/TODO.md DELETED
@@ -1,53 +0,0 @@
1
- # Human-in-the-Loop Implementation Progress
2
-
3
- ## Core Implementation
4
- - [x] Set up package structure
5
- - [x] Define core types and interfaces
6
- - [x] Implement factory function
7
- - [x] Create basic tests
8
-
9
- ## Platform Implementations
10
- - [x] Slack implementation
11
- - [x] Create SlackHumanFunction class
12
- - [x] Implement request and getResponse methods
13
- - [ ] Implement real Slack Block Kit integration
14
- - [x] Microsoft Teams implementation
15
- - [x] Create TeamsHumanFunction class
16
- - [x] Implement request and getResponse methods
17
- - [ ] Implement real Teams Adaptive Cards integration
18
- - [x] React/ShadCN implementation
19
- - [x] Create ReactHumanFunction class
20
- - [x] Implement HumanFeedback component
21
- - [x] Add styling and theming support
22
- - [ ] Add ShadCN UI components
23
- - [x] Email (React Email) implementation
24
- - [x] Create EmailHumanFunction class
25
- - [x] Implement EmailTemplate component
26
- - [ ] Implement real email sending functionality
27
-
28
- ## Documentation
29
- - [x] Create README with usage examples
30
- - [x] Add JSDoc comments to public APIs
31
- - [ ] Create API reference documentation
32
- - [ ] Add more comprehensive examples
33
-
34
- ## Technical Challenges and Blockers
35
- - [ ] Need to implement real platform integrations (currently mocked)
36
- - [ ] Need to add proper error handling and timeout mechanisms
37
- - [ ] Need to implement persistent storage for responses
38
-
39
- ## Verification Requirements
40
- - [x] Basic unit tests for factory function
41
- - [ ] Integration tests for each platform
42
- - [ ] End-to-end tests with real platform integrations
43
- - [ ] Performance tests for large-scale usage
44
-
45
- ## Future Enhancements
46
- - [ ] Add more comprehensive tests
47
- - [ ] Implement real platform integrations (currently mocked)
48
- - [ ] Add more customization options for each platform
49
- - [ ] Create example applications
50
- - [ ] Add support for more platforms (Discord, Telegram, etc.)
51
- - [ ] Implement webhook handlers for platform callbacks
52
- - [ ] Add authentication and authorization mechanisms
53
- - [ ] Implement rate limiting and throttling