Skip to content

Commit

Permalink
Feat/add ability to specify initial state in overrideConfig (#2893)
Browse files Browse the repository at this point in the history
add ability to specify initial state in overrideConfig
  • Loading branch information
HenryHengZJ authored Jul 27, 2024
1 parent 3e54d53 commit e39fd13
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/components/nodes/sequentialagents/Agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ const getReturnOutput = async (nodeData: INodeData, input: string, options: ICom
const tabIdentifier = nodeData.inputs?.[`${TAB_IDENTIFIER}_${nodeData.id}`] as string
const updateStateMemoryUI = nodeData.inputs?.updateStateMemoryUI as string
const updateStateMemoryCode = nodeData.inputs?.updateStateMemoryCode as string
const updateStateMemory = nodeData.inputs?.updateStateMemory as string

const selectedTab = tabIdentifier ? tabIdentifier.split(`_${nodeData.id}`)[0] : 'updateStateMemoryUI'
const variables = await getVars(appDataSource, databaseEntities, nodeData)
Expand All @@ -768,6 +769,27 @@ const getReturnOutput = async (nodeData: INodeData, input: string, options: ICom
vars: prepareSandboxVars(variables)
}

if (updateStateMemory && updateStateMemory !== 'updateStateMemoryUI' && updateStateMemory !== 'updateStateMemoryCode') {
try {
const parsedSchema = typeof updateStateMemory === 'string' ? JSON.parse(updateStateMemory) : updateStateMemory
const obj: ICommonObject = {}
for (const sch of parsedSchema) {
const key = sch.Key
if (!key) throw new Error(`Key is required`)
let value = sch.Value as string
if (value.startsWith('$flow')) {
value = customGet(flow, sch.Value.replace('$flow.', ''))
} else if (value.startsWith('$vars')) {
value = customGet(flow, sch.Value.replace('$', ''))
}
obj[key] = value
}
return obj
} catch (e) {
throw new Error(e)
}
}

if (selectedTab === 'updateStateMemoryUI' && updateStateMemoryUI) {
try {
const parsedSchema = typeof updateStateMemoryUI === 'string' ? JSON.parse(updateStateMemoryUI) : updateStateMemoryUI
Expand Down
22 changes: 22 additions & 0 deletions packages/components/nodes/sequentialagents/LLMNode/LLMNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ const getReturnOutput = async (nodeData: INodeData, input: string, options: ICom
const tabIdentifier = nodeData.inputs?.[`${TAB_IDENTIFIER}_${nodeData.id}`] as string
const updateStateMemoryUI = nodeData.inputs?.updateStateMemoryUI as string
const updateStateMemoryCode = nodeData.inputs?.updateStateMemoryCode as string
const updateStateMemory = nodeData.inputs?.updateStateMemory as string

const selectedTab = tabIdentifier ? tabIdentifier.split(`_${nodeData.id}`)[0] : 'updateStateMemoryUI'
const variables = await getVars(appDataSource, databaseEntities, nodeData)
Expand All @@ -571,6 +572,27 @@ const getReturnOutput = async (nodeData: INodeData, input: string, options: ICom
vars: prepareSandboxVars(variables)
}

if (updateStateMemory && updateStateMemory !== 'updateStateMemoryUI' && updateStateMemory !== 'updateStateMemoryCode') {
try {
const parsedSchema = typeof updateStateMemory === 'string' ? JSON.parse(updateStateMemory) : updateStateMemory
const obj: ICommonObject = {}
for (const sch of parsedSchema) {
const key = sch.Key
if (!key) throw new Error(`Key is required`)
let value = sch.Value as string
if (value.startsWith('$flow')) {
value = customGet(flow, sch.Value.replace('$flow.', ''))
} else if (value.startsWith('$vars')) {
value = customGet(flow, sch.Value.replace('$', ''))
}
obj[key] = value
}
return obj
} catch (e) {
throw new Error(e)
}
}

if (selectedTab === 'updateStateMemoryUI' && updateStateMemoryUI) {
try {
const parsedSchema = typeof updateStateMemoryUI === 'string' ? JSON.parse(updateStateMemoryUI) : updateStateMemoryUI
Expand Down
37 changes: 37 additions & 0 deletions packages/components/nodes/sequentialagents/State/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,43 @@ class State_SeqAgents implements INode {
const appDataSource = options.appDataSource as DataSource
const databaseEntities = options.databaseEntities as IDatabaseEntity
const selectedTab = tabIdentifier ? tabIdentifier.split(`_${nodeData.id}`)[0] : 'stateMemoryUI'
const stateMemory = nodeData.inputs?.stateMemory as string

if (stateMemory && stateMemory !== 'stateMemoryUI' && stateMemory !== 'stateMemoryCode') {
try {
const parsedSchema = typeof stateMemory === 'string' ? JSON.parse(stateMemory) : stateMemory
const obj: ICommonObject = {}
for (const sch of parsedSchema) {
const key = sch.Key
if (!key) throw new Error(`Key is required`)
const type = sch.Operation
const defaultValue = sch['Default Value']

if (type === 'Append') {
obj[key] = {
value: (x: any, y: any) => (Array.isArray(y) ? x.concat(y) : x.concat([y])),
default: () => (defaultValue ? JSON.parse(defaultValue) : [])
}
} else {
obj[key] = {
value: (x: any, y: any) => y ?? x,
default: () => defaultValue
}
}
}
const returnOutput: ISeqAgentNode = {
id: nodeData.id,
node: obj,
name: 'state',
label: 'state',
type: 'state',
output: START
}
return returnOutput
} catch (e) {
throw new Error(e)
}
}

if (!stateMemoryUI && !stateMemoryCode) {
const returnOutput: ISeqAgentNode = {
Expand Down
22 changes: 22 additions & 0 deletions packages/components/nodes/sequentialagents/ToolNode/ToolNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ const getReturnOutput = async (
const tabIdentifier = nodeData.inputs?.[`${TAB_IDENTIFIER}_${nodeData.id}`] as string
const updateStateMemoryUI = nodeData.inputs?.updateStateMemoryUI as string
const updateStateMemoryCode = nodeData.inputs?.updateStateMemoryCode as string
const updateStateMemory = nodeData.inputs?.updateStateMemory as string

const selectedTab = tabIdentifier ? tabIdentifier.split(`_${nodeData.id}`)[0] : 'updateStateMemoryUI'
const variables = await getVars(appDataSource, databaseEntities, nodeData)
Expand All @@ -464,6 +465,27 @@ const getReturnOutput = async (
vars: prepareSandboxVars(variables)
}

if (updateStateMemory && updateStateMemory !== 'updateStateMemoryUI' && updateStateMemory !== 'updateStateMemoryCode') {
try {
const parsedSchema = typeof updateStateMemory === 'string' ? JSON.parse(updateStateMemory) : updateStateMemory
const obj: ICommonObject = {}
for (const sch of parsedSchema) {
const key = sch.Key
if (!key) throw new Error(`Key is required`)
let value = sch.Value as string
if (value.startsWith('$flow')) {
value = customGet(flow, sch.Value.replace('$flow.', ''))
} else if (value.startsWith('$vars')) {
value = customGet(flow, sch.Value.replace('$', ''))
}
obj[key] = value
}
return obj
} catch (e) {
throw new Error(e)
}
}

if (selectedTab === 'updateStateMemoryUI' && updateStateMemoryUI) {
try {
const parsedSchema = typeof updateStateMemoryUI === 'string' ? JSON.parse(updateStateMemoryUI) : updateStateMemoryUI
Expand Down

0 comments on commit e39fd13

Please sign in to comment.