@townco/agent 0.1.123 → 0.1.125
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/acp-server/session-storage.js +25 -0
- package/dist/runner/langchain/tools/subagent-connections.d.ts +2 -0
- package/dist/runner/langchain/tools/subagent.js +13 -6
- package/dist/templates/index.d.ts +8 -0
- package/dist/templates/index.js +43 -14
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/templates/index.ts +63 -20
- package/dist/runner/langchain/tools/conversation_search.d.ts +0 -22
- package/dist/runner/langchain/tools/conversation_search.js +0 -137
|
@@ -45,11 +45,36 @@ const subagentContentBlockSchema = z.discriminatedUnion("type", [
|
|
|
45
45
|
toolCall: subagentToolCallBlockSchema,
|
|
46
46
|
}),
|
|
47
47
|
]);
|
|
48
|
+
const subagentContextSizeSchema = z.object({
|
|
49
|
+
systemPromptTokens: z.number(),
|
|
50
|
+
toolOverheadTokens: z.number().optional(),
|
|
51
|
+
mcpOverheadTokens: z.number().optional(),
|
|
52
|
+
userMessagesTokens: z.number(),
|
|
53
|
+
assistantMessagesTokens: z.number(),
|
|
54
|
+
toolInputTokens: z.number(),
|
|
55
|
+
toolResultsTokens: z.number(),
|
|
56
|
+
totalEstimated: z.number(),
|
|
57
|
+
llmReportedInputTokens: z.number().optional(),
|
|
58
|
+
modelContextWindow: z.number().optional(),
|
|
59
|
+
});
|
|
48
60
|
const subagentMessageSchema = z.object({
|
|
49
61
|
id: z.string(),
|
|
50
62
|
content: z.string(),
|
|
51
63
|
contentBlocks: z.array(subagentContentBlockSchema).optional(),
|
|
52
64
|
toolCalls: z.array(subagentToolCallBlockSchema).optional(),
|
|
65
|
+
// Subagent streaming metadata (for UI)
|
|
66
|
+
_meta: z
|
|
67
|
+
.object({
|
|
68
|
+
semanticName: z.string().optional(),
|
|
69
|
+
agentDefinitionName: z.string().optional(),
|
|
70
|
+
currentActivity: z.string().optional(),
|
|
71
|
+
statusGenerating: z.boolean().optional(),
|
|
72
|
+
context_size: subagentContextSizeSchema.optional(),
|
|
73
|
+
})
|
|
74
|
+
.passthrough()
|
|
75
|
+
.optional(),
|
|
76
|
+
// Some older payloads may send context_size top-level
|
|
77
|
+
context_size: subagentContextSizeSchema.optional(),
|
|
53
78
|
});
|
|
54
79
|
const toolCallBlockSchema = z.object({
|
|
55
80
|
type: z.literal("tool_call"),
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ContextSize } from "../../../utils/context-size-calculator.js";
|
|
1
2
|
/**
|
|
2
3
|
* Sub-agent tool call tracked during streaming
|
|
3
4
|
*/
|
|
@@ -33,6 +34,7 @@ export interface SubagentMessage {
|
|
|
33
34
|
agentDefinitionName?: string;
|
|
34
35
|
currentActivity?: string;
|
|
35
36
|
statusGenerating?: boolean;
|
|
37
|
+
context_size?: ContextSize;
|
|
36
38
|
};
|
|
37
39
|
}
|
|
38
40
|
/**
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as crypto from "node:crypto";
|
|
2
2
|
import * as fs from "node:fs/promises";
|
|
3
|
-
import { mkdir } from "node:fs/promises";
|
|
4
3
|
import * as path from "node:path";
|
|
5
4
|
import Anthropic from "@anthropic-ai/sdk";
|
|
6
5
|
import { context, propagation, trace } from "@opentelemetry/api";
|
|
@@ -8,7 +7,7 @@ import { createLogger } from "@townco/core";
|
|
|
8
7
|
import { z } from "zod";
|
|
9
8
|
import { AgentAcpAdapter, SUBAGENT_MODE_KEY, } from "../../../acp-server/adapter.js";
|
|
10
9
|
import { makeRunnerFromDefinition } from "../../index.js";
|
|
11
|
-
import {
|
|
10
|
+
import { getAbortSignal, getSessionContext } from "../../session-context.js";
|
|
12
11
|
import { emitSubagentMessages, hashQuery, } from "./subagent-connections.js";
|
|
13
12
|
const logger = createLogger("subagent-tool", "debug");
|
|
14
13
|
/**
|
|
@@ -404,6 +403,14 @@ function createStreamingConnection(queryHash, currentMessage, toolCallMap, toolN
|
|
|
404
403
|
sessionUpdate: (notification) => {
|
|
405
404
|
const update = notification.update;
|
|
406
405
|
let shouldEmit = false;
|
|
406
|
+
// Capture subagent context size (usually sent by adapter via _meta.context_size)
|
|
407
|
+
const meta = update._meta;
|
|
408
|
+
const contextSize = meta?.context_size ??
|
|
409
|
+
update.context_size;
|
|
410
|
+
if (contextSize != null && currentMessage._meta) {
|
|
411
|
+
currentMessage._meta.context_size = contextSize;
|
|
412
|
+
shouldEmit = true;
|
|
413
|
+
}
|
|
407
414
|
// Handle agent_message_chunk
|
|
408
415
|
if (update.sessionUpdate === "agent_message_chunk") {
|
|
409
416
|
const content = update.content;
|
|
@@ -424,13 +431,13 @@ function createStreamingConnection(queryHash, currentMessage, toolCallMap, toolN
|
|
|
424
431
|
}
|
|
425
432
|
// Handle tool_call
|
|
426
433
|
if (update.sessionUpdate === "tool_call" && update.toolCallId) {
|
|
427
|
-
const
|
|
434
|
+
const toolMeta = update._meta;
|
|
428
435
|
const rawInput = update.rawInput;
|
|
429
436
|
const toolCall = {
|
|
430
437
|
id: update.toolCallId,
|
|
431
438
|
title: update.title || "Tool call",
|
|
432
|
-
prettyName:
|
|
433
|
-
icon:
|
|
439
|
+
prettyName: toolMeta?.prettyName,
|
|
440
|
+
icon: toolMeta?.icon,
|
|
434
441
|
status: update.status || "pending",
|
|
435
442
|
rawInput,
|
|
436
443
|
};
|
|
@@ -608,7 +615,7 @@ async function querySubagent(agentName, agentPath, agentWorkingDirectory, query,
|
|
|
608
615
|
try {
|
|
609
616
|
// Invoke through adapter (gets full session tracking + hook execution)
|
|
610
617
|
// The adapter will call connection.sessionUpdate() for streaming updates
|
|
611
|
-
|
|
618
|
+
await adapter.prompt(promptRequest);
|
|
612
619
|
logger.info("[DEBUG] Subagent adapter.prompt() completed", {
|
|
613
620
|
agentName,
|
|
614
621
|
queryHash,
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { z } from "zod";
|
|
2
2
|
import type { AgentDefinition, McpConfigSchema } from "../definition";
|
|
3
3
|
type McpConfig = z.infer<typeof McpConfigSchema>;
|
|
4
|
+
/** Config for a subagent to wire up via makeSubagentsTool */
|
|
5
|
+
export interface SubagentConfig {
|
|
6
|
+
agentName: string;
|
|
7
|
+
toolDescription: string;
|
|
8
|
+
displayName?: string;
|
|
9
|
+
path: string;
|
|
10
|
+
}
|
|
4
11
|
export interface TemplateVars {
|
|
5
12
|
name: string;
|
|
6
13
|
model: string;
|
|
@@ -21,6 +28,7 @@ export interface TemplateVars {
|
|
|
21
28
|
schema: unknown;
|
|
22
29
|
}>;
|
|
23
30
|
mcps?: McpConfig[];
|
|
31
|
+
subagents?: SubagentConfig[];
|
|
24
32
|
systemPrompt: string | null;
|
|
25
33
|
hasWebSearch: boolean;
|
|
26
34
|
hasGenerateImage: boolean;
|
package/dist/templates/index.js
CHANGED
|
@@ -23,6 +23,11 @@ export function getTemplateVars(name, definition) {
|
|
|
23
23
|
if (definition.mcps && definition.mcps.length > 0) {
|
|
24
24
|
result.mcps = definition.mcps;
|
|
25
25
|
}
|
|
26
|
+
// Handle subagents if defined (using type assertion since AgentDefinition may not have subagents yet)
|
|
27
|
+
const defWithSubagents = definition;
|
|
28
|
+
if (defWithSubagents.subagents && defWithSubagents.subagents.length > 0) {
|
|
29
|
+
result.subagents = defWithSubagents.subagents;
|
|
30
|
+
}
|
|
26
31
|
return result;
|
|
27
32
|
}
|
|
28
33
|
export function generatePackageJson(vars) {
|
|
@@ -59,33 +64,57 @@ export function generatePackageJson(vars) {
|
|
|
59
64
|
return JSON.stringify(pkg, null, 2);
|
|
60
65
|
}
|
|
61
66
|
export async function generateIndexTs(vars) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
const hasSubagents = vars.subagents && vars.subagents.length > 0;
|
|
68
|
+
let imports = 'import type { AgentDefinition } from "@townco/agent/definition";';
|
|
69
|
+
if (hasSubagents) {
|
|
70
|
+
imports += '\nimport { makeSubagentsTool } from "@townco/agent/utils";';
|
|
71
|
+
}
|
|
72
|
+
// Build tools code - either plain JSON or with makeSubagentsTool call
|
|
73
|
+
const toolsCode = (() => {
|
|
74
|
+
if (!hasSubagents) {
|
|
75
|
+
return JSON.stringify(vars.tools);
|
|
76
|
+
}
|
|
77
|
+
// makeSubagentsTool expects 'description' not 'toolDescription'
|
|
78
|
+
const subagentConfigs = vars.subagents.map((s) => ({
|
|
79
|
+
agentName: s.agentName,
|
|
80
|
+
description: s.toolDescription,
|
|
81
|
+
...(s.displayName && { displayName: s.displayName }),
|
|
82
|
+
path: s.path,
|
|
83
|
+
}));
|
|
84
|
+
const subagentToolCall = `makeSubagentsTool(${JSON.stringify(subagentConfigs, null, 2)})`;
|
|
85
|
+
const toolItems = vars.tools.map((t) => JSON.stringify(t));
|
|
86
|
+
toolItems.push(subagentToolCall);
|
|
87
|
+
return `[${toolItems.join(", ")}]`;
|
|
88
|
+
})();
|
|
89
|
+
// Build optional fields
|
|
90
|
+
const optionalFields = [];
|
|
66
91
|
if (vars.displayName) {
|
|
67
|
-
|
|
92
|
+
optionalFields.push(`displayName: ${JSON.stringify(vars.displayName)},`);
|
|
68
93
|
}
|
|
69
94
|
if (vars.description) {
|
|
70
|
-
|
|
95
|
+
optionalFields.push(`description: ${JSON.stringify(vars.description)},`);
|
|
71
96
|
}
|
|
72
97
|
if (vars.suggestedPrompts) {
|
|
73
|
-
|
|
98
|
+
optionalFields.push(`suggestedPrompts: ${JSON.stringify(vars.suggestedPrompts)},`);
|
|
74
99
|
}
|
|
75
|
-
agentDef.systemPrompt = vars.systemPrompt;
|
|
76
|
-
agentDef.tools = vars.tools;
|
|
77
100
|
if (vars.mcps && vars.mcps.length > 0) {
|
|
78
|
-
|
|
101
|
+
optionalFields.push(`mcps: ${JSON.stringify(vars.mcps)},`);
|
|
79
102
|
}
|
|
80
103
|
if (vars.hooks) {
|
|
81
|
-
|
|
104
|
+
optionalFields.push(`hooks: ${JSON.stringify(vars.hooks)},`);
|
|
82
105
|
}
|
|
83
|
-
|
|
106
|
+
const optionalFieldsStr = optionalFields.length > 0 ? `\n ${optionalFields.join("\n ")}` : "";
|
|
107
|
+
const code = `${imports}
|
|
84
108
|
|
|
85
|
-
const agent: AgentDefinition =
|
|
109
|
+
const agent: AgentDefinition = {
|
|
110
|
+
model: ${JSON.stringify(vars.model)},${optionalFieldsStr}
|
|
111
|
+
systemPrompt: ${JSON.stringify(vars.systemPrompt)},
|
|
112
|
+
tools: ${toolsCode},
|
|
113
|
+
};
|
|
86
114
|
|
|
87
115
|
export default agent;
|
|
88
|
-
|
|
116
|
+
`;
|
|
117
|
+
return prettier.format(code, { parser: "typescript" });
|
|
89
118
|
}
|
|
90
119
|
export function generateBinTs() {
|
|
91
120
|
return `#!/usr/bin/env bun
|