feat:实现携带路径发送信息的优化

- 将路径通过doc包裹起来便于后端读取
This commit is contained in:
Roe-xin
2026-03-20 11:45:01 +08:00
parent 4ba096d898
commit 1207d2b91a
8 changed files with 89 additions and 31 deletions

View File

@ -8,7 +8,7 @@ import * as vscode from "vscode";
type Environment = "dev" | "test" | "prod"; type Environment = "dev" | "test" | "prod";
/** 当前环境 - 修改这里切换环境 */ /** 当前环境 - 修改这里切换环境 */
const CURRENT_ENV: Environment = "prod"; const CURRENT_ENV: Environment = "test";
/** 服务等级类型 */ /** 服务等级类型 */
export type ServiceTier = "lite" | "syntaxic" | "max" | "auto"; export type ServiceTier = "lite" | "syntaxic" | "max" | "auto";
@ -42,9 +42,9 @@ const ENV_CONFIG: Record<Environment, IccoderConfig> = {
}, },
/** 测试服务器环境 - 通过 Gateway 路由 */ /** 测试服务器环境 - 通过 Gateway 路由 */
test: { test: {
backendUrl: "http://192.168.1.108:2029/iccoder", backendUrl: "http://192.168.1.134:2233",
backendUrlStrongeLoop: "http://192.168.1.108:2029", backendUrlStrongeLoop: "http://192.168.1.134:2233",
loginUrl: "http://192.168.1.108:2005/login", loginUrl: "http://192.168.1.134/login",
timeout: 60000, timeout: 60000,
userId: "default-user", userId: "default-user",
serviceTier: "max", serviceTier: "max",

View File

@ -7,6 +7,20 @@
import * as vscode from "vscode"; import * as vscode from "vscode";
let globalContext: vscode.ExtensionContext; let globalContext: vscode.ExtensionContext;
const DOCUMENT_SETS_KEY = "iccoder.documentSets";
export interface DocumentFile {
name: string;
absolutePath: string;
size: number;
}
export interface DocumentSet {
id: string;
name: string;
files: DocumentFile[];
updatedAt: number;
}
export function initializeContextHelper(context: vscode.ExtensionContext) { export function initializeContextHelper(context: vscode.ExtensionContext) {
globalContext = context; globalContext = context;
@ -138,26 +152,26 @@ export async function handleGetDocumentSetList(panel: vscode.WebviewPanel) {
}); });
} }
let documentSet: Array<{ path: string; relativePath: string }> = []; let documentSet: DocumentFile[] = [];
export function getDocumentSet() { export function getDocumentSet() {
return documentSet; return documentSet;
} }
export function addToDocumentSet(docs: Array<{ path: string; relativePath: string }>) { export function addToDocumentSet(docs: DocumentFile[]) {
documentSet = [...documentSet, ...docs]; documentSet = [...documentSet, ...docs];
} }
export function saveDocumentSet( export function saveDocumentSet(
docs: Array<{ path: string; relativePath: string }>, docs: DocumentFile[],
name: string, name: string,
panel: vscode.WebviewPanel panel: vscode.WebviewPanel
) { ) {
const newDocSet = { const newDocSet: DocumentSet = {
id: Date.now().toString(), id: Date.now().toString(),
name, name,
documents: docs, files: docs,
updatedAt: new Date().toISOString(), updatedAt: Date.now(),
}; };
documentSets.push(newDocSet); documentSets.push(newDocSet);
persistDocumentSets(); persistDocumentSets();
@ -167,22 +181,44 @@ export function saveDocumentSet(
}); });
} }
let documentSets: Array<{ let documentSets: DocumentSet[] = [];
id: string;
name: string;
documents: Array<{ path: string; relativePath: string }>;
updatedAt: string;
}> = [];
function loadDocumentSets() { function loadDocumentSets() {
const saved = globalContext.globalState.get<typeof documentSets>("documentSets"); const saved =
globalContext.globalState.get<Array<any>>(DOCUMENT_SETS_KEY) ||
globalContext.globalState.get<Array<any>>("documentSets", []);
if (saved) { if (saved) {
documentSets = saved; documentSets = saved.map((item: any) => ({
id: String(item.id),
name: String(item.name || ""),
files: Array.isArray(item.files)
? item.files.map((file: any) => ({
name: String(file.name || ""),
absolutePath: String(file.absolutePath || file.path || ""),
size: Number(file.size || 0),
}))
: Array.isArray(item.documents)
? item.documents.map((file: any) => ({
name: String(
file.name ||
file.relativePath?.split(/[\\/]/).pop() ||
file.path?.split(/[\\/]/).pop() ||
"",
),
absolutePath: String(file.absolutePath || file.path || ""),
size: Number(file.size || 0),
}))
: [],
updatedAt:
typeof item.updatedAt === "number"
? item.updatedAt
: new Date(item.updatedAt || Date.now()).getTime(),
}));
} }
} }
function persistDocumentSets() { function persistDocumentSets() {
globalContext.globalState.update("documentSets", documentSets); globalContext.globalState.update(DOCUMENT_SETS_KEY, documentSets);
} }
export function getDocumentSets() { export function getDocumentSets() {
@ -202,7 +238,7 @@ export function changeDocumentSetName(id: string, newName: string, panel: vscode
const docSet = documentSets.find(ds => ds.id === id); const docSet = documentSets.find(ds => ds.id === id);
if (docSet) { if (docSet) {
docSet.name = newName; docSet.name = newName;
docSet.updatedAt = new Date().toISOString(); docSet.updatedAt = Date.now();
persistDocumentSets(); persistDocumentSets();
panel.webview.postMessage({ panel.webview.postMessage({
command: "documentSetSaved", command: "documentSetSaved",

View File

@ -408,7 +408,11 @@ export async function handleWebviewMessage(
if (uris && uris.length > 0) { if (uris && uris.length > 0) {
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const selectedFiles: Array<{ path: string; relativePath: string; size: number }> = []; const selectedFiles: Array<{
name: string;
absolutePath: string;
size: number;
}> = [];
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
const MAX_TOTAL_SIZE = 50 * 1024 * 1024; // 50 MB const MAX_TOTAL_SIZE = 50 * 1024 * 1024; // 50 MB
const MAX_FILES = 1000; const MAX_FILES = 1000;
@ -440,8 +444,8 @@ export async function handleWebviewMessage(
} }
selectedFiles.push({ selectedFiles.push({
path: filePath, name: path.basename(filePath),
relativePath: vscode.workspace.asRelativePath(filePath), absolutePath: filePath,
size: stat.size, size: stat.size,
}); });
totalSize += stat.size; totalSize += stat.size;

View File

@ -162,6 +162,8 @@ export async function startStreamDialog(
const body = JSON.stringify(request); const body = JSON.stringify(request);
console.log(`[SSE] 开始流式对话: taskId=${request.taskId}, mode=${request.mode}, url=${urlString}`); console.log(`[SSE] 开始流式对话: taskId=${request.taskId}, mode=${request.mode}, url=${urlString}`);
console.log("[SSE] 完整请求体:", request);
console.log("[SSE] 请求体 JSON:", body);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const options: http.RequestOptions = { const options: http.RequestOptions = {

View File

@ -268,8 +268,19 @@ async function handleUserMessageWithBackend(
let enhancedText = text; let enhancedText = text;
if (contextItems && contextItems.length > 0) { if (contextItems && contextItems.length > 0) {
console.log("[MessageHandler] 处理上下文项:", contextItems.length); console.log("[MessageHandler] 处理上下文项:", contextItems.length);
const paths = contextItems.map((item) => item.path).join("\n"); const docTypes = new Set(["file", "document", "docset"]);
enhancedText = `${paths}\n\n${text}`; const regularPaths = contextItems
.filter((item) => !docTypes.has(item.type))
.map((item) => item.path);
const docTags = contextItems
.filter((item) => docTypes.has(item.type))
.map((item) => `<doc>${item.path}</doc>`);
if (regularPaths.length > 0) {
enhancedText = `${regularPaths.join("\n")}\n\n${enhancedText}`;
}
if (docTags.length > 0) {
enhancedText = `${enhancedText}\n\n${docTags.join("\n")}`;
}
} }
// 获取 historyManager 中的 taskId由 ICHelperPanel 创建) // 获取 historyManager 中的 taskId由 ICHelperPanel 创建)

View File

@ -554,8 +554,8 @@ export function getContextButtonScript(): string {
if (currentListType === 'documentSetItem') { if (currentListType === 'documentSetItem') {
const selected = currentListData.filter(item => selectedItems.has(item.id)); const selected = currentListData.filter(item => selectedItems.has(item.id));
selected.forEach(docSet => { selected.forEach(docSet => {
docSet.documents.forEach(doc => { (docSet.files || []).forEach(doc => {
addContextItem('file', doc.path, doc.relativePath || doc.path); addContextItem('docset', doc.absolutePath, doc.name || doc.absolutePath);
}); });
}); });
} else { } else {
@ -609,9 +609,13 @@ export function getContextButtonScript(): string {
searchInput.addEventListener('input', function(e) { searchInput.addEventListener('input', function(e) {
const keyword = (e.target.value || '').toLowerCase().trim(); const keyword = (e.target.value || '').toLowerCase().trim();
const filtered = currentListData.filter(item => const filtered = currentListData.filter(item =>
(item.relativePath || item.path || '').toLowerCase().includes(keyword) (item.name || item.relativePath || item.path || '').toLowerCase().includes(keyword)
); );
renderList(filtered); if (currentListType === 'documentSetItem') {
renderDocumentSetList(filtered);
} else {
renderList(filtered);
}
}); });
} }

View File

@ -181,6 +181,7 @@ export function getContextDisplayScript(): string {
case 'folder': icon = getFolderIcon(); break; case 'folder': icon = getFolderIcon(); break;
case 'image': icon = getImageIcon(); break; case 'image': icon = getImageIcon(); break;
case 'document': icon = getDocumentIcon(); break; case 'document': icon = getDocumentIcon(); break;
case 'docset': icon = getDocumentIcon(); break;
case 'code': icon = getCodeIcon(); break; case 'code': icon = getCodeIcon(); break;
} }

View File

@ -409,7 +409,7 @@ export function getDocsetDialogScript(): string {
listEl.innerHTML = docsetFiles.map((file, index) => \` listEl.innerHTML = docsetFiles.map((file, index) => \`
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 12px; padding: 4px 0; color: var(--vscode-foreground);"> <div style="display: flex; justify-content: space-between; align-items: center; font-size: 12px; padding: 4px 0; color: var(--vscode-foreground);">
<span>\${file.relativePath || file.path}</span> <span>\${file.name || file.absolutePath}</span>
<button onclick="removeDocsetFile(\${index})" style="background: transparent; border: none; color: var(--vscode-foreground); cursor: pointer; padding: 0 4px; opacity: 0.7;"> <button onclick="removeDocsetFile(\${index})" style="background: transparent; border: none; color: var(--vscode-foreground); cursor: pointer; padding: 0 4px; opacity: 0.7;">
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"> <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
<path d="M10 3h3v1h-1v9l-1 1H4l-1-1V4H2V3h3V2a1 1 0 0 1 1-1h3a1 1 0 0 1 1 1v1zM9 2H6v1h3V2zM4 13h7V4H4v9zm2-8H5v7h1V5zm1 0h1v7H7V5zm2 0h1v7H9V5z"/> <path d="M10 3h3v1h-1v9l-1 1H4l-1-1V4H2V3h3V2a1 1 0 0 1 1-1h3a1 1 0 0 1 1 1v1zM9 2H6v1h3V2zM4 13h7V4H4v9zm2-8H5v7h1V5zm1 0h1v7H7V5zm2 0h1v7H9V5z"/>
@ -538,7 +538,7 @@ export function getDocsetDialogScript(): string {
for (const file of message.files) { for (const file of message.files) {
if (file.size > MAX_FILE_SIZE) { if (file.size > MAX_FILE_SIZE) {
errors.push(\`\${file.path} 超过单个文件大小限制(\${formatFileSize(MAX_FILE_SIZE)}\`); errors.push(\`\${file.name || file.absolutePath} 超过单个文件大小限制(\${formatFileSize(MAX_FILE_SIZE)}\`);
} else { } else {
vaildFiles.push(file); vaildFiles.push(file);
} }