Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix NodeJSExpress template; Simplify template state
Tomas Valenta4325a8cc125e
diff --git a/components/Editor/Sidebar/Agent/index.tsx b/components/Editor/Sidebar/Agent/index.tsxindex 24d9f61c0..de7f5499d 100644--- a/components/Editor/Sidebar/Agent/index.tsx+++ b/components/Editor/Sidebar/Agent/index.tsx@@ -54,8 +54,8 @@ function Agent({const router = useRouter()const [creds] = useModelProviderArgs()- const model = selector.use.model()- const missingCreds = getMissingCreds(model.provider, creds)+ const modelConfig = selector.use.selectedModelConfig()+ const missingCreds = modelConfig ? getMissingCreds(modelConfig?.provider, creds) : []async function saveAnswer({logID,diff --git a/components/Editor/Sidebar/Model/ProviderCard/ModelSection.tsx b/components/Editor/Sidebar/Model/ProviderCard/ModelSection.tsxindex 184d11d1e..a94e56c6c 100644--- a/components/Editor/Sidebar/Model/ProviderCard/ModelSection.tsx+++ b/components/Editor/Sidebar/Model/ProviderCard/ModelSection.tsx@@ -1,13 +1,13 @@import React, { useState } from 'react'+import clsx from 'clsx'+import { CheckSquare, ChevronDown, Square } from 'lucide-react'-import { ModelProvider, ModelTemplate } from 'state/model'+import { ModelProvider, ModelTemplate, ModelConfig } from 'state/model'import Text from 'components/Text'-import { CheckSquare, ChevronDown, Square } from 'lucide-react'-import clsx from 'clsx'-import ArgHandler from './ArgHandler'-import { ModelConfig } from 'state/store'import { useStateStore } from 'state/StoreProvider'+import ArgHandler from './ArgHandler'+interface Props {provider: ModelProvidermodelTemplate: Omit<ModelTemplate, 'provider'>diff --git a/components/Editor/Sidebar/Prompt/PromptEditor.tsx b/components/Editor/Sidebar/Prompt/PromptEditor.tsxindex 02472fa5b..dc8ce8c5b 100644--- a/components/Editor/Sidebar/Prompt/PromptEditor.tsx+++ b/components/Editor/Sidebar/Prompt/PromptEditor.tsx@@ -6,7 +6,7 @@ import Text from 'components/Text'import useDocEditor from 'hooks/useDocEditor'import { Reference } from 'editor/referenceType'import { useStateStore } from 'state/StoreProvider'-import { ModelConfig } from 'state/store'+import { ModelConfig } from 'state/model'export interface Props {title?: stringdiff --git a/components/Editor/Sidebar/index.tsx b/components/Editor/Sidebar/index.tsxindex ffdf6afbb..880babb8d 100644--- a/components/Editor/Sidebar/index.tsx+++ b/components/Editor/Sidebar/index.tsx@@ -3,51 +3,61 @@ import clsx from 'clsx'import useSWRMutation from 'swr/mutation'import { projects } from '@prisma/client'-import { defaultTemplateID, PromptPart, Route } from 'state/store'import { useLatestDeployment } from 'hooks/useLatestDeployment'import { useStateStore } from 'state/StoreProvider'import { ModelConfig, getModelArgs } from 'state/model'-import useModelProviderArgs from 'hooks/useModelProviderArgs'-import { defaultPromptTemplate, evaluatePrompt } from 'state/prompt'+import useModelProviderArgs, { Creds } from 'hooks/useModelProviderArgs'+import { evaluatePrompt } from 'state/prompt'import Agent from './Agent'import Envs from './Envs'import Model from './Model'import Prompt from './Prompt'import { MenuSection } from '../SidebarMenu'+import { EnvVar } from 'state/envs'+import { Instructions, InstructionsTransform } from 'state/instruction'export interface Props {project: projectsactiveMenuSection?: MenuSection}++interface PostGenerateBody {+ projectID: string+ modelConfig: ModelConfig+ envs: EnvVar[]+}+async function handlePostGenerate(url: string, { arg }: {arg: {controller: AbortController,+ creds: Creds,projectID: string,- route: Route,modelConfig: ModelConfig,- promptTemplate: PromptPart[],- envs: { key: string, value: string }[],+ instructions: Instructions,+ instructionsTransform: InstructionsTransform,+ envs: EnvVar[],}}) {+ const body: PostGenerateBody = {+ projectID: arg.projectID,+ modelConfig: {+ ...arg.modelConfig,+ args: getModelArgs(arg.modelConfig, arg.creds),+ prompt: evaluatePrompt(+ arg.instructions,+ arg.instructionsTransform,+ arg.modelConfig.prompt,+ ),+ },+ envs: arg.envs,+ }+return await fetch(url, {signal: arg.controller.signal,method: 'POST',- body: JSON.stringify({- projectID: arg.projectID,- routeID: arg.route.id,- prompt: evaluatePrompt(- arg.route.blocks,- arg.promptTemplate,- {- Method: arg.route.method.toLowerCase(),- Route: arg.route.route,- },- ),- modelConfig: arg.modelConfig,- envs: arg.envs,- }),+ body: JSON.stringify(body),headers: {'Content-Type': 'application/json',},@@ -73,21 +83,14 @@ function Sidebar({const [selectors] = useStateStore()const envs = selectors.use.envs()- const model = selectors.use.model()-- const prompt = selectors.use.modelSetups().find(p =>- p.templateID === defaultTemplateID &&- p.provider === model.provider &&- p.modelName === model.name- )?.prompt || defaultPromptTemplate-const modelConfig = selectors.use.selectedModelConfig()+ const instructions = selectors.use.instructions()+ const instructionsTransform = selectors.use.instructionsTransform()const [creds] = useModelProviderArgs()async function deploy() {- const config = getModelArgs(model, creds)- if (!config) {+ if (!modelConfig) {console.error('Cannot get model config')return}@@ -97,10 +100,11 @@ function Sidebar({await generate({controller,- route,projectID: project.id,- promptTemplate: prompt,- modelConfig: config,+ modelConfig,+ creds,+ instructions,+ instructionsTransform,envs,})}@@ -132,12 +136,6 @@ function Sidebar({`,)}>- {/* {activeMenuSection === MenuSection.Deploy &&- <Deploy />- } */}- {/* {activeMenuSection === MenuSection.Context &&- <Context />- } */}{activeMenuSection === MenuSection.Envs &&<Envs />}diff --git a/components/Editor/Template/NodeJSExpressTemplate/Instructions.tsx b/components/Editor/Template/NodeJSExpressTemplate/Instructions.tsxindex 518c8f26b..8b45f7638 100644--- a/components/Editor/Template/NodeJSExpressTemplate/Instructions.tsx+++ b/components/Editor/Template/NodeJSExpressTemplate/Instructions.tsx@@ -1,20 +1,43 @@-import useInstruction from 'components/Editor/Template/NodeJSExpressTemplate/useInstruction'+import { useCallback } from 'react'++import useReferences from 'hooks/useReferences'import InstructionsEditor from './InstructionsEditor'import RequestBodyEditor from './RequestBodyEditor'-import useReferences from 'hooks/useReferences'+import { RouteInfo } from './useRoutes'export interface Props {- route?: Route+ setRoute: (route: Partial<RouteInfo> & Pick<RouteInfo, 'id'>) => void,+ route?: RouteInfo}-function RouteEditor({ route }: Props) {- const [descriptionBlock, updateDescriptionBlock] = useInstruction('Description', 'xml')- const [requestBodyBlock, updateRequestBodyBlock] = useInstruction('RequestBody', 'text')- const [instructionsBlock, updateInstructionsBlock] = useInstruction('Instructions', 'xml')-+function RouteEditor({ route, setRoute }: Props) {const [referenceSearch] = useReferences()+ const updateDescription = useCallback((content: string) => {+ if (!route?.id) return+ setRoute({+ id: route.id,+ Description: content,+ })+ }, [route?.id, setRoute])++ const updateRequestBody = useCallback((content: string) => {+ if (!route?.id) return+ setRoute({+ id: route.id,+ RequestBody: content,+ })+ }, [route?.id, setRoute])++ const updateInstructions = useCallback((content: string) => {+ if (!route?.id) return+ setRoute({+ id: route.id,+ Instructions: content,+ })+ }, [route?.id, setRoute])+return (<div className="py-8@@ -43,30 +66,24 @@ function RouteEditor({ route }: Props) {max-w-[65ch]grow">- {descriptionBlock &&- <InstructionsEditor- referenceSearch={referenceSearch}- title="What should this route do?"- placeholder="This is an API endpoint that ..."- content={descriptionBlock.content}- onChange={updateDescriptionBlock}- />- }- {requestBodyBlock &&- <RequestBodyEditor- block={requestBodyBlock}- onChange={updateRequestBodyBlock}- />- }- {instructionsBlock &&- <InstructionsEditor- referenceSearch={referenceSearch}- title="Step-by-step instructions"- placeholder="1. Check if the incoming `email` is not empty ..."- content={instructionsBlock.content}- onChange={updateInstructionsBlock}- />- }+ <InstructionsEditor+ referenceSearch={referenceSearch}+ title="What should this route do?"+ placeholder="This is an API endpoint that ..."+ content={route.Description}+ onChange={updateDescription}+ />+ <RequestBodyEditor+ content={route.RequestBody}+ onChange={updateRequestBody}+ />+ <InstructionsEditor+ referenceSearch={referenceSearch}+ title="Step-by-step instructions"+ placeholder="1. Check if the incoming `email` is not empty ..."+ content={route.Instructions}+ onChange={updateInstructions}+ /></div>}</div >diff --git a/components/Editor/Template/NodeJSExpressTemplate/RequestBodyEditor.tsx b/components/Editor/Template/NodeJSExpressTemplate/RequestBodyEditor.tsxindex 9054fcf83..28b5c979e 100644--- a/components/Editor/Template/NodeJSExpressTemplate/RequestBodyEditor.tsx+++ b/components/Editor/Template/NodeJSExpressTemplate/RequestBodyEditor.tsx@@ -1,17 +1,16 @@import clsx from 'clsx'import TextareaAutosize from 'react-textarea-autosize'-import { TemplateBlock } from 'state/store'import Text from 'components/Text'export interface Props {- onChange: (value: string) => void- block: TemplateBlock+ onChange: (content: string) => void+ content: string}function RequestBodyEditor({onChange,- block,+ content,}: Props) {return (<div className="@@ -45,7 +44,7 @@ function RequestBodyEditor({spellCheck="false"name="block"placeholder="email: string // Optionally add comments to explain what the field is for"- value={block.content}+ value={content}onChange={e => onChange(e.target.value)}className={clsx('w-full',diff --git a/components/Editor/Template/NodeJSExpressTemplate/Routes.tsx b/components/Editor/Template/NodeJSExpressTemplate/Routes.tsxindex 1f482e65c..bba9b52fd 100644--- a/components/Editor/Template/NodeJSExpressTemplate/Routes.tsx+++ b/components/Editor/Template/NodeJSExpressTemplate/Routes.tsx@@ -4,39 +4,26 @@ import { Plus } from 'lucide-react'import Button from 'components/Button'import Text from 'components/Text'import Select from 'components/Select'-import { useStateStore } from 'state/StoreProvider'-export enum Method {- POST = 'post',- GET = 'get',- PUT = 'put',- DELETE = 'delete',- PATCH = 'patch',-}--export const methods = Object- .keys(Method)- .filter(item => isNaN(Number(item)))- .map(v => v.toLowerCase())+import { Method, methods, RouteInfo } from './useRoutes'export interface Props {- routes: Route[]- selectRoute: (id: string) => void- selectedRouteID?: string- deleteRoute: (id: string) => void+ routes: RouteInfo[],+ selectedRoute?: RouteInfo,+ addRoute: (route?: RouteInfo) => void,+ setRoute: (route: Partial<RouteInfo> & Pick<RouteInfo, 'id'>) => void,+ deleteRoute: (id: string) => void,+ selectRoute: (id: string) => void,}function Routes({routes,- selectedRouteID,+ selectedRoute,deleteRoute,selectRoute,+ addRoute,+ setRoute,}: Props) {- const [selectors] = useStateStore()-- const addRoute = selectors.use.addRoute()- const changeRoute = selectors.use.changeRoute()-return (<div><div@@ -60,7 +47,7 @@ function Routes({/><Buttontext="New"- onClick={addRoute}+ onClick={() => addRoute()}variant={Button.variant.Outline}icon={<Plus size="16px" />}isDisabled={routes.length >= 1}@@ -76,7 +63,7 @@ function Routes({">{routes.map(r =><div- key={r.id}+ key={r.Method + r.Path}className={clsx(`flexitems-center@@ -85,8 +72,8 @@ function Routes({px-2`,{- 'text-green-800 group': selectedRouteID === r.id,- 'text-slate-300': selectedRouteID !== r.id,+ 'text-green-800 group': selectedRoute?.id === r.id,+ 'text-slate-300': selectedRoute?.id !== r.id,})}>@@ -100,10 +87,13 @@ function Routes({<div className="flex items-center space-x-2"><Selectdirection="left"- selectedValue={{ key: r.method, title: r.method.toUpperCase() }}+ selectedValue={{ key: r.Method, title: r.Method.toUpperCase() }}values={methods.map(m => ({ key: m, title: m.toUpperCase() }))}- onChange={m => changeRoute(r.id, { method: m.key as Method })}- isSelected={r.id === selectedRouteID}+ onChange={m => setRoute({+ ...r,+ Method: m.key as Method,+ })}+ isSelected={r.id === selectedRoute?.id}/></div>diff --git a/components/Editor/Template/NodeJSExpressTemplate/index.tsx b/components/Editor/Template/NodeJSExpressTemplate/index.tsxindex bb53cd5ff..967d29f5f 100644--- a/components/Editor/Template/NodeJSExpressTemplate/index.tsx+++ b/components/Editor/Template/NodeJSExpressTemplate/index.tsx@@ -1,5 +1,3 @@--import RouteEditor from './Instructions'import Routes from './Routes'import useRoutes from './useRoutes'@@ -8,20 +6,24 @@ function NodeJSExpressTemplate() {const {deleteRoute,routes,- updateRoute,selectedRoute,selectRoute,+ addRoute,+ setRoute,} = useRoutes()return (<><Routesroutes={routes}- selectRoute={selec}- selectedRouteID={selectedRoute?.id}- deleteRoute={handleDeleteRoute}+ selectRoute={selectRoute}+ selectedRoute={selectedRoute}+ deleteRoute={deleteRoute}+ setRoute={setRoute}+ addRoute={addRoute}/><RouteEditor+ setRoute={setRoute}route={selectedRoute}/></>diff --git a/components/Editor/Template/NodeJSExpressTemplate/useRoutes.ts b/components/Editor/Template/NodeJSExpressTemplate/useRoutes.tsindex cd5ad20eb..68dda763f 100644--- a/components/Editor/Template/NodeJSExpressTemplate/useRoutes.ts+++ b/components/Editor/Template/NodeJSExpressTemplate/useRoutes.ts@@ -1,48 +1,130 @@-import { useCallback } from 'react'-import { Instruction } from 'state/instruction'+import { nanoid } from 'nanoid'+import { useCallback, useEffect } from 'react'import { useStateStore } from 'state/StoreProvider'-import { Method } from './Routes'++export enum Method {+ POST = 'post',+ GET = 'get',+ PUT = 'put',+ DELETE = 'delete',+ PATCH = 'patch',+}++export const methods = Object+ .keys(Method)+ .filter(item => isNaN(Number(item)))+ .map(v => v.toLowerCase())export interface Route {- Method: Instruction- Path: Instruction+ id: string+ Method: string+ Path: string}export interface RouteInfo extends Route {- Description: Instruction- RequestBody: Instruction- Instructions: Instruction+ Description: string+ RequestBody: string+ Instructions: string+}++export interface RoutesState {+ Routes?: RouteInfo[]+ selectedRouteID?: string}+/**+ * This hook is specific to the NodeJSExpress template.+ *+ * Because the structure of instructions (the text/prompt that user specifies before generating the code with "Run")+ * in each template could be completely different (sometimes you need array of routes, othertime you just need few fields)+ * we let the template specific hooks handle this and expose just an `any` object from `instructions`.+ */function useRoutes(): {- routes: Route[],- selectedRoute?: Route,- updateRoute: (method: Method, path: string, route: Route) => void,- deleteRoute: (method: Method, path: string) => void,- selectRoute: (method: Method, path: string) => void,+ routes: RouteInfo[],+ selectedRoute?: RouteInfo,+ addRoute: (route?: RouteInfo) => void,+ setRoute: (route: Partial<RouteInfo> & Pick<RouteInfo, 'id'>) => void,+ deleteRoute: (id: string) => void,+ selectRoute: (id: string) => void,} {const [selectors] = useStateStore()const setInstructions = selectors.use.setInstructions()+ const setInstructionTransform = selectors.use.setInstructionTransform()const instructions = selectors.use.instructions()- const routes = instructions['Routes']- const selectedRoute = instructions['selectedRoute']+ const routes = instructions?.['Routes'] as RouteInfo[] | undefined+ const selectedRouteID = instructions?.['selectedRouteID'] as string | undefined+ const selectedRouteInfo = routes?.find(r => r.id === selectedRouteID)++ // Define which fields saved in instructions are in XML format and need to be transfomed before constructing prompt+ // Use format from https://github.com/JSONPath-Plus/JSONPath#syntax-through-examples+ useEffect(function setInstructionsTransform() {+ setInstructionTransform('$.Routes[*].[Description, Instructions]', { type: 'xml' })+ }, [setInstructionTransform])++ const selectRoute = useCallback((id: string) => {+ setInstructions<RoutesState>(i => {+ i['selectedRouteID'] = id+ })+ }, [setInstructions])++ const deleteRoute = useCallback((id: string) => {+ setInstructions<RoutesState>(i => {+ if (!i.Routes) return++ const index = i.Routes?.findIndex(r => r.id === id)+ if (index !== -1) i.Routes.splice(index, 1)+ })+ }, [setInstructions])++ const setRoute = useCallback((route: Partial<RouteInfo> & Pick<RouteInfo, 'id'>) => {+ setInstructions<RoutesState>(i => {+ if (!i.Routes) return+ const index = i.Routes?.findIndex(todo => todo.id === route.id)+ if (index !== -1) i.Routes[index] = {+ ...i.Routes[index],+ ...route,+ }+ })+ }, [setInstructions])++ const addRoute = useCallback((route?: RouteInfo) => {+ setInstructions<RoutesState>(i => {+ route = route || {+ id: nanoid(),+ Description: '',+ Instructions: '',+ Method: Method.POST,+ Path: '/',+ RequestBody: '',+ }- const selectRoute = useCallback((method: Method, path: string) => {- setInstructions(i => {- i['selectedRoute'] = {-+ if (!i.Routes) {+ i.Routes = [route]+ } else {+ i.Routes.push(route)}})+ }, [setInstructions])- }, [setInstructions])+ useEffect(function addDefaultRoute() {+ console.log(routes)+ if (routes === undefined || routes?.length === 0) {+ const id = nanoid()+ addRoute()+ selectRoute(id)+ }+ }, [routes, addRoute, selectRoute])return {- routes,- updateRoute,+ routes: routes || [],+ setRoute,+ addRoute,deleteRoute,+ selectRoute,+ selectedRoute: selectedRouteInfo,}}diff --git a/components/Editor/Template/index.tsx b/components/Editor/Template/index.tsxindex 9b6396b2c..691dd0663 100644--- a/components/Editor/Template/index.tsx+++ b/components/Editor/Template/index.tsx@@ -17,7 +17,7 @@ const Template = forwardRef<HTMLDivElement, Props>(({ }, ref) => {flex-colflex-1">- {templates[templateID].component}+ {templateID && templates[templateID].component}</div>)})diff --git a/package-lock.json b/package-lock.jsonindex 5e121ae53..f1e8eb498 100644--- a/package-lock.json+++ b/package-lock.json@@ -29,7 +29,7 @@"http-proxy-middleware": "^3.0.0-beta.1","human-id": "^4.0.0","immer": "^9.0.19",- "lodash.mapvalues": "^4.6.0",+ "jsonpath-plus": "^7.2.0","lucide-react": "^0.161.0","mustache": "^4.2.0","nanoid": "^4.0.1",@@ -51,7 +51,6 @@},"devDependencies": {"@tailwindcss/typography": "^0.5.9",- "@types/lodash.mapvalues": "^4.6.7","@types/mustache": "^4.2.2","@types/node": "^18.14.6","@types/randomcolor": "^0.5.7",@@ -1025,21 +1024,6 @@"integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==","dev": true},- "node_modules/@types/lodash": {- "version": "4.14.194",- "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.194.tgz",- "integrity": "sha512-r22s9tAS7imvBt2lyHC9B8AGwWnXaYb1tY09oyLkXDs4vArpYJzw09nj8MLx5VfciBPGIb+ZwG0ssYnEPJxn/g==",- "dev": true- },- "node_modules/@types/lodash.mapvalues": {- "version": "4.6.7",- "resolved": "https://registry.npmjs.org/@types/lodash.mapvalues/-/lodash.mapvalues-4.6.7.tgz",- "integrity": "sha512-yGTo9zD60Iw1Q+YBvx4Ad7RDj5rA6EpvYTpVPVsfe6pwHcwDcGChSqL1n2sPBMbCeHJ3R5kcaqpkdlrFe/y4Vg==",- "dev": true,- "dependencies": {- "@types/lodash": "*"- }- },"node_modules/@types/mdast": {"version": "3.0.10","resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.10.tgz",@@ -4398,6 +4382,14 @@"json5": "lib/cli.js"}},+ "node_modules/jsonpath-plus": {+ "version": "7.2.0",+ "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz",+ "integrity": "sha512-zBfiUPM5nD0YZSBT/o/fbCUlCcepMIdP0CJZxM1+KgA4f2T206f6VAg9e7mX35+KlMaIc5qXW34f3BnwJ3w+RA==",+ "engines": {+ "node": ">=12.0.0"+ }+ },"node_modules/jsx-ast-utils": {"version": "3.3.3","resolved": "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.3.tgz",@@ -4496,11 +4488,6 @@"integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==","dev": true},- "node_modules/lodash.mapvalues": {- "version": "4.6.0",- "resolved": "https://registry.npmjs.org/lodash.mapvalues/-/lodash.mapvalues-4.6.0.tgz",- "integrity": "sha512-JPFqXFeZQ7BfS00H58kClY7SPVeHertPE0lNuCyZ26/XlN8TvakYD7b9bGyNmXbT/D3BbtPAAmq90gPWqLkxlQ=="- },"node_modules/lodash.memoize": {"version": "4.1.2","resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz",diff --git a/package.json b/package.jsonindex 4c1417f9f..d589db958 100644--- a/package.json+++ b/package.json@@ -58,7 +58,7 @@"http-proxy-middleware": "^3.0.0-beta.1","human-id": "^4.0.0","immer": "^9.0.19",- "lodash.mapvalues": "^4.6.0",+ "jsonpath-plus": "^7.2.0","lucide-react": "^0.161.0","mustache": "^4.2.0","nanoid": "^4.0.1",@@ -80,7 +80,6 @@},"devDependencies": {"@tailwindcss/typography": "^0.5.9",- "@types/lodash.mapvalues": "^4.6.7","@types/mustache": "^4.2.2","@types/node": "^18.14.6","@types/randomcolor": "^0.5.7",diff --git a/state/instruction.ts b/state/instruction.tsindex 1c9bcff8a..7bae731cd 100644--- a/state/instruction.ts+++ b/state/instruction.ts@@ -1,47 +1,46 @@-import mapValues from 'lodash.mapvalues'+import { JSONPath } from 'jsonpath-plus'import { Reference } from 'editor/referenceType'import { html2markdown } from 'editor/schema'-export interface Instruction {- type: 'text' | 'xml'- // `content` must be valid XML when `type` is `xml`- content: string-}--export type NestedInstruction =- undefined |- Instruction |- Instruction[] |- InstructionsRoot |- InstructionsRoot[]--export interface InstructionsRoot {- [key: string]: NestedInstruction-}+export type Instructions = any-export function evaluateInstruction(instruction: Instruction): [string, Reference[]] {- if (instruction.type === 'xml') {- return html2markdown(instruction.content)- }- return [instruction.content, []]+export interface TransformType {+ type: 'xml'}-export function isInstruction(value: Instruction | InstructionsRoot): value is Instruction {- return value.content !== undefined && value.type !== undefined+export interface InstructionsTransform {+ [jsonPath: string]: TransformType | undefined}-export function mapNestedInstructions(instructions: InstructionsRoot, transform: (i: Instruction) => string) {- function transformValues(i: Instruction | InstructionsRoot): any {- return isInstruction(i)- ? transform(i)- : mapNestedInstructions(i, transform)+export function evaluateInstruction(instruction: Instructions, transformType?: TransformType['type']): [Instructions, Reference[]] {+ if (typeof instruction === 'string' && transformType === 'xml') {+ return html2markdown(instruction)}+ return [instruction, []]+}- return mapValues(instructions, (value, key) => {- if (!value) return- return Array.isArray(value)- ? value.map(transformValues)- : transformValues(value)+export function transformInstructions(+ instructions: Instructions,+ instructionsTransform: InstructionsTransform,+ transform: (i: Instructions, transformType?: TransformType['type']) => Instructions,+) {+ let newInstructions = JSON.parse(JSON.stringify(instructions))++ Object.entries(instructionsTransform).forEach(([jsonPath, transformType]) => {+ JSONPath({+ path: jsonPath,+ json: newInstructions,+ callback: (payload, payloadType, fullPayload) => {+ const newPayload = transform(payload, transformType?.type)+ if (fullPayload.parent) {+ fullPayload.parent[fullPayload.parentProperty] = newPayload+ } else if (fullPayload.parent === null) {+ newInstructions = newPayload+ }+ }+ })})-}\ No newline at end of file++ return newInstructions+}diff --git a/state/model.ts b/state/model.tsindex 664953e00..fbec629bc 100644--- a/state/model.ts+++ b/state/model.ts@@ -1,6 +1,7 @@import { Creds } from 'hooks/useModelProviderArgs'import { PromptFragment } from './prompt'+import { TemplateID, templates } from './template'export enum ModelProvider {OpenAI = 'OpenAI',@@ -461,9 +462,9 @@ export const providerTemplates: {export function getModelArgs(modelConfig: Pick<ModelConfig, 'provider' | 'name' | 'args'>,creds: Creds,-): ModelArgs | undefined {+): ModelArgs {const template = providerTemplates[modelConfig.provider]?.models.find(m => m.name === modelConfig.name)- if (!template) return+ if (!template) throw new Error(`Cannot find model template ${modelConfig.provider}/${modelConfig.name}`)const defaultArgs = Object.entries(template.args || {})@@ -487,11 +488,13 @@ export function getMissingCreds(provider: ModelProvider, creds: Creds) {.filter(([key,]) => creds[provider]?.creds?.[key] === undefined)}-export function getDefaultModelConfig(): ModelConfig {+export function getDefaultModelConfig(templateID: TemplateID): ModelConfig {+ const prompt = templates[templateID].prompt+return {provider: ModelProvider.OpenAI,name: providerTemplates[ModelProvider.OpenAI].models[0].name,args: {},- prompt: [],+ prompt,}}diff --git a/state/prompt.ts b/state/prompt.tsindex b550522d6..47e3d470e 100644--- a/state/prompt.ts+++ b/state/prompt.ts@@ -4,7 +4,7 @@ import { html2markdown } from 'editor/schema'import { Reference } from 'editor/referenceType'import { identity } from 'utils/identity'-import { evaluateInstruction, mapNestedInstructions, InstructionsRoot } from './instruction'+import { evaluateInstruction, Instructions, InstructionsTransform, transformInstructions } from './instruction'export function getPromptLabel(fragment: PromptFragment) {if (fragment.role === 'system' && fragment.type === 'prefix') {@@ -27,16 +27,20 @@ export interface PromptFragment {}export function evaluatePrompt(- instructions: InstructionsRoot,+ instructions: Instructions,+ instructionsTransform: InstructionsTransform,prompt: PromptFragment[],): PromptFragment[] {const references: Reference[] = []- const textualInstructionsView = mapNestedInstructions(instructions, (i) => {- const [markdown, references] = evaluateInstruction(i)- references.push(...references)- return markdown- })+ const instructionsView = transformInstructions(+ instructions,+ instructionsTransform,+ (i, type) => {+ const [markdown, references] = evaluateInstruction(i, type)+ references.push(...references)+ return markdown+ })const textualPrompt = prompt.map(p => {@@ -51,9 +55,9 @@ export function evaluatePrompt(return {...p,content: Mustache.render(p.content, {- ...textualInstructionsView,// TODO: Use the references to build contextReferences: references,+ ...instructionsView,}, undefined, {escape: identity,}),@@ -61,6 +65,7 @@ export function evaluatePrompt(})- console.log('Prompt', textualPrompt)+ console.log('Prompt:', textualPrompt)+ console.log('Instructions view:', instructionsView)return textualPrompt}diff --git a/state/store.ts b/state/store.tsindex acaebd430..ea3937fbe 100644--- a/state/store.ts+++ b/state/store.ts@@ -10,44 +10,65 @@ import { projectsTable } from 'db/tables'import { ModelConfig, getDefaultModelConfig, Model } from './model'import { PromptFragment } from './prompt'-import { defaultTemplateID, TemplateID, templates } from './template'+import { defaultTemplateID, TemplateID } from './template'import { EnvVar, getDefaultEnv } from './envs'-import { InstructionsRoot } from './instruction'+import { Instructions, InstructionsTransform, TransformType } from './instruction'export interface SerializedState {- instructions: InstructionsRoot- envs: { key: string, value: string }[]+ /**+ * This should be an object that contains information provided by a user that is then injected to prompt when you use "Run".+ *+ * For example - the instructions for NodeJSExpress template contains an array of `Routes` that each has+ * `Description`, `RequestBody`, `Instructions` (Step-by-step instructions), `Method` and `Path` fields.+ * You then use these fields in the prompt via `{{Method}}`, etc.+ *+ * This object's structure can be specific for each template so the default type is `any`.+ */+ instructions: Instructions+ /**+ * This field specifies which fields from `instructions` are XML+ * and should be transformed to pure text before injected to prompt.+ */+ instructionsTransform: InstructionsTransform+ /**+ * Configuration (prompt, model args, etc.) for models.+ *+ * The model's config is only added if the model was selected.+ */modelConfigs: ModelConfig[]- templateID: TemplateID+ envs: EnvVar[]+ templateID: TemplateID | nullselectedModel: Model}export interface State extends SerializedState {- setInstructions: (set: (nextStateOrUpdater: InstructionsRoot | Partial<InstructionsRoot> | ((state: WritableDraft<InstructionsRoot>) => void)) => void) => void+ /**+ * Because the instructions can be different for each template we expose an update function that can modify them.+ * @param updater function that can modify the current instructions via a provided immer.js draft object.+ */+ setInstructions: <T extends Instructions>(updater: (state: WritableDraft<T>) => void) => void+ setInstructionTransform: (jsonPath: string, transform?: TransformType) => voidsetEnvs: (pair: EnvVar[]) => voidsetEnv: (pair: EnvVar, idx: number) => voidsetModelConfigPrompt: (config: Model, idx: number, prompt: Partial<PromptFragment>) => voidsetModelConfig: (config: Partial<ModelConfig> & Model) => void- selectedModelConfig: ModelConfig | undefinedselectModel: (model: Model) => void+ selectedModelConfig: ModelConfig | null}function getDefaultState(templateID: TemplateID): SerializedState {- const prompt = templates[templateID].prompt- const defaultModel = getDefaultModelConfig()+ const defaultModelConfig = getDefaultModelConfig(templateID)return {+ instructionsTransform: {},envs: [getDefaultEnv()],- modelConfigs: [{- ...defaultModel,- prompt,- }],+ modelConfigs: [defaultModelConfig],instructions: {},templateID,selectedModel: {- name: defaultModel.name,- provider: defaultModel.provider,- }+ name: defaultModelConfig.name,+ provider: defaultModelConfig.provider,+ },}}@@ -66,75 +87,92 @@ export function createStore(project: projects, client?: SupabaseClient<Database>}if (!initialState.modelConfigs) {- initialState.modelConfigs = [{- ...getDefaultModelConfig(),- prompt: templates[defaultTemplateID].prompt,- }]+ initialState.modelConfigs = [getDefaultModelConfig(defaultTemplateID)]}if (!initialState.selectedModel) {- const defaultModel = getDefaultModelConfig()+ const firstConfig = initialState.modelConfigs[0]initialState.selectedModel = {- provider: defaultModel.provider,- name: defaultModel.name,+ name: firstConfig.name,+ provider: firstConfig.provider,}}const immerStore = immer<State>((set, get) => ({...initialState,- setInstructions: transform => set(state => transform(state.instructions)),- setEnvs: (envs) => set(state => state.envs = envs),- setEnv: (pair, idx) => set(state => state.envs[idx] = pair),- selectModel: (model) => set(state => {- state.selectedModel = {- provider: model.provider,- name: model.name- }- }),- setModelConfigPrompt: (config, idx, prompt) => set(state => {- const existingConfigIndex = state.modelConfigs.findIndex(c =>- c.provider === config.provider &&- c.name === config.name,- )-- if (existingConfigIndex !== -1) {- const configPrompt = state.modelConfigs[existingConfigIndex].prompt- if (configPrompt.length > idx) {- configPrompt[idx] = {- ...configPrompt[idx],- ...prompt,+ setInstructions: transform =>+ set(state => {+ console.log('set instructs')+ transform(state.instructions)+ }),+ setInstructionTransform: (jsonPath, instructionTransform) =>+ set(state => {+ console.log('set instructs transforms')+ state.instructionsTransform[jsonPath] = instructionTransform+ }),+ setEnvs: (envs) =>+ set(state => {+ state.envs = envs+ }),+ setEnv: (pair, idx) =>+ set(state => {+ state.envs[idx] = pair+ }),+ selectModel: (model) =>+ set(state => {+ state.selectedModel = {+ provider: model.provider,+ name: model.name+ }+ }),+ setModelConfigPrompt: (config, idx, prompt) =>+ set(state => {+ console.log('set config prompt')+ const existingConfigIndex = state.modelConfigs.findIndex(c =>+ c.provider === config.provider &&+ c.name === config.name,+ )++ if (existingConfigIndex !== -1) {+ const configPrompt = state.modelConfigs[existingConfigIndex].prompt+ if (configPrompt.length > idx) {+ configPrompt[idx] = {+ ...configPrompt[idx],+ ...prompt,+ }+ return}- return}- }- console.error('Invalid index when setting model config prompt', config, idx)- }),- setModelConfig: (config) => set(state => {- const existingConfigIndex = state.modelConfigs.findIndex(c =>- c.provider === config.provider &&- c.name === config.name,- )-- if (existingConfigIndex === -1) {- state.modelConfigs.push({- args: {},- prompt: [],- ...config,- })- } else {- state.modelConfigs[existingConfigIndex] = {- ...state.modelConfigs[existingConfigIndex],- ...config+ console.error('Invalid index when setting model config prompt', config, idx)+ }),+ setModelConfig: (config) =>+ set(state => {+ console.log('set config')+ const existingConfigIndex = state.modelConfigs.findIndex(c =>+ c.provider === config.provider &&+ c.name === config.name,+ )++ if (existingConfigIndex === -1) {+ state.modelConfigs.push({+ args: {},+ prompt: [],+ ...config,+ })+ } else {+ state.modelConfigs[existingConfigIndex] = {+ ...state.modelConfigs[existingConfigIndex],+ ...config+ }}- }- }),+ }),get selectedModelConfig() {const state = get()const model = state.selectedModelreturn state.modelConfigs.find(c =>c.provider === model.provider &&c.name === model.name,- )+ ) || null},}))