@eigenpal/sdk 0.0.0-placeholder
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/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +179 -0
- package/package.json +51 -0
- package/src/client.ts +237 -0
- package/src/errors.ts +103 -0
- package/src/generated/client/client.gen.ts +280 -0
- package/src/generated/client/index.ts +25 -0
- package/src/generated/client/types.gen.ts +214 -0
- package/src/generated/client/utils.gen.ts +318 -0
- package/src/generated/client.gen.ts +22 -0
- package/src/generated/core/auth.gen.ts +41 -0
- package/src/generated/core/bodySerializer.gen.ts +82 -0
- package/src/generated/core/params.gen.ts +169 -0
- package/src/generated/core/pathSerializer.gen.ts +171 -0
- package/src/generated/core/queryKeySerializer.gen.ts +117 -0
- package/src/generated/core/serverSentEvents.gen.ts +242 -0
- package/src/generated/core/types.gen.ts +104 -0
- package/src/generated/core/utils.gen.ts +140 -0
- package/src/generated/index.ts +63 -0
- package/src/generated/sdk.gen.ts +151 -0
- package/src/generated/types.gen.ts +608 -0
- package/src/index.ts +64 -0
- package/src/lib/files.ts +128 -0
- package/src/resources/executions.ts +179 -0
- package/src/resources/workflows.ts +154 -0
- package/src/runtime-config.ts +15 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @eigenpal/sdk
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
- Initial release.
|
|
6
|
+
- Coverage: workflow trigger (sync + async), execution polling, cancel, workflow & version listing.
|
|
7
|
+
- `Eigenpal` facade with API key auth, automatic retries on 5xx / 429 / network errors with `Retry-After` honoring, typed `EigenpalError` subclasses, and `executions.runAndWait()` for client-side polling.
|
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 describing the origin of the Work and
|
|
141
|
+
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 accept and charge a
|
|
167
|
+
fee for, or grant any of the foregoing rights, accept support,
|
|
168
|
+
warranty, indemnity, or other liability obligations and/or rights
|
|
169
|
+
consistent with this License. However, in accepting such obligations,
|
|
170
|
+
You may act only on Your own behalf and on Your sole responsibility,
|
|
171
|
+
not on behalf of any other Contributor, and only if You agree to
|
|
172
|
+
indemnify, defend, and hold each Contributor harmless for any
|
|
173
|
+
liability incurred by, or claims asserted against, such Contributor
|
|
174
|
+
by reason 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 Eigenpal
|
|
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,179 @@
|
|
|
1
|
+
# @eigenpal/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for the [EigenPal](https://eigenpal.com) API.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @eigenpal/sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Eigenpal } from '@eigenpal/sdk';
|
|
11
|
+
|
|
12
|
+
const client = new Eigenpal({
|
|
13
|
+
apiKey: process.env.EIGENPAL_API_KEY,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Pass a File / Blob / { content, filename, mimeType }. The SDK uploads
|
|
17
|
+
// the request as multipart/form-data, no base64 needed.
|
|
18
|
+
const result = await client.executions.runAndWait('extract-invoice', {
|
|
19
|
+
contract_document: file,
|
|
20
|
+
});
|
|
21
|
+
console.log(result.status, result.result);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Authentication
|
|
25
|
+
|
|
26
|
+
Generate an API key from the dashboard under **Settings → API Keys**, then pass it explicitly:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
const client = new Eigenpal({ apiKey: process.env.EIGENPAL_API_KEY });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `apiKey` constructor option always wins. If you omit it, the SDK falls back to `process.env.EIGENPAL_API_KEY` for convenience, handy in scripts where you'd be writing exactly the line above.
|
|
33
|
+
|
|
34
|
+
## Self-hosted
|
|
35
|
+
|
|
36
|
+
Point the SDK at your own deployment via `baseUrl`:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const client = new Eigenpal({
|
|
40
|
+
apiKey: process.env.EIGENPAL_API_KEY,
|
|
41
|
+
baseUrl: process.env.EIGENPAL_BASE_URL ?? 'https://eigenpal.acme.internal',
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`baseUrl` likewise wins over the `EIGENPAL_BASE_URL` env fallback. Defaults to `https://app.eigenpal.com` (the hosted cloud).
|
|
46
|
+
|
|
47
|
+
## Triggering workflows
|
|
48
|
+
|
|
49
|
+
`workflows.run(workflowId, input?, options?)` enqueues a workflow execution.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// Async: returns immediately with { executionId }.
|
|
53
|
+
const { executionId } = await client.workflows.run('extract-invoice', {
|
|
54
|
+
contract_document: file,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Sync: server holds the connection up to 60 seconds.
|
|
58
|
+
const result = await client.workflows.run(
|
|
59
|
+
'extract-invoice',
|
|
60
|
+
{ contract_document: file },
|
|
61
|
+
{ waitForCompletion: 60 }
|
|
62
|
+
);
|
|
63
|
+
console.log(result.status, result.result);
|
|
64
|
+
|
|
65
|
+
// Long-running: client-side polling, default 5min cap.
|
|
66
|
+
const final = await client.executions.runAndWait('extract-invoice', {
|
|
67
|
+
contract_document: file,
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The second argument is the workflow input map keyed by input name (as declared in the workflow). Pass `undefined` for inputs-less workflows. `options` carries `version`, `waitForCompletion`, and `overrides`.
|
|
72
|
+
|
|
73
|
+
## File inputs
|
|
74
|
+
|
|
75
|
+
When a workflow input is a file, pass a `File`, `Blob`, or explicit `{ content, filename, mimeType }` descriptor. The SDK auto-detects them and uploads the request as `multipart/form-data` (the same shape as `curl -F`, no base64 round-trip):
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
// Browser: File from <input type="file">
|
|
79
|
+
await client.workflows.run('extract-invoice', { contract_document: file });
|
|
80
|
+
|
|
81
|
+
// Node: Buffer from fs
|
|
82
|
+
import { readFile } from 'node:fs/promises';
|
|
83
|
+
const buffer = await readFile('contract.pdf');
|
|
84
|
+
await client.workflows.run('extract-invoice', {
|
|
85
|
+
contract_document: {
|
|
86
|
+
content: buffer,
|
|
87
|
+
filename: 'contract.pdf',
|
|
88
|
+
mimeType: 'application/pdf',
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
> **Don't** base64-encode files yourself. The SDK is multipart-first; base64 doubles the payload size and skips the optimised upload path.
|
|
94
|
+
|
|
95
|
+
## Execution polling
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const status = await client.executions.get(executionId);
|
|
99
|
+
// { executionId, status, result?, error?, createdAt, completedAt? }
|
|
100
|
+
|
|
101
|
+
const list = await client.executions.list({
|
|
102
|
+
workflowId: 'extract-invoice',
|
|
103
|
+
status: 'failed',
|
|
104
|
+
fromDate: 'now()-7d',
|
|
105
|
+
limit: 50,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await client.executions.cancel(executionId);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Workflows
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
await client.workflows.list({ search: 'invoice', limit: 20 });
|
|
115
|
+
await client.workflows.get('extract-invoice');
|
|
116
|
+
await client.workflows.versions('extract-invoice');
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Errors
|
|
120
|
+
|
|
121
|
+
Every non-2xx response throws a typed subclass of `EigenpalError`:
|
|
122
|
+
|
|
123
|
+
| HTTP | Class | Notes |
|
|
124
|
+
| --------------- | ------------------------- | ---------------------------------------------------- |
|
|
125
|
+
| 400 | `EigenpalValidationError` | `.issues` carries the per-field problems |
|
|
126
|
+
| 401 | `EigenpalAuthError` | Bad / missing API key |
|
|
127
|
+
| 403 | `EigenpalForbiddenError` | API trigger disabled, scope mismatch |
|
|
128
|
+
| 404 | `EigenpalNotFoundError` | Workflow / execution doesn't exist |
|
|
129
|
+
| 429 | `EigenpalRateLimitError` | `.retryAfter` is the server-suggested wait (seconds) |
|
|
130
|
+
| 5xx | `EigenpalServerError` | Auto-retried up to `maxRetries` |
|
|
131
|
+
| timeout / abort | `EigenpalTimeoutError` | |
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { Eigenpal, EigenpalValidationError } from '@eigenpal/sdk';
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await client.workflows.run('extract-invoice');
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (err instanceof EigenpalValidationError) {
|
|
140
|
+
for (const issue of err.issues) {
|
|
141
|
+
console.error(`${issue.field}: ${issue.message}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
throw err;
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Configuration
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
new Eigenpal({
|
|
152
|
+
apiKey: 'eg_…', // or EIGENPAL_API_KEY
|
|
153
|
+
baseUrl: 'https://app.eigenpal.com', // or EIGENPAL_BASE_URL
|
|
154
|
+
timeoutMs: 60_000, // per-request timeout
|
|
155
|
+
maxRetries: 3, // 5xx / 429 / network
|
|
156
|
+
defaultHeaders: {
|
|
157
|
+
// merged into every request
|
|
158
|
+
'X-Trace-Id': '…',
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The SDK retries on 5xx, 429 (honoring `Retry-After`), and network errors. 4xx errors are surfaced immediately as typed exceptions and are not retried.
|
|
164
|
+
|
|
165
|
+
## TypeScript
|
|
166
|
+
|
|
167
|
+
The full request/response types are exported:
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import type {
|
|
171
|
+
WorkflowSummary,
|
|
172
|
+
ExecutionStatusResponse,
|
|
173
|
+
ListWorkflowsResponse,
|
|
174
|
+
} from '@eigenpal/sdk';
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## License
|
|
178
|
+
|
|
179
|
+
Apache-2.0.
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eigenpal/sdk",
|
|
3
|
+
"version": "0.0.0-placeholder",
|
|
4
|
+
"description": "Official TypeScript SDK for the EigenPal API",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"CHANGELOG.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"codegen": "openapi-ts -f openapi-ts.config.ts",
|
|
19
|
+
"codegen:check": "bun run codegen && git diff --exit-code src/generated/",
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"lint": "eslint src/",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"test": "bun test"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@hey-api/client-fetch": "^0.13.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@hey-api/openapi-ts": "^0.97.1",
|
|
30
|
+
"typescript": "^6.0.3"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/eigenpal/sdk-typescript.git"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/eigenpal/sdk-typescript/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/eigenpal/sdk-typescript#readme",
|
|
43
|
+
"license": "Apache-2.0",
|
|
44
|
+
"keywords": [
|
|
45
|
+
"eigenpal",
|
|
46
|
+
"sdk",
|
|
47
|
+
"api",
|
|
48
|
+
"client",
|
|
49
|
+
"workflow"
|
|
50
|
+
]
|
|
51
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { EigenpalError, EigenpalTimeoutError, errorFromResponse } from './errors';
|
|
2
|
+
import { createClient, createConfig, type Client, type Config } from './generated/client';
|
|
3
|
+
import type { ApiErrorEnvelope } from './generated/types.gen';
|
|
4
|
+
import { ExecutionsResource } from './resources/executions';
|
|
5
|
+
import { WorkflowsResource } from './resources/workflows';
|
|
6
|
+
|
|
7
|
+
export interface EigenpalOptions {
|
|
8
|
+
/**
|
|
9
|
+
* API key issued from Settings → API Keys (`eg_…`).
|
|
10
|
+
*
|
|
11
|
+
* Falls back to the `EIGENPAL_API_KEY` environment variable when omitted,
|
|
12
|
+
* so most users only need `new Eigenpal()`.
|
|
13
|
+
*/
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Override the API base URL.
|
|
17
|
+
*
|
|
18
|
+
* Defaults to `EIGENPAL_BASE_URL` if set, otherwise `https://app.eigenpal.com`.
|
|
19
|
+
*/
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
/** Per-request timeout in milliseconds. Defaults to 60_000. */
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
/** How many times to retry on 5xx / 429 / network errors. Defaults to 3. */
|
|
24
|
+
maxRetries?: number;
|
|
25
|
+
/** Inject a custom fetch implementation (testing). Defaults to global fetch. */
|
|
26
|
+
fetch?: typeof globalThis.fetch;
|
|
27
|
+
/** Extra headers attached to every request (debug ids, custom user-agent suffix, etc.). */
|
|
28
|
+
defaultHeaders?: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_BASE_URL = 'https://app.eigenpal.com';
|
|
32
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
33
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
34
|
+
const SDK_VERSION = '0.0.0-placeholder';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Result shape returned by hey-api operation calls. We let hey-api default
|
|
38
|
+
* to `throwOnError: false` so we can centralize error mapping and retries
|
|
39
|
+
* in `_request`. The `error` field carries the parsed `ApiErrorEnvelope`
|
|
40
|
+
* for non-2xx responses; `data` is the success payload. `response` is
|
|
41
|
+
* optional only because hey-api's types declare it that way — at runtime
|
|
42
|
+
* it's always present once the fetch resolves.
|
|
43
|
+
*/
|
|
44
|
+
export interface OperationResult<T> {
|
|
45
|
+
data?: T;
|
|
46
|
+
error?: unknown;
|
|
47
|
+
response?: Response;
|
|
48
|
+
request?: Request;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The Eigenpal SDK client.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { Eigenpal } from '@eigenpal/sdk';
|
|
56
|
+
*
|
|
57
|
+
* // Reads EIGENPAL_API_KEY from env automatically.
|
|
58
|
+
* const client = new Eigenpal();
|
|
59
|
+
*
|
|
60
|
+
* // Async — enqueue and poll later.
|
|
61
|
+
* const { executionId } = await client.workflows.run('wf_abc', { language: 'en' });
|
|
62
|
+
*
|
|
63
|
+
* // Sync (server holds the connection up to 60s).
|
|
64
|
+
* const result = await client.workflows.run('wf_abc', { language: 'en' }, {
|
|
65
|
+
* waitForCompletion: 60,
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* // Client-side polling for long-running runs (default 5min cap).
|
|
69
|
+
* const final = await client.executions.runAndWait('wf_abc', { language: 'en' });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export class Eigenpal {
|
|
73
|
+
/** Workflow operations: `list`, `get`, `versions`, `run`. */
|
|
74
|
+
public readonly workflows: WorkflowsResource;
|
|
75
|
+
/** Execution operations: `list`, `get`, `cancel`, `runAndWait`. */
|
|
76
|
+
public readonly executions: ExecutionsResource;
|
|
77
|
+
|
|
78
|
+
/** Underlying hey-api client. Use `getRawClient()` for advanced cases. */
|
|
79
|
+
private readonly client: Client;
|
|
80
|
+
private readonly maxRetries: number;
|
|
81
|
+
private readonly timeoutMs: number;
|
|
82
|
+
|
|
83
|
+
constructor(options: EigenpalOptions = {}) {
|
|
84
|
+
const apiKey = options.apiKey ?? readEnv('EIGENPAL_API_KEY');
|
|
85
|
+
if (!apiKey) {
|
|
86
|
+
throw new EigenpalError(
|
|
87
|
+
'Missing API key. Pass `new Eigenpal({ apiKey })` or set the EIGENPAL_API_KEY environment variable.',
|
|
88
|
+
{ status: 0 }
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
93
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
94
|
+
|
|
95
|
+
const baseUrl = options.baseUrl ?? readEnv('EIGENPAL_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
96
|
+
const config: Config = createConfig({
|
|
97
|
+
baseUrl,
|
|
98
|
+
headers: {
|
|
99
|
+
Authorization: `Bearer ${apiKey}`,
|
|
100
|
+
'User-Agent': `eigenpal-sdk-js/${SDK_VERSION}`,
|
|
101
|
+
...(options.defaultHeaders ?? {}),
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
this.client = createClient(config);
|
|
106
|
+
if (options.fetch) {
|
|
107
|
+
// hey-api's Config types `fetch` as `typeof fetch` (DOM type that
|
|
108
|
+
// includes `preconnect`). At runtime any `(Request) => Promise<Response>`
|
|
109
|
+
// works; cast to bypass the structural mismatch.
|
|
110
|
+
this.client.setConfig({ fetch: options.fetch as never });
|
|
111
|
+
}
|
|
112
|
+
this.installTimeoutInterceptor();
|
|
113
|
+
|
|
114
|
+
this.workflows = new WorkflowsResource(this.client, this._request.bind(this));
|
|
115
|
+
this.executions = new ExecutionsResource(this.client, this._request.bind(this));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Expose the underlying hey-api client for advanced use (custom interceptors, etc.). */
|
|
119
|
+
getRawClient(): Client {
|
|
120
|
+
return this.client;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Run an operation with automatic retries on 5xx / 429 / network errors,
|
|
125
|
+
* and centralized error mapping into typed `EigenpalError` subclasses.
|
|
126
|
+
*
|
|
127
|
+
* Resources call this via the bound `_request` they receive at construction.
|
|
128
|
+
* The retry budget is `maxRetries`; backoff is exponential (250ms × 2^attempt)
|
|
129
|
+
* unless the response carries a `Retry-After` header.
|
|
130
|
+
*/
|
|
131
|
+
private async _request<T>(call: () => Promise<OperationResult<T>>): Promise<T> {
|
|
132
|
+
for (let attempt = 0; ; attempt++) {
|
|
133
|
+
try {
|
|
134
|
+
const result = await call();
|
|
135
|
+
const response = result.response;
|
|
136
|
+
if (response && response.ok && result.data !== undefined) {
|
|
137
|
+
return result.data;
|
|
138
|
+
}
|
|
139
|
+
// Non-2xx (or response missing — treat as opaque failure).
|
|
140
|
+
const status = response?.status ?? 0;
|
|
141
|
+
if (isRetriableStatus(status) && attempt < this.maxRetries) {
|
|
142
|
+
await sleep(retryDelay(response, attempt));
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const envelope = asEnvelope(result.error);
|
|
146
|
+
const retryAfter = parseRetryAfter(response?.headers.get('retry-after') ?? null);
|
|
147
|
+
throw errorFromResponse(status, envelope, retryAfter);
|
|
148
|
+
} catch (err) {
|
|
149
|
+
// Re-throw mapped Eigenpal errors as-is.
|
|
150
|
+
if (err instanceof EigenpalError) throw err;
|
|
151
|
+
// Network/abort error — retry if budget allows. AbortSignal aborts
|
|
152
|
+
// surface as DOMException — we treat them as terminal (don't retry).
|
|
153
|
+
if (isAbortError(err)) throw err;
|
|
154
|
+
if (attempt < this.maxRetries) {
|
|
155
|
+
await sleep(backoff(attempt));
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
throw err;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Add a request-level default timeout. The hey-api client honors any
|
|
165
|
+
* `signal` the caller passes; we only attach a fresh AbortController when
|
|
166
|
+
* none is provided (so per-call signals always win).
|
|
167
|
+
*/
|
|
168
|
+
private installTimeoutInterceptor(): void {
|
|
169
|
+
const timeoutMs = this.timeoutMs;
|
|
170
|
+
if (timeoutMs <= 0) return;
|
|
171
|
+
|
|
172
|
+
this.client.interceptors.request.use(async (req) => {
|
|
173
|
+
if (req.signal) return req;
|
|
174
|
+
const ctrl = new AbortController();
|
|
175
|
+
const timer = setTimeout(() => ctrl.abort(new EigenpalTimeoutError()), timeoutMs);
|
|
176
|
+
// unref keeps the timer from holding the Node.js event loop open after
|
|
177
|
+
// the request resolves. Browsers don't have unref — guard with `?.`.
|
|
178
|
+
timer.unref?.();
|
|
179
|
+
return new Request(req, { signal: ctrl.signal });
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isRetriableStatus(status: number): boolean {
|
|
185
|
+
return status >= 500 || status === 429;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function retryDelay(response: Response | undefined, attempt: number): number {
|
|
189
|
+
const retryAfterSec = parseRetryAfter(response?.headers.get('retry-after') ?? null);
|
|
190
|
+
return retryAfterSec !== undefined ? retryAfterSec * 1000 : backoff(attempt);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function backoff(attempt: number): number {
|
|
194
|
+
return 250 * 2 ** attempt; // 250, 500, 1000, 2000ms
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function parseRetryAfter(value: string | null): number | undefined {
|
|
198
|
+
if (!value) return undefined;
|
|
199
|
+
const seconds = Number(value);
|
|
200
|
+
return Number.isFinite(seconds) && seconds >= 0 ? seconds : undefined;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function sleep(ms: number): Promise<void> {
|
|
204
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function isAbortError(err: unknown): boolean {
|
|
208
|
+
if (err instanceof EigenpalTimeoutError) return true;
|
|
209
|
+
if (typeof err === 'object' && err !== null && 'name' in err) {
|
|
210
|
+
return (err as { name: string }).name === 'AbortError';
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function asEnvelope(error: unknown): ApiErrorEnvelope | undefined {
|
|
216
|
+
if (error && typeof error === 'object' && Array.isArray((error as { issues?: unknown }).issues)) {
|
|
217
|
+
return error as ApiErrorEnvelope;
|
|
218
|
+
}
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Read a single env var as a fallback for an explicit constructor option.
|
|
224
|
+
*
|
|
225
|
+
* The repo's `no-process-env` rule pushes other packages toward
|
|
226
|
+
* `@eigenpal/env`, but this is a published SDK — it can't pull internal
|
|
227
|
+
* env infrastructure. The disables below are scoped to this one helper.
|
|
228
|
+
*/
|
|
229
|
+
function readEnv(name: string): string | undefined {
|
|
230
|
+
// eslint-disable-next-line no-process-env
|
|
231
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
232
|
+
// eslint-disable-next-line no-process-env
|
|
233
|
+
const v = process.env[name];
|
|
234
|
+
return v && v.length > 0 ? v : undefined;
|
|
235
|
+
}
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|