@intentproof/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/LICENSE +201 -0
- package/README.md +443 -0
- package/dist/index.cjs +783 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +243 -0
- package/dist/index.d.ts +243 -0
- package/dist/index.js +744 -0
- package/dist/index.js.map +1 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 The IntentProof Authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
# @intentproof/sdk
|
|
2
|
+
|
|
3
|
+
**IntentProof** turns function calls into **verifiable records** of what your system **meant** to do and what **actually happened**—**intent**, **action**, and **outcome** on the same wire. That is not another log stream: logs narrate; **IntentProof** gives you **proof** you can reconcile, attest to, or feed a verifier, because every record is tied to a real invocation with structured inputs, a stable label pair, and a completed result or error snapshot.
|
|
4
|
+
|
|
5
|
+
This package is the Node.js / TypeScript SDK: **`IntentProofClient.wrap`** is the bridge from your code to those records. You keep your functions; each call through the wrapper emits one canonical **`ExecutionEvent`** for export.
|
|
6
|
+
|
|
7
|
+
## What this SDK does
|
|
8
|
+
|
|
9
|
+
Each such invocation produces one **`ExecutionEvent`**: **`intent`** / **`action`** (what you declared this call represents), JSON-safe **`inputs`** and **`output`** (or an **`error`** snapshot for what occurred), **`startedAt`** / **`completedAt`**, **`durationMs`**, a unique **`id`**, optional **`correlationId`** (async context or wrap options), and merged **`attributes`**. Serialization is configurable (`snapshot`, redaction, custom capture hooks) so the proof stays bounded and policy-aware.
|
|
10
|
+
|
|
11
|
+
Events are delivered to every configured **`Exporter`** (`export(event)`). Built-ins cover an in-memory ring buffer, HTTP ingest, and a bounded async queue. If an exporter throws or rejects, **`onExporterError`** is notified; the wrapped function’s return value or thrown error is unchanged—your runtime behavior stays authoritative; the record is best-effort delivery.
|
|
12
|
+
|
|
13
|
+
The wire shape stays small and canonical so verifiers and ingest paths reason about **intent / action / outcome**, not ad hoc log lines or internal object graphs.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- **Node.js** 22 or newer
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @intentproof/sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
Below, the object printed from **`getEvents()`** is the **proof artifact** for a single call: intent and action you chose at wrap time, inputs and output the runtime saw, plus timing and id.
|
|
28
|
+
|
|
29
|
+
Use a **`MemoryExporter`** so you can inspect that record immediately (the default singleton client also uses memory, but you need your own instance to call **`getEvents()`**).
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { createIntentProofClient, MemoryExporter } from "@intentproof/sdk";
|
|
33
|
+
|
|
34
|
+
const memory = new MemoryExporter({ maxEvents: 50 });
|
|
35
|
+
const client = createIntentProofClient({ exporters: [memory] });
|
|
36
|
+
|
|
37
|
+
const add = client.wrap(
|
|
38
|
+
{ intent: "demo", action: "math.add" },
|
|
39
|
+
(a: number, b: number) => a + b,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const sum = add(2, 3);
|
|
43
|
+
console.assert(sum === 5);
|
|
44
|
+
|
|
45
|
+
const [event] = memory.getEvents();
|
|
46
|
+
console.log(event);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Calling **`add(2, 3)`** runs your original function and, after it returns, emits one event per exporter. A typical emitted object looks like this (values vary at runtime):
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
|
54
|
+
"intent": "demo",
|
|
55
|
+
"action": "math.add",
|
|
56
|
+
"inputs": [2, 3],
|
|
57
|
+
"status": "ok",
|
|
58
|
+
"output": 5,
|
|
59
|
+
"startedAt": "2026-05-02T12:00:00.000Z",
|
|
60
|
+
"completedAt": "2026-05-02T12:00:00.003Z",
|
|
61
|
+
"durationMs": 3
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- **`id`** — Random UUID for this invocation.
|
|
66
|
+
- **`inputs` / `output`** — Produced via **`snapshot()`** (see `SerializeOptions` / `WrapOptions`) unless you override with **`captureInput`** / **`captureOutput`**.
|
|
67
|
+
- **`correlationId`** — Omitted here; present when async context or wrap options supply one (see examples below).
|
|
68
|
+
- **`attributes`** — Omitted when empty; otherwise merged from client **`defaultAttributes`** and per-wrap **`attributes`**.
|
|
69
|
+
|
|
70
|
+
## `IntentProofClient` API
|
|
71
|
+
|
|
72
|
+
| Member | Description |
|
|
73
|
+
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
74
|
+
| **`constructor(config?)`** | Creates a client. Default exporters: a single **`MemoryExporter`** if you omit **`config.exporters`**. |
|
|
75
|
+
| **`configure(config)`** | Re-applies **`IntentProofConfig`** fields (exporters, error hook, defaults, stack policy). |
|
|
76
|
+
| **`wrap(options, fn)`** | Returns a function that records one **`ExecutionEvent`** per call (sync or async). **`options`** must satisfy **`assertWrapOptionsShape`** (`intent` / `action` non-empty strings, etc.). |
|
|
77
|
+
| **`flush()`** | Awaits optional **`Exporter.flush`** on all exporters in parallel. |
|
|
78
|
+
| **`shutdown()`** | Awaits **`Exporter.shutdown`** when defined, otherwise **`flush`**. |
|
|
79
|
+
| **`getCorrelationId()`** | Returns the correlation id from **`AsyncLocalStorage`**, if any. |
|
|
80
|
+
| **`withCorrelation(fn)`** | Runs **`fn`** with a **fresh UUID** as correlation id for nested wraps. |
|
|
81
|
+
| **`withCorrelation(id, fn)`** | Runs **`fn`** with **`id`** trimmed; blank / whitespace-only **`id`** falls back to a UUID. |
|
|
82
|
+
|
|
83
|
+
### Module-level helpers (same module as the client)
|
|
84
|
+
|
|
85
|
+
These use the same async correlation store as **`IntentProofClient`** instances:
|
|
86
|
+
|
|
87
|
+
| Export | Description |
|
|
88
|
+
| -------------------------------------- | ---------------------------------------------------------------------- |
|
|
89
|
+
| **`createIntentProofClient(config?)`** | New isolated client (tests, workers, multi-tenant). |
|
|
90
|
+
| **`getIntentProofClient()`** | Lazy singleton used by **`client`**. |
|
|
91
|
+
| **`client`** | Default singleton instance. |
|
|
92
|
+
| **`getCorrelationId()`** | Same behavior as the instance method. |
|
|
93
|
+
| **`runWithCorrelationId(id, fn)`** | Requires a **non-empty** correlation id after trim; throws if invalid. |
|
|
94
|
+
| **`assertCorrelationId(id)`** | Runtime assertion for correlation id shape. |
|
|
95
|
+
| **`assertWrapOptionsShape(options)`** | Runtime validation for **`WrapOptions`**. |
|
|
96
|
+
|
|
97
|
+
## `ExecutionEvent` fields
|
|
98
|
+
|
|
99
|
+
| Field | Type | When present | Meaning |
|
|
100
|
+
| ------------------- | ------------------------ | ------------ | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
101
|
+
| **`id`** | `string` | always | Unique event id (UUID). |
|
|
102
|
+
| **`correlationId`** | `string` | optional | Request / trace id from context or wrap options. |
|
|
103
|
+
| **`intent`** | `string` | always | Human-readable label for what this invocation is meant to prove (outcome, policy goal, or domain). |
|
|
104
|
+
| **`action`** | `string` | always | Stable operation id for this step (often dotted or namespaced). |
|
|
105
|
+
| **`inputs`** | `unknown` | always | JSON-safe snapshot of call arguments (default) or **`captureInput`** result. |
|
|
106
|
+
| **`output`** | `unknown` | success | JSON-safe return value or **`captureOutput`** result. On **`status: "error"`**, set only if **`captureError`** returned a value. |
|
|
107
|
+
| **`error`** | `ExecutionErrorSnapshot` | failure | **`name`**, **`message`**, optional **`stack`** (see **`includeErrorStack`**). |
|
|
108
|
+
| **`status`** | `"ok" \| "error"` | always | Outcome of the wrapped invocation. |
|
|
109
|
+
| **`startedAt`** | ISO string | always | Start timestamp. |
|
|
110
|
+
| **`completedAt`** | ISO string | always | Completion timestamp. |
|
|
111
|
+
| **`durationMs`** | `number` | always | Wall time between start and completion. |
|
|
112
|
+
| **`attributes`** | plain record | optional | String / number / boolean values only; merged from client defaults and wrap options. |
|
|
113
|
+
|
|
114
|
+
## `WrapOptions` and `IntentProofConfig`
|
|
115
|
+
|
|
116
|
+
### `WrapOptions` (passed to **`wrap`**)
|
|
117
|
+
|
|
118
|
+
| Field | Description |
|
|
119
|
+
| ---------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
|
120
|
+
| **`intent`**, **`action`** | Required, non-empty after trim. |
|
|
121
|
+
| **`correlationId`** | Optional; when set, non-empty after trim. Otherwise the active context id is used if any. |
|
|
122
|
+
| **`attributes`** | Per-invocation dimensions merged over **`defaultAttributes`**. |
|
|
123
|
+
| **`captureInput`**, **`captureOutput`**, **`captureError`** | Optional hooks to replace default **`snapshot`** behavior for inputs, success output, or error-side extra **`output`**. |
|
|
124
|
+
| **`includeErrorStack`** | When `false`, omit **`error.stack`** for this wrap (overrides client default). |
|
|
125
|
+
| **`maxDepth`**, **`maxKeys`**, **`redactKeys`**, **`maxStringLength`** | Forwarded to **`snapshot`** for inputs and outputs (see **`SerializeOptions`** in types). |
|
|
126
|
+
|
|
127
|
+
### `IntentProofConfig` (constructor / **`configure`**)
|
|
128
|
+
|
|
129
|
+
| Field | Description |
|
|
130
|
+
| ----------------------- | ----------------------------------------------------------------------------------------------- |
|
|
131
|
+
| **`exporters`** | Ordered list of **`Exporter`** instances; each receives every event. |
|
|
132
|
+
| **`onExporterError`** | Called when **`export`** throws or returns a rejected promise. Defaults to **`console.error`**. |
|
|
133
|
+
| **`defaultAttributes`** | Merged into every event’s **`attributes`** (wrap-specific attributes win on key collision). |
|
|
134
|
+
| **`includeErrorStack`** | Default `true`; set `false` in production if stacks must not leave the trust zone. |
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Examples
|
|
139
|
+
|
|
140
|
+
### 1 — Refund saga: money back, customer notified, ops alerted (one correlation id)
|
|
141
|
+
|
|
142
|
+
Support approves **order `ORD-1042`**. Your service runs one cohesive workflow: create the **Stripe refund**, email the customer a receipt, post to **Slack** so billing ops see it. **`runWithCorrelationId`** ties all three calls to **`req_refund_ord_1042`**. Each wrap has its own **`intent`** (the outcome you are proving for that step) and **`action`** (how it is done); **`correlationId`** is what stitches the saga together.
|
|
143
|
+
|
|
144
|
+
**`captureInput` / `captureOutput`** trim each record to the fields you want in proof (refund id, amounts, message id, Slack metadata)—not full vendor payloads.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
const createRefund = client.wrap(
|
|
148
|
+
{
|
|
149
|
+
intent: "Return captured funds to the customer's original card network",
|
|
150
|
+
action: "stripe.refund.create",
|
|
151
|
+
attributes: { vendor: "stripe", step: "refund_money" },
|
|
152
|
+
captureInput: (args) => {
|
|
153
|
+
const [input] = args as [
|
|
154
|
+
{
|
|
155
|
+
paymentIntentId: string;
|
|
156
|
+
amountCents: number;
|
|
157
|
+
reason?: "requested_by_customer" | "duplicate";
|
|
158
|
+
},
|
|
159
|
+
];
|
|
160
|
+
return {
|
|
161
|
+
paymentIntentId: input.paymentIntentId,
|
|
162
|
+
amountCents: input.amountCents,
|
|
163
|
+
reason: input.reason,
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
captureOutput: (result) => {
|
|
167
|
+
const r = result as {
|
|
168
|
+
id: string;
|
|
169
|
+
status: "succeeded";
|
|
170
|
+
amountCents: number;
|
|
171
|
+
};
|
|
172
|
+
return {
|
|
173
|
+
refundId: r.id,
|
|
174
|
+
status: r.status,
|
|
175
|
+
amountCents: r.amountCents,
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
(input: {
|
|
180
|
+
paymentIntentId: string;
|
|
181
|
+
amountCents: number;
|
|
182
|
+
reason?: "requested_by_customer" | "duplicate";
|
|
183
|
+
}) => ({
|
|
184
|
+
id: "re_3SAMPLEabcdefghijklmnop",
|
|
185
|
+
status: "succeeded" as const,
|
|
186
|
+
amountCents: input.amountCents,
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const sendRefundReceipt = client.wrap(
|
|
191
|
+
{
|
|
192
|
+
intent: "Deliver a customer-visible refund confirmation for the ledger entry",
|
|
193
|
+
action: "email.customer.refund_receipt",
|
|
194
|
+
attributes: { channel: "email", step: "notify_customer" },
|
|
195
|
+
captureInput: (args) => {
|
|
196
|
+
const [p] = args as [
|
|
197
|
+
{
|
|
198
|
+
customerId: string;
|
|
199
|
+
orderId: string;
|
|
200
|
+
refundId: string;
|
|
201
|
+
amountCents: number;
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
return {
|
|
205
|
+
customerId: p.customerId,
|
|
206
|
+
orderId: p.orderId,
|
|
207
|
+
refundId: p.refundId,
|
|
208
|
+
amountCents: p.amountCents,
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
captureOutput: (result) => {
|
|
212
|
+
const r = result as { messageId: string; status: "queued" };
|
|
213
|
+
return { messageId: r.messageId, status: r.status };
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
(p: {
|
|
217
|
+
customerId: string;
|
|
218
|
+
orderId: string;
|
|
219
|
+
refundId: string;
|
|
220
|
+
amountCents: number;
|
|
221
|
+
}) => ({ messageId: "msg_49401_sample", status: "queued" as const }),
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const notifyOpsRefund = client.wrap(
|
|
225
|
+
{
|
|
226
|
+
intent: "Surface the completed refund to billing operations for review",
|
|
227
|
+
action: "slack.operations.refund_posted",
|
|
228
|
+
attributes: { channel: "slack", step: "notify_ops" },
|
|
229
|
+
captureInput: (args) => {
|
|
230
|
+
const [p] = args as [{ refundId: string; orderId: string; amountCents: number }];
|
|
231
|
+
return {
|
|
232
|
+
refundId: p.refundId,
|
|
233
|
+
orderId: p.orderId,
|
|
234
|
+
amountCents: p.amountCents,
|
|
235
|
+
};
|
|
236
|
+
},
|
|
237
|
+
captureOutput: (result) => {
|
|
238
|
+
const r = result as {
|
|
239
|
+
ok: boolean;
|
|
240
|
+
channel: string;
|
|
241
|
+
ts: string;
|
|
242
|
+
};
|
|
243
|
+
return { ok: r.ok, channel: r.channel, ts: r.ts };
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
(p: { refundId: string; orderId: string; amountCents: number }) => ({
|
|
247
|
+
ok: true,
|
|
248
|
+
channel: "#billing-alerts",
|
|
249
|
+
ts: "1714648800.000100",
|
|
250
|
+
}),
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
await runWithCorrelationId("req_refund_ord_1042", async () => {
|
|
254
|
+
const refund = createRefund({
|
|
255
|
+
paymentIntentId: "pi_3SAMPLEabcdefghijklmnop",
|
|
256
|
+
amountCents: 4999,
|
|
257
|
+
reason: "requested_by_customer",
|
|
258
|
+
});
|
|
259
|
+
await Promise.resolve(
|
|
260
|
+
sendRefundReceipt({
|
|
261
|
+
customerId: "cus_SAMPLEabcdefghijkl",
|
|
262
|
+
orderId: "ORD-1042",
|
|
263
|
+
refundId: refund.id,
|
|
264
|
+
amountCents: refund.amountCents,
|
|
265
|
+
}),
|
|
266
|
+
);
|
|
267
|
+
await Promise.resolve(
|
|
268
|
+
notifyOpsRefund({
|
|
269
|
+
refundId: refund.id,
|
|
270
|
+
orderId: "ORD-1042",
|
|
271
|
+
amountCents: refund.amountCents,
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Emitted events (same **`correlationId`** on each; distinct **`intent`** per step; **`id`** / timestamps omitted):
|
|
278
|
+
|
|
279
|
+
```json
|
|
280
|
+
[
|
|
281
|
+
{
|
|
282
|
+
"correlationId": "req_refund_ord_1042",
|
|
283
|
+
"intent": "Return captured funds to the customer's original card network",
|
|
284
|
+
"action": "stripe.refund.create",
|
|
285
|
+
"inputs": {
|
|
286
|
+
"paymentIntentId": "pi_3SAMPLEabcdefghijklmnop",
|
|
287
|
+
"amountCents": 4999,
|
|
288
|
+
"reason": "requested_by_customer"
|
|
289
|
+
},
|
|
290
|
+
"status": "ok",
|
|
291
|
+
"output": {
|
|
292
|
+
"refundId": "re_3SAMPLEabcdefghijklmnop",
|
|
293
|
+
"status": "succeeded",
|
|
294
|
+
"amountCents": 4999
|
|
295
|
+
},
|
|
296
|
+
"attributes": {
|
|
297
|
+
"service": "billing-api",
|
|
298
|
+
"env": "test",
|
|
299
|
+
"vendor": "stripe",
|
|
300
|
+
"step": "refund_money"
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
"correlationId": "req_refund_ord_1042",
|
|
305
|
+
"intent": "Deliver a customer-visible refund confirmation for the ledger entry",
|
|
306
|
+
"action": "email.customer.refund_receipt",
|
|
307
|
+
"inputs": {
|
|
308
|
+
"customerId": "cus_SAMPLEabcdefghijkl",
|
|
309
|
+
"orderId": "ORD-1042",
|
|
310
|
+
"refundId": "re_3SAMPLEabcdefghijklmnop",
|
|
311
|
+
"amountCents": 4999
|
|
312
|
+
},
|
|
313
|
+
"status": "ok",
|
|
314
|
+
"output": { "messageId": "msg_49401_sample", "status": "queued" },
|
|
315
|
+
"attributes": {
|
|
316
|
+
"service": "billing-api",
|
|
317
|
+
"env": "test",
|
|
318
|
+
"channel": "email",
|
|
319
|
+
"step": "notify_customer"
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
"correlationId": "req_refund_ord_1042",
|
|
324
|
+
"intent": "Surface the completed refund to billing operations for review",
|
|
325
|
+
"action": "slack.operations.refund_posted",
|
|
326
|
+
"inputs": {
|
|
327
|
+
"refundId": "re_3SAMPLEabcdefghijklmnop",
|
|
328
|
+
"orderId": "ORD-1042",
|
|
329
|
+
"amountCents": 4999
|
|
330
|
+
},
|
|
331
|
+
"status": "ok",
|
|
332
|
+
"output": {
|
|
333
|
+
"ok": true,
|
|
334
|
+
"channel": "#billing-alerts",
|
|
335
|
+
"ts": "1714648800.000100"
|
|
336
|
+
},
|
|
337
|
+
"attributes": {
|
|
338
|
+
"service": "billing-api",
|
|
339
|
+
"env": "test",
|
|
340
|
+
"channel": "slack",
|
|
341
|
+
"step": "notify_ops"
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
]
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### 2 — Payment failure with operator metadata (`captureError`)
|
|
348
|
+
|
|
349
|
+
When a capture **throws**, the record still carries **`status: "error"`** and **`error.message`** for proof of failure. **`captureError`** adds a small, JSON-safe **`output`** for dashboards (e.g. decline code) without pretending the business call succeeded.
|
|
350
|
+
|
|
351
|
+
```ts
|
|
352
|
+
const capturePayment = client.wrap(
|
|
353
|
+
{
|
|
354
|
+
intent: "Capture authorized funds",
|
|
355
|
+
action: "stripe.payment_intent.capture",
|
|
356
|
+
captureInput: (args) => {
|
|
357
|
+
const [{ paymentIntentId }] = args as [{ paymentIntentId: string }];
|
|
358
|
+
return { paymentIntentId };
|
|
359
|
+
},
|
|
360
|
+
captureError: () => ({ code: "card_declined", retryable: false }),
|
|
361
|
+
},
|
|
362
|
+
async (_input: { paymentIntentId: string }) => {
|
|
363
|
+
throw new Error("Your card was declined.");
|
|
364
|
+
},
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
await capturePayment({ paymentIntentId: "pi_3SAMPLEabcdefghijklmnop" });
|
|
369
|
+
} catch {
|
|
370
|
+
/* card declined — expected */
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
```json
|
|
375
|
+
{
|
|
376
|
+
"intent": "Capture authorized funds",
|
|
377
|
+
"action": "stripe.payment_intent.capture",
|
|
378
|
+
"inputs": { "paymentIntentId": "pi_3SAMPLEabcdefghijklmnop" },
|
|
379
|
+
"status": "error",
|
|
380
|
+
"error": {
|
|
381
|
+
"name": "Error",
|
|
382
|
+
"message": "Your card was declined."
|
|
383
|
+
},
|
|
384
|
+
"output": { "code": "card_declined", "retryable": false }
|
|
385
|
+
}
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### 3 — Proof delivery over HTTP (same event shape)
|
|
389
|
+
|
|
390
|
+
**`HttpExporter`** POSTs the same **`ExecutionEvent`** your verifiers see in memory—here alongside **`MemoryExporter`** so tests can assert the wire without a real collector. The request uses **`credentials: "omit"`**; the body is **`{ intentproof: "1", event: … }`** (see exporter implementation). For authenticated collectors, pass **`headers`** (e.g. **`Authorization`**, API keys) — see [Security](#security).
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
const runProbe = client.wrap({ intent: "HTTP test", action: "test.http" }, () => 42);
|
|
394
|
+
runProbe();
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
```json
|
|
398
|
+
{
|
|
399
|
+
"intent": "HTTP test",
|
|
400
|
+
"action": "test.http",
|
|
401
|
+
"inputs": [],
|
|
402
|
+
"status": "ok",
|
|
403
|
+
"output": 42
|
|
404
|
+
}
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
409
|
+
## Security
|
|
410
|
+
|
|
411
|
+
**IntentProof events are data you ship off-process.** Treat **`ExecutionEvent`** like structured logs or audit payloads: they can include PII, secrets, stack traces, and business identifiers depending on your **`snapshot`** / **`capture*`** hooks.
|
|
412
|
+
|
|
413
|
+
- **Minimize payload:** Use **`redactKeys`**, **`maxDepth` / `maxKeys` / `maxStringLength`**, and narrow **`captureInput` / `captureOutput` / `captureError`** so proof records contain only what verifiers need.
|
|
414
|
+
- **Stacks:** Set **`includeErrorStack: false`** on the client (or per wrap) when traces must not leave your trust zone.
|
|
415
|
+
- **HTTP ingest:** Keep collector **`url`** and any redirect behavior under **trusted configuration** (avoid SSRF if URLs were ever influenced by untrusted input). Prefer **HTTPS** and **short-lived credentials** end-to-end.
|
|
416
|
+
- **`HttpExporter` auth:** Pass credentials in **`headers`** (for example **`Authorization: Bearer …`**, **`x-api-key`**, or whatever your collector expects). The SDK does **not** log header values; use short-lived tokens and scope them to ingest only.
|
|
417
|
+
- **Browser vs server:** This package targets **Node**; if you wrap code in a browser, treat the ingest endpoint and headers as you would any cross-origin credential (CORS, CSP, token storage policies are your app’s responsibility).
|
|
418
|
+
- **Delivery semantics:** Exporter failures invoke **`onExporterError`** and do **not** roll back the wrapped function’s side effects—design compensating controls if you need strict “delivered exactly once” guarantees.
|
|
419
|
+
|
|
420
|
+
Custom **`body`** serializers: if **`body(event)`** throws, **`HttpExporter`** notifies **`onError`** and falls back to the same **JSON envelope** path as the default serializer (full event, then a partial envelope, then a minimal `eventSerializeFailed` payload) so **`export()`** still completes and **`fetch`** runs when possible.
|
|
421
|
+
|
|
422
|
+
---
|
|
423
|
+
|
|
424
|
+
## Related exports
|
|
425
|
+
|
|
426
|
+
- **`MemoryExporter`**, **`HttpExporter`**, **`BoundedQueueExporter`** — Delivery implementations; each implements **`Exporter`**.
|
|
427
|
+
- **`snapshot`** — Same JSON-safe serializer the client uses internally, if you build custom tooling.
|
|
428
|
+
- **`VERSION`** — Package version string injected at build time.
|
|
429
|
+
|
|
430
|
+
## Monorepo development
|
|
431
|
+
|
|
432
|
+
This repository is an npm workspace; the publishable package is [`packages/sdk`](packages/sdk).
|
|
433
|
+
|
|
434
|
+
Requires **Node.js 22+** (see `.nvmrc` and workspace `engines`).
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
npm ci
|
|
438
|
+
npm run ci
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
## License
|
|
442
|
+
|
|
443
|
+
Apache-2.0 (see `LICENSE` at the repository root and in the published npm package).
|