This commit is contained in:
seb
2026-10-06 00:27:26 +02:00
parent 609ca55af2
commit 87f9a8d97e
2 changed files with 29 additions and 6 deletions

View File

@@ -557,7 +557,7 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: parsedId.error }); return res.status(400).json({ error: parsedId.error });
} }
const { message, tool_call_id, role, content, answer } = req.body ?? {}; const { message, tool_call_id, role, content, answer, name } = req.body ?? {};
try { try {
const manager = getOrCreateManager(parsedId.id, { const manager = getOrCreateManager(parsedId.id, {
@@ -574,6 +574,7 @@ export function register(app, basePath = '') {
tool_call_id, tool_call_id,
content, content,
answer: answer !== undefined ? answer : message, answer: answer !== undefined ? answer : message,
name,
}); });
} else { } else {
if (!message) { if (!message) {

View File

@@ -337,7 +337,7 @@ export class ConversationManager {
* @returns {Promise<object>} The created tool message * @returns {Promise<object>} The created tool message
* @throws {Error} 'Conversation is not idle' while a run is active. * @throws {Error} 'Conversation is not idle' while a run is active.
*/ */
async handleToolMessage(googleAuthId, { tool_call_id, content, answer }) { async handleToolMessage(googleAuthId, { tool_call_id, content, answer, name }) {
if (!tool_call_id) throw new Error('tool_call_id is required'); if (!tool_call_id) throw new Error('tool_call_id is required');
if (this.isProcessing) throw new Error('Conversation is not idle'); if (this.isProcessing) throw new Error('Conversation is not idle');
this.isProcessing = true; this.isProcessing = true;
@@ -347,7 +347,11 @@ export class ConversationManager {
const rawContent = content !== undefined const rawContent = content !== undefined
? (typeof content === 'string' ? content : JSON.stringify(content)) ? (typeof content === 'string' ? content : JSON.stringify(content))
: JSON.stringify({ answer: answer ?? '' }); : JSON.stringify({ answer: answer ?? '' });
const payload = JSON.stringify({ content: rawContent, tool_call_id }); const payload = JSON.stringify({
content: rawContent,
tool_call_id,
name: name || 'ask_user',
});
toolMsg = await db.addMessage(googleAuthId, this.conversationId, { message: payload, role: 'tool' }); toolMsg = await db.addMessage(googleAuthId, this.conversationId, { message: payload, role: 'tool' });
await db.setState(googleAuthId, this.conversationId, 'busy'); await db.setState(googleAuthId, this.conversationId, 'busy');
} catch (err) { } catch (err) {
@@ -439,11 +443,15 @@ export class ConversationManager {
} }
const trimmedContent = (content || text).trim(); const trimmedContent = (content || text).trim();
if (trimmedContent) { const hasToolCalls = Array.isArray(tool_calls) && tool_calls.length > 0;
if (trimmedContent || hasToolCalls) {
try { try {
await db.addMessage(googleAuthId, this.conversationId, { await db.addMessage(googleAuthId, this.conversationId, {
role: 'assistant', role: 'assistant',
message: JSON.stringify({ content: trimmedContent }), message: JSON.stringify({
content: trimmedContent,
...(hasToolCalls ? { tool_calls } : {}),
}),
}); });
text = ''; text = '';
emitCompanyEvent('modelconversation', { companyId: this.companyId }); emitCompanyEvent('modelconversation', { companyId: this.companyId });
@@ -452,7 +460,17 @@ export class ConversationManager {
} }
} }
}, },
onToolCall: async () => {}, onToolCall: async ({ name, args, id, callItem }) => {
const rawArgs = callItem?.function?.arguments || (typeof args === 'string' ? args : JSON.stringify(args || {}));
console.log(`[conversation ${this.conversationId}] tool call: ${name} args=${rawArgs}`);
this.broadcast('state', {
state: 'toolcall',
id: id || callItem?.id || '',
name,
arguments: rawArgs,
content: rawArgs,
});
},
onToolResult: async ({ name, result, id, callItem }) => { onToolResult: async ({ name, result, id, callItem }) => {
const rawArgs = callItem?.function?.arguments || ''; const rawArgs = callItem?.function?.arguments || '';
this.broadcast('state', { this.broadcast('state', {
@@ -591,6 +609,10 @@ export class ConversationManager {
// Direct return (e.g. from ask_user) halts without adding an additional assistant message. // Direct return (e.g. from ask_user) halts without adding an additional assistant message.
if (result?.directReturn) { if (result?.directReturn) {
console.log(
`[conversation ${this.conversationId}] direct return halted loop: ` +
JSON.stringify(result.data)
);
return; return;
} }