Skip to content

Commit

Permalink
feat: Show current reflections in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Oct 11, 2024
1 parent 571f2c9 commit 24212fd
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/app/api/[..._path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LANGGRAPH_API_URL } from "../../../constants";
import { NextRequest, NextResponse } from "next/server";

function getCorsHeaders() {
Expand Down Expand Up @@ -30,8 +31,7 @@ async function handleRequest(req: NextRequest, method: string) {
options.body = await req.text();
}

const apiUrl = process.env.LANGGRAPH_API_URL ?? "http://localhost:58740";
const res = await fetch(`${apiUrl}/${path}${queryString}`, options);
const res = await fetch(`${LANGGRAPH_API_URL}/${path}${queryString}`, options);

const headers = new Headers({
...getCorsHeaders(),
Expand Down
44 changes: 44 additions & 0 deletions src/app/api/store/delete/route.ts
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" },
}
);
}
}
44 changes: 44 additions & 0 deletions src/app/api/store/get/route.ts
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" },
}
);
}
}
6 changes: 6 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ArtifactRenderer } from "@/components/artifacts/ArtifactRenderer";
import { ContentComposerChatInterface } from "@/components/ContentComposer";
import { useToast } from "@/hooks/use-toast";
import { useGraph } from "@/hooks/useGraph";
import { useStore } from "@/hooks/useStore";
import { getLanguageTemplate } from "@/lib/get_language_template";
import { cn } from "@/lib/utils";
import { ProgrammingLanguageOptions } from "@/types";
Expand All @@ -27,7 +28,12 @@ export default function Home() {
selectedArtifactId,
createThread,
setArtifactContent,
assistantId,
} = useGraph();
const {
reflections,
deleteReflections
} = useStore(assistantId);

const createThreadWithChatStarted = async () => {
setChatStarted(false);
Expand Down
12 changes: 1 addition & 11 deletions src/constants.ts
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";
1 change: 1 addition & 0 deletions src/hooks/useGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ export function useGraph() {
artifacts,
selectedArtifactId,
messages,
assistantId,
setSelectedArtifact: setSelectedArtifactById,
setArtifacts,
setMessages,
Expand Down
72 changes: 72 additions & 0 deletions src/hooks/useStore.tsx
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,
};
}

0 comments on commit 24212fd

Please sign in to comment.