@cognipeer/agent-sdk 0.4.6 → 0.4.7

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
@@ -2912,6 +2912,24 @@ function getLastAssistantMessage(messages) {
2912
2912
  }
2913
2913
  return void 0;
2914
2914
  }
2915
+ function getActiveSummarizationThreshold(opts) {
2916
+ const summarization = opts.summarization;
2917
+ if (!summarization || typeof summarization !== "object" || summarization.enable === false) {
2918
+ return void 0;
2919
+ }
2920
+ return summarization.summaryTriggerTokens || summarization.maxTokens || opts.limits?.maxContextTokens || 5e4;
2921
+ }
2922
+ function clearNeedsSummarization(state) {
2923
+ if (!state.ctx?.__needsSummarization) {
2924
+ return state;
2925
+ }
2926
+ const nextCtx = { ...state.ctx || {} };
2927
+ delete nextCtx.__needsSummarization;
2928
+ return {
2929
+ ...state,
2930
+ ctx: Object.keys(nextCtx).length > 0 ? nextCtx : void 0
2931
+ };
2932
+ }
2915
2933
  function createAgent(opts) {
2916
2934
  const resolver = createResolverNode();
2917
2935
  const agentCore = createAgentCoreNode(opts);
@@ -2960,6 +2978,7 @@ function createAgent(opts) {
2960
2978
  outputSchema: opts.outputSchema,
2961
2979
  tracing: opts.tracing
2962
2980
  };
2981
+ const summarizationThreshold = getActiveSummarizationThreshold(opts);
2963
2982
  async function runLoop(initial, config, emit) {
2964
2983
  let state = await resolver(initial);
2965
2984
  if (state.ctx?.__paused) {
@@ -2976,6 +2995,9 @@ function createAgent(opts) {
2976
2995
  delete nextCtx.__resumeStage;
2977
2996
  state = { ...state, ctx: Object.keys(nextCtx).length > 0 ? nextCtx : void 0 };
2978
2997
  }
2998
+ if (summarizationThreshold === void 0) {
2999
+ state = clearNeedsSummarization(state);
3000
+ }
2979
3001
  const mergedLimits = {
2980
3002
  ...opts.limits || {},
2981
3003
  ...config?.limits || {}
@@ -3084,25 +3106,18 @@ function createAgent(opts) {
3084
3106
  }
3085
3107
  }
3086
3108
  }
3087
- let maxTok;
3088
- if (opts.summarization && typeof opts.summarization === "object") {
3089
- maxTok = opts.summarization.summaryTriggerTokens || opts.summarization.maxTokens;
3090
- }
3091
- if (maxTok === void 0) {
3092
- maxTok = 5e4;
3093
- }
3094
- if (maxTok) {
3109
+ if (summarizationThreshold !== void 0) {
3095
3110
  const tokCountMessages = (state.messages || []).filter((message) => {
3096
3111
  if (isSyntheticSummaryMessage(message)) return false;
3097
3112
  if (message.role === "system" && (message.name === "context_summary" || message.name === "memory_context")) return false;
3098
3113
  return true;
3099
3114
  });
3100
3115
  const tokenCount = countMessagesTokens(tokCountMessages);
3101
- if (tokenCount > maxTok) {
3116
+ if (tokenCount > summarizationThreshold) {
3102
3117
  const hasFreshSummary = (state.messages || []).some(
3103
3118
  (m) => m.role === "tool" && typeof m.content === "string" && (m.content === "SUMMARIZED" || m.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))
3104
3119
  );
3105
- if (hasFreshSummary && tokenCount <= maxTok * 1.15) ; else {
3120
+ if (hasFreshSummary && tokenCount <= summarizationThreshold * 1.15) ; else {
3106
3121
  const ctx = { ...state.ctx || {}, __needsSummarization: true };
3107
3122
  state = { ...state, ctx };
3108
3123
  break;