@reduck-ai/sdk 0.1.0 → 1.0.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/dist/client.d.ts +12 -0
- package/dist/client.js +60 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/run.d.ts +12 -0
- package/dist/run.js +72 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.js +1 -0
- package/package.json +1 -1
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ReduckClientOptions, RunOptions } from "./types.js";
|
|
2
|
+
import { Run } from "./run.js";
|
|
3
|
+
export declare class ReduckClient {
|
|
4
|
+
private readonly _apiKey;
|
|
5
|
+
private readonly _deviceId;
|
|
6
|
+
private readonly _baseUrl;
|
|
7
|
+
constructor({ apiKey, deviceId, baseUrl }: ReduckClientOptions);
|
|
8
|
+
run(prompt: string, { startUrl, mount }?: RunOptions): Run;
|
|
9
|
+
interrupt(runId: string): Promise<void>;
|
|
10
|
+
private _createRun;
|
|
11
|
+
private _mount;
|
|
12
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { cp, mkdir } from "node:fs/promises";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { Run } from "./run.js";
|
|
5
|
+
const DEFAULT_BASE_URL = "https://runner.pointandclick.ai";
|
|
6
|
+
export class ReduckClient {
|
|
7
|
+
_apiKey;
|
|
8
|
+
_deviceId;
|
|
9
|
+
_baseUrl;
|
|
10
|
+
constructor({ apiKey, deviceId, baseUrl = DEFAULT_BASE_URL }) {
|
|
11
|
+
const resolvedDeviceId = deviceId ?? process.env.REDUCK_DEVICE_ID;
|
|
12
|
+
if (!resolvedDeviceId)
|
|
13
|
+
throw new Error("deviceId is required (or set REDUCK_DEVICE_ID)");
|
|
14
|
+
this._apiKey = apiKey;
|
|
15
|
+
this._deviceId = resolvedDeviceId;
|
|
16
|
+
this._baseUrl = baseUrl.replace(/\/$/, "");
|
|
17
|
+
}
|
|
18
|
+
run(prompt, { startUrl, mount } = {}) {
|
|
19
|
+
const runIdPromise = this._createRun(prompt, startUrl, mount);
|
|
20
|
+
const wsBase = this._baseUrl
|
|
21
|
+
.replace("https://", "wss://")
|
|
22
|
+
.replace("http://", "ws://");
|
|
23
|
+
return new Run(runIdPromise, wsBase, this._apiKey);
|
|
24
|
+
}
|
|
25
|
+
async interrupt(runId) {
|
|
26
|
+
await fetch(`${this._baseUrl}/run/${runId}/cancel`, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: { Authorization: `Bearer ${this._apiKey}` },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async _createRun(prompt, startUrl, mount) {
|
|
32
|
+
const mountedPaths = mount?.length ? await this._mount(mount) : undefined;
|
|
33
|
+
const res = await fetch(`${this._baseUrl}/query`, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
headers: {
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
Authorization: `Bearer ${this._apiKey}`,
|
|
38
|
+
},
|
|
39
|
+
body: JSON.stringify({
|
|
40
|
+
prompt,
|
|
41
|
+
device_id: this._deviceId,
|
|
42
|
+
...(startUrl ? { start_url: startUrl } : {}),
|
|
43
|
+
...(mountedPaths?.length ? { mount: mountedPaths } : {}),
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
if (!res.ok)
|
|
47
|
+
throw new Error(`POST /query failed: ${res.status}`);
|
|
48
|
+
const data = (await res.json());
|
|
49
|
+
return data.run_id;
|
|
50
|
+
}
|
|
51
|
+
async _mount(paths) {
|
|
52
|
+
const destDir = join(homedir(), "Downloads", ".reduck");
|
|
53
|
+
await mkdir(destDir, { recursive: true });
|
|
54
|
+
return Promise.all(paths.map(async (p) => {
|
|
55
|
+
const dest = join(destDir, basename(p));
|
|
56
|
+
await cp(p, dest);
|
|
57
|
+
return dest;
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RunEvent } from "./types.js";
|
|
2
|
+
export declare class Run implements AsyncIterable<RunEvent> {
|
|
3
|
+
private readonly _runIdPromise;
|
|
4
|
+
private readonly _wsBase;
|
|
5
|
+
private readonly _apiKey;
|
|
6
|
+
private _ws;
|
|
7
|
+
constructor(runIdPromise: Promise<string>, wsBase: string, apiKey: string);
|
|
8
|
+
get id(): Promise<string>;
|
|
9
|
+
[Symbol.asyncIterator](): AsyncGenerator<RunEvent>;
|
|
10
|
+
private _stream;
|
|
11
|
+
cancel(): Promise<void>;
|
|
12
|
+
}
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
export class Run {
|
|
3
|
+
_runIdPromise;
|
|
4
|
+
_wsBase;
|
|
5
|
+
_apiKey;
|
|
6
|
+
_ws = null;
|
|
7
|
+
constructor(runIdPromise, wsBase, apiKey) {
|
|
8
|
+
this._runIdPromise = runIdPromise;
|
|
9
|
+
this._wsBase = wsBase;
|
|
10
|
+
this._apiKey = apiKey;
|
|
11
|
+
}
|
|
12
|
+
get id() {
|
|
13
|
+
return this._runIdPromise;
|
|
14
|
+
}
|
|
15
|
+
[Symbol.asyncIterator]() {
|
|
16
|
+
return this._stream();
|
|
17
|
+
}
|
|
18
|
+
async *_stream() {
|
|
19
|
+
const runId = await this._runIdPromise;
|
|
20
|
+
const wsUrl = `${this._wsBase}/run/${runId}/events`;
|
|
21
|
+
const ws = new WebSocket(wsUrl, {
|
|
22
|
+
headers: { Authorization: `Bearer ${this._apiKey}` },
|
|
23
|
+
});
|
|
24
|
+
this._ws = ws;
|
|
25
|
+
const queue = [];
|
|
26
|
+
let resolve = null;
|
|
27
|
+
let done = false;
|
|
28
|
+
let error = null;
|
|
29
|
+
ws.on("message", (data) => {
|
|
30
|
+
const event = JSON.parse(data.toString());
|
|
31
|
+
queue.push(event);
|
|
32
|
+
resolve?.();
|
|
33
|
+
});
|
|
34
|
+
ws.on("error", (err) => {
|
|
35
|
+
error = err;
|
|
36
|
+
done = true;
|
|
37
|
+
resolve?.();
|
|
38
|
+
});
|
|
39
|
+
ws.on("close", () => {
|
|
40
|
+
done = true;
|
|
41
|
+
resolve?.();
|
|
42
|
+
});
|
|
43
|
+
await new Promise((r) => ws.once("open", r));
|
|
44
|
+
while (true) {
|
|
45
|
+
while (queue.length > 0) {
|
|
46
|
+
const event = queue.shift();
|
|
47
|
+
yield event;
|
|
48
|
+
if (event.type === "done" || event.type === "failure" || event.type === "cancel") {
|
|
49
|
+
ws.close();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (done) {
|
|
54
|
+
if (error)
|
|
55
|
+
throw error;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
await new Promise((r) => { resolve = r; });
|
|
59
|
+
resolve = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async cancel() {
|
|
63
|
+
const runId = await this._runIdPromise;
|
|
64
|
+
const baseUrl = this._wsBase
|
|
65
|
+
.replace("wss://", "https://")
|
|
66
|
+
.replace("ws://", "http://");
|
|
67
|
+
await fetch(`${baseUrl}/run/${runId}/cancel`, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
headers: { Authorization: `Bearer ${this._apiKey}` },
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type RunEvent = {
|
|
2
|
+
type: "progress";
|
|
3
|
+
text: string;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
} | {
|
|
6
|
+
type: "assistant";
|
|
7
|
+
text: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
} | {
|
|
10
|
+
type: "turn_completed";
|
|
11
|
+
timestamp: number;
|
|
12
|
+
} | {
|
|
13
|
+
type: "run_started";
|
|
14
|
+
timestamp: number;
|
|
15
|
+
} | {
|
|
16
|
+
type: "run_completed";
|
|
17
|
+
timestamp: number;
|
|
18
|
+
} | {
|
|
19
|
+
type: "done";
|
|
20
|
+
success: boolean;
|
|
21
|
+
output?: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: "failure";
|
|
24
|
+
error: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "cancel";
|
|
27
|
+
};
|
|
28
|
+
export interface RunOptions {
|
|
29
|
+
startUrl?: string;
|
|
30
|
+
mount?: string[];
|
|
31
|
+
}
|
|
32
|
+
export interface ReduckClientOptions {
|
|
33
|
+
apiKey: string;
|
|
34
|
+
deviceId?: string;
|
|
35
|
+
baseUrl?: string;
|
|
36
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|