@cognipeer/agent-sdk 0.2.1 → 0.2.2

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
@@ -2240,12 +2240,16 @@ function getSummarizationConfig(opts) {
2240
2240
  if (typeof opts.summarization === "object") {
2241
2241
  return {
2242
2242
  enabled: opts.summarization.enable !== false,
2243
- maxTokens: opts.summarization.maxTokens
2243
+ maxTokens: opts.summarization.maxTokens ?? 5e4,
2244
+ // Max tokens we can send to the model for summarization prompt itself
2245
+ // This should be well under the model's context limit
2246
+ summaryPromptMaxTokens: opts.summarization.summaryPromptMaxTokens ?? 8e3
2244
2247
  };
2245
2248
  }
2246
2249
  return {
2247
2250
  enabled: opts.summarization !== false,
2248
- maxTokens: 5e4
2251
+ maxTokens: 5e4,
2252
+ summaryPromptMaxTokens: 8e3
2249
2253
  };
2250
2254
  }
2251
2255
  function createContextSummarizeNode(opts) {
@@ -2258,16 +2262,33 @@ function createContextSummarizeNode(opts) {
2258
2262
  if (compressableMessages.length === 0) {
2259
2263
  return {};
2260
2264
  }
2261
- const conversationText = messages.map((m) => {
2265
+ const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
2266
+ const availableTokensForConversation = summaryPromptMaxTokens - 500;
2267
+ const messageTexts = [];
2268
+ let accumulatedTokens = 0;
2269
+ for (let i = messages.length - 1; i >= 0; i--) {
2270
+ const m = messages[i];
2262
2271
  let content = "";
2263
2272
  if (typeof m.content === "string") content = m.content;
2264
2273
  else if (Array.isArray(m.content)) {
2265
2274
  content = m.content.map((c) => c.text || JSON.stringify(c)).join(" ");
2266
2275
  }
2276
+ const maxContentLength = 2e3;
2277
+ if (content.length > maxContentLength) {
2278
+ content = content.substring(0, maxContentLength) + "... [TRUNCATED]";
2279
+ }
2267
2280
  let prefix = `${m.role.toUpperCase()}`;
2268
2281
  if (m.name) prefix += ` (${m.name})`;
2269
- return `${prefix}: ${content}`;
2270
- }).join("\n\n");
2282
+ const messageText = `${prefix}: ${content}`;
2283
+ const messageTokens = countApproxTokens(messageText);
2284
+ if (accumulatedTokens + messageTokens > availableTokensForConversation) {
2285
+ messageTexts.unshift("[Earlier conversation history omitted for brevity]");
2286
+ break;
2287
+ }
2288
+ messageTexts.unshift(messageText);
2289
+ accumulatedTokens += messageTokens;
2290
+ }
2291
+ const conversationText = messageTexts.join("\n\n");
2271
2292
  const summaryPrompt = [
2272
2293
  systemMessage("You are a helpful assistant that summarizes conversation history efficiently."),
2273
2294
  humanMessage(