@cognipeer/agent-sdk 0.4.4 → 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.mjs CHANGED
@@ -2181,7 +2181,7 @@ function resolveToolResponsePolicy(toolName, output, config) {
2181
2181
  const byTool = config.toolResponses.toolResponseRetentionByTool[toolName];
2182
2182
  let retentionPolicy = byTool || config.toolResponses.defaultPolicy;
2183
2183
  if (classification === "critical") {
2184
- retentionPolicy = byTool || "keep_structured";
2184
+ retentionPolicy = byTool || config.toolResponses.fallbackPolicy;
2185
2185
  } else if (classification === "verbose") {
2186
2186
  retentionPolicy = byTool || config.toolResponses.largeResponsePolicy;
2187
2187
  } else if (classification === "redundant") {
@@ -3075,7 +3075,7 @@ function createAgent(opts) {
3075
3075
  const tokenCount = countMessagesTokens(tokCountMessages);
3076
3076
  if (tokenCount > maxTok) {
3077
3077
  const hasFreshSummary = (state.messages || []).some(
3078
- (m) => m.role === "tool" && m.content === "SUMMARIZED"
3078
+ (m) => m.role === "tool" && typeof m.content === "string" && (m.content === "SUMMARIZED" || m.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))
3079
3079
  );
3080
3080
  if (hasFreshSummary && tokenCount <= maxTok * 1.15) ; else {
3081
3081
  const ctx = { ...state.ctx || {}, __needsSummarization: true };
@@ -3557,7 +3557,7 @@ function estimateContextRotScore(state) {
3557
3557
  const summaryCount = state.summaryRecords?.length || 0;
3558
3558
  const archivedCount = state.toolHistoryArchived?.length || 0;
3559
3559
  const messageCount = state.messages?.length || 0;
3560
- const summarizedMessages = state.messages?.filter((message) => message.role === "tool" && message.content === "SUMMARIZED").length || 0;
3560
+ const summarizedMessages = state.messages?.filter((message) => message.role === "tool" && typeof message.content === "string" && (message.content === "SUMMARIZED" || message.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))).length || 0;
3561
3561
  const numerator = summaryCount * 2 + archivedCount + summarizedMessages;
3562
3562
  const denominator = Math.max(messageCount, 1);
3563
3563
  return Number((numerator / denominator).toFixed(3));
@@ -3674,6 +3674,69 @@ function extractCanonicalFacts(messages) {
3674
3674
  }
3675
3675
  return [...facts.values()];
3676
3676
  }
3677
+ function flattenToolMessageContent(message) {
3678
+ if (typeof message.content === "string") {
3679
+ return message.content;
3680
+ }
3681
+ if (Array.isArray(message.content)) {
3682
+ return message.content.map((part) => typeof part === "string" ? part : part?.text ?? part?.content ?? String(part ?? "")).join(" ");
3683
+ }
3684
+ return "";
3685
+ }
3686
+ function isSummarizedToolPlaceholder(content) {
3687
+ return typeof content === "string" && (content === "SUMMARIZED" || content.startsWith("SUMMARIZED_TOOL_RESPONSE"));
3688
+ }
3689
+ function summarizeToolMessagePreview(message) {
3690
+ const preview = flattenToolMessageContent(message).trim();
3691
+ if (!preview) {
3692
+ return "No summary available.";
3693
+ }
3694
+ return preview.length > 240 ? `${preview.slice(0, 240)}...` : preview;
3695
+ }
3696
+ function findToolHistoryEntry(state, toolCallId, toolName) {
3697
+ const entries = [
3698
+ ...state.toolHistory || [],
3699
+ ...state.toolHistoryArchived || []
3700
+ ];
3701
+ if (toolCallId) {
3702
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
3703
+ const entry = entries[index];
3704
+ if (entry?.tool_call_id === toolCallId) {
3705
+ return entry;
3706
+ }
3707
+ }
3708
+ }
3709
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
3710
+ const entry = entries[index];
3711
+ if (entry?.toolName === toolName) {
3712
+ return entry;
3713
+ }
3714
+ }
3715
+ return void 0;
3716
+ }
3717
+ function buildSummarizedToolMessage(message, state) {
3718
+ const toolName = message.name || "unknown_tool";
3719
+ const toolCallId = message.tool_call_id;
3720
+ const historyEntry = findToolHistoryEntry(state, toolCallId, toolName);
3721
+ const executionId = historyEntry?.executionId;
3722
+ const summary = historyEntry?.summary || summarizeToolMessagePreview(message);
3723
+ const references = [
3724
+ `toolName=${toolName}`,
3725
+ toolCallId ? `toolCallId=${toolCallId}` : null,
3726
+ executionId ? `executionId=${executionId}` : null
3727
+ ].filter(Boolean).join("; ");
3728
+ let retrievalHint = "Use get_tool_response if exact details are still needed.";
3729
+ if (executionId && toolCallId) {
3730
+ retrievalHint = `Use get_tool_response with executionId "${executionId}" or tool call id "${toolCallId}" if exact details are still needed.`;
3731
+ } else if (executionId || toolCallId) {
3732
+ retrievalHint = `Use get_tool_response with executionId "${executionId || toolCallId}" if exact details are still needed.`;
3733
+ }
3734
+ return [
3735
+ `SUMMARIZED_TOOL_RESPONSE [${references}]`,
3736
+ `Summary: ${summary}`,
3737
+ retrievalHint
3738
+ ].join("\n");
3739
+ }
3677
3740
  function mergeStableFacts(summary, additionalFacts) {
3678
3741
  if (additionalFacts.length === 0) return summary;
3679
3742
  const merged = new Map(summary.stable_facts.map((fact) => [fact.key, fact]));
@@ -3732,7 +3795,7 @@ function createContextSummarizeNode(opts) {
3732
3795
  const onEvent = state.ctx?.__onEvent;
3733
3796
  const traceSession = state.ctx?.__traceSession;
3734
3797
  const compressableMessages = messages.filter(
3735
- (m) => m.role === "tool" && m.content !== "SUMMARIZED" && !isSyntheticSummaryToolMessage(m)
3798
+ (m) => m.role === "tool" && !isSummarizedToolPlaceholder(m.content) && !isSyntheticSummaryToolMessage(m)
3736
3799
  );
3737
3800
  if (compressableMessages.length === 0) {
3738
3801
  return {};
@@ -3897,10 +3960,10 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
3897
3960
  if (!toolCallId || !toolCallIdToAssistantIdx.has(toolCallId)) {
3898
3961
  return null;
3899
3962
  }
3900
- if (m.content === "SUMMARIZED") return m;
3963
+ if (isSummarizedToolPlaceholder(m.content)) return m;
3901
3964
  return {
3902
3965
  ...m,
3903
- content: "SUMMARIZED"
3966
+ content: buildSummarizedToolMessage(m, state)
3904
3967
  };
3905
3968
  }
3906
3969
  return m;
@@ -4210,8 +4273,8 @@ function createContextTools(stateRef, opts) {
4210
4273
  }
4211
4274
  const getTool = createTool({
4212
4275
  name: "get_tool_response",
4213
- 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.",
4214
- schema: z.object({ executionId: z.string().describe("Tool execution id") }),
4276
+ 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.",
4277
+ schema: z.object({ executionId: z.string().describe("Tool execution id or original tool_call_id") }),
4215
4278
  func: async ({ executionId }) => {
4216
4279
  const matchesExecution = (t) => t?.executionId === executionId || t?.tool_call_id === executionId;
4217
4280
  let execution = stateRef.toolHistory?.find((t) => matchesExecution(t));