@townco/agent 0.1.111 → 0.1.113
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/adapter.js +83 -20
- package/dist/acp-server/http.js +21 -0
- package/dist/runner/langchain/index.js +179 -116
- package/dist/runner/langchain/tools/artifacts.d.ts +68 -0
- package/dist/runner/langchain/tools/artifacts.js +474 -0
- package/dist/runner/langchain/tools/conversation_search.d.ts +22 -0
- package/dist/runner/langchain/tools/conversation_search.js +137 -0
- package/dist/runner/langchain/tools/generate_image.d.ts +47 -0
- package/dist/runner/langchain/tools/generate_image.js +175 -0
- package/dist/runner/langchain/tools/subagent-connections.d.ts +7 -22
- package/dist/runner/langchain/tools/subagent-connections.js +26 -50
- package/dist/runner/langchain/tools/subagent.js +137 -378
- package/dist/runner/session-context.d.ts +18 -0
- package/dist/runner/session-context.js +35 -0
- package/dist/telemetry/setup.js +21 -5
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { GoogleGenAI } from "@google/genai";
|
|
4
|
+
import { getShedAuth } from "@townco/core/auth";
|
|
5
|
+
import { tool } from "langchain";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { getSessionContext, getToolOutputDir, hasSessionContext, } from "../../session-context";
|
|
8
|
+
let _directGenaiClient = null;
|
|
9
|
+
let _townGenaiClient = null;
|
|
10
|
+
/** Get Google GenAI client using direct GEMINI_API_KEY/GOOGLE_API_KEY environment variable */
|
|
11
|
+
function getDirectGenAIClient() {
|
|
12
|
+
if (_directGenaiClient) {
|
|
13
|
+
return _directGenaiClient;
|
|
14
|
+
}
|
|
15
|
+
const apiKey = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
|
|
16
|
+
if (!apiKey) {
|
|
17
|
+
throw new Error("GEMINI_API_KEY or GOOGLE_API_KEY environment variable is required to use the generate_image tool. " +
|
|
18
|
+
"Please set one of them to your Google AI API key.");
|
|
19
|
+
}
|
|
20
|
+
_directGenaiClient = new GoogleGenAI({ apiKey });
|
|
21
|
+
return _directGenaiClient;
|
|
22
|
+
}
|
|
23
|
+
/** Get Google GenAI client using Town proxy with authenticated credentials */
|
|
24
|
+
function getTownGenAIClient() {
|
|
25
|
+
if (_townGenaiClient) {
|
|
26
|
+
return _townGenaiClient;
|
|
27
|
+
}
|
|
28
|
+
const shedAuth = getShedAuth();
|
|
29
|
+
if (!shedAuth) {
|
|
30
|
+
throw new Error("Not logged in. Run 'town login' or set SHED_API_KEY to use the town_generate_image tool.");
|
|
31
|
+
}
|
|
32
|
+
// Configure the client to use shed as proxy
|
|
33
|
+
// The SDK will send requests to {shedUrl}/api/gemini/{apiVersion}/{path}
|
|
34
|
+
_townGenaiClient = new GoogleGenAI({
|
|
35
|
+
apiKey: shedAuth.accessToken,
|
|
36
|
+
httpOptions: {
|
|
37
|
+
baseUrl: `${shedAuth.shedUrl}/api/gemini/`,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
return _townGenaiClient;
|
|
41
|
+
}
|
|
42
|
+
function makeGenerateImageToolInternal(getClient) {
|
|
43
|
+
const generateImage = tool(async ({ prompt, aspectRatio = "1:1" }) => {
|
|
44
|
+
try {
|
|
45
|
+
if (!hasSessionContext()) {
|
|
46
|
+
throw new Error("GenerateImage tool requires session context. Ensure the tool is called within a session.");
|
|
47
|
+
}
|
|
48
|
+
const { sessionId } = getSessionContext();
|
|
49
|
+
const toolOutputDir = getToolOutputDir("GenerateImage");
|
|
50
|
+
const client = getClient();
|
|
51
|
+
// Use Gemini 3 Pro Image for image generation
|
|
52
|
+
// Note: imageConfig is a valid API option but not yet in the TypeScript types
|
|
53
|
+
// biome-ignore lint/suspicious/noExplicitAny: imageConfig not yet typed in @google/genai
|
|
54
|
+
const config = {
|
|
55
|
+
responseModalities: ["TEXT", "IMAGE"],
|
|
56
|
+
imageConfig: {
|
|
57
|
+
aspectRatio: aspectRatio,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
const response = await client.models.generateContent({
|
|
61
|
+
model: "gemini-3-pro-image-preview",
|
|
62
|
+
contents: [{ text: prompt }],
|
|
63
|
+
config,
|
|
64
|
+
});
|
|
65
|
+
if (!response.candidates || response.candidates.length === 0) {
|
|
66
|
+
return {
|
|
67
|
+
success: false,
|
|
68
|
+
error: "No response from the model. The request may have been filtered.",
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const candidate = response.candidates[0];
|
|
72
|
+
if (!candidate) {
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
error: "No candidate in the response.",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const parts = candidate.content?.parts;
|
|
79
|
+
if (!parts || parts.length === 0) {
|
|
80
|
+
return {
|
|
81
|
+
success: false,
|
|
82
|
+
error: "No content parts in the response.",
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
let imageData;
|
|
86
|
+
let textResponse;
|
|
87
|
+
let mimeType;
|
|
88
|
+
for (const part of parts) {
|
|
89
|
+
if (part.text) {
|
|
90
|
+
textResponse = part.text;
|
|
91
|
+
}
|
|
92
|
+
else if (part.inlineData) {
|
|
93
|
+
imageData = part.inlineData.data;
|
|
94
|
+
mimeType = part.inlineData.mimeType || "image/png";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!imageData) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: "No image was generated in the response.",
|
|
101
|
+
...(textResponse ? { textResponse } : {}),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// Save image to session-scoped tool output directory
|
|
105
|
+
await mkdir(toolOutputDir, { recursive: true });
|
|
106
|
+
// Generate unique filename
|
|
107
|
+
const timestamp = Date.now();
|
|
108
|
+
const extension = mimeType === "image/jpeg" ? "jpg" : "png";
|
|
109
|
+
const fileName = `image-${timestamp}.${extension}`;
|
|
110
|
+
const filePath = join(toolOutputDir, fileName);
|
|
111
|
+
// Save image to file
|
|
112
|
+
const buffer = Buffer.from(imageData, "base64");
|
|
113
|
+
await writeFile(filePath, buffer);
|
|
114
|
+
// Create URL for the static file server
|
|
115
|
+
// The agent HTTP server serves static files from the agent directory
|
|
116
|
+
// Use AGENT_BASE_URL if set (for production), otherwise construct from BIND_HOST/PORT
|
|
117
|
+
const port = process.env.PORT || "3100";
|
|
118
|
+
const hostname = process.env.BIND_HOST || "localhost";
|
|
119
|
+
const baseUrl = process.env.AGENT_BASE_URL || `http://${hostname}:${port}`;
|
|
120
|
+
const imageUrl = `${baseUrl}/static/.sessions/${sessionId}/artifacts/tool-GenerateImage/${fileName}`;
|
|
121
|
+
return {
|
|
122
|
+
success: true,
|
|
123
|
+
filePath,
|
|
124
|
+
fileName,
|
|
125
|
+
imageUrl,
|
|
126
|
+
...(mimeType ? { mimeType } : {}),
|
|
127
|
+
...(textResponse ? { textResponse } : {}),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
132
|
+
return {
|
|
133
|
+
success: false,
|
|
134
|
+
error: `Image generation failed: ${errorMessage}`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}, {
|
|
138
|
+
name: "GenerateImage",
|
|
139
|
+
description: "Generate an image based on a text prompt using Google's Gemini image generation model. " +
|
|
140
|
+
"Returns an imageUrl that can be displayed to the user. After calling this tool, " +
|
|
141
|
+
"include the imageUrl in your response as a markdown image like  " +
|
|
142
|
+
"so the user can see the generated image.\n" +
|
|
143
|
+
"- Creates images from detailed text descriptions\n" +
|
|
144
|
+
"- Supports various aspect ratios for different use cases\n" +
|
|
145
|
+
"- Be specific in prompts about style, composition, colors, and subjects\n" +
|
|
146
|
+
"\n" +
|
|
147
|
+
"Usage notes:\n" +
|
|
148
|
+
" - Provide detailed, specific prompts for best results\n" +
|
|
149
|
+
" - The generated image is saved to the session directory and served via URL\n" +
|
|
150
|
+
" - Always display the result using markdown: \n",
|
|
151
|
+
schema: z.object({
|
|
152
|
+
prompt: z
|
|
153
|
+
.string()
|
|
154
|
+
.describe("A detailed description of the image to generate. Be specific about style, composition, colors, and subjects."),
|
|
155
|
+
aspectRatio: z
|
|
156
|
+
.enum(["1:1", "3:4", "4:3", "9:16", "16:9", "5:4"])
|
|
157
|
+
.optional()
|
|
158
|
+
.default("1:1")
|
|
159
|
+
.describe("The aspect ratio of the generated image."),
|
|
160
|
+
}),
|
|
161
|
+
});
|
|
162
|
+
// biome-ignore lint/suspicious/noExplicitAny: Need to add custom properties to LangChain tool
|
|
163
|
+
generateImage.prettyName = "Generate Image";
|
|
164
|
+
// biome-ignore lint/suspicious/noExplicitAny: Need to add custom properties to LangChain tool
|
|
165
|
+
generateImage.icon = "Image";
|
|
166
|
+
return generateImage;
|
|
167
|
+
}
|
|
168
|
+
/** Create generate image tool using direct GEMINI_API_KEY/GOOGLE_API_KEY */
|
|
169
|
+
export function makeGenerateImageTool() {
|
|
170
|
+
return makeGenerateImageToolInternal(getDirectGenAIClient);
|
|
171
|
+
}
|
|
172
|
+
/** Create generate image tool using Town proxy */
|
|
173
|
+
export function makeTownGenerateImageTool() {
|
|
174
|
+
return makeGenerateImageToolInternal(getTownGenAIClient);
|
|
175
|
+
}
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
import { EventEmitter } from "node:events";
|
|
2
|
-
/**
|
|
3
|
-
* Registry for subagent connection info.
|
|
4
|
-
* Maps query hash to connection info so the runner can emit tool_call_update.
|
|
5
|
-
*/
|
|
6
|
-
export interface SubagentConnectionInfo {
|
|
7
|
-
port: number;
|
|
8
|
-
sessionId: string;
|
|
9
|
-
}
|
|
10
1
|
/**
|
|
11
2
|
* Sub-agent tool call tracked during streaming
|
|
12
3
|
*/
|
|
@@ -36,14 +27,11 @@ export interface SubagentMessage {
|
|
|
36
27
|
contentBlocks: SubagentContentBlock[];
|
|
37
28
|
toolCalls: SubagentToolCall[];
|
|
38
29
|
}
|
|
39
|
-
/**
|
|
40
|
-
* Event emitter for subagent connection events.
|
|
41
|
-
* The runner listens to these events and emits tool_call_update.
|
|
42
|
-
*/
|
|
43
|
-
export declare const subagentEvents: EventEmitter<[never]>;
|
|
44
30
|
/**
|
|
45
31
|
* Maps query hash to toolCallId.
|
|
46
32
|
* Set by the runner when it sees a subagent tool_call.
|
|
33
|
+
* This is still global because the registration happens in the parent runner
|
|
34
|
+
* before subagent execution begins.
|
|
47
35
|
*/
|
|
48
36
|
export declare const queryToToolCallId: Map<string, string>;
|
|
49
37
|
/**
|
|
@@ -51,12 +39,9 @@ export declare const queryToToolCallId: Map<string, string>;
|
|
|
51
39
|
*/
|
|
52
40
|
export declare function hashQuery(query: string): string;
|
|
53
41
|
/**
|
|
54
|
-
* Called by the subagent tool
|
|
55
|
-
* Emits an event
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Called by the subagent tool when it completes with accumulated messages.
|
|
60
|
-
* Emits an event with the messages for session storage.
|
|
42
|
+
* Called by the subagent tool during execution to emit incremental messages.
|
|
43
|
+
* Emits an event with the messages for live streaming to the UI.
|
|
44
|
+
* Uses the invocation context's EventEmitter to ensure messages go to the correct parent.
|
|
45
|
+
* @param completed - If true, signals that the subagent stream has finished
|
|
61
46
|
*/
|
|
62
|
-
export declare function emitSubagentMessages(queryHash: string, messages: SubagentMessage[]): void;
|
|
47
|
+
export declare function emitSubagentMessages(queryHash: string, messages: SubagentMessage[], completed?: boolean): void;
|
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { EventEmitter } from "node:events";
|
|
3
2
|
import { createLogger } from "@townco/core";
|
|
3
|
+
import { getInvocationContext } from "../../session-context.js";
|
|
4
4
|
const logger = createLogger("subagent-connections");
|
|
5
|
-
/**
|
|
6
|
-
* Event emitter for subagent connection events.
|
|
7
|
-
* The runner listens to these events and emits tool_call_update.
|
|
8
|
-
*/
|
|
9
|
-
export const subagentEvents = new EventEmitter();
|
|
10
5
|
/**
|
|
11
6
|
* Maps query hash to toolCallId.
|
|
12
7
|
* Set by the runner when it sees a subagent tool_call.
|
|
8
|
+
* This is still global because the registration happens in the parent runner
|
|
9
|
+
* before subagent execution begins.
|
|
13
10
|
*/
|
|
14
11
|
export const queryToToolCallId = new Map();
|
|
15
|
-
/**
|
|
16
|
-
* Maps query hash to resolved toolCallId (preserved after connection event).
|
|
17
|
-
* Used to correlate messages when the tool completes.
|
|
18
|
-
*/
|
|
19
|
-
const queryToResolvedToolCallId = new Map();
|
|
20
12
|
/**
|
|
21
13
|
* Generate a hash from the query string for correlation.
|
|
22
14
|
*/
|
|
@@ -29,61 +21,45 @@ export function hashQuery(query) {
|
|
|
29
21
|
return hash;
|
|
30
22
|
}
|
|
31
23
|
/**
|
|
32
|
-
* Called by the subagent tool
|
|
33
|
-
* Emits an event
|
|
24
|
+
* Called by the subagent tool during execution to emit incremental messages.
|
|
25
|
+
* Emits an event with the messages for live streaming to the UI.
|
|
26
|
+
* Uses the invocation context's EventEmitter to ensure messages go to the correct parent.
|
|
27
|
+
* @param completed - If true, signals that the subagent stream has finished
|
|
34
28
|
*/
|
|
35
|
-
export function
|
|
36
|
-
logger.info("emitSubagentConnection called", {
|
|
37
|
-
queryHash,
|
|
38
|
-
port: connectionInfo.port,
|
|
39
|
-
sessionId: connectionInfo.sessionId,
|
|
40
|
-
registeredHashes: Array.from(queryToToolCallId.keys()),
|
|
41
|
-
});
|
|
29
|
+
export function emitSubagentMessages(queryHash, messages, completed = false) {
|
|
42
30
|
const toolCallId = queryToToolCallId.get(queryHash);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
toolCallId,
|
|
47
|
-
port: connectionInfo.port,
|
|
48
|
-
sessionId: connectionInfo.sessionId,
|
|
49
|
-
});
|
|
50
|
-
subagentEvents.emit("connection", {
|
|
51
|
-
toolCallId,
|
|
52
|
-
...connectionInfo,
|
|
53
|
-
});
|
|
54
|
-
// Preserve the toolCallId for message emission, but remove from pending lookup
|
|
55
|
-
queryToResolvedToolCallId.set(queryHash, toolCallId);
|
|
56
|
-
queryToToolCallId.delete(queryHash);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
logger.warn("No toolCallId found for queryHash", {
|
|
31
|
+
const invocationCtx = getInvocationContext();
|
|
32
|
+
if (!invocationCtx) {
|
|
33
|
+
logger.warn("✗ No invocation context found - cannot emit subagent messages", {
|
|
60
34
|
queryHash,
|
|
61
|
-
|
|
35
|
+
hasToolCallId: !!toolCallId,
|
|
62
36
|
});
|
|
37
|
+
return;
|
|
63
38
|
}
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Called by the subagent tool when it completes with accumulated messages.
|
|
67
|
-
* Emits an event with the messages for session storage.
|
|
68
|
-
*/
|
|
69
|
-
export function emitSubagentMessages(queryHash, messages) {
|
|
70
|
-
const toolCallId = queryToResolvedToolCallId.get(queryHash);
|
|
71
39
|
if (toolCallId) {
|
|
72
|
-
|
|
40
|
+
const firstMessage = messages[0];
|
|
41
|
+
logger.info("✓ Emitting subagent messages for live streaming", {
|
|
73
42
|
queryHash,
|
|
74
43
|
toolCallId,
|
|
75
44
|
messageCount: messages.length,
|
|
45
|
+
hasContent: firstMessage ? firstMessage.content.length > 0 : false,
|
|
46
|
+
hasToolCalls: firstMessage ? firstMessage.toolCalls.length > 0 : false,
|
|
47
|
+
completed,
|
|
48
|
+
invocationId: invocationCtx.invocationId,
|
|
76
49
|
});
|
|
77
|
-
|
|
50
|
+
// Emit to the parent's invocation-scoped EventEmitter
|
|
51
|
+
invocationCtx.subagentEventEmitter.emit("messages", {
|
|
78
52
|
toolCallId,
|
|
79
53
|
messages,
|
|
54
|
+
completed,
|
|
80
55
|
});
|
|
81
|
-
// Clean up the resolved mapping
|
|
82
|
-
queryToResolvedToolCallId.delete(queryHash);
|
|
83
56
|
}
|
|
84
57
|
else {
|
|
85
|
-
logger.warn("No
|
|
58
|
+
logger.warn("✗ No toolCallId found for messages emission (RACE CONDITION)", {
|
|
86
59
|
queryHash,
|
|
60
|
+
registeredHashes: Array.from(queryToToolCallId.keys()),
|
|
61
|
+
mapSize: queryToToolCallId.size,
|
|
62
|
+
invocationId: invocationCtx.invocationId,
|
|
87
63
|
});
|
|
88
64
|
}
|
|
89
65
|
}
|