@economic/agents 1.6.4 → 1.6.5
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 +31 -14
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -604,7 +604,7 @@ function formatMessagesForSummary(messages) {
|
|
|
604
604
|
}
|
|
605
605
|
if (parts.length > 0) lines.push(`${roleLabel}: ${parts.join(" ")}`);
|
|
606
606
|
}
|
|
607
|
-
return lines.join("\n
|
|
607
|
+
return lines.join("\n");
|
|
608
608
|
}
|
|
609
609
|
/**
|
|
610
610
|
* Calls the model to produce a concise summary of old + recent message windows.
|
|
@@ -662,14 +662,37 @@ async function compactIfNeeded(messages, model, tailSize) {
|
|
|
662
662
|
}
|
|
663
663
|
//#endregion
|
|
664
664
|
//#region src/server/agent-chat/features/conversations/conversations.ts
|
|
665
|
+
/**
|
|
666
|
+
* Renders only the user- and assistant-visible text parts of a message
|
|
667
|
+
* array for conversation title/summary generation.
|
|
668
|
+
*
|
|
669
|
+
* Tool calls, tool results, system messages, and reasoning parts are
|
|
670
|
+
* intentionally excluded so the model receives a clean transcript.
|
|
671
|
+
*/
|
|
672
|
+
function formatMessagesForConversationSummary(messages) {
|
|
673
|
+
const lines = [];
|
|
674
|
+
for (const message of messages) {
|
|
675
|
+
if (message.role !== "user" && message.role !== "assistant") continue;
|
|
676
|
+
let text = "";
|
|
677
|
+
if (typeof message.content === "string") text = message.content.trim();
|
|
678
|
+
else text = message.content.filter((part) => part.type === "text").map((part) => part.text.trim()).filter(Boolean).join(" ");
|
|
679
|
+
if (!text) continue;
|
|
680
|
+
const roleLabel = message.role === "user" ? "User" : "Assistant";
|
|
681
|
+
lines.push(`${roleLabel}: ${text}`);
|
|
682
|
+
}
|
|
683
|
+
return lines.join("\n");
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Number of recent messages passed to `generateSummary` for rolling
|
|
687
|
+
* summarization. Keeping this bounded prevents the prompt growing
|
|
688
|
+
* unboundedly regardless of conversation length.
|
|
689
|
+
*/
|
|
690
|
+
const SUMMARY_CONTEXT_MESSAGES = 15;
|
|
665
691
|
async function recordConversationSummary(db, durableObjectName, messages, model) {
|
|
666
692
|
if (!await getConversationSummary(db, durableObjectName)) {
|
|
667
693
|
const { title, summary } = await generateTitleAndSummary(messages, model);
|
|
668
694
|
await upsertConversationSummary(db, durableObjectName, title, summary);
|
|
669
|
-
} else
|
|
670
|
-
await upsertConversationSummary(db, durableObjectName);
|
|
671
|
-
if (messages.length % SUMMARY_CONTEXT_MESSAGES === 0) await generateConversationSummary(db, durableObjectName, messages, model);
|
|
672
|
-
}
|
|
695
|
+
} else if (messages.length <= 7 || messages.length % SUMMARY_CONTEXT_MESSAGES === 0) await generateConversationSummary(db, durableObjectName, messages, model);
|
|
673
696
|
}
|
|
674
697
|
/**
|
|
675
698
|
* Records a conversation row in the `conversations` D1 table.
|
|
@@ -689,12 +712,6 @@ async function upsertConversationSummary(db, durableObjectName, title, summary)
|
|
|
689
712
|
updated_at = excluded.updated_at`).bind(durableObjectName, title ?? null, summary ?? null, now, now).run();
|
|
690
713
|
}
|
|
691
714
|
/**
|
|
692
|
-
* Number of recent messages passed to `generateSummary` for rolling
|
|
693
|
-
* summarization. Keeping this bounded prevents the prompt growing
|
|
694
|
-
* unboundedly regardless of conversation length.
|
|
695
|
-
*/
|
|
696
|
-
const SUMMARY_CONTEXT_MESSAGES = 30;
|
|
697
|
-
/**
|
|
698
715
|
* Returns the current `title` and `summary` for a conversation row,
|
|
699
716
|
* or `null` if the row does not exist yet.
|
|
700
717
|
*/
|
|
@@ -733,7 +750,7 @@ async function updateConversationSummary(db, durableObjectName, title, summary)
|
|
|
733
750
|
*/
|
|
734
751
|
async function generateTitleAndSummary(messages, model, existingSummary) {
|
|
735
752
|
const recentMessages = await convertToModelMessages(messages.slice(-SUMMARY_CONTEXT_MESSAGES));
|
|
736
|
-
const previousContext = existingSummary ? `Previous summary
|
|
753
|
+
const previousContext = existingSummary ? `Previous summary:\n${existingSummary}\n\nMost recent messages:` : "Conversation:";
|
|
737
754
|
const { output } = await generateText({
|
|
738
755
|
model,
|
|
739
756
|
output: Output.object({ schema: jsonSchema({
|
|
@@ -745,12 +762,12 @@ async function generateTitleAndSummary(messages, model, existingSummary) {
|
|
|
745
762
|
},
|
|
746
763
|
summary: {
|
|
747
764
|
type: "string",
|
|
748
|
-
description: "2
|
|
765
|
+
description: "1-2 short sentence summary of the conversation. If the conversation direction has changed from the previous summary, reflect the new direction."
|
|
749
766
|
}
|
|
750
767
|
},
|
|
751
768
|
required: ["title", "summary"]
|
|
752
769
|
}) }),
|
|
753
|
-
prompt: `${previousContext}\n\n${
|
|
770
|
+
prompt: `${previousContext}\n\n${formatMessagesForConversationSummary(recentMessages)}`
|
|
754
771
|
});
|
|
755
772
|
return output;
|
|
756
773
|
}
|