@townco/agent 0.1.121 → 0.1.122

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.
@@ -1,137 +0,0 @@
1
- import { existsSync, readdirSync, readFileSync } from "node:fs";
2
- import { join } from "node:path";
3
- import { tool } from "langchain";
4
- import { z } from "zod";
5
- export const CONVERSATION_SEARCH_TOOL_NAME = "conversation_search";
6
- const conversationSearchSchema = z.object({
7
- query: z
8
- .string()
9
- .min(1)
10
- .describe("The text to search for in conversation messages (case-insensitive)"),
11
- date_from: z
12
- .string()
13
- .optional()
14
- .describe("ISO date string to filter conversations from this date onwards"),
15
- date_to: z
16
- .string()
17
- .optional()
18
- .describe("ISO date string to filter conversations up to this date"),
19
- max_results: z
20
- .number()
21
- .optional()
22
- .describe("Maximum number of matching conversations to return (default: 10)"),
23
- });
24
- /**
25
- * Creates a conversation search tool that searches past agent chat sessions.
26
- * @param agentDir - The directory of the current agent (e.g., agents/researcher)
27
- */
28
- export function makeConversationSearchTool(agentDir) {
29
- const conversationSearch = tool(async ({ query, date_from, date_to, max_results = 10 }) => {
30
- const results = [];
31
- const queryLower = query.toLowerCase();
32
- const dateFromParsed = date_from ? new Date(date_from) : null;
33
- const dateToParsed = date_to ? new Date(date_to) : null;
34
- const sessionsDir = join(agentDir, ".sessions");
35
- if (!existsSync(sessionsDir)) {
36
- return `No conversations found matching "${query}".`;
37
- }
38
- let sessionFiles;
39
- try {
40
- sessionFiles = readdirSync(sessionsDir).filter((f) => f.endsWith(".json") && !f.endsWith(".tmp"));
41
- }
42
- catch {
43
- return `No conversations found matching "${query}".`;
44
- }
45
- for (const file of sessionFiles) {
46
- if (results.length >= max_results)
47
- break;
48
- try {
49
- const content = readFileSync(join(sessionsDir, file), "utf-8");
50
- const session = JSON.parse(content);
51
- // Date filtering
52
- const sessionDate = new Date(session.metadata.updatedAt);
53
- if (dateFromParsed && sessionDate < dateFromParsed)
54
- continue;
55
- if (dateToParsed && sessionDate > dateToParsed)
56
- continue;
57
- // Search messages
58
- const matches = [];
59
- for (let i = 0; i < session.messages.length; i++) {
60
- const msg = session.messages[i];
61
- if (!msg)
62
- continue;
63
- const textBlocks = msg.content.filter((c) => c.type === "text");
64
- for (const block of textBlocks) {
65
- if (block.text.toLowerCase().includes(queryLower)) {
66
- // Extract matched snippet with surrounding context
67
- const matchIndex = block.text.toLowerCase().indexOf(queryLower);
68
- const snippetStart = Math.max(0, matchIndex - 50);
69
- const snippetEnd = Math.min(block.text.length, matchIndex + query.length + 50);
70
- const matchedText = block.text.slice(snippetStart, snippetEnd);
71
- matches.push({
72
- messageIndex: i,
73
- role: msg.role,
74
- timestamp: msg.timestamp,
75
- matchedText: (snippetStart > 0 ? "..." : "") +
76
- matchedText +
77
- (snippetEnd < block.text.length ? "..." : ""),
78
- });
79
- break; // One match per message is enough
80
- }
81
- }
82
- }
83
- if (matches.length > 0) {
84
- results.push({
85
- sessionId: session.sessionId,
86
- agentName: session.metadata.agentName,
87
- createdAt: session.metadata.createdAt,
88
- updatedAt: session.metadata.updatedAt,
89
- matches,
90
- });
91
- }
92
- }
93
- catch {
94
- // Skip invalid session files
95
- continue;
96
- }
97
- }
98
- if (results.length === 0) {
99
- return `No conversations found matching "${query}".`;
100
- }
101
- // Format results as readable string
102
- let output = `Found ${results.length} conversation(s) matching "${query}":\n\n`;
103
- for (const result of results) {
104
- output += `=== Session: ${result.sessionId} (${result.agentName}) ===\n`;
105
- output += `Created: ${result.createdAt} | Updated: ${result.updatedAt}\n\n`;
106
- for (const match of result.matches) {
107
- output += `[Message ${match.messageIndex + 1} - ${match.role} @ ${match.timestamp}]\n`;
108
- output += `"${match.matchedText}"\n\n`;
109
- }
110
- output += "---\n\n";
111
- }
112
- return output;
113
- }, {
114
- name: CONVERSATION_SEARCH_TOOL_NAME,
115
- description: `Search across past chat conversations to find previous discussions on specific topics.
116
-
117
- Use this tool to:
118
- - Find previous conversations that discussed specific topics
119
- - Recall information from past sessions
120
- - Search for patterns or recurring themes across conversations
121
-
122
- Parameters:
123
- - query: The text to search for (case-insensitive)
124
- - date_from: (optional) Only search conversations from this date onwards (ISO format, e.g., "2025-01-01")
125
- - date_to: (optional) Only search conversations up to this date (ISO format)
126
- - max_results: (optional) Maximum results to return (default: 10)`,
127
- schema: conversationSearchSchema,
128
- });
129
- conversationSearch.prettyName = "Conversation Search";
130
- conversationSearch.icon = "MessageSquare";
131
- conversationSearch.verbiage = {
132
- active: "Searching conversations for {query}",
133
- past: "Searched conversations for {query}",
134
- paramKey: "query",
135
- };
136
- return conversationSearch;
137
- }
@@ -1,47 +0,0 @@
1
- import { z } from "zod";
2
- interface GenerateImageResult {
3
- success: boolean;
4
- filePath?: string | undefined;
5
- fileName?: string | undefined;
6
- imageUrl?: string | undefined;
7
- textResponse?: string | undefined;
8
- mimeType?: string | undefined;
9
- error?: string | undefined;
10
- }
11
- /** Create generate image tool using direct GEMINI_API_KEY/GOOGLE_API_KEY */
12
- export declare function makeGenerateImageTool(): import("langchain").DynamicStructuredTool<z.ZodObject<{
13
- prompt: z.ZodString;
14
- aspectRatio: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
15
- "16:9": "16:9";
16
- "1:1": "1:1";
17
- "3:4": "3:4";
18
- "4:3": "4:3";
19
- "5:4": "5:4";
20
- "9:16": "9:16";
21
- }>>>;
22
- }, z.core.$strip>, {
23
- prompt: string;
24
- aspectRatio: "16:9" | "1:1" | "3:4" | "4:3" | "5:4" | "9:16";
25
- }, {
26
- prompt: string;
27
- aspectRatio?: "16:9" | "1:1" | "3:4" | "4:3" | "5:4" | "9:16" | undefined;
28
- }, GenerateImageResult>;
29
- /** Create generate image tool using Town proxy */
30
- export declare function makeTownGenerateImageTool(): import("langchain").DynamicStructuredTool<z.ZodObject<{
31
- prompt: z.ZodString;
32
- aspectRatio: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
33
- "16:9": "16:9";
34
- "1:1": "1:1";
35
- "3:4": "3:4";
36
- "4:3": "4:3";
37
- "5:4": "5:4";
38
- "9:16": "9:16";
39
- }>>>;
40
- }, z.core.$strip>, {
41
- prompt: string;
42
- aspectRatio: "16:9" | "1:1" | "3:4" | "4:3" | "5:4" | "9:16";
43
- }, {
44
- prompt: string;
45
- aspectRatio?: "16:9" | "1:1" | "3:4" | "4:3" | "5:4" | "9:16" | undefined;
46
- }, GenerateImageResult>;
47
- export {};
@@ -1,175 +0,0 @@
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 ![Description](imageUrl) " +
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: ![description](imageUrl)\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,8 +0,0 @@
1
- /**
2
- * Check if a port is available
3
- */
4
- export declare function isPortAvailable(port: number): Promise<boolean>;
5
- /**
6
- * Find the next available port starting from the given port
7
- */
8
- export declare function findAvailablePort(startPort: number, maxAttempts?: number): Promise<number>;
@@ -1,35 +0,0 @@
1
- import { createServer } from "node:net";
2
- /**
3
- * Check if a port is available
4
- */
5
- export async function isPortAvailable(port) {
6
- return new Promise((resolve) => {
7
- const server = createServer();
8
- server.once("error", (err) => {
9
- if (err.code === "EADDRINUSE") {
10
- resolve(false);
11
- }
12
- else {
13
- resolve(false);
14
- }
15
- });
16
- server.once("listening", () => {
17
- server.close();
18
- resolve(true);
19
- });
20
- server.listen(port);
21
- });
22
- }
23
- /**
24
- * Find the next available port starting from the given port
25
- */
26
- export async function findAvailablePort(startPort, maxAttempts = 100) {
27
- for (let i = 0; i < maxAttempts; i++) {
28
- const port = startPort + i;
29
- const available = await isPortAvailable(port);
30
- if (available) {
31
- return port;
32
- }
33
- }
34
- throw new Error(`Could not find an available port between ${startPort} and ${startPort + maxAttempts - 1}`);
35
- }