@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.mjs CHANGED
@@ -2902,6 +2902,24 @@ function getLastAssistantMessage(messages) {
2902
2902
  }
2903
2903
  return void 0;
2904
2904
  }
2905
+ function getActiveSummarizationThreshold(opts) {
2906
+ const summarization = opts.summarization;
2907
+ if (!summarization || typeof summarization !== "object" || summarization.enable === false) {
2908
+ return void 0;
2909
+ }
2910
+ return summarization.summaryTriggerTokens || summarization.maxTokens || opts.limits?.maxContextTokens || 5e4;
2911
+ }
2912
+ function clearNeedsSummarization(state) {
2913
+ if (!state.ctx?.__needsSummarization) {
2914
+ return state;
2915
+ }
2916
+ const nextCtx = { ...state.ctx || {} };
2917
+ delete nextCtx.__needsSummarization;
2918
+ return {
2919
+ ...state,
2920
+ ctx: Object.keys(nextCtx).length > 0 ? nextCtx : void 0
2921
+ };
2922
+ }
2905
2923
  function createAgent(opts) {
2906
2924
  const resolver = createResolverNode();
2907
2925
  const agentCore = createAgentCoreNode(opts);
@@ -2950,6 +2968,7 @@ function createAgent(opts) {
2950
2968
  outputSchema: opts.outputSchema,
2951
2969
  tracing: opts.tracing
2952
2970
  };
2971
+ const summarizationThreshold = getActiveSummarizationThreshold(opts);
2953
2972
  async function runLoop(initial, config, emit) {
2954
2973
  let state = await resolver(initial);
2955
2974
  if (state.ctx?.__paused) {
@@ -2966,6 +2985,9 @@ function createAgent(opts) {
2966
2985
  delete nextCtx.__resumeStage;
2967
2986
  state = { ...state, ctx: Object.keys(nextCtx).length > 0 ? nextCtx : void 0 };
2968
2987
  }
2988
+ if (summarizationThreshold === void 0) {
2989
+ state = clearNeedsSummarization(state);
2990
+ }
2969
2991
  const mergedLimits = {
2970
2992
  ...opts.limits || {},
2971
2993
  ...config?.limits || {}
@@ -3074,25 +3096,18 @@ function createAgent(opts) {
3074
3096
  }
3075
3097
  }
3076
3098
  }
3077
- let maxTok;
3078
- if (opts.summarization && typeof opts.summarization === "object") {
3079
- maxTok = opts.summarization.summaryTriggerTokens || opts.summarization.maxTokens;
3080
- }
3081
- if (maxTok === void 0) {
3082
- maxTok = 5e4;
3083
- }
3084
- if (maxTok) {
3099
+ if (summarizationThreshold !== void 0) {
3085
3100
  const tokCountMessages = (state.messages || []).filter((message) => {
3086
3101
  if (isSyntheticSummaryMessage(message)) return false;
3087
3102
  if (message.role === "system" && (message.name === "context_summary" || message.name === "memory_context")) return false;
3088
3103
  return true;
3089
3104
  });
3090
3105
  const tokenCount = countMessagesTokens(tokCountMessages);
3091
- if (tokenCount > maxTok) {
3106
+ if (tokenCount > summarizationThreshold) {
3092
3107
  const hasFreshSummary = (state.messages || []).some(
3093
3108
  (m) => m.role === "tool" && typeof m.content === "string" && (m.content === "SUMMARIZED" || m.content.startsWith("SUMMARIZED_TOOL_RESPONSE"))
3094
3109
  );
3095
- if (hasFreshSummary && tokenCount <= maxTok * 1.15) ; else {
3110
+ if (hasFreshSummary && tokenCount <= summarizationThreshold * 1.15) ; else {
3096
3111
  const ctx = { ...state.ctx || {}, __needsSummarization: true };
3097
3112
  state = { ...state, ctx };
3098
3113
  break;