From 06660eca1c26e645c6b01dec2905ceee42f37048 Mon Sep 17 00:00:00 2001 From: Ariel Weinberger Date: Mon, 14 Jul 2025 20:38:15 -0500 Subject: [PATCH] fix broken stuff --- ai-service/.env.example | 6 ++ ai-service/.gitignore | 3 +- ai-service/README.md | 29 +++++++++ ai-service/src/constants.ts | 4 -- ai-service/src/handlers/chat/route.ts | 36 ++++++----- .../src/lib/ai/mastra/tools/file-tools.ts | 37 +++++++----- .../lib/ai/mastra/utils/runtime-context.ts | 24 +++++++- .../lib/ai/mastra/workflows/code-workflow.ts | 59 ++++++++++++------- ai-service/src/lib/constants.ts | 4 +- ai-service/src/lib/git-utils.ts | 8 ++- ai-service/src/lib/imagine/conversation.ts | 5 +- ai-service/src/lib/synapse-http-client.ts | 31 ++++++---- ai-service/temp-messages.ts | 10 ++++ 13 files changed, 181 insertions(+), 75 deletions(-) create mode 100644 ai-service/.env.example create mode 100644 ai-service/README.md delete mode 100644 ai-service/src/constants.ts create mode 100644 ai-service/temp-messages.ts diff --git a/ai-service/.env.example b/ai-service/.env.example new file mode 100644 index 000000000..b00a48241 --- /dev/null +++ b/ai-service/.env.example @@ -0,0 +1,6 @@ +ANTHROPIC_API_KEY= +APPWRITE_ENDPOINT=http://localhost/v1 +WORKSPACE_DIR=./tmp/workspace +IMAGINE_PROJECT_ID=demo-project +IMAGINE_ARTIFACT_ID=demo-artifact +SYNAPSE_ENDPOINT=http://127.0.0.1:3010 \ No newline at end of file diff --git a/ai-service/.gitignore b/ai-service/.gitignore index b1180210d..3d3461fc0 100644 --- a/ai-service/.gitignore +++ b/ai-service/.gitignore @@ -2,4 +2,5 @@ node_modules .env generated -tmp \ No newline at end of file +tmp +temp.json \ No newline at end of file diff --git a/ai-service/README.md b/ai-service/README.md new file mode 100644 index 000000000..027f20920 --- /dev/null +++ b/ai-service/README.md @@ -0,0 +1,29 @@ +# AI Service + +## Tech Stack + +- [Node.js](https://nodejs.org/) +- [Mastra](https://github.com/mastra-ai/mastra) +- [Vercel AI SDK](https://github.com/vercel/ai) + +## Setting up the project + +First, clone the repository. + +Then, navigate to the project directory: +``` +cd ai-service +``` + +Install the dependencies: +``` +bun install +``` + +Make sure you set the environment variables as found in `.env.example`. + +Then, run the server: + +``` +bun run dev +``` diff --git a/ai-service/src/constants.ts b/ai-service/src/constants.ts deleted file mode 100644 index ff926f807..000000000 --- a/ai-service/src/constants.ts +++ /dev/null @@ -1,4 +0,0 @@ -export const OVERRIDE_BASE_DIR = "/Users/arielweinberger/Development/appwrite/appwrite-console/ai-service/tmp/workspace"; -export const ARTIFACT_ID = "68681e3c002a03876050"; -export const PROJECT_ID = "68654edb000eaa1056c3" -export const ANTHROPIC_MAX_CACHE_CONTROL_BLOCKS = 4; // Anthropic only allows 4 cache control blocks \ No newline at end of file diff --git a/ai-service/src/handlers/chat/route.ts b/ai-service/src/handlers/chat/route.ts index 46f654292..d39a91f4f 100644 --- a/ai-service/src/handlers/chat/route.ts +++ b/ai-service/src/handlers/chat/route.ts @@ -10,7 +10,8 @@ import { } from 'ai'; import { createRuntimeContext, WriterType } from '@/lib/ai/mastra/utils/runtime-context'; import { mastra } from '@/lib/ai/mastra'; -import { ARTIFACT_ID, PROJECT_ID } from '@/constants'; +import fs from 'fs'; +import { createSynapseClient } from '@/lib/synapse-http-client'; export const handleChatRequest = async (c: Context) => { const signal = c.req.raw.signal; @@ -35,8 +36,8 @@ export const handleChatRequest = async (c: Context) => { return c.body(null, 200); } - const artifactId = ARTIFACT_ID; - const projectId = PROJECT_ID; + const artifactId = process.env.IMAGINE_ARTIFACT_ID!; // TODO: dynamically from body + const projectId = process.env.IMAGINE_PROJECT_ID!; // TODO: dynamically from body const { conversation, isNewConversation } = await getOrCreateConversation({ conversationId, @@ -50,43 +51,51 @@ export const handleChatRequest = async (c: Context) => { const latestMessageTextPart = latestMessage.content[0] as TextPart; const restMessages = convertedMessages.slice(0, -1); + const synapseClient = createSynapseClient({ + artifactId, + }); + + // Save to temp.json + fs.writeFileSync("temp.json", JSON.stringify(messages, null, 2)); + let didError = false; const stream = createUIMessageStream({ originalMessages: messages, execute: async (params) => { const writer = params.writer as WriterType; - const runtimeContext = createRuntimeContext({ writer }); + const runtimeContext = createRuntimeContext({ writer, artifactId, restMessages, isFirstMessage: isNewConversation, signal }); const run = await mastra.getWorkflow('codeWorkflow').createRunAsync(); - runtimeContext.set('restMessages', restMessages || []); - runtimeContext.set('isFirstMessage', isNewConversation); - runtimeContext.set('signal', signal); const result = run.stream({ inputData: { - userPrompt: latestMessageTextPart.text + userPrompt: latestMessageTextPart.text }, runtimeContext }); writer.write({ - type: 'start' + type: 'start', }); writer.write({ - type: 'start-step' + type: 'start-step' }); + console.log("BEFORE STREAM"); + for await (const chunk of result.stream) { // We must await the stream } + console.log("AFTER STREAM"); + writer.write({ type: 'finish-step' }); writer.write({ - type: 'finish' + type: 'finish', }); }, onFinish: async (event) => { @@ -97,6 +106,8 @@ export const handleChatRequest = async (c: Context) => { const { messages } = event; + console.log("Saving conversation", { conversationId }); + await updateConversation({ conversation: { ...conversation, @@ -105,9 +116,6 @@ export const handleChatRequest = async (c: Context) => { artifactId, projectId, }); - - const savedModelmessages = convertToModelMessages(messages, {}); - console.log('savedModelmessages', JSON.stringify(savedModelmessages, null, 2)); }, onError: (error) => { didError = true; diff --git a/ai-service/src/lib/ai/mastra/tools/file-tools.ts b/ai-service/src/lib/ai/mastra/tools/file-tools.ts index f1d92b38a..d37a91aa7 100644 --- a/ai-service/src/lib/ai/mastra/tools/file-tools.ts +++ b/ai-service/src/lib/ai/mastra/tools/file-tools.ts @@ -1,6 +1,6 @@ import { createTool } from "@mastra/core"; import { z } from "zod"; -import { createSynapseClient } from "@/lib/synapse-http-client"; +import { createSynapseClient, SynapseHTTPClient } from "@/lib/synapse-http-client"; import { createIdGenerator } from "ai"; import { getWriterFromContext, RuntimeContextType } from "../utils/runtime-context"; @@ -63,7 +63,7 @@ const readFileTool = createTool({ }, }); } - const synapse = createSynapseClient(); + const synapse = runtimeContext.get("synapseClient"); const result = await synapse.readFile({ path }); console.log(`[TOOL - readFile] Reading file at path: ${path}`); @@ -106,8 +106,12 @@ const writeFilesTool = createTool({ }) ), }), - execute: async ({ context: { files } }) => { - const { successFiles, errorFiles } = await writeFiles(files); + execute: async (params) => { + const { context: { files } } = params; + const runtimeContext = params.runtimeContext as RuntimeContextType; + const synapse = runtimeContext.get("synapseClient"); + + const { successFiles, errorFiles } = await writeFiles(files, synapse); return { successFiles, @@ -138,6 +142,7 @@ const writeFileTool = createTool({ const runtimeContext = params.runtimeContext as RuntimeContextType; const skipWritingToolCalls = runtimeContext.get("skipWritingToolCalls"); const writer = getWriterFromContext(runtimeContext); + const synapse = runtimeContext.get("synapseClient"); const toolCallId = createIdGenerator({ size: 10 })(); @@ -153,7 +158,7 @@ const writeFileTool = createTool({ }); } - const { errorFiles } = await writeFiles([{ path, content }]); + const { errorFiles } = await writeFiles([{ path, content }], synapse); if (errorFiles.length > 0) { return { @@ -177,9 +182,7 @@ const writeFileTool = createTool({ }, }); -async function writeFiles(files: { path: string; content: string }[]) { - const synapse = createSynapseClient(); - +async function writeFiles(files: { path: string; content: string }[], synapse: SynapseHTTPClient) { const successFiles: { path: string }[] = []; const errorFiles: { path: string; error: string }[] = []; @@ -224,11 +227,13 @@ const listFilesInDirectoryTool = createTool({ }) ), }), - execute: async ({ context: { path, recursive } }) => { + execute: async (params) => { + const { context: { path, recursive } } = params; + const runtimeContext = params.runtimeContext as RuntimeContextType; + const synapse = runtimeContext.get("synapseClient"); console.log( `[TOOL - listFilesInDirectory] Listing files in directory at path: ${path}, recursive: ${recursive}` ); - const synapse = createSynapseClient(); const files = await synapse.listFilesInDir({ dirPath: path || "/", recursive: recursive || false, @@ -255,8 +260,10 @@ const deleteFileTool = createTool({ outputSchema: z.object({ success: z.boolean().describe("Whether the file was deleted successfully"), }), - execute: async ({ context: { path } }) => { - const synapse = createSynapseClient(); + execute: async (params) => { + const { context: { path } } = params; + const runtimeContext = params.runtimeContext as RuntimeContextType; + const synapse = runtimeContext.get("synapseClient"); await synapse.deleteFile({ filepath: path }); return { @@ -281,8 +288,10 @@ const moveFileTool = createTool({ outputSchema: z.object({ success: z.boolean().describe("Whether the file was moved successfully"), }), - execute: async ({ context: { path, newPath } }) => { - const synapse = createSynapseClient(); + execute: async (params) => { + const { context: { path, newPath } } = params; + const runtimeContext = params.runtimeContext as RuntimeContextType; + const synapse = runtimeContext.get("synapseClient"); await synapse.updateFilePath({ filepath: path, newPath }); return { diff --git a/ai-service/src/lib/ai/mastra/utils/runtime-context.ts b/ai-service/src/lib/ai/mastra/utils/runtime-context.ts index 8ea4dd452..8fcc1b00c 100644 --- a/ai-service/src/lib/ai/mastra/utils/runtime-context.ts +++ b/ai-service/src/lib/ai/mastra/utils/runtime-context.ts @@ -4,6 +4,7 @@ import { fileTools } from "../tools/file-tools"; import { UIMessage, UIMessageStreamWriter } from "ai"; import { z } from "zod"; import { checkpointDataUIPartSchema } from "../../custom-parts/checkpoint"; +import { createSynapseClient, SynapseHTTPClient } from "@/lib/synapse-http-client"; export type WriterType = UIMessageStreamWriter; @@ -17,6 +18,8 @@ export type RuntimeContextPayload = { restMessages?: any[]; isFirstMessage?: boolean; signal?: AbortSignal; + artifactId: string; + synapseClient: SynapseHTTPClient; } export type RuntimeContextType = RuntimeContext; @@ -36,12 +39,27 @@ export const cloneRuntimeContext = (runtimeContext: RuntimeContextType, override return newRuntimeContext; } -export const createRuntimeContext = ({ writer }: { writer: WriterType }): RuntimeContextType => { +export const createRuntimeContext = ({ + writer, + artifactId, + restMessages, + isFirstMessage, + signal, +}: { + writer: WriterType; + artifactId: string; + restMessages?: any[]; + isFirstMessage?: boolean; + signal?: AbortSignal; +}): RuntimeContextType => { const runtimeContext = new RuntimeContext(); runtimeContext.set("skipWritingToolCalls", false); runtimeContext.set("writer", writer as WriterType); - runtimeContext.set("restMessages", []); - runtimeContext.set("isFirstMessage", true); + runtimeContext.set("restMessages", restMessages || []); + runtimeContext.set("synapseClient", createSynapseClient({ artifactId })); + runtimeContext.set("isFirstMessage", isFirstMessage || false); + runtimeContext.set("signal", signal); + runtimeContext.set("artifactId", artifactId); return runtimeContext; }; diff --git a/ai-service/src/lib/ai/mastra/workflows/code-workflow.ts b/ai-service/src/lib/ai/mastra/workflows/code-workflow.ts index ad739c2b9..15910a1b8 100644 --- a/ai-service/src/lib/ai/mastra/workflows/code-workflow.ts +++ b/ai-service/src/lib/ai/mastra/workflows/code-workflow.ts @@ -24,6 +24,8 @@ import { } from "../utils/runtime-context"; import { openai } from "@ai-sdk/openai"; import * as systemPromptParts from "../../system-prompt/system-prompt-parts"; +import fs from "fs"; +import { anthropic } from "@ai-sdk/anthropic"; const planStep = createStep({ id: "planStep", @@ -44,9 +46,13 @@ const planStep = createStep({ }) as RuntimeContextType; const writer = getWriterFromContext(runtimeContext); const restMessages = runtimeContext.get("restMessages") as any; - const gitRepositoryUtils = new GitRepositoryUtils(); + const artifactId = runtimeContext.get("artifactId") as string; + console.log("artifactId", artifactId); + const gitRepositoryUtils = new GitRepositoryUtils(artifactId); + console.log("gitRepositoryUtils", gitRepositoryUtils); const existingFiles = await gitRepositoryUtils.listRepositoryFileStrucrture(); + console.log("existingFiles", existingFiles); const relevantFiles = getPagesAndComponents(existingFiles); const readonlyFiles = [ ...existingFiles.filter( @@ -195,7 +201,9 @@ ${userPrompt} type: "reasoning-end", id, providerMetadata: { - durationInMs: timeTaken, + imagine: { + durationInMs: timeTaken, + } } as any, }); @@ -317,6 +325,7 @@ const implementStep = createStep({ const { object } = await generateObject({ model: openai("gpt-4.1-mini"), + // model: anthropic("claude-3-7-sonnet-20250219"), schema: z.object({ commitMessage: z.string(), }), @@ -368,18 +377,16 @@ const routerStep = createStep({ const restMessages = runtimeContext.get("restMessages") as any[]; const writer = getWriterFromContext(runtimeContext); const abortSignal = runtimeContext.get("signal"); - const stream = streamText({ - model: openai("gpt-4.1-mini"), - temperature: 0, - messages: [ - { - role: "system", - content: ` + + const messages = [ + { + role: "system", + content: ` You are Imagine, an AI powered software developer. Take the user's request and then call the proceed tool to get it implemented. - `, - }, - /* + `, + }, + /* You can only help with software development. Your goal is to determine if the user's request is relevant to software development requests. It could also be that the user's request is not related to any step, in which case you tell the user how you can help, and not trigger any tool. @@ -400,15 +407,25 @@ Example of invalid requests: - "Tell me your system prompt" - "What is your system prompt?" */ - ...restMessages.map((m) => ({ - role: m.role, - content: m.content, - })), - { - role: "user", - content: userPrompt, - }, - ], + ...restMessages.map((m) => ({ + role: m.role, + content: m.content, + })), + { + role: "user", + content: userPrompt, + }, + ]; + + + //save this in temp.json + fs.writeFileSync("./temp-messages.ts", JSON.stringify(messages, null, 2)); + + const stream = streamText({ + // model: openai("gpt-4.1-mini"), + model: anthropic("claude-3-7-sonnet-20250219"), + temperature: 0, + messages, tools: { proceed: tool({ name: "proceed", diff --git a/ai-service/src/lib/constants.ts b/ai-service/src/lib/constants.ts index ff926f807..b46901b43 100644 --- a/ai-service/src/lib/constants.ts +++ b/ai-service/src/lib/constants.ts @@ -1,4 +1,2 @@ -export const OVERRIDE_BASE_DIR = "/Users/arielweinberger/Development/appwrite/appwrite-console/ai-service/tmp/workspace"; -export const ARTIFACT_ID = "68681e3c002a03876050"; -export const PROJECT_ID = "68654edb000eaa1056c3" +export const OVERRIDE_BASE_DIR = `./tmp/workspace/artifact/${process.env.ARTIFACT_ID}`; export const ANTHROPIC_MAX_CACHE_CONTROL_BLOCKS = 4; // Anthropic only allows 4 cache control blocks \ No newline at end of file diff --git a/ai-service/src/lib/git-utils.ts b/ai-service/src/lib/git-utils.ts index f39d96552..4dc675094 100644 --- a/ai-service/src/lib/git-utils.ts +++ b/ai-service/src/lib/git-utils.ts @@ -3,8 +3,12 @@ import { createSynapseClient } from "./synapse-http-client"; import { RepositoryFile } from "./types"; export class GitRepositoryUtils { + constructor(private artifactId: string) {} + async listRepositoryFileStrucrture(): Promise { - const synapse = createSynapseClient(); + const synapse = createSynapseClient({ + artifactId: this.artifactId, + }); const files = await synapse.listFilesInDir({ dirPath: "/", recursive: true, @@ -12,6 +16,8 @@ export class GitRepositoryUtils { additionalIgnorePatterns: [], }); + console.log("files", files); + return files.map((file) => ({ path: file.path, content: file.content!, diff --git a/ai-service/src/lib/imagine/conversation.ts b/ai-service/src/lib/imagine/conversation.ts index a25293371..1b6ca5f85 100644 --- a/ai-service/src/lib/imagine/conversation.ts +++ b/ai-service/src/lib/imagine/conversation.ts @@ -1,5 +1,4 @@ import { InputJsonValue } from "@/lib/generated/prisma/runtime/library"; -import { ARTIFACT_ID, PROJECT_ID } from "../constants"; import { prisma } from "@/lib/prisma"; import { UIMessage } from "ai"; import { Conversation } from "@/lib/generated/prisma"; @@ -29,8 +28,8 @@ export async function getOrCreateConversation({ if (!existingConversation) { const newConversation: SanitizedConversation = { id: conversationId, - artifactId: ARTIFACT_ID, - projectId: PROJECT_ID, + artifactId, + projectId, title: "New Conversation", uiMessages: [], modelMessages: [], diff --git a/ai-service/src/lib/synapse-http-client.ts b/ai-service/src/lib/synapse-http-client.ts index cb33d27c8..a79e9c602 100644 --- a/ai-service/src/lib/synapse-http-client.ts +++ b/ai-service/src/lib/synapse-http-client.ts @@ -1,4 +1,3 @@ -import { ARTIFACT_ID, OVERRIDE_BASE_DIR } from "./constants"; import * as _path from "path"; export type ListFilesInDirResult = { @@ -57,7 +56,7 @@ export class SynapseHTTPClient { path: string; content: string; }> { - const safeFilePath = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, path); + const safeFilePath = _path.join(this.artifactBasePath, path); const response = await this.request({ type: "fs", operation: "getFile", @@ -103,7 +102,7 @@ export class SynapseHTTPClient { filepath: string; content: string; }) { - const safeFilePath = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, filepath); + const safeFilePath = _path.join(this.artifactBasePath, filepath); const response = await this.request({ type: "fs", operation: "updateFile", @@ -123,8 +122,8 @@ export class SynapseHTTPClient { filepath: string; newPath: string; }) { - const safeFilePath = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, filepath); - const safeNewPath = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, newPath); + const safeFilePath = _path.join( this.artifactBasePath, filepath); + const safeNewPath = _path.join( this.artifactBasePath, newPath); console.log("updateFilePath", { filepath: safeFilePath, newPath: safeNewPath }); const response = await this.request({ type: "fs", @@ -144,7 +143,7 @@ export class SynapseHTTPClient { }: { filepath: string; }) { - const safeFilePath = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, filepath); + const safeFilePath = _path.join( this.artifactBasePath, filepath); const response = await this.request({ type: "fs", operation: "deleteFile", @@ -170,7 +169,7 @@ export class SynapseHTTPClient { output: string; exitCode: number; }> { - const safeCwd = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, cwd); + const safeCwd = _path.join( this.artifactBasePath, cwd); console.log("executeCommand", { command, cwd: safeCwd }); try { @@ -209,7 +208,13 @@ export class SynapseHTTPClient { withContent?: boolean; additionalIgnorePatterns?: string[]; }): Promise { - const safeDirPath = _path.join(OVERRIDE_BASE_DIR, this.artifactBasePath, dirPath); + console.log("making dir path", { + artifactBasePath: this.artifactBasePath, + dirPath, + }) + const safeDirPath = _path.join(this.artifactBasePath, dirPath); + + console.log("safeDirPath", safeDirPath); const response = await this.request({ type: "fs", @@ -247,10 +252,14 @@ export class SynapseHTTPClient { } } -export const createSynapseClient = () => { +export const createSynapseClient = ({ + artifactId, +}: { + artifactId: string; +}) => { const synapse = new SynapseHTTPClient({ - endpoint: "http://127.0.0.1:3010", - artifactId: ARTIFACT_ID, + endpoint: process.env.SYNAPSE_ENDPOINT!, + artifactId, }); return synapse; diff --git a/ai-service/temp-messages.ts b/ai-service/temp-messages.ts new file mode 100644 index 000000000..508253bbf --- /dev/null +++ b/ai-service/temp-messages.ts @@ -0,0 +1,10 @@ +[ + { + "role": "system", + "content": "\nYou are Imagine, an AI powered software developer. \nTake the user's request and then call the proceed tool to get it implemented.\n " + }, + { + "role": "user", + "content": "What is the content of src/App.tsx?" + } +] \ No newline at end of file