@townco/agent 0.1.46 → 0.1.48

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.
@@ -17,15 +17,25 @@ function createContextSnapshot(messageCount, timestamp, previousContext) {
17
17
  if (previousContext) {
18
18
  // Start with all messages from previous context
19
19
  messages.push(...previousContext.messages);
20
- // Find the highest pointer index in previous context
21
- let maxPointerIndex = -1;
22
- for (const entry of previousContext.messages) {
23
- if (entry.type === "pointer" && entry.index > maxPointerIndex) {
24
- maxPointerIndex = entry.index;
20
+ // Determine the starting index for new pointers
21
+ let startPointerIndex;
22
+ if (previousContext.compactedUpTo !== undefined) {
23
+ // If previous context was a compaction, start adding pointers
24
+ // for messages after the compaction boundary
25
+ startPointerIndex = previousContext.compactedUpTo + 1;
26
+ }
27
+ else {
28
+ // Find the highest pointer index in previous context
29
+ let maxPointerIndex = -1;
30
+ for (const entry of previousContext.messages) {
31
+ if (entry.type === "pointer" && entry.index > maxPointerIndex) {
32
+ maxPointerIndex = entry.index;
33
+ }
25
34
  }
35
+ startPointerIndex = maxPointerIndex + 1;
26
36
  }
27
37
  // Add pointers for any new messages since the previous context
28
- for (let i = maxPointerIndex + 1; i < messageCount; i++) {
38
+ for (let i = startPointerIndex; i < messageCount; i++) {
29
39
  messages.push({ type: "pointer", index: i });
30
40
  }
31
41
  }
@@ -294,6 +304,18 @@ export class AgentAcpAdapter {
294
304
  if (hookResult.newContextEntries.length > 0) {
295
305
  logger.info(`Appending ${hookResult.newContextEntries.length} new context entries from hooks`);
296
306
  session.context.push(...hookResult.newContextEntries);
307
+ // Save session immediately after hooks to persist compacted context
308
+ if (this.storage) {
309
+ try {
310
+ await this.storage.saveSession(params.sessionId, session.messages, session.context);
311
+ logger.info("Session saved after hook execution with new context entries");
312
+ }
313
+ catch (error) {
314
+ logger.error(`Failed to save session ${params.sessionId} after hook execution`, {
315
+ error: error instanceof Error ? error.message : String(error),
316
+ });
317
+ }
318
+ }
297
319
  }
298
320
  }
299
321
  // Resolve context to messages for agent invocation
@@ -44,6 +44,12 @@ export type ContextMessageEntry = MessagePointer | FullMessageEntry;
44
44
  export interface ContextEntry {
45
45
  timestamp: string;
46
46
  messages: ContextMessageEntry[];
47
+ /**
48
+ * When set, indicates this context entry represents a compaction
49
+ * and all messages up to and including this index have been
50
+ * compacted into the full message(s) in this entry
51
+ */
52
+ compactedUpTo?: number | undefined;
47
53
  }
48
54
  /**
49
55
  * Session metadata
@@ -55,6 +55,7 @@ const contextMessageEntrySchema = z.discriminatedUnion("type", [
55
55
  const contextEntrySchema = z.object({
56
56
  timestamp: z.string(),
57
57
  messages: z.array(contextMessageEntrySchema),
58
+ compactedUpTo: z.number().optional(),
58
59
  });
59
60
  const sessionMetadataSchema = z.object({
60
61
  createdAt: z.string(),
@@ -1,6 +1,6 @@
1
1
  import { type HookCallback } from "../types";
2
2
  /**
3
- * Placeholder compaction tool for testing hooks
4
- * Logs the context and returns null (no new context entry)
3
+ * Compaction tool that uses an LLM to summarize conversation history
4
+ * when context size reaches the configured threshold
5
5
  */
6
6
  export declare const compactionTool: HookCallback;
@@ -1,9 +1,11 @@
1
+ import { ChatAnthropic } from "@langchain/anthropic";
2
+ import { HumanMessage, SystemMessage } from "@langchain/core/messages";
1
3
  import { createLogger } from "@townco/core";
2
4
  import { createContextEntry, createFullMessageEntry, } from "../types";
3
5
  const logger = createLogger("compaction-tool");
4
6
  /**
5
- * Placeholder compaction tool for testing hooks
6
- * Logs the context and returns null (no new context entry)
7
+ * Compaction tool that uses an LLM to summarize conversation history
8
+ * when context size reaches the configured threshold
7
9
  */
8
10
  export const compactionTool = async (ctx) => {
9
11
  logger.info("Compaction tool triggered", {
@@ -14,29 +16,103 @@ export const compactionTool = async (ctx) => {
14
16
  totalMessages: ctx.session.messages.length,
15
17
  model: ctx.model,
16
18
  });
17
- // TODO: Implement actual compaction logic
18
- // Example: Create a new context entry with a summary message
19
- // const summaryEntry = createFullMessageEntry(
20
- // "assistant",
21
- // "Summary of previous conversation: ..."
22
- // );
23
- //
24
- // const newContextEntry = createContextEntry([summaryEntry]);
25
- //
26
- // return {
27
- // newContextEntry,
28
- // metadata: {
29
- // action: "compacted",
30
- // messagesRemoved: ctx.session.messages.length - 1,
31
- // tokensSaved: 1000,
32
- // },
33
- // };
34
- // For now, just return null (no new context entry)
35
- return {
36
- newContextEntry: null,
37
- metadata: {
38
- action: "placeholder",
39
- message: "Compaction tool called but no action taken (placeholder)",
40
- },
41
- };
19
+ try {
20
+ // Create the LLM client using the same model as the agent
21
+ const model = new ChatAnthropic({
22
+ model: ctx.model,
23
+ temperature: 0,
24
+ });
25
+ // Build the conversation history to compact
26
+ const messagesToCompact = ctx.session.messages;
27
+ // Convert session messages to text for context
28
+ const conversationText = messagesToCompact
29
+ .map((msg) => {
30
+ const textContent = msg.content
31
+ .filter((block) => block.type === "text")
32
+ .map((block) => block.text)
33
+ .join("\n");
34
+ return `${msg.role.toUpperCase()}:\n${textContent}`;
35
+ })
36
+ .join("\n\n---\n\n");
37
+ // Create system prompt for compaction
38
+ const systemPrompt = new SystemMessage("You are a helpful AI assistant tasked with summarizing conversations.");
39
+ // Create detailed compaction instructions with a generic, domain-agnostic approach
40
+ const userPrompt = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
41
+ This summary should be thorough in capturing important details, decisions, and context that would be essential for continuing the conversation/task without losing important information.
42
+
43
+ Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
44
+
45
+ 1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
46
+ - The user's explicit requests and intents
47
+ - Your approach to addressing the user's requests
48
+ - Key decisions and important concepts discussed
49
+ - Specific details that are important to remember
50
+ - Challenges encountered and how they were addressed
51
+ - Pay special attention to specific user feedback, especially if the user told you to do something differently
52
+ 2. Double-check for accuracy and completeness, addressing each required element thoroughly
53
+
54
+ Your summary should include the following sections:
55
+
56
+ 1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
57
+ 2. Key Topics and Concepts: List all important topics, concepts, and themes discussed in the conversation
58
+ 3. Important Details and Information: Document specific details, information, or content that was shared, examined, or created. Pay special attention to the most recent messages and include important details where applicable
59
+ 4. Challenges and Solutions: List any challenges, issues, or obstacles that came up and how they were addressed. Pay special attention to specific user feedback, especially if the user asked for a different approach
60
+ 5. Problem Solving: Document problems solved and any ongoing work or troubleshooting efforts
61
+ 6. All User Messages: List ALL user messages (excluding tool results). These are critical for understanding the user's feedback and changing intent
62
+ 7. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on
63
+ 8. Current Work: Describe in detail precisely what was being worked on immediately before this summary, paying special attention to the most recent messages from both user and assistant
64
+ 9. Optional Next Step: List the next step related to the most recent work. IMPORTANT: ensure this step is DIRECTLY in line with the user's most recent explicit requests and the task you were working on. If the last task was concluded, only list next steps if they are explicitly in line with the user's request. Do not start on tangential requests or old requests that were already completed without confirming with the user first.
65
+ If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
66
+
67
+ Here's the conversation to summarize:
68
+
69
+ ${conversationText}
70
+
71
+ Please provide your summary based on the conversation above, following this structure and ensuring precision and thoroughness in your response.`;
72
+ const userMessage = new HumanMessage(userPrompt);
73
+ // Invoke the LLM
74
+ logger.info("Invoking LLM for compaction summary");
75
+ const response = await model.invoke([systemPrompt, userMessage]);
76
+ // Extract the summary text from the response
77
+ const summaryText = typeof response.content === "string"
78
+ ? response.content
79
+ : Array.isArray(response.content)
80
+ ? response.content
81
+ .filter((block) => typeof block === "object" && block !== null && "text" in block)
82
+ .map((block) => block.text)
83
+ .join("\n")
84
+ : "Failed to extract summary";
85
+ logger.info("Generated compaction summary", {
86
+ originalMessages: messagesToCompact.length,
87
+ summaryLength: summaryText.length,
88
+ estimatedTokensSaved: Math.round(ctx.currentTokens * 0.7),
89
+ });
90
+ // Create a new context entry with the summary
91
+ const summaryEntry = createFullMessageEntry("user", `This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:\n${summaryText}`);
92
+ // Set compactedUpTo to indicate all messages have been compacted into the summary
93
+ const lastMessageIndex = messagesToCompact.length - 1;
94
+ const newContextEntry = createContextEntry([summaryEntry], undefined, lastMessageIndex);
95
+ return {
96
+ newContextEntry,
97
+ metadata: {
98
+ action: "compacted",
99
+ messagesRemoved: messagesToCompact.length - 1,
100
+ tokensSaved: Math.round(ctx.currentTokens * 0.7),
101
+ summaryGenerated: true,
102
+ },
103
+ };
104
+ }
105
+ catch (error) {
106
+ logger.error("Compaction tool error", {
107
+ error: error instanceof Error ? error.message : String(error),
108
+ stack: error instanceof Error ? error.stack : undefined,
109
+ });
110
+ return {
111
+ newContextEntry: null,
112
+ metadata: {
113
+ action: "failed",
114
+ error: error instanceof Error ? error.message : String(error),
115
+ },
116
+ };
117
+ }
42
118
  };
@@ -106,7 +106,7 @@ export declare function createContextEntry(messages: Array<{
106
106
  } | {
107
107
  type: "full";
108
108
  message: SessionMessage;
109
- }>, timestamp?: string): ContextEntry;
109
+ }>, timestamp?: string, compactedUpTo?: number): ContextEntry;
110
110
  /**
111
111
  * Helper function to create a full message entry for context
112
112
  * Use this when hooks need to inject new messages into context
@@ -2,11 +2,15 @@
2
2
  * Helper function to create a new context entry
3
3
  * Use this when hooks want to create a new context snapshot
4
4
  */
5
- export function createContextEntry(messages, timestamp) {
6
- return {
5
+ export function createContextEntry(messages, timestamp, compactedUpTo) {
6
+ const entry = {
7
7
  timestamp: timestamp || new Date().toISOString(),
8
8
  messages,
9
9
  };
10
+ if (compactedUpTo !== undefined) {
11
+ entry.compactedUpTo = compactedUpTo;
12
+ }
13
+ return entry;
10
14
  }
11
15
  /**
12
16
  * Helper function to create a full message entry for context