@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.mjs
CHANGED
|
@@ -5299,7 +5299,10 @@ function shouldSignalSummarization(state, threshold) {
|
|
|
5299
5299
|
});
|
|
5300
5300
|
const tokenCount = countMessagesTokens(tokCountMessages);
|
|
5301
5301
|
if (tokenCount <= threshold) return "skip";
|
|
5302
|
-
if (state.ctx?.__summarizationExhausted)
|
|
5302
|
+
if (state.ctx?.__summarizationExhausted) {
|
|
5303
|
+
const exhaustedAt = state.ctx?.__summarizationExhaustedAtToolCount;
|
|
5304
|
+
if (typeof exhaustedAt !== "number" || (state.toolHistory || []).length <= exhaustedAt) return "skip";
|
|
5305
|
+
}
|
|
5303
5306
|
const hasFreshSummary = (state.messages || []).some(
|
|
5304
5307
|
(m) => m.role === "tool" && typeof m.content === "string" && (m.content === "SUMMARIZED" || m.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))
|
|
5305
5308
|
);
|
|
@@ -6344,6 +6347,20 @@ function extractCanonicalFacts(messages) {
|
|
|
6344
6347
|
function isSummarizedToolPlaceholder(content) {
|
|
6345
6348
|
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"));
|
|
6346
6349
|
}
|
|
6350
|
+
function collectLatestTurnToolCallIds(messages) {
|
|
6351
|
+
const ids = /* @__PURE__ */ new Set();
|
|
6352
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
6353
|
+
const m = messages[i];
|
|
6354
|
+
if (isSyntheticSummaryMessage(m)) continue;
|
|
6355
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
6356
|
+
for (const tc of m.tool_calls) {
|
|
6357
|
+
if (tc.id) ids.add(tc.id);
|
|
6358
|
+
}
|
|
6359
|
+
break;
|
|
6360
|
+
}
|
|
6361
|
+
}
|
|
6362
|
+
return ids;
|
|
6363
|
+
}
|
|
6347
6364
|
function findToolHistoryEntry(state, toolCallId, toolName) {
|
|
6348
6365
|
const entries = [
|
|
6349
6366
|
...state.toolHistory || [],
|
|
@@ -6438,6 +6455,10 @@ function createContextSummarizeNode(opts) {
|
|
|
6438
6455
|
if (compressableMessages.length === 0) {
|
|
6439
6456
|
return {};
|
|
6440
6457
|
}
|
|
6458
|
+
const protectedToolCallIds = collectLatestTurnToolCallIds(messages);
|
|
6459
|
+
if (compressableMessages.every((m) => m.tool_call_id && protectedToolCallIds.has(m.tool_call_id))) {
|
|
6460
|
+
return {};
|
|
6461
|
+
}
|
|
6441
6462
|
const tokenCountBefore = countMessagesTokens(messages);
|
|
6442
6463
|
const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
|
|
6443
6464
|
const availableTokensForConversation = summaryPromptMaxTokens - 500;
|
|
@@ -6457,7 +6478,8 @@ function createContextSummarizeNode(opts) {
|
|
|
6457
6478
|
const historyEntry = findToolHistoryEntry(state, m.tool_call_id, m.name);
|
|
6458
6479
|
if (historyEntry) {
|
|
6459
6480
|
const original = historyEntry.rawOutput ?? historyEntry.output;
|
|
6460
|
-
|
|
6481
|
+
const recovered = typeof original === "string" ? original : JSON.stringify(original);
|
|
6482
|
+
if (typeof recovered === "string") content = recovered;
|
|
6461
6483
|
}
|
|
6462
6484
|
}
|
|
6463
6485
|
const maxContentLength = 2e3;
|
|
@@ -6596,22 +6618,6 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
|
|
|
6596
6618
|
});
|
|
6597
6619
|
}
|
|
6598
6620
|
});
|
|
6599
|
-
const protectedToolCallIds = /* @__PURE__ */ new Set();
|
|
6600
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
6601
|
-
const m = messages[i];
|
|
6602
|
-
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
6603
|
-
const thisToolCallIds = new Set(m.tool_calls.map((tc) => tc.id).filter(Boolean));
|
|
6604
|
-
const otherCompressable = compressableMessages.filter(
|
|
6605
|
-
(cm) => !thisToolCallIds.has(cm.tool_call_id)
|
|
6606
|
-
);
|
|
6607
|
-
if (otherCompressable.length > 0) {
|
|
6608
|
-
for (const tc of m.tool_calls) {
|
|
6609
|
-
if (tc.id) protectedToolCallIds.add(tc.id);
|
|
6610
|
-
}
|
|
6611
|
-
}
|
|
6612
|
-
break;
|
|
6613
|
-
}
|
|
6614
|
-
}
|
|
6615
6621
|
const retentionDeclarations = collectToolRetentionDeclarations(state.agent?.tools);
|
|
6616
6622
|
const digestableToolCallIds = /* @__PURE__ */ new Set();
|
|
6617
6623
|
for (const m of messages) {
|
|
@@ -8196,11 +8202,16 @@ function createSmartAgent(opts) {
|
|
|
8196
8202
|
if (!summarizer) return { state: currentState, rawMessages: currentRawMessages, compressed: false };
|
|
8197
8203
|
const delta = await summarizer(currentState);
|
|
8198
8204
|
if (!delta || Object.keys(delta).length === 0) {
|
|
8199
|
-
const ctx2 = {
|
|
8205
|
+
const ctx2 = {
|
|
8206
|
+
...currentState.ctx || {},
|
|
8207
|
+
__summarizationExhausted: true,
|
|
8208
|
+
__summarizationExhaustedAtToolCount: (currentState.toolHistory || []).length
|
|
8209
|
+
};
|
|
8200
8210
|
return { state: { ...currentState, ctx: ctx2 }, rawMessages: currentRawMessages, compressed: false };
|
|
8201
8211
|
}
|
|
8202
8212
|
const ctx = { ...currentState.ctx || {} };
|
|
8203
8213
|
delete ctx.__summarizationExhausted;
|
|
8214
|
+
delete ctx.__summarizationExhaustedAtToolCount;
|
|
8204
8215
|
const updated = await persistLatestSummary(syncPlanStateWith(ref, { ...currentState, ...delta, ctx }));
|
|
8205
8216
|
return { state: updated, rawMessages: [...updated.messages || currentRawMessages], compressed: true };
|
|
8206
8217
|
}
|