@townco/agent 0.1.106 → 0.1.108

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,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
- }