@g9tm/aiassistant 1.13.0 → 1.14.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/dist/api.d.ts +6 -1
- package/dist/api.js +11 -6
- package/dist/assistant.d.ts +39 -3
- package/dist/assistant.js +100 -10
- package/dist/conversation.d.ts +8 -0
- package/dist/conversation.js +17 -0
- package/dist/transport.d.ts +14 -5
- package/dist/transport.js +33 -11
- package/package.json +2 -2
package/dist/api.d.ts
CHANGED
|
@@ -53,13 +53,18 @@ export declare class AssistantApi {
|
|
|
53
53
|
private readonly host;
|
|
54
54
|
/** The time one request may take (a long poll waits up to 25 s on the server). */
|
|
55
55
|
timeoutMs: number;
|
|
56
|
+
/**
|
|
57
|
+
* The time a short call may take: a send, a cancel, a device tool's answer. A send hanging on a dead network (a phone's
|
|
58
|
+
* DNS) held the question for the whole minute (a host, 2026-12-07). A long poll takes its wait plus 15 s.
|
|
59
|
+
*/
|
|
60
|
+
shortTimeoutMs: number;
|
|
56
61
|
private readonly base;
|
|
57
62
|
/** `base` is where the API is mounted; a missing trailing slash is added ("/api/x" called "/api/xstate"). */
|
|
58
63
|
constructor(host: AssistantHost, base?: string);
|
|
59
64
|
request(method: NonNullable<HostRequest["method"]>, path: string, body?: unknown, raw?: {
|
|
60
65
|
bytes: Uint8Array;
|
|
61
66
|
type: string;
|
|
62
|
-
}): Promise<unknown>;
|
|
67
|
+
}, timeoutMs?: number): Promise<unknown>;
|
|
63
68
|
send(input: SendInput): Promise<{
|
|
64
69
|
conversationId: string;
|
|
65
70
|
runId: string;
|
package/dist/api.js
CHANGED
|
@@ -35,9 +35,14 @@ export class AssistantApi {
|
|
|
35
35
|
this.host = host;
|
|
36
36
|
/** The time one request may take (a long poll waits up to 25 s on the server). */
|
|
37
37
|
this.timeoutMs = 60000;
|
|
38
|
+
/**
|
|
39
|
+
* The time a short call may take: a send, a cancel, a device tool's answer. A send hanging on a dead network (a phone's
|
|
40
|
+
* DNS) held the question for the whole minute (a host, 2026-12-07). A long poll takes its wait plus 15 s.
|
|
41
|
+
*/
|
|
42
|
+
this.shortTimeoutMs = 20000;
|
|
38
43
|
this.base = base.endsWith("/") ? base : base + "/";
|
|
39
44
|
}
|
|
40
|
-
async request(method, path, body, raw) {
|
|
45
|
+
async request(method, path, body, raw, timeoutMs = this.timeoutMs) {
|
|
41
46
|
const headers = { Accept: "application/json", "Accept-Language": this.host.locale };
|
|
42
47
|
let payload;
|
|
43
48
|
if (raw) {
|
|
@@ -51,7 +56,7 @@ export class AssistantApi {
|
|
|
51
56
|
const t = timer(this.host);
|
|
52
57
|
let handle;
|
|
53
58
|
const timeout = new Promise((_, reject) => {
|
|
54
|
-
handle = t.set(() => reject(new ApiError(0, "timeout", null)),
|
|
59
|
+
handle = t.set(() => reject(new ApiError(0, "timeout", null)), timeoutMs);
|
|
55
60
|
});
|
|
56
61
|
const init = payload === undefined ? { method, headers } : { method, headers, body: payload };
|
|
57
62
|
try {
|
|
@@ -84,10 +89,10 @@ export class AssistantApi {
|
|
|
84
89
|
body["refs"] = input.refs;
|
|
85
90
|
if (input.history)
|
|
86
91
|
body["history"] = input.history;
|
|
87
|
-
return this.request("POST", path, body).then((r) => ({ conversationId: str(obj(r)["conversationId"]), runId: str(obj(r)["runId"]) }));
|
|
92
|
+
return this.request("POST", path, body, undefined, this.shortTimeoutMs).then((r) => ({ conversationId: str(obj(r)["conversationId"]), runId: str(obj(r)["runId"]) }));
|
|
88
93
|
}
|
|
89
94
|
async events(runId, after, wait) {
|
|
90
|
-
const r = obj(await this.request("GET", "runs/" + encodeURIComponent(runId) + "/events?after=" + String(after) + "&wait=" + String(wait)));
|
|
95
|
+
const r = obj(await this.request("GET", "runs/" + encodeURIComponent(runId) + "/events?after=" + String(after) + "&wait=" + String(wait), undefined, undefined, wait * 1000 + 15000));
|
|
91
96
|
return { events: arr(r["events"]).map(toRawEvent), last: num(r["last"], after), finished: r["finished"] === true };
|
|
92
97
|
}
|
|
93
98
|
async ticket(runId) {
|
|
@@ -96,7 +101,7 @@ export class AssistantApi {
|
|
|
96
101
|
streamUrl(runId, after, ticket) {
|
|
97
102
|
return this.base + "runs/" + encodeURIComponent(runId) + "/events?after=" + String(after) + "&ticket=" + encodeURIComponent(ticket);
|
|
98
103
|
}
|
|
99
|
-
cancel(runId) { return this.request("POST", "runs/" + encodeURIComponent(runId) + "/cancel"); }
|
|
104
|
+
cancel(runId) { return this.request("POST", "runs/" + encodeURIComponent(runId) + "/cancel", undefined, undefined, this.shortTimeoutMs); }
|
|
100
105
|
confirm(planId, hash, code, skip) {
|
|
101
106
|
return this.request("POST", "plans/" + encodeURIComponent(planId) + "/confirm", { hash, code, skip });
|
|
102
107
|
}
|
|
@@ -108,7 +113,7 @@ export class AssistantApi {
|
|
|
108
113
|
lock() { return this.request("POST", "lock"); }
|
|
109
114
|
/** A device tool's answer to a `client.call` of a run. */
|
|
110
115
|
clientResult(runId, body) {
|
|
111
|
-
return this.request("POST", "runs/" + encodeURIComponent(runId) + "/client-results", body);
|
|
116
|
+
return this.request("POST", "runs/" + encodeURIComponent(runId) + "/client-results", body, undefined, this.shortTimeoutMs);
|
|
112
117
|
}
|
|
113
118
|
/** The person's flow pack (`unchanged` when `version` is current). */
|
|
114
119
|
flows(version) { return this.request("GET", "flows" + (version ? "?version=" + encodeURIComponent(version) : "")); }
|
package/dist/assistant.d.ts
CHANGED
|
@@ -40,6 +40,18 @@ export interface AssistantOptions {
|
|
|
40
40
|
* last six lines, and `open` takes the messages the host saved instead of asking the server.
|
|
41
41
|
*/
|
|
42
42
|
blind?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* A run whose polls fail this long in a row (offline, DNS, the server down) ends as `failed` with code `network`; a
|
|
45
|
+
* 404 on the run ends it at once with code `lost`. `resume()` follows it again. Default 45 s.
|
|
46
|
+
*/
|
|
47
|
+
lostAfterMs?: number;
|
|
48
|
+
/** The longest a run may work before it ends here as `failed` with code `timeout` (cancelled on the server, best effort). Default 0: no cap. */
|
|
49
|
+
maxRunMs?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Offline with no device flow, or a send the network lost: the question is kept `queued` (not the needs-a-connection
|
|
52
|
+
* line) and sent by `resume()`, which the host calls when the network comes back. Default off.
|
|
53
|
+
*/
|
|
54
|
+
queueOffline?: boolean;
|
|
43
55
|
}
|
|
44
56
|
/** How the conversation reads and writes; every option has a safe default for every host. */
|
|
45
57
|
export interface ChatOptions {
|
|
@@ -64,8 +76,8 @@ export interface ChatOptions {
|
|
|
64
76
|
*/
|
|
65
77
|
feedback?: boolean;
|
|
66
78
|
}
|
|
67
|
-
/** Where `ask` sent a question. */
|
|
68
|
-
export type AskedVia = "device" | "offline" | "server" | "refused";
|
|
79
|
+
/** Where `ask` sent a question (`queued`: kept for `resume()`, with `queueOffline`). */
|
|
80
|
+
export type AskedVia = "device" | "offline" | "server" | "refused" | "queued";
|
|
69
81
|
export interface AssistantSnapshot {
|
|
70
82
|
conversationId: string | null;
|
|
71
83
|
messages: MessageView[];
|
|
@@ -79,6 +91,9 @@ export interface AssistantSnapshot {
|
|
|
79
91
|
}[];
|
|
80
92
|
state: AssistantState;
|
|
81
93
|
error: string | null;
|
|
94
|
+
/** When the last question went out (ms), and when the server took it (its send answered); null before. */
|
|
95
|
+
sentAt: number | null;
|
|
96
|
+
acceptedAt: number | null;
|
|
82
97
|
}
|
|
83
98
|
export declare const ATTACH_ACCEPT: readonly string[];
|
|
84
99
|
export declare class Assistant {
|
|
@@ -110,6 +125,15 @@ export declare class Assistant {
|
|
|
110
125
|
/** The conversation's reading and writing options as given (`chatOf` in "@g9tm/aiassistant/pages" resolves the defaults). */
|
|
111
126
|
readonly chat: ChatOptions;
|
|
112
127
|
private readonly blind;
|
|
128
|
+
private readonly maxRunMs;
|
|
129
|
+
private readonly queueOffline;
|
|
130
|
+
/** Questions that did not go, by message id, with what they were sent with. */
|
|
131
|
+
private readonly unsent;
|
|
132
|
+
/** The run whose follow failed (network, lost): `resume()` follows it again. */
|
|
133
|
+
private lost;
|
|
134
|
+
private cap;
|
|
135
|
+
private sentAt;
|
|
136
|
+
private acceptedAt;
|
|
113
137
|
constructor(host: AssistantHost, options?: AssistantOptions);
|
|
114
138
|
/** Listens to every change; returns the function that stops listening. */
|
|
115
139
|
subscribe(listener: (s: AssistantSnapshot) => void): () => void;
|
|
@@ -123,6 +147,15 @@ export declare class Assistant {
|
|
|
123
147
|
fresh(): void;
|
|
124
148
|
/** Sends a question; refused while a run is working (the button is "stop" then). With `refs`, the things picked by `@`. */
|
|
125
149
|
send(text: string, page?: SendInput["page"], refs?: readonly PickedRef[]): Promise<boolean>;
|
|
150
|
+
/** Sends a failed or queued question again in its place (the last one when no id is given). */
|
|
151
|
+
retry(messageId?: string): Promise<boolean>;
|
|
152
|
+
/**
|
|
153
|
+
* The network is back (the host calls it on a network change): a run whose follow failed is followed again from its
|
|
154
|
+
* cursor, else the first queued question is sent.
|
|
155
|
+
*/
|
|
156
|
+
resume(): Promise<void>;
|
|
157
|
+
/** Ends the run here at once (stop, the cap, a lost follow): its question settles, nothing spins. */
|
|
158
|
+
private end;
|
|
126
159
|
/** Follows a run (null stops following). */
|
|
127
160
|
follow(runId: string | null, after?: number): void;
|
|
128
161
|
/**
|
|
@@ -134,7 +167,10 @@ export declare class Assistant {
|
|
|
134
167
|
syncDevice(): Promise<boolean>;
|
|
135
168
|
/** Runs a run's `client.call` on the device once and answers the server. */
|
|
136
169
|
private clientCall;
|
|
137
|
-
/**
|
|
170
|
+
/**
|
|
171
|
+
* Stops the run that is working: here at once (offline the cancel failed and the stop button did nothing), then on
|
|
172
|
+
* the server, best effort.
|
|
173
|
+
*/
|
|
138
174
|
stop(): Promise<void>;
|
|
139
175
|
private notePlan;
|
|
140
176
|
/** Ticks or unticks a step of a plan card. */
|
package/dist/assistant.js
CHANGED
|
@@ -33,9 +33,17 @@ export class Assistant {
|
|
|
33
33
|
this.error = null;
|
|
34
34
|
/** `client.call`s already answered (a replayed event never runs a tool twice). */
|
|
35
35
|
this.answeredCalls = new Set();
|
|
36
|
+
/** Questions that did not go, by message id, with what they were sent with. */
|
|
37
|
+
this.unsent = new Map();
|
|
38
|
+
/** The run whose follow failed (network, lost): `resume()` follows it again. */
|
|
39
|
+
this.lost = null;
|
|
40
|
+
this.sentAt = null;
|
|
41
|
+
this.acceptedAt = null;
|
|
36
42
|
/** Counts fresh(): a send that returns after one is dropped. */
|
|
37
43
|
this.generation = 0;
|
|
38
44
|
this.blind = options.blind === true;
|
|
45
|
+
this.maxRunMs = options.maxRunMs ?? 0;
|
|
46
|
+
this.queueOffline = options.queueOffline === true;
|
|
39
47
|
this.api = new AssistantApi(host, options.base);
|
|
40
48
|
this.texts = createTexts(host.locale, options.texts);
|
|
41
49
|
this.transport = pickTransport(this.api, host, options);
|
|
@@ -63,6 +71,8 @@ export class Assistant {
|
|
|
63
71
|
attachments: [...this.attachments],
|
|
64
72
|
state: this.stateNow,
|
|
65
73
|
error: this.error,
|
|
74
|
+
sentAt: this.sentAt,
|
|
75
|
+
acceptedAt: this.acceptedAt,
|
|
66
76
|
};
|
|
67
77
|
}
|
|
68
78
|
emit() {
|
|
@@ -73,6 +83,7 @@ export class Assistant {
|
|
|
73
83
|
/** Opens a conversation from its history (from the end; history never animates); blind: from `saved`, the host's copy. */
|
|
74
84
|
async open(conversationId, saved) {
|
|
75
85
|
this.follow(null);
|
|
86
|
+
this.unsent.clear();
|
|
76
87
|
this.store.reset(conversationId);
|
|
77
88
|
const r = this.blind ? saved ?? [] : await this.api.messages(conversationId);
|
|
78
89
|
const raw = Array.isArray(r) ? r : Array.isArray(r.messages) ? r.messages : [];
|
|
@@ -94,6 +105,8 @@ export class Assistant {
|
|
|
94
105
|
if (working)
|
|
95
106
|
void this.api.cancel(working).catch(() => undefined);
|
|
96
107
|
this.store.reset(null);
|
|
108
|
+
this.unsent.clear();
|
|
109
|
+
this.sentAt = this.acceptedAt = null;
|
|
97
110
|
this.plans.clear();
|
|
98
111
|
this.attachments = [];
|
|
99
112
|
this.emit();
|
|
@@ -109,7 +122,10 @@ export class Assistant {
|
|
|
109
122
|
return false;
|
|
110
123
|
}
|
|
111
124
|
this.error = null;
|
|
112
|
-
|
|
125
|
+
const t0 = timer(this.host).now();
|
|
126
|
+
this.sentAt = t0;
|
|
127
|
+
this.acceptedAt = null;
|
|
128
|
+
this.store.optimistic(trimmed, new Date(t0).toISOString());
|
|
113
129
|
this.emit();
|
|
114
130
|
const input = { text: trimmed };
|
|
115
131
|
if (this.blind)
|
|
@@ -136,24 +152,80 @@ export class Assistant {
|
|
|
136
152
|
}
|
|
137
153
|
this.store.id = t.conversationId;
|
|
138
154
|
this.attachments = [];
|
|
155
|
+
this.acceptedAt = timer(this.host).now();
|
|
139
156
|
this.follow(t.runId);
|
|
140
157
|
return true;
|
|
141
158
|
}
|
|
142
159
|
catch (e) {
|
|
143
|
-
|
|
160
|
+
// Not sent: the question stays, marked failed (retry sends it in its place), or queued when the network lost it.
|
|
161
|
+
const queued = this.queueOffline && !(e instanceof ApiError && e.status > 0);
|
|
162
|
+
const id = this.store.fail(queued);
|
|
163
|
+
if (id)
|
|
164
|
+
this.unsent.set(id, { text: trimmed, page, refs, queued });
|
|
144
165
|
this.error = e instanceof ApiError ? e.code : "error.network";
|
|
145
166
|
this.emit();
|
|
146
167
|
return false;
|
|
147
168
|
}
|
|
148
169
|
}
|
|
170
|
+
/** Sends a failed or queued question again in its place (the last one when no id is given). */
|
|
171
|
+
async retry(messageId) {
|
|
172
|
+
const id = messageId ?? [...this.unsent.keys()].pop();
|
|
173
|
+
const u = id === undefined ? undefined : this.unsent.get(id);
|
|
174
|
+
if (!u || id === undefined || this.snapshot().busy)
|
|
175
|
+
return false;
|
|
176
|
+
this.unsent.delete(id);
|
|
177
|
+
this.store.remove(id);
|
|
178
|
+
return this.send(u.text, u.page, u.refs);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The network is back (the host calls it on a network change): a run whose follow failed is followed again from its
|
|
182
|
+
* cursor, else the first queued question is sent.
|
|
183
|
+
*/
|
|
184
|
+
async resume() {
|
|
185
|
+
if (this.host.online && !this.host.online())
|
|
186
|
+
return;
|
|
187
|
+
const r = this.run;
|
|
188
|
+
if (r && this.lost === r.runId) {
|
|
189
|
+
this.follow(r.runId, r.seq);
|
|
190
|
+
this.run = { ...r, state: "working" };
|
|
191
|
+
delete this.run.code;
|
|
192
|
+
this.emit();
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const next = [...this.unsent].find(([, u]) => u.queued);
|
|
196
|
+
if (next)
|
|
197
|
+
await this.retry(next[0]);
|
|
198
|
+
}
|
|
199
|
+
/** Ends the run here at once (stop, the cap, a lost follow): its question settles, nothing spins. */
|
|
200
|
+
end(state, code) {
|
|
201
|
+
if (!this.run || finished(this.run))
|
|
202
|
+
return;
|
|
203
|
+
this.run = { ...this.run, state, longRunning: false, stages: this.run.stages.map((s) => (s.state === "on" ? { ...s, state: "done" } : s)) };
|
|
204
|
+
if (code)
|
|
205
|
+
this.run.code = code;
|
|
206
|
+
this.store.settle();
|
|
207
|
+
}
|
|
149
208
|
/** Follows a run (null stops following). */
|
|
150
209
|
follow(runId, after = 0) {
|
|
151
210
|
this.stopFollowing?.();
|
|
152
211
|
this.stopFollowing = null;
|
|
212
|
+
this.lost = null;
|
|
213
|
+
const t = timer(this.host);
|
|
214
|
+
t.clear(this.cap);
|
|
153
215
|
if (runId === null)
|
|
154
216
|
return;
|
|
155
217
|
this.run = emptyRun(runId);
|
|
156
218
|
this.emit();
|
|
219
|
+
if (this.maxRunMs > 0) {
|
|
220
|
+
this.cap = t.set(() => {
|
|
221
|
+
if (this.run?.runId !== runId || finished(this.run))
|
|
222
|
+
return;
|
|
223
|
+
this.follow(null);
|
|
224
|
+
this.end("failed", "timeout");
|
|
225
|
+
this.emit();
|
|
226
|
+
void this.api.cancel(runId).catch(() => undefined);
|
|
227
|
+
}, this.maxRunMs);
|
|
228
|
+
}
|
|
157
229
|
this.stopFollowing = this.transport.follow(runId, after, {
|
|
158
230
|
onEvent: (e) => {
|
|
159
231
|
if (!this.run || this.run.runId !== runId)
|
|
@@ -172,7 +244,15 @@ export class Assistant {
|
|
|
172
244
|
}
|
|
173
245
|
this.emit();
|
|
174
246
|
},
|
|
175
|
-
onEnd: () =>
|
|
247
|
+
onEnd: (reason, code) => {
|
|
248
|
+
if (reason === "failed" && this.run?.runId === runId && !finished(this.run)) {
|
|
249
|
+
this.end("failed", code ?? "network");
|
|
250
|
+
this.lost = runId;
|
|
251
|
+
}
|
|
252
|
+
this.emit();
|
|
253
|
+
if (reason === "finished" && [...this.unsent.values()].some((u) => u.queued))
|
|
254
|
+
void this.resume();
|
|
255
|
+
},
|
|
176
256
|
});
|
|
177
257
|
}
|
|
178
258
|
/**
|
|
@@ -202,6 +282,14 @@ export class Assistant {
|
|
|
202
282
|
void this.devices.flush().catch(() => undefined);
|
|
203
283
|
return "device";
|
|
204
284
|
}
|
|
285
|
+
if (!online && this.queueOffline) {
|
|
286
|
+
this.store.optimistic(trimmed, at());
|
|
287
|
+
const id = this.store.fail(true);
|
|
288
|
+
if (id)
|
|
289
|
+
this.unsent.set(id, { text: trimmed, page, refs, queued: true });
|
|
290
|
+
this.emit();
|
|
291
|
+
return "queued";
|
|
292
|
+
}
|
|
205
293
|
if (!online) {
|
|
206
294
|
this.store.local("user", trimmed, at());
|
|
207
295
|
this.store.local("assistant", this.texts.t("device.needs_online"), at(), [], "device", "error");
|
|
@@ -275,16 +363,18 @@ export class Assistant {
|
|
|
275
363
|
// the run timed out or ended: it went on without this answer
|
|
276
364
|
}
|
|
277
365
|
}
|
|
278
|
-
/**
|
|
366
|
+
/**
|
|
367
|
+
* Stops the run that is working: here at once (offline the cancel failed and the stop button did nothing), then on
|
|
368
|
+
* the server, best effort.
|
|
369
|
+
*/
|
|
279
370
|
async stop() {
|
|
280
371
|
if (!this.run || finished(this.run))
|
|
281
372
|
return;
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
373
|
+
const id = this.run.runId;
|
|
374
|
+
this.follow(null);
|
|
375
|
+
this.end("cancelled");
|
|
376
|
+
this.emit();
|
|
377
|
+
await this.api.cancel(id).catch(() => undefined); // the run may have finished meanwhile, or the network is gone
|
|
288
378
|
}
|
|
289
379
|
notePlan(m) {
|
|
290
380
|
if (m.card?.kind === "plan" && !this.plans.has(m.card.id))
|
package/dist/conversation.d.ts
CHANGED
|
@@ -18,6 +18,10 @@ export interface MessageView {
|
|
|
18
18
|
body: Record<string, unknown>;
|
|
19
19
|
} | null;
|
|
20
20
|
pending: boolean;
|
|
21
|
+
/** The person's question did not reach the server: `Assistant.retry(id)` sends it again in its place. */
|
|
22
|
+
failed?: boolean;
|
|
23
|
+
/** Offline with `queueOffline`: the question waits and goes on `Assistant.resume()`. */
|
|
24
|
+
queued?: boolean;
|
|
21
25
|
}
|
|
22
26
|
/** A stored or streamed message, read with type checks only. */
|
|
23
27
|
export declare function messageView(raw: unknown): MessageView;
|
|
@@ -33,6 +37,10 @@ export declare class ConversationStore {
|
|
|
33
37
|
optimistic(text: string, at: string): MessageView;
|
|
34
38
|
/** The question stays, no longer dim: its run ended (answered or not) or it could not be sent; the server's copy still replaces it. */
|
|
35
39
|
settle(): void;
|
|
40
|
+
/** The waiting question did not go (or waits for a connection): marked, and no longer the one a reply replaces. */
|
|
41
|
+
fail(queued: boolean): string | null;
|
|
42
|
+
/** Removes one message (a failed question sent again takes its place). */
|
|
43
|
+
remove(id: string): void;
|
|
36
44
|
private dropPending;
|
|
37
45
|
/**
|
|
38
46
|
* A message made on the device (a flow's answer, the "needs a connection" line): never sent to the server, shown in
|
package/dist/conversation.js
CHANGED
|
@@ -65,6 +65,23 @@ export class ConversationStore {
|
|
|
65
65
|
if (m && this.pendingId !== null)
|
|
66
66
|
this.byId.set(this.pendingId, { ...m, pending: false });
|
|
67
67
|
}
|
|
68
|
+
/** The waiting question did not go (or waits for a connection): marked, and no longer the one a reply replaces. */
|
|
69
|
+
fail(queued) {
|
|
70
|
+
const id = this.pendingId;
|
|
71
|
+
const m = id === null ? undefined : this.byId.get(id);
|
|
72
|
+
if (!m || id === null)
|
|
73
|
+
return null;
|
|
74
|
+
this.byId.set(id, queued ? { ...m, pending: false, queued } : { ...m, pending: false, failed: true });
|
|
75
|
+
this.pendingId = null;
|
|
76
|
+
return id;
|
|
77
|
+
}
|
|
78
|
+
/** Removes one message (a failed question sent again takes its place). */
|
|
79
|
+
remove(id) {
|
|
80
|
+
this.byId.delete(id);
|
|
81
|
+
this.order = this.order.filter((x) => x !== id);
|
|
82
|
+
if (this.pendingId === id)
|
|
83
|
+
this.pendingId = null;
|
|
84
|
+
}
|
|
68
85
|
dropPending() {
|
|
69
86
|
if (this.pendingId === null)
|
|
70
87
|
return;
|
package/dist/transport.d.ts
CHANGED
|
@@ -8,22 +8,30 @@ import type { AssistantHost } from "./host.js";
|
|
|
8
8
|
import type { RawEvent } from "./run.js";
|
|
9
9
|
export interface TransportHandlers {
|
|
10
10
|
onEvent(e: RawEvent): void;
|
|
11
|
-
/**
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The run finished, or the transport stopped for good (with the reason). `failed` carries a code: `lost` (the server
|
|
13
|
+
* no longer knows the run) or `network` (every poll failed for `lostAfterMs`).
|
|
14
|
+
*/
|
|
15
|
+
onEnd(reason: "finished" | "stopped" | "failed", code?: string): void;
|
|
13
16
|
}
|
|
14
17
|
export interface Transport {
|
|
15
18
|
readonly kind: "sse" | "poll" | "sync";
|
|
16
19
|
/** Follows a run from a cursor; returns a function that stops following. */
|
|
17
20
|
follow(runId: string, after: number, handlers: TransportHandlers): () => void;
|
|
18
21
|
}
|
|
19
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Long polling: waits up to 25 s on the server; between empty answers 1.2 s, then 3 s after 30 s, 6 s after 2 minutes.
|
|
24
|
+
* A failed poll is tried again in 3 s; polls failing for `lostAfterMs` in a row, or a 404 (the run is gone), end the
|
|
25
|
+
* follow as `failed`: retrying for ever kept the work card spinning and refused every new question (a host, 2026-12-07).
|
|
26
|
+
*/
|
|
20
27
|
export declare class PollTransport implements Transport {
|
|
21
28
|
private readonly api;
|
|
22
29
|
private readonly host;
|
|
23
30
|
private readonly wait;
|
|
31
|
+
private readonly lostAfterMs;
|
|
24
32
|
readonly kind: "poll";
|
|
25
33
|
private hidden;
|
|
26
|
-
constructor(api: AssistantApi, host: AssistantHost, wait?: number);
|
|
34
|
+
constructor(api: AssistantApi, host: AssistantHost, wait?: number, lostAfterMs?: number);
|
|
27
35
|
static gap(sinceStartMs: number): number;
|
|
28
36
|
follow(runId: string, after: number, h: TransportHandlers): () => void;
|
|
29
37
|
}
|
|
@@ -31,7 +39,7 @@ export declare class PollTransport implements Transport {
|
|
|
31
39
|
export declare class SyncTransport implements Transport {
|
|
32
40
|
readonly kind: "sync";
|
|
33
41
|
private readonly poll;
|
|
34
|
-
constructor(api: AssistantApi, host: AssistantHost);
|
|
42
|
+
constructor(api: AssistantApi, host: AssistantHost, lostAfterMs?: number);
|
|
35
43
|
follow(runId: string, after: number, h: TransportHandlers): () => void;
|
|
36
44
|
}
|
|
37
45
|
/** Server-sent events through the host's EventSource, with a stream ticket (EventSource sends no headers). */
|
|
@@ -48,4 +56,5 @@ export declare class SseTransport implements Transport {
|
|
|
48
56
|
export declare function pickTransport(api: AssistantApi, host: AssistantHost, options?: {
|
|
49
57
|
sse?: boolean;
|
|
50
58
|
sync?: boolean;
|
|
59
|
+
lostAfterMs?: number;
|
|
51
60
|
}): Transport;
|
package/dist/transport.js
CHANGED
|
@@ -3,15 +3,20 @@
|
|
|
3
3
|
* synchronous mode. Each hands the same events to the run, so the renderer never knows which is in use. Two stream
|
|
4
4
|
* failures in a row fall back to polling for the rest of the session; polling pauses while the app is in the background.
|
|
5
5
|
*/
|
|
6
|
-
import { toRawEvent } from "./api.js";
|
|
6
|
+
import { ApiError, toRawEvent } from "./api.js";
|
|
7
7
|
import { timer } from "./host.js";
|
|
8
8
|
const EVENT_TYPES = ["run.started", "stage", "tier", "progress", "tool", "text.delta", "message", "card", "waiting", "round", "guard", "result", "reconnect", "run.finished"];
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Long polling: waits up to 25 s on the server; between empty answers 1.2 s, then 3 s after 30 s, 6 s after 2 minutes.
|
|
11
|
+
* A failed poll is tried again in 3 s; polls failing for `lostAfterMs` in a row, or a 404 (the run is gone), end the
|
|
12
|
+
* follow as `failed`: retrying for ever kept the work card spinning and refused every new question (a host, 2026-12-07).
|
|
13
|
+
*/
|
|
10
14
|
export class PollTransport {
|
|
11
|
-
constructor(api, host, wait = 25) {
|
|
15
|
+
constructor(api, host, wait = 25, lostAfterMs = 45000) {
|
|
12
16
|
this.api = api;
|
|
13
17
|
this.host = host;
|
|
14
18
|
this.wait = wait;
|
|
19
|
+
this.lostAfterMs = lostAfterMs;
|
|
15
20
|
this.kind = "poll";
|
|
16
21
|
this.hidden = false;
|
|
17
22
|
host.onBackground?.((h) => { this.hidden = h; });
|
|
@@ -25,6 +30,7 @@ export class PollTransport {
|
|
|
25
30
|
let stopped = false;
|
|
26
31
|
let cursor = after;
|
|
27
32
|
let handle;
|
|
33
|
+
let failingSince = -1;
|
|
28
34
|
const tick = async () => {
|
|
29
35
|
if (stopped)
|
|
30
36
|
return;
|
|
@@ -36,6 +42,7 @@ export class PollTransport {
|
|
|
36
42
|
const page = await this.api.events(runId, cursor, this.wait);
|
|
37
43
|
if (stopped)
|
|
38
44
|
return;
|
|
45
|
+
failingSince = -1;
|
|
39
46
|
for (const e of page.events) {
|
|
40
47
|
cursor = Math.max(cursor, e.seq);
|
|
41
48
|
h.onEvent(e);
|
|
@@ -47,20 +54,35 @@ export class PollTransport {
|
|
|
47
54
|
}
|
|
48
55
|
handle = t.set(() => void tick(), page.events.length > 0 ? 0 : PollTransport.gap(t.now() - started));
|
|
49
56
|
}
|
|
50
|
-
catch {
|
|
51
|
-
if (
|
|
52
|
-
|
|
57
|
+
catch (e) {
|
|
58
|
+
if (stopped)
|
|
59
|
+
return;
|
|
60
|
+
if (failingSince < 0)
|
|
61
|
+
failingSince = t.now();
|
|
62
|
+
const gone = e instanceof ApiError && e.status === 404;
|
|
63
|
+
if (gone || t.now() - failingSince >= this.lostAfterMs) {
|
|
64
|
+
stopped = true;
|
|
65
|
+
h.onEnd("failed", gone ? "lost" : "network");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
handle = t.set(() => void tick(), 3000);
|
|
53
69
|
}
|
|
54
70
|
};
|
|
55
71
|
void tick();
|
|
56
|
-
return () => {
|
|
72
|
+
return () => {
|
|
73
|
+
t.clear(handle);
|
|
74
|
+
if (stopped)
|
|
75
|
+
return; // already ended as failed
|
|
76
|
+
stopped = true;
|
|
77
|
+
h.onEnd("stopped");
|
|
78
|
+
};
|
|
57
79
|
}
|
|
58
80
|
}
|
|
59
81
|
/** The synchronous mode (the migration period): one request that waits up to 50 s, then polling. */
|
|
60
82
|
export class SyncTransport {
|
|
61
|
-
constructor(api, host) {
|
|
83
|
+
constructor(api, host, lostAfterMs) {
|
|
62
84
|
this.kind = "sync";
|
|
63
|
-
this.poll = new PollTransport(api, host, 50);
|
|
85
|
+
this.poll = new PollTransport(api, host, 50, lostAfterMs);
|
|
64
86
|
}
|
|
65
87
|
follow(runId, after, h) {
|
|
66
88
|
return this.poll.follow(runId, after, h);
|
|
@@ -144,9 +166,9 @@ export class SseTransport {
|
|
|
144
166
|
}
|
|
145
167
|
/** The transport to use: streaming when the host can and it has not failed here; else polling; sync when asked. */
|
|
146
168
|
export function pickTransport(api, host, options = {}) {
|
|
147
|
-
const poll = new PollTransport(api, host);
|
|
169
|
+
const poll = new PollTransport(api, host, 25, options.lostAfterMs);
|
|
148
170
|
if (options.sync)
|
|
149
|
-
return new SyncTransport(api, host);
|
|
171
|
+
return new SyncTransport(api, host, options.lostAfterMs);
|
|
150
172
|
if (options.sse !== false && host.eventSource)
|
|
151
173
|
return new SseTransport(api, host, poll);
|
|
152
174
|
return poll;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@g9tm/aiassistant",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"description": "Headless client of the G9 AI assistant: API client, run state, transports, block view models, safe text and Persian helpers. No DOM, no framework.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@g9tm/aiassistant-contracts": "1.
|
|
36
|
+
"@g9tm/aiassistant-contracts": "1.14.0"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.json",
|