diff --git a/src/controllers/intent.controller.js b/src/controllers/intent.controller.js index 6b5b7ed..36a59f4 100644 --- a/src/controllers/intent.controller.js +++ b/src/controllers/intent.controller.js @@ -31,17 +31,51 @@ exports.getIntentCommand = async (req, res) => { const intents = await Intent.findAll(); const intent = intents.find(intent => intent.description === text); - if (command) { + if (intent) { const commandId = await redis.client.xAdd( 'commands', '*', { device_id: device_id, - intent: intent.command, + command: intent.command, processed_at: new Date().toISOString(), source: 'fallback_api', } ); + + console.log(`Intent matched and published to stream: ${commandId}`); + + return res.json({ + success: true, + intent: intent.description, + command: intent.command + }); + } else { + const errorId = await redis.client.xAdd( + 'commands', + '*', + { + device_id: device_id, + intent: 'error', + command: 'intent_not_found', + processed_at: new Date().toISOString(), + source: 'fallback_api' + } + ); + + console.log(`No intent match, error published: ${errorId}`); + + return res.status(400).json({ + success: false, + error: 'intent_not_found', + message: `Could not understand: "${text}"`, + stream_id: errorId, + source: 'fallback_api', + }); } + } catch (err) { + console.error("getIntentCommand error:", err); + + return res.status(500).send({message: err.message || "Internal server error."}); } -} \ No newline at end of file +}; \ No newline at end of file