@eigenpal/sdk 0.25.14 → 1.0.0-rc.2
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 +27 -105
- package/dist/client.d.ts +164 -0
- package/dist/client.js +234 -0
- package/dist/core.d.ts +82 -0
- package/dist/core.js +311 -0
- package/dist/errors.d.ts +64 -0
- package/dist/errors.js +132 -0
- package/dist/generated/operations.gen.d.ts +336 -0
- package/dist/generated/operations.gen.js +135 -0
- package/dist/generated/public.gen.d.ts +2 -0
- package/dist/generated/public.gen.js +2 -0
- package/dist/generated/resources.gen.d.ts +490 -0
- package/dist/generated/resources.gen.js +659 -0
- package/dist/generated/types.gen.d.ts +2457 -0
- package/dist/generated/types.gen.js +2 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +15 -0
- package/dist/lib/fetch-body.d.ts +23 -0
- package/dist/lib/fetch-body.js +66 -0
- package/dist/lib/files.d.ts +134 -0
- package/dist/lib/files.js +296 -0
- package/dist/lib/upload-app-chunked.d.ts +36 -0
- package/dist/lib/upload-app-chunked.js +94 -0
- package/dist/lib/upload-limits.d.ts +32 -0
- package/dist/lib/upload-limits.js +60 -0
- package/dist/lib/upload-presigned-multipart.d.ts +74 -0
- package/dist/lib/upload-presigned-multipart.js +212 -0
- package/dist/page.d.ts +30 -0
- package/dist/page.js +38 -0
- package/dist/resources.d.ts +85 -0
- package/dist/resources.js +247 -0
- package/dist/telemetry.d.ts +20 -0
- package/{src/telemetry.ts → dist/telemetry.js} +26 -31
- package/dist/upload-file.d.ts +31 -0
- package/dist/upload-file.js +286 -0
- package/{src/upload.ts → dist/upload.d.ts} +4 -4
- package/dist/upload.js +12 -0
- package/package.json +19 -15
- package/src/client.ts +0 -656
- package/src/errors.ts +0 -130
- package/src/generated/client/client.gen.ts +0 -277
- package/src/generated/client/index.ts +0 -27
- package/src/generated/client/types.gen.ts +0 -218
- package/src/generated/client/utils.gen.ts +0 -316
- package/src/generated/client.gen.ts +0 -18
- package/src/generated/core/auth.gen.ts +0 -48
- package/src/generated/core/bodySerializer.gen.ts +0 -82
- package/src/generated/core/params.gen.ts +0 -178
- package/src/generated/core/pathSerializer.gen.ts +0 -171
- package/src/generated/core/queryKeySerializer.gen.ts +0 -117
- package/src/generated/core/serverSentEvents.gen.ts +0 -242
- package/src/generated/core/types.gen.ts +0 -110
- package/src/generated/core/utils.gen.ts +0 -140
- package/src/generated/index.ts +0 -4
- package/src/generated/sdk.gen.ts +0 -1361
- package/src/generated/types.gen.ts +0 -9282
- package/src/index.ts +0 -120
- package/src/lib/fetch-body.ts +0 -92
- package/src/lib/files.ts +0 -390
- package/src/lib/upload-app-chunked.ts +0 -121
- package/src/lib/upload-limits.ts +0 -72
- package/src/lib/upload-presigned-multipart.ts +0 -285
- package/src/resources/auth.ts +0 -18
- package/src/resources/automations.ts +0 -830
- package/src/resources/dataset-review-requests.ts +0 -38
- package/src/resources/email-servers.ts +0 -95
- package/src/resources/files.ts +0 -592
- package/src/resources/folders.ts +0 -77
- package/src/resources/human-reviews.ts +0 -106
- package/src/resources/models.ts +0 -37
- package/src/resources/runs.ts +0 -350
- package/src/resources/templates.ts +0 -153
- package/src/runtime-config.ts +0 -14
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# @eigenpal/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Run, evaluate and release EigenPal automations from TypeScript. Every method mirrors a CLI command and calls the same route: `eigenpal runs get <run>` is `client.runs.get(run)`.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@eigenpal/sdk)
|
|
6
|
-
[](https://www.npmjs.com/package/@eigenpal/sdk)
|
|
7
6
|
[](https://github.com/eigenpal/sdk-typescript/blob/main/LICENSE)
|
|
8
7
|
|
|
9
8
|
## Install
|
|
@@ -12,123 +11,46 @@ Trigger and inspect Eigenpal automations from TypeScript.
|
|
|
12
11
|
npm i @eigenpal/sdk
|
|
13
12
|
```
|
|
14
13
|
|
|
15
|
-
Requires a TypeScript-aware runtime: Bun, Deno, Node 22+ (native TS), `tsx`, Next.js, Vite, or any modern bundler.
|
|
14
|
+
Requires a TypeScript-aware runtime: Bun, Deno, Node 22+ (native TS), `tsx`, Next.js, Vite, or any modern bundler. Get an API key in Studio under **Settings > API Keys**.
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
## Quick Start
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import { EigenpalClient } from '@eigenpal/sdk';
|
|
23
|
-
|
|
24
|
-
const client = new EigenpalClient({ apiKey: process.env.EIGENPAL_API_KEY });
|
|
25
|
-
|
|
26
|
-
const result = await client.run(
|
|
27
|
-
'workflows.extract-invoice',
|
|
28
|
-
{ contract_document: file },
|
|
29
|
-
{ waitForCompletion: 60 }
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
if (result.finished) {
|
|
33
|
-
console.log(result.output);
|
|
34
|
-
}
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
`target` accepts a bare workflow ID such as `wf_abc`, or a typed target such as `workflows.<id-or-slug>` or `agents.<slug>`. Slugs need the prefix because workflow and agent slugs can overlap.
|
|
38
|
-
|
|
39
|
-
## Automations
|
|
40
|
-
|
|
41
|
-
Workflows and agents are exposed as automations.
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
const { data } = await client.automations.list({ search: 'invoice', folderId: 'fldr_…' });
|
|
45
|
-
const automation = await client.automations.get('workflows.extract-invoice');
|
|
46
|
-
await client.automations.move('workflows.extract-invoice', { folderPath: 'billing/invoices' });
|
|
47
|
-
await client.automations.delete('workflows.extract-invoice');
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
`list({ folderId: 'null' })` returns unfiled YAML workflows at the tenant root. Agent automations have no folder model.
|
|
51
|
-
|
|
52
|
-
Workflow delete archives the automations parent and keeps execution history. Agent delete removes the agent implementation and history, matching the dashboard, with best-effort leftover storage cleanup.
|
|
53
|
-
|
|
54
|
-
## Folders
|
|
55
|
-
|
|
56
|
-
Workflow and template trees are a first-class resource. Nested agent directories in Git are source organization only.
|
|
57
|
-
|
|
58
|
-
```ts
|
|
59
|
-
const tree = await client.folders.list({ type: 'workflow', tree: 'true' });
|
|
60
|
-
const folder = await client.folders.create({ name: 'invoices', type: 'workflow' });
|
|
61
|
-
await client.folders.update(folder.id, { name: 'billing' });
|
|
62
|
-
await client.folders.delete(folder.id); // unfiles workflows; does not delete them
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Runs
|
|
16
|
+
## Quick start
|
|
66
17
|
|
|
67
18
|
```ts
|
|
68
|
-
|
|
19
|
+
import { EigenPalClient } from '@eigenpal/sdk';
|
|
69
20
|
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
const steps = await client.runs.steps(id);
|
|
73
|
-
const events = await client.runs.events(id);
|
|
74
|
-
const trace = await client.runs.trace.get(id);
|
|
75
|
-
|
|
76
|
-
await client.runs.cancel(id);
|
|
77
|
-
await client.rerun(id, { waitForCompletion: 60 });
|
|
78
|
-
```
|
|
21
|
+
// EIGENPAL_API_KEY and EIGENPAL_PROJECT come from the environment.
|
|
22
|
+
const client = new EigenPalClient({ project: 'finance-ops' });
|
|
79
23
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const recentFailures = await client.runs.list({
|
|
84
|
-
type: 'workflow',
|
|
85
|
-
status: 'failed,cancelled',
|
|
86
|
-
limit: 50,
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
## Files And Artifacts
|
|
91
|
-
|
|
92
|
-
Use `client.files` for reusable upload-first blobs. When referenced by a run input, Eigenpal snapshots the file into run-scoped artifacts.
|
|
93
|
-
|
|
94
|
-
```ts
|
|
95
|
-
const uploaded = await client.files.upload(file);
|
|
24
|
+
// Waits for the outcome, as `eigenpal run` does.
|
|
25
|
+
const run = await client.run('invoice-extract', { document: { path: './invoice.pdf' } });
|
|
26
|
+
console.log(run.state, run.output);
|
|
96
27
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
});
|
|
28
|
+
// Lists answer a page; `for await` walks every page.
|
|
29
|
+
for await (const r of client.runs.list({ state: 'failed' })) console.log(r.id);
|
|
100
30
|
|
|
101
|
-
|
|
102
|
-
|
|
31
|
+
// Release what is live as the next minor version.
|
|
32
|
+
await client.release('minor', { notes: 'Higher limit' });
|
|
103
33
|
```
|
|
104
34
|
|
|
105
|
-
|
|
35
|
+
A failed run is data (`state: 'failed'`), not an exception. Exceptions are transport and API errors (`EigenPalError`, with the server's `code` and `hint`) and a wait that ran out (`EigenPalTimeoutError`, with the last record in `.record`).
|
|
106
36
|
|
|
107
|
-
##
|
|
37
|
+
## Time units
|
|
108
38
|
|
|
109
|
-
Every
|
|
39
|
+
Every time in this SDK is in milliseconds and is named `timeoutMs`: per request on the client, per wait on a method.
|
|
110
40
|
|
|
111
|
-
|
|
112
|
-
| --------------- | ------------------------- |
|
|
113
|
-
| 400 | `EigenpalValidationError` |
|
|
114
|
-
| 401 | `EigenpalAuthError` |
|
|
115
|
-
| 403 | `EigenpalForbiddenError` |
|
|
116
|
-
| 404 | `EigenpalNotFoundError` |
|
|
117
|
-
| 429 | `EigenpalRateLimitError` |
|
|
118
|
-
| 5xx | `EigenpalServerError` |
|
|
119
|
-
| timeout / abort | `EigenpalTimeoutError` |
|
|
41
|
+
## Guides
|
|
120
42
|
|
|
121
|
-
|
|
43
|
+
- [Configuration](./docs/configuration.md): API key, base URL, project, timeouts, retries.
|
|
44
|
+
- [Automations](./docs/automations.md): automations, status, release, promote, rollback.
|
|
45
|
+
- [Runs](./docs/runs.md): run, wait, logs, download, rerun, cancel.
|
|
46
|
+
- [Evaluation](./docs/evaluation.md): eval, experiments, dataset reviews.
|
|
47
|
+
- [Files](./docs/files.md): file inputs and uploads.
|
|
48
|
+
- [Errors](./docs/errors.md): the error classes.
|
|
49
|
+
- [Reference](./docs/reference.md): every method, generated from the API and the CLI.
|
|
50
|
+
- [Migrating from 0.x](./docs/migration.md).
|
|
122
51
|
|
|
123
|
-
|
|
124
|
-
| ----------------------------------------- | ------------------------------------------------------------------ |
|
|
125
|
-
| [Automations](./docs/workflows.md) | List, inspect, move, delete, versions, triggers, folders. |
|
|
126
|
-
| [Runs](./docs/executions.md) | Start, poll, cancel, rerun, usage, steps, events, traces, reviews. |
|
|
127
|
-
| [File inputs](./docs/files.md) | Multipart upload from File, Blob, Buffer, or path. |
|
|
128
|
-
| [Errors](./docs/errors.md) | Typed exceptions, retries, request ids. |
|
|
129
|
-
| [Configuration](./docs/configuration.md) | API key, baseUrl, timeouts, headers. |
|
|
130
|
-
| [Full API reference](./docs/reference.md) | Every method, generated from OpenAPI. |
|
|
52
|
+
Anything in the [API reference](https://docs.eigenpal.com/api-reference) without a method is one call away: `client.api('GET', '/v1/runs/exec_123/scores')`.
|
|
131
53
|
|
|
132
54
|
## License
|
|
133
55
|
|
|
134
|
-
Apache-2.0
|
|
56
|
+
Apache-2.0
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `EigenPalClient`: the generated surface (one method per CLI command, same
|
|
3
|
+
* names) plus the hand-written project verbs `run`, `eval`, `release`,
|
|
4
|
+
* `promote`, `rollback` and the `api` escape hatch.
|
|
5
|
+
*/
|
|
6
|
+
import { type CoreOptions } from './core.js';
|
|
7
|
+
import { EigenPalClientBase } from './generated/resources.gen.js';
|
|
8
|
+
import type { CreatedProjectRelease, ExperimentDetail, ProjectPromotion, ProjectRollback, PromotionOperation, ReleaseOperation, Run, RunAccepted } from './generated/types.gen.js';
|
|
9
|
+
export interface EigenPalOptions extends CoreOptions {
|
|
10
|
+
/**
|
|
11
|
+
* API key from Settings > API Keys. Defaults to `EIGENPAL_API_KEY`.
|
|
12
|
+
*/
|
|
13
|
+
apiKey?: string;
|
|
14
|
+
/** API base URL. Defaults to `EIGENPAL_BASE_URL`, then `https://api.eigenpal.com`. */
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
/** Project (slug or id) for project routes and name lookups. Defaults to `EIGENPAL_PROJECT`. */
|
|
17
|
+
project?: string;
|
|
18
|
+
/** Per-request timeout in milliseconds (default 60 000). */
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
/** Retries on 429, 5xx and network errors (default 3). */
|
|
21
|
+
maxRetries?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Largest multipart run body before files are uploaded first (default
|
|
24
|
+
* `EIGENPAL_MULTIPART_MAX_BYTES` or 4.5 MiB); `null` keeps every file in the
|
|
25
|
+
* run request (self-hosted).
|
|
26
|
+
*/
|
|
27
|
+
multipartMaxBytes?: number | null;
|
|
28
|
+
}
|
|
29
|
+
export type RunInput = Record<string, unknown>;
|
|
30
|
+
export interface RunOptions {
|
|
31
|
+
/**
|
|
32
|
+
* What to run: `live` (default), a release (`1.4.0`), a range of releases
|
|
33
|
+
* (`^1.4`) or a 40-character commit sha. Also `name@version` in the first
|
|
34
|
+
* argument.
|
|
35
|
+
*/
|
|
36
|
+
version?: string;
|
|
37
|
+
tags?: string[];
|
|
38
|
+
/** Project slug. Also `project/name` in the first argument. */
|
|
39
|
+
project?: string;
|
|
40
|
+
overrides?: {
|
|
41
|
+
steps?: Record<string, Record<string, unknown>>;
|
|
42
|
+
};
|
|
43
|
+
metadata?: Record<string, unknown>;
|
|
44
|
+
/** Wait for the outcome (default true, as `eigenpal run`). `false` answers as soon as it is queued. */
|
|
45
|
+
wait?: boolean;
|
|
46
|
+
/** How long to wait (default 30 minutes); then `EigenPalTimeoutError` with `.record`. */
|
|
47
|
+
timeoutMs?: number;
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
}
|
|
50
|
+
export interface EvalOptions {
|
|
51
|
+
/** Example names; default every example of the dataset. */
|
|
52
|
+
examples?: string[];
|
|
53
|
+
/** A released version to evaluate; default what is live. */
|
|
54
|
+
version?: string;
|
|
55
|
+
/** Runs at the same time. */
|
|
56
|
+
concurrency?: number;
|
|
57
|
+
tags?: string[];
|
|
58
|
+
project?: string;
|
|
59
|
+
wait?: boolean;
|
|
60
|
+
timeoutMs?: number;
|
|
61
|
+
signal?: AbortSignal;
|
|
62
|
+
}
|
|
63
|
+
export interface LifecycleOptions {
|
|
64
|
+
project?: string;
|
|
65
|
+
/** Answer what would happen (`planned`) and change nothing. */
|
|
66
|
+
dryRun?: boolean;
|
|
67
|
+
wait?: boolean;
|
|
68
|
+
timeoutMs?: number;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The EigenPal API client. Methods mirror the CLI: `eigenpal runs get <run>`
|
|
73
|
+
* is `client.runs.get(run)`.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* const client = new EigenPalClient({ project: 'finance-ops' }); // EIGENPAL_API_KEY from env
|
|
77
|
+
* const run = await client.run('invoice-extract', { document: { path: './invoice.pdf' } });
|
|
78
|
+
* for await (const r of client.runs.list({ state: 'failed' })) console.log(r.id);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare class EigenPalClient extends EigenPalClientBase {
|
|
82
|
+
#private;
|
|
83
|
+
constructor(options?: EigenPalOptions);
|
|
84
|
+
/**
|
|
85
|
+
* Run an automation (`[<project>/]<name>[@<version>]`) and wait for the
|
|
86
|
+
* outcome: the answer is the `Run`. With `wait: false` the answer is
|
|
87
|
+
* `RunAccepted`, as soon as it is queued. Inputs are values or files
|
|
88
|
+
* (`{ path }`, Blob, `toFile(...)`); large files are uploaded first.
|
|
89
|
+
* A failed run is data (`state: 'failed'`), not an exception.
|
|
90
|
+
*
|
|
91
|
+
* CLI: `eigenpal run [<project>/]<automation>[@<version>]` · Route: `POST /v1/runs`
|
|
92
|
+
*/
|
|
93
|
+
run(automation: string, input: RunInput | undefined, options: RunOptions & {
|
|
94
|
+
wait: false;
|
|
95
|
+
}): Promise<RunAccepted>;
|
|
96
|
+
run(automation: string, input?: RunInput, options?: RunOptions & {
|
|
97
|
+
wait?: true;
|
|
98
|
+
}): Promise<Run>;
|
|
99
|
+
run(automation: string, input?: RunInput, options?: RunOptions): Promise<Run | RunAccepted>;
|
|
100
|
+
/**
|
|
101
|
+
* Evaluate an automation on its dataset (what is live, or `version`) and
|
|
102
|
+
* wait for the scores.
|
|
103
|
+
*
|
|
104
|
+
* CLI: `eigenpal eval <automation>[@<version>]` · Route: `POST /v1/automations/{id}/experiments`
|
|
105
|
+
*/
|
|
106
|
+
eval(automation: string, options?: EvalOptions): Promise<ExperimentDetail>;
|
|
107
|
+
/**
|
|
108
|
+
* Release the project: `patch`, `minor`, `major` or `X.Y.Z`. From a branch
|
|
109
|
+
* (`branch`) it lands the branch first. The server works on it as an
|
|
110
|
+
* operation; this polls it until it ends (`state`: succeeded with the
|
|
111
|
+
* release in `result`, failed with `error`, or waiting for a person to
|
|
112
|
+
* merge the request). `wait: false` answers the operation at once;
|
|
113
|
+
* `dryRun` answers the plan.
|
|
114
|
+
*
|
|
115
|
+
* CLI: `eigenpal release <patch|minor|major|X.Y.Z>` · Route: `POST /v1/projects/{id}/releases`
|
|
116
|
+
*/
|
|
117
|
+
release(size: string, options: LifecycleOptions & {
|
|
118
|
+
notes?: string;
|
|
119
|
+
ref?: string;
|
|
120
|
+
branch?: string;
|
|
121
|
+
dryRun: true;
|
|
122
|
+
}): Promise<CreatedProjectRelease>;
|
|
123
|
+
release(size: string, options?: LifecycleOptions & {
|
|
124
|
+
notes?: string;
|
|
125
|
+
ref?: string;
|
|
126
|
+
branch?: string;
|
|
127
|
+
}): Promise<ReleaseOperation>;
|
|
128
|
+
/**
|
|
129
|
+
* Move a release or a checked commit to the next environment (`environment`).
|
|
130
|
+
* The server works on it as an operation (it builds the source first when
|
|
131
|
+
* needed); this polls it until the request is open (`result`) or it failed.
|
|
132
|
+
* `wait: false` answers the operation at once; `dryRun` answers the plan.
|
|
133
|
+
*
|
|
134
|
+
* CLI: `eigenpal promote [version]` · Route: `POST /v1/projects/{id}/promote`
|
|
135
|
+
*/
|
|
136
|
+
promote(version: string | undefined, options: LifecycleOptions & {
|
|
137
|
+
environment?: string;
|
|
138
|
+
ref?: string;
|
|
139
|
+
dryRun: true;
|
|
140
|
+
}): Promise<ProjectPromotion>;
|
|
141
|
+
promote(version?: string, options?: LifecycleOptions & {
|
|
142
|
+
environment?: string;
|
|
143
|
+
ref?: string;
|
|
144
|
+
}): Promise<PromotionOperation>;
|
|
145
|
+
/**
|
|
146
|
+
* Make the build of an earlier release live again (default the one live
|
|
147
|
+
* before). The release left is withdrawn unless `keepRelease`.
|
|
148
|
+
*
|
|
149
|
+
* CLI: `eigenpal rollback [version]` · Route: `POST /v1/projects/{id}/rollback`
|
|
150
|
+
*/
|
|
151
|
+
rollback(version?: string, options?: Omit<LifecycleOptions, 'wait' | 'timeoutMs'> & {
|
|
152
|
+
keepRelease?: boolean;
|
|
153
|
+
environment?: string;
|
|
154
|
+
}): Promise<ProjectRollback>;
|
|
155
|
+
/**
|
|
156
|
+
* Any route of the API reference, by method and path (`/v1/...`): the
|
|
157
|
+
* escape hatch, as `eigenpal api`.
|
|
158
|
+
*/
|
|
159
|
+
api(method: string, path: string, options?: {
|
|
160
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
161
|
+
body?: unknown;
|
|
162
|
+
signal?: AbortSignal;
|
|
163
|
+
}): Promise<unknown>;
|
|
164
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `EigenPalClient`: the generated surface (one method per CLI command, same
|
|
3
|
+
* names) plus the hand-written project verbs `run`, `eval`, `release`,
|
|
4
|
+
* `promote`, `rollback` and the `api` escape hatch.
|
|
5
|
+
*/
|
|
6
|
+
import { Core, readEnv } from './core.js';
|
|
7
|
+
import { EigenPalError } from './errors.js';
|
|
8
|
+
import { EigenPalClientBase } from './generated/resources.gen.js';
|
|
9
|
+
import { buildRunJsonBody, buildRunMultipart, hasFileInput, isFileInput, isPathFileInput, isReadStream, resolveFileBlob, statUploadSize, } from './lib/files.js';
|
|
10
|
+
import { DEFAULT_MULTIPART_MAX_BYTES, keysRequiringPreUpload } from './lib/upload-limits.js';
|
|
11
|
+
import { RESOURCE_IMPLS, runSettled, waitUntil } from './resources.js';
|
|
12
|
+
import { uploadFile } from './upload-file.js';
|
|
13
|
+
/**
|
|
14
|
+
* The EigenPal API client. Methods mirror the CLI: `eigenpal runs get <run>`
|
|
15
|
+
* is `client.runs.get(run)`.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const client = new EigenPalClient({ project: 'finance-ops' }); // EIGENPAL_API_KEY from env
|
|
19
|
+
* const run = await client.run('invoice-extract', { document: { path: './invoice.pdf' } });
|
|
20
|
+
* for await (const r of client.runs.list({ state: 'failed' })) console.log(r.id);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export class EigenPalClient extends EigenPalClientBase {
|
|
24
|
+
#multipartMaxBytes;
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
const core = new Core(options);
|
|
27
|
+
super(core, RESOURCE_IMPLS);
|
|
28
|
+
this.#multipartMaxBytes = resolveMultipartMaxBytes(options.multipartMaxBytes);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The live automation of a bare name in the project. The server answers 409
|
|
32
|
+
* `ambiguous_automation` when a workflow and an agent share the name.
|
|
33
|
+
*/
|
|
34
|
+
async #automation(name, project, signal) {
|
|
35
|
+
const detail = await this.core.request('automations.get', {
|
|
36
|
+
path: { id: name },
|
|
37
|
+
query: { project: project ?? this.core.project },
|
|
38
|
+
signal,
|
|
39
|
+
});
|
|
40
|
+
return { detail };
|
|
41
|
+
}
|
|
42
|
+
async run(automation, input = {}, options = {}) {
|
|
43
|
+
const address = parseAddress(automation);
|
|
44
|
+
const prepared = await this.#prepareRunFileInputs(input, options.signal);
|
|
45
|
+
const wait = options.wait ?? true;
|
|
46
|
+
const start = {
|
|
47
|
+
project: options.project ?? address.project ?? this.core.project,
|
|
48
|
+
automation: address.automation,
|
|
49
|
+
version: options.version ?? address.version,
|
|
50
|
+
// The server holds a short run's answer, so it often returns in one call.
|
|
51
|
+
wait: wait ? SERVER_HOLD_SECONDS : undefined,
|
|
52
|
+
};
|
|
53
|
+
const extra = {
|
|
54
|
+
tags: options.tags,
|
|
55
|
+
overrides: options.overrides,
|
|
56
|
+
metadata: options.metadata,
|
|
57
|
+
};
|
|
58
|
+
const body = hasFileInput(prepared)
|
|
59
|
+
? (await buildRunMultipart({ ...start, ...extra, input: prepared })).formData
|
|
60
|
+
: buildRunJsonBody(start, prepared, extra);
|
|
61
|
+
const started = await this.core.request('runs.start', {
|
|
62
|
+
body,
|
|
63
|
+
signal: options.signal,
|
|
64
|
+
});
|
|
65
|
+
if (!wait)
|
|
66
|
+
return started;
|
|
67
|
+
if (runSettled(started))
|
|
68
|
+
return started;
|
|
69
|
+
return waitUntil(this.core, () => this.core.request('runs.get', { path: { id: started.id }, signal: options.signal }), runSettled, options);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Evaluate an automation on its dataset (what is live, or `version`) and
|
|
73
|
+
* wait for the scores.
|
|
74
|
+
*
|
|
75
|
+
* CLI: `eigenpal eval <automation>[@<version>]` · Route: `POST /v1/automations/{id}/experiments`
|
|
76
|
+
*/
|
|
77
|
+
async eval(automation, options = {}) {
|
|
78
|
+
const address = parseAddress(automation);
|
|
79
|
+
const { detail } = await this.#automation(address.automation, options.project ?? address.project, options.signal);
|
|
80
|
+
const version = options.version ?? address.version;
|
|
81
|
+
const created = await this.core.request('automations.experiments.create', {
|
|
82
|
+
path: { id: detail.id },
|
|
83
|
+
body: {
|
|
84
|
+
...(options.examples ? { examples: options.examples } : {}),
|
|
85
|
+
...(version ? { version } : {}),
|
|
86
|
+
...(options.concurrency ? { concurrency: options.concurrency } : {}),
|
|
87
|
+
...(options.tags ? { tags: options.tags } : {}),
|
|
88
|
+
},
|
|
89
|
+
signal: options.signal,
|
|
90
|
+
});
|
|
91
|
+
const read = () => this.core.request('automations.experiments.get', {
|
|
92
|
+
path: { id: detail.id, experimentId: created.id },
|
|
93
|
+
signal: options.signal,
|
|
94
|
+
});
|
|
95
|
+
if (options.wait === false)
|
|
96
|
+
return read();
|
|
97
|
+
return waitUntil(this.core, read, (e) => ['succeeded', 'failed', 'cancelled'].includes(String(e.state)), options);
|
|
98
|
+
}
|
|
99
|
+
release(size, options = {}) {
|
|
100
|
+
return this.core.request('projects.releases.create', {
|
|
101
|
+
body: {
|
|
102
|
+
version: size,
|
|
103
|
+
notes: options.notes,
|
|
104
|
+
ref: options.ref,
|
|
105
|
+
branch: options.branch,
|
|
106
|
+
dryRun: options.dryRun,
|
|
107
|
+
},
|
|
108
|
+
project: options.project,
|
|
109
|
+
signal: options.signal,
|
|
110
|
+
}, { project: 'path', wait: operationWait(options) });
|
|
111
|
+
}
|
|
112
|
+
promote(version, options = {}) {
|
|
113
|
+
return this.core.request('projects.promote', {
|
|
114
|
+
body: {
|
|
115
|
+
environment: options.environment,
|
|
116
|
+
ref: version ?? options.ref,
|
|
117
|
+
dryRun: options.dryRun,
|
|
118
|
+
},
|
|
119
|
+
project: options.project,
|
|
120
|
+
signal: options.signal,
|
|
121
|
+
}, { project: 'path', wait: operationWait(options) });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Make the build of an earlier release live again (default the one live
|
|
125
|
+
* before). The release left is withdrawn unless `keepRelease`.
|
|
126
|
+
*
|
|
127
|
+
* CLI: `eigenpal rollback [version]` · Route: `POST /v1/projects/{id}/rollback`
|
|
128
|
+
*/
|
|
129
|
+
rollback(version, options = {}) {
|
|
130
|
+
return this.core.request('projects.rollback', {
|
|
131
|
+
body: {
|
|
132
|
+
to: version,
|
|
133
|
+
keepRelease: options.keepRelease,
|
|
134
|
+
environment: options.environment,
|
|
135
|
+
dryRun: options.dryRun,
|
|
136
|
+
},
|
|
137
|
+
project: options.project,
|
|
138
|
+
signal: options.signal,
|
|
139
|
+
}, { project: 'path' });
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Any route of the API reference, by method and path (`/v1/...`): the
|
|
143
|
+
* escape hatch, as `eigenpal api`.
|
|
144
|
+
*/
|
|
145
|
+
api(method, path, options = {}) {
|
|
146
|
+
return this.core.api(method, path, options);
|
|
147
|
+
}
|
|
148
|
+
/** Upload files that would push the multipart run body over the limit; keep the rest inline. */
|
|
149
|
+
async #prepareRunFileInputs(input, signal) {
|
|
150
|
+
if (!hasFileInput(input))
|
|
151
|
+
return input;
|
|
152
|
+
const resolved = [];
|
|
153
|
+
for (const [key, value] of Object.entries(input)) {
|
|
154
|
+
if (!isFileInput(value))
|
|
155
|
+
continue;
|
|
156
|
+
if (isPathFileInput(value) || (isReadStream(value) && typeof value.path === 'string')) {
|
|
157
|
+
try {
|
|
158
|
+
const meta = await statUploadSize(value);
|
|
159
|
+
const file = isPathFileInput(value) ? value : { path: String(value.path) };
|
|
160
|
+
resolved.push({ key, file, filename: meta.filename, size: meta.size });
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// An unreadable path or a fake stream still goes inline.
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const { blob, filename } = await resolveFileBlob(value);
|
|
168
|
+
resolved.push({ key, file: blob, filename, size: blob.size });
|
|
169
|
+
}
|
|
170
|
+
const upload = keysRequiringPreUpload(resolved.map(({ key, size }) => ({ key, size })), this.#multipartMaxBytes);
|
|
171
|
+
const next = { ...input };
|
|
172
|
+
for (const { key, file, filename } of resolved) {
|
|
173
|
+
if (upload.has(key)) {
|
|
174
|
+
const uploaded = await uploadFile(this.core, file, {
|
|
175
|
+
filename,
|
|
176
|
+
signal,
|
|
177
|
+
purpose: 'run_input',
|
|
178
|
+
});
|
|
179
|
+
next[key] = { $fileId: uploaded.id };
|
|
180
|
+
}
|
|
181
|
+
else if (typeof Blob !== 'undefined' && file instanceof Blob) {
|
|
182
|
+
next[key] = new File([file], filename, { type: file.type || 'application/octet-stream' });
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
next[key] = file;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return next;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** Seconds the server holds a run start before answering `202`. */
|
|
192
|
+
const SERVER_HOLD_SECONDS = 30;
|
|
193
|
+
/** `[<project>/]<automation>[@<version>]`, as the CLI writes it. The server checks the parts. */
|
|
194
|
+
function parseAddress(text) {
|
|
195
|
+
const at = text.indexOf('@');
|
|
196
|
+
const path = at < 0 ? text : text.slice(0, at);
|
|
197
|
+
const slash = path.indexOf('/');
|
|
198
|
+
return {
|
|
199
|
+
...(slash < 0 ? {} : { project: path.slice(0, slash) }),
|
|
200
|
+
automation: path.slice(slash + 1),
|
|
201
|
+
...(at < 0 ? {} : { version: text.slice(at + 1) }),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** Poll a release or promotion operation until it ends (not for a dry run, which answers a plan). */
|
|
205
|
+
function operationWait(options) {
|
|
206
|
+
return {
|
|
207
|
+
enabled: !options.dryRun && (options.wait ?? true),
|
|
208
|
+
poll: 'projects.operations.get',
|
|
209
|
+
idParam: 'operationId',
|
|
210
|
+
field: 'state',
|
|
211
|
+
final: ['succeeded', 'failed', 'waiting'],
|
|
212
|
+
timeoutMs: options.timeoutMs,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function resolveMultipartMaxBytes(explicit) {
|
|
216
|
+
if (explicit !== undefined) {
|
|
217
|
+
if (explicit === null)
|
|
218
|
+
return null;
|
|
219
|
+
if (Number.isSafeInteger(explicit) && explicit >= 0)
|
|
220
|
+
return explicit;
|
|
221
|
+
throw new EigenPalError('multipartMaxBytes must be a non-negative integer or null.', {
|
|
222
|
+
status: 0,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
const raw = readEnv('EIGENPAL_MULTIPART_MAX_BYTES')?.trim().toLowerCase();
|
|
226
|
+
if (!raw)
|
|
227
|
+
return DEFAULT_MULTIPART_MAX_BYTES;
|
|
228
|
+
if (raw === 'none' || raw === 'null' || raw === 'unlimited')
|
|
229
|
+
return null;
|
|
230
|
+
const parsed = Number(raw);
|
|
231
|
+
if (Number.isSafeInteger(parsed) && parsed >= 0)
|
|
232
|
+
return parsed;
|
|
233
|
+
throw new EigenPalError('EIGENPAL_MULTIPART_MAX_BYTES must be a non-negative integer or one of: none, null, unlimited.', { status: 0 });
|
|
234
|
+
}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type OperationId } from './generated/operations.gen.js';
|
|
2
|
+
import { PagePromise, type PageShape } from './page.js';
|
|
3
|
+
export interface CoreOptions {
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
project?: string;
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
maxRetries?: number;
|
|
9
|
+
fetch?: typeof globalThis.fetch;
|
|
10
|
+
defaultHeaders?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
export interface CallRequest {
|
|
13
|
+
path?: Record<string, string | undefined>;
|
|
14
|
+
query?: Record<string, unknown>;
|
|
15
|
+
body?: unknown;
|
|
16
|
+
project?: string;
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
export interface WaitSpec {
|
|
21
|
+
enabled: boolean;
|
|
22
|
+
/** Operation to poll with the answer's `id`. */
|
|
23
|
+
poll: OperationId;
|
|
24
|
+
/** The path parameter that takes the answer's `id` (default `id`). */
|
|
25
|
+
idParam?: string;
|
|
26
|
+
field: string;
|
|
27
|
+
final: readonly string[];
|
|
28
|
+
timeoutMs?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface CallMeta {
|
|
31
|
+
project?: 'path' | 'query' | 'body';
|
|
32
|
+
binary?: boolean;
|
|
33
|
+
wait?: WaitSpec;
|
|
34
|
+
}
|
|
35
|
+
export declare const DEFAULT_BASE_URL = "https://api.eigenpal.com";
|
|
36
|
+
export declare const DEFAULT_WAIT_TIMEOUT_MS: number;
|
|
37
|
+
/** Read one env var as a fallback (a published SDK cannot use `@eigenpal/env`). */
|
|
38
|
+
export declare function readEnv(name: string): string | undefined;
|
|
39
|
+
export declare const sleep: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
40
|
+
export declare class Core {
|
|
41
|
+
readonly baseUrl: string;
|
|
42
|
+
readonly project: string | undefined;
|
|
43
|
+
private readonly apiKey;
|
|
44
|
+
private readonly timeoutMs;
|
|
45
|
+
private readonly maxRetries;
|
|
46
|
+
private readonly fetchImpl;
|
|
47
|
+
private readonly headers;
|
|
48
|
+
/** Clock and sleep, replaceable in tests. */
|
|
49
|
+
sleep: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
50
|
+
now: () => number;
|
|
51
|
+
constructor(options?: CoreOptions);
|
|
52
|
+
/** Call one operation of the table. */
|
|
53
|
+
request<T>(op: OperationId, req: CallRequest, meta?: CallMeta): Promise<T>;
|
|
54
|
+
/** Call a list operation and answer its page, iterable over every item. */
|
|
55
|
+
page<T, R>(op: OperationId, req: CallRequest, meta: CallMeta & {
|
|
56
|
+
list: PageShape;
|
|
57
|
+
}): PagePromise<T, R>;
|
|
58
|
+
/** The escape hatch: any route, by method and path (`/v1/...`). */
|
|
59
|
+
api(method: string, path: string, options?: {
|
|
60
|
+
query?: Record<string, unknown>;
|
|
61
|
+
body?: unknown;
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
}): Promise<unknown>;
|
|
64
|
+
/** Raw response of an operation (streams, files); the status is checked. */
|
|
65
|
+
raw(op: OperationId, req: CallRequest, meta?: CallMeta): Promise<Response>;
|
|
66
|
+
private withProject;
|
|
67
|
+
private send;
|
|
68
|
+
private waitFor;
|
|
69
|
+
/** `baseUrl` + `/v1/...`, without a doubled `/api/v1` or `/v1`. */
|
|
70
|
+
url(path: string, query?: Record<string, unknown>): string;
|
|
71
|
+
fetchWithRetries(method: string, url: string, body: unknown, options: {
|
|
72
|
+
signal?: AbortSignal;
|
|
73
|
+
idempotent: boolean;
|
|
74
|
+
headers?: Record<string, string>;
|
|
75
|
+
binary?: boolean;
|
|
76
|
+
}): Promise<Response>;
|
|
77
|
+
private readJson;
|
|
78
|
+
/** The API key (for upload helpers that talk to the same host). */
|
|
79
|
+
get authorization(): string;
|
|
80
|
+
}
|
|
81
|
+
export declare function fillPath(template: string, params?: Record<string, string | undefined>): string;
|
|
82
|
+
export declare function parseRetryAfter(value: string | null): number | undefined;
|