Files
arcs/apps/arcs-mini/mcp-tools/mcp_tool_volume.c
2026-08-13 16:50:52 +08:00

123 lines
5.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "lisa_log.h"
#include "mcp.h"
#include "service_volume.h"
#define TAG "mcp_tool_volume"
static cJSON *volume_control_list(const char *name)
{
cJSON *tool = mcp_tool_list_info_create_default(name,
"音量控制与调节工具。当用户发出调节音量、控制声音大小的指令时调用。支持绝对值设置调到50%、相对值调节大声点、调小2档以及极值控制静音、最大声");
if (!tool) {
return NULL;
}
mcp_tool_info_add_property(tool, "text", "用户输入的关于音量调节的意图", "string", true);
cJSON *intent_property = cJSON_CreateObject();
cJSON_AddStringToObject(intent_property, "type", "string");
cJSON_AddStringToObject(intent_property, "description",
"调节类型set设定到具体值或极值调到10档、最大声、最小声adjustUp调大声、加两档adjustDown调小声、小一点");
cJSON *intent_enum = cJSON_CreateArray();
cJSON_AddItemToArray(intent_enum, cJSON_CreateString("set"));
cJSON_AddItemToArray(intent_enum, cJSON_CreateString("adjustUp"));
cJSON_AddItemToArray(intent_enum, cJSON_CreateString("adjustDown"));
cJSON_AddItemToObject(intent_property, "enum", intent_enum);
mcp_tool_info_add_json_property(tool, "intent", intent_property, true);
mcp_tool_info_add_property(tool, "value",
"具体的数值或状态1. 数字1, 10, 502. 标准状态位max最大声、满格、min最小声、静音、mid中等音量如果没有明确参数则留空大点儿声音",
"string", false);
cJSON *unit_property = cJSON_CreateObject();
cJSON_AddStringToObject(unit_property, "type", "string");
cJSON_AddStringToObject(unit_property, "description", "描述数值的单位。若指令中提及'档'、'级'、'%'则对应填写,否则留空。");
cJSON *unit_enum = cJSON_CreateArray();
cJSON_AddItemToArray(unit_enum, cJSON_CreateString(""));
cJSON_AddItemToArray(unit_enum, cJSON_CreateString(""));
cJSON_AddItemToArray(unit_enum, cJSON_CreateString("百分比"));
cJSON_AddItemToArray(unit_enum, cJSON_CreateString(""));
cJSON_AddItemToObject(unit_property, "enum", unit_enum);
mcp_tool_info_add_json_property(tool, "unit", unit_property, false);
return tool;
}
static cJSON *volume_control_call(const char *id, const char *name, cJSON *args)
{
const cJSON *text_json = mcp_tool_call_args_get(args, "text");
const cJSON *intent_json = mcp_tool_call_args_get(args, "intent");
const cJSON *value_json = mcp_tool_call_args_get(args, "value");
const cJSON *unit_json = mcp_tool_call_args_get(args, "unit");
if (!text_json || !cJSON_IsString(text_json)) {
LOGE("text parameter not found or invalid");
return NULL;
}
if (!intent_json || !cJSON_IsString(intent_json)) {
LOGE("intent parameter not found or invalid");
return NULL;
}
const char *text = text_json->valuestring;
const char *intent = intent_json->valuestring;
const char *value = value_json && cJSON_IsString(value_json) ? value_json->valuestring : NULL;
const char *unit = unit_json && cJSON_IsString(unit_json) ? unit_json->valuestring : "";
LOGI("Volume control - text: %s, intent: %s, value: %s, unit: %s",
text, intent, value ? value : "null", unit);
if (strcmp(intent, "set") == 0) {
if (value) {
if (strcmp(value, "max") == 0) {
service_volume_set(100);
} else if (strcmp(value, "min") == 0) {
service_volume_set(0);
} else if (strcmp(value, "mid") == 0) {
service_volume_set(50);
} else {
int target_vol = atoi(value);
service_volume_set(target_vol);
}
}
} else if (strcmp(intent, "adjustUp") == 0) {
int adjust_step = 10;
if (value) {
adjust_step = atoi(value);
if (adjust_step <= 0) adjust_step = 10;
}
service_volume_adjust(adjust_step);
} else if (strcmp(intent, "adjustDown") == 0) {
int adjust_step = 10;
if (value) {
adjust_step = atoi(value);
if (adjust_step <= 0) adjust_step = 10;
}
service_volume_adjust(-adjust_step);
}
cJSON *result = mcp_tool_call_result_create(name);
if (!result) {
return NULL;
}
cJSON *content_array = cJSON_CreateArray();
cJSON *content_item = cJSON_CreateObject();
cJSON_AddStringToObject(content_item, "type", "text");
cJSON_AddStringToObject(content_item, "text", "已完成操作");
cJSON_AddItemToArray(content_array, content_item);
cJSON_AddItemToObject(result, "content", content_array);
cJSON_AddBoolToObject(result, "isError", false);
return result;
}
MCP_TOOL_DEFINE(ls.set_volume, volume_control_list, volume_control_call);