github-actions[bot]
commited on
Commit
·
e434d49
1
Parent(s):
95bed4c
Update from GitHub Actions
Browse files- src/dust-client.js +32 -4
src/dust-client.js
CHANGED
@@ -305,6 +305,33 @@ export class DustClient {
|
|
305 |
}
|
306 |
}
|
307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
/**
|
309 |
* Main chat completion method - converts OpenAI format to Dust API
|
310 |
*/
|
@@ -321,7 +348,9 @@ export class DustClient {
|
|
321 |
throw new Error('Last message must be from user');
|
322 |
}
|
323 |
|
324 |
-
|
|
|
|
|
325 |
|
326 |
// 根据请求的模型找到对应的 agent
|
327 |
const modelId = openaiRequest.model || 'dust';
|
@@ -333,14 +362,13 @@ export class DustClient {
|
|
333 |
|
334 |
console.log(`Using agent: ${agent.name} (${agent.sId})`);
|
335 |
|
336 |
-
//
|
337 |
const conversationResult = await this.createConversation({
|
338 |
-
content:
|
339 |
mentions: [{ configurationId: agent.sId }]
|
340 |
});
|
341 |
|
342 |
const conversation = conversationResult.conversation;
|
343 |
-
const userMessage = conversationResult.message;
|
344 |
|
345 |
// 获取agent消息的sId - 从conversation.content中获取最后一个agent_message
|
346 |
const agentMessage = this.findAgentMessage(conversation);
|
|
|
305 |
}
|
306 |
}
|
307 |
|
308 |
+
/**
|
309 |
+
* Build conversation context from OpenAI messages array
|
310 |
+
* Combines all messages into a single context string for Dust API
|
311 |
+
*/
|
312 |
+
buildConversationContext(messages) {
|
313 |
+
if (!messages || !Array.isArray(messages)) {
|
314 |
+
return '';
|
315 |
+
}
|
316 |
+
|
317 |
+
// 将所有消息格式化为对话上下文
|
318 |
+
const contextParts = messages.map(msg => {
|
319 |
+
const role = msg.role === 'assistant' ? 'Assistant' :
|
320 |
+
msg.role === 'system' ? 'System' : 'User';
|
321 |
+
return `${role}: ${msg.content}`;
|
322 |
+
});
|
323 |
+
|
324 |
+
// 如果有多条消息,添加上下文说明;如果只有一条消息,直接返回内容
|
325 |
+
if (messages.length === 1) {
|
326 |
+
return messages[0].content;
|
327 |
+
}
|
328 |
+
|
329 |
+
// 添加当前请求的标识
|
330 |
+
contextParts.push('Please respond to the above conversation.');
|
331 |
+
|
332 |
+
return contextParts.join('\n\n');
|
333 |
+
}
|
334 |
+
|
335 |
/**
|
336 |
* Main chat completion method - converts OpenAI format to Dust API
|
337 |
*/
|
|
|
348 |
throw new Error('Last message must be from user');
|
349 |
}
|
350 |
|
351 |
+
// 将所有消息合并为一个上下文字符串
|
352 |
+
const conversationContext = this.buildConversationContext(openaiRequest.messages);
|
353 |
+
console.log('Processing chat completion with full conversation context');
|
354 |
|
355 |
// 根据请求的模型找到对应的 agent
|
356 |
const modelId = openaiRequest.model || 'dust';
|
|
|
362 |
|
363 |
console.log(`Using agent: ${agent.name} (${agent.sId})`);
|
364 |
|
365 |
+
// 创建对话,使用完整的对话上下文
|
366 |
const conversationResult = await this.createConversation({
|
367 |
+
content: conversationContext,
|
368 |
mentions: [{ configurationId: agent.sId }]
|
369 |
});
|
370 |
|
371 |
const conversation = conversationResult.conversation;
|
|
|
372 |
|
373 |
// 获取agent消息的sId - 从conversation.content中获取最后一个agent_message
|
374 |
const agentMessage = this.findAgentMessage(conversation);
|