@eigenpal/sdk 0.25.12 → 1.0.0-rc.1
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The official TypeScript SDK for the EigenPal API. Every method mirrors a
|
|
3
|
+
* CLI command and calls the same route (`docs/reference.md`).
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { EigenPalClient } from '@eigenpal/sdk';
|
|
7
|
+
*
|
|
8
|
+
* const client = new EigenPalClient({ project: 'finance-ops' });
|
|
9
|
+
* const run = await client.run('invoice-extract', { document: { path: './invoice.pdf' } });
|
|
10
|
+
* console.log(run.state, run.output);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export { EigenPalClient, type EigenPalOptions, type EvalOptions, type LifecycleOptions, type RunInput, type RunOptions, } from './client.js';
|
|
14
|
+
export { EigenPalAuthError, EigenPalConflictError, EigenPalError, EigenPalForbiddenError, EigenPalNotFoundError, EigenPalPayloadTooLargeError, EigenPalRateLimitError, EigenPalServerError, EigenPalTimeoutError, EigenPalValidationError, } from './errors.js';
|
|
15
|
+
export type * from './generated/public.gen.js';
|
|
16
|
+
export { toFile } from './lib/files.js';
|
|
17
|
+
export type { FileDescriptor, FileInput, NodeReadableStream, PathFileInput, StreamFactoryFileInput, } from './lib/files.js';
|
|
18
|
+
export type { Page, PagePromise } from './page.js';
|
|
19
|
+
export type { DatasetsReviews, EmailServers, Experiments, Files, Runs, Templates, } from './resources.js';
|
|
20
|
+
export type { UploadOptions } from './upload-file.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The official TypeScript SDK for the EigenPal API. Every method mirrors a
|
|
3
|
+
* CLI command and calls the same route (`docs/reference.md`).
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { EigenPalClient } from '@eigenpal/sdk';
|
|
7
|
+
*
|
|
8
|
+
* const client = new EigenPalClient({ project: 'finance-ops' });
|
|
9
|
+
* const run = await client.run('invoice-extract', { document: { path: './invoice.pdf' } });
|
|
10
|
+
* console.log(run.state, run.output);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export { EigenPalClient, } from './client.js';
|
|
14
|
+
export { EigenPalAuthError, EigenPalConflictError, EigenPalError, EigenPalForbiddenError, EigenPalNotFoundError, EigenPalPayloadTooLargeError, EigenPalRateLimitError, EigenPalServerError, EigenPalTimeoutError, EigenPalValidationError, } from './errors.js';
|
|
15
|
+
export { toFile } from './lib/files.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Request init for Node/Bun fetch when streaming a Node readable body. */
|
|
2
|
+
export type StreamableRequestInit = RequestInit & {
|
|
3
|
+
duplex?: 'half';
|
|
4
|
+
};
|
|
5
|
+
/** True when `body` is a Node.js readable stream (fs.createReadStream, etc.). */
|
|
6
|
+
export declare function isNodeReadableStream(body: unknown): body is NodeJS.ReadableStream;
|
|
7
|
+
/**
|
|
8
|
+
* Copy a byte view into a standalone ArrayBuffer accepted by DOM `BodyInit`.
|
|
9
|
+
* Avoids TS generic mismatches on `Uint8Array<ArrayBufferLike>`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function byteViewToArrayBuffer(view: Uint8Array): ArrayBuffer;
|
|
12
|
+
/** Convert bytes or a Node readable into a fetch-compatible PUT body. */
|
|
13
|
+
export declare function toPutBody(body: Uint8Array | NodeJS.ReadableStream): BodyInit;
|
|
14
|
+
export declare function putRequestInit(body: Uint8Array | NodeJS.ReadableStream, headers: HeadersInit, signal?: AbortSignal): StreamableRequestInit;
|
|
15
|
+
/** Drop a Node readable that never started sending (fetch failed before upload). */
|
|
16
|
+
export declare function destroyUnreadNodeReadable(stream: NodeJS.ReadableStream): void;
|
|
17
|
+
/**
|
|
18
|
+
* Block until every byte of a Node readable PUT body has been consumed.
|
|
19
|
+
* Real fetch/undici should drain the stream before resolving; mocks that
|
|
20
|
+
* return early leave bytes unread and must be drained here so callers do
|
|
21
|
+
* not destroy the stream while a retry or background upload is still running.
|
|
22
|
+
*/
|
|
23
|
+
export declare function ensureNodeReadableFullySent(stream: NodeJS.ReadableStream, expectedByteLength?: number): Promise<number>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** True when `body` is a Node.js readable stream (fs.createReadStream, etc.). */
|
|
2
|
+
export function isNodeReadableStream(body) {
|
|
3
|
+
return typeof body.pipe === 'function';
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Copy a byte view into a standalone ArrayBuffer accepted by DOM `BodyInit`.
|
|
7
|
+
* Avoids TS generic mismatches on `Uint8Array<ArrayBufferLike>`.
|
|
8
|
+
*/
|
|
9
|
+
export function byteViewToArrayBuffer(view) {
|
|
10
|
+
if (view.buffer instanceof ArrayBuffer &&
|
|
11
|
+
view.byteOffset === 0 &&
|
|
12
|
+
view.byteLength === view.buffer.byteLength) {
|
|
13
|
+
return view.buffer;
|
|
14
|
+
}
|
|
15
|
+
const copy = new Uint8Array(view.byteLength);
|
|
16
|
+
copy.set(view);
|
|
17
|
+
return copy.buffer;
|
|
18
|
+
}
|
|
19
|
+
/** Convert bytes or a Node readable into a fetch-compatible PUT body. */
|
|
20
|
+
export function toPutBody(body) {
|
|
21
|
+
if (isNodeReadableStream(body)) {
|
|
22
|
+
// Node/Bun fetch accepts Node readables with `duplex: 'half'`.
|
|
23
|
+
return body;
|
|
24
|
+
}
|
|
25
|
+
return byteViewToArrayBuffer(body);
|
|
26
|
+
}
|
|
27
|
+
export function putRequestInit(body, headers, signal) {
|
|
28
|
+
const stream = isNodeReadableStream(body);
|
|
29
|
+
return {
|
|
30
|
+
method: 'PUT',
|
|
31
|
+
headers,
|
|
32
|
+
body: toPutBody(body),
|
|
33
|
+
signal,
|
|
34
|
+
...(stream ? { duplex: 'half' } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Drop a Node readable that never started sending (fetch failed before upload). */
|
|
38
|
+
export function destroyUnreadNodeReadable(stream) {
|
|
39
|
+
const readable = stream;
|
|
40
|
+
if (!readable.readableEnded && !readable.destroyed && typeof readable.destroy === 'function') {
|
|
41
|
+
readable.destroy();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Block until every byte of a Node readable PUT body has been consumed.
|
|
46
|
+
* Real fetch/undici should drain the stream before resolving; mocks that
|
|
47
|
+
* return early leave bytes unread and must be drained here so callers do
|
|
48
|
+
* not destroy the stream while a retry or background upload is still running.
|
|
49
|
+
*/
|
|
50
|
+
export async function ensureNodeReadableFullySent(stream, expectedByteLength) {
|
|
51
|
+
const readable = stream;
|
|
52
|
+
if (readable.readableEnded) {
|
|
53
|
+
return expectedByteLength ?? 0;
|
|
54
|
+
}
|
|
55
|
+
if (readable.destroyed) {
|
|
56
|
+
throw new Error('Upload stream was destroyed before the request body finished sending');
|
|
57
|
+
}
|
|
58
|
+
let bytes = 0;
|
|
59
|
+
for await (const chunk of stream) {
|
|
60
|
+
bytes += typeof chunk === 'string' ? Buffer.byteLength(chunk) : chunk.length;
|
|
61
|
+
}
|
|
62
|
+
if (expectedByteLength !== undefined && bytes !== expectedByteLength) {
|
|
63
|
+
throw new Error(`Upload sent ${bytes} byte(s) but ${expectedByteLength} were expected for this part`);
|
|
64
|
+
}
|
|
65
|
+
return bytes;
|
|
66
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File-input helpers for `client.run()`.
|
|
3
|
+
*
|
|
4
|
+
* A workflow input value is treated as a file when it is one of:
|
|
5
|
+
* - a Node readable stream — `fs.createReadStream('contract.pdf')`
|
|
6
|
+
* - a `File` or `Blob`
|
|
7
|
+
* - a `{ content, filename, mimeType? }` descriptor — build one with `toFile()`
|
|
8
|
+
*
|
|
9
|
+
* File values are auto-detected and uploaded as `multipart/form-data`
|
|
10
|
+
* (matching `curl -F`) — no base64 round-trip.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Explicit file descriptor — raw bytes plus metadata. Build one with
|
|
14
|
+
* `toFile()` when you have a `Buffer` / `Uint8Array` / `ArrayBuffer` / `Blob`
|
|
15
|
+
* in memory rather than a path or a stream.
|
|
16
|
+
*/
|
|
17
|
+
export interface FileDescriptor {
|
|
18
|
+
content: ArrayBuffer | ArrayBufferView | Blob;
|
|
19
|
+
filename: string;
|
|
20
|
+
mimeType?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Disk path that can be stat'd and re-opened per part for resumable multipart.
|
|
24
|
+
*/
|
|
25
|
+
export interface PathFileInput {
|
|
26
|
+
path: string;
|
|
27
|
+
filename?: string;
|
|
28
|
+
mimeType?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Replayable byte-range factory for Node streams that are not a disk path.
|
|
32
|
+
* `open` must return a fresh body for `[start, start+length)` on every call.
|
|
33
|
+
*/
|
|
34
|
+
export interface StreamFactoryFileInput {
|
|
35
|
+
size: number;
|
|
36
|
+
filename: string;
|
|
37
|
+
mimeType?: string;
|
|
38
|
+
open: (start: number, length: number) => Blob | Uint8Array | ReadableStream | NodeJS.ReadableStream;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A Node readable stream — typically `fs.createReadStream('contract.pdf')`.
|
|
42
|
+
* Streams with a `path` are uploaded from disk without draining. A one-shot
|
|
43
|
+
* stream without a path cannot be multipart-resumed.
|
|
44
|
+
*/
|
|
45
|
+
export interface NodeReadableStream extends AsyncIterable<unknown> {
|
|
46
|
+
/** Source path; used to infer the upload filename. */
|
|
47
|
+
readonly path?: string;
|
|
48
|
+
}
|
|
49
|
+
/** Any value the SDK accepts as a "file" workflow input. */
|
|
50
|
+
export type FileInput = Blob | FileDescriptor | PathFileInput | StreamFactoryFileInput | NodeReadableStream;
|
|
51
|
+
/**
|
|
52
|
+
* Attach a filename (and optional MIME type) to raw bytes. The escape hatch
|
|
53
|
+
* for when you have a `Buffer` / `ArrayBuffer` / `Blob` in memory rather than
|
|
54
|
+
* a file path or stream.
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* await client.run('workflows.extract-invoice', {
|
|
58
|
+
* contract_document: toFile(buffer, 'contract.pdf'),
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare function toFile(content: ArrayBuffer | ArrayBufferView | Blob, filename: string, mimeType?: string): FileDescriptor;
|
|
63
|
+
/** Detect a Node readable stream (`fs.createReadStream`, etc.). */
|
|
64
|
+
export declare function isReadStream(value: unknown): value is NodeReadableStream;
|
|
65
|
+
/** Detect an explicit `{ content, filename }` descriptor. */
|
|
66
|
+
export declare function isFileDescriptor(value: unknown): value is FileDescriptor;
|
|
67
|
+
export declare function isPathFileInput(value: unknown): value is PathFileInput;
|
|
68
|
+
export declare function isStreamFactoryFileInput(value: unknown): value is StreamFactoryFileInput;
|
|
69
|
+
export declare function isFileInput(value: unknown): value is FileInput;
|
|
70
|
+
export declare function hasFileInput(input: Record<string, unknown> | undefined): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Resolve a `FileInput` to a `{ blob, filename }` pair for `FormData.append`.
|
|
73
|
+
*
|
|
74
|
+
* One-shot streams without a path are drained here so the body can be replayed
|
|
75
|
+
* on HTTP retries. Disk paths and stream factories stay unbuffered until the
|
|
76
|
+
* caller asks for bytes (small multipart leftovers).
|
|
77
|
+
*/
|
|
78
|
+
export declare function resolveFileBlob(file: FileInput): Promise<{
|
|
79
|
+
blob: Blob;
|
|
80
|
+
filename: string;
|
|
81
|
+
}>;
|
|
82
|
+
export declare function statUploadSize(file: FileInput): Promise<{
|
|
83
|
+
size: number;
|
|
84
|
+
filename: string;
|
|
85
|
+
contentType: string;
|
|
86
|
+
}>;
|
|
87
|
+
export interface MultipartParts {
|
|
88
|
+
/** FormData ready to send as the request body. */
|
|
89
|
+
formData: FormData;
|
|
90
|
+
/** Number of file fields appended (0 means no files were detected). */
|
|
91
|
+
fileCount: number;
|
|
92
|
+
}
|
|
93
|
+
/** What a run start says besides its input: the address of what runs, and the wait. */
|
|
94
|
+
export interface RunStartFields {
|
|
95
|
+
project?: string;
|
|
96
|
+
automation: string;
|
|
97
|
+
/** Omitted: `live`. */
|
|
98
|
+
version?: string;
|
|
99
|
+
/** Seconds the server holds the answer for the run to end. */
|
|
100
|
+
wait?: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* JSON-body counterpart of {@link buildRunMultipart}:
|
|
104
|
+
* `{ project?, automation, version?, wait?, input, tags?, overrides?, metadata? }`.
|
|
105
|
+
*/
|
|
106
|
+
export declare function buildRunJsonBody(start: RunStartFields, input: Record<string, unknown> | undefined, extra?: {
|
|
107
|
+
tags?: string[];
|
|
108
|
+
overrides?: {
|
|
109
|
+
steps?: Record<string, Record<string, unknown>>;
|
|
110
|
+
};
|
|
111
|
+
metadata?: Record<string, unknown>;
|
|
112
|
+
}): Record<string, unknown>;
|
|
113
|
+
/**
|
|
114
|
+
* Build multipart for `client.run(...)` (`POST /v1/runs`):
|
|
115
|
+
*
|
|
116
|
+
* - `project`, `automation`, `version` and `wait` are text fields.
|
|
117
|
+
* - Scalar inputs go in the `input` JSON text field.
|
|
118
|
+
* - Each top-level file in `input` becomes `files.<fieldName>`.
|
|
119
|
+
* - Per-step overrides and caller metadata use `overrides` / `metadata` JSON parts.
|
|
120
|
+
*
|
|
121
|
+
* Async because stream inputs are drained to bytes here. Only top-level file
|
|
122
|
+
* values are extracted — files nested inside arrays / objects keep their
|
|
123
|
+
* position in the JSON sidecar (the server has no nested-upload path).
|
|
124
|
+
*/
|
|
125
|
+
export declare function buildRunMultipart(args: RunStartFields & {
|
|
126
|
+
tags?: string | string[];
|
|
127
|
+
input?: Record<string, unknown>;
|
|
128
|
+
overrides?: {
|
|
129
|
+
steps?: Record<string, Record<string, unknown>>;
|
|
130
|
+
};
|
|
131
|
+
metadata?: Record<string, unknown>;
|
|
132
|
+
}): Promise<MultipartParts>;
|
|
133
|
+
/** Build multipart for endpoints that accept one file field plus optional name. */
|
|
134
|
+
export declare function buildSingleFileMultipart(file: FileInput, name?: string): Promise<FormData>;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File-input helpers for `client.run()`.
|
|
3
|
+
*
|
|
4
|
+
* A workflow input value is treated as a file when it is one of:
|
|
5
|
+
* - a Node readable stream — `fs.createReadStream('contract.pdf')`
|
|
6
|
+
* - a `File` or `Blob`
|
|
7
|
+
* - a `{ content, filename, mimeType? }` descriptor — build one with `toFile()`
|
|
8
|
+
*
|
|
9
|
+
* File values are auto-detected and uploaded as `multipart/form-data`
|
|
10
|
+
* (matching `curl -F`) — no base64 round-trip.
|
|
11
|
+
*/
|
|
12
|
+
const DEFAULT_MIME = 'application/octet-stream';
|
|
13
|
+
/** Common file extensions → MIME type, for upload filename inference. */
|
|
14
|
+
const MIME_BY_EXT = {
|
|
15
|
+
pdf: 'application/pdf',
|
|
16
|
+
png: 'image/png',
|
|
17
|
+
jpg: 'image/jpeg',
|
|
18
|
+
jpeg: 'image/jpeg',
|
|
19
|
+
gif: 'image/gif',
|
|
20
|
+
webp: 'image/webp',
|
|
21
|
+
txt: 'text/plain',
|
|
22
|
+
csv: 'text/csv',
|
|
23
|
+
json: 'application/json',
|
|
24
|
+
xml: 'application/xml',
|
|
25
|
+
html: 'text/html',
|
|
26
|
+
md: 'text/markdown',
|
|
27
|
+
doc: 'application/msword',
|
|
28
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
29
|
+
xls: 'application/vnd.ms-excel',
|
|
30
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
31
|
+
xlsm: 'application/vnd.ms-excel.sheet.macroenabled.12',
|
|
32
|
+
ppt: 'application/vnd.ms-powerpoint',
|
|
33
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
34
|
+
msg: 'application/vnd.ms-outlook',
|
|
35
|
+
};
|
|
36
|
+
/** Guess a MIME type from a filename extension; falls back to octet-stream. */
|
|
37
|
+
function guessMimeType(filename) {
|
|
38
|
+
const ext = filename.includes('.') ? filename.split('.').pop().toLowerCase() : '';
|
|
39
|
+
return MIME_BY_EXT[ext] ?? DEFAULT_MIME;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Attach a filename (and optional MIME type) to raw bytes. The escape hatch
|
|
43
|
+
* for when you have a `Buffer` / `ArrayBuffer` / `Blob` in memory rather than
|
|
44
|
+
* a file path or stream.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* await client.run('workflows.extract-invoice', {
|
|
48
|
+
* contract_document: toFile(buffer, 'contract.pdf'),
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function toFile(content, filename, mimeType) {
|
|
53
|
+
return { content, filename, mimeType: mimeType ?? guessMimeType(filename) };
|
|
54
|
+
}
|
|
55
|
+
/** Detect a Node readable stream (`fs.createReadStream`, etc.). */
|
|
56
|
+
export function isReadStream(value) {
|
|
57
|
+
if (value === null || typeof value !== 'object')
|
|
58
|
+
return false;
|
|
59
|
+
const v = value;
|
|
60
|
+
return (typeof v[Symbol.asyncIterator] === 'function' &&
|
|
61
|
+
(typeof v.pipe === 'function' || typeof v.read === 'function'));
|
|
62
|
+
}
|
|
63
|
+
/** Detect an explicit `{ content, filename }` descriptor. */
|
|
64
|
+
export function isFileDescriptor(value) {
|
|
65
|
+
if (value === null || typeof value !== 'object')
|
|
66
|
+
return false;
|
|
67
|
+
const v = value;
|
|
68
|
+
return (typeof v.filename === 'string' &&
|
|
69
|
+
v.content != null &&
|
|
70
|
+
(v.content instanceof ArrayBuffer ||
|
|
71
|
+
ArrayBuffer.isView(v.content) ||
|
|
72
|
+
(typeof Blob !== 'undefined' && v.content instanceof Blob)));
|
|
73
|
+
}
|
|
74
|
+
export function isPathFileInput(value) {
|
|
75
|
+
if (value === null || typeof value !== 'object' || isReadStream(value))
|
|
76
|
+
return false;
|
|
77
|
+
const v = value;
|
|
78
|
+
return typeof v.path === 'string' && typeof v.open !== 'function';
|
|
79
|
+
}
|
|
80
|
+
export function isStreamFactoryFileInput(value) {
|
|
81
|
+
if (value === null || typeof value !== 'object')
|
|
82
|
+
return false;
|
|
83
|
+
const v = value;
|
|
84
|
+
return (typeof v.open === 'function' &&
|
|
85
|
+
typeof v.filename === 'string' &&
|
|
86
|
+
typeof v.size === 'number' &&
|
|
87
|
+
Number.isFinite(v.size));
|
|
88
|
+
}
|
|
89
|
+
export function isFileInput(value) {
|
|
90
|
+
if (typeof Blob !== 'undefined' && value instanceof Blob)
|
|
91
|
+
return true;
|
|
92
|
+
if (isReadStream(value))
|
|
93
|
+
return true;
|
|
94
|
+
if (isPathFileInput(value) || isStreamFactoryFileInput(value))
|
|
95
|
+
return true;
|
|
96
|
+
return isFileDescriptor(value);
|
|
97
|
+
}
|
|
98
|
+
export function hasFileInput(input) {
|
|
99
|
+
if (!input)
|
|
100
|
+
return false;
|
|
101
|
+
for (const value of Object.values(input)) {
|
|
102
|
+
if (isFileInput(value))
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
/** Last path segment of a (possibly nested) file path. */
|
|
108
|
+
function basename(path) {
|
|
109
|
+
const segments = path.split(/[\\/]/);
|
|
110
|
+
return segments[segments.length - 1] || 'file';
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolve a `FileInput` to a `{ blob, filename }` pair for `FormData.append`.
|
|
114
|
+
*
|
|
115
|
+
* One-shot streams without a path are drained here so the body can be replayed
|
|
116
|
+
* on HTTP retries. Disk paths and stream factories stay unbuffered until the
|
|
117
|
+
* caller asks for bytes (small multipart leftovers).
|
|
118
|
+
*/
|
|
119
|
+
export async function resolveFileBlob(file) {
|
|
120
|
+
// `File` extends `Blob`, so this branch covers both.
|
|
121
|
+
if (typeof Blob !== 'undefined' && file instanceof Blob) {
|
|
122
|
+
return { blob: file, filename: file.name || 'file' };
|
|
123
|
+
}
|
|
124
|
+
if (isPathFileInput(file) || (isReadStream(file) && typeof file.path === 'string')) {
|
|
125
|
+
const filePath = isPathFileInput(file) ? file.path : file.path;
|
|
126
|
+
const filename = (isPathFileInput(file) ? file.filename : undefined) ?? basename(filePath);
|
|
127
|
+
if (await pathIsFile(filePath)) {
|
|
128
|
+
const { readFile } = await import('node:fs/promises');
|
|
129
|
+
const bytes = await readFile(filePath);
|
|
130
|
+
const type = (isPathFileInput(file) ? file.mimeType : undefined) ?? guessMimeType(filename);
|
|
131
|
+
return { blob: new Blob([bytes], { type }), filename };
|
|
132
|
+
}
|
|
133
|
+
if (isPathFileInput(file)) {
|
|
134
|
+
throw new Error(`Upload path is not a file: ${filePath}`);
|
|
135
|
+
}
|
|
136
|
+
// Named stream whose path is not readable — drain the in-memory stream.
|
|
137
|
+
}
|
|
138
|
+
if (isStreamFactoryFileInput(file)) {
|
|
139
|
+
const body = file.open(0, file.size);
|
|
140
|
+
if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
|
141
|
+
return { blob: body, filename: file.filename };
|
|
142
|
+
}
|
|
143
|
+
if (body instanceof Uint8Array) {
|
|
144
|
+
return {
|
|
145
|
+
blob: new Blob([body], { type: file.mimeType ?? guessMimeType(file.filename) }),
|
|
146
|
+
filename: file.filename,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
throw new Error('Stream factory returned a non-Blob body; pass a path or Blob for small multipart leftovers');
|
|
150
|
+
}
|
|
151
|
+
// Node readable stream without a path — drain it into a Blob now.
|
|
152
|
+
if (isReadStream(file)) {
|
|
153
|
+
const filename = typeof file.path === 'string' ? basename(file.path) : 'file';
|
|
154
|
+
const parts = [];
|
|
155
|
+
for await (const chunk of file) {
|
|
156
|
+
parts.push(chunk);
|
|
157
|
+
}
|
|
158
|
+
return { blob: new Blob(parts, { type: guessMimeType(filename) }), filename };
|
|
159
|
+
}
|
|
160
|
+
// Explicit descriptor.
|
|
161
|
+
const desc = file;
|
|
162
|
+
const type = desc.mimeType ?? DEFAULT_MIME;
|
|
163
|
+
if (!(desc.content instanceof Blob)) {
|
|
164
|
+
// ArrayBuffer / ArrayBufferView — wrap the bytes in a typed Blob.
|
|
165
|
+
return { blob: new Blob([desc.content], { type }), filename: desc.filename };
|
|
166
|
+
}
|
|
167
|
+
// Already a Blob — reuse it, re-slicing only to apply a different MIME type.
|
|
168
|
+
const blob = desc.content.type === type ? desc.content : desc.content.slice(0, desc.content.size, type);
|
|
169
|
+
return { blob, filename: desc.filename };
|
|
170
|
+
}
|
|
171
|
+
export async function statUploadSize(file) {
|
|
172
|
+
if (typeof Blob !== 'undefined' && file instanceof Blob) {
|
|
173
|
+
return {
|
|
174
|
+
size: file.size,
|
|
175
|
+
filename: file.name || 'file',
|
|
176
|
+
contentType: file.type || DEFAULT_MIME,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (isPathFileInput(file) || (isReadStream(file) && typeof file.path === 'string')) {
|
|
180
|
+
const filePath = isPathFileInput(file) ? file.path : file.path;
|
|
181
|
+
const filename = (isPathFileInput(file) ? file.filename : undefined) ?? basename(filePath);
|
|
182
|
+
if (await pathIsFile(filePath)) {
|
|
183
|
+
const { stat } = await import('node:fs/promises');
|
|
184
|
+
const info = await stat(filePath);
|
|
185
|
+
return {
|
|
186
|
+
size: info.size,
|
|
187
|
+
filename,
|
|
188
|
+
contentType: (isPathFileInput(file) ? file.mimeType : undefined) ?? guessMimeType(filename),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (isPathFileInput(file)) {
|
|
192
|
+
throw new Error(`Upload path is not a file: ${filePath}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (isStreamFactoryFileInput(file)) {
|
|
196
|
+
return {
|
|
197
|
+
size: file.size,
|
|
198
|
+
filename: file.filename,
|
|
199
|
+
contentType: file.mimeType ?? guessMimeType(file.filename),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (isFileDescriptor(file)) {
|
|
203
|
+
const size = file.content instanceof Blob
|
|
204
|
+
? file.content.size
|
|
205
|
+
: file.content instanceof ArrayBuffer
|
|
206
|
+
? file.content.byteLength
|
|
207
|
+
: file.content.byteLength;
|
|
208
|
+
return {
|
|
209
|
+
size,
|
|
210
|
+
filename: file.filename,
|
|
211
|
+
contentType: file.mimeType ?? guessMimeType(file.filename),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
throw new Error('Cannot determine size of a non-replayable stream. Pass a file path, Blob, or a stream factory with size.');
|
|
215
|
+
}
|
|
216
|
+
async function pathIsFile(filePath) {
|
|
217
|
+
try {
|
|
218
|
+
const { stat } = await import('node:fs/promises');
|
|
219
|
+
return (await stat(filePath)).isFile();
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/** Drain `input` into a FormData, appending each detected file as `files.<name>`. */
|
|
226
|
+
async function appendFiles(fd, input) {
|
|
227
|
+
const scalars = {};
|
|
228
|
+
let fileCount = 0;
|
|
229
|
+
for (const [key, value] of Object.entries(input ?? {})) {
|
|
230
|
+
if (isFileInput(value)) {
|
|
231
|
+
const { blob, filename } = await resolveFileBlob(value);
|
|
232
|
+
fd.append(`files.${key}`, blob, filename);
|
|
233
|
+
fileCount += 1;
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
scalars[key] = value;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return { scalars, fileCount };
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* JSON-body counterpart of {@link buildRunMultipart}:
|
|
243
|
+
* `{ project?, automation, version?, wait?, input, tags?, overrides?, metadata? }`.
|
|
244
|
+
*/
|
|
245
|
+
export function buildRunJsonBody(start, input, extra = {}) {
|
|
246
|
+
const body = { ...start, input: input ?? {} };
|
|
247
|
+
if (extra.tags)
|
|
248
|
+
body.tags = extra.tags;
|
|
249
|
+
if (extra.overrides)
|
|
250
|
+
body.overrides = extra.overrides;
|
|
251
|
+
if (extra.metadata)
|
|
252
|
+
body.metadata = extra.metadata;
|
|
253
|
+
for (const key of Object.keys(body))
|
|
254
|
+
if (body[key] === undefined)
|
|
255
|
+
delete body[key];
|
|
256
|
+
return body;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Build multipart for `client.run(...)` (`POST /v1/runs`):
|
|
260
|
+
*
|
|
261
|
+
* - `project`, `automation`, `version` and `wait` are text fields.
|
|
262
|
+
* - Scalar inputs go in the `input` JSON text field.
|
|
263
|
+
* - Each top-level file in `input` becomes `files.<fieldName>`.
|
|
264
|
+
* - Per-step overrides and caller metadata use `overrides` / `metadata` JSON parts.
|
|
265
|
+
*
|
|
266
|
+
* Async because stream inputs are drained to bytes here. Only top-level file
|
|
267
|
+
* values are extracted — files nested inside arrays / objects keep their
|
|
268
|
+
* position in the JSON sidecar (the server has no nested-upload path).
|
|
269
|
+
*/
|
|
270
|
+
export async function buildRunMultipart(args) {
|
|
271
|
+
const fd = new FormData();
|
|
272
|
+
for (const key of ['project', 'automation', 'version', 'wait']) {
|
|
273
|
+
if (args[key] !== undefined)
|
|
274
|
+
fd.append(key, String(args[key]));
|
|
275
|
+
}
|
|
276
|
+
if (args.tags !== undefined)
|
|
277
|
+
fd.append('tags', JSON.stringify(args.tags));
|
|
278
|
+
const { scalars, fileCount } = await appendFiles(fd, args.input);
|
|
279
|
+
fd.append('input', JSON.stringify(scalars));
|
|
280
|
+
if (args.overrides) {
|
|
281
|
+
fd.append('overrides', JSON.stringify(args.overrides));
|
|
282
|
+
}
|
|
283
|
+
if (args.metadata) {
|
|
284
|
+
fd.append('metadata', JSON.stringify(args.metadata));
|
|
285
|
+
}
|
|
286
|
+
return { formData: fd, fileCount };
|
|
287
|
+
}
|
|
288
|
+
/** Build multipart for endpoints that accept one file field plus optional name. */
|
|
289
|
+
export async function buildSingleFileMultipart(file, name) {
|
|
290
|
+
const fd = new FormData();
|
|
291
|
+
const { blob, filename } = await resolveFileBlob(file);
|
|
292
|
+
fd.append('file', blob, filename);
|
|
293
|
+
if (name)
|
|
294
|
+
fd.append('name', name);
|
|
295
|
+
return fd;
|
|
296
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare const HTTP_CHUNKED_MAX_RETRIES = 4;
|
|
2
|
+
export declare const HTTP_CHUNKED_RETRY_BASE_MS = 250;
|
|
3
|
+
export type HttpChunkedSession = {
|
|
4
|
+
transport: 'http_chunked';
|
|
5
|
+
uploadId: string;
|
|
6
|
+
fileId: string;
|
|
7
|
+
chunkSizeBytes: number;
|
|
8
|
+
chunkCount: number;
|
|
9
|
+
chunksUrl: string;
|
|
10
|
+
completeUrl: string;
|
|
11
|
+
expiresAt: string;
|
|
12
|
+
maxFileSizeBytes: number;
|
|
13
|
+
};
|
|
14
|
+
export declare function expectedHttpChunkByteLength(totalSize: number, chunkSizeBytes: number, chunkIndex: number, chunkCount: number): number;
|
|
15
|
+
export declare function chunkByteOffset(chunkSizeBytes: number, chunkIndex: number): number;
|
|
16
|
+
export declare function shouldAbortHttpChunkedUploadSession(options: {
|
|
17
|
+
chunksReady: boolean;
|
|
18
|
+
}): boolean;
|
|
19
|
+
export declare function httpChunkedCompleteRetryHint(uploadId: string): string;
|
|
20
|
+
export declare function annotateHttpChunkedCompleteFailure(uploadId: string, error: unknown): unknown;
|
|
21
|
+
export declare class ChunkUploadHttpError extends Error {
|
|
22
|
+
readonly status: number;
|
|
23
|
+
constructor(status: number);
|
|
24
|
+
}
|
|
25
|
+
export declare function uploadHttpChunks(options: {
|
|
26
|
+
chunkCount: number;
|
|
27
|
+
chunkSizeBytes: number;
|
|
28
|
+
totalSize: number;
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
onProgress?: (uploadedBytes: number, totalBytes: number) => void;
|
|
31
|
+
putChunk: (args: {
|
|
32
|
+
chunkIndex: number;
|
|
33
|
+
start: number;
|
|
34
|
+
length: number;
|
|
35
|
+
}) => Promise<void>;
|
|
36
|
+
}): Promise<void>;
|