Compare commits
200 Commits
feat/plugi
...
feat/backe
| Author | SHA1 | Date | |
|---|---|---|---|
| 1881615860 | |||
| d0462ca4b9 | |||
| eae3968465 | |||
| a734ccbb88 | |||
| 7444bb1140 | |||
| 6ef7e976cc | |||
| 31419e93a1 | |||
| 173132399e | |||
| ae703091d4 | |||
| 8daea722bd | |||
| 032dd1b215 | |||
| 885e2cef75 | |||
| 9296b10150 | |||
| 423c9ddb0e | |||
| 50eacdafde | |||
| d90cca7cef | |||
| 5347425327 | |||
| 28d93c7e75 | |||
| 5339212de9 | |||
| 73a1510de4 | |||
| 606f757699 | |||
| 342bf22f3f | |||
| d2ec73f796 | |||
| c9f597beec | |||
| e9a201ef01 | |||
| 77a89847cb | |||
| c14b7f4dbc | |||
| 64724bf48c | |||
| c9e160f2ef | |||
| 3a19cc638f | |||
| a2e8e74572 | |||
| ad96743fad | |||
| 95b1bd7678 | |||
| 94b6fb056f | |||
| a24fd71636 | |||
| 7d1b8f7e26 | |||
| 5753e120ba | |||
| f55a5bfbcb | |||
| 83b706d5be | |||
| b9e63bc9a9 | |||
| ef0c8748f7 | |||
| 430a2c4062 | |||
| f5bd35c71a | |||
| f958683f53 | |||
| 21a8abd5cf | |||
| 4b2da8244f | |||
| c571cd9137 | |||
| 8cf0e32184 | |||
| 1cbd0c5fe7 | |||
| 72a84ed9e2 | |||
| 58113fb109 | |||
| 25966bc1e2 | |||
| 3c93c07afd | |||
| 85a37b546c | |||
| 37a121c3de | |||
| 341b6540fa | |||
| 1d074e5a94 | |||
| 5a5d82eef8 | |||
| 43189e144a | |||
| fd11eadc19 | |||
| 1231ef0892 | |||
| a1e88d473b | |||
| d08f9a7366 | |||
| faa7b63aee | |||
| e440dd2773 | |||
| a02027e7c9 | |||
| 772b067202 | |||
| a3fd5df8e8 | |||
| bdc55c727a | |||
| 52e4522ed0 | |||
| d44b316c9a | |||
| 939768986c | |||
| 1e99f3cb20 | |||
| 2af79cf1dc | |||
| 5b225126f1 | |||
| 4abb979eab | |||
| 4a790b5aca | |||
| 9786b7141c | |||
| 4a7af49fea | |||
| 15a1de3a90 | |||
| 4687c3faa6 | |||
| 5c19be22d3 | |||
| feff8ea4d3 | |||
| 6abec8c7b7 | |||
| f9b3699bda | |||
| 8da1177bf3 | |||
| a85a044a9b | |||
| 5546791549 | |||
| c58e3603de | |||
| 178f3a7498 | |||
| 940584e1ea | |||
| 4037e9e2d7 | |||
| 4b2f6967dc | |||
| 79ef879b97 | |||
| 1df7462778 | |||
| 0bcdc615e3 | |||
| 5577fe17bb | |||
| 820ee2f848 | |||
| be8365c8cb | |||
| b1dd2442b8 | |||
| 9281d1d724 | |||
| 226bb46094 | |||
| 251289a340 | |||
| c22081c5e9 | |||
| cca82c7885 | |||
| e4ff49bade | |||
| ada4806493 | |||
| 3831de2849 | |||
| 0df529c4fd | |||
| 5c53d7f0e9 | |||
| ef2a0dc16e | |||
| 5ce420295b | |||
| 1d7f3d7626 | |||
| 9b0d2d5e01 | |||
| 27e3351b55 | |||
| de3e84aa4e | |||
| e48e822d07 | |||
| 8dc34ee435 | |||
| d8cd86361e | |||
| acf3f9ff37 | |||
| c27b08cccf | |||
| 9fc3c9f056 | |||
| 60d8eaf0eb | |||
| df6f983e83 | |||
| acf60f2a17 | |||
| f933d84cd1 | |||
| b794d1ceb0 | |||
| 259310a29d | |||
| 715eac5949 | |||
| c2936395d9 | |||
| 8762eacb3e | |||
| 3d535fd3e1 | |||
| ecdbe0bdc0 | |||
| c49aaf753c | |||
| 0f8674e1c7 | |||
| ef2159f1bd | |||
| b662d25c9c | |||
| 1ce1ed715c | |||
| 2587018405 | |||
| 28b75e8475 | |||
| 5287d483d8 | |||
| 16e91bd2c0 | |||
| e6b6bc3698 | |||
| d43cd610a0 | |||
| 842e5fb49b | |||
| 430581598b | |||
| 2d5b297171 | |||
| 023fdb66c3 | |||
| 42481cd314 | |||
| e77194628a | |||
| 2aff54de74 | |||
| 91fadf591f | |||
| 02b56a7031 | |||
| c42ebdfe59 | |||
| 3f0cc8ae29 | |||
| 0f458f6299 | |||
| d415d8ee4e | |||
| bd7a85b705 | |||
| 44bbcde5cf | |||
| f9c9fa1840 | |||
| 53e91fc5a0 | |||
| 4288607ee2 | |||
| d4d86df7de | |||
| 4b8d255207 | |||
| a5dba25a8e | |||
| 719d1396b0 | |||
| 5b6ac43e13 | |||
| f7c2d86a46 | |||
| 83db55c790 | |||
| 94d41c3da9 | |||
| 83f9e2f005 | |||
| 318d3964bd | |||
| 770da72ce3 | |||
| d4c726ea9c | |||
| 082ef923b2 | |||
| c050f0e167 | |||
| 3daa66ea01 | |||
| 9bdaf34471 | |||
| 5cb68652f9 | |||
| 9bfa774336 | |||
| 009da59d38 | |||
| 5ea5ddba6e | |||
| 25a8ea5aa4 | |||
| ef83016b7f | |||
| 2e6812d00d | |||
| b676846b2f | |||
| 9c787627a9 | |||
| 463eedf1dd | |||
| fb1156d24f | |||
| 0b4ec2ca6e | |||
| 10f0877a5e | |||
| 5c2ea0f15c | |||
| 6c5d470bad | |||
| c21ad95963 | |||
| 7c1f1fae07 | |||
| c61e29a41f | |||
| 703912bb5f | |||
| 8ad6a48e8f | |||
| ba75541dd6 | |||
| f87adab7be |
11
.gitignore
vendored
@ -3,3 +3,14 @@ dist
|
|||||||
node_modules
|
node_modules
|
||||||
.vscode-test/
|
.vscode-test/
|
||||||
*.vsix
|
*.vsix
|
||||||
|
|
||||||
|
# waveform_trace 打包产物(exe 太大,通过 Release 发布)
|
||||||
|
tools/waveform_trace/bin/
|
||||||
|
tools/waveform_trace/src/build/
|
||||||
|
tools/waveform_trace/src/dist/
|
||||||
|
tools/waveform_trace/src/*.spec
|
||||||
|
|
||||||
|
# Python 缓存
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
|||||||
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
9
.idea/IC-Coder-Plugin.iml
generated
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
16
.idea/codeStyles/Project.xml
generated
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<code_scheme name="Project" version="173">
|
||||||
|
<JSCodeStyleSettings version="0">
|
||||||
|
<option name="FORCE_SEMICOLON_STYLE" value="true" />
|
||||||
|
</JSCodeStyleSettings>
|
||||||
|
<JavaCodeStyleSettings>
|
||||||
|
<option name="ENABLE_JAVADOC_FORMATTING" value="false" />
|
||||||
|
</JavaCodeStyleSettings>
|
||||||
|
<codeStyleSettings language="JavaScript">
|
||||||
|
<option name="IF_BRACE_FORCE" value="3" />
|
||||||
|
<option name="DOWHILE_BRACE_FORCE" value="3" />
|
||||||
|
<option name="WHILE_BRACE_FORCE" value="3" />
|
||||||
|
<option name="FOR_BRACE_FORCE" value="3" />
|
||||||
|
</codeStyleSettings>
|
||||||
|
</code_scheme>
|
||||||
|
</component>
|
||||||
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
|
||||||
|
</state>
|
||||||
|
</component>
|
||||||
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/IC-Coder-Plugin.iml" filepath="$PROJECT_DIR$/.idea/IC-Coder-Plugin.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
4
.vscode/settings.json
vendored
@ -9,5 +9,7 @@
|
|||||||
"dist": true // set this to false to include "dist" folder in search results
|
"dist": true // set this to false to include "dist" folder in search results
|
||||||
},
|
},
|
||||||
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
|
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
|
||||||
"typescript.tsc.autoDetect": "off"
|
"typescript.tsc.autoDetect": "off",
|
||||||
|
// IC Coder 后端服务地址
|
||||||
|
"icCoder.backendUrl": "http://192.168.1.108:2233"
|
||||||
}
|
}
|
||||||
|
|||||||
24
CHANGELOG.md
@ -1,9 +1,23 @@
|
|||||||
# Change Log
|
# 更新日志
|
||||||
|
|
||||||
All notable changes to the "ic-coder" extension will be documented in this file.
|
所有重要的项目变更都将记录在此文件中。
|
||||||
|
|
||||||
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
## [1.0.2] - 2026-01-13
|
||||||
|
|
||||||
## [Unreleased]
|
IC Coder插件端正式发布。
|
||||||
|
|
||||||
- Initial release
|
IC Coder 插件端是一个专为 FPGA 开发设计的 VS Code 扩展,提供 AI 驱动的智能辅助功能。
|
||||||
|
|
||||||
|
主要功能:
|
||||||
|
- VCD波形解析
|
||||||
|
- 自动生成完整工程
|
||||||
|
- 自动仿真
|
||||||
|
- 自主代码迭代
|
||||||
|
- 智能匹配最优模型
|
||||||
|
- 多线程任务处理
|
||||||
|
- 实时跟随
|
||||||
|
- 丰富的上下文工具
|
||||||
|
- 全双工交互
|
||||||
|
- 多层次安全保障
|
||||||
|
- 自动搭建电路架构
|
||||||
|
- 多平台支持
|
||||||
|
|||||||
90
CLAUDE.md
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Project Overview
|
||||||
|
|
||||||
|
IC Coder Plugin 是一个 VS Code 扩展,为 Verilog/FPGA 开发提供智能辅助功能,包括代码生成、文件操作、iverilog 仿真和 VCD 波形查看。
|
||||||
|
|
||||||
|
## Build Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 安装依赖
|
||||||
|
pnpm install
|
||||||
|
|
||||||
|
# 编译 (开发模式)
|
||||||
|
pnpm run compile
|
||||||
|
|
||||||
|
# 监听模式编译
|
||||||
|
pnpm run watch
|
||||||
|
|
||||||
|
# 生产环境打包
|
||||||
|
pnpm run package
|
||||||
|
|
||||||
|
# 代码检查
|
||||||
|
pnpm run lint
|
||||||
|
|
||||||
|
# 运行测试
|
||||||
|
pnpm run test
|
||||||
|
|
||||||
|
# 编译测试文件
|
||||||
|
pnpm run compile-tests
|
||||||
|
```
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
- 按 F5 在 VS Code 中启动调试模式
|
||||||
|
- 使用 webpack 打包,入口文件为 `src/extension.ts`
|
||||||
|
- 输出目录为 `dist/`
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
```
|
||||||
|
src/
|
||||||
|
├── extension.ts # 插件入口,注册命令和视图
|
||||||
|
├── panels/
|
||||||
|
│ ├── ICHelperPanel.ts # 主聊天面板 (WebviewPanel)
|
||||||
|
│ └── VCDViewerPanel.ts # VCD 波形查看器面板
|
||||||
|
├── views/
|
||||||
|
│ ├── ICViewProvider.ts # 侧边栏视图提供者
|
||||||
|
│ └── webviewContent.ts # Webview HTML 内容 (大文件,使用搜索)
|
||||||
|
├── utils/
|
||||||
|
│ ├── messageHandler.ts # 消息处理核心逻辑 (大文件,使用搜索)
|
||||||
|
│ ├── iverilogRunner.ts # iverilog 编译和仿真执行
|
||||||
|
│ ├── chatHistoryManager.ts # 会话历史管理
|
||||||
|
│ ├── createFiles.ts # 文件创建工具
|
||||||
|
│ └── readFiles.ts # 文件读取工具
|
||||||
|
├── types/
|
||||||
|
│ └── chatHistory.ts # 消息类型定义 (LangChain4j 格式)
|
||||||
|
└── test/ # 测试文件
|
||||||
|
```
|
||||||
|
|
||||||
|
### Key Components
|
||||||
|
|
||||||
|
**消息流程**: Webview -> `onDidReceiveMessage` -> `messageHandler.ts` -> 后端处理 -> `panel.webview.postMessage` -> Webview
|
||||||
|
|
||||||
|
**消息类型** (`src/types/chatHistory.ts`):
|
||||||
|
- `MessageType.SYSTEM` / `USER` / `AI` / `TOOL_EXECUTION_RESULT`
|
||||||
|
- 兼容 LangChain4j 格式
|
||||||
|
|
||||||
|
**iverilog 集成** (`tools/iverilog/`):
|
||||||
|
- 内置 Windows x64 版本的 iverilog/vvp
|
||||||
|
- 通过 `IVERILOG_ROOT` 环境变量配置库路径
|
||||||
|
- 支持命令: "生成 VCD"、"运行仿真"、"生成波形"
|
||||||
|
|
||||||
|
## VS Code Extension Points
|
||||||
|
|
||||||
|
- 命令: `ic-coder.openPanel`, `ic-coder.openChat`, `ic-coder.openVCDViewer`
|
||||||
|
- 侧边栏视图: `ic-coder.mainView`
|
||||||
|
- 激活事件: `onLanguage:verilog`, `onLanguage:vhdl`, `onStartupFinished`
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
- `vcdrom`, `vcd-stream`, `waveql` - VCD 波形处理
|
||||||
|
- `@wavedrom/doppler`, `onml` - 波形渲染
|
||||||
|
- `iconv-lite` - 编码转换
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- `webviewContent.ts` 和 `messageHandler.ts` 文件较大,建议使用搜索而非完整读取
|
||||||
|
- 当前仅支持 Windows 平台的 iverilog,其他平台需用户自行安装
|
||||||
12
LICENSE
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
Copyright (c) 2025 IC Coder Team. All rights reserved.
|
||||||
|
|
||||||
|
本软件及其相关文档文件(以下简称"软件")的版权归 IC Coder 所有。
|
||||||
|
|
||||||
|
未经版权所有者事先书面许可,不得以任何形式或方式(电子、机械、复印、录制或其他方式)
|
||||||
|
复制、分发、传播、展示、修改或创建本软件的衍生作品。
|
||||||
|
|
||||||
|
本软件按"原样"提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性
|
||||||
|
和非侵权性的保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,
|
||||||
|
无论是在合同诉讼、侵权行为还是其他方面。
|
||||||
|
|
||||||
|
如需商业使用或获取许可,请联系:[pyjtkj@pyjtkj.com]
|
||||||
399
PUBLISH.md
Normal file
@ -0,0 +1,399 @@
|
|||||||
|
# IC Coder 插件发布流程文档
|
||||||
|
|
||||||
|
本文档详细说明如何将 IC Coder 插件发布到 VS Code 插件市场进行测试和正式发布。
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [前置准备](#前置准备)
|
||||||
|
- [账号配置](#账号配置)
|
||||||
|
- [插件信息完善](#插件信息完善)
|
||||||
|
- [打包与发布](#打包与发布)
|
||||||
|
- [版本更新](#版本更新)
|
||||||
|
- [常见问题](#常见问题)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 前置准备
|
||||||
|
|
||||||
|
### 环境要求
|
||||||
|
|
||||||
|
- Node.js 和 pnpm 已安装
|
||||||
|
- VS Code 1.80.0 或更高版本
|
||||||
|
- 已安装 `@vscode/vsce` 工具(项目已包含)
|
||||||
|
|
||||||
|
### 检查清单
|
||||||
|
|
||||||
|
在发布前,请确保以下文件和配置已准备就绪:
|
||||||
|
|
||||||
|
- [x] `package.json` - 插件配置文件
|
||||||
|
- [x] `README.md` - 插件说明文档
|
||||||
|
- [x] `dist/` - 编译后的代码
|
||||||
|
- [x] `media/` - 图标和资源文件
|
||||||
|
- [ ] `CHANGELOG.md` - 版本更新日志(建议添加)
|
||||||
|
- [x] `LICENSE` - 开源许可证(建议添加)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 账号配置
|
||||||
|
|
||||||
|
### 1. 创建 Azure DevOps 账号
|
||||||
|
|
||||||
|
1. 访问 [Azure DevOps](https://dev.azure.com)
|
||||||
|
2. 使用 Microsoft 账号注册或登录
|
||||||
|
3. 创建一个组织(如果还没有)
|
||||||
|
|
||||||
|
### 2. 生成 Personal Access Token (PAT)
|
||||||
|
|
||||||
|
这是发布插件的关键凭证,请妥善保管。
|
||||||
|
|
||||||
|
**步骤:**
|
||||||
|
|
||||||
|
1. 登录 Azure DevOps
|
||||||
|
2. 点击右上角用户图标 → **User settings** → **Personal access tokens**
|
||||||
|
3. 点击 **New Token** 按钮
|
||||||
|
4. 配置 Token 信息:
|
||||||
|
- **Name**: `vscode-publisher`(或其他易识别的名称)
|
||||||
|
- **Organization**: 选择 **All accessible organizations**
|
||||||
|
- **Expiration**: 建议选择较长期限(如 90 天或自定义)
|
||||||
|
- **Scopes**: 选择 **Custom defined**
|
||||||
|
- 展开 **Marketplace**
|
||||||
|
- 勾选 **Manage**(包含发布和管理权限)
|
||||||
|
5. 点击 **Create** 生成 Token
|
||||||
|
6. **重要**: 立即复制并保存 Token,页面关闭后将无法再次查看
|
||||||
|
|
||||||
|
**Token 示例格式:**
|
||||||
|
|
||||||
|
```
|
||||||
|
CO03l8nmFBBTNPDg7lN9a9fYwDdgsRIDVDwTrx6Esggi6HnzmrMTJQQJ99BLACAAAAAAAAAAAAAGAZDOVVyT
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
//蔡工的token
|
||||||
|
6CB3tOZPiwNi6rrOuFHMe6QzrVWBnajW5fJsNgCWu8jtERUCCRnJJQQJ99CAACAAAAAAAAAAAAASAZDO3FnY
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 创建发布者账号
|
||||||
|
|
||||||
|
发布者账号是你在 VS Code 市场的身份标识。
|
||||||
|
|
||||||
|
**步骤:**
|
||||||
|
|
||||||
|
1. 访问 [VS Code Marketplace 管理页面](https://marketplace.visualstudio.com/manage)
|
||||||
|
2. 使用 Azure DevOps 账号登录
|
||||||
|
3. 点击 **Create publisher** 按钮
|
||||||
|
4. 填写发布者信息:
|
||||||
|
- **ID**: `ICCoder`(必须与 package.json 中的 `publisher` 字段一致)
|
||||||
|
- **Name**: `IC Coder`(显示名称,可自定义)
|
||||||
|
- **Email**: 你的联系邮箱
|
||||||
|
5. 点击 **Create** 完成创建
|
||||||
|
|
||||||
|
**注意事项:**
|
||||||
|
- Publisher ID 一旦创建无法修改
|
||||||
|
- Publisher ID 必须全局唯一
|
||||||
|
- 建议使用有意义且专业的 ID
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 插件信息完善
|
||||||
|
|
||||||
|
### 1. 完善 package.json
|
||||||
|
|
||||||
|
建议在 `package.json` 中添加以下字段以提升插件质量:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/your-org/ic-coder.git"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/your-org/ic-coder#readme",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/your-org/ic-coder/issues"
|
||||||
|
},
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 创建 CHANGELOG.md
|
||||||
|
|
||||||
|
版本更新日志帮助用户了解每个版本的变化。
|
||||||
|
|
||||||
|
**示例内容:**
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
# 更新日志
|
||||||
|
|
||||||
|
## [0.0.2] - 2025-12-29
|
||||||
|
|
||||||
|
### 新增
|
||||||
|
- 添加发送和暂停按钮功能
|
||||||
|
- 添加一键优化按钮组件
|
||||||
|
- 添加 Plan 开关组件
|
||||||
|
- 添加模式选择器组件
|
||||||
|
- 添加上下文压缩功能
|
||||||
|
|
||||||
|
### 改进
|
||||||
|
- 优化用户界面交互体验
|
||||||
|
|
||||||
|
## [0.0.1] - 2025-12-XX
|
||||||
|
|
||||||
|
### 新增
|
||||||
|
- 初始版本发布
|
||||||
|
- Verilog 代码智能生成
|
||||||
|
- 集成 iverilog 仿真工具
|
||||||
|
- VCD 波形文件查看器
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 创建 LICENSE 文件
|
||||||
|
|
||||||
|
如果使用 MIT 许可证,创建 `LICENSE` 文件:
|
||||||
|
|
||||||
|
```
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 IC Coder Team
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction...
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 优化 README.md
|
||||||
|
|
||||||
|
确保 README 包含:
|
||||||
|
- 清晰的功能介绍
|
||||||
|
- 使用截图或 GIF 演示
|
||||||
|
- 详细的使用说明
|
||||||
|
- 系统要求
|
||||||
|
- 常见问题解答
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 打包与发布
|
||||||
|
|
||||||
|
### 方式一:命令行发布(推荐)
|
||||||
|
|
||||||
|
这是最便捷的发布方式,适合频繁更新。
|
||||||
|
|
||||||
|
**步骤:**
|
||||||
|
|
||||||
|
1. **登录发布者账号**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm vsce login ic-coder-team
|
||||||
|
```
|
||||||
|
|
||||||
|
系统会提示输入 Personal Access Token,粘贴之前创建的 PAT。
|
||||||
|
|
||||||
|
2. **打包插件**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 执行生产环境构建
|
||||||
|
pnpm run package
|
||||||
|
|
||||||
|
# 打包成 .vsix 文件
|
||||||
|
pnpm vsce package
|
||||||
|
```
|
||||||
|
|
||||||
|
这会生成 `ic-coder-plugin-0.0.2.vsix` 文件。
|
||||||
|
|
||||||
|
3. **发布到市场**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm vsce publish
|
||||||
|
```
|
||||||
|
|
||||||
|
发布成功后会显示插件的市场链接。
|
||||||
|
|
||||||
|
**一键发布(跳过打包步骤):**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 直接发布当前版本
|
||||||
|
pnpm vsce publish
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方式二:手动上传
|
||||||
|
|
||||||
|
适合首次发布或网络环境受限的情况。
|
||||||
|
|
||||||
|
**步骤:**
|
||||||
|
|
||||||
|
1. 本地打包插件:
|
||||||
|
```bash
|
||||||
|
pnpm run package
|
||||||
|
pnpm vsce package[pnpm vsce package --no-dependencies]
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 访问 [发布者管理页面](https://marketplace.visualstudio.com/manage/publishers/ic-coder-team)
|
||||||
|
|
||||||
|
3. 点击 **New extension** → **Visual Studio Code**
|
||||||
|
|
||||||
|
4. 上传 `ic-coder-plugin-0.0.2.vsix` 文件
|
||||||
|
|
||||||
|
5. 填写插件信息(如果需要)并提交
|
||||||
|
|
||||||
|
6. 等待审核通过
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 版本更新
|
||||||
|
|
||||||
|
### 自动更新版本号
|
||||||
|
|
||||||
|
使用 `vsce publish` 命令可以自动更新版本号并发布:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 补丁版本更新(0.0.2 → 0.0.3)
|
||||||
|
pnpm vsce publish patch
|
||||||
|
|
||||||
|
# 次版本更新(0.0.2 → 0.1.0)
|
||||||
|
pnpm vsce publish minor
|
||||||
|
|
||||||
|
# 主版本更新(0.0.2 → 1.0.0)
|
||||||
|
pnpm vsce publish major
|
||||||
|
```
|
||||||
|
|
||||||
|
### 手动指定版本
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 发布指定版本
|
||||||
|
pnpm vsce publish 0.0.3
|
||||||
|
```
|
||||||
|
|
||||||
|
### 更新流程建议
|
||||||
|
|
||||||
|
1. 修改代码并测试
|
||||||
|
2. 更新 `CHANGELOG.md` 记录变更
|
||||||
|
3. 提交代码到 Git
|
||||||
|
4. 执行发布命令
|
||||||
|
5. 验证市场上的插件是否正常
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 更新流程
|
||||||
|
|
||||||
|
1. 修改版本号
|
||||||
|
|
||||||
|
手动修改 修改package.json文件
|
||||||
|
|
||||||
|
命令修改
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#补丁版本 (1.0.0 -> 1.0.1)
|
||||||
|
pnpm version patch
|
||||||
|
|
||||||
|
#次要版本 (1.0.0 -> 1.1.0)
|
||||||
|
pnpm version minor
|
||||||
|
|
||||||
|
#主要版本 (1.0.0 -> 2.0.0)
|
||||||
|
pnpm version major
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 打包
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#先build
|
||||||
|
pnpm run build
|
||||||
|
|
||||||
|
#后打包成.vsix
|
||||||
|
pnpm vsce package --no-dependencies
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
3. 手动上传/命令上传
|
||||||
|
|
||||||
|
- https://marketplace.visualstudio.com/ 在这个里面手动上传 更新就选择update
|
||||||
|
- 命令上传:vsce publish
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 常见问题
|
||||||
|
|
||||||
|
### 1. 发布失败:Authentication failed
|
||||||
|
|
||||||
|
**原因:** PAT Token 无效或过期
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
- 重新生成 PAT Token
|
||||||
|
- 重新登录:`pnpm vsce login ic-coder-team`
|
||||||
|
|
||||||
|
### 2. 发布失败:Publisher not found
|
||||||
|
|
||||||
|
**原因:** Publisher ID 不存在或不匹配
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
- 检查 `package.json` 中的 `publisher` 字段
|
||||||
|
- 确认已在市场创建对应的 Publisher
|
||||||
|
|
||||||
|
### 3. 打包失败:Missing files
|
||||||
|
|
||||||
|
**原因:** 必需文件缺失
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
- 确保 `dist/` 目录存在且包含编译后的代码
|
||||||
|
- 运行 `pnpm run package` 重新构建
|
||||||
|
|
||||||
|
### 4. 插件审核被拒
|
||||||
|
|
||||||
|
**常见原因:**
|
||||||
|
- 插件名称或描述违反市场规则
|
||||||
|
- 图标不符合要求(建议 128x128 PNG)
|
||||||
|
- README 内容不完整
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
- 查看审核反馈邮件
|
||||||
|
- 修改相关内容后重新发布
|
||||||
|
|
||||||
|
### 5. 如何撤回已发布的版本?
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 取消发布指定版本
|
||||||
|
pnpm vsce unpublish ic-coder-team.ic-coder-plugin@0.0.2
|
||||||
|
|
||||||
|
# 取消发布整个插件(慎用)
|
||||||
|
pnpm vsce unpublish ic-coder-team.ic-coder-plugin
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. 如何本地测试 .vsix 文件?
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 在 VS Code 中安装本地 .vsix 文件
|
||||||
|
code --install-extension ic-coder-plugin-0.0.2.vsix
|
||||||
|
```
|
||||||
|
|
||||||
|
或者在 VS Code 中:
|
||||||
|
1. 打开扩展面板
|
||||||
|
2. 点击 `...` 菜单
|
||||||
|
3. 选择 **Install from VSIX...**
|
||||||
|
4. 选择 `.vsix` 文件
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 发布检查清单
|
||||||
|
|
||||||
|
在正式发布前,请确认以下事项:
|
||||||
|
|
||||||
|
- [ ] 代码已充分测试,无明显 Bug
|
||||||
|
- [ ] `package.json` 版本号已更新
|
||||||
|
- [ ] `CHANGELOG.md` 已记录本次更新内容
|
||||||
|
- [ ] README.md 内容完整且准确
|
||||||
|
- [ ] 图标和资源文件正常显示
|
||||||
|
- [ ] 已在本地安装测试 .vsix 文件
|
||||||
|
- [ ] 已创建 Azure DevOps PAT Token
|
||||||
|
- [ ] 已创建 VS Code Marketplace Publisher
|
||||||
|
- [ ] 已执行 `pnpm run package` 构建生产版本
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 参考资源
|
||||||
|
|
||||||
|
- [VS Code 插件发布官方文档](https://code.visualstudio.com/api/working-with-extensions/publishing-extension)
|
||||||
|
- [vsce 工具文档](https://github.com/microsoft/vscode-vsce)
|
||||||
|
- [Azure DevOps 文档](https://docs.microsoft.com/en-us/azure/devops/)
|
||||||
|
- [VS Code 插件市场](https://marketplace.visualstudio.com/)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**文档维护:** IC Coder Team
|
||||||
|
**最后更新:** 2025-12-29
|
||||||
94
README.md
@ -1,24 +1,88 @@
|
|||||||
# IC Coder Plugin
|
## 什么是IC Coder
|
||||||
|
|
||||||
IC Coder 是一个面向 Verilog/FPGA 开发的智能辅助插件。
|
IC Coder是一款**The Agentic AI Verilog Coding Platform(自主式人工智能 Verilog 编码平台)**。我们立志于用AI重塑芯片开发者的效率,将芯片设计与验证的效率提升至少20倍!让芯片开发者们,都能享受到AI发展所带来的科技福利!目标成为全球最好用的"LLM生成Verilog"的平台!
|
||||||
|
|
||||||
## 功能特性
|

|
||||||
|
|
||||||
- Verilog 代码智能生成
|
### 核心技术架构
|
||||||
- 文件操作支持(创建、读取、修改、删除)
|
|
||||||
- 集成 iverilog 仿真工具
|
|
||||||
- VCD 波形文件生成
|
|
||||||
- 智能对话助手
|
|
||||||
|
|
||||||
## 使用说明
|
**我们采用全球顶尖的大语言模型**,加上自研的针对芯片设计领域深度优化的微调模型,为代码生成提供强大的AI能力支撑。
|
||||||
|
|
||||||
安装插件后,点击侧边栏的 IC Coder 图标即可开始使用。
|
**核心技术栈**包括:
|
||||||
|
|
||||||
## 系统要求
|
- **多智能体架构(Multi-Agent System)**:多个专业化AI智能体协同工作,分别负责架构设计、代码生成、验证测试等不同环节
|
||||||
|
- **增强上下文引擎**:智能理解和管理大规模设计上下文,确保生成代码的一致性和准确性
|
||||||
|
- **自研EDA工具集**:完整的仿真、综合、时序分析工具链,无缝集成到AI工作流中
|
||||||
|
|
||||||
- VS Code 1.107.0 或更高版本
|
这些技术共同支撑着从需求分析、架构设计、代码生成到验证调试的全流程智能化开发体验。
|
||||||
- 插件已内置 iverilog 工具(Windows 平台)
|
|
||||||
|
|
||||||
## 许可证
|

|
||||||
|
|
||||||
MIT
|
## 自动搭建电路架构
|
||||||
|
|
||||||
|
IC Coder能够根据自然语言描述的设计需求,自动生成完整的电路架构。系统会:
|
||||||
|
|
||||||
|
- **智能解析需求**:理解功能规格、性能指标、接口要求等设计约束
|
||||||
|
- **自动模块划分**:根据功能将设计合理拆分为多个子模块,确保模块化和可复用性
|
||||||
|
- **生成层次结构**:建立清晰的模块层次关系,自动处理模块间的信号连接
|
||||||
|
- **结构化信号管理**:将所有电路信号关系进行结构化表示,包括数据流向、控制逻辑、时序关系等
|
||||||
|
- **可视化展示**:以图形化方式展示整体架构,便于理解和审查设计方案
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## AI自主仿真
|
||||||
|
|
||||||
|
IC Coder提供完全自动化的仿真验证流程,无需手动编写测试代码:
|
||||||
|
|
||||||
|
- **智能Testbench生成**:根据设计模块自动生成完整的测试平台,包括激励生成、时钟复位、接口驱动等
|
||||||
|
- **测试用例自动化**:根据设计规格自动生成覆盖各种场景的测试用例,包括正常功能、边界条件、异常情况等
|
||||||
|
- **一键运行仿真**:自动调用集成仿真器执行仿真
|
||||||
|
- **波形自动生成**:仿真完成后自动生成VCD、波形文件,便于后续分析
|
||||||
|
- **实时进度反馈**:仿真过程中实时显示执行状态和日志信息
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## AI自主代码迭代
|
||||||
|
|
||||||
|
IC Coder实现了真正的自主式开发循环,能够持续优化代码直到满足设计要求:
|
||||||
|
|
||||||
|
- **智能问题诊断**:根据波形分析结果,自动定位代码中的问题根源
|
||||||
|
- **自动代码修复**:针对发现的问题自动生成修复方案并更新代码
|
||||||
|
- **迭代验证循环**:修复后自动重新运行仿真和波形分析,验证问题是否解决
|
||||||
|
- **持续优化**:如果仍存在问题,继续分析和修复,形成闭环迭代
|
||||||
|
- **收敛保证**:智能判断迭代进展,避免无效循环,确保最终收敛到正确设计
|
||||||
|
- **全程可追溯**:记录每次迭代的修改内容和验证结果,便于回溯和审查
|
||||||
|
|
||||||
|
这种自主迭代能力大幅减少了人工调试时间,让设计验证过程更加高效可靠。
|
||||||
|
|
||||||
|
## 随时可掌控
|
||||||
|
|
||||||
|
IC Coder提供透明化的开发过程,让用户始终掌握AI的工作状态:
|
||||||
|
|
||||||
|
- **实时流程展示**:可视化展示当前执行到哪个阶段(需求分析、架构设计、代码生成、仿真验证等)
|
||||||
|
- **详细执行日志**:记录每一步操作的详细信息,包括AI的思考过程、决策依据、执行结果
|
||||||
|
- **人机协同交互**:在关键决策点支持用户介入,可随时提供反馈、调整方向或修改参数
|
||||||
|
- **进度实时追踪**:显示任务完成进度、预计剩余步骤,让开发过程更加可预期
|
||||||
|
- **智能建议系统**:AI主动提供优化建议和替代方案,用户可选择采纳或自定义
|
||||||
|
- **即时响应机制**:支持随时暂停、恢复或调整AI的工作流程
|
||||||
|
|
||||||
|
这种透明可控的设计理念,让AI开发不再是"黑盒",而是真正的智能协作伙伴。
|
||||||
|
|
||||||
|
## 多层次安全保障
|
||||||
|
|
||||||
|
IC Coder将数据安全和隐私保护作为核心设计原则,提供企业级的安全保障:
|
||||||
|
|
||||||
|
- **本地优先存储**:所有设计文件默认存储在本地,用户完全掌控自己的代码资产
|
||||||
|
- **全链路加密传输**:与云端通信采用TLS/SSL加密,确保数据传输过程中不被窃取或篡改
|
||||||
|
- **云端零存储策略**:云端服务器不保存用户的源代码,仅处理加密后的临时数据,处理完成后立即销毁
|
||||||
|
- **定制化部署选项**:支持企业私有云或本地部署,满足高安全等级需求
|
||||||
|
|
||||||
|
真正做到了代码全链路加密传输、云端零存储,让芯片设计企业可以放心使用AI工具。
|
||||||
|
|
||||||
|
## 反馈
|
||||||
|
|
||||||
|
无论是想与我们深入交流还是遇到任何问题,欢迎您[进入社区](https://iccoder.com:888/community)与我们联系
|
||||||
|
|
||||||
|
## 服务条款和隐私协议
|
||||||
|
|
||||||
|
请阅读我们的[服务条款](https://iccoder.com:888/guides/legal/terms-of-service)和[隐私协议](https://iccoder.com:888/guides/legal/privacy-policy)了解更多细节。
|
||||||
|
|||||||
573
docs/authentication-implementation.md
Normal file
@ -0,0 +1,573 @@
|
|||||||
|
# IC Coder 认证系统实现文档
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
本文档详细说明了 IC Coder 插件如何集成 VSCode Authentication API,实现用户登录功能,并在 VSCode 左下角账户区域显示登录状态。
|
||||||
|
|
||||||
|
## 架构设计
|
||||||
|
|
||||||
|
### 核心组件
|
||||||
|
|
||||||
|
1. **ICCoderAuthenticationProvider** - 认证提供者
|
||||||
|
2. **VSCode Authentication API** - VSCode 官方认证接口
|
||||||
|
3. **本地 HTTP 服务器** - 处理登录回调
|
||||||
|
4. **ICViewProvider** - 侧边栏视图(根据登录状态显示不同按钮)
|
||||||
|
|
||||||
|
### 工作流程
|
||||||
|
|
||||||
|
```
|
||||||
|
用户点击登录
|
||||||
|
↓
|
||||||
|
调用 vscode.authentication.getSession()
|
||||||
|
↓
|
||||||
|
ICCoderAuthenticationProvider.createSession()
|
||||||
|
↓
|
||||||
|
启动本地 HTTP 服务器(动态端口)
|
||||||
|
↓
|
||||||
|
打开浏览器访问登录页面
|
||||||
|
↓
|
||||||
|
用户在网站完成登录
|
||||||
|
↓
|
||||||
|
网站重定向到 http://localhost:{port}/callback?token=xxx
|
||||||
|
↓
|
||||||
|
本地服务器接收 token
|
||||||
|
↓
|
||||||
|
创建 AuthenticationSession
|
||||||
|
↓
|
||||||
|
VSCode 左下角显示账户信息
|
||||||
|
```
|
||||||
|
|
||||||
|
## 详细实现
|
||||||
|
|
||||||
|
### 1. Authentication Provider 实现
|
||||||
|
|
||||||
|
文件:`src/services/icCoderAuthProvider.ts`
|
||||||
|
|
||||||
|
#### 1.1 类定义
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export class ICCoderAuthenticationProvider
|
||||||
|
implements vscode.AuthenticationProvider
|
||||||
|
{
|
||||||
|
private _onDidChangeSessions =
|
||||||
|
new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
|
||||||
|
public readonly onDidChangeSessions = this._onDidChangeSessions.event;
|
||||||
|
|
||||||
|
private _sessions: vscode.AuthenticationSession[] = [];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键点:**
|
||||||
|
|
||||||
|
- 实现 `vscode.AuthenticationProvider` 接口
|
||||||
|
- 使用 `EventEmitter` 通知会话变化
|
||||||
|
- 在内存中维护会话列表
|
||||||
|
|
||||||
|
#### 1.2 核心方法
|
||||||
|
|
||||||
|
##### getSessions() - 获取会话列表
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async getSessions(scopes?: readonly string[]): Promise<readonly vscode.AuthenticationSession[]> {
|
||||||
|
return this._sessions;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
##### createSession() - 创建会话(登录)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async createSession(scopes: readonly string[]): Promise<vscode.AuthenticationSession> {
|
||||||
|
const token = await this.login();
|
||||||
|
|
||||||
|
const session: vscode.AuthenticationSession = {
|
||||||
|
id: this.generateSessionId(),
|
||||||
|
accessToken: token,
|
||||||
|
account: {
|
||||||
|
id: "iccoder-user",
|
||||||
|
label: "IC Coder 用户",
|
||||||
|
},
|
||||||
|
scopes: [...scopes],
|
||||||
|
};
|
||||||
|
|
||||||
|
this._sessions.push(session);
|
||||||
|
await this.saveSessions();
|
||||||
|
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [session],
|
||||||
|
removed: [],
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键点:**
|
||||||
|
|
||||||
|
- 调用 `login()` 方法获取 token
|
||||||
|
- 创建 `AuthenticationSession` 对象
|
||||||
|
- 保存到 `globalState`
|
||||||
|
- 触发 `onDidChangeSessions` 事件通知 VSCode
|
||||||
|
|
||||||
|
##### removeSession() - 删除会话(登出)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async removeSession(sessionId: string): Promise<void> {
|
||||||
|
const sessionIndex = this._sessions.findIndex((s) => s.id === sessionId);
|
||||||
|
if (sessionIndex > -1) {
|
||||||
|
const session = this._sessions[sessionIndex];
|
||||||
|
this._sessions.splice(sessionIndex, 1);
|
||||||
|
await this.saveSessions();
|
||||||
|
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [],
|
||||||
|
removed: [session],
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 本地 HTTP 服务器实现
|
||||||
|
|
||||||
|
#### 2.1 动态端口分配
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
server.listen(0, () => {
|
||||||
|
const address = server.address();
|
||||||
|
const port = typeof address === "object" && address ? address.port : 3000;
|
||||||
|
resolve({ server, port });
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键点:**
|
||||||
|
|
||||||
|
- 使用端口 `0` 让系统自动分配可用端口
|
||||||
|
- 避免端口冲突问题
|
||||||
|
- 支持多个用户同时使用
|
||||||
|
|
||||||
|
#### 2.2 回调处理
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
if (url.pathname === "/callback") {
|
||||||
|
const token = url.searchParams.get("token");
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
// 返回成功页面
|
||||||
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
||||||
|
res.end(this.getSuccessPage(iconBase64));
|
||||||
|
|
||||||
|
// 关闭服务器
|
||||||
|
server.close();
|
||||||
|
|
||||||
|
// 返回 token
|
||||||
|
if ((server as any)._loginResolve) {
|
||||||
|
(server as any)._loginResolve(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. package.json 配置
|
||||||
|
|
||||||
|
#### 3.1 注册 Authentication Provider
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"contributes": {
|
||||||
|
"authentication": [
|
||||||
|
{
|
||||||
|
"id": "iccoder",
|
||||||
|
"label": "IC Coder"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键点:**
|
||||||
|
|
||||||
|
- `id` 必须与代码中使用的 ID 一致
|
||||||
|
- `label` 会显示在 VSCode 账户菜单中
|
||||||
|
|
||||||
|
#### 3.2 注册命令
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"contributes": {
|
||||||
|
"commands": [
|
||||||
|
{
|
||||||
|
"command": "ic-coder.login",
|
||||||
|
"title": "IC Coder: 登录账户",
|
||||||
|
"category": "IC Coder"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "ic-coder.logout",
|
||||||
|
"title": "IC Coder: 退出登录",
|
||||||
|
"category": "IC Coder"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. extension.ts 注册
|
||||||
|
|
||||||
|
#### 4.1 注册 Authentication Provider
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
|
// 注册 Authentication Provider
|
||||||
|
const authProvider = new ICCoderAuthenticationProvider(context);
|
||||||
|
context.subscriptions.push(
|
||||||
|
vscode.authentication.registerAuthenticationProvider(
|
||||||
|
"iccoder",
|
||||||
|
"IC Coder",
|
||||||
|
authProvider
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4.2 登录命令
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const loginCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.login",
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
createIfNone: true,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
vscode.window.showErrorMessage(`登录失败: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
**关键点:**
|
||||||
|
|
||||||
|
- `createIfNone: true` 会在没有会话时自动调用 `createSession()`
|
||||||
|
- VSCode 会自动处理 UI 交互
|
||||||
|
|
||||||
|
#### 4.3 登出命令
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const logoutCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.logout",
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
createIfNone: false,
|
||||||
|
});
|
||||||
|
if (session) {
|
||||||
|
await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
clearSessionPreference: true,
|
||||||
|
forceNewSession: true,
|
||||||
|
});
|
||||||
|
vscode.window.showInformationMessage("已退出登录");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
vscode.window.showInformationMessage("当前未登录");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. ICViewProvider 集成
|
||||||
|
|
||||||
|
#### 5.1 检查登录状态
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
private async checkLoginStatus(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
|
||||||
|
return !!session;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.2 根据登录状态显示不同按钮
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
resolveWebviewView(webviewView: vscode.WebviewView) {
|
||||||
|
this.checkLoginStatus().then((isLoggedIn) => {
|
||||||
|
webviewView.webview.html = this.getWebviewContent(
|
||||||
|
webviewView.webview,
|
||||||
|
isLoggedIn
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
${isLoggedIn
|
||||||
|
? '<button class="btn" onclick="openChat()">开始创作</button>'
|
||||||
|
: '<button class="btn" onclick="login()">登录账户</button>'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. 网站前端配置
|
||||||
|
|
||||||
|
#### 6.1 检测插件登录请求
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 在登录页面检测 redirect_uri 参数
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
const redirectUri = urlParams.get("redirect_uri");
|
||||||
|
|
||||||
|
if (redirectUri) {
|
||||||
|
// 保存回调地址
|
||||||
|
localStorage.setItem("plugin_redirect_uri", redirectUri);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 6.2 登录成功后重定向
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 用户登录成功,拿到 token
|
||||||
|
const token = response.data.token;
|
||||||
|
|
||||||
|
// 检查是否需要重定向回插件
|
||||||
|
const redirectUri = localStorage.getItem("plugin_redirect_uri");
|
||||||
|
|
||||||
|
if (redirectUri) {
|
||||||
|
// 重定向回插件,带上 token
|
||||||
|
window.location.href = `${redirectUri}?token=${token}`;
|
||||||
|
localStorage.removeItem("plugin_redirect_uri");
|
||||||
|
} else {
|
||||||
|
// 正常登录流程
|
||||||
|
router.push("/dashboard");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 关键技术点
|
||||||
|
|
||||||
|
### 1. 动态端口分配
|
||||||
|
|
||||||
|
**问题:** 固定端口可能被占用,导致登录失败
|
||||||
|
|
||||||
|
**解决方案:** 使用端口 `0` 让系统自动分配可用端口
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
server.listen(0, () => {
|
||||||
|
const address = server.address();
|
||||||
|
const port = typeof address === "object" && address ? address.port : 3000;
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Promise 异步等待
|
||||||
|
|
||||||
|
**问题:** 需要等待浏览器登录完成后才能继续
|
||||||
|
|
||||||
|
**解决方案:** 使用 Promise 包装回调逻辑
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
(server as any)._loginResolve = resolve;
|
||||||
|
(server as any)._loginReject = reject;
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 会话持久化
|
||||||
|
|
||||||
|
**问题:** 重启 VSCode 后需要重新登录
|
||||||
|
|
||||||
|
**解决方案:** 使用 `globalState` 保存会话
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await this.context.globalState.update("icCoderSessions", this._sessions);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 事件通知机制
|
||||||
|
|
||||||
|
**问题:** VSCode 需要知道会话状态变化
|
||||||
|
|
||||||
|
**解决方案:** 使用 `EventEmitter` 触发事件
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [session],
|
||||||
|
removed: [],
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## 用户体验
|
||||||
|
|
||||||
|
### 登录流程
|
||||||
|
|
||||||
|
1. 用户点击侧边栏"登录账户"按钮
|
||||||
|
2. 浏览器自动打开登录页面
|
||||||
|
3. 用户在网站完成登录
|
||||||
|
4. 浏览器自动跳转到成功页面
|
||||||
|
5. VSCode 左下角显示"IC Coder 用户"
|
||||||
|
6. 侧边栏按钮变为"开始创作"
|
||||||
|
|
||||||
|
### 登出流程
|
||||||
|
|
||||||
|
1. 点击 VSCode 左下角账户图标
|
||||||
|
2. 选择"IC Coder"账户
|
||||||
|
3. 点击"退出"按钮
|
||||||
|
4. 或使用命令 `IC Coder: 退出登录`
|
||||||
|
|
||||||
|
## 常见问题
|
||||||
|
|
||||||
|
### Q1: 为什么不直接使用 globalState 存储 token?
|
||||||
|
|
||||||
|
**A:** 使用 VSCode Authentication API 的优势:
|
||||||
|
|
||||||
|
- ✅ 统一的用户体验(左下角账户区域)
|
||||||
|
- ✅ VSCode 自动管理会话生命周期
|
||||||
|
- ✅ 支持多账户切换
|
||||||
|
- ✅ 更好的安全性(VSCode 负责加密存储)
|
||||||
|
|
||||||
|
### Q2: 如何处理 token 过期?
|
||||||
|
|
||||||
|
**A:** 可以在 API 请求失败时:
|
||||||
|
|
||||||
|
1. 检测 401 错误
|
||||||
|
2. 调用 `removeSession()` 清除过期会话
|
||||||
|
3. 提示用户重新登录
|
||||||
|
|
||||||
|
### Q3: 如何支持多个账户?
|
||||||
|
|
||||||
|
**A:** 修改 `account` 对象:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
account: {
|
||||||
|
id: userInfo.id,
|
||||||
|
label: userInfo.username,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Q4: 登录页面如何获取用户信息?
|
||||||
|
|
||||||
|
**A:** 可以在登录成功后,通过 API 获取用户信息:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const userInfo = await fetch("https://api.iccoder.com/user/info", {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
});
|
||||||
|
|
||||||
|
const session: vscode.AuthenticationSession = {
|
||||||
|
account: {
|
||||||
|
id: userInfo.id,
|
||||||
|
label: userInfo.username,
|
||||||
|
},
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## 安全考虑
|
||||||
|
|
||||||
|
### 1. Token 存储
|
||||||
|
|
||||||
|
- ✅ 使用 VSCode `globalState` 加密存储
|
||||||
|
- ✅ 不在代码中硬编码敏感信息
|
||||||
|
- ✅ Token 仅在内存和加密存储中传递
|
||||||
|
|
||||||
|
### 2. 本地服务器
|
||||||
|
|
||||||
|
- ✅ 仅监听 `localhost`,不暴露到外网
|
||||||
|
- ✅ 使用动态端口,避免固定端口被劫持
|
||||||
|
- ✅ 接收到 token 后立即关闭服务器
|
||||||
|
- ✅ 设置 5 分钟超时,防止服务器长期运行
|
||||||
|
|
||||||
|
### 3. HTTPS 考虑
|
||||||
|
|
||||||
|
**当前实现:** 使用 HTTP 本地回调
|
||||||
|
|
||||||
|
**生产环境建议:**
|
||||||
|
|
||||||
|
- 网站使用 HTTPS
|
||||||
|
- 本地回调使用 HTTP(localhost 不受浏览器限制)
|
||||||
|
- 或使用 `vscode://` 协议(需要网站支持)
|
||||||
|
|
||||||
|
## 测试指南
|
||||||
|
|
||||||
|
### 1. 本地测试
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 启动调试模式
|
||||||
|
按 F5
|
||||||
|
|
||||||
|
# 测试登录
|
||||||
|
1. 打开侧边栏
|
||||||
|
2. 点击"登录账户"
|
||||||
|
3. 在浏览器完成登录
|
||||||
|
4. 检查左下角是否显示账户
|
||||||
|
|
||||||
|
# 测试登出
|
||||||
|
1. 点击左下角账户
|
||||||
|
2. 选择"IC Coder"
|
||||||
|
3. 点击"退出"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 调试技巧
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 在 ICCoderAuthenticationProvider 中添加日志
|
||||||
|
console.log("🔐 创建会话:", session);
|
||||||
|
console.log("🔑 Token:", token);
|
||||||
|
|
||||||
|
// 在 ICViewProvider 中添加日志
|
||||||
|
console.log("🔍 登录状态:", isLoggedIn);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 常见错误排查
|
||||||
|
|
||||||
|
| 错误 | 原因 | 解决方案 |
|
||||||
|
| ------------------------------- | --------------- | ---------------------------------- |
|
||||||
|
| `getSessions is not a function` | VSCode 版本过低 | 升级到 1.63.0+ |
|
||||||
|
| 端口被占用 | 固定端口冲突 | 使用动态端口(已实现) |
|
||||||
|
| 登录后未显示账户 | 未触发事件 | 检查 `_onDidChangeSessions.fire()` |
|
||||||
|
| 重启后需要重新登录 | 未保存会话 | 检查 `saveSessions()` 调用 |
|
||||||
|
|
||||||
|
## 文件结构
|
||||||
|
|
||||||
|
```
|
||||||
|
ic-coder/
|
||||||
|
├── src/
|
||||||
|
│ ├── services/
|
||||||
|
│ │ └── icCoderAuthProvider.ts # Authentication Provider 实现
|
||||||
|
│ ├── views/
|
||||||
|
│ │ └── ICViewProvider.ts # 侧边栏视图(集成登录状态)
|
||||||
|
│ └── extension.ts # 注册 Provider 和命令
|
||||||
|
├── package.json # 配置 authentication 和 commands
|
||||||
|
└── docs/
|
||||||
|
└── authentication-implementation.md # 本文档
|
||||||
|
```
|
||||||
|
|
||||||
|
## 参考资料
|
||||||
|
|
||||||
|
- [VSCode Authentication API](https://code.visualstudio.com/api/references/vscode-api#authentication)
|
||||||
|
- [Authentication Provider Sample](https://github.com/microsoft/vscode-extension-samples/tree/main/authentication-sample)
|
||||||
|
- [VSCode Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
|
||||||
|
|
||||||
|
## 总结
|
||||||
|
|
||||||
|
本实现通过以下步骤完成了 VSCode Authentication API 的集成:
|
||||||
|
|
||||||
|
1. ✅ 创建 `ICCoderAuthenticationProvider` 类实现认证逻辑
|
||||||
|
2. ✅ 在 `package.json` 中注册 authentication provider
|
||||||
|
3. ✅ 在 `extension.ts` 中注册 provider 和命令
|
||||||
|
4. ✅ 实现本地 HTTP 服务器处理登录回调
|
||||||
|
5. ✅ 使用动态端口避免冲突
|
||||||
|
6. ✅ 集成到侧边栏视图,根据登录状态显示不同按钮
|
||||||
|
7. ✅ 配置网站前端支持插件登录重定向
|
||||||
|
|
||||||
|
**最终效果:**
|
||||||
|
|
||||||
|
- 用户登录后,VSCode 左下角显示"IC Coder 用户"
|
||||||
|
- 侧边栏根据登录状态显示"登录账户"或"开始创作"按钮
|
||||||
|
- 支持通过账户菜单或命令进行登录/登出操作
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**文档版本:** 1.0
|
||||||
|
**最后更新:** 2025-12-29
|
||||||
|
**作者:** Roe-xin
|
||||||
739
docs/invitation-code-design.md
Normal file
@ -0,0 +1,739 @@
|
|||||||
|
# 邀请码验证功能设计方案
|
||||||
|
|
||||||
|
## 一、整体流程
|
||||||
|
|
||||||
|
```
|
||||||
|
用户首次使用 → 检查邀请码状态 → 未验证则弹窗输入 → 后端验证 → 验证通过后可正常对话
|
||||||
|
```
|
||||||
|
|
||||||
|
## 二、前端设计
|
||||||
|
|
||||||
|
### 2.1 邀请码状态管理
|
||||||
|
|
||||||
|
在 `ExtensionContext.globalState` 中存储邀请码验证状态:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 存储结构
|
||||||
|
{
|
||||||
|
"invitationCodeVerified": true,
|
||||||
|
"invitationCode": "INVITE2024ABC",
|
||||||
|
"verifiedTime": "2024-01-20T10:30:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 UI 交互流程
|
||||||
|
|
||||||
|
#### 弹窗输入邀请码
|
||||||
|
使用 `vscode.window.showInputBox` 实现:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const invitationCode = await vscode.window.showInputBox({
|
||||||
|
prompt: '请输入邀请码以继续使用 IC Coder',
|
||||||
|
placeHolder: '例如:INVITE2024ABC',
|
||||||
|
ignoreFocusOut: true,
|
||||||
|
validateInput: (value) => {
|
||||||
|
if (!value || value.trim().length === 0) {
|
||||||
|
return '邀请码不能为空';
|
||||||
|
}
|
||||||
|
if (value.length < 6) {
|
||||||
|
return '邀请码格式不正确';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 验证结果提示
|
||||||
|
- 成功:`vscode.window.showInformationMessage('邀请码验证成功!')`
|
||||||
|
- 失败:`vscode.window.showErrorMessage('邀请码无效或已过期,请重新输入')`
|
||||||
|
|
||||||
|
### 2.3 验证时机
|
||||||
|
|
||||||
|
在以下场景触发邀请码验证:
|
||||||
|
|
||||||
|
1. **用户首次发送消息时**(在 `handleUserMessage` 中检查)
|
||||||
|
2. **用户登录后**(在登录成功回调中检查)
|
||||||
|
3. **Token 过期重新登录后**
|
||||||
|
|
||||||
|
### 2.4 前端验证流程图
|
||||||
|
|
||||||
|
```
|
||||||
|
发送消息前检查
|
||||||
|
├─ 检查是否已登录
|
||||||
|
│ └─ 未登录 → 提示登录
|
||||||
|
├─ 检查邀请码是否已验证
|
||||||
|
│ ├─ 未验证
|
||||||
|
│ │ ├─ 弹窗输入邀请码
|
||||||
|
│ │ ├─ 调用后端验证接口 POST /api/invitation/verify
|
||||||
|
│ │ ├─ 验证成功
|
||||||
|
│ │ │ ├─ 保存验证状态到 globalState
|
||||||
|
│ │ │ └─ 继续发送消息
|
||||||
|
│ │ └─ 验证失败
|
||||||
|
│ │ ├─ 显示错误提示
|
||||||
|
│ │ └─ 阻止发送消息
|
||||||
|
│ └─ 已验证 → 继续发送消息
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.5 前端文件修改清单
|
||||||
|
|
||||||
|
#### 新增文件
|
||||||
|
|
||||||
|
**`src/services/invitationService.ts`** - 邀请码服务
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 邀请码验证服务
|
||||||
|
*/
|
||||||
|
export class InvitationService {
|
||||||
|
/**
|
||||||
|
* 检查用户是否已验证邀请码
|
||||||
|
*/
|
||||||
|
static async isVerified(context: vscode.ExtensionContext): Promise<boolean>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证邀请码
|
||||||
|
*/
|
||||||
|
static async verifyCode(code: string): Promise<boolean>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存验证状态
|
||||||
|
*/
|
||||||
|
static async saveVerificationStatus(
|
||||||
|
context: vscode.ExtensionContext,
|
||||||
|
code: string
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除验证状态(用于退出登录)
|
||||||
|
*/
|
||||||
|
static async clearVerificationStatus(
|
||||||
|
context: vscode.ExtensionContext
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示邀请码输入弹窗
|
||||||
|
*/
|
||||||
|
static async showInputDialog(): Promise<string | undefined>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 修改文件
|
||||||
|
|
||||||
|
**`src/utils/messageHandler.ts`**
|
||||||
|
- 在 `handleUserMessage` 函数开头添加邀请码验证检查
|
||||||
|
|
||||||
|
**`src/services/apiClient.ts`**
|
||||||
|
- 添加 `verifyInvitationCode` 函数
|
||||||
|
- 添加 `checkInvitationStatus` 函数
|
||||||
|
|
||||||
|
**`src/types/api.ts`**
|
||||||
|
- 添加邀请码相关类型定义
|
||||||
|
|
||||||
|
**`src/panels/ICHelperPanel.ts`**
|
||||||
|
- 在面板创建时检查邀请码状态(可选)
|
||||||
|
|
||||||
|
**`src/extension.ts`**
|
||||||
|
- 在登录成功后检查邀请码状态
|
||||||
|
|
||||||
|
### 2.6 类型定义
|
||||||
|
|
||||||
|
在 `src/types/api.ts` 中添加:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ============== 邀请码验证 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码验证请求
|
||||||
|
* POST /api/invitation/verify
|
||||||
|
*/
|
||||||
|
export interface InvitationVerifyRequest {
|
||||||
|
/** 邀请码 */
|
||||||
|
code: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码验证响应
|
||||||
|
*/
|
||||||
|
export interface InvitationVerifyResponse {
|
||||||
|
/** 响应代码 */
|
||||||
|
code: number;
|
||||||
|
/** 响应消息 */
|
||||||
|
msg: string;
|
||||||
|
/** 验证结果数据 */
|
||||||
|
data?: {
|
||||||
|
/** 是否验证成功 */
|
||||||
|
verified: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码状态响应
|
||||||
|
* GET /api/invitation/status
|
||||||
|
*/
|
||||||
|
export interface InvitationStatusResponse {
|
||||||
|
/** 响应代码 */
|
||||||
|
code: number;
|
||||||
|
/** 响应消息 */
|
||||||
|
msg?: string;
|
||||||
|
/** 状态数据 */
|
||||||
|
data?: {
|
||||||
|
/** 是否已验证 */
|
||||||
|
verified: boolean;
|
||||||
|
/** 使用的邀请码 */
|
||||||
|
invitationCode?: string;
|
||||||
|
/** 验证时间 */
|
||||||
|
verifiedTime?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 三、后端设计
|
||||||
|
|
||||||
|
### 3.1 数据库设计
|
||||||
|
|
||||||
|
#### 邀请码表 (invitation_codes)
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE invitation_codes (
|
||||||
|
id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
|
||||||
|
code VARCHAR(32) UNIQUE NOT NULL COMMENT '邀请码',
|
||||||
|
max_uses INT DEFAULT 1 COMMENT '最大使用次数,-1表示无限制',
|
||||||
|
used_count INT DEFAULT 0 COMMENT '已使用次数',
|
||||||
|
expire_time DATETIME COMMENT '过期时间,NULL表示永不过期',
|
||||||
|
created_by BIGINT COMMENT '创建者用户ID',
|
||||||
|
created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||||
|
updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||||
|
status TINYINT DEFAULT 1 COMMENT '状态:1-有效,0-禁用',
|
||||||
|
remark VARCHAR(500) COMMENT '备注',
|
||||||
|
INDEX idx_code (code),
|
||||||
|
INDEX idx_status (status),
|
||||||
|
INDEX idx_expire_time (expire_time)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='邀请码表';
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 用户邀请码关联表 (user_invitation)
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE user_invitation (
|
||||||
|
id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
|
||||||
|
user_id BIGINT NOT NULL COMMENT '用户ID',
|
||||||
|
invitation_code VARCHAR(32) NOT NULL COMMENT '使用的邀请码',
|
||||||
|
verified_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '验证时间',
|
||||||
|
ip_address VARCHAR(50) COMMENT '验证时的IP地址',
|
||||||
|
UNIQUE KEY uk_user_id (user_id),
|
||||||
|
INDEX idx_invitation_code (invitation_code),
|
||||||
|
INDEX idx_verified_time (verified_time)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户邀请码关联表';
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 API 接口设计
|
||||||
|
|
||||||
|
#### 3.2.1 验证邀请码
|
||||||
|
|
||||||
|
**接口地址**:`POST /api/invitation/verify`
|
||||||
|
|
||||||
|
**请求头**:
|
||||||
|
```
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
Content-Type: application/json
|
||||||
|
```
|
||||||
|
|
||||||
|
**请求体**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": "INVITE2024ABC"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应示例**:
|
||||||
|
|
||||||
|
成功:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 200,
|
||||||
|
"msg": "验证成功",
|
||||||
|
"data": {
|
||||||
|
"verified": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
失败:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 400,
|
||||||
|
"msg": "邀请码无效或已过期"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 400,
|
||||||
|
"msg": "邀请码使用次数已达上限"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 400,
|
||||||
|
"msg": "您已验证过邀请码,无需重复验证"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3.2.2 查询验证状态
|
||||||
|
|
||||||
|
**接口地址**:`GET /api/invitation/status`
|
||||||
|
|
||||||
|
**请求头**:
|
||||||
|
```
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应示例**:
|
||||||
|
|
||||||
|
已验证:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 200,
|
||||||
|
"msg": "success",
|
||||||
|
"data": {
|
||||||
|
"verified": true,
|
||||||
|
"invitationCode": "INVITE2024ABC",
|
||||||
|
"verifiedTime": "2024-01-20T10:30:00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
未验证:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 200,
|
||||||
|
"msg": "success",
|
||||||
|
"data": {
|
||||||
|
"verified": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3.2.3 管理接口(管理员使用)
|
||||||
|
|
||||||
|
**生成邀请码**:`POST /api/admin/invitation/generate`
|
||||||
|
|
||||||
|
请求体:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"count": 10,
|
||||||
|
"maxUses": 1,
|
||||||
|
"expireTime": "2024-12-31T23:59:59",
|
||||||
|
"remark": "2024年1月批次"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
响应:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 200,
|
||||||
|
"msg": "生成成功",
|
||||||
|
"data": {
|
||||||
|
"codes": [
|
||||||
|
"INVITE2024001",
|
||||||
|
"INVITE2024002",
|
||||||
|
"..."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**查询邀请码列表**:`GET /api/admin/invitation/list`
|
||||||
|
|
||||||
|
**禁用邀请码**:`PUT /api/admin/invitation/disable/{code}`
|
||||||
|
|
||||||
|
**查询使用记录**:`GET /api/admin/invitation/usage/{code}`
|
||||||
|
|
||||||
|
### 3.3 后端验证逻辑
|
||||||
|
|
||||||
|
#### 验证流程
|
||||||
|
|
||||||
|
```java
|
||||||
|
public boolean verifyInvitationCode(Long userId, String code) {
|
||||||
|
// 1. 检查邀请码是否存在
|
||||||
|
InvitationCode invitationCode = invitationCodeMapper.selectByCode(code);
|
||||||
|
if (invitationCode == null) {
|
||||||
|
throw new BusinessException("邀请码不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 检查邀请码状态
|
||||||
|
if (invitationCode.getStatus() != 1) {
|
||||||
|
throw new BusinessException("邀请码已被禁用");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 检查是否过期
|
||||||
|
if (invitationCode.getExpireTime() != null
|
||||||
|
&& invitationCode.getExpireTime().before(new Date())) {
|
||||||
|
throw new BusinessException("邀请码已过期");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 检查使用次数
|
||||||
|
if (invitationCode.getMaxUses() != -1
|
||||||
|
&& invitationCode.getUsedCount() >= invitationCode.getMaxUses()) {
|
||||||
|
throw new BusinessException("邀请码使用次数已达上限");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 检查用户是否已验证过
|
||||||
|
UserInvitation existing = userInvitationMapper.selectByUserId(userId);
|
||||||
|
if (existing != null) {
|
||||||
|
throw new BusinessException("您已验证过邀请码,无需重复验证");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. 创建用户验证记录
|
||||||
|
UserInvitation userInvitation = new UserInvitation();
|
||||||
|
userInvitation.setUserId(userId);
|
||||||
|
userInvitation.setInvitationCode(code);
|
||||||
|
userInvitation.setVerifiedTime(new Date());
|
||||||
|
userInvitationMapper.insert(userInvitation);
|
||||||
|
|
||||||
|
// 7. 增加邀请码使用次数
|
||||||
|
invitationCodeMapper.incrementUsedCount(code);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.4 权限拦截
|
||||||
|
|
||||||
|
在对话接口中添加邀请码验证拦截:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@PostMapping("/dialog/stream")
|
||||||
|
public SseEmitter dialog(@RequestBody DialogRequest request) {
|
||||||
|
// 获取当前用户ID
|
||||||
|
Long userId = SecurityUtils.getUserId();
|
||||||
|
|
||||||
|
// 检查用户是否已验证邀请码
|
||||||
|
if (!invitationService.isUserVerified(userId)) {
|
||||||
|
throw new BusinessException("请先验证邀请码后再使用对话功能");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 继续处理对话请求
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
或者使用拦截器统一处理:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Component
|
||||||
|
public class InvitationInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private InvitationService invitationService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
Object handler) throws Exception {
|
||||||
|
// 获取当前用户ID
|
||||||
|
Long userId = SecurityUtils.getUserId();
|
||||||
|
|
||||||
|
// 检查是否需要验证邀请码的接口
|
||||||
|
String uri = request.getRequestURI();
|
||||||
|
if (needsInvitationVerification(uri)) {
|
||||||
|
if (!invitationService.isUserVerified(userId)) {
|
||||||
|
throw new BusinessException("请先验证邀请码");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean needsInvitationVerification(String uri) {
|
||||||
|
// 需要验证邀请码的接口列表
|
||||||
|
return uri.startsWith("/api/dialog/")
|
||||||
|
|| uri.startsWith("/api/task/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.5 后端文件清单
|
||||||
|
|
||||||
|
#### 实体类
|
||||||
|
- `com.iccoder.entity.InvitationCode` - 邀请码实体
|
||||||
|
- `com.iccoder.entity.UserInvitation` - 用户邀请码关联实体
|
||||||
|
|
||||||
|
#### Mapper
|
||||||
|
- `com.iccoder.mapper.InvitationCodeMapper` - 邀请码数据访问
|
||||||
|
- `com.iccoder.mapper.UserInvitationMapper` - 用户邀请码关联数据访问
|
||||||
|
|
||||||
|
#### Service
|
||||||
|
- `com.iccoder.service.InvitationService` - 邀请码业务逻辑
|
||||||
|
- `com.iccoder.service.impl.InvitationServiceImpl` - 实现类
|
||||||
|
|
||||||
|
#### Controller
|
||||||
|
- `com.iccoder.controller.InvitationController` - 邀请码接口
|
||||||
|
- `com.iccoder.controller.admin.InvitationAdminController` - 管理接口
|
||||||
|
|
||||||
|
#### 拦截器
|
||||||
|
- `com.iccoder.interceptor.InvitationInterceptor` - 邀请码验证拦截器
|
||||||
|
|
||||||
|
## 四、用户体验优化
|
||||||
|
|
||||||
|
### 4.1 首次使用引导
|
||||||
|
|
||||||
|
在用户首次打开聊天面板时,如果未验证邀请码,显示友好的引导信息:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 在 ICHelperPanel.ts 中
|
||||||
|
if (!await InvitationService.isVerified(context)) {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: 'showInvitationGuide',
|
||||||
|
message: '欢迎使用 IC Coder!请先输入邀请码以开始使用。'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 状态持久化
|
||||||
|
|
||||||
|
验证状态保存在 `globalState` 中,避免重复验证:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 保存验证状态
|
||||||
|
await context.globalState.update('invitationCodeVerified', true);
|
||||||
|
await context.globalState.update('invitationCode', code);
|
||||||
|
await context.globalState.update('verifiedTime', new Date().toISOString());
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.3 错误提示优化
|
||||||
|
|
||||||
|
根据不同的错误类型,提供清晰的提示信息:
|
||||||
|
|
||||||
|
| 错误类型 | 提示信息 |
|
||||||
|
|---------|---------|
|
||||||
|
| 邀请码不存在 | "邀请码不存在,请检查后重新输入" |
|
||||||
|
| 邀请码已过期 | "邀请码已过期,请联系管理员获取新的邀请码" |
|
||||||
|
| 使用次数已达上限 | "该邀请码使用次数已达上限,请使用其他邀请码" |
|
||||||
|
| 已验证过 | "您已验证过邀请码,无需重复验证" |
|
||||||
|
| 网络错误 | "网络连接失败,请检查网络后重试" |
|
||||||
|
|
||||||
|
### 4.4 支持重新验证
|
||||||
|
|
||||||
|
提供命令允许用户更换邀请码:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 在 extension.ts 中注册命令
|
||||||
|
context.subscriptions.push(
|
||||||
|
vscode.commands.registerCommand('ic-coder.changeInvitationCode', async () => {
|
||||||
|
const confirm = await vscode.window.showWarningMessage(
|
||||||
|
'确定要更换邀请码吗?',
|
||||||
|
'确定',
|
||||||
|
'取消'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (confirm === '确定') {
|
||||||
|
await InvitationService.clearVerificationStatus(context);
|
||||||
|
vscode.window.showInformationMessage('已清除邀请码,请重新验证');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.5 Webview 中显示状态(可选)
|
||||||
|
|
||||||
|
在聊天界面顶部显示邀请码验证状态:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!-- 已验证 -->
|
||||||
|
<div class="invitation-status verified">
|
||||||
|
<span class="icon">✓</span>
|
||||||
|
<span>邀请码已验证</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 未验证 -->
|
||||||
|
<div class="invitation-status unverified">
|
||||||
|
<span class="icon">!</span>
|
||||||
|
<span>请先验证邀请码</span>
|
||||||
|
<button onclick="verifyInvitationCode()">立即验证</button>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 五、安全考虑
|
||||||
|
|
||||||
|
### 5.1 邀请码生成规则
|
||||||
|
|
||||||
|
使用安全的随机算法生成邀请码:
|
||||||
|
|
||||||
|
```java
|
||||||
|
public String generateInvitationCode() {
|
||||||
|
// 使用 UUID + 时间戳 + 随机数
|
||||||
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
||||||
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
|
String random = RandomStringUtils.randomAlphanumeric(6);
|
||||||
|
|
||||||
|
// 组合并取前16位
|
||||||
|
String combined = uuid + timestamp + random;
|
||||||
|
String code = DigestUtils.sha256Hex(combined).substring(0, 16).toUpperCase();
|
||||||
|
|
||||||
|
return "IC" + code; // 添加前缀,例如:IC3F2A9B1C4D5E6F
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 防暴力破解
|
||||||
|
|
||||||
|
限制验证频率,添加验证失败次数限制:
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 使用 Redis 记录验证失败次数
|
||||||
|
String key = "invitation:fail:" + userId;
|
||||||
|
Integer failCount = redisTemplate.opsForValue().get(key);
|
||||||
|
|
||||||
|
if (failCount != null && failCount >= 5) {
|
||||||
|
throw new BusinessException("验证失败次数过多,请1小时后再试");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证失败时增加计数
|
||||||
|
if (!verifySuccess) {
|
||||||
|
redisTemplate.opsForValue().increment(key);
|
||||||
|
redisTemplate.expire(key, 1, TimeUnit.HOURS);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.3 Token 绑定
|
||||||
|
|
||||||
|
邀请码验证状态与用户 Token 绑定,退出登录时清除:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 在退出登录时清除验证状态
|
||||||
|
vscode.commands.registerCommand('ic-coder.logout', async () => {
|
||||||
|
// 清除 session
|
||||||
|
await clearSession();
|
||||||
|
|
||||||
|
// 清除邀请码验证状态
|
||||||
|
await InvitationService.clearVerificationStatus(context);
|
||||||
|
|
||||||
|
vscode.window.showInformationMessage('已退出登录');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.4 日志记录
|
||||||
|
|
||||||
|
记录所有验证尝试,便于审计和分析:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Slf4j
|
||||||
|
public class InvitationServiceImpl implements InvitationService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean verifyInvitationCode(Long userId, String code) {
|
||||||
|
log.info("用户 {} 尝试验证邀请码: {}", userId, code);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 验证逻辑
|
||||||
|
// ...
|
||||||
|
|
||||||
|
log.info("用户 {} 验证邀请码成功: {}", userId, code);
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("用户 {} 验证邀请码失败: {}, 原因: {}",
|
||||||
|
userId, code, e.getMessage());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.5 敏感信息保护
|
||||||
|
|
||||||
|
- 邀请码在数据库中可以考虑加密存储(可选)
|
||||||
|
- API 响应中不暴露邀请码的详细信息(如剩余次数)
|
||||||
|
- 前端不缓存邀请码明文,只保存验证状态
|
||||||
|
|
||||||
|
## 六、实施步骤
|
||||||
|
|
||||||
|
### 阶段一:后端开发(优先)
|
||||||
|
|
||||||
|
1. 创建数据库表
|
||||||
|
2. 实现邀请码生成和管理功能
|
||||||
|
3. 实现验证接口
|
||||||
|
4. 添加权限拦截
|
||||||
|
5. 测试接口功能
|
||||||
|
|
||||||
|
### 阶段二:前端开发
|
||||||
|
|
||||||
|
1. 添加类型定义
|
||||||
|
2. 实现 `InvitationService`
|
||||||
|
3. 修改 `apiClient.ts` 添加接口调用
|
||||||
|
4. 修改 `messageHandler.ts` 添加验证检查
|
||||||
|
5. 测试完整流程
|
||||||
|
|
||||||
|
### 阶段三:联调测试
|
||||||
|
|
||||||
|
1. 前后端联调
|
||||||
|
2. 测试各种异常场景
|
||||||
|
3. 优化用户体验
|
||||||
|
4. 性能测试
|
||||||
|
|
||||||
|
### 阶段四:上线部署
|
||||||
|
|
||||||
|
1. 生成初始邀请码
|
||||||
|
2. 更新用户文档
|
||||||
|
3. 灰度发布
|
||||||
|
4. 监控运行状态
|
||||||
|
|
||||||
|
## 七、测试用例
|
||||||
|
|
||||||
|
### 7.1 正常流程测试
|
||||||
|
|
||||||
|
| 测试场景 | 预期结果 |
|
||||||
|
|---------|---------|
|
||||||
|
| 首次使用,输入有效邀请码 | 验证成功,可以正常对话 |
|
||||||
|
| 已验证用户再次打开面板 | 无需重复验证,直接使用 |
|
||||||
|
| 退出登录后重新登录 | 需要重新验证邀请码 |
|
||||||
|
|
||||||
|
### 7.2 异常场景测试
|
||||||
|
|
||||||
|
| 测试场景 | 预期结果 |
|
||||||
|
|---------|---------|
|
||||||
|
| 输入不存在的邀请码 | 提示"邀请码不存在" |
|
||||||
|
| 输入已过期的邀请码 | 提示"邀请码已过期" |
|
||||||
|
| 输入使用次数已满的邀请码 | 提示"使用次数已达上限" |
|
||||||
|
| 已验证用户尝试再次验证 | 提示"已验证过,无需重复验证" |
|
||||||
|
| 网络断开时验证 | 提示"网络连接失败" |
|
||||||
|
| 连续输入错误邀请码5次 | 提示"验证失败次数过多,请稍后再试" |
|
||||||
|
|
||||||
|
### 7.3 边界条件测试
|
||||||
|
|
||||||
|
| 测试场景 | 预期结果 |
|
||||||
|
|---------|---------|
|
||||||
|
| 邀请码为空 | 前端验证拦截,提示"邀请码不能为空" |
|
||||||
|
| 邀请码长度不足 | 前端验证拦截,提示"邀请码格式不正确" |
|
||||||
|
| 邀请码包含特殊字符 | 后端验证失败,提示"邀请码不存在" |
|
||||||
|
| 同一邀请码多人同时使用 | 使用数据库锁,确保不超过最大次数 |
|
||||||
|
|
||||||
|
## 八、FAQ
|
||||||
|
|
||||||
|
### Q1: 用户忘记邀请码怎么办?
|
||||||
|
A: 邀请码验证成功后,用户无需记住邀请码。如果需要查看,可以在设置中显示已验证的邀请码。
|
||||||
|
|
||||||
|
### Q2: 邀请码可以重复使用吗?
|
||||||
|
A: 取决于邀请码的 `maxUses` 设置。可以设置为 1(一次性)、N(限定次数)或 -1(无限制)。
|
||||||
|
|
||||||
|
### Q3: 如何批量生成邀请码?
|
||||||
|
A: 使用管理接口 `POST /api/admin/invitation/generate`,指定生成数量即可。
|
||||||
|
|
||||||
|
### Q4: 邀请码验证失败会影响登录吗?
|
||||||
|
A: 不会。邀请码验证是独立的,只影响对话功能的使用,不影响登录。
|
||||||
|
|
||||||
|
### Q5: 可以为不同用户群体设置不同的邀请码吗?
|
||||||
|
A: 可以。通过 `remark` 字段标记不同批次的邀请码,便于管理和统计。
|
||||||
|
|
||||||
|
## 九、后续优化方向
|
||||||
|
|
||||||
|
1. **邀请码分级**:不同等级的邀请码对应不同的权限(如对话次数、模型选择等)
|
||||||
|
2. **邀请奖励**:邀请他人使用可获得积分或额外权限
|
||||||
|
3. **邀请统计**:统计每个邀请码的使用情况和用户活跃度
|
||||||
|
4. **自动过期**:根据使用情况自动延长或缩短邀请码有效期
|
||||||
|
5. **白名单机制**:特定用户可以免邀请码使用
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**文档版本**:v1.0
|
||||||
|
**最后更新**:2026-01-27
|
||||||
|
**维护者**:IC Coder Team
|
||||||
911
docs/system-notification-implementation.md
Normal file
@ -0,0 +1,911 @@
|
|||||||
|
# IC Coder 系统通知功能实现方案
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [1. 需求背景](#1-需求背景)
|
||||||
|
- [2. 技术方案对比](#2-技术方案对比)
|
||||||
|
- [3. 推荐方案详解](#3-推荐方案详解)
|
||||||
|
- [4. 实现步骤](#4-实现步骤)
|
||||||
|
- [5. API 设计](#5-api-设计)
|
||||||
|
- [6. 配置选项](#6-配置选项)
|
||||||
|
- [7. 测试方案](#7-测试方案)
|
||||||
|
- [8. 注意事项](#8-注意事项)
|
||||||
|
- [9. 常见问题](#9-常见问题)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 需求背景
|
||||||
|
|
||||||
|
### 1.1 问题描述
|
||||||
|
|
||||||
|
当前 IC Coder 插件使用 VS Code 内置的通知 API (`vscode.window.showInformationMessage`) 来提示用户任务完成。这种方式存在以下问题:
|
||||||
|
|
||||||
|
- **可见性问题**: 用户切换到其他应用时,无法看到 VS Code 内部的通知
|
||||||
|
- **错过通知**: 长时间运行的任务(如 iverilog 仿真)完成时,用户可能已经离开 VS Code
|
||||||
|
- **用户体验**: 需要用户主动回到 VS Code 才能知道任务状态
|
||||||
|
|
||||||
|
### 1.2 目标
|
||||||
|
|
||||||
|
实现系统级通知功能,使得:
|
||||||
|
|
||||||
|
1. 用户在任何应用中都能收到任务完成通知
|
||||||
|
2. 通知显示在操作系统的通知中心(Windows Action Center / macOS Notification Center / Linux notify-send)
|
||||||
|
3. 支持自定义通知内容、图标、声音
|
||||||
|
4. 用户可以配置是否启用系统通知
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 技术方案对比
|
||||||
|
|
||||||
|
### 2.1 方案一:node-notifier(推荐)
|
||||||
|
|
||||||
|
**描述**: 使用 `node-notifier` 库,封装了各平台的原生通知 API
|
||||||
|
|
||||||
|
**优点**:
|
||||||
|
- ✅ 跨平台支持(Windows/macOS/Linux)
|
||||||
|
- ✅ API 简单易用
|
||||||
|
- ✅ 支持自定义图标、声音、操作按钮
|
||||||
|
- ✅ 活跃维护,社区支持良好
|
||||||
|
- ✅ 支持通知点击回调
|
||||||
|
|
||||||
|
**缺点**:
|
||||||
|
- ❌ 需要添加额外依赖(~500KB)
|
||||||
|
- ❌ 首次使用需要用户授权
|
||||||
|
|
||||||
|
**适用场景**: 需要跨平台支持的生产环境
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.2 方案二:Windows PowerShell Toast 通知
|
||||||
|
|
||||||
|
**描述**: 使用 PowerShell 脚本调用 Windows 10/11 的 Toast 通知 API
|
||||||
|
|
||||||
|
**优点**:
|
||||||
|
- ✅ 无需额外依赖
|
||||||
|
- ✅ 支持丰富的 Toast 样式(按钮、输入框等)
|
||||||
|
- ✅ 与 Windows 系统深度集成
|
||||||
|
|
||||||
|
**缺点**:
|
||||||
|
- ❌ 仅支持 Windows 10/11
|
||||||
|
- ❌ 需要执行 PowerShell 脚本,可能有安全限制
|
||||||
|
- ❌ 实现复杂度较高
|
||||||
|
|
||||||
|
**适用场景**: 仅针对 Windows 平台的专用功能
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.3 方案三:Electron Notification API
|
||||||
|
|
||||||
|
**描述**: 使用 Electron 的 `Notification` API(VS Code 基于 Electron)
|
||||||
|
|
||||||
|
**优点**:
|
||||||
|
- ✅ 无需额外依赖
|
||||||
|
- ✅ 跨平台支持
|
||||||
|
- ✅ API 简洁
|
||||||
|
|
||||||
|
**缺点**:
|
||||||
|
- ❌ VS Code 扩展 API 未直接暴露 Electron API
|
||||||
|
- ❌ 需要通过 `@vscode/webview-ui-toolkit` 或其他方式间接调用
|
||||||
|
- ❌ 可能存在兼容性问题
|
||||||
|
|
||||||
|
**适用场景**: 理论可行,但实际受限于 VS Code 扩展沙箱
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.4 方案四:结合 VS Code 通知 + 系统通知
|
||||||
|
|
||||||
|
**描述**: 同时使用 VS Code 内置通知和系统通知
|
||||||
|
|
||||||
|
**优点**:
|
||||||
|
- ✅ 双重保障,覆盖所有场景
|
||||||
|
- ✅ 用户在 VS Code 内外都能看到
|
||||||
|
|
||||||
|
**缺点**:
|
||||||
|
- ❌ 可能显得冗余
|
||||||
|
- ❌ 需要处理两种通知的协调逻辑
|
||||||
|
|
||||||
|
**适用场景**: 对通知可靠性要求极高的场景
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.5 方案对比表
|
||||||
|
|
||||||
|
| 方案 | 跨平台 | 依赖大小 | 实现难度 | 用户体验 | 推荐度 |
|
||||||
|
|------|--------|----------|----------|----------|--------|
|
||||||
|
| node-notifier | ✅ | ~500KB | ⭐ 低 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
|
||||||
|
| PowerShell Toast | ❌ Windows Only | 0 | ⭐⭐⭐ 高 | ⭐⭐⭐⭐ | ⭐⭐ |
|
||||||
|
| Electron API | ✅ | 0 | ⭐⭐⭐⭐ 很高 | ⭐⭐⭐ | ⭐ |
|
||||||
|
| 双重通知 | ✅ | ~500KB | ⭐⭐ 中 | ⭐⭐⭐⭐ | ⭐⭐⭐ |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 推荐方案详解
|
||||||
|
|
||||||
|
### 3.1 选择 node-notifier 的理由
|
||||||
|
|
||||||
|
1. **成熟稳定**: 被广泛使用(npm 周下载量 > 200 万)
|
||||||
|
2. **跨平台**: 自动适配不同操作系统的通知机制
|
||||||
|
3. **功能丰富**: 支持图标、声音、操作按钮、回调
|
||||||
|
4. **易于集成**: 与 VS Code 扩展开发无缝集成
|
||||||
|
|
||||||
|
### 3.2 node-notifier 工作原理
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
|
│ IC Coder Extension │
|
||||||
|
│ ┌────────────────────────────────────────────────────────┐ │
|
||||||
|
│ │ notificationService.ts │ │
|
||||||
|
│ │ ┌──────────────────────────────────────────────────┐ │ │
|
||||||
|
│ │ │ sendSystemNotification(title, message, options) │ │ │
|
||||||
|
│ │ └──────────────────┬───────────────────────────────┘ │ │
|
||||||
|
│ └────────────────────┼──────────────────────────────────┘ │
|
||||||
|
└────────────────────────┼─────────────────────────────────────┘
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
┌──────────────────────┐
|
||||||
|
│ node-notifier │
|
||||||
|
│ (跨平台适配层) │
|
||||||
|
└──────────┬───────────┘
|
||||||
|
│
|
||||||
|
┌───────────────┼───────────────┐
|
||||||
|
│ │ │
|
||||||
|
▼ ▼ ▼
|
||||||
|
┌─────────┐ ┌──────────┐ ┌──────────┐
|
||||||
|
│ Windows │ │ macOS │ │ Linux │
|
||||||
|
│ Toast │ │ NSUser │ │ notify- │
|
||||||
|
│ Notif. │ │ Notif. │ │ send │
|
||||||
|
└─────────┘ └──────────┘ └──────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 各平台通知效果
|
||||||
|
|
||||||
|
#### Windows 10/11
|
||||||
|
- 显示在右下角 Action Center
|
||||||
|
- 支持应用图标、标题、消息、操作按钮
|
||||||
|
- 可以播放系统声音
|
||||||
|
- 通知历史保存在通知中心
|
||||||
|
|
||||||
|
#### macOS
|
||||||
|
- 显示在右上角 Notification Center
|
||||||
|
- 支持应用图标、标题、副标题、消息
|
||||||
|
- 可以播放系统声音
|
||||||
|
- 支持回复和操作按钮
|
||||||
|
|
||||||
|
#### Linux
|
||||||
|
- 使用 `notify-send` 或 `libnotify`
|
||||||
|
- 显示位置取决于桌面环境(GNOME/KDE/XFCE)
|
||||||
|
- 支持图标、标题、消息、紧急程度
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 实现步骤
|
||||||
|
|
||||||
|
### 4.1 安装依赖
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 安装 node-notifier
|
||||||
|
pnpm add node-notifier
|
||||||
|
|
||||||
|
# 安装类型定义
|
||||||
|
pnpm add -D @types/node-notifier
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 创建通知服务模块
|
||||||
|
|
||||||
|
创建 `src/services/notificationService.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import * as notifier from 'node-notifier';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知类型枚举
|
||||||
|
*/
|
||||||
|
export enum NotificationType {
|
||||||
|
INFO = 'info',
|
||||||
|
SUCCESS = 'success',
|
||||||
|
WARNING = 'warning',
|
||||||
|
ERROR = 'error'
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知选项接口
|
||||||
|
*/
|
||||||
|
export interface NotificationOptions {
|
||||||
|
/** 通知标题 */
|
||||||
|
title: string;
|
||||||
|
/** 通知消息 */
|
||||||
|
message: string;
|
||||||
|
/** 通知类型 */
|
||||||
|
type?: NotificationType;
|
||||||
|
/** 是否播放声音 */
|
||||||
|
sound?: boolean;
|
||||||
|
/** 超时时间(秒),0 表示不自动消失 */
|
||||||
|
timeout?: number;
|
||||||
|
/** 自定义图标路径 */
|
||||||
|
icon?: string;
|
||||||
|
/** 点击通知时的回调 */
|
||||||
|
onClick?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统通知服务类
|
||||||
|
*/
|
||||||
|
export class NotificationService {
|
||||||
|
private static instance: NotificationService;
|
||||||
|
private readonly extensionPath: string;
|
||||||
|
private readonly iconPath: string;
|
||||||
|
|
||||||
|
private constructor(context: vscode.ExtensionContext) {
|
||||||
|
this.extensionPath = context.extensionPath;
|
||||||
|
this.iconPath = path.join(this.extensionPath, 'resources', 'icon.png');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例实例
|
||||||
|
*/
|
||||||
|
public static getInstance(context?: vscode.ExtensionContext): NotificationService {
|
||||||
|
if (!NotificationService.instance && context) {
|
||||||
|
NotificationService.instance = new NotificationService(context);
|
||||||
|
}
|
||||||
|
return NotificationService.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否启用系统通知
|
||||||
|
*/
|
||||||
|
private isSystemNotificationEnabled(): boolean {
|
||||||
|
const config = vscode.workspace.getConfiguration('ic-coder');
|
||||||
|
return config.get<boolean>('enableSystemNotification', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送系统通知
|
||||||
|
*/
|
||||||
|
public sendNotification(options: NotificationOptions): void {
|
||||||
|
// 检查用户配置
|
||||||
|
if (!this.isSystemNotificationEnabled()) {
|
||||||
|
console.log('[NotificationService] 系统通知已禁用');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type = NotificationType.INFO,
|
||||||
|
sound = true,
|
||||||
|
timeout = 10,
|
||||||
|
icon,
|
||||||
|
onClick
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
// 准备通知参数
|
||||||
|
const notificationConfig: notifier.Notification = {
|
||||||
|
title: title,
|
||||||
|
message: message,
|
||||||
|
icon: icon || this.iconPath,
|
||||||
|
sound: sound,
|
||||||
|
wait: false,
|
||||||
|
timeout: timeout,
|
||||||
|
appID: 'IC Coder' // Windows 10/11 需要
|
||||||
|
};
|
||||||
|
|
||||||
|
// 发送通知
|
||||||
|
notifier.notify(notificationConfig, (err, response, metadata) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('[NotificationService] 通知发送失败:', err);
|
||||||
|
// 降级到 VS Code 内置通知
|
||||||
|
this.fallbackToVSCodeNotification(title, message, type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[NotificationService] 通知已发送:', response, metadata);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 监听通知点击事件
|
||||||
|
if (onClick) {
|
||||||
|
notifier.on('click', (notifierObject, options, event) => {
|
||||||
|
onClick();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 降级到 VS Code 内置通知
|
||||||
|
*/
|
||||||
|
private fallbackToVSCodeNotification(
|
||||||
|
title: string,
|
||||||
|
message: string,
|
||||||
|
type: NotificationType
|
||||||
|
): void {
|
||||||
|
const fullMessage = `${title}: ${message}`;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case NotificationType.ERROR:
|
||||||
|
vscode.window.showErrorMessage(fullMessage);
|
||||||
|
break;
|
||||||
|
case NotificationType.WARNING:
|
||||||
|
vscode.window.showWarningMessage(fullMessage);
|
||||||
|
break;
|
||||||
|
case NotificationType.SUCCESS:
|
||||||
|
case NotificationType.INFO:
|
||||||
|
default:
|
||||||
|
vscode.window.showInformationMessage(fullMessage);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送成功通知
|
||||||
|
*/
|
||||||
|
public success(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.SUCCESS,
|
||||||
|
sound: true,
|
||||||
|
timeout: 10,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送错误通知
|
||||||
|
*/
|
||||||
|
public error(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.ERROR,
|
||||||
|
sound: true,
|
||||||
|
timeout: 15,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送警告通知
|
||||||
|
*/
|
||||||
|
public warning(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.WARNING,
|
||||||
|
sound: true,
|
||||||
|
timeout: 10,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送信息通知
|
||||||
|
*/
|
||||||
|
public info(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.INFO,
|
||||||
|
sound: false,
|
||||||
|
timeout: 8,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.3 在扩展入口初始化服务
|
||||||
|
|
||||||
|
修改 `src/extension.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { NotificationService } from './services/notificationService';
|
||||||
|
|
||||||
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
|
// 初始化通知服务
|
||||||
|
const notificationService = NotificationService.getInstance(context);
|
||||||
|
|
||||||
|
// ... 其他初始化代码
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.4 在消息处理器中使用
|
||||||
|
|
||||||
|
修改 `src/utils/messageHandler.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { NotificationService } from '../services/notificationService';
|
||||||
|
|
||||||
|
// 在适当的位置添加通知
|
||||||
|
export async function handleMessage(message: any, panel: vscode.WebviewPanel) {
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
|
||||||
|
// 示例:iverilog 仿真完成
|
||||||
|
if (message.type === 'simulationComplete') {
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder - 仿真完成',
|
||||||
|
'iverilog 仿真已成功完成,VCD 文件已生成',
|
||||||
|
() => {
|
||||||
|
// 点击通知时聚焦到 VS Code
|
||||||
|
vscode.window.showTextDocument(vscode.window.activeTextEditor!.document);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例:仿真失败
|
||||||
|
if (message.type === 'simulationError') {
|
||||||
|
notificationService.error(
|
||||||
|
'IC Coder - 仿真失败',
|
||||||
|
`仿真过程中发生错误: ${message.error}`,
|
||||||
|
() => {
|
||||||
|
// 点击通知时打开输出面板
|
||||||
|
panel.reveal();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.5 添加配置项
|
||||||
|
|
||||||
|
修改 `package.json`
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"contributes": {
|
||||||
|
"configuration": {
|
||||||
|
"title": "IC Coder",
|
||||||
|
"properties": {
|
||||||
|
"ic-coder.enableSystemNotification": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"description": "启用系统级通知(任务完成时显示操作系统通知)"
|
||||||
|
},
|
||||||
|
"ic-coder.notificationSound": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"description": "通知时播放系统声音"
|
||||||
|
},
|
||||||
|
"ic-coder.notificationTimeout": {
|
||||||
|
"type": "number",
|
||||||
|
"default": 10,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 60,
|
||||||
|
"description": "通知自动消失时间(秒),0 表示不自动消失"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. API 设计
|
||||||
|
|
||||||
|
### 5.1 核心 API
|
||||||
|
|
||||||
|
#### `NotificationService.getInstance(context?)`
|
||||||
|
|
||||||
|
获取通知服务单例实例
|
||||||
|
|
||||||
|
**参数**:
|
||||||
|
- `context` (可选): `vscode.ExtensionContext` - 扩展上下文,首次调用时必须提供
|
||||||
|
|
||||||
|
**返回**: `NotificationService` 实例
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```typescript
|
||||||
|
const notificationService = NotificationService.getInstance(context);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### `sendNotification(options)`
|
||||||
|
|
||||||
|
发送自定义通知
|
||||||
|
|
||||||
|
**参数**:
|
||||||
|
- `options`: `NotificationOptions` - 通知选项对象
|
||||||
|
|
||||||
|
**返回**: `void`
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```typescript
|
||||||
|
notificationService.sendNotification({
|
||||||
|
title: 'IC Coder',
|
||||||
|
message: '任务已完成',
|
||||||
|
type: NotificationType.SUCCESS,
|
||||||
|
sound: true,
|
||||||
|
timeout: 10,
|
||||||
|
onClick: () => {
|
||||||
|
console.log('用户点击了通知');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### `success(title, message, onClick?)`
|
||||||
|
|
||||||
|
发送成功通知(快捷方法)
|
||||||
|
|
||||||
|
**参数**:
|
||||||
|
- `title`: `string` - 通知标题
|
||||||
|
- `message`: `string` - 通知消息
|
||||||
|
- `onClick` (可选): `() => void` - 点击回调
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```typescript
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder',
|
||||||
|
'VCD 文件生成成功',
|
||||||
|
() => panel.reveal()
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### `error(title, message, onClick?)`
|
||||||
|
|
||||||
|
发送错误通知(快捷方法)
|
||||||
|
|
||||||
|
**参数**:
|
||||||
|
- `title`: `string` - 通知标题
|
||||||
|
- `message`: `string` - 通知消息
|
||||||
|
- `onClick` (可选): `() => void` - 点击回调
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```typescript
|
||||||
|
notificationService.error(
|
||||||
|
'IC Coder',
|
||||||
|
'编译失败: 语法错误',
|
||||||
|
() => vscode.commands.executeCommand('workbench.action.showErrorsWarnings')
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### `warning(title, message, onClick?)`
|
||||||
|
|
||||||
|
发送警告通知(快捷方法)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### `info(title, message, onClick?)`
|
||||||
|
|
||||||
|
发送信息通知(快捷方法)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.2 类型定义
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
enum NotificationType {
|
||||||
|
INFO = 'info',
|
||||||
|
SUCCESS = 'success',
|
||||||
|
WARNING = 'warning',
|
||||||
|
ERROR = 'error'
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NotificationOptions {
|
||||||
|
title: string;
|
||||||
|
message: string;
|
||||||
|
type?: NotificationType;
|
||||||
|
sound?: boolean;
|
||||||
|
timeout?: number;
|
||||||
|
icon?: string;
|
||||||
|
onClick?: () => void;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 配置选项
|
||||||
|
|
||||||
|
### 6.1 用户配置项
|
||||||
|
|
||||||
|
| 配置项 | 类型 | 默认值 | 说明 |
|
||||||
|
|--------|------|--------|------|
|
||||||
|
| `ic-coder.enableSystemNotification` | `boolean` | `true` | 是否启用系统通知 |
|
||||||
|
| `ic-coder.notificationSound` | `boolean` | `true` | 是否播放通知声音 |
|
||||||
|
| `ic-coder.notificationTimeout` | `number` | `10` | 通知自动消失时间(秒) |
|
||||||
|
|
||||||
|
### 6.2 配置方式
|
||||||
|
|
||||||
|
#### 方式 1: VS Code 设置界面
|
||||||
|
|
||||||
|
1. 打开 VS Code 设置 (`Ctrl+,` / `Cmd+,`)
|
||||||
|
2. 搜索 "IC Coder"
|
||||||
|
3. 找到 "Enable System Notification" 选项
|
||||||
|
4. 勾选或取消勾选
|
||||||
|
|
||||||
|
#### 方式 2: settings.json
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"ic-coder.enableSystemNotification": true,
|
||||||
|
"ic-coder.notificationSound": true,
|
||||||
|
"ic-coder.notificationTimeout": 10
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 测试方案
|
||||||
|
|
||||||
|
### 7.1 单元测试
|
||||||
|
|
||||||
|
创建 `src/test/suite/notificationService.test.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import * as assert from 'assert';
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import { NotificationService, NotificationType } from '../../services/notificationService';
|
||||||
|
|
||||||
|
suite('NotificationService Test Suite', () => {
|
||||||
|
let notificationService: NotificationService;
|
||||||
|
|
||||||
|
suiteSetup(() => {
|
||||||
|
const context = {
|
||||||
|
extensionPath: __dirname
|
||||||
|
} as vscode.ExtensionContext;
|
||||||
|
notificationService = NotificationService.getInstance(context);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('应该成功创建单例实例', () => {
|
||||||
|
const instance1 = NotificationService.getInstance();
|
||||||
|
const instance2 = NotificationService.getInstance();
|
||||||
|
assert.strictEqual(instance1, instance2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('应该发送成功通知', (done) => {
|
||||||
|
notificationService.success('测试标题', '测试消息');
|
||||||
|
setTimeout(() => done(), 1000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.2 手动测试清单
|
||||||
|
|
||||||
|
#### Windows 测试
|
||||||
|
- [ ] 通知显示在 Action Center
|
||||||
|
- [ ] 点击通知能够聚焦到 VS Code
|
||||||
|
- [ ] 通知声音正常播放
|
||||||
|
- [ ] 通知图标正确显示
|
||||||
|
- [ ] 通知在设定时间后自动消失
|
||||||
|
- [ ] 禁用系统通知后不再显示
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 注意事项
|
||||||
|
|
||||||
|
### 8.1 权限问题
|
||||||
|
|
||||||
|
**Windows**:
|
||||||
|
- 首次使用时,Windows 可能会弹出权限请求
|
||||||
|
- 用户需要在"设置 > 系统 > 通知和操作"中允许应用通知
|
||||||
|
|
||||||
|
**macOS**:
|
||||||
|
- 需要在"系统偏好设置 > 通知"中允许 VS Code 发送通知
|
||||||
|
|
||||||
|
**Linux**:
|
||||||
|
- 需要安装 `libnotify-bin` 包
|
||||||
|
- 不同桌面环境的通知样式可能不同
|
||||||
|
|
||||||
|
### 8.2 通知频率控制
|
||||||
|
|
||||||
|
为避免通知轰炸,建议实现防抖机制:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export class NotificationService {
|
||||||
|
private lastNotificationTime: Map<string, number> = new Map();
|
||||||
|
private readonly DEBOUNCE_INTERVAL = 3000; // 3 秒
|
||||||
|
|
||||||
|
private shouldSendNotification(key: string): boolean {
|
||||||
|
const now = Date.now();
|
||||||
|
const lastTime = this.lastNotificationTime.get(key) || 0;
|
||||||
|
|
||||||
|
if (now - lastTime < this.DEBOUNCE_INTERVAL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastNotificationTime.set(key, now);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.3 错误处理
|
||||||
|
|
||||||
|
通知发送失败时,自动降级到 VS Code 内置通知。
|
||||||
|
|
||||||
|
### 8.4 安全考虑
|
||||||
|
|
||||||
|
- **不要在通知中显示敏感信息**(如 token、密码)
|
||||||
|
- **验证通知内容**,防止 XSS 攻击
|
||||||
|
- **限制通知频率**,防止滥用
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 常见问题
|
||||||
|
|
||||||
|
### 9.1 通知不显示
|
||||||
|
|
||||||
|
**问题**: 调用通知 API 后,系统没有显示通知
|
||||||
|
|
||||||
|
**可能原因**:
|
||||||
|
1. 用户禁用了系统通知权限
|
||||||
|
2. 操作系统的"勿扰模式"已启用
|
||||||
|
3. `node-notifier` 安装失败或版本不兼容
|
||||||
|
|
||||||
|
**解决方案**:
|
||||||
|
```typescript
|
||||||
|
// 添加调试日志
|
||||||
|
notifier.notify(notificationConfig, (err, response, metadata) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('[NotificationService] 错误:', err);
|
||||||
|
} else {
|
||||||
|
console.log('[NotificationService] 响应:', response);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.2 通知点击回调不触发
|
||||||
|
|
||||||
|
**问题**: 点击通知后,`onClick` 回调没有执行
|
||||||
|
|
||||||
|
**解决方案**:
|
||||||
|
```typescript
|
||||||
|
// 设置 wait: true
|
||||||
|
const notificationConfig: notifier.Notification = {
|
||||||
|
title: title,
|
||||||
|
message: message,
|
||||||
|
wait: true, // 等待用户交互
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.3 通知图标不显示
|
||||||
|
|
||||||
|
**问题**: 通知显示时没有自定义图标
|
||||||
|
|
||||||
|
**解决方案**:
|
||||||
|
```typescript
|
||||||
|
import * as fs from 'fs';
|
||||||
|
|
||||||
|
// 检查图标是否存在
|
||||||
|
if (!fs.existsSync(this.iconPath)) {
|
||||||
|
console.warn(`图标文件不存在: ${this.iconPath}`);
|
||||||
|
this.iconPath = ''; // 使用系统默认图标
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.4 Linux 上通知不工作
|
||||||
|
|
||||||
|
**问题**: 在 Linux 系统上通知无法显示
|
||||||
|
|
||||||
|
**解决方案**:
|
||||||
|
```bash
|
||||||
|
# Ubuntu/Debian
|
||||||
|
sudo apt-get install libnotify-bin
|
||||||
|
|
||||||
|
# Fedora/RHEL
|
||||||
|
sudo dnf install libnotify
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 最佳实践
|
||||||
|
|
||||||
|
### 10.1 通知时机
|
||||||
|
|
||||||
|
**推荐发送通知的场景**:
|
||||||
|
- ✅ 长时间运行的任务完成(> 10 秒)
|
||||||
|
- ✅ 后台任务完成(用户可能已切换到其他应用)
|
||||||
|
- ✅ 发生错误需要用户关注
|
||||||
|
- ✅ 重要状态变更
|
||||||
|
|
||||||
|
**不推荐发送通知的场景**:
|
||||||
|
- ❌ 即时完成的操作(< 3 秒)
|
||||||
|
- ❌ 用户主动触发且立即完成的操作
|
||||||
|
- ❌ 频繁发生的事件(如自动保存)
|
||||||
|
- ❌ 调试信息或日志
|
||||||
|
|
||||||
|
### 10.2 通知内容
|
||||||
|
|
||||||
|
**标题**:
|
||||||
|
- 简洁明了,不超过 20 个字符
|
||||||
|
- 包含应用名称(如 "IC Coder - 仿真完成")
|
||||||
|
- 使用动作完成时态("已完成" 而不是 "完成中")
|
||||||
|
|
||||||
|
**消息**:
|
||||||
|
- 提供具体信息,不超过 100 个字符
|
||||||
|
- 包含关键细节(如文件名、错误类型)
|
||||||
|
- 避免技术术语,使用用户友好的语言
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```typescript
|
||||||
|
// ✅ 好的通知
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder - 仿真完成',
|
||||||
|
'testbench.v 仿真成功,VCD 文件已生成'
|
||||||
|
);
|
||||||
|
|
||||||
|
// ❌ 不好的通知
|
||||||
|
notificationService.success('完成', '操作已完成');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 10.3 通知优先级
|
||||||
|
|
||||||
|
根据重要性设置不同的通知类型和超时时间:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 高优先级:错误(15 秒)
|
||||||
|
notificationService.error(
|
||||||
|
'IC Coder - 编译失败',
|
||||||
|
'发现 3 个语法错误,请检查代码'
|
||||||
|
);
|
||||||
|
|
||||||
|
// 中优先级:警告(10 秒)
|
||||||
|
notificationService.warning(
|
||||||
|
'IC Coder - 警告',
|
||||||
|
'仿真时间过长,可能存在死循环'
|
||||||
|
);
|
||||||
|
|
||||||
|
// 低优先级:信息(8 秒,无声音)
|
||||||
|
notificationService.info(
|
||||||
|
'IC Coder - 提示',
|
||||||
|
'已自动保存工作区'
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11. 性能指标
|
||||||
|
|
||||||
|
### 11.1 预期性能
|
||||||
|
|
||||||
|
| 指标 | 目标值 | 说明 |
|
||||||
|
|------|--------|------|
|
||||||
|
| 通知发送延迟 | < 100ms | 从调用到系统显示 |
|
||||||
|
| 内存占用 | < 5MB | 通知服务常驻内存 |
|
||||||
|
| CPU 占用 | < 1% | 空闲时 CPU 使用率 |
|
||||||
|
| 包体积增加 | ~500KB | node-notifier 依赖 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12. 参考资料
|
||||||
|
|
||||||
|
### 12.1 官方文档
|
||||||
|
|
||||||
|
- [node-notifier GitHub](https://github.com/mikaelbr/node-notifier)
|
||||||
|
- [VS Code Extension API](https://code.visualstudio.com/api)
|
||||||
|
- [Windows Toast Notifications](https://docs.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-notifications-overview)
|
||||||
|
|
||||||
|
### 12.2 相关文章
|
||||||
|
|
||||||
|
- [Best Practices for Desktop Notifications](https://web.dev/notifications/)
|
||||||
|
- [Designing Better Notifications](https://uxdesign.cc/designing-better-notifications-36ba9c0b3e0e)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 13. 总结
|
||||||
|
|
||||||
|
本文档详细介绍了在 IC Coder 插件中实现系统级通知功能的完整方案,包括:
|
||||||
|
|
||||||
|
✅ **技术选型**: 选择 `node-notifier` 作为跨平台通知解决方案
|
||||||
|
✅ **架构设计**: 单例模式的通知服务类,支持多种通知类型
|
||||||
|
✅ **实现细节**: 完整的代码示例和配置说明
|
||||||
|
✅ **测试方案**: 单元测试、集成测试和手动测试清单
|
||||||
|
✅ **最佳实践**: 通知时机、内容设计和用户体验优化
|
||||||
|
✅ **故障排查**: 常见问题和解决方案
|
||||||
|
|
||||||
|
通过实现系统级通知,IC Coder 插件能够在用户切换到其他应用时仍然及时通知任务状态,显著提升用户体验。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**文档版本**: v1.0
|
||||||
|
**最后更新**: 2026-01-26
|
||||||
|
**作者**: IC Coder Team
|
||||||
|
**许可**: MIT License
|
||||||
|
|
||||||
277
docs/token-expiration-check.md
Normal file
@ -0,0 +1,277 @@
|
|||||||
|
# Token 过期检查实现方案
|
||||||
|
|
||||||
|
## 1. 概述
|
||||||
|
|
||||||
|
实现三个关键时机的 Token 过期检查:
|
||||||
|
- 插件激活时
|
||||||
|
- 发起 API 请求前
|
||||||
|
- 用户交互时(打开面板/侧边栏)
|
||||||
|
|
||||||
|
## 2. 数据存储
|
||||||
|
|
||||||
|
### 2.1 存储位置
|
||||||
|
使用 VS Code 的 `globalState` 存储:
|
||||||
|
```typescript
|
||||||
|
context.globalState.update('tokenExp', exp);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 存储内容
|
||||||
|
- `token`: 用户 token
|
||||||
|
- `tokenExp`: 过期时间戳(秒)
|
||||||
|
- `userInfo`: 用户信息
|
||||||
|
|
||||||
|
## 3. 核心函数设计
|
||||||
|
|
||||||
|
### 3.1 过期检查函数
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 检查 token 是否过期
|
||||||
|
* @param exp - 过期时间戳(秒)
|
||||||
|
* @param bufferSeconds - 提前判断过期的缓冲时间(默认 60 秒)
|
||||||
|
* @returns true 表示已过期或即将过期
|
||||||
|
*/
|
||||||
|
function isTokenExpired(exp: number | undefined, bufferSeconds: number = 60): boolean {
|
||||||
|
if (!exp) {
|
||||||
|
return true; // 没有过期时间,视为已过期
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = Math.floor(Date.now() / 1000); // 当前时间戳(秒)
|
||||||
|
return now >= (exp - bufferSeconds); // 提前 60 秒判断过期
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 清除登录状态函数
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 清除所有登录相关状态
|
||||||
|
*/
|
||||||
|
async function clearAuthState(context: vscode.ExtensionContext): Promise<void> {
|
||||||
|
await context.globalState.update('token', undefined);
|
||||||
|
await context.globalState.update('tokenExp', undefined);
|
||||||
|
await context.globalState.update('userInfo', undefined);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 统一过期处理函数
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 处理 token 过期情况
|
||||||
|
* @param context - 扩展上下文
|
||||||
|
* @param showMessage - 是否显示提示消息
|
||||||
|
*/
|
||||||
|
async function handleTokenExpired(
|
||||||
|
context: vscode.ExtensionContext,
|
||||||
|
showMessage: boolean = true
|
||||||
|
): Promise<void> {
|
||||||
|
await clearAuthState(context);
|
||||||
|
|
||||||
|
if (showMessage) {
|
||||||
|
const action = await vscode.window.showWarningMessage(
|
||||||
|
'登录已过期,请重新登录',
|
||||||
|
'立即登录'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (action === '立即登录') {
|
||||||
|
// 触发登录流程(打开登录面板)
|
||||||
|
vscode.commands.executeCommand('ic-coder.openPanel');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. 三个检查时机实现
|
||||||
|
|
||||||
|
### 4.1 插件激活时检查
|
||||||
|
|
||||||
|
**位置**: `src/extension.ts` 的 `activate` 函数
|
||||||
|
|
||||||
|
**实现**:
|
||||||
|
```typescript
|
||||||
|
export async function activate(context: vscode.ExtensionContext) {
|
||||||
|
console.log('IC Coder 插件正在激活...');
|
||||||
|
|
||||||
|
// 1. 检查 token 是否过期
|
||||||
|
const tokenExp = context.globalState.get<number>('tokenExp');
|
||||||
|
if (isTokenExpired(tokenExp)) {
|
||||||
|
// 静默清除,不显示提示(避免启动时打扰用户)
|
||||||
|
await handleTokenExpired(context, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... 其他激活逻辑
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**说明**: 启动时静默检查,如果过期则清除状态,但不弹窗提示
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4.2 发起 API 请求前检查
|
||||||
|
|
||||||
|
**位置**: `src/utils/messageHandler.ts` 的 API 请求函数
|
||||||
|
|
||||||
|
**实现**:
|
||||||
|
```typescript
|
||||||
|
// 在发送消息到后端前检查
|
||||||
|
async function sendMessageToBackend(message: string, context: vscode.ExtensionContext) {
|
||||||
|
// 1. 检查 token 是否过期
|
||||||
|
const tokenExp = context.globalState.get<number>('tokenExp');
|
||||||
|
if (isTokenExpired(tokenExp)) {
|
||||||
|
await handleTokenExpired(context, true); // 显示提示
|
||||||
|
return; // 中断请求
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = context.globalState.get<string>('token');
|
||||||
|
if (!token) {
|
||||||
|
vscode.window.showWarningMessage('请先登录');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 继续发送请求
|
||||||
|
// ... 原有请求逻辑
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**说明**: 每次 API 请求前检查,如果过期则提示用户并中断请求
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4.3 用户交互时检查
|
||||||
|
|
||||||
|
**位置**:
|
||||||
|
- `src/panels/ICHelperPanel.ts` - 打开聊天面板时
|
||||||
|
- `src/views/ICViewProvider.ts` - 侧边栏视图加载时
|
||||||
|
|
||||||
|
**实现 - 聊天面板**:
|
||||||
|
```typescript
|
||||||
|
// ICHelperPanel.ts
|
||||||
|
public static render(extensionUri: vscode.Uri, context: vscode.ExtensionContext) {
|
||||||
|
// 1. 检查 token 是否过期
|
||||||
|
const tokenExp = context.globalState.get<number>('tokenExp');
|
||||||
|
if (isTokenExpired(tokenExp)) {
|
||||||
|
handleTokenExpired(context, true); // 显示提示
|
||||||
|
// 继续渲染面板,但会显示未登录状态
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 创建或显示面板
|
||||||
|
// ... 原有逻辑
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**实现 - 侧边栏视图**:
|
||||||
|
```typescript
|
||||||
|
// ICViewProvider.ts
|
||||||
|
public resolveWebviewView(webviewView: vscode.WebviewView) {
|
||||||
|
// 1. 检查 token 是否过期
|
||||||
|
const tokenExp = this._context.globalState.get<number>('tokenExp');
|
||||||
|
if (isTokenExpired(tokenExp)) {
|
||||||
|
handleTokenExpired(this._context, false); // 静默清除
|
||||||
|
// 继续渲染,显示未登录状态
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 渲染视图
|
||||||
|
// ... 原有逻辑
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**说明**: 打开面板时检查,聊天面板显示提示,侧边栏静默处理
|
||||||
|
|
||||||
|
## 5. 后端响应处理
|
||||||
|
|
||||||
|
### 5.1 保存 exp 字段
|
||||||
|
|
||||||
|
**位置**: `src/utils/messageHandler.ts` 处理登录响应的地方
|
||||||
|
|
||||||
|
**实现**:
|
||||||
|
```typescript
|
||||||
|
// 处理登录成功响应
|
||||||
|
if (response.data.token) {
|
||||||
|
await context.globalState.update('token', response.data.token);
|
||||||
|
|
||||||
|
// 保存过期时间
|
||||||
|
if (response.data.exp) {
|
||||||
|
await context.globalState.update('tokenExp', response.data.exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存用户信息
|
||||||
|
if (response.data.userInfo) {
|
||||||
|
await context.globalState.update('userInfo', response.data.userInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 处理 401 响应
|
||||||
|
|
||||||
|
**实现**:
|
||||||
|
```typescript
|
||||||
|
// API 请求错误处理
|
||||||
|
if (error.response?.status === 401) {
|
||||||
|
// 后端返回 401,说明 token 无效或过期
|
||||||
|
await handleTokenExpired(context, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6. 工具函数位置
|
||||||
|
|
||||||
|
建议创建新文件 `src/utils/authHelper.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export function isTokenExpired(exp: number | undefined, bufferSeconds: number = 60): boolean {
|
||||||
|
if (!exp) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
return now >= (exp - bufferSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function clearAuthState(context: vscode.ExtensionContext): Promise<void> {
|
||||||
|
await context.globalState.update('token', undefined);
|
||||||
|
await context.globalState.update('tokenExp', undefined);
|
||||||
|
await context.globalState.update('userInfo', undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function handleTokenExpired(
|
||||||
|
context: vscode.ExtensionContext,
|
||||||
|
showMessage: boolean = true
|
||||||
|
): Promise<void> {
|
||||||
|
await clearAuthState(context);
|
||||||
|
|
||||||
|
if (showMessage) {
|
||||||
|
const action = await vscode.window.showWarningMessage(
|
||||||
|
'登录已过期,请重新登录',
|
||||||
|
'立即登录'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (action === '立即登录') {
|
||||||
|
vscode.commands.executeCommand('ic-coder.openPanel');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7. 测试场景
|
||||||
|
|
||||||
|
1. **启动测试**: 设置过期的 exp,重启插件,验证状态被清除
|
||||||
|
2. **请求测试**: 设置即将过期的 exp,发送消息,验证被拦截
|
||||||
|
3. **交互测试**: 设置过期的 exp,打开面板,验证提示显示
|
||||||
|
4. **401 测试**: 模拟后端返回 401,验证状态清除
|
||||||
|
|
||||||
|
## 8. 注意事项
|
||||||
|
|
||||||
|
- 使用 60 秒缓冲时间,避免请求中途过期
|
||||||
|
- 启动和侧边栏加载时静默处理,避免打扰用户
|
||||||
|
- 主动操作(发消息、打开聊天面板)时显示提示
|
||||||
|
- 所有时间戳使用秒为单位(与后端保持一致)
|
||||||
|
- 过期检查应该在所有需要 token 的操作前执行
|
||||||
|
|
||||||
|
## 9. 修改文件清单
|
||||||
|
|
||||||
|
需要修改的文件:
|
||||||
|
1. **新建**: `src/utils/authHelper.ts` - 认证辅助工具函数
|
||||||
|
2. **修改**: `src/extension.ts` - 插件激活时检查
|
||||||
|
3. **修改**: `src/utils/messageHandler.ts` - API 请求前检查 + 保存 exp + 处理 401
|
||||||
|
4. **修改**: `src/panels/ICHelperPanel.ts` - 打开聊天面板时检查
|
||||||
|
5. **修改**: `src/views/ICViewProvider.ts` - 侧边栏加载时检查
|
||||||
|
|
||||||
751
docs/会话存储技术文档.md
Normal file
@ -0,0 +1,751 @@
|
|||||||
|
# IC Coder 会话存储技术文档
|
||||||
|
|
||||||
|
## 1. 概述
|
||||||
|
|
||||||
|
IC Coder 的会话存储系统负责持久化保存用户与 AI 的对话历史,支持多项目、多任务的会话管理。系统采用文件系统存储方案,将会话数据按项目和任务组织,便于管理和检索。
|
||||||
|
|
||||||
|
### 1.1 核心特性
|
||||||
|
|
||||||
|
- **多项目支持**:不同项目的会话数据独立存储
|
||||||
|
- **任务级管理**:每个会话作为独立任务进行管理
|
||||||
|
- **分页加载**:支持历史会话的分页查询,提升性能
|
||||||
|
- **实时更新**:会话数据实时保存,防止数据丢失
|
||||||
|
- **统计信息**:记录 Token 使用量、对话轮次等统计数据
|
||||||
|
|
||||||
|
### 1.2 技术栈
|
||||||
|
|
||||||
|
- **存储方式**:文件系统(JSON/JSONL 格式)
|
||||||
|
- **存储位置**:`~/.iccoder/projects/{项目路径编码}/{taskId}/`
|
||||||
|
- **数据格式**:
|
||||||
|
- `meta.json`:任务元数据
|
||||||
|
- `conversation.json`:完整对话历史
|
||||||
|
- `conversation_meta.jsonl`:对话轮次元数据(JSONL 格式)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 架构设计
|
||||||
|
|
||||||
|
### 2.1 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
~/.iccoder/
|
||||||
|
└── projects/
|
||||||
|
└── {项目路径编码}/
|
||||||
|
└── {taskId}/
|
||||||
|
├── meta.json # 任务元数据
|
||||||
|
├── conversation.json # 对话历史
|
||||||
|
└── conversation_meta.jsonl # 对话元数据
|
||||||
|
```
|
||||||
|
|
||||||
|
**项目路径编码规则**:
|
||||||
|
- 移除冒号 `:`
|
||||||
|
- 将斜杠 `/` 和反斜杠 `\` 替换为 `--`
|
||||||
|
- 示例:`C:\Users\admin\Documents\Project` → `C--Users--admin--Documents--Project`
|
||||||
|
|
||||||
|
**任务 ID 格式**:
|
||||||
|
- 格式:`task_{date}_{sequence}`
|
||||||
|
- 示例:`task_20231226_a3f9k2`
|
||||||
|
- `date`:8 位日期(YYYYMMDD)
|
||||||
|
- `sequence`:6 位随机字符串
|
||||||
|
|
||||||
|
### 2.2 核心类:ChatHistoryManager
|
||||||
|
|
||||||
|
`ChatHistoryManager` 是会话存储的核心管理类,采用单例模式设计。
|
||||||
|
|
||||||
|
**主要职责**:
|
||||||
|
1. 管理会话存储目录
|
||||||
|
2. 创建和切换任务
|
||||||
|
3. 保存和加载对话历史
|
||||||
|
4. 记录统计信息
|
||||||
|
5. 提供会话历史查询接口
|
||||||
|
|
||||||
|
**关键属性**:
|
||||||
|
```typescript
|
||||||
|
private static instance: ChatHistoryManager;
|
||||||
|
private baseDir: string; // ~/.iccoder
|
||||||
|
private currentTaskId: string | null; // 当前任务 ID
|
||||||
|
private currentProjectPath: string | null; // 当前项目路径
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 数据模型
|
||||||
|
|
||||||
|
### 3.1 TaskMeta(任务元数据)
|
||||||
|
|
||||||
|
存储在 `meta.json` 文件中,记录任务的基本信息和统计数据。
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface TaskMeta {
|
||||||
|
taskId: string; // 任务 ID
|
||||||
|
taskName: string; // 任务名称
|
||||||
|
projectPath: string; // 项目路径
|
||||||
|
createdAt: string; // 创建时间(ISO 8601)
|
||||||
|
updatedAt: string; // 更新时间(ISO 8601)
|
||||||
|
stats: {
|
||||||
|
credits: number; // 消耗的积分
|
||||||
|
totalTokens: number; // 总 Token 数
|
||||||
|
inputTokens: number; // 输入 Token 数
|
||||||
|
outputTokens: number; // 输出 Token 数
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"taskId": "task_20231226_a3f9k2",
|
||||||
|
"taskName": "实现计数器功能",
|
||||||
|
"projectPath": "C:\\Users\\admin\\Documents\\Project",
|
||||||
|
"createdAt": "2023-12-26T10:30:00.000Z",
|
||||||
|
"updatedAt": "2023-12-26T11:45:00.000Z",
|
||||||
|
"stats": {
|
||||||
|
"credits": 0,
|
||||||
|
"totalTokens": 15420,
|
||||||
|
"inputTokens": 8200,
|
||||||
|
"outputTokens": 7220
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 ChatMessage(对话消息)
|
||||||
|
|
||||||
|
存储在 `conversation.json` 文件中,记录完整的对话历史。
|
||||||
|
|
||||||
|
**消息类型枚举**:
|
||||||
|
```typescript
|
||||||
|
enum MessageType {
|
||||||
|
USER = "USER", // 用户消息
|
||||||
|
AI = "AI", // AI 消息
|
||||||
|
SYSTEM = "SYSTEM", // 系统消息
|
||||||
|
TOOL_EXECUTION_RESULT = "TOOL_EXECUTION_RESULT" // 工具执行结果
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**用户消息**:
|
||||||
|
```typescript
|
||||||
|
interface UserMessage {
|
||||||
|
type: MessageType.USER;
|
||||||
|
contents: Array<{
|
||||||
|
type: "TEXT";
|
||||||
|
text: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**AI 消息**:
|
||||||
|
```typescript
|
||||||
|
interface AiMessage {
|
||||||
|
type: MessageType.AI;
|
||||||
|
text: string;
|
||||||
|
toolExecutionRequests?: Array<{
|
||||||
|
id: string;
|
||||||
|
toolName: string;
|
||||||
|
parameters: any;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**系统消息**:
|
||||||
|
```typescript
|
||||||
|
interface SystemMessage {
|
||||||
|
type: MessageType.SYSTEM;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**工具执行结果消息**:
|
||||||
|
```typescript
|
||||||
|
interface ToolExecutionResultMessage {
|
||||||
|
type: MessageType.TOOL_EXECUTION_RESULT;
|
||||||
|
id: string;
|
||||||
|
toolName: string;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 ConversationMeta(对话轮次元数据)
|
||||||
|
|
||||||
|
存储在 `conversation_meta.jsonl` 文件中,每行一条记录(JSONL 格式)。
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ConversationMeta {
|
||||||
|
turnId: number; // 对话轮次 ID
|
||||||
|
timestamp: string; // 时间戳(ISO 8601)
|
||||||
|
usage?: {
|
||||||
|
inputTokens?: number;
|
||||||
|
outputTokens?: number;
|
||||||
|
totalTokens?: number;
|
||||||
|
};
|
||||||
|
model?: string; // 使用的模型
|
||||||
|
duration?: number; // 耗时(毫秒)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**示例**:
|
||||||
|
```jsonl
|
||||||
|
{"turnId":1,"timestamp":"2023-12-26T10:30:15.000Z","usage":{"inputTokens":120,"outputTokens":350,"totalTokens":470},"model":"gpt-4","duration":2500}
|
||||||
|
{"turnId":2,"timestamp":"2023-12-26T10:32:30.000Z","usage":{"inputTokens":200,"outputTokens":450,"totalTokens":650},"model":"gpt-4","duration":3200}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 核心功能实现
|
||||||
|
|
||||||
|
### 4.1 任务创建
|
||||||
|
|
||||||
|
**方法**:`createTask(projectPath: string, taskName: string): Promise<TaskMeta>`
|
||||||
|
|
||||||
|
**流程**:
|
||||||
|
1. 生成唯一的任务 ID
|
||||||
|
2. 创建任务元数据对象
|
||||||
|
3. 创建任务目录
|
||||||
|
4. 保存 `meta.json`
|
||||||
|
5. 初始化空的 `conversation.json`
|
||||||
|
6. 设置为当前任务
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:114-146`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
public async createTask(projectPath: string, taskName: string): Promise<TaskMeta> {
|
||||||
|
const taskId = this.generateTaskId();
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
|
||||||
|
const meta: TaskMeta = {
|
||||||
|
taskId,
|
||||||
|
taskName,
|
||||||
|
projectPath,
|
||||||
|
createdAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
stats: {
|
||||||
|
credits: 0,
|
||||||
|
totalTokens: 0,
|
||||||
|
inputTokens: 0,
|
||||||
|
outputTokens: 0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.currentTaskId = taskId;
|
||||||
|
this.currentProjectPath = projectPath;
|
||||||
|
|
||||||
|
// 创建任务目录
|
||||||
|
const taskDir = this.getTaskDir(projectPath, taskId);
|
||||||
|
await this.ensureTaskDir(taskDir);
|
||||||
|
|
||||||
|
// 保存 meta.json
|
||||||
|
await this.saveTaskMeta(meta);
|
||||||
|
|
||||||
|
// 初始化空的 conversation.json
|
||||||
|
await this.saveConversation([]);
|
||||||
|
|
||||||
|
return meta;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 消息保存
|
||||||
|
|
||||||
|
系统提供了四种消息保存方法:
|
||||||
|
|
||||||
|
#### 4.2.1 添加用户消息
|
||||||
|
|
||||||
|
**方法**:`addUserMessage(text: string): Promise<void>`
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:285-299`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
public async addUserMessage(text: string): Promise<void> {
|
||||||
|
await this.ensureCurrentTask();
|
||||||
|
const messages = await this.loadConversation();
|
||||||
|
|
||||||
|
const userMessage: UserMessage = {
|
||||||
|
type: MessageType.USER,
|
||||||
|
contents: [{ type: "TEXT", text }]
|
||||||
|
};
|
||||||
|
|
||||||
|
messages.push(userMessage);
|
||||||
|
await this.saveConversation(messages);
|
||||||
|
|
||||||
|
// 更新任务元数据
|
||||||
|
await this.updateTaskTimestamp();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4.2.2 添加 AI 消息
|
||||||
|
|
||||||
|
**方法**:`addAiMessage(text: string, toolRequests?: any[]): Promise<void>`
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:304-319`
|
||||||
|
|
||||||
|
#### 4.2.3 添加系统消息
|
||||||
|
|
||||||
|
**方法**:`addSystemMessage(text: string): Promise<void>`
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:324-335`
|
||||||
|
|
||||||
|
#### 4.2.4 添加工具执行结果
|
||||||
|
|
||||||
|
**方法**:`addToolExecutionResult(id: string, toolName: string, result: string): Promise<void>`
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:340-353`
|
||||||
|
|
||||||
|
### 4.3 对话元数据记录
|
||||||
|
|
||||||
|
**方法**:`recordTurnMeta(turnId, usage?, model?, duration?): Promise<void>`
|
||||||
|
|
||||||
|
**功能**:记录每轮对话的元数据,包括 Token 使用量、模型信息、耗时等。
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:358-378`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
public async recordTurnMeta(
|
||||||
|
turnId: number,
|
||||||
|
usage?: { inputTokens?: number; outputTokens?: number; totalTokens?: number },
|
||||||
|
model?: string,
|
||||||
|
duration?: number
|
||||||
|
): Promise<void> {
|
||||||
|
const meta: ConversationMeta = {
|
||||||
|
turnId,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
usage,
|
||||||
|
model,
|
||||||
|
duration
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.appendConversationMeta(meta);
|
||||||
|
|
||||||
|
// 更新任务统计
|
||||||
|
if (usage) {
|
||||||
|
await this.updateTaskStats(usage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.4 会话历史查询
|
||||||
|
|
||||||
|
**方法**:`getConversationHistoryList(projectPath, offset, limit): Promise<{items, total, hasMore}>`
|
||||||
|
|
||||||
|
**功能**:分页查询项目的会话历史列表。
|
||||||
|
|
||||||
|
**参数**:
|
||||||
|
- `projectPath`:项目路径
|
||||||
|
- `offset`:偏移量(从第几条开始,默认 0)
|
||||||
|
- `limit`:每页数量(默认 10)
|
||||||
|
|
||||||
|
**返回值**:
|
||||||
|
```typescript
|
||||||
|
{
|
||||||
|
items: Array<{
|
||||||
|
id: string; // 任务 ID
|
||||||
|
title: string; // 会话标题(第一句用户消息)
|
||||||
|
timestamp: string; // 创建时间
|
||||||
|
}>;
|
||||||
|
total: number; // 总数
|
||||||
|
hasMore: boolean; // 是否还有更多
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**代码位置**:`chatHistoryManager.ts:525-590`
|
||||||
|
|
||||||
|
**实现逻辑**:
|
||||||
|
1. 获取项目的所有任务列表(按更新时间倒序)
|
||||||
|
2. 根据 offset 和 limit 进行分页
|
||||||
|
3. 读取每个任务的 `conversation.json`
|
||||||
|
4. 提取第一条用户消息作为标题(截取前 50 个字符)
|
||||||
|
5. 返回分页结果
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 前端集成
|
||||||
|
|
||||||
|
### 5.1 会话历史栏组件
|
||||||
|
|
||||||
|
**文件**:`conversationHistoryBar.ts`
|
||||||
|
|
||||||
|
**组件结构**:
|
||||||
|
- 下拉按钮:显示 "Past Conversations"
|
||||||
|
- 下拉菜单:显示会话历史列表
|
||||||
|
- 新建按钮:创建新会话
|
||||||
|
|
||||||
|
**关键功能**:
|
||||||
|
|
||||||
|
#### 5.1.1 加载会话历史
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function loadMoreHistory() {
|
||||||
|
if (isLoadingHistory || (currentOffset > 0 && !hasMoreHistory)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已达到最大数量(100 条)
|
||||||
|
if (currentOffset >= MAX_HISTORY_ITEMS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoadingHistory = true;
|
||||||
|
vscode.postMessage({
|
||||||
|
command: 'loadConversationHistory',
|
||||||
|
offset: currentOffset,
|
||||||
|
limit: HISTORY_PAGE_SIZE
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.1.2 渲染会话列表
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function renderConversationHistory(data) {
|
||||||
|
isLoadingHistory = false;
|
||||||
|
|
||||||
|
// 追加新数据
|
||||||
|
conversationHistory = conversationHistory.concat(data.items);
|
||||||
|
totalHistory = data.total;
|
||||||
|
hasMoreHistory = data.hasMore;
|
||||||
|
currentOffset += data.items.length;
|
||||||
|
|
||||||
|
// 渲染所有历史记录
|
||||||
|
historyList.innerHTML = conversationHistory.map(item => `
|
||||||
|
<div class="history-item" onclick="selectConversation('${item.id}')">
|
||||||
|
<div class="history-item-title">${item.title || '未命名会话'}</div>
|
||||||
|
<div class="history-item-time">${formatTime(item.timestamp)}</div>
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
|
||||||
|
// 如果还有更多数据,添加"加载更多"提示
|
||||||
|
if (hasMoreHistory && currentOffset < MAX_HISTORY_ITEMS) {
|
||||||
|
historyList.innerHTML += `
|
||||||
|
<div class="history-load-more">
|
||||||
|
<span>滚动加载更多...</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.1.3 滚动加载
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
historyDropdownMenu.addEventListener('scroll', () => {
|
||||||
|
const menu = historyDropdownMenu;
|
||||||
|
const scrollTop = menu.scrollTop;
|
||||||
|
const scrollHeight = menu.scrollHeight;
|
||||||
|
const clientHeight = menu.clientHeight;
|
||||||
|
|
||||||
|
// 当滚动到距离底部 50px 时,加载更多
|
||||||
|
if (scrollHeight - scrollTop - clientHeight < 50) {
|
||||||
|
loadMoreHistory();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.1.4 时间格式化
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function formatTime(timestamp) {
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
const now = new Date();
|
||||||
|
const diff = now - date;
|
||||||
|
|
||||||
|
if (diff < 60000) return '刚刚';
|
||||||
|
if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前';
|
||||||
|
if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前';
|
||||||
|
if (diff < 604800000) return Math.floor(diff / 86400000) + '天前';
|
||||||
|
|
||||||
|
// 超过7天显示具体日期
|
||||||
|
return date.toLocaleDateString('zh-CN', {
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 后端消息处理
|
||||||
|
|
||||||
|
**文件**:`ICHelperPanel.ts`
|
||||||
|
|
||||||
|
**消息处理流程**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
case "loadConversationHistory":
|
||||||
|
// 加载会话历史(支持分页)
|
||||||
|
loadConversationHistory(panel, message.offset || 0, message.limit || 10);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "selectConversation":
|
||||||
|
// 选择会话(暂未实现)
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "createNewConversation":
|
||||||
|
// 创建新会话 - 在当前编辑器组中打开新标签页
|
||||||
|
showICHelperPanel(context, panel.viewColumn);
|
||||||
|
break;
|
||||||
|
```
|
||||||
|
|
||||||
|
**加载会话历史实现**:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async function loadConversationHistory(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
offset: number = 0,
|
||||||
|
limit: number = 10
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
|
||||||
|
|
||||||
|
if (!workspacePath) {
|
||||||
|
// 没有打开的工作区,返回空历史
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "conversationHistory",
|
||||||
|
items: [],
|
||||||
|
total: 0,
|
||||||
|
hasMore: false,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取会话历史列表(支持分页)
|
||||||
|
const result = await historyManager.getConversationHistoryList(
|
||||||
|
workspacePath,
|
||||||
|
offset,
|
||||||
|
limit
|
||||||
|
);
|
||||||
|
|
||||||
|
// 发送会话历史到前端
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "conversationHistory",
|
||||||
|
items: result.items,
|
||||||
|
total: result.total,
|
||||||
|
hasMore: result.hasMore,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("加载会话历史失败:", error);
|
||||||
|
// 发生错误时返回空历史
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "conversationHistory",
|
||||||
|
items: [],
|
||||||
|
total: 0,
|
||||||
|
hasMore: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 使用示例
|
||||||
|
|
||||||
|
### 6.1 创建新任务并保存对话
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
|
||||||
|
// 创建新任务
|
||||||
|
const task = await historyManager.createTask(
|
||||||
|
'C:\\Users\\admin\\Documents\\Project',
|
||||||
|
'实现计数器功能'
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加用户消息
|
||||||
|
await historyManager.addUserMessage('请帮我生成一个4位计数器');
|
||||||
|
|
||||||
|
// 添加 AI 消息
|
||||||
|
await historyManager.addAiMessage(
|
||||||
|
'好的,我来帮你生成一个4位计数器...',
|
||||||
|
[{ id: '1', toolName: 'generateCode', parameters: {} }]
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加工具执行结果
|
||||||
|
await historyManager.addToolExecutionResult(
|
||||||
|
'1',
|
||||||
|
'generateCode',
|
||||||
|
'代码生成成功'
|
||||||
|
);
|
||||||
|
|
||||||
|
// 记录对话元数据
|
||||||
|
await historyManager.recordTurnMeta(
|
||||||
|
1,
|
||||||
|
{ inputTokens: 120, outputTokens: 350, totalTokens: 470 },
|
||||||
|
'gpt-4',
|
||||||
|
2500
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.2 查询会话历史
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
|
||||||
|
// 获取第一页(前10条)
|
||||||
|
const page1 = await historyManager.getConversationHistoryList(
|
||||||
|
'C:\\Users\\admin\\Documents\\Project',
|
||||||
|
0,
|
||||||
|
10
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('总数:', page1.total);
|
||||||
|
console.log('是否还有更多:', page1.hasMore);
|
||||||
|
console.log('会话列表:', page1.items);
|
||||||
|
|
||||||
|
// 获取第二页(第11-20条)
|
||||||
|
const page2 = await historyManager.getConversationHistoryList(
|
||||||
|
'C:\\Users\\admin\\Documents\\Project',
|
||||||
|
10,
|
||||||
|
10
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.3 切换任务
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
|
||||||
|
// 切换到指定任务
|
||||||
|
const success = await historyManager.switchTask(
|
||||||
|
'C:\\Users\\admin\\Documents\\Project',
|
||||||
|
'task_20231226_a3f9k2'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
// 获取当前任务会话
|
||||||
|
const session = await historyManager.getCurrentTaskSession();
|
||||||
|
console.log('任务元数据:', session.meta);
|
||||||
|
console.log('对话历史:', session.messages);
|
||||||
|
console.log('对话元数据:', session.conversationMeta);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 性能优化
|
||||||
|
|
||||||
|
### 7.1 分页加载
|
||||||
|
|
||||||
|
- 前端默认每页加载 10 条记录
|
||||||
|
- 最多显示 100 条历史记录
|
||||||
|
- 滚动到底部时自动加载下一页
|
||||||
|
|
||||||
|
### 7.2 懒加载
|
||||||
|
|
||||||
|
- 只在打开下拉菜单时才加载会话历史
|
||||||
|
- 避免不必要的文件读取操作
|
||||||
|
|
||||||
|
### 7.3 缓存机制
|
||||||
|
|
||||||
|
- 前端缓存已加载的会话列表
|
||||||
|
- 避免重复请求相同数据
|
||||||
|
|
||||||
|
### 7.4 文件格式优化
|
||||||
|
|
||||||
|
- 使用 JSONL 格式存储对话元数据,支持追加写入
|
||||||
|
- 避免频繁读写整个文件
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 错误处理
|
||||||
|
|
||||||
|
### 8.1 目录不存在
|
||||||
|
|
||||||
|
系统会自动创建不存在的目录:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
private async ensureTaskDir(taskDir: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const uri = vscode.Uri.file(taskDir);
|
||||||
|
try {
|
||||||
|
await vscode.workspace.fs.stat(uri);
|
||||||
|
} catch {
|
||||||
|
// 目录不存在,创建它
|
||||||
|
await vscode.workspace.fs.createDirectory(uri);
|
||||||
|
console.log(`创建任务目录: ${taskDir}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("创建任务目录失败:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.2 文件读取失败
|
||||||
|
|
||||||
|
读取失败时返回默认值:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
private async loadConversation(): Promise<ChatMessage[]> {
|
||||||
|
try {
|
||||||
|
const uri = vscode.Uri.file(conversationPath);
|
||||||
|
const content = await vscode.workspace.fs.readFile(uri);
|
||||||
|
const data = Buffer.from(content).toString('utf-8');
|
||||||
|
return JSON.parse(data);
|
||||||
|
} catch (error) {
|
||||||
|
// 文件不存在或读取失败,返回空数组
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.3 无工作区处理
|
||||||
|
|
||||||
|
没有打开工作区时,自动创建默认任务:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
private async ensureCurrentTask(): Promise<void> {
|
||||||
|
if (!this.currentTaskId || !this.currentProjectPath) {
|
||||||
|
const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
|
||||||
|
if (workspacePath) {
|
||||||
|
await this.createTask(workspacePath, "默认任务");
|
||||||
|
} else {
|
||||||
|
throw new Error("没有打开的工作区,无法创建任务");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 未来扩展
|
||||||
|
|
||||||
|
### 9.1 会话切换功能
|
||||||
|
|
||||||
|
目前 `selectConversation` 功能暂未实现,未来可以支持:
|
||||||
|
- 点击历史会话,加载该会话的完整对话历史
|
||||||
|
- 在新标签页中打开历史会话
|
||||||
|
- 继续历史会话的对话
|
||||||
|
|
||||||
|
### 9.2 会话搜索
|
||||||
|
|
||||||
|
- 支持按关键词搜索会话
|
||||||
|
- 支持按时间范围筛选
|
||||||
|
- 支持按 Token 使用量排序
|
||||||
|
|
||||||
|
### 9.3 会话导出
|
||||||
|
|
||||||
|
- 导出为 Markdown 格式
|
||||||
|
- 导出为 JSON 格式
|
||||||
|
- 导出为 PDF 格式
|
||||||
|
|
||||||
|
### 9.4 会话统计
|
||||||
|
|
||||||
|
- 显示总对话轮次
|
||||||
|
- 显示总 Token 使用量
|
||||||
|
- 显示平均响应时间
|
||||||
|
|
||||||
|
### 9.5 云端同步
|
||||||
|
|
||||||
|
- 支持将会话数据同步到云端
|
||||||
|
- 支持多设备访问
|
||||||
|
- 支持团队协作
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 总结
|
||||||
|
|
||||||
|
IC Coder 的会话存储系统采用文件系统存储方案,具有以下优势:
|
||||||
|
|
||||||
|
1. **简单可靠**:无需额外的数据库依赖
|
||||||
|
2. **易于备份**:直接复制文件即可备份
|
||||||
|
3. **跨平台**:支持 Windows、macOS、Linux
|
||||||
|
4. **可扩展**:易于添加新的数据字段
|
||||||
|
5. **高性能**:分页加载,避免一次性加载大量数据
|
||||||
|
|
||||||
|
系统已经实现了核心的会话管理功能,包括任务创建、消息保存、历史查询等,为用户提供了完整的会话历史管理体验。
|
||||||
1027
docs/数据流程详解.md
Normal file
|
Before Width: | Height: | Size: 160 KiB |
BIN
media/description/auto-build-architecture-copy.png
Normal file
|
After Width: | Height: | Size: 168 KiB |
BIN
media/description/auto-build-architecture.png
Normal file
|
After Width: | Height: | Size: 81 KiB |
BIN
media/description/auto-simulation-1.png
Normal file
|
After Width: | Height: | Size: 212 KiB |
BIN
media/description/auto-simulation-2.png
Normal file
|
After Width: | Height: | Size: 251 KiB |
BIN
media/description/auto-simulation-3.png
Normal file
|
After Width: | Height: | Size: 266 KiB |
BIN
media/description/input-requirement-1.png
Normal file
|
After Width: | Height: | Size: 113 KiB |
BIN
media/description/input-requirement-2.png
Normal file
|
After Width: | Height: | Size: 188 KiB |
BIN
media/description/input-requirement-3.png
Normal file
|
After Width: | Height: | Size: 242 KiB |
BIN
media/description/plan-design-doc-1.png
Normal file
|
After Width: | Height: | Size: 349 KiB |
BIN
media/description/plan-design-doc-2.png
Normal file
|
After Width: | Height: | Size: 302 KiB |
BIN
media/description/real-time-follow-1.png
Normal file
|
After Width: | Height: | Size: 157 KiB |
BIN
media/description/real-time-follow-2.png
Normal file
|
After Width: | Height: | Size: 305 KiB |
BIN
media/description/real-time-follow-3.png
Normal file
|
After Width: | Height: | Size: 253 KiB |
BIN
media/description/real-time-follow-4.png
Normal file
|
After Width: | Height: | Size: 209 KiB |
BIN
media/homepage-logo.png
Normal file
|
After Width: | Height: | Size: 252 KiB |
|
Before Width: | Height: | Size: 889 KiB After Width: | Height: | Size: 889 KiB |
|
Before Width: | Height: | Size: 681 B After Width: | Height: | Size: 681 B |
200
media/surfer/index.html
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
|
|
||||||
|
<!-- Disable zooming: -->
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<!-- change this to your project name -->
|
||||||
|
<title>Surfer</title>
|
||||||
|
|
||||||
|
<!-- config for our rust wasm binary. go to https://trunkrs.dev/assets/#rust for more customization -->
|
||||||
|
<script type="module">
|
||||||
|
import init from '/surfer.js';
|
||||||
|
await init({module_or_path: '/surfer_bg.wasm'});
|
||||||
|
import {WebHandle, inject_message, id_of_name, draw_text_arrow} from '/surfer.js';
|
||||||
|
window.inject_message = inject_message;
|
||||||
|
window.id_of_name = id_of_name;
|
||||||
|
window.draw_text_arrow = draw_text_arrow;
|
||||||
|
/*SURFER_SETUP_HOOKS*/
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!-- this is the base url relative to which other urls will be constructed. trunk will insert this from the public-url option -->
|
||||||
|
<base href="/" />
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function on_surfer_error(msg) {
|
||||||
|
console.log("Setting error message")
|
||||||
|
document.getElementById("error_message").innerHTML = msg
|
||||||
|
document.getElementById("error_container").style.display = "block"
|
||||||
|
}
|
||||||
|
window.on_surfer_error = on_surfer_error;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<link rel="manifest" href="manifest.json">
|
||||||
|
<meta name="theme-color" media="(prefers-color-scheme: light)" content="white">
|
||||||
|
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#404040">
|
||||||
|
|
||||||
|
<style>
|
||||||
|
html {
|
||||||
|
/* Remove touch delay: */
|
||||||
|
touch-action: manipulation;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
/* Light mode background color for what is not covered by the egui canvas,
|
||||||
|
or where the egui canvas is translucent. */
|
||||||
|
background: #909090;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
body {
|
||||||
|
/* Dark mode background color for what is not covered by the egui canvas,
|
||||||
|
or where the egui canvas is translucent. */
|
||||||
|
background: #404040;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allow canvas to fill entire web page: */
|
||||||
|
html,
|
||||||
|
body {
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 0 !important;
|
||||||
|
padding: 0 !important;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Make canvas fill entire document: */
|
||||||
|
canvas {
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: auto;
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.centered {
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: auto;
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
color: #f0f0f0;
|
||||||
|
font-size: 24px;
|
||||||
|
font-family: Ubuntu-Light, Helvetica, sans-serif;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------- */
|
||||||
|
/* Loading animation from https://loading.io/css/ */
|
||||||
|
.lds-dual-ring {
|
||||||
|
display: inline-block;
|
||||||
|
width: 24px;
|
||||||
|
height: 24px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lds-dual-ring:after {
|
||||||
|
content: " ";
|
||||||
|
display: block;
|
||||||
|
width: 24px;
|
||||||
|
height: 24px;
|
||||||
|
margin: 0px;
|
||||||
|
border-radius: 50%;
|
||||||
|
border: 3px solid #fff;
|
||||||
|
border-color: #fff transparent #fff transparent;
|
||||||
|
animation: lds-dual-ring 1.2s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes lds-dual-ring {
|
||||||
|
0% {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_container {
|
||||||
|
padding: 1em;
|
||||||
|
border-radius: 0.5em;
|
||||||
|
margin: 0px auto;
|
||||||
|
max-width: 980px;
|
||||||
|
color: #ffffff;
|
||||||
|
background-color: black;
|
||||||
|
position: relative;
|
||||||
|
height: 90%;
|
||||||
|
overflow: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_container a {
|
||||||
|
color: #ff9999;
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_message {
|
||||||
|
overflow: scroll;
|
||||||
|
white-space: break-spaces;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<link rel="modulepreload" href="/surfer.js" crossorigin="anonymous" integrity="sha384-s5jcnzgSMjwjfa1Jq5kr3vQVXGQ7D+ZdMsCBdbbcmKefqvRKw652YAYaaHZJQob6"><link rel="preload" href="/surfer_bg.wasm" crossorigin="anonymous" integrity="sha384-YzYZZQJDXiKIAVpyBMziailnMHJ/sxzBq0VNMP854yLbTd2lneCR5ZgcvB4cYMFc" as="fetch" type="application/wasm"></head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<!-- The WASM code will resize the canvas dynamically -->
|
||||||
|
<!-- the id is hardcoded in main.rs . so, make sure both match. -->
|
||||||
|
<canvas id="the_canvas_id"></canvas>
|
||||||
|
|
||||||
|
<div id="error_container" style="display: none;">
|
||||||
|
<h1>Sorry, Surfer crashed 🔥</h1>
|
||||||
|
<p>
|
||||||
|
Something caused Surfer to crash. Please report the error on
|
||||||
|
<a href="https://gitlab.com/surfer-project/surfer/-/issues/new">
|
||||||
|
gitlab
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Any report is appreciated, but it is extra helpful if you can attach the waveform that caused
|
||||||
|
the crash and/or the steps to reproduce the crash.
|
||||||
|
</p>
|
||||||
|
<h3>
|
||||||
|
Backtrace:
|
||||||
|
</h3>
|
||||||
|
<div class="error_container">
|
||||||
|
<!-- This is filled in by javascript -->
|
||||||
|
<code id="error_message"></code>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Register the message listener system -->
|
||||||
|
<script src="integration.js"></script>
|
||||||
|
<script>
|
||||||
|
register_message_listener()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!--Register Service Worker. this will cache the wasm / js scripts for offline use (for PWA functionality). -->
|
||||||
|
<!-- Force refresh (Ctrl + F5) to load the latest files instead of cached files -->
|
||||||
|
<script>
|
||||||
|
// We disable caching during development so that we always view the latest version.
|
||||||
|
if ('serviceWorker' in navigator && window.location.hash !== "#dev") {
|
||||||
|
window.addEventListener('load', function () {
|
||||||
|
navigator.serviceWorker.register('sw.js');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
||||||
|
<!-- Powered by egui: https://github.com/emilk/egui/ -->
|
||||||
65
media/surfer/integration.js
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
// Web apps which integrate Surfer as an iframe can give commands to surfer via
|
||||||
|
// the .postMessage [1] function on the iframe.
|
||||||
|
//
|
||||||
|
// For example, to tell Surfer to load waveforms from a URL, use
|
||||||
|
// `.postMessage({command: "LoadUrl", url: "https://app.surfer-project.org/picorv32.vcd"})`
|
||||||
|
//
|
||||||
|
// For more complex functionality, one can also inject any `Message` defined
|
||||||
|
// in `surfer::Message` in surfer/main.rs. However, the API of these messages
|
||||||
|
// is not stable and may change at any time. If you add functionality via
|
||||||
|
// these, make sure to test the new functionality when changing Surfer version.
|
||||||
|
//
|
||||||
|
// [1] https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
|
||||||
|
|
||||||
|
function register_message_listener() {
|
||||||
|
window.addEventListener("message", (event) => {
|
||||||
|
// JSON decode the message
|
||||||
|
const decoded = event.data
|
||||||
|
|
||||||
|
switch (decoded.command) {
|
||||||
|
// Load a waveform from a URL. The format is inferred from the data.
|
||||||
|
// Example: `{command: "LoadUrl", url: "https://app.surfer-project.org/picorv32.vcd"}`
|
||||||
|
|
||||||
|
case 'LoadUrl': {
|
||||||
|
const msg = {
|
||||||
|
LoadWaveformFileFromUrl: [
|
||||||
|
decoded.url,
|
||||||
|
"Clear"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
inject_message(JSON.stringify(msg))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'ToggleMenu': {
|
||||||
|
const msg = "ToggleMenu"
|
||||||
|
inject_message(JSON.stringify(msg))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load waveform data directly from string content
|
||||||
|
case 'LoadData': {
|
||||||
|
const msg = {
|
||||||
|
LoadFromData: [
|
||||||
|
decoded.content,
|
||||||
|
decoded.fileName || "waveform.vcd",
|
||||||
|
"Clear"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
inject_message(JSON.stringify(msg))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inject any other message supported by Surfer in the surfer::Message enum.
|
||||||
|
// NOTE: The API of these is unstable.
|
||||||
|
case 'InjectMessage': {
|
||||||
|
inject_message(decoded.message);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.log(`Unknown message.command ${decoded.command}`)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
10
media/surfer/manifest.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"background_color": "white",
|
||||||
|
"display": "standalone",
|
||||||
|
"id": "/index.html",
|
||||||
|
"lang": "en-US",
|
||||||
|
"name": "Surfer",
|
||||||
|
"short_name": "surfer",
|
||||||
|
"start_url": "./index.html",
|
||||||
|
"theme_color": "white"
|
||||||
|
}
|
||||||
2227
media/surfer/surfer.js
Normal file
200
media/surfer/surfer/index.html
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
|
|
||||||
|
<!-- Disable zooming: -->
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<!-- change this to your project name -->
|
||||||
|
<title>Surfer</title>
|
||||||
|
|
||||||
|
<!-- config for our rust wasm binary. go to https://trunkrs.dev/assets/#rust for more customization -->
|
||||||
|
<script type="module">
|
||||||
|
import init from '/surfer.js';
|
||||||
|
await init({module_or_path: '/surfer_bg.wasm'});
|
||||||
|
import {WebHandle, inject_message, id_of_name, draw_text_arrow} from '/surfer.js';
|
||||||
|
window.inject_message = inject_message;
|
||||||
|
window.id_of_name = id_of_name;
|
||||||
|
window.draw_text_arrow = draw_text_arrow;
|
||||||
|
/*SURFER_SETUP_HOOKS*/
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!-- this is the base url relative to which other urls will be constructed. trunk will insert this from the public-url option -->
|
||||||
|
<base href="/" />
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function on_surfer_error(msg) {
|
||||||
|
console.log("Setting error message")
|
||||||
|
document.getElementById("error_message").innerHTML = msg
|
||||||
|
document.getElementById("error_container").style.display = "block"
|
||||||
|
}
|
||||||
|
window.on_surfer_error = on_surfer_error;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<link rel="manifest" href="manifest.json">
|
||||||
|
<meta name="theme-color" media="(prefers-color-scheme: light)" content="white">
|
||||||
|
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#404040">
|
||||||
|
|
||||||
|
<style>
|
||||||
|
html {
|
||||||
|
/* Remove touch delay: */
|
||||||
|
touch-action: manipulation;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
/* Light mode background color for what is not covered by the egui canvas,
|
||||||
|
or where the egui canvas is translucent. */
|
||||||
|
background: #909090;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
body {
|
||||||
|
/* Dark mode background color for what is not covered by the egui canvas,
|
||||||
|
or where the egui canvas is translucent. */
|
||||||
|
background: #404040;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allow canvas to fill entire web page: */
|
||||||
|
html,
|
||||||
|
body {
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 0 !important;
|
||||||
|
padding: 0 !important;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Make canvas fill entire document: */
|
||||||
|
canvas {
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: auto;
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.centered {
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: auto;
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
color: #f0f0f0;
|
||||||
|
font-size: 24px;
|
||||||
|
font-family: Ubuntu-Light, Helvetica, sans-serif;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------- */
|
||||||
|
/* Loading animation from https://loading.io/css/ */
|
||||||
|
.lds-dual-ring {
|
||||||
|
display: inline-block;
|
||||||
|
width: 24px;
|
||||||
|
height: 24px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lds-dual-ring:after {
|
||||||
|
content: " ";
|
||||||
|
display: block;
|
||||||
|
width: 24px;
|
||||||
|
height: 24px;
|
||||||
|
margin: 0px;
|
||||||
|
border-radius: 50%;
|
||||||
|
border: 3px solid #fff;
|
||||||
|
border-color: #fff transparent #fff transparent;
|
||||||
|
animation: lds-dual-ring 1.2s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes lds-dual-ring {
|
||||||
|
0% {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_container {
|
||||||
|
padding: 1em;
|
||||||
|
border-radius: 0.5em;
|
||||||
|
margin: 0px auto;
|
||||||
|
max-width: 980px;
|
||||||
|
color: #ffffff;
|
||||||
|
background-color: black;
|
||||||
|
position: relative;
|
||||||
|
height: 90%;
|
||||||
|
overflow: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_container a {
|
||||||
|
color: #ff9999;
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_message {
|
||||||
|
overflow: scroll;
|
||||||
|
white-space: break-spaces;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<link rel="modulepreload" href="/surfer.js" crossorigin="anonymous" integrity="sha384-s5jcnzgSMjwjfa1Jq5kr3vQVXGQ7D+ZdMsCBdbbcmKefqvRKw652YAYaaHZJQob6"><link rel="preload" href="/surfer_bg.wasm" crossorigin="anonymous" integrity="sha384-YzYZZQJDXiKIAVpyBMziailnMHJ/sxzBq0VNMP854yLbTd2lneCR5ZgcvB4cYMFc" as="fetch" type="application/wasm"></head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<!-- The WASM code will resize the canvas dynamically -->
|
||||||
|
<!-- the id is hardcoded in main.rs . so, make sure both match. -->
|
||||||
|
<canvas id="the_canvas_id"></canvas>
|
||||||
|
|
||||||
|
<div id="error_container" style="display: none;">
|
||||||
|
<h1>Sorry, Surfer crashed 🔥</h1>
|
||||||
|
<p>
|
||||||
|
Something caused Surfer to crash. Please report the error on
|
||||||
|
<a href="https://gitlab.com/surfer-project/surfer/-/issues/new">
|
||||||
|
gitlab
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Any report is appreciated, but it is extra helpful if you can attach the waveform that caused
|
||||||
|
the crash and/or the steps to reproduce the crash.
|
||||||
|
</p>
|
||||||
|
<h3>
|
||||||
|
Backtrace:
|
||||||
|
</h3>
|
||||||
|
<div class="error_container">
|
||||||
|
<!-- This is filled in by javascript -->
|
||||||
|
<code id="error_message"></code>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Register the message listener system -->
|
||||||
|
<script src="integration.js"></script>
|
||||||
|
<script>
|
||||||
|
register_message_listener()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!--Register Service Worker. this will cache the wasm / js scripts for offline use (for PWA functionality). -->
|
||||||
|
<!-- Force refresh (Ctrl + F5) to load the latest files instead of cached files -->
|
||||||
|
<script>
|
||||||
|
// We disable caching during development so that we always view the latest version.
|
||||||
|
if ('serviceWorker' in navigator && window.location.hash !== "#dev") {
|
||||||
|
window.addEventListener('load', function () {
|
||||||
|
navigator.serviceWorker.register('sw.js');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
||||||
|
<!-- Powered by egui: https://github.com/emilk/egui/ -->
|
||||||
52
media/surfer/surfer/integration.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Web apps which integrate Surfer as an iframe can give commands to surfer via
|
||||||
|
// the .postMessage [1] function on the iframe.
|
||||||
|
//
|
||||||
|
// For example, to tell Surfer to load waveforms from a URL, use
|
||||||
|
// `.postMessage({command: "LoadUrl", url: "https://app.surfer-project.org/picorv32.vcd"})`
|
||||||
|
//
|
||||||
|
// For more complex functionality, one can also inject any `Message` defined
|
||||||
|
// in `surfer::Message` in surfer/main.rs. However, the API of these messages
|
||||||
|
// is not stable and may change at any time. If you add functionality via
|
||||||
|
// these, make sure to test the new functionality when changing Surfer version.
|
||||||
|
//
|
||||||
|
// [1] https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
|
||||||
|
|
||||||
|
function register_message_listener() {
|
||||||
|
window.addEventListener("message", (event) => {
|
||||||
|
// JSON decode the message
|
||||||
|
const decoded = event.data
|
||||||
|
|
||||||
|
switch (decoded.command) {
|
||||||
|
// Load a waveform from a URL. The format is inferred from the data.
|
||||||
|
// Example: `{command: "LoadUrl", url: "https://app.surfer-project.org/picorv32.vcd"}`
|
||||||
|
|
||||||
|
case 'LoadUrl': {
|
||||||
|
const msg = {
|
||||||
|
LoadWaveformFileFromUrl: [
|
||||||
|
decoded.url,
|
||||||
|
"Clear"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
inject_message(JSON.stringify(msg))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'ToggleMenu': {
|
||||||
|
const msg = "ToggleMenu"
|
||||||
|
inject_message(JSON.stringify(msg))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inject any other message supported by Surfer in the surfer::Message enum.
|
||||||
|
// NOTE: The API of these is unstable.
|
||||||
|
case 'InjectMessage': {
|
||||||
|
inject_message(decoded.message);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.log(`Unknown message.command ${decoded.command}`)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
10
media/surfer/surfer/manifest.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"background_color": "white",
|
||||||
|
"display": "standalone",
|
||||||
|
"id": "/index.html",
|
||||||
|
"lang": "en-US",
|
||||||
|
"name": "Surfer",
|
||||||
|
"short_name": "surfer",
|
||||||
|
"start_url": "./index.html",
|
||||||
|
"theme_color": "white"
|
||||||
|
}
|
||||||
2227
media/surfer/surfer/surfer.js
Normal file
BIN
media/surfer/surfer/surfer_bg.wasm
Normal file
37
media/surfer/surfer/sw.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
self.addEventListener("install", function () {
|
||||||
|
self.skipWaiting();
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener("activate", (event) => {
|
||||||
|
event.waitUntil(self.clients.claim());
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener("fetch", function (event) {
|
||||||
|
if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.respondWith(
|
||||||
|
fetch(event.request)
|
||||||
|
.then(function (response) {
|
||||||
|
// It seems like we only need to set the headers for index.html
|
||||||
|
// If you want to be on the safe side, comment this out
|
||||||
|
// if (!response.url.includes("index.html")) return response;
|
||||||
|
|
||||||
|
const newHeaders = new Headers(response.headers);
|
||||||
|
newHeaders.set("Cross-Origin-Embedder-Policy", "require-corp");
|
||||||
|
newHeaders.set("Cross-Origin-Opener-Policy", "same-origin");
|
||||||
|
|
||||||
|
const moddedResponse = new Response(response.body, {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
|
||||||
|
return moddedResponse;
|
||||||
|
})
|
||||||
|
.catch(function (e) {
|
||||||
|
console.error(e);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
BIN
media/surfer/surfer_bg.wasm
Normal file
37
media/surfer/sw.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
self.addEventListener("install", function () {
|
||||||
|
self.skipWaiting();
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener("activate", (event) => {
|
||||||
|
event.waitUntil(self.clients.claim());
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener("fetch", function (event) {
|
||||||
|
if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.respondWith(
|
||||||
|
fetch(event.request)
|
||||||
|
.then(function (response) {
|
||||||
|
// It seems like we only need to set the headers for index.html
|
||||||
|
// If you want to be on the safe side, comment this out
|
||||||
|
// if (!response.url.includes("index.html")) return response;
|
||||||
|
|
||||||
|
const newHeaders = new Headers(response.headers);
|
||||||
|
newHeaders.set("Cross-Origin-Embedder-Policy", "require-corp");
|
||||||
|
newHeaders.set("Cross-Origin-Opener-Policy", "same-origin");
|
||||||
|
|
||||||
|
const moddedResponse = new Response(response.body, {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
headers: newHeaders,
|
||||||
|
});
|
||||||
|
|
||||||
|
return moddedResponse;
|
||||||
|
})
|
||||||
|
.catch(function (e) {
|
||||||
|
console.error(e);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
101
package.json
@ -1,14 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "ic-coder-plugin",
|
"name": "iccoder",
|
||||||
"displayName": "IC Coder plugin",
|
"displayName": "IC Coder: Agentic Verilog Platform",
|
||||||
"description": "Agentic Verilog Coding Platform for Real-World FPGAs",
|
"description": "Agentic Verilog Coding Platform for Real-World FPGAs",
|
||||||
"version": "0.0.2",
|
"version": "1.0.3",
|
||||||
|
"publisher": "ICCoderAgenticVerilogPlatform",
|
||||||
"engines": {
|
"engines": {
|
||||||
"vscode": "^1.107.0"
|
"vscode": "^1.80.0"
|
||||||
},
|
},
|
||||||
"icon": "media/图案(方底).png",
|
"icon": "media/icon.png",
|
||||||
"categories": [
|
"categories": [
|
||||||
"Other"
|
"Chat",
|
||||||
|
"Programming Languages"
|
||||||
],
|
],
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IC",
|
"IC",
|
||||||
@ -18,6 +20,11 @@
|
|||||||
"eda",
|
"eda",
|
||||||
"assistant"
|
"assistant"
|
||||||
],
|
],
|
||||||
|
"license": "SEE LICENSE IN LICENSE",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.pengyejiatu.com/pengyejiatu/IC-Coder-Plugin.git"
|
||||||
|
},
|
||||||
"activationEvents": [
|
"activationEvents": [
|
||||||
"onCommand:ic-coder.openPanel",
|
"onCommand:ic-coder.openPanel",
|
||||||
"onView:ic-coder-sidebar",
|
"onView:ic-coder-sidebar",
|
||||||
@ -44,33 +51,8 @@
|
|||||||
"category": "IC Coder"
|
"category": "IC Coder"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "ic-coder.viewHistory",
|
"command": "ic-coder.testNotification",
|
||||||
"title": "查看会话历史",
|
"title": "测试系统通知",
|
||||||
"category": "IC Coder"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"command": "ic-coder.newSession",
|
|
||||||
"title": "新建会话",
|
|
||||||
"category": "IC Coder"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"command": "ic-coder.exportSession",
|
|
||||||
"title": "导出当前会话",
|
|
||||||
"category": "IC Coder"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"command": "ic-coder.deleteSession",
|
|
||||||
"title": "删除会话",
|
|
||||||
"category": "IC Coder"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"command": "ic-coder.clearHistory",
|
|
||||||
"title": "清空会话历史",
|
|
||||||
"category": "IC Coder"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"command": "ic-coder.searchSession",
|
|
||||||
"title": "搜索会话",
|
|
||||||
"category": "IC Coder"
|
"category": "IC Coder"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -79,7 +61,7 @@
|
|||||||
{
|
{
|
||||||
"id": "ic-coder-sidebar",
|
"id": "ic-coder-sidebar",
|
||||||
"title": "IC Coder",
|
"title": "IC Coder",
|
||||||
"icon": "media/侧边栏logo.png"
|
"icon": "media/sidebar-icon.png"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -91,6 +73,46 @@
|
|||||||
"type": "webview"
|
"type": "webview"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"authentication": [
|
||||||
|
{
|
||||||
|
"id": "iccoder",
|
||||||
|
"label": "IC Coder"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"customEditors": [
|
||||||
|
{
|
||||||
|
"viewType": "ic-coder.vcdViewer",
|
||||||
|
"displayName": "VCD 波形查看器",
|
||||||
|
"selector": [
|
||||||
|
{
|
||||||
|
"filenamePattern": "*.vcd"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"priority": "default"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"configuration": {
|
||||||
|
"title": "IC Coder",
|
||||||
|
"properties": {
|
||||||
|
"ic-coder.enableSystemNotification": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"description": "启用系统级通知(任务完成时显示操作系统通知)"
|
||||||
|
},
|
||||||
|
"ic-coder.notificationSound": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"description": "通知时播放系统声音"
|
||||||
|
},
|
||||||
|
"ic-coder.notificationTimeout": {
|
||||||
|
"type": "number",
|
||||||
|
"default": 10,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 60,
|
||||||
|
"description": "通知自动消失时间(秒),0 表示不自动消失"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -108,9 +130,11 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/mocha": "^10.0.10",
|
"@types/mocha": "^10.0.10",
|
||||||
"@types/node": "22.x",
|
"@types/node": "22.x",
|
||||||
"@types/vscode": "^1.107.0",
|
"@types/node-notifier": "^8.0.5",
|
||||||
|
"@types/vscode": "^1.80.0",
|
||||||
"@vscode/test-cli": "^0.0.12",
|
"@vscode/test-cli": "^0.0.12",
|
||||||
"@vscode/test-electron": "^2.5.2",
|
"@vscode/test-electron": "^2.5.2",
|
||||||
|
"@vscode/vsce": "^3.7.1",
|
||||||
"eslint": "^9.39.1",
|
"eslint": "^9.39.1",
|
||||||
"ts-loader": "^9.5.4",
|
"ts-loader": "^9.5.4",
|
||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3",
|
||||||
@ -121,11 +145,16 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"media",
|
"media",
|
||||||
"tools"
|
"tools",
|
||||||
|
"src/assets",
|
||||||
|
"LICENSE",
|
||||||
|
"CHANGELOG.md"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@wavedrom/doppler": "^1.14.0",
|
"@wavedrom/doppler": "^1.14.0",
|
||||||
|
"eventsource-parser": "^3.0.6",
|
||||||
"iconv-lite": "^0.7.1",
|
"iconv-lite": "^0.7.1",
|
||||||
|
"node-notifier": "^10.0.1",
|
||||||
"onml": "^2.1.0",
|
"onml": "^2.1.0",
|
||||||
"style-mod": "^4.1.3",
|
"style-mod": "^4.1.3",
|
||||||
"vcd-stream": "^1.5.0",
|
"vcd-stream": "^1.5.0",
|
||||||
|
|||||||
2003
pnpm-lock.yaml
generated
BIN
rustup-init.exe
Normal file
BIN
src/assets/QRCode/wx.png
Normal file
|
After Width: | Height: | Size: 119 KiB |
BIN
src/assets/model/Auto.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
src/assets/model/Max.png
Normal file
|
After Width: | Height: | Size: 2.4 KiB |
BIN
src/assets/model/Sy.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
src/assets/model/lite.png
Normal file
|
After Width: | Height: | Size: 112 KiB |
BIN
src/assets/titleIcon/PRO+.png
Normal file
|
After Width: | Height: | Size: 1.3 MiB |
BIN
src/assets/titleIcon/PRO-Try.png
Normal file
|
After Width: | Height: | Size: 302 KiB |
BIN
src/assets/titleIcon/PRO.png
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
BIN
src/assets/titleIcon/free.png
Normal file
|
After Width: | Height: | Size: 138 KiB |
237
src/components/codeHighlight.ts
Normal file
@ -0,0 +1,237 @@
|
|||||||
|
/**
|
||||||
|
* 代码高亮组件
|
||||||
|
*
|
||||||
|
* 功能说明:
|
||||||
|
* - 使用 highlight.js 提供专业的代码语法高亮
|
||||||
|
* - 支持多种编程语言(Verilog, JavaScript, Python 等)
|
||||||
|
* - 提供行内代码和代码块的不同样式
|
||||||
|
* - 自动检测语言类型
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 highlight.js 的 CDN 链接
|
||||||
|
*/
|
||||||
|
export function getHighlightJsLinks(): string {
|
||||||
|
return `
|
||||||
|
<!-- Highlight.js CSS (VS Code Dark+ 主题) -->
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/vs2015.min.css">
|
||||||
|
<!-- Highlight.js 核心库 -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
||||||
|
<!-- Verilog 语言支持 -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/verilog.min.js"></script>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取代码高亮的样式
|
||||||
|
*/
|
||||||
|
export function getCodeHighlightStyles(): string {
|
||||||
|
return `
|
||||||
|
/* 代码块基础样式 */
|
||||||
|
.segment-text pre {
|
||||||
|
background: var(--vscode-textCodeBlock-background);
|
||||||
|
border: 1px solid var(--vscode-panel-border);
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 12px;
|
||||||
|
overflow-x: auto;
|
||||||
|
margin: 12px 0;
|
||||||
|
position: relative;
|
||||||
|
white-space: pre;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segment-text pre code {
|
||||||
|
background: transparent !important;
|
||||||
|
padding: 0;
|
||||||
|
border: none;
|
||||||
|
display: block;
|
||||||
|
line-height: 1.5;
|
||||||
|
white-space: pre;
|
||||||
|
font-family: 'Courier New', Consolas, 'Monaco', monospace;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 行内代码样式 */
|
||||||
|
.segment-text code:not(pre code) {
|
||||||
|
background: var(--vscode-textCodeBlock-background);
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: 3px;
|
||||||
|
color: var(--vscode-textPreformat-foreground);
|
||||||
|
border: 1px solid var(--vscode-panel-border);
|
||||||
|
font-family: 'Courier New', Consolas, 'Monaco', monospace;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 覆盖 highlight.js 的背景色,使用 VSCode 主题色 */
|
||||||
|
.segment-text pre code.hljs {
|
||||||
|
background: transparent !important;
|
||||||
|
padding: 0 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 代码块语言标签 */
|
||||||
|
.code-block-wrapper {
|
||||||
|
position: relative;
|
||||||
|
margin: -20px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.code-language-label {
|
||||||
|
position: absolute;
|
||||||
|
top: 8px;
|
||||||
|
right: 8px;
|
||||||
|
background: var(--vscode-badge-background);
|
||||||
|
color: var(--vscode-badge-foreground);
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 3px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 500;
|
||||||
|
text-transform: uppercase;
|
||||||
|
opacity: 0.8;
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 代码块复制按钮 */
|
||||||
|
.code-copy-btn {
|
||||||
|
position: absolute;
|
||||||
|
top: 8px;
|
||||||
|
right: 8px;
|
||||||
|
background: var(--vscode-button-secondaryBackground);
|
||||||
|
color: var(--vscode-button-secondaryForeground);
|
||||||
|
border: 1px solid var(--vscode-button-border);
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 4px 8px;
|
||||||
|
font-size: 11px;
|
||||||
|
cursor: pointer;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.2s ease;
|
||||||
|
z-index: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.code-block-wrapper:hover .code-copy-btn {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.code-copy-btn:hover {
|
||||||
|
background: var(--vscode-button-secondaryHoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.code-copy-btn.copied {
|
||||||
|
background: var(--vscode-button-background);
|
||||||
|
color: var(--vscode-button-foreground);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 代码块滚动条样式 */
|
||||||
|
.segment-text pre::-webkit-scrollbar {
|
||||||
|
height: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segment-text pre::-webkit-scrollbar-track {
|
||||||
|
background: var(--vscode-scrollbarSlider-background);
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segment-text pre::-webkit-scrollbar-thumb {
|
||||||
|
background: var(--vscode-scrollbarSlider-hoverBackground);
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segment-text pre::-webkit-scrollbar-thumb:hover {
|
||||||
|
background: var(--vscode-scrollbarSlider-activeBackground);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取代码高亮的脚本
|
||||||
|
*/
|
||||||
|
export function getCodeHighlightScript(): string {
|
||||||
|
return `
|
||||||
|
/**
|
||||||
|
* 使用 highlight.js 进行代码高亮
|
||||||
|
*/
|
||||||
|
function highlightCodeBlocks() {
|
||||||
|
// 等待 highlight.js 加载完成
|
||||||
|
if (typeof hljs === 'undefined') {
|
||||||
|
setTimeout(highlightCodeBlocks, 100);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const codeBlocks = document.querySelectorAll('.segment-text pre code:not(.hljs)');
|
||||||
|
codeBlocks.forEach((block) => {
|
||||||
|
hljs.highlightElement(block);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为代码块添加复制按钮
|
||||||
|
*/
|
||||||
|
function enhanceCodeBlocks() {
|
||||||
|
const codeBlocks = document.querySelectorAll('.segment-text pre code');
|
||||||
|
|
||||||
|
codeBlocks.forEach((codeElement) => {
|
||||||
|
const preElement = codeElement.parentElement;
|
||||||
|
if (!preElement || preElement.classList.contains('enhanced')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 标记为已增强,避免重复处理
|
||||||
|
preElement.classList.add('enhanced');
|
||||||
|
|
||||||
|
// 应用语法高亮
|
||||||
|
if (typeof hljs !== 'undefined' && !codeElement.classList.contains('hljs')) {
|
||||||
|
hljs.highlightElement(codeElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建包装器
|
||||||
|
const wrapper = document.createElement('div');
|
||||||
|
wrapper.className = 'code-block-wrapper';
|
||||||
|
preElement.parentNode.insertBefore(wrapper, preElement);
|
||||||
|
wrapper.appendChild(preElement);
|
||||||
|
|
||||||
|
// 添加复制按钮
|
||||||
|
const copyBtn = document.createElement('button');
|
||||||
|
copyBtn.className = 'code-copy-btn';
|
||||||
|
copyBtn.textContent = '复制';
|
||||||
|
copyBtn.onclick = function() {
|
||||||
|
const code = codeElement.textContent;
|
||||||
|
navigator.clipboard.writeText(code).then(() => {
|
||||||
|
copyBtn.textContent = '已复制';
|
||||||
|
copyBtn.classList.add('copied');
|
||||||
|
setTimeout(() => {
|
||||||
|
copyBtn.textContent = '复制';
|
||||||
|
copyBtn.classList.remove('copied');
|
||||||
|
}, 2000);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
wrapper.appendChild(copyBtn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听 DOM 变化,自动增强新添加的代码块
|
||||||
|
*/
|
||||||
|
function observeCodeBlocks() {
|
||||||
|
const observer = new MutationObserver((mutations) => {
|
||||||
|
mutations.forEach((mutation) => {
|
||||||
|
if (mutation.addedNodes.length > 0) {
|
||||||
|
enhanceCodeBlocks();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(document.getElementById('messages'), {
|
||||||
|
childList: true,
|
||||||
|
subtree: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化代码块增强
|
||||||
|
if (document.readyState === 'loading') {
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
enhanceCodeBlocks();
|
||||||
|
observeCodeBlocks();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
enhanceCodeBlocks();
|
||||||
|
observeCodeBlocks();
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
99
src/config/settings.ts
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
/**
|
||||||
|
* 配置管理
|
||||||
|
* 支持 dev(本地开发)和 test(测试服务器)两种环境
|
||||||
|
*/
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
|
/** 环境类型 */
|
||||||
|
type Environment = "dev" | "test" | "prod";
|
||||||
|
|
||||||
|
/** 当前环境 - 修改这里切换环境 */
|
||||||
|
const CURRENT_ENV: Environment = "prod";
|
||||||
|
|
||||||
|
/** 服务等级类型 */
|
||||||
|
export type ServiceTier = "lite" | "syntaxic" | "max" | "auto";
|
||||||
|
|
||||||
|
/** 配置项接口 */
|
||||||
|
export interface IccoderConfig {
|
||||||
|
/** 后端服务地址 */
|
||||||
|
backendUrl: string;
|
||||||
|
/** 登录页面地址 */
|
||||||
|
loginUrl: string;
|
||||||
|
/** 后端服务地址(strangeLoop) */
|
||||||
|
backendUrlStrongeLoop: string;
|
||||||
|
/** 请求超时时间(毫秒) */
|
||||||
|
timeout: number;
|
||||||
|
/** 用户ID(临时使用,后续对接认证) */
|
||||||
|
userId: string;
|
||||||
|
/** 服务等级 */
|
||||||
|
serviceTier: ServiceTier;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 环境配置 */
|
||||||
|
const ENV_CONFIG: Record<Environment, IccoderConfig> = {
|
||||||
|
/** 本地开发环境 - 通过 Gateway 路由 */
|
||||||
|
dev: {
|
||||||
|
backendUrl: "http://localhost:8080/iccoder",
|
||||||
|
backendUrlStrongeLoop: "http://localhost:8080",
|
||||||
|
loginUrl: "http://localhost/login",
|
||||||
|
timeout: 300000,
|
||||||
|
userId: "default-user",
|
||||||
|
serviceTier: "max", // 默认使用 max
|
||||||
|
},
|
||||||
|
/** 测试服务器环境 - 通过 Gateway 路由 */
|
||||||
|
test: {
|
||||||
|
backendUrl: "http://192.168.1.108:2029/iccoder",
|
||||||
|
backendUrlStrongeLoop: "http://192.168.1.108:2029",
|
||||||
|
loginUrl: "http://192.168.1.108:2005/login",
|
||||||
|
timeout: 60000,
|
||||||
|
userId: "default-user",
|
||||||
|
serviceTier: "max",
|
||||||
|
},
|
||||||
|
/** 生产环境 - 通过 Gateway 路由 */
|
||||||
|
prod: {
|
||||||
|
backendUrl: "https://api.iccoder.com",
|
||||||
|
backendUrlStrongeLoop: "http://192.168.1.115:2029",
|
||||||
|
loginUrl: "https://iccoder.com/login",
|
||||||
|
timeout: 60000,
|
||||||
|
userId: "default-user",
|
||||||
|
serviceTier: "auto",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前环境
|
||||||
|
*/
|
||||||
|
export function getCurrentEnv(): Environment {
|
||||||
|
return CURRENT_ENV;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置项
|
||||||
|
*/
|
||||||
|
export function getConfig(): IccoderConfig {
|
||||||
|
return { ...ENV_CONFIG[CURRENT_ENV] };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取后端 API 地址
|
||||||
|
*/
|
||||||
|
export function getApiUrl(path: string): string {
|
||||||
|
const { backendUrl } = getConfig();
|
||||||
|
const baseUrl = backendUrl.endsWith("/")
|
||||||
|
? backendUrl.slice(0, -1)
|
||||||
|
: backendUrl;
|
||||||
|
const apiPath = path.startsWith("/") ? path : `/${path}`;
|
||||||
|
return `${baseUrl}${apiPath}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 StrangeLoop 服务 API 地址(用于用户信息等)
|
||||||
|
*/
|
||||||
|
export function getStrangeLoopApiUrl(path: string): string {
|
||||||
|
const { backendUrlStrongeLoop } = getConfig();
|
||||||
|
const baseUrl = backendUrlStrongeLoop.endsWith("/")
|
||||||
|
? backendUrlStrongeLoop.slice(0, -1)
|
||||||
|
: backendUrlStrongeLoop;
|
||||||
|
const apiPath = path.startsWith("/") ? path : `/${path}`;
|
||||||
|
return `${baseUrl}${apiPath}`;
|
||||||
|
}
|
||||||
197
src/constants/toolIcons.ts
Normal file
215
src/extension.ts
@ -1,28 +1,97 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { ICViewProvider } from "./views/ICViewProvider";
|
import { ICViewProvider } from "./views/ICViewProvider";
|
||||||
import { showICHelperPanel } from "./panels/ICHelperPanel";
|
import { showICHelperPanel } from "./panels/ICHelperPanel";
|
||||||
import { VCDViewerPanel } from "./panels/VCDViewerPanel";
|
import { VCDViewerPanel, VCDViewerEditorProvider } from "./panels/VCDViewerPanel";
|
||||||
import { ChatHistoryManager } from "./utils/chatHistoryManager";
|
import { ChatHistoryManager } from "./utils/chatHistoryManager";
|
||||||
|
import { ICCoderAuthenticationProvider } from "./services/icCoderAuthProvider";
|
||||||
|
import { VCDFileServer } from "./services/vcdFileServer";
|
||||||
|
import { initUserService } from "./services/userService";
|
||||||
|
import { initCreditsService } from "./services/creditsService";
|
||||||
|
import { isTokenExpired } from "./utils/jwtUtils";
|
||||||
|
import { NotificationService } from "./services/notificationService";
|
||||||
|
import { InvitationService } from "./services/invitationService";
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export async function activate(context: vscode.ExtensionContext) {
|
||||||
console.log("🎉 IC Coder 插件已激活!");
|
console.log("🎉 IC Coder 插件已激活!");
|
||||||
|
|
||||||
// 自动打开聊天面板
|
// 初始化通知服务
|
||||||
vscode.commands.executeCommand("ic-coder.openChat");
|
const notificationService = NotificationService.getInstance(context);
|
||||||
|
console.log('[Extension] 通知服务已初始化');
|
||||||
|
|
||||||
|
// 【关键】在创建 AuthProvider 之前,先检查并清除过期的 session
|
||||||
|
const storedSessions = context.globalState.get<any[]>('icCoderSessions', []);
|
||||||
|
console.log('[Extension] 检查 sessions 数量:', storedSessions.length);
|
||||||
|
|
||||||
|
if (storedSessions.length > 0) {
|
||||||
|
const session = storedSessions[0];
|
||||||
|
const token = session.accessToken;
|
||||||
|
console.log('[Extension] 检查 token 是否过期...');
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
const expired = isTokenExpired(token);
|
||||||
|
console.log('[Extension] token 过期检查结果:', expired);
|
||||||
|
|
||||||
|
if (expired) {
|
||||||
|
// 必须等待清除完成后再创建 AuthProvider
|
||||||
|
await context.globalState.update('icCoderSessions', []);
|
||||||
|
await context.globalState.update('icCoderUserInfo', undefined);
|
||||||
|
console.log('[Extension] Token 已过期,已清除所有登录状态');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化用户服务
|
||||||
|
initUserService(context);
|
||||||
|
|
||||||
|
// 初始化 Credits 服务
|
||||||
|
initCreditsService(context);
|
||||||
|
|
||||||
|
// 初始化 VCD 文件服务器
|
||||||
|
const vcdFileServer = new VCDFileServer(context.extensionUri);
|
||||||
|
vcdFileServer.start().then((port) => {
|
||||||
|
console.log(`VCD 文件服务器已启动,端口: ${port}`);
|
||||||
|
}).catch((error) => {
|
||||||
|
console.error("启动 VCD 文件服务器失败:", error);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 在插件停用时关闭服务器
|
||||||
|
context.subscriptions.push({
|
||||||
|
dispose: () => vcdFileServer.stop()
|
||||||
|
});
|
||||||
|
|
||||||
|
// 注册 Authentication Provider(此时 icCoderSessions 已经被清除)
|
||||||
|
const authProvider = new ICCoderAuthenticationProvider(context);
|
||||||
|
context.subscriptions.push(
|
||||||
|
vscode.authentication.registerAuthenticationProvider(
|
||||||
|
"iccoder",
|
||||||
|
"IC Coder",
|
||||||
|
authProvider
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 检查登录状态,如果已登录则自动打开聊天面板
|
||||||
|
vscode.authentication.getSession("iccoder", [], { createIfNone: false })
|
||||||
|
.then((session) => {
|
||||||
|
if (session) {
|
||||||
|
vscode.commands.executeCommand("ic-coder.openChat");
|
||||||
|
}
|
||||||
|
}, () => {
|
||||||
|
// 未登录,不做任何操作
|
||||||
|
});
|
||||||
|
|
||||||
// 注册命令:打开助手面板
|
// 注册命令:打开助手面板
|
||||||
const openPanelCommand = vscode.commands.registerCommand(
|
const openPanelCommand = vscode.commands.registerCommand(
|
||||||
"ic-coder.openPanel",
|
"ic-coder.openPanel",
|
||||||
() => {
|
async () => {
|
||||||
showICHelperPanel(context);
|
await showICHelperPanel(context);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 注册命令:打开聊天(用于侧边栏)
|
// 注册命令:打开聊天(用于侧边栏)
|
||||||
const openChatCommand = vscode.commands.registerCommand(
|
const openChatCommand = vscode.commands.registerCommand(
|
||||||
"ic-coder.openChat",
|
"ic-coder.openChat",
|
||||||
() => {
|
async () => {
|
||||||
showICHelperPanel(context);
|
await showICHelperPanel(context);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -50,7 +119,122 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VCDViewerPanel.createOrShow(context.extensionUri, vcdFilePath);
|
VCDViewerPanel.createOrShow(context.extensionUri, vcdFilePath, vcdFileServer);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册命令:在浏览器中打开 VCD 波形查看器
|
||||||
|
const openVCDViewerInBrowserCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.openVCDViewerInBrowser",
|
||||||
|
async (vcdFilePath?: string) => {
|
||||||
|
if (!vcdFilePath) {
|
||||||
|
const fileUri = await vscode.window.showOpenDialog({
|
||||||
|
canSelectFiles: true,
|
||||||
|
canSelectFolders: false,
|
||||||
|
canSelectMany: false,
|
||||||
|
filters: {
|
||||||
|
"VCD 文件": ["vcd"],
|
||||||
|
"所有文件": ["*"],
|
||||||
|
},
|
||||||
|
title: "选择 VCD 文件",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (fileUri && fileUri[0]) {
|
||||||
|
vcdFilePath = fileUri[0].fsPath;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注册文件到服务器
|
||||||
|
const fileId = vcdFileServer.registerFile(vcdFilePath);
|
||||||
|
const viewerUrl = vcdFileServer.getViewerUrl(fileId);
|
||||||
|
|
||||||
|
// 在默认浏览器中打开
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse(viewerUrl));
|
||||||
|
vscode.window.showInformationMessage(`波形查看器已在浏览器中打开`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册命令:用户登录
|
||||||
|
const loginCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.login",
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
// 先清除 session 偏好,避免 VSCode 弹出"账户不一致"确认框
|
||||||
|
try {
|
||||||
|
await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
clearSessionPreference: true,
|
||||||
|
createIfNone: false
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// 忽略错误
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建新 session
|
||||||
|
await vscode.authentication.getSession("iccoder", [], { createIfNone: true });
|
||||||
|
} catch (error) {
|
||||||
|
vscode.window.showErrorMessage(`登录失败: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册命令:用户登出
|
||||||
|
const logoutCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.logout",
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
|
||||||
|
if (session) {
|
||||||
|
// 调用 authProvider 的 removeSession 方法
|
||||||
|
await authProvider.removeSession(session.id);
|
||||||
|
// 清除邀请码验证状态
|
||||||
|
await InvitationService.clearVerificationStatus(context);
|
||||||
|
} else {
|
||||||
|
vscode.window.showInformationMessage("当前未登录");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
vscode.window.showInformationMessage("当前未登录");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册命令:更换邀请码
|
||||||
|
const changeInvitationCodeCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.changeInvitationCode",
|
||||||
|
async () => {
|
||||||
|
const confirm = await vscode.window.showWarningMessage(
|
||||||
|
'确定要更换邀请码吗?',
|
||||||
|
'确定',
|
||||||
|
'取消'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (confirm === '确定') {
|
||||||
|
await InvitationService.clearVerificationStatus(context);
|
||||||
|
vscode.window.showInformationMessage('已清除邀请码,请重新验证');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册命令:测试系统通知
|
||||||
|
const testNotificationCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.testNotification",
|
||||||
|
() => {
|
||||||
|
console.log('[Extension] ========== 测试通知命令被调用 ==========');
|
||||||
|
|
||||||
|
// 先显示 VS Code 通知确认命令执行
|
||||||
|
vscode.window.showInformationMessage('正在测试系统通知...');
|
||||||
|
|
||||||
|
// 发送系统通知
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder - 测试通知',
|
||||||
|
'系统通知功能正常工作!',
|
||||||
|
() => {
|
||||||
|
vscode.window.showInformationMessage('您点击了系统通知!');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('[Extension] 测试通知命令执行完成');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -102,17 +286,25 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// 注册侧边栏视图
|
// 注册侧边栏视图
|
||||||
const viewProvider = new ICViewProvider(context.extensionUri);
|
const viewProvider = new ICViewProvider(context.extensionUri, context);
|
||||||
const viewRegistration = vscode.window.registerWebviewViewProvider(
|
const viewRegistration = vscode.window.registerWebviewViewProvider(
|
||||||
"ic-coder.mainView",
|
"ic-coder.mainView",
|
||||||
viewProvider
|
viewProvider
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 注册 VCD 自定义编辑器
|
||||||
|
const vcdEditorProvider = VCDViewerEditorProvider.register(context, vcdFileServer);
|
||||||
|
|
||||||
// 添加到订阅
|
// 添加到订阅
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
openPanelCommand,
|
openPanelCommand,
|
||||||
openChatCommand,
|
openChatCommand,
|
||||||
openVCDViewerCommand,
|
openVCDViewerCommand,
|
||||||
|
openVCDViewerInBrowserCommand,
|
||||||
|
loginCommand,
|
||||||
|
logoutCommand,
|
||||||
|
changeInvitationCodeCommand,
|
||||||
|
testNotificationCommand,
|
||||||
// TODO: 等待重新实现这些命令
|
// TODO: 等待重新实现这些命令
|
||||||
// viewHistoryCommand,
|
// viewHistoryCommand,
|
||||||
// newSessionCommand,
|
// newSessionCommand,
|
||||||
@ -120,7 +312,8 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
// deleteSessionCommand,
|
// deleteSessionCommand,
|
||||||
// clearHistoryCommand,
|
// clearHistoryCommand,
|
||||||
// searchSessionCommand,
|
// searchSessionCommand,
|
||||||
viewRegistration
|
viewRegistration,
|
||||||
|
vcdEditorProvider
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,19 +1,77 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
import { VCDFileServer } from "../services/vcdFileServer";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* VCD 波形查看器面板
|
* VCD 波形查看器自定义编辑器提供者
|
||||||
|
*/
|
||||||
|
export class VCDViewerEditorProvider implements vscode.CustomReadonlyEditorProvider {
|
||||||
|
public static register(context: vscode.ExtensionContext, vcdFileServer: VCDFileServer): vscode.Disposable {
|
||||||
|
const provider = new VCDViewerEditorProvider(context, vcdFileServer);
|
||||||
|
const providerRegistration = vscode.window.registerCustomEditorProvider(
|
||||||
|
"ic-coder.vcdViewer",
|
||||||
|
provider,
|
||||||
|
{
|
||||||
|
webviewOptions: {
|
||||||
|
retainContextWhenHidden: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return providerRegistration;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly context: vscode.ExtensionContext,
|
||||||
|
private readonly vcdFileServer: VCDFileServer
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async openCustomDocument(
|
||||||
|
uri: vscode.Uri,
|
||||||
|
openContext: vscode.CustomDocumentOpenContext,
|
||||||
|
token: vscode.CancellationToken
|
||||||
|
): Promise<vscode.CustomDocument> {
|
||||||
|
return {
|
||||||
|
uri,
|
||||||
|
dispose: () => {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async resolveCustomEditor(
|
||||||
|
document: vscode.CustomDocument,
|
||||||
|
webviewPanel: vscode.WebviewPanel,
|
||||||
|
token: vscode.CancellationToken
|
||||||
|
): Promise<void> {
|
||||||
|
webviewPanel.webview.options = {
|
||||||
|
enableScripts: true,
|
||||||
|
localResourceRoots: [this.context.extensionUri],
|
||||||
|
};
|
||||||
|
|
||||||
|
// 使用公共工厂方法创建 VCD 查看器实例
|
||||||
|
VCDViewerPanel.createFromWebviewPanel(
|
||||||
|
webviewPanel,
|
||||||
|
this.context.extensionUri,
|
||||||
|
document.uri.fsPath,
|
||||||
|
this.vcdFileServer
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VCD 波形查看器面板 (使用 Surfer)
|
||||||
*/
|
*/
|
||||||
export class VCDViewerPanel {
|
export class VCDViewerPanel {
|
||||||
public static currentPanel: VCDViewerPanel | undefined;
|
public static currentPanel: VCDViewerPanel | undefined;
|
||||||
private readonly _panel: vscode.WebviewPanel;
|
private readonly _panel: vscode.WebviewPanel;
|
||||||
private readonly _extensionUri: vscode.Uri;
|
private readonly _extensionUri: vscode.Uri;
|
||||||
private _disposables: vscode.Disposable[] = [];
|
private _disposables: vscode.Disposable[] = [];
|
||||||
|
private _currentVcdPath: string | undefined;
|
||||||
|
private _vcdFileServer: VCDFileServer | undefined;
|
||||||
|
|
||||||
private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
|
private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri, vcdFileServer?: VCDFileServer) {
|
||||||
this._panel = panel;
|
this._panel = panel;
|
||||||
this._extensionUri = extensionUri;
|
this._extensionUri = extensionUri;
|
||||||
|
this._vcdFileServer = vcdFileServer;
|
||||||
|
|
||||||
// 设置初始 HTML 内容
|
// 设置初始 HTML 内容
|
||||||
this._panel.webview.html = this._getLoadingHtml();
|
this._panel.webview.html = this._getLoadingHtml();
|
||||||
@ -24,12 +82,20 @@ export class VCDViewerPanel {
|
|||||||
// 监听来自 webview 的消息
|
// 监听来自 webview 的消息
|
||||||
this._panel.webview.onDidReceiveMessage(
|
this._panel.webview.onDidReceiveMessage(
|
||||||
(message) => {
|
(message) => {
|
||||||
|
console.log("[VCDViewerPanel] 收到消息:", message);
|
||||||
switch (message.command) {
|
switch (message.command) {
|
||||||
case "loadVCD":
|
case "loadVCD":
|
||||||
if (message.filePath) {
|
if (message.filePath) {
|
||||||
this.loadVCDFile(message.filePath);
|
this.loadVCDFile(message.filePath);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "loaded":
|
||||||
|
// Surfer iframe 加载完成,发送 VCD 文件
|
||||||
|
console.log("[VCDViewerPanel] Surfer 已加载,当前 VCD 路径:", this._currentVcdPath);
|
||||||
|
if (this._currentVcdPath) {
|
||||||
|
this.sendVcdToSurfer(this._currentVcdPath);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
@ -40,8 +106,9 @@ export class VCDViewerPanel {
|
|||||||
/**
|
/**
|
||||||
* 创建或显示 VCD 查看器面板
|
* 创建或显示 VCD 查看器面板
|
||||||
*/
|
*/
|
||||||
public static createOrShow(extensionUri: vscode.Uri, vcdFilePath?: string) {
|
public static createOrShow(extensionUri: vscode.Uri, vcdFilePath?: string, vcdFileServer?: VCDFileServer) {
|
||||||
const column = vscode.ViewColumn.One;
|
// 在当前活动编辑器旁边打开新列
|
||||||
|
const column = vscode.ViewColumn.Beside;
|
||||||
|
|
||||||
// 如果已经有面板打开,则显示它
|
// 如果已经有面板打开,则显示它
|
||||||
if (VCDViewerPanel.currentPanel) {
|
if (VCDViewerPanel.currentPanel) {
|
||||||
@ -64,7 +131,7 @@ export class VCDViewerPanel {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
VCDViewerPanel.currentPanel = new VCDViewerPanel(panel, extensionUri);
|
VCDViewerPanel.currentPanel = new VCDViewerPanel(panel, extensionUri, vcdFileServer);
|
||||||
|
|
||||||
// 如果提供了 VCD 文件路径,加载它
|
// 如果提供了 VCD 文件路径,加载它
|
||||||
if (vcdFilePath) {
|
if (vcdFilePath) {
|
||||||
@ -72,23 +139,44 @@ export class VCDViewerPanel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从已有的 webview panel 创建 VCD 查看器(用于自定义编辑器)
|
||||||
|
*/
|
||||||
|
public static createFromWebviewPanel(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
extensionUri: vscode.Uri,
|
||||||
|
vcdFilePath: string,
|
||||||
|
vcdFileServer?: VCDFileServer
|
||||||
|
) {
|
||||||
|
const viewer = new VCDViewerPanel(panel, extensionUri, vcdFileServer);
|
||||||
|
viewer.loadVCDFile(vcdFilePath);
|
||||||
|
return viewer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载 VCD 文件
|
* 加载 VCD 文件
|
||||||
*/
|
*/
|
||||||
public loadVCDFile(vcdFilePath: string) {
|
public loadVCDFile(vcdFilePath: string) {
|
||||||
try {
|
try {
|
||||||
|
console.log("[VCDViewerPanel] 开始加载 VCD 文件:", vcdFilePath);
|
||||||
|
|
||||||
// 检查文件是否存在
|
// 检查文件是否存在
|
||||||
if (!fs.existsSync(vcdFilePath)) {
|
if (!fs.existsSync(vcdFilePath)) {
|
||||||
vscode.window.showErrorMessage(`VCD 文件不存在: ${vcdFilePath}`);
|
vscode.window.showErrorMessage(`VCD 文件不存在: ${vcdFilePath}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 保存当前 VCD 路径
|
||||||
|
this._currentVcdPath = vcdFilePath;
|
||||||
|
console.log("[VCDViewerPanel] VCD 路径已保存:", this._currentVcdPath);
|
||||||
|
|
||||||
// 更新面板标题
|
// 更新面板标题
|
||||||
const fileName = path.basename(vcdFilePath);
|
const fileName = path.basename(vcdFilePath);
|
||||||
this._panel.title = `VCD 波形查看器 - ${fileName}`;
|
this._panel.title = `Surfer 波形查看器 - ${fileName}`;
|
||||||
|
|
||||||
// 设置 HTML 内容
|
// 设置 HTML 内容
|
||||||
this._panel.webview.html = this._getWebviewContent(vcdFilePath);
|
this._panel.webview.html = this._getWebviewContent();
|
||||||
|
console.log("[VCDViewerPanel] Webview HTML 已设置");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
`加载 VCD 文件失败: ${error instanceof Error ? error.message : "未知错误"}`
|
`加载 VCD 文件失败: ${error instanceof Error ? error.message : "未知错误"}`
|
||||||
@ -96,6 +184,104 @@ export class VCDViewerPanel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 VCD 文件获取根模块及其直接子模块名称
|
||||||
|
*/
|
||||||
|
private parseVcdRootScope(vcdFilePath: string): string[] {
|
||||||
|
try {
|
||||||
|
// 读取 VCD 文件
|
||||||
|
const buffer = fs.readFileSync(vcdFilePath, { encoding: 'utf8' });
|
||||||
|
const lines = buffer.split('\n');
|
||||||
|
|
||||||
|
const scopeNames: string[] = [];
|
||||||
|
let scopeDepth = 0;
|
||||||
|
const scopeStack: string[] = [];
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
|
||||||
|
// 遇到 $enddefinitions 就停止解析
|
||||||
|
if (trimmed.startsWith('$enddefinitions')) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找 $scope 定义
|
||||||
|
const scopeMatch = trimmed.match(/^\$scope\s+(\w+)\s+(\w+)/);
|
||||||
|
if (scopeMatch) {
|
||||||
|
const scopeType = scopeMatch[1];
|
||||||
|
const scopeName = scopeMatch[2];
|
||||||
|
|
||||||
|
// 记录顶层 module (depth = 0)
|
||||||
|
if (scopeDepth === 0 && scopeType === 'module') {
|
||||||
|
scopeStack.push(scopeName);
|
||||||
|
console.log("[VCDViewerPanel] 找到顶层作用域:", scopeName);
|
||||||
|
}
|
||||||
|
// 记录顶层下的直接子模块 (depth = 1)
|
||||||
|
else if (scopeDepth === 1 && scopeType === 'module') {
|
||||||
|
const fullPath = [...scopeStack, scopeName];
|
||||||
|
scopeNames.push(fullPath.join('.'));
|
||||||
|
console.log("[VCDViewerPanel] 找到子模块:", fullPath.join('.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
scopeDepth++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遇到 $upscope 减少深度
|
||||||
|
if (trimmed.startsWith('$upscope')) {
|
||||||
|
scopeDepth--;
|
||||||
|
if (scopeDepth === 0) {
|
||||||
|
scopeStack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return scopeNames;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[VCDViewerPanel] 解析 VCD 文件失败:", error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 VCD 文件到 Surfer
|
||||||
|
*/
|
||||||
|
private sendVcdToSurfer(vcdFilePath: string) {
|
||||||
|
try {
|
||||||
|
console.log("[VCDViewerPanel] 准备发送 VCD 到 Surfer:", vcdFilePath);
|
||||||
|
|
||||||
|
if (!this._vcdFileServer) {
|
||||||
|
throw new Error("VCD 文件服务器未初始化");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析 VCD 文件获取根模块名称
|
||||||
|
const scopeNames = this.parseVcdRootScope(vcdFilePath);
|
||||||
|
console.log("[VCDViewerPanel] 解析到的作用域名称:", scopeNames);
|
||||||
|
|
||||||
|
// 注册文件到 HTTP 服务器
|
||||||
|
const fileId = this._vcdFileServer.registerFile(vcdFilePath);
|
||||||
|
const httpUrl = this._vcdFileServer.getFileUrl(fileId);
|
||||||
|
const fileName = path.basename(vcdFilePath);
|
||||||
|
|
||||||
|
console.log("[VCDViewerPanel] 文件名:", fileName);
|
||||||
|
console.log("[VCDViewerPanel] HTTP URL:", httpUrl);
|
||||||
|
|
||||||
|
// 使用 LoadUrl 命令通过 HTTP 加载文件
|
||||||
|
this._panel.webview.postMessage({
|
||||||
|
command: "loadVcdUrl",
|
||||||
|
url: httpUrl,
|
||||||
|
fileName: fileName,
|
||||||
|
scopeNames: scopeNames, // 传递解析到的作用域名称
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("[VCDViewerPanel] 已发送 loadVcdUrl 消息到 webview");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[VCDViewerPanel] 发送 VCD 数据失败:", error);
|
||||||
|
vscode.window.showErrorMessage(
|
||||||
|
`发送 VCD 数据失败: ${error instanceof Error ? error.message : "未知错误"}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清理资源
|
* 清理资源
|
||||||
*/
|
*/
|
||||||
@ -163,188 +349,239 @@ export class VCDViewerPanel {
|
|||||||
/**
|
/**
|
||||||
* 获取 Webview 的 HTML 内容
|
* 获取 Webview 的 HTML 内容
|
||||||
*/
|
*/
|
||||||
private _getWebviewContent(vcdFilePath: string): string {
|
private _getWebviewContent(): string {
|
||||||
// 获取资源 URI
|
// 获取 surfer 资源 URI
|
||||||
const vcdromJsUri = this._panel.webview.asWebviewUri(
|
const surferJsUri = this._panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(this._extensionUri, "media", "vcdrom", "vcdrom.js")
|
vscode.Uri.joinPath(this._extensionUri, "media", "surfer", "surfer.js")
|
||||||
);
|
);
|
||||||
const vcdWasmUri = this._panel.webview.asWebviewUri(
|
const surferWasmUri = this._panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(this._extensionUri, "media", "vcdrom", "vcd.wasm")
|
vscode.Uri.joinPath(this._extensionUri, "media", "surfer", "surfer_bg.wasm")
|
||||||
);
|
);
|
||||||
const fontRegularUri = this._panel.webview.asWebviewUri(
|
const integrationJsUri = this._panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(this._extensionUri, "media", "vcdrom", "IosevkaDrom-Regular.woff2")
|
vscode.Uri.joinPath(this._extensionUri, "media", "surfer", "integration.js")
|
||||||
);
|
);
|
||||||
const fontObliqueUri = this._panel.webview.asWebviewUri(
|
|
||||||
vscode.Uri.joinPath(this._extensionUri, "media", "vcdrom", "IosevkaDrom-Oblique.woff2")
|
|
||||||
);
|
|
||||||
const fontItalicUri = this._panel.webview.asWebviewUri(
|
|
||||||
vscode.Uri.joinPath(this._extensionUri, "media", "vcdrom", "IosevkaDrom-Italic.woff2")
|
|
||||||
);
|
|
||||||
|
|
||||||
// 读取 VCD 文件内容并转换为 base64
|
|
||||||
const vcdContent = fs.readFileSync(vcdFilePath, "utf-8");
|
|
||||||
const vcdBase64 = Buffer.from(vcdContent).toString("base64");
|
|
||||||
|
|
||||||
return `<!DOCTYPE html>
|
return `<!DOCTYPE html>
|
||||||
<html lang="zh-CN">
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${this._panel.webview.cspSource}; style-src 'unsafe-inline' ${this._panel.webview.cspSource}; script-src 'unsafe-inline' 'unsafe-eval' ${this._panel.webview.cspSource}; img-src ${this._panel.webview.cspSource} data:; connect-src ${this._panel.webview.cspSource};">
|
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline' 'unsafe-eval' ${this._panel.webview.cspSource}; worker-src blob:; connect-src ${this._panel.webview.cspSource} blob: http://127.0.0.1:*;">
|
||||||
<title>VCD 波形查看器</title>
|
<title>Surfer 波形查看器</title>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// 获取 VS Code API(只能调用一次)
|
||||||
|
const vscode = acquireVsCodeApi();
|
||||||
|
window.vscode = vscode;
|
||||||
|
window.surferReady = false;
|
||||||
|
window.pendingVcdData = null;
|
||||||
|
|
||||||
|
function on_surfer_error(msg) {
|
||||||
|
console.log("Surfer error:", msg);
|
||||||
|
document.getElementById("error_message").innerHTML = msg;
|
||||||
|
document.getElementById("error_container").style.display = "block";
|
||||||
|
}
|
||||||
|
window.on_surfer_error = on_surfer_error;
|
||||||
|
|
||||||
|
// 加载 VCD URL 的函数
|
||||||
|
function loadVcdUrl(data) {
|
||||||
|
try {
|
||||||
|
console.log('[Webview] ========== 开始加载 VCD URL ==========');
|
||||||
|
console.log('[Webview] URL:', data.url);
|
||||||
|
console.log('[Webview] Scope names from VCD:', data.scopeNames);
|
||||||
|
|
||||||
|
// 使用 setTimeout 确保 Surfer 完全准备好
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('[Webview] 通过 postMessage 发送 LoadUrl 命令');
|
||||||
|
|
||||||
|
// 使用 integration.js 提供的标准 LoadUrl 命令
|
||||||
|
window.postMessage({
|
||||||
|
command: 'LoadUrl',
|
||||||
|
url: data.url
|
||||||
|
}, '*');
|
||||||
|
|
||||||
|
console.log('[Webview] ✅ 已发送 LoadUrl 命令');
|
||||||
|
|
||||||
|
// 等待文件加载完成后,自动添加所有信号
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
console.log('[Webview] Attempting to add all signals automatically');
|
||||||
|
|
||||||
|
// 使用从 VCD 文件解析出来的作用域名称
|
||||||
|
let scopeNamesToTry = [];
|
||||||
|
|
||||||
|
if (data.scopeNames && data.scopeNames.length > 0) {
|
||||||
|
// 使用解析出来的实际子模块路径(例如 "tb.dut")
|
||||||
|
scopeNamesToTry = data.scopeNames.map(path => path.split('.'));
|
||||||
|
console.log('[Webview] Using parsed scope names:', scopeNamesToTry);
|
||||||
|
} else {
|
||||||
|
// 回退到常见的根作用域名称
|
||||||
|
scopeNamesToTry = [
|
||||||
|
['top'],
|
||||||
|
['testbench'],
|
||||||
|
['tb'],
|
||||||
|
['test'],
|
||||||
|
['dut']
|
||||||
|
];
|
||||||
|
console.log('[Webview] Using fallback scope names');
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < scopeNamesToTry.length; i++) {
|
||||||
|
const scopeName = scopeNamesToTry[i];
|
||||||
|
try {
|
||||||
|
const addScopeMsg = {
|
||||||
|
"AddScope": [
|
||||||
|
{
|
||||||
|
"strs": scopeName,
|
||||||
|
"id": {"Wellen": i + 1}
|
||||||
|
},
|
||||||
|
true // 递归添加子模块的所有信号
|
||||||
|
]
|
||||||
|
};
|
||||||
|
window.inject_message(JSON.stringify(addScopeMsg));
|
||||||
|
console.log('[Webview] Sent AddScope for: ' + scopeName.join('.') + ' (recursive)');
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[Webview] Failed for scope: ' + scopeName.join('.'), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待信号加载完成后,自动缩放到全部时间范围
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
window.inject_message(JSON.stringify("ZoomToFit"));
|
||||||
|
console.log('[Webview] Sent ZoomToFit command');
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[Webview] ZoomToFit failed:', e);
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[Webview] Failed to add signals:', e);
|
||||||
|
}
|
||||||
|
}, 1500);
|
||||||
|
|
||||||
|
}, 100);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Webview] ❌ 加载 VCD 失败:', error);
|
||||||
|
on_surfer_error(error.message + '\\n' + error.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.loadVcdUrl = loadVcdUrl;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="module">
|
||||||
|
console.log('[Webview] 开始初始化 Surfer...');
|
||||||
|
import init from '${surferJsUri}';
|
||||||
|
await init({module_or_path: '${surferWasmUri}'});
|
||||||
|
console.log('[Webview] Surfer WASM 已加载');
|
||||||
|
|
||||||
|
import {WebHandle, inject_message, id_of_name, draw_text_arrow} from '${surferJsUri}';
|
||||||
|
window.inject_message = inject_message;
|
||||||
|
window.id_of_name = id_of_name;
|
||||||
|
window.draw_text_arrow = draw_text_arrow;
|
||||||
|
|
||||||
|
console.log('[Webview] Surfer 函数已导入,inject_message 类型:', typeof window.inject_message);
|
||||||
|
|
||||||
|
// 等待一小段时间确保 Surfer 完全初始化
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
|
||||||
|
window.surferReady = true;
|
||||||
|
console.log('[Webview] Surfer 已完全初始化并准备就绪');
|
||||||
|
|
||||||
|
// 关闭 Surfer 的日志面板(如果打开的话)
|
||||||
|
try {
|
||||||
|
window.inject_message(JSON.stringify("ToggleLogs"));
|
||||||
|
console.log('[Webview] 已发送关闭日志面板命令');
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[Webview] 关闭日志面板失败:', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有待处理的 VCD 数据,现在加载它
|
||||||
|
if (window.pendingVcdData) {
|
||||||
|
console.log('[Webview] 发现待处理的 VCD 数据,立即加载');
|
||||||
|
loadVcdUrl(window.pendingVcdData);
|
||||||
|
window.pendingVcdData = null;
|
||||||
|
} else {
|
||||||
|
console.log('[Webview] 没有待处理的 VCD 数据');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通知 VS Code surfer 已加载完成
|
||||||
|
console.log('[Webview] 发送 loaded 消息到 VS Code');
|
||||||
|
window.vscode.postMessage({ command: 'loaded' });
|
||||||
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@font-face {
|
html, body {
|
||||||
font-family: 'Iosevka Drom Web';
|
|
||||||
font-display: swap;
|
|
||||||
font-weight: 400;
|
|
||||||
font-stretch: normal;
|
|
||||||
font-style: normal;
|
|
||||||
src: url('${fontRegularUri}') format('woff2');
|
|
||||||
}
|
|
||||||
|
|
||||||
@font-face {
|
|
||||||
font-family: 'Iosevka Drom Web';
|
|
||||||
font-display: swap;
|
|
||||||
font-weight: 400;
|
|
||||||
font-stretch: normal;
|
|
||||||
font-style: oblique;
|
|
||||||
src: url('${fontObliqueUri}') format('woff2');
|
|
||||||
}
|
|
||||||
|
|
||||||
@font-face {
|
|
||||||
font-family: 'Iosevka Drom Web';
|
|
||||||
font-display: swap;
|
|
||||||
font-weight: 400;
|
|
||||||
font-stretch: normal;
|
|
||||||
font-style: italic;
|
|
||||||
src: url('${fontItalicUri}') format('woff2');
|
|
||||||
}
|
|
||||||
|
|
||||||
* {
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
font-family: 'Iosevka Drom Web', monospace;
|
|
||||||
color: var(--vscode-foreground);
|
|
||||||
background-color: var(--vscode-editor-background);
|
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
margin: 0 !important;
|
||||||
|
padding: 0 !important;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
background: var(--vscode-editor-background);
|
||||||
}
|
}
|
||||||
|
|
||||||
#waveform-container {
|
canvas {
|
||||||
width: 100vw;
|
margin-right: auto;
|
||||||
height: 100vh;
|
margin-left: auto;
|
||||||
overflow: auto;
|
display: block;
|
||||||
}
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
#waveform1 {
|
left: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.loading {
|
#error_container {
|
||||||
display: flex;
|
padding: 1em;
|
||||||
justify-content: center;
|
border-radius: 0.5em;
|
||||||
align-items: center;
|
margin: 0px auto;
|
||||||
height: 100vh;
|
max-width: 980px;
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
|
|
||||||
.spinner {
|
|
||||||
border: 4px solid var(--vscode-progressBar-background);
|
|
||||||
border-top: 4px solid var(--vscode-progressBar-foreground);
|
|
||||||
border-radius: 50%;
|
|
||||||
width: 40px;
|
|
||||||
height: 40px;
|
|
||||||
animation: spin 1s linear infinite;
|
|
||||||
margin-bottom: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes spin {
|
|
||||||
0% { transform: rotate(0deg); }
|
|
||||||
100% { transform: rotate(360deg); }
|
|
||||||
}
|
|
||||||
|
|
||||||
.error-message {
|
|
||||||
padding: 20px;
|
|
||||||
color: var(--vscode-errorForeground);
|
color: var(--vscode-errorForeground);
|
||||||
background-color: var(--vscode-inputValidation-errorBackground);
|
background-color: var(--vscode-inputValidation-errorBackground);
|
||||||
border: 1px solid var(--vscode-inputValidation-errorBorder);
|
position: relative;
|
||||||
border-radius: 4px;
|
height: 90%;
|
||||||
margin: 20px;
|
overflow: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
#error_message {
|
||||||
|
overflow: scroll;
|
||||||
|
white-space: break-spaces;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<script src="${vcdromJsUri}"></script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="waveform-container">
|
<canvas id="the_canvas_id"></canvas>
|
||||||
<div class="loading">
|
|
||||||
<div class="spinner"></div>
|
<div id="error_container" style="display: none;">
|
||||||
<p>正在加载 VCD 波形...</p>
|
<h3>❌ Surfer 加载失败</h3>
|
||||||
</div>
|
<code id="error_message"></code>
|
||||||
<div id="waveform1"></div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script src="${integrationJsUri}"></script>
|
||||||
<script>
|
<script>
|
||||||
(async function() {
|
register_message_listener();
|
||||||
try {
|
|
||||||
// 设置 WASM 文件路径
|
|
||||||
window.wasmBinaryFile = '${vcdWasmUri}';
|
|
||||||
|
|
||||||
// 解码 base64 VCD 内容
|
console.log('[Webview] 注册 VS Code 消息监听器');
|
||||||
const vcdBase64 = '${vcdBase64}';
|
// 监听来自 VS Code 扩展的消息(使用 vscode API)
|
||||||
const vcdContent = atob(vcdBase64);
|
window.addEventListener('message', event => {
|
||||||
|
const message = event.data;
|
||||||
|
|
||||||
// 隐藏加载提示
|
// 检查是否来自 VS Code
|
||||||
document.querySelector('.loading').style.display = 'none';
|
if (message.command === 'loadVcdUrl') {
|
||||||
|
console.log('[Webview] 收到 VS Code 消息,命令:', message.command);
|
||||||
|
console.log('[Webview] Surfer 就绪状态:', window.surferReady);
|
||||||
|
|
||||||
// 创建一个函数来提供 VCD 数据流
|
if (window.surferReady) {
|
||||||
const vcdProvider = async (handler) => {
|
// Surfer 已就绪,立即加载
|
||||||
// 将 VCD 内容转换为 Uint8Array
|
loadVcdUrl(message);
|
||||||
const encoder = new TextEncoder();
|
|
||||||
const vcdData = encoder.encode(vcdContent);
|
|
||||||
|
|
||||||
// 创建一个 ReadableStream reader
|
|
||||||
const stream = new ReadableStream({
|
|
||||||
start(controller) {
|
|
||||||
controller.enqueue(vcdData);
|
|
||||||
controller.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const reader = stream.getReader();
|
|
||||||
|
|
||||||
// 调用 handler 并传递 reader
|
|
||||||
await handler([{
|
|
||||||
key: 'local',
|
|
||||||
value: 'waveform.vcd',
|
|
||||||
format: 'raw',
|
|
||||||
baseName: 'waveform.vcd',
|
|
||||||
ext: 'vcd',
|
|
||||||
reader: reader
|
|
||||||
}]);
|
|
||||||
};
|
|
||||||
|
|
||||||
// 初始化 VCDrom,使用函数回调方式
|
|
||||||
if (typeof VCDrom === 'function') {
|
|
||||||
await VCDrom('waveform1', vcdProvider);
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('VCDrom 未正确加载');
|
// Surfer 未就绪,保存数据等待加载
|
||||||
|
console.log('[Webview] Surfer 未就绪,保存数据待加载');
|
||||||
|
window.pendingVcdData = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('加载 VCD 波形失败:', error);
|
|
||||||
document.getElementById('waveform-container').innerHTML =
|
|
||||||
'<div class="error-message">' +
|
|
||||||
'<h3>❌ 加载 VCD 波形失败</h3>' +
|
|
||||||
'<p>' + error.message + '</p>' +
|
|
||||||
'<p style="margin-top: 10px;">请确保 VCD 文件格式正确。</p>' +
|
|
||||||
'<pre style="margin-top: 10px; padding: 10px; background: rgba(0,0,0,0.1); overflow: auto;">' + error.stack + '</pre>' +
|
|
||||||
'</div>';
|
|
||||||
}
|
}
|
||||||
})();
|
}, true); // 使用捕获阶段,优先于 integration.js 的监听器
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
|
|||||||
395
src/services/apiClient.ts
Normal file
@ -0,0 +1,395 @@
|
|||||||
|
/**
|
||||||
|
* API 客户端
|
||||||
|
* 封装与后端的 HTTP 通信
|
||||||
|
*/
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
import * as https from "https";
|
||||||
|
import * as http from "http";
|
||||||
|
import { URL } from "url";
|
||||||
|
import { getApiUrl, getConfig } from "../config/settings";
|
||||||
|
import type {
|
||||||
|
ToolCallResult,
|
||||||
|
AnswerRequest,
|
||||||
|
ToolResultResponse,
|
||||||
|
AnswerResponse,
|
||||||
|
ToolConfirmResponse,
|
||||||
|
UserInfoResponse,
|
||||||
|
InvitationVerifyRequest,
|
||||||
|
InvitationVerifyResponse,
|
||||||
|
InvitationStatusResponse,
|
||||||
|
} from "../types/api";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP 请求选项
|
||||||
|
*/
|
||||||
|
interface RequestOptions {
|
||||||
|
method: "GET" | "POST" | "PUT" | "DELETE";
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
body?: unknown;
|
||||||
|
timeout?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录的 Token
|
||||||
|
*/
|
||||||
|
async function getAuthToken(): Promise<string | undefined> {
|
||||||
|
try {
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
silent: true,
|
||||||
|
});
|
||||||
|
return session?.accessToken;
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 HTTP 请求
|
||||||
|
*/
|
||||||
|
async function request<T>(path: string, options: RequestOptions): Promise<T> {
|
||||||
|
const url = new URL(getApiUrl(path));
|
||||||
|
const { timeout } = getConfig();
|
||||||
|
|
||||||
|
// 自动获取 Token
|
||||||
|
const token = await getAuthToken();
|
||||||
|
|
||||||
|
const isHttps = url.protocol === "https:";
|
||||||
|
const httpModule = isHttps ? https : http;
|
||||||
|
|
||||||
|
const requestOptions: http.RequestOptions = {
|
||||||
|
hostname: url.hostname,
|
||||||
|
port: url.port || (isHttps ? 443 : 80),
|
||||||
|
path: url.pathname + url.search,
|
||||||
|
method: options.method,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
|
...options.headers,
|
||||||
|
},
|
||||||
|
timeout: options.timeout || timeout,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("[HTTP] 请求详情:", {
|
||||||
|
url: url.toString(),
|
||||||
|
method: options.method,
|
||||||
|
headers: requestOptions.headers,
|
||||||
|
hasToken: !!token,
|
||||||
|
body: options.body,
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const req = httpModule.request(requestOptions, (res) => {
|
||||||
|
let data = "";
|
||||||
|
|
||||||
|
// console.log('[HTTP] 响应状态码:', res.statusCode);
|
||||||
|
// console.log('[HTTP] 响应头:', res.headers);
|
||||||
|
|
||||||
|
res.on("data", (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on("end", () => {
|
||||||
|
console.log("[HTTP] 响应体:", data);
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
// console.log('[HTTP] 解析后的响应:', JSON.stringify(json, null, 2));
|
||||||
|
|
||||||
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
console.log("[HTTP] 请求成功");
|
||||||
|
resolve(json as T);
|
||||||
|
} else {
|
||||||
|
console.error("[HTTP] 请求失败:", {
|
||||||
|
statusCode: res.statusCode,
|
||||||
|
error: json.error,
|
||||||
|
message: json.message,
|
||||||
|
msg: json.msg,
|
||||||
|
});
|
||||||
|
reject(
|
||||||
|
new Error(
|
||||||
|
json.error ||
|
||||||
|
json.message ||
|
||||||
|
json.msg ||
|
||||||
|
`HTTP ${res.statusCode}`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// console.error('[HTTP] 解析响应失败:', e);
|
||||||
|
// console.error('[HTTP] 原始响应:', data);
|
||||||
|
reject(new Error(`解析响应失败: ${data}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on("error", (error) => {
|
||||||
|
// console.error('[HTTP] 请求错误:', error);
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on("timeout", () => {
|
||||||
|
// console.error('[HTTP] 请求超时');
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error("请求超时"));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (options.body) {
|
||||||
|
const bodyStr = JSON.stringify(options.body);
|
||||||
|
// console.log('[HTTP] 发送请求体:', bodyStr);
|
||||||
|
req.write(bodyStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
// console.log('[HTTP] 请求已发送');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交工具执行结果
|
||||||
|
* POST /api/tool/result
|
||||||
|
*/
|
||||||
|
export async function submitToolResult(
|
||||||
|
result: ToolCallResult,
|
||||||
|
): Promise<ToolResultResponse> {
|
||||||
|
console.log(`[API] 提交工具结果: callId=${result.id}`);
|
||||||
|
return request<ToolResultResponse>("/api/tool/result", {
|
||||||
|
method: "POST",
|
||||||
|
body: result,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交用户回答
|
||||||
|
* POST /api/task/answer
|
||||||
|
*/
|
||||||
|
export async function submitAnswer(
|
||||||
|
answer: AnswerRequest,
|
||||||
|
): Promise<AnswerResponse> {
|
||||||
|
console.log(`[API] 提交用户回答: askId=${answer.askId}`);
|
||||||
|
return request<AnswerResponse>("/api/task/answer", {
|
||||||
|
method: "POST",
|
||||||
|
body: answer,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交工具确认响应(Ask 模式)
|
||||||
|
* POST /api/tool/confirm
|
||||||
|
*/
|
||||||
|
export async function submitToolConfirm(
|
||||||
|
response: ToolConfirmResponse,
|
||||||
|
): Promise<ToolResultResponse> {
|
||||||
|
console.log(
|
||||||
|
`[API] 提交工具确认: confirmId=${response.confirmId}, approved=${response.approved}`,
|
||||||
|
);
|
||||||
|
return request<ToolResultResponse>("/api/tool/confirm", {
|
||||||
|
method: "POST",
|
||||||
|
body: response,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 健康检查
|
||||||
|
* GET /api/dialog/health
|
||||||
|
*/
|
||||||
|
export async function healthCheck(): Promise<{ status: string }> {
|
||||||
|
return request<{ status: string }>("/api/dialog/health", {
|
||||||
|
method: "GET",
|
||||||
|
timeout: 5000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止对话请求
|
||||||
|
*/
|
||||||
|
export interface StopDialogRequest {
|
||||||
|
taskId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止对话响应
|
||||||
|
*/
|
||||||
|
export interface StopDialogResponse {
|
||||||
|
success: boolean;
|
||||||
|
taskId: string;
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止对话
|
||||||
|
* POST /api/dialog/stop
|
||||||
|
*/
|
||||||
|
export async function stopDialog(taskId: string): Promise<StopDialogResponse> {
|
||||||
|
console.log(`[API] 停止对话: taskId=${taskId}`);
|
||||||
|
return request<StopDialogResponse>("/api/dialog/stop", {
|
||||||
|
method: "POST",
|
||||||
|
body: { taskId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 压缩对话响应 */
|
||||||
|
export interface CompactDialogResponse {
|
||||||
|
success: boolean;
|
||||||
|
taskId: string;
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手动压缩对话历史
|
||||||
|
* POST /api/dialog/compact
|
||||||
|
*/
|
||||||
|
export async function compactDialog(
|
||||||
|
taskId: string,
|
||||||
|
): Promise<CompactDialogResponse> {
|
||||||
|
console.log(`[API] 压缩对话: taskId=${taskId}`);
|
||||||
|
return request<CompactDialogResponse>("/api/dialog/compact", {
|
||||||
|
method: "POST",
|
||||||
|
body: { taskId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建成功的工具结果
|
||||||
|
*/
|
||||||
|
export function createSuccessResult(id: number, text: string): ToolCallResult {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
content: [{ type: "text", text }],
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建业务错误的工具结果(如编译失败)
|
||||||
|
*/
|
||||||
|
export function createBusinessErrorResult(
|
||||||
|
id: number,
|
||||||
|
errorMessage: string,
|
||||||
|
): ToolCallResult {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
content: [{ type: "text", text: errorMessage }],
|
||||||
|
isError: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建系统错误的工具结果
|
||||||
|
*/
|
||||||
|
export function createSystemErrorResult(
|
||||||
|
id: number,
|
||||||
|
code: number,
|
||||||
|
message: string,
|
||||||
|
): ToolCallResult {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: { code, message },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
* GET /system/user/getInfo
|
||||||
|
*/
|
||||||
|
export async function getUserInfo(): Promise<UserInfoResponse> {
|
||||||
|
console.log("[API] 获取用户信息");
|
||||||
|
return request<UserInfoResponse>("/system/user/getInfo", {
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 余额查询响应 */
|
||||||
|
export interface CreditBalanceResponse {
|
||||||
|
success: boolean;
|
||||||
|
balance?: number;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询用户资源点余额
|
||||||
|
* GET /api/dialog/balance?userId=xxx
|
||||||
|
*/
|
||||||
|
export async function getCreditBalance(
|
||||||
|
userId: string,
|
||||||
|
): Promise<CreditBalanceResponse> {
|
||||||
|
console.log("[API] 查询余额: userId=", userId);
|
||||||
|
return request<CreditBalanceResponse>(
|
||||||
|
`/api/dialog/balance?userId=${userId}`,
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
timeout: 5000,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证邀请码
|
||||||
|
* POST /api/invitation/verify
|
||||||
|
*/
|
||||||
|
export async function verifyInvitationCode(
|
||||||
|
code: string,
|
||||||
|
): Promise<InvitationVerifyResponse> {
|
||||||
|
// console.log('[API] 验证邀请码 - 开始');
|
||||||
|
console.log("[API] 邀请码:", code);
|
||||||
|
const body: InvitationVerifyRequest = { code };
|
||||||
|
console.log("[API] 请求体:", JSON.stringify(body));
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request<InvitationVerifyResponse>(
|
||||||
|
"/api/invitation/verify",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
body,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
console.log("[API] 验证邀请码 - 响应:", JSON.stringify(response));
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[API] 验证邀请码 - 错误:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询邀请码验证状态
|
||||||
|
* GET /api/invitation/status
|
||||||
|
*/
|
||||||
|
export async function checkInvitationStatus(): Promise<InvitationStatusResponse> {
|
||||||
|
console.log("[API] 查询邀请码验证状态");
|
||||||
|
return request<InvitationStatusResponse>("/api/invitation/status", {
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重置邀请码验证状态(退出登录时调用)
|
||||||
|
* POST /api/invitation/reset
|
||||||
|
*/
|
||||||
|
export async function resetInvitationVerification(): Promise<{
|
||||||
|
code: number;
|
||||||
|
msg: string;
|
||||||
|
}> {
|
||||||
|
console.log("[API] 重置邀请码验证状态");
|
||||||
|
try {
|
||||||
|
const response = await request<{ code: number; msg: string }>(
|
||||||
|
"/api/invitation/reset",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
console.log("[API] 重置邀请码验证状态 - 响应:", JSON.stringify(response));
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[API] 重置邀请码验证状态 - 错误:", error);
|
||||||
|
// 即使失败也不影响退出登录流程
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
255
src/services/creditsService.ts
Normal file
@ -0,0 +1,255 @@
|
|||||||
|
/**
|
||||||
|
* 资源点余额管理服务
|
||||||
|
* 负责缓存余额、主动查询、发送前检测
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as https from 'https';
|
||||||
|
import * as http from 'http';
|
||||||
|
import { URL } from 'url';
|
||||||
|
import { getStrangeLoopApiUrl } from '../config/settings';
|
||||||
|
import { getCachedUserInfo } from './userService';
|
||||||
|
|
||||||
|
/** 低余额阈值 */
|
||||||
|
const LOW_CREDIT_THRESHOLD = 5;
|
||||||
|
|
||||||
|
/** 缓存的余额 */
|
||||||
|
let cachedBalance: number | null = null;
|
||||||
|
|
||||||
|
/** 最后更新时间 */
|
||||||
|
let lastUpdateTime: number = 0;
|
||||||
|
|
||||||
|
/** 缓存有效期(5分钟) */
|
||||||
|
const CACHE_TTL_MS = 5 * 60 * 1000;
|
||||||
|
|
||||||
|
/** ExtensionContext 用于持久化存储 */
|
||||||
|
let extensionContext: vscode.ExtensionContext | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化 Credits 服务(设置 context)
|
||||||
|
*/
|
||||||
|
export function initCreditsService(context: vscode.ExtensionContext): void {
|
||||||
|
extensionContext = context;
|
||||||
|
// 从持久化存储加载余额
|
||||||
|
const savedBalance = extensionContext.globalState.get<number>('icCoderCreditsBalance');
|
||||||
|
if (savedBalance !== undefined) {
|
||||||
|
cachedBalance = savedBalance;
|
||||||
|
lastUpdateTime = Date.now();
|
||||||
|
console.log('[CreditsService] 从持久化存储加载余额:', savedBalance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存余额到持久化存储
|
||||||
|
*/
|
||||||
|
async function saveBalance(balance: number): Promise<void> {
|
||||||
|
if (extensionContext) {
|
||||||
|
await extensionContext.globalState.update('icCoderCreditsBalance', balance);
|
||||||
|
console.log('[CreditsService] 余额已保存到持久化存储:', balance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新缓存的余额(从 SSE credit_update 事件调用)
|
||||||
|
*/
|
||||||
|
export function updateCachedBalance(balance: number): void {
|
||||||
|
cachedBalance = balance;
|
||||||
|
lastUpdateTime = Date.now();
|
||||||
|
console.log('[CreditsService] 余额已更新:', balance);
|
||||||
|
// 异步保存到持久化存储
|
||||||
|
saveBalance(balance).catch(err => {
|
||||||
|
console.error('[CreditsService] 保存余额失败:', err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存的余额
|
||||||
|
*/
|
||||||
|
export function getCachedBalance(): number | null {
|
||||||
|
return cachedBalance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查缓存是否有效
|
||||||
|
*/
|
||||||
|
function isCacheValid(): boolean {
|
||||||
|
if (cachedBalance === null) return false;
|
||||||
|
return Date.now() - lastUpdateTime < CACHE_TTL_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* StrangeLoop 余额响应类型
|
||||||
|
*/
|
||||||
|
interface StrangeLoopBalanceResponse {
|
||||||
|
userId?: number;
|
||||||
|
availableCredits?: number;
|
||||||
|
totalCredits?: number;
|
||||||
|
error?: string;
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主动查询余额(直接调用 StrangeLoop 接口)
|
||||||
|
*/
|
||||||
|
export async function fetchBalance(): Promise<number | null> {
|
||||||
|
try {
|
||||||
|
// 获取 JWT token
|
||||||
|
const session = await vscode.authentication.getSession('iccoder', [], { silent: true });
|
||||||
|
if (!session?.accessToken) {
|
||||||
|
console.warn('[CreditsService] 无法查询余额:未登录');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await fetchBalanceWithToken(session.accessToken);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[CreditsService] 查询余额异常:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用指定 token 查询余额(登录过程中使用)
|
||||||
|
*/
|
||||||
|
export async function fetchBalanceWithToken(token: string): Promise<number | null> {
|
||||||
|
try {
|
||||||
|
console.log('[CreditsService] 开始查询余额,token 长度:', token.length);
|
||||||
|
|
||||||
|
// 直接调用 StrangeLoop 的 /api/credit/balance 接口
|
||||||
|
const response = await callStrangeLoopBalance(token);
|
||||||
|
|
||||||
|
if (response.availableCredits !== undefined) {
|
||||||
|
const balance = response.availableCredits;
|
||||||
|
updateCachedBalance(balance);
|
||||||
|
console.log('[CreditsService] 余额查询成功:', balance);
|
||||||
|
return balance;
|
||||||
|
} else {
|
||||||
|
console.warn('[CreditsService] 查询余额失败:', response.error || response.message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[CreditsService] 查询余额异常:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用 StrangeLoop 余额接口
|
||||||
|
*/
|
||||||
|
async function callStrangeLoopBalance(token: string): Promise<StrangeLoopBalanceResponse> {
|
||||||
|
const urlStr = getStrangeLoopApiUrl('/strangeloop/api/credit/balance');
|
||||||
|
const url = new URL(urlStr);
|
||||||
|
|
||||||
|
const isHttps = url.protocol === 'https:';
|
||||||
|
const httpModule = isHttps ? https : http;
|
||||||
|
|
||||||
|
// 余额查询使用固定短超时,避免阻塞发送前检查
|
||||||
|
const BALANCE_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
|
const requestOptions: http.RequestOptions = {
|
||||||
|
hostname: url.hostname,
|
||||||
|
port: url.port || (isHttps ? 443 : 80),
|
||||||
|
path: url.pathname + url.search,
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
},
|
||||||
|
timeout: BALANCE_TIMEOUT_MS
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const req = httpModule.request(requestOptions, (res) => {
|
||||||
|
let data = '';
|
||||||
|
|
||||||
|
res.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
console.log('[CreditsService] 响应状态码:', res.statusCode);
|
||||||
|
console.log('[CreditsService] 响应内容:', data);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
resolve(json as StrangeLoopBalanceResponse);
|
||||||
|
} else if (res.statusCode === 401 || res.statusCode === 403) {
|
||||||
|
// 登录过期或无权限
|
||||||
|
resolve({ error: '登录已过期,请重新登录' });
|
||||||
|
} else {
|
||||||
|
resolve({ error: json.error || json.message || json.msg || `HTTP ${res.statusCode}` });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
resolve({ error: `解析响应失败: ${data}` });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('timeout', () => {
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error('请求超时'));
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前余额(优先使用缓存,过期则主动查询)
|
||||||
|
*/
|
||||||
|
export async function getBalance(): Promise<number | null> {
|
||||||
|
if (isCacheValid()) {
|
||||||
|
return cachedBalance;
|
||||||
|
}
|
||||||
|
return await fetchBalance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查余额是否足够发送消息
|
||||||
|
* @returns { allowed: boolean, balance: number | null, message?: string }
|
||||||
|
*/
|
||||||
|
export async function checkBalanceBeforeSend(): Promise<{
|
||||||
|
allowed: boolean;
|
||||||
|
balance: number | null;
|
||||||
|
message?: string;
|
||||||
|
}> {
|
||||||
|
const userInfo = getCachedUserInfo();
|
||||||
|
if (!userInfo) {
|
||||||
|
// 未登录,允许发送(后端会处理)
|
||||||
|
return { allowed: true, balance: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
const balance = await getBalance();
|
||||||
|
|
||||||
|
if (balance === null) {
|
||||||
|
// 无法获取余额,允许发送(后端会处理)
|
||||||
|
console.warn('[CreditsService] 无法获取余额,允许发送');
|
||||||
|
return { allowed: true, balance: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (balance < LOW_CREDIT_THRESHOLD) {
|
||||||
|
return {
|
||||||
|
allowed: false,
|
||||||
|
balance,
|
||||||
|
message: `资源点余额不足!当前余额 ${balance.toFixed(2)} 点,低于最低要求 ${LOW_CREDIT_THRESHOLD} 点。请充值后再试。`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { allowed: true, balance };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除缓存(登出时调用)
|
||||||
|
*/
|
||||||
|
export async function clearBalanceCache(): Promise<void> {
|
||||||
|
cachedBalance = null;
|
||||||
|
lastUpdateTime = 0;
|
||||||
|
if (extensionContext) {
|
||||||
|
await extensionContext.globalState.update('icCoderCreditsBalance', undefined);
|
||||||
|
}
|
||||||
|
console.log('[CreditsService] 余额缓存已清除');
|
||||||
|
}
|
||||||
1159
src/services/dialogService.ts
Normal file
487
src/services/icCoderAuthProvider.ts
Normal file
@ -0,0 +1,487 @@
|
|||||||
|
import * as vscode from "vscode";
|
||||||
|
import * as http from "http";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as fs from "fs";
|
||||||
|
import { onTokenReceived, type UserInfo, clearUserInfo } from "./userService";
|
||||||
|
import { getConfig } from "../config/settings";
|
||||||
|
import { resetInvitationVerification } from "./apiClient";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IC Coder Authentication Provider
|
||||||
|
* 集成到 VSCode 账户系统
|
||||||
|
*/
|
||||||
|
export class ICCoderAuthenticationProvider
|
||||||
|
implements vscode.AuthenticationProvider
|
||||||
|
{
|
||||||
|
private static readonly AUTH_TYPE = "iccoder";
|
||||||
|
private static readonly AUTH_NAME = "IC Coder";
|
||||||
|
private static loginServer: http.Server | null = null;
|
||||||
|
private static currentPort: number | null = null;
|
||||||
|
|
||||||
|
private _onDidChangeSessions =
|
||||||
|
new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
|
||||||
|
public readonly onDidChangeSessions = this._onDidChangeSessions.event;
|
||||||
|
|
||||||
|
private _sessions: vscode.AuthenticationSession[] = [];
|
||||||
|
|
||||||
|
constructor(private readonly context: vscode.ExtensionContext) {
|
||||||
|
// 从存储中恢复会话(同步执行)
|
||||||
|
this.loadSessionsSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从存储中加载会话(同步版本)
|
||||||
|
*/
|
||||||
|
private loadSessionsSync(): void {
|
||||||
|
const storedSessions = this.context.globalState.get<
|
||||||
|
vscode.AuthenticationSession[]
|
||||||
|
>("icCoderSessions", []);
|
||||||
|
this._sessions = storedSessions;
|
||||||
|
console.log("[AuthProvider] 同步加载 sessions, 数量:", this._sessions.length);
|
||||||
|
if (this._sessions.length > 0) {
|
||||||
|
console.log("[AuthProvider] Session ID:", this._sessions[0].id);
|
||||||
|
console.log("[AuthProvider] Account:", this._sessions[0].account.label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从存储中加载会话
|
||||||
|
*/
|
||||||
|
private async loadSessions(): Promise<void> {
|
||||||
|
const storedSessions = this.context.globalState.get<
|
||||||
|
vscode.AuthenticationSession[]
|
||||||
|
>("icCoderSessions", []);
|
||||||
|
this._sessions = storedSessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存会话到存储
|
||||||
|
*/
|
||||||
|
private async saveSessions(): Promise<void> {
|
||||||
|
console.log("[AuthProvider] 保存 sessions, 数量:", this._sessions.length);
|
||||||
|
await this.context.globalState.update("icCoderSessions", this._sessions);
|
||||||
|
console.log("[AuthProvider] sessions 已保存到 globalState");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取会话列表
|
||||||
|
*/
|
||||||
|
async getSessions(
|
||||||
|
scopes?: readonly string[]
|
||||||
|
): Promise<vscode.AuthenticationSession[]> {
|
||||||
|
console.log("[AuthProvider] getSessions 被调用, 当前 sessions 数量:", this._sessions.length);
|
||||||
|
return [...this._sessions];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建会话(登录)
|
||||||
|
*/
|
||||||
|
async createSession(
|
||||||
|
scopes: readonly string[]
|
||||||
|
): Promise<vscode.AuthenticationSession> {
|
||||||
|
try {
|
||||||
|
// 先删除旧的 session(静默删除,不弹窗、不重载窗口)
|
||||||
|
if (this._sessions.length > 0) {
|
||||||
|
const oldSession = this._sessions[0];
|
||||||
|
this._sessions = [];
|
||||||
|
await this.saveSessions();
|
||||||
|
await clearUserInfo();
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [],
|
||||||
|
removed: [oldSession],
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
console.log("🔄 已清除旧的 session");
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = await this.login();
|
||||||
|
|
||||||
|
// 获取到 token 后立即调用用户信息接口
|
||||||
|
const userInfo = await onTokenReceived(token);
|
||||||
|
|
||||||
|
// 创建会话
|
||||||
|
const session: vscode.AuthenticationSession = {
|
||||||
|
id: this.generateSessionId(),
|
||||||
|
accessToken: token,
|
||||||
|
account: {
|
||||||
|
id: userInfo?.userId || "iccoder-user",
|
||||||
|
label: userInfo?.nickname || userInfo?.username || "IC Coder 用户",
|
||||||
|
},
|
||||||
|
scopes: [...scopes],
|
||||||
|
};
|
||||||
|
|
||||||
|
this._sessions.push(session);
|
||||||
|
await this.saveSessions();
|
||||||
|
|
||||||
|
// 触发会话变化事件
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [session],
|
||||||
|
removed: [],
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
vscode.window.showInformationMessage("登录成功!窗口将自动刷新...");
|
||||||
|
|
||||||
|
// 延迟 1 秒后重新加载窗口,让用户看到成功消息
|
||||||
|
setTimeout(() => {
|
||||||
|
vscode.commands.executeCommand("workbench.action.reloadWindow");
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
return session;
|
||||||
|
} catch (error) {
|
||||||
|
vscode.window.showErrorMessage(
|
||||||
|
`登录失败: ${error instanceof Error ? error.message : String(error)}`
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除会话(登出)
|
||||||
|
*/
|
||||||
|
async removeSession(sessionId: string): Promise<void> {
|
||||||
|
const sessionIndex = this._sessions.findIndex((s) => s.id === sessionId);
|
||||||
|
if (sessionIndex > -1) {
|
||||||
|
const session = this._sessions[sessionIndex];
|
||||||
|
|
||||||
|
// 1. 先调用后端重置邀请码验证状态
|
||||||
|
try {
|
||||||
|
await resetInvitationVerification();
|
||||||
|
console.log("[AuthProvider] 邀请码验证状态已重置");
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[AuthProvider] 重置邀请码验证状态失败,但继续退出流程:", error);
|
||||||
|
// 即使失败也继续退出流程
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 清除本地 session
|
||||||
|
this._sessions.splice(sessionIndex, 1);
|
||||||
|
await this.saveSessions();
|
||||||
|
|
||||||
|
// 3. 清除用户信息缓存
|
||||||
|
await clearUserInfo();
|
||||||
|
|
||||||
|
// 4. 触发会话变化事件
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [],
|
||||||
|
removed: [session],
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
vscode.window.showInformationMessage("已退出登录!窗口将自动刷新...");
|
||||||
|
|
||||||
|
// 延迟 1 秒后重新加载窗口,让用户看到成功消息
|
||||||
|
setTimeout(() => {
|
||||||
|
vscode.commands.executeCommand("workbench.action.reloadWindow");
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成会话 ID
|
||||||
|
*/
|
||||||
|
private generateSessionId(): string {
|
||||||
|
return `iccoder-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录逻辑(打开浏览器并等待回调)
|
||||||
|
*/
|
||||||
|
private async login(): Promise<string> {
|
||||||
|
// 如果已有服务器在运行,先关闭
|
||||||
|
if (ICCoderAuthenticationProvider.loginServer) {
|
||||||
|
ICCoderAuthenticationProvider.loginServer.close();
|
||||||
|
ICCoderAuthenticationProvider.loginServer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建本地服务器监听回调
|
||||||
|
const { server, port } = await this.createCallbackServer();
|
||||||
|
ICCoderAuthenticationProvider.loginServer = server;
|
||||||
|
ICCoderAuthenticationProvider.currentPort = port;
|
||||||
|
|
||||||
|
// 构建登录 URL
|
||||||
|
const callbackUrl = `http://localhost:${port}/callback`;
|
||||||
|
const config = getConfig();
|
||||||
|
const loginUrl = `${config.loginUrl}?redirect_uri=${encodeURIComponent(callbackUrl)}`;
|
||||||
|
|
||||||
|
console.log("🔐 登录服务器已启动,监听端口:", port);
|
||||||
|
console.log("🌐 登录 URL:", loginUrl);
|
||||||
|
|
||||||
|
// 打开浏览器登录
|
||||||
|
await vscode.env.openExternal(vscode.Uri.parse(loginUrl));
|
||||||
|
|
||||||
|
vscode.window.showInformationMessage(
|
||||||
|
"请在浏览器中完成登录,登录成功后将自动返回..."
|
||||||
|
);
|
||||||
|
|
||||||
|
// 等待 token(通过 Promise)
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
if (ICCoderAuthenticationProvider.loginServer) {
|
||||||
|
ICCoderAuthenticationProvider.loginServer.close();
|
||||||
|
ICCoderAuthenticationProvider.loginServer = null;
|
||||||
|
reject(new Error("登录超时"));
|
||||||
|
}
|
||||||
|
}, 5 * 60 * 1000);
|
||||||
|
|
||||||
|
// 将 resolve 和 reject 保存到服务器上下文
|
||||||
|
(server as any)._loginResolve = resolve;
|
||||||
|
(server as any)._loginReject = reject;
|
||||||
|
(server as any)._loginTimeout = timeout;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建本地回调服务器
|
||||||
|
*/
|
||||||
|
private createCallbackServer(): Promise<{
|
||||||
|
server: http.Server;
|
||||||
|
port: number;
|
||||||
|
}> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// 读取 icon.png 并转换为 Base64
|
||||||
|
const iconPath = path.join(
|
||||||
|
this.context.extensionPath,
|
||||||
|
"media",
|
||||||
|
"icon.png"
|
||||||
|
);
|
||||||
|
let iconBase64 = "";
|
||||||
|
try {
|
||||||
|
const iconBuffer = fs.readFileSync(iconPath);
|
||||||
|
iconBase64 = `data:image/png;base64,${iconBuffer.toString("base64")}`;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("无法读取 icon.png:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
const server = http.createServer(async (req, res) => {
|
||||||
|
try {
|
||||||
|
console.log("📥 收到回调请求:", req.url);
|
||||||
|
|
||||||
|
const url = new URL(
|
||||||
|
req.url!,
|
||||||
|
`http://localhost:${ICCoderAuthenticationProvider.currentPort}`
|
||||||
|
);
|
||||||
|
console.log("📍 路径:", url.pathname);
|
||||||
|
console.log("📋 所有参数:", Object.fromEntries(url.searchParams));
|
||||||
|
|
||||||
|
if (url.pathname === "/callback") {
|
||||||
|
const token = url.searchParams.get("token");
|
||||||
|
console.log("🔑 Token:", token ? "已获取" : "未找到");
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
// 返回成功页面
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": "text/html; charset=utf-8",
|
||||||
|
});
|
||||||
|
res.end(this.getSuccessPage(iconBase64));
|
||||||
|
|
||||||
|
// 关闭服务器
|
||||||
|
server.close();
|
||||||
|
ICCoderAuthenticationProvider.loginServer = null;
|
||||||
|
|
||||||
|
// 清除超时
|
||||||
|
if ((server as any)._loginTimeout) {
|
||||||
|
clearTimeout((server as any)._loginTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回 token
|
||||||
|
if ((server as any)._loginResolve) {
|
||||||
|
(server as any)._loginResolve(token);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.writeHead(400, {
|
||||||
|
"Content-Type": "text/html; charset=utf-8",
|
||||||
|
});
|
||||||
|
res.end(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head><meta charset="utf-8"><title>登录失败</title></head>
|
||||||
|
<body><h1>❌ 登录失败</h1><p>未获取到有效的 Token</p></body>
|
||||||
|
</html>
|
||||||
|
`);
|
||||||
|
|
||||||
|
if ((server as any)._loginReject) {
|
||||||
|
(server as any)._loginReject(new Error("未获取到有效的 Token"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.writeHead(404);
|
||||||
|
res.end("Not Found");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.writeHead(500);
|
||||||
|
res.end("Internal Server Error");
|
||||||
|
if ((server as any)._loginReject) {
|
||||||
|
(server as any)._loginReject(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 监听端口(使用 0 表示自动分配可用端口)
|
||||||
|
server.listen(0, () => {
|
||||||
|
const address = server.address();
|
||||||
|
const port =
|
||||||
|
typeof address === "object" && address ? address.port : 3000;
|
||||||
|
resolve({ server, port });
|
||||||
|
});
|
||||||
|
|
||||||
|
// 处理错误
|
||||||
|
server.on("error", (error: NodeJS.ErrnoException) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取登录成功页面 HTML
|
||||||
|
*/
|
||||||
|
private getSuccessPage(iconBase64: string): string {
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>登录成功 - IC Coder</title>
|
||||||
|
<style>
|
||||||
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
background: linear-gradient(135deg, #dbeafe 0%, #93c5fd 100%);
|
||||||
|
overflow: hidden;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.bg-circle {
|
||||||
|
position: absolute;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: rgba(255, 255, 255, 0.1);
|
||||||
|
animation: float 20s infinite ease-in-out;
|
||||||
|
}
|
||||||
|
.bg-circle:nth-child(1) { width: 300px; height: 300px; top: -150px; left: -150px; }
|
||||||
|
.bg-circle:nth-child(2) { width: 200px; height: 200px; bottom: -100px; right: -100px; animation-delay: 5s; }
|
||||||
|
.bg-circle:nth-child(3) { width: 150px; height: 150px; top: 50%; right: 10%; animation-delay: 10s; }
|
||||||
|
@keyframes float {
|
||||||
|
0%, 100% { transform: translate(0, 0) scale(1); }
|
||||||
|
33% { transform: translate(30px, -30px) scale(1.1); }
|
||||||
|
66% { transform: translate(-20px, 20px) scale(0.9); }
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
position: relative;
|
||||||
|
z-index: 10;
|
||||||
|
text-align: center;
|
||||||
|
background: rgba(255, 255, 255, 0.95);
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
padding: 60px 50px;
|
||||||
|
border-radius: 24px;
|
||||||
|
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
||||||
|
max-width: 500px;
|
||||||
|
animation: slideUp 0.6s ease-out;
|
||||||
|
}
|
||||||
|
@keyframes slideUp {
|
||||||
|
from { opacity: 0; transform: translateY(30px); }
|
||||||
|
to { opacity: 1; transform: translateY(0); }
|
||||||
|
}
|
||||||
|
.success-icon {
|
||||||
|
width: 80px;
|
||||||
|
height: 80px;
|
||||||
|
margin: 0 auto 30px;
|
||||||
|
background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
|
||||||
|
border-radius: 50%;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
animation: scaleIn 0.5s ease-out 0.2s both;
|
||||||
|
}
|
||||||
|
@keyframes scaleIn {
|
||||||
|
from { transform: scale(0); opacity: 0; }
|
||||||
|
to { transform: scale(1); opacity: 1; }
|
||||||
|
}
|
||||||
|
.checkmark {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border: 4px solid white;
|
||||||
|
border-radius: 50%;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.checkmark::after {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
left: 8px;
|
||||||
|
top: 3px;
|
||||||
|
width: 12px;
|
||||||
|
height: 20px;
|
||||||
|
border: solid white;
|
||||||
|
border-width: 0 4px 4px 0;
|
||||||
|
transform: rotate(45deg);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: #2d3748;
|
||||||
|
font-size: 32px;
|
||||||
|
font-weight: 700;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
animation: fadeIn 0.6s ease-out 0.3s both;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
color: #718096;
|
||||||
|
font-size: 18px;
|
||||||
|
line-height: 1.6;
|
||||||
|
margin-bottom: 30px;
|
||||||
|
animation: fadeIn 0.6s ease-out 0.4s both;
|
||||||
|
}
|
||||||
|
@keyframes fadeIn {
|
||||||
|
from { opacity: 0; }
|
||||||
|
to { opacity: 1; }
|
||||||
|
}
|
||||||
|
.brand {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 20px;
|
||||||
|
padding-top: 20px;
|
||||||
|
border-top: 1px solid #e2e8f0;
|
||||||
|
animation: fadeIn 0.6s ease-out 0.5s both;
|
||||||
|
}
|
||||||
|
.brand-logo {
|
||||||
|
width: 32px;
|
||||||
|
height: 32px;
|
||||||
|
border-radius: 6px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.brand-logo img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
object-fit: cover;
|
||||||
|
}
|
||||||
|
.brand-text {
|
||||||
|
color: #4a5568;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="bg-circle"></div>
|
||||||
|
<div class="bg-circle"></div>
|
||||||
|
<div class="bg-circle"></div>
|
||||||
|
<div class="container">
|
||||||
|
<div class="success-icon">
|
||||||
|
<div class="checkmark"></div>
|
||||||
|
</div>
|
||||||
|
<h1>登录成功!</h1>
|
||||||
|
<p>您已成功登录 IC Coder<br>现在可以返回 VSCode 继续使用</p>
|
||||||
|
<div class="brand">
|
||||||
|
<div class="brand-logo">
|
||||||
|
<img src="${iconBase64}" alt="IC Coder" />
|
||||||
|
</div>
|
||||||
|
<span class="brand-text">IC Coder</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
113
src/services/invitationService.ts
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
/**
|
||||||
|
* 邀请码验证服务
|
||||||
|
*/
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
import { verifyInvitationCode, checkInvitationStatus } from "./apiClient";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码验证服务类
|
||||||
|
*/
|
||||||
|
export class InvitationService {
|
||||||
|
/**
|
||||||
|
* 检查用户是否已验证邀请码
|
||||||
|
*/
|
||||||
|
static async isVerified(context: vscode.ExtensionContext): Promise<boolean> {
|
||||||
|
// 【临时】使用本地验证,不调用后端
|
||||||
|
const localVerified = context.globalState.get<boolean>(
|
||||||
|
"invitationCodeVerified",
|
||||||
|
);
|
||||||
|
return localVerified || false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证邀请码
|
||||||
|
*/
|
||||||
|
static async verifyCode(
|
||||||
|
code: string,
|
||||||
|
): Promise<{ success: boolean; message: string }> {
|
||||||
|
try {
|
||||||
|
// console.log('[InvitationService] ========== 开始验证邀请码 ==========');
|
||||||
|
// console.log('[InvitationService] 邀请码:', code);
|
||||||
|
// console.log('[InvitationService] 邀请码长度:', code.length);
|
||||||
|
|
||||||
|
const response = await verifyInvitationCode(code);
|
||||||
|
|
||||||
|
// console.log('[InvitationService] 收到响应:', JSON.stringify(response, null, 2));
|
||||||
|
// console.log('[InvitationService] 响应代码:', response.code);
|
||||||
|
// console.log('[InvitationService] 响应消息:', response.msg);
|
||||||
|
// console.log('[InvitationService] 验证结果:', response.data?.verified);
|
||||||
|
|
||||||
|
if (response.code === 200 && response.data?.verified) {
|
||||||
|
console.log("[InvitationService] ✓ 验证成功");
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: response.msg || "验证成功",
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
console.log("[InvitationService] ✗ 验证失败");
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: response.msg || "验证失败",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
// console.error('[InvitationService] ========== 验证邀请码异常 ==========');
|
||||||
|
// console.error('[InvitationService] 错误类型:', error.constructor.name);
|
||||||
|
// console.error('[InvitationService] 错误消息:', error.message);
|
||||||
|
// console.error('[InvitationService] 错误堆栈:', error.stack);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: error.message || "网络连接失败,请检查网络后重试",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存验证状态到本地
|
||||||
|
*/
|
||||||
|
static async saveVerificationStatus(
|
||||||
|
context: vscode.ExtensionContext,
|
||||||
|
code: string,
|
||||||
|
verifiedTime?: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await context.globalState.update("invitationCodeVerified", true);
|
||||||
|
await context.globalState.update("invitationCode", code);
|
||||||
|
await context.globalState.update(
|
||||||
|
"invitationVerifiedTime",
|
||||||
|
verifiedTime || new Date().toISOString(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除验证状态(用于退出登录或更换邀请码)
|
||||||
|
*/
|
||||||
|
static async clearVerificationStatus(
|
||||||
|
context: vscode.ExtensionContext,
|
||||||
|
): Promise<void> {
|
||||||
|
await context.globalState.update("invitationCodeVerified", undefined);
|
||||||
|
await context.globalState.update("invitationCode", undefined);
|
||||||
|
await context.globalState.update("invitationVerifiedTime", undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示邀请码输入弹窗
|
||||||
|
*/
|
||||||
|
static async showInputDialog(): Promise<string | undefined> {
|
||||||
|
const code = await vscode.window.showInputBox({
|
||||||
|
prompt: "请输入邀请码以继续使用 IC Coder",
|
||||||
|
placeHolder: "例如:INVITE2024ABC",
|
||||||
|
ignoreFocusOut: true,
|
||||||
|
validateInput: (value) => {
|
||||||
|
if (!value || value.trim().length === 0) {
|
||||||
|
return "邀请码不能为空";
|
||||||
|
}
|
||||||
|
if (value.trim().length < 6) {
|
||||||
|
return "邀请码格式不正确";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return code?.trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
257
src/services/notificationService.ts
Normal file
@ -0,0 +1,257 @@
|
|||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
// 使用 require 导入 node-notifier
|
||||||
|
const notifier = require('node-notifier');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知类型枚举
|
||||||
|
*/
|
||||||
|
export enum NotificationType {
|
||||||
|
INFO = 'info',
|
||||||
|
SUCCESS = 'success',
|
||||||
|
WARNING = 'warning',
|
||||||
|
ERROR = 'error'
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知选项接口
|
||||||
|
*/
|
||||||
|
export interface NotificationOptions {
|
||||||
|
/** 通知标题 */
|
||||||
|
title: string;
|
||||||
|
/** 通知消息 */
|
||||||
|
message: string;
|
||||||
|
/** 通知类型 */
|
||||||
|
type?: NotificationType;
|
||||||
|
/** 是否播放声音 */
|
||||||
|
sound?: boolean;
|
||||||
|
/** 超时时间(秒),0 表示不自动消失 */
|
||||||
|
timeout?: number;
|
||||||
|
/** 自定义图标路径 */
|
||||||
|
icon?: string;
|
||||||
|
/** 点击通知时的回调 */
|
||||||
|
onClick?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统通知服务类
|
||||||
|
*/
|
||||||
|
export class NotificationService {
|
||||||
|
private static instance: NotificationService;
|
||||||
|
private readonly extensionPath: string;
|
||||||
|
private readonly iconPath: string;
|
||||||
|
private lastNotificationTime: Map<string, number> = new Map();
|
||||||
|
private readonly DEBOUNCE_INTERVAL = 3000; // 3 秒防抖
|
||||||
|
|
||||||
|
private constructor(context: vscode.ExtensionContext) {
|
||||||
|
this.extensionPath = context.extensionPath;
|
||||||
|
this.iconPath = path.join(this.extensionPath, 'media', 'icon.png');
|
||||||
|
console.log('[NotificationService] 初始化通知服务');
|
||||||
|
console.log('[NotificationService] 扩展路径:', this.extensionPath);
|
||||||
|
console.log('[NotificationService] 图标路径:', this.iconPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例实例
|
||||||
|
*/
|
||||||
|
public static getInstance(context?: vscode.ExtensionContext): NotificationService {
|
||||||
|
if (!NotificationService.instance && context) {
|
||||||
|
NotificationService.instance = new NotificationService(context);
|
||||||
|
}
|
||||||
|
return NotificationService.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否启用系统通知
|
||||||
|
*/
|
||||||
|
private isSystemNotificationEnabled(): boolean {
|
||||||
|
const config = vscode.workspace.getConfiguration('ic-coder');
|
||||||
|
return config.get<boolean>('enableSystemNotification', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否应该发送通知(防抖)
|
||||||
|
*/
|
||||||
|
private shouldSendNotification(key: string): boolean {
|
||||||
|
const now = Date.now();
|
||||||
|
const lastTime = this.lastNotificationTime.get(key) || 0;
|
||||||
|
|
||||||
|
if (now - lastTime < this.DEBOUNCE_INTERVAL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastNotificationTime.set(key, now);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送系统通知
|
||||||
|
*/
|
||||||
|
public sendNotification(options: NotificationOptions): void {
|
||||||
|
console.log('[NotificationService] ========== 开始发送通知 ==========');
|
||||||
|
console.log('[NotificationService] 通知选项:', options);
|
||||||
|
|
||||||
|
// 检查用户配置
|
||||||
|
if (!this.isSystemNotificationEnabled()) {
|
||||||
|
console.log('[NotificationService] 系统通知已禁用');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log('[NotificationService] 系统通知已启用');
|
||||||
|
|
||||||
|
const {
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type = NotificationType.INFO,
|
||||||
|
onClick
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
// 防抖检查
|
||||||
|
const notificationKey = `${title}-${message}`;
|
||||||
|
if (!this.shouldSendNotification(notificationKey)) {
|
||||||
|
console.log('[NotificationService] 通知被防抖机制拦截');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log('[NotificationService] 通过防抖检查');
|
||||||
|
|
||||||
|
// 使用 node-notifier 发送系统通知
|
||||||
|
console.log('[NotificationService] 使用 node-notifier 发送系统通知');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const notificationConfig: any = {
|
||||||
|
title: title,
|
||||||
|
message: message,
|
||||||
|
sound: true,
|
||||||
|
wait: false,
|
||||||
|
timeout: 10,
|
||||||
|
appID: 'IC Coder'
|
||||||
|
};
|
||||||
|
|
||||||
|
// Windows 特定配置
|
||||||
|
if (process.platform === 'win32') {
|
||||||
|
notificationConfig.icon = this.iconPath;
|
||||||
|
console.log('[NotificationService] Windows 平台,图标路径:', this.iconPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[NotificationService] 通知配置:', notificationConfig);
|
||||||
|
|
||||||
|
notifier.notify(notificationConfig, (err: any, response: any, metadata: any) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('[NotificationService] ❌ node-notifier 失败:', err);
|
||||||
|
} else {
|
||||||
|
console.log('[NotificationService] ✅ node-notifier 成功');
|
||||||
|
console.log('[NotificationService] 响应:', response);
|
||||||
|
console.log('[NotificationService] 元数据:', metadata);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (onClick) {
|
||||||
|
notifier.on('click', () => {
|
||||||
|
console.log('[NotificationService] 用户点击了系统通知');
|
||||||
|
onClick();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[NotificationService] ❌ node-notifier 异常:', error);
|
||||||
|
// 如果系统通知失败,显示 VS Code 内置通知作为备用
|
||||||
|
console.log('[NotificationService] 系统通知失败,显示 VS Code 内置通知');
|
||||||
|
this.showVSCodeNotification(title, message, type, onClick);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示 VS Code 内置通知
|
||||||
|
*/
|
||||||
|
private showVSCodeNotification(
|
||||||
|
title: string,
|
||||||
|
message: string,
|
||||||
|
type: NotificationType,
|
||||||
|
onClick?: () => void
|
||||||
|
): void {
|
||||||
|
const fullMessage = `${title}: ${message}`;
|
||||||
|
console.log('[NotificationService] 显示 VS Code 通知:', fullMessage);
|
||||||
|
|
||||||
|
let notificationPromise: Thenable<string | undefined>;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case NotificationType.ERROR:
|
||||||
|
notificationPromise = vscode.window.showErrorMessage(fullMessage, '查看详情');
|
||||||
|
break;
|
||||||
|
case NotificationType.WARNING:
|
||||||
|
notificationPromise = vscode.window.showWarningMessage(fullMessage, '查看详情');
|
||||||
|
break;
|
||||||
|
case NotificationType.SUCCESS:
|
||||||
|
case NotificationType.INFO:
|
||||||
|
default:
|
||||||
|
notificationPromise = vscode.window.showInformationMessage(fullMessage, '查看详情');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理点击事件
|
||||||
|
if (onClick) {
|
||||||
|
notificationPromise.then((selection) => {
|
||||||
|
if (selection === '查看详情') {
|
||||||
|
console.log('[NotificationService] 用户点击了通知');
|
||||||
|
onClick();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送成功通知
|
||||||
|
*/
|
||||||
|
public success(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.SUCCESS,
|
||||||
|
sound: true,
|
||||||
|
timeout: 10,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送错误通知
|
||||||
|
*/
|
||||||
|
public error(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.ERROR,
|
||||||
|
sound: true,
|
||||||
|
timeout: 15,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送警告通知
|
||||||
|
*/
|
||||||
|
public warning(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.WARNING,
|
||||||
|
sound: true,
|
||||||
|
timeout: 10,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送信息通知
|
||||||
|
*/
|
||||||
|
public info(title: string, message: string, onClick?: () => void): void {
|
||||||
|
this.sendNotification({
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
type: NotificationType.INFO,
|
||||||
|
sound: false,
|
||||||
|
timeout: 8,
|
||||||
|
onClick
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
103
src/services/promptOptimizeService.ts
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
/**
|
||||||
|
* 提示词优化服务
|
||||||
|
* 调用后端 API 优化用户输入的提示词
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as https from 'https';
|
||||||
|
import * as http from 'http';
|
||||||
|
import { URL } from 'url';
|
||||||
|
import { getApiUrl } from '../config/settings';
|
||||||
|
|
||||||
|
/** 优化响应类型 */
|
||||||
|
interface OptimizeResponse {
|
||||||
|
success: boolean;
|
||||||
|
optimizedPrompt?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 优化提示词
|
||||||
|
* @param prompt 原始提示词
|
||||||
|
* @returns 优化后的提示词
|
||||||
|
*/
|
||||||
|
export async function optimizePrompt(prompt: string): Promise<string> {
|
||||||
|
// 获取 JWT token
|
||||||
|
const session = await vscode.authentication.getSession('iccoder', [], { silent: true });
|
||||||
|
if (!session?.accessToken) {
|
||||||
|
throw new Error('未登录,请先登录');
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await callOptimizeApi(prompt, session.accessToken);
|
||||||
|
|
||||||
|
if (response.success && response.optimizedPrompt) {
|
||||||
|
return response.optimizedPrompt;
|
||||||
|
} else {
|
||||||
|
throw new Error(response.error || '优化失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用后端优化 API
|
||||||
|
*/
|
||||||
|
async function callOptimizeApi(prompt: string, token: string): Promise<OptimizeResponse> {
|
||||||
|
const urlStr = getApiUrl('/api/prompt/optimize');
|
||||||
|
const url = new URL(urlStr);
|
||||||
|
|
||||||
|
const isHttps = url.protocol === 'https:';
|
||||||
|
const httpModule = isHttps ? https : http;
|
||||||
|
|
||||||
|
const body = JSON.stringify({ prompt });
|
||||||
|
|
||||||
|
const requestOptions: http.RequestOptions = {
|
||||||
|
hostname: url.hostname,
|
||||||
|
port: url.port || (isHttps ? 443 : 80),
|
||||||
|
path: url.pathname + url.search,
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Content-Length': Buffer.byteLength(body),
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
},
|
||||||
|
timeout: 30000
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const req = httpModule.request(requestOptions, (res) => {
|
||||||
|
let data = '';
|
||||||
|
|
||||||
|
res.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
console.log('[PromptOptimize] 响应状态码:', res.statusCode);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
resolve(json as OptimizeResponse);
|
||||||
|
} else if (res.statusCode === 401 || res.statusCode === 403) {
|
||||||
|
resolve({ success: false, error: '登录已过期,请重新登录' });
|
||||||
|
} else {
|
||||||
|
resolve({ success: false, error: json.error || json.message || `HTTP ${res.statusCode}` });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
resolve({ success: false, error: `解析响应失败: ${data}` });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('timeout', () => {
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error('请求超时'));
|
||||||
|
});
|
||||||
|
|
||||||
|
req.write(body);
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
411
src/services/sseHandler.ts
Normal file
@ -0,0 +1,411 @@
|
|||||||
|
/**
|
||||||
|
* SSE 事件处理器
|
||||||
|
* 处理与后端的流式通信
|
||||||
|
* 使用 eventsource-parser + Node.js 原生 http 模块
|
||||||
|
*/
|
||||||
|
import * as http from 'http';
|
||||||
|
import * as https from 'https';
|
||||||
|
import { URL } from 'url';
|
||||||
|
import { createParser, type EventSourceParser } from 'eventsource-parser';
|
||||||
|
import { getApiUrl, getConfig } from '../config/settings';
|
||||||
|
import type {
|
||||||
|
DialogRequest,
|
||||||
|
SSEEventType,
|
||||||
|
TextDeltaEvent,
|
||||||
|
ToolCallRequest,
|
||||||
|
ToolConfirmEvent,
|
||||||
|
PlanConfirmEvent,
|
||||||
|
AskUserEvent,
|
||||||
|
CompleteEvent,
|
||||||
|
ErrorEvent,
|
||||||
|
ToolStartEvent,
|
||||||
|
ToolCompleteEvent,
|
||||||
|
ToolErrorEvent,
|
||||||
|
WarningEvent,
|
||||||
|
NotificationEvent,
|
||||||
|
DepthUpdateEvent,
|
||||||
|
AgentStartEvent,
|
||||||
|
AgentProgressEvent,
|
||||||
|
AgentCompleteEvent,
|
||||||
|
AgentErrorEvent,
|
||||||
|
ContextUsageEvent,
|
||||||
|
CreditUpdateEvent
|
||||||
|
} from '../types/api';
|
||||||
|
import type { MemoryCompactedEvent } from '../types/memory';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SSE 事件回调接口
|
||||||
|
*/
|
||||||
|
export interface SSECallbacks {
|
||||||
|
/** 收到文本增量 */
|
||||||
|
onTextDelta?: (data: TextDeltaEvent) => void;
|
||||||
|
/** 收到工具调用请求 */
|
||||||
|
onToolCall?: (data: ToolCallRequest) => void;
|
||||||
|
/** 收到工具确认请求(Ask 模式) */
|
||||||
|
onToolConfirm?: (data: ToolConfirmEvent) => void;
|
||||||
|
/** 收到计划确认请求(Plan 模式) */
|
||||||
|
onPlanConfirm?: (data: PlanConfirmEvent) => void;
|
||||||
|
/** 阶段进度更新 */
|
||||||
|
onPhaseProgress?: (data: import('../types/api').PhaseProgressEvent) => void;
|
||||||
|
/** 添加计划步骤 */
|
||||||
|
onPlanStepAdd?: (data: import('../types/api').PlanStepAddEvent) => void;
|
||||||
|
/** 删除计划步骤 */
|
||||||
|
onPlanStepRemove?: (data: import('../types/api').PlanStepRemoveEvent) => void;
|
||||||
|
/** 更新计划步骤 */
|
||||||
|
onPlanStepUpdate?: (data: import('../types/api').PlanStepUpdateEvent) => void;
|
||||||
|
/** 更新计划摘要 */
|
||||||
|
onPlanSummaryUpdate?: (data: import('../types/api').PlanSummaryUpdateEvent) => void;
|
||||||
|
/** 工具开始执行 */
|
||||||
|
onToolStart?: (data: ToolStartEvent) => void;
|
||||||
|
/** 工具执行完成 */
|
||||||
|
onToolComplete?: (data: ToolCompleteEvent) => void;
|
||||||
|
/** 工具执行错误 */
|
||||||
|
onToolError?: (data: ToolErrorEvent) => void;
|
||||||
|
/** 收到用户提问 */
|
||||||
|
onAskUser?: (data: AskUserEvent) => void;
|
||||||
|
/** 对话完成 */
|
||||||
|
onComplete?: (data: CompleteEvent) => void;
|
||||||
|
/** 错误 */
|
||||||
|
onError?: (data: ErrorEvent) => void;
|
||||||
|
/** 警告 */
|
||||||
|
onWarning?: (data: WarningEvent) => void;
|
||||||
|
/** 通知 */
|
||||||
|
onNotification?: (data: NotificationEvent) => void;
|
||||||
|
/** 深度更新 */
|
||||||
|
onDepthUpdate?: (data: DepthUpdateEvent) => void;
|
||||||
|
/** 子智能体启动 */
|
||||||
|
onAgentStart?: (data: AgentStartEvent) => void;
|
||||||
|
/** 子智能体进度 */
|
||||||
|
onAgentProgress?: (data: AgentProgressEvent) => void;
|
||||||
|
/** 子智能体完成 */
|
||||||
|
onAgentComplete?: (data: AgentCompleteEvent) => void;
|
||||||
|
/** 子智能体错误 */
|
||||||
|
onAgentError?: (data: AgentErrorEvent) => void;
|
||||||
|
/** 记忆压缩完成 */
|
||||||
|
onMemoryCompacted?: (data: MemoryCompactedEvent) => void;
|
||||||
|
/** 上下文使用量更新 */
|
||||||
|
onContextUsage?: (data: ContextUsageEvent) => void;
|
||||||
|
/** 资源点余额更新 */
|
||||||
|
onCreditUpdate?: (data: CreditUpdateEvent) => void;
|
||||||
|
/** 连接打开 */
|
||||||
|
onOpen?: () => void;
|
||||||
|
/** 连接关闭 */
|
||||||
|
onClose?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SSE 会话控制器
|
||||||
|
*/
|
||||||
|
export class SSEController {
|
||||||
|
private request: http.ClientRequest | null = null;
|
||||||
|
private isConnected = false;
|
||||||
|
private isAborted = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否已连接
|
||||||
|
*/
|
||||||
|
get connected(): boolean {
|
||||||
|
return this.isConnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置请求对象
|
||||||
|
*/
|
||||||
|
setRequest(req: http.ClientRequest): void {
|
||||||
|
this.request = req;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置连接状态
|
||||||
|
*/
|
||||||
|
setConnected(connected: boolean): void {
|
||||||
|
this.isConnected = connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否已中止
|
||||||
|
*/
|
||||||
|
get aborted(): boolean {
|
||||||
|
return this.isAborted;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中止当前连接
|
||||||
|
*/
|
||||||
|
abort(): void {
|
||||||
|
if (this.request && !this.isAborted) {
|
||||||
|
this.isAborted = true;
|
||||||
|
this.request.destroy();
|
||||||
|
this.request = null;
|
||||||
|
this.isConnected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发起流式对话
|
||||||
|
* @param request 对话请求
|
||||||
|
* @param callbacks 事件回调
|
||||||
|
* @returns SSE 控制器(用于中止连接)
|
||||||
|
*/
|
||||||
|
export async function startStreamDialog(
|
||||||
|
request: DialogRequest,
|
||||||
|
callbacks: SSECallbacks
|
||||||
|
): Promise<SSEController> {
|
||||||
|
const controller = new SSEController();
|
||||||
|
|
||||||
|
const urlString = getApiUrl('/api/dialog/stream');
|
||||||
|
const url = new URL(urlString);
|
||||||
|
const isHttps = url.protocol === 'https:';
|
||||||
|
const httpModule = isHttps ? https : http;
|
||||||
|
|
||||||
|
const body = JSON.stringify(request);
|
||||||
|
|
||||||
|
console.log(`[SSE] 开始流式对话: taskId=${request.taskId}, mode=${request.mode}, url=${urlString}`);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const options: http.RequestOptions = {
|
||||||
|
hostname: url.hostname,
|
||||||
|
port: url.port || (isHttps ? 443 : 80),
|
||||||
|
path: url.pathname + url.search,
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'text/event-stream',
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
'Content-Length': Buffer.byteLength(body),
|
||||||
|
...(request.token ? { 'Authorization': `Bearer ${request.token}` } : {})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const req = httpModule.request(options, (res) => {
|
||||||
|
// 检查响应状态
|
||||||
|
if (res.statusCode !== 200) {
|
||||||
|
let errorBody = '';
|
||||||
|
res.on('data', chunk => errorBody += chunk);
|
||||||
|
res.on('end', () => {
|
||||||
|
// 检测是否是登录状态过期
|
||||||
|
const isLoginExpired = errorBody.includes('登录状态已过期') ||
|
||||||
|
errorBody.includes('token') && errorBody.includes('过期') ||
|
||||||
|
res.statusCode === 401;
|
||||||
|
|
||||||
|
if (isLoginExpired) {
|
||||||
|
const error = new Error('LOGIN_EXPIRED:登录状态已过期,请重新登录');
|
||||||
|
callbacks.onError?.({ message: error.message });
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
const error = new Error(`SSE 连接失败: ${res.statusCode} ${errorBody}`);
|
||||||
|
callbacks.onError?.({ message: error.message });
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接成功
|
||||||
|
console.log('[SSE] 连接已建立');
|
||||||
|
controller.setConnected(true);
|
||||||
|
callbacks.onOpen?.();
|
||||||
|
resolve(controller);
|
||||||
|
|
||||||
|
// 创建 SSE 解析器
|
||||||
|
const parser = createParser({
|
||||||
|
onEvent: (event) => {
|
||||||
|
const eventType = event.event as SSEEventType;
|
||||||
|
const eventData = event.data;
|
||||||
|
|
||||||
|
if (!eventData) {
|
||||||
|
console.log(`[SSE] 收到空事件: ${eventType}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(eventData);
|
||||||
|
console.log(`[SSE] 收到事件: ${eventType}`, data);
|
||||||
|
|
||||||
|
// 分发事件到对应回调
|
||||||
|
dispatchEvent(eventType, data, callbacks);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[SSE] 解析事件数据失败: ${eventData}`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 设置编码
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
|
||||||
|
// 处理数据流
|
||||||
|
res.on('data', (chunk: string) => {
|
||||||
|
if (!controller.aborted) {
|
||||||
|
console.log('[SSE] 收到原始数据块:', chunk.substring(0, 200));
|
||||||
|
|
||||||
|
// 检查是否是业务错误码(Gateway 返回 HTTP 200 但响应体是错误 JSON)
|
||||||
|
try {
|
||||||
|
const trimmed = chunk.trim();
|
||||||
|
if (trimmed.startsWith('{') && trimmed.includes('"code"')) {
|
||||||
|
const json = JSON.parse(trimmed);
|
||||||
|
if (json.code === 401 || json.msg?.includes('登录状态已过期')) {
|
||||||
|
console.log('[SSE] 检测到登录过期业务错误');
|
||||||
|
const error = new Error('LOGIN_EXPIRED:登录状态已过期,请重新登录');
|
||||||
|
callbacks.onError?.({ message: error.message });
|
||||||
|
controller.abort();
|
||||||
|
reject(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 不是 JSON 格式,继续正常处理
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.feed(chunk);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 处理连接关闭
|
||||||
|
res.on('end', () => {
|
||||||
|
console.log('[SSE] 连接已关闭');
|
||||||
|
controller.setConnected(false);
|
||||||
|
callbacks.onClose?.();
|
||||||
|
});
|
||||||
|
|
||||||
|
// 处理错误
|
||||||
|
res.on('error', (err) => {
|
||||||
|
if (!controller.aborted) {
|
||||||
|
console.error('[SSE] 响应错误:', err);
|
||||||
|
controller.setConnected(false);
|
||||||
|
callbacks.onError?.({ message: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 保存请求引用用于中止
|
||||||
|
controller.setRequest(req);
|
||||||
|
|
||||||
|
// 处理请求错误
|
||||||
|
req.on('error', (err) => {
|
||||||
|
if (!controller.aborted) {
|
||||||
|
console.error('[SSE] 请求错误:', err);
|
||||||
|
controller.setConnected(false);
|
||||||
|
callbacks.onError?.({ message: err.message });
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 处理超时
|
||||||
|
const { timeout } = getConfig();
|
||||||
|
req.setTimeout(timeout, () => {
|
||||||
|
if (!controller.aborted) {
|
||||||
|
console.error('[SSE] 请求超时');
|
||||||
|
controller.abort();
|
||||||
|
const error = new Error('请求超时');
|
||||||
|
callbacks.onError?.({ message: error.message });
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 发送请求体
|
||||||
|
req.write(body);
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分发 SSE 事件到对应回调
|
||||||
|
*/
|
||||||
|
function dispatchEvent(
|
||||||
|
eventType: SSEEventType,
|
||||||
|
data: unknown,
|
||||||
|
callbacks: SSECallbacks
|
||||||
|
): void {
|
||||||
|
switch (eventType) {
|
||||||
|
case 'text_delta':
|
||||||
|
callbacks.onTextDelta?.(data as TextDeltaEvent);
|
||||||
|
break;
|
||||||
|
case 'tool_call':
|
||||||
|
callbacks.onToolCall?.(data as ToolCallRequest);
|
||||||
|
break;
|
||||||
|
case 'tool_confirm':
|
||||||
|
callbacks.onToolConfirm?.(data as ToolConfirmEvent);
|
||||||
|
break;
|
||||||
|
case 'plan_confirm':
|
||||||
|
callbacks.onPlanConfirm?.(data as PlanConfirmEvent);
|
||||||
|
break;
|
||||||
|
case 'phase_progress':
|
||||||
|
callbacks.onPhaseProgress?.(data as import('../types/api').PhaseProgressEvent);
|
||||||
|
break;
|
||||||
|
case 'plan_step_add':
|
||||||
|
callbacks.onPlanStepAdd?.(data as import('../types/api').PlanStepAddEvent);
|
||||||
|
break;
|
||||||
|
case 'plan_step_remove':
|
||||||
|
callbacks.onPlanStepRemove?.(data as import('../types/api').PlanStepRemoveEvent);
|
||||||
|
break;
|
||||||
|
case 'plan_step_update':
|
||||||
|
callbacks.onPlanStepUpdate?.(data as import('../types/api').PlanStepUpdateEvent);
|
||||||
|
break;
|
||||||
|
case 'plan_summary_update':
|
||||||
|
callbacks.onPlanSummaryUpdate?.(data as import('../types/api').PlanSummaryUpdateEvent);
|
||||||
|
break;
|
||||||
|
case 'tool_start':
|
||||||
|
callbacks.onToolStart?.(data as ToolStartEvent);
|
||||||
|
break;
|
||||||
|
case 'tool_complete':
|
||||||
|
callbacks.onToolComplete?.(data as ToolCompleteEvent);
|
||||||
|
break;
|
||||||
|
case 'tool_error':
|
||||||
|
callbacks.onToolError?.(data as ToolErrorEvent);
|
||||||
|
break;
|
||||||
|
case 'ask_user':
|
||||||
|
callbacks.onAskUser?.(data as AskUserEvent);
|
||||||
|
break;
|
||||||
|
case 'complete':
|
||||||
|
callbacks.onComplete?.(data as CompleteEvent);
|
||||||
|
break;
|
||||||
|
case 'error':
|
||||||
|
callbacks.onError?.(data as ErrorEvent);
|
||||||
|
break;
|
||||||
|
case 'warning':
|
||||||
|
callbacks.onWarning?.(data as WarningEvent);
|
||||||
|
break;
|
||||||
|
case 'notification':
|
||||||
|
callbacks.onNotification?.(data as NotificationEvent);
|
||||||
|
break;
|
||||||
|
case 'depth_update':
|
||||||
|
callbacks.onDepthUpdate?.(data as DepthUpdateEvent);
|
||||||
|
break;
|
||||||
|
case 'agent_start':
|
||||||
|
callbacks.onAgentStart?.(data as AgentStartEvent);
|
||||||
|
break;
|
||||||
|
case 'agent_progress':
|
||||||
|
callbacks.onAgentProgress?.(data as AgentProgressEvent);
|
||||||
|
break;
|
||||||
|
case 'agent_complete':
|
||||||
|
callbacks.onAgentComplete?.(data as AgentCompleteEvent);
|
||||||
|
break;
|
||||||
|
case 'agent_error':
|
||||||
|
callbacks.onAgentError?.(data as AgentErrorEvent);
|
||||||
|
break;
|
||||||
|
case 'memory_compacted':
|
||||||
|
callbacks.onMemoryCompacted?.(data as MemoryCompactedEvent);
|
||||||
|
break;
|
||||||
|
case 'context_usage':
|
||||||
|
callbacks.onContextUsage?.(data as ContextUsageEvent);
|
||||||
|
break;
|
||||||
|
case 'credit_update':
|
||||||
|
callbacks.onCreditUpdate?.(data as CreditUpdateEvent);
|
||||||
|
break;
|
||||||
|
case 'heartbeat':
|
||||||
|
// 心跳事件:仅用于保持连接,不需要特殊处理
|
||||||
|
// Node.js req.setTimeout 会在收到数据时自动重置计时器
|
||||||
|
console.log('[SSE] 收到心跳');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log(`[SSE] 未知事件类型: ${eventType}`, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成任务ID
|
||||||
|
*/
|
||||||
|
export function generateTaskId(): string {
|
||||||
|
return `task-${Date.now()}-${Math.random().toString(36).substring(2, 8)}`;
|
||||||
|
}
|
||||||
525
src/services/toolExecutor.ts
Normal file
@ -0,0 +1,525 @@
|
|||||||
|
/**
|
||||||
|
* 工具执行器
|
||||||
|
* 接收后端的 tool_call 事件,执行本地工具,返回结果
|
||||||
|
*/
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as os from 'os';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import { readFileContent, readDirectory } from '../utils/readFiles';
|
||||||
|
import { createOrOverwriteFile } from '../utils/createFiles';
|
||||||
|
import { generateVCD, checkIverilogAvailable, generateMultiVCD, DumpModule } from '../utils/iverilogRunner';
|
||||||
|
import { analyzeVcdFile } from '../utils/vcdParser';
|
||||||
|
import { executeWaveformTrace, WaveformTraceArgs } from '../utils/waveformTracer';
|
||||||
|
import {
|
||||||
|
submitToolResult,
|
||||||
|
createSuccessResult,
|
||||||
|
createBusinessErrorResult,
|
||||||
|
createSystemErrorResult
|
||||||
|
} from './apiClient';
|
||||||
|
import type {
|
||||||
|
ToolCallRequest,
|
||||||
|
ToolName,
|
||||||
|
FileReadArgs,
|
||||||
|
FileWriteArgs,
|
||||||
|
FileDeleteArgs,
|
||||||
|
FileListArgs,
|
||||||
|
SyntaxCheckArgs,
|
||||||
|
IverilogArgs,
|
||||||
|
SimulationArgs,
|
||||||
|
WaveformSummaryArgs,
|
||||||
|
KnowledgeSaveArgs,
|
||||||
|
KnowledgeLoadArgs
|
||||||
|
} from '../types/api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具执行器上下文
|
||||||
|
*/
|
||||||
|
export interface ToolExecutorContext {
|
||||||
|
/** 扩展路径(用于 iverilog) */
|
||||||
|
extensionPath: string;
|
||||||
|
/** 工作区路径 */
|
||||||
|
workspacePath: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行工具调用
|
||||||
|
* @param request 工具调用请求
|
||||||
|
* @param context 执行上下文
|
||||||
|
*/
|
||||||
|
export async function executeToolCall(
|
||||||
|
request: ToolCallRequest,
|
||||||
|
context: ToolExecutorContext
|
||||||
|
): Promise<void> {
|
||||||
|
const toolName = request.params.name as ToolName;
|
||||||
|
const args = request.params.arguments;
|
||||||
|
const callId = request.id;
|
||||||
|
|
||||||
|
console.log(`[ToolExecutor] 执行工具: ${toolName}, callId=${callId}`, args);
|
||||||
|
|
||||||
|
try {
|
||||||
|
let resultText: string;
|
||||||
|
|
||||||
|
switch (toolName) {
|
||||||
|
case 'file_read':
|
||||||
|
resultText = await executeFileRead(args as unknown as FileReadArgs);
|
||||||
|
break;
|
||||||
|
case 'file_write':
|
||||||
|
resultText = await executeFileWrite(args as unknown as FileWriteArgs);
|
||||||
|
break;
|
||||||
|
case 'file_delete':
|
||||||
|
resultText = await executeFileDelete(args as unknown as FileDeleteArgs);
|
||||||
|
break;
|
||||||
|
case 'file_list':
|
||||||
|
resultText = await executeFileList(args as unknown as FileListArgs);
|
||||||
|
break;
|
||||||
|
case 'syntax_check':
|
||||||
|
resultText = await executeSyntaxCheck(args as unknown as SyntaxCheckArgs, context);
|
||||||
|
break;
|
||||||
|
case 'iverilog':
|
||||||
|
resultText = await executeIverilog(args as unknown as IverilogArgs, context);
|
||||||
|
break;
|
||||||
|
case 'simulation':
|
||||||
|
resultText = await executeSimulation(args as unknown as SimulationArgs, context);
|
||||||
|
break;
|
||||||
|
case 'waveform_summary':
|
||||||
|
resultText = await executeWaveformSummary(args as unknown as WaveformSummaryArgs);
|
||||||
|
break;
|
||||||
|
case 'waveform_trace':
|
||||||
|
resultText = await executeWaveformTrace(args as unknown as WaveformTraceArgs, context);
|
||||||
|
break;
|
||||||
|
case 'knowledge_save':
|
||||||
|
resultText = await executeKnowledgeSave(args as unknown as KnowledgeSaveArgs);
|
||||||
|
break;
|
||||||
|
case 'knowledge_load':
|
||||||
|
resultText = await executeKnowledgeLoad();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`未知工具: ${toolName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提交成功结果
|
||||||
|
const result = createSuccessResult(callId, resultText);
|
||||||
|
await submitToolResult(result);
|
||||||
|
console.log(`[ToolExecutor] 工具执行成功: ${toolName}, callId=${callId}`);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
||||||
|
console.error(`[ToolExecutor] 工具执行失败: ${toolName}, callId=${callId}`, error);
|
||||||
|
|
||||||
|
// 提交错误结果
|
||||||
|
const result = createBusinessErrorResult(callId, errorMessage);
|
||||||
|
await submitToolResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 file_read 工具
|
||||||
|
*/
|
||||||
|
async function executeFileRead(args: FileReadArgs): Promise<string> {
|
||||||
|
const content = await readFileContent(args.path);
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 file_write 工具
|
||||||
|
*/
|
||||||
|
async function executeFileWrite(args: FileWriteArgs): Promise<string> {
|
||||||
|
await createOrOverwriteFile(args.path, args.content);
|
||||||
|
|
||||||
|
// Verilog 文件添加知识图谱提示
|
||||||
|
const isVerilogFile = args.path.endsWith('.v') || args.path.endsWith('.sv');
|
||||||
|
if (isVerilogFile) {
|
||||||
|
return `文件已写入: ${args.path}\n\n[提示] 如有新信号或规则,请更新知识图谱`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `文件已写入: ${args.path}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 file_delete 工具
|
||||||
|
* 删除指定路径的文件
|
||||||
|
*/
|
||||||
|
async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
|
||||||
|
const filePath = args.path;
|
||||||
|
|
||||||
|
// 获取工作区路径
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||||
|
|
||||||
|
// 解析文件路径(支持相对路径和绝对路径)
|
||||||
|
const absolutePath = path.isAbsolute(filePath)
|
||||||
|
? filePath
|
||||||
|
: path.join(workspacePath, filePath);
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (!fs.existsSync(absolutePath)) {
|
||||||
|
throw new Error(`文件不存在: ${filePath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否为文件(不允许删除目录)
|
||||||
|
const stat = fs.statSync(absolutePath);
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
throw new Error(`不能删除目录,请指定文件路径: ${filePath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除文件
|
||||||
|
fs.unlinkSync(absolutePath);
|
||||||
|
|
||||||
|
// Verilog 文件添加知识图谱提示
|
||||||
|
const isVerilogFile = filePath.endsWith('.v') || filePath.endsWith('.sv');
|
||||||
|
if (isVerilogFile) {
|
||||||
|
return `文件已删除: ${filePath}\n\n[提示] 请删除知识图谱中相关节点`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `文件已删除: ${filePath}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 file_list 工具
|
||||||
|
*/
|
||||||
|
async function executeFileList(args: FileListArgs): Promise<string> {
|
||||||
|
const dirPath = args.path || '.';
|
||||||
|
const extensions = args.extension ? [args.extension] : undefined;
|
||||||
|
|
||||||
|
const files = await readDirectory(dirPath, extensions);
|
||||||
|
const fileList = files.map(f => f.path).join('\n');
|
||||||
|
|
||||||
|
return fileList || '(目录为空)';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 syntax_check 工具
|
||||||
|
* 将代码写入临时文件,调用 iverilog 检查语法
|
||||||
|
*/
|
||||||
|
async function executeSyntaxCheck(
|
||||||
|
args: SyntaxCheckArgs,
|
||||||
|
context: ToolExecutorContext
|
||||||
|
): Promise<string> {
|
||||||
|
// 检查 iverilog 是否可用
|
||||||
|
const iverilogCheck = await checkIverilogAvailable(context.extensionPath);
|
||||||
|
if (!iverilogCheck.available) {
|
||||||
|
throw new Error(`iverilog 不可用: ${iverilogCheck.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建临时文件
|
||||||
|
const tempDir = os.tmpdir();
|
||||||
|
const tempFile = path.join(tempDir, `iccoder_syntax_${Date.now()}.v`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 写入代码到临时文件
|
||||||
|
fs.writeFileSync(tempFile, args.code, 'utf-8');
|
||||||
|
|
||||||
|
// 调用 iverilog 进行语法检查
|
||||||
|
const { spawn } = require('child_process');
|
||||||
|
const iverilogPath = getIverilogPath(context.extensionPath);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const child = spawn(iverilogPath, ['-t', 'null', tempFile], {
|
||||||
|
cwd: tempDir,
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
IVERILOG_ROOT: path.join(context.extensionPath, 'tools', 'iverilog')
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let stdout = '';
|
||||||
|
let stderr = '';
|
||||||
|
|
||||||
|
child.stdout.on('data', (data: Buffer) => {
|
||||||
|
stdout += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
child.stderr.on('data', (data: Buffer) => {
|
||||||
|
stderr += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('close', (code: number) => {
|
||||||
|
// 清理临时文件
|
||||||
|
try {
|
||||||
|
fs.unlinkSync(tempFile);
|
||||||
|
} catch (e) {
|
||||||
|
// 忽略清理错误
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 0) {
|
||||||
|
resolve('语法检查通过,无错误。');
|
||||||
|
} else {
|
||||||
|
resolve(`语法检查发现错误:\n${stderr || stdout}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('error', (error: Error) => {
|
||||||
|
try {
|
||||||
|
fs.unlinkSync(tempFile);
|
||||||
|
} catch (e) {
|
||||||
|
// 忽略清理错误
|
||||||
|
}
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
// 确保清理临时文件
|
||||||
|
try {
|
||||||
|
fs.unlinkSync(tempFile);
|
||||||
|
} catch (e) {
|
||||||
|
// 忽略
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 iverilog 工具
|
||||||
|
* 直接执行 iverilog 命令
|
||||||
|
*/
|
||||||
|
async function executeIverilog(
|
||||||
|
args: IverilogArgs,
|
||||||
|
context: ToolExecutorContext
|
||||||
|
): Promise<string> {
|
||||||
|
// 检查 iverilog 是否可用
|
||||||
|
const iverilogCheck = await checkIverilogAvailable(context.extensionPath);
|
||||||
|
if (!iverilogCheck.available) {
|
||||||
|
throw new Error(`iverilog 不可用: ${iverilogCheck.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取工作目录
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('没有打开的工作区');
|
||||||
|
}
|
||||||
|
const projectPath = workspaceFolders[0].uri.fsPath;
|
||||||
|
const workDir = args.workDir
|
||||||
|
? path.join(projectPath, args.workDir)
|
||||||
|
: projectPath;
|
||||||
|
|
||||||
|
// 解析参数
|
||||||
|
const iverilogPath = getIverilogPath(context.extensionPath);
|
||||||
|
const cmdArgs = args.args.split(/\s+/).filter(a => a.length > 0);
|
||||||
|
|
||||||
|
const { spawn } = require('child_process');
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const child = spawn(iverilogPath, cmdArgs, {
|
||||||
|
cwd: workDir,
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
IVERILOG_ROOT: path.join(context.extensionPath, 'tools', 'iverilog')
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let stdout = '';
|
||||||
|
let stderr = '';
|
||||||
|
|
||||||
|
child.stdout.on('data', (data: Buffer) => {
|
||||||
|
stdout += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
child.stderr.on('data', (data: Buffer) => {
|
||||||
|
stderr += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('close', (code: number) => {
|
||||||
|
const output = stderr || stdout || '(无输出)';
|
||||||
|
if (code === 0) {
|
||||||
|
resolve(`执行成功\n${output}`);
|
||||||
|
} else {
|
||||||
|
resolve(`执行失败 (exit code: ${code})\n${output}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('error', (error: Error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 simulation 工具
|
||||||
|
*/
|
||||||
|
async function executeSimulation(
|
||||||
|
args: SimulationArgs,
|
||||||
|
context: ToolExecutorContext
|
||||||
|
): Promise<string> {
|
||||||
|
// 获取工作区路径
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectPath = workspaceFolders[0].uri.fsPath;
|
||||||
|
|
||||||
|
// 检查是否有 dumpModules 参数(多 VCD 模式)
|
||||||
|
if (args.dumpModules) {
|
||||||
|
const modules = parseDumpModules(args.dumpModules);
|
||||||
|
const vcdDir = args.vcdDir || 'vcd';
|
||||||
|
|
||||||
|
const result = await generateMultiVCD(
|
||||||
|
projectPath,
|
||||||
|
context.extensionPath,
|
||||||
|
args.tbPath,
|
||||||
|
modules,
|
||||||
|
vcdDir
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
const vcdList = result.vcdFiles
|
||||||
|
.map(f => `- ${f.moduleName}: ${f.success ? f.vcdPath : '失败 - ' + f.error}`)
|
||||||
|
.join('\n');
|
||||||
|
return `${result.message}\n\nVCD 文件列表:\n${vcdList}${result.stdout ? '\n\n仿真输出:' + result.stdout : ''}`;
|
||||||
|
} else {
|
||||||
|
throw new Error(result.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 原有单 VCD 逻辑
|
||||||
|
const result = await generateVCD(projectPath, context.extensionPath);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
let message = result.message;
|
||||||
|
if (result.stdout) {
|
||||||
|
message += `\n\n仿真输出:\n${result.stdout}`;
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
} else {
|
||||||
|
let errorMessage = result.message;
|
||||||
|
if (result.stderr) {
|
||||||
|
errorMessage += `\n\n错误输出:\n${result.stderr}`;
|
||||||
|
}
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 dumpModules 参数
|
||||||
|
* 格式:name:path,name:path
|
||||||
|
*/
|
||||||
|
function parseDumpModules(dumpModules: string): DumpModule[] {
|
||||||
|
return dumpModules.split(',').map(item => {
|
||||||
|
const [name, modulePath] = item.trim().split(':');
|
||||||
|
return { name: name.trim(), path: modulePath.trim() };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 waveform_summary 工具
|
||||||
|
* 解析 VCD 文件并返回波形摘要
|
||||||
|
*/
|
||||||
|
async function executeWaveformSummary(args: WaveformSummaryArgs): Promise<string> {
|
||||||
|
const { vcdPath, signals, checkpoints } = args;
|
||||||
|
|
||||||
|
// 获取工作区路径
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||||
|
|
||||||
|
// 解析 VCD 文件路径(支持相对路径)
|
||||||
|
const absolutePath = path.isAbsolute(vcdPath)
|
||||||
|
? vcdPath
|
||||||
|
: path.join(workspacePath, vcdPath);
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (!fs.existsSync(absolutePath)) {
|
||||||
|
throw new Error(`VCD 文件不存在: ${vcdPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析检查点时间
|
||||||
|
const checkpoint = checkpoints ? parseInt(checkpoints, 10) : undefined;
|
||||||
|
|
||||||
|
// 调用 VCD 解析器
|
||||||
|
const result = analyzeVcdFile(absolutePath, signals, checkpoint);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 knowledge_save 工具
|
||||||
|
* 保存知识图谱到 .iccoder/knowledge.json
|
||||||
|
*/
|
||||||
|
async function executeKnowledgeSave(args: KnowledgeSaveArgs): Promise<string> {
|
||||||
|
const workspaceFolder = getWorkspaceFolder();
|
||||||
|
if (!workspaceFolder) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const iccoderDirUri = vscode.Uri.joinPath(workspaceFolder.uri, '.iccoder');
|
||||||
|
const knowledgeUri = vscode.Uri.joinPath(iccoderDirUri, 'knowledge.json');
|
||||||
|
|
||||||
|
// 确保 .iccoder 目录存在(兼容远程/虚拟工作区)
|
||||||
|
await vscode.workspace.fs.createDirectory(iccoderDirUri);
|
||||||
|
|
||||||
|
// 写入知识图谱(UTF-8)
|
||||||
|
await vscode.workspace.fs.writeFile(knowledgeUri, Buffer.from(args.data || '', 'utf-8'));
|
||||||
|
|
||||||
|
return `知识图谱已保存: .iccoder/knowledge.json`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 knowledge_load 工具
|
||||||
|
* 从 .iccoder/knowledge.json 加载知识图谱
|
||||||
|
*/
|
||||||
|
async function executeKnowledgeLoad(): Promise<string> {
|
||||||
|
const workspaceFolder = getWorkspaceFolder();
|
||||||
|
if (!workspaceFolder) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const knowledgeUri = vscode.Uri.joinPath(workspaceFolder.uri, '.iccoder', 'knowledge.json');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const bytes = await vscode.workspace.fs.readFile(knowledgeUri);
|
||||||
|
const content = Buffer.from(bytes).toString('utf-8');
|
||||||
|
return content;
|
||||||
|
} catch (error) {
|
||||||
|
// 文件不存在:返回空图谱
|
||||||
|
if (error instanceof vscode.FileSystemError && error.code === 'FileNotFound') {
|
||||||
|
// 与后端 KnowledgeGraph 结构保持一致(nodes/edges + nodeClass 多态字段)
|
||||||
|
return JSON.stringify({ taskId: '', version: 1, module: null, nodes: [], edges: [] });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getWorkspaceFolder(): vscode.WorkspaceFolder | undefined {
|
||||||
|
const folders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!folders || folders.length === 0) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeUri = vscode.window.activeTextEditor?.document?.uri;
|
||||||
|
const activeFolder = activeUri ? vscode.workspace.getWorkspaceFolder(activeUri) : undefined;
|
||||||
|
return activeFolder ?? folders[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 iverilog 路径
|
||||||
|
*/
|
||||||
|
function getIverilogPath(extensionPath: string): string {
|
||||||
|
const platform = process.platform;
|
||||||
|
if (platform === 'win32') {
|
||||||
|
return path.join(extensionPath, 'tools', 'iverilog', 'bin', 'iverilog.exe');
|
||||||
|
} else {
|
||||||
|
return path.join(extensionPath, 'tools', 'iverilog', 'bin', 'iverilog');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建工具执行器上下文
|
||||||
|
*/
|
||||||
|
export function createToolExecutorContext(extensionPath: string): ToolExecutorContext {
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
const workspacePath = workspaceFolders?.[0]?.uri.fsPath || '';
|
||||||
|
|
||||||
|
return {
|
||||||
|
extensionPath,
|
||||||
|
workspacePath
|
||||||
|
};
|
||||||
|
}
|
||||||
193
src/services/userInteraction.ts
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
/**
|
||||||
|
* 用户交互处理器
|
||||||
|
* 处理 ask_user 事件,通过 WebView 显示问题并收集用户回答
|
||||||
|
*/
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import { submitAnswer, submitToolConfirm } from './apiClient';
|
||||||
|
import type { AskUserEvent, AnswerRequest } from '../types/api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 待处理的用户问题
|
||||||
|
*/
|
||||||
|
interface PendingQuestion {
|
||||||
|
askId: string;
|
||||||
|
taskId: string;
|
||||||
|
question: string;
|
||||||
|
options: string[];
|
||||||
|
resolve: (answer: string) => void;
|
||||||
|
reject: (error: Error) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户交互管理器
|
||||||
|
*/
|
||||||
|
export class UserInteractionManager {
|
||||||
|
private pendingQuestions = new Map<string, PendingQuestion>();
|
||||||
|
private webviewPanel: vscode.WebviewPanel | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置 WebView 面板(用于发送消息)
|
||||||
|
*/
|
||||||
|
setWebviewPanel(panel: vscode.WebviewPanel): void {
|
||||||
|
this.webviewPanel = panel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 WebView 面板
|
||||||
|
*/
|
||||||
|
getWebviewPanel(): vscode.WebviewPanel | null {
|
||||||
|
return this.webviewPanel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 ask_user 事件
|
||||||
|
* @param event ask_user 事件数据
|
||||||
|
* @param taskId 当前任务ID
|
||||||
|
*/
|
||||||
|
async handleAskUser(event: AskUserEvent, taskId: string): Promise<void> {
|
||||||
|
const { askId, question, options } = event;
|
||||||
|
|
||||||
|
console.log(`[UserInteraction] 收到问题: askId=${askId}, question=${question}`);
|
||||||
|
|
||||||
|
// 注意:问题显示已经通过 dialogService 的 onSegmentUpdate 统一处理
|
||||||
|
// 这里不再单独发送 showQuestion 命令,避免重复显示
|
||||||
|
|
||||||
|
// 创建 Promise 等待用户回答
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.pendingQuestions.set(askId, {
|
||||||
|
askId,
|
||||||
|
taskId,
|
||||||
|
question,
|
||||||
|
options,
|
||||||
|
resolve: (answer: string) => {
|
||||||
|
this.submitUserAnswer(askId, taskId, answer)
|
||||||
|
.then(() => resolve())
|
||||||
|
.catch(reject);
|
||||||
|
},
|
||||||
|
reject
|
||||||
|
});
|
||||||
|
|
||||||
|
// 设置超时(2小时)
|
||||||
|
setTimeout(() => {
|
||||||
|
if (this.pendingQuestions.has(askId)) {
|
||||||
|
this.pendingQuestions.delete(askId);
|
||||||
|
reject(new Error('用户回答超时'));
|
||||||
|
}
|
||||||
|
}, 7200000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理用户提交的回答(从 WebView 调用)
|
||||||
|
* @param askId 问题ID
|
||||||
|
* @param selected 选中的选项
|
||||||
|
* @param customInput 自定义输入
|
||||||
|
* @param fallbackTaskId 当问题不存在时使用的 taskId(用于直接发送到后端)
|
||||||
|
*/
|
||||||
|
async receiveAnswer(
|
||||||
|
askId: string,
|
||||||
|
selected?: string[],
|
||||||
|
customInput?: string,
|
||||||
|
fallbackTaskId?: string
|
||||||
|
): Promise<void> {
|
||||||
|
const pending = this.pendingQuestions.get(askId);
|
||||||
|
const answer = customInput || selected?.join(', ') || '';
|
||||||
|
|
||||||
|
if (!pending) {
|
||||||
|
// 问题不存在(可能是页面刷新或会话切换后),尝试直接发送到后端
|
||||||
|
if (fallbackTaskId) {
|
||||||
|
console.log(`[UserInteraction] 问题不在 pendingQuestions 中,直接发送到后端: askId=${askId}, taskId=${fallbackTaskId}`);
|
||||||
|
await this.submitUserAnswer(askId, fallbackTaskId, answer);
|
||||||
|
} else {
|
||||||
|
console.warn(`[UserInteraction] 问题不存在且无 fallbackTaskId: askId=${askId}`);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[UserInteraction] 收到用户回答: askId=${askId}, answer=${answer}`);
|
||||||
|
|
||||||
|
// 移除待处理问题
|
||||||
|
this.pendingQuestions.delete(askId);
|
||||||
|
|
||||||
|
// 触发 resolve
|
||||||
|
pending.resolve(answer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交用户回答到后端
|
||||||
|
*/
|
||||||
|
private async submitUserAnswer(
|
||||||
|
askId: string,
|
||||||
|
taskId: string,
|
||||||
|
answer: string
|
||||||
|
): Promise<void> {
|
||||||
|
// 检查是否是工具确认类型的问题
|
||||||
|
if (askId.startsWith('tool_confirm_')) {
|
||||||
|
// 提取 confirmId
|
||||||
|
const confirmId = parseInt(askId.replace('tool_confirm_', ''));
|
||||||
|
const approved = answer === '确认执行';
|
||||||
|
|
||||||
|
console.log(`[UserInteraction] 提交工具确认: confirmId=${confirmId}, approved=${approved}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await submitToolConfirm({
|
||||||
|
confirmId,
|
||||||
|
taskId,
|
||||||
|
approved
|
||||||
|
});
|
||||||
|
if (!response.success) {
|
||||||
|
throw new Error(response.error || '提交工具确认失败');
|
||||||
|
}
|
||||||
|
console.log(`[UserInteraction] 工具确认已提交: confirmId=${confirmId}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[UserInteraction] 提交工具确认失败: confirmId=${confirmId}`, error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 普通问题回答
|
||||||
|
const request: AnswerRequest = {
|
||||||
|
askId,
|
||||||
|
taskId,
|
||||||
|
customInput: answer
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await submitAnswer(request);
|
||||||
|
if (!response.success) {
|
||||||
|
throw new Error(response.error || '提交回答失败');
|
||||||
|
}
|
||||||
|
console.log(`[UserInteraction] 回答已提交: askId=${askId}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[UserInteraction] 提交回答失败: askId=${askId}`, error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消所有待处理的问题
|
||||||
|
*/
|
||||||
|
cancelAll(): void {
|
||||||
|
for (const [askId, pending] of this.pendingQuestions) {
|
||||||
|
pending.reject(new Error('用户交互已取消'));
|
||||||
|
}
|
||||||
|
this.pendingQuestions.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否有待处理的问题
|
||||||
|
*/
|
||||||
|
hasPendingQuestions(): boolean {
|
||||||
|
return this.pendingQuestions.size > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查特定问题是否存在
|
||||||
|
*/
|
||||||
|
hasPendingQuestion(askId: string): boolean {
|
||||||
|
return this.pendingQuestions.has(askId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 全局实例
|
||||||
|
export const userInteractionManager = new UserInteractionManager();
|
||||||
378
src/services/userService.ts
Normal file
@ -0,0 +1,378 @@
|
|||||||
|
/**
|
||||||
|
* 用户服务
|
||||||
|
* 管理用户信息和认证相关的 API 调用
|
||||||
|
*/
|
||||||
|
import * as https from 'https';
|
||||||
|
import * as http from 'http';
|
||||||
|
import { URL } from 'url';
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import { getStrangeLoopApiUrl, getConfig } from '../config/settings';
|
||||||
|
import type { UserInfoResponse, MembershipResponse, MultiMembershipVO, MembershipItemVO } from '../types/api';
|
||||||
|
import { fetchBalanceWithToken, getCachedBalance } from './creditsService';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP 请求选项
|
||||||
|
*/
|
||||||
|
interface RequestOptions {
|
||||||
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
body?: unknown;
|
||||||
|
timeout?: number;
|
||||||
|
token?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 HTTP 请求(带 token)
|
||||||
|
*/
|
||||||
|
async function request<T>(path: string, options: RequestOptions): Promise<T> {
|
||||||
|
const url = new URL(getStrangeLoopApiUrl(path));
|
||||||
|
const { timeout } = getConfig();
|
||||||
|
|
||||||
|
const isHttps = url.protocol === 'https:';
|
||||||
|
const httpModule = isHttps ? https : http;
|
||||||
|
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...options.headers
|
||||||
|
};
|
||||||
|
|
||||||
|
// 如果有 token,添加到请求头
|
||||||
|
if (options.token) {
|
||||||
|
headers['Authorization'] = `Bearer ${options.token}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestOptions: http.RequestOptions = {
|
||||||
|
hostname: url.hostname,
|
||||||
|
port: url.port || (isHttps ? 443 : 80),
|
||||||
|
path: url.pathname + url.search,
|
||||||
|
method: options.method,
|
||||||
|
headers,
|
||||||
|
timeout: options.timeout || timeout
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const req = httpModule.request(requestOptions, (res) => {
|
||||||
|
let data = '';
|
||||||
|
|
||||||
|
res.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
console.log(`[HTTP] 响应状态码: ${res.statusCode}`);
|
||||||
|
console.log(`[HTTP] 响应内容: ${data}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
resolve(json as T);
|
||||||
|
} else {
|
||||||
|
reject(new Error(json.error || json.message || json.msg || `HTTP ${res.statusCode}`));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// 如果不是 JSON,直接返回原始内容
|
||||||
|
reject(new Error(`解析响应失败 (${res.statusCode}): ${data}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('timeout', () => {
|
||||||
|
req.destroy();
|
||||||
|
reject(new Error('请求超时'));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (options.body) {
|
||||||
|
req.write(JSON.stringify(options.body));
|
||||||
|
}
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息数据结构(实际返回的数据)
|
||||||
|
*/
|
||||||
|
export interface UserInfo {
|
||||||
|
userId: string;
|
||||||
|
username: string;
|
||||||
|
nickname: string;
|
||||||
|
email?: string;
|
||||||
|
phonenumber?: string;
|
||||||
|
avatar?: string;
|
||||||
|
roles?: string[];
|
||||||
|
permissions?: string[];
|
||||||
|
createTime?: string;
|
||||||
|
loginDate?: string;
|
||||||
|
// 会员信息
|
||||||
|
membership?: {
|
||||||
|
tierCode: string;
|
||||||
|
tierName: string;
|
||||||
|
tierLevel: number;
|
||||||
|
remainingDays?: number;
|
||||||
|
monthlyCredits?: number;
|
||||||
|
};
|
||||||
|
// Credits 余额
|
||||||
|
credits?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
* GET /system/user/getInfo
|
||||||
|
*/
|
||||||
|
export async function getUserInfo(token: string): Promise<UserInfo | null> {
|
||||||
|
const apiPath = '/system/user/getInfo';
|
||||||
|
const fullUrl = getStrangeLoopApiUrl(apiPath);
|
||||||
|
console.log('[UserService] 获取用户信息');
|
||||||
|
console.log('[UserService] 请求地址:', fullUrl);
|
||||||
|
console.log('[UserService] Token:', token ? '已提供' : '未提供');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request<UserInfoResponse>(apiPath, {
|
||||||
|
method: 'GET',
|
||||||
|
token
|
||||||
|
});
|
||||||
|
|
||||||
|
// 处理响应数据 - 检查 code 是否为 200
|
||||||
|
if (response.code === 200 && response.user) {
|
||||||
|
const user = response.user;
|
||||||
|
return {
|
||||||
|
userId: String(user.userId),
|
||||||
|
username: user.userName,
|
||||||
|
nickname: user.nickName,
|
||||||
|
email: user.email,
|
||||||
|
phonenumber: user.phonenumber,
|
||||||
|
avatar: user.avatar,
|
||||||
|
roles: response.roles,
|
||||||
|
permissions: response.permissions,
|
||||||
|
createTime: user.createTime,
|
||||||
|
loginDate: user.loginDate
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error('[UserService] 获取用户信息失败:', response);
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[UserService] 请求失败:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户会员信息
|
||||||
|
* GET /strangeloop/api/membership/current
|
||||||
|
*/
|
||||||
|
export async function getMembershipInfo(token: string): Promise<MultiMembershipVO | null> {
|
||||||
|
const apiPath = '/strangeloop/api/membership/current';
|
||||||
|
const fullUrl = getStrangeLoopApiUrl(apiPath);
|
||||||
|
console.log('[UserService] 获取会员信息');
|
||||||
|
console.log('[UserService] 请求地址:', fullUrl);
|
||||||
|
console.log('[UserService] Token:', token ? '已提供' : '未提供');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await request<MembershipResponse>(apiPath, {
|
||||||
|
method: 'GET',
|
||||||
|
token
|
||||||
|
});
|
||||||
|
|
||||||
|
// 处理响应数据 - 检查 code 是否为 200
|
||||||
|
if (response.code === 200 && response.data) {
|
||||||
|
console.log('[UserService] 会员信息获取成功:', response.data);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error('[UserService] 获取会员信息失败:', response);
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[UserService] 请求会员信息失败:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会员等级映射
|
||||||
|
*/
|
||||||
|
const TIER_LEVEL_MAP: Record<string, number> = {
|
||||||
|
'BASIC': 1,
|
||||||
|
'TRIAL': 2,
|
||||||
|
'ADVANCED': 3,
|
||||||
|
'PROFESSIONAL': 4
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取最高等级的会员信息
|
||||||
|
*/
|
||||||
|
function getHighestTierMembership(allMemberships?: MembershipItemVO[]): MembershipItemVO | null {
|
||||||
|
if (!allMemberships || allMemberships.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按等级排序,获取最高等级
|
||||||
|
return allMemberships.reduce((highest, current) => {
|
||||||
|
const currentLevel = TIER_LEVEL_MAP[current.tierCode] || 0;
|
||||||
|
const highestLevel = TIER_LEVEL_MAP[highest.tierCode] || 0;
|
||||||
|
return currentLevel > highestLevel ? current : highest;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当获取到 token 时自动调用此函数
|
||||||
|
* 用于在登录成功后立即获取用户信息
|
||||||
|
*/
|
||||||
|
export async function onTokenReceived(token: string): Promise<UserInfo | null> {
|
||||||
|
try {
|
||||||
|
console.log('[UserService] Token 已获取,正在获取用户信息、会员信息和余额...');
|
||||||
|
|
||||||
|
// 并行获取用户信息、会员信息和余额
|
||||||
|
const [userInfo, membershipInfo, credits] = await Promise.all([
|
||||||
|
getUserInfo(token),
|
||||||
|
getMembershipInfo(token),
|
||||||
|
fetchBalanceWithToken(token)
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!userInfo) {
|
||||||
|
console.warn('[UserService] 未能获取到用户信息');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加 Credits 余额到用户信息
|
||||||
|
console.log('[UserService] 获取到的 Credits 余额:', credits);
|
||||||
|
if (credits !== null) {
|
||||||
|
userInfo.credits = credits;
|
||||||
|
console.log('[UserService] Credits 已添加到用户信息');
|
||||||
|
} else {
|
||||||
|
console.warn('[UserService] Credits 余额为 null,未添加到用户信息');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印用户信息到控制台
|
||||||
|
console.log('='.repeat(60));
|
||||||
|
console.log('用户信息详情:');
|
||||||
|
console.log('='.repeat(60));
|
||||||
|
console.log(`用户ID: ${userInfo.userId}`);
|
||||||
|
console.log(`用户名: ${userInfo.username}`);
|
||||||
|
console.log(`昵称: ${userInfo.nickname}`);
|
||||||
|
if (userInfo.email) {
|
||||||
|
console.log(`邮箱: ${userInfo.email}`);
|
||||||
|
}
|
||||||
|
if (userInfo.phonenumber) {
|
||||||
|
console.log(`手机号: ${userInfo.phonenumber}`);
|
||||||
|
}
|
||||||
|
if (userInfo.avatar) {
|
||||||
|
console.log(`头像: ${userInfo.avatar}`);
|
||||||
|
}
|
||||||
|
if (userInfo.roles && userInfo.roles.length > 0) {
|
||||||
|
console.log(`角色: ${userInfo.roles.join(', ')}`);
|
||||||
|
}
|
||||||
|
if (userInfo.permissions && userInfo.permissions.length > 0) {
|
||||||
|
console.log(`权限: ${userInfo.permissions.join(', ')}`);
|
||||||
|
}
|
||||||
|
if (userInfo.createTime) {
|
||||||
|
console.log(`创建时间: ${userInfo.createTime}`);
|
||||||
|
}
|
||||||
|
if (userInfo.loginDate) {
|
||||||
|
console.log(`最后登录: ${userInfo.loginDate}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印会员信息 - 从 allMemberships 中获取最高等级
|
||||||
|
if (membershipInfo && membershipInfo.allMemberships) {
|
||||||
|
const highestTier = getHighestTierMembership(membershipInfo.allMemberships);
|
||||||
|
|
||||||
|
if (highestTier) {
|
||||||
|
console.log('');
|
||||||
|
console.log('会员信息:');
|
||||||
|
console.log(`会员等级: ${highestTier.tierName} (${highestTier.tierCode})`);
|
||||||
|
console.log(`等级层级: ${highestTier.tierLevel}`);
|
||||||
|
console.log(`剩余天数: ${highestTier.remainingDays === -1 ? '永久' : highestTier.remainingDays + '天'}`);
|
||||||
|
console.log(`月度积分: ${highestTier.monthlyCredits}`);
|
||||||
|
|
||||||
|
// 将最高等级会员信息合并到用户信息中
|
||||||
|
userInfo.membership = {
|
||||||
|
tierCode: highestTier.tierCode,
|
||||||
|
tierName: highestTier.tierName,
|
||||||
|
tierLevel: highestTier.tierLevel,
|
||||||
|
remainingDays: highestTier.remainingDays,
|
||||||
|
monthlyCredits: highestTier.monthlyCredits
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印 Credits 余额
|
||||||
|
console.log('');
|
||||||
|
console.log('资源点余额:');
|
||||||
|
if (userInfo.credits !== undefined) {
|
||||||
|
console.log(`当前余额: ${userInfo.credits} Credits`);
|
||||||
|
} else {
|
||||||
|
console.log('当前余额: 未获取到余额信息');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('='.repeat(60));
|
||||||
|
|
||||||
|
// 保存到持久化存储
|
||||||
|
await saveUserInfo(userInfo);
|
||||||
|
|
||||||
|
return userInfo;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[UserService] 获取用户信息失败:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 持久化存储 ==============
|
||||||
|
|
||||||
|
let extensionContext: vscode.ExtensionContext | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化用户服务(设置 context)
|
||||||
|
*/
|
||||||
|
export function initUserService(context: vscode.ExtensionContext): void {
|
||||||
|
extensionContext = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户信息到持久化存储
|
||||||
|
*/
|
||||||
|
export async function saveUserInfo(userInfo: UserInfo): Promise<void> {
|
||||||
|
if (!extensionContext) {
|
||||||
|
console.warn('[UserService] ExtensionContext 未初始化');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await extensionContext.globalState.update('icCoderUserInfo', userInfo);
|
||||||
|
console.log('[UserService] 用户信息已保存到持久化存储');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从持久化存储获取用户信息
|
||||||
|
*/
|
||||||
|
export function getCachedUserInfo(): UserInfo | null {
|
||||||
|
if (!extensionContext) {
|
||||||
|
console.warn('[UserService] ExtensionContext 未初始化');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const userInfo = extensionContext.globalState.get<UserInfo>('icCoderUserInfo') || null;
|
||||||
|
|
||||||
|
// 从 creditsService 加载余额并合并到用户信息中
|
||||||
|
if (userInfo) {
|
||||||
|
const cachedCredits = getCachedBalance();
|
||||||
|
if (cachedCredits !== null) {
|
||||||
|
userInfo.credits = cachedCredits;
|
||||||
|
console.log('[UserService] 从 creditsService 加载余额:', cachedCredits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除持久化存储的用户信息
|
||||||
|
*/
|
||||||
|
export async function clearUserInfo(): Promise<void> {
|
||||||
|
if (!extensionContext) {
|
||||||
|
console.warn('[UserService] ExtensionContext 未初始化');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await extensionContext.globalState.update('icCoderUserInfo', undefined);
|
||||||
|
console.log('[UserService] 用户信息已清除');
|
||||||
|
}
|
||||||
500
src/services/vcdFileServer.ts
Normal file
@ -0,0 +1,500 @@
|
|||||||
|
import * as http from "http";
|
||||||
|
import * as fs from "fs";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VCD 文件 HTTP 服务器
|
||||||
|
* 用于为 Surfer 波形查看器提供 VCD 文件访问
|
||||||
|
*/
|
||||||
|
export class VCDFileServer {
|
||||||
|
private server: http.Server | null = null;
|
||||||
|
private port: number = 0;
|
||||||
|
private vcdFiles: Map<string, string> = new Map(); // fileId -> filePath
|
||||||
|
private extensionUri: vscode.Uri;
|
||||||
|
|
||||||
|
constructor(extensionUri: vscode.Uri) {
|
||||||
|
this.extensionUri = extensionUri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动服务器
|
||||||
|
*/
|
||||||
|
public async start(): Promise<number> {
|
||||||
|
if (this.server) {
|
||||||
|
return this.port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.server = http.createServer((req, res) => {
|
||||||
|
this.handleRequest(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 监听随机端口
|
||||||
|
this.server.listen(0, "127.0.0.1", () => {
|
||||||
|
const address = this.server!.address();
|
||||||
|
if (address && typeof address === "object") {
|
||||||
|
this.port = address.port;
|
||||||
|
console.log(`[VCDFileServer] 服务器已启动,端口: ${this.port}`);
|
||||||
|
resolve(this.port);
|
||||||
|
} else {
|
||||||
|
reject(new Error("无法获取服务器端口"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.server.on("error", (error) => {
|
||||||
|
console.error("[VCDFileServer] 服务器错误:", error);
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止服务器
|
||||||
|
*/
|
||||||
|
public stop(): void {
|
||||||
|
if (this.server) {
|
||||||
|
this.server.close();
|
||||||
|
this.server = null;
|
||||||
|
this.port = 0;
|
||||||
|
this.vcdFiles.clear();
|
||||||
|
console.log("[VCDFileServer] 服务器已停止");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册 VCD 文件
|
||||||
|
*/
|
||||||
|
public registerFile(filePath: string): string {
|
||||||
|
const fileId = this.generateFileId(filePath);
|
||||||
|
this.vcdFiles.set(fileId, filePath);
|
||||||
|
console.log(`[VCDFileServer] 注册文件: ${fileId} -> ${filePath}`);
|
||||||
|
return fileId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件 URL
|
||||||
|
*/
|
||||||
|
public getFileUrl(fileId: string): string {
|
||||||
|
return `http://127.0.0.1:${this.port}/vcd/${fileId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取波形查看器 URL
|
||||||
|
*/
|
||||||
|
public getViewerUrl(fileId: string): string {
|
||||||
|
return `http://127.0.0.1:${this.port}/viewer/${fileId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成文件 ID
|
||||||
|
*/
|
||||||
|
private generateFileId(filePath: string): string {
|
||||||
|
const timestamp = Date.now();
|
||||||
|
const fileName = path.basename(filePath);
|
||||||
|
return `${timestamp}-${fileName}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 HTTP 请求
|
||||||
|
*/
|
||||||
|
private handleRequest(req: http.IncomingMessage, res: http.ServerResponse): void {
|
||||||
|
const url = req.url || "";
|
||||||
|
console.log(`[VCDFileServer] 收到请求: ${url}`);
|
||||||
|
|
||||||
|
// 设置 CORS 头
|
||||||
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||||
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
||||||
|
|
||||||
|
// 处理 OPTIONS 请求
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
res.writeHead(200);
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 路由处理
|
||||||
|
if (url.startsWith("/viewer/")) {
|
||||||
|
this.handleViewerRequest(url, res);
|
||||||
|
} else if (url.startsWith("/vcd/")) {
|
||||||
|
this.handleVcdFileRequest(url, res);
|
||||||
|
} else if (url.startsWith("/static/")) {
|
||||||
|
this.handleStaticFileRequest(url, res);
|
||||||
|
} else {
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Not Found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理查看器页面请求
|
||||||
|
*/
|
||||||
|
private handleViewerRequest(url: string, res: http.ServerResponse): void {
|
||||||
|
const match = url.match(/^\/viewer\/(.+)$/);
|
||||||
|
if (!match) {
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileId = match[1];
|
||||||
|
const filePath = this.vcdFiles.get(fileId);
|
||||||
|
|
||||||
|
if (!filePath) {
|
||||||
|
console.error(`[VCDFileServer] 文件 ID 不存在: ${fileId}`);
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("File Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成 HTML 页面
|
||||||
|
const html = this.generateViewerHtml(fileId, filePath);
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": "text/html; charset=utf-8",
|
||||||
|
"Content-Length": Buffer.byteLength(html),
|
||||||
|
});
|
||||||
|
res.end(html);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 VCD 文件请求
|
||||||
|
*/
|
||||||
|
private handleVcdFileRequest(url: string, res: http.ServerResponse): void {
|
||||||
|
const match = url.match(/^\/vcd\/(.+)$/);
|
||||||
|
if (!match) {
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileId = match[1];
|
||||||
|
const filePath = this.vcdFiles.get(fileId);
|
||||||
|
|
||||||
|
if (!filePath) {
|
||||||
|
console.error(`[VCDFileServer] 文件 ID 不存在: ${fileId}`);
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("File Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
console.error(`[VCDFileServer] 文件不存在: ${filePath}`);
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("File Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取并发送文件
|
||||||
|
try {
|
||||||
|
const fileContent = fs.readFileSync(filePath);
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
"Content-Length": fileContent.length,
|
||||||
|
});
|
||||||
|
res.end(fileContent);
|
||||||
|
console.log(`[VCDFileServer] 成功发送文件: ${filePath}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[VCDFileServer] 读取文件失败:`, error);
|
||||||
|
res.writeHead(500, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Internal Server Error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理静态文件请求(Surfer 资源)
|
||||||
|
*/
|
||||||
|
private handleStaticFileRequest(url: string, res: http.ServerResponse): void {
|
||||||
|
const match = url.match(/^\/static\/(.+)$/);
|
||||||
|
if (!match) {
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileName = match[1];
|
||||||
|
const filePath = path.join(this.extensionUri.fsPath, "media", "surfer", fileName);
|
||||||
|
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
console.error(`[VCDFileServer] 静态文件不存在: ${filePath}`);
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("File Not Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const fileContent = fs.readFileSync(filePath);
|
||||||
|
const contentType = this.getContentType(fileName);
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": contentType,
|
||||||
|
"Content-Length": fileContent.length,
|
||||||
|
});
|
||||||
|
res.end(fileContent);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[VCDFileServer] 读取静态文件失败:`, error);
|
||||||
|
res.writeHead(500, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Internal Server Error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件的 Content-Type
|
||||||
|
*/
|
||||||
|
private getContentType(fileName: string): string {
|
||||||
|
const ext = path.extname(fileName).toLowerCase();
|
||||||
|
const contentTypes: { [key: string]: string } = {
|
||||||
|
".js": "application/javascript",
|
||||||
|
".wasm": "application/wasm",
|
||||||
|
".html": "text/html",
|
||||||
|
".css": "text/css",
|
||||||
|
};
|
||||||
|
return contentTypes[ext] || "application/octet-stream";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 VCD 文件获取根模块及其直接子模块名称
|
||||||
|
*/
|
||||||
|
private parseVcdRootScope(vcdFilePath: string): string[] {
|
||||||
|
try {
|
||||||
|
const buffer = fs.readFileSync(vcdFilePath, { encoding: 'utf8' });
|
||||||
|
const lines = buffer.split('\n');
|
||||||
|
|
||||||
|
const scopeNames: string[] = [];
|
||||||
|
let scopeDepth = 0;
|
||||||
|
const scopeStack: string[] = [];
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
|
||||||
|
if (trimmed.startsWith('$enddefinitions')) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scopeMatch = trimmed.match(/^\$scope\s+(\w+)\s+(\w+)/);
|
||||||
|
if (scopeMatch) {
|
||||||
|
const scopeType = scopeMatch[1];
|
||||||
|
const scopeName = scopeMatch[2];
|
||||||
|
|
||||||
|
if (scopeDepth === 0 && scopeType === 'module') {
|
||||||
|
scopeStack.push(scopeName);
|
||||||
|
} else if (scopeDepth === 1 && scopeType === 'module') {
|
||||||
|
const fullPath = [...scopeStack, scopeName];
|
||||||
|
scopeNames.push(fullPath.join('.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
scopeDepth++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trimmed.startsWith('$upscope')) {
|
||||||
|
scopeDepth--;
|
||||||
|
if (scopeDepth === 0) {
|
||||||
|
scopeStack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return scopeNames;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[VCDFileServer] 解析 VCD 文件失败:", error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成波形查看器 HTML 页面
|
||||||
|
*/
|
||||||
|
private generateViewerHtml(fileId: string, vcdFilePath: string): string {
|
||||||
|
const vcdUrl = this.getFileUrl(fileId);
|
||||||
|
const fileName = path.basename(vcdFilePath);
|
||||||
|
const scopeNames = this.parseVcdRootScope(vcdFilePath);
|
||||||
|
const scopeNamesJson = JSON.stringify(scopeNames);
|
||||||
|
|
||||||
|
const htmlPart1 = this.getHtmlPart1(fileName);
|
||||||
|
const htmlPart2 = this.getHtmlPart2(vcdUrl, scopeNamesJson);
|
||||||
|
const htmlPart3 = this.getHtmlPart3();
|
||||||
|
|
||||||
|
return htmlPart1 + htmlPart2 + htmlPart3;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHtmlPart1(fileName: string): string {
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||||
|
<title>Surfer 波形查看器 - ${fileName}</title>
|
||||||
|
<script>
|
||||||
|
window.surferReady = false;
|
||||||
|
window.pendingVcdData = null;
|
||||||
|
|
||||||
|
function on_surfer_error(msg) {
|
||||||
|
console.log("Surfer error:", msg);
|
||||||
|
document.getElementById("error_message").innerHTML = msg;
|
||||||
|
document.getElementById("error_container").style.display = "block";
|
||||||
|
}
|
||||||
|
window.on_surfer_error = on_surfer_error;
|
||||||
|
</script>
|
||||||
|
<script type="module">
|
||||||
|
console.log('[Browser] 开始初始化 Surfer...');
|
||||||
|
import init from '/static/surfer.js';
|
||||||
|
await init({module_or_path: '/static/surfer_bg.wasm'});
|
||||||
|
console.log('[Browser] Surfer WASM 已加载');
|
||||||
|
|
||||||
|
import {WebHandle, inject_message, id_of_name, draw_text_arrow} from '/static/surfer.js';
|
||||||
|
window.inject_message = inject_message;
|
||||||
|
window.id_of_name = id_of_name;
|
||||||
|
window.draw_text_arrow = draw_text_arrow;
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
window.surferReady = true;
|
||||||
|
console.log('[Browser] Surfer 已完全初始化并准备就绪');
|
||||||
|
|
||||||
|
try {
|
||||||
|
window.inject_message(JSON.stringify("ToggleLogs"));
|
||||||
|
console.log('[Browser] 已发送关闭日志面板命令');
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[Browser] 关闭日志面板失败:', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.pendingVcdData) {
|
||||||
|
console.log('[Browser] 发现待处理的 VCD 数据,立即加载');
|
||||||
|
loadVcdUrl(window.pendingVcdData);
|
||||||
|
window.pendingVcdData = null;
|
||||||
|
}
|
||||||
|
</script>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHtmlPart2(vcdUrl: string, scopeNamesJson: string): string {
|
||||||
|
return `
|
||||||
|
<script>
|
||||||
|
function loadVcdUrl(data) {
|
||||||
|
try {
|
||||||
|
console.log('[Browser] ========== 开始加载 VCD URL ==========');
|
||||||
|
console.log('[Browser] URL:', data.url);
|
||||||
|
console.log('[Browser] Scope names from VCD:', data.scopeNames);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('[Browser] 通过 postMessage 发送 LoadUrl 命令');
|
||||||
|
window.postMessage({
|
||||||
|
command: 'LoadUrl',
|
||||||
|
url: data.url
|
||||||
|
}, '*');
|
||||||
|
console.log('[Browser] ✅ 已发送 LoadUrl 命令');
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
console.log('[Browser] 尝试自动添加所有信号');
|
||||||
|
let scopeNamesToTry = [];
|
||||||
|
|
||||||
|
if (data.scopeNames && data.scopeNames.length > 0) {
|
||||||
|
scopeNamesToTry = data.scopeNames.map(path => path.split('.'));
|
||||||
|
console.log('[Browser] 使用解析的作用域名称:', scopeNamesToTry);
|
||||||
|
} else {
|
||||||
|
scopeNamesToTry = [['top'], ['testbench'], ['tb'], ['test'], ['dut']];
|
||||||
|
console.log('[Browser] 使用回退作用域名称');
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < scopeNamesToTry.length; i++) {
|
||||||
|
const scopeName = scopeNamesToTry[i];
|
||||||
|
try {
|
||||||
|
const addScopeMsg = {
|
||||||
|
"AddScope": [
|
||||||
|
{"strs": scopeName, "id": {"Wellen": i + 1}},
|
||||||
|
true
|
||||||
|
]
|
||||||
|
};
|
||||||
|
window.inject_message(JSON.stringify(addScopeMsg));
|
||||||
|
console.log('[Browser] 已发送 AddScope: ' + scopeName.join('.'));
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[Browser] AddScope 失败: ' + scopeName.join('.'), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
window.inject_message(JSON.stringify("ZoomToFit"));
|
||||||
|
console.log('[Browser] 已发送 ZoomToFit 命令');
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[Browser] ZoomToFit 失败:', e);
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[Browser] 添加信号失败:', e);
|
||||||
|
}
|
||||||
|
}, 1500);
|
||||||
|
}, 100);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Browser] ❌ 加载 VCD 失败:', error);
|
||||||
|
on_surfer_error(error.message + '\\n' + error.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.loadVcdUrl = loadVcdUrl;
|
||||||
|
|
||||||
|
// 页面加载完成后自动加载 VCD
|
||||||
|
window.addEventListener('load', () => {
|
||||||
|
const vcdData = {
|
||||||
|
url: '${vcdUrl}',
|
||||||
|
scopeNames: ${scopeNamesJson}
|
||||||
|
};
|
||||||
|
if (window.surferReady) {
|
||||||
|
loadVcdUrl(vcdData);
|
||||||
|
} else {
|
||||||
|
window.pendingVcdData = vcdData;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHtmlPart3(): string {
|
||||||
|
return `
|
||||||
|
<style>
|
||||||
|
html, body {
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 0 !important;
|
||||||
|
padding: 0 !important;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
background: #1e1e1e;
|
||||||
|
}
|
||||||
|
canvas {
|
||||||
|
margin-right: auto;
|
||||||
|
margin-left: auto;
|
||||||
|
display: block;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
#error_container {
|
||||||
|
padding: 1em;
|
||||||
|
border-radius: 0.5em;
|
||||||
|
margin: 0px auto;
|
||||||
|
max-width: 980px;
|
||||||
|
color: #f48771;
|
||||||
|
background-color: #5a1d1d;
|
||||||
|
position: relative;
|
||||||
|
height: 90%;
|
||||||
|
overflow: scroll;
|
||||||
|
}
|
||||||
|
#error_message {
|
||||||
|
overflow: scroll;
|
||||||
|
white-space: break-spaces;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<canvas id="the_canvas_id"></canvas>
|
||||||
|
<div id="error_container" style="display: none;">
|
||||||
|
<h3>❌ Surfer 加载失败</h3>
|
||||||
|
<code id="error_message"></code>
|
||||||
|
</div>
|
||||||
|
<script src="/static/integration.js"></script>
|
||||||
|
<script>
|
||||||
|
register_message_listener();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
630
src/types/api.ts
Normal file
@ -0,0 +1,630 @@
|
|||||||
|
/**
|
||||||
|
* 后端 API 类型定义
|
||||||
|
* 对应后端 IC Coder Backend 的接口格式
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { CompactedMemory, CompactedMessage } from "./memory";
|
||||||
|
|
||||||
|
// ============== 对话请求/响应 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行模式类型
|
||||||
|
* - plan: 只读模式,只能查询分析
|
||||||
|
* - ask: 逐个确认,每个写操作需确认
|
||||||
|
* - agent: 智能体自主(默认)
|
||||||
|
* - auto: 完全自动
|
||||||
|
*/
|
||||||
|
export type RunMode = "plan" | "ask" | "agent" | "auto";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务等级类型
|
||||||
|
* - lite: 轻量级
|
||||||
|
* - syntaxic: 语法级
|
||||||
|
* - max: 最大性能
|
||||||
|
* - auto: 自动选择
|
||||||
|
*/
|
||||||
|
export type ServiceTier = "lite" | "syntaxic" | "max" | "auto";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对话请求
|
||||||
|
* POST /api/dialog/stream
|
||||||
|
*/
|
||||||
|
export interface DialogRequest {
|
||||||
|
/** 任务ID(用于记忆隔离) */
|
||||||
|
taskId: string;
|
||||||
|
/** 用户消息 */
|
||||||
|
message: string;
|
||||||
|
/** 用户ID */
|
||||||
|
userId: string;
|
||||||
|
/** 运行模式 */
|
||||||
|
mode: RunMode;
|
||||||
|
/** 服务等级 */
|
||||||
|
serviceTier?: ServiceTier;
|
||||||
|
/** JWT Token(用于认证和扣费) */
|
||||||
|
token?: string;
|
||||||
|
/** 压缩后的记忆数据(用于后端重启后恢复) */
|
||||||
|
compactedData?: CompactedMemory;
|
||||||
|
/** 压缩后产生的新消息 */
|
||||||
|
newMessages?: CompactedMessage[];
|
||||||
|
/** 知识图谱数据(JSON 字符串,用于恢复知识图谱) */
|
||||||
|
knowledgeData?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== SSE 事件类型 ==============
|
||||||
|
|
||||||
|
/** SSE 事件类型枚举 */
|
||||||
|
export type SSEEventType =
|
||||||
|
| "text_delta" // 文本增量
|
||||||
|
| "tool_call" // 客户端工具调用请求
|
||||||
|
| "tool_confirm" // 工具确认请求(Ask 模式)
|
||||||
|
| "plan_confirm" // 计划确认请求(Plan 模式)
|
||||||
|
| "phase_progress" // 阶段进度更新
|
||||||
|
| "plan_step_add" // 添加计划步骤
|
||||||
|
| "plan_step_remove" // 删除计划步骤
|
||||||
|
| "plan_step_update" // 更新计划步骤
|
||||||
|
| "plan_summary_update" // 更新计划摘要
|
||||||
|
| "tool_start" // 工具开始执行
|
||||||
|
| "tool_complete" // 工具执行完成
|
||||||
|
| "tool_error" // 工具执行错误
|
||||||
|
| "ask_user" // 向用户提问
|
||||||
|
| "agent_start" // 子智能体启动
|
||||||
|
| "agent_progress" // 子智能体进度
|
||||||
|
| "agent_complete" // 子智能体完成
|
||||||
|
| "agent_error" // 子智能体错误
|
||||||
|
| "memory_compacted" // 记忆压缩完成
|
||||||
|
| "context_usage" // 上下文使用量
|
||||||
|
| "credit_update" // 资源点余额更新
|
||||||
|
| "complete" // 对话完成
|
||||||
|
| "error" // 错误
|
||||||
|
| "warning" // 警告
|
||||||
|
| "notification" // 通知
|
||||||
|
| "depth_update" // 深度更新
|
||||||
|
| "heartbeat"; // 心跳
|
||||||
|
|
||||||
|
/** text_delta 事件数据 */
|
||||||
|
export interface TextDeltaEvent {
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tool_start 事件数据 */
|
||||||
|
export interface ToolStartEvent {
|
||||||
|
tool_name: string;
|
||||||
|
tool_input: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tool_complete 事件数据 */
|
||||||
|
export interface ToolCompleteEvent {
|
||||||
|
tool_name: string;
|
||||||
|
result: string;
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tool_error 事件数据 */
|
||||||
|
export interface ToolErrorEvent {
|
||||||
|
tool_name: string;
|
||||||
|
error: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** tool_confirm 事件数据(Ask 模式确认请求) */
|
||||||
|
export interface ToolConfirmEvent {
|
||||||
|
/** 确认ID,用于匹配响应 */
|
||||||
|
confirmId: number;
|
||||||
|
/** 工具名称 */
|
||||||
|
toolName: string;
|
||||||
|
/** 工具输入参数 */
|
||||||
|
toolInput: Record<string, unknown>;
|
||||||
|
/** 时间戳 */
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 计划步骤 */
|
||||||
|
export interface PlanStep {
|
||||||
|
/** 步骤名称 */
|
||||||
|
name: string;
|
||||||
|
/** 步骤描述 */
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 计划阶段 */
|
||||||
|
export interface PlanPhase {
|
||||||
|
/** 阶段ID: spec/design/sim/done */
|
||||||
|
id: string;
|
||||||
|
/** 阶段名称 */
|
||||||
|
name: string;
|
||||||
|
/** 阶段状态: skipped/completed/current/pending */
|
||||||
|
status: string;
|
||||||
|
/** 跳过原因 */
|
||||||
|
reason?: string;
|
||||||
|
/** 阶段内的步骤 */
|
||||||
|
steps: PlanStep[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** plan_confirm 事件数据(Plan 模式计划确认) */
|
||||||
|
export interface PlanConfirmEvent {
|
||||||
|
/** 确认ID */
|
||||||
|
confirmId: number;
|
||||||
|
/** 计划标题 */
|
||||||
|
title: string;
|
||||||
|
/** 四阶段计划列表(新格式) */
|
||||||
|
phases?: PlanPhase[];
|
||||||
|
/** 执行步骤列表(旧格式,兼容) */
|
||||||
|
steps?: string[];
|
||||||
|
/** 计划摘要 */
|
||||||
|
summary: string;
|
||||||
|
/** 时间戳 */
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** phase_progress 事件数据(阶段进度更新) */
|
||||||
|
export interface PhaseProgressEvent {
|
||||||
|
/** 阶段ID: spec/design/sim/done */
|
||||||
|
phaseId: string;
|
||||||
|
/** 状态: current/completed */
|
||||||
|
status: string;
|
||||||
|
/** 时间戳 */
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** plan_step_add 事件数据(添加计划步骤) */
|
||||||
|
export interface PlanStepAddEvent {
|
||||||
|
phaseId: string;
|
||||||
|
step: PlanStep;
|
||||||
|
index: number;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** plan_step_remove 事件数据(删除计划步骤) */
|
||||||
|
export interface PlanStepRemoveEvent {
|
||||||
|
phaseId: string;
|
||||||
|
stepIndex: number;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** plan_step_update 事件数据(更新计划步骤) */
|
||||||
|
export interface PlanStepUpdateEvent {
|
||||||
|
phaseId: string;
|
||||||
|
stepIndex: number;
|
||||||
|
step: PlanStep;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** plan_summary_update 事件数据(更新计划摘要) */
|
||||||
|
export interface PlanSummaryUpdateEvent {
|
||||||
|
summary: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** ask_user 事件数据 */
|
||||||
|
export interface AskUserEvent {
|
||||||
|
askId: string;
|
||||||
|
question: string;
|
||||||
|
options: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** complete 事件数据 */
|
||||||
|
export interface CompleteEvent {
|
||||||
|
status: string;
|
||||||
|
finish_reason: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** error 事件数据 */
|
||||||
|
export interface ErrorEvent {
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** warning 事件数据 */
|
||||||
|
export interface WarningEvent {
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** notification 事件数据 */
|
||||||
|
export interface NotificationEvent {
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** depth_update 事件数据 */
|
||||||
|
export interface DepthUpdateEvent {
|
||||||
|
depth: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 智能体事件类型 ==============
|
||||||
|
|
||||||
|
/** agent_start 事件数据 */
|
||||||
|
export interface AgentStartEvent {
|
||||||
|
agentId: string;
|
||||||
|
agentType: string;
|
||||||
|
agentName: string;
|
||||||
|
instruction: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** agent_progress 事件数据 */
|
||||||
|
export interface AgentProgressEvent {
|
||||||
|
agentId: string;
|
||||||
|
step: number;
|
||||||
|
toolName: string;
|
||||||
|
toolInput?: unknown;
|
||||||
|
toolResult?: string;
|
||||||
|
status: "running" | "completed" | "error";
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** agent_complete 事件数据 */
|
||||||
|
export interface AgentCompleteEvent {
|
||||||
|
agentId: string;
|
||||||
|
agentType: string;
|
||||||
|
summary: string;
|
||||||
|
stats: Record<string, unknown>;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** agent_error 事件数据 */
|
||||||
|
export interface AgentErrorEvent {
|
||||||
|
agentId: string;
|
||||||
|
agentType: string;
|
||||||
|
error: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** context_usage 事件数据 */
|
||||||
|
export interface ContextUsageEvent {
|
||||||
|
currentTokens: number;
|
||||||
|
maxTokens: number;
|
||||||
|
percentage: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** credit_update 事件数据 */
|
||||||
|
export interface CreditUpdateEvent {
|
||||||
|
deductedCredits: number;
|
||||||
|
remainingCredits: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 工具调用协议 (MCP 格式) ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具调用请求(MCP格式)
|
||||||
|
* 后端通过 SSE tool_call 事件推送
|
||||||
|
*/
|
||||||
|
export interface ToolCallRequest {
|
||||||
|
/** JSON-RPC版本,固定为"2.0" */
|
||||||
|
jsonrpc: "2.0";
|
||||||
|
/** 请求ID,用于匹配响应 */
|
||||||
|
id: number;
|
||||||
|
/** 方法名,固定为"tools/call" */
|
||||||
|
method: "tools/call";
|
||||||
|
/** 调用参数 */
|
||||||
|
params: {
|
||||||
|
/** 工具名称 */
|
||||||
|
name: string;
|
||||||
|
/** 工具参数 */
|
||||||
|
arguments: Record<string, unknown>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具执行结果(MCP格式)
|
||||||
|
* POST /api/tool/result
|
||||||
|
*/
|
||||||
|
export interface ToolCallResult {
|
||||||
|
/** JSON-RPC版本 */
|
||||||
|
jsonrpc: "2.0";
|
||||||
|
/** 请求ID,与ToolCallRequest.id对应 */
|
||||||
|
id: number;
|
||||||
|
/** 执行结果(与error互斥) */
|
||||||
|
result?: ToolResultContent;
|
||||||
|
/** 错误信息(与result互斥) */
|
||||||
|
error?: ToolResultError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 工具执行结果内容 */
|
||||||
|
export interface ToolResultContent {
|
||||||
|
/** 内容列表 */
|
||||||
|
content: ContentItem[];
|
||||||
|
/** 是否为错误结果(业务错误,如编译失败) */
|
||||||
|
isError: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 内容项 */
|
||||||
|
export interface ContentItem {
|
||||||
|
/** 内容类型:text, image, resource */
|
||||||
|
type: string;
|
||||||
|
/** 文本内容 */
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 工具系统错误 */
|
||||||
|
export interface ToolResultError {
|
||||||
|
/** 错误码 */
|
||||||
|
code: number;
|
||||||
|
/** 错误消息 */
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 用户回答 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户回答请求
|
||||||
|
* POST /api/task/answer
|
||||||
|
*/
|
||||||
|
export interface AnswerRequest {
|
||||||
|
/** 问题ID */
|
||||||
|
askId: string;
|
||||||
|
/** 任务ID */
|
||||||
|
taskId: string;
|
||||||
|
/** 选中的选项列表 */
|
||||||
|
selected?: string[];
|
||||||
|
/** 自定义输入内容 */
|
||||||
|
customInput?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 用户回答响应 */
|
||||||
|
export interface AnswerResponse {
|
||||||
|
success: boolean;
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 工具结果响应 ==============
|
||||||
|
|
||||||
|
/** 工具结果响应 */
|
||||||
|
export interface ToolResultResponse {
|
||||||
|
success: boolean;
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 工具确认响应 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具确认响应请求
|
||||||
|
* POST /api/tool/confirm
|
||||||
|
*/
|
||||||
|
export interface ToolConfirmResponse {
|
||||||
|
/** 确认ID,与 ToolConfirmEvent.confirmId 对应 */
|
||||||
|
confirmId: number;
|
||||||
|
/** 任务ID */
|
||||||
|
taskId: string;
|
||||||
|
/** 是否批准执行 */
|
||||||
|
approved: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 用户信息 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息响应
|
||||||
|
* GET /system/user/getInfo
|
||||||
|
*/
|
||||||
|
export interface UserInfoResponse {
|
||||||
|
/** 响应消息 */
|
||||||
|
msg: string;
|
||||||
|
/** 响应代码 (200 表示成功) */
|
||||||
|
code: number;
|
||||||
|
/** 权限列表 */
|
||||||
|
permissions: string[];
|
||||||
|
/** 角色列表 */
|
||||||
|
roles: string[];
|
||||||
|
/** 是否默认修改密码 */
|
||||||
|
isDefaultModifyPwd: boolean;
|
||||||
|
/** 密码是否过期 */
|
||||||
|
isPasswordExpired: boolean;
|
||||||
|
/** 用户信息 */
|
||||||
|
user: {
|
||||||
|
userId: number;
|
||||||
|
userName: string;
|
||||||
|
nickName: string;
|
||||||
|
email?: string;
|
||||||
|
phonenumber?: string;
|
||||||
|
sex?: string;
|
||||||
|
avatar?: string;
|
||||||
|
status?: string;
|
||||||
|
createTime?: string;
|
||||||
|
loginDate?: string;
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 会员信息 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会员单条记录
|
||||||
|
*/
|
||||||
|
export interface MembershipItemVO {
|
||||||
|
membershipId: number | null;
|
||||||
|
tierCode: string;
|
||||||
|
tierName: string;
|
||||||
|
tierLevel: number;
|
||||||
|
expireTime: string | null;
|
||||||
|
remainingDays: number;
|
||||||
|
permanent: boolean;
|
||||||
|
nextGrantTime: string | null;
|
||||||
|
lastGrantTime: string | null;
|
||||||
|
grantCycle: number;
|
||||||
|
totalGranted: number;
|
||||||
|
monthlyCredits: number;
|
||||||
|
teamSeat: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户会员信息
|
||||||
|
*/
|
||||||
|
export interface UserMembershipVO {
|
||||||
|
userId: number;
|
||||||
|
tierCode: string;
|
||||||
|
tierName: string;
|
||||||
|
tierLevel: number;
|
||||||
|
allowedModelCombinations: string[];
|
||||||
|
description?: string;
|
||||||
|
createdTime?: string;
|
||||||
|
updatedTime?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多会员信息响应
|
||||||
|
*/
|
||||||
|
export interface MultiMembershipVO extends UserMembershipVO {
|
||||||
|
displayTier?: MembershipItemVO;
|
||||||
|
allMemberships?: MembershipItemVO[];
|
||||||
|
totalMonthlyCredits?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会员信息响应
|
||||||
|
* GET /strangeloop/api/membership/current
|
||||||
|
*/
|
||||||
|
export interface MembershipResponse {
|
||||||
|
code: number;
|
||||||
|
msg?: string;
|
||||||
|
message?: string;
|
||||||
|
data?: MultiMembershipVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== 辅助类型 ==============
|
||||||
|
|
||||||
|
/** 后端工具名称 */
|
||||||
|
export type ToolName =
|
||||||
|
| "file_read"
|
||||||
|
| "file_write"
|
||||||
|
| "file_delete"
|
||||||
|
| "file_list"
|
||||||
|
| "syntax_check"
|
||||||
|
| "iverilog"
|
||||||
|
| "simulation"
|
||||||
|
| "waveform_summary"
|
||||||
|
| "waveform_trace"
|
||||||
|
| "knowledge_save"
|
||||||
|
| "knowledge_load";
|
||||||
|
|
||||||
|
/** file_read 工具参数 */
|
||||||
|
export interface FileReadArgs {
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** file_write 工具参数 */
|
||||||
|
export interface FileWriteArgs {
|
||||||
|
path: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** file_delete 工具参数 */
|
||||||
|
export interface FileDeleteArgs {
|
||||||
|
/** 要删除的文件路径 */
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** file_list 工具参数 */
|
||||||
|
export interface FileListArgs {
|
||||||
|
path?: string;
|
||||||
|
extension?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** syntax_check 工具参数 */
|
||||||
|
export interface SyntaxCheckArgs {
|
||||||
|
code: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** iverilog 工具参数 */
|
||||||
|
export interface IverilogArgs {
|
||||||
|
args: string;
|
||||||
|
workDir?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** simulation 工具参数 */
|
||||||
|
export interface SimulationArgs {
|
||||||
|
rtlPath: string;
|
||||||
|
tbPath: string;
|
||||||
|
duration?: string;
|
||||||
|
/** 要dump的模块列表,格式:name:path,name:path */
|
||||||
|
dumpModules?: string;
|
||||||
|
/** VCD输出目录,默认'vcd' */
|
||||||
|
vcdDir?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** waveform_summary 工具参数 */
|
||||||
|
export interface WaveformSummaryArgs {
|
||||||
|
vcdPath: string;
|
||||||
|
signals: string;
|
||||||
|
checkpoints?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** waveform_trace 工具参数 */
|
||||||
|
export interface WaveformTraceArgs {
|
||||||
|
/** Verilog 源文件路径(相对于项目根目录) */
|
||||||
|
verilogPath: string;
|
||||||
|
/** VCD 波形文件路径(相对于项目根目录) */
|
||||||
|
vcdPath: string;
|
||||||
|
/** 仿真工具的输出字符串(包含 mismatch 信息) */
|
||||||
|
simOutput: string;
|
||||||
|
/** BFS 回溯层数,默认 2 */
|
||||||
|
traceLevel?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** knowledge_save 工具参数 */
|
||||||
|
export interface KnowledgeSaveArgs {
|
||||||
|
/** 知识图谱 JSON 数据 */
|
||||||
|
data: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** knowledge_load 工具参数 */
|
||||||
|
export interface KnowledgeLoadArgs {
|
||||||
|
// 无参数,直接读取 .iccoder/knowledge.json
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 工具参数联合类型 */
|
||||||
|
export type ToolArgs =
|
||||||
|
| FileReadArgs
|
||||||
|
| FileWriteArgs
|
||||||
|
| FileDeleteArgs
|
||||||
|
| FileListArgs
|
||||||
|
| SyntaxCheckArgs
|
||||||
|
| IverilogArgs
|
||||||
|
| SimulationArgs
|
||||||
|
| WaveformSummaryArgs
|
||||||
|
| WaveformTraceArgs
|
||||||
|
| KnowledgeSaveArgs
|
||||||
|
| KnowledgeLoadArgs;
|
||||||
|
|
||||||
|
// ============== 邀请码验证 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码验证请求
|
||||||
|
* POST /api/invitation/verify
|
||||||
|
*/
|
||||||
|
export interface InvitationVerifyRequest {
|
||||||
|
/** 邀请码 */
|
||||||
|
code: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码验证响应
|
||||||
|
*/
|
||||||
|
export interface InvitationVerifyResponse {
|
||||||
|
/** 响应代码 */
|
||||||
|
code: number;
|
||||||
|
/** 响应消息 */
|
||||||
|
msg: string;
|
||||||
|
/** 验证结果数据 */
|
||||||
|
data?: {
|
||||||
|
/** 是否验证成功 */
|
||||||
|
verified: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请码状态响应
|
||||||
|
* GET /api/invitation/status
|
||||||
|
*/
|
||||||
|
export interface InvitationStatusResponse {
|
||||||
|
/** 响应代码 */
|
||||||
|
code: number;
|
||||||
|
/** 响应消息 */
|
||||||
|
msg?: string;
|
||||||
|
/** 状态数据 */
|
||||||
|
data?: {
|
||||||
|
/** 是否已验证 */
|
||||||
|
verified: boolean;
|
||||||
|
/** 使用的邀请码 */
|
||||||
|
invitationCode?: string;
|
||||||
|
/** 验证时间 */
|
||||||
|
verifiedTime?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -5,7 +5,8 @@ export enum MessageType {
|
|||||||
SYSTEM = "SYSTEM",
|
SYSTEM = "SYSTEM",
|
||||||
USER = "USER",
|
USER = "USER",
|
||||||
AI = "AI",
|
AI = "AI",
|
||||||
TOOL_EXECUTION_RESULT = "TOOL_EXECUTION_RESULT"
|
TOOL_EXECUTION_RESULT = "TOOL_EXECUTION_RESULT",
|
||||||
|
COMPACTION_SUMMARY = "COMPACTION_SUMMARY" // 压缩摘要
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,6 +57,7 @@ export interface AiMessage extends BaseMessage {
|
|||||||
text?: string;
|
text?: string;
|
||||||
toolExecutionRequests?: ToolExecutionRequest[];
|
toolExecutionRequests?: ToolExecutionRequest[];
|
||||||
thinking?: string;
|
thinking?: string;
|
||||||
|
segments?: any[]; // 保存完整的 segments 信息用于还原显示
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,10 +70,22 @@ export interface ToolExecutionResultMessage extends BaseMessage {
|
|||||||
text: string; // JSON字符串
|
text: string; // JSON字符串
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 压缩摘要消息
|
||||||
|
*/
|
||||||
|
export interface CompactionSummaryMessage extends BaseMessage {
|
||||||
|
type: MessageType.COMPACTION_SUMMARY;
|
||||||
|
summary: string;
|
||||||
|
version: number;
|
||||||
|
compactedAt: string;
|
||||||
|
originalMessageCount: number;
|
||||||
|
compactedMessageCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 联合消息类型
|
* 联合消息类型
|
||||||
*/
|
*/
|
||||||
export type ChatMessage = SystemMessage | UserMessage | AiMessage | ToolExecutionResultMessage;
|
export type ChatMessage = SystemMessage | UserMessage | AiMessage | ToolExecutionResultMessage | CompactionSummaryMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对话轮次元数据
|
* 对话轮次元数据
|
||||||
|
|||||||
42
src/types/memory.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* 压缩记忆相关类型定义
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 压缩后的记忆数据
|
||||||
|
*/
|
||||||
|
export interface CompactedMemory {
|
||||||
|
taskId: string;
|
||||||
|
version: number;
|
||||||
|
compactedAt: string;
|
||||||
|
summary: string;
|
||||||
|
recentMessages: CompactedMessage[];
|
||||||
|
originalMessageCount: number;
|
||||||
|
compactedMessageCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 压缩消息格式
|
||||||
|
*/
|
||||||
|
export interface CompactedMessage {
|
||||||
|
type: 'USER' | 'AI' | 'SYSTEM' | 'TOOL_RESULT';
|
||||||
|
content: string;
|
||||||
|
toolCall?: ToolCallInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具调用信息
|
||||||
|
*/
|
||||||
|
export interface ToolCallInfo {
|
||||||
|
toolName: string;
|
||||||
|
toolInput: string;
|
||||||
|
toolOutput?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记忆压缩 SSE 事件
|
||||||
|
*/
|
||||||
|
export interface MemoryCompactedEvent {
|
||||||
|
taskId: string;
|
||||||
|
compactedData: CompactedMemory;
|
||||||
|
}
|
||||||
@ -8,8 +8,11 @@ import {
|
|||||||
MessageType,
|
MessageType,
|
||||||
UserMessage,
|
UserMessage,
|
||||||
AiMessage,
|
AiMessage,
|
||||||
SystemMessage
|
SystemMessage,
|
||||||
|
ToolExecutionResultMessage,
|
||||||
|
CompactionSummaryMessage
|
||||||
} from '../types/chatHistory';
|
} from '../types/chatHistory';
|
||||||
|
import { CompactedMemory, CompactedMessage } from '../types/memory';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 会话历史管理器
|
* 会话历史管理器
|
||||||
@ -20,6 +23,10 @@ export class ChatHistoryManager {
|
|||||||
private baseDir: string; // ~/.iccoder
|
private baseDir: string; // ~/.iccoder
|
||||||
private currentTaskId: string | null = null;
|
private currentTaskId: string | null = null;
|
||||||
private currentProjectPath: string | null = null;
|
private currentProjectPath: string | null = null;
|
||||||
|
// 存储每个面板的任务信息(taskId 和 projectPath)
|
||||||
|
private panelTaskMap: Map<string, { taskId: string; projectPath: string }> = new Map();
|
||||||
|
// 追踪压缩后产生的新消息
|
||||||
|
private newMessagesSinceCompaction: CompactedMessage[] = [];
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
// 设置存储路径: ~/.iccoder
|
// 设置存储路径: ~/.iccoder
|
||||||
@ -33,12 +40,13 @@ export class ChatHistoryManager {
|
|||||||
* 规则:
|
* 规则:
|
||||||
* - 替换 \ 和 / 为 --
|
* - 替换 \ 和 / 为 --
|
||||||
* - 替换 : 为空
|
* - 替换 : 为空
|
||||||
* 例如:C:\Users\admin\Documents\Project -> C--Users-admin-Documents-Project
|
* 例如:C:\Users\admin\Documents\Project -> C--Users--admin--Documents--Project
|
||||||
*/
|
*/
|
||||||
private encodeProjectPath(projectPath: string): string {
|
private encodeProjectPath(projectPath: string): string {
|
||||||
return projectPath
|
return projectPath
|
||||||
.replace(/:/g, '') // 移除冒号
|
.replace(/\\/g, '--') // 替换反斜杠为 --
|
||||||
.replace(/[/\\]/g, '--'); // 替换斜杠为 --
|
.replace(/\//g, '--') // 替换正斜杠为 --
|
||||||
|
.replace(/:/g, ''); // 移除冒号
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,6 +115,43 @@ export class ChatHistoryManager {
|
|||||||
return ChatHistoryManager.instance;
|
return ChatHistoryManager.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为面板设置任务ID
|
||||||
|
*/
|
||||||
|
public setPanelTask(panelId: string, taskId: string, projectPath: string): void {
|
||||||
|
this.panelTaskMap.set(panelId, { taskId, projectPath });
|
||||||
|
this.currentTaskId = taskId;
|
||||||
|
this.currentProjectPath = projectPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取面板的任务ID
|
||||||
|
*/
|
||||||
|
public getPanelTask(panelId: string): string | null {
|
||||||
|
const taskInfo = this.panelTaskMap.get(panelId);
|
||||||
|
return taskInfo ? taskInfo.taskId : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切换到指定面板的任务上下文
|
||||||
|
*/
|
||||||
|
public switchToPanelTask(panelId: string): boolean {
|
||||||
|
const taskInfo = this.panelTaskMap.get(panelId);
|
||||||
|
if (taskInfo) {
|
||||||
|
this.currentTaskId = taskInfo.taskId;
|
||||||
|
this.currentProjectPath = taskInfo.projectPath;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除面板的任务映射
|
||||||
|
*/
|
||||||
|
public removePanelTask(panelId: string): void {
|
||||||
|
this.panelTaskMap.delete(panelId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建新任务
|
* 创建新任务
|
||||||
*/
|
*/
|
||||||
@ -264,17 +309,11 @@ export class ChatHistoryManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 确保有当前任务,如果没有则自动创建
|
* 确保有当前任务,如果没有则抛出错误
|
||||||
*/
|
*/
|
||||||
private async ensureCurrentTask(): Promise<void> {
|
private async ensureCurrentTask(): Promise<void> {
|
||||||
if (!this.currentTaskId || !this.currentProjectPath) {
|
if (!this.currentTaskId || !this.currentProjectPath) {
|
||||||
// 获取当前工作区路径
|
throw new Error("没有当前任务上下文,请确保已正确初始化面板任务");
|
||||||
const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
|
|
||||||
if (workspacePath) {
|
|
||||||
await this.createTask(workspacePath, "默认任务");
|
|
||||||
} else {
|
|
||||||
throw new Error("没有打开的工作区,无法创建任务");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,14 +339,15 @@ export class ChatHistoryManager {
|
|||||||
/**
|
/**
|
||||||
* 添加AI消息
|
* 添加AI消息
|
||||||
*/
|
*/
|
||||||
public async addAiMessage(text: string, toolRequests?: any[]): Promise<void> {
|
public async addAiMessage(text: string, toolRequests?: any[], segments?: any[]): Promise<void> {
|
||||||
await this.ensureCurrentTask();
|
await this.ensureCurrentTask();
|
||||||
const messages = await this.loadConversation();
|
const messages = await this.loadConversation();
|
||||||
|
|
||||||
const aiMessage: AiMessage = {
|
const aiMessage: AiMessage = {
|
||||||
type: MessageType.AI,
|
type: MessageType.AI,
|
||||||
text,
|
text,
|
||||||
toolExecutionRequests: toolRequests
|
toolExecutionRequests: toolRequests,
|
||||||
|
segments // 保存完整的 segments 信息
|
||||||
};
|
};
|
||||||
|
|
||||||
messages.push(aiMessage);
|
messages.push(aiMessage);
|
||||||
@ -333,6 +373,24 @@ export class ChatHistoryManager {
|
|||||||
await this.saveConversation(messages);
|
await this.saveConversation(messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加工具执行结果消息
|
||||||
|
*/
|
||||||
|
public async addToolExecutionResult(id: string, toolName: string, result: string): Promise<void> {
|
||||||
|
await this.ensureCurrentTask();
|
||||||
|
const messages = await this.loadConversation();
|
||||||
|
|
||||||
|
const toolResultMessage: ToolExecutionResultMessage = {
|
||||||
|
type: MessageType.TOOL_EXECUTION_RESULT,
|
||||||
|
id,
|
||||||
|
toolName,
|
||||||
|
text: result
|
||||||
|
};
|
||||||
|
|
||||||
|
messages.push(toolResultMessage);
|
||||||
|
await this.saveConversation(messages);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录对话轮次元数据
|
* 记录对话轮次元数据
|
||||||
*/
|
*/
|
||||||
@ -450,6 +508,20 @@ export class ChatHistoryManager {
|
|||||||
tasks.push(JSON.parse(data));
|
tasks.push(JSON.parse(data));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`加载任务 ${taskId} 失败:`, error);
|
console.error(`加载任务 ${taskId} 失败:`, error);
|
||||||
|
// 跳过无效的任务目录
|
||||||
|
// 尝试清理空目录
|
||||||
|
try {
|
||||||
|
const taskDirUri = vscode.Uri.file(path.join(projectDir, taskId));
|
||||||
|
const taskDirEntries = await vscode.workspace.fs.readDirectory(taskDirUri);
|
||||||
|
if (taskDirEntries.length === 0) {
|
||||||
|
// 目录为空,删除它
|
||||||
|
await vscode.workspace.fs.delete(taskDirUri, { recursive: false });
|
||||||
|
console.log(`已清理空任务目录: ${taskId}`);
|
||||||
|
}
|
||||||
|
} catch (cleanupError) {
|
||||||
|
// 清理失败,忽略错误
|
||||||
|
console.warn(`清理任务目录 ${taskId} 失败:`, cleanupError);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -494,4 +566,358 @@ export class ChatHistoryManager {
|
|||||||
public getBaseDir(): string {
|
public getBaseDir(): string {
|
||||||
return this.baseDir;
|
return this.baseDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载指定任务的会话内容
|
||||||
|
* @param projectPath 项目路径
|
||||||
|
* @param taskId 任务ID
|
||||||
|
* @returns 任务会话内容,如果任务不存在则返回null
|
||||||
|
*/
|
||||||
|
public async loadTaskSession(projectPath: string, taskId: string): Promise<TaskSession | null> {
|
||||||
|
const taskDir = this.getTaskDir(projectPath, taskId);
|
||||||
|
const metaPath = path.join(taskDir, 'meta.json');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 检查任务是否存在
|
||||||
|
const metaUri = vscode.Uri.file(metaPath);
|
||||||
|
const metaContent = await vscode.workspace.fs.readFile(metaUri);
|
||||||
|
const meta: TaskMeta = JSON.parse(Buffer.from(metaContent).toString('utf-8'));
|
||||||
|
|
||||||
|
// 读取会话内容
|
||||||
|
const conversationPath = path.join(taskDir, 'conversation.json');
|
||||||
|
let messages: ChatMessage[] = [];
|
||||||
|
try {
|
||||||
|
const conversationUri = vscode.Uri.file(conversationPath);
|
||||||
|
const conversationContent = await vscode.workspace.fs.readFile(conversationUri);
|
||||||
|
messages = JSON.parse(Buffer.from(conversationContent).toString('utf-8'));
|
||||||
|
} catch {
|
||||||
|
// 会话文件不存在,使用空数组
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取会话元数据
|
||||||
|
const conversationMetaPath = path.join(taskDir, 'conversation_meta.jsonl');
|
||||||
|
let conversationMeta: ConversationMeta[] = [];
|
||||||
|
try {
|
||||||
|
const metaUri = vscode.Uri.file(conversationMetaPath);
|
||||||
|
const content = await vscode.workspace.fs.readFile(metaUri);
|
||||||
|
const data = Buffer.from(content).toString('utf-8');
|
||||||
|
conversationMeta = data
|
||||||
|
.split('\n')
|
||||||
|
.filter(line => line.trim())
|
||||||
|
.map(line => JSON.parse(line));
|
||||||
|
} catch {
|
||||||
|
// 元数据文件不存在,使用空数组
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
meta,
|
||||||
|
messages,
|
||||||
|
conversationMeta
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`加载任务 ${taskId} 的会话失败:`, error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取会话历史列表(支持分页)
|
||||||
|
* 返回格式:{ id: taskId, title: 第一句用户消息, timestamp: 创建时间 }
|
||||||
|
* @param projectPath 项目路径
|
||||||
|
* @param offset 偏移量(从第几条开始,默认0)
|
||||||
|
* @param limit 每页数量(默认10条)
|
||||||
|
* @returns { items: 历史列表, total: 总数, hasMore: 是否还有更多 }
|
||||||
|
*/
|
||||||
|
public async getConversationHistoryList(
|
||||||
|
projectPath: string,
|
||||||
|
offset: number = 0,
|
||||||
|
limit: number = 10
|
||||||
|
): Promise<{
|
||||||
|
items: Array<{ id: string; title: string; timestamp: string }>;
|
||||||
|
total: number;
|
||||||
|
hasMore: boolean;
|
||||||
|
}> {
|
||||||
|
const tasks = await this.listProjectTasks(projectPath);
|
||||||
|
const total = tasks.length;
|
||||||
|
const historyList: Array<{ id: string; title: string; timestamp: string }> = [];
|
||||||
|
|
||||||
|
// 计算分页范围
|
||||||
|
const start = offset;
|
||||||
|
const end = Math.min(offset + limit, total);
|
||||||
|
const limitedTasks = tasks.slice(start, end);
|
||||||
|
|
||||||
|
for (const task of limitedTasks) {
|
||||||
|
// 读取该任务的 conversation.json 获取第一句用户消息
|
||||||
|
const taskDir = this.getTaskDir(task.projectPath, task.taskId);
|
||||||
|
const conversationPath = path.join(taskDir, 'conversation.json');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const uri = vscode.Uri.file(conversationPath);
|
||||||
|
const content = await vscode.workspace.fs.readFile(uri);
|
||||||
|
const data = Buffer.from(content).toString('utf-8');
|
||||||
|
const messages: ChatMessage[] = JSON.parse(data);
|
||||||
|
|
||||||
|
// 找到第一条用户消息
|
||||||
|
const firstUserMessage = messages.find(msg => msg.type === MessageType.USER) as UserMessage | undefined;
|
||||||
|
|
||||||
|
let title = '未命名会话';
|
||||||
|
if (firstUserMessage && firstUserMessage.contents && firstUserMessage.contents.length > 0) {
|
||||||
|
const textContent = firstUserMessage.contents.find(c => c.type === 'TEXT');
|
||||||
|
if (textContent && 'text' in textContent) {
|
||||||
|
// 截取前50个字符作为标题
|
||||||
|
title = textContent.text.length > 50
|
||||||
|
? textContent.text.substring(0, 50) + '...'
|
||||||
|
: textContent.text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
historyList.push({
|
||||||
|
id: task.taskId,
|
||||||
|
title,
|
||||||
|
timestamp: task.createdAt
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`读取任务 ${task.taskId} 的会话历史失败:`, error);
|
||||||
|
// 如果读取失败,使用任务名称作为标题
|
||||||
|
historyList.push({
|
||||||
|
id: task.taskId,
|
||||||
|
title: task.taskName || '未命名会话',
|
||||||
|
timestamp: task.createdAt
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回分页结果
|
||||||
|
return {
|
||||||
|
items: historyList,
|
||||||
|
total,
|
||||||
|
hasMore: end < total
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========== 压缩数据相关方法 ==========
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存压缩数据(存入 conversation.json 作为压缩摘要消息)
|
||||||
|
*/
|
||||||
|
public async saveCompactedData(compacted: CompactedMemory): Promise<void> {
|
||||||
|
// 尝试从多个来源获取 projectPath
|
||||||
|
let projectPath = this.currentProjectPath;
|
||||||
|
|
||||||
|
if (!projectPath) {
|
||||||
|
for (const [, taskInfo] of this.panelTaskMap) {
|
||||||
|
if (taskInfo.taskId === compacted.taskId) {
|
||||||
|
projectPath = taskInfo.projectPath;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!projectPath) {
|
||||||
|
console.error('[ChatHistoryManager] 无法保存压缩数据:projectPath 为空');
|
||||||
|
// 通知用户压缩数据保存失败
|
||||||
|
vscode.window.showWarningMessage(
|
||||||
|
'对话历史压缩数据保存失败:无法确定项目路径。后端重启后可能无法恢复完整对话历史。'
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取现有对话历史
|
||||||
|
const taskDir = this.getTaskDir(projectPath, compacted.taskId);
|
||||||
|
const conversationPath = path.join(taskDir, 'conversation.json');
|
||||||
|
let messages: ChatMessage[] = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const uri = vscode.Uri.file(conversationPath);
|
||||||
|
const content = await vscode.workspace.fs.readFile(uri);
|
||||||
|
messages = JSON.parse(Buffer.from(content).toString('utf-8'));
|
||||||
|
} catch {
|
||||||
|
// 文件不存在,使用空数组
|
||||||
|
}
|
||||||
|
|
||||||
|
// 版本检查:防止旧版本覆盖新版本(从尾部扫描,与加载逻辑一致)
|
||||||
|
let existingSummary: CompactionSummaryMessage | null = null;
|
||||||
|
for (let i = messages.length - 1; i >= 0; i--) {
|
||||||
|
if (messages[i].type === MessageType.COMPACTION_SUMMARY) {
|
||||||
|
existingSummary = messages[i] as CompactionSummaryMessage;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (existingSummary && existingSummary.version >= compacted.version) {
|
||||||
|
console.log(`[ChatHistoryManager] 跳过旧版本压缩数据: 现有版本=${existingSummary.version}, 新版本=${compacted.version}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建压缩摘要消息
|
||||||
|
const summaryMessage: CompactionSummaryMessage = {
|
||||||
|
type: MessageType.COMPACTION_SUMMARY,
|
||||||
|
summary: compacted.summary,
|
||||||
|
version: compacted.version,
|
||||||
|
compactedAt: compacted.compactedAt,
|
||||||
|
originalMessageCount: compacted.originalMessageCount,
|
||||||
|
compactedMessageCount: compacted.compactedMessageCount
|
||||||
|
};
|
||||||
|
|
||||||
|
// 添加到对话历史
|
||||||
|
messages.push(summaryMessage);
|
||||||
|
|
||||||
|
// 保存
|
||||||
|
const uri = vscode.Uri.file(conversationPath);
|
||||||
|
const content = Buffer.from(JSON.stringify(messages, null, 2), 'utf-8');
|
||||||
|
await vscode.workspace.fs.writeFile(uri, content);
|
||||||
|
|
||||||
|
// 重置新消息追踪
|
||||||
|
this.newMessagesSinceCompaction = [];
|
||||||
|
|
||||||
|
console.log(`[ChatHistoryManager] 压缩摘要已保存到 conversation.json: taskId=${compacted.taskId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载压缩数据(从 conversation.json 构建)
|
||||||
|
*/
|
||||||
|
public async loadCompactedData(taskId: string): Promise<CompactedMemory | null> {
|
||||||
|
// 尝试从多个来源获取 projectPath
|
||||||
|
let projectPath = this.currentProjectPath;
|
||||||
|
|
||||||
|
if (!projectPath) {
|
||||||
|
for (const [, taskInfo] of this.panelTaskMap) {
|
||||||
|
if (taskInfo.taskId === taskId) {
|
||||||
|
projectPath = taskInfo.projectPath;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!projectPath) {
|
||||||
|
console.log('[ChatHistoryManager] loadCompactedData: projectPath 为空');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取 conversation.json
|
||||||
|
const taskDir = this.getTaskDir(projectPath, taskId);
|
||||||
|
const conversationPath = path.join(taskDir, 'conversation.json');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const uri = vscode.Uri.file(conversationPath);
|
||||||
|
const content = await vscode.workspace.fs.readFile(uri);
|
||||||
|
const messages: ChatMessage[] = JSON.parse(Buffer.from(content).toString('utf-8'));
|
||||||
|
|
||||||
|
if (messages.length === 0) {
|
||||||
|
console.log('[ChatHistoryManager] conversation.json 为空');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从 conversation.json 构建 CompactedMemory
|
||||||
|
return this.buildCompactedMemoryFromConversation(taskId, messages);
|
||||||
|
} catch {
|
||||||
|
console.log('[ChatHistoryManager] conversation.json 不存在:', conversationPath);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 conversation.json 构建 CompactedMemory
|
||||||
|
*/
|
||||||
|
private buildCompactedMemoryFromConversation(taskId: string, messages: ChatMessage[]): CompactedMemory {
|
||||||
|
// 查找最后一个压缩摘要消息
|
||||||
|
let lastSummary: CompactionSummaryMessage | null = null;
|
||||||
|
let summaryIndex = -1;
|
||||||
|
|
||||||
|
for (let i = messages.length - 1; i >= 0; i--) {
|
||||||
|
if (messages[i].type === MessageType.COMPACTION_SUMMARY) {
|
||||||
|
lastSummary = messages[i] as CompactionSummaryMessage;
|
||||||
|
summaryIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取摘要后的消息(或全部消息)
|
||||||
|
const recentMessages = summaryIndex >= 0
|
||||||
|
? messages.slice(summaryIndex + 1)
|
||||||
|
: messages;
|
||||||
|
|
||||||
|
// 转换为 CompactedMessage 格式
|
||||||
|
const compactedMessages: CompactedMessage[] = recentMessages.map(msg => ({
|
||||||
|
type: this.mapMessageType(msg.type),
|
||||||
|
content: this.extractMessageContent(msg)
|
||||||
|
}));
|
||||||
|
|
||||||
|
return {
|
||||||
|
taskId,
|
||||||
|
version: lastSummary?.version || Date.now(),
|
||||||
|
compactedAt: lastSummary?.compactedAt || new Date().toISOString(),
|
||||||
|
summary: lastSummary?.summary || '',
|
||||||
|
recentMessages: compactedMessages,
|
||||||
|
originalMessageCount: messages.length,
|
||||||
|
compactedMessageCount: compactedMessages.length
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 映射消息类型
|
||||||
|
*/
|
||||||
|
private mapMessageType(type: MessageType): 'USER' | 'AI' | 'SYSTEM' | 'TOOL_RESULT' {
|
||||||
|
switch (type) {
|
||||||
|
case MessageType.USER: return 'USER';
|
||||||
|
case MessageType.AI: return 'AI';
|
||||||
|
case MessageType.SYSTEM: return 'SYSTEM';
|
||||||
|
case MessageType.TOOL_EXECUTION_RESULT: return 'TOOL_RESULT';
|
||||||
|
default: return 'USER';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提取消息内容
|
||||||
|
*/
|
||||||
|
private extractMessageContent(msg: ChatMessage): string {
|
||||||
|
switch (msg.type) {
|
||||||
|
case MessageType.USER:
|
||||||
|
return (msg as UserMessage).contents?.[0]?.text || '';
|
||||||
|
case MessageType.AI:
|
||||||
|
return (msg as AiMessage).text || '';
|
||||||
|
case MessageType.SYSTEM:
|
||||||
|
return (msg as SystemMessage).text || '';
|
||||||
|
case MessageType.TOOL_EXECUTION_RESULT:
|
||||||
|
return (msg as ToolExecutionResultMessage).text || '';
|
||||||
|
default:
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取压缩后产生的新消息
|
||||||
|
*/
|
||||||
|
public getNewMessagesSinceCompaction(): CompactedMessage[] {
|
||||||
|
return this.newMessagesSinceCompaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 追踪新消息(用户消息)
|
||||||
|
*/
|
||||||
|
public trackUserMessage(text: string): void {
|
||||||
|
this.newMessagesSinceCompaction.push({
|
||||||
|
type: 'USER',
|
||||||
|
content: text
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 追踪新消息(AI消息)
|
||||||
|
*/
|
||||||
|
public trackAiMessage(text: string): void {
|
||||||
|
this.newMessagesSinceCompaction.push({
|
||||||
|
type: 'AI',
|
||||||
|
content: text
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 追踪新消息(工具执行结果)
|
||||||
|
*/
|
||||||
|
public trackToolResult(toolName: string, result: string): void {
|
||||||
|
this.newMessagesSinceCompaction.push({
|
||||||
|
type: 'TOOL_RESULT',
|
||||||
|
content: `[${toolName}] ${result}`
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,9 @@ export async function createFile(
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法创建相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您创建文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +30,7 @@ export async function createFile(
|
|||||||
throw new Error(`文件已存在: ${absolutePath}`);
|
throw new Error(`文件已存在: ${absolutePath}`);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// 如果文件不存在,继续创建
|
// 如果文件不存在,继续创建
|
||||||
if (error.code !== 'FileNotFound') {
|
if (error.code !== "FileNotFound") {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +67,9 @@ export async function createOrOverwriteFile(
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法创建相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您创建文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +103,9 @@ export async function createDirectory(dirPath: string): Promise<void> {
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, dirPath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, dirPath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法创建相对路径的目录");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您创建目录了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +121,7 @@ export async function createDirectory(dirPath: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// 如果目录不存在,继续创建
|
// 如果目录不存在,继续创建
|
||||||
if (error.code !== 'FileNotFound') {
|
if (error.code !== "FileNotFound") {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,7 +167,9 @@ export async function deleteFile(filePath: string): Promise<void> {
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法删除相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您删除文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +205,9 @@ export async function updateFile(
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法修改相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您修改文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +246,9 @@ export async function appendToFile(
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法追加相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您追加文件内容了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,7 +286,9 @@ export async function replaceFile(
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法修改相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您修改文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,14 +305,17 @@ export async function replaceFile(
|
|||||||
|
|
||||||
// 转义特殊字符,将字符串作为字面量处理
|
// 转义特殊字符,将字符串作为字面量处理
|
||||||
const escapeRegExp = (str: string) => {
|
const escapeRegExp = (str: string) => {
|
||||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
};
|
};
|
||||||
|
|
||||||
// 替换内容 - 如果是字符串,先转义特殊字符
|
// 替换内容 - 如果是字符串,先转义特殊字符
|
||||||
let newContent: string;
|
let newContent: string;
|
||||||
if (typeof searchValue === 'string') {
|
if (typeof searchValue === "string") {
|
||||||
const escapedSearch = escapeRegExp(searchValue);
|
const escapedSearch = escapeRegExp(searchValue);
|
||||||
newContent = fileContent.replace(new RegExp(escapedSearch, "g"), replaceValue);
|
newContent = fileContent.replace(
|
||||||
|
new RegExp(escapedSearch, "g"),
|
||||||
|
replaceValue
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
newContent = fileContent.replace(searchValue, replaceValue);
|
newContent = fileContent.replace(searchValue, replaceValue);
|
||||||
}
|
}
|
||||||
@ -330,7 +347,9 @@ export async function insertAtLine(
|
|||||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法修改相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您修改文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -382,7 +401,9 @@ export async function renameFile(
|
|||||||
absoluteNewPath = path.join(workspaceRoot, newPath);
|
absoluteNewPath = path.join(workspaceRoot, newPath);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error("没有打开的工作区,无法重命名相对路径的文件");
|
throw new Error(
|
||||||
|
"请先打开一个文件夹作为工作区,这样我就能为您重命名文件了"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldUri = vscode.Uri.file(absoluteOldPath);
|
const oldUri = vscode.Uri.file(absoluteOldPath);
|
||||||
@ -401,10 +422,13 @@ export async function renameFile(
|
|||||||
throw new Error(`目标文件已存在: ${absoluteNewPath}`);
|
throw new Error(`目标文件已存在: ${absoluteNewPath}`);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// 如果文件不存在,继续重命名
|
// 如果文件不存在,继续重命名
|
||||||
if (error.code !== 'FileNotFound' && !error.message.includes('目标文件已存在')) {
|
if (
|
||||||
|
error.code !== "FileNotFound" &&
|
||||||
|
!error.message.includes("目标文件已存在")
|
||||||
|
) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
if (error.message.includes('目标文件已存在')) {
|
if (error.message.includes("目标文件已存在")) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -413,3 +413,193 @@ export async function checkIverilogAvailable(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 要 dump 的模块定义
|
||||||
|
*/
|
||||||
|
export interface DumpModule {
|
||||||
|
name: string; // 模块名(用于 VCD 文件名和宏名)
|
||||||
|
path: string; // 实例路径(如 dut.u_tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多 VCD 生成结果
|
||||||
|
*/
|
||||||
|
export interface MultiVCDResult {
|
||||||
|
success: boolean;
|
||||||
|
vcdFiles: Array<{
|
||||||
|
moduleName: string;
|
||||||
|
vcdPath: string;
|
||||||
|
success: boolean;
|
||||||
|
error?: string;
|
||||||
|
}>;
|
||||||
|
message: string;
|
||||||
|
stdout?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 testbench 中注入条件编译代码
|
||||||
|
* 将原有的 $dumpfile/$dumpvars 替换为条件编译版本
|
||||||
|
*/
|
||||||
|
function injectConditionalDump(
|
||||||
|
tbContent: string,
|
||||||
|
dumpModules: DumpModule[],
|
||||||
|
vcdDir: string
|
||||||
|
): string {
|
||||||
|
// 匹配 $dumpfile 和 $dumpvars 语句(可能跨多行)
|
||||||
|
const dumpPattern = /(\$dumpfile\s*\([^)]+\)\s*;[\s\S]*?\$dumpvars\s*\([^)]+\)\s*;)/g;
|
||||||
|
|
||||||
|
// 生成条件编译代码
|
||||||
|
const conditionalCode = generateConditionalDumpCode(dumpModules, vcdDir);
|
||||||
|
|
||||||
|
// 替换原有的 dump 语句
|
||||||
|
const modified = tbContent.replace(dumpPattern, conditionalCode);
|
||||||
|
|
||||||
|
// 如果没有找到匹配,尝试单独匹配 $dumpfile
|
||||||
|
if (modified === tbContent) {
|
||||||
|
const singleDumpPattern = /\$dumpfile\s*\([^)]+\)\s*;/g;
|
||||||
|
return tbContent.replace(singleDumpPattern, conditionalCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成条件编译的 dump 代码
|
||||||
|
*/
|
||||||
|
function generateConditionalDumpCode(
|
||||||
|
dumpModules: DumpModule[],
|
||||||
|
vcdDir: string
|
||||||
|
): string {
|
||||||
|
if (dumpModules.length === 0) {
|
||||||
|
return '$dumpfile("output.vcd");\n $dumpvars(0, dut);';
|
||||||
|
}
|
||||||
|
|
||||||
|
const lines: string[] = [];
|
||||||
|
|
||||||
|
dumpModules.forEach((module, index) => {
|
||||||
|
const macroName = `DUMP_${module.name.toUpperCase()}`;
|
||||||
|
const vcdPath = `${vcdDir}/${module.name}.vcd`;
|
||||||
|
const directive = index === 0 ? '`ifdef' : '`elsif';
|
||||||
|
|
||||||
|
lines.push(`${directive} ${macroName}`);
|
||||||
|
lines.push(` $dumpfile("${vcdPath}");`);
|
||||||
|
lines.push(` $dumpvars(1, ${module.path});`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加默认分支(使用第一个模块)
|
||||||
|
lines.push('`else');
|
||||||
|
lines.push(` $dumpfile("${vcdDir}/${dumpModules[0].name}.vcd");`);
|
||||||
|
lines.push(` $dumpvars(1, ${dumpModules[0].path});`);
|
||||||
|
lines.push('`endif');
|
||||||
|
|
||||||
|
return lines.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成多个 VCD 文件(为不同子模块)
|
||||||
|
*/
|
||||||
|
export async function generateMultiVCD(
|
||||||
|
projectPath: string,
|
||||||
|
extensionPath: string,
|
||||||
|
tbPath: string,
|
||||||
|
dumpModules: DumpModule[],
|
||||||
|
vcdDir: string = 'vcd'
|
||||||
|
): Promise<MultiVCDResult> {
|
||||||
|
const results: MultiVCDResult['vcdFiles'] = [];
|
||||||
|
let allStdout = '';
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 创建 vcd 目录
|
||||||
|
const vcdDirPath = path.join(projectPath, vcdDir);
|
||||||
|
const vcdDirUri = vscode.Uri.file(vcdDirPath);
|
||||||
|
try {
|
||||||
|
await vscode.workspace.fs.createDirectory(vcdDirUri);
|
||||||
|
} catch {
|
||||||
|
// 目录可能已存在
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 读取原始 testbench
|
||||||
|
const tbFullPath = path.isAbsolute(tbPath) ? tbPath : path.join(projectPath, tbPath);
|
||||||
|
const tbUri = vscode.Uri.file(tbFullPath);
|
||||||
|
const tbBytes = await vscode.workspace.fs.readFile(tbUri);
|
||||||
|
const originalTb = Buffer.from(tbBytes).toString('utf-8');
|
||||||
|
|
||||||
|
// 3. 注入条件编译代码
|
||||||
|
const modifiedTb = injectConditionalDump(originalTb, dumpModules, vcdDir);
|
||||||
|
await vscode.workspace.fs.writeFile(tbUri, Buffer.from(modifiedTb, 'utf-8'));
|
||||||
|
|
||||||
|
console.log('[generateMultiVCD] Testbench 已修改,开始多次仿真...');
|
||||||
|
|
||||||
|
// 4. 获取工具路径
|
||||||
|
const iverilogPath = await getIverilogPath(extensionPath);
|
||||||
|
const vvpPath = await getVvpPath(extensionPath);
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
IVERILOG_ROOT: path.join(extensionPath, "tools", "iverilog"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// 5. 获取所有 Verilog 文件
|
||||||
|
const projectCheck = await checkVerilogProject(projectPath);
|
||||||
|
const outputFile = path.join(projectPath, "simulation.vvp");
|
||||||
|
|
||||||
|
// 6. 循环执行仿真
|
||||||
|
for (const module of dumpModules) {
|
||||||
|
const macroName = `DUMP_${module.name.toUpperCase()}`;
|
||||||
|
const vcdPath = path.join(vcdDirPath, `${module.name}.vcd`);
|
||||||
|
|
||||||
|
console.log(`[generateMultiVCD] 仿真模块: ${module.name} (${macroName})`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 编译(带宏定义)
|
||||||
|
const compileArgs = [
|
||||||
|
`-D${macroName}`,
|
||||||
|
"-o", outputFile,
|
||||||
|
...projectCheck.allVerilogFiles
|
||||||
|
];
|
||||||
|
await execCommand(iverilogPath, compileArgs, { cwd: projectPath, env });
|
||||||
|
|
||||||
|
// 仿真
|
||||||
|
const simResult = await execCommand(vvpPath, [outputFile], { cwd: projectPath, env });
|
||||||
|
allStdout += `\n[${module.name}] ${simResult.stdout}`;
|
||||||
|
|
||||||
|
results.push({
|
||||||
|
moduleName: module.name,
|
||||||
|
vcdPath: vcdPath,
|
||||||
|
success: true
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`[generateMultiVCD] 模块 ${module.name} 仿真失败:`, error.message);
|
||||||
|
results.push({
|
||||||
|
moduleName: module.name,
|
||||||
|
vcdPath: vcdPath,
|
||||||
|
success: false,
|
||||||
|
error: error.message
|
||||||
|
});
|
||||||
|
// 继续执行其他模块
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. 清理中间文件
|
||||||
|
try {
|
||||||
|
await vscode.workspace.fs.delete(vscode.Uri.file(outputFile));
|
||||||
|
} catch {
|
||||||
|
// 忽略
|
||||||
|
}
|
||||||
|
|
||||||
|
const successCount = results.filter(r => r.success).length;
|
||||||
|
return {
|
||||||
|
success: successCount > 0,
|
||||||
|
vcdFiles: results,
|
||||||
|
message: `生成完成:${successCount}/${dumpModules.length} 个 VCD 文件成功`,
|
||||||
|
stdout: allStdout
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
vcdFiles: results,
|
||||||
|
message: `生成多 VCD 文件失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
104
src/utils/jwtUtils.ts
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/**
|
||||||
|
* JWT 工具函数
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT Payload 接口
|
||||||
|
*/
|
||||||
|
export interface JwtPayload {
|
||||||
|
sub?: string; // subject (通常是 userId)
|
||||||
|
userId?: number; // 用户ID (驼峰命名)
|
||||||
|
user_id?: number; // 用户ID (下划线命名)
|
||||||
|
exp?: number; // 过期时间
|
||||||
|
iat?: number; // 签发时间
|
||||||
|
[key: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 JWT token 的 payload
|
||||||
|
* @param token JWT token
|
||||||
|
* @returns 解析后的 payload,解析失败返回 null
|
||||||
|
*/
|
||||||
|
export function parseJwtPayload(token: string): JwtPayload | null {
|
||||||
|
try {
|
||||||
|
const parts = token.split(".");
|
||||||
|
if (parts.length !== 3) {
|
||||||
|
console.warn("[JWT] token 格式不正确,期望3部分,实际:", parts.length);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// payload 是第二部分,base64url 编码
|
||||||
|
const payload = parts[1];
|
||||||
|
|
||||||
|
// base64url 转 base64
|
||||||
|
const base64 = payload.replace(/-/g, "+").replace(/_/g, "/");
|
||||||
|
|
||||||
|
// 解码
|
||||||
|
const jsonStr = Buffer.from(base64, "base64").toString("utf-8");
|
||||||
|
const parsed = JSON.parse(jsonStr);
|
||||||
|
|
||||||
|
console.log("[JWT] 解析成功, payload 字段:", Object.keys(parsed));
|
||||||
|
console.log("[JWT] payload 内容:", JSON.stringify(parsed));
|
||||||
|
return parsed;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[JWT] 解析失败:", error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 JWT token 中获取用户ID
|
||||||
|
* @param token JWT token
|
||||||
|
* @returns 用户ID字符串,获取失败返回 null
|
||||||
|
*/
|
||||||
|
export function getUserIdFromToken(token: string): string | null {
|
||||||
|
const payload = parseJwtPayload(token);
|
||||||
|
if (!payload) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 支持多种字段名:user_id, userId, sub
|
||||||
|
if (payload.user_id !== undefined) {
|
||||||
|
return String(payload.user_id);
|
||||||
|
}
|
||||||
|
if (payload.userId !== undefined) {
|
||||||
|
return String(payload.userId);
|
||||||
|
}
|
||||||
|
if (payload.sub !== undefined) {
|
||||||
|
return String(payload.sub);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn("[JWT] payload 中没有 user_id, userId 或 sub 字段");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测 JWT token 是否已过期
|
||||||
|
* @param token JWT token
|
||||||
|
* @param bufferSeconds 提前多少秒判定为过期(默认60秒)
|
||||||
|
* @returns true 表示已过期,false 表示未过期,null 表示无法判断
|
||||||
|
*/
|
||||||
|
export function isTokenExpired(
|
||||||
|
token: string,
|
||||||
|
bufferSeconds: number = 60,
|
||||||
|
): boolean | null {
|
||||||
|
const payload = parseJwtPayload(token);
|
||||||
|
if (!payload) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (payload.exp === undefined) {
|
||||||
|
console.warn("[JWT] payload 中没有 exp 字段,无法判断过期");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
const expTime = payload.exp - bufferSeconds;
|
||||||
|
const isExpired = now >= expTime;
|
||||||
|
|
||||||
|
if (isExpired) {
|
||||||
|
console.warn("[JWT] token 已过期,exp:", payload.exp, "当前:", now);
|
||||||
|
}
|
||||||
|
|
||||||
|
return isExpired;
|
||||||
|
}
|
||||||
@ -15,6 +15,27 @@ import {
|
|||||||
checkIverilogAvailable,
|
checkIverilogAvailable,
|
||||||
} from "./iverilogRunner";
|
} from "./iverilogRunner";
|
||||||
import { ChatHistoryManager } from "./chatHistoryManager";
|
import { ChatHistoryManager } from "./chatHistoryManager";
|
||||||
|
import { dialogManager, DialogSession } from "../services/dialogService";
|
||||||
|
import { userInteractionManager } from "../services/userInteraction";
|
||||||
|
import { healthCheck } from "../services/apiClient";
|
||||||
|
import { isTokenExpired } from "./jwtUtils";
|
||||||
|
import {
|
||||||
|
checkBalanceBeforeSend,
|
||||||
|
fetchBalance,
|
||||||
|
} from "../services/creditsService";
|
||||||
|
import { optimizePrompt } from "../services/promptOptimizeService";
|
||||||
|
import { NotificationService } from "../services/notificationService";
|
||||||
|
|
||||||
|
import type { RunMode, ServiceTier } from "../types/api";
|
||||||
|
|
||||||
|
/** 是否使用后端服务(可通过配置控制) */
|
||||||
|
let useBackendService = true;
|
||||||
|
|
||||||
|
/** 当前对话会话 */
|
||||||
|
let currentSession: DialogSession | null = null;
|
||||||
|
|
||||||
|
/** 最后一个活跃的 taskId(用于压缩等操作) */
|
||||||
|
let lastTaskId: string | null = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理用户消息
|
* 处理用户消息
|
||||||
@ -22,44 +43,516 @@ import { ChatHistoryManager } from "./chatHistoryManager";
|
|||||||
export async function handleUserMessage(
|
export async function handleUserMessage(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
text: string,
|
text: string,
|
||||||
extensionPath?: string
|
extensionPath?: string,
|
||||||
|
mode?: RunMode,
|
||||||
|
serviceTier?: ServiceTier // 新增:服务等级参数
|
||||||
) {
|
) {
|
||||||
console.log("收到用户消息:", text);
|
console.log("收到用户消息:", text);
|
||||||
|
|
||||||
// 记录用户消息到历史
|
// 检查 token 是否过期
|
||||||
const historyManager = ChatHistoryManager.getInstance();
|
const context = (panel as any).__context;
|
||||||
await historyManager.addUserMessage(text);
|
if (context) {
|
||||||
|
// 从 session 中获取 token
|
||||||
|
let token: string | undefined;
|
||||||
|
try {
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
|
||||||
|
token = session?.accessToken;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[MessageHandler] 获取 session 失败:", error);
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否是 VCD 生成命令
|
if (!token) {
|
||||||
|
console.warn("[MessageHandler] 未登录,阻止发送");
|
||||||
|
|
||||||
|
// 保存待发送的消息
|
||||||
|
await context.globalState.update('pendingMessage', {
|
||||||
|
text,
|
||||||
|
mode,
|
||||||
|
serviceTier,
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
|
// 显示弹窗提示
|
||||||
|
const action = await vscode.window.showWarningMessage(
|
||||||
|
'请先登录后再发送消息',
|
||||||
|
'立即登录'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (action === '立即登录') {
|
||||||
|
vscode.commands.executeCommand("ic-coder.login");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTokenExpired(token)) {
|
||||||
|
console.warn("[MessageHandler] Token 已过期,阻止发送");
|
||||||
|
|
||||||
|
// 保存待发送的消息
|
||||||
|
await context.globalState.update('pendingMessage', {
|
||||||
|
text,
|
||||||
|
mode,
|
||||||
|
serviceTier,
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
|
// 清除过期的 session
|
||||||
|
await context.globalState.update('icCoderSessions', []);
|
||||||
|
await context.globalState.update('icCoderUserInfo', undefined);
|
||||||
|
|
||||||
|
// 显示弹窗提示
|
||||||
|
const action = await vscode.window.showWarningMessage(
|
||||||
|
'登录已过期,请重新登录',
|
||||||
|
'立即登录'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (action === '立即登录') {
|
||||||
|
vscode.commands.executeCommand("ic-coder.login");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录用户消息到历史(允许失败,不阻塞主流程)
|
||||||
|
try {
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
await historyManager.addUserMessage(text);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("记录消息历史失败(可能没有打开工作区):", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置 WebView 面板用于用户交互
|
||||||
|
userInteractionManager.setWebviewPanel(panel);
|
||||||
|
|
||||||
|
// 检查是否是 VCD 生成命令(本地处理)
|
||||||
if (isVCDGenerationCommand(text)) {
|
if (isVCDGenerationCommand(text)) {
|
||||||
await handleVCDGeneration(panel, extensionPath || "");
|
await handleVCDGeneration(panel, extensionPath || "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否是文件操作命令
|
// 检查是否是文件操作命令(本地处理)
|
||||||
const fileOperation = parseFileOperation(text);
|
const fileOperation = parseFileOperation(text);
|
||||||
|
|
||||||
console.log("解析结果:", fileOperation);
|
|
||||||
|
|
||||||
if (fileOperation) {
|
if (fileOperation) {
|
||||||
console.log("执行文件操作:", fileOperation.type, fileOperation.filePath);
|
console.log("执行文件操作:", fileOperation.type, fileOperation.filePath);
|
||||||
await handleFileOperation(panel, fileOperation);
|
await handleFileOperation(panel, fileOperation);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 普通消息处理
|
// 发送前检测余额
|
||||||
console.log("作为普通消息处理");
|
const balanceCheck = await checkBalanceBeforeSend();
|
||||||
const reply = getMockReply(text);
|
if (!balanceCheck.allowed) {
|
||||||
|
console.warn("[MessageHandler] 余额不足,阻止发送:", balanceCheck.message);
|
||||||
// 记录助手回复到历史
|
// 显示错误提示
|
||||||
await historyManager.addAiMessage(reply);
|
const selection = await vscode.window.showWarningMessage(
|
||||||
|
balanceCheck.message || "资源点余额不足",
|
||||||
setTimeout(() => {
|
"去充值"
|
||||||
|
);
|
||||||
|
if (selection === "去充值") {
|
||||||
|
vscode.env.openExternal(
|
||||||
|
vscode.Uri.parse("https://iccoder.com/memberCenter")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// 恢复输入状态
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "receiveMessage",
|
command: "updateSegments",
|
||||||
text: reply,
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
});
|
});
|
||||||
}, 500);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试使用后端服务
|
||||||
|
if (useBackendService && extensionPath) {
|
||||||
|
try {
|
||||||
|
await handleUserMessageWithBackend(
|
||||||
|
panel,
|
||||||
|
text,
|
||||||
|
extensionPath,
|
||||||
|
mode,
|
||||||
|
undefined,
|
||||||
|
serviceTier
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("后端服务不可用:", error);
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateStatus",
|
||||||
|
text: "后端服务不可用",
|
||||||
|
type: "error",
|
||||||
|
});
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有 extensionPath,显示错误
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateStatus",
|
||||||
|
text: "无法处理消息:缺少必要参数",
|
||||||
|
type: "error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用后端服务处理用户消息
|
||||||
|
*/
|
||||||
|
async function handleUserMessageWithBackend(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
text: string,
|
||||||
|
extensionPath: string,
|
||||||
|
mode?: RunMode,
|
||||||
|
reuseTaskId?: string, // 可选,复用现有 taskId(用于 Plan 模式确认后继续执行)
|
||||||
|
serviceTier?: ServiceTier // 新增:服务等级参数
|
||||||
|
): Promise<void> {
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
|
||||||
|
// 获取 historyManager 中的 taskId(由 ICHelperPanel 创建)
|
||||||
|
// 优先使用 reuseTaskId,其次使用 historyManager 的 taskId
|
||||||
|
const taskIdToUse = reuseTaskId || historyManager.getCurrentTaskId();
|
||||||
|
|
||||||
|
// 创建会话(dialogManager 会自动处理旧会话的中止)
|
||||||
|
currentSession = dialogManager.createSession(
|
||||||
|
extensionPath,
|
||||||
|
taskIdToUse || undefined
|
||||||
|
);
|
||||||
|
// 保存 taskId 用于后续操作(如压缩)
|
||||||
|
lastTaskId = currentSession.getTaskId();
|
||||||
|
console.log(
|
||||||
|
"[MessageHandler] 创建会话: taskId=",
|
||||||
|
lastTaskId,
|
||||||
|
"来源=",
|
||||||
|
taskIdToUse ? "historyManager" : "新生成"
|
||||||
|
);
|
||||||
|
|
||||||
|
// 显示状态栏
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateStatus",
|
||||||
|
text: "思考中...",
|
||||||
|
type: "thinking",
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
currentSession!.sendMessage(
|
||||||
|
text,
|
||||||
|
{
|
||||||
|
onText: (fullText, isStreaming) => {
|
||||||
|
// 不再单独处理文本,统一通过 onSegmentUpdate 处理
|
||||||
|
},
|
||||||
|
|
||||||
|
onSegmentUpdate: (segments) => {
|
||||||
|
// 实时发送段落更新,按后端返回顺序展示
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: segments,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onToolStart: (toolName) => {
|
||||||
|
// 更新状态栏
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateStatus",
|
||||||
|
text: `正在执行 ${toolName}...`,
|
||||||
|
type: "working",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onToolComplete: (toolName, result) => {
|
||||||
|
// 工具完成,不需要单独处理,通过 onSegmentUpdate 统一更新
|
||||||
|
},
|
||||||
|
|
||||||
|
onToolError: (toolName, error) => {
|
||||||
|
// 工具错误,不需要单独处理,通过 onSegmentUpdate 统一更新
|
||||||
|
},
|
||||||
|
|
||||||
|
onQuestion: (askId, question, options) => {
|
||||||
|
// 只更新状态栏,问题显示由 onSegmentUpdate 统一处理
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateStatus",
|
||||||
|
text: "等待用户回答...",
|
||||||
|
type: "working",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onComplete: async (segments) => {
|
||||||
|
console.log("[MessageHandler] 对话完成, 段落数:", segments.length);
|
||||||
|
|
||||||
|
// 先保存到历史记录(优先级最高,确保数据不丢失)
|
||||||
|
try {
|
||||||
|
// 将完整的 segments 保存到一条 AI 消息中
|
||||||
|
// 这样加载时可以完整还原对话样式
|
||||||
|
const textContent = segments
|
||||||
|
.filter((s) => s.type === "text" && s.content)
|
||||||
|
.map((s) => s.content)
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
await historyManager.addAiMessage(textContent, undefined, segments);
|
||||||
|
console.log("[MessageHandler] AI响应已保存到历史记录");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[MessageHandler] 保存AI响应历史失败:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对话完成后重新获取余额(因为已经消耗了 Credits)
|
||||||
|
try {
|
||||||
|
console.log("[MessageHandler] 对话完成,重新获取余额...");
|
||||||
|
const newBalance = await fetchBalance();
|
||||||
|
if (newBalance !== null) {
|
||||||
|
console.log("[MessageHandler] 余额已更新:", newBalance);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[MessageHandler] 获取余额失败:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试更新面板(如果面板已关闭,这些操作会失败,但不影响数据保存)
|
||||||
|
try {
|
||||||
|
// 隐藏状态栏
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "hideStatus",
|
||||||
|
});
|
||||||
|
|
||||||
|
// 最后一次发送完整的段落
|
||||||
|
const result = await panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: segments,
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
console.log("[MessageHandler] postMessage 返回值:", result);
|
||||||
|
|
||||||
|
// 发送系统通知 - AI 响应完成
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder - AI 响应完成',
|
||||||
|
'您的问题已得到回复,点击查看详情',
|
||||||
|
() => {
|
||||||
|
// 点击通知时聚焦到面板
|
||||||
|
panel.reveal();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[MessageHandler] 更新面板失败(面板可能已关闭):", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
},
|
||||||
|
|
||||||
|
onError: (message) => {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "hideLoading",
|
||||||
|
});
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "receiveMessage",
|
||||||
|
text: `❌ 错误: ${message}`,
|
||||||
|
});
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
reject(new Error(message));
|
||||||
|
},
|
||||||
|
|
||||||
|
onNotification: (message) => {
|
||||||
|
vscode.window.showInformationMessage(message);
|
||||||
|
},
|
||||||
|
|
||||||
|
onContextUsage: (data) => {
|
||||||
|
// 发送上下文使用量到 WebView
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "contextUsage",
|
||||||
|
currentTokens: data.currentTokens,
|
||||||
|
maxTokens: data.maxTokens,
|
||||||
|
percentage: data.percentage,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onPhaseProgress: (phaseId, status) => {
|
||||||
|
// 发送阶段进度更新到 WebView
|
||||||
|
// 映射 phaseId: sim -> simulation
|
||||||
|
const stepMap: Record<string, string> = {
|
||||||
|
spec: "spec",
|
||||||
|
design: "design",
|
||||||
|
sim: "simulation",
|
||||||
|
done: "done",
|
||||||
|
};
|
||||||
|
const step = stepMap[phaseId] || phaseId;
|
||||||
|
|
||||||
|
if (status === "current") {
|
||||||
|
// 显示进度条并更新到当前步骤
|
||||||
|
panel.webview.postMessage({ type: "showProgress" });
|
||||||
|
panel.webview.postMessage({ type: "updateProgress", step });
|
||||||
|
} else if (status === "completed") {
|
||||||
|
// 更新到下一步(或完成)
|
||||||
|
const steps = ["spec", "design", "simulation", "done"];
|
||||||
|
const currentIndex = steps.indexOf(step);
|
||||||
|
if (currentIndex < steps.length - 1) {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
type: "updateProgress",
|
||||||
|
step: steps[currentIndex + 1],
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
panel.webview.postMessage({ type: "completeProgress" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
mode,
|
||||||
|
serviceTier // 传递服务等级
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理用户回答(从 WebView 调用)
|
||||||
|
*/
|
||||||
|
export async function handleUserAnswer(
|
||||||
|
askId: string,
|
||||||
|
selected?: string[],
|
||||||
|
customInput?: string
|
||||||
|
): Promise<void> {
|
||||||
|
if (currentSession) {
|
||||||
|
await currentSession.submitAnswer(askId, selected, customInput);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中止当前对话
|
||||||
|
*/
|
||||||
|
export async function abortCurrentDialog(): Promise<void> {
|
||||||
|
if (currentSession) {
|
||||||
|
// 保存当前已有的对话内容
|
||||||
|
const segments = currentSession.getSegments();
|
||||||
|
if (segments && segments.length > 0) {
|
||||||
|
try {
|
||||||
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
const textContent = segments
|
||||||
|
.filter((s) => s.type === "text" && s.content)
|
||||||
|
.map((s) => s.content)
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
// 添加中止标记
|
||||||
|
const abortedContent = textContent + "\n\n[对话已被用户中止]";
|
||||||
|
await historyManager.addAiMessage(abortedContent, undefined, segments);
|
||||||
|
console.log("[MessageHandler] 已保存中止前的对话内容");
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[MessageHandler] 保存中止对话失败:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通知 WebView 重置分段消息容器
|
||||||
|
const panel = userInteractionManager.getWebviewPanel();
|
||||||
|
if (panel) {
|
||||||
|
panel.webview.postMessage({ command: "resetSegmentedMessage" });
|
||||||
|
console.log("[MessageHandler] 已发送重置分段消息命令");
|
||||||
|
}
|
||||||
|
|
||||||
|
dialogManager.abortCurrentSession();
|
||||||
|
currentSession = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前会话的 taskId
|
||||||
|
*/
|
||||||
|
export function getCurrentTaskId(): string | null {
|
||||||
|
return currentSession?.getTaskId() || lastTaskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置最后的 taskId(加载历史会话时调用)
|
||||||
|
*/
|
||||||
|
export function setLastTaskId(taskId: string): void {
|
||||||
|
lastTaskId = taskId;
|
||||||
|
console.log("[MessageHandler] 设置 lastTaskId:", taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理计划操作(Plan 模式)
|
||||||
|
* @param panel WebView 面板
|
||||||
|
* @param action 操作类型:confirm/modify/cancel
|
||||||
|
* @param planTitle 计划标题
|
||||||
|
* @param extensionPath 扩展路径
|
||||||
|
*/
|
||||||
|
export async function handlePlanAction(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
action: string,
|
||||||
|
planTitle: string,
|
||||||
|
extensionPath: string,
|
||||||
|
serviceTier?: ServiceTier
|
||||||
|
): Promise<void> {
|
||||||
|
console.log(
|
||||||
|
"[handlePlanAction] action:",
|
||||||
|
action,
|
||||||
|
"planTitle:",
|
||||||
|
planTitle,
|
||||||
|
"serviceTier:",
|
||||||
|
serviceTier
|
||||||
|
);
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
case "confirm":
|
||||||
|
// 确认执行:切换到 Agent 模式并发送执行消息
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "switchMode",
|
||||||
|
mode: "agent",
|
||||||
|
});
|
||||||
|
// 发送执行消息
|
||||||
|
await handleUserMessage(
|
||||||
|
panel,
|
||||||
|
`请按照刚才的计划执行:${planTitle}`,
|
||||||
|
extensionPath,
|
||||||
|
"agent",
|
||||||
|
serviceTier
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "modify":
|
||||||
|
// 修改计划:提示用户输入修改建议
|
||||||
|
const modification = await vscode.window.showInputBox({
|
||||||
|
prompt: "请输入您对计划的修改建议",
|
||||||
|
placeHolder: "例如:第2步需要先检查文件是否存在...",
|
||||||
|
ignoreFocusOut: true,
|
||||||
|
});
|
||||||
|
if (modification) {
|
||||||
|
await handleUserMessage(
|
||||||
|
panel,
|
||||||
|
`请根据以下建议修改计划:${modification}`,
|
||||||
|
extensionPath,
|
||||||
|
"plan",
|
||||||
|
serviceTier
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "cancel":
|
||||||
|
// 取消计划:通知用户
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "addMessage",
|
||||||
|
text: "计划已取消。",
|
||||||
|
sender: "bot",
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.warn("[handlePlanAction] 未知操作:", action);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,7 +590,9 @@ function parseFileOperation(text: string): {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 匹配重命名文件:将 xxx.ts 重命名为 yyy.ts 或 把 xxx.ts 改名为 yyy.ts(优先匹配,避免被修改匹配)
|
// 匹配重命名文件:将 xxx.ts 重命名为 yyy.ts 或 把 xxx.ts 改名为 yyy.ts(优先匹配,避免被修改匹配)
|
||||||
const renameMatch = lowerText.match(/(?:将|把)\s*(.+?\.\w+)\s*(?:重命名|改名)\s*(?:为|成)\s*(.+?\.\w+)/);
|
const renameMatch = lowerText.match(
|
||||||
|
/(?:将|把)\s*(.+?\.\w+)\s*(?:重命名|改名)\s*(?:为|成)\s*(.+?\.\w+)/
|
||||||
|
);
|
||||||
if (renameMatch) {
|
if (renameMatch) {
|
||||||
const oldPath = renameMatch[1].trim();
|
const oldPath = renameMatch[1].trim();
|
||||||
const newPath = renameMatch[2].trim();
|
const newPath = renameMatch[2].trim();
|
||||||
@ -112,7 +607,9 @@ function parseFileOperation(text: string): {
|
|||||||
// 格式1: 在 xxx.ts 中将 "aaa" 替换为 "bbb"
|
// 格式1: 在 xxx.ts 中将 "aaa" 替换为 "bbb"
|
||||||
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
||||||
// 格式3: 将 xxx.ts 文件 'aaa' 替换为 'bbb'
|
// 格式3: 将 xxx.ts 文件 'aaa' 替换为 'bbb'
|
||||||
const replaceMatch1 = lowerText.match(/在\s*(.+?\.\w+)\s*(?:文件)?中?\s*(?:将|把)\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/);
|
const replaceMatch1 = lowerText.match(
|
||||||
|
/在\s*(.+?\.\w+)\s*(?:文件)?中?\s*(?:将|把)\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/
|
||||||
|
);
|
||||||
if (replaceMatch1) {
|
if (replaceMatch1) {
|
||||||
const filePath = replaceMatch1[1].trim();
|
const filePath = replaceMatch1[1].trim();
|
||||||
const searchText = replaceMatch1[2].trim();
|
const searchText = replaceMatch1[2].trim();
|
||||||
@ -126,7 +623,9 @@ function parseFileOperation(text: string): {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
||||||
const replaceMatch2 = lowerText.match(/(?:将|把)\s*(.+?\.\w+)\s*(?:文件)?\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/);
|
const replaceMatch2 = lowerText.match(
|
||||||
|
/(?:将|把)\s*(.+?\.\w+)\s*(?:文件)?\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/
|
||||||
|
);
|
||||||
if (replaceMatch2) {
|
if (replaceMatch2) {
|
||||||
const filePath = replaceMatch2[1].trim();
|
const filePath = replaceMatch2[1].trim();
|
||||||
const searchText = replaceMatch2[2].trim();
|
const searchText = replaceMatch2[2].trim();
|
||||||
@ -358,6 +857,16 @@ export async function handleCreateFile(
|
|||||||
message: " 文件创建成功",
|
message: " 文件创建成功",
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(`文件创建成功: ${filePath}`);
|
vscode.window.showInformationMessage(`文件创建成功: ${filePath}`);
|
||||||
|
|
||||||
|
// 发送系统通知
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder - 文件创建',
|
||||||
|
`文件已创建: ${path.basename(filePath)}`,
|
||||||
|
() => {
|
||||||
|
vscode.commands.executeCommand('vscode.open', vscode.Uri.file(filePath));
|
||||||
|
}
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "fileCreateError",
|
command: "fileCreateError",
|
||||||
@ -385,6 +894,13 @@ export async function handleUpdateFile(
|
|||||||
message: " 文件更新成功",
|
message: " 文件更新成功",
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(`文件更新成功: ${filePath}`);
|
vscode.window.showInformationMessage(`文件更新成功: ${filePath}`);
|
||||||
|
|
||||||
|
// 发送系统通知
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.info(
|
||||||
|
'IC Coder - 文件更新',
|
||||||
|
`文件已更新: ${path.basename(filePath)}`
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "fileUpdateError",
|
command: "fileUpdateError",
|
||||||
@ -454,41 +970,6 @@ export async function handleReplaceInFile(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取模拟回复
|
|
||||||
*/
|
|
||||||
function getMockReply(question: string): string {
|
|
||||||
const replies = [
|
|
||||||
`已收到您的问题:"${question}"
|
|
||||||
|
|
||||||
这是一个演示版本,实际需要连接AI服务。
|
|
||||||
|
|
||||||
示例回复:这是一个计数器模板:
|
|
||||||
\`\`\`verilog
|
|
||||||
module counter (
|
|
||||||
input clk,
|
|
||||||
input rst_n,
|
|
||||||
output reg [3:0] count
|
|
||||||
);
|
|
||||||
always @(posedge clk or negedge rst_n) begin
|
|
||||||
if (!rst_n) count <= 0;
|
|
||||||
else count <= count + 1;
|
|
||||||
end
|
|
||||||
endmodule
|
|
||||||
\`\`\``,
|
|
||||||
|
|
||||||
`感谢提问!关于"${question}",在真实版本中我会:
|
|
||||||
1. 分析您的代码上下文
|
|
||||||
2. 提供优化建议
|
|
||||||
3. 生成完整代码
|
|
||||||
4. 解释设计原理
|
|
||||||
|
|
||||||
当前是演示版,请点击侧边栏按钮快速生成代码。`,
|
|
||||||
];
|
|
||||||
|
|
||||||
return replies[Math.floor(Math.random() * replies.length)];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将代码插入到编辑器
|
* 将代码插入到编辑器
|
||||||
*/
|
*/
|
||||||
@ -581,7 +1062,8 @@ async function handleVCDGeneration(
|
|||||||
|
|
||||||
if (!projectCheck.hasTestbench) {
|
if (!projectCheck.hasTestbench) {
|
||||||
errorMsg += "• ❌ 缺少 testbench 文件\n";
|
errorMsg += "• ❌ 缺少 testbench 文件\n";
|
||||||
errorMsg += "\n提示: testbench 文件应包含 $dumpfile 和 $dumpvars 语句来生成 VCD 文件。\n";
|
errorMsg +=
|
||||||
|
"\n提示: testbench 文件应包含 $dumpfile 和 $dumpvars 语句来生成 VCD 文件。\n";
|
||||||
} else {
|
} else {
|
||||||
errorMsg += `• ✅ Testbench: ${projectCheck.testbenchFile}\n`;
|
errorMsg += `• ✅ Testbench: ${projectCheck.testbenchFile}\n`;
|
||||||
}
|
}
|
||||||
@ -625,8 +1107,17 @@ async function handleVCDGeneration(
|
|||||||
fileName: fileName,
|
fileName: fileName,
|
||||||
});
|
});
|
||||||
|
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(`VCD 文件生成成功: ${fileName}`);
|
||||||
`VCD 文件生成成功: ${fileName}`
|
|
||||||
|
// 发送系统通知
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.success(
|
||||||
|
'IC Coder - 仿真完成',
|
||||||
|
`VCD 文件已生成: ${fileName}`,
|
||||||
|
() => {
|
||||||
|
// 点击通知时打开 VCD 查看器
|
||||||
|
vscode.commands.executeCommand('ic-coder.openVCDViewer', result.vcdFilePath);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
@ -651,6 +1142,17 @@ async function handleVCDGeneration(
|
|||||||
});
|
});
|
||||||
|
|
||||||
vscode.window.showErrorMessage("VCD 文件生成失败");
|
vscode.window.showErrorMessage("VCD 文件生成失败");
|
||||||
|
|
||||||
|
// 发送系统通知
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.error(
|
||||||
|
'IC Coder - 仿真失败',
|
||||||
|
'VCD 文件生成失败,请查看错误信息',
|
||||||
|
() => {
|
||||||
|
// 点击通知时聚焦到面板
|
||||||
|
panel.reveal();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMsg = `❌ 生成 VCD 文件时出错: ${
|
const errorMsg = `❌ 生成 VCD 文件时出错: ${
|
||||||
@ -663,5 +1165,47 @@ async function handleVCDGeneration(
|
|||||||
});
|
});
|
||||||
|
|
||||||
vscode.window.showErrorMessage(errorMsg);
|
vscode.window.showErrorMessage(errorMsg);
|
||||||
|
|
||||||
|
// 发送系统通知
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.error(
|
||||||
|
'IC Coder - 仿真错误',
|
||||||
|
error instanceof Error ? error.message : '生成 VCD 文件时出错',
|
||||||
|
() => {
|
||||||
|
panel.reveal();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理提示词优化请求
|
||||||
|
*/
|
||||||
|
export async function handleOptimizePrompt(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
prompt: string
|
||||||
|
): Promise<void> {
|
||||||
|
console.log("[MessageHandler] ========== 收到提示词优化请求 ==========");
|
||||||
|
console.log("[MessageHandler] prompt:", prompt);
|
||||||
|
console.log("[MessageHandler] prompt 长度:", prompt?.length);
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log("[MessageHandler] 开始调用 optimizePrompt...");
|
||||||
|
const optimized = await optimizePrompt(prompt);
|
||||||
|
console.log("[MessageHandler] 优化成功,结果:", optimized);
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "optimizeResult",
|
||||||
|
success: true,
|
||||||
|
optimizedPrompt: optimized,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const errorMsg = error instanceof Error ? error.message : "优化失败";
|
||||||
|
console.error("[MessageHandler] 提示词优化失败:", errorMsg);
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "optimizeResult",
|
||||||
|
success: false,
|
||||||
|
error: errorMsg,
|
||||||
|
});
|
||||||
|
vscode.window.showErrorMessage(`提示词优化失败: ${errorMsg}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,6 +94,13 @@ export async function readDirectory(
|
|||||||
const results = [];
|
const results = [];
|
||||||
|
|
||||||
for (const [fileName, fileType] of entries) {
|
for (const [fileName, fileType] of entries) {
|
||||||
|
// 处理目录
|
||||||
|
if (fileType === vscode.FileType.Directory) {
|
||||||
|
results.push({ path: fileName + '/', content: '[目录]', isDirectory: true });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理文件
|
||||||
if (fileType === vscode.FileType.File) {
|
if (fileType === vscode.FileType.File) {
|
||||||
// 如果指定了扩展名过滤
|
// 如果指定了扩展名过滤
|
||||||
if (extensions && extensions.length > 0) {
|
if (extensions && extensions.length > 0) {
|
||||||
@ -108,7 +115,7 @@ export async function readDirectory(
|
|||||||
const fileUri = vscode.Uri.file(filePath);
|
const fileUri = vscode.Uri.file(filePath);
|
||||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||||
const content = Buffer.from(contentBytes).toString("utf-8");
|
const content = Buffer.from(contentBytes).toString("utf-8");
|
||||||
results.push({ path: fileName, content });
|
results.push({ path: fileName, content, isDirectory: false });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 跳过无法读取的文件
|
// 跳过无法读取的文件
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
467
src/utils/vcdParser.ts
Normal file
@ -0,0 +1,467 @@
|
|||||||
|
/**
|
||||||
|
* VCD (Value Change Dump) 解析器
|
||||||
|
* 纯 TypeScript 实现,参照 VerilogCoder 项目格式
|
||||||
|
*
|
||||||
|
* @deprecated 当前未使用,保留备用
|
||||||
|
* 目前使用 waveformTracer.ts 调用 Python 打包的 waveform_trace.exe
|
||||||
|
* 未来可能用此文件替换 Python 实现
|
||||||
|
*/
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
// ==================== 类型定义 ====================
|
||||||
|
|
||||||
|
/** 信号定义 */
|
||||||
|
export interface VcdSignal {
|
||||||
|
name: string; // 完整路径名,如 "tb.top_module.data"
|
||||||
|
shortName: string; // 短名,如 "data"
|
||||||
|
symbolId: string; // VCD 符号 ID,如 "!", "#"
|
||||||
|
width: number; // 位宽
|
||||||
|
varType: string; // 变量类型:wire, reg
|
||||||
|
module: string; // 所属模块
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 时间-值对 */
|
||||||
|
export interface TimeValue {
|
||||||
|
time: number;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 信号波形数据 */
|
||||||
|
export interface SignalWaveform {
|
||||||
|
signal: VcdSignal;
|
||||||
|
changes: TimeValue[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** VCD 解析结果 */
|
||||||
|
export interface VcdData {
|
||||||
|
date?: string;
|
||||||
|
version?: string;
|
||||||
|
timescale: string;
|
||||||
|
endTime: number;
|
||||||
|
signals: Map<string, VcdSignal>; // symbolId -> signal
|
||||||
|
waveforms: Map<string, TimeValue[]>; // symbolId -> changes
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mismatch 信息 */
|
||||||
|
export interface MismatchInfo {
|
||||||
|
time: number;
|
||||||
|
signal: string;
|
||||||
|
dutValue: string;
|
||||||
|
refValue: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== VCD 解析器 ====================
|
||||||
|
|
||||||
|
export class VcdParser {
|
||||||
|
private signals: Map<string, VcdSignal> = new Map();
|
||||||
|
private waveforms: Map<string, TimeValue[]> = new Map();
|
||||||
|
private scopeStack: string[] = [];
|
||||||
|
private timescale: string = '1ns';
|
||||||
|
private currentTime: number = 0;
|
||||||
|
private endTime: number = 0;
|
||||||
|
private date?: string;
|
||||||
|
private version?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 VCD 文件
|
||||||
|
*/
|
||||||
|
parse(filePath: string): VcdData {
|
||||||
|
const content = fs.readFileSync(filePath, 'utf-8');
|
||||||
|
return this.parseContent(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 VCD 内容
|
||||||
|
*/
|
||||||
|
parseContent(content: string): VcdData {
|
||||||
|
// 预处理:将多行指令合并成单行
|
||||||
|
const normalizedContent = this.normalizeVcdContent(content);
|
||||||
|
const lines = normalizedContent.split('\n');
|
||||||
|
let inDefinitions = true;
|
||||||
|
|
||||||
|
for (const rawLine of lines) {
|
||||||
|
const line = rawLine.trim();
|
||||||
|
if (!line) continue;
|
||||||
|
|
||||||
|
if (inDefinitions) {
|
||||||
|
// 解析定义区
|
||||||
|
if (line.startsWith('$enddefinitions')) {
|
||||||
|
inDefinitions = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
this.parseDefinition(line);
|
||||||
|
} else {
|
||||||
|
// 解析数据区
|
||||||
|
this.parseValueChange(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
date: this.date,
|
||||||
|
version: this.version,
|
||||||
|
timescale: this.timescale,
|
||||||
|
endTime: this.endTime,
|
||||||
|
signals: this.signals,
|
||||||
|
waveforms: this.waveforms
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseDefinition(line: string): void {
|
||||||
|
if (line.startsWith('$date')) {
|
||||||
|
this.date = this.extractValue(line);
|
||||||
|
} else if (line.startsWith('$version')) {
|
||||||
|
this.version = this.extractValue(line);
|
||||||
|
} else if (line.startsWith('$timescale')) {
|
||||||
|
this.timescale = this.extractValue(line) || '1ns';
|
||||||
|
} else if (line.startsWith('$scope')) {
|
||||||
|
const match = line.match(/\$scope\s+\w+\s+(\S+)/);
|
||||||
|
if (match) {
|
||||||
|
this.scopeStack.push(match[1]);
|
||||||
|
}
|
||||||
|
} else if (line.startsWith('$upscope')) {
|
||||||
|
this.scopeStack.pop();
|
||||||
|
} else if (line.startsWith('$var')) {
|
||||||
|
this.parseVariable(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseVariable(line: string): void {
|
||||||
|
// $var wire 8 # data [7:0] $end
|
||||||
|
// $var reg 1 ! clk $end
|
||||||
|
const match = line.match(/\$var\s+(\w+)\s+(\d+)\s+(\S+)\s+(\S+)/);
|
||||||
|
if (!match) return;
|
||||||
|
|
||||||
|
const [, varType, widthStr, symbolId, name] = match;
|
||||||
|
const width = parseInt(widthStr, 10);
|
||||||
|
const module = this.scopeStack.join('.');
|
||||||
|
const fullName = module ? `${module}.${name}` : name;
|
||||||
|
|
||||||
|
const signal: VcdSignal = {
|
||||||
|
name: fullName,
|
||||||
|
shortName: name.replace(/\[\d+:\d+\]/, ''), // 移除位宽标注
|
||||||
|
symbolId,
|
||||||
|
width,
|
||||||
|
varType,
|
||||||
|
module
|
||||||
|
};
|
||||||
|
|
||||||
|
this.signals.set(symbolId, signal);
|
||||||
|
this.waveforms.set(symbolId, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseValueChange(line: string): void {
|
||||||
|
if (line.startsWith('#')) {
|
||||||
|
// 时间戳: #100
|
||||||
|
this.currentTime = parseInt(line.substring(1), 10);
|
||||||
|
this.endTime = Math.max(this.endTime, this.currentTime);
|
||||||
|
} else if (line.startsWith('b') || line.startsWith('B')) {
|
||||||
|
// 多位值: b10101010 #
|
||||||
|
const spaceIdx = line.indexOf(' ');
|
||||||
|
if (spaceIdx > 0) {
|
||||||
|
const value = line.substring(1, spaceIdx);
|
||||||
|
const symbolId = line.substring(spaceIdx + 1).trim();
|
||||||
|
this.addChange(symbolId, value);
|
||||||
|
}
|
||||||
|
} else if (line.length >= 2 && !line.startsWith('$')) {
|
||||||
|
// 单位值: 0! 或 1# 或 x$
|
||||||
|
const value = line[0];
|
||||||
|
const symbolId = line.substring(1).trim();
|
||||||
|
if (symbolId && this.signals.has(symbolId)) {
|
||||||
|
this.addChange(symbolId, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private addChange(symbolId: string, value: string): void {
|
||||||
|
const changes = this.waveforms.get(symbolId);
|
||||||
|
if (changes) {
|
||||||
|
changes.push({ time: this.currentTime, value });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extractValue(line: string): string {
|
||||||
|
// 提取 $xxx value $end 中的 value
|
||||||
|
const match = line.match(/\$\w+\s+(.+?)\s*\$end/);
|
||||||
|
return match ? match[1].trim() : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 预处理 VCD 内容,将多行指令合并成单行
|
||||||
|
*/
|
||||||
|
private normalizeVcdContent(content: string): string {
|
||||||
|
// 将多行 $xxx ... $end 合并成单行
|
||||||
|
return content.replace(/(\$\w+)\s*\n\s*([^\$]+?)\s*\n\s*(\$end)/g, '$1 $2 $3');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 波形分析工具 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二进制字符串转十六进制
|
||||||
|
*/
|
||||||
|
export function binaryToHex(binary: string): string {
|
||||||
|
if (binary === 'x' || binary === 'X' || binary.includes('x')) {
|
||||||
|
return 'xx';
|
||||||
|
}
|
||||||
|
if (binary === 'z' || binary === 'Z' || binary.includes('z')) {
|
||||||
|
return 'zz';
|
||||||
|
}
|
||||||
|
if (binary.length <= 1) {
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
|
// 补齐到 4 的倍数
|
||||||
|
const padded = binary.padStart(Math.ceil(binary.length / 4) * 4, '0');
|
||||||
|
let hex = '';
|
||||||
|
for (let i = 0; i < padded.length; i += 4) {
|
||||||
|
hex += parseInt(padded.substring(i, i + 4), 2).toString(16);
|
||||||
|
}
|
||||||
|
return hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取信号在指定时间的值
|
||||||
|
*/
|
||||||
|
export function getValueAtTime(
|
||||||
|
changes: TimeValue[],
|
||||||
|
time: number
|
||||||
|
): string {
|
||||||
|
let value = 'x';
|
||||||
|
for (const change of changes) {
|
||||||
|
if (change.time <= time) {
|
||||||
|
value = change.value;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找 DUT 和 REF 信号的第一个 mismatch
|
||||||
|
*/
|
||||||
|
export function findFirstMismatch(
|
||||||
|
vcdData: VcdData,
|
||||||
|
dutSignals: string[],
|
||||||
|
refSignals: string[]
|
||||||
|
): MismatchInfo | null {
|
||||||
|
// 收集所有时间点
|
||||||
|
const allTimes = new Set<number>();
|
||||||
|
for (const changes of vcdData.waveforms.values()) {
|
||||||
|
for (const c of changes) {
|
||||||
|
allTimes.add(c.time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const sortedTimes = Array.from(allTimes).sort((a, b) => a - b);
|
||||||
|
|
||||||
|
// 按信号名匹配 DUT 和 REF
|
||||||
|
for (const time of sortedTimes) {
|
||||||
|
for (let i = 0; i < dutSignals.length; i++) {
|
||||||
|
const dutSig = findSignalByName(vcdData, dutSignals[i]);
|
||||||
|
const refSig = findSignalByName(vcdData, refSignals[i]);
|
||||||
|
|
||||||
|
if (!dutSig || !refSig) continue;
|
||||||
|
|
||||||
|
const dutChanges = vcdData.waveforms.get(dutSig.symbolId) || [];
|
||||||
|
const refChanges = vcdData.waveforms.get(refSig.symbolId) || [];
|
||||||
|
|
||||||
|
const dutVal = getValueAtTime(dutChanges, time);
|
||||||
|
const refVal = getValueAtTime(refChanges, time);
|
||||||
|
|
||||||
|
// 跳过未知值
|
||||||
|
if (dutVal.includes('x') || refVal.includes('x')) continue;
|
||||||
|
|
||||||
|
if (dutVal !== refVal) {
|
||||||
|
return {
|
||||||
|
time,
|
||||||
|
signal: dutSig.shortName,
|
||||||
|
dutValue: binaryToHex(dutVal),
|
||||||
|
refValue: binaryToHex(refVal)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按名称查找信号
|
||||||
|
*/
|
||||||
|
function findSignalByName(vcdData: VcdData, name: string): VcdSignal | null {
|
||||||
|
for (const signal of vcdData.signals.values()) {
|
||||||
|
if (signal.name.endsWith(name) || signal.shortName === name) {
|
||||||
|
return signal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成波形表格(参照 VerilogCoder 格式)
|
||||||
|
*/
|
||||||
|
export function generateWaveformTable(
|
||||||
|
vcdData: VcdData,
|
||||||
|
signalNames: string[],
|
||||||
|
startTime: number = 0,
|
||||||
|
endTime?: number,
|
||||||
|
windowSize: number = 20
|
||||||
|
): string {
|
||||||
|
const actualEndTime = endTime ?? vcdData.endTime;
|
||||||
|
|
||||||
|
// 查找信号
|
||||||
|
const signals: VcdSignal[] = [];
|
||||||
|
for (const name of signalNames) {
|
||||||
|
const sig = findSignalByName(vcdData, name);
|
||||||
|
if (sig) signals.push(sig);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (signals.length === 0) {
|
||||||
|
return '未找到指定信号';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 收集时间点
|
||||||
|
const times = new Set<number>();
|
||||||
|
for (const sig of signals) {
|
||||||
|
const changes = vcdData.waveforms.get(sig.symbolId) || [];
|
||||||
|
for (const c of changes) {
|
||||||
|
if (c.time >= startTime && c.time <= actualEndTime) {
|
||||||
|
times.add(c.time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let sortedTimes = Array.from(times).sort((a, b) => a - b);
|
||||||
|
if (sortedTimes.length > windowSize) {
|
||||||
|
sortedTimes = sortedTimes.slice(0, windowSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成表头
|
||||||
|
const headers = ['time(ns)', ...signals.map(s => s.shortName)];
|
||||||
|
const colWidths = headers.map(h => Math.max(h.length, 8));
|
||||||
|
|
||||||
|
let table = '### Waveform Trace ###\n';
|
||||||
|
table += headers.map((h, i) => h.padEnd(colWidths[i])).join(' ') + '\n';
|
||||||
|
table += colWidths.map(w => '─'.repeat(w)).join('──') + '\n';
|
||||||
|
|
||||||
|
// 生成数据行
|
||||||
|
for (const time of sortedTimes) {
|
||||||
|
const row = [time.toString()];
|
||||||
|
for (const sig of signals) {
|
||||||
|
const changes = vcdData.waveforms.get(sig.symbolId) || [];
|
||||||
|
const val = getValueAtTime(changes, time);
|
||||||
|
row.push(binaryToHex(val));
|
||||||
|
}
|
||||||
|
table += row.map((v, i) => v.padEnd(colWidths[i])).join(' ') + '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
table += '### Waveform Trace End ###\n';
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取信号显示名称(模块.信号名[位宽])
|
||||||
|
*/
|
||||||
|
function getSignalDisplayName(sig: VcdSignal): string {
|
||||||
|
const moduleParts = sig.module.split('.');
|
||||||
|
const moduleShort = moduleParts[moduleParts.length - 1] || '';
|
||||||
|
const bitInfo = sig.width > 1 ? `[${sig.width - 1}:0]` : '';
|
||||||
|
return moduleShort ? `${moduleShort}.${sig.shortName}${bitInfo}` : `${sig.shortName}${bitInfo}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成变化日志格式(只记录信号变化)
|
||||||
|
*/
|
||||||
|
export function generateChangeLog(vcdData: VcdData): string {
|
||||||
|
// 筛选信号(排除 parameter)
|
||||||
|
const signals: VcdSignal[] = [];
|
||||||
|
for (const sig of vcdData.signals.values()) {
|
||||||
|
if (sig.varType !== 'parameter') {
|
||||||
|
signals.push(sig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 收集所有时间点
|
||||||
|
const times = new Set<number>();
|
||||||
|
for (const sig of signals) {
|
||||||
|
const changes = vcdData.waveforms.get(sig.symbolId) || [];
|
||||||
|
for (const c of changes) {
|
||||||
|
times.add(c.time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const sortedTimes = Array.from(times).sort((a, b) => a - b);
|
||||||
|
|
||||||
|
// 记录每个信号的上一个值
|
||||||
|
const lastValues = new Map<string, string | null>();
|
||||||
|
for (const sig of signals) {
|
||||||
|
lastValues.set(sig.symbolId, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
let log = '';
|
||||||
|
|
||||||
|
for (const time of sortedTimes) {
|
||||||
|
const changes: string[] = [];
|
||||||
|
|
||||||
|
for (const sig of signals) {
|
||||||
|
const waveform = vcdData.waveforms.get(sig.symbolId) || [];
|
||||||
|
const currentVal = binaryToHex(getValueAtTime(waveform, time));
|
||||||
|
const lastVal = lastValues.get(sig.symbolId);
|
||||||
|
|
||||||
|
if (lastVal === null) {
|
||||||
|
changes.push(`${getSignalDisplayName(sig)}=${currentVal}`);
|
||||||
|
} else if (currentVal !== lastVal) {
|
||||||
|
changes.push(`${getSignalDisplayName(sig)} ${lastVal}→${currentVal}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastValues.set(sig.symbolId, currentVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changes.length > 0) {
|
||||||
|
log += `#${time}: ${changes.join(', ')}\n`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return log;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分析 VCD 文件(主入口)
|
||||||
|
*/
|
||||||
|
export function analyzeVcdFile(
|
||||||
|
filePath: string,
|
||||||
|
signalFilter?: string,
|
||||||
|
checkpoint?: number
|
||||||
|
): string {
|
||||||
|
// 解析 VCD
|
||||||
|
const parser = new VcdParser();
|
||||||
|
const vcdData = parser.parse(filePath);
|
||||||
|
|
||||||
|
// 解析信号过滤器
|
||||||
|
const signalNames = signalFilter
|
||||||
|
? signalFilter.split(',').map(s => s.trim())
|
||||||
|
: Array.from(vcdData.signals.values()).map(s => s.shortName);
|
||||||
|
|
||||||
|
// 生成摘要
|
||||||
|
let result = `=== VCD 波形分析 ===\n`;
|
||||||
|
result += `文件: ${path.basename(filePath)}\n`;
|
||||||
|
result += `时间单位: ${vcdData.timescale}\n`;
|
||||||
|
result += `仿真时长: 0 - ${vcdData.endTime}${vcdData.timescale}\n\n`;
|
||||||
|
|
||||||
|
// 信号列表
|
||||||
|
result += `--- 信号列表 (${vcdData.signals.size} 个) ---\n`;
|
||||||
|
let idx = 1;
|
||||||
|
for (const sig of vcdData.signals.values()) {
|
||||||
|
if (idx <= 10) {
|
||||||
|
result += `${idx}. ${sig.shortName} (${sig.width}-bit, ${sig.varType})\n`;
|
||||||
|
}
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
if (vcdData.signals.size > 10) {
|
||||||
|
result += `... 还有 ${vcdData.signals.size - 10} 个信号\n`;
|
||||||
|
}
|
||||||
|
result += '\n';
|
||||||
|
|
||||||
|
// 变化日志
|
||||||
|
result += generateChangeLog(vcdData);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
145
src/utils/waveformTracer.ts
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
/**
|
||||||
|
* 波形追踪工具
|
||||||
|
* 调用 PyInstaller 打包的 waveform_trace 可执行文件
|
||||||
|
*/
|
||||||
|
import { spawn } from 'child_process';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 波形追踪参数
|
||||||
|
*/
|
||||||
|
export interface WaveformTraceArgs {
|
||||||
|
/** Verilog 源文件路径(相对于项目根目录) */
|
||||||
|
verilogPath: string;
|
||||||
|
/** VCD 波形文件路径(相对于项目根目录) */
|
||||||
|
vcdPath: string;
|
||||||
|
/** 仿真工具的输出字符串(包含 mismatch 信息) */
|
||||||
|
simOutput: string;
|
||||||
|
/** BFS 回溯层数,默认 2 */
|
||||||
|
traceLevel?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行波形追踪
|
||||||
|
* @param args 追踪参数
|
||||||
|
* @param context 执行上下文
|
||||||
|
* @returns 追踪结果字符串
|
||||||
|
*/
|
||||||
|
export async function executeWaveformTrace(
|
||||||
|
args: WaveformTraceArgs,
|
||||||
|
context: { extensionPath: string }
|
||||||
|
): Promise<string> {
|
||||||
|
// 获取可执行文件路径
|
||||||
|
const tracerPath = getWaveformTracerPath(context.extensionPath);
|
||||||
|
|
||||||
|
// 检查可执行文件是否存在
|
||||||
|
if (!fs.existsSync(tracerPath)) {
|
||||||
|
throw new Error(
|
||||||
|
`waveform_trace 工具未安装: ${tracerPath}\n` +
|
||||||
|
'请确保插件包含 tools/waveform_trace/bin/ 目录'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取工作区路径
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||||
|
|
||||||
|
// 解析路径(支持相对路径)
|
||||||
|
const verilogAbsPath = path.isAbsolute(args.verilogPath)
|
||||||
|
? args.verilogPath
|
||||||
|
: path.join(workspacePath, args.verilogPath);
|
||||||
|
const vcdAbsPath = path.isAbsolute(args.vcdPath)
|
||||||
|
? args.vcdPath
|
||||||
|
: path.join(workspacePath, args.vcdPath);
|
||||||
|
|
||||||
|
// 验证文件存在
|
||||||
|
if (!fs.existsSync(verilogAbsPath)) {
|
||||||
|
throw new Error(`Verilog 文件不存在: ${args.verilogPath}`);
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(vcdAbsPath)) {
|
||||||
|
throw new Error(`VCD 文件不存在: ${args.vcdPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用可执行文件
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const child = spawn(tracerPath, [
|
||||||
|
'--verilog', verilogAbsPath,
|
||||||
|
'--vcd', vcdAbsPath,
|
||||||
|
'--sim-output', args.simOutput,
|
||||||
|
'--trace-level', String(args.traceLevel || 2),
|
||||||
|
'--output-format', 'text'
|
||||||
|
], {
|
||||||
|
windowsHide: true,
|
||||||
|
cwd: workspacePath,
|
||||||
|
shell: false
|
||||||
|
});
|
||||||
|
|
||||||
|
let stdout = '';
|
||||||
|
let stderr = '';
|
||||||
|
|
||||||
|
child.stdout.on('data', (data: Buffer) => {
|
||||||
|
stdout += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
child.stderr.on('data', (data: Buffer) => {
|
||||||
|
stderr += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('close', (code: number | null) => {
|
||||||
|
if (code === 0) {
|
||||||
|
resolve(stdout);
|
||||||
|
} else {
|
||||||
|
reject(new Error(
|
||||||
|
`waveform_trace 执行失败 (code=${code}):\n${stderr || stdout}`
|
||||||
|
));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('error', (error: Error) => {
|
||||||
|
reject(new Error(`waveform_trace 启动失败: ${error.message}`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 waveform_trace 可执行文件路径
|
||||||
|
*/
|
||||||
|
function getWaveformTracerPath(extensionPath: string): string {
|
||||||
|
const platform = process.platform;
|
||||||
|
let binName = 'waveform_trace';
|
||||||
|
|
||||||
|
if (platform === 'win32') {
|
||||||
|
binName = 'waveform_trace.exe';
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.join(extensionPath, 'tools', 'waveform_trace', 'bin', binName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 waveform_trace 工具是否可用
|
||||||
|
*/
|
||||||
|
export function checkWaveformTraceAvailable(extensionPath: string): {
|
||||||
|
available: boolean;
|
||||||
|
message: string;
|
||||||
|
path?: string;
|
||||||
|
} {
|
||||||
|
const tracerPath = getWaveformTracerPath(extensionPath);
|
||||||
|
|
||||||
|
if (fs.existsSync(tracerPath)) {
|
||||||
|
return {
|
||||||
|
available: true,
|
||||||
|
message: 'waveform_trace 工具可用',
|
||||||
|
path: tracerPath
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
available: false,
|
||||||
|
message: `waveform_trace 工具未找到: ${tracerPath}`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,16 +1,23 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { getWebviewContent } from "./webviewContent";
|
import { getWebviewContent } from "./webviewContent";
|
||||||
|
import { isTokenExpired } from "../utils/jwtUtils";
|
||||||
import {
|
import {
|
||||||
handleUserMessage,
|
handleUserMessage,
|
||||||
insertCodeToEditor,
|
insertCodeToEditor,
|
||||||
handleReadFile,
|
handleReadFile,
|
||||||
handleCreateFile,
|
handleCreateFile,
|
||||||
|
handleUpdateFile,
|
||||||
|
handleRenameFile,
|
||||||
|
handleReplaceInFile,
|
||||||
|
handleUserAnswer,
|
||||||
|
abortCurrentDialog,
|
||||||
|
handleOptimizePrompt,
|
||||||
} from "../utils/messageHandler";
|
} from "../utils/messageHandler";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建并显示IC 侧边栏视图
|
* 创建并显示IC 侧边栏视图
|
||||||
*/
|
*/
|
||||||
export function showICHelperPanel(content: vscode.ExtensionContext) {
|
export function showICHelperPanel(context: vscode.ExtensionContext) {
|
||||||
// 创建WebView面板
|
// 创建WebView面板
|
||||||
const panel = vscode.window.createWebviewPanel(
|
const panel = vscode.window.createWebviewPanel(
|
||||||
"icCoder", // 面板ID
|
"icCoder", // 面板ID
|
||||||
@ -19,34 +26,87 @@ export function showICHelperPanel(content: vscode.ExtensionContext) {
|
|||||||
{
|
{
|
||||||
enableScripts: true,
|
enableScripts: true,
|
||||||
retainContextWhenHidden: true,
|
retainContextWhenHidden: true,
|
||||||
localResourceRoots: [vscode.Uri.joinPath(content.extensionUri, "media")],
|
localResourceRoots: [
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "media"),
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "src", "assets")
|
||||||
|
],
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 设置标签页图标
|
// 设置标签页图标
|
||||||
panel.iconPath = vscode.Uri.joinPath(
|
panel.iconPath = vscode.Uri.joinPath(
|
||||||
content.extensionUri,
|
context.extensionUri,
|
||||||
"media",
|
"media",
|
||||||
"图案(方底).png"
|
"icon.png"
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取页面内图标URI
|
// 获取页面内图标URI
|
||||||
const iconUri = panel.webview.asWebviewUri(
|
const iconUri = panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(content.extensionUri, "media", "图案(方底).png")
|
vscode.Uri.joinPath(context.extensionUri, "media", "icon.png")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 获取模型图标URI
|
||||||
|
const autoIconUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "Auto.png")
|
||||||
|
);
|
||||||
|
const liteIconUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "lite.png")
|
||||||
|
);
|
||||||
|
const syIconUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "Sy.png")
|
||||||
|
);
|
||||||
|
const maxIconUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "Max.png")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 获取二维码图片URI
|
||||||
|
const qrCodeUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "QRCode", "wx.png")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 获取Logo URI
|
||||||
|
const logoUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "media", "homepage-logo.png")
|
||||||
|
);
|
||||||
|
|
||||||
// 设置HTML内容
|
// 设置HTML内容
|
||||||
panel.webview.html = getWebviewContent(iconUri.toString());
|
panel.webview.html = getWebviewContent(
|
||||||
|
iconUri.toString(),
|
||||||
|
autoIconUri.toString(),
|
||||||
|
liteIconUri.toString(),
|
||||||
|
syIconUri.toString(),
|
||||||
|
maxIconUri.toString(),
|
||||||
|
qrCodeUri.toString(),
|
||||||
|
logoUri.toString()
|
||||||
|
);
|
||||||
|
|
||||||
// 处理消息
|
// 处理消息
|
||||||
panel.webview.onDidReceiveMessage(
|
panel.webview.onDidReceiveMessage(
|
||||||
(message) => {
|
(message) => {
|
||||||
|
console.log("[ICViewProvider] ====== 收到 WebView 消息 ======");
|
||||||
|
console.log("[ICViewProvider] command:", message.command);
|
||||||
|
console.log("[ICViewProvider] 完整消息:", JSON.stringify(message));
|
||||||
switch (message.command) {
|
switch (message.command) {
|
||||||
case "sendMessage":
|
case "sendMessage":
|
||||||
handleUserMessage(panel, message.text);
|
handleUserMessage(panel, message.text, context.extensionPath, message.mode);
|
||||||
break;
|
break;
|
||||||
case "readFile":
|
case "readFile":
|
||||||
handleReadFile(panel, message.filePath);
|
handleReadFile(panel, message.filePath);
|
||||||
break;
|
break;
|
||||||
|
case "updateFile":
|
||||||
|
handleUpdateFile(panel, message.filePath, message.content);
|
||||||
|
break;
|
||||||
|
case "renameFile":
|
||||||
|
handleRenameFile(panel, message.oldPath, message.newPath);
|
||||||
|
break;
|
||||||
|
case "replaceInFile":
|
||||||
|
handleReplaceInFile(
|
||||||
|
panel,
|
||||||
|
message.filePath,
|
||||||
|
message.searchText,
|
||||||
|
message.replaceText
|
||||||
|
);
|
||||||
|
break;
|
||||||
case "insertCode":
|
case "insertCode":
|
||||||
insertCodeToEditor(message.code);
|
insertCodeToEditor(message.code);
|
||||||
break;
|
break;
|
||||||
@ -61,10 +121,29 @@ export function showICHelperPanel(content: vscode.ExtensionContext) {
|
|||||||
case "showInfo":
|
case "showInfo":
|
||||||
vscode.window.showInformationMessage(message.text);
|
vscode.window.showInformationMessage(message.text);
|
||||||
break;
|
break;
|
||||||
|
case "showWarning":
|
||||||
|
vscode.window.showWarningMessage(message.message);
|
||||||
|
break;
|
||||||
|
// 新增:处理用户回答
|
||||||
|
case "submitAnswer":
|
||||||
|
handleUserAnswer(
|
||||||
|
message.askId,
|
||||||
|
message.selected,
|
||||||
|
message.customInput
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
// 新增:中止对话
|
||||||
|
case "abortDialog":
|
||||||
|
void abortCurrentDialog();
|
||||||
|
break;
|
||||||
|
// 新增:优化提示词
|
||||||
|
case "optimizePrompt":
|
||||||
|
handleOptimizePrompt(panel, message.prompt);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
undefined,
|
undefined,
|
||||||
content.subscriptions
|
context.subscriptions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,27 +151,122 @@ export function showICHelperPanel(content: vscode.ExtensionContext) {
|
|||||||
* 侧边栏视图提供者
|
* 侧边栏视图提供者
|
||||||
*/
|
*/
|
||||||
export class ICViewProvider implements vscode.WebviewViewProvider {
|
export class ICViewProvider implements vscode.WebviewViewProvider {
|
||||||
constructor(private readonly extensionUri: vscode.Uri) {}
|
private _view?: vscode.WebviewView;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly extensionUri: vscode.Uri,
|
||||||
|
private readonly context: vscode.ExtensionContext
|
||||||
|
) {
|
||||||
|
// 监听认证状态变化
|
||||||
|
this.context.subscriptions.push(
|
||||||
|
vscode.authentication.onDidChangeSessions((e) => {
|
||||||
|
if (e.provider.id === "iccoder") {
|
||||||
|
this.refreshLoginStatus();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新登录状态并更新视图
|
||||||
|
*/
|
||||||
|
private async refreshLoginStatus(): Promise<void> {
|
||||||
|
if (this._view) {
|
||||||
|
const isLoggedIn = await this.checkLoginStatus();
|
||||||
|
this._view.webview.html = this.getWebviewContent(
|
||||||
|
this._view.webview,
|
||||||
|
isLoggedIn
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查登录状态(使用 Authentication API)
|
||||||
|
*/
|
||||||
|
private async checkLoginStatus(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
|
||||||
|
console.log("[ICViewProvider] 检查登录状态, session:", session ? "存在" : "不存在");
|
||||||
|
if (!session) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 检查 token 是否过期
|
||||||
|
const expired = isTokenExpired(session.accessToken);
|
||||||
|
console.log("[ICViewProvider] token 过期检查结果:", expired);
|
||||||
|
// 只有明确过期才认为未登录,无法判断时认为已登录
|
||||||
|
if (expired === true) {
|
||||||
|
console.log("[ICViewProvider] Token 已过期");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.log("[ICViewProvider] 检查登录状态失败:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resolveWebviewView(webviewView: vscode.WebviewView) {
|
resolveWebviewView(webviewView: vscode.WebviewView) {
|
||||||
|
// 保存引用以便后续刷新
|
||||||
|
this._view = webviewView;
|
||||||
|
|
||||||
webviewView.webview.options = {
|
webviewView.webview.options = {
|
||||||
enableScripts: true,
|
enableScripts: true,
|
||||||
localResourceRoots: [vscode.Uri.joinPath(this.extensionUri, "media")],
|
localResourceRoots: [vscode.Uri.joinPath(this.extensionUri, "media")],
|
||||||
};
|
};
|
||||||
|
|
||||||
webviewView.webview.html = this.getWebviewContent(webviewView.webview);
|
// 异步检查 token 是否过期并清除
|
||||||
|
vscode.authentication.getSession("iccoder", [], { createIfNone: false })
|
||||||
|
.then((session) => {
|
||||||
|
const token = session?.accessToken;
|
||||||
|
if (token && isTokenExpired(token)) {
|
||||||
|
// 静默清除过期的 session
|
||||||
|
this.context.globalState.update('icCoderSessions', []);
|
||||||
|
this.context.globalState.update('icCoderUserInfo', undefined);
|
||||||
|
console.log('[ICViewProvider] Token 已过期,已清除所有登录状态');
|
||||||
|
}
|
||||||
|
}, () => {
|
||||||
|
// 忽略错误
|
||||||
|
});
|
||||||
|
|
||||||
|
// 检查是否已登录(使用 Authentication API)
|
||||||
|
this.checkLoginStatus().then((isLoggedIn) => {
|
||||||
|
webviewView.webview.html = this.getWebviewContent(
|
||||||
|
webviewView.webview,
|
||||||
|
isLoggedIn
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// 处理侧边栏的消息
|
// 处理侧边栏的消息
|
||||||
webviewView.webview.onDidReceiveMessage((message) => {
|
webviewView.webview.onDidReceiveMessage(
|
||||||
if (message.command === "openChat") {
|
(message) => {
|
||||||
vscode.commands.executeCommand("ic-coder.openChat");
|
if (message.command === "openChat") {
|
||||||
}
|
vscode.commands.executeCommand("ic-coder.openChat");
|
||||||
});
|
} else if (message.command === "login") {
|
||||||
|
vscode.commands.executeCommand("ic-coder.login");
|
||||||
|
} else if (message.command === "logout") {
|
||||||
|
// 退出登录(前端已有确认对话框)
|
||||||
|
vscode.commands.executeCommand('iccoder.logout');
|
||||||
|
} else if (message.command === "openICCoder") {
|
||||||
|
// 打开 IC Coder 官网
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse('https://www.iccoder.com'));
|
||||||
|
} else if (message.command === "openExternalUrl") {
|
||||||
|
// 打开外部链接
|
||||||
|
if (message.url) {
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse(message.url));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
this.context.subscriptions
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getWebviewContent(webview: vscode.Webview): string {
|
private getWebviewContent(
|
||||||
|
webview: vscode.Webview,
|
||||||
|
isLoggedIn: boolean
|
||||||
|
): string {
|
||||||
const logoUri = webview.asWebviewUri(
|
const logoUri = webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(this.extensionUri, "media", "ICCoder主页标志.png")
|
vscode.Uri.joinPath(this.extensionUri, "media", "icon.png")
|
||||||
);
|
);
|
||||||
|
|
||||||
return `
|
return `
|
||||||
@ -153,7 +327,11 @@ export class ICViewProvider implements vscode.WebviewViewProvider {
|
|||||||
<div class="container">
|
<div class="container">
|
||||||
<img src="${logoUri}" alt="IC Coder" width="120" />
|
<img src="${logoUri}" alt="IC Coder" width="120" />
|
||||||
<h2>欢迎使用 IC Coder</h2>
|
<h2>欢迎使用 IC Coder</h2>
|
||||||
<button class="btn" onclick="openChat()">开始创作</button>
|
${
|
||||||
|
isLoggedIn
|
||||||
|
? '<button class="btn" onclick="openChat()">开始创作</button>'
|
||||||
|
: '<button class="btn" onclick="login()">登录账户</button>'
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@ -163,6 +341,11 @@ export class ICViewProvider implements vscode.WebviewViewProvider {
|
|||||||
vscode.postMessage({ command: 'openChat' });
|
vscode.postMessage({ command: 'openChat' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 登录功能
|
||||||
|
function login() {
|
||||||
|
vscode.postMessage({ command: 'login' });
|
||||||
|
}
|
||||||
|
|
||||||
function generateCode(type) {
|
function generateCode(type) {
|
||||||
const code = getCodeTemplate(type);
|
const code = getCodeTemplate(type);
|
||||||
vscode.postMessage({
|
vscode.postMessage({
|
||||||
|
|||||||
204
src/views/agentCard.ts
Normal file
@ -0,0 +1,204 @@
|
|||||||
|
/**
|
||||||
|
* 智能体卡片组件
|
||||||
|
*
|
||||||
|
* 功能说明:
|
||||||
|
* - 提供智能体执行状态的可视化展示
|
||||||
|
* - 显示智能体名称、状态和执行步骤
|
||||||
|
* - 支持实时更新步骤信息
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { agentIconSvg } from "../constants/toolIcons";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取智能体卡片的样式
|
||||||
|
*/
|
||||||
|
export function getAgentCardStyles(): string {
|
||||||
|
return `
|
||||||
|
/* 智能体卡片样式 */
|
||||||
|
.segment-agent {
|
||||||
|
margin: 8px 0;
|
||||||
|
}
|
||||||
|
.agent-card {
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
border-radius: 8px;
|
||||||
|
overflow: hidden;
|
||||||
|
background: var(--vscode-editor-background);
|
||||||
|
}
|
||||||
|
.agent-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
background: var(--vscode-sideBar-background);
|
||||||
|
border-bottom: 1px solid var(--vscode-input-border);
|
||||||
|
}
|
||||||
|
.agent-icon {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
.agent-name {
|
||||||
|
font-weight: 500;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
.agent-status {
|
||||||
|
font-size: 11px;
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 10px;
|
||||||
|
}
|
||||||
|
.agent-status.running {
|
||||||
|
background: var(--vscode-inputValidation-infoBackground);
|
||||||
|
color: var(--vscode-inputValidation-infoForeground);
|
||||||
|
}
|
||||||
|
.agent-status.completed {
|
||||||
|
background: #28a745;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.agent-status.error {
|
||||||
|
background: #dc3545;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.agent-body {
|
||||||
|
padding: 8px;
|
||||||
|
}
|
||||||
|
.agent-steps-container {
|
||||||
|
max-height: 150px;
|
||||||
|
overflow-y: auto;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.agent-step {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 4px 8px;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
background: var(--vscode-list-hoverBackground);
|
||||||
|
}
|
||||||
|
.agent-step:last-child {
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
.step-icon {
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.step-name {
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
}
|
||||||
|
.step-result {
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
font-size: 11px;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
.agent-step-placeholder {
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
font-style: italic;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
/* 低调显示的工具调用样式 */
|
||||||
|
.agent-step.low-profile {
|
||||||
|
opacity: 0.85;
|
||||||
|
font-size: 12px;
|
||||||
|
padding: 4px 8px;
|
||||||
|
background: transparent;
|
||||||
|
margin-bottom: 2px;
|
||||||
|
}
|
||||||
|
.agent-step.low-profile .step-icon {
|
||||||
|
opacity: 0.8;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.agent-step.low-profile .step-name {
|
||||||
|
font-weight: 400;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
.agent-step.low-profile .step-result {
|
||||||
|
opacity: 0.85;
|
||||||
|
font-size: 11px;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取智能体卡片的脚本
|
||||||
|
*/
|
||||||
|
export function getAgentCardScript(): string {
|
||||||
|
return `
|
||||||
|
// 工具名称中文映射
|
||||||
|
function getAgentToolDisplayName(toolName) {
|
||||||
|
const toolNameMap = {
|
||||||
|
'file_read': '文件读取',
|
||||||
|
'file_write': '文件写入',
|
||||||
|
'file_delete': '文件删除',
|
||||||
|
'file_list': '检索文件',
|
||||||
|
'syntax_check': '语法检查',
|
||||||
|
'simulation': '仿真',
|
||||||
|
'waveform_summary': '波形分析',
|
||||||
|
'knowledge_save': '保存知识库',
|
||||||
|
'knowledge_load': '加载知识库',
|
||||||
|
'queryKnowledgeSummary': '查询知识摘要',
|
||||||
|
'queryRules': '查询规则',
|
||||||
|
'setModule': '设置模块',
|
||||||
|
'addSignal': '正在分析信号定义',
|
||||||
|
'addSignalExample': '正在处理信号示例',
|
||||||
|
'validateKnowledgeGraph': '验证知识图谱',
|
||||||
|
'querySignals': '查询信号',
|
||||||
|
'addPlan': '添加计划',
|
||||||
|
'addEdge': '添加边',
|
||||||
|
'showPlan': '显示计划',
|
||||||
|
'spawnExplorer': '代码探索',
|
||||||
|
'spawnDebugger': '波形调试',
|
||||||
|
'queryByBFS': 'BFS查询',
|
||||||
|
'queryStateTransitions': '查询状态转移',
|
||||||
|
'addStateTransition': '添加状态转移'
|
||||||
|
};
|
||||||
|
return toolNameMap[toolName] || toolName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 渲染智能体卡片
|
||||||
|
* @param {Object} segment - 智能体段落数据
|
||||||
|
* @param {HTMLElement} segmentDiv - 段落容器元素
|
||||||
|
*/
|
||||||
|
function renderAgentCard(segment, segmentDiv) {
|
||||||
|
segmentDiv.className += ' segment-agent';
|
||||||
|
|
||||||
|
const statusText = segment.agentStatus === 'completed' ? '完成'
|
||||||
|
: segment.agentStatus === 'error' ? '错误' : '执行中';
|
||||||
|
const statusClass = segment.agentStatus || 'running';
|
||||||
|
|
||||||
|
const stepsHtml = (segment.agentSteps || []).map(step => {
|
||||||
|
const icon = step.status === 'completed' ? '✅' : step.status === 'error' ? '❌' : '🔄';
|
||||||
|
const displayName = getAgentToolDisplayName(step.toolName);
|
||||||
|
const result = step.toolResult ? \`: \${step.toolResult.substring(0, 50)}\${step.toolResult.length > 50 ? '...' : ''}\` : '';
|
||||||
|
// 所有工具调用都使用低调样式
|
||||||
|
const stepClass = 'agent-step low-profile';
|
||||||
|
return \`<div class="\${stepClass}"><span class="step-icon">\${icon}</span><span class="step-name">\${displayName}</span><span class="step-result">\${result}</span></div>\`;
|
||||||
|
}).join('');
|
||||||
|
|
||||||
|
segmentDiv.innerHTML = \`
|
||||||
|
<div class="agent-card">
|
||||||
|
<div class="agent-header">
|
||||||
|
<span class="agent-icon">${agentIconSvg}</span>
|
||||||
|
<span class="agent-name">\${segment.agentName || '智能体'}</span>
|
||||||
|
<span class="agent-status \${statusClass}">\${statusText}</span>
|
||||||
|
</div>
|
||||||
|
<div class="agent-body">
|
||||||
|
<div class="agent-steps-container">
|
||||||
|
\${stepsHtml || '<div class="agent-step-placeholder">等待执行...</div>'}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
\`;
|
||||||
|
|
||||||
|
// 自动滚动到最新步骤
|
||||||
|
setTimeout(() => {
|
||||||
|
const container = segmentDiv.querySelector('.agent-steps-container');
|
||||||
|
if (container) {
|
||||||
|
container.scrollTop = container.scrollHeight;
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
231
src/views/agentModeSelector.ts
Normal file
@ -0,0 +1,231 @@
|
|||||||
|
/**
|
||||||
|
* 模式选择器组件
|
||||||
|
* 提供 Plan/Ask/Agent 四种模式的选择功能
|
||||||
|
*
|
||||||
|
* 模式说明:
|
||||||
|
* - Plan: 只读模式,只能查询分析,不能写文件
|
||||||
|
* - Ask: 逐个确认,每个写操作需用户确认
|
||||||
|
* - Agent: 智能体自主,自动执行大部分操作
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
plannerIconSvg,
|
||||||
|
askIconSvg,
|
||||||
|
agentIconSvg,
|
||||||
|
} from "../constants/toolIcons";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模式选择器的 HTML 内容
|
||||||
|
*/
|
||||||
|
export function getModeSelectorContent(): string {
|
||||||
|
return `
|
||||||
|
<div class="tooltip">
|
||||||
|
<div class="mode-select" id="modeSelect">
|
||||||
|
<div class="mode-trigger" onclick="toggleModeDropdown()">
|
||||||
|
<span class="mode-value" id="modeValue">Agent</span>
|
||||||
|
<svg class="mode-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="mode-dropdown" id="modeDropdown">
|
||||||
|
<div class="mode-option" data-value="plan" onclick="selectMode('plan', 'Plan')">
|
||||||
|
<div class="mode-option-header">
|
||||||
|
<span class="mode-option-icon">${plannerIconSvg}</span>
|
||||||
|
<span class="mode-option-label">Plan</span>
|
||||||
|
</div>
|
||||||
|
<span class="mode-option-desc">仅根据需求生成设计文档,之后由用户决定下一步,可以提高工程质量</span>
|
||||||
|
</div>
|
||||||
|
<div class="mode-option" data-value="ask" onclick="selectMode('ask', 'Ask')">
|
||||||
|
<div class="mode-option-header">
|
||||||
|
<span class="mode-option-icon">${askIconSvg}</span>
|
||||||
|
<span class="mode-option-label">Ask</span>
|
||||||
|
</div>
|
||||||
|
<span class="mode-option-desc">仅给与智能体读权限,用于依据项目回答用户问题,或者与用户进行探讨</span>
|
||||||
|
</div>
|
||||||
|
<div class="mode-option selected" data-value="agent" onclick="selectMode('agent', 'Agent')">
|
||||||
|
<div class="mode-option-header">
|
||||||
|
<span class="mode-option-icon">${agentIconSvg}</span>
|
||||||
|
<span class="mode-option-label">Agent</span>
|
||||||
|
</div>
|
||||||
|
<span class="mode-option-desc">用于快速生成工程、调试修改现有代码</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<span class="tooltiptext" id="modeTooltip">智能体自主模式</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模式选择器的样式
|
||||||
|
*/
|
||||||
|
export function getModeSelectorStyles(): string {
|
||||||
|
return `
|
||||||
|
/* 模式选择器样式 */
|
||||||
|
.mode-select {
|
||||||
|
position: relative;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.mode-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;
|
||||||
|
}
|
||||||
|
.mode-trigger:hover {
|
||||||
|
background: var(--vscode-list-hoverBackground);
|
||||||
|
}
|
||||||
|
.mode-value {
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
.mode-arrow {
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
transition: transform 0.2s ease;
|
||||||
|
}
|
||||||
|
.mode-select.active .mode-arrow {
|
||||||
|
transform: rotate(180deg);
|
||||||
|
}
|
||||||
|
.mode-dropdown {
|
||||||
|
position: absolute;
|
||||||
|
bottom: calc(100% + 2px);
|
||||||
|
left: 0;
|
||||||
|
min-width: 200px;
|
||||||
|
max-width: 300px;
|
||||||
|
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: hidden;
|
||||||
|
}
|
||||||
|
.mode-select.active .mode-dropdown {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
/* 模式选择器的选项样式 */
|
||||||
|
.mode-option {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
padding: 8px 12px;
|
||||||
|
font-size: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.2s ease;
|
||||||
|
}
|
||||||
|
.mode-option:hover {
|
||||||
|
background: rgba(128, 128, 128, 0.3);
|
||||||
|
}
|
||||||
|
.mode-option.selected {
|
||||||
|
background: rgba(64, 158, 255, 0.2);
|
||||||
|
}
|
||||||
|
.mode-option-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
.mode-option-icon {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.mode-option-icon svg {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.mode-option-label {
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
.mode-option-desc {
|
||||||
|
font-size: 10px;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
line-height: 1.4;
|
||||||
|
word-wrap: break-word;
|
||||||
|
white-space: normal;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模式选择器的脚本
|
||||||
|
*/
|
||||||
|
export function getModeSelectorScript(): string {
|
||||||
|
return `
|
||||||
|
// 模式选择器相关变量
|
||||||
|
let currentMode = 'agent';
|
||||||
|
|
||||||
|
// 切换模式下拉框显示/隐藏
|
||||||
|
function toggleModeDropdown() {
|
||||||
|
const modeSelect = document.getElementById('modeSelect');
|
||||||
|
const modelSelect = document.getElementById('modelSelect');
|
||||||
|
if (modeSelect) {
|
||||||
|
modeSelect.classList.toggle('active');
|
||||||
|
// 关闭模型下拉框
|
||||||
|
if (modelSelect) {
|
||||||
|
modelSelect.classList.remove('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 选择模式
|
||||||
|
function selectMode(value, label) {
|
||||||
|
currentMode = value;
|
||||||
|
const modeValue = document.getElementById('modeValue');
|
||||||
|
const modeTooltip = document.getElementById('modeTooltip');
|
||||||
|
|
||||||
|
if (modeValue) {
|
||||||
|
modeValue.textContent = label;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新 tooltip
|
||||||
|
if (modeTooltip) {
|
||||||
|
const tooltipMap = {
|
||||||
|
'plan': 'plan模式',
|
||||||
|
'ask': 'ask模式',
|
||||||
|
'agent': 'agent模式'
|
||||||
|
};
|
||||||
|
modeTooltip.textContent = tooltipMap[value] || '切换模式';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新选中状态
|
||||||
|
const options = document.querySelectorAll('.mode-option');
|
||||||
|
options.forEach(option => {
|
||||||
|
if (option.getAttribute('data-value') === value) {
|
||||||
|
option.classList.add('selected');
|
||||||
|
} else {
|
||||||
|
option.classList.remove('selected');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 关闭下拉框
|
||||||
|
const modeSelect = document.getElementById('modeSelect');
|
||||||
|
if (modeSelect) {
|
||||||
|
modeSelect.classList.remove('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前模式
|
||||||
|
function getCurrentMode() {
|
||||||
|
return currentMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 点击外部关闭模式下拉框
|
||||||
|
document.addEventListener('click', (event) => {
|
||||||
|
const modeSelect = document.getElementById('modeSelect');
|
||||||
|
|
||||||
|
if (modeSelect && !modeSelect.contains(event.target)) {
|
||||||
|
modeSelect.classList.remove('active');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`;
|
||||||
|
}
|
||||||
506
src/views/contextButton.ts
Normal file
@ -0,0 +1,506 @@
|
|||||||
|
/**
|
||||||
|
* 添加上下文按钮组件
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取添加上下文按钮的 HTML 内容
|
||||||
|
*/
|
||||||
|
export function getContextButtonContent(): string {
|
||||||
|
return `
|
||||||
|
<div class="context-selector-wrapper">
|
||||||
|
<div class="tooltip">
|
||||||
|
<button class="add-context-button" onclick="toggleContextMenu()">
|
||||||
|
<svg t="1766915545722" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4994" width="200" height="200">
|
||||||
|
<path d="M469.333333 469.333333V170.666667h85.333334v298.666666h298.666666v85.333334h-298.666666v298.666666h-85.333334v-298.666666H170.666667v-85.333334h298.666666z" fill="#8a8a8a" p-id="4995"></path>
|
||||||
|
</svg>
|
||||||
|
<span class="add-context-label">添加上下文</span>
|
||||||
|
|
||||||
|
</button>
|
||||||
|
<span class="tooltiptext">添加文件、文件夹、图片或文档作为上下文</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 上拉菜单 -->
|
||||||
|
<div class="context-menu" id="contextMenu">
|
||||||
|
<!-- 主菜单 -->
|
||||||
|
<div class="context-menu-main" id="contextMenuMain">
|
||||||
|
<div class="context-menu-item" onclick="showFileList()">
|
||||||
|
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M854.6 288.6L639.4 73.4c-6-6-14.1-9.4-22.6-9.4H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V311.3c0-8.5-3.4-16.7-9.4-22.7zM790.2 326H602V137.8L790.2 326z m1.8 562H232V136h302v216c0 23.2 18.8 42 42 42h216v494z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
<span>文件</span>
|
||||||
|
<svg class="arrow-right" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M340.864 149.312l384 384-384 384-45.248-45.248L634.368 533.312 295.616 194.56z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div class="context-menu-item" onclick="showFolderList()">
|
||||||
|
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M880 298.4H521L403.7 186.2c-1.5-1.4-3.5-2.2-5.5-2.2H144c-17.7 0-32 14.3-32 32v592c0 17.7 14.3 32 32 32h736c17.7 0 32-14.3 32-32V330.4c0-17.7-14.3-32-32-32zM840 768H184V256h188.5l119.6 114.4H840V768z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
<span>文件夹</span>
|
||||||
|
<svg class="arrow-right" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M340.864 149.312l384 384-384 384-45.248-45.248L634.368 533.312 295.616 194.56z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div class="context-menu-item" onclick="handleAddImage()">
|
||||||
|
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M928 160H96c-17.7 0-32 14.3-32 32v640c0 17.7 14.3 32 32 32h832c17.7 0 32-14.3 32-32V192c0-17.7-14.3-32-32-32z m-40 632H136V232h752v560z m-120-240c0 55.2-44.8 100-100 100s-100-44.8-100-100 44.8-100 100-100 100 44.8 100 100z m-476 0l164 164h476L696 480 536 640l-84-84-160 160z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
<span>图片</span>
|
||||||
|
</div>
|
||||||
|
<div class="context-menu-item" onclick="handleAddDocument()">
|
||||||
|
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M832 64H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V96c0-17.7-14.3-32-32-32z m-40 824H232V136h560v752z m-120-568H352c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8h320c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8z m0 144H352c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8h320c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8z m0 144H352c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8h320c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
<span>文档库</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 文件/文件夹列表视图 -->
|
||||||
|
<div class="context-menu-list" id="contextMenuList" style="display: none;">
|
||||||
|
<div class="context-menu-list-header">
|
||||||
|
<button class="context-menu-back" onclick="backToMainMenu()">
|
||||||
|
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8c-16.4 12.8-16.4 37.5 0 50.3l450.8 352.1c5.3 4.1 12.9 0.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<span id="contextMenuListTitle">选择文件</span>
|
||||||
|
</div>
|
||||||
|
<div class="context-menu-list-body" id="contextMenuListBody">
|
||||||
|
<!-- 动态加载列表 -->
|
||||||
|
</div>
|
||||||
|
<div class="context-menu-list-footer">
|
||||||
|
<input type="text" id="contextMenuSearch" placeholder="搜索..." />
|
||||||
|
<div class="context-menu-list-actions">
|
||||||
|
<span id="contextMenuListCount">已选择 0 项</span>
|
||||||
|
<button class="primary" onclick="confirmSelection()">确定</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取添加上下文按钮的样式
|
||||||
|
*/
|
||||||
|
export function getContextButtonStyles(): string {
|
||||||
|
return `
|
||||||
|
/* 上下文选择器容器 */
|
||||||
|
.context-selector-wrapper {
|
||||||
|
position: relative;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 添加上下文按钮样式 */
|
||||||
|
.add-context-button {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 6px 12px;
|
||||||
|
background: rgba(128, 128, 128, 0.2);
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
border-radius: 6px;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-context-button:hover {
|
||||||
|
background: rgba(128, 128, 128, 0.3);
|
||||||
|
border-color: var(--vscode-focusBorder);
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-context-button svg.icon {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
color: #409eff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-context-button .dropdown-arrow {
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
transition: transform 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-context-button.active .dropdown-arrow {
|
||||||
|
transform: rotate(180deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-context-label {
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 上拉菜单样式 */
|
||||||
|
.context-menu {
|
||||||
|
position: absolute;
|
||||||
|
bottom: calc(100% + 8px);
|
||||||
|
left: 0;
|
||||||
|
background: var(--vscode-dropdown-background);
|
||||||
|
border: 1px solid var(--vscode-dropdown-border);
|
||||||
|
border-radius: 6px;
|
||||||
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
||||||
|
min-width: 180px;
|
||||||
|
z-index: 1000;
|
||||||
|
display: none;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu.show {
|
||||||
|
display: block;
|
||||||
|
animation: slideUp 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes slideUp {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(10px);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 10px 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.2s ease;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-item:hover {
|
||||||
|
background: var(--vscode-list-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-item svg {
|
||||||
|
width: 18px;
|
||||||
|
height: 18px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-item span {
|
||||||
|
font-size: 13px;
|
||||||
|
white-space: nowrap;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-item .arrow-right {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
opacity: 0.6;
|
||||||
|
margin-left: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 列表视图样式 */
|
||||||
|
.context-menu-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
max-height: 350px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 10px 12px;
|
||||||
|
border-bottom: 1px solid var(--vscode-panel-border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-back {
|
||||||
|
width: 28px;
|
||||||
|
height: 28px;
|
||||||
|
padding: 0;
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
cursor: pointer;
|
||||||
|
border-radius: 4px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-back:hover {
|
||||||
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-back svg {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-header span {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-body {
|
||||||
|
flex: 1;
|
||||||
|
overflow-y: auto;
|
||||||
|
padding: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 6px 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
border-radius: 4px;
|
||||||
|
transition: background 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-item:hover {
|
||||||
|
background: var(--vscode-list-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-item.selected {
|
||||||
|
background: var(--vscode-list-activeSelectionBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-item input[type="checkbox"] {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-item label {
|
||||||
|
flex: 1;
|
||||||
|
font-size: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-footer {
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-top: 1px solid var(--vscode-panel-border);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-footer input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 6px 10px;
|
||||||
|
background: var(--vscode-input-background);
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
border-radius: 4px;
|
||||||
|
color: var(--vscode-input-foreground);
|
||||||
|
font-size: 12px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-footer span {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-footer button {
|
||||||
|
padding: 4px 12px;
|
||||||
|
background: var(--vscode-button-background);
|
||||||
|
color: var(--vscode-button-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-menu-list-footer button:hover {
|
||||||
|
background: var(--vscode-button-hoverBackground);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取添加上下文按钮的脚本
|
||||||
|
*/
|
||||||
|
export function getContextButtonScript(): string {
|
||||||
|
return `
|
||||||
|
// 上下文菜单状态
|
||||||
|
let currentListData = [];
|
||||||
|
let currentListType = '';
|
||||||
|
let selectedItems = new Set();
|
||||||
|
|
||||||
|
// 切换上下文菜单显示/隐藏
|
||||||
|
function toggleContextMenu() {
|
||||||
|
const menu = document.getElementById('contextMenu');
|
||||||
|
const button = document.querySelector('.add-context-button');
|
||||||
|
|
||||||
|
if (menu && button) {
|
||||||
|
const isShown = menu.classList.contains('show');
|
||||||
|
|
||||||
|
if (isShown) {
|
||||||
|
menu.classList.remove('show');
|
||||||
|
button.classList.remove('active');
|
||||||
|
backToMainMenu(); // 关闭时回到主菜单
|
||||||
|
} else {
|
||||||
|
menu.classList.add('show');
|
||||||
|
button.classList.add('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 点击外部关闭菜单
|
||||||
|
document.addEventListener('click', function(event) {
|
||||||
|
const wrapper = document.querySelector('.context-selector-wrapper');
|
||||||
|
const menu = document.getElementById('contextMenu');
|
||||||
|
const button = document.querySelector('.add-context-button');
|
||||||
|
|
||||||
|
if (wrapper && menu && button && !wrapper.contains(event.target)) {
|
||||||
|
menu.classList.remove('show');
|
||||||
|
button.classList.remove('active');
|
||||||
|
backToMainMenu();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 显示文件列表
|
||||||
|
function showFileList() {
|
||||||
|
vscode.postMessage({ command: 'addContextFile' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示文件夹列表
|
||||||
|
function showFolderList() {
|
||||||
|
vscode.postMessage({ command: 'addContextFolder' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回主菜单
|
||||||
|
function backToMainMenu() {
|
||||||
|
const mainMenu = document.getElementById('contextMenuMain');
|
||||||
|
const listView = document.getElementById('contextMenuList');
|
||||||
|
|
||||||
|
if (mainMenu && listView) {
|
||||||
|
mainMenu.style.display = 'block';
|
||||||
|
listView.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedItems.clear();
|
||||||
|
currentListData = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 切换到列表视图
|
||||||
|
function switchToListView(title, type, data) {
|
||||||
|
const mainMenu = document.getElementById('contextMenuMain');
|
||||||
|
const listView = document.getElementById('contextMenuList');
|
||||||
|
const titleEl = document.getElementById('contextMenuListTitle');
|
||||||
|
|
||||||
|
if (mainMenu && listView && titleEl) {
|
||||||
|
mainMenu.style.display = 'none';
|
||||||
|
listView.style.display = 'flex';
|
||||||
|
titleEl.textContent = title;
|
||||||
|
|
||||||
|
currentListType = type;
|
||||||
|
currentListData = data;
|
||||||
|
selectedItems.clear();
|
||||||
|
|
||||||
|
renderList(data);
|
||||||
|
updateSelectedCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染列表
|
||||||
|
function renderList(data) {
|
||||||
|
const body = document.getElementById('contextMenuListBody');
|
||||||
|
if (!body) return;
|
||||||
|
|
||||||
|
body.innerHTML = data.map((item, index) => \`
|
||||||
|
<div class="context-menu-list-item" onclick="toggleItemSelection(\${index})">
|
||||||
|
<input type="checkbox" id="item-\${index}" />
|
||||||
|
<label for="item-\${index}">\${item.relativePath}</label>
|
||||||
|
</div>
|
||||||
|
\`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 切换项选择
|
||||||
|
function toggleItemSelection(index) {
|
||||||
|
const checkbox = document.getElementById('item-' + index);
|
||||||
|
const item = document.querySelectorAll('.context-menu-list-item')[index];
|
||||||
|
|
||||||
|
if (checkbox && item) {
|
||||||
|
checkbox.checked = !checkbox.checked;
|
||||||
|
|
||||||
|
if (checkbox.checked) {
|
||||||
|
selectedItems.add(index);
|
||||||
|
item.classList.add('selected');
|
||||||
|
} else {
|
||||||
|
selectedItems.delete(index);
|
||||||
|
item.classList.remove('selected');
|
||||||
|
}
|
||||||
|
|
||||||
|
updateSelectedCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新选中数量
|
||||||
|
function updateSelectedCount() {
|
||||||
|
const countEl = document.getElementById('contextMenuListCount');
|
||||||
|
if (countEl) {
|
||||||
|
countEl.textContent = '已选择 ' + selectedItems.size + ' 项';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确认选择
|
||||||
|
function confirmSelection() {
|
||||||
|
const selected = Array.from(selectedItems).map(index => currentListData[index]);
|
||||||
|
|
||||||
|
if (selected.length > 0) {
|
||||||
|
selected.forEach(item => {
|
||||||
|
addContextItem(currentListType, item.path);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleContextMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加图片
|
||||||
|
function handleAddImage() {
|
||||||
|
vscode.postMessage({ command: 'addContextImage' });
|
||||||
|
toggleContextMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加文档
|
||||||
|
function handleAddDocument() {
|
||||||
|
vscode.postMessage({ command: 'addContextDocument' });
|
||||||
|
toggleContextMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 搜索功能
|
||||||
|
const searchInput = document.getElementById('contextMenuSearch');
|
||||||
|
if (searchInput) {
|
||||||
|
searchInput.addEventListener('input', function(e) {
|
||||||
|
const keyword = e.target.value.toLowerCase();
|
||||||
|
const filtered = currentListData.filter(item =>
|
||||||
|
item.relativePath.toLowerCase().includes(keyword)
|
||||||
|
);
|
||||||
|
renderList(filtered);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理后端消息
|
||||||
|
window.addEventListener('message', event => {
|
||||||
|
const message = event.data;
|
||||||
|
|
||||||
|
if (message.command === 'showWorkspaceFileList') {
|
||||||
|
switchToListView('选择文件', 'file', message.files);
|
||||||
|
} else if (message.command === 'showWorkspaceFolderList') {
|
||||||
|
switchToListView('选择文件夹', 'folder', message.folders);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`;
|
||||||
|
}
|
||||||
249
src/views/contextCompress.ts
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
/**
|
||||||
|
* 上下文压缩组件
|
||||||
|
* 提供上下文使用情况显示和压缩功能
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文压缩组件的 HTML 内容
|
||||||
|
*/
|
||||||
|
export function getContextCompressContent(): string {
|
||||||
|
return `
|
||||||
|
<!-- 上下文显示 -->
|
||||||
|
<div class="context-display">
|
||||||
|
<div class="context-info" onclick="toggleContextPanel()">
|
||||||
|
<div class="database-icon">
|
||||||
|
<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" class="db-svg">
|
||||||
|
<!-- 数据库容器主体 - 底层灰色 -->
|
||||||
|
<path d="M870.4 57.6C780.8 19.2 652.8 0 512 0 371.2 0 243.2 19.2 153.6 57.6 51.2 102.4 0 153.6 0 211.2l0 595.2c0 57.6 51.2 115.2 153.6 153.6C243.2 1004.8 371.2 1024 512 1024c140.8 0 268.8-19.2 358.4-57.6 96-38.4 153.6-96 153.6-153.6L1024 211.2C1024 153.6 972.8 102.4 870.4 57.6L870.4 57.6zM812.8 320C729.6 352 614.4 364.8 512 364.8 403.2 364.8 294.4 352 211.2 320 115.2 294.4 70.4 256 70.4 211.2c0-38.4 51.2-76.8 140.8-108.8C294.4 76.8 403.2 64 512 64c102.4 0 217.6 19.2 300.8 44.8 89.6 32 140.8 70.4 140.8 108.8C953.6 256 908.8 294.4 812.8 320L812.8 320zM819.2 505.6C736 531.2 620.8 550.4 512 550.4c-108.8 0-217.6-19.2-307.2-44.8C115.2 473.6 64 435.2 64 396.8L64 326.4C128 352 172.8 384 243.2 396.8 326.4 416 416 428.8 512 428.8c96 0 185.6-12.8 268.8-32C851.2 384 896 352 960 326.4l0 76.8C960 435.2 908.8 473.6 819.2 505.6L819.2 505.6zM819.2 710.4c-83.2 25.6-198.4 44.8-307.2 44.8-108.8 0-217.6-19.2-307.2-44.8C115.2 684.8 64 646.4 64 601.6L64 505.6c64 32 108.8 57.6 179.2 76.8C326.4 601.6 416 614.4 512 614.4c96 0 185.6-12.8 268.8-32C851.2 563.2 896 537.6 960 505.6l0 96C960 646.4 908.8 684.8 819.2 710.4L819.2 710.4zM512 960c-108.8 0-217.6-19.2-307.2-44.8C115.2 889.6 64 851.2 64 812.8l0-96c64 32 108.8 57.6 179.2 76.8 76.8 19.2 172.8 32 262.4 32 96 0 185.6-12.8 268.8-32 76.8-19.2 121.6-44.8 185.6-76.8l0 96c0 38.4-51.2 76.8-140.8 108.8C736 947.2 614.4 960 512 960L512 960z" fill="#94a3b8" class="db-body"/>
|
||||||
|
|
||||||
|
<!-- 填充进度效果 - 从下往上填充蓝色 -->
|
||||||
|
<defs>
|
||||||
|
<mask id="fill-mask">
|
||||||
|
<rect x="0" y="0" width="1024" height="1024" id="fillRect" fill="white"/>
|
||||||
|
</mask>
|
||||||
|
</defs>
|
||||||
|
|
||||||
|
<g mask="url(#fill-mask)">
|
||||||
|
<path d="M870.4 57.6C780.8 19.2 652.8 0 512 0 371.2 0 243.2 19.2 153.6 57.6 51.2 102.4 0 153.6 0 211.2l0 595.2c0 57.6 51.2 115.2 153.6 153.6C243.2 1004.8 371.2 1024 512 1024c140.8 0 268.8-19.2 358.4-57.6 96-38.4 153.6-96 153.6-153.6L1024 211.2C1024 153.6 972.8 102.4 870.4 57.6L870.4 57.6zM812.8 320C729.6 352 614.4 364.8 512 364.8 403.2 364.8 294.4 352 211.2 320 115.2 294.4 70.4 256 70.4 211.2c0-38.4 51.2-76.8 140.8-108.8C294.4 76.8 403.2 64 512 64c102.4 0 217.6 19.2 300.8 44.8 89.6 32 140.8 70.4 140.8 108.8C953.6 256 908.8 294.4 812.8 320L812.8 320zM819.2 505.6C736 531.2 620.8 550.4 512 550.4c-108.8 0-217.6-19.2-307.2-44.8C115.2 473.6 64 435.2 64 396.8L64 326.4C128 352 172.8 384 243.2 396.8 326.4 416 416 428.8 512 428.8c96 0 185.6-12.8 268.8-32C851.2 384 896 352 960 326.4l0 76.8C960 435.2 908.8 473.6 819.2 505.6L819.2 505.6zM819.2 710.4c-83.2 25.6-198.4 44.8-307.2 44.8-108.8 0-217.6-19.2-307.2-44.8C115.2 684.8 64 646.4 64 601.6L64 505.6c64 32 108.8 57.6 179.2 76.8C326.4 601.6 416 614.4 512 614.4c96 0 185.6-12.8 268.8-32C851.2 563.2 896 537.6 960 505.6l0 96C960 646.4 908.8 684.8 819.2 710.4L819.2 710.4zM512 960c-108.8 0-217.6-19.2-307.2-44.8C115.2 889.6 64 851.2 64 812.8l0-96c64 32 108.8 57.6 179.2 76.8 76.8 19.2 172.8 32 262.4 32 96 0 185.6-12.8 268.8-32 76.8-19.2 121.6-44.8 185.6-76.8l0 96c0 38.4-51.2 76.8-140.8 108.8C736 947.2 614.4 960 512 960L512 960z" fill="#409eff" class="db-fill"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<span class="context-percentage" id="contextPercentage">0%</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 上下文信息弹窗 -->
|
||||||
|
<div id="contextPanel" class="context-panel">
|
||||||
|
<div class="context-panel-content">
|
||||||
|
<div class="context-info-text" id="contextInfoText">
|
||||||
|
0k / 200k 已用上下文
|
||||||
|
</div>
|
||||||
|
<button class="compress-button" onclick="compressConversation()">
|
||||||
|
压缩会话
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文压缩组件的样式
|
||||||
|
*/
|
||||||
|
export function getContextCompressStyles(): string {
|
||||||
|
return `
|
||||||
|
/* 上下文显示样式 */
|
||||||
|
.context-display {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.context-info {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
height: 40px;
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
transition: opacity 0.3s ease;
|
||||||
|
box-shadow: none;
|
||||||
|
position: relative;
|
||||||
|
overflow: hidden;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.context-info:hover {
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
.database-icon {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.db-svg {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
.db-body {
|
||||||
|
fill: #ffffff;
|
||||||
|
}
|
||||||
|
.db-fill {
|
||||||
|
fill: #409eff;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
.context-percentage {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
/* 上下文信息弹窗样式 */
|
||||||
|
.context-panel {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 100%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
margin-bottom: 8px;
|
||||||
|
z-index: 1000;
|
||||||
|
animation: fadeInUp 0.2s ease-out;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.context-panel.active {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.context-panel::after {
|
||||||
|
content: "";
|
||||||
|
position: absolute;
|
||||||
|
bottom: -6px;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
width: 0;
|
||||||
|
height: 0;
|
||||||
|
border-left: 6px solid transparent;
|
||||||
|
border-right: 6px solid transparent;
|
||||||
|
border-top: 6px solid #ffffff;
|
||||||
|
}
|
||||||
|
.context-panel-content {
|
||||||
|
background: #ffffff;
|
||||||
|
border: 1px solid rgba(0, 0, 0, 0.1);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 12px;
|
||||||
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
min-width: 160px;
|
||||||
|
}
|
||||||
|
.context-info-text {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #374151;
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
.compress-button {
|
||||||
|
width: 100%;
|
||||||
|
background: linear-gradient(145deg, #3b82f6 0%, #1d4ed8 100%);
|
||||||
|
border: 1px solid rgba(59, 130, 246, 0.3);
|
||||||
|
border-radius: 6px;
|
||||||
|
color: white;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 500;
|
||||||
|
padding: 6px 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
.compress-button:hover {
|
||||||
|
background: linear-gradient(145deg, #2563eb 0%, #1e40af 100%);
|
||||||
|
transform: translateY(-1px);
|
||||||
|
box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
|
||||||
|
}
|
||||||
|
.compress-button:active {
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
@keyframes fadeInUp {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateX(-50%) translateY(10px);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateX(-50%) translateY(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文压缩组件的脚本
|
||||||
|
*/
|
||||||
|
export function getContextCompressScript(): string {
|
||||||
|
return `
|
||||||
|
// 上下文面板相关函数
|
||||||
|
function toggleContextPanel() {
|
||||||
|
const contextPanel = document.getElementById('contextPanel');
|
||||||
|
if (contextPanel) {
|
||||||
|
if (contextPanel.classList.contains('active')) {
|
||||||
|
contextPanel.classList.remove('active');
|
||||||
|
} else {
|
||||||
|
contextPanel.classList.add('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function compressConversation() {
|
||||||
|
// 发送压缩会话请求
|
||||||
|
vscode.postMessage({ command: 'compressConversation' });
|
||||||
|
addMessage('正在压缩会话...', 'bot');
|
||||||
|
|
||||||
|
// 关闭面板
|
||||||
|
const contextPanel = document.getElementById('contextPanel');
|
||||||
|
if (contextPanel) {
|
||||||
|
contextPanel.classList.remove('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateContextDisplay(currentTokens, maxTokens) {
|
||||||
|
const percentage = Math.min(Math.round((currentTokens / maxTokens) * 100), 100);
|
||||||
|
|
||||||
|
// 更新百分比显示
|
||||||
|
const contextPercentage = document.getElementById('contextPercentage');
|
||||||
|
if (contextPercentage) {
|
||||||
|
contextPercentage.textContent = percentage + '%';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新详细信息
|
||||||
|
const contextInfoText = document.getElementById('contextInfoText');
|
||||||
|
if (contextInfoText) {
|
||||||
|
const currentK = Math.round((currentTokens / 1000) * 10) / 10;
|
||||||
|
const maxK = Math.round(maxTokens / 1000);
|
||||||
|
contextInfoText.textContent = \`\${currentK}k / \${maxK}k 已用上下文\`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新SVG填充效果(从下往上填充)
|
||||||
|
const fillRect = document.getElementById('fillRect');
|
||||||
|
if (fillRect) {
|
||||||
|
const fillHeight = (1024 * percentage) / 100;
|
||||||
|
const fillY = 1024 - fillHeight;
|
||||||
|
fillRect.setAttribute('y', fillY.toString());
|
||||||
|
fillRect.setAttribute('height', fillHeight.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 点击外部关闭上下文面板
|
||||||
|
document.addEventListener('click', (event) => {
|
||||||
|
const contextDisplay = document.querySelector('.context-display');
|
||||||
|
const contextPanel = document.getElementById('contextPanel');
|
||||||
|
|
||||||
|
if (contextPanel && contextPanel.classList.contains('active') && contextDisplay) {
|
||||||
|
if (!contextDisplay.contains(event.target)) {
|
||||||
|
contextPanel.classList.remove('active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
`;
|
||||||
|
}
|
||||||
225
src/views/contextDisplay.ts
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
/**
|
||||||
|
* 上下文显示组件
|
||||||
|
* 用于显示已选择的文件、文件夹、图片和文档
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文显示区域的 HTML 内容
|
||||||
|
*/
|
||||||
|
export function getContextDisplayContent(): string {
|
||||||
|
return `
|
||||||
|
<div class="context-display-area" id="contextDisplayArea" style="display: none;">
|
||||||
|
<div class="context-items-container" id="contextItemsContainer">
|
||||||
|
<!-- 动态添加的上下文项将显示在这里 -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文显示区域的样式
|
||||||
|
*/
|
||||||
|
export function getContextDisplayStyles(): string {
|
||||||
|
return `
|
||||||
|
/* 上下文显示区域 */
|
||||||
|
.context-display-area {
|
||||||
|
margin-bottom: 8px;
|
||||||
|
padding: 8px;
|
||||||
|
background: rgba(128, 128, 128, 0.1);
|
||||||
|
border-radius: 6px;
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-items-container {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 上下文项样式 */
|
||||||
|
.context-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 6px 10px;
|
||||||
|
background: var(--vscode-input-background);
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
max-width: 300px;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-item:hover {
|
||||||
|
background: var(--vscode-list-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-item svg {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-item-name {
|
||||||
|
flex: 1;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-item-remove {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
cursor: pointer;
|
||||||
|
opacity: 0.6;
|
||||||
|
transition: opacity 0.2s ease;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-item-remove:hover {
|
||||||
|
opacity: 1;
|
||||||
|
color: #f56c6c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 图片预览样式 */
|
||||||
|
.context-item.image-item {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.context-item-preview {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
object-fit: cover;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文显示区域的脚本
|
||||||
|
*/
|
||||||
|
export function getContextDisplayScript(): string {
|
||||||
|
return `
|
||||||
|
// 存储上下文项
|
||||||
|
let contextItems = [];
|
||||||
|
|
||||||
|
// 获取文件图标 SVG
|
||||||
|
function getFileIcon() {
|
||||||
|
return '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M854.6 288.6L639.4 73.4c-6-6-14.1-9.4-22.6-9.4H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V311.3c0-8.5-3.4-16.7-9.4-22.7z" fill="currentColor"/></svg>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件夹图标 SVG
|
||||||
|
function getFolderIcon() {
|
||||||
|
return '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M880 298.4H521L403.7 186.2c-1.5-1.4-3.5-2.2-5.5-2.2H144c-17.7 0-32 14.3-32 32v592c0 17.7 14.3 32 32 32h736c17.7 0 32-14.3 32-32V330.4c0-17.7-14.3-32-32-32z" fill="currentColor"/></svg>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取图片图标 SVG
|
||||||
|
function getImageIcon() {
|
||||||
|
return '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M928 160H96c-17.7 0-32 14.3-32 32v640c0 17.7 14.3 32 32 32h832c17.7 0 32-14.3 32-32V192c0-17.7-14.3-32-32-32z" fill="currentColor"/></svg>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文档图标 SVG
|
||||||
|
function getDocumentIcon() {
|
||||||
|
return '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M832 64H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V96c0-17.7-14.3-32-32-32z" fill="currentColor"/></svg>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取删除图标 SVG
|
||||||
|
function getRemoveIcon() {
|
||||||
|
return '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9c-4.4 5.2-.7 13.1 6.1 13.1h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z" fill="currentColor"/></svg>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取文件名
|
||||||
|
function getFileName(path) {
|
||||||
|
return path.split(/[\\\\/]/).pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加上下文项
|
||||||
|
function addContextItem(type, path) {
|
||||||
|
const id = Date.now() + Math.random();
|
||||||
|
contextItems.push({ id, type, path });
|
||||||
|
renderContextItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除上下文项
|
||||||
|
function removeContextItem(id) {
|
||||||
|
contextItems = contextItems.filter(item => item.id !== id);
|
||||||
|
renderContextItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染上下文项
|
||||||
|
function renderContextItems() {
|
||||||
|
const container = document.getElementById('contextItemsContainer');
|
||||||
|
const displayArea = document.getElementById('contextDisplayArea');
|
||||||
|
|
||||||
|
if (!container || !displayArea) return;
|
||||||
|
|
||||||
|
if (contextItems.length === 0) {
|
||||||
|
displayArea.style.display = 'none';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
displayArea.style.display = 'block';
|
||||||
|
container.innerHTML = contextItems.map(item => {
|
||||||
|
let icon = '';
|
||||||
|
switch(item.type) {
|
||||||
|
case 'file': icon = getFileIcon(); break;
|
||||||
|
case 'folder': icon = getFolderIcon(); break;
|
||||||
|
case 'image': icon = getImageIcon(); break;
|
||||||
|
case 'document': icon = getDocumentIcon(); break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return \`
|
||||||
|
<div class="context-item" title="\${item.path}">
|
||||||
|
\${icon}
|
||||||
|
<span class="context-item-name">\${getFileName(item.path)}</span>
|
||||||
|
<span class="context-item-remove" onclick="removeContextItem(\${item.id})">
|
||||||
|
\${getRemoveIcon()}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
\`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理后端返回的文件选择结果
|
||||||
|
window.addEventListener('message', event => {
|
||||||
|
const message = event.data;
|
||||||
|
|
||||||
|
switch(message.command) {
|
||||||
|
case 'contextFilesSelected':
|
||||||
|
if (message.files && message.files.length > 0) {
|
||||||
|
message.files.forEach(file => addContextItem('file', file));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'contextFoldersSelected':
|
||||||
|
if (message.folders && message.folders.length > 0) {
|
||||||
|
message.folders.forEach(folder => addContextItem('folder', folder));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'contextImagesSelected':
|
||||||
|
if (message.images && message.images.length > 0) {
|
||||||
|
message.images.forEach(image => addContextItem('image', image));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'contextDocumentsSelected':
|
||||||
|
if (message.documents && message.documents.length > 0) {
|
||||||
|
message.documents.forEach(doc => addContextItem('document', doc));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 获取所有上下文项(供发送消息时使用)
|
||||||
|
window.getContextItems = function() {
|
||||||
|
return contextItems;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 清空上下文项(供清空对话时使用)
|
||||||
|
window.clearContextItems = function() {
|
||||||
|
contextItems = [];
|
||||||
|
renderContextItems();
|
||||||
|
};
|
||||||
|
`;
|
||||||
|
}
|
||||||
@ -1,3 +1,24 @@
|
|||||||
|
import {
|
||||||
|
getUserInfoComponentContent,
|
||||||
|
getUserInfoComponentStyles,
|
||||||
|
getUserInfoComponentScript,
|
||||||
|
} from "./userInfoComponent";
|
||||||
|
import {
|
||||||
|
getMoreOptionsComponentContent,
|
||||||
|
getMoreOptionsComponentStyles,
|
||||||
|
getMoreOptionsComponentScript,
|
||||||
|
} from "./moreOptionsComponent";
|
||||||
|
import {
|
||||||
|
getSettingsComponentContent,
|
||||||
|
getSettingsComponentStyles,
|
||||||
|
getSettingsComponentScript,
|
||||||
|
} from "./settingsComponent";
|
||||||
|
import {
|
||||||
|
userAvatarIconSvg,
|
||||||
|
moreIconSvg,
|
||||||
|
setting,
|
||||||
|
} from "../constants/toolIcons";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取会话历史栏的 HTML 内容
|
* 获取会话历史栏的 HTML 内容
|
||||||
*/
|
*/
|
||||||
@ -6,7 +27,7 @@ export function getConversationHistoryBarContent(): string {
|
|||||||
<div class="conversation-history-bar">
|
<div class="conversation-history-bar">
|
||||||
<div class="history-dropdown-container">
|
<div class="history-dropdown-container">
|
||||||
<button class="history-dropdown-button" onclick="toggleHistoryDropdown()">
|
<button class="history-dropdown-button" onclick="toggleHistoryDropdown()">
|
||||||
<span class="dropdown-label">Past Conversations</span>
|
<span class="dropdown-label">历史对话</span>
|
||||||
<svg class="dropdown-icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
<svg class="dropdown-icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
|
||||||
<path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3 0.1-12.7-6.4-12.7z" fill="currentColor"/>
|
<path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3 0.1-12.7-6.4-12.7z" fill="currentColor"/>
|
||||||
</svg>
|
</svg>
|
||||||
@ -19,12 +40,36 @@ export function getConversationHistoryBarContent(): string {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button class="new-conversation-button" onclick="createNewConversation()" title="新建对话">
|
<div class="right-actions">
|
||||||
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
<button class="new-conversation-button" onclick="createNewConversation()" title="新建对话">
|
||||||
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" fill="currentColor"/>
|
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
||||||
</svg>
|
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" fill="currentColor"/>
|
||||||
</button>
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div class="user-info-container">
|
||||||
|
<button class="user-avatar-icon-button" id="userAvatarIconButton" style="display: none;" title="查看用户信息" onclick="openUserDetailModal()">
|
||||||
|
${userAvatarIconSvg}
|
||||||
|
</button>
|
||||||
|
${getUserInfoComponentContent()}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class='setting'>
|
||||||
|
<button class="setting-btn" title="设置" onclick="openSettingsModal()">
|
||||||
|
${setting}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class='more-container'>
|
||||||
|
<button class="more-button" title="更多选项" onclick="toggleMoreOptionsDropdown()">
|
||||||
|
${moreIconSvg}
|
||||||
|
</button>
|
||||||
|
${getMoreOptionsComponentContent()}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
${getSettingsComponentContent()}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,13 +94,125 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.right-actions {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-info-container {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-avatar-icon-button {
|
||||||
|
width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
padding: 0;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 50%;
|
||||||
|
cursor: pointer;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-avatar-icon-button:hover {
|
||||||
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
transform: scale(1.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-avatar-icon-button:active {
|
||||||
|
transform: scale(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-avatar-icon-button.active {
|
||||||
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-avatar-icon-button svg {
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
${getUserInfoComponentStyles()}
|
||||||
|
|
||||||
|
${getSettingsComponentStyles()}
|
||||||
|
|
||||||
|
.setting {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-btn {
|
||||||
|
width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
padding: 0;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 50%;
|
||||||
|
cursor: pointer;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-btn:hover {
|
||||||
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
transform: scale(1.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.setting-btn:active {
|
||||||
|
transform: scale(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
.more-container {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.more-button {
|
||||||
|
width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
padding: 0;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 50%;
|
||||||
|
cursor: pointer;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.more-button:hover {
|
||||||
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
transform: scale(1.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.more-button:active {
|
||||||
|
transform: scale(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
.more-button.active {
|
||||||
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
${getMoreOptionsComponentStyles()}
|
||||||
|
|
||||||
.history-dropdown-button {
|
.history-dropdown-button {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 6px;
|
gap: 6px;
|
||||||
padding: 8px 12px;
|
padding: 8px 12px;
|
||||||
background: transparent;
|
background: transparent;
|
||||||
color: var(--vscode-input-foreground);
|
color: var(--vscode-foreground);
|
||||||
border: none;
|
border: none;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
@ -64,7 +221,7 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.history-dropdown-button:hover {
|
.history-dropdown-button:hover {
|
||||||
opacity: 0.8;
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
}
|
}
|
||||||
|
|
||||||
.dropdown-label {
|
.dropdown-label {
|
||||||
@ -111,6 +268,10 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background 0.2s ease;
|
transition: background 0.2s ease;
|
||||||
border-bottom: 1px solid var(--vscode-panel-border);
|
border-bottom: 1px solid var(--vscode-panel-border);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.history-item:last-child {
|
.history-item:last-child {
|
||||||
@ -124,15 +285,17 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
.history-item-title {
|
.history-item-title {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
margin-bottom: 4px;
|
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.history-item-time {
|
.history-item-time {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
opacity: 0.7;
|
opacity: 0.7;
|
||||||
|
white-space: nowrap;
|
||||||
|
flex-shrink: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.history-empty {
|
.history-empty {
|
||||||
@ -142,14 +305,22 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.history-load-more {
|
||||||
|
padding: 12px 16px;
|
||||||
|
text-align: center;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
font-size: 12px;
|
||||||
|
border-top: 1px solid var(--vscode-panel-border);
|
||||||
|
}
|
||||||
|
|
||||||
.new-conversation-button {
|
.new-conversation-button {
|
||||||
width: 36px;
|
width: 30px;
|
||||||
height: 36px;
|
height: 30px;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
background: transparent;
|
background: transparent;
|
||||||
color: var(--vscode-foreground);
|
color: var(--vscode-foreground);
|
||||||
border: none;
|
border: none;
|
||||||
border-radius: 4px;
|
border-radius: 50%;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@ -159,11 +330,12 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.new-conversation-button:hover {
|
.new-conversation-button:hover {
|
||||||
opacity: 0.7;
|
background: var(--vscode-toolbar-hoverBackground);
|
||||||
|
transform: scale(1.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
.new-conversation-button:active {
|
.new-conversation-button:active {
|
||||||
opacity: 0.5;
|
transform: scale(0.95);
|
||||||
}
|
}
|
||||||
|
|
||||||
.new-conversation-button svg {
|
.new-conversation-button svg {
|
||||||
@ -196,9 +368,43 @@ export function getConversationHistoryBarStyles(): string {
|
|||||||
*/
|
*/
|
||||||
export function getConversationHistoryBarScript(): string {
|
export function getConversationHistoryBarScript(): string {
|
||||||
return `
|
return `
|
||||||
|
${getUserInfoComponentScript()}
|
||||||
|
|
||||||
|
${getMoreOptionsComponentScript()}
|
||||||
|
|
||||||
|
${getSettingsComponentScript()}
|
||||||
|
|
||||||
|
// 更新用户头像图标按钮显示
|
||||||
|
function updateUserAvatarIconButton(userInfo) {
|
||||||
|
const userAvatarIconButton = document.getElementById('userAvatarIconButton');
|
||||||
|
|
||||||
|
if (userInfo && userInfo.nickname) {
|
||||||
|
// 显示用户头像图标按钮
|
||||||
|
if (userAvatarIconButton) {
|
||||||
|
userAvatarIconButton.style.display = 'flex';
|
||||||
|
}
|
||||||
|
// 同时更新用户详情弹窗的数据
|
||||||
|
if (typeof updateUserInfoDisplay === 'function') {
|
||||||
|
updateUserInfoDisplay(userInfo);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 隐藏用户头像图标按钮
|
||||||
|
if (userAvatarIconButton) {
|
||||||
|
userAvatarIconButton.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 会话历史相关变量
|
// 会话历史相关变量
|
||||||
let conversationHistory = [];
|
let conversationHistory = [];
|
||||||
let currentConversationId = null;
|
let currentConversationId = null;
|
||||||
|
let currentOffset = 0;
|
||||||
|
let totalHistory = 0;
|
||||||
|
let hasMoreHistory = false;
|
||||||
|
let isLoadingHistory = false;
|
||||||
|
let currentLoadRequestId = 0; // 请求 ID,用于防止并发加载
|
||||||
|
const HISTORY_PAGE_SIZE = 10;
|
||||||
|
const MAX_HISTORY_ITEMS = 100;
|
||||||
|
|
||||||
// 切换历史记录下拉菜单
|
// 切换历史记录下拉菜单
|
||||||
function toggleHistoryDropdown() {
|
function toggleHistoryDropdown() {
|
||||||
@ -211,33 +417,102 @@ export function getConversationHistoryBarScript(): string {
|
|||||||
} else {
|
} else {
|
||||||
menu.classList.add('active');
|
menu.classList.add('active');
|
||||||
button.classList.add('active');
|
button.classList.add('active');
|
||||||
// 加载会话历史
|
// 重置并加载会话历史
|
||||||
loadConversationHistory();
|
resetAndLoadHistory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载会话历史
|
// 重置并加载会话历史
|
||||||
function loadConversationHistory() {
|
function resetAndLoadHistory() {
|
||||||
vscode.postMessage({ command: 'loadConversationHistory' });
|
conversationHistory = [];
|
||||||
|
currentOffset = 0;
|
||||||
|
totalHistory = 0;
|
||||||
|
hasMoreHistory = false;
|
||||||
|
const historyList = document.getElementById('historyList');
|
||||||
|
if (historyList) {
|
||||||
|
historyList.innerHTML = '<div class="history-empty">加载中...</div>';
|
||||||
|
}
|
||||||
|
loadMoreHistory();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染会话历史列表
|
// 加载更多会话历史
|
||||||
function renderConversationHistory(history) {
|
function loadMoreHistory() {
|
||||||
conversationHistory = history;
|
if (isLoadingHistory || (currentOffset > 0 && !hasMoreHistory)) {
|
||||||
const historyList = document.getElementById('historyList');
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!history || history.length === 0) {
|
// 检查是否已达到最大数量
|
||||||
|
if (currentOffset >= MAX_HISTORY_ITEMS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成新的请求 ID,用于防止并发加载
|
||||||
|
const requestId = ++currentLoadRequestId;
|
||||||
|
|
||||||
|
isLoadingHistory = true;
|
||||||
|
vscode.postMessage({
|
||||||
|
command: 'loadConversationHistory',
|
||||||
|
offset: currentOffset,
|
||||||
|
limit: HISTORY_PAGE_SIZE,
|
||||||
|
requestId: requestId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染会话历史列表(支持追加)
|
||||||
|
function renderConversationHistory(data) {
|
||||||
|
isLoadingHistory = false;
|
||||||
|
|
||||||
|
if (!data || !data.items) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 追加新数据(去重)
|
||||||
|
const existingIds = new Set(conversationHistory.map(item => item.id));
|
||||||
|
const newItems = [];
|
||||||
|
for (const item of data.items) {
|
||||||
|
if (!existingIds.has(item.id)) {
|
||||||
|
existingIds.add(item.id);
|
||||||
|
newItems.push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
conversationHistory = conversationHistory.concat(newItems);
|
||||||
|
totalHistory = data.total;
|
||||||
|
hasMoreHistory = data.hasMore;
|
||||||
|
currentOffset = conversationHistory.length;
|
||||||
|
|
||||||
|
const historyList = document.getElementById('historyList');
|
||||||
|
if (!historyList) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有任何历史记录
|
||||||
|
if (conversationHistory.length === 0) {
|
||||||
historyList.innerHTML = '<div class="history-empty">暂无会话历史</div>';
|
historyList.innerHTML = '<div class="history-empty">暂无会话历史</div>';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
historyList.innerHTML = history.map(item => \`
|
// 渲染所有历史记录
|
||||||
<div class="history-item"
|
historyList.innerHTML = conversationHistory.map(item => \`
|
||||||
onclick="selectConversation('\${item.id}')">
|
<div class="history-item" onclick="selectConversation('\${item.id}')">
|
||||||
<div class="history-item-title">\${item.title || '未命名会话'}</div>
|
<div class="history-item-title">\${item.title || '未命名会话'}</div>
|
||||||
<div class="history-item-time">\${formatTime(item.timestamp)}</div>
|
<div class="history-item-time">\${formatTime(item.timestamp)}</div>
|
||||||
</div>
|
</div>
|
||||||
\`).join('');
|
\`).join('');
|
||||||
|
|
||||||
|
// 如果还有更多数据,添加"加载更多"提示
|
||||||
|
if (hasMoreHistory && currentOffset < MAX_HISTORY_ITEMS) {
|
||||||
|
historyList.innerHTML += \`
|
||||||
|
<div class="history-load-more" id="loadMoreIndicator">
|
||||||
|
<span>滚动加载更多...</span>
|
||||||
|
</div>
|
||||||
|
\`;
|
||||||
|
} else if (currentOffset >= MAX_HISTORY_ITEMS && hasMoreHistory) {
|
||||||
|
historyList.innerHTML += \`
|
||||||
|
<div class="history-load-more">
|
||||||
|
<span>已显示最近 \${MAX_HISTORY_ITEMS} 条记录</span>
|
||||||
|
</div>
|
||||||
|
\`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 选择会话
|
// 选择会话
|
||||||
@ -291,6 +566,23 @@ export function getConversationHistoryBarScript(): string {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 监听下拉菜单滚动事件(防止重复注册)
|
||||||
|
const historyDropdownMenu = document.getElementById('historyDropdownMenu');
|
||||||
|
if (historyDropdownMenu && !historyDropdownMenu._scrollListenerAdded) {
|
||||||
|
historyDropdownMenu._scrollListenerAdded = true;
|
||||||
|
historyDropdownMenu.addEventListener('scroll', () => {
|
||||||
|
const menu = historyDropdownMenu;
|
||||||
|
const scrollTop = menu.scrollTop;
|
||||||
|
const scrollHeight = menu.scrollHeight;
|
||||||
|
const clientHeight = menu.clientHeight;
|
||||||
|
|
||||||
|
// 当滚动到距离底部 50px 时,加载更多
|
||||||
|
if (scrollHeight - scrollTop - clientHeight < 50) {
|
||||||
|
loadMoreHistory();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 点击外部关闭下拉菜单
|
// 点击外部关闭下拉菜单
|
||||||
document.addEventListener('click', (event) => {
|
document.addEventListener('click', (event) => {
|
||||||
const container = document.querySelector('.history-dropdown-container');
|
const container = document.querySelector('.history-dropdown-container');
|
||||||
|
|||||||
284
src/views/exampleShowcase.ts
Normal file
@ -0,0 +1,284 @@
|
|||||||
|
/**
|
||||||
|
* 获取展示区域的 HTML 内容
|
||||||
|
*/
|
||||||
|
export function getExampleShowcaseContent(): string {
|
||||||
|
return `
|
||||||
|
<div class="example-showcase" id="exampleShowcase">
|
||||||
|
<div class="showcase-title">示例</div>
|
||||||
|
<div class="example-cards">
|
||||||
|
<div class="example-card" onclick="sendExample(0)">
|
||||||
|
<div class="example-icon">
|
||||||
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M14 2H6C5.46957 2 4.96086 2.21071 4.58579 2.58579C4.21071 2.96086 4 3.46957 4 4V20C4 20.5304 4.21071 21.0391 4.58579 21.4142C4.96086 21.7893 5.46957 22 6 22H18C18.5304 22 19.0391 21.7893 19.4142 21.4142C19.7893 21.0391 20 20.5304 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
<path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
<path d="M16 13H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
<path d="M16 17H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
<path d="M10 9H9H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div class="example-content">
|
||||||
|
<div class="example-title">生成一个SPI控制器</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="example-card" onclick="sendExample(1)">
|
||||||
|
<div class="example-icon">
|
||||||
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
<path d="M2 17L12 22L22 17" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
<path d="M2 12L12 17L22 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div class="example-content">
|
||||||
|
<div class="example-title">生成一个GMII接口的以太网UDP通信模块</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="web-link">
|
||||||
|
<a href="https://iccoder.com" target="_blank" class="web-link-button">
|
||||||
|
<span class="link-icon">🌐</span>
|
||||||
|
<span>IC Coder Web端</span>
|
||||||
|
<span class="link-arrow">→</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取展示区域的样式
|
||||||
|
*/
|
||||||
|
export function getExampleShowcaseStyles(): string {
|
||||||
|
return `
|
||||||
|
.example-showcase {
|
||||||
|
margin-top: 24px;
|
||||||
|
padding: 0;
|
||||||
|
opacity: 1;
|
||||||
|
transition: opacity 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-showcase.hidden {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.showcase-title {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
margin-bottom: 12px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-cards {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-card {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
background: var(--vscode-input-background);
|
||||||
|
border: 1px solid var(--vscode-input-border);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
position: relative;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-card::before {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
background: linear-gradient(135deg, rgba(79, 172, 254, 0.1) 0%, rgba(0, 242, 254, 0.1) 50%, rgba(168, 85, 247, 0.1) 100%);
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-card:hover::before {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-card:hover {
|
||||||
|
border-color: var(--vscode-focusBorder);
|
||||||
|
transform: translateY(-3px);
|
||||||
|
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-icon {
|
||||||
|
flex-shrink: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-icon svg {
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-content {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
position: relative;
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-title {
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
line-height: 1.4;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-card:hover .example-title {
|
||||||
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 50%, #a855f7 100%);
|
||||||
|
-webkit-background-clip: text;
|
||||||
|
-webkit-text-fill-color: transparent;
|
||||||
|
background-clip: text;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example-desc {
|
||||||
|
font-size: 11px;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
line-height: 1.4;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
display: -webkit-box;
|
||||||
|
-webkit-line-clamp: 2;
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
.web-link {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
padding-top: 20px;
|
||||||
|
border-top: 1px solid var(--vscode-panel-border);
|
||||||
|
margin-top: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.web-link-button {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 10px 20px;
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
text-decoration: none;
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 50%, #a855f7 100%);
|
||||||
|
-webkit-background-clip: text;
|
||||||
|
-webkit-text-fill-color: transparent;
|
||||||
|
background-clip: text;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.web-link-button:focus {
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.web-link-button:hover {
|
||||||
|
transform: translateY(-1px);
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.link-icon {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.link-arrow {
|
||||||
|
font-size: 16px;
|
||||||
|
transition: transform 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.web-link-button:hover .link-arrow {
|
||||||
|
transform: translateX(3px);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取展示区域的脚本
|
||||||
|
*/
|
||||||
|
export function getExampleShowcaseScript(): string {
|
||||||
|
return `
|
||||||
|
// 示例文本数组
|
||||||
|
const exampleTexts = [
|
||||||
|
'生成一个SPI控制器',
|
||||||
|
'生成一个GMII接口的以太网UDP通信模块'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 存储待发送的示例索引
|
||||||
|
let pendingExampleIndex = -1;
|
||||||
|
|
||||||
|
// 直接发送示例消息
|
||||||
|
function sendExample(index) {
|
||||||
|
// 先检查邀请码验证状态
|
||||||
|
pendingExampleIndex = index;
|
||||||
|
vscode.postMessage({
|
||||||
|
command: 'checkInvitationCode'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实际发送示例消息
|
||||||
|
function doSendExample(index) {
|
||||||
|
const messageInput = document.getElementById('messageInput');
|
||||||
|
const sendButton = document.getElementById('sendButton');
|
||||||
|
|
||||||
|
if (messageInput && exampleTexts[index]) {
|
||||||
|
messageInput.value = exampleTexts[index];
|
||||||
|
|
||||||
|
// 触发自动调整高度
|
||||||
|
if (typeof autoResizeTextarea === 'function') {
|
||||||
|
autoResizeTextarea();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 直接触发发送
|
||||||
|
if (sendButton && typeof sendButton.click === 'function') {
|
||||||
|
sendButton.click();
|
||||||
|
} else if (typeof sendMessage === 'function') {
|
||||||
|
sendMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监听消息变化,自动隐藏/显示展示区域
|
||||||
|
function updateShowcaseVisibility() {
|
||||||
|
const showcase = document.getElementById('exampleShowcase');
|
||||||
|
if (showcase) {
|
||||||
|
if (hasMessages) {
|
||||||
|
showcase.classList.add('hidden');
|
||||||
|
} else {
|
||||||
|
showcase.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 扩展原有的布局更新函数
|
||||||
|
const originalUpdateInputAreaLayout = updateInputAreaLayout;
|
||||||
|
updateInputAreaLayout = function() {
|
||||||
|
if (originalUpdateInputAreaLayout) {
|
||||||
|
originalUpdateInputAreaLayout();
|
||||||
|
}
|
||||||
|
updateShowcaseVisibility();
|
||||||
|
};
|
||||||
|
`;
|
||||||
|
}
|
||||||