@cognipeer/agent-sdk 0.4.3 → 0.4.5

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 CHANGED
@@ -2191,7 +2191,7 @@ function resolveToolResponsePolicy(toolName, output, config) {
2191
2191
  const byTool = config.toolResponses.toolResponseRetentionByTool[toolName];
2192
2192
  let retentionPolicy = byTool || config.toolResponses.defaultPolicy;
2193
2193
  if (classification === "critical") {
2194
- retentionPolicy = byTool || "keep_structured";
2194
+ retentionPolicy = byTool || config.toolResponses.fallbackPolicy;
2195
2195
  } else if (classification === "verbose") {
2196
2196
  retentionPolicy = byTool || config.toolResponses.largeResponsePolicy;
2197
2197
  } else if (classification === "redundant") {
@@ -3085,7 +3085,7 @@ function createAgent(opts) {
3085
3085
  const tokenCount = countMessagesTokens(tokCountMessages);
3086
3086
  if (tokenCount > maxTok) {
3087
3087
  const hasFreshSummary = (state.messages || []).some(
3088
- (m) => m.role === "tool" && m.content === "SUMMARIZED"
3088
+ (m) => m.role === "tool" && typeof m.content === "string" && (m.content === "SUMMARIZED" || m.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))
3089
3089
  );
3090
3090
  if (hasFreshSummary && tokenCount <= maxTok * 1.15) ; else {
3091
3091
  const ctx = { ...state.ctx || {}, __needsSummarization: true };
@@ -3567,7 +3567,7 @@ function estimateContextRotScore(state) {
3567
3567
  const summaryCount = state.summaryRecords?.length || 0;
3568
3568
  const archivedCount = state.toolHistoryArchived?.length || 0;
3569
3569
  const messageCount = state.messages?.length || 0;
3570
- const summarizedMessages = state.messages?.filter((message) => message.role === "tool" && message.content === "SUMMARIZED").length || 0;
3570
+ const summarizedMessages = state.messages?.filter((message) => message.role === "tool" && typeof message.content === "string" && (message.content === "SUMMARIZED" || message.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))).length || 0;
3571
3571
  const numerator = summaryCount * 2 + archivedCount + summarizedMessages;
3572
3572
  const denominator = Math.max(messageCount, 1);
3573
3573
  return Number((numerator / denominator).toFixed(3));
@@ -3684,6 +3684,69 @@ function extractCanonicalFacts(messages) {
3684
3684
  }
3685
3685
  return [...facts.values()];
3686
3686
  }
3687
+ function flattenToolMessageContent(message) {
3688
+ if (typeof message.content === "string") {
3689
+ return message.content;
3690
+ }
3691
+ if (Array.isArray(message.content)) {
3692
+ return message.content.map((part) => typeof part === "string" ? part : part?.text ?? part?.content ?? String(part ?? "")).join(" ");
3693
+ }
3694
+ return "";
3695
+ }
3696
+ function isSummarizedToolPlaceholder(content) {
3697
+ return typeof content === "string" && (content === "SUMMARIZED" || content.startsWith("SUMMARIZED_TOOL_RESPONSE"));
3698
+ }
3699
+ function summarizeToolMessagePreview(message) {
3700
+ const preview = flattenToolMessageContent(message).trim();
3701
+ if (!preview) {
3702
+ return "No summary available.";
3703
+ }
3704
+ return preview.length > 240 ? `${preview.slice(0, 240)}...` : preview;
3705
+ }
3706
+ function findToolHistoryEntry(state, toolCallId, toolName) {
3707
+ const entries = [
3708
+ ...state.toolHistory || [],
3709
+ ...state.toolHistoryArchived || []
3710
+ ];
3711
+ if (toolCallId) {
3712
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
3713
+ const entry = entries[index];
3714
+ if (entry?.tool_call_id === toolCallId) {
3715
+ return entry;
3716
+ }
3717
+ }
3718
+ }
3719
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
3720
+ const entry = entries[index];
3721
+ if (entry?.toolName === toolName) {
3722
+ return entry;
3723
+ }
3724
+ }
3725
+ return void 0;
3726
+ }
3727
+ function buildSummarizedToolMessage(message, state) {
3728
+ const toolName = message.name || "unknown_tool";
3729
+ const toolCallId = message.tool_call_id;
3730
+ const historyEntry = findToolHistoryEntry(state, toolCallId, toolName);
3731
+ const executionId = historyEntry?.executionId;
3732
+ const summary = historyEntry?.summary || summarizeToolMessagePreview(message);
3733
+ const references = [
3734
+ `toolName=${toolName}`,
3735
+ toolCallId ? `toolCallId=${toolCallId}` : null,
3736
+ executionId ? `executionId=${executionId}` : null
3737
+ ].filter(Boolean).join("; ");
3738
+ let retrievalHint = "Use get_tool_response if exact details are still needed.";
3739
+ if (executionId && toolCallId) {
3740
+ retrievalHint = `Use get_tool_response with executionId "${executionId}" or tool call id "${toolCallId}" if exact details are still needed.`;
3741
+ } else if (executionId || toolCallId) {
3742
+ retrievalHint = `Use get_tool_response with executionId "${executionId || toolCallId}" if exact details are still needed.`;
3743
+ }
3744
+ return [
3745
+ `SUMMARIZED_TOOL_RESPONSE [${references}]`,
3746
+ `Summary: ${summary}`,
3747
+ retrievalHint
3748
+ ].join("\n");
3749
+ }
3687
3750
  function mergeStableFacts(summary, additionalFacts) {
3688
3751
  if (additionalFacts.length === 0) return summary;
3689
3752
  const merged = new Map(summary.stable_facts.map((fact) => [fact.key, fact]));
@@ -3742,7 +3805,7 @@ function createContextSummarizeNode(opts) {
3742
3805
  const onEvent = state.ctx?.__onEvent;
3743
3806
  const traceSession = state.ctx?.__traceSession;
3744
3807
  const compressableMessages = messages.filter(
3745
- (m) => m.role === "tool" && m.content !== "SUMMARIZED" && !isSyntheticSummaryToolMessage(m)
3808
+ (m) => m.role === "tool" && !isSummarizedToolPlaceholder(m.content) && !isSyntheticSummaryToolMessage(m)
3746
3809
  );
3747
3810
  if (compressableMessages.length === 0) {
3748
3811
  return {};
@@ -3907,10 +3970,10 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
3907
3970
  if (!toolCallId || !toolCallIdToAssistantIdx.has(toolCallId)) {
3908
3971
  return null;
3909
3972
  }
3910
- if (m.content === "SUMMARIZED") return m;
3973
+ if (isSummarizedToolPlaceholder(m.content)) return m;
3911
3974
  return {
3912
3975
  ...m,
3913
- content: "SUMMARIZED"
3976
+ content: buildSummarizedToolMessage(m, state)
3914
3977
  };
3915
3978
  }
3916
3979
  return m;
@@ -4220,8 +4283,8 @@ function createContextTools(stateRef, opts) {
4220
4283
  }
4221
4284
  const getTool = createTool({
4222
4285
  name: "get_tool_response",
4223
- description: "RETRIEVE the full output of a tool execution whose response was archived or dropped. When a tool response appears as 'ARCHIVED_TOOL_RESPONSE [executionId=xxx]' or 'DROPPED_TOOL_RESPONSE [executionId=xxx]', pass that exact executionId to this tool to get the complete data. You can also pass the tool_call_id from your original tool call.",
4224
- schema: zod.z.object({ executionId: zod.z.string().describe("Tool execution id") }),
4286
+ description: "RETRIEVE the full output of a tool execution whose response was archived, dropped, or later compacted in conversation history. When a tool response appears as 'ARCHIVED_TOOL_RESPONSE [executionId=xxx]', 'DROPPED_TOOL_RESPONSE [executionId=xxx]', or 'SUMMARIZED_TOOL_RESPONSE [...]', pass the referenced executionId to this tool to get the complete data. You can also pass the original tool_call_id from the tool call.",
4287
+ schema: zod.z.object({ executionId: zod.z.string().describe("Tool execution id or original tool_call_id") }),
4225
4288
  func: async ({ executionId }) => {
4226
4289
  const matchesExecution = (t) => t?.executionId === executionId || t?.tool_call_id === executionId;
4227
4290
  let execution = stateRef.toolHistory?.find((t) => matchesExecution(t));