新增AI操作分类,支持AI对话(预设翻译、总结、shell生成)

This commit is contained in:
fofolee
2025-02-17 17:44:01 +08:00
parent 89a2e07b13
commit d7508c36a7
9 changed files with 502 additions and 5 deletions

View File

@@ -0,0 +1,175 @@
import { newVarInputVal } from "js/composer/varInputValManager";
export const aiCommands = {
label: "AI操作",
icon: "smart_toy",
defaultOpened: false,
commands: [
{
value: "quickcomposer.ai.getModels",
label: "获取可用模型",
desc: "获取API支持的模型列表",
asyncMode: "await",
icon: "list",
showLoading: true,
config: [
{
label: "API配置",
component: "OptionEditor",
icon: "settings",
width: 12,
options: {
modelType: {
component: "ButtonGroup",
width: 12,
height: "26px",
options: [
{ label: "OpenAI", value: "openai" },
{ label: "Ollama", value: "ollama" },
],
},
apiUrl: {
label: "API地址",
component: "VariableInput",
icon: "link",
width: 12,
placeholder: "输入API地址",
},
apiToken: {
label: "API令牌",
component: "VariableInput",
icon: "key",
width: 12,
placeholder: "ollama 则留空",
},
},
defaultValue: {
modelType: "openai",
apiUrl: newVarInputVal("str", "https://api.openai.com/v1/models"),
},
},
],
outputs: {
label: "获取模型列表结果",
suggestName: "modelListResult",
structure: {
success: {
label: "是否成功",
suggestName: "isSuccess",
},
result: {
label: "模型列表",
suggestName: "modelList",
},
error: {
label: "错误信息",
suggestName: "resultErrorInfo",
},
},
},
},
{
value: "quickcomposer.ai.chat",
label: "AI对话",
desc: "与AI助手进行对话",
asyncMode: "await",
icon: "chat",
showLoading: true,
config: [
{
label: "API配置",
component: "OptionEditor",
icon: "settings",
width: 12,
options: {
modelType: {
component: "ButtonGroup",
width: 12,
height: "26px",
options: [
{ label: "OpenAI", value: "openai" },
{ label: "Ollama", value: "ollama" },
],
},
apiUrl: {
label: "API地址",
component: "VariableInput",
icon: "link",
width: 12,
placeholder: "输入API地址",
},
apiToken: {
label: "API令牌",
component: "VariableInput",
icon: "key",
width: 6,
placeholder: "ollama 则留空",
},
model: {
label: "模型名称",
component: "VariableInput",
icon: "smart_toy",
width: 6,
placeholder: "如 gpt-3.5-turbo",
},
},
defaultValue: {
modelType: "openai",
apiUrl: newVarInputVal(
"str",
"https://api.openai.com/v1/chat/completions"
),
model: newVarInputVal("str", "gpt-3.5-turbo"),
},
},
{
label: "对话内容",
component: "OptionEditor",
icon: "chat",
width: 12,
options: {
presetPrompt: {
component: "ButtonGroup",
width: 12,
height: "26px",
options: [
{ label: "自由对话", value: "" },
{ label: "翻译", value: "translate" },
{ label: "总结", value: "summarize" },
{ label: "生成SHELL命令", value: "shell" },
],
},
prompt: {
label: "提示词",
component: "VariableInput",
icon: "edit",
width: 12,
placeholder: "输入要询问AI的内容",
},
},
defaultValue: {
presetPrompt: "",
},
},
],
outputs: {
label: "AI响应",
suggestName: "aiResponse",
structure: {
success: {
label: "是否成功",
suggestName: "isAiResponseSuccess",
},
result: {
label: "响应内容",
suggestName: "aiResponseContent",
},
error: {
label: "错误信息",
suggestName: "aiResponseError",
},
},
},
},
],
};

View File

@@ -1,5 +1,3 @@
import { newVarInputVal } from "js/composer/varInputValManager";
// 系统音效选项
const SYSTEM_SOUNDS = [
{ label: "提示音", value: "beep" },

View File

@@ -20,6 +20,7 @@ import { macosCommands } from "./macosCommands";
import { scriptCommands } from "./scriptCommands";
import { browserCommands } from "./browserCommands";
import { videoCommands } from "./videoCommands";
import { aiCommands } from "./aiCommands";
export const commandCategories = [
fileCommands,
@@ -38,6 +39,7 @@ export const commandCategories = [
scriptCommands,
uiCommands,
simulateCommands,
aiCommands,
statusCommands,
mathCommands,
userdataCommands,

View File

@@ -2512,4 +2512,100 @@ interface quickcomposerApi {
*/
waitForElement(selector: string, timeout?: number): Promise<void>;
};
/**
* AI 相关功能
*/
ai: {
/**
* 与 AI 进行对话
* @param apiConfig API配置
* @param content 对话内容
* @example
* // OpenAI 示例
* const response = await quickcomposer.ai.chat(
* {
* modelType: "openai",
* apiUrl: "https://api.openai.com/v1/chat/completions",
* apiToken: "your-api-token",
* model: "gpt-3.5-turbo"
* },
* {
* prompt: "你好",
* presetPrompt: "" // 使用预设提示词translate/shell/summarize
* }
* );
*
* // Ollama 示例
* const response = await quickcomposer.ai.chat(
* {
* modelType: "ollama",
* apiUrl: "http://localhost:11434/api/generate",
* model: "qwen2.5:32b"
* },
* {
* prompt: "查找进程名为chrome的进程并关闭",
* presetPrompt: "shell"
* }
* );
*/
chat(
apiConfig: {
/** 模型类型openai/ollama */
modelType: "openai" | "ollama";
/** API地址 */
apiUrl: string;
/** API令牌仅 OpenAI 需要) */
apiToken?: string;
/** 模型名称 */
model: string;
},
content: {
/** 提示词 */
prompt: string;
/** 预设提示词类型 */
presetPrompt?: "" | "translate" | "shell" | "summarize";
}
): Promise<{
/** 是否成功 */
success: boolean;
/** AI 响应内容 */
result?: string;
/** 错误信息 */
error?: string;
}>;
/**
* 获取 API 支持的模型列表
* @param apiConfig API配置
* @example
* // OpenAI 示例
* const models = await quickcomposer.ai.getModels({
* modelType: "openai",
* apiUrl: "https://api.openai.com/v1/models",
* apiToken: "your-api-token"
* });
*
* // Ollama 示例
* const models = await quickcomposer.ai.getModels({
* modelType: "ollama",
* apiUrl: "http://localhost:11434"
* });
*/
getModels(apiConfig: {
/** 模型类型openai/ollama */
modelType: "openai" | "ollama";
/** API地址 */
apiUrl: string;
/** API令牌仅 OpenAI 需要) */
apiToken?: string;
}): Promise<{
/** 是否成功 */
success: boolean;
/** 模型名称列表 */
result?: string[];
/** 错误信息 */
error?: string;
}>;
};
}