From 0d8eb368a0643837e4143ec29224dd11d527ce5c Mon Sep 17 00:00:00 2001 From: bracesproul Date: Thu, 10 Oct 2024 12:55:52 -0700 Subject: [PATCH] cr --- package.json | 2 +- src/agent/open-canvas/index.ts | 3 ++- .../open-canvas/nodes/generateFollowup.ts | 1 + src/agent/open-canvas/nodes/reflect.ts | 10 ++++++---- src/agent/open-canvas/nodes/rewriteArtifact.ts | 6 ------ .../open-canvas/nodes/rewriteArtifactTheme.ts | 6 ------ .../nodes/rewriteCodeArtifactTheme.ts | 6 ------ src/agent/reflection/index.ts | 18 ++++++++++++++---- src/agent/reflection/prompts.ts | 4 ++++ src/app/api/[..._path]/route.ts | 2 +- src/hooks/useGraph.tsx | 3 ++- yarn.lock | 8 ++++---- 12 files changed, 35 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index fb2cc75f..d98d9173 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "@codemirror/lang-php": "^6.0.1", "@codemirror/lang-python": "^6.1.6", "@langchain/anthropic": "^0.3.3", - "@langchain/core": "^0.3.3", + "@langchain/core": "^0.3.9", "@langchain/langgraph": "^0.2.10", "@langchain/langgraph-sdk": "^0.0.14", "@langchain/openai": "^0.3.5", diff --git a/src/agent/open-canvas/index.ts b/src/agent/open-canvas/index.ts index 3b88d08a..102493d2 100644 --- a/src/agent/open-canvas/index.ts +++ b/src/agent/open-canvas/index.ts @@ -9,6 +9,7 @@ import { updateArtifact } from "./nodes/updateArtifact"; import { respondToQuery } from "./nodes/respondToQuery"; import { rewriteCodeArtifactTheme } from "./nodes/rewriteCodeArtifactTheme"; import { reflect } from "../reflection"; +import { reflectNode } from "./nodes/reflect"; const defaultInputs: Omit< typeof OpenCanvasGraphAnnotation.State, @@ -58,7 +59,7 @@ const builder = new StateGraph(OpenCanvasGraphAnnotation) .addNode("generateArtifact", generateArtifact) .addNode("generateFollowup", generateFollowup) .addNode("cleanState", cleanState) - .addNode("reflect", reflect) + .addNode("reflect", reflectNode) // Edges .addEdge("generateArtifact", "generateFollowup") .addEdge("updateArtifact", "generateFollowup") diff --git a/src/agent/open-canvas/nodes/generateFollowup.ts b/src/agent/open-canvas/nodes/generateFollowup.ts index ef67784b..2b8c3b37 100644 --- a/src/agent/open-canvas/nodes/generateFollowup.ts +++ b/src/agent/open-canvas/nodes/generateFollowup.ts @@ -36,6 +36,7 @@ export const generateFollowup = async ( recentArtifact.content ).replace("{reflections}", memoriesAsString); + // TODO: Include the chat history as well. const response = await smallModel.invoke([ { role: "user", content: formattedPrompt }, ]); diff --git a/src/agent/open-canvas/nodes/reflect.ts b/src/agent/open-canvas/nodes/reflect.ts index ce24836c..4d36d7c1 100644 --- a/src/agent/open-canvas/nodes/reflect.ts +++ b/src/agent/open-canvas/nodes/reflect.ts @@ -2,12 +2,14 @@ import { Client } from "@langchain/langgraph-sdk"; import { OpenCanvasGraphAnnotation } from "../state"; import { LangGraphRunnableConfig } from "@langchain/langgraph"; -export const reflect = async ( +export const reflectNode = async ( state: typeof OpenCanvasGraphAnnotation.State, config: LangGraphRunnableConfig ) => { - const langGraphClient = new Client(); - + const langGraphClient = new Client({ + apiUrl: `http://localhost:${process.env.PORT}`, + }); + console.log("reflectNode", state); const selectedArtifact = state.selectedArtifactId ? state.artifacts.find((art) => art.id === state.selectedArtifactId) : state.artifacts[state.artifacts.length - 1]; @@ -19,7 +21,7 @@ export const reflect = async ( configurable: { // Ensure we pass in the current graph's assistant ID as this is // how we fetch & store the memories. - assistant_id: config.configurable?.assistant_id, + open_canvas_assistant_id: config.configurable?.assistant_id, }, }; diff --git a/src/agent/open-canvas/nodes/rewriteArtifact.ts b/src/agent/open-canvas/nodes/rewriteArtifact.ts index 0c363c4b..59c28c06 100644 --- a/src/agent/open-canvas/nodes/rewriteArtifact.ts +++ b/src/agent/open-canvas/nodes/rewriteArtifact.ts @@ -56,11 +56,5 @@ export const rewriteArtifact = async ( return { artifacts: [newArtifact], - selectedArtifactId: undefined, - highlighted: undefined, - language: undefined, - artifactLength: undefined, - regenerateWithEmojis: undefined, - readingLevel: undefined, }; }; diff --git a/src/agent/open-canvas/nodes/rewriteArtifactTheme.ts b/src/agent/open-canvas/nodes/rewriteArtifactTheme.ts index ef000fea..20b2eab0 100644 --- a/src/agent/open-canvas/nodes/rewriteArtifactTheme.ts +++ b/src/agent/open-canvas/nodes/rewriteArtifactTheme.ts @@ -112,11 +112,5 @@ export const rewriteArtifactTheme = async ( return { artifacts: [newArtifact], - selectedArtifactId: undefined, - highlighted: undefined, - language: undefined, - artifactLength: undefined, - regenerateWithEmojis: undefined, - readingLevel: undefined, }; }; diff --git a/src/agent/open-canvas/nodes/rewriteCodeArtifactTheme.ts b/src/agent/open-canvas/nodes/rewriteCodeArtifactTheme.ts index 69466ac5..3fd953ce 100644 --- a/src/agent/open-canvas/nodes/rewriteCodeArtifactTheme.ts +++ b/src/agent/open-canvas/nodes/rewriteCodeArtifactTheme.ts @@ -78,11 +78,5 @@ export const rewriteCodeArtifactTheme = async ( return { artifacts: [newArtifact], - selectedArtifactId: undefined, - highlighted: undefined, - language: undefined, - artifactLength: undefined, - regenerateWithEmojis: undefined, - readingLevel: undefined, }; }; diff --git a/src/agent/reflection/index.ts b/src/agent/reflection/index.ts index 3dbfc396..a4470365 100644 --- a/src/agent/reflection/index.ts +++ b/src/agent/reflection/index.ts @@ -6,7 +6,7 @@ import { } from "@langchain/langgraph"; import { ReflectionGraphAnnotation, ReflectionGraphReturnType } from "./state"; import { Reflections } from "../../types"; -import { REFLECT_SYSTEM_PROMPT } from "./prompts"; +import { REFLECT_SYSTEM_PROMPT, REFLECT_USER_PROMPT } from "./prompts"; import { z } from "zod"; import { ensureStoreInConfig, formatReflections } from "../utils"; @@ -15,9 +15,9 @@ export const reflect = async ( config: LangGraphRunnableConfig ): Promise => { const store = ensureStoreInConfig(config); - const assistantId = config.configurable?.assistant_id; + const assistantId = config.configurable?.open_canvas_assistant_id; if (!assistantId) { - throw new Error("`assistant_id` not found in configurable"); + throw new Error("`open_canvas_assistant_id` not found in configurable"); } const memoryNamespace = ["memories", assistantId]; const memoryKey = "reflection"; @@ -48,12 +48,22 @@ export const reflect = async ( state.artifact?.content ?? "No artifact found." ).replace("{reflections}", memoriesAsString); + const formattedUserPrompt = REFLECT_USER_PROMPT.replace( + "{conversation}", + state.messages + .map((msg) => `<${msg.getType()}>\n${msg.content}\n`) + .join("\n\n") + ); + const result = await model.invoke([ { role: "system", content: formattedSystemPrompt, }, - ...state.messages, + { + role: "user", + content: formattedUserPrompt, + }, ]); const newMemories = { diff --git a/src/agent/reflection/prompts.ts b/src/agent/reflection/prompts.ts index a5777100..eb3b4b35 100644 --- a/src/agent/reflection/prompts.ts +++ b/src/agent/reflection/prompts.ts @@ -28,3 +28,7 @@ Your job is to take all of the context and existing reflections and re-generate Finally, use the 'generate_reflections' tool to generate the new, full list of reflections.`; + +export const REFLECT_USER_PROMPT = `Here is my conversation: + +{conversation}`; diff --git a/src/app/api/[..._path]/route.ts b/src/app/api/[..._path]/route.ts index 17ba5f32..bfd4875f 100644 --- a/src/app/api/[..._path]/route.ts +++ b/src/app/api/[..._path]/route.ts @@ -30,7 +30,7 @@ async function handleRequest(req: NextRequest, method: string) { options.body = await req.text(); } - const apiUrl = process.env.LANGGRAPH_API_URL ?? "http://localhost:50014"; + const apiUrl = process.env.LANGGRAPH_API_URL ?? "http://localhost:50930"; const res = await fetch(`${apiUrl}/${path}${queryString}`, options); const headers = new Headers({ diff --git a/src/hooks/useGraph.tsx b/src/hooks/useGraph.tsx index 0d15be31..dbf7fafc 100644 --- a/src/hooks/useGraph.tsx +++ b/src/hooks/useGraph.tsx @@ -13,7 +13,7 @@ import { import { parsePartialJson } from "@langchain/core/output_parsers"; import { useRuns } from "./useRuns"; import { reverseCleanContent } from "@/lib/normalize_string"; -import { getCookie } from "@/lib/cookies"; +import { getCookie, setCookie } from "@/lib/cookies"; // import { DEFAULT_ARTIFACTS, DEFAULT_MESSAGES } from "@/lib/dummy"; interface ArtifactToolResponse { @@ -97,6 +97,7 @@ export function useGraph() { graphId: "agent", }); setAssistantId(assistant.assistant_id); + setCookie("oc_assistant_id", assistant.assistant_id); }; const streamMessage = async (params: GraphInput) => { diff --git a/yarn.lock b/yarn.lock index 54ca5e4b..b7d2692c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -497,10 +497,10 @@ zod "^3.22.4" zod-to-json-schema "^3.22.4" -"@langchain/core@^0.3.3": - version "0.3.3" - resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.3.3.tgz#af12fd767ff2fcedb0a71bd79e6588d7dd52b6b6" - integrity sha512-WAtkmhbdl2T41qzimTzhb3pXCHQxO4onqxzPxgdf3KftQdTwLq0YYBDhozRMZLNAd/+cfH0ymZGaZSsnc9Ogsg== +"@langchain/core@^0.3.9": + version "0.3.9" + resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.3.9.tgz#1fa75be84a22d951946277bc4ad3ea656c77fafc" + integrity sha512-Rttr9FuFwU+CWIoEyuLqQUPYg+3pKL1YpDgo3nvoDVhinoHqwGQ7aNGzZ/Sf+qASMi76sPSLm+75pHMJwwOiWg== dependencies: ansi-styles "^5.0.0" camelcase "6"