agentchatme 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +161 -1
- package/README.md +22 -17
- package/dist/index.cjs +171 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +231 -54
- package/dist/index.d.ts +231 -54
- package/dist/index.js +171 -29
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -274,7 +274,7 @@ function createAgentChatError(body, status, headers) {
|
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
// src/version.ts
|
|
277
|
-
var VERSION = "1.3.
|
|
277
|
+
var VERSION = "1.3.2" ;
|
|
278
278
|
|
|
279
279
|
// src/runtime.ts
|
|
280
280
|
function detectRuntime() {
|
|
@@ -4743,6 +4743,8 @@ var NO_PROGRESS_BACKOFF_MS = 1e3;
|
|
|
4743
4743
|
var NO_PROGRESS_BACKOFF_MAX_MS = 6e4;
|
|
4744
4744
|
var GATE_CALL_TIMEOUT_MS = 6e4;
|
|
4745
4745
|
var CYCLE_TIMEOUT_MS = 10 * 6e4;
|
|
4746
|
+
var GATE_CHUNK_SIZE = 10;
|
|
4747
|
+
var REPLY_TURN_PLACEHOLDER = "<conversation_id>";
|
|
4746
4748
|
var RECENT_CYCLES = 20;
|
|
4747
4749
|
var Pacer = class {
|
|
4748
4750
|
constructor(paceMs, now2) {
|
|
@@ -4813,10 +4815,12 @@ var ReceiveLoop = class {
|
|
|
4813
4815
|
cycleAbort = null;
|
|
4814
4816
|
gateCallTimeoutMs;
|
|
4815
4817
|
cycleTimeoutMs;
|
|
4818
|
+
gateChunkSize;
|
|
4816
4819
|
lastError = null;
|
|
4817
4820
|
counts = {
|
|
4818
4821
|
frames: 0,
|
|
4819
4822
|
echoes: 0,
|
|
4823
|
+
notices: 0,
|
|
4820
4824
|
turnsStarted: 0,
|
|
4821
4825
|
turnsRefused: 0,
|
|
4822
4826
|
turnsQueued: 0,
|
|
@@ -4825,6 +4829,8 @@ var ReceiveLoop = class {
|
|
|
4825
4829
|
runsCompleted: 0,
|
|
4826
4830
|
runsFailed: 0,
|
|
4827
4831
|
runsNotStarted: 0,
|
|
4832
|
+
chunksOk: 0,
|
|
4833
|
+
chunksFailed: 0,
|
|
4828
4834
|
outcomeRecordFailures: 0
|
|
4829
4835
|
};
|
|
4830
4836
|
outcomes = {};
|
|
@@ -4838,6 +4844,11 @@ var ReceiveLoop = class {
|
|
|
4838
4844
|
}
|
|
4839
4845
|
const maxTurns = options.maxConcurrentTurns ?? 1;
|
|
4840
4846
|
if (!Number.isInteger(maxTurns) || maxTurns < 1) throw new RangeError("ReceiveLoop: maxConcurrentTurns must be an integer >= 1");
|
|
4847
|
+
const chunkSize = options.gateChunkSize ?? GATE_CHUNK_SIZE;
|
|
4848
|
+
if (!Number.isInteger(chunkSize) || chunkSize < 1 || chunkSize > TRAY_LIMIT) {
|
|
4849
|
+
throw new RangeError(`ReceiveLoop: gateChunkSize must be an integer from 1 to ${TRAY_LIMIT}`);
|
|
4850
|
+
}
|
|
4851
|
+
this.gateChunkSize = chunkSize;
|
|
4841
4852
|
this.options = options;
|
|
4842
4853
|
this.client = options.client;
|
|
4843
4854
|
this.now = options.now ?? Date.now;
|
|
@@ -4981,6 +4992,11 @@ var ReceiveLoop = class {
|
|
|
4981
4992
|
this.counts.frames += 1;
|
|
4982
4993
|
const payload = message.payload ?? {};
|
|
4983
4994
|
const conversationId = typeof payload.conversation_id === "string" ? payload.conversation_id : void 0;
|
|
4995
|
+
if (payload.type === "system") {
|
|
4996
|
+
this.counts.notices += 1;
|
|
4997
|
+
this.emit({ type: "notice", ...conversationId ? { conversationId } : {} });
|
|
4998
|
+
return;
|
|
4999
|
+
}
|
|
4984
5000
|
const raw = payload.sender ?? payload.from ?? payload.sender_handle;
|
|
4985
5001
|
const sender = typeof raw === "string" ? normalizeHandle(raw) : "";
|
|
4986
5002
|
if (this.handle !== null && sender === this.handle) {
|
|
@@ -5023,6 +5039,7 @@ var ReceiveLoop = class {
|
|
|
5023
5039
|
token,
|
|
5024
5040
|
signal: controller.signal,
|
|
5025
5041
|
stale: () => this.cycleToken !== token || this.stopped,
|
|
5042
|
+
abandoned: () => this.cycleToken !== token,
|
|
5026
5043
|
exclude: async (work2) => {
|
|
5027
5044
|
if (depth++ === 0) excludedSince = this.now();
|
|
5028
5045
|
try {
|
|
@@ -5037,7 +5054,7 @@ var ReceiveLoop = class {
|
|
|
5037
5054
|
};
|
|
5038
5055
|
const work = (async () => {
|
|
5039
5056
|
const summary = this.options.mode === "gateCall" ? await this.gateCallCycle(run2) : await this.singleRunCycle(run2);
|
|
5040
|
-
if (!run2.
|
|
5057
|
+
if (!run2.abandoned()) this.finishCycle(summary);
|
|
5041
5058
|
})();
|
|
5042
5059
|
let timer = null;
|
|
5043
5060
|
const overdue = new Promise((resolve) => {
|
|
@@ -5097,8 +5114,20 @@ var ReceiveLoop = class {
|
|
|
5097
5114
|
}
|
|
5098
5115
|
};
|
|
5099
5116
|
}
|
|
5100
|
-
/**
|
|
5117
|
+
/**
|
|
5118
|
+
* The runtime's model call, bounded by `gateCallTimeoutMs` (and the cycle's
|
|
5119
|
+
* own abandonment). Every call (one per chunk) gets its own deadline and
|
|
5120
|
+
* its own `signal`. While a call has a deadline, its time does not count
|
|
5121
|
+
* against the cycle's: a tray gated in k chunks may take k deadlines, and a
|
|
5122
|
+
* cycle that is making progress is not abandoned for it. With the deadline
|
|
5123
|
+
* off the watchdog is the calls' only bound, so they count.
|
|
5124
|
+
*/
|
|
5101
5125
|
boundedModelCall(run2) {
|
|
5126
|
+
const call = this.deadlineModelCall(run2);
|
|
5127
|
+
if (!Number.isFinite(this.gateCallTimeoutMs)) return call;
|
|
5128
|
+
return (prompt, options) => run2.exclude(() => call(prompt, options));
|
|
5129
|
+
}
|
|
5130
|
+
deadlineModelCall(run2) {
|
|
5102
5131
|
return (prompt) => new Promise((resolve, reject) => {
|
|
5103
5132
|
const controller = new AbortController();
|
|
5104
5133
|
const abort = () => {
|
|
@@ -5164,7 +5193,7 @@ var ReceiveLoop = class {
|
|
|
5164
5193
|
if (tray === null) return this.summary("tray_failed", { failure: this.lastError });
|
|
5165
5194
|
const cards = cardsOf(tray);
|
|
5166
5195
|
const invites = trayInvites(tray);
|
|
5167
|
-
const unheld = this.
|
|
5196
|
+
const unheld = this.unheld(cards);
|
|
5168
5197
|
const heldByTurns = cards.length - unheld.length;
|
|
5169
5198
|
if (heldByTurns > 0) this.heldWhileRunning = true;
|
|
5170
5199
|
const gateCards = unheld.filter((card) => !this.pacer.held(`conversation:${card.conversation_id}`, anchorFrom(card)));
|
|
@@ -5181,8 +5210,8 @@ var ReceiveLoop = class {
|
|
|
5181
5210
|
const unready = await this.checkReady();
|
|
5182
5211
|
if (run2.stale()) return this.summary("abandoned", base);
|
|
5183
5212
|
if (unready !== null) return this.summary("tools_unavailable", { ...base, failure: unready });
|
|
5184
|
-
let gated = 0;
|
|
5185
5213
|
const tally = {
|
|
5214
|
+
gated: 0,
|
|
5186
5215
|
reply: 0,
|
|
5187
5216
|
dismiss: 0,
|
|
5188
5217
|
defer: 0,
|
|
@@ -5191,44 +5220,127 @@ var ReceiveLoop = class {
|
|
|
5191
5220
|
recordFailures: 0,
|
|
5192
5221
|
turnsStarted: 0,
|
|
5193
5222
|
turnsRefused: 0,
|
|
5194
|
-
turnsQueued: 0
|
|
5223
|
+
turnsQueued: 0,
|
|
5224
|
+
chunksOk: 0,
|
|
5225
|
+
chunksFailed: 0
|
|
5195
5226
|
};
|
|
5227
|
+
let heldDuringGate = 0;
|
|
5196
5228
|
let outcome = "invites_only";
|
|
5197
5229
|
let failure = null;
|
|
5198
5230
|
if (owedCards.length > 0) {
|
|
5199
|
-
|
|
5200
|
-
|
|
5201
|
-
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
tally.
|
|
5210
|
-
|
|
5211
|
-
tally.
|
|
5212
|
-
if (result.outcome === "fail_closed") {
|
|
5213
|
-
outcome = "fail_closed";
|
|
5214
|
-
failure = result.failure;
|
|
5215
|
-
} else {
|
|
5231
|
+
const gate = await this.gateInChunks(run2, tray, gateCards, texts);
|
|
5232
|
+
heldDuringGate = gate.held;
|
|
5233
|
+
tally.gated = gate.gated;
|
|
5234
|
+
tally.reply = gate.verdicts.reply;
|
|
5235
|
+
tally.dismiss = gate.verdicts.dismiss;
|
|
5236
|
+
tally.defer = gate.verdicts.defer;
|
|
5237
|
+
tally.undecided = gate.undecided;
|
|
5238
|
+
tally.recorded = gate.recorded;
|
|
5239
|
+
tally.recordFailures = gate.recordFailures;
|
|
5240
|
+
tally.chunksOk = gate.chunks.filter((c) => c.outcome === "decided").length;
|
|
5241
|
+
tally.chunksFailed = gate.chunks.length - tally.chunksOk;
|
|
5242
|
+
failure = chunkFailure(gate.chunks);
|
|
5243
|
+
if (tally.chunksOk > 0) {
|
|
5216
5244
|
outcome = "decided";
|
|
5217
|
-
for (const reply of
|
|
5245
|
+
for (const reply of gate.reply) {
|
|
5218
5246
|
if (run2.stale()) break;
|
|
5219
5247
|
tally[launchKey(await run2.exclude(() => this.startReplyTurn(reply, texts)))] += 1;
|
|
5220
5248
|
}
|
|
5249
|
+
} else if (tally.chunksFailed > 0) {
|
|
5250
|
+
outcome = "fail_closed";
|
|
5251
|
+
} else if (owedInvites.length === 0 && !run2.stale()) {
|
|
5252
|
+
return this.summary("nothing_owed", { ...base, heldByTurns: heldByTurns + heldDuringGate });
|
|
5221
5253
|
}
|
|
5222
5254
|
}
|
|
5223
5255
|
for (const invite of owedInvites) {
|
|
5224
5256
|
if (run2.stale()) break;
|
|
5225
5257
|
tally[launchKey(await run2.exclude(() => this.startInviteTurn(invite, texts)))] += 1;
|
|
5226
5258
|
}
|
|
5227
|
-
|
|
5259
|
+
const parts = { ...base, heldByTurns: heldByTurns + heldDuringGate, ...tally };
|
|
5260
|
+
if (run2.stale()) return this.summary("abandoned", parts);
|
|
5228
5261
|
if (tray.truncated === true && outcome === "decided") {
|
|
5229
5262
|
this.readTheRest(tally.recorded > 0 || tally.turnsStarted > 0);
|
|
5230
5263
|
}
|
|
5231
|
-
return this.summary(outcome, { ...
|
|
5264
|
+
return this.summary(outcome, { ...parts, failure });
|
|
5265
|
+
}
|
|
5266
|
+
/**
|
|
5267
|
+
* Held out of the gate: a running or waiting turn, or a verdict still being
|
|
5268
|
+
* recorded (an abandoned cycle's last call): gating it again could record
|
|
5269
|
+
* the same verdict twice (a second defer climbs the ladder).
|
|
5270
|
+
*/
|
|
5271
|
+
unheld(cards) {
|
|
5272
|
+
return this.tracker.withoutRunning(cards).filter((card) => !this.recordsInFlight.has(card.conversation_id));
|
|
5273
|
+
}
|
|
5274
|
+
/**
|
|
5275
|
+
* The gate in chunks (contract v1 §3.6): the cards, in tray order, split
|
|
5276
|
+
* into chunks of at most `gateChunkSize`, one gate call per chunk, one
|
|
5277
|
+
* after another. Each chunk's prompt is the gate prompt for its own cards
|
|
5278
|
+
* (N = the cards in that chunk), each call has its own deadline and the
|
|
5279
|
+
* cycle's cancellation, and each chunk's dismiss / defer verdicts are
|
|
5280
|
+
* recorded as soon as it answers. A chunk that fails closed leaves only its
|
|
5281
|
+
* own cards undecided. Before each call the chunk is checked again for
|
|
5282
|
+
* cards a turn has taken since the tray was read (a shared tracker), so a
|
|
5283
|
+
* longer gate never decides a card a turn holds; a chunk left with muted
|
|
5284
|
+
* cards only (or none) is not called. Stops once the cycle is stale; a call
|
|
5285
|
+
* a stop cut short is not counted as a failed chunk, and a cycle the
|
|
5286
|
+
* watchdog abandoned reports and counts no chunk after its summary.
|
|
5287
|
+
*/
|
|
5288
|
+
async gateInChunks(run2, tray, cards, texts) {
|
|
5289
|
+
const size = this.gateChunkSize;
|
|
5290
|
+
const of = Math.ceil(cards.length / size);
|
|
5291
|
+
const callModel = this.boundedModelCall(run2);
|
|
5292
|
+
const client = this.recordingClient(run2);
|
|
5293
|
+
const out = {
|
|
5294
|
+
gated: 0,
|
|
5295
|
+
held: 0,
|
|
5296
|
+
reply: [],
|
|
5297
|
+
verdicts: { reply: 0, dismiss: 0, defer: 0 },
|
|
5298
|
+
undecided: 0,
|
|
5299
|
+
recorded: 0,
|
|
5300
|
+
recordFailures: 0,
|
|
5301
|
+
chunks: []
|
|
5302
|
+
};
|
|
5303
|
+
for (let index = 1; index <= of; index++) {
|
|
5304
|
+
if (run2.stale()) break;
|
|
5305
|
+
const planned = cards.slice((index - 1) * size, index * size);
|
|
5306
|
+
const chunk = this.unheld(planned);
|
|
5307
|
+
out.held += planned.length - chunk.length;
|
|
5308
|
+
if (chunk.length < planned.length) this.heldWhileRunning = true;
|
|
5309
|
+
if (!chunk.some((card) => card.muted !== true)) continue;
|
|
5310
|
+
const result = await run({
|
|
5311
|
+
tray: { ...tray, conversations: chunk },
|
|
5312
|
+
callModel,
|
|
5313
|
+
client,
|
|
5314
|
+
texts,
|
|
5315
|
+
signal: run2.signal,
|
|
5316
|
+
...this.options.toolNamer ? { toolNamer: this.options.toolNamer } : {}
|
|
5317
|
+
});
|
|
5318
|
+
if (run2.abandoned()) break;
|
|
5319
|
+
if (run2.stale() && result.outcome !== "decided") break;
|
|
5320
|
+
out.gated += chunk.length;
|
|
5321
|
+
const undecided = new Set(result.undecided);
|
|
5322
|
+
for (const verdict of result.verdicts) {
|
|
5323
|
+
if (!undecided.has(verdict.conversation_id)) out.verdicts[verdict.decision] += 1;
|
|
5324
|
+
}
|
|
5325
|
+
out.reply.push(...result.reply);
|
|
5326
|
+
out.undecided += result.undecided.length;
|
|
5327
|
+
out.recorded += result.recorded;
|
|
5328
|
+
out.recordFailures += result.recordFailures.length;
|
|
5329
|
+
const report = {
|
|
5330
|
+
cycle: run2.token,
|
|
5331
|
+
index,
|
|
5332
|
+
of,
|
|
5333
|
+
cards: chunk.length,
|
|
5334
|
+
outcome: result.outcome === "decided" ? "decided" : "fail_closed",
|
|
5335
|
+
undecided: result.undecided.length,
|
|
5336
|
+
failure: result.failure
|
|
5337
|
+
};
|
|
5338
|
+
out.chunks.push(report);
|
|
5339
|
+
if (report.outcome === "decided") this.counts.chunksOk += 1;
|
|
5340
|
+
else this.counts.chunksFailed += 1;
|
|
5341
|
+
this.emit({ type: "gate_chunk", ...report });
|
|
5342
|
+
}
|
|
5343
|
+
return out;
|
|
5232
5344
|
}
|
|
5233
5345
|
/**
|
|
5234
5346
|
* More conversations wait than one tray shows. After a cycle that moved
|
|
@@ -5360,14 +5472,26 @@ invite_id: ${invite.invite_id}`,
|
|
|
5360
5472
|
return true;
|
|
5361
5473
|
});
|
|
5362
5474
|
}
|
|
5363
|
-
/**
|
|
5475
|
+
/**
|
|
5476
|
+
* Start a turn now when a slot is free, else let it wait for one (its
|
|
5477
|
+
* tracker entry holds the card out of the gate).
|
|
5478
|
+
*
|
|
5479
|
+
* Waiting turns go first, in order. A slot can be free while turns still
|
|
5480
|
+
* wait: the pump frees it when an admission is refused and hands it on to
|
|
5481
|
+
* the next waiting turn only after an await. A turn gated in that window
|
|
5482
|
+
* queues behind them instead of taking the slot. A refused admission here
|
|
5483
|
+
* hands its slot on the same way.
|
|
5484
|
+
*/
|
|
5364
5485
|
async launch(handle, admit) {
|
|
5365
|
-
if (this.activeTurns >= this.maxTurns) {
|
|
5486
|
+
if (this.waiting.length > 0 || this.activeTurns >= this.maxTurns) {
|
|
5366
5487
|
this.waiting.push({ handle, admit });
|
|
5367
5488
|
this.counts.turnsQueued += 1;
|
|
5489
|
+
if (this.activeTurns < this.maxTurns) void this.pump();
|
|
5368
5490
|
return "queued";
|
|
5369
5491
|
}
|
|
5370
|
-
|
|
5492
|
+
if (await this.admitInSlot(handle, admit)) return "started";
|
|
5493
|
+
if (this.waiting.length > 0) void this.pump();
|
|
5494
|
+
return "refused";
|
|
5371
5495
|
}
|
|
5372
5496
|
async admitInSlot(handle, admit) {
|
|
5373
5497
|
this.activeTurns += 1;
|
|
@@ -5539,6 +5663,8 @@ invite_id: ${invite.invite_id}`,
|
|
|
5539
5663
|
turnsQueued: 0,
|
|
5540
5664
|
heldByTurns: 0,
|
|
5541
5665
|
paced: 0,
|
|
5666
|
+
chunksOk: 0,
|
|
5667
|
+
chunksFailed: 0,
|
|
5542
5668
|
truncated: false,
|
|
5543
5669
|
failure: null,
|
|
5544
5670
|
...parts
|
|
@@ -5557,10 +5683,26 @@ invite_id: ${invite.invite_id}`,
|
|
|
5557
5683
|
}
|
|
5558
5684
|
};
|
|
5559
5685
|
function replyPrompt(texts, conversationId) {
|
|
5686
|
+
const raw = texts.reply_turn_template;
|
|
5687
|
+
const template = typeof raw === "string" ? raw.trim() : "";
|
|
5688
|
+
if (template.length > 0) {
|
|
5689
|
+
if (template.includes(REPLY_TURN_PLACEHOLDER)) return template.split(REPLY_TURN_PLACEHOLDER).join(conversationId);
|
|
5690
|
+
return `${template}
|
|
5691
|
+
|
|
5692
|
+
conversation_id: ${conversationId}`;
|
|
5693
|
+
}
|
|
5560
5694
|
return `${texts.wake_instruction.trim()}
|
|
5561
5695
|
|
|
5562
5696
|
conversation_id: ${conversationId}`;
|
|
5563
5697
|
}
|
|
5698
|
+
function chunkFailure(chunks) {
|
|
5699
|
+
const failed = chunks.filter((chunk) => chunk.outcome === "fail_closed");
|
|
5700
|
+
const first = failed[0];
|
|
5701
|
+
if (first === void 0) return null;
|
|
5702
|
+
const reason = first.failure ?? "no usable verdicts in the answer";
|
|
5703
|
+
if (chunks.length === 1) return reason;
|
|
5704
|
+
return `${failed.length} of ${chunks.length} gate chunks failed; chunk ${first.index}: ${reason}`;
|
|
5705
|
+
}
|
|
5564
5706
|
function timeoutOption(value, fallback, name) {
|
|
5565
5707
|
if (value === void 0) return fallback;
|
|
5566
5708
|
if (value === 0 || value === Number.POSITIVE_INFINITY) return Number.POSITIVE_INFINITY;
|