@cognipeer/agent-sdk 0.8.11 → 0.8.12
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/index.cjs +30 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +30 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -5309,7 +5309,10 @@ function shouldSignalSummarization(state, threshold) {
|
|
|
5309
5309
|
});
|
|
5310
5310
|
const tokenCount = countMessagesTokens(tokCountMessages);
|
|
5311
5311
|
if (tokenCount <= threshold) return "skip";
|
|
5312
|
-
if (state.ctx?.__summarizationExhausted)
|
|
5312
|
+
if (state.ctx?.__summarizationExhausted) {
|
|
5313
|
+
const exhaustedAt = state.ctx?.__summarizationExhaustedAtToolCount;
|
|
5314
|
+
if (typeof exhaustedAt !== "number" || (state.toolHistory || []).length <= exhaustedAt) return "skip";
|
|
5315
|
+
}
|
|
5313
5316
|
const hasFreshSummary = (state.messages || []).some(
|
|
5314
5317
|
(m) => m.role === "tool" && typeof m.content === "string" && (m.content === "SUMMARIZED" || m.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))
|
|
5315
5318
|
);
|
|
@@ -6354,6 +6357,20 @@ function extractCanonicalFacts(messages) {
|
|
|
6354
6357
|
function isSummarizedToolPlaceholder(content) {
|
|
6355
6358
|
return typeof content === "string" && (content === "SUMMARIZED" || content.startsWith("SUMMARIZED_TOOL_RESPONSE") || content.startsWith("ARCHIVED_TOOL_RESPONSE") || content.startsWith("STRUCTURED_TOOL_RESPONSE") || content.startsWith("DROPPED_TOOL_RESPONSE"));
|
|
6356
6359
|
}
|
|
6360
|
+
function collectLatestTurnToolCallIds(messages) {
|
|
6361
|
+
const ids = /* @__PURE__ */ new Set();
|
|
6362
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
6363
|
+
const m = messages[i];
|
|
6364
|
+
if (isSyntheticSummaryMessage(m)) continue;
|
|
6365
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
6366
|
+
for (const tc of m.tool_calls) {
|
|
6367
|
+
if (tc.id) ids.add(tc.id);
|
|
6368
|
+
}
|
|
6369
|
+
break;
|
|
6370
|
+
}
|
|
6371
|
+
}
|
|
6372
|
+
return ids;
|
|
6373
|
+
}
|
|
6357
6374
|
function findToolHistoryEntry(state, toolCallId, toolName) {
|
|
6358
6375
|
const entries = [
|
|
6359
6376
|
...state.toolHistory || [],
|
|
@@ -6448,6 +6465,10 @@ function createContextSummarizeNode(opts) {
|
|
|
6448
6465
|
if (compressableMessages.length === 0) {
|
|
6449
6466
|
return {};
|
|
6450
6467
|
}
|
|
6468
|
+
const protectedToolCallIds = collectLatestTurnToolCallIds(messages);
|
|
6469
|
+
if (compressableMessages.every((m) => m.tool_call_id && protectedToolCallIds.has(m.tool_call_id))) {
|
|
6470
|
+
return {};
|
|
6471
|
+
}
|
|
6451
6472
|
const tokenCountBefore = countMessagesTokens(messages);
|
|
6452
6473
|
const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
|
|
6453
6474
|
const availableTokensForConversation = summaryPromptMaxTokens - 500;
|
|
@@ -6467,7 +6488,8 @@ function createContextSummarizeNode(opts) {
|
|
|
6467
6488
|
const historyEntry = findToolHistoryEntry(state, m.tool_call_id, m.name);
|
|
6468
6489
|
if (historyEntry) {
|
|
6469
6490
|
const original = historyEntry.rawOutput ?? historyEntry.output;
|
|
6470
|
-
|
|
6491
|
+
const recovered = typeof original === "string" ? original : JSON.stringify(original);
|
|
6492
|
+
if (typeof recovered === "string") content = recovered;
|
|
6471
6493
|
}
|
|
6472
6494
|
}
|
|
6473
6495
|
const maxContentLength = 2e3;
|
|
@@ -6606,22 +6628,6 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
|
|
|
6606
6628
|
});
|
|
6607
6629
|
}
|
|
6608
6630
|
});
|
|
6609
|
-
const protectedToolCallIds = /* @__PURE__ */ new Set();
|
|
6610
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
6611
|
-
const m = messages[i];
|
|
6612
|
-
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
6613
|
-
const thisToolCallIds = new Set(m.tool_calls.map((tc) => tc.id).filter(Boolean));
|
|
6614
|
-
const otherCompressable = compressableMessages.filter(
|
|
6615
|
-
(cm) => !thisToolCallIds.has(cm.tool_call_id)
|
|
6616
|
-
);
|
|
6617
|
-
if (otherCompressable.length > 0) {
|
|
6618
|
-
for (const tc of m.tool_calls) {
|
|
6619
|
-
if (tc.id) protectedToolCallIds.add(tc.id);
|
|
6620
|
-
}
|
|
6621
|
-
}
|
|
6622
|
-
break;
|
|
6623
|
-
}
|
|
6624
|
-
}
|
|
6625
6631
|
const retentionDeclarations = collectToolRetentionDeclarations(state.agent?.tools);
|
|
6626
6632
|
const digestableToolCallIds = /* @__PURE__ */ new Set();
|
|
6627
6633
|
for (const m of messages) {
|
|
@@ -8206,11 +8212,16 @@ function createSmartAgent(opts) {
|
|
|
8206
8212
|
if (!summarizer) return { state: currentState, rawMessages: currentRawMessages, compressed: false };
|
|
8207
8213
|
const delta = await summarizer(currentState);
|
|
8208
8214
|
if (!delta || Object.keys(delta).length === 0) {
|
|
8209
|
-
const ctx2 = {
|
|
8215
|
+
const ctx2 = {
|
|
8216
|
+
...currentState.ctx || {},
|
|
8217
|
+
__summarizationExhausted: true,
|
|
8218
|
+
__summarizationExhaustedAtToolCount: (currentState.toolHistory || []).length
|
|
8219
|
+
};
|
|
8210
8220
|
return { state: { ...currentState, ctx: ctx2 }, rawMessages: currentRawMessages, compressed: false };
|
|
8211
8221
|
}
|
|
8212
8222
|
const ctx = { ...currentState.ctx || {} };
|
|
8213
8223
|
delete ctx.__summarizationExhausted;
|
|
8224
|
+
delete ctx.__summarizationExhaustedAtToolCount;
|
|
8214
8225
|
const updated = await persistLatestSummary(syncPlanStateWith(ref, { ...currentState, ...delta, ctx }));
|
|
8215
8226
|
return { state: updated, rawMessages: [...updated.messages || currentRawMessages], compressed: true };
|
|
8216
8227
|
}
|