ruicode 0.75.1 → 0.83.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 +32 -1
- package/dist/binary.d.ts +0 -3
- package/dist/binary.js +0 -4
- package/dist/client.d.ts +0 -16
- package/dist/client.js +0 -39
- package/dist/errors.d.ts +5 -1
- package/dist/errors.js +5 -3
- package/dist/feed.d.ts +8 -8
- package/dist/feed.js +56 -30
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -2
- package/dist/notifications.d.ts +0 -1
- package/dist/notifications.js +0 -2
- package/dist/parts.d.ts +27 -0
- package/dist/parts.js +19 -0
- package/dist/sessions.d.ts +44 -16
- package/dist/sessions.js +48 -38
- package/dist/settings.d.ts +0 -3
- package/dist/settings.js +1 -12
- package/dist/transport.d.ts +1 -9
- package/dist/transport.js +1 -12
- package/dist/wire.d.ts +1131 -760
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -155,6 +155,21 @@ Only top-level field names become camelCase (for example `callOrdinal`); nested
|
|
|
155
155
|
Text-bearing kinds add `text`, or `truncated: true` when the text exceeds 16 KiB; `contentBytes` gives its byte length.
|
|
156
156
|
Use `session.readContent(entry)` or `readContentBytes(entry)` for the whole text.
|
|
157
157
|
|
|
158
|
+
A parts value has `entry.parts`, the certified ordered list, and no `entry.text`. Any entry that carries content can carry parts. Text parts have `text` inline, in order, while the entry's inline total stays within 16 KiB; a part past that has `truncated: true`. Image parts include their MIME type as the Host reports it, dimensions and optional name. `contentBytes` is the manifest length, not the sum of the parts. Read any part's exact bytes with `session.readPart(entry, part.ordinal)`. `readContent` and `readContentBytes` reject with `ContentIsPartsError` for a parts value, as the Host refuses it on its text route.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
for await (const entry of session.listActivity()) {
|
|
162
|
+
if ("parts" in entry && entry.parts) {
|
|
163
|
+
for (const part of entry.parts) {
|
|
164
|
+
if (part.type === "text") console.log(part.text ?? `(use readPart for ${part.bytes} bytes)`);
|
|
165
|
+
else await writeFile(`image-${part.ordinal}.${part.mime === "image/png" ? "png" : "jpg"}`, await session.readPart(entry, part.ordinal));
|
|
166
|
+
}
|
|
167
|
+
} else if ("text" in entry) console.log(entry.text);
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
The example uses `writeFile` from `node:fs/promises`.
|
|
172
|
+
|
|
158
173
|
Following polls with backoff from 250 ms to 2 s only while the Session is busy.
|
|
159
174
|
An empty page in `idle`, `finished`, `failed`, or `stopped` state with nothing queued puts the iterator to sleep **without a timer**.
|
|
160
175
|
It wakes after a successful `send`, `update`, `decide`, `interrupt`, `stop`, `archive`, or `unarchive` through any handle for that Session ID on the same Rui instance, then starts polling again at 250 ms.
|
|
@@ -203,7 +218,14 @@ await session.update({ model: "gpt-5.5" });
|
|
|
203
218
|
`await session.status()` reads the Current report: work `state` and `since`, `turn`, `operation`, `pendingMessages`, `parent`, `label`, `model`, `effort`, `tools`, `permissionMode`, `latestOutcome` and `archiveState`.
|
|
204
219
|
Counts, times and Turn/Operation IDs stay decimal strings; absent targets, parents and outcomes are `null`.
|
|
205
220
|
A separate ending is not in the report, so status does not invent one.
|
|
206
|
-
`
|
|
221
|
+
`interruptTarget` is the Host-served target from that snapshot. Pass it explicitly with a key to interrupt that observed call in one request, and reuse the same key to recover its answer:
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
const { interruptTarget } = await session.status();
|
|
225
|
+
if (interruptTarget) await session.interrupt(interruptTarget, { key: "interrupt:1" });
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
`key` is also accepted by other writes using `WriteOptions`; it takes precedence if `idempotencyKey` is also supplied. `operation_resolved` and `target_mismatch` resolve without effect; other rejections throw. The convenience `interrupt({ key: "interrupt:1" })` uses the last target observed by this handle through `status()` or a feed page. It sends nothing if the handle has observed no target, or its last observation had a null target. Neither form reads first.
|
|
207
229
|
|
|
208
230
|
Presets are complete agent specs passed at connect and kept only in Host memory.
|
|
209
231
|
Creation copies the preset, with every supplied field (including `null`) overriding it.
|
|
@@ -241,3 +263,12 @@ A later Message to an archived Session restores it transactionally before admitt
|
|
|
241
263
|
`unarchive()` restores only organization and never resumes stopped work.
|
|
242
264
|
Replaying an archive request with its original key does not archive again after a later restore.
|
|
243
265
|
Both methods notify local activity followers without submitting a Message or starting work.
|
|
266
|
+
|
|
267
|
+
## Stop one Action
|
|
268
|
+
|
|
269
|
+
`session.stopAction(action, { idempotencyKey })` selects one Action in that Session for stop.
|
|
270
|
+
It returns the existing Resolution position when the Action has already ended, or `null` when stop selection precedes completion.
|
|
271
|
+
Use `session.readAction(action)` to read completion, saved output, and published values. To continue a value page, pass its `values.next` as the third argument: `session.readAction(action, "0", values.next)`. The second argument is the independent final-output byte offset.
|
|
272
|
+
Retain the key and exact inputs before sending. Retry them unchanged to recover a lost reply.
|
|
273
|
+
A stop selection suppresses that Action's final Session input and preserves its sole Resolution.
|
|
274
|
+
Other Actions continue.
|
package/dist/binary.d.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
/** The platform packages, by `process.platform` and `process.arch` (D127, D186). */
|
|
2
1
|
export declare const PLATFORM_PACKAGES: Record<string, Record<string, string>>;
|
|
3
2
|
export interface BinaryLookup {
|
|
4
3
|
platform?: string;
|
|
5
4
|
arch?: string;
|
|
6
|
-
/** A file URL or path that module resolution starts from. Default: this module. */
|
|
7
5
|
from?: string;
|
|
8
6
|
}
|
|
9
|
-
/** The executable to start: `binary` if given, else the installed platform package's, else `rui` on PATH. */
|
|
10
7
|
export declare function resolveBinary(binary: string | undefined, lookup?: BinaryLookup): string;
|
package/dist/binary.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
// Where the `rui` executable comes from when the caller names none: the
|
|
2
|
-
// platform package installed beside this one (D125, D127), else PATH.
|
|
3
1
|
import { createRequire } from "node:module";
|
|
4
2
|
import { dirname, join } from "node:path";
|
|
5
|
-
/** The platform packages, by `process.platform` and `process.arch` (D127, D186). */
|
|
6
3
|
export const PLATFORM_PACKAGES = {
|
|
7
4
|
linux: { x64: "ruicode-linux-x64", arm64: "ruicode-linux-arm64" },
|
|
8
5
|
darwin: { arm64: "ruicode-darwin-arm64" },
|
|
9
6
|
};
|
|
10
|
-
/** The executable to start: `binary` if given, else the installed platform package's, else `rui` on PATH. */
|
|
11
7
|
export function resolveBinary(binary, lookup = {}) {
|
|
12
8
|
if (binary !== undefined)
|
|
13
9
|
return binary;
|
package/dist/client.d.ts
CHANGED
|
@@ -34,7 +34,6 @@ export declare class Rui extends EventEmitter<{
|
|
|
34
34
|
exit: [ExitEvent];
|
|
35
35
|
}> {
|
|
36
36
|
#private;
|
|
37
|
-
/** Local Session write notifications, shared by every handle. Internal. */
|
|
38
37
|
readonly activity: SessionNotifications;
|
|
39
38
|
/** Create and list Sessions. */
|
|
40
39
|
readonly sessions: Sessions;
|
|
@@ -53,27 +52,12 @@ export declare class Rui extends EventEmitter<{
|
|
|
53
52
|
* Host's 10 s shutdown (D129). Later calls fail with RuiUnavailableError.
|
|
54
53
|
*/
|
|
55
54
|
close(): Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Sends one request to the ready Host. A call that finds the child gone
|
|
58
|
-
* waits for the restarted Host and is sent once more. Internal: the
|
|
59
|
-
* package's calls are built on it.
|
|
60
|
-
*/
|
|
61
55
|
call(path: string, body: string, environment?: Buffer, limits?: ReplyLimits): Promise<Buffer>;
|
|
62
|
-
/**
|
|
63
|
-
* Sends one request built for the Host's Store. `build` receives the
|
|
64
|
-
* canonical Store name the Host reported at readiness. Internal: the
|
|
65
|
-
* package's calls are built on it.
|
|
66
|
-
*/
|
|
67
56
|
exchange(path: string, build: (store: string) => string, environment?: Buffer, limits?: ReplyLimits): Promise<Buffer>;
|
|
68
|
-
/** The running child's process id. Internal. */
|
|
69
57
|
get pid(): number | undefined;
|
|
70
|
-
/** The latest host-info reply. Internal. */
|
|
71
58
|
get hostInfo(): HostInfo | undefined;
|
|
72
59
|
}
|
|
73
60
|
export declare function launchFor(settings: HostSettings, runtimeDir: string, options?: ClientOptions): Launch;
|
|
74
|
-
/** One length-prefixed JSON frame: a 4-byte big-endian length, then UTF-8 JSON (D130). */
|
|
75
61
|
export declare function encodeSettings(document: Schemas["HostSettings"]): Buffer;
|
|
76
|
-
/** Only the fixed leading child receipt is authoritative; later path bytes are not records. */
|
|
77
62
|
export declare function readinessInstance(prefix: Buffer): string | undefined;
|
|
78
|
-
/** The refusal a child wrote as its one stdout line, `{"rui_start_error":{code,message}}` (D211). */
|
|
79
63
|
export declare function startError(output: string): RuiStartError | undefined;
|
package/dist/client.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
// Rui.connect starts the Host as a child of this process, hands it the settings
|
|
2
|
-
// document on stdin and waits until it answers host-info on the socket in its
|
|
3
|
-
// private interface (D107, D112, D130, D131). By default the child lives as
|
|
4
|
-
// long as stdin stays open (D5, D118); settings may select detached lifetime.
|
|
5
|
-
// A child that exits on its own is restarted with
|
|
6
|
-
// capped backoff, and calls in flight are retried once the new Host is ready;
|
|
7
|
-
// every write carries a request key, so a retry is safe (D118, D129).
|
|
8
1
|
import { spawn } from "node:child_process";
|
|
9
2
|
import { EventEmitter } from "node:events";
|
|
10
3
|
import { mkdtemp, rm, stat, symlink } from "node:fs/promises";
|
|
@@ -22,15 +15,11 @@ const DEFAULT_LOCK_WAIT_MS = 120_000;
|
|
|
22
15
|
const LOCK_RETRY_MS = 500;
|
|
23
16
|
const FIRST_RESTART_MS = 500;
|
|
24
17
|
const MAX_RESTART_MS = 30_000;
|
|
25
|
-
/** The Host bounds its own shutdown at 10 s (D118); this leaves room for the exit to arrive. */
|
|
26
18
|
const CLOSE_GRACE_MS = 15_000;
|
|
27
19
|
const READY_POLL_MS = 25;
|
|
28
20
|
const READINESS_RECEIPT_BYTES = Buffer.byteLength("ready instance=") + 32 + 1;
|
|
29
|
-
/** How long a refused connection waits for the child's exit before it counts as unanswered. */
|
|
30
21
|
const EXIT_NOTICE_MS = 2_000;
|
|
31
|
-
/** Transient refusals during automatic restart, not initial connect. */
|
|
32
22
|
const RETRYABLE_START_CODES = new Set(["store_locked", "mysql_unreachable"]);
|
|
33
|
-
/** A failed start, with the child's exit when there was a child. */
|
|
34
23
|
class AttemptFailed extends Error {
|
|
35
24
|
error;
|
|
36
25
|
exit;
|
|
@@ -44,14 +33,12 @@ export class Rui extends EventEmitter {
|
|
|
44
33
|
#launch;
|
|
45
34
|
#runtimeDir;
|
|
46
35
|
#host;
|
|
47
|
-
/** The child of the latest start, ready or not. */
|
|
48
36
|
#child;
|
|
49
37
|
#waiters = [];
|
|
50
38
|
#backoff = FIRST_RESTART_MS;
|
|
51
39
|
#restart;
|
|
52
40
|
#closing;
|
|
53
41
|
#dead;
|
|
54
|
-
/** Local Session write notifications, shared by every handle. Internal. */
|
|
55
42
|
activity = new SessionNotifications();
|
|
56
43
|
/** Create and list Sessions. */
|
|
57
44
|
sessions = new Sessions(this);
|
|
@@ -133,11 +120,6 @@ export class Rui extends EventEmitter {
|
|
|
133
120
|
await rm(this.#runtimeDir, { recursive: true, force: true });
|
|
134
121
|
}
|
|
135
122
|
}
|
|
136
|
-
/**
|
|
137
|
-
* Sends one request to the ready Host. A call that finds the child gone
|
|
138
|
-
* waits for the restarted Host and is sent once more. Internal: the
|
|
139
|
-
* package's calls are built on it.
|
|
140
|
-
*/
|
|
141
123
|
async call(path, body, environment, limits) {
|
|
142
124
|
let retried = false;
|
|
143
125
|
for (;;) {
|
|
@@ -162,20 +144,13 @@ export class Rui extends EventEmitter {
|
|
|
162
144
|
}
|
|
163
145
|
}
|
|
164
146
|
}
|
|
165
|
-
/**
|
|
166
|
-
* Sends one request built for the Host's Store. `build` receives the
|
|
167
|
-
* canonical Store name the Host reported at readiness. Internal: the
|
|
168
|
-
* package's calls are built on it.
|
|
169
|
-
*/
|
|
170
147
|
async exchange(path, build, environment, limits) {
|
|
171
148
|
const host = await this.#ready();
|
|
172
149
|
return this.call(path, build(host.info.store), environment, limits);
|
|
173
150
|
}
|
|
174
|
-
/** The running child's process id. Internal. */
|
|
175
151
|
get pid() {
|
|
176
152
|
return this.#host?.child.pid;
|
|
177
153
|
}
|
|
178
|
-
/** The latest host-info reply. Internal. */
|
|
179
154
|
get hostInfo() {
|
|
180
155
|
return this.#host?.info;
|
|
181
156
|
}
|
|
@@ -208,13 +183,11 @@ export class Rui extends EventEmitter {
|
|
|
208
183
|
waiter.reject(error);
|
|
209
184
|
}
|
|
210
185
|
#scheduleRestart() {
|
|
211
|
-
// Once close has begun nothing may start again, whoever asked (an exit listener may close).
|
|
212
186
|
if (this.#closing)
|
|
213
187
|
return;
|
|
214
188
|
const delay = this.#backoff;
|
|
215
189
|
this.#backoff = Math.min(this.#backoff * 2, MAX_RESTART_MS);
|
|
216
190
|
this.#restart = setTimeout(() => void this.#restartNow(), delay);
|
|
217
|
-
// An idle client does not keep the process alive; a call waiting for the restart does (see #ready).
|
|
218
191
|
if (this.#waiters.length === 0)
|
|
219
192
|
this.#restart.unref();
|
|
220
193
|
}
|
|
@@ -246,7 +219,6 @@ export class Rui extends EventEmitter {
|
|
|
246
219
|
}
|
|
247
220
|
}
|
|
248
221
|
export function launchFor(settings, runtimeDir, options = {}) {
|
|
249
|
-
// Inherit program/home lookup plus only the secret sources this document names.
|
|
250
222
|
const env = Object.create(null);
|
|
251
223
|
for (const name of ["PATH", "HOME", "TMPDIR"]) {
|
|
252
224
|
const value = process.env[name];
|
|
@@ -259,13 +231,11 @@ export function launchFor(settings, runtimeDir, options = {}) {
|
|
|
259
231
|
socketPath: join(runtimeDir, "interface", SOCKET_NAME), env, detached,
|
|
260
232
|
};
|
|
261
233
|
}
|
|
262
|
-
/** Release all restart-owned secret bytes/references on every terminal transition. */
|
|
263
234
|
function disposeLaunch(launch) {
|
|
264
235
|
launch.document.fill(0);
|
|
265
236
|
for (const name of Object.keys(launch.env))
|
|
266
237
|
delete launch.env[name];
|
|
267
238
|
}
|
|
268
|
-
/** Starts one child and resolves once it answers host-info, or throws AttemptFailed. */
|
|
269
239
|
async function start(launch, spawned) {
|
|
270
240
|
let child;
|
|
271
241
|
try {
|
|
@@ -279,7 +249,6 @@ async function start(launch, spawned) {
|
|
|
279
249
|
}
|
|
280
250
|
spawned(child);
|
|
281
251
|
const stdout = [];
|
|
282
|
-
// This fixed prefix precedes every caller-controlled path in the child's record.
|
|
283
252
|
const receipt = Buffer.alloc(READINESS_RECEIPT_BYTES);
|
|
284
253
|
let receiptBytes = 0;
|
|
285
254
|
let readyInstance;
|
|
@@ -301,12 +270,10 @@ async function start(launch, spawned) {
|
|
|
301
270
|
const outputEnded = new Promise((done) => child.stdout?.once("close", done));
|
|
302
271
|
const launchError = new Promise((done) => child.once("error", done));
|
|
303
272
|
const exited = exitOf(child);
|
|
304
|
-
// The child must not keep this process alive; its stdin closing is how it learns this process ended.
|
|
305
273
|
child.unref();
|
|
306
274
|
child.stdout?.unref();
|
|
307
275
|
child.stdin?.unref();
|
|
308
276
|
child.stdin?.on("error", () => { });
|
|
309
|
-
// write owns this copy until its callback; retain the restart document separately.
|
|
310
277
|
const bytes = Buffer.from(launch.document);
|
|
311
278
|
const wipeWrite = () => bytes.fill(0);
|
|
312
279
|
child.stdin?.once("close", wipeWrite);
|
|
@@ -332,7 +299,6 @@ async function start(launch, spawned) {
|
|
|
332
299
|
if (ended instanceof Error) {
|
|
333
300
|
throw new AttemptFailed(new RuiUnavailableError(`could not start ${launch.binary}: ${ended.message}`, { cause: ended }), undefined);
|
|
334
301
|
}
|
|
335
|
-
// The refusal line is written before the exit; give the pipe a moment to deliver it.
|
|
336
302
|
await Promise.race([outputEnded, sleep(1_000)]);
|
|
337
303
|
const output = Buffer.concat(stdout);
|
|
338
304
|
let refusal;
|
|
@@ -353,7 +319,6 @@ async function start(launch, spawned) {
|
|
|
353
319
|
stdout.length = 0;
|
|
354
320
|
}
|
|
355
321
|
}
|
|
356
|
-
/** Readiness proves the Host admitted the path; decoding and alias effects belong after it. */
|
|
357
322
|
async function createInterfaceAlias(launch) {
|
|
358
323
|
const dir = launch.interfaceDir;
|
|
359
324
|
const target = typeof dir === "string" ? dir : Buffer.from(dir.hex, "hex");
|
|
@@ -379,7 +344,6 @@ async function probe(launch, instance) {
|
|
|
379
344
|
throw new RuiUnavailableError("the Host socket did not match the started child's readiness instance");
|
|
380
345
|
return info;
|
|
381
346
|
}
|
|
382
|
-
/** One length-prefixed JSON frame: a 4-byte big-endian length, then UTF-8 JSON (D130). */
|
|
383
347
|
export function encodeSettings(document) {
|
|
384
348
|
const json = Buffer.from(JSON.stringify(document));
|
|
385
349
|
try {
|
|
@@ -392,13 +356,11 @@ export function encodeSettings(document) {
|
|
|
392
356
|
json.fill(0);
|
|
393
357
|
}
|
|
394
358
|
}
|
|
395
|
-
/** Only the fixed leading child receipt is authoritative; later path bytes are not records. */
|
|
396
359
|
export function readinessInstance(prefix) {
|
|
397
360
|
if (prefix.length !== READINESS_RECEIPT_BYTES)
|
|
398
361
|
return undefined;
|
|
399
362
|
return /^ready instance=([0-9a-f]{32}) $/.exec(prefix.toString("latin1"))?.[1];
|
|
400
363
|
}
|
|
401
|
-
/** The refusal a child wrote as its one stdout line, `{"rui_start_error":{code,message}}` (D211). */
|
|
402
364
|
export function startError(output) {
|
|
403
365
|
for (const line of output.split("\n").reverse()) {
|
|
404
366
|
if (!line.trim())
|
|
@@ -411,7 +373,6 @@ export function startError(output) {
|
|
|
411
373
|
}
|
|
412
374
|
}
|
|
413
375
|
catch {
|
|
414
|
-
// Not the refusal line.
|
|
415
376
|
}
|
|
416
377
|
}
|
|
417
378
|
return undefined;
|
package/dist/errors.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ type Status<T> = T extends {
|
|
|
12
12
|
} ? S : never;
|
|
13
13
|
export type RequestAnswer = Answer<Reply>;
|
|
14
14
|
export type RequestRejectionCode = Code<RequestAnswer>;
|
|
15
|
-
/** HTTP ErrorReply deliberately accepts codes beyond the command rejection enums. */
|
|
16
15
|
export type RequestErrorCode = RequestRejectionCode | Schemas["ErrorReply"]["code"];
|
|
17
16
|
type RequestErrorType = Schemas["ErrorReply"]["type"] | Status<Extract<RequestAnswer, {
|
|
18
17
|
code: string;
|
|
@@ -39,4 +38,9 @@ export declare class RuiUnavailableError extends Error {
|
|
|
39
38
|
cause?: unknown;
|
|
40
39
|
});
|
|
41
40
|
}
|
|
41
|
+
/** The entry contains parts; read an individual part instead. */
|
|
42
|
+
export declare class ContentIsPartsError extends Error {
|
|
43
|
+
readonly name = "ContentIsPartsError";
|
|
44
|
+
constructor();
|
|
45
|
+
}
|
|
42
46
|
export {};
|
package/dist/errors.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// The three error types a caller sees (D116). Request failures are
|
|
2
|
-
// RuiRequestError, a refusal at open is RuiStartError, and a call that finds
|
|
3
|
-
// no running Host is RuiUnavailableError.
|
|
4
1
|
/** Rui rejected one request. `code` is the Host's rejection code; `message` preserves advisory detail when provided. */
|
|
5
2
|
export class RuiRequestError extends Error {
|
|
6
3
|
name = "RuiRequestError";
|
|
@@ -29,3 +26,8 @@ export class RuiUnavailableError extends Error {
|
|
|
29
26
|
super(message, options);
|
|
30
27
|
}
|
|
31
28
|
}
|
|
29
|
+
/** The entry contains parts; read an individual part instead. */
|
|
30
|
+
export class ContentIsPartsError extends Error {
|
|
31
|
+
name = "ContentIsPartsError";
|
|
32
|
+
constructor() { super("Content contains parts; use session.readPart(entry, ordinal)"); }
|
|
33
|
+
}
|
package/dist/feed.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { Link } from "./sessions.ts";
|
|
1
|
+
import type { InterruptTarget, Link, ServedWork } from "./sessions.ts";
|
|
2
|
+
import { type Part } from "./parts.ts";
|
|
2
3
|
import type { components } from "./wire.ts";
|
|
3
4
|
type Schemas = components["schemas"];
|
|
5
|
+
export declare function servedWork(work: Pick<Schemas["SessionWork"], "attention" | "group" | "actions">): ServedWork;
|
|
4
6
|
type Row = Schemas["ActivityItem"];
|
|
5
7
|
/** Every kind the Host's feed has, in its own order. */
|
|
6
8
|
export declare const FEED_KINDS: readonly ["admission", "user", "assistant", "call", "tool_result", "hosted_call", "turn_ended", "stop", "sent", "send_failed", "received", "approval_requested", "session_changed", "compacted", "model_call"];
|
|
@@ -23,7 +25,6 @@ export interface ListActivityOptions {
|
|
|
23
25
|
/** Ends the iterator, even while it waits for new entries: a pending `next()` then resolves as done. */
|
|
24
26
|
signal?: AbortSignal;
|
|
25
27
|
}
|
|
26
|
-
/** Maps only the row's own keys; nested values retain their wire spelling (D128). */
|
|
27
28
|
type CamelKey<Key extends string> = Key extends `${infer Head}_${infer Tail}` ? `${Head}${Capitalize<CamelKey<Tail>>}` : Key;
|
|
28
29
|
type CamelRow<Payload> = {
|
|
29
30
|
[Key in keyof Payload as Key extends "session" ? "sessionId" : Key extends string ? CamelKey<Key> : Key]: Payload[Key];
|
|
@@ -33,8 +34,10 @@ type EntryText = {
|
|
|
33
34
|
text?: string;
|
|
34
35
|
/** Text exceeds 16 KiB: use `session.readContent(entry)`. */
|
|
35
36
|
truncated?: boolean;
|
|
36
|
-
/** The
|
|
37
|
+
/** The value's length in bytes when this kind carries one: the text's length, or the manifest's for a parts value. */
|
|
37
38
|
contentBytes?: string;
|
|
39
|
+
/** Certified parts, in order, when the content is a parts value. */
|
|
40
|
+
parts?: Part[];
|
|
38
41
|
};
|
|
39
42
|
type EntryFromRow<R extends Row> = R extends Row ? {
|
|
40
43
|
[Kind in keyof R["value"]]: Omit<R, "value"> & {
|
|
@@ -48,23 +51,21 @@ type EntryFromRow<R extends Row> = R extends Row ? {
|
|
|
48
51
|
* `session_changed` uses `reasoning_effort_refused` and inline `providerText` for a refused effort change.
|
|
49
52
|
*/
|
|
50
53
|
export type FeedEntry = EntryFromRow<Row>;
|
|
51
|
-
/** An opaque cursor: after the row at this position and ordinal. */
|
|
52
54
|
export declare function encodeCursor(position: string, ordinal: string): string;
|
|
53
55
|
export declare function decodeCursor(cursor: string): {
|
|
54
56
|
position: string;
|
|
55
57
|
ordinal: string;
|
|
56
58
|
};
|
|
57
|
-
/** The
|
|
59
|
+
/** The Host owns the rule: its text route refuses a parts value as `ContentIsParts`. */
|
|
58
60
|
export declare function readContentBytes(link: Link, session: string, entry: {
|
|
59
61
|
position: string;
|
|
60
62
|
ordinal: string;
|
|
61
63
|
}): Promise<Buffer>;
|
|
62
|
-
/** The whole text of an entry as a string. A text past the JavaScript string limit (about 512 MiB) throws; use `readContentBytes`. */
|
|
63
64
|
export declare function readContent(link: Link, session: string, entry: {
|
|
64
65
|
position: string;
|
|
65
66
|
ordinal: string;
|
|
66
67
|
}): Promise<string>;
|
|
67
|
-
export declare function listActivity(link: Link, session: string, options?: ListActivityOptions): AsyncGenerator<FeedEntry, void, undefined>;
|
|
68
|
+
export declare function listActivity(link: Link, session: string, options?: ListActivityOptions, observe?: (target: InterruptTarget | null) => void): AsyncGenerator<FeedEntry, void, undefined>;
|
|
68
69
|
export interface SearchActivityOptions {
|
|
69
70
|
/** Resume before this search entry's cursor. */
|
|
70
71
|
before?: string;
|
|
@@ -75,6 +76,5 @@ export interface SearchActivityOptions {
|
|
|
75
76
|
export type ActivitySearchEntry = Schemas["ActivitySearchReply"]["items"][number] & {
|
|
76
77
|
cursor: string;
|
|
77
78
|
};
|
|
78
|
-
/** Search public feed text newest first. Empty budget-limited pages still advance. */
|
|
79
79
|
export declare function searchActivity(link: Link, session: string, query: string, options?: SearchActivityOptions): AsyncGenerator<ActivitySearchEntry, void, undefined>;
|
|
80
80
|
export {};
|
package/dist/feed.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// references only, and no cursor type. The client encodes the cursor from a
|
|
4
|
-
// row's position and ordinal, and fills `text` from /v1/activity-content, so
|
|
5
|
-
// the Host holds no reply buffer for either (D168).
|
|
1
|
+
import { ContentIsPartsError, RuiRequestError } from "./errors.js";
|
|
2
|
+
import { listParts, readPartBytes } from "./parts.js";
|
|
6
3
|
import { WIRE_VERSION } from "./transport.js";
|
|
4
|
+
export function servedWork(work) {
|
|
5
|
+
if (!work || !("attention" in work) || !("group" in work) || !work.actions ||
|
|
6
|
+
typeof work.actions.stop !== "boolean" || !("interrupt" in work.actions) || !("decide" in work.actions) ||
|
|
7
|
+
!["needs_you", "working", "queued", "done"].includes(work.group) ||
|
|
8
|
+
(work.attention !== null && (typeof work.attention !== "object" ||
|
|
9
|
+
!["approval", "failed", "finished"].includes(work.attention.kind) ||
|
|
10
|
+
typeof work.attention.turn !== "string" || typeof work.attention.step !== "string")) ||
|
|
11
|
+
(work.actions.decide !== null && typeof work.actions.decide !== "string") ||
|
|
12
|
+
(work.actions.interrupt !== null && (typeof work.actions.interrupt !== "object" ||
|
|
13
|
+
typeof work.actions.interrupt.turn !== "string" || typeof work.actions.interrupt.operation !== "string"))) {
|
|
14
|
+
throw new Error("Invalid work reply: missing served facts");
|
|
15
|
+
}
|
|
16
|
+
return { attention: work.attention, group: work.group, canStop: work.actions.stop,
|
|
17
|
+
decide: work.actions.decide, interruptTarget: work.actions.interrupt };
|
|
18
|
+
}
|
|
7
19
|
/** Every kind the Host's feed has, in its own order. */
|
|
8
20
|
export const FEED_KINDS = [
|
|
9
21
|
"admission",
|
|
@@ -22,7 +34,6 @@ export const FEED_KINDS = [
|
|
|
22
34
|
"compacted",
|
|
23
35
|
"model_call",
|
|
24
36
|
];
|
|
25
|
-
// A kind the wire gains fails to compile here until the list has it.
|
|
26
37
|
const complete = true;
|
|
27
38
|
void complete;
|
|
28
39
|
/** Entry text is inline up to this many bytes (D168). */
|
|
@@ -30,9 +41,7 @@ export const INLINE_TEXT_BYTES = 16 * 1024;
|
|
|
30
41
|
const PAGE_ROWS = 16;
|
|
31
42
|
const FIRST_POLL_MS = 250;
|
|
32
43
|
const MAX_POLL_MS = 2_000;
|
|
33
|
-
/** The only endings whose `turn_ended` entry carries text (D168). */
|
|
34
44
|
const ENDINGS_WITH_TEXT = new Set(["provider_refused", "failed"]);
|
|
35
|
-
/** An opaque cursor: after the row at this position and ordinal. */
|
|
36
45
|
export function encodeCursor(position, ordinal) {
|
|
37
46
|
return Buffer.from(`1:${position}:${ordinal}`).toString("base64url");
|
|
38
47
|
}
|
|
@@ -50,13 +59,15 @@ function camel(name) {
|
|
|
50
59
|
function camelCase(row) {
|
|
51
60
|
return Object.fromEntries(Object.entries(row).map(([name, value]) => [camel(name), value]));
|
|
52
61
|
}
|
|
53
|
-
function
|
|
54
|
-
|
|
62
|
+
function contentOf(payload) {
|
|
63
|
+
const value = payload.content;
|
|
64
|
+
if (typeof value !== "object" || value === null || !("type" in value))
|
|
65
|
+
return undefined;
|
|
66
|
+
return value.type === "text" || value.type === "parts" ? value : undefined;
|
|
55
67
|
}
|
|
56
|
-
/** Builds an entry from a row, without its text. Returns the content reference when text is to be read. */
|
|
57
68
|
function entryOf(row) {
|
|
58
69
|
const [kind, payload] = Object.entries(row.value)[0];
|
|
59
|
-
const reference =
|
|
70
|
+
const reference = contentOf(payload);
|
|
60
71
|
const wanted = kind !== "turn_ended" || ENDINGS_WITH_TEXT.has(String(payload.ending));
|
|
61
72
|
const entry = {
|
|
62
73
|
kind,
|
|
@@ -66,7 +77,7 @@ function entryOf(row) {
|
|
|
66
77
|
at: row.at,
|
|
67
78
|
...camelCase(payload),
|
|
68
79
|
...(reference && wanted ? { contentBytes: reference.bytes } : {}),
|
|
69
|
-
};
|
|
80
|
+
};
|
|
70
81
|
return { entry, reference: reference && wanted ? reference : undefined };
|
|
71
82
|
}
|
|
72
83
|
function activityBody(store, session, request) {
|
|
@@ -94,17 +105,21 @@ function contentBody(store, session, entry) {
|
|
|
94
105
|
ordinal: entry.ordinal,
|
|
95
106
|
});
|
|
96
107
|
}
|
|
97
|
-
/** The Host stores up to a little under 1 GiB of text in one entry. */
|
|
98
108
|
const MAX_CONTENT_BYTES = 1024 * 1024 * 1024;
|
|
99
|
-
/** The
|
|
100
|
-
export function readContentBytes(link, session, entry) {
|
|
101
|
-
|
|
109
|
+
/** The Host owns the rule: its text route refuses a parts value as `ContentIsParts`. */
|
|
110
|
+
export async function readContentBytes(link, session, entry) {
|
|
111
|
+
try {
|
|
112
|
+
return await link.exchange("/v1/activity-content", (store) => contentBody(store, session, entry), undefined, { max: MAX_CONTENT_BYTES });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error instanceof RuiRequestError && error.code === "ContentIsParts")
|
|
116
|
+
throw new ContentIsPartsError();
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
102
119
|
}
|
|
103
|
-
/** The whole text of an entry as a string. A text past the JavaScript string limit (about 512 MiB) throws; use `readContentBytes`. */
|
|
104
120
|
export async function readContent(link, session, entry) {
|
|
105
121
|
return (await readContentBytes(link, session, entry)).toString("utf8");
|
|
106
122
|
}
|
|
107
|
-
/** Waits `ms`, or less when `signal` aborts; with no `ms`, waits only for the abort. */
|
|
108
123
|
function pause(ms, signal) {
|
|
109
124
|
return new Promise((resolve) => {
|
|
110
125
|
if (signal?.aborted)
|
|
@@ -118,8 +133,7 @@ function pause(ms, signal) {
|
|
|
118
133
|
signal?.addEventListener("abort", done, { once: true });
|
|
119
134
|
});
|
|
120
135
|
}
|
|
121
|
-
export async function* listActivity(link, session, options = {}) {
|
|
122
|
-
// The Host wants kinds in its own order, each once.
|
|
136
|
+
export async function* listActivity(link, session, options = {}, observe) {
|
|
123
137
|
const kinds = options.kinds === undefined ? FEED_KINDS : FEED_KINDS.filter((kind) => options.kinds?.includes(kind));
|
|
124
138
|
let position = "0";
|
|
125
139
|
let ordinal = null;
|
|
@@ -136,7 +150,6 @@ export async function* listActivity(link, session, options = {}) {
|
|
|
136
150
|
const watcher = options.follow ? link.activity?.watch(session) : undefined;
|
|
137
151
|
try {
|
|
138
152
|
while (!signal?.aborted) {
|
|
139
|
-
// Capture before I/O so a write racing an idle reply cannot be lost.
|
|
140
153
|
const revision = watcher?.revision ?? 0;
|
|
141
154
|
const page = JSON.parse((await link.exchange("/v1/activity-page", (store) => activityBody(store, session, { end: pinnedEnd, position, ordinal, kinds, since }))).toString("utf8"));
|
|
142
155
|
let receivedItems = false;
|
|
@@ -144,13 +157,32 @@ export async function* listActivity(link, session, options = {}) {
|
|
|
144
157
|
since = page.since;
|
|
145
158
|
}
|
|
146
159
|
else {
|
|
160
|
+
const served = servedWork(page.work);
|
|
161
|
+
observe?.(served.interruptTarget);
|
|
147
162
|
for (const row of page.items) {
|
|
148
|
-
// Cancellation is honoured before each entry, and again after any read that took time.
|
|
149
163
|
if (signal?.aborted)
|
|
150
164
|
return;
|
|
151
165
|
const { entry, reference } = entryOf(row);
|
|
152
166
|
if (reference && "content" in entry) {
|
|
153
|
-
if (
|
|
167
|
+
if (reference.type === "parts") {
|
|
168
|
+
entry.parts = await listParts(link, session, reference.owner);
|
|
169
|
+
// One entry inlines at most INLINE_TEXT_BYTES across its Text parts, as a text entry does.
|
|
170
|
+
let inline = INLINE_TEXT_BYTES;
|
|
171
|
+
for (const part of entry.parts) {
|
|
172
|
+
if (part.type !== "text")
|
|
173
|
+
continue;
|
|
174
|
+
if (signal?.aborted)
|
|
175
|
+
return;
|
|
176
|
+
const bytes = Number(part.bytes);
|
|
177
|
+
if (bytes > inline) {
|
|
178
|
+
part.truncated = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
inline -= bytes;
|
|
182
|
+
part.text = (await readPartBytes(link, session, reference.owner, part.ordinal, { prefix: INLINE_TEXT_BYTES })).toString("utf8");
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (Number(reference.bytes) > INLINE_TEXT_BYTES) {
|
|
154
186
|
entry.truncated = true;
|
|
155
187
|
}
|
|
156
188
|
else {
|
|
@@ -165,9 +197,7 @@ export async function* listActivity(link, session, options = {}) {
|
|
|
165
197
|
ordinal = row.ordinal;
|
|
166
198
|
}
|
|
167
199
|
if (page.more) {
|
|
168
|
-
// A bounded read keeps the end the first page named; a following one asks again.
|
|
169
200
|
pinnedEnd = options.follow ? null : page.end;
|
|
170
|
-
// A Session token cannot stand in for rows not yet consumed.
|
|
171
201
|
since = null;
|
|
172
202
|
wait = FIRST_POLL_MS;
|
|
173
203
|
continue;
|
|
@@ -175,8 +205,6 @@ export async function* listActivity(link, session, options = {}) {
|
|
|
175
205
|
if (!options.follow)
|
|
176
206
|
return;
|
|
177
207
|
pinnedEnd = null;
|
|
178
|
-
// Exhausted: everything the filter matches up to the end has been read, so the next poll starts at the end
|
|
179
|
-
// rather than rescanning the rows between the last match and it.
|
|
180
208
|
position = page.end;
|
|
181
209
|
ordinal = null;
|
|
182
210
|
since = page.since;
|
|
@@ -186,7 +214,6 @@ export async function* listActivity(link, session, options = {}) {
|
|
|
186
214
|
if (receivedItems)
|
|
187
215
|
wait = FIRST_POLL_MS;
|
|
188
216
|
else if (idle) {
|
|
189
|
-
// A bare internal Link has no local writes to observe; cancellation still ends its idle wait.
|
|
190
217
|
await (watcher ? watcher.wait(revision, signal) : pause(undefined, signal));
|
|
191
218
|
wait = FIRST_POLL_MS;
|
|
192
219
|
}
|
|
@@ -203,7 +230,6 @@ export async function* listActivity(link, session, options = {}) {
|
|
|
203
230
|
function dormant(kind) {
|
|
204
231
|
return kind === "idle" || kind === "finished" || kind === "failed" || kind === "stopped";
|
|
205
232
|
}
|
|
206
|
-
/** Search public feed text newest first. Empty budget-limited pages still advance. */
|
|
207
233
|
export async function* searchActivity(link, session, query, options = {}) {
|
|
208
234
|
const bytes = Buffer.byteLength(query, "utf8");
|
|
209
235
|
if (bytes === 0 || bytes > 256 || Buffer.from(query, "utf8").toString("utf8") !== query)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { Rui, type HostSettings, type ClientOptions, type ExitEvent } from "./client.ts";
|
|
2
|
-
export { RuiRequestError, RuiStartError, RuiUnavailableError } from "./errors.ts";
|
|
3
|
-
export { Session, Sessions, type AgentSpec, type ActionStatus, type ListSessionsOptions, type SessionArchiveState, type SessionArchiveResult, type SessionPinResult, type SessionCreate, type ModelSummary, type SendOptions, type SessionSpec, type SessionSummary, type SessionStatus, type SessionUpdate, type WriteOptions, } from "./sessions.ts";
|
|
2
|
+
export { ContentIsPartsError, RuiRequestError, RuiStartError, RuiUnavailableError } from "./errors.ts";
|
|
3
|
+
export { Session, Sessions, type AgentSpec, type ActionStatus, type ActionSelection, type ListSessionsOptions, type SessionArchiveState, type SessionArchiveResult, type SessionPinResult, type SessionCreate, type ModelSummary, type SendOptions, type SessionSpec, type SessionSummary, type SessionStatus, type InterruptTarget, type SessionUpdate, type WriteOptions, } from "./sessions.ts";
|
|
4
4
|
export { FEED_KINDS, INLINE_TEXT_BYTES, type FeedEntry, type FeedKind, type ListActivityOptions, type SearchActivityOptions, type ActivitySearchEntry } from "./feed.ts";
|
|
5
|
+
export type { ContentValue, PartOwner, Part } from "./parts.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
// The package's public surface. Transport types stay internal (D19).
|
|
2
1
|
export { Rui } from "./client.js";
|
|
3
|
-
export { RuiRequestError, RuiStartError, RuiUnavailableError } from "./errors.js";
|
|
2
|
+
export { ContentIsPartsError, RuiRequestError, RuiStartError, RuiUnavailableError } from "./errors.js";
|
|
4
3
|
export { Session, Sessions, } from "./sessions.js";
|
|
5
4
|
export { FEED_KINDS, INLINE_TEXT_BYTES } from "./feed.js";
|
package/dist/notifications.d.ts
CHANGED
package/dist/notifications.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// Owned by one Rui instance. Only active followers occupy the per-Session map.
|
|
2
1
|
export class SessionNotifications {
|
|
3
2
|
#sessions = new Map();
|
|
4
3
|
watch(session) {
|
|
@@ -12,7 +11,6 @@ export class SessionNotifications {
|
|
|
12
11
|
watches.add(watch);
|
|
13
12
|
return watch;
|
|
14
13
|
}
|
|
15
|
-
/** Rejects and releases every active follower, including one still reading its page. */
|
|
16
14
|
fail(error) {
|
|
17
15
|
for (const watches of this.#sessions.values()) {
|
|
18
16
|
for (const watch of watches)
|
package/dist/parts.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Link } from "./sessions.ts";
|
|
2
|
+
import type { components } from "./wire.ts";
|
|
3
|
+
type Schemas = components["schemas"];
|
|
4
|
+
export type ContentValue = Schemas["ContentValue"];
|
|
5
|
+
export type PartOwner = Schemas["PartOwner"];
|
|
6
|
+
export type Part = {
|
|
7
|
+
ordinal: number;
|
|
8
|
+
type: "text";
|
|
9
|
+
bytes: string;
|
|
10
|
+
sha256: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
truncated?: boolean;
|
|
13
|
+
} | {
|
|
14
|
+
ordinal: number;
|
|
15
|
+
type: "image";
|
|
16
|
+
mime: string;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
name: string | null;
|
|
20
|
+
bytes: string;
|
|
21
|
+
sha256: string;
|
|
22
|
+
};
|
|
23
|
+
export declare function listParts(link: Link, session: string, owner: PartOwner): Promise<Part[]>;
|
|
24
|
+
export declare function readPartBytes(link: Link, session: string, owner: PartOwner, ordinal: number, options?: {
|
|
25
|
+
prefix?: number;
|
|
26
|
+
}): Promise<Buffer>;
|
|
27
|
+
export {};
|