@shalwin04/x404r-sdk 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.
- package/README.md +391 -0
- package/dist/ai/index.d.ts +21 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/index.js +344 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/backend/cloud.d.ts +91 -0
- package/dist/backend/cloud.d.ts.map +1 -0
- package/dist/backend/cloud.js +257 -0
- package/dist/backend/cloud.js.map +1 -0
- package/dist/backend/embedded.d.ts +79 -0
- package/dist/backend/embedded.d.ts.map +1 -0
- package/dist/backend/embedded.js +307 -0
- package/dist/backend/embedded.js.map +1 -0
- package/dist/backend/index.d.ts +11 -0
- package/dist/backend/index.d.ts.map +1 -0
- package/dist/backend/index.js +8 -0
- package/dist/backend/index.js.map +1 -0
- package/dist/backend/interface.d.ts +88 -0
- package/dist/backend/interface.d.ts.map +1 -0
- package/dist/backend/interface.js +11 -0
- package/dist/backend/interface.js.map +1 -0
- package/dist/chaos.d.ts +117 -0
- package/dist/chaos.d.ts.map +1 -0
- package/dist/chaos.js +215 -0
- package/dist/chaos.js.map +1 -0
- package/dist/client.d.ts +242 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +401 -0
- package/dist/client.js.map +1 -0
- package/dist/context.d.ts +18 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +65 -0
- package/dist/context.js.map +1 -0
- package/dist/durable.d.ts +90 -0
- package/dist/durable.d.ts.map +1 -0
- package/dist/durable.js +143 -0
- package/dist/durable.js.map +1 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +76 -0
- package/dist/index.js.map +1 -0
- package/dist/metrics.d.ts +279 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +693 -0
- package/dist/metrics.js.map +1 -0
- package/dist/types.d.ts +240 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/dist/worker.d.ts +74 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +269 -0
- package/dist/worker.js.map +1 -0
- package/dist/workflow.d.ts +48 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +190 -0
- package/dist/workflow.js.map +1 -0
- package/package.json +93 -0
package/dist/context.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Step Execution Context
|
|
3
|
+
* Provides checkpoint, logging, and AI access to step handlers
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Create a step context for task execution
|
|
7
|
+
*/
|
|
8
|
+
export function createStepContext(client, workflow, task, initialState = {}) {
|
|
9
|
+
let stepIndex = 0;
|
|
10
|
+
const state = { ...initialState };
|
|
11
|
+
const context = {
|
|
12
|
+
input: task.input,
|
|
13
|
+
state,
|
|
14
|
+
workflow: {
|
|
15
|
+
id: workflow.id,
|
|
16
|
+
name: workflow.name,
|
|
17
|
+
input: workflow.input,
|
|
18
|
+
},
|
|
19
|
+
task: {
|
|
20
|
+
id: task.id,
|
|
21
|
+
name: task.name,
|
|
22
|
+
attemptCount: task.attemptCount,
|
|
23
|
+
},
|
|
24
|
+
ai: client.ai,
|
|
25
|
+
checkpoint: async (newState) => {
|
|
26
|
+
if (newState) {
|
|
27
|
+
Object.assign(state, newState);
|
|
28
|
+
}
|
|
29
|
+
stepIndex++;
|
|
30
|
+
await client.createCheckpoint(task.id, stepIndex, state);
|
|
31
|
+
client.log(`Checkpoint created at step ${stepIndex}`, { taskId: task.id });
|
|
32
|
+
},
|
|
33
|
+
log: (message, data) => {
|
|
34
|
+
const prefix = `[${workflow.name}/${task.name}]`;
|
|
35
|
+
if (data) {
|
|
36
|
+
console.log(`${prefix} ${message}`, data);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
console.log(`${prefix} ${message}`);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
sleep: async (ms) => {
|
|
43
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
return context;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Restore context from checkpoint
|
|
50
|
+
*/
|
|
51
|
+
export async function restoreFromCheckpoint(client, workflow, task) {
|
|
52
|
+
const checkpoint = await client.getCheckpoint(task.id);
|
|
53
|
+
if (!checkpoint) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
client.log(`Resuming from checkpoint at step ${checkpoint.stepIndex}`, {
|
|
57
|
+
taskId: task.id,
|
|
58
|
+
});
|
|
59
|
+
const context = createStepContext(client, workflow, task, checkpoint.state);
|
|
60
|
+
return {
|
|
61
|
+
context,
|
|
62
|
+
resumeFromStep: checkpoint.stepIndex,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAe,EACf,QAAkB,EAClB,IAAU,EACV,eAAwC,EAAE;IAE1C,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,MAAM,KAAK,GAAG,EAAE,GAAG,YAAY,EAAE,CAAC;IAElC,MAAM,OAAO,GAAwB;QACnC,KAAK,EAAE,IAAI,CAAC,KAAe;QAC3B,KAAK;QAEL,QAAQ,EAAE;YACR,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,IAAI,EAAE,QAAQ,CAAC,IAAI;YACnB,KAAK,EAAE,QAAQ,CAAC,KAAK;SACtB;QAED,IAAI,EAAE;YACJ,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,YAAY,EAAE,IAAI,CAAC,YAAY;SAChC;QAED,EAAE,EAAE,MAAM,CAAC,E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AM,CAAC,GAAG,CAAC,oCAAoC,UAAU,CAAC,SAAS,EAAE,EAAE;QACrE,MAAM,EAAE,IAAI,CAAC,EAAE;KAChB,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,iBAAiB,CAC/B,MAAM,EACN,QAAQ,EACR,IAAI,EACJ,UAAU,CAAC,KAAK,CACjB,CAAC;IAEF,OAAO;QACL,OAAO;QACP,cAAc,EAAE,UAAU,CAAC,SAAS;KACrC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-Line Durable API
|
|
3
|
+
*
|
|
4
|
+
* Simplifies crash-proof execution to a single function call.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { durable } from '@x404-r/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const result = await durable('process-data', async (ctx) => {
|
|
11
|
+
* const data = await fetchData();
|
|
12
|
+
* await ctx.checkpoint('fetched');
|
|
13
|
+
*
|
|
14
|
+
* const processed = await processData(data);
|
|
15
|
+
* await ctx.checkpoint('processed');
|
|
16
|
+
*
|
|
17
|
+
* return processed;
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import type { AIConfig, AIProvider } from './types.js';
|
|
22
|
+
export interface DurableContext {
|
|
23
|
+
/** Create a named checkpoint (survives crashes) */
|
|
24
|
+
checkpoint: (name: string, state?: Record<string, unknown>) => Promise<void>;
|
|
25
|
+
/** Access persistent state */
|
|
26
|
+
state: Record<string, unknown>;
|
|
27
|
+
/** AI provider for LLM calls */
|
|
28
|
+
ai: AIProvider;
|
|
29
|
+
/** Log a message */
|
|
30
|
+
log: (message: string, data?: Record<string, unknown>) => void;
|
|
31
|
+
/** Sleep for specified milliseconds */
|
|
32
|
+
sleep: (ms: number) => Promise<void>;
|
|
33
|
+
/** Current execution info */
|
|
34
|
+
execution: {
|
|
35
|
+
id: string;
|
|
36
|
+
attemptCount: number;
|
|
37
|
+
checkpointName?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export interface DurableOptions {
|
|
41
|
+
/** CockroachDB connection string (or uses DATABASE_URL env) */
|
|
42
|
+
connectionString?: string;
|
|
43
|
+
/** AI provider configuration */
|
|
44
|
+
ai?: AIConfig;
|
|
45
|
+
/** Maximum retry attempts */
|
|
46
|
+
maxAttempts?: number;
|
|
47
|
+
/** Timeout in milliseconds */
|
|
48
|
+
timeout?: number;
|
|
49
|
+
/** Tenant ID for multi-tenancy */
|
|
50
|
+
tenantId?: string;
|
|
51
|
+
}
|
|
52
|
+
type DurableHandler<T> = (ctx: DurableContext) => Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Execute a function durably with automatic crash recovery.
|
|
55
|
+
*
|
|
56
|
+
* The function can call `ctx.checkpoint()` at any point to save progress.
|
|
57
|
+
* If the process crashes, execution resumes from the last checkpoint.
|
|
58
|
+
*
|
|
59
|
+
* @param name - Unique name for this durable function
|
|
60
|
+
* @param handler - The function to execute durably
|
|
61
|
+
* @param options - Configuration options
|
|
62
|
+
* @returns The result of the handler function
|
|
63
|
+
*/
|
|
64
|
+
export declare function durable<T>(name: string, handler: DurableHandler<T>, options?: DurableOptions): Promise<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Create a reusable durable function with preset options.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const processOrder = createDurable<{ orderId: string }, void>('process-order', {
|
|
71
|
+
* maxAttempts: 5,
|
|
72
|
+
* timeout: 60000,
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* // Later:
|
|
76
|
+
* await processOrder({ orderId: '123' }, async (ctx) => {
|
|
77
|
+
* // ... implementation
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function createDurable<TInput, TOutput>(name: string, options?: DurableOptions): (input: TInput, handler: (ctx: DurableContext & {
|
|
82
|
+
input: TInput;
|
|
83
|
+
}) => Promise<TOutput>) => Promise<TOutput>;
|
|
84
|
+
/**
|
|
85
|
+
* Close the global runtime connection.
|
|
86
|
+
* Call this when shutting down your application.
|
|
87
|
+
*/
|
|
88
|
+
export declare function closeDurable(): Promise<void>;
|
|
89
|
+
export {};
|
|
90
|
+
//# sourceMappingURL=durable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durable.d.ts","sourceRoot":"","sources":["../src/durable.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAe,MAAM,YAAY,CAAC;AAEpE,MAAM,WAAW,cAAc;IAC7B,mDAAmD;IACnD,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7E,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE/B,gCAAgC;IAChC,EAAE,EAAE,UAAU,CAAC;IAEf,oBAAoB;IACpB,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAE/D,uCAAuC;IACvC,KAAK,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAErC,6BAA6B;IAC7B,SAAS,EAAE;QACT,EAAE,EAAE,MAAM,CAAC;QACX,YAAY,EAAE,MAAM,CAAC;QACrB,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,CAAC;CACH;AAED,MAAM,WAAW,cAAc;IAC7B,+DAA+D;IAC/D,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B,gCAAgC;IAChC,EAAE,CAAC,EAAE,QAAQ,CAAC;IAEd,6BAA6B;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,kCAAkC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,KAAK,cAAc,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;AAyB7D;;;;;;;;;;GAUG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,EAC1B,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,CAAC,CAAC,CA4DZ;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,EAC3C,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,cAAc,GACvB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,cAAc,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAc7G;AAED;;;GAGG;AACH,wBAAsB,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAKlD"}
|
package/dist/durable.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-Line Durable API
|
|
3
|
+
*
|
|
4
|
+
* Simplifies crash-proof execution to a single function call.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { durable } from '@x404-r/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const result = await durable('process-data', async (ctx) => {
|
|
11
|
+
* const data = await fetchData();
|
|
12
|
+
* await ctx.checkpoint('fetched');
|
|
13
|
+
*
|
|
14
|
+
* const processed = await processData(data);
|
|
15
|
+
* await ctx.checkpoint('processed');
|
|
16
|
+
*
|
|
17
|
+
* return processed;
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { AgentDB } from './client.js';
|
|
22
|
+
// Global connection pool (reused across calls)
|
|
23
|
+
let globalRuntime = null;
|
|
24
|
+
async function getRuntime(options) {
|
|
25
|
+
if (globalRuntime) {
|
|
26
|
+
return globalRuntime;
|
|
27
|
+
}
|
|
28
|
+
const connectionString = options?.connectionString || process.env.DATABASE_URL;
|
|
29
|
+
if (!connectionString) {
|
|
30
|
+
throw new Error('DATABASE_URL environment variable or connectionString option required');
|
|
31
|
+
}
|
|
32
|
+
globalRuntime = new AgentDB({
|
|
33
|
+
connectionString,
|
|
34
|
+
tenantId: options?.tenantId,
|
|
35
|
+
ai: options?.ai,
|
|
36
|
+
});
|
|
37
|
+
await globalRuntime.ready();
|
|
38
|
+
return globalRuntime;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Execute a function durably with automatic crash recovery.
|
|
42
|
+
*
|
|
43
|
+
* The function can call `ctx.checkpoint()` at any point to save progress.
|
|
44
|
+
* If the process crashes, execution resumes from the last checkpoint.
|
|
45
|
+
*
|
|
46
|
+
* @param name - Unique name for this durable function
|
|
47
|
+
* @param handler - The function to execute durably
|
|
48
|
+
* @param options - Configuration options
|
|
49
|
+
* @returns The result of the handler function
|
|
50
|
+
*/
|
|
51
|
+
export async function durable(name, handler, options) {
|
|
52
|
+
const runtime = await getRuntime(options);
|
|
53
|
+
// Create a workflow with a single step
|
|
54
|
+
const workflow = runtime.workflow(name, {
|
|
55
|
+
version: '1.0.0',
|
|
56
|
+
steps: [
|
|
57
|
+
{
|
|
58
|
+
name: 'execute',
|
|
59
|
+
maxAttempts: options?.maxAttempts ?? 3,
|
|
60
|
+
timeout: options?.timeout,
|
|
61
|
+
handler: async (ctx) => {
|
|
62
|
+
// Create durable context wrapper
|
|
63
|
+
let checkpointIndex = 0;
|
|
64
|
+
const durableCtx = {
|
|
65
|
+
state: ctx.state,
|
|
66
|
+
ai: ctx.ai,
|
|
67
|
+
log: ctx.log,
|
|
68
|
+
sleep: ctx.sleep,
|
|
69
|
+
execution: {
|
|
70
|
+
id: ctx.task.id,
|
|
71
|
+
attemptCount: ctx.task.attemptCount,
|
|
72
|
+
checkpointName: ctx.state.__lastCheckpoint,
|
|
73
|
+
},
|
|
74
|
+
checkpoint: async (checkpointName, state) => {
|
|
75
|
+
checkpointIndex++;
|
|
76
|
+
await ctx.checkpoint({
|
|
77
|
+
...state,
|
|
78
|
+
__lastCheckpoint: checkpointName,
|
|
79
|
+
__checkpointIndex: checkpointIndex,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
return handler(durableCtx);
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
});
|
|
88
|
+
// Create worker and run
|
|
89
|
+
const worker = runtime.worker({ concurrency: 1 });
|
|
90
|
+
worker.register(workflow);
|
|
91
|
+
// Start worker in background
|
|
92
|
+
const workerPromise = worker.start();
|
|
93
|
+
try {
|
|
94
|
+
// Run workflow and wait for completion
|
|
95
|
+
const result = await workflow.run({}, { wait: true, timeout: options?.timeout });
|
|
96
|
+
if (result.status === 'failed') {
|
|
97
|
+
throw new Error(result.error || 'Durable function failed');
|
|
98
|
+
}
|
|
99
|
+
return result.output;
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
// Stop worker
|
|
103
|
+
await worker.stop();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a reusable durable function with preset options.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const processOrder = createDurable<{ orderId: string }, void>('process-order', {
|
|
112
|
+
* maxAttempts: 5,
|
|
113
|
+
* timeout: 60000,
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* // Later:
|
|
117
|
+
* await processOrder({ orderId: '123' }, async (ctx) => {
|
|
118
|
+
* // ... implementation
|
|
119
|
+
* });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export function createDurable(name, options) {
|
|
123
|
+
return async (input, handler) => {
|
|
124
|
+
return durable(name, async (ctx) => {
|
|
125
|
+
const contextWithInput = {
|
|
126
|
+
...ctx,
|
|
127
|
+
input,
|
|
128
|
+
};
|
|
129
|
+
return handler(contextWithInput);
|
|
130
|
+
}, options);
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Close the global runtime connection.
|
|
135
|
+
* Call this when shutting down your application.
|
|
136
|
+
*/
|
|
137
|
+
export async function closeDurable() {
|
|
138
|
+
if (globalRuntime) {
|
|
139
|
+
await globalRuntime.close();
|
|
140
|
+
globalRuntime = null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=durable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"durable.js","sourceRoot":"","sources":["../src/durable.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AA8CtC,+CAA+C;AAC/C,IAAI,aAAa,GAAmB,IAAI,CAAC;AAEzC,KAAK,UAAU,UAAU,CAAC,OAAwB;IAChD,IAAI,aAAa,EAAE,CAAC;QAClB,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,MAAM,gBAAgB,GAAG,OAAO,EAAE,gBAAgB,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;IAC/E,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,uEAAuE,CAAC,CAAC;IAC3F,CAAC;IAED,aAAa,GAAG,IAAI,OAAO,CAAC;QAC1B,gBAAgB;QAChB,QAAQ,EAAE,OAAO,EAAE,QAAQ;QAC3B,EAAE,EAAE,OAAO,EAAE,EAAE;KAChB,CAAC,CAAC;IAEH,MAAM,aA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AC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x404-r SDK
|
|
3
|
+
* The runtime where context is never lost
|
|
4
|
+
* Database-native infrastructure for crash-proof AI agents
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { x404r } from '@x404-r/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const runtime = await new x404r({
|
|
11
|
+
* connectionString: process.env.DATABASE_URL,
|
|
12
|
+
* ai: {
|
|
13
|
+
* provider: 'gemini',
|
|
14
|
+
* apiKey: process.env.GEMINI_API_KEY,
|
|
15
|
+
* },
|
|
16
|
+
* }).ready();
|
|
17
|
+
*
|
|
18
|
+
* // Define a crash-proof workflow
|
|
19
|
+
* const refactor = runtime.workflow('refactor-code', {
|
|
20
|
+
* steps: [
|
|
21
|
+
* {
|
|
22
|
+
* name: 'analyze',
|
|
23
|
+
* handler: async (ctx) => {
|
|
24
|
+
* const analysis = await ctx.ai.generate(
|
|
25
|
+
* `Analyze this code: ${ctx.input.code}`
|
|
26
|
+
* );
|
|
27
|
+
* return { analysis };
|
|
28
|
+
* },
|
|
29
|
+
* },
|
|
30
|
+
* {
|
|
31
|
+
* name: 'refactor',
|
|
32
|
+
* dependsOn: ['analyze'],
|
|
33
|
+
* handler: async (ctx) => {
|
|
34
|
+
* // Checkpoint after each file
|
|
35
|
+
* for (const file of ctx.input.files) {
|
|
36
|
+
* await ctx.checkpoint({ lastFile: file });
|
|
37
|
+
* // If we crash here, we resume from checkpoint
|
|
38
|
+
* }
|
|
39
|
+
* return { success: true };
|
|
40
|
+
* },
|
|
41
|
+
* },
|
|
42
|
+
* ],
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Start a worker (stateless - can crash anytime)
|
|
46
|
+
* const worker = runtime.worker({ concurrency: 5 });
|
|
47
|
+
* worker.register(refactor);
|
|
48
|
+
* await worker.start();
|
|
49
|
+
*
|
|
50
|
+
* // Run a workflow
|
|
51
|
+
* const result = await refactor.run({
|
|
52
|
+
* code: 'function hello() { return "world"; }',
|
|
53
|
+
* files: ['src/index.ts'],
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export { AgentDB, x404r } from './client.js';
|
|
58
|
+
export type { Backend } from './backend/index.js';
|
|
59
|
+
export { EmbeddedBackend, CloudBackend, isEmbeddedBackend } from './backend/index.js';
|
|
60
|
+
export type { EmbeddedBackendConfig, CloudBackendConfig } from './backend/index.js';
|
|
61
|
+
export { WorkflowBuilder } from './workflow.js';
|
|
62
|
+
export { Worker } from './worker.js';
|
|
63
|
+
export { createStepContext, restoreFromCheckpoint } from './context.js';
|
|
64
|
+
export { createAIProvider, MockAIProvider, estimateCost } from './ai/index.js';
|
|
65
|
+
export { durable, createDurable, closeDurable } from './durable.js';
|
|
66
|
+
export type { DurableContext, DurableOptions } from './durable.js';
|
|
67
|
+
export { ChaosEngine, ChaosError, createChaosCheckpoint, withChaos, isChaosEnabled, DEFAULT_CHAOS_CONFIG, STRESS_CHAOS_CONFIG, } from './chaos.js';
|
|
68
|
+
export type { ChaosConfig, ChaosFailureType } from './chaos.js';
|
|
69
|
+
export { MetricsCollector, Histogram, RateCalculator, metrics, observe, withMetrics, } from './metrics.js';
|
|
70
|
+
export type { MetricEvent, MetricsSummary, MetricsDatabaseConfig, ExecutionMetrics, CostMetrics, PerformanceMetrics, ReliabilityMetrics, AIMetrics, MemoryMetrics, } from './metrics.js';
|
|
71
|
+
export type { Workflow, WorkflowStatus, Task, TaskStatus, Checkpoint, WorkflowDefinition, StepDefinition, StepHandler, StepContext, AIProvider, AIConfig, GenerateOptions, JSONSchema, TokenUsage, CostEstimate, GenerateResult, X404rConfig, EmbeddedConfig, CloudConfig, AgentDBConfig, // deprecated alias
|
|
72
|
+
WorkerConfig, WorkflowEvent, EventHandler, RunOptions, RunResult, } from './types.js';
|
|
73
|
+
export { isEmbeddedConfig, isCloudConfig } from './types.js';
|
|
74
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAGH,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAG7C,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACtF,YAAY,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAGpF,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAGhD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAGxE,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAG/E,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACpE,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnE,OAAO,EACL,WAAW,EACX,UAAU,EACV,qBAAqB,EACrB,SAAS,EACT,cAAc,EACd,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGhE,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,cAAc,EACd,OAAO,EACP,OAAO,EACP,WAAW,GACZ,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,WAAW,EACX,cAAc,EACd,qBAAqB,EACrB,gBAAgB,EAChB,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,SAAS,EACT,aAAa,GACd,MAAM,cAAc,CAAC;AAGtB,YAAY,EAEV,QAAQ,EACR,cAAc,EACd,IAAI,EACJ,UAAU,EACV,UAAU,EAGV,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,WAAW,EAGX,UAAU,EACV,QAAQ,EACR,eAAe,EACf,UAAU,EAGV,UAAU,EACV,YAAY,EACZ,cAAc,EAGd,WAAW,EACX,cAAc,EACd,WAAW,EACX,aAAa,EAAE,mBAAmB;AAClC,YAAY,EAGZ,aAAa,EACb,YAAY,EAGZ,UAAU,EACV,SAAS,GACV,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x404-r SDK
|
|
3
|
+
* The runtime where context is never lost
|
|
4
|
+
* Database-native infrastructure for crash-proof AI agents
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { x404r } from '@x404-r/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const runtime = await new x404r({
|
|
11
|
+
* connectionString: process.env.DATABASE_URL,
|
|
12
|
+
* ai: {
|
|
13
|
+
* provider: 'gemini',
|
|
14
|
+
* apiKey: process.env.GEMINI_API_KEY,
|
|
15
|
+
* },
|
|
16
|
+
* }).ready();
|
|
17
|
+
*
|
|
18
|
+
* // Define a crash-proof workflow
|
|
19
|
+
* const refactor = runtime.workflow('refactor-code', {
|
|
20
|
+
* steps: [
|
|
21
|
+
* {
|
|
22
|
+
* name: 'analyze',
|
|
23
|
+
* handler: async (ctx) => {
|
|
24
|
+
* const analysis = await ctx.ai.generate(
|
|
25
|
+
* `Analyze this code: ${ctx.input.code}`
|
|
26
|
+
* );
|
|
27
|
+
* return { analysis };
|
|
28
|
+
* },
|
|
29
|
+
* },
|
|
30
|
+
* {
|
|
31
|
+
* name: 'refactor',
|
|
32
|
+
* dependsOn: ['analyze'],
|
|
33
|
+
* handler: async (ctx) => {
|
|
34
|
+
* // Checkpoint after each file
|
|
35
|
+
* for (const file of ctx.input.files) {
|
|
36
|
+
* await ctx.checkpoint({ lastFile: file });
|
|
37
|
+
* // If we crash here, we resume from checkpoint
|
|
38
|
+
* }
|
|
39
|
+
* return { success: true };
|
|
40
|
+
* },
|
|
41
|
+
* },
|
|
42
|
+
* ],
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Start a worker (stateless - can crash anytime)
|
|
46
|
+
* const worker = runtime.worker({ concurrency: 5 });
|
|
47
|
+
* worker.register(refactor);
|
|
48
|
+
* await worker.start();
|
|
49
|
+
*
|
|
50
|
+
* // Run a workflow
|
|
51
|
+
* const result = await refactor.run({
|
|
52
|
+
* code: 'function hello() { return "world"; }',
|
|
53
|
+
* files: ['src/index.ts'],
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
// Main client
|
|
58
|
+
export { AgentDB, x404r } from './client.js';
|
|
59
|
+
export { EmbeddedBackend, CloudBackend, isEmbeddedBackend } from './backend/index.js';
|
|
60
|
+
// Workflow builder
|
|
61
|
+
export { WorkflowBuilder } from './workflow.js';
|
|
62
|
+
// Worker
|
|
63
|
+
export { Worker } from './worker.js';
|
|
64
|
+
// Context
|
|
65
|
+
export { createStepContext, restoreFromCheckpoint } from './context.js';
|
|
66
|
+
// AI providers
|
|
67
|
+
export { createAIProvider, MockAIProvider, estimateCost } from './ai/index.js';
|
|
68
|
+
// One-line durable API
|
|
69
|
+
export { durable, createDurable, closeDurable } from './durable.js';
|
|
70
|
+
// Chaos engineering
|
|
71
|
+
export { ChaosEngine, ChaosError, createChaosCheckpoint, withChaos, isChaosEnabled, DEFAULT_CHAOS_CONFIG, STRESS_CHAOS_CONFIG, } from './chaos.js';
|
|
72
|
+
// Metrics & Observability
|
|
73
|
+
export { MetricsCollector, Histogram, RateCalculator, metrics, observe, withMetrics, } from './metrics.js';
|
|
74
|
+
// Type guards
|
|
75
|
+
export { isEmbeddedConfig, isCloudConfig } from './types.js';
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAEH,cAAc;AACd,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAI7C,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAGtF,mBAAmB;AACnB,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,SAAS;AACT,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,UAAU;AACV,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAExE,eAAe;AACf,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE/E,uBAAuB;AACvB,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGpE,oBAAoB;AACpB,OAAO,EACL,WAAW,EACX,UAAU,EACV,qBAAqB,EACrB,SAAS,EACT,cAAc,EACd,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,YAAY,CAAC;AAGpB,0BAA0B;AAC1B,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,cAAc,EACd,OAAO,EACP,OAAO,EACP,WAAW,GACZ,MAAM,cAAc,CAAC;AAuDtB,cAAc;AACd,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
|