diff --git a/packages/create-llama/templates/components/use-cases/typescript/code_generator/src/app/workflow.ts b/packages/create-llama/templates/components/use-cases/typescript/code_generator/src/app/workflow.ts index 0e8c26e09..0700e0fd4 100644 --- a/packages/create-llama/templates/components/use-cases/typescript/code_generator/src/app/workflow.ts +++ b/packages/create-llama/templates/components/use-cases/typescript/code_generator/src/app/workflow.ts @@ -1,5 +1,5 @@ import { extractLastArtifact } from "@llamaindex/server"; -import { ChatMemoryBuffer, LLM, MessageContent, Settings } from "llamaindex"; +import { ChatMemoryBuffer, MessageContent, Settings } from "llamaindex"; import { agentStreamEvent, @@ -12,12 +12,6 @@ import { import { z } from "zod"; -export const workflowFactory = async (reqBody: any) => { - const workflow = createCodeArtifactWorkflow(reqBody); - - return workflow; -}; - export const RequirementSchema = z.object({ next_step: z.enum(["answering", "coding"]), language: z.string().nullable().optional(), @@ -71,32 +65,28 @@ const artifactEvent = workflowEvent<{ }; }>(); -export function createCodeArtifactWorkflow(reqBody: any, llm?: LLM) { - if (!llm) { - llm = Settings.llm; - } +export function workflowFactory(reqBody: any) { + const llm = Settings.llm; + const { withState, getContext } = createStatefulMiddleware(() => { return { - memory: new ChatMemoryBuffer({ - llm, - chatHistory: reqBody.chatHistory, - }), + memory: new ChatMemoryBuffer({ llm }), lastArtifact: extractLastArtifact(reqBody), }; }); const workflow = withState(createWorkflow()); - workflow.handle([startAgentEvent], async ({ data: { userInput } }) => { + workflow.handle([startAgentEvent], async ({ data }) => { + const { userInput, chatHistory = [] } = data; // Prepare chat history const { state } = getContext(); // Put user input to the memory if (!userInput) { throw new Error("Missing user input to start the workflow"); } - state.memory.put({ - role: "user", - content: userInput, - }); + state.memory.set(chatHistory); + state.memory.put({ role: "user", content: userInput }); + return planEvent.with({ userInput: userInput, }); diff --git a/packages/create-llama/templates/components/use-cases/typescript/document_generator/src/app/workflow.ts b/packages/create-llama/templates/components/use-cases/typescript/document_generator/src/app/workflow.ts index 8e8a52b7d..70ba93cb9 100644 --- a/packages/create-llama/templates/components/use-cases/typescript/document_generator/src/app/workflow.ts +++ b/packages/create-llama/templates/components/use-cases/typescript/document_generator/src/app/workflow.ts @@ -1,5 +1,5 @@ import { extractLastArtifact } from "@llamaindex/server"; -import { ChatMemoryBuffer, LLM, MessageContent, Settings } from "llamaindex"; +import { ChatMemoryBuffer, MessageContent, Settings } from "llamaindex"; import { agentStreamEvent, @@ -12,12 +12,6 @@ import { import { z } from "zod"; -export const workflowFactory = async (reqBody: any) => { - const workflow = createDocumentArtifactWorkflow(reqBody); - - return workflow; -}; - export const DocumentRequirementSchema = z.object({ type: z.enum(["markdown", "html"]), title: z.string(), @@ -74,32 +68,28 @@ const artifactEvent = workflowEvent<{ }; }>(); -export function createDocumentArtifactWorkflow(reqBody: any, llm?: LLM) { - if (!llm) { - llm = Settings.llm; - } +export function workflowFactory(reqBody: any) { + const llm = Settings.llm; + const { withState, getContext } = createStatefulMiddleware(() => { return { - memory: new ChatMemoryBuffer({ - llm, - chatHistory: reqBody.chatHistory, - }), + memory: new ChatMemoryBuffer({ llm }), lastArtifact: extractLastArtifact(reqBody), }; }); const workflow = withState(createWorkflow()); - workflow.handle([startAgentEvent], async ({ data: { userInput } }) => { + workflow.handle([startAgentEvent], async ({ data }) => { + const { userInput, chatHistory = [] } = data; // Prepare chat history const { state } = getContext(); // Put user input to the memory if (!userInput) { throw new Error("Missing user input to start the workflow"); } - state.memory.put({ - role: "user", - content: userInput, - }); + state.memory.set(chatHistory); + state.memory.put({ role: "user", content: userInput }); + return planEvent.with({ userInput, context: state.lastArtifact