@townco/agent 0.1.50 → 0.1.52

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.
Files changed (60) hide show
  1. package/dist/acp-server/adapter.d.ts +10 -0
  2. package/dist/acp-server/adapter.js +287 -80
  3. package/dist/acp-server/cli.d.ts +1 -3
  4. package/dist/acp-server/http.js +8 -1
  5. package/dist/acp-server/index.js +5 -0
  6. package/dist/acp-server/session-storage.d.ts +17 -3
  7. package/dist/acp-server/session-storage.js +9 -0
  8. package/dist/bin.js +0 -0
  9. package/dist/check-jaeger.d.ts +5 -0
  10. package/dist/check-jaeger.js +82 -0
  11. package/dist/definition/index.d.ts +16 -4
  12. package/dist/definition/index.js +17 -4
  13. package/dist/index.js +1 -1
  14. package/dist/run-subagents.d.ts +9 -0
  15. package/dist/run-subagents.js +110 -0
  16. package/dist/runner/agent-runner.d.ts +10 -2
  17. package/dist/runner/agent-runner.js +4 -0
  18. package/dist/runner/hooks/executor.d.ts +17 -0
  19. package/dist/runner/hooks/executor.js +66 -0
  20. package/dist/runner/hooks/predefined/compaction-tool.js +9 -1
  21. package/dist/runner/hooks/predefined/tool-response-compactor.d.ts +6 -0
  22. package/dist/runner/hooks/predefined/tool-response-compactor.js +461 -0
  23. package/dist/runner/hooks/registry.js +2 -0
  24. package/dist/runner/hooks/types.d.ts +39 -3
  25. package/dist/runner/hooks/types.js +9 -4
  26. package/dist/runner/index.d.ts +1 -3
  27. package/dist/runner/langchain/custom-stream-types.d.ts +36 -0
  28. package/dist/runner/langchain/custom-stream-types.js +23 -0
  29. package/dist/runner/langchain/index.js +102 -76
  30. package/dist/runner/langchain/otel-callbacks.js +67 -1
  31. package/dist/runner/langchain/tools/bash.d.ts +14 -0
  32. package/dist/runner/langchain/tools/bash.js +135 -0
  33. package/dist/scaffold/link-local.d.ts +1 -0
  34. package/dist/scaffold/link-local.js +54 -0
  35. package/dist/scaffold/project-scaffold.js +1 -0
  36. package/dist/telemetry/setup.d.ts +3 -1
  37. package/dist/telemetry/setup.js +33 -3
  38. package/dist/templates/index.d.ts +7 -0
  39. package/dist/test-telemetry.d.ts +5 -0
  40. package/dist/test-telemetry.js +88 -0
  41. package/dist/tsconfig.tsbuildinfo +1 -1
  42. package/dist/utils/context-size-calculator.d.ts +29 -0
  43. package/dist/utils/context-size-calculator.js +78 -0
  44. package/dist/utils/index.d.ts +2 -0
  45. package/dist/utils/index.js +2 -0
  46. package/dist/utils/token-counter.d.ts +19 -0
  47. package/dist/utils/token-counter.js +44 -0
  48. package/index.ts +1 -1
  49. package/package.json +7 -6
  50. package/templates/index.ts +18 -6
  51. package/dist/definition/mcp.d.ts +0 -0
  52. package/dist/definition/mcp.js +0 -0
  53. package/dist/definition/tools/todo.d.ts +0 -49
  54. package/dist/definition/tools/todo.js +0 -80
  55. package/dist/definition/tools/web_search.d.ts +0 -4
  56. package/dist/definition/tools/web_search.js +0 -26
  57. package/dist/dev-agent/index.d.ts +0 -2
  58. package/dist/dev-agent/index.js +0 -18
  59. package/dist/example.d.ts +0 -2
  60. package/dist/example.js +0 -19
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Context size calculation utilities
3
+ * Calculates full context size by counting ALL tokens in messages
4
+ */
5
+ import type { SessionMessage } from "../acp-server/session-storage.js";
6
+ export interface ContextSize {
7
+ systemPromptTokens: number;
8
+ userMessagesTokens: number;
9
+ assistantMessagesTokens: number;
10
+ toolInputTokens: number;
11
+ toolResultsTokens: number;
12
+ totalEstimated: number;
13
+ llmReportedInputTokens?: number | undefined;
14
+ }
15
+ /**
16
+ * Calculate the full context size by counting ALL tokens in the provided messages.
17
+ * This should be called every time a new context entry is created.
18
+ *
19
+ * How LLM-reported tokens work:
20
+ * - The LLM API returns `usage_metadata.input_tokens` which is the ACTUAL token
21
+ * count for EVERYTHING sent to the API: system prompt, tool declarations,
22
+ * all messages, and all tool results
23
+ * - We pass this as `llmReportedTokens` for comparison with our estimate
24
+ * - This helps us validate the accuracy of our tokenizer estimates
25
+ * - Tool declarations are NOT counted separately in our estimate since they're
26
+ * included in the LLM-reported value
27
+ */
28
+ export declare function calculateContextSize(messages: SessionMessage[], // Resolved messages from context entry
29
+ systemPrompt?: string, llmReportedTokens?: number): ContextSize;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Context size calculation utilities
3
+ * Calculates full context size by counting ALL tokens in messages
4
+ */
5
+ import { countTokens, countToolResultTokens } from "./token-counter.js";
6
+ /**
7
+ * Extract and count tokens from a content block based on its type
8
+ */
9
+ function countContentBlock(block) {
10
+ if (block.type === "text") {
11
+ return {
12
+ textTokens: countTokens(block.text),
13
+ toolInputTokens: 0,
14
+ toolResultTokens: 0,
15
+ };
16
+ }
17
+ if (block.type === "tool_call") {
18
+ return {
19
+ textTokens: 0,
20
+ toolInputTokens: block.rawInput
21
+ ? countToolResultTokens(block.rawInput)
22
+ : 0,
23
+ toolResultTokens: block.rawOutput
24
+ ? countToolResultTokens(block.rawOutput)
25
+ : 0,
26
+ };
27
+ }
28
+ return { textTokens: 0, toolInputTokens: 0, toolResultTokens: 0 };
29
+ }
30
+ /**
31
+ * Calculate the full context size by counting ALL tokens in the provided messages.
32
+ * This should be called every time a new context entry is created.
33
+ *
34
+ * How LLM-reported tokens work:
35
+ * - The LLM API returns `usage_metadata.input_tokens` which is the ACTUAL token
36
+ * count for EVERYTHING sent to the API: system prompt, tool declarations,
37
+ * all messages, and all tool results
38
+ * - We pass this as `llmReportedTokens` for comparison with our estimate
39
+ * - This helps us validate the accuracy of our tokenizer estimates
40
+ * - Tool declarations are NOT counted separately in our estimate since they're
41
+ * included in the LLM-reported value
42
+ */
43
+ export function calculateContextSize(messages, // Resolved messages from context entry
44
+ systemPrompt, llmReportedTokens) {
45
+ const systemPromptTokens = systemPrompt ? countTokens(systemPrompt) : 0;
46
+ let userMessagesTokens = 0;
47
+ let assistantMessagesTokens = 0;
48
+ let toolInputTokens = 0;
49
+ let toolResultsTokens = 0;
50
+ // Go through ALL messages in this context snapshot
51
+ for (const message of messages) {
52
+ for (const block of message.content) {
53
+ const counts = countContentBlock(block);
54
+ // Accumulate based on message role
55
+ if (message.role === "user") {
56
+ userMessagesTokens += counts.textTokens;
57
+ }
58
+ else if (message.role === "assistant") {
59
+ assistantMessagesTokens += counts.textTokens;
60
+ toolInputTokens += counts.toolInputTokens;
61
+ toolResultsTokens += counts.toolResultTokens;
62
+ }
63
+ }
64
+ }
65
+ return {
66
+ systemPromptTokens,
67
+ userMessagesTokens,
68
+ assistantMessagesTokens,
69
+ toolInputTokens,
70
+ toolResultsTokens,
71
+ totalEstimated: systemPromptTokens +
72
+ userMessagesTokens +
73
+ assistantMessagesTokens +
74
+ toolInputTokens +
75
+ toolResultsTokens,
76
+ llmReportedInputTokens: llmReportedTokens,
77
+ };
78
+ }
@@ -1,2 +1,4 @@
1
1
  export { makeSubagentsTool } from "../runner/langchain/tools/subagent.js";
2
+ export * from "./context-size-calculator.js";
3
+ export * from "./token-counter.js";
2
4
  export * from "./tool.js";
@@ -1,2 +1,4 @@
1
1
  export { makeSubagentsTool } from "../runner/langchain/tools/subagent.js";
2
+ export * from "./context-size-calculator.js";
3
+ export * from "./token-counter.js";
2
4
  export * from "./tool.js";
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Token counting utilities for tracking context size
3
+ * Uses Anthropic's tokenizer for rough approximation
4
+ */
5
+ /**
6
+ * Count tokens in a string using Anthropic's tokenizer
7
+ * Note: This is a rough approximation for Claude 3+ models
8
+ * For exact counts, use the API's usage_metadata where available
9
+ */
10
+ export declare function countTokens(text: string): number;
11
+ /**
12
+ * Count tokens in a tool result (rawOutput)
13
+ * Handles various data types that might be in tool outputs
14
+ */
15
+ export declare function countToolResultTokens(rawOutput: Record<string, unknown> | undefined): number;
16
+ /**
17
+ * Count tokens in multiple tool results
18
+ */
19
+ export declare function countMultipleToolResults(results: Array<Record<string, unknown> | undefined>): number;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Token counting utilities for tracking context size
3
+ * Uses Anthropic's tokenizer for rough approximation
4
+ */
5
+ import { countTokens as anthropicCountTokens } from "@anthropic-ai/tokenizer";
6
+ /**
7
+ * Count tokens in a string using Anthropic's tokenizer
8
+ * Note: This is a rough approximation for Claude 3+ models
9
+ * For exact counts, use the API's usage_metadata where available
10
+ */
11
+ export function countTokens(text) {
12
+ try {
13
+ return anthropicCountTokens(text);
14
+ }
15
+ catch (error) {
16
+ // Fallback to rough estimation if tokenizer fails
17
+ // Approximate: ~4 characters per token
18
+ return Math.ceil(text.length / 4);
19
+ }
20
+ }
21
+ /**
22
+ * Count tokens in a tool result (rawOutput)
23
+ * Handles various data types that might be in tool outputs
24
+ */
25
+ export function countToolResultTokens(rawOutput) {
26
+ if (!rawOutput) {
27
+ return 0;
28
+ }
29
+ try {
30
+ // Convert to string representation (as it would be sent to the LLM)
31
+ const text = JSON.stringify(rawOutput);
32
+ return countTokens(text);
33
+ }
34
+ catch (error) {
35
+ // If JSON.stringify fails, return 0
36
+ return 0;
37
+ }
38
+ }
39
+ /**
40
+ * Count tokens in multiple tool results
41
+ */
42
+ export function countMultipleToolResults(results) {
43
+ return results.reduce((total, result) => total + countToolResultTokens(result), 0);
44
+ }
package/index.ts CHANGED
@@ -45,7 +45,7 @@ const exampleAgent: AgentDefinition = {
45
45
  {
46
46
  type: "context_size",
47
47
  setting: {
48
- threshold: 95,
48
+ threshold: 80,
49
49
  },
50
50
  callback: "compaction_tool",
51
51
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@townco/agent",
3
- "version": "0.1.50",
3
+ "version": "0.1.52",
4
4
  "type": "module",
5
5
  "module": "index.ts",
6
6
  "files": [
@@ -55,6 +55,7 @@
55
55
  "@agentclientprotocol/sdk": "^0.5.1",
56
56
  "@anthropic-ai/sandbox-runtime": "^0.0.2",
57
57
  "@anthropic-ai/sdk": "^0.70.0",
58
+ "@anthropic-ai/tokenizer": "^0.0.4",
58
59
  "@electric-sql/pglite": "^0.2.15",
59
60
  "@langchain/anthropic": "1.0.1",
60
61
  "@langchain/core": "^1.0.3",
@@ -72,11 +73,11 @@
72
73
  "@opentelemetry/sdk-trace-base": "^1.28.0",
73
74
  "@opentelemetry/sdk-trace-node": "^1.28.0",
74
75
  "@opentelemetry/semantic-conventions": "^1.28.0",
75
- "@townco/core": "0.0.23",
76
- "@townco/gui-template": "0.1.42",
77
- "@townco/tui-template": "0.1.42",
78
- "@townco/tsconfig": "0.1.42",
79
- "@townco/ui": "0.1.45",
76
+ "@townco/core": "0.0.25",
77
+ "@townco/gui-template": "0.1.44",
78
+ "@townco/tsconfig": "0.1.44",
79
+ "@townco/tui-template": "0.1.44",
80
+ "@townco/ui": "0.1.47",
80
81
  "exa-js": "^2.0.0",
81
82
  "hono": "^4.10.4",
82
83
  "langchain": "^1.0.3",
@@ -20,11 +20,23 @@ export interface TemplateVars {
20
20
  systemPrompt: string | null;
21
21
  hasWebSearch: boolean;
22
22
  hooks?:
23
- | Array<{
24
- type: "context_size";
25
- setting?: { threshold: number } | undefined;
26
- callback: string;
27
- }>
23
+ | Array<
24
+ | {
25
+ type: "context_size";
26
+ setting?: { threshold: number } | undefined;
27
+ callback: string;
28
+ }
29
+ | {
30
+ type: "tool_response";
31
+ setting?:
32
+ | {
33
+ maxContextThreshold?: number | undefined;
34
+ responseTruncationThreshold?: number | undefined;
35
+ }
36
+ | undefined;
37
+ callback: string;
38
+ }
39
+ >
28
40
  | undefined;
29
41
  }
30
42
 
@@ -41,7 +53,7 @@ export function getTemplateVars(
41
53
  hasWebSearch: tools.some(
42
54
  (tool) => typeof tool === "string" && tool === "web_search",
43
55
  ),
44
- hooks: definition.hooks,
56
+ hooks: definition.hooks as TemplateVars["hooks"],
45
57
  };
46
58
  }
47
59
 
File without changes
File without changes
@@ -1,49 +0,0 @@
1
- import { z } from "zod";
2
- export declare const todoItemSchema: z.ZodObject<
3
- {
4
- content: z.ZodString;
5
- status: z.ZodEnum<{
6
- pending: "pending";
7
- in_progress: "in_progress";
8
- completed: "completed";
9
- }>;
10
- activeForm: z.ZodString;
11
- },
12
- z.core.$strip
13
- >;
14
- export declare const todoWrite: import("langchain").DynamicStructuredTool<
15
- z.ZodObject<
16
- {
17
- todos: z.ZodArray<
18
- z.ZodObject<
19
- {
20
- content: z.ZodString;
21
- status: z.ZodEnum<{
22
- pending: "pending";
23
- in_progress: "in_progress";
24
- completed: "completed";
25
- }>;
26
- activeForm: z.ZodString;
27
- },
28
- z.core.$strip
29
- >
30
- >;
31
- },
32
- z.core.$strip
33
- >,
34
- {
35
- todos: {
36
- content: string;
37
- status: "pending" | "in_progress" | "completed";
38
- activeForm: string;
39
- }[];
40
- },
41
- {
42
- todos: {
43
- content: string;
44
- status: "pending" | "in_progress" | "completed";
45
- activeForm: string;
46
- }[];
47
- },
48
- string
49
- >;
@@ -1,80 +0,0 @@
1
- import { tool } from "langchain";
2
- import { z } from "zod";
3
- export const todoItemSchema = z.object({
4
- content: z.string().min(1),
5
- status: z.enum(["pending", "in_progress", "completed"]),
6
- activeForm: z.string().min(1),
7
- });
8
- export const todoWrite = tool(
9
- ({ todos }) => {
10
- // Simple implementation that confirms the todos were written
11
- return `Successfully updated todo list with ${todos.length} items`;
12
- },
13
- {
14
- name: "todo_write",
15
- description: `Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
16
- It also helps the user understand the progress of the task and overall progress of their requests.
17
-
18
- ## When to Use This Tool
19
- Use this tool proactively in these scenarios:
20
-
21
- 1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
22
- 2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
23
- 3. User explicitly requests todo list - When the user directly asks you to use the todo list
24
- 4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
25
- 5. After receiving new instructions - Immediately capture user requirements as todos
26
- 6. When you start working on a task - Mark it as in_progress BEFORE beginning work. Ideally you should only have one todo as in_progress at a time
27
- 7. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation
28
-
29
- ## When NOT to Use This Tool
30
-
31
- Skip using this tool when:
32
- 1. There is only a single, straightforward task
33
- 2. The task is trivial and tracking it provides no organizational benefit
34
- 3. The task can be completed in less than 3 trivial steps
35
- 4. The task is purely conversational or informational
36
-
37
- NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
38
-
39
- ## Task States and Management
40
-
41
- 1. **Task States**: Use these states to track progress:
42
- - pending: Task not yet started
43
- - in_progress: Currently working on (limit to ONE task at a time)
44
- - completed: Task finished successfully
45
-
46
- **IMPORTANT**: Task descriptions must have two forms:
47
- - content: The imperative form describing what needs to be done (e.g., "Run tests", "Build the project")
48
- - activeForm: The present continuous form shown during execution (e.g., "Running tests", "Building the project")
49
-
50
- 2. **Task Management**:
51
- - Update task status in real-time as you work
52
- - Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
53
- - Exactly ONE task must be in_progress at any time (not less, not more)
54
- - Complete current tasks before starting new ones
55
- - Remove tasks that are no longer relevant from the list entirely
56
-
57
- 3. **Task Completion Requirements**:
58
- - ONLY mark a task as completed when you have FULLY accomplished it
59
- - If you encounter errors, blockers, or cannot finish, keep the task as in_progress
60
- - When blocked, create a new task describing what needs to be resolved
61
- - Never mark a task as completed if:
62
- - Tests are failing
63
- - Implementation is partial
64
- - You encountered unresolved errors
65
- - You couldn't find necessary files or dependencies
66
-
67
- 4. **Task Breakdown**:
68
- - Create specific, actionable items
69
- - Break complex tasks into smaller, manageable steps
70
- - Use clear, descriptive task names
71
- - Always provide both forms:
72
- - content: "Fix authentication bug"
73
- - activeForm: "Fixing authentication bug"
74
-
75
- When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.`,
76
- schema: z.object({
77
- todos: z.array(todoItemSchema),
78
- }),
79
- },
80
- );
@@ -1,4 +0,0 @@
1
- import { ExaSearchResults } from "@langchain/exa";
2
- export declare function makeWebSearchTool(): ExaSearchResults<{
3
- text: true;
4
- }>;
@@ -1,26 +0,0 @@
1
- import { ExaSearchResults } from "@langchain/exa";
2
- import Exa from "exa-js";
3
-
4
- let _webSearchInstance = null;
5
- export function makeWebSearchTool() {
6
- if (_webSearchInstance) {
7
- return _webSearchInstance;
8
- }
9
- const apiKey = process.env.EXA_API_KEY;
10
- if (!apiKey) {
11
- throw new Error(
12
- "EXA_API_KEY environment variable is required to use the web_search tool. " +
13
- "Please set it to your Exa API key from https://exa.ai",
14
- );
15
- }
16
- const client = new Exa(apiKey);
17
- _webSearchInstance = new ExaSearchResults({
18
- client,
19
- searchArgs: {
20
- numResults: 5,
21
- type: "auto",
22
- text: true,
23
- },
24
- });
25
- return _webSearchInstance;
26
- }
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env bun
2
- export {};
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { readFileSync } from "node:fs";
3
- import { join } from "node:path";
4
- import { makeHttpTransport, makeStdioTransport } from "../acp-server/index";
5
- // Load agent definition from JSON file
6
- const configPath = join(import.meta.dir, "agent.json");
7
- const agent = JSON.parse(readFileSync(configPath, "utf-8"));
8
- const transport = process.argv[2] || "stdio";
9
- if (transport === "http") {
10
- makeHttpTransport(agent);
11
- }
12
- else if (transport === "stdio") {
13
- makeStdioTransport(agent);
14
- }
15
- else {
16
- console.error(`Invalid transport: ${transport}`);
17
- process.exit(1);
18
- }
package/dist/example.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env bun
2
- export {};
package/dist/example.js DELETED
@@ -1,19 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { makeHttpTransport, makeStdioTransport } from "./acp-server/index.js";
3
-
4
- const exampleAgent = {
5
- model: "claude-sonnet-4-5-20250929",
6
- systemPrompt: "You are a helpful assistant.",
7
- tools: ["todo_write", "get_weather", "web_search"],
8
- };
9
- // Parse transport type from command line argument
10
- const transport = process.argv[2] || "stdio";
11
- if (transport === "http") {
12
- makeHttpTransport(exampleAgent);
13
- } else if (transport === "stdio") {
14
- makeStdioTransport(exampleAgent);
15
- } else {
16
- console.error(`Invalid transport: ${transport}`);
17
- console.error("Usage: bun run example.ts [stdio|http]");
18
- process.exit(1);
19
- }