@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 +53 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +53 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -183,6 +183,8 @@ type SmartAgentOptions = {
|
|
|
183
183
|
summarization?: boolean | {
|
|
184
184
|
enable: boolean;
|
|
185
185
|
maxTokens: number;
|
|
186
|
+
/** Max tokens to include in the summarization prompt itself (to avoid sending huge context to summarizer). Default: 8000 */
|
|
187
|
+
summaryPromptMaxTokens?: number;
|
|
186
188
|
};
|
|
187
189
|
systemPrompt?: string;
|
|
188
190
|
useTodoList?: boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -183,6 +183,8 @@ type SmartAgentOptions = {
|
|
|
183
183
|
summarization?: boolean | {
|
|
184
184
|
enable: boolean;
|
|
185
185
|
maxTokens: number;
|
|
186
|
+
/** Max tokens to include in the summarization prompt itself (to avoid sending huge context to summarizer). Default: 8000 */
|
|
187
|
+
summaryPromptMaxTokens?: number;
|
|
186
188
|
};
|
|
187
189
|
systemPrompt?: string;
|
|
188
190
|
useTodoList?: boolean;
|
package/dist/index.mjs
CHANGED
|
@@ -1872,10 +1872,13 @@ function createAgent(opts) {
|
|
|
1872
1872
|
}
|
|
1873
1873
|
}
|
|
1874
1874
|
}
|
|
1875
|
-
let maxTok
|
|
1875
|
+
let maxTok;
|
|
1876
1876
|
if (opts.summarization && typeof opts.summarization === "object" && opts.summarization.maxTokens) {
|
|
1877
1877
|
maxTok = opts.summarization.maxTokens;
|
|
1878
1878
|
}
|
|
1879
|
+
if (maxTok === void 0) {
|
|
1880
|
+
maxTok = 5e4;
|
|
1881
|
+
}
|
|
1879
1882
|
if (maxTok) {
|
|
1880
1883
|
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");
|
|
1881
1884
|
const tokenCount = countApproxTokens(allText);
|
|
@@ -2228,12 +2231,16 @@ function getSummarizationConfig(opts) {
|
|
|
2228
2231
|
if (typeof opts.summarization === "object") {
|
|
2229
2232
|
return {
|
|
2230
2233
|
enabled: opts.summarization.enable !== false,
|
|
2231
|
-
maxTokens: opts.summarization.maxTokens
|
|
2234
|
+
maxTokens: opts.summarization.maxTokens ?? 5e4,
|
|
2235
|
+
// Max tokens we can send to the model for summarization prompt itself
|
|
2236
|
+
// This should be well under the model's context limit
|
|
2237
|
+
summaryPromptMaxTokens: opts.summarization.summaryPromptMaxTokens ?? 8e3
|
|
2232
2238
|
};
|
|
2233
2239
|
}
|
|
2234
2240
|
return {
|
|
2235
2241
|
enabled: opts.summarization !== false,
|
|
2236
|
-
maxTokens: 5e4
|
|
2242
|
+
maxTokens: 5e4,
|
|
2243
|
+
summaryPromptMaxTokens: 8e3
|
|
2237
2244
|
};
|
|
2238
2245
|
}
|
|
2239
2246
|
function createContextSummarizeNode(opts) {
|
|
@@ -2242,16 +2249,37 @@ function createContextSummarizeNode(opts) {
|
|
|
2242
2249
|
if (!config.enabled) return {};
|
|
2243
2250
|
const model = opts.model;
|
|
2244
2251
|
const messages = state.messages || [];
|
|
2245
|
-
const
|
|
2252
|
+
const compressableMessages = messages.filter((m) => m.role === "tool" && m.content !== "SUMMARIZED");
|
|
2253
|
+
if (compressableMessages.length === 0) {
|
|
2254
|
+
return {};
|
|
2255
|
+
}
|
|
2256
|
+
const summaryPromptMaxTokens = config.summaryPromptMaxTokens;
|
|
2257
|
+
const availableTokensForConversation = summaryPromptMaxTokens - 500;
|
|
2258
|
+
const messageTexts = [];
|
|
2259
|
+
let accumulatedTokens = 0;
|
|
2260
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
2261
|
+
const m = messages[i];
|
|
2246
2262
|
let content = "";
|
|
2247
2263
|
if (typeof m.content === "string") content = m.content;
|
|
2248
2264
|
else if (Array.isArray(m.content)) {
|
|
2249
2265
|
content = m.content.map((c) => c.text || JSON.stringify(c)).join(" ");
|
|
2250
2266
|
}
|
|
2267
|
+
const maxContentLength = 2e3;
|
|
2268
|
+
if (content.length > maxContentLength) {
|
|
2269
|
+
content = content.substring(0, maxContentLength) + "... [TRUNCATED]";
|
|
2270
|
+
}
|
|
2251
2271
|
let prefix = `${m.role.toUpperCase()}`;
|
|
2252
2272
|
if (m.name) prefix += ` (${m.name})`;
|
|
2253
|
-
|
|
2254
|
-
|
|
2273
|
+
const messageText = `${prefix}: ${content}`;
|
|
2274
|
+
const messageTokens = countApproxTokens(messageText);
|
|
2275
|
+
if (accumulatedTokens + messageTokens > availableTokensForConversation) {
|
|
2276
|
+
messageTexts.unshift("[Earlier conversation history omitted for brevity]");
|
|
2277
|
+
break;
|
|
2278
|
+
}
|
|
2279
|
+
messageTexts.unshift(messageText);
|
|
2280
|
+
accumulatedTokens += messageTokens;
|
|
2281
|
+
}
|
|
2282
|
+
const conversationText = messageTexts.join("\n\n");
|
|
2255
2283
|
const summaryPrompt = [
|
|
2256
2284
|
systemMessage("You are a helpful assistant that summarizes conversation history efficiently."),
|
|
2257
2285
|
humanMessage(
|
|
@@ -2404,10 +2432,16 @@ function createContextTools(stateRef, opts) {
|
|
|
2404
2432
|
|
|
2405
2433
|
// src/graph/decisions.ts
|
|
2406
2434
|
function needsSummarization(state, opts, summarizationEnabled) {
|
|
2407
|
-
|
|
2408
|
-
if (!maxTok || !summarizationEnabled) {
|
|
2435
|
+
if (!summarizationEnabled) {
|
|
2409
2436
|
return false;
|
|
2410
2437
|
}
|
|
2438
|
+
let maxTok;
|
|
2439
|
+
if (typeof opts.summarization === "object" && typeof opts.summarization.maxTokens === "number") {
|
|
2440
|
+
maxTok = opts.summarization.maxTokens;
|
|
2441
|
+
}
|
|
2442
|
+
if (maxTok === void 0) {
|
|
2443
|
+
maxTok = 5e4;
|
|
2444
|
+
}
|
|
2411
2445
|
try {
|
|
2412
2446
|
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");
|
|
2413
2447
|
const tokenCount = countApproxTokens(allText);
|
|
@@ -2474,8 +2508,10 @@ function createSmartAgent(opts) {
|
|
|
2474
2508
|
const next = summarizationEnabled ? decideBefore(state) : "agent";
|
|
2475
2509
|
if (next === "contextSummarize" && summarizer) {
|
|
2476
2510
|
const delta = await summarizer(state);
|
|
2477
|
-
|
|
2478
|
-
|
|
2511
|
+
if (!delta || Object.keys(delta).length === 0) ; else {
|
|
2512
|
+
state = { ...state, ...delta };
|
|
2513
|
+
continue;
|
|
2514
|
+
}
|
|
2479
2515
|
}
|
|
2480
2516
|
const res = await base.invoke(state, config);
|
|
2481
2517
|
lastResult = res;
|
|
@@ -2485,7 +2521,9 @@ function createSmartAgent(opts) {
|
|
|
2485
2521
|
delete ctx.__needsSummarization;
|
|
2486
2522
|
state = { ...state, ctx };
|
|
2487
2523
|
const delta = await summarizer(state);
|
|
2488
|
-
|
|
2524
|
+
if (delta && Object.keys(delta).length > 0) {
|
|
2525
|
+
state = { ...state, ...delta };
|
|
2526
|
+
}
|
|
2489
2527
|
continue;
|
|
2490
2528
|
}
|
|
2491
2529
|
if (state.ctx?.__finalizedDueToStructuredOutput) break;
|
|
@@ -2493,8 +2531,10 @@ function createSmartAgent(opts) {
|
|
|
2493
2531
|
const after = decideAfter(state);
|
|
2494
2532
|
if (after === "contextSummarize" && summarizer) {
|
|
2495
2533
|
const delta = await summarizer(state);
|
|
2496
|
-
|
|
2497
|
-
|
|
2534
|
+
if (delta && Object.keys(delta).length > 0) {
|
|
2535
|
+
state = { ...state, ...delta };
|
|
2536
|
+
continue;
|
|
2537
|
+
}
|
|
2498
2538
|
}
|
|
2499
2539
|
}
|
|
2500
2540
|
break;
|