diff --git a/AGENTS.md b/AGENTS.md index 62a8f790..9b557f25 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -52,6 +52,7 @@ Shakespeare provides AI agents with specialized tools for project development (n - **NostrGenerateKindTool**: Generate unused Nostr event kind numbers - **NostrEncodeTool**: Encode hex values into NIP-19 bech32 entities (npub, note, nprofile, nevent, naddr) - **NostrDecodeTool**: Decode NIP-19 bech32 entities into hex and structured data +- **BlossomUploadTool**: Upload files from the project to Blossom media hosting, returns a public URL - **ReadConsoleMessagesTool**: Read console messages from project preview with filtering capabilities #### Shell Commands diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 09b5abc8..8c868752 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -37,6 +37,7 @@ import { NostrGenerateKindTool } from '@/lib/tools/NostrGenerateKindTool'; import { NostrPublishEventsTool } from '@/lib/tools/NostrPublishEventsTool'; import { NostrEncodeTool } from '@/lib/tools/NostrEncodeTool'; import { NostrDecodeTool } from '@/lib/tools/NostrDecodeTool'; +import { BlossomUploadTool } from '@/lib/tools/BlossomUploadTool'; import { ShellTool } from '@/lib/tools/ShellTool'; import { ReadConsoleMessagesTool } from '@/lib/tools/ReadConsoleMessagesTool'; import { SkillTool } from '@/lib/tools/SkillTool'; @@ -221,6 +222,7 @@ export const ChatPane = forwardRef(({ nostr_publish_events: new NostrPublishEventsTool(), nostr_encode: new NostrEncodeTool(), nostr_decode: new NostrDecodeTool(), + blossom_upload: new BlossomUploadTool(fs, cwd, user?.signer), shell: new ShellTool(fs, cwd, git, config.corsProxy, user?.signer), read_console_messages: new ReadConsoleMessagesTool(), skill: new SkillTool(fs, availableSkills), diff --git a/src/lib/tools/BlossomUploadTool.ts b/src/lib/tools/BlossomUploadTool.ts new file mode 100644 index 00000000..4d19b273 --- /dev/null +++ b/src/lib/tools/BlossomUploadTool.ts @@ -0,0 +1,59 @@ +import { join } from "path-browserify"; +import { z } from "zod"; +import mime from 'mime'; +import { BlossomUploader } from '@nostrify/nostrify/uploaders'; +import type { NostrSigner } from '@nostrify/nostrify'; + +import type { Tool, ToolResult } from "./Tool"; +import type { JSRuntimeFS } from "../JSRuntime"; + +interface BlossomUploadParams { + filePath: string; +} + +export class BlossomUploadTool implements Tool { + readonly description = "Upload a file from the project to Blossom (a media hosting service). Returns the public URL and file metadata. Useful for hosting images, assets, or any file that needs a public URL."; + + readonly inputSchema = z.object({ + filePath: z.string() + .describe('Path to the file to upload. Can be absolute or relative to the project directory.'), + }); + + constructor( + private fs: JSRuntimeFS, + private cwd: string, + private signer?: NostrSigner, + ) {} + + async execute(args: BlossomUploadParams): Promise { + if (!this.signer) { + throw new Error('User must be logged in to upload files to Blossom.'); + } + + let filePath = args.filePath; + + // Handle relative paths + if (!filePath.startsWith('/')) { + filePath = join(this.cwd, filePath); + } + + const content = await this.fs.readFile(filePath); + const fileName = filePath.split('/').pop() || 'file'; + + const file = new File([content], fileName, { + type: mime.getType(filePath) || undefined, + }); + + const uploader = new BlossomUploader({ + servers: ['https://blossom.primal.net/'], + signer: this.signer, + }); + + const tags = await uploader.upload(file); + const url = tags.find(([name]) => name === 'url')?.[1]; + + return { + content: JSON.stringify({ url, tags }, null, 2), + }; + } +}