mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 23:01:09 +03:00
Feature/Code Interpreter (#3183)
* Base changes for ServerSide Events (instead of socket.io) * lint fixes * adding of interface and separate methods for streaming events * lint * first draft, handles both internal and external prediction end points. * lint fixes * additional internal end point for streaming and associated changes * return streamresponse as true to build agent flow * 1) JSON formatting for internal events 2) other fixes * 1) convert internal event to metadata to maintain consistency with external response * fix action and metadata streaming * fix for error when agent flow is aborted * prevent subflows from streaming and other code cleanup * prevent streaming from enclosed tools * add fix for preventing chaintool streaming * update lock file * add open when hidden to sse * Streaming errors * Streaming errors * add fix for showing error message * add code interpreter * add artifacts to view message dialog * Update pnpm-lock.yaml --------- Co-authored-by: Vinod Paidimarry <vinodkiran@outlook.in>
This commit is contained in:
@@ -21,7 +21,7 @@ import {
|
||||
IDocument,
|
||||
IStateWithMessages
|
||||
} from '../../../src/Interface'
|
||||
import { ToolCallingAgentOutputParser, AgentExecutor, SOURCE_DOCUMENTS_PREFIX } from '../../../src/agents'
|
||||
import { ToolCallingAgentOutputParser, AgentExecutor, SOURCE_DOCUMENTS_PREFIX, ARTIFACTS_PREFIX } from '../../../src/agents'
|
||||
import { getInputVariables, getVars, handleEscapeCharacters, prepareSandboxVars } from '../../../src/utils'
|
||||
import {
|
||||
customGet,
|
||||
@@ -35,7 +35,6 @@ import {
|
||||
} from '../commonUtils'
|
||||
import { END, StateGraph } from '@langchain/langgraph'
|
||||
import { StructuredTool } from '@langchain/core/tools'
|
||||
import { DynamicStructuredTool } from '../../tools/CustomTool/core'
|
||||
|
||||
const defaultApprovalPrompt = `You are about to execute tool: {tools}. Ask if user want to proceed`
|
||||
const examplePrompt = 'You are a research assistant who can search for up-to-date info using search engine.'
|
||||
@@ -739,7 +738,12 @@ async function agentNode(
|
||||
|
||||
// If the last message is a tool message and is an interrupted message, format output into standard agent output
|
||||
if (lastMessage._getType() === 'tool' && lastMessage.additional_kwargs?.nodeId === nodeData.id) {
|
||||
let formattedAgentResult: { output?: string; usedTools?: IUsedTool[]; sourceDocuments?: IDocument[] } = {}
|
||||
let formattedAgentResult: {
|
||||
output?: string
|
||||
usedTools?: IUsedTool[]
|
||||
sourceDocuments?: IDocument[]
|
||||
artifacts?: ICommonObject[]
|
||||
} = {}
|
||||
formattedAgentResult.output = result.content
|
||||
if (lastMessage.additional_kwargs?.usedTools) {
|
||||
formattedAgentResult.usedTools = lastMessage.additional_kwargs.usedTools as IUsedTool[]
|
||||
@@ -747,6 +751,9 @@ async function agentNode(
|
||||
if (lastMessage.additional_kwargs?.sourceDocuments) {
|
||||
formattedAgentResult.sourceDocuments = lastMessage.additional_kwargs.sourceDocuments as IDocument[]
|
||||
}
|
||||
if (lastMessage.additional_kwargs?.artifacts) {
|
||||
formattedAgentResult.artifacts = lastMessage.additional_kwargs.artifacts as ICommonObject[]
|
||||
}
|
||||
result = formattedAgentResult
|
||||
} else {
|
||||
result.name = name
|
||||
@@ -765,12 +772,18 @@ async function agentNode(
|
||||
if (result.sourceDocuments) {
|
||||
additional_kwargs.sourceDocuments = result.sourceDocuments
|
||||
}
|
||||
if (result.artifacts) {
|
||||
additional_kwargs.artifacts = result.artifacts
|
||||
}
|
||||
if (result.output) {
|
||||
result.content = result.output
|
||||
delete result.output
|
||||
}
|
||||
|
||||
const outputContent = typeof result === 'string' ? result : result.content || result.output
|
||||
let outputContent = typeof result === 'string' ? result : result.content || result.output
|
||||
|
||||
// remove invalid markdown image pattern: 
|
||||
outputContent = typeof outputContent === 'string' ? outputContent.replace(/!\[.*?\]\(.*?\)/g, '') : outputContent
|
||||
|
||||
if (nodeData.inputs?.updateStateMemoryUI || nodeData.inputs?.updateStateMemoryCode) {
|
||||
let formattedOutput = {
|
||||
@@ -931,6 +944,9 @@ class ToolNode<T extends BaseMessage[] | MessagesState> extends RunnableCallable
|
||||
// Extract all properties except messages for IStateWithMessages
|
||||
const { messages: _, ...inputWithoutMessages } = Array.isArray(input) ? { messages: input } : input
|
||||
const ChannelsWithoutMessages = {
|
||||
chatId: this.options.chatId,
|
||||
sessionId: this.options.sessionId,
|
||||
input: this.inputQuery,
|
||||
state: inputWithoutMessages
|
||||
}
|
||||
|
||||
@@ -940,12 +956,14 @@ class ToolNode<T extends BaseMessage[] | MessagesState> extends RunnableCallable
|
||||
if (tool === undefined) {
|
||||
throw new Error(`Tool ${call.name} not found.`)
|
||||
}
|
||||
if (tool && tool instanceof DynamicStructuredTool) {
|
||||
if (tool && (tool as any).setFlowObject) {
|
||||
// @ts-ignore
|
||||
tool.setFlowObject(ChannelsWithoutMessages)
|
||||
}
|
||||
let output = await tool.invoke(call.args, config)
|
||||
let sourceDocuments: Document[] = []
|
||||
let artifacts = []
|
||||
|
||||
if (output?.includes(SOURCE_DOCUMENTS_PREFIX)) {
|
||||
const outputArray = output.split(SOURCE_DOCUMENTS_PREFIX)
|
||||
output = outputArray[0]
|
||||
@@ -956,12 +974,23 @@ class ToolNode<T extends BaseMessage[] | MessagesState> extends RunnableCallable
|
||||
console.error('Error parsing source documents from tool')
|
||||
}
|
||||
}
|
||||
if (output?.includes(ARTIFACTS_PREFIX)) {
|
||||
const outputArray = output.split(ARTIFACTS_PREFIX)
|
||||
output = outputArray[0]
|
||||
try {
|
||||
artifacts = JSON.parse(outputArray[1])
|
||||
} catch (e) {
|
||||
console.error('Error parsing artifacts from tool')
|
||||
}
|
||||
}
|
||||
|
||||
return new ToolMessage({
|
||||
name: tool.name,
|
||||
content: typeof output === 'string' ? output : JSON.stringify(output),
|
||||
tool_call_id: call.id!,
|
||||
additional_kwargs: {
|
||||
sourceDocuments,
|
||||
artifacts,
|
||||
args: call.args,
|
||||
usedTools: [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user