transactional-ai 0.2.3 → 0.2.4
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 +78 -111
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,69 +7,33 @@ Implement the Saga Pattern with persistent rollback and state recovery for Long-
|
|
|
7
7
|
[](https://opensource.org/licenses/MIT)
|
|
8
8
|
[](https://github.com/Grafikui/Transactional-ai/actions/workflows/ci.yml)
|
|
9
9
|
|
|
10
|
-
## Why This Is Called Transactional AI
|
|
11
|
-
|
|
12
|
-
This library does **not** wrap an LLM and does **not** claim that “AI” is making decisions inside the engine.
|
|
13
|
-
|
|
14
|
-
The term **Transactional AI** describes the *problem domain*, not the implementation.
|
|
15
|
-
|
|
16
|
-
Modern AI systems, agents, and LLM-driven workflows introduce the worst possible failure conditions:
|
|
17
|
-
- Long-running, multi-step execution
|
|
18
|
-
- External side effects (payments, emails, file writes, API calls)
|
|
19
|
-
- Retries, timeouts, and partial completion
|
|
20
|
-
- Process crashes and restarts
|
|
21
|
-
- Non-deterministic outputs that cannot simply be re-run
|
|
22
|
-
|
|
23
|
-
Naive async code is unsafe in this environment.
|
|
24
|
-
|
|
25
|
-
`transactional-ai` provides a **transactional execution layer** for these AI-adjacent workflows:
|
|
26
|
-
- Deterministic step execution
|
|
27
|
-
- Durable state persistence
|
|
28
|
-
- Automatic compensation (rollback)
|
|
29
|
-
- Safe retries and resumability after failure
|
|
30
|
-
|
|
31
|
-
In short:
|
|
32
|
-
> **AI is the workload. Transactional guarantees are the missing infrastructure.**
|
|
33
|
-
|
|
34
|
-
That is what this library provides.
|
|
35
|
-
|
|
36
|
-
|
|
37
10
|
**[Live Demo](https://grafikui.github.io/Transactional-ai/)** | [Documentation](#quick-start) | [Examples](#advanced-usage)
|
|
38
11
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
This library does **not** wrap an LLM and does **not** claim that “AI” is making decisions inside the engine.
|
|
42
|
-
|
|
43
|
-
The term **Transactional AI** describes the *problem domain*, not the implementation.
|
|
44
|
-
|
|
45
|
-
Modern AI systems, agents, and LLM-driven workflows introduce the worst possible failure conditions:
|
|
46
|
-
- Long-running, multi-step execution
|
|
47
|
-
- External side effects (payments, emails, file writes, API calls)
|
|
48
|
-
- Retries, timeouts, and partial completion
|
|
49
|
-
- Process crashes and restarts
|
|
50
|
-
- Non-deterministic outputs that cannot simply be re-run
|
|
51
|
-
|
|
52
|
-
Naive async code is unsafe in this environment.
|
|
12
|
+
---
|
|
53
13
|
|
|
54
|
-
|
|
55
|
-
- Deterministic step execution
|
|
56
|
-
- Durable state persistence
|
|
57
|
-
- Automatic compensation (rollback)
|
|
58
|
-
- Safe retries and resumability after failure
|
|
14
|
+
## 🧠 Why "Transactional AI"?
|
|
59
15
|
|
|
60
|
-
|
|
61
|
-
> **AI is the workload. Transactional guarantees are the missing infrastructure.**
|
|
16
|
+
> **Clarification:** This library does **not** wrap an LLM. It does **not** claim that "AI" is making decisions inside the engine.
|
|
62
17
|
|
|
63
|
-
|
|
18
|
+
The term *Transactional AI* describes the **problem domain**, not the implementation.
|
|
64
19
|
|
|
20
|
+
Modern AI systems, agents, and LLM-driven workflows introduce the worst possible failure conditions:
|
|
21
|
+
* **Long-running execution:** Processes that take minutes, not milliseconds.
|
|
22
|
+
* **External side effects:** Payments, emails, file writes, API calls.
|
|
23
|
+
* **Instability:** Retries, timeouts, partial completions, and hallucinations.
|
|
24
|
+
* **Non-determinism:** Outputs that cannot simply be re-run blindly.
|
|
65
25
|
|
|
66
|
-
|
|
26
|
+
**Naive async code is unsafe in this environment.**
|
|
67
27
|
|
|
68
|
-
|
|
28
|
+
`transactional-ai` provides the **transactional execution layer** for these workflows:
|
|
29
|
+
* ✅ **Deterministic** step execution
|
|
30
|
+
* ✅ **Durable** state persistence
|
|
31
|
+
* ✅ **Automatic** compensation (rollback)
|
|
32
|
+
* ✅ **Safe** retries and resumability after failure
|
|
69
33
|
|
|
70
|
-
|
|
34
|
+
**In short:** AI is the workload. Transactional guarantees are the missing infrastructure.
|
|
71
35
|
|
|
72
|
-
|
|
36
|
+
---
|
|
73
37
|
|
|
74
38
|
## Interactive Demo
|
|
75
39
|
|
|
@@ -95,17 +59,17 @@ npm run demo
|
|
|
95
59
|
npm run showcase -- --all
|
|
96
60
|
```
|
|
97
61
|
|
|
98
|
-
---
|
|
99
|
-
|
|
100
62
|
## Why use this?
|
|
101
63
|
|
|
102
|
-
AI Agents are flaky. Steps fail, APIs time out, and hallucinations happen.
|
|
103
|
-
|
|
64
|
+
AI Agents are flaky. Steps fail, APIs time out, and hallucinations happen. transactional-ai gives you:
|
|
65
|
+
|
|
66
|
+
- Automatic Rollbacks: If step 3 fails, steps 2 and 1 are compensated (undone) automatically.
|
|
67
|
+
|
|
68
|
+
- Concurrency Safety: Distributed locking prevents race conditions when scaling workers.
|
|
104
69
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
4. **Resilience**: Built-in retry policies for flaky LLM APIs.
|
|
70
|
+
- Persistence: Transactions survive process crashes using Redis, Postgres, or File storage.
|
|
71
|
+
|
|
72
|
+
- Resilience: Built-in retry policies for flaky LLM APIs.
|
|
109
73
|
|
|
110
74
|
## Installation
|
|
111
75
|
|
|
@@ -113,11 +77,9 @@ AI Agents are flaky. Steps fail, APIs time out, and hallucinations happen.
|
|
|
113
77
|
npm install transactional-ai
|
|
114
78
|
```
|
|
115
79
|
|
|
116
|
-
---
|
|
117
|
-
|
|
118
80
|
## Quick Start
|
|
119
81
|
|
|
120
|
-
|
|
82
|
+
1. The "Litmus Test" (Basic Usage)
|
|
121
83
|
|
|
122
84
|
Define a transaction where every action has a compensating rollback action.
|
|
123
85
|
|
|
@@ -155,7 +117,7 @@ agent
|
|
|
155
117
|
await agent.run({ initialData: "foo" });
|
|
156
118
|
```
|
|
157
119
|
|
|
158
|
-
|
|
120
|
+
## Adding Persistence (Redis)
|
|
159
121
|
|
|
160
122
|
To survive process crashes, simply provide a storage adapter.
|
|
161
123
|
|
|
@@ -180,11 +142,11 @@ await agent.run();
|
|
|
180
142
|
|
|
181
143
|
## Enterprise Features (v0.2.0)
|
|
182
144
|
|
|
183
|
-
###
|
|
145
|
+
### Distributed Locking (Redis)
|
|
184
146
|
|
|
185
147
|
Prevent race conditions when running multiple agent workers on the same Transaction ID.
|
|
186
148
|
|
|
187
|
-
```
|
|
149
|
+
```typescript
|
|
188
150
|
import { Transaction, RedisStorage, RedisLock } from 'transactional-ai';
|
|
189
151
|
|
|
190
152
|
const connection = 'redis://localhost:6379';
|
|
@@ -200,30 +162,29 @@ const agent = new Transaction('tx-unique-id', storage, {
|
|
|
200
162
|
await agent.run();
|
|
201
163
|
```
|
|
202
164
|
|
|
203
|
-
###
|
|
165
|
+
### Postgres Storage (SQL)
|
|
204
166
|
|
|
205
167
|
Use Postgres for strict ACID compliance and auditability.
|
|
206
168
|
|
|
207
169
|
Prerequisite: Run schema.sql in your database.
|
|
208
170
|
|
|
209
|
-
```
|
|
171
|
+
```typescript
|
|
210
172
|
import { Transaction, PostgresStorage } from 'transactional-ai';
|
|
211
173
|
|
|
212
174
|
const storage = new PostgresStorage('postgresql://user:pass@localhost:5432/mydb');
|
|
213
175
|
const agent = new Transaction('tx-id', storage);
|
|
214
176
|
```
|
|
215
177
|
|
|
216
|
-
|
|
217
|
-
|
|
178
|
+
## Setting Up Postgres
|
|
218
179
|
Before using PostgresStorage, create the required table in your database:
|
|
219
180
|
|
|
220
|
-
|
|
221
|
-
```bash
|
|
181
|
+
Option 1: Run the provided schema
|
|
182
|
+
``` bash
|
|
222
183
|
# From project root
|
|
223
184
|
psql -U your_user -d your_database -f schema.sql
|
|
224
185
|
```
|
|
225
186
|
|
|
226
|
-
|
|
187
|
+
## Option 2: Copy-paste this SQL
|
|
227
188
|
```sql
|
|
228
189
|
CREATE TABLE IF NOT EXISTS transactions (
|
|
229
190
|
id VARCHAR(255) PRIMARY KEY,
|
|
@@ -236,13 +197,13 @@ CREATE TABLE IF NOT EXISTS transactions (
|
|
|
236
197
|
CREATE INDEX IF NOT EXISTS idx_transactions_status ON transactions(status);
|
|
237
198
|
```
|
|
238
199
|
|
|
239
|
-
**Important
|
|
200
|
+
* **Important**: The table must exist before running your first transaction. It will NOT be auto-created.
|
|
240
201
|
|
|
241
|
-
###
|
|
202
|
+
### Retry Policies
|
|
242
203
|
|
|
243
204
|
Handle flaky APIs (e.g., OpenAI 500 errors) automatically without failing the transaction.
|
|
244
205
|
|
|
245
|
-
```
|
|
206
|
+
```typescript
|
|
246
207
|
agent.step({
|
|
247
208
|
name: 'generate-text',
|
|
248
209
|
execute: async () => await openai.createCompletion(...),
|
|
@@ -254,7 +215,7 @@ agent.step({
|
|
|
254
215
|
});
|
|
255
216
|
```
|
|
256
217
|
|
|
257
|
-
### 4. Step Timeouts
|
|
218
|
+
### 4. Step Timeouts
|
|
258
219
|
|
|
259
220
|
Prevent steps from hanging indefinitely with configurable timeouts.
|
|
260
221
|
|
|
@@ -266,10 +227,8 @@ await tx.step('call-external-api', {
|
|
|
266
227
|
});
|
|
267
228
|
```
|
|
268
229
|
|
|
269
|
-
###
|
|
270
|
-
|
|
230
|
+
### Observability Hooks
|
|
271
231
|
Integrate with your logging, metrics, and alerting systems using event hooks.
|
|
272
|
-
|
|
273
232
|
```typescript
|
|
274
233
|
import { Transaction, TransactionEvents } from 'transactional-ai';
|
|
275
234
|
|
|
@@ -290,14 +249,25 @@ const events: TransactionEvents = {
|
|
|
290
249
|
const tx = new Transaction('workflow-123', storage, { events });
|
|
291
250
|
```
|
|
292
251
|
|
|
293
|
-
|
|
294
|
-
- `onTransactionStart`, `onTransactionComplete`, `onTransactionFailed`
|
|
295
|
-
- `onStepStart`, `onStepComplete`, `onStepFailed`, `onStepRetry`, `onStepSkipped`, `onStepTimeout`
|
|
296
|
-
- `onCompensationStart`, `onCompensationComplete`, `onCompensationFailed`
|
|
252
|
+
## Available Events
|
|
297
253
|
|
|
298
|
-
|
|
254
|
+
| Event Name | Description |
|
|
255
|
+
|------------------------------|------------------------------------------------|
|
|
256
|
+
| onTransactionStart | Triggered when a transaction begins |
|
|
257
|
+
| onTransactionComplete | Triggered when a transaction completes |
|
|
258
|
+
| onTransactionFailed | Triggered when a transaction fails |
|
|
259
|
+
| onStepStart | Triggered when an individual step starts |
|
|
260
|
+
| onStepComplete | Triggered when an individual step completes |
|
|
261
|
+
| onStepFailed | Triggered when an individual step fails |
|
|
262
|
+
| onStepRetry | Triggered when a step is retried |
|
|
263
|
+
| onStepSkipped | Triggered when a step is skipped |
|
|
264
|
+
| onStepTimeout | Triggered when a step exceeds its timeout |
|
|
265
|
+
| onCompensationStart | Triggered when compensation (rollback) begins |
|
|
266
|
+
| onCompensationComplete | Triggered when compensation completes |
|
|
267
|
+
| onCompensationFailed | Triggered when compensation fails |
|
|
299
268
|
|
|
300
|
-
|
|
269
|
+
|
|
270
|
+
**See full example:** `npm run example:observability`
|
|
301
271
|
|
|
302
272
|
## CLI Inspector
|
|
303
273
|
|
|
@@ -314,7 +284,7 @@ npx tai-inspect workflow-id-555
|
|
|
314
284
|
|
|
315
285
|
Output:
|
|
316
286
|
|
|
317
|
-
```
|
|
287
|
+
```bash
|
|
318
288
|
🔍 Inspecting: workflow-id-555
|
|
319
289
|
Source: RedisStorage
|
|
320
290
|
|
|
@@ -326,11 +296,9 @@ Output:
|
|
|
326
296
|
└── (comp) create-f..| ✅ completed
|
|
327
297
|
```
|
|
328
298
|
|
|
329
|
-
---
|
|
330
|
-
|
|
331
299
|
## Advanced Usage
|
|
332
300
|
|
|
333
|
-
|
|
301
|
+
**Audit Mode (Governance)**
|
|
334
302
|
|
|
335
303
|
By default, logs are cleared upon success to save storage space. To keep a permanent audit trail for compliance (e.g., "Why did the agent do this?"), enable Audit Mode:
|
|
336
304
|
|
|
@@ -340,7 +308,7 @@ const agent = new Transaction("id", storage, {
|
|
|
340
308
|
});
|
|
341
309
|
```
|
|
342
310
|
|
|
343
|
-
|
|
311
|
+
## Manual Rollbacks
|
|
344
312
|
|
|
345
313
|
The library handles rollbacks automatically on error. You can trigger them manually by throwing an error inside any step:
|
|
346
314
|
|
|
@@ -362,7 +330,7 @@ agent.step({
|
|
|
362
330
|
});
|
|
363
331
|
```
|
|
364
332
|
|
|
365
|
-
|
|
333
|
+
## Testing Utilities
|
|
366
334
|
|
|
367
335
|
Write fast, isolated tests for your transaction workflows with built-in testing utilities.
|
|
368
336
|
|
|
@@ -400,37 +368,36 @@ describe('My Workflow', () => {
|
|
|
400
368
|
});
|
|
401
369
|
```
|
|
402
370
|
|
|
403
|
-
|
|
404
|
-
- `MemoryStorage` - Fast in-memory storage for tests
|
|
405
|
-
- `MockLock` - Simulated distributed lock
|
|
406
|
-
- `createEventSpy()` - Track event emissions
|
|
371
|
+
## Testing utilities:
|
|
407
372
|
|
|
408
|
-
|
|
373
|
+
- MemoryStorage - Fast in-memory storage for tests
|
|
409
374
|
|
|
410
|
-
|
|
375
|
+
- MockLock - Simulated distributed lock
|
|
411
376
|
|
|
412
|
-
|
|
377
|
+
- createEventSpy() - Track event emissions
|
|
413
378
|
|
|
414
|
-
|
|
379
|
+
## Roadmap
|
|
415
380
|
|
|
416
|
-
[x]
|
|
381
|
+
- [x] Core Saga Engine (Execute/Compensate)
|
|
417
382
|
|
|
418
|
-
[x]
|
|
383
|
+
- [x] Persistence Adapters (File, Redis)
|
|
419
384
|
|
|
420
|
-
[x]
|
|
385
|
+
- [x] Resumability (Skip completed steps)
|
|
421
386
|
|
|
422
|
-
[x]
|
|
387
|
+
- [x] CLI Inspector (tai-inspect)
|
|
423
388
|
|
|
424
|
-
[x]
|
|
389
|
+
- [x] Concurrent Transaction Locking
|
|
425
390
|
|
|
426
|
-
[x]
|
|
391
|
+
- [x] Postgres/SQL Storage Adapter
|
|
427
392
|
|
|
428
|
-
[x]
|
|
393
|
+
- [x] Step Retry Policies
|
|
429
394
|
|
|
430
|
-
[x]
|
|
395
|
+
- [x] Step Timeouts (v0.2.1)
|
|
431
396
|
|
|
432
|
-
|
|
397
|
+
- [x] Observability Event Hooks (v0.2.1)
|
|
398
|
+
|
|
399
|
+
- [x] Testing Utilities (v0.2.1)
|
|
433
400
|
|
|
434
401
|
## License
|
|
435
402
|
|
|
436
|
-
MIT
|
|
403
|
+
MIT
|