forked from langchain-ai/open-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Show current reflections in UI
- Loading branch information
1 parent
571f2c9
commit 24212fd
Showing
7 changed files
with
170 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { Client } from "@langchain/langgraph-sdk"; | ||
import { LANGGRAPH_API_URL } from "@/constants"; | ||
|
||
export async function POST(req: NextRequest) { | ||
const { assistantId } = await req.json(); | ||
|
||
if (!assistantId) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: "`assistantId` is required to fetch stored data.", | ||
}), | ||
{ | ||
status: 400, | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
|
||
const lgClient = new Client({ | ||
apiKey: process.env.LANGCHAIN_API_KEY, | ||
apiUrl: LANGGRAPH_API_URL, | ||
}); | ||
|
||
const memoryNamespace = ["memories", assistantId]; | ||
const memoryKey = "reflection"; | ||
|
||
try { | ||
await lgClient.store.deleteItem(memoryNamespace, memoryKey); | ||
|
||
return new NextResponse(JSON.stringify({ success: true }), { | ||
status: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} catch (error) { | ||
return new NextResponse( | ||
JSON.stringify({ error: "Failed to share run after multiple attempts." }), | ||
{ | ||
status: 500, | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { Client } from "@langchain/langgraph-sdk"; | ||
import { LANGGRAPH_API_URL } from "@/constants"; | ||
|
||
export async function POST(req: NextRequest) { | ||
const { assistantId } = await req.json(); | ||
|
||
if (!assistantId) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: "`assistantId` is required to fetch stored data.", | ||
}), | ||
{ | ||
status: 400, | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
|
||
const lgClient = new Client({ | ||
apiKey: process.env.LANGCHAIN_API_KEY, | ||
apiUrl: LANGGRAPH_API_URL, | ||
}); | ||
|
||
const memoryNamespace = ["memories", assistantId]; | ||
const memoryKey = "reflection"; | ||
|
||
try { | ||
const memories = await lgClient.store.getItem(memoryNamespace, memoryKey); | ||
|
||
return new NextResponse(JSON.stringify({ memories: memories?.value }), { | ||
status: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} catch (error) { | ||
return new NextResponse( | ||
JSON.stringify({ error: "Failed to share run after multiple attempts." }), | ||
{ | ||
status: 500, | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1 @@ | ||
const COOKIE_PREFIX = "content_composer"; | ||
export const ASSISTANT_ID_COOKIE = `${COOKIE_PREFIX}_assistant_id`; | ||
export const HAS_SEEN_DIALOG = `${COOKIE_PREFIX}_has_seen_dialog`; | ||
export const USER_TIED_TO_ASSISTANT = `${COOKIE_PREFIX}_user_tied_to_assistant`; | ||
export const USER_ID_COOKIE = `${COOKIE_PREFIX}_user_id`; | ||
export const DEFAULT_SYSTEM_RULES = [ | ||
"Ensure the tone and style of the generated text matches the user's desired tone and style.", | ||
"Be concise and avoid unnecessary information unless the user specifies otherwise.", | ||
"Maintain consistency in terminology and phrasing as per the user's revisions.", | ||
"Be mindful of the context and purpose of the text to ensure it aligns with the user's goals.", | ||
]; | ||
export const LANGGRAPH_API_URL = process.env.LANGGRAPH_API_URL ?? "http://localhost:58740"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Reflections } from "@/types"; | ||
import { useEffect, useState } from "react"; | ||
import { useToast } from "./use-toast"; | ||
|
||
export function useStore(assistantId: string | undefined) { | ||
const { toast } = useToast(); | ||
const [reflections, setReflections] = useState<Reflections & { assistantId: string }>(); | ||
|
||
useEffect(() => { | ||
if (!assistantId || typeof window === "undefined") return; | ||
// Don't re-fetch reflections if they already exist & are for the same assistant | ||
if ((reflections?.content || reflections?.styleRules) && reflections.assistantId === assistantId) return; | ||
|
||
getReflections(); | ||
}, [assistantId]); | ||
|
||
const getReflections = async (): Promise<void> => { | ||
if (!assistantId) { | ||
return; | ||
} | ||
const res = await fetch("/api/store/get", { | ||
method: "POST", | ||
body: JSON.stringify({ assistantId }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
return; | ||
} | ||
|
||
const { memories } = await res.json(); | ||
setReflections({ | ||
...memories, | ||
assistantId, | ||
}); | ||
}; | ||
|
||
const deleteReflections = async (): Promise<boolean> => { | ||
if (!assistantId) { | ||
return false; | ||
} | ||
const res = await fetch("/api/store/delete", { | ||
method: "POST", | ||
body: JSON.stringify({ assistantId }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
return false; | ||
} | ||
|
||
const { success } = await res.json(); | ||
if (success) { | ||
setReflections(undefined); | ||
} else { | ||
toast({ | ||
title: "Failed to delete reflections", | ||
description: "Please try again later.", | ||
}) | ||
} | ||
return success; | ||
}; | ||
|
||
return { | ||
reflections, | ||
deleteReflections, | ||
}; | ||
} |