Skip to content

Commit d4f8039

Browse files
authored
Feature - Add option to start a new session with each interaction with the Chatflow tool (#2633)
* Feature - Add option to start a new session with each interaction with the Chatflow tool * ChatflowTool - Create random chatId when startNewSession is set
1 parent 842bfc6 commit d4f8039

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

packages/components/nodes/tools/ChatflowTool/ChatflowTool.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CallbackManagerForToolRun, Callbacks, CallbackManager, parseCallbackCon
66
import { StructuredTool } from '@langchain/core/tools'
77
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
88
import { availableDependencies, defaultAllowBuiltInDep, getCredentialData, getCredentialParam } from '../../../src/utils'
9+
import { v4 as uuidv4 } from 'uuid'
910

1011
class ChatflowTool_Tools implements INode {
1112
label: string
@@ -22,7 +23,7 @@ class ChatflowTool_Tools implements INode {
2223
constructor() {
2324
this.label = 'Chatflow Tool'
2425
this.name = 'ChatflowTool'
25-
this.version = 2.0
26+
this.version = 3.0
2627
this.type = 'ChatflowTool'
2728
this.icon = 'chatflowTool.svg'
2829
this.category = 'Tools'
@@ -66,6 +67,16 @@ class ChatflowTool_Tools implements INode {
6667
optional: true,
6768
additionalParams: true
6869
},
70+
{
71+
label: 'Start new session per message',
72+
name: 'startNewSession',
73+
type: 'boolean',
74+
description:
75+
'Whether to continue the session with the Chatflow tool or start a new one with each interaction. Useful for Chatflows with memory if you want to avoid it.',
76+
default: false,
77+
optional: true,
78+
additionalParams: true
79+
},
6980
{
7081
label: 'Use Question from Chat',
7182
name: 'useQuestionFromChat',
@@ -117,6 +128,8 @@ class ChatflowTool_Tools implements INode {
117128
const useQuestionFromChat = nodeData.inputs?.useQuestionFromChat as boolean
118129
const customInput = nodeData.inputs?.customInput as string
119130

131+
const startNewSession = nodeData.inputs?.startNewSession as boolean
132+
120133
const baseURL = (nodeData.inputs?.baseURL as string) || (options.baseURL as string)
121134

122135
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
@@ -136,7 +149,7 @@ class ChatflowTool_Tools implements INode {
136149

137150
let name = _name || 'chatflow_tool'
138151

139-
return new ChatflowTool({ name, baseURL, description, chatflowid: selectedChatflowId, headers, input: toolInput })
152+
return new ChatflowTool({ name, baseURL, description, chatflowid: selectedChatflowId, startNewSession, headers, input: toolInput })
140153
}
141154
}
142155

@@ -153,6 +166,8 @@ class ChatflowTool extends StructuredTool {
153166

154167
chatflowid = ''
155168

169+
startNewSession = false
170+
156171
baseURL = 'http://localhost:3000'
157172

158173
headers = {}
@@ -166,13 +181,15 @@ class ChatflowTool extends StructuredTool {
166181
description,
167182
input,
168183
chatflowid,
184+
startNewSession,
169185
baseURL,
170186
headers
171187
}: {
172188
name: string
173189
description: string
174190
input: string
175191
chatflowid: string
192+
startNewSession: boolean
176193
baseURL: string
177194
headers: ICommonObject
178195
}) {
@@ -181,6 +198,7 @@ class ChatflowTool extends StructuredTool {
181198
this.description = description
182199
this.input = input
183200
this.baseURL = baseURL
201+
this.startNewSession = startNewSession
184202
this.headers = headers
185203
this.chatflowid = chatflowid
186204
}
@@ -240,9 +258,9 @@ class ChatflowTool extends StructuredTool {
240258

241259
const body = {
242260
question: inputQuestion,
243-
chatId: flowConfig?.chatId,
261+
chatId: this.startNewSession ? uuidv4() : flowConfig?.chatId,
244262
overrideConfig: {
245-
sessionId: flowConfig?.sessionId
263+
sessionId: this.startNewSession ? uuidv4() : flowConfig?.sessionId
246264
}
247265
}
248266

0 commit comments

Comments
 (0)