feat: 添加模型选择器组件,整合模型选择功能到输入区域

This commit is contained in:
Roe-xin
2025-12-29 11:17:10 +08:00
parent c050f0e167
commit 770da72ce3
2 changed files with 265 additions and 67 deletions

View File

@ -1,4 +1,9 @@
import { getWaveformPreviewContent } from "./waveformPreviewContent"; import { getWaveformPreviewContent } from "./waveformPreviewContent";
import {
getModelSelectorContent,
getModelSelectorStyles,
getModelSelectorScript
} from "./modelSelector";
/** /**
* 获取输入区域的 HTML 内容 * 获取输入区域的 HTML 内容
@ -54,24 +59,7 @@ export function getInputAreaContent(): string {
<span class="tooltiptext">切换模式</span> <span class="tooltiptext">切换模式</span>
</div> </div>
<!-- 模型选择 --> ${getModelSelectorContent()}
<div class="tooltip">
<div class="custom-select" id="modelSelect">
<div class="select-trigger" onclick="toggleModelDropdown()">
<span class="select-value" id="modelValue">Auto</span>
<svg class="select-arrow" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
<path d="M507.8 727.728a30.016 30.016 0 0 1-21.288-8.824L231.104 463.496a30.088 30.088 0 0 1 0-42.568 30.088 30.088 0 0 1 42.568 0l234.128 234.128 234.16-234.128a30.088 30.088 0 0 1 42.568 0 30.088 30.088 0 0 1 0 42.568L529.08 718.904a30 30 0 0 1-21.28 8.824z" fill="#8a8a8a"/>
</svg>
</div>
<div class="select-dropdown" id="modelDropdown">
<div class="select-option" data-value="lite" onclick="selectModel('lite', 'Lite')">Lite</div>
<div class="select-option selected" data-value="auto" onclick="selectModel('auto', 'Auto')">Auto</div>
<div class="select-option" data-value="syntaxic" onclick="selectModel('syntaxic', 'Syntaxic')">Syntaxic</div>
<div class="select-option" data-value="max" onclick="selectModel('max', 'Max')">Max</div>
</div>
</div>
<span class="tooltiptext">选择模型</span>
</div>
</div> </div>
<div class="input-actions"> <div class="input-actions">
<!-- 上下文显示 --> <!-- 上下文显示 -->
@ -132,6 +120,7 @@ export function getInputAreaContent(): string {
*/ */
export function getInputAreaStyles(): string { export function getInputAreaStyles(): string {
return ` return `
${getModelSelectorStyles()}
.input-area { .input-area {
border-top: 1px solid var(--vscode-panel-border); border-top: 1px solid var(--vscode-panel-border);
padding-top: 15px; padding-top: 15px;
@ -256,7 +245,7 @@ export function getInputAreaStyles(): string {
gap: 8px; gap: 8px;
position: relative; position: relative;
} }
/* 自定义下拉框样式 */ /* 自定义下拉框样式(用于模式选择) */
.custom-select { .custom-select {
position: relative; position: relative;
user-select: none; user-select: none;
@ -305,17 +294,18 @@ export function getInputAreaStyles(): string {
.custom-select.active .select-dropdown { .custom-select.active .select-dropdown {
display: block; display: block;
} }
.select-option { /* 模式选择器的选项样式 */
#selectDropdown .select-option {
padding: 6px 12px; padding: 6px 12px;
font-size: 12px; font-size: 12px;
cursor: pointer; cursor: pointer;
transition: background 0.2s ease; transition: background 0.2s ease;
white-space: nowrap; white-space: nowrap;
} }
.select-option:hover { #selectDropdown .select-option:hover {
background: rgba(128, 128, 128, 0.3); background: rgba(128, 128, 128, 0.3);
} }
.select-option.selected { #selectDropdown .select-option.selected {
background: rgba(128, 128, 128, 0.5); background: rgba(128, 128, 128, 0.5);
color: var(--vscode-foreground); color: var(--vscode-foreground);
} }
@ -571,6 +561,8 @@ export function getInputAreaStyles(): string {
*/ */
export function getInputAreaScript(): string { export function getInputAreaScript(): string {
return ` return `
${getModelSelectorScript()}
// 自动调整 textarea 高度 // 自动调整 textarea 高度
function autoResizeTextarea() { function autoResizeTextarea() {
if (messageInput) { if (messageInput) {
@ -592,7 +584,6 @@ export function getInputAreaScript(): string {
// 自定义下拉框相关变量 // 自定义下拉框相关变量
let currentMode = 'agent'; let currentMode = 'agent';
let currentModel = 'auto';
// 切换模式下拉框显示/隐藏 // 切换模式下拉框显示/隐藏
function toggleModeDropdown() { function toggleModeDropdown() {
@ -607,19 +598,6 @@ export function getInputAreaScript(): string {
} }
} }
// 切换模型下拉框显示/隐藏
function toggleModelDropdown() {
const modelSelect = document.getElementById('modelSelect');
const customSelect = document.getElementById('customSelect');
if (modelSelect) {
modelSelect.classList.toggle('active');
// 关闭模式下拉框
if (customSelect) {
customSelect.classList.remove('active');
}
}
}
// 选择模式 // 选择模式
function selectMode(value, label) { function selectMode(value, label) {
currentMode = value; currentMode = value;
@ -645,43 +623,13 @@ export function getInputAreaScript(): string {
} }
} }
// 选择模型
function selectModel(value, label) {
currentModel = value;
const modelValue = document.getElementById('modelValue');
if (modelValue) {
modelValue.textContent = label;
}
// 更新选中状态
const options = document.querySelectorAll('#modelDropdown .select-option');
options.forEach(option => {
if (option.getAttribute('data-value') === value) {
option.classList.add('selected');
} else {
option.classList.remove('selected');
}
});
// 关闭下拉框
const modelSelect = document.getElementById('modelSelect');
if (modelSelect) {
modelSelect.classList.remove('active');
}
}
// 点击外部关闭下拉框 // 点击外部关闭下拉框
document.addEventListener('click', (event) => { document.addEventListener('click', (event) => {
const customSelect = document.getElementById('customSelect'); const customSelect = document.getElementById('customSelect');
const modelSelect = document.getElementById('modelSelect');
if (customSelect && !customSelect.contains(event.target)) { if (customSelect && !customSelect.contains(event.target)) {
customSelect.classList.remove('active'); customSelect.classList.remove('active');
} }
if (modelSelect && !modelSelect.contains(event.target)) {
modelSelect.classList.remove('active');
}
}); });
function sendMessage() { function sendMessage() {
@ -689,7 +637,7 @@ export function getInputAreaScript(): string {
if (!text) return; if (!text) return;
const mode = currentMode; const mode = currentMode;
const model = currentModel; const model = getCurrentModel(); // 从模型选择器组件获取当前模型
addMessage(text, 'user'); addMessage(text, 'user');
vscode.postMessage({ command: 'sendMessage', text: text, mode: mode, model: model }); vscode.postMessage({ command: 'sendMessage', text: text, mode: mode, model: model });

250
src/views/modelSelector.ts Normal file
View File

@ -0,0 +1,250 @@
/**
* 模型选择器组件
*/
/**
* 获取模型选择器的 HTML 内容
*/
export function getModelSelectorContent(): string {
return `
<!-- 模型选择 -->
<div class="tooltip">
<div class="custom-select" id="modelSelect">
<div class="select-trigger" onclick="toggleModelDropdown()">
<span class="select-value" id="modelValue">Auto</span>
<svg class="select-arrow" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
<path d="M507.8 727.728a30.016 30.016 0 0 1-21.288-8.824L231.104 463.496a30.088 30.088 0 0 1 0-42.568 30.088 30.088 0 0 1 42.568 0l234.128 234.128 234.16-234.128a30.088 30.088 0 0 1 42.568 0 30.088 30.088 0 0 1 0 42.568L529.08 718.904a30 30 0 0 1-21.28 8.824z" fill="#8a8a8a"/>
</svg>
</div>
<div class="select-dropdown" id="modelDropdown">
<div class="select-option" data-value="lite" data-tooltip="快速响应,适合简单任务" onclick="selectModel('lite', 'Lite')">Lite</div>
<div class="select-option selected" data-value="auto" data-tooltip="自动选择最佳模型" onclick="selectModel('auto', 'Auto')">Auto</div>
<div class="select-option" data-value="syntaxic" data-tooltip="语法分析和代码理解" onclick="selectModel('syntaxic', 'Syntaxic')">Syntaxic</div>
<div class="select-option" data-value="max" data-tooltip="最强性能,复杂任务" onclick="selectModel('max', 'Max')">Max</div>
</div>
<!-- 模型选择器的 tooltip 容器 -->
<div id="modelTooltip" class="model-tooltip"></div>
</div>
<span class="tooltiptext">选择模型</span>
</div>
`;
}
/**
* 获取模型选择器的样式
*/
export function getModelSelectorStyles(): string {
return `
/* 自定义下拉框样式 */
.custom-select {
position: relative;
user-select: none;
}
.select-trigger {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 8px;
background: var(--vscode-input-background);
color: var(--vscode-foreground);
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
transition: background 0.2s ease;
}
.select-trigger:hover {
background: var(--vscode-list-hoverBackground);
}
.select-value {
white-space: nowrap;
}
.select-arrow {
width: 12px;
height: 12px;
flex-shrink: 0;
transition: transform 0.2s ease;
}
.custom-select.active .select-arrow {
transform: rotate(180deg);
}
.select-dropdown {
position: absolute;
bottom: calc(100% + 2px);
left: 0;
min-width: 100%;
background: var(--vscode-dropdown-background);
border: 1px solid var(--vscode-dropdown-border);
border-radius: 4px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
z-index: 1100;
display: none;
overflow: visible;
}
.custom-select.active .select-dropdown {
display: block;
}
/* 模型选择器的选项样式 */
#modelDropdown .select-option {
position: relative;
padding: 6px 12px;
font-size: 12px;
cursor: pointer;
transition: background 0.2s ease;
white-space: nowrap;
}
#modelDropdown .select-option:hover {
background: rgba(128, 128, 128, 0.3);
}
#modelDropdown .select-option.selected {
background: rgba(128, 128, 128, 0.5);
color: var(--vscode-foreground);
}
/* 模型选择器的 tooltip 样式 */
.model-tooltip {
position: fixed;
background: #1e1e1e;
color: #ffffff;
padding: 8px 12px;
border-radius: 6px;
font-size: 12px;
white-space: nowrap;
pointer-events: none;
z-index: 10000;
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.6);
opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease, visibility 0.2s ease;
}
.model-tooltip.show {
opacity: 1;
visibility: visible;
}
/* tooltip 箭头 */
.model-tooltip::before {
content: "";
position: absolute;
right: 100%;
top: 50%;
transform: translateY(-50%);
border-width: 7px;
border-style: solid;
border-color: transparent rgba(255, 255, 255, 0.2) transparent transparent;
z-index: -1;
}
.model-tooltip::after {
content: "";
position: absolute;
right: 100%;
top: 50%;
transform: translateY(-50%);
border-width: 6px;
border-style: solid;
border-color: transparent #1e1e1e transparent transparent;
margin-right: 1px;
}
`;
}
/**
* 获取模型选择器的脚本
*/
export function getModelSelectorScript(): string {
return `
// 模型选择相关变量
let currentModel = 'auto';
// 切换模型下拉框显示/隐藏
function toggleModelDropdown() {
const modelSelect = document.getElementById('modelSelect');
const customSelect = document.getElementById('customSelect');
if (modelSelect) {
modelSelect.classList.toggle('active');
// 关闭模式下拉框
if (customSelect) {
customSelect.classList.remove('active');
}
}
}
// 选择模型
function selectModel(value, label) {
currentModel = value;
const modelValue = document.getElementById('modelValue');
if (modelValue) {
modelValue.textContent = label;
}
// 更新选中状态
const options = document.querySelectorAll('#modelDropdown .select-option');
options.forEach(option => {
if (option.getAttribute('data-value') === value) {
option.classList.add('selected');
} else {
option.classList.remove('selected');
}
});
// 关闭下拉框
const modelSelect = document.getElementById('modelSelect');
if (modelSelect) {
modelSelect.classList.remove('active');
}
}
// 点击外部关闭模型下拉框
document.addEventListener('click', (event) => {
const modelSelect = document.getElementById('modelSelect');
if (modelSelect && !modelSelect.contains(event.target)) {
modelSelect.classList.remove('active');
}
});
// 获取当前选中的模型
function getCurrentModel() {
return currentModel;
}
// 模型选择器 tooltip 功能
(function initModelTooltip() {
const modelDropdown = document.getElementById('modelDropdown');
const modelTooltip = document.getElementById('modelTooltip');
if (!modelDropdown || !modelTooltip) return;
// 为每个选项添加鼠标事件
const options = modelDropdown.querySelectorAll('.select-option');
options.forEach(option => {
option.addEventListener('mouseenter', function(e) {
const tooltipText = this.getAttribute('data-tooltip');
if (!tooltipText) return;
// 设置 tooltip 内容
modelTooltip.textContent = tooltipText;
// 获取选项的位置
const rect = this.getBoundingClientRect();
// 计算 tooltip 位置(在选项右侧)
const tooltipRect = modelTooltip.getBoundingClientRect();
const left = rect.right + 12;
const top = rect.top + (rect.height / 2) - (tooltipRect.height / 2);
// 设置位置
modelTooltip.style.left = left + 'px';
modelTooltip.style.top = top + 'px';
// 显示 tooltip
modelTooltip.classList.add('show');
});
option.addEventListener('mouseleave', function() {
// 隐藏 tooltip
modelTooltip.classList.remove('show');
});
});
})();
`;
}