This commit is contained in:
Quan HL
2025-06-10 13:23:11 +07:00
parent f8a69b4b79
commit 7f5bb9c141

View File

@@ -1,4 +1,5 @@
const Task = require('../../task');
const crypto = require('crypto');
const TaskName = 'Llm_Aws_s2s';
const {LlmEvents_Aws} = require('../../../utils/constants');
const ClientEvent = 'client.event';
@@ -47,10 +48,13 @@ class TaskLlmAws_S2S extends Task {
this.actionHook = this.data.actionHook;
this.eventHook = this.data.eventHook;
this.toolHook = this.data.toolHook;
const {inferenceConfiguration, toolConfiguration} = this.data.llmOptions;
const {inferenceConfiguration, toolConfiguration, voiceId, systemPrompt} = this.data.llmOptions;
this.inferenceConfiguration = inferenceConfiguration;
this.toolConfiguration = toolConfiguration;
this.voiceId = voiceId;
this.systemPrompt = systemPrompt;
this.promptName = crypto.randomUUID();
this.results = {
completionReason: 'normal conversation end'
@@ -170,7 +174,7 @@ class TaskLlmAws_S2S extends Task {
this._registerHandlers(ep);
try {
const args = [ep.uuid, 'session.create', this.host, this.path, this.authType, this.apiKey];
const args = [ep.uuid, 'session.create', this.aws_region, this.model, this.access_key_id, this.secret_access_key];
await this._api(ep, args);
} catch (err) {
this.logger.error({err}, 'TaskLlmAws_S2S:_startListening');
@@ -192,36 +196,107 @@ class TaskLlmAws_S2S extends Task {
}
async _sendInitialMessage(ep) {
let obj = {type: 'response.create', response: this.response_create};
if (!await this._sendClientEvent(ep, obj)) {
this.notifyTaskDone();
}
/* send immediate session.update if present */
else if (this.session_update) {
if (this.parent.isMcpEnabled) {
this.logger.debug('TaskLlmAws_S2S:_sendInitialMessage - mcp enabled');
const tools = await this.parent.mcpService.getAvailableMcpTools();
if (tools && tools.length > 0 && this.session_update) {
const convertedTools = tools.map((tool) => ({
name: tool.name,
type: 'function',
description: tool.description,
parameters: tool.inputSchema
}));
this.session_update.tools = [
...convertedTools,
...(this.session_update.tools || [])
];
const settionStart = {
event: {
sessionStart: {
inferenceConfiguration: this.inferenceConfiguration || {
maxTokens: 1024,
temperature: 0.7,
topP: 0.9,
},
}
}
obj = {type: 'session.update', session: this.session_update};
this.logger.debug({obj}, 'TaskLlmAws_S2S:_sendInitialMessage - sending session.update');
if (!await this._sendClientEvent(ep, obj)) {
this.notifyTaskDone();
};
if (!await this._sendClientEvent(ep, settionStart)) {
return this.notifyTaskDone();
}
const promptStart = {
event: {
promptStart: {
promptName: this.promptName,
textOutputConfiguration: {
mediaType: 'text/plain',
},
audioOutputConfiguration: {
voiceId: this.voiceId || 'tiffany',
},
toolUseOutputConfiguration: {
mediaType: 'application/json',
},
...(this.toolConfiguration && {
toolConfiguration: this.toolConfiguration
})
}
}
};
if (!await this._sendClientEvent(ep, promptStart)) {
return this.notifyTaskDone();
}
if (this.systemPrompt) {
const contentName = crypto.randomUUID();
const systemPromptContentStart = {
event: {
contentStart: {
promptName: this.promptName,
contentName,
type: 'TEXT',
interactive: true,
role: 'SYSTEM',
textInputConfiguration: {
mediaType: 'text/plain',
}
}
}
};
if (!await this._sendClientEvent(ep, systemPromptContentStart)) {
return this.notifyTaskDone();
}
const systemPromptContent = {
event: {
textInput: {
promptName: this.promptName,
contentName,
content: this.systemPrompt
}
}
};
if (!await this._sendClientEvent(ep, systemPromptContent)) {
return this.notifyTaskDone();
}
const systemPromptContentEnd = {
event: {
contentEnd: {
promptName: this.promptName,
contentName,
}
}
};
if (!await this._sendClientEvent(ep, systemPromptContentEnd)) {
return this.notifyTaskDone();
}
}
const audioContentStart = {
event: {
contentStart: {
promptName: this.promptName,
contentName: crypto.randomUUID(),
type: 'AUDIO',
interactive: true,
role: 'USER',
}
}
};
if (!await this._sendClientEvent(ep, audioContentStart)) {
return this.notifyTaskDone();
}
}
_registerHandlers(ep) {