ruicode 0.88.1 → 0.93.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 +19 -42
- package/dist/binary.d.ts +2 -0
- package/dist/binary.js +15 -5
- package/dist/client.d.ts +16 -4
- package/dist/client.js +248 -20
- package/dist/errors.d.ts +29 -2
- package/dist/errors.js +31 -3
- package/dist/feed.d.ts +21 -9
- package/dist/feed.js +82 -99
- package/dist/follow.d.ts +84 -0
- package/dist/follow.js +237 -0
- package/dist/host-environment.d.ts +2 -0
- package/dist/host-environment.js +3 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/line.d.ts +45 -0
- package/dist/line.js +132 -0
- package/dist/rejections.d.ts +1 -0
- package/dist/rejections.js +52 -0
- package/dist/sessions.d.ts +30 -6
- package/dist/sessions.js +86 -15
- package/dist/transport.d.ts +2 -1
- package/dist/transport.js +2 -2
- package/dist/wire.d.ts +285 -121
- package/package.json +8 -4
- package/dist/notifications.d.ts +0 -16
- package/dist/notifications.js +0 -67
package/dist/errors.js
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
import { REJECTION_SENTENCES } from "./rejections.js";
|
|
2
|
+
/**
|
|
3
|
+
* Rui rejected one request. `code` is the Host's rejection code. The message is the Host's advisory detail when it sent
|
|
4
|
+
* one, else the Host catalog's plain sentence for `code`, with any missing settings and the code.
|
|
5
|
+
*/
|
|
2
6
|
export class RuiRequestError extends Error {
|
|
3
7
|
name = "RuiRequestError";
|
|
4
8
|
code;
|
|
5
9
|
/** The Host's error class, such as `invocation_error` or `busy`. */
|
|
6
10
|
type;
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
/** Every setting a new Session lacked, set exactly when `code` is `missing_settings`. */
|
|
12
|
+
missing;
|
|
13
|
+
constructor(type, code, message, missing) {
|
|
14
|
+
const sentence = REJECTION_SENTENCES[code] ?? "Rui rejected the request.";
|
|
15
|
+
super(message ?? `${sentence}${missing ? ` Missing: ${missing.join(", ")}.` : ""} [${code}]`);
|
|
9
16
|
this.type = type;
|
|
10
17
|
this.code = code;
|
|
18
|
+
this.missing = missing ?? undefined;
|
|
11
19
|
}
|
|
12
20
|
}
|
|
13
21
|
/** Rui refused to open. `code` is one of the refusal codes at open (D136). */
|
|
@@ -26,8 +34,28 @@ export class RuiUnavailableError extends Error {
|
|
|
26
34
|
super(message, options);
|
|
27
35
|
}
|
|
28
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* The Store was fenced under this client: the Host answered `canonical_store_failure`, or a MySQL Host wrote `store_fenced`
|
|
39
|
+
* before it exited. The client latches it once, so every pending and later call rejects with this same error, and it does
|
|
40
|
+
* not restart the Host. Close the client and connect again to use a new Host.
|
|
41
|
+
*/
|
|
42
|
+
export class RuiStoreFailedError extends Error {
|
|
43
|
+
name = "RuiStoreFailedError";
|
|
44
|
+
code = "store_failed";
|
|
45
|
+
constructor(options) {
|
|
46
|
+
super("The Rui Store failed, so this client rejects every call. Close it and connect again.", options);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
29
49
|
/** The entry contains parts; read an individual part instead. */
|
|
30
50
|
export class ContentIsPartsError extends Error {
|
|
31
51
|
name = "ContentIsPartsError";
|
|
32
52
|
constructor() { super("Content contains parts; use session.readPart(entry, ordinal)"); }
|
|
33
53
|
}
|
|
54
|
+
/** A wait ended before its condition held. `code` is `wait_timed_out`, the same code the CLI's wait reports. */
|
|
55
|
+
export class RuiWaitTimeoutError extends Error {
|
|
56
|
+
name = "RuiWaitTimeoutError";
|
|
57
|
+
code = "wait_timed_out";
|
|
58
|
+
constructor(message) {
|
|
59
|
+
super(message);
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/feed.d.ts
CHANGED
|
@@ -9,22 +9,19 @@ export declare const FEED_KINDS: readonly ["admission", "user", "assistant", "ca
|
|
|
9
9
|
export type FeedKind = (typeof FEED_KINDS)[number];
|
|
10
10
|
/** Entry text is inline up to this many bytes (D168). */
|
|
11
11
|
export declare const INLINE_TEXT_BYTES: number;
|
|
12
|
-
|
|
12
|
+
/** Options for following a Session's feed on the client's line. */
|
|
13
|
+
export interface WatchOptions {
|
|
13
14
|
/** The `cursor` of the last entry read. Left out, the feed starts at its beginning. */
|
|
14
15
|
after?: string;
|
|
15
|
-
/**
|
|
16
|
-
* Follows new entries, polling at 250 ms rising to 2 s only while busy.
|
|
17
|
-
* An empty idle/finished/failed/stopped page with nothing queued sleeps without a timer
|
|
18
|
-
* until send/update/decide/interrupt/stop succeeds through any handle for this Session
|
|
19
|
-
* on this Rui instance. Writes from another process to the same Store are not noticed
|
|
20
|
-
* while asleep. `signal` abort ends the iterator even while asleep.
|
|
21
|
-
*/
|
|
22
|
-
follow?: boolean;
|
|
23
16
|
/** Only these kinds, by their wire names. Left out, every kind. */
|
|
24
17
|
kinds?: readonly FeedKind[];
|
|
25
18
|
/** Ends the iterator, even while it waits for new entries: a pending `next()` then resolves as done. */
|
|
26
19
|
signal?: AbortSignal;
|
|
27
20
|
}
|
|
21
|
+
export interface ListActivityOptions extends WatchOptions {
|
|
22
|
+
/** Follows new entries through `watch`, so the wait ends on the Host's nudges, not on a timer. */
|
|
23
|
+
follow?: boolean;
|
|
24
|
+
}
|
|
28
25
|
type CamelKey<Key extends string> = Key extends `${infer Head}_${infer Tail}` ? `${Head}${Capitalize<CamelKey<Tail>>}` : Key;
|
|
29
26
|
type CamelRow<Payload> = {
|
|
30
27
|
[Key in keyof Payload as Key extends "session" ? "sessionId" : Key extends string ? CamelKey<Key> : Key]: Payload[Key];
|
|
@@ -65,6 +62,21 @@ export declare function readContent(link: Link, session: string, entry: {
|
|
|
65
62
|
position: string;
|
|
66
63
|
ordinal: string;
|
|
67
64
|
}): Promise<string>;
|
|
65
|
+
/** A page request: the next row's cursor, the `since` token of the last full reply, and the kinds. `end` pins a bounded traversal. */
|
|
66
|
+
export interface PageRequest {
|
|
67
|
+
end: string | null;
|
|
68
|
+
position: string;
|
|
69
|
+
ordinal: string | null;
|
|
70
|
+
since: string | null;
|
|
71
|
+
kinds: readonly FeedKind[];
|
|
72
|
+
}
|
|
73
|
+
export declare function readPage(link: Link, session: string, request: PageRequest): Promise<Schemas["ActivityPageReply"]>;
|
|
74
|
+
/**
|
|
75
|
+
* Follows a Session's feed on the client's one line: yields each new row once, in order. Each nudge for the Session reads
|
|
76
|
+
* from this iterator's cursor (`Follows`); an idle Session makes no reads until the Host nudges it.
|
|
77
|
+
*/
|
|
78
|
+
export declare function watchActivity(link: Link, session: string, options?: WatchOptions, observe?: (target: InterruptTarget | null) => void): AsyncGenerator<FeedEntry, void, undefined>;
|
|
79
|
+
/** The Session's feed from `after` to its current end, or with `follow`, `watchActivity`. */
|
|
68
80
|
export declare function listActivity(link: Link, session: string, options?: ListActivityOptions, observe?: (target: InterruptTarget | null) => void): AsyncGenerator<FeedEntry, void, undefined>;
|
|
69
81
|
export interface SearchActivityOptions {
|
|
70
82
|
/** Resume before this search entry's cursor. */
|
package/dist/feed.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ContentIsPartsError, RuiRequestError } from "./errors.js";
|
|
1
|
+
import { ContentIsPartsError, RuiRequestError, RuiUnavailableError } from "./errors.js";
|
|
2
2
|
import { listParts, readPartBytes } from "./parts.js";
|
|
3
3
|
import { WIRE_VERSION } from "./transport.js";
|
|
4
4
|
export function servedWork(work) {
|
|
@@ -39,9 +39,7 @@ void complete;
|
|
|
39
39
|
/** Entry text is inline up to this many bytes (D168). */
|
|
40
40
|
export const INLINE_TEXT_BYTES = 16 * 1024;
|
|
41
41
|
const PAGE_ROWS = 16;
|
|
42
|
-
const
|
|
43
|
-
const MAX_POLL_MS = 2_000;
|
|
44
|
-
const ENDINGS_WITH_TEXT = new Set(["provider_refused", "failed"]);
|
|
42
|
+
const ENDINGS_WITH_TEXT = new Set(["provider_refused", "out_of_quota", "failed"]);
|
|
45
43
|
export function encodeCursor(position, ordinal) {
|
|
46
44
|
return Buffer.from(`1:${position}:${ordinal}`).toString("base64url");
|
|
47
45
|
}
|
|
@@ -120,115 +118,100 @@ export async function readContentBytes(link, session, entry) {
|
|
|
120
118
|
export async function readContent(link, session, entry) {
|
|
121
119
|
return (await readContentBytes(link, session, entry)).toString("utf8");
|
|
122
120
|
}
|
|
123
|
-
function
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return resolve();
|
|
127
|
-
const done = () => {
|
|
128
|
-
clearTimeout(timer);
|
|
129
|
-
signal?.removeEventListener("abort", done);
|
|
130
|
-
resolve();
|
|
131
|
-
};
|
|
132
|
-
const timer = ms === undefined ? undefined : setTimeout(done, ms);
|
|
133
|
-
signal?.addEventListener("abort", done, { once: true });
|
|
134
|
-
});
|
|
121
|
+
export async function readPage(link, session, request) {
|
|
122
|
+
const reply = await link.exchange("/v1/activity-page", (store) => activityBody(store, session, request));
|
|
123
|
+
return JSON.parse(reply.toString("utf8"));
|
|
135
124
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (page.delta === "not_modified") {
|
|
157
|
-
since = page.since;
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
const served = servedWork(page.work);
|
|
161
|
-
observe?.(served.interruptTarget);
|
|
162
|
-
for (const row of page.items) {
|
|
125
|
+
function kindsOf(kinds) {
|
|
126
|
+
return kinds === undefined ? FEED_KINDS : FEED_KINDS.filter((kind) => kinds.includes(kind));
|
|
127
|
+
}
|
|
128
|
+
function startOf(after) {
|
|
129
|
+
return after === undefined ? { position: "0", ordinal: null } : decodeCursor(after);
|
|
130
|
+
}
|
|
131
|
+
/** One page's rows as entries, with their inline content read in order. Stops at an abort. */
|
|
132
|
+
async function* entriesOf(link, session, items, signal) {
|
|
133
|
+
for (const row of items) {
|
|
134
|
+
if (signal?.aborted)
|
|
135
|
+
return;
|
|
136
|
+
const { entry, reference } = entryOf(row);
|
|
137
|
+
if (reference && "content" in entry) {
|
|
138
|
+
if (reference.type === "parts") {
|
|
139
|
+
entry.parts = await listParts(link, session, reference.owner);
|
|
140
|
+
// One entry inlines at most INLINE_TEXT_BYTES across its Text parts, as a text entry does.
|
|
141
|
+
let inline = INLINE_TEXT_BYTES;
|
|
142
|
+
for (const part of entry.parts) {
|
|
143
|
+
if (part.type !== "text")
|
|
144
|
+
continue;
|
|
163
145
|
if (signal?.aborted)
|
|
164
146
|
return;
|
|
165
|
-
const
|
|
166
|
-
if (
|
|
167
|
-
|
|
168
|
-
|
|
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) {
|
|
186
|
-
entry.truncated = true;
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
const prefix = await link.exchange("/v1/activity-content", (store) => contentBody(store, session, row), undefined, { prefix: INLINE_TEXT_BYTES });
|
|
190
|
-
entry.text = prefix.toString("utf8");
|
|
191
|
-
}
|
|
147
|
+
const bytes = Number(part.bytes);
|
|
148
|
+
if (bytes > inline) {
|
|
149
|
+
part.truncated = true;
|
|
150
|
+
continue;
|
|
192
151
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
yield entry;
|
|
196
|
-
position = row.position;
|
|
197
|
-
ordinal = row.ordinal;
|
|
152
|
+
inline -= bytes;
|
|
153
|
+
part.text = (await readPartBytes(link, session, reference.owner, part.ordinal, { prefix: INLINE_TEXT_BYTES })).toString("utf8");
|
|
198
154
|
}
|
|
199
|
-
if (page.more) {
|
|
200
|
-
pinnedEnd = options.follow ? null : page.end;
|
|
201
|
-
since = null;
|
|
202
|
-
wait = FIRST_POLL_MS;
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
if (!options.follow)
|
|
206
|
-
return;
|
|
207
|
-
pinnedEnd = null;
|
|
208
|
-
position = page.end;
|
|
209
|
-
ordinal = null;
|
|
210
|
-
since = page.since;
|
|
211
|
-
idle = page.queued.count === "0" && dormant(page.work.state.kind);
|
|
212
|
-
receivedItems = page.items.length > 0;
|
|
213
155
|
}
|
|
214
|
-
if (
|
|
215
|
-
|
|
216
|
-
else if (idle) {
|
|
217
|
-
await (watcher ? watcher.wait(revision, signal) : pause(undefined, signal));
|
|
218
|
-
wait = FIRST_POLL_MS;
|
|
156
|
+
else if (Number(reference.bytes) > INLINE_TEXT_BYTES) {
|
|
157
|
+
entry.truncated = true;
|
|
219
158
|
}
|
|
220
159
|
else {
|
|
221
|
-
await
|
|
222
|
-
|
|
160
|
+
const prefix = await link.exchange("/v1/activity-content", (store) => contentBody(store, session, row), undefined, { prefix: INLINE_TEXT_BYTES });
|
|
161
|
+
entry.text = prefix.toString("utf8");
|
|
223
162
|
}
|
|
224
163
|
}
|
|
164
|
+
if (signal?.aborted)
|
|
165
|
+
return;
|
|
166
|
+
yield entry;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Follows a Session's feed on the client's one line: yields each new row once, in order. Each nudge for the Session reads
|
|
171
|
+
* from this iterator's cursor (`Follows`); an idle Session makes no reads until the Host nudges it.
|
|
172
|
+
*/
|
|
173
|
+
export async function* watchActivity(link, session, options = {}, observe) {
|
|
174
|
+
const follows = link.follows;
|
|
175
|
+
if (!follows)
|
|
176
|
+
throw new RuiUnavailableError("this client has no line to follow a Session on");
|
|
177
|
+
const watcher = follows.join(session, { ...startOf(options.after), since: null }, kindsOf(options.kinds));
|
|
178
|
+
try {
|
|
179
|
+
for (;;) {
|
|
180
|
+
const page = await watcher.take(options.signal);
|
|
181
|
+
if (page === undefined)
|
|
182
|
+
return;
|
|
183
|
+
observe?.(servedWork(page.work).interruptTarget);
|
|
184
|
+
yield* entriesOf(link, session, page.items, options.signal);
|
|
185
|
+
}
|
|
225
186
|
}
|
|
226
187
|
finally {
|
|
227
|
-
watcher
|
|
188
|
+
follows.leave(watcher);
|
|
228
189
|
}
|
|
229
190
|
}
|
|
230
|
-
|
|
231
|
-
|
|
191
|
+
/** The Session's feed from `after` to its current end, or with `follow`, `watchActivity`. */
|
|
192
|
+
export async function* listActivity(link, session, options = {}, observe) {
|
|
193
|
+
if (options.follow) {
|
|
194
|
+
yield* watchActivity(link, session, options, observe);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const kinds = kindsOf(options.kinds);
|
|
198
|
+
let { position, ordinal } = startOf(options.after);
|
|
199
|
+
let end = null;
|
|
200
|
+
while (!options.signal?.aborted) {
|
|
201
|
+
const page = await readPage(link, session, { end, position, ordinal, since: null, kinds });
|
|
202
|
+
if (page.delta !== "full")
|
|
203
|
+
throw new Error("the Host answered not_modified to a request with no token");
|
|
204
|
+
observe?.(servedWork(page.work).interruptTarget);
|
|
205
|
+
yield* entriesOf(link, session, page.items, options.signal);
|
|
206
|
+
if (!page.more)
|
|
207
|
+
return;
|
|
208
|
+
end = page.end;
|
|
209
|
+
const last = page.items.at(-1);
|
|
210
|
+
if (last) {
|
|
211
|
+
position = last.position;
|
|
212
|
+
ordinal = last.ordinal;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
232
215
|
}
|
|
233
216
|
export async function* searchActivity(link, session, query, options = {}) {
|
|
234
217
|
const bytes = Buffer.byteLength(query, "utf8");
|
package/dist/follow.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { FeedKind, PageRequest } from "./feed.ts";
|
|
2
|
+
import type { components } from "./wire.ts";
|
|
3
|
+
type Reply = components["schemas"]["ActivityPageReply"];
|
|
4
|
+
/** A page of rows; a `not_modified` reply carries none. */
|
|
5
|
+
export type Page = Extract<Reply, {
|
|
6
|
+
delta: "full";
|
|
7
|
+
}>;
|
|
8
|
+
/** The most Sessions one line may watch (docs/contracts/inspection.md#live-line). */
|
|
9
|
+
export declare const MAX_WATCHED_SESSIONS = 256;
|
|
10
|
+
/** Replaces the held line's watched set. An empty set closes the line. */
|
|
11
|
+
export interface LineControl {
|
|
12
|
+
watch(sessions: readonly string[]): void;
|
|
13
|
+
}
|
|
14
|
+
/** Reads one page of a Session's feed from a cursor. */
|
|
15
|
+
export type PageReader = (session: string, request: PageRequest) => Promise<Reply>;
|
|
16
|
+
export interface Cursor {
|
|
17
|
+
position: string;
|
|
18
|
+
ordinal: string | null;
|
|
19
|
+
since: string | null;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The one way this client follows a Session. Every watcher of the client shares one line. A nudge (`changed`, `resync`,
|
|
23
|
+
* a reconnect) makes each affected watcher read its own cursor once. The nudge carries no fact: rows come from feed reads.
|
|
24
|
+
*/
|
|
25
|
+
export declare class Follows {
|
|
26
|
+
#private;
|
|
27
|
+
constructor(read: PageReader, line: LineControl);
|
|
28
|
+
/**
|
|
29
|
+
* Registers a watcher at `cursor`. A Session's first watcher adds it to the line, and the Host answers a watch set with one
|
|
30
|
+
* nudge per listed Session, which reads it. A later watcher of a watched Session gets no frame, so it reads itself.
|
|
31
|
+
*/
|
|
32
|
+
join(session: string, cursor: Cursor, kinds: readonly FeedKind[]): Watcher;
|
|
33
|
+
/** Releases a watcher. The last watcher of a Session drops it from the line; the last one of the client closes the line. */
|
|
34
|
+
leave(watcher: Watcher): void;
|
|
35
|
+
/** A commit touched `session`, or the Host asked for a re-read: each of its watchers reads once. */
|
|
36
|
+
nudge(session: string): void;
|
|
37
|
+
/** The change ring no longer covers the line: every watched Session is re-read from its cursors. */
|
|
38
|
+
nudgeAll(): void;
|
|
39
|
+
/** A read of `follow` failed: its watchers end with `error`. A Session that is no longer registered only ends its own watchers. */
|
|
40
|
+
failFollow(follow: SessionFollow, error: Error): void;
|
|
41
|
+
/** The client can no longer follow: every watcher ends with `error`, and the line closes. */
|
|
42
|
+
fail(error: Error): void;
|
|
43
|
+
/** The client's page reader, for the read loop of each watched Session. */
|
|
44
|
+
get read(): PageReader;
|
|
45
|
+
}
|
|
46
|
+
/** The watchers of one Session. At most one feed read is in flight for it; watchers at the same cursor share that read. */
|
|
47
|
+
declare class SessionFollow {
|
|
48
|
+
#private;
|
|
49
|
+
readonly session: string;
|
|
50
|
+
readonly watchers: Set<Watcher>;
|
|
51
|
+
constructor(session: string, follows: Follows);
|
|
52
|
+
nudge(): void;
|
|
53
|
+
/** Starts the read loop unless one is running; a running loop re-checks for due watchers after each read. */
|
|
54
|
+
drive(): void;
|
|
55
|
+
failWatchers(error: Error): void;
|
|
56
|
+
}
|
|
57
|
+
/** One consumer's cursor and the pages it has not yet taken. The Session's read loop delivers pages; `take` waits for one. */
|
|
58
|
+
export declare class Watcher {
|
|
59
|
+
#private;
|
|
60
|
+
position: string;
|
|
61
|
+
ordinal: string | null;
|
|
62
|
+
since: string | null;
|
|
63
|
+
readonly kinds: readonly FeedKind[];
|
|
64
|
+
readonly follow: SessionFollow;
|
|
65
|
+
/** A read is owed from this cursor: set by a nudge, by a `more` page, and by joining. */
|
|
66
|
+
due: boolean;
|
|
67
|
+
constructor(cursor: Cursor, kinds: readonly FeedKind[], follow: SessionFollow);
|
|
68
|
+
get hasPages(): boolean;
|
|
69
|
+
get request(): PageRequest;
|
|
70
|
+
/** Watchers with equal keys read the same request. */
|
|
71
|
+
get key(): string;
|
|
72
|
+
isWaitingAt(key: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Applies a page read from this cursor. A `not_modified` reply moves only the token. `settled` is false when a nudge
|
|
75
|
+
* arrived after the read was issued: the rows are still this cursor's facts, but the watcher stays due for another read.
|
|
76
|
+
*/
|
|
77
|
+
deliver(page: Reply, settled: boolean): void;
|
|
78
|
+
fail(error: Error): void;
|
|
79
|
+
/** Waits for the next page, resolving `undefined` once `signal` aborts. Taking a page starts the next owed read. */
|
|
80
|
+
take(signal: AbortSignal | undefined): Promise<Page | undefined>;
|
|
81
|
+
/** A released watcher neither takes nor receives pages again. */
|
|
82
|
+
release(): void;
|
|
83
|
+
}
|
|
84
|
+
export {};
|
package/dist/follow.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/** The most Sessions one line may watch (docs/contracts/inspection.md#live-line). */
|
|
2
|
+
export const MAX_WATCHED_SESSIONS = 256;
|
|
3
|
+
/** A Session id in the Host's grammar: six characters of its Crockford alphabet. The Host closes a line whose watch set breaks it. */
|
|
4
|
+
const SESSION_ID = /^[0-9a-hjkmnp-tv-z]{6}$/;
|
|
5
|
+
/**
|
|
6
|
+
* The one way this client follows a Session. Every watcher of the client shares one line. A nudge (`changed`, `resync`,
|
|
7
|
+
* a reconnect) makes each affected watcher read its own cursor once. The nudge carries no fact: rows come from feed reads.
|
|
8
|
+
*/
|
|
9
|
+
export class Follows {
|
|
10
|
+
#read;
|
|
11
|
+
#line;
|
|
12
|
+
#sessions = new Map();
|
|
13
|
+
constructor(read, line) {
|
|
14
|
+
this.#read = read;
|
|
15
|
+
this.#line = line;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Registers a watcher at `cursor`. A Session's first watcher adds it to the line, and the Host answers a watch set with one
|
|
19
|
+
* nudge per listed Session, which reads it. A later watcher of a watched Session gets no frame, so it reads itself.
|
|
20
|
+
*/
|
|
21
|
+
join(session, cursor, kinds) {
|
|
22
|
+
if (!SESSION_ID.test(session))
|
|
23
|
+
throw new RangeError(`not a Session id: ${JSON.stringify(session)}`);
|
|
24
|
+
let follow = this.#sessions.get(session);
|
|
25
|
+
const added = follow === undefined;
|
|
26
|
+
if (!follow) {
|
|
27
|
+
if (this.#sessions.size >= MAX_WATCHED_SESSIONS)
|
|
28
|
+
throw new RangeError(`a client watches at most ${MAX_WATCHED_SESSIONS} Sessions`);
|
|
29
|
+
follow = new SessionFollow(session, this);
|
|
30
|
+
this.#sessions.set(session, follow);
|
|
31
|
+
}
|
|
32
|
+
const watcher = new Watcher(cursor, kinds, follow);
|
|
33
|
+
follow.watchers.add(watcher);
|
|
34
|
+
if (added)
|
|
35
|
+
this.#line.watch([...this.#sessions.keys()]);
|
|
36
|
+
else
|
|
37
|
+
follow.drive();
|
|
38
|
+
return watcher;
|
|
39
|
+
}
|
|
40
|
+
/** Releases a watcher. The last watcher of a Session drops it from the line; the last one of the client closes the line. */
|
|
41
|
+
leave(watcher) {
|
|
42
|
+
const follow = watcher.follow;
|
|
43
|
+
if (!follow.watchers.delete(watcher))
|
|
44
|
+
return;
|
|
45
|
+
watcher.release();
|
|
46
|
+
if (follow.watchers.size === 0 && this.#sessions.get(follow.session) === follow) {
|
|
47
|
+
this.#sessions.delete(follow.session);
|
|
48
|
+
this.#line.watch([...this.#sessions.keys()]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** A commit touched `session`, or the Host asked for a re-read: each of its watchers reads once. */
|
|
52
|
+
nudge(session) {
|
|
53
|
+
this.#sessions.get(session)?.nudge();
|
|
54
|
+
}
|
|
55
|
+
/** The change ring no longer covers the line: every watched Session is re-read from its cursors. */
|
|
56
|
+
nudgeAll() {
|
|
57
|
+
for (const follow of this.#sessions.values())
|
|
58
|
+
follow.nudge();
|
|
59
|
+
}
|
|
60
|
+
/** A read of `follow` failed: its watchers end with `error`. A Session that is no longer registered only ends its own watchers. */
|
|
61
|
+
failFollow(follow, error) {
|
|
62
|
+
if (this.#sessions.get(follow.session) === follow) {
|
|
63
|
+
this.#sessions.delete(follow.session);
|
|
64
|
+
this.#line.watch([...this.#sessions.keys()]);
|
|
65
|
+
}
|
|
66
|
+
follow.failWatchers(error);
|
|
67
|
+
}
|
|
68
|
+
/** The client can no longer follow: every watcher ends with `error`, and the line closes. */
|
|
69
|
+
fail(error) {
|
|
70
|
+
const all = [...this.#sessions.values()];
|
|
71
|
+
this.#sessions.clear();
|
|
72
|
+
for (const follow of all)
|
|
73
|
+
follow.failWatchers(error);
|
|
74
|
+
this.#line.watch([]);
|
|
75
|
+
}
|
|
76
|
+
/** The client's page reader, for the read loop of each watched Session. */
|
|
77
|
+
get read() {
|
|
78
|
+
return this.#read;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** The watchers of one Session. At most one feed read is in flight for it; watchers at the same cursor share that read. */
|
|
82
|
+
class SessionFollow {
|
|
83
|
+
session;
|
|
84
|
+
watchers = new Set();
|
|
85
|
+
#follows;
|
|
86
|
+
#reading = false;
|
|
87
|
+
/** Counts nudges. A read issued before the latest nudge may miss its commit, so its reply leaves the watchers due. */
|
|
88
|
+
#nudges = 0;
|
|
89
|
+
constructor(session, follows) {
|
|
90
|
+
this.session = session;
|
|
91
|
+
this.#follows = follows;
|
|
92
|
+
}
|
|
93
|
+
nudge() {
|
|
94
|
+
this.#nudges++;
|
|
95
|
+
for (const watcher of this.watchers)
|
|
96
|
+
watcher.due = true;
|
|
97
|
+
this.drive();
|
|
98
|
+
}
|
|
99
|
+
/** Starts the read loop unless one is running; a running loop re-checks for due watchers after each read. */
|
|
100
|
+
drive() {
|
|
101
|
+
if (this.#reading)
|
|
102
|
+
return;
|
|
103
|
+
this.#reading = true;
|
|
104
|
+
void this.#run();
|
|
105
|
+
}
|
|
106
|
+
async #run() {
|
|
107
|
+
try {
|
|
108
|
+
for (let next = this.#nextDue(); next !== undefined; next = this.#nextDue()) {
|
|
109
|
+
const key = next.key;
|
|
110
|
+
const stamp = this.#nudges;
|
|
111
|
+
const page = await this.#follows.read(this.session, next.request);
|
|
112
|
+
const settled = stamp === this.#nudges;
|
|
113
|
+
for (const watcher of this.watchers) {
|
|
114
|
+
if (watcher.due && watcher.isWaitingAt(key))
|
|
115
|
+
watcher.deliver(page, settled);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
this.#reading = false;
|
|
121
|
+
this.#follows.failFollow(this, error instanceof Error ? error : new Error(String(error)));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
// No await since the last check: a nudge either marked a watcher due before that check, or finds the loop cleared.
|
|
125
|
+
this.#reading = false;
|
|
126
|
+
}
|
|
127
|
+
#nextDue() {
|
|
128
|
+
for (const watcher of this.watchers)
|
|
129
|
+
if (watcher.due && !watcher.hasPages)
|
|
130
|
+
return watcher;
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
failWatchers(error) {
|
|
134
|
+
const watchers = [...this.watchers];
|
|
135
|
+
this.watchers.clear();
|
|
136
|
+
for (const watcher of watchers)
|
|
137
|
+
watcher.fail(error);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** One consumer's cursor and the pages it has not yet taken. The Session's read loop delivers pages; `take` waits for one. */
|
|
141
|
+
export class Watcher {
|
|
142
|
+
position;
|
|
143
|
+
ordinal;
|
|
144
|
+
since;
|
|
145
|
+
kinds;
|
|
146
|
+
follow;
|
|
147
|
+
/** A read is owed from this cursor: set by a nudge, by a `more` page, and by joining. */
|
|
148
|
+
due = true;
|
|
149
|
+
#pages = [];
|
|
150
|
+
#error;
|
|
151
|
+
#wake;
|
|
152
|
+
constructor(cursor, kinds, follow) {
|
|
153
|
+
this.position = cursor.position;
|
|
154
|
+
this.ordinal = cursor.ordinal;
|
|
155
|
+
this.since = cursor.since;
|
|
156
|
+
this.kinds = kinds;
|
|
157
|
+
this.follow = follow;
|
|
158
|
+
}
|
|
159
|
+
get hasPages() {
|
|
160
|
+
return this.#pages.length > 0;
|
|
161
|
+
}
|
|
162
|
+
get request() {
|
|
163
|
+
return { end: null, position: this.position, ordinal: this.ordinal, since: this.since, kinds: this.kinds };
|
|
164
|
+
}
|
|
165
|
+
/** Watchers with equal keys read the same request. */
|
|
166
|
+
get key() {
|
|
167
|
+
return JSON.stringify([this.position, this.ordinal, this.since, this.kinds]);
|
|
168
|
+
}
|
|
169
|
+
isWaitingAt(key) {
|
|
170
|
+
return this.key === key && !this.hasPages;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Applies a page read from this cursor. A `not_modified` reply moves only the token. `settled` is false when a nudge
|
|
174
|
+
* arrived after the read was issued: the rows are still this cursor's facts, but the watcher stays due for another read.
|
|
175
|
+
*/
|
|
176
|
+
deliver(page, settled) {
|
|
177
|
+
if (page.delta === "not_modified") {
|
|
178
|
+
this.since = page.since;
|
|
179
|
+
this.due = !settled;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.#pages.push(page);
|
|
183
|
+
const last = page.items.at(-1);
|
|
184
|
+
if (page.more && last) {
|
|
185
|
+
this.position = last.position;
|
|
186
|
+
this.ordinal = last.ordinal;
|
|
187
|
+
this.since = null;
|
|
188
|
+
this.due = true;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.position = page.end;
|
|
192
|
+
this.ordinal = null;
|
|
193
|
+
this.since = page.since;
|
|
194
|
+
this.due = !settled;
|
|
195
|
+
}
|
|
196
|
+
this.#notify();
|
|
197
|
+
}
|
|
198
|
+
fail(error) {
|
|
199
|
+
this.#error = error;
|
|
200
|
+
this.#notify();
|
|
201
|
+
}
|
|
202
|
+
/** Waits for the next page, resolving `undefined` once `signal` aborts. Taking a page starts the next owed read. */
|
|
203
|
+
async take(signal) {
|
|
204
|
+
for (;;) {
|
|
205
|
+
if (this.#error)
|
|
206
|
+
throw this.#error;
|
|
207
|
+
const page = this.#pages.shift();
|
|
208
|
+
if (page !== undefined) {
|
|
209
|
+
if (this.due)
|
|
210
|
+
this.follow.drive();
|
|
211
|
+
return page;
|
|
212
|
+
}
|
|
213
|
+
if (signal?.aborted)
|
|
214
|
+
return undefined;
|
|
215
|
+
await new Promise((resolve) => {
|
|
216
|
+
const wake = () => {
|
|
217
|
+
signal?.removeEventListener("abort", wake);
|
|
218
|
+
this.#wake = undefined;
|
|
219
|
+
resolve();
|
|
220
|
+
};
|
|
221
|
+
this.#wake = wake;
|
|
222
|
+
signal?.addEventListener("abort", wake, { once: true });
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/** A released watcher neither takes nor receives pages again. */
|
|
227
|
+
release() {
|
|
228
|
+
this.#pages.length = 0;
|
|
229
|
+
this.due = false;
|
|
230
|
+
this.#notify();
|
|
231
|
+
}
|
|
232
|
+
#notify() {
|
|
233
|
+
const wake = this.#wake;
|
|
234
|
+
this.#wake = undefined;
|
|
235
|
+
wake?.();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// Generated from src/tls_client/environment.zig by `zig build openapi`. Do not edit.
|
|
2
|
+
/** The environment variables a Host's transport reads: its https proxy, no-proxy list and OpenSSL trust store. */
|
|
3
|
+
export const TRANSPORT_ENVIRONMENT = ["https_proxy", "HTTPS_PROXY", "no_proxy", "NO_PROXY", "SSL_CERT_FILE", "SSL_CERT_DIR"];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Rui, type HostSettings, type ClientOptions, type ExitEvent } from "./client.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
|
-
export { FEED_KINDS, INLINE_TEXT_BYTES, type FeedEntry, type FeedKind, type ListActivityOptions, type SearchActivityOptions, type ActivitySearchEntry } from "./feed.ts";
|
|
2
|
+
export { ContentIsPartsError, RuiRequestError, RuiStartError, RuiStoreFailedError, RuiUnavailableError, RuiWaitTimeoutError, type MissingSetting } 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, type ToolReloadOutcome, } from "./sessions.ts";
|
|
4
|
+
export { FEED_KINDS, INLINE_TEXT_BYTES, type FeedEntry, type FeedKind, type ListActivityOptions, type WatchOptions, type SearchActivityOptions, type ActivitySearchEntry } from "./feed.ts";
|
|
5
5
|
export type { ContentValue, PartOwner, Part } from "./parts.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Rui } from "./client.js";
|
|
2
|
-
export { ContentIsPartsError, RuiRequestError, RuiStartError, RuiUnavailableError } from "./errors.js";
|
|
2
|
+
export { ContentIsPartsError, RuiRequestError, RuiStartError, RuiStoreFailedError, RuiUnavailableError, RuiWaitTimeoutError } from "./errors.js";
|
|
3
3
|
export { Session, Sessions, } from "./sessions.js";
|
|
4
4
|
export { FEED_KINDS, INLINE_TEXT_BYTES } from "./feed.js";
|