@cognipeer/agent-sdk 0.2.0 → 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
@@ -1881,10 +1881,13 @@ function createAgent(opts) {
1881
1881
  }
1882
1882
  }
1883
1883
  }
1884
- let maxTok = 5e4;
1884
+ let maxTok;
1885
1885
  if (opts.summarization && typeof opts.summarization === "object" && opts.summarization.maxTokens) {
1886
1886
  maxTok = opts.summarization.maxTokens;
1887
1887
  }
1888
+ if (maxTok === void 0) {
1889
+ maxTok = 5e4;
1890
+ }
1888
1891
  if (maxTok) {
1889
1892
  const allText = (state.messages || []).map((m) => typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? "").join("") : "").join("\n");
1890
1893
  const tokenCount = countApproxTokens(allText);
@@ -2237,12 +2240,16 @@ function getSummarizationConfig(opts) {
2237
2240
  if (typeof opts.summarization === "object") {
2238
2241
  return {
2239
2242
  enabled: opts.summarization.enable !== false,
2240
- 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
2241
2247
  };
2242
2248
  }
2243
2249
  return {
2244
2250
  enabled: opts.summarization !== false,
2245
- maxTokens: 5e4
2251
+ maxTokens: 5e4,
2252
+ summaryPromptMaxTokens: 8e3
2246
2253
  };
2247
2254
  }
2248
2255
  function createContextSummarizeNode(opts) {
@@ -2251,16 +2258,37 @@ function createContextSummarizeNode(opts) {
2251
2258
  if (!config.enabled) return {};
2252
2259
  const model = opts.model;
2253
2260
  const messages = state.messages || [];
2254
- const conversationText = messages.map((m) => {
2261
+ const compressableMessages = messages.filter((m) => m.role === "tool" && m.content !== "SUMMARIZED");
2262
+ if (compressableMessages.length === 0) {
2263
+ return {};
2264
+ }
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];
2255
2271
  let content = "";
2256
2272
  if (typeof m.content === "string") content = m.content;
2257
2273
  else if (Array.isArray(m.content)) {
2258
2274
  content = m.content.map((c) => c.text || JSON.stringify(c)).join(" ");
2259
2275
  }
2276
+ const maxContentLength = 2e3;
2277
+ if (content.length > maxContentLength) {
2278
+ content = content.substring(0, maxContentLength) + "... [TRUNCATED]";
2279
+ }
2260
2280
  let prefix = `${m.role.toUpperCase()}`;
2261
2281
  if (m.name) prefix += ` (${m.name})`;
2262
- return `${prefix}: ${content}`;
2263
- }).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");
2264
2292
  const summaryPrompt = [
2265
2293
  systemMessage("You are a helpful assistant that summarizes conversation history efficiently."),
2266
2294
  humanMessage(
@@ -2413,10 +2441,16 @@ function createContextTools(stateRef, opts) {
2413
2441
 
2414
2442
  // src/graph/decisions.ts
2415
2443
  function needsSummarization(state, opts, summarizationEnabled) {
2416
- const maxTok = opts.limits?.maxToken ?? opts.limits?.contextTokenLimit;
2417
- if (!maxTok || !summarizationEnabled) {
2444
+ if (!summarizationEnabled) {
2418
2445
  return false;
2419
2446
  }
2447
+ let maxTok;
2448
+ if (typeof opts.summarization === "object" && typeof opts.summarization.maxTokens === "number") {
2449
+ maxTok = opts.summarization.maxTokens;
2450
+ }
2451
+ if (maxTok === void 0) {
2452
+ maxTok = 5e4;
2453
+ }
2420
2454
  try {
2421
2455
  const allText = (state.messages || []).map((m) => typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? "").join("") : "").join("\n");
2422
2456
  const tokenCount = countApproxTokens(allText);
@@ -2483,8 +2517,10 @@ function createSmartAgent(opts) {
2483
2517
  const next = summarizationEnabled ? decideBefore(state) : "agent";
2484
2518
  if (next === "contextSummarize" && summarizer) {
2485
2519
  const delta = await summarizer(state);
2486
- state = { ...state, ...delta };
2487
- continue;
2520
+ if (!delta || Object.keys(delta).length === 0) ; else {
2521
+ state = { ...state, ...delta };
2522
+ continue;
2523
+ }
2488
2524
  }
2489
2525
  const res = await base.invoke(state, config);
2490
2526
  lastResult = res;
@@ -2494,7 +2530,9 @@ function createSmartAgent(opts) {
2494
2530
  delete ctx.__needsSummarization;
2495
2531
  state = { ...state, ctx };
2496
2532
  const delta = await summarizer(state);
2497
- state = { ...state, ...delta };
2533
+ if (delta && Object.keys(delta).length > 0) {
2534
+ state = { ...state, ...delta };
2535
+ }
2498
2536
  continue;
2499
2537
  }
2500
2538
  if (state.ctx?.__finalizedDueToStructuredOutput) break;
@@ -2502,8 +2540,10 @@ function createSmartAgent(opts) {
2502
2540
  const after = decideAfter(state);
2503
2541
  if (after === "contextSummarize" && summarizer) {
2504
2542
  const delta = await summarizer(state);
2505
- state = { ...state, ...delta };
2506
- continue;
2543
+ if (delta && Object.keys(delta).length > 0) {
2544
+ state = { ...state, ...delta };
2545
+ continue;
2546
+ }
2507
2547
  }
2508
2548
  }
2509
2549
  break;