@reduck-ai/sdk 1.1.20 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -202
- package/dist/client.d.ts +11 -18
- package/dist/client.js +19 -101
- package/dist/generated/client/client.gen.d.ts +2 -0
- package/dist/generated/client/client.gen.js +216 -0
- package/dist/generated/client/index.d.ts +10 -0
- package/dist/generated/client/index.js +6 -0
- package/dist/generated/client/types.gen.d.ts +120 -0
- package/dist/generated/client/types.gen.js +2 -0
- package/dist/generated/client/utils.gen.d.ts +37 -0
- package/dist/generated/client/utils.gen.js +228 -0
- package/dist/generated/client.gen.d.ts +12 -0
- package/dist/generated/client.gen.js +3 -0
- package/dist/generated/core/auth.gen.d.ts +25 -0
- package/dist/generated/core/auth.gen.js +14 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +25 -0
- package/dist/generated/core/bodySerializer.gen.js +57 -0
- package/dist/generated/core/params.gen.d.ts +43 -0
- package/dist/generated/core/params.gen.js +109 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +33 -0
- package/dist/generated/core/pathSerializer.gen.js +106 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/generated/core/queryKeySerializer.gen.js +92 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/generated/core/serverSentEvents.gen.js +132 -0
- package/dist/generated/core/types.gen.d.ts +83 -0
- package/dist/generated/core/types.gen.js +2 -0
- package/dist/generated/core/utils.gen.d.ts +19 -0
- package/dist/generated/core/utils.gen.js +87 -0
- package/dist/generated/index.d.ts +2 -0
- package/dist/generated/index.js +3 -0
- package/dist/generated/sdk.gen.d.ts +471 -0
- package/dist/generated/sdk.gen.js +862 -0
- package/dist/generated/types.gen.d.ts +5489 -0
- package/dist/generated/types.gen.js +56 -0
- package/dist/index.d.ts +3 -5
- package/dist/index.js +4 -4
- package/openapi.json +16668 -0
- package/package.json +23 -25
- package/dist/build-env.generated.d.ts +0 -3
- package/dist/build-env.generated.js +0 -4
- package/dist/device.d.ts +0 -3
- package/dist/device.js +0 -7
- package/dist/errors.d.ts +0 -9
- package/dist/errors.js +0 -21
- package/dist/get.d.ts +0 -2
- package/dist/get.js +0 -8
- package/dist/run.d.ts +0 -12
- package/dist/run.js +0 -73
- package/dist/schemas.d.ts +0 -565
- package/dist/schemas.js +0 -99
- package/dist/search.d.ts +0 -2
- package/dist/search.js +0 -7
- package/dist/session.d.ts +0 -3
- package/dist/session.js +0 -14
- package/dist/types.d.ts +0 -30
- package/dist/types.js +0 -8
package/README.md
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
# @reduck-ai/sdk
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
[](https://www.npmjs.com/package/@reduck-ai/sdk)
|
|
8
|
+
|
|
9
|
+
TypeScript SDK for [Reduck](https://reduck.ai). Use it to manage your browser
|
|
10
|
+
scripts, devices and runs from your code. Each function and type is generated
|
|
11
|
+
from the [OpenAPI specification](./openapi.json) that reduck.ai publishes at
|
|
12
|
+
`https://reduck.ai/api/openapi.json`.
|
|
8
13
|
|
|
9
14
|
## Installation
|
|
10
15
|
|
|
@@ -15,228 +20,126 @@ npm install @reduck-ai/sdk
|
|
|
15
20
|
## Quick Start
|
|
16
21
|
|
|
17
22
|
```typescript
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
const client =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
// API key (recommended for server-side)
|
|
45
|
-
const client = new ReduckClient({ apiKey: "<your-api-key>" })
|
|
46
|
-
|
|
47
|
-
// OAuth token (for user-facing apps)
|
|
48
|
-
const client = new ReduckClient({ oauthToken: "<your-oauth-token>" })
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
Generate an API key from your [Reduck dashboard](https://reduck.ai).
|
|
52
|
-
|
|
53
|
-
## API
|
|
54
|
-
|
|
55
|
-
### `new ReduckClient(options)`
|
|
56
|
-
|
|
57
|
-
| Option | Type | Description |
|
|
58
|
-
|---|---|---|
|
|
59
|
-
| `apiKey` | `string` | API key authentication |
|
|
60
|
-
| `oauthToken` | `string` | OAuth bearer token authentication |
|
|
61
|
-
| `runnerBaseUrl` | `string` | Override runner base URL (default: hosted) |
|
|
62
|
-
| `appBaseUrl` | `string` | Override app base URL (default: hosted) |
|
|
63
|
-
| `headers` | `Record<string, string>` | Extra headers to send on every request |
|
|
64
|
-
|
|
65
|
-
One of `apiKey` or `oauthToken` is required.
|
|
66
|
-
|
|
67
|
-
### `client.run(query, options)`
|
|
68
|
-
|
|
69
|
-
Starts an automation and returns a `Run` — an `AsyncIterable<RunEvent>` that streams events in real-time via WebSocket. The first event yielded is `run_requested` (synchronously after the server accepts the run, before WebSocket events flow).
|
|
70
|
-
|
|
71
|
-
When invoking a typed method, pass `query: ""` and set `method` + `methodArgs`.
|
|
72
|
-
|
|
73
|
-
| Option | Type | Description |
|
|
74
|
-
|---|---|---|
|
|
75
|
-
| `deviceId` | `string` | Target device ID (required) |
|
|
76
|
-
| `method` | `string` | Invoke a typed method `<host>/<slug>` (mutually exclusive with `outputSchema`) |
|
|
77
|
-
| `methodArgs` | `Record<string, string>` | Arguments rendered into the method's `prompt_template`. Requires `method` |
|
|
78
|
-
| `mount` | `string[]` | Local file paths to upload and make available to `click_upload` |
|
|
79
|
-
| `pasteSlots` | `Record<string, string>` | Named slots inserted via `paste(slot=...)` |
|
|
80
|
-
| `outputSchema` | `object` | Inline vision extractor JSON schema (mutually exclusive with `method`) |
|
|
81
|
-
| `slug` | `string` | Custom extractor slug for inline `outputSchema` (default: `summary`) |
|
|
82
|
-
|
|
83
|
-
### `run.id`
|
|
84
|
-
|
|
85
|
-
`Promise<string>` — resolves to the run ID once the server accepts the run.
|
|
86
|
-
|
|
87
|
-
### `run.cancel()`
|
|
88
|
-
|
|
89
|
-
Cancels the running automation.
|
|
90
|
-
|
|
91
|
-
### `client.listDevices()`
|
|
92
|
-
|
|
93
|
-
Returns `Promise<Device[]>` — all devices for the authenticated user.
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
interface Device {
|
|
97
|
-
id: string
|
|
98
|
-
name: string | null
|
|
99
|
-
browser: string | null
|
|
100
|
-
os: string | null
|
|
101
|
-
reachable: boolean
|
|
102
|
-
created_at: string
|
|
103
|
-
}
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### `client.listRuns(page?)`
|
|
107
|
-
|
|
108
|
-
Returns `Promise<RunsPage>` — a paginated list of runs.
|
|
109
|
-
|
|
110
|
-
```typescript
|
|
111
|
-
interface RunsPage {
|
|
112
|
-
data: RunRecord[]
|
|
113
|
-
hasNextPage: boolean
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
### `client.getRun(runId)`
|
|
118
|
-
|
|
119
|
-
Returns `Promise<RunRecord>` — a single run by ID.
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
interface RunRecord {
|
|
123
|
-
id: string
|
|
124
|
-
started_at: string
|
|
125
|
-
finished_at: string | null
|
|
126
|
-
result: {
|
|
127
|
-
success?: boolean
|
|
128
|
-
cancelled?: boolean
|
|
129
|
-
output?: string
|
|
130
|
-
error?: { code: string; message: string } | null
|
|
131
|
-
final_url?: string
|
|
132
|
-
extractions?: SlugExtraction[]
|
|
133
|
-
downloads?: DownloadedFile[]
|
|
134
|
-
} | null
|
|
135
|
-
resolved: {
|
|
136
|
-
url?: string
|
|
137
|
-
prompt?: string
|
|
138
|
-
} | null
|
|
139
|
-
}
|
|
23
|
+
import { createReduckClient, createScript, listScripts } from "@reduck-ai/sdk";
|
|
24
|
+
|
|
25
|
+
const client = createReduckClient({ apiKey: process.env.REDUCK_API_KEY! });
|
|
26
|
+
|
|
27
|
+
// Every function takes { client, path?, query?, body? } and returns { data, error }.
|
|
28
|
+
const { data: scripts } = await listScripts({
|
|
29
|
+
client,
|
|
30
|
+
path: { handle: "@username" },
|
|
31
|
+
query: { q: "linkedin" }
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const { data: script, error } = await createScript({
|
|
35
|
+
client,
|
|
36
|
+
path: { handle: "@username" },
|
|
37
|
+
body: {
|
|
38
|
+
slug: "linkedin-jobs",
|
|
39
|
+
host: "linkedin.com",
|
|
40
|
+
description: "Pull job posts",
|
|
41
|
+
content: "await page.goto('https://linkedin.com/jobs')",
|
|
42
|
+
input_schema: {},
|
|
43
|
+
output_schema: {},
|
|
44
|
+
side_effects: "none"
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (error) throw new Error(error.error);
|
|
140
48
|
```
|
|
141
49
|
|
|
142
|
-
|
|
50
|
+
Inputs and responses are fully typed and autocomplete in your editor.
|
|
143
51
|
|
|
144
|
-
|
|
52
|
+
## Authentication
|
|
145
53
|
|
|
146
|
-
|
|
54
|
+
`createReduckClient` takes one credential: `apiKey` or `oauthToken`.
|
|
147
55
|
|
|
148
56
|
```typescript
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
// Full surface of a host
|
|
153
|
-
const site = await client.getSite("linkedin.com")
|
|
57
|
+
const client = createReduckClient({ apiKey: "<your-api-key>" }); // server-side
|
|
58
|
+
const client = createReduckClient({ oauthToken: "<your-oauth-token>" }); // user-facing apps
|
|
154
59
|
```
|
|
155
60
|
|
|
156
|
-
|
|
61
|
+
Create an API key at [reduck.ai/api-keys](https://reduck.ai/api-keys).
|
|
157
62
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
63
|
+
An `oauthToken` must be an access token **issued for this API**: request it with
|
|
64
|
+
`resource=https://reduck.ai` (RFC 8707), and make sure it carries the scopes the
|
|
65
|
+
endpoints you call need: `script:read` / `script:write`, `run:read`,
|
|
66
|
+
`device:read` / `device:write`, `app:read`, `profile`. The API refuses a token
|
|
67
|
+
issued for a different Reduck resource, such as the MCP server: an access token
|
|
68
|
+
is only valid at the resource it names.
|
|
164
69
|
|
|
165
|
-
|
|
166
|
-
method_slug: string
|
|
167
|
-
description: string
|
|
168
|
-
args: Record<string, unknown> // JSON Schema for method args
|
|
169
|
-
output_schema: Record<string, unknown> | null
|
|
170
|
-
}
|
|
171
|
-
```
|
|
70
|
+
## Options
|
|
172
71
|
|
|
173
|
-
|
|
72
|
+
| Option | Default | Description |
|
|
73
|
+
| --------- | ------------------- | ------------------------------------------------- |
|
|
74
|
+
| `baseUrl` | `https://reduck.ai` | The origin of the API. The paths include `/api`. |
|
|
75
|
+
| `headers` | none | More headers to send with each request. |
|
|
174
76
|
|
|
175
|
-
|
|
77
|
+
## API
|
|
176
78
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
|
182
|
-
|
|
|
183
|
-
| `
|
|
184
|
-
| `
|
|
185
|
-
| `
|
|
186
|
-
| `
|
|
187
|
-
| `
|
|
79
|
+
Pass the returned `client` to any function. List endpoints return
|
|
80
|
+
`{ data, has_next_page }` and take `query: { page?, page_size?, … }`; the rest take
|
|
81
|
+
`path` and/or `body`.
|
|
82
|
+
|
|
83
|
+
| Function | Args (besides `client`) |
|
|
84
|
+
| --------------------------- | ------------------------------------------------------------------------- |
|
|
85
|
+
| `getMe` | none |
|
|
86
|
+
| `listDevices` | none |
|
|
87
|
+
| `listRuns` | `query: { page?, page_size? }` |
|
|
88
|
+
| `getRun` | `path: { run_id }` |
|
|
89
|
+
| `listAllScripts` | `query: { host?, q?, visibility?, archived? }` |
|
|
90
|
+
| `listScripts` | `path: { handle }`, `query: { host?, slug?, visibility?, archived?, q? }` |
|
|
91
|
+
| `getScript` | `path: { handle, host, slug }` |
|
|
92
|
+
| `createScript` | `path: { handle }`, `body` |
|
|
93
|
+
| `patchScript` | `path: { handle, host, slug }`, `body: { name?, description? }` |
|
|
94
|
+
| `publishScript` | `path: { handle, host, slug }` |
|
|
95
|
+
| `unpublishScript` | `path: { handle, host, slug }` |
|
|
96
|
+
| `archiveScript` | `path: { handle, host, slug }` |
|
|
97
|
+
| `restoreScript` | `path: { handle, host, slug }` |
|
|
98
|
+
| `listScriptVersions` | `path: { handle, host, slug }`, `query` |
|
|
99
|
+
| `createDraftScriptVersion` | `path: { handle, host, slug }`, `body` |
|
|
100
|
+
| `getScriptVersion` | `path: { handle, host, slug, version_id }` |
|
|
101
|
+
| `listScriptTransferTargets` | `path: { handle, host, slug }` |
|
|
102
|
+
| `transferScript` | `path: { handle, host, slug }`, `body: { target }` |
|
|
103
|
+
|
|
104
|
+
`handle` is `@username` for a personal scope or a project slug. The table shows
|
|
105
|
+
the frequent functions. Your editor shows all of them, or see
|
|
106
|
+
[openapi.json](./openapi.json).
|
|
107
|
+
|
|
108
|
+
## Errors
|
|
109
|
+
|
|
110
|
+
Functions do not throw by default: they return `{ data, error }`. On a failed
|
|
111
|
+
request, `data` is undefined and `error` holds the error that the server sent:
|
|
112
|
+
|
|
113
|
+
- `status`: the HTTP status of the response.
|
|
114
|
+
- `code`: identifies the error. Use this value in your code. New values can be
|
|
115
|
+
added: for a value that you do not know, use the status.
|
|
116
|
+
- `error`: describes the error for a person. The text can change: do not use
|
|
117
|
+
it in your code.
|
|
118
|
+
- `variables`: for a request body that is not valid, the errors of each field.
|
|
119
|
+
|
|
120
|
+
After a check of `error.status`, the type of `error.code` has only the codes that
|
|
121
|
+
the function can answer with that status.
|
|
188
122
|
|
|
189
123
|
```typescript
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
| (RunEventBase & { type: "run_completed"; timestamp: number })
|
|
199
|
-
| (RunEventBase & {
|
|
200
|
-
type: "done"
|
|
201
|
-
success: boolean
|
|
202
|
-
output?: string
|
|
203
|
-
final_url?: string
|
|
204
|
-
extractions?: SlugExtraction[]
|
|
205
|
-
downloads?: DownloadedFile[]
|
|
206
|
-
})
|
|
207
|
-
| (RunEventBase & { type: "failure"; error: string })
|
|
208
|
-
| (RunEventBase & { type: "cancel" })
|
|
209
|
-
|
|
210
|
-
interface SlugExtraction {
|
|
211
|
-
host: string
|
|
212
|
-
slug: string
|
|
213
|
-
records: object[]
|
|
214
|
-
output_schema: object
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
interface DownloadedFile {
|
|
218
|
-
filename: string
|
|
219
|
-
path?: string // full path on the runner — re-usable as `mount` in a follow-up run
|
|
220
|
-
mimetype?: string
|
|
221
|
-
url?: string | null
|
|
124
|
+
const { data, error } = await getScript({
|
|
125
|
+
client,
|
|
126
|
+
path: { handle, host, slug }
|
|
127
|
+
});
|
|
128
|
+
if (error) {
|
|
129
|
+
if (error.code === "not_found") return null;
|
|
130
|
+
console.error(error.error); // or per-field error.variables
|
|
131
|
+
return;
|
|
222
132
|
}
|
|
223
133
|
```
|
|
224
134
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
## Schemas as JSON Schema
|
|
135
|
+
## Versions
|
|
228
136
|
|
|
229
|
-
|
|
137
|
+
The package follows [semantic versioning](https://semver.org). A change to the
|
|
138
|
+
API that can break your code gives a major version.
|
|
230
139
|
|
|
231
|
-
|
|
232
|
-
import { schemas } from "@reduck-ai/sdk"
|
|
233
|
-
|
|
234
|
-
schemas.RunEventSchema.parse(json) // validate at runtime
|
|
235
|
-
schemas.RunResultSchema // the final `done` event shape
|
|
236
|
-
schemas.SiteSchema, schemas.MethodSchema, schemas.DeviceSchema, ...
|
|
237
|
-
```
|
|
140
|
+
## Contributing
|
|
238
141
|
|
|
239
|
-
|
|
142
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md).
|
|
240
143
|
|
|
241
144
|
## License
|
|
242
145
|
|
package/dist/client.d.ts
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
searchSites(query: string): Promise<Site[]>;
|
|
13
|
-
getSite(host: string): Promise<Site>;
|
|
14
|
-
interrupt(runId: string): Promise<void>;
|
|
15
|
-
private _createRun;
|
|
16
|
-
private _mount;
|
|
17
|
-
private _pasteFiles;
|
|
18
|
-
}
|
|
1
|
+
export declare const DEFAULT_BASE_URL = "https://reduck.ai";
|
|
2
|
+
export type ReduckCredentials = {
|
|
3
|
+
oauthToken: string;
|
|
4
|
+
} | {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function credentialHeaders(cred: ReduckCredentials | {}): Record<string, string>;
|
|
8
|
+
export declare function createReduckClient(opts?: (ReduckCredentials | {}) & {
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
}): import("./generated/client/types.gen.js").Client;
|
package/dist/client.js
CHANGED
|
@@ -1,102 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
run(query, { mount, deviceId, method, methodArgs, pasteSlots, outputSchema, slug }) {
|
|
22
|
-
if (!deviceId)
|
|
23
|
-
throw new ReduckError("deviceId is required to start a run", { code: "MISSING_DEVICE" });
|
|
24
|
-
const requestedPromise = this._createRun(deviceId, query, mount, method, methodArgs, pasteSlots, outputSchema, slug);
|
|
25
|
-
const wsBase = this._runnerBaseUrl
|
|
26
|
-
.replace("https://", "wss://")
|
|
27
|
-
.replace("http://", "ws://");
|
|
28
|
-
return new Run(requestedPromise, wsBase, this._headers);
|
|
29
|
-
}
|
|
30
|
-
async listDevices() {
|
|
31
|
-
return fetchDevices(this._appBaseUrl, this._headers);
|
|
32
|
-
}
|
|
33
|
-
async listRuns(page = 0) {
|
|
34
|
-
return fetchRuns(this._appBaseUrl, this._headers, page);
|
|
35
|
-
}
|
|
36
|
-
async getRun(runId) {
|
|
37
|
-
return fetchRun(this._appBaseUrl, this._headers, runId);
|
|
38
|
-
}
|
|
39
|
-
async searchSites(query) {
|
|
40
|
-
return fetchSearchSites(this._runnerBaseUrl, this._headers, query);
|
|
41
|
-
}
|
|
42
|
-
async getSite(host) {
|
|
43
|
-
return fetchGetSite(this._runnerBaseUrl, this._headers, host);
|
|
44
|
-
}
|
|
45
|
-
async interrupt(runId) {
|
|
46
|
-
await fetch(`${this._runnerBaseUrl}/run/${runId}/cancel`, {
|
|
47
|
-
method: "POST",
|
|
48
|
-
headers: this._headers,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
async _createRun(deviceId, query, mount, method, methodArgs, pasteSlots, outputSchema, slug) {
|
|
52
|
-
const mountedPaths = mount?.length ? await this._mount(mount) : undefined;
|
|
53
|
-
const stagedPaste = pasteSlots ? await this._pasteFiles(pasteSlots) : undefined;
|
|
54
|
-
const res = await fetch(`${this._runnerBaseUrl}/query`, {
|
|
55
|
-
method: "POST",
|
|
56
|
-
headers: {
|
|
57
|
-
"Content-Type": "application/json",
|
|
58
|
-
...this._headers,
|
|
59
|
-
},
|
|
60
|
-
body: JSON.stringify({
|
|
61
|
-
query,
|
|
62
|
-
device_id: deviceId,
|
|
63
|
-
...(mountedPaths?.length ? { mount: mountedPaths } : {}),
|
|
64
|
-
...(method ? { method } : {}),
|
|
65
|
-
...(methodArgs && Object.keys(methodArgs).length ? { method_args: methodArgs } : {}),
|
|
66
|
-
...(stagedPaste && Object.keys(stagedPaste).length ? { paste_slots: stagedPaste } : {}),
|
|
67
|
-
...(outputSchema ? { output_schema: outputSchema, slug: slug ?? "summary" } : {}),
|
|
68
|
-
}),
|
|
69
|
-
});
|
|
70
|
-
if (!res.ok) {
|
|
71
|
-
const detail = await res.json().then((d) => d?.error).catch(() => undefined);
|
|
72
|
-
throw ReduckError.fromResponse("POST /query", res.status, detail);
|
|
73
|
-
}
|
|
74
|
-
return (await res.json());
|
|
75
|
-
}
|
|
76
|
-
async _mount(paths) {
|
|
77
|
-
const destDir = join(homedir(), "Downloads", ".reduck");
|
|
78
|
-
await mkdir(destDir, { recursive: true });
|
|
79
|
-
return Promise.all(paths.map(async (p) => {
|
|
80
|
-
const dest = join(destDir, basename(p));
|
|
81
|
-
await cp(p, dest);
|
|
82
|
-
return dest;
|
|
83
|
-
}));
|
|
84
|
-
}
|
|
85
|
-
async _pasteFiles(slots) {
|
|
86
|
-
const destDir = join(homedir(), "Downloads", ".reduck", "paste");
|
|
87
|
-
await mkdir(destDir, { recursive: true });
|
|
88
|
-
const result = {};
|
|
89
|
-
await Promise.all(Object.entries(slots).map(async ([key, val]) => {
|
|
90
|
-
if (val.startsWith("@")) {
|
|
91
|
-
const src = val.slice(1);
|
|
92
|
-
const dest = join(destDir, basename(src));
|
|
93
|
-
await cp(src, dest);
|
|
94
|
-
result[key] = dest;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
result[key] = val;
|
|
98
|
-
}
|
|
99
|
-
}));
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
1
|
+
import { createClient, createConfig } from "./generated/client/index.js";
|
|
2
|
+
export const DEFAULT_BASE_URL = "https://reduck.ai";
|
|
3
|
+
// The single place a credential becomes headers, so a caller reaching an endpoint the generated
|
|
4
|
+
// client does not cover still authenticates identically.
|
|
5
|
+
export function credentialHeaders(cred) {
|
|
6
|
+
if ("oauthToken" in cred)
|
|
7
|
+
return { Authorization: `Bearer ${cred.oauthToken.trim()}` };
|
|
8
|
+
if ("apiKey" in cred)
|
|
9
|
+
return { "X-API-Key": cred.apiKey.trim() };
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
// Returns a client preconfigured with the API base URL and the auth header. Pass it to any
|
|
13
|
+
// generated function: getScript({ client, path: { handle, host, slug } }).
|
|
14
|
+
// Omit credentials for unauthenticated access to public routes.
|
|
15
|
+
export function createReduckClient(opts = {}) {
|
|
16
|
+
return createClient(createConfig({
|
|
17
|
+
baseUrl: (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, ""),
|
|
18
|
+
headers: { ...credentialHeaders(opts), ...opts.headers }
|
|
19
|
+
}));
|
|
102
20
|
}
|