@townco/agent 0.1.122 → 0.1.124

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 (33) hide show
  1. package/dist/acp-server/adapter.d.ts +1 -0
  2. package/dist/acp-server/adapter.js +133 -11
  3. package/dist/acp-server/session-storage.js +25 -0
  4. package/dist/runner/agent-runner.d.ts +7 -0
  5. package/dist/runner/hooks/executor.js +1 -1
  6. package/dist/runner/hooks/predefined/context-validator.d.ts +1 -1
  7. package/dist/runner/hooks/predefined/context-validator.js +2 -2
  8. package/dist/runner/hooks/predefined/document-context-extractor/chunk-manager.d.ts +1 -1
  9. package/dist/runner/hooks/predefined/document-context-extractor/chunk-manager.js +3 -3
  10. package/dist/runner/hooks/predefined/document-context-extractor/content-extractor.js +2 -2
  11. package/dist/runner/hooks/predefined/document-context-extractor/index.js +5 -5
  12. package/dist/runner/hooks/predefined/document-context-extractor/relevance-scorer.js +2 -2
  13. package/dist/runner/hooks/predefined/tool-response-compactor.js +9 -9
  14. package/dist/runner/langchain/index.js +301 -9
  15. package/dist/runner/langchain/otel-callbacks.d.ts +5 -0
  16. package/dist/runner/langchain/otel-callbacks.js +8 -0
  17. package/dist/runner/langchain/tools/artifacts.d.ts +68 -0
  18. package/dist/runner/langchain/tools/artifacts.js +474 -0
  19. package/dist/runner/langchain/tools/document_extract.js +1 -1
  20. package/dist/runner/langchain/tools/generate_image.d.ts +47 -0
  21. package/dist/runner/langchain/tools/generate_image.js +175 -0
  22. package/dist/runner/langchain/tools/port-utils.d.ts +8 -0
  23. package/dist/runner/langchain/tools/port-utils.js +35 -0
  24. package/dist/runner/langchain/tools/subagent-connections.d.ts +2 -0
  25. package/dist/runner/langchain/tools/subagent.js +13 -6
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/dist/utils/context-size-calculator.d.ts +1 -1
  28. package/dist/utils/context-size-calculator.js +9 -14
  29. package/dist/utils/token-counter.d.ts +9 -7
  30. package/dist/utils/token-counter.js +30 -11
  31. package/dist/utils/tool-overhead-calculator.d.ts +2 -2
  32. package/dist/utils/tool-overhead-calculator.js +5 -4
  33. package/package.json +8 -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 ![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
+ }
@@ -0,0 +1,8 @@
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>;
@@ -0,0 +1,35 @@
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
+ }
@@ -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 { bindGeneratorToSessionContext, getAbortSignal, getSessionContext, } from "../../session-context.js";
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 meta = update._meta;
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: meta?.prettyName,
433
- icon: meta?.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
- const response = await adapter.prompt(promptRequest);
618
+ await adapter.prompt(promptRequest);
612
619
  logger.info("[DEBUG] Subagent adapter.prompt() completed", {
613
620
  agentName,
614
621
  queryHash,