@oyasmi/pipiclaw 0.9.5-beta.3 → 0.9.5-beta.4
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/dingtalk/delivery.d.ts +2 -3
- package/dist/dingtalk/delivery.js +72 -174
- package/dist/dingtalk/progress.d.ts +28 -42
- package/dist/dingtalk/progress.js +75 -97
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The DingTalk implementation of the turn output port (`ChannelContext`): it reads a turn's
|
|
3
3
|
* structured events and renders them as AI-Card progress, plain messages and a final answer,
|
|
4
|
-
* including the "card failed, send it plainly" fallbacks. Progress
|
|
5
|
-
*
|
|
4
|
+
* including the "card failed, send it plainly" fallbacks. Progress grows in a local window,
|
|
5
|
+
* folds at its byte ceiling, and publishes paced deltas between replacement snapshots. The answer
|
|
6
6
|
* is a normal message and the card then folds to one line.
|
|
7
7
|
*/
|
|
8
8
|
import type { ChannelContext } from "../channel/channel-context.js";
|
|
@@ -23,7 +23,6 @@ export interface DeliveryPort {
|
|
|
23
23
|
isCardGenerationCurrent(channelId: string, generation: symbol): boolean;
|
|
24
24
|
replaceCard(channelId: string, content: string, finalize?: boolean, failed?: boolean, generation?: symbol): Promise<boolean>;
|
|
25
25
|
appendToCard(channelId: string, content: string, finalize?: boolean, failed?: boolean, generation?: symbol): Promise<boolean>;
|
|
26
|
-
/** False once this process has learned that the template rejects an appended fragment. */
|
|
27
26
|
supportsStreamingAppend(): boolean;
|
|
28
27
|
finalizeCard(channelId: string, content: string, generation?: symbol): Promise<boolean>;
|
|
29
28
|
finalizeExistingCard(channelId: string, content: string, generation?: symbol): Promise<boolean>;
|
|
@@ -2,8 +2,10 @@ import * as log from "../log.js";
|
|
|
2
2
|
import { formatElapsedZh } from "../shared/format.js";
|
|
3
3
|
import { errorMessage } from "../shared/text-utils.js";
|
|
4
4
|
import { describeToolCall } from "../tools/contract/presentation.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import { ProgressWindow, renderCardStatus } from "./progress.js";
|
|
6
|
+
// A minimum gap after each completed write gives the client time to display a fold. This is
|
|
7
|
+
// a throttle, not a debounce: continuous events cannot postpone the next frame indefinitely.
|
|
8
|
+
const PROGRESS_INTERVAL_MS = 800;
|
|
7
9
|
const WAITING_TEXT = "正在处理…";
|
|
8
10
|
const MAX_PROGRESS_FAILURES = 2;
|
|
9
11
|
const SILENT_STATUS = "已处理,无需答复。";
|
|
@@ -11,8 +13,6 @@ const SILENT_STATUS = "已处理,无需答复。";
|
|
|
11
13
|
// has its own timeout, so the sync loop is already bounded; this only guards against
|
|
12
14
|
// an unforeseen stall so run()'s finally can never hang the channel forever.
|
|
13
15
|
const FLUSH_DEADLINE_MS = 60_000;
|
|
14
|
-
/** How long a turn may stay silent before a "正在处理…" card appears. */
|
|
15
|
-
const CARD_WARMUP_MS = 3500;
|
|
16
16
|
class ChannelDeliveryController {
|
|
17
17
|
constructor(turn, bot, archive,
|
|
18
18
|
/** A quiet turn (a background wake) shows no progress, whatever the channel is configured for. */
|
|
@@ -20,13 +20,10 @@ class ChannelDeliveryController {
|
|
|
20
20
|
this.bot = bot;
|
|
21
21
|
this.archive = archive;
|
|
22
22
|
this.quiet = quiet;
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
/** What the card shows after the warmup line has been replaced. Empty until the first real frame. */
|
|
23
|
+
this.nextProgressAt = 0;
|
|
24
|
+
/** What the card shows after the waiting line has been replaced. Empty until the first real frame. */
|
|
26
25
|
this.document = "";
|
|
27
|
-
|
|
28
|
-
this.pendingLines = [];
|
|
29
|
-
this.warmupDelivered = false;
|
|
26
|
+
this.documentEpoch = -1;
|
|
30
27
|
this.mode = "progress";
|
|
31
28
|
this.desiredRevision = 0;
|
|
32
29
|
this.appliedRevision = 0;
|
|
@@ -35,19 +32,21 @@ class ChannelDeliveryController {
|
|
|
35
32
|
this.finalResponseDelivered = false;
|
|
36
33
|
this.ending = false;
|
|
37
34
|
this.progressFailures = 0;
|
|
38
|
-
this.
|
|
39
|
-
this.cardWarmupTriggered = false;
|
|
40
|
-
this.progressStartedAt = 0;
|
|
35
|
+
this.progressStartedAt = Date.now();
|
|
41
36
|
this.toolCallCount = 0;
|
|
42
37
|
this.timer = null;
|
|
43
|
-
this.cardWarmupTimer = null;
|
|
44
38
|
this.flushWaiters = [];
|
|
45
39
|
this.finalReplacementText = "";
|
|
46
40
|
/** Deliveries run one after another in the order the events arrived. */
|
|
47
41
|
this.chain = Promise.resolve();
|
|
48
42
|
this.lastAnswer = "";
|
|
49
43
|
this.event = { channelId: turn.channelId };
|
|
44
|
+
this.progress = new ProgressWindow(this.progressStyle === "rolling" ? "rolling" : "full");
|
|
50
45
|
this.cardGeneration = bot.getCardGeneration(turn.channelId);
|
|
46
|
+
// Start the waiting card in the same sync loop as progress and completion. Do not await
|
|
47
|
+
// its HTTP request: the caller can start the model while card creation is in flight.
|
|
48
|
+
if (this.progressStyle !== "none")
|
|
49
|
+
this.bumpRevision(true);
|
|
51
50
|
}
|
|
52
51
|
get isCurrent() {
|
|
53
52
|
return this.bot.isCardGenerationCurrent(this.event.channelId, this.cardGeneration);
|
|
@@ -101,8 +100,8 @@ class ChannelDeliveryController {
|
|
|
101
100
|
await this.remember(event.failure, "✕");
|
|
102
101
|
return;
|
|
103
102
|
case "reasoning":
|
|
104
|
-
//
|
|
105
|
-
await this.remember(event.text, "✦", {
|
|
103
|
+
// Both modes record thinking chronologically; full retains the latest thought on a fold.
|
|
104
|
+
await this.remember(event.text, "✦", { tail: true });
|
|
106
105
|
return;
|
|
107
106
|
case "commentary":
|
|
108
107
|
await this.deliverCommentary(event.text, event.level);
|
|
@@ -183,6 +182,7 @@ class ChannelDeliveryController {
|
|
|
183
182
|
await this.silence();
|
|
184
183
|
break;
|
|
185
184
|
case "cancelled":
|
|
185
|
+
await this.silence();
|
|
186
186
|
break;
|
|
187
187
|
case "completed":
|
|
188
188
|
if (this.lastAnswer && !this.finalResponseDelivered) {
|
|
@@ -204,32 +204,6 @@ class ChannelDeliveryController {
|
|
|
204
204
|
await this.close();
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
-
primeCard(delayMs) {
|
|
208
|
-
if (this.closed ||
|
|
209
|
-
this.ending ||
|
|
210
|
-
this.progressStyle === "none" ||
|
|
211
|
-
this.cardWarmupScheduled ||
|
|
212
|
-
this.cardWarmupTriggered) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
this.cardWarmupScheduled = true;
|
|
216
|
-
this.cardWarmupTimer = setTimeout(() => {
|
|
217
|
-
this.cardWarmupScheduled = false;
|
|
218
|
-
this.cardWarmupTimer = null;
|
|
219
|
-
if (this.closed || this.ending || !this.isCurrent || this.desiredRevision > 0)
|
|
220
|
-
return;
|
|
221
|
-
this.cardWarmupTriggered = true;
|
|
222
|
-
// Use the same serialized loop as progress and completion; no detached create request.
|
|
223
|
-
this.bumpRevision(true);
|
|
224
|
-
}, Math.max(0, delayMs));
|
|
225
|
-
}
|
|
226
|
-
clearCardWarmup() {
|
|
227
|
-
this.cardWarmupScheduled = false;
|
|
228
|
-
if (this.cardWarmupTimer) {
|
|
229
|
-
clearTimeout(this.cardWarmupTimer);
|
|
230
|
-
this.cardWarmupTimer = null;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
207
|
archiveBotResponse(text) {
|
|
234
208
|
void this.archive(this.event.channelId, text).catch((err) => {
|
|
235
209
|
log.logWarn("channel.archive.write_failed", "Bot response archive failed", {
|
|
@@ -239,8 +213,7 @@ class ChannelDeliveryController {
|
|
|
239
213
|
});
|
|
240
214
|
}
|
|
241
215
|
/**
|
|
242
|
-
* Record
|
|
243
|
-
* itself is queued and appended; the header is rewritten only when the card is compressed.
|
|
216
|
+
* Record the latest desired view; bursts update it while one paced request publishes it.
|
|
244
217
|
*/
|
|
245
218
|
async remember(text, marker, options) {
|
|
246
219
|
if (this.closed || this.ending || !this.isCurrent || !text.trim())
|
|
@@ -249,42 +222,22 @@ class ChannelDeliveryController {
|
|
|
249
222
|
// so we never create or flicker a card before the final replacement.
|
|
250
223
|
if (this.progressStyle === "none")
|
|
251
224
|
return;
|
|
252
|
-
this.clearCardWarmup();
|
|
253
|
-
if (this.progressStartedAt === 0)
|
|
254
|
-
this.progressStartedAt = Date.now();
|
|
255
225
|
if (options?.countTool)
|
|
256
226
|
this.toolCallCount++;
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
else {
|
|
261
|
-
this.activities.push({ marker, text, tail: options?.tail === true });
|
|
262
|
-
while (this.activities.length > PROGRESS_WINDOW)
|
|
263
|
-
this.activities.shift();
|
|
264
|
-
}
|
|
265
|
-
this.queueLine(marker, text, options?.tail === true);
|
|
227
|
+
const changed = this.progress.record({ marker, text, tail: options?.tail === true });
|
|
228
|
+
if (!changed && !options?.countTool)
|
|
229
|
+
return;
|
|
266
230
|
this.mode = "progress";
|
|
267
231
|
this.bumpRevision(false);
|
|
268
232
|
}
|
|
269
|
-
/** Queue one finished list item. An unchanged tail is not sent again. */
|
|
270
|
-
queueLine(marker, text, tail) {
|
|
271
|
-
const line = renderProgressLine({ marker, text, tail });
|
|
272
|
-
if (!line)
|
|
273
|
-
return;
|
|
274
|
-
const last = this.pendingLines.at(-1) ?? (this.document ? this.document.split("\n").at(-1) : undefined);
|
|
275
|
-
if (line === last)
|
|
276
|
-
return;
|
|
277
|
-
this.pendingLines.push(line);
|
|
278
|
-
}
|
|
279
233
|
async sendFinal(text, shouldLog, title) {
|
|
280
234
|
if (this.closed || this.ending || !this.isCurrent || !text.trim())
|
|
281
235
|
return this.finalResponseDelivered;
|
|
282
236
|
this.ending = true;
|
|
283
|
-
this.clearCardWarmup();
|
|
284
237
|
if (this.timer)
|
|
285
238
|
clearTimeout(this.timer);
|
|
286
239
|
this.timer = null;
|
|
287
|
-
if (this.
|
|
240
|
+
if (this.progressStyle !== "none" && !this.hasRecordedProgress()) {
|
|
288
241
|
// The card only ever said "正在处理…": the answer goes into it rather than into a second
|
|
289
242
|
// message under a card that would just read "已答复。".
|
|
290
243
|
this.finalReplacementText = text;
|
|
@@ -313,7 +266,6 @@ class ChannelDeliveryController {
|
|
|
313
266
|
if (this.closed || this.finalResponseDelivered || !this.isCurrent || !text.trim())
|
|
314
267
|
return;
|
|
315
268
|
this.ending = true;
|
|
316
|
-
this.clearCardWarmup();
|
|
317
269
|
this.finalReplacementText = text;
|
|
318
270
|
this.mode = "finalize-with-fallback";
|
|
319
271
|
this.bumpRevision(true);
|
|
@@ -326,7 +278,6 @@ class ChannelDeliveryController {
|
|
|
326
278
|
if (this.closed || this.finalResponseDelivered)
|
|
327
279
|
return;
|
|
328
280
|
this.ending = true;
|
|
329
|
-
this.clearCardWarmup();
|
|
330
281
|
this.mode = "silent";
|
|
331
282
|
this.bumpRevision(true);
|
|
332
283
|
}
|
|
@@ -337,20 +288,20 @@ class ChannelDeliveryController {
|
|
|
337
288
|
schedule(forceImmediate) {
|
|
338
289
|
if (this.running)
|
|
339
290
|
return;
|
|
340
|
-
if (this.timer) {
|
|
341
|
-
clearTimeout(this.timer);
|
|
342
|
-
this.timer = null;
|
|
343
|
-
}
|
|
344
|
-
// Endings go out immediately. Progress waits one macrotask so events already queued on the
|
|
345
|
-
// delivery chain collapse into one write. Nothing polls on a clock.
|
|
346
291
|
if (forceImmediate || this.mode !== "progress") {
|
|
292
|
+
if (this.timer)
|
|
293
|
+
clearTimeout(this.timer);
|
|
294
|
+
this.timer = null;
|
|
347
295
|
void this.runSyncLoop();
|
|
348
296
|
return;
|
|
349
297
|
}
|
|
298
|
+
if (this.timer)
|
|
299
|
+
return;
|
|
300
|
+
// One macrotask coalesces the first burst; subsequent frames respect the minimum gap.
|
|
350
301
|
this.timer = setTimeout(() => {
|
|
351
302
|
this.timer = null;
|
|
352
303
|
void this.runSyncLoop();
|
|
353
|
-
}, 0);
|
|
304
|
+
}, Math.max(0, this.nextProgressAt - Date.now()));
|
|
354
305
|
}
|
|
355
306
|
async runSyncLoop() {
|
|
356
307
|
if (this.running)
|
|
@@ -369,7 +320,7 @@ class ChannelDeliveryController {
|
|
|
369
320
|
}
|
|
370
321
|
}
|
|
371
322
|
else if (mode === "finalize-existing") {
|
|
372
|
-
if (this.
|
|
323
|
+
if (this.progressStyle !== "none") {
|
|
373
324
|
touchedRemote = await this.bot.finalizeExistingCard(this.event.channelId, this.buildSummaryText(), this.cardGeneration);
|
|
374
325
|
}
|
|
375
326
|
}
|
|
@@ -380,7 +331,7 @@ class ChannelDeliveryController {
|
|
|
380
331
|
}
|
|
381
332
|
}
|
|
382
333
|
else if (mode === "silent") {
|
|
383
|
-
if (this.
|
|
334
|
+
if (this.progressStyle !== "none") {
|
|
384
335
|
touchedRemote = await this.bot.finalizeExistingCard(this.event.channelId, renderCardStatus(SILENT_STATUS), this.cardGeneration);
|
|
385
336
|
}
|
|
386
337
|
}
|
|
@@ -394,6 +345,10 @@ class ChannelDeliveryController {
|
|
|
394
345
|
this.progressFailures++;
|
|
395
346
|
}
|
|
396
347
|
this.appliedRevision = revision;
|
|
348
|
+
// Events received during HTTP must wait for the next display interval. An ending
|
|
349
|
+
// still runs immediately after the in-flight write, without draining old progress.
|
|
350
|
+
if (mode === "progress" && this.mode === "progress")
|
|
351
|
+
break;
|
|
397
352
|
}
|
|
398
353
|
}
|
|
399
354
|
finally {
|
|
@@ -445,7 +400,6 @@ class ChannelDeliveryController {
|
|
|
445
400
|
return;
|
|
446
401
|
}
|
|
447
402
|
this.closed = true;
|
|
448
|
-
this.clearCardWarmup();
|
|
449
403
|
if (this.mode === "progress") {
|
|
450
404
|
this.mode = "silent";
|
|
451
405
|
this.bumpRevision(true);
|
|
@@ -453,102 +407,49 @@ class ChannelDeliveryController {
|
|
|
453
407
|
await this.flush();
|
|
454
408
|
this.bot.discardCard(this.event.channelId, this.cardGeneration);
|
|
455
409
|
}
|
|
456
|
-
/** True once this turn has something to show besides the
|
|
410
|
+
/** True once this turn has something to show besides the waiting line. */
|
|
457
411
|
hasRecordedProgress() {
|
|
458
|
-
return this.
|
|
412
|
+
return this.progress.hasContent || this.document !== "";
|
|
459
413
|
}
|
|
460
|
-
/**
|
|
414
|
+
/** Publish one bounded write per display interval, anchored to the last successful write. */
|
|
461
415
|
async publishProgress() {
|
|
462
|
-
if (this.
|
|
463
|
-
return this.publishWarmup();
|
|
464
|
-
let touched = false;
|
|
465
|
-
while (this.pendingLines.length > 0 && this.progressFailures < MAX_PROGRESS_FAILURES) {
|
|
466
|
-
const step = await this.sendPending();
|
|
467
|
-
if (step.touched)
|
|
468
|
-
touched = true;
|
|
469
|
-
if (!step.cont)
|
|
470
|
-
break;
|
|
471
|
-
}
|
|
472
|
-
return touched;
|
|
473
|
-
}
|
|
474
|
-
async publishWarmup() {
|
|
475
|
-
if (this.document || !this.cardWarmupTriggered || this.warmupDelivered)
|
|
416
|
+
if (this.ending || !this.isCurrent)
|
|
476
417
|
return false;
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
this.
|
|
418
|
+
const hasProgress = this.progress.hasContent;
|
|
419
|
+
const planned = hasProgress
|
|
420
|
+
? this.progress.plan(this.buildLiveHeader(), { epoch: this.documentEpoch, content: this.document }, this.bot.supportsStreamingAppend())
|
|
421
|
+
: { epoch: -1, content: WAITING_TEXT, append: false, document: "", complete: true };
|
|
422
|
+
if (!planned)
|
|
480
423
|
return false;
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
fallback: this.snapshot(CARD_REQUEST_MAX_BYTES, this.activities),
|
|
492
|
-
compact: this.snapshot(CARD_COMPACT_MAX_BYTES, this.activities.slice(-1)),
|
|
493
|
-
});
|
|
494
|
-
if (!planned || planned.method === "drop") {
|
|
495
|
-
if (planned?.method === "drop")
|
|
496
|
-
this.pendingLines.shift();
|
|
497
|
-
else
|
|
498
|
-
this.pendingLines = [];
|
|
499
|
-
return { touched: false, cont: planned?.method === "drop" };
|
|
500
|
-
}
|
|
501
|
-
const ok = await this.writeProgress(planned);
|
|
502
|
-
if (ok) {
|
|
503
|
-
this.applySent(planned);
|
|
504
|
-
this.progressFailures = 0;
|
|
505
|
-
return { touched: true, cont: true };
|
|
506
|
-
}
|
|
507
|
-
if (planned.method === "append") {
|
|
508
|
-
const frame = this.bot.supportsStreamingAppend()
|
|
509
|
-
? this.snapshot(CARD_COMPACT_MAX_BYTES, this.activities.slice(-1))
|
|
510
|
-
: this.snapshot(CARD_REQUEST_MAX_BYTES, this.activities);
|
|
511
|
-
const replaced = await this.bot.replaceCard(this.event.channelId, frame, false, false, this.cardGeneration);
|
|
512
|
-
if (replaced) {
|
|
513
|
-
this.document = frame;
|
|
514
|
-
this.pendingLines = this.bot.supportsStreamingAppend()
|
|
515
|
-
? this.pendingLines.filter((line) => !frame.split("\n").includes(line))
|
|
516
|
-
: [];
|
|
424
|
+
try {
|
|
425
|
+
const write = planned.append ? this.bot.appendToCard.bind(this.bot) : this.bot.replaceCard.bind(this.bot);
|
|
426
|
+
const ok = await write(this.event.channelId, planned.content, false, false, this.cardGeneration);
|
|
427
|
+
if (ok) {
|
|
428
|
+
if (hasProgress) {
|
|
429
|
+
// Apply this immutable request's baseline, even if newer events folded the desired
|
|
430
|
+
// view during HTTP. Its epoch forces the next write to replace that old cycle.
|
|
431
|
+
this.document = planned.document;
|
|
432
|
+
this.documentEpoch = planned.epoch;
|
|
433
|
+
}
|
|
517
434
|
this.progressFailures = 0;
|
|
518
|
-
|
|
435
|
+
if (!planned.complete && !this.ending)
|
|
436
|
+
this.bumpRevision(false);
|
|
519
437
|
}
|
|
438
|
+
else {
|
|
439
|
+
this.progressFailures++;
|
|
440
|
+
if (planned.append) {
|
|
441
|
+
// The server may have applied an append whose response was lost. Replacing a short
|
|
442
|
+
// new cycle restores certainty without duplicating that delta or replaying history.
|
|
443
|
+
this.progress.rebase();
|
|
444
|
+
if (this.progressFailures < MAX_PROGRESS_FAILURES && !this.ending)
|
|
445
|
+
this.bumpRevision(false);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return ok;
|
|
520
449
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
async writeProgress(planned) {
|
|
525
|
-
if (planned.method === "append") {
|
|
526
|
-
return this.bot.appendToCard(this.event.channelId, planned.content, false, false, this.cardGeneration);
|
|
527
|
-
}
|
|
528
|
-
return this.bot.replaceCard(this.event.channelId, planned.content, false, false, this.cardGeneration);
|
|
529
|
-
}
|
|
530
|
-
applySent(planned) {
|
|
531
|
-
if (planned.method === "append") {
|
|
532
|
-
this.document += planned.content;
|
|
533
|
-
this.pendingLines.shift();
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
this.document = planned.content;
|
|
537
|
-
if (planned.clearPending) {
|
|
538
|
-
this.pendingLines = [];
|
|
539
|
-
return;
|
|
450
|
+
finally {
|
|
451
|
+
this.nextProgressAt = Date.now() + PROGRESS_INTERVAL_MS;
|
|
540
452
|
}
|
|
541
|
-
const lines = new Set(planned.content.split("\n"));
|
|
542
|
-
this.pendingLines = this.pendingLines.filter((line) => !lines.has(line));
|
|
543
|
-
}
|
|
544
|
-
/** A full snapshot: the live header, pinned thinking in full mode, and the given activities. */
|
|
545
|
-
snapshot(maxBytes, activities) {
|
|
546
|
-
return renderProgressFrame({
|
|
547
|
-
header: this.buildLiveHeader(),
|
|
548
|
-
thinking: this.progressStyle === "full" ? this.thinking : undefined,
|
|
549
|
-
activities,
|
|
550
|
-
maxBytes,
|
|
551
|
-
});
|
|
552
453
|
}
|
|
553
454
|
/** Clock-style elapsed time (`0:42`, `1:02`, `1:05:09`) for the card's first line. */
|
|
554
455
|
formatClock(ms) {
|
|
@@ -560,10 +461,11 @@ class ChannelDeliveryController {
|
|
|
560
461
|
return `${Math.floor(minutes / 60)}:${String(minutes % 60).padStart(2, "0")}:${ss}`;
|
|
561
462
|
}
|
|
562
463
|
elapsedMs() {
|
|
563
|
-
return
|
|
464
|
+
return Date.now() - this.progressStartedAt;
|
|
564
465
|
}
|
|
565
466
|
/**
|
|
566
|
-
*
|
|
467
|
+
* Cycle header, refreshed on replacement so it remains a stable prefix for appended progress.
|
|
468
|
+
* A long turn can retain only the latest thought and a few activity lines after a fold, so
|
|
567
469
|
* without this a long turn looks identical to one that just started. Counted as tool calls,
|
|
568
470
|
* never "steps" — a step is a task's unit (spec 053, D4).
|
|
569
471
|
*/
|
|
@@ -580,9 +482,5 @@ class ChannelDeliveryController {
|
|
|
580
482
|
*/
|
|
581
483
|
export function createDingTalkContext(turn, port, archive, quiet) {
|
|
582
484
|
const controller = new ChannelDeliveryController(turn, port, archive, quiet);
|
|
583
|
-
|
|
584
|
-
// Same warm-up as before: a card that says "正在处理…" appears if the first output is slow.
|
|
585
|
-
if (!quiet)
|
|
586
|
-
controller.primeCard(CARD_WARMUP_MS);
|
|
587
|
-
return context;
|
|
485
|
+
return controller.buildContext();
|
|
588
486
|
}
|
|
@@ -1,57 +1,43 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* (`isFull: false`). The configured template accepts both. Each request stays within
|
|
6
|
-
* {@link CARD_REQUEST_MAX_BYTES}. Once the next item would carry the card past
|
|
7
|
-
* {@link CARD_DOCUMENT_MAX_BYTES}, the card is replaced by a short snapshot and appending resumes.
|
|
8
|
-
* Dynamic text is made markdown-inert first, and every line is finished before it is cut.
|
|
2
|
+
* Locally owned progress cycles. Full mode grows to the document budget; rolling keeps recent
|
|
3
|
+
* activities. Epochs distinguish a replacement from growth, even when retained text is identical.
|
|
4
|
+
* Writes contain only acknowledged-prefix deltas, bounded to complete markdown list items.
|
|
9
5
|
*/
|
|
10
6
|
export declare const CARD_REQUEST_MAX_BYTES = 1000;
|
|
11
7
|
export declare const CARD_DOCUMENT_MAX_BYTES = 2900;
|
|
12
|
-
export declare const CARD_COMPACT_MAX_BYTES = 200;
|
|
13
8
|
/** A line already classified by the delivery controller. `tail` keeps the end of the text. */
|
|
14
9
|
export interface ProgressActivity {
|
|
15
10
|
marker: "" | "➜" | "✕" | "✦";
|
|
16
11
|
text: string;
|
|
17
12
|
tail?: boolean;
|
|
18
13
|
}
|
|
19
|
-
/**
|
|
20
|
-
export
|
|
21
|
-
|
|
14
|
+
/** One bounded, complete list item; long model text cannot cause a sudden height jump. */
|
|
15
|
+
export declare function renderProgressLine(activity: ProgressActivity, maxBytes?: number): string;
|
|
16
|
+
export interface ProgressFrame {
|
|
17
|
+
epoch: number;
|
|
22
18
|
content: string;
|
|
23
|
-
/** The snapshot already stands for every queued line. */
|
|
24
|
-
clearPending: boolean;
|
|
25
19
|
}
|
|
26
|
-
|
|
27
|
-
export declare function renderProgressLine(activity: ProgressActivity, maxBytes?: number): string;
|
|
28
|
-
/**
|
|
29
|
-
* One complete markdown list: a header, the latest thinking excerpt, then the activity window.
|
|
30
|
-
* Lines are dropped whole, then shortened, until the frame fits `maxBytes`.
|
|
31
|
-
*/
|
|
32
|
-
export declare function renderProgressFrame(input: {
|
|
33
|
-
header: string;
|
|
34
|
-
thinking?: string;
|
|
35
|
-
activities: ProgressActivity[];
|
|
36
|
-
maxBytes?: number;
|
|
37
|
-
}): string;
|
|
38
|
-
/**
|
|
39
|
-
* The first real progress frame. It replaces the warmup line instead of appending onto it, and
|
|
40
|
-
* it keeps only the queued items that fit in one request. The rest stay queued for append.
|
|
41
|
-
*/
|
|
42
|
-
export declare function renderOpeningFrame(header: string, pending: readonly string[]): string;
|
|
43
|
-
/**
|
|
44
|
-
* Choose the next progress write. Appending continues while the card stays under the document
|
|
45
|
-
* ceiling; crossing it sends the short snapshot first. With append unavailable, every update is
|
|
46
|
-
* one snapshot that already fits in a single request.
|
|
47
|
-
*/
|
|
48
|
-
export declare function planProgressWrite(input: {
|
|
49
|
-
document: string;
|
|
50
|
-
pending: readonly string[];
|
|
20
|
+
export interface ProgressWrite extends ProgressFrame {
|
|
51
21
|
append: boolean;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
22
|
+
/** The document acknowledged if this write succeeds, including any prior prefix. */
|
|
23
|
+
document: string;
|
|
24
|
+
complete: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** Desired view independent of an in-flight write; old cycles never remain in a replay queue. */
|
|
27
|
+
export declare class ProgressWindow {
|
|
28
|
+
private readonly style;
|
|
29
|
+
private thinking;
|
|
30
|
+
private activities;
|
|
31
|
+
private epoch;
|
|
32
|
+
private header;
|
|
33
|
+
constructor(style: "full" | "rolling");
|
|
34
|
+
get hasContent(): boolean;
|
|
35
|
+
record(activity: ProgressActivity): boolean;
|
|
36
|
+
plan(header: string, acknowledged: ProgressFrame, append: boolean): ProgressWrite | undefined;
|
|
37
|
+
/** An ambiguous append is recovered with a short replacement, never by replaying its delta. */
|
|
38
|
+
rebase(): void;
|
|
39
|
+
private restart;
|
|
40
|
+
private body;
|
|
41
|
+
}
|
|
56
42
|
/** A one-line card ending. Falls back to a fixed line if the text could not stay plain markdown. */
|
|
57
43
|
export declare function renderCardStatus(text: string): string;
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* (`isFull: false`). The configured template accepts both. Each request stays within
|
|
6
|
-
* {@link CARD_REQUEST_MAX_BYTES}. Once the next item would carry the card past
|
|
7
|
-
* {@link CARD_DOCUMENT_MAX_BYTES}, the card is replaced by a short snapshot and appending resumes.
|
|
8
|
-
* Dynamic text is made markdown-inert first, and every line is finished before it is cut.
|
|
2
|
+
* Locally owned progress cycles. Full mode grows to the document budget; rolling keeps recent
|
|
3
|
+
* activities. Epochs distinguish a replacement from growth, even when retained text is identical.
|
|
4
|
+
* Writes contain only acknowledged-prefix deltas, bounded to complete markdown list items.
|
|
9
5
|
*/
|
|
10
6
|
export const CARD_REQUEST_MAX_BYTES = 1000;
|
|
11
7
|
export const CARD_DOCUMENT_MAX_BYTES = 2900;
|
|
12
|
-
|
|
8
|
+
const ROLLING_WINDOW_SIZE = 5;
|
|
9
|
+
const RETAINED_ACTIVITIES = 2;
|
|
13
10
|
const LINE_BYTES = 180;
|
|
11
|
+
// Reserve enough room for all five rolling entries plus their newlines in one snapshot.
|
|
12
|
+
const HEADER_BYTES = CARD_REQUEST_MAX_BYTES - ROLLING_WINDOW_SIZE * (LINE_BYTES + 1);
|
|
14
13
|
const ELLIPSIS = "…";
|
|
15
14
|
/**
|
|
16
15
|
* Make one line of model or tool text safe to drop into a markdown list.
|
|
@@ -83,8 +82,8 @@ function isCompleteMarkdownList(frame) {
|
|
|
83
82
|
const lines = frame.split("\n");
|
|
84
83
|
return lines.length > 0 && lines.every((line) => line.startsWith("- ") && line === line.trim() && line.length > 2);
|
|
85
84
|
}
|
|
86
|
-
/** One list item
|
|
87
|
-
export function renderProgressLine(activity, maxBytes =
|
|
85
|
+
/** One bounded, complete list item; long model text cannot cause a sudden height jump. */
|
|
86
|
+
export function renderProgressLine(activity, maxBytes = LINE_BYTES) {
|
|
88
87
|
const text = markedLine(activity.marker, activity.text, Math.max(0, maxBytes - 2), activity.tail === true);
|
|
89
88
|
if (!text)
|
|
90
89
|
return "";
|
|
@@ -93,102 +92,81 @@ export function renderProgressLine(activity, maxBytes = CARD_REQUEST_MAX_BYTES -
|
|
|
93
92
|
return "";
|
|
94
93
|
return line;
|
|
95
94
|
}
|
|
96
|
-
/**
|
|
97
|
-
|
|
98
|
-
* Lines are dropped whole, then shortened, until the frame fits `maxBytes`.
|
|
99
|
-
*/
|
|
100
|
-
export function renderProgressFrame(input) {
|
|
101
|
-
const maxBytes = input.maxBytes ?? CARD_REQUEST_MAX_BYTES;
|
|
102
|
-
const headerLine = listItem(inert(input.header) || "正在处理…");
|
|
103
|
-
let thinkingBudget = Math.min(LINE_BYTES, maxBytes);
|
|
104
|
-
let lineBudget = Math.min(LINE_BYTES, maxBytes);
|
|
105
|
-
let activities = input.activities.filter((activity) => inert(activity.text));
|
|
106
|
-
const build = () => {
|
|
107
|
-
const lines = [headerLine];
|
|
108
|
-
if (input.thinking) {
|
|
109
|
-
const thinking = markedLine("✦", input.thinking, thinkingBudget, true);
|
|
110
|
-
if (thinking)
|
|
111
|
-
lines.push(listItem(thinking));
|
|
112
|
-
}
|
|
113
|
-
for (const activity of activities) {
|
|
114
|
-
const line = markedLine(activity.marker, activity.text, lineBudget, activity.tail === true);
|
|
115
|
-
if (line)
|
|
116
|
-
lines.push(listItem(line));
|
|
117
|
-
}
|
|
118
|
-
return lines.join("\n");
|
|
119
|
-
};
|
|
120
|
-
let frame = build();
|
|
121
|
-
while (byteLength(frame) > maxBytes && activities.length > 1) {
|
|
122
|
-
activities = activities.slice(1);
|
|
123
|
-
frame = build();
|
|
124
|
-
}
|
|
125
|
-
while (byteLength(frame) > maxBytes && thinkingBudget > 24) {
|
|
126
|
-
thinkingBudget = Math.floor(thinkingBudget * 0.6);
|
|
127
|
-
frame = build();
|
|
128
|
-
}
|
|
129
|
-
while (byteLength(frame) > maxBytes && lineBudget > 24) {
|
|
130
|
-
lineBudget = Math.floor(lineBudget * 0.6);
|
|
131
|
-
frame = build();
|
|
132
|
-
}
|
|
133
|
-
while (byteLength(frame) > maxBytes && activities.length > 0) {
|
|
134
|
-
activities = activities.slice(1);
|
|
135
|
-
frame = build();
|
|
136
|
-
}
|
|
137
|
-
if (byteLength(frame) <= maxBytes && isCompleteMarkdownList(frame))
|
|
138
|
-
return frame;
|
|
139
|
-
if (byteLength(headerLine) <= maxBytes && isCompleteMarkdownList(headerLine))
|
|
140
|
-
return headerLine;
|
|
141
|
-
return "- 正在处理…";
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* The first real progress frame. It replaces the warmup line instead of appending onto it, and
|
|
145
|
-
* it keeps only the queued items that fit in one request. The rest stay queued for append.
|
|
146
|
-
*/
|
|
147
|
-
export function renderOpeningFrame(header, pending) {
|
|
148
|
-
const head = renderProgressLine({ marker: "", text: header });
|
|
95
|
+
/** Take a request-sized prefix without cutting a list item or UTF-8 character. */
|
|
96
|
+
function requestPrefix(content) {
|
|
149
97
|
const lines = [];
|
|
150
|
-
|
|
151
|
-
lines.
|
|
152
|
-
for (const line of pending) {
|
|
153
|
-
const candidate = [...lines, line].join("\n");
|
|
154
|
-
if (byteLength(candidate) > CARD_REQUEST_MAX_BYTES)
|
|
98
|
+
for (const line of content.split("\n")) {
|
|
99
|
+
if (byteLength([...lines, line].join("\n")) > CARD_REQUEST_MAX_BYTES)
|
|
155
100
|
break;
|
|
156
101
|
lines.push(line);
|
|
157
102
|
}
|
|
158
|
-
|
|
159
|
-
if (frame && byteLength(frame) <= CARD_REQUEST_MAX_BYTES && isCompleteMarkdownList(frame))
|
|
160
|
-
return frame;
|
|
161
|
-
return head && byteLength(head) <= CARD_REQUEST_MAX_BYTES ? head : "- 正在处理…";
|
|
103
|
+
return lines.join("\n");
|
|
162
104
|
}
|
|
163
|
-
/**
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return undefined;
|
|
172
|
-
if (!input.append) {
|
|
173
|
-
if (!input.fallback || input.fallback === input.document)
|
|
174
|
-
return undefined;
|
|
175
|
-
return { method: "replace", content: input.fallback, clearPending: true };
|
|
105
|
+
/** Desired view independent of an in-flight write; old cycles never remain in a replay queue. */
|
|
106
|
+
export class ProgressWindow {
|
|
107
|
+
constructor(style) {
|
|
108
|
+
this.style = style;
|
|
109
|
+
this.thinking = "";
|
|
110
|
+
this.activities = [];
|
|
111
|
+
this.epoch = 0;
|
|
112
|
+
this.header = "";
|
|
176
113
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
114
|
+
get hasContent() {
|
|
115
|
+
return this.activities.length > 0;
|
|
116
|
+
}
|
|
117
|
+
record(activity) {
|
|
118
|
+
const line = renderProgressLine(activity);
|
|
119
|
+
if (!line || line === this.activities.at(-1)?.line)
|
|
120
|
+
return false;
|
|
121
|
+
const thinking = activity.marker === "✦";
|
|
122
|
+
if (thinking)
|
|
123
|
+
this.thinking = line;
|
|
124
|
+
this.activities.push({ line, thinking });
|
|
125
|
+
if (this.style === "rolling" && this.activities.length > ROLLING_WINDOW_SIZE) {
|
|
126
|
+
this.activities = this.activities.slice(-ROLLING_WINDOW_SIZE);
|
|
127
|
+
this.restart();
|
|
128
|
+
}
|
|
129
|
+
else if (byteLength(this.body()) > CARD_DOCUMENT_MAX_BYTES) {
|
|
130
|
+
// Bound storage while HTTP is stalled too. Render also accounts for the header.
|
|
131
|
+
this.rebase();
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
plan(header, acknowledged, append) {
|
|
136
|
+
// Without append support, a full cycle must fit in one snapshot. Its header can refresh
|
|
137
|
+
// freely; otherwise it stays stable until replacement so ordinary growth is a prefix.
|
|
138
|
+
if (!append)
|
|
139
|
+
this.header = "";
|
|
140
|
+
this.header ||= renderProgressLine({ marker: "", text: header }, HEADER_BYTES);
|
|
141
|
+
const maxBytes = append ? CARD_DOCUMENT_MAX_BYTES : CARD_REQUEST_MAX_BYTES;
|
|
142
|
+
if (byteLength(`${this.header}\n${this.body()}`) > maxBytes) {
|
|
143
|
+
this.rebase();
|
|
144
|
+
this.header = renderProgressLine({ marker: "", text: header }, HEADER_BYTES);
|
|
145
|
+
}
|
|
146
|
+
const frame = `${this.header}\n${this.body()}`;
|
|
147
|
+
const canAppend = append && acknowledged.epoch === this.epoch && frame.startsWith(acknowledged.content);
|
|
148
|
+
const delta = canAppend ? frame.slice(acknowledged.content.length) : frame;
|
|
149
|
+
if (!delta)
|
|
180
150
|
return undefined;
|
|
181
|
-
|
|
151
|
+
const content = requestPrefix(delta);
|
|
152
|
+
const document = canAppend ? acknowledged.content + content : content;
|
|
153
|
+
return { epoch: this.epoch, content, append: canAppend, document, complete: document === frame };
|
|
154
|
+
}
|
|
155
|
+
/** An ambiguous append is recovered with a short replacement, never by replaying its delta. */
|
|
156
|
+
rebase() {
|
|
157
|
+
if (this.style === "full") {
|
|
158
|
+
const recent = this.activities.filter((activity) => !activity.thinking).slice(-RETAINED_ACTIVITIES);
|
|
159
|
+
this.activities = this.thinking ? [{ line: this.thinking, thinking: true }, ...recent] : recent;
|
|
160
|
+
}
|
|
161
|
+
this.restart();
|
|
162
|
+
}
|
|
163
|
+
restart() {
|
|
164
|
+
this.epoch++;
|
|
165
|
+
this.header = "";
|
|
182
166
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return { method: "drop", content: "", clearPending: false };
|
|
186
|
-
if (byteLength(input.document) + byteLength(delta) > CARD_DOCUMENT_MAX_BYTES) {
|
|
187
|
-
if (!input.compact || input.compact === input.document)
|
|
188
|
-
return { method: "drop", content: "", clearPending: false };
|
|
189
|
-
return { method: "replace", content: input.compact, clearPending: false };
|
|
167
|
+
body() {
|
|
168
|
+
return this.activities.map((activity) => activity.line).join("\n");
|
|
190
169
|
}
|
|
191
|
-
return { method: "append", content: delta, clearPending: false };
|
|
192
170
|
}
|
|
193
171
|
/** A one-line card ending. Falls back to a fixed line if the text could not stay plain markdown. */
|
|
194
172
|
export function renderCardStatus(text) {
|
package/package.json
CHANGED