@reduck-ai/sdk 0.1.0 → 1.0.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.
@@ -0,0 +1,13 @@
1
+ import { type ReduckClientOptions, type RunOptions } from "./types.js";
2
+ import { Run } from "./run.js";
3
+ export declare class ReduckClient {
4
+ private readonly _auth;
5
+ private readonly _deviceId;
6
+ private readonly _baseUrl;
7
+ constructor({ auth, deviceId, baseUrl }: ReduckClientOptions);
8
+ consentNotice(): Promise<void>;
9
+ run(prompt: string, { startUrl, mount }?: RunOptions): Run;
10
+ interrupt(runId: string): Promise<void>;
11
+ private _createRun;
12
+ private _mount;
13
+ }
package/dist/client.js ADDED
@@ -0,0 +1,85 @@
1
+ import { cp, mkdir } from "node:fs/promises";
2
+ import { createInterface } from "node:readline";
3
+ import { basename, join } from "node:path";
4
+ import { homedir } from "node:os";
5
+ import { authHeader } from "./types.js";
6
+ import { Run } from "./run.js";
7
+ const DEFAULT_BASE_URL = "https://runner.pointandclick.ai";
8
+ export class ReduckClient {
9
+ _auth;
10
+ _deviceId;
11
+ _baseUrl;
12
+ constructor({ auth, deviceId, baseUrl = DEFAULT_BASE_URL }) {
13
+ const resolvedDeviceId = deviceId ?? process.env.REDUCK_DEVICE_ID;
14
+ if (!resolvedDeviceId)
15
+ throw new Error("deviceId is required (or set REDUCK_DEVICE_ID)");
16
+ this._auth = auth;
17
+ this._deviceId = resolvedDeviceId;
18
+ this._baseUrl = baseUrl.replace(/\/$/, "");
19
+ }
20
+ async consentNotice() {
21
+ const res = await fetch(`${this._baseUrl}/consent-notice`, {
22
+ headers: authHeader(this._auth),
23
+ });
24
+ if (!res.ok)
25
+ throw new Error(`GET /consent-notice failed: ${res.status}`);
26
+ const data = (await res.json());
27
+ if (!data.content)
28
+ return;
29
+ const answer = await new Promise((resolve) => {
30
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
31
+ rl.question(`${data.content} (y/n) `, (a) => { rl.close(); resolve(a.trim().toLowerCase()); });
32
+ });
33
+ if (answer !== "y" && answer !== "yes")
34
+ throw new Error("Consent not given");
35
+ const post = await fetch(`${this._baseUrl}/consent-notice`, {
36
+ method: "POST",
37
+ headers: { "Content-Type": "application/json", ...authHeader(this._auth) },
38
+ body: JSON.stringify({ version: data.version }),
39
+ });
40
+ if (!post.ok)
41
+ throw new Error(`POST /consent-notice failed: ${post.status}`);
42
+ }
43
+ run(prompt, { startUrl, mount } = {}) {
44
+ const runIdPromise = this._createRun(prompt, startUrl, mount);
45
+ const wsBase = this._baseUrl
46
+ .replace("https://", "wss://")
47
+ .replace("http://", "ws://");
48
+ return new Run(runIdPromise, wsBase, this._auth);
49
+ }
50
+ async interrupt(runId) {
51
+ await fetch(`${this._baseUrl}/run/${runId}/cancel`, {
52
+ method: "POST",
53
+ headers: authHeader(this._auth),
54
+ });
55
+ }
56
+ async _createRun(prompt, startUrl, mount) {
57
+ const mountedPaths = mount?.length ? await this._mount(mount) : undefined;
58
+ const res = await fetch(`${this._baseUrl}/query`, {
59
+ method: "POST",
60
+ headers: {
61
+ "Content-Type": "application/json",
62
+ ...authHeader(this._auth),
63
+ },
64
+ body: JSON.stringify({
65
+ prompt,
66
+ device_id: this._deviceId,
67
+ ...(startUrl ? { start_url: startUrl } : {}),
68
+ ...(mountedPaths?.length ? { mount: mountedPaths } : {}),
69
+ }),
70
+ });
71
+ if (!res.ok)
72
+ throw new Error(`POST /query failed: ${res.status}`);
73
+ const data = (await res.json());
74
+ return data.run_id;
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
+ }
@@ -0,0 +1,3 @@
1
+ export { ReduckClient } from "./client.js";
2
+ export { Run } from "./run.js";
3
+ export type { AuthConfig, RunEvent, RunOptions, ReduckClientOptions } from "./types.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { ReduckClient } from "./client.js";
2
+ export { Run } from "./run.js";
package/dist/run.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { type AuthConfig, type RunEvent } from "./types.js";
2
+ export declare class Run implements AsyncIterable<RunEvent> {
3
+ private readonly _runIdPromise;
4
+ private readonly _wsBase;
5
+ private readonly _auth;
6
+ private _ws;
7
+ constructor(runIdPromise: Promise<string>, wsBase: string, auth: AuthConfig);
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,71 @@
1
+ import WebSocket from "ws";
2
+ import { authHeader } from "./types.js";
3
+ export class Run {
4
+ _runIdPromise;
5
+ _wsBase;
6
+ _auth;
7
+ _ws = null;
8
+ constructor(runIdPromise, wsBase, auth) {
9
+ this._runIdPromise = runIdPromise;
10
+ this._wsBase = wsBase;
11
+ this._auth = auth;
12
+ }
13
+ get id() {
14
+ return this._runIdPromise;
15
+ }
16
+ [Symbol.asyncIterator]() {
17
+ return this._stream();
18
+ }
19
+ async *_stream() {
20
+ const runId = await this._runIdPromise;
21
+ const wsUrl = `${this._wsBase}/run/${runId}/events`;
22
+ const ws = new WebSocket(wsUrl, { headers: authHeader(this._auth) });
23
+ this._ws = ws;
24
+ const queue = [];
25
+ let resolve = null;
26
+ let done = false;
27
+ let error = null;
28
+ ws.on("message", (data) => {
29
+ const event = JSON.parse(data.toString());
30
+ queue.push(event);
31
+ resolve?.();
32
+ });
33
+ ws.on("error", (err) => {
34
+ error = err;
35
+ done = true;
36
+ resolve?.();
37
+ });
38
+ ws.on("close", () => {
39
+ done = true;
40
+ resolve?.();
41
+ });
42
+ await new Promise((r) => ws.once("open", r));
43
+ while (true) {
44
+ while (queue.length > 0) {
45
+ const event = queue.shift();
46
+ yield event;
47
+ if (event.type === "done" || event.type === "failure" || event.type === "cancel") {
48
+ ws.close();
49
+ return;
50
+ }
51
+ }
52
+ if (done) {
53
+ if (error)
54
+ throw error;
55
+ return;
56
+ }
57
+ await new Promise((r) => { resolve = r; });
58
+ resolve = null;
59
+ }
60
+ }
61
+ async cancel() {
62
+ const runId = await this._runIdPromise;
63
+ const baseUrl = this._wsBase
64
+ .replace("wss://", "https://")
65
+ .replace("ws://", "http://");
66
+ await fetch(`${baseUrl}/run/${runId}/cancel`, {
67
+ method: "POST",
68
+ headers: authHeader(this._auth),
69
+ });
70
+ }
71
+ }
@@ -0,0 +1,41 @@
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 AuthConfig {
33
+ type: "oauth" | "api_key";
34
+ value: string;
35
+ }
36
+ export declare function authHeader(auth: AuthConfig): Record<string, string>;
37
+ export interface ReduckClientOptions {
38
+ auth: AuthConfig;
39
+ deviceId?: string;
40
+ baseUrl?: string;
41
+ }
package/dist/types.js ADDED
@@ -0,0 +1,5 @@
1
+ export function authHeader(auth) {
2
+ return auth.type === "oauth"
3
+ ? { Authorization: `Bearer ${auth.value}` }
4
+ : { "X-API-Key": auth.value };
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reduck-ai/sdk",
3
- "version": "0.1.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -17,15 +17,15 @@
17
17
  "publishConfig": {
18
18
  "access": "public"
19
19
  },
20
- "scripts": {
21
- "build": "pnpm exec tsc",
22
- "dev": "pnpm exec tsc --watch"
23
- },
24
20
  "dependencies": {
25
21
  "ws": "^8.0.0"
26
22
  },
27
23
  "devDependencies": {
28
24
  "@types/ws": "^8.0.0",
29
25
  "typescript": "^5.0.0"
26
+ },
27
+ "scripts": {
28
+ "build": "pnpm exec tsc",
29
+ "dev": "pnpm exec tsc --watch"
30
30
  }
31
- }
31
+ }