Skip to content

Commit 40d36b5

Browse files
authored
fix(robot): 优化robot的消息处理 (#3491)
1 parent 1e33249 commit 40d36b5

File tree

1 file changed

+29
-61
lines changed
  • examples/sites/src/views/components-doc/composition

1 file changed

+29
-61
lines changed

examples/sites/src/views/components-doc/composition/utils.ts

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ export async function handleSSEStream(
4141
)
4242
}
4343

44+
let messageIndex = 0
45+
function printMessage(data, str: string, endln = false) {
46+
handler.onData({
47+
id: '',
48+
created: data.created_at,
49+
choices: [
50+
{
51+
index: messageIndex++,
52+
delta: {
53+
role: 'assistant',
54+
content: str + (endln ? '\n\n' : '')
55+
},
56+
finish_reason: null
57+
}
58+
],
59+
object: '',
60+
model: ''
61+
})
62+
}
4463
try {
4564
while (true) {
4665
if (signal?.aborted) {
@@ -59,8 +78,6 @@ export async function handleSSEStream(
5978
const lines = buffer.split('\n\n')
6079
buffer = lines.pop() || ''
6180

62-
let messageIndex = 0
63-
6481
for (const line of lines) {
6582
if (line.trim() === '') continue
6683
if (line.trim() === 'data: [DONE]') {
@@ -75,68 +92,19 @@ export async function handleSSEStream(
7592

7693
const data = JSON.parse(dataMatch[1])
7794
// console.log('SSE data:', data)
78-
if (data?.event === 'workflow_started') {
79-
handler.onData({
80-
id: data.workflow_run_id,
81-
created: data.created_at,
82-
choices: [
83-
{
84-
index: messageIndex++,
85-
delta: {
86-
role: 'assistant',
87-
content: ' '
88-
},
89-
finish_reason: null
90-
}
91-
],
92-
object: '',
93-
model: ''
94-
})
95-
message.value.slice(-1)[0].loading = true
96-
if (!globalConversation.id) {
97-
globalConversation.id = data.conversation_id
98-
}
95+
if (data?.event === 'node_started') {
96+
printMessage(data, `${data.data.title} 节点运行...`, true)
9997
}
100-
101-
if (data?.event === 'message' && data?.answer) {
102-
handler.onData({
103-
id: data.id,
104-
created: data.created_at,
105-
choices: [
106-
{
107-
index: messageIndex++,
108-
delta: {
109-
role: 'assistant',
110-
content: data.answer
111-
},
112-
finish_reason: null
113-
}
114-
],
115-
object: '',
116-
model: ''
117-
})
118-
message.value.slice(-1)[0].loading = false
98+
if (data?.event === 'node_finished') {
99+
printMessage(
100+
data,
101+
`${data.data.title} 节点结束\n\n` +
102+
(data.data.node_type === 'answer' ? `${data.data.outputs.answer}` : '')
103+
)
119104
}
120-
if (data?.event === 'message_end') {
121-
handler.onData({
122-
id: data.id,
123-
created: data.created_at,
124-
choices: [
125-
{
126-
index: messageIndex++,
127-
delta: {
128-
role: 'assistant',
129-
content: ''
130-
},
131-
finish_reason: 'stop'
132-
}
133-
],
134-
object: '',
135-
model: ''
136-
})
137-
message.value.slice(-1)[0].loading = false
105+
if (data?.event === 'agent_log' && data.data.status === 'success' && data.data.label.startsWith('CALL')) {
106+
printMessage(data, `--${data.data.label}(${JSON.stringify(data.data.data.output.tool_call_input)})`, true)
138107
}
139-
// handler.onData(data)
140108
} catch (error) {
141109
console.error('Error parsing SSE message:', error)
142110
}

0 commit comments

Comments
 (0)