khotan-data 0.0.1 → 0.1.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.
Files changed (51) hide show
  1. package/AGENTS.md +54 -0
  2. package/README.md +62 -0
  3. package/dist/cli.js +2585 -0
  4. package/dist/factory.cjs +2319 -0
  5. package/dist/factory.cjs.map +1 -0
  6. package/dist/factory.d.cts +475 -0
  7. package/dist/factory.d.ts +475 -0
  8. package/dist/factory.js +2311 -0
  9. package/dist/factory.js.map +1 -0
  10. package/dist/plug-client.cjs +99 -0
  11. package/dist/plug-client.cjs.map +1 -0
  12. package/dist/plug-client.d.cts +71 -0
  13. package/dist/plug-client.d.ts +71 -0
  14. package/dist/plug-client.js +96 -0
  15. package/dist/plug-client.js.map +1 -0
  16. package/dist/templates/agent-skill.md +73 -0
  17. package/dist/templates/agents.md +41 -0
  18. package/dist/templates/catch.example.ts +36 -0
  19. package/dist/templates/catch.ts +107 -0
  20. package/dist/templates/config-page.tsx +20 -0
  21. package/dist/templates/debug-index-page.tsx +101 -0
  22. package/dist/templates/debug-page.tsx +48 -0
  23. package/dist/templates/graph-page.tsx +11 -0
  24. package/dist/templates/hub.tsx +450 -0
  25. package/dist/templates/inflow.example.ts +61 -0
  26. package/dist/templates/inflow.ts +99 -0
  27. package/dist/templates/khotan-config.ts +40 -0
  28. package/dist/templates/khotan-route.ts +13 -0
  29. package/dist/templates/logs-page.tsx +9 -0
  30. package/dist/templates/logs.tsx +20 -0
  31. package/dist/templates/outflow.example.ts +52 -0
  32. package/dist/templates/outflow.ts +90 -0
  33. package/dist/templates/pass.example.ts +51 -0
  34. package/dist/templates/pass.ts +124 -0
  35. package/dist/templates/plug-debugger.tsx +1185 -0
  36. package/dist/templates/plug.example.ts +93 -0
  37. package/dist/templates/plug.ts +806 -0
  38. package/dist/templates/relay.example.ts +61 -0
  39. package/dist/templates/relay.ts +95 -0
  40. package/dist/templates/runs-table.tsx +592 -0
  41. package/dist/templates/schema.ts +424 -0
  42. package/dist/templates/skill-dashboard.md +144 -0
  43. package/dist/templates/skill-plug.md +193 -0
  44. package/dist/templates/skill-setup.md +119 -0
  45. package/dist/templates/skill-webhook.md +196 -0
  46. package/dist/templates/topology-canvas.tsx +1406 -0
  47. package/dist/templates/var-panel.tsx +276 -0
  48. package/dist/templates/webhook-events-table.tsx +241 -0
  49. package/dist/templates/wire-panel.tsx +216 -0
  50. package/dist/templates/wire.ts +155 -0
  51. package/package.json +46 -5
@@ -0,0 +1,475 @@
1
+ import { PgDatabase } from 'drizzle-orm/pg-core';
2
+
3
+ interface ResourceRegistration {
4
+ name: string;
5
+ connectField: string;
6
+ description?: string;
7
+ }
8
+ type FlowType = "inflow" | "outflow" | "relay" | "webhook";
9
+ type KhotanRunStatus = "pending" | "running" | "completed" | "partial" | "failed" | "cancelled";
10
+ type KhotanTerminalRunStatus = "completed" | "partial" | "failed" | "cancelled";
11
+ interface FlowRunResult {
12
+ status?: KhotanTerminalRunStatus;
13
+ extracted?: number;
14
+ transformed?: number;
15
+ created?: number;
16
+ updated?: number;
17
+ deleted?: number;
18
+ failed?: number;
19
+ error?: string | null;
20
+ metadata?: Record<string, unknown> | null;
21
+ }
22
+ interface FlowRunContext {
23
+ plug: {
24
+ get<T>(path: string, options?: {
25
+ params?: Record<string, unknown>;
26
+ headers?: Record<string, string>;
27
+ }): Promise<T>;
28
+ post<T>(path: string, options?: {
29
+ body?: unknown;
30
+ headers?: Record<string, string>;
31
+ }): Promise<T>;
32
+ put<T>(path: string, options?: {
33
+ body?: unknown;
34
+ headers?: Record<string, string>;
35
+ }): Promise<T>;
36
+ patch<T>(path: string, options?: {
37
+ body?: unknown;
38
+ headers?: Record<string, string>;
39
+ }): Promise<T>;
40
+ delete<T>(path: string, options?: {
41
+ headers?: Record<string, string>;
42
+ }): Promise<T>;
43
+ };
44
+ flow: {
45
+ id: string;
46
+ name: string;
47
+ type: FlowType;
48
+ resource?: string | null;
49
+ to?: string | null;
50
+ };
51
+ runType: string;
52
+ body?: unknown;
53
+ vars: Record<string, string>;
54
+ setVars(updates: Record<string, string>): Promise<void>;
55
+ }
56
+ interface FlowWorkflowContext {
57
+ flow: {
58
+ id: string;
59
+ name: string;
60
+ type: FlowType;
61
+ resource?: string | null;
62
+ to?: string | null;
63
+ };
64
+ runType: string;
65
+ body?: unknown;
66
+ vars: Record<string, string>;
67
+ khotanRunId: string;
68
+ }
69
+ interface KhotanRunUpdate {
70
+ type?: "progress" | "log" | "metric" | "error";
71
+ message: string;
72
+ progress?: number;
73
+ extracted?: number;
74
+ transformed?: number;
75
+ created?: number;
76
+ updated?: number;
77
+ deleted?: number;
78
+ failed?: number;
79
+ metadata?: Record<string, unknown>;
80
+ }
81
+ interface FlowRegistration {
82
+ name: string;
83
+ type: FlowType;
84
+ schedule?: string;
85
+ resource?: string;
86
+ to?: string;
87
+ workflow?(ctx: FlowWorkflowContext): Promise<FlowRunResult | void>;
88
+ run?(ctx: FlowRunContext): Promise<FlowRunResult | void>;
89
+ }
90
+ interface WireSubscribeContext {
91
+ plug: {
92
+ get<T>(path: string, options?: {
93
+ params?: Record<string, unknown>;
94
+ headers?: Record<string, string>;
95
+ }): Promise<T>;
96
+ post<T>(path: string, options?: {
97
+ body?: unknown;
98
+ headers?: Record<string, string>;
99
+ }): Promise<T>;
100
+ put<T>(path: string, options?: {
101
+ body?: unknown;
102
+ headers?: Record<string, string>;
103
+ }): Promise<T>;
104
+ patch<T>(path: string, options?: {
105
+ body?: unknown;
106
+ headers?: Record<string, string>;
107
+ }): Promise<T>;
108
+ delete<T>(path: string, options?: {
109
+ headers?: Record<string, string>;
110
+ }): Promise<T>;
111
+ };
112
+ callbackUrl: string;
113
+ events: string[];
114
+ wireVars: Record<string, string>;
115
+ setWireVars(updates: Record<string, string>): Promise<void>;
116
+ }
117
+ interface WireUnsubscribeContext {
118
+ plug: {
119
+ get<T>(path: string, options?: {
120
+ params?: Record<string, unknown>;
121
+ headers?: Record<string, string>;
122
+ }): Promise<T>;
123
+ post<T>(path: string, options?: {
124
+ body?: unknown;
125
+ headers?: Record<string, string>;
126
+ }): Promise<T>;
127
+ put<T>(path: string, options?: {
128
+ body?: unknown;
129
+ headers?: Record<string, string>;
130
+ }): Promise<T>;
131
+ patch<T>(path: string, options?: {
132
+ body?: unknown;
133
+ headers?: Record<string, string>;
134
+ }): Promise<T>;
135
+ delete<T>(path: string, options?: {
136
+ headers?: Record<string, string>;
137
+ }): Promise<T>;
138
+ };
139
+ remoteId: string;
140
+ wireVars: Record<string, string>;
141
+ setWireVars(updates: Record<string, string>): Promise<void>;
142
+ }
143
+ interface WireVerifyContext {
144
+ headers: Record<string, string>;
145
+ body: string;
146
+ wireVars: Record<string, string>;
147
+ }
148
+ interface WireRegistration {
149
+ events: string[];
150
+ onSubscribe(ctx: WireSubscribeContext): Promise<{
151
+ remoteId: string;
152
+ }>;
153
+ onUnsubscribe(ctx: WireUnsubscribeContext): Promise<void>;
154
+ onVerify?(ctx: WireVerifyContext): Promise<boolean>;
155
+ }
156
+ interface CatchRegistration {
157
+ type: "catch";
158
+ name: string;
159
+ events?: string[];
160
+ workflow: (ctx: {
161
+ event: Record<string, unknown>;
162
+ eventType: string;
163
+ headers: Record<string, string>;
164
+ khotanRunId: string;
165
+ }) => Promise<void>;
166
+ }
167
+ interface PassRegistration {
168
+ type: "pass";
169
+ name: string;
170
+ to: string;
171
+ events?: string[];
172
+ workflow: (ctx: {
173
+ event: Record<string, unknown>;
174
+ eventType: string;
175
+ headers: Record<string, string>;
176
+ destVars: Record<string, string>;
177
+ khotanRunId: string;
178
+ }) => Promise<void>;
179
+ }
180
+ type WebhookRegistration = CatchRegistration | PassRegistration;
181
+ interface VarField {
182
+ readonly key: string;
183
+ label: string;
184
+ type: "text" | "password" | "url";
185
+ secret?: boolean;
186
+ hidden?: boolean;
187
+ required?: boolean;
188
+ placeholder?: string;
189
+ defaultValue?: string;
190
+ }
191
+ interface PlugRegistration {
192
+ name: string;
193
+ plug: {
194
+ baseUrl: string;
195
+ authType: string;
196
+ varFields?: readonly VarField[];
197
+ endpoints?: Record<string, {
198
+ method: string;
199
+ path: string;
200
+ description?: string;
201
+ body?: {
202
+ _def?: unknown;
203
+ shape?: Record<string, unknown>;
204
+ };
205
+ query?: {
206
+ _def?: unknown;
207
+ shape?: Record<string, unknown>;
208
+ };
209
+ responses?: Record<number, {
210
+ _def?: unknown;
211
+ shape?: Record<string, unknown>;
212
+ }>;
213
+ }>;
214
+ get<T>(path: string, options?: {
215
+ params?: Record<string, unknown>;
216
+ headers?: Record<string, string>;
217
+ vars?: Record<string, string>;
218
+ _setVars?: (updates: Record<string, string>) => Promise<void>;
219
+ _skipHooks?: boolean;
220
+ }): Promise<T>;
221
+ post<T>(path: string, options?: {
222
+ body?: unknown;
223
+ headers?: Record<string, string>;
224
+ vars?: Record<string, string>;
225
+ _setVars?: (updates: Record<string, string>) => Promise<void>;
226
+ _skipHooks?: boolean;
227
+ }): Promise<T>;
228
+ put<T>(path: string, options?: {
229
+ body?: unknown;
230
+ headers?: Record<string, string>;
231
+ vars?: Record<string, string>;
232
+ _setVars?: (updates: Record<string, string>) => Promise<void>;
233
+ _skipHooks?: boolean;
234
+ }): Promise<T>;
235
+ patch<T>(path: string, options?: {
236
+ body?: unknown;
237
+ headers?: Record<string, string>;
238
+ vars?: Record<string, string>;
239
+ _setVars?: (updates: Record<string, string>) => Promise<void>;
240
+ _skipHooks?: boolean;
241
+ }): Promise<T>;
242
+ delete<T>(path: string, options?: {
243
+ headers?: Record<string, string>;
244
+ vars?: Record<string, string>;
245
+ _setVars?: (updates: Record<string, string>) => Promise<void>;
246
+ _skipHooks?: boolean;
247
+ }): Promise<T>;
248
+ };
249
+ vars?: VarField[];
250
+ flows?: FlowRegistration[];
251
+ endpoints?: Record<string, {
252
+ method: string;
253
+ path: string;
254
+ }>;
255
+ wires?: WireRegistration[];
256
+ webhooks?: WebhookRegistration[];
257
+ catches?: CatchRegistration[];
258
+ passes?: PassRegistration[];
259
+ }
260
+ interface KhotanAdapter {
261
+ upsertPlug(plug: {
262
+ name: string;
263
+ baseUrl: string;
264
+ authType: string;
265
+ }): Promise<{
266
+ id: string;
267
+ }>;
268
+ upsertFlow(flow: {
269
+ plugId: string;
270
+ name: string;
271
+ type: string;
272
+ schedule?: string | null;
273
+ }): Promise<{
274
+ id: string;
275
+ }>;
276
+ listPlugs(): Promise<Record<string, unknown>[]>;
277
+ getPlug(id: string): Promise<Record<string, unknown> | null>;
278
+ getPlugFlows(plugId: string): Promise<Record<string, unknown>[]>;
279
+ getFlow(flowId: string): Promise<Record<string, unknown> | null>;
280
+ listFlows(): Promise<Record<string, unknown>[]>;
281
+ getRun(runId: string): Promise<Record<string, unknown> | null>;
282
+ listRuns(flowId: string): Promise<Record<string, unknown>[]>;
283
+ listRunsPage(params: {
284
+ limit: number;
285
+ offset: number;
286
+ }): Promise<{
287
+ items: Record<string, unknown>[];
288
+ hasMore: boolean;
289
+ }>;
290
+ upsertResource(resource: {
291
+ name: string;
292
+ connectField: string;
293
+ description?: string | null;
294
+ }): Promise<{
295
+ id: string;
296
+ }>;
297
+ listResources(): Promise<Record<string, unknown>[]>;
298
+ getResource(id: string): Promise<Record<string, unknown> | null>;
299
+ getResourceFlows(resourceId: string): Promise<Record<string, unknown>[]>;
300
+ upsertMapping(mapping: {
301
+ id?: string;
302
+ resourceId: string;
303
+ connectValue: string;
304
+ refs: Record<string, string>;
305
+ metadata?: Record<string, unknown> | null;
306
+ }): Promise<{
307
+ id: string;
308
+ created: boolean;
309
+ }>;
310
+ getMapping(id: string): Promise<Record<string, unknown> | null>;
311
+ listMappings(resourceId: string): Promise<Record<string, unknown>[]>;
312
+ deleteMapping(id: string): Promise<void>;
313
+ lookupMapping(params: {
314
+ resourceId: string;
315
+ plugName: string;
316
+ ref: string;
317
+ }): Promise<Record<string, unknown> | null>;
318
+ updateFlowResourceId(flowId: string, resourceId: string): Promise<void>;
319
+ togglePlugEnabled(plugId: string, enabled: boolean): Promise<void>;
320
+ toggleFlowEnabled(flowId: string, enabled: boolean): Promise<void>;
321
+ toggleWebhookHandlerEnabled(handlerId: string, enabled: boolean): Promise<void>;
322
+ insertWire(wire: {
323
+ plugId: string;
324
+ remoteId: string;
325
+ callbackUrl: string;
326
+ eventTypes: string[];
327
+ }): Promise<{
328
+ id: string;
329
+ }>;
330
+ upsertWire(wire: {
331
+ plugId: string;
332
+ }): Promise<{
333
+ id: string;
334
+ }>;
335
+ getActiveWire(plugId: string): Promise<Record<string, unknown> | null>;
336
+ getPlugWire(plugId: string): Promise<Record<string, unknown> | null>;
337
+ getWire(wireId: string): Promise<Record<string, unknown> | null>;
338
+ updateWireStatus(wireId: string, status: "active" | "disabled" | "pending"): Promise<void>;
339
+ updateWireDetails(wireId: string, details: {
340
+ remoteId: string;
341
+ callbackUrl: string;
342
+ eventTypes: string[];
343
+ status: "active";
344
+ }): Promise<void>;
345
+ getWireMetadata(wireId: string): Promise<string | null>;
346
+ updateWireMetadata(wireId: string, metadata: string): Promise<void>;
347
+ getEncryptedVariables(plugId: string): Promise<string | null>;
348
+ setEncryptedVariables(plugId: string, encrypted: string): Promise<void>;
349
+ clearEncryptedVariables(plugId: string): Promise<void>;
350
+ upsertWebhookHandler(handler: {
351
+ wireId: string;
352
+ name: string;
353
+ type: "catch" | "pass";
354
+ destinationPlugId?: string | null;
355
+ }): Promise<{
356
+ id: string;
357
+ }>;
358
+ listWebhookHandlers(wireId: string): Promise<Record<string, unknown>[]>;
359
+ getLatestWebhookHandlerRun(handlerId: string): Promise<Record<string, unknown> | null>;
360
+ insertRun(run: {
361
+ flowId?: string | null;
362
+ wireId?: string | null;
363
+ webhookHandlerId?: string | null;
364
+ workflowRunId?: string | null;
365
+ runType: string;
366
+ status: string;
367
+ }): Promise<{
368
+ id: string;
369
+ }>;
370
+ updateRun(runId: string, updates: {
371
+ status: KhotanRunStatus;
372
+ workflowRunId?: string | null;
373
+ completedAt?: Date;
374
+ durationMs?: number;
375
+ extracted?: number;
376
+ transformed?: number;
377
+ created?: number;
378
+ updated?: number;
379
+ deleted?: number;
380
+ failed?: number;
381
+ error?: string | null;
382
+ metadata?: Record<string, unknown> | null;
383
+ }): Promise<void>;
384
+ insertWebhookEvent(event: {
385
+ wireId: string;
386
+ webhookHandlerId: string;
387
+ khotanRunId: string;
388
+ eventType: string;
389
+ payload: Record<string, unknown>;
390
+ headers: Record<string, string>;
391
+ }): Promise<{
392
+ id: string;
393
+ }>;
394
+ listWebhookEventsPage(params: {
395
+ limit: number;
396
+ offset: number;
397
+ }): Promise<{
398
+ items: Record<string, unknown>[];
399
+ hasMore: boolean;
400
+ }>;
401
+ updateFlowLastRun(flowId: string, updates: {
402
+ lastRunAt: Date;
403
+ lastRunStatus: KhotanTerminalRunStatus;
404
+ }): Promise<void>;
405
+ }
406
+ interface KhotanConfig {
407
+ adapter: KhotanAdapter;
408
+ plugs: PlugRegistration[];
409
+ resources?: ResourceRegistration[];
410
+ secret?: string;
411
+ }
412
+ type KhotanHandler = (request: Request) => Promise<Response>;
413
+ interface WireInstance {
414
+ create(callbackUrl: string): Promise<Record<string, unknown>>;
415
+ delete(wireId: string): Promise<void>;
416
+ get(): Promise<Record<string, unknown> | null>;
417
+ }
418
+ interface FlowStartOptions {
419
+ runType?: string;
420
+ body?: unknown;
421
+ }
422
+ interface FlowSelectorOptions {
423
+ plugName?: string;
424
+ }
425
+ interface FlowInstance {
426
+ start(options?: FlowStartOptions): Promise<Record<string, unknown>>;
427
+ }
428
+ interface KhotanInstance {
429
+ handler: KhotanHandler;
430
+ init(): Promise<void>;
431
+ flow(flowNameOrId: string, options?: FlowSelectorOptions): FlowInstance;
432
+ wire(plugName: string): WireInstance;
433
+ getVars(plugName: string): Promise<Record<string, string>>;
434
+ setVars(plugName: string, vars: Record<string, string>): Promise<void>;
435
+ clearVars(plugName: string): Promise<void>;
436
+ hasVars(plugName: string): Promise<boolean>;
437
+ getVarFields(plugName: string): readonly VarField[];
438
+ getPlug(plugName: string): PlugRegistration["plug"];
439
+ }
440
+ declare function drizzleAdapter(db: PgDatabase<any, any, any>): KhotanAdapter;
441
+ type WorkflowStartFn = (workflowFn: (...args: any[]) => any, args: unknown[]) => Promise<unknown>;
442
+ interface WorkflowRunHandle {
443
+ runId?: string;
444
+ status?: Promise<string>;
445
+ returnValue?: Promise<unknown>;
446
+ cancel?: () => Promise<void>;
447
+ getReadable?: (options?: {
448
+ startIndex?: number;
449
+ namespace?: string;
450
+ }) => ReadableStream;
451
+ }
452
+ type WorkflowGetRunFn = (runId: string) => WorkflowRunHandle;
453
+ type WorkflowGetWritableFn = <T = unknown>(options?: {
454
+ namespace?: string;
455
+ }) => WritableStream<T>;
456
+ declare function __setWorkflowStartForTests(start: WorkflowStartFn | null): void;
457
+ declare function __setWorkflowGetRunForTests(getRun: WorkflowGetRunFn | null): void;
458
+ declare function __setWorkflowGetWritableForTests(getWritable: WorkflowGetWritableFn | null): void;
459
+ declare function sendUpdate(update: KhotanRunUpdate | string, options?: {
460
+ namespace?: string;
461
+ }): Promise<void>;
462
+ declare function khotan(config: KhotanConfig): KhotanInstance;
463
+ interface NextJsRequest extends Request {
464
+ nextUrl?: URL;
465
+ }
466
+ interface NextJsRouteHandlers {
467
+ GET: (req: NextJsRequest) => Promise<Response>;
468
+ POST: (req: NextJsRequest) => Promise<Response>;
469
+ PUT: (req: NextJsRequest) => Promise<Response>;
470
+ PATCH: (req: NextJsRequest) => Promise<Response>;
471
+ DELETE: (req: NextJsRequest) => Promise<Response>;
472
+ }
473
+ declare function toNextJsHandler(factoryHandler: KhotanHandler): NextJsRouteHandlers;
474
+
475
+ export { type CatchRegistration, type FlowInstance, type FlowRegistration, type FlowRunContext, type FlowRunResult, type FlowSelectorOptions, type FlowStartOptions, type FlowType, type FlowWorkflowContext, type KhotanAdapter, type KhotanConfig, type KhotanHandler, type KhotanInstance, type KhotanRunStatus, type KhotanRunUpdate, type KhotanTerminalRunStatus, type PassRegistration, type PlugRegistration, type ResourceRegistration, type VarField, type WebhookRegistration, type WireInstance, type WireRegistration, type WireSubscribeContext, type WireUnsubscribeContext, type WireVerifyContext, __setWorkflowGetRunForTests, __setWorkflowGetWritableForTests, __setWorkflowStartForTests, drizzleAdapter, khotan, sendUpdate, toNextJsHandler };