Compare commits
191 Commits
feat/plugi
...
feat/ningD
| Author | SHA1 | Date | |
|---|---|---|---|
| 47f95afabb | |||
| 7fe87e515b | |||
| 081ddec55c | |||
| 12c2f634bd | |||
| 790110ba7e | |||
| b9dc631bf7 | |||
| 6425496d2e | |||
| fd5a01c67f | |||
| 29e80ce296 | |||
| c244a308d7 | |||
| a25d68f527 | |||
| 77b54aebf0 | |||
| 840436eb36 | |||
| f5dd7534f0 | |||
| ebb9de5294 | |||
| 531d140b99 | |||
| 97b8e8aa7d | |||
| 4ed998e937 | |||
| ad0f0336d5 | |||
| 7cde4fa138 | |||
| 1b7259d1c1 | |||
| 09ff812562 | |||
| e7c631d532 | |||
| 06573e37d7 | |||
| d740f4da44 | |||
| f24bd38ec7 | |||
| 45934baf0a | |||
| 4384ee53c5 | |||
| d89c326be5 | |||
| 2dccb4f871 | |||
| a9ddf3074e | |||
| db087bb184 | |||
| 5e9083041f | |||
| be0555d6bc | |||
| ea19dfcbe6 | |||
| fa55e32153 | |||
| f6b1f5c45a | |||
| 1f9a1822c9 | |||
| 63015c6bbc | |||
| 24b30df992 | |||
| 67b1003831 | |||
| 00d37bdaf0 | |||
| c5fcb1427e | |||
| 9118ebd662 | |||
| 19cdf47bed | |||
| 95bac94479 | |||
| 421a8934a7 | |||
| f7f45668d3 | |||
| c8e9a5b897 | |||
| a1bfa62796 | |||
| 64e11cbc3c | |||
| 15445aa13c | |||
| 52834047f2 | |||
| 76817675f1 | |||
| 2cce8f94c9 | |||
| 9b5f102d9f | |||
| 68de33165e | |||
| f56ad33366 | |||
| 35c63802b5 | |||
| 3458f6fe23 | |||
| 8f305033f7 | |||
| 4a18f1c418 | |||
| 373edb6d80 | |||
| 1c66e0e599 | |||
| 536e7720cb | |||
| 75eac4b1ce | |||
| 9ed0afee6b | |||
| f700473967 | |||
| 5fc0fd2a95 | |||
| 5f88c7ceac | |||
| 4c7ec65577 | |||
| 3e18299099 | |||
| 021be88880 | |||
| a479e81682 | |||
| c3e3012a94 | |||
| c9e9df3825 | |||
| 7ca2fa1bcc | |||
| 208c24682b | |||
| 316c784bde | |||
| 1467ae8a89 | |||
| 1881615860 | |||
| 0ea3afbe70 | |||
| 4f1d7f495a | |||
| 7c4ecb013e | |||
| ed5976a22c | |||
| 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 | |||
| e4ff49bade | |||
| ada4806493 | |||
| e48e822d07 |
10
.gitignore
vendored
@ -3,3 +3,13 @@ dist
|
|||||||
node_modules
|
node_modules
|
||||||
.vscode-test/
|
.vscode-test/
|
||||||
*.vsix
|
*.vsix
|
||||||
|
|
||||||
|
# waveform_trace 打包产物
|
||||||
|
tools/waveform_trace/src/build/
|
||||||
|
tools/waveform_trace/src/dist/
|
||||||
|
tools/waveform_trace/src/*.spec
|
||||||
|
|
||||||
|
# Python 缓存
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
|||||||
4
.npmrc
@ -1 +1,3 @@
|
|||||||
enable-pre-post-scripts = true
|
enable-pre-post-scripts = true
|
||||||
|
shamefully-hoist = true
|
||||||
|
public-hoist-pattern[] = *
|
||||||
33
.vscodeignore
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# 开发文件
|
||||||
|
.vscode/**
|
||||||
|
.vscode-test/**
|
||||||
|
src/**
|
||||||
|
.gitignore
|
||||||
|
.yarnrc
|
||||||
|
vsc-extension-quickstart.md
|
||||||
|
**/tsconfig.json
|
||||||
|
**/.eslintrc.json
|
||||||
|
**/*.map
|
||||||
|
**/*.ts
|
||||||
|
|
||||||
|
# 测试文件
|
||||||
|
out/test/**
|
||||||
|
|
||||||
|
# 依赖
|
||||||
|
node_modules/**
|
||||||
|
|
||||||
|
# 文档(避免中文文件名打包问题)
|
||||||
|
docs/**
|
||||||
|
CLAUDE.md
|
||||||
|
|
||||||
|
# 只排除 waveform_trace 的 src/dist 目录
|
||||||
|
tools/waveform_trace/src/**
|
||||||
|
tools/iverilog/examples/**
|
||||||
|
tools/iverilog/INSTALL.md
|
||||||
|
tools/iverilog/README.md
|
||||||
|
tools/iverilog/DOWNLOAD_INSTRUCTIONS.md
|
||||||
|
|
||||||
|
|
||||||
|
# Git 相关
|
||||||
|
.git/**
|
||||||
|
.github/**
|
||||||
69
CHANGELOG.md
@ -1,9 +1,68 @@
|
|||||||
# 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.12] - 2026-03-06
|
||||||
|
|
||||||
## [Unreleased]
|
### 新增
|
||||||
|
|
||||||
- Initial release
|
- 支持 AskUserQuestion 多问题和多选功能
|
||||||
|
|
||||||
|
## [1.0.9] - 2026-03-04
|
||||||
|
|
||||||
|
### 优化
|
||||||
|
|
||||||
|
- 将工具折叠图标颜色从蓝色改为灰色
|
||||||
|
- 统一使用蓝色主题色
|
||||||
|
- 优化打包配置,排除重复的 exe 文件
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- 修复代码变更继续对话查找不到之前的代码变更信息的 bug
|
||||||
|
- 修复对话展示两遍的问题
|
||||||
|
|
||||||
|
## [1.0.8] - 2026-03-03
|
||||||
|
|
||||||
|
### 新增
|
||||||
|
|
||||||
|
- 删除文件确认功能
|
||||||
|
- 文件路径标签显示
|
||||||
|
- 企业试用用户欢迎弹窗优化
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- 修复继续对话时消息覆盖问题
|
||||||
|
- 修复试用用户欢迎弹窗显示逻辑
|
||||||
|
- 修复企业试用用户仍弹出邀请码的问题
|
||||||
|
- 修复登录过期点击重新登录失败的问题
|
||||||
|
|
||||||
|
## [1.0.7] - 2026-03-02
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- 修复 AI 响应内容重复显示问题
|
||||||
|
|
||||||
|
## [1.0.6] - 2026-03-02
|
||||||
|
|
||||||
|
### 新增
|
||||||
|
|
||||||
|
- Git Diff 功能:支持查看当前文件的 Git 差异对比
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- 修复添加上下文搜索选择文件不匹配的问题
|
||||||
|
- 修复过期认证状态未清除导致重新登录失败的问题
|
||||||
|
|
||||||
|
## [1.0.4] - 2026-01-28
|
||||||
|
|
||||||
|
IC Coder插件端正式上线。
|
||||||
|
|
||||||
|
IC Coder 插件端是一个是一个自主式人工智能 Verilog 编码平台,可以将芯片设计与验证的效率提升至少20倍!
|
||||||
|
|
||||||
|
主要功能:
|
||||||
|
|
||||||
|
- 自动搭建电路架构:够根据自然语言描述的设计需求,自动生成完整的电路架构
|
||||||
|
- AI自主仿真:IC Coder提供完全自动化的仿真验证流程,无需手动编写测试代码
|
||||||
|
- AI自主代码迭代:实现了真正的自主式开发循环,能够持续优化代码直到满足设计要求
|
||||||
|
- 随时可掌控:提供透明化的开发过程,让用户始终掌握AI的工作状态
|
||||||
|
- 多层次安全保障:将数据安全和隐私保护作为核心设计原则,提供企业级的安全保障
|
||||||
|
|||||||
93
README.md
@ -1,24 +1,87 @@
|
|||||||
# 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智能体协同工作,分别负责架构设计、代码生成、验证测试等不同环节
|
||||||
|
- **增强上下文引擎**:智能理解和管理大规模设计上下文,确保生成代码的一致性和准确性
|
||||||
|
|
||||||
- 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)了解更多细节。
|
||||||
|
|||||||
261
docs/AskUserQuestion-API设计.md
Normal file
@ -0,0 +1,261 @@
|
|||||||
|
# AskUserQuestion 多选支持 - API 设计文档
|
||||||
|
|
||||||
|
## 问题描述
|
||||||
|
|
||||||
|
当前 AI 询问用户问题时存在以下问题:
|
||||||
|
1. 后端返回的选项不准确
|
||||||
|
2. 多个问题只给几个选项
|
||||||
|
3. 不支持多选方式
|
||||||
|
|
||||||
|
## 需求
|
||||||
|
|
||||||
|
实现一个问题对应多个选项,支持多选的方式。
|
||||||
|
|
||||||
|
## 数据结构设计
|
||||||
|
|
||||||
|
### 后端返回格式
|
||||||
|
|
||||||
|
后端通过 SSE 的 `ask_user` 事件返回以下格式:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"askId": "ask_1234567890",
|
||||||
|
"questions": [
|
||||||
|
{
|
||||||
|
"question": "请确认 SPI 控制器的配置需求:工作模式?",
|
||||||
|
"options": [
|
||||||
|
"Master/8位/模式0/固定分频/需要CS",
|
||||||
|
"Master/可配置位宽/可配置模式/需要CS",
|
||||||
|
"Slave模式"
|
||||||
|
],
|
||||||
|
"multiSelect": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "数据位宽?",
|
||||||
|
"options": [
|
||||||
|
"8位 还是其他?"
|
||||||
|
],
|
||||||
|
"multiSelect": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "时钟极性和相位?",
|
||||||
|
"options": [
|
||||||
|
"CPOL=0/CPHA=0 (模式0) 还是其他模式?"
|
||||||
|
],
|
||||||
|
"multiSelect": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "时钟分频?",
|
||||||
|
"options": [
|
||||||
|
"需要可配置的分频比吗?"
|
||||||
|
],
|
||||||
|
"multiSelect": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "是否需要芯片选信号 (CS) 控制?",
|
||||||
|
"options": [
|
||||||
|
"是",
|
||||||
|
"否"
|
||||||
|
],
|
||||||
|
"multiSelect": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 前端数据结构
|
||||||
|
|
||||||
|
#### 1. API 类型定义 (`src/types/api.ts`)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
/** ask_user 事件数据 */
|
||||||
|
export interface AskUserEvent {
|
||||||
|
askId: string;
|
||||||
|
questions: QuestionItem[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 单个问题项 */
|
||||||
|
export interface QuestionItem {
|
||||||
|
question: string;
|
||||||
|
options: string[];
|
||||||
|
multiSelect?: boolean; // 是否支持多选,默认 false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. MessageSegment 类型 (`src/services/dialogService.ts`)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export interface MessageSegment {
|
||||||
|
type: "text" | "tool" | "question" | "agent" | "plan" | "progress";
|
||||||
|
// ... 其他字段
|
||||||
|
askId?: string;
|
||||||
|
questions?: QuestionItem[]; // 改为问题数组
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 用户回答格式 (`src/types/api.ts`)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export interface AnswerRequest {
|
||||||
|
taskId: string;
|
||||||
|
askId: string;
|
||||||
|
answers: {
|
||||||
|
[questionIndex: number]: string[]; // 每个问题的答案数组(支持多选)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 前端实现要点
|
||||||
|
|
||||||
|
### 1. 显示多个问题
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 遍历 questions 数组,为每个问题生成 UI
|
||||||
|
segment.questions?.forEach((q, index) => {
|
||||||
|
// 显示问题标题
|
||||||
|
// 显示选项(单选或多选)
|
||||||
|
// 收集答案
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 多选支持
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
if (q.multiSelect) {
|
||||||
|
// 渲染复选框
|
||||||
|
// 允许选择多个选项
|
||||||
|
} else {
|
||||||
|
// 渲染单选按钮
|
||||||
|
// 只允许选择一个选项
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 提交答案
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const answers = {
|
||||||
|
0: ["Master/8位/模式0/固定分频/需要CS"], // 第1个问题的答案
|
||||||
|
1: ["8位 还是其他?"], // 第2个问题的答案
|
||||||
|
2: ["CPOL=0/CPHA=0 (模式0) 还是其他模式?"], // 第3个问题的答案
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
vscode.postMessage({
|
||||||
|
command: 'userAnswer',
|
||||||
|
askId: 'ask_1234567890',
|
||||||
|
answers: answers
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## 后端需要做的修改
|
||||||
|
|
||||||
|
### 1. 修改 AskUserQuestion 工具的返回格式
|
||||||
|
|
||||||
|
从:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"askId": "xxx",
|
||||||
|
"question": "单个问题",
|
||||||
|
"options": ["选项1", "选项2"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
改为:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"askId": "xxx",
|
||||||
|
"questions": [
|
||||||
|
{
|
||||||
|
"question": "问题1",
|
||||||
|
"options": ["选项1", "选项2"],
|
||||||
|
"multiSelect": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "问题2",
|
||||||
|
"options": ["选项A", "选项B", "选项C"],
|
||||||
|
"multiSelect": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 接收答案的格式
|
||||||
|
|
||||||
|
从:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"taskId": "xxx",
|
||||||
|
"askId": "xxx",
|
||||||
|
"selected": ["选项1"],
|
||||||
|
"customInput": "自定义输入"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
改为:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"taskId": "xxx",
|
||||||
|
"askId": "xxx",
|
||||||
|
"answers": {
|
||||||
|
"0": ["选项1"], // 第1个问题的答案
|
||||||
|
"1": ["选项A", "选项B"] // 第2个问题的答案(多选)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 示例场景
|
||||||
|
|
||||||
|
### 场景:SPI 控制器配置
|
||||||
|
|
||||||
|
**后端发送:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"askId": "ask_spi_config",
|
||||||
|
"questions": [
|
||||||
|
{
|
||||||
|
"question": "工作模式?",
|
||||||
|
"options": [
|
||||||
|
"Master/8位/模式0/固定分频/需要CS",
|
||||||
|
"Master/可配置位宽/可配置模式/需要CS",
|
||||||
|
"Slave模式"
|
||||||
|
],
|
||||||
|
"multiSelect": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"question": "需要哪些功能?",
|
||||||
|
"options": [
|
||||||
|
"可配置时钟分频",
|
||||||
|
"可配置数据位宽",
|
||||||
|
"支持多个CS",
|
||||||
|
"DMA支持"
|
||||||
|
],
|
||||||
|
"multiSelect": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**用户选择:**
|
||||||
|
- 问题1:选择 "Master/8位/模式0/固定分频/需要CS"
|
||||||
|
- 问题2:选择 "可配置时钟分频" 和 "可配置数据位宽"
|
||||||
|
|
||||||
|
**前端提交:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"taskId": "task_xxx",
|
||||||
|
"askId": "ask_spi_config",
|
||||||
|
"answers": {
|
||||||
|
"0": ["Master/8位/模式0/固定分频/需要CS"],
|
||||||
|
"1": ["可配置时钟分频", "可配置数据位宽"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 总结
|
||||||
|
|
||||||
|
这个设计方案:
|
||||||
|
1. ✅ 支持多个问题
|
||||||
|
2. ✅ 每个问题有多个选项
|
||||||
|
3. ✅ 支持单选和多选
|
||||||
|
4. ✅ 数据结构清晰,易于扩展
|
||||||
|
5. ✅ 向后兼容(可以只有一个问题)
|
||||||
@ -67,6 +67,11 @@
|
|||||||
CO03l8nmFBBTNPDg7lN9a9fYwDdgsRIDVDwTrx6Esggi6HnzmrMTJQQJ99BLACAAAAAAAAAAAAAGAZDOVVyT
|
CO03l8nmFBBTNPDg7lN9a9fYwDdgsRIDVDwTrx6Esggi6HnzmrMTJQQJ99BLACAAAAAAAAAAAAAGAZDOVVyT
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
//蔡工的token
|
||||||
|
6CB3tOZPiwNi6rrOuFHMe6QzrVWBnajW5fJsNgCWu8jtERUCCRnJJQQJ99CAACAAAAAAAAAAAAASAZDO3FnY
|
||||||
|
```
|
||||||
|
|
||||||
### 3. 创建发布者账号
|
### 3. 创建发布者账号
|
||||||
|
|
||||||
发布者账号是你在 VS Code 市场的身份标识。
|
发布者账号是你在 VS Code 市场的身份标识。
|
||||||
@ -83,6 +88,7 @@ CO03l8nmFBBTNPDg7lN9a9fYwDdgsRIDVDwTrx6Esggi6HnzmrMTJQQJ99BLACAAAAAAAAAAAAAGAZDO
|
|||||||
5. 点击 **Create** 完成创建
|
5. 点击 **Create** 完成创建
|
||||||
|
|
||||||
**注意事项:**
|
**注意事项:**
|
||||||
|
|
||||||
- Publisher ID 一旦创建无法修改
|
- Publisher ID 一旦创建无法修改
|
||||||
- Publisher ID 必须全局唯一
|
- Publisher ID 必须全局唯一
|
||||||
- 建议使用有意义且专业的 ID
|
- 建议使用有意义且专业的 ID
|
||||||
@ -121,6 +127,7 @@ CO03l8nmFBBTNPDg7lN9a9fYwDdgsRIDVDwTrx6Esggi6HnzmrMTJQQJ99BLACAAAAAAAAAAAAAGAZDO
|
|||||||
## [0.0.2] - 2025-12-29
|
## [0.0.2] - 2025-12-29
|
||||||
|
|
||||||
### 新增
|
### 新增
|
||||||
|
|
||||||
- 添加发送和暂停按钮功能
|
- 添加发送和暂停按钮功能
|
||||||
- 添加一键优化按钮组件
|
- 添加一键优化按钮组件
|
||||||
- 添加 Plan 开关组件
|
- 添加 Plan 开关组件
|
||||||
@ -128,11 +135,13 @@ CO03l8nmFBBTNPDg7lN9a9fYwDdgsRIDVDwTrx6Esggi6HnzmrMTJQQJ99BLACAAAAAAAAAAAAAGAZDO
|
|||||||
- 添加上下文压缩功能
|
- 添加上下文压缩功能
|
||||||
|
|
||||||
### 改进
|
### 改进
|
||||||
|
|
||||||
- 优化用户界面交互体验
|
- 优化用户界面交互体验
|
||||||
|
|
||||||
## [0.0.1] - 2025-12-XX
|
## [0.0.1] - 2025-12-XX
|
||||||
|
|
||||||
### 新增
|
### 新增
|
||||||
|
|
||||||
- 初始版本发布
|
- 初始版本发布
|
||||||
- Verilog 代码智能生成
|
- Verilog 代码智能生成
|
||||||
- 集成 iverilog 仿真工具
|
- 集成 iverilog 仿真工具
|
||||||
@ -156,6 +165,7 @@ in the Software without restriction...
|
|||||||
### 4. 优化 README.md
|
### 4. 优化 README.md
|
||||||
|
|
||||||
确保 README 包含:
|
确保 README 包含:
|
||||||
|
|
||||||
- 清晰的功能介绍
|
- 清晰的功能介绍
|
||||||
- 使用截图或 GIF 演示
|
- 使用截图或 GIF 演示
|
||||||
- 详细的使用说明
|
- 详细的使用说明
|
||||||
@ -214,6 +224,7 @@ pnpm vsce publish
|
|||||||
**步骤:**
|
**步骤:**
|
||||||
|
|
||||||
1. 本地打包插件:
|
1. 本地打包插件:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm run package
|
pnpm run package
|
||||||
pnpm vsce package[pnpm vsce package --no-dependencies]
|
pnpm vsce package[pnpm vsce package --no-dependencies]
|
||||||
@ -252,7 +263,7 @@ pnpm vsce publish major
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 发布指定版本
|
# 发布指定版本
|
||||||
pnpm vsce publish 0.0.3
|
npx vsce publish --packagePath iccoder-1.0.7.vsix
|
||||||
```
|
```
|
||||||
|
|
||||||
### 更新流程建议
|
### 更新流程建议
|
||||||
@ -263,6 +274,42 @@ pnpm vsce publish 0.0.3
|
|||||||
4. 执行发布命令
|
4. 执行发布命令
|
||||||
5. 验证市场上的插件是否正常
|
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
|
||||||
|
#先编译
|
||||||
|
pnpm run compile
|
||||||
|
|
||||||
|
#中间build
|
||||||
|
pnpm run build
|
||||||
|
|
||||||
|
#后打包成.vsix
|
||||||
|
pnpm vsce package --no-dependencies
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 手动上传/命令上传
|
||||||
|
- https://marketplace.visualstudio.com/ 在这个里面手动上传 更新就选择update
|
||||||
|
- 命令上传:vsce publish
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
@ -272,6 +319,7 @@ pnpm vsce publish 0.0.3
|
|||||||
**原因:** PAT Token 无效或过期
|
**原因:** PAT Token 无效或过期
|
||||||
|
|
||||||
**解决方案:**
|
**解决方案:**
|
||||||
|
|
||||||
- 重新生成 PAT Token
|
- 重新生成 PAT Token
|
||||||
- 重新登录:`pnpm vsce login ic-coder-team`
|
- 重新登录:`pnpm vsce login ic-coder-team`
|
||||||
|
|
||||||
@ -280,6 +328,7 @@ pnpm vsce publish 0.0.3
|
|||||||
**原因:** Publisher ID 不存在或不匹配
|
**原因:** Publisher ID 不存在或不匹配
|
||||||
|
|
||||||
**解决方案:**
|
**解决方案:**
|
||||||
|
|
||||||
- 检查 `package.json` 中的 `publisher` 字段
|
- 检查 `package.json` 中的 `publisher` 字段
|
||||||
- 确认已在市场创建对应的 Publisher
|
- 确认已在市场创建对应的 Publisher
|
||||||
|
|
||||||
@ -288,17 +337,20 @@ pnpm vsce publish 0.0.3
|
|||||||
**原因:** 必需文件缺失
|
**原因:** 必需文件缺失
|
||||||
|
|
||||||
**解决方案:**
|
**解决方案:**
|
||||||
|
|
||||||
- 确保 `dist/` 目录存在且包含编译后的代码
|
- 确保 `dist/` 目录存在且包含编译后的代码
|
||||||
- 运行 `pnpm run package` 重新构建
|
- 运行 `pnpm run package` 重新构建
|
||||||
|
|
||||||
### 4. 插件审核被拒
|
### 4. 插件审核被拒
|
||||||
|
|
||||||
**常见原因:**
|
**常见原因:**
|
||||||
|
|
||||||
- 插件名称或描述违反市场规则
|
- 插件名称或描述违反市场规则
|
||||||
- 图标不符合要求(建议 128x128 PNG)
|
- 图标不符合要求(建议 128x128 PNG)
|
||||||
- README 内容不完整
|
- README 内容不完整
|
||||||
|
|
||||||
**解决方案:**
|
**解决方案:**
|
||||||
|
|
||||||
- 查看审核反馈邮件
|
- 查看审核反馈邮件
|
||||||
- 修改相关内容后重新发布
|
- 修改相关内容后重新发布
|
||||||
|
|
||||||
@ -320,6 +372,7 @@ code --install-extension ic-coder-plugin-0.0.2.vsix
|
|||||||
```
|
```
|
||||||
|
|
||||||
或者在 VS Code 中:
|
或者在 VS Code 中:
|
||||||
|
|
||||||
1. 打开扩展面板
|
1. 打开扩展面板
|
||||||
2. 点击 `...` 菜单
|
2. 点击 `...` 菜单
|
||||||
3. 选择 **Install from VSIX...**
|
3. 选择 **Install from VSIX...**
|
||||||
804
docs/VSCode-Extension-API-Guide.md
Normal file
@ -0,0 +1,804 @@
|
|||||||
|
# VS Code Extension API 核心知识点
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
- [1. Extension 生命周期](#1-extension-生命周期) ⭐⭐⭐
|
||||||
|
- [2. 激活事件 (Activation Events)](#2-激活事件-activation-events) ⭐⭐
|
||||||
|
- [3. 命令系统 (Commands)](#3-命令系统-commands) ⭐⭐
|
||||||
|
- [4. Webview API](#4-webview-api) ⭐⭐⭐⭐⭐ **面试重点**
|
||||||
|
- [5. TreeView 和自定义视图](#5-treeview-和自定义视图) ⭐⭐
|
||||||
|
- [6. 文件系统操作](#6-文件系统操作) ⭐⭐⭐
|
||||||
|
- [7. 配置和存储](#7-配置和存储) ⭐⭐⭐⭐ **面试重点**
|
||||||
|
- [8. 消息通知](#8-消息通知) ⭐
|
||||||
|
- [9. 语言特性支持](#9-语言特性支持) ⭐
|
||||||
|
- [10. 调试和诊断](#10-调试和诊断) ⭐
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Extension 生命周期 ⭐⭐⭐
|
||||||
|
|
||||||
|
### 1.1 核心函数 🔥必考
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// extension.ts
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
// 插件激活时调用(只调用一次)
|
||||||
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
|
console.log('Extension is now active!');
|
||||||
|
|
||||||
|
// 注册命令、视图、事件监听等
|
||||||
|
// 使用 context.subscriptions 管理资源
|
||||||
|
}
|
||||||
|
|
||||||
|
// 插件停用时调用(清理资源)
|
||||||
|
export function deactivate() {
|
||||||
|
console.log('Extension is deactivated');
|
||||||
|
// 清理资源、关闭连接等
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 1.2 ExtensionContext 重要属性 🔥必考
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface ExtensionContext {
|
||||||
|
// 插件订阅管理(自动清理)
|
||||||
|
subscriptions: { dispose(): any }[];
|
||||||
|
|
||||||
|
// 工作区存储路径
|
||||||
|
storageUri: vscode.Uri | undefined;
|
||||||
|
globalStorageUri: vscode.Uri;
|
||||||
|
|
||||||
|
// 插件路径
|
||||||
|
extensionUri: vscode.Uri;
|
||||||
|
extensionPath: string;
|
||||||
|
|
||||||
|
// 状态存储
|
||||||
|
workspaceState: Memento; // 工作区级别
|
||||||
|
globalState: Memento; // 全局级别
|
||||||
|
secrets: SecretStorage; // 敏感信息存储
|
||||||
|
|
||||||
|
// 环境变量
|
||||||
|
environmentVariableCollection: EnvironmentVariableCollection;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 1.3 资源管理最佳实践 🔥必考
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
|
// ✅ 推荐:使用 context.subscriptions 自动管理
|
||||||
|
context.subscriptions.push(
|
||||||
|
vscode.commands.registerCommand('extension.command', () => {})
|
||||||
|
);
|
||||||
|
|
||||||
|
// ❌ 不推荐:手动管理容易忘记清理
|
||||||
|
const disposable = vscode.commands.registerCommand('extension.command', () => {});
|
||||||
|
// 需要在 deactivate 中手动调用 disposable.dispose()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 激活事件 (Activation Events) ⭐⭐
|
||||||
|
|
||||||
|
### 2.1 常用激活事件 📌重要
|
||||||
|
|
||||||
|
```json
|
||||||
|
// package.json
|
||||||
|
{
|
||||||
|
"activationEvents": [
|
||||||
|
// 启动时激活
|
||||||
|
"onStartupFinished",
|
||||||
|
|
||||||
|
// 执行命令时激活
|
||||||
|
"onCommand:extension.helloWorld",
|
||||||
|
|
||||||
|
// 打开特定语言文件时激活
|
||||||
|
"onLanguage:javascript",
|
||||||
|
"onLanguage:verilog",
|
||||||
|
|
||||||
|
// 打开特定文件类型时激活
|
||||||
|
"onFileSystem:sftp",
|
||||||
|
|
||||||
|
// 打开特定视图时激活
|
||||||
|
"onView:myCustomView",
|
||||||
|
|
||||||
|
// 调试时激活
|
||||||
|
"onDebug",
|
||||||
|
|
||||||
|
// 打开特定 URI 时激活
|
||||||
|
"onUri",
|
||||||
|
|
||||||
|
// Webview 恢复时激活
|
||||||
|
"onWebviewPanel:myWebview",
|
||||||
|
|
||||||
|
// 任务执行时激活
|
||||||
|
"onTaskType:npm"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 延迟激活策略 🔥必考
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ 推荐:使用 onStartupFinished 延迟激活
|
||||||
|
"activationEvents": ["onStartupFinished"]
|
||||||
|
|
||||||
|
// ❌ 不推荐:使用 * 会拖慢启动速度
|
||||||
|
"activationEvents": ["*"]
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 命令系统 (Commands)
|
||||||
|
|
||||||
|
### 3.1 注册命令
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 注册简单命令
|
||||||
|
const disposable = vscode.commands.registerCommand(
|
||||||
|
'extension.helloWorld',
|
||||||
|
() => {
|
||||||
|
vscode.window.showInformationMessage('Hello World!');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
context.subscriptions.push(disposable);
|
||||||
|
|
||||||
|
// 注册带参数的命令
|
||||||
|
vscode.commands.registerCommand(
|
||||||
|
'extension.openFile',
|
||||||
|
(filePath: string) => {
|
||||||
|
vscode.workspace.openTextDocument(filePath).then(doc => {
|
||||||
|
vscode.window.showTextDocument(doc);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 执行命令
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 执行内置命令
|
||||||
|
await vscode.commands.executeCommand('workbench.action.files.save');
|
||||||
|
|
||||||
|
// 执行自定义命令
|
||||||
|
await vscode.commands.executeCommand('extension.openFile', '/path/to/file');
|
||||||
|
|
||||||
|
// 获取所有可用命令
|
||||||
|
const commands = await vscode.commands.getCommands();
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 常用内置命令
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 文件操作
|
||||||
|
'workbench.action.files.save'
|
||||||
|
'workbench.action.files.saveAll'
|
||||||
|
'workbench.action.closeActiveEditor'
|
||||||
|
|
||||||
|
// 编辑器操作
|
||||||
|
'editor.action.formatDocument'
|
||||||
|
'editor.action.commentLine'
|
||||||
|
'editor.action.selectAll'
|
||||||
|
|
||||||
|
// 窗口操作
|
||||||
|
'workbench.action.toggleSidebarVisibility'
|
||||||
|
'workbench.action.terminal.new'
|
||||||
|
'workbench.action.quickOpen'
|
||||||
|
|
||||||
|
// Git 操作
|
||||||
|
'git.commit'
|
||||||
|
'git.push'
|
||||||
|
'git.pull'
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Webview API ⭐⭐⭐⭐⭐ **面试重点**
|
||||||
|
|
||||||
|
### 4.1 创建 Webview Panel 🔥必考
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const panel = vscode.window.createWebviewPanel(
|
||||||
|
'myWebview', // viewType(唯一标识)
|
||||||
|
'My Webview', // 标题
|
||||||
|
vscode.ViewColumn.One, // 显示位置
|
||||||
|
{
|
||||||
|
enableScripts: true, // 启用 JavaScript
|
||||||
|
retainContextWhenHidden: true, // 隐藏时保留状态
|
||||||
|
localResourceRoots: [ // 允许访问的本地资源路径
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, 'media')
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 设置 Webview 内容
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
panel.webview.html = getWebviewContent();
|
||||||
|
|
||||||
|
function getWebviewContent() {
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>My Webview</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Hello from Webview!</h1>
|
||||||
|
<button onclick="sendMessage()">Send Message</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const vscode = acquireVsCodeApi();
|
||||||
|
|
||||||
|
function sendMessage() {
|
||||||
|
vscode.postMessage({
|
||||||
|
command: 'alert',
|
||||||
|
text: 'Hello from Webview!'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接收来自 Extension 的消息
|
||||||
|
window.addEventListener('message', event => {
|
||||||
|
const message = event.data;
|
||||||
|
console.log('Received:', message);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.3 Webview 消息通信 🔥必考(项目核心)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Extension → Webview
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: 'update',
|
||||||
|
data: 'some data'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Webview → Extension
|
||||||
|
panel.webview.onDidReceiveMessage(
|
||||||
|
message => {
|
||||||
|
switch (message.command) {
|
||||||
|
case 'alert':
|
||||||
|
vscode.window.showInformationMessage(message.text);
|
||||||
|
break;
|
||||||
|
case 'getData':
|
||||||
|
// 处理数据请求
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: 'dataResponse',
|
||||||
|
data: fetchData()
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
context.subscriptions
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.4 Webview 生命周期管理 📌重要
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 监听 Webview 关闭事件
|
||||||
|
panel.onDidDispose(
|
||||||
|
() => {
|
||||||
|
// 清理资源
|
||||||
|
console.log('Webview disposed');
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
context.subscriptions
|
||||||
|
);
|
||||||
|
|
||||||
|
// 监听 Webview 可见性变化
|
||||||
|
panel.onDidChangeViewState(
|
||||||
|
e => {
|
||||||
|
if (e.webviewPanel.visible) {
|
||||||
|
console.log('Webview is now visible');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
context.subscriptions
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.5 加载本地资源 📌重要
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 获取本地资源 URI
|
||||||
|
const scriptUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, 'media', 'script.js')
|
||||||
|
);
|
||||||
|
|
||||||
|
const styleUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, 'media', 'style.css')
|
||||||
|
);
|
||||||
|
|
||||||
|
// 在 HTML 中使用
|
||||||
|
const html = `
|
||||||
|
<link href="${styleUri}" rel="stylesheet">
|
||||||
|
<script src="${scriptUri}"></script>
|
||||||
|
`;
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.6 Webview 状态持久化 📌重要
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Webview 中保存状态
|
||||||
|
const vscode = acquireVsCodeApi();
|
||||||
|
const state = vscode.getState() || { count: 0 };
|
||||||
|
|
||||||
|
// 更新状态
|
||||||
|
state.count++;
|
||||||
|
vscode.setState(state);
|
||||||
|
|
||||||
|
// Extension 中序列化状态
|
||||||
|
panel.webview.options = {
|
||||||
|
enableScripts: true,
|
||||||
|
retainContextWhenHidden: true
|
||||||
|
};
|
||||||
|
|
||||||
|
// 恢复 Webview
|
||||||
|
vscode.window.registerWebviewPanelSerializer('myWebview', {
|
||||||
|
async deserializeWebviewPanel(webviewPanel, state) {
|
||||||
|
webviewPanel.webview.html = getWebviewContent();
|
||||||
|
// 恢复状态
|
||||||
|
webviewPanel.webview.postMessage({ command: 'restore', state });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. TreeView 和自定义视图
|
||||||
|
|
||||||
|
### 5.1 创建 TreeView Provider
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class MyTreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
|
||||||
|
private _onDidChangeTreeData = new vscode.EventEmitter<TreeItem | undefined>();
|
||||||
|
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
|
||||||
|
|
||||||
|
refresh(): void {
|
||||||
|
this._onDidChangeTreeData.fire(undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
getTreeItem(element: TreeItem): vscode.TreeItem {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
getChildren(element?: TreeItem): Thenable<TreeItem[]> {
|
||||||
|
if (!element) {
|
||||||
|
// 返回根节点
|
||||||
|
return Promise.resolve([
|
||||||
|
new TreeItem('Item 1', vscode.TreeItemCollapsibleState.None),
|
||||||
|
new TreeItem('Item 2', vscode.TreeItemCollapsibleState.Collapsed)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
// 返回子节点
|
||||||
|
return Promise.resolve([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TreeItem extends vscode.TreeItem {
|
||||||
|
constructor(
|
||||||
|
public readonly label: string,
|
||||||
|
public readonly collapsibleState: vscode.TreeItemCollapsibleState
|
||||||
|
) {
|
||||||
|
super(label, collapsibleState);
|
||||||
|
this.tooltip = `Tooltip for ${label}`;
|
||||||
|
this.command = {
|
||||||
|
command: 'extension.itemClicked',
|
||||||
|
title: 'Click Item',
|
||||||
|
arguments: [this]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 注册 TreeView
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const treeDataProvider = new MyTreeDataProvider();
|
||||||
|
const treeView = vscode.window.createTreeView('myTreeView', {
|
||||||
|
treeDataProvider,
|
||||||
|
showCollapseAll: true
|
||||||
|
});
|
||||||
|
|
||||||
|
context.subscriptions.push(treeView);
|
||||||
|
|
||||||
|
// 刷新视图
|
||||||
|
treeDataProvider.refresh();
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.3 WebviewView Provider(侧边栏 Webview)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class MyWebviewViewProvider implements vscode.WebviewViewProvider {
|
||||||
|
resolveWebviewView(
|
||||||
|
webviewView: vscode.WebviewView,
|
||||||
|
context: vscode.WebviewViewResolveContext,
|
||||||
|
token: vscode.CancellationToken
|
||||||
|
) {
|
||||||
|
webviewView.webview.options = {
|
||||||
|
enableScripts: true
|
||||||
|
};
|
||||||
|
|
||||||
|
webviewView.webview.html = getWebviewContent();
|
||||||
|
|
||||||
|
webviewView.webview.onDidReceiveMessage(message => {
|
||||||
|
// 处理消息
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注册
|
||||||
|
vscode.window.registerWebviewViewProvider(
|
||||||
|
'myWebviewView',
|
||||||
|
new MyWebviewViewProvider()
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 文件系统操作 ⭐⭐⭐
|
||||||
|
|
||||||
|
### 6.1 读取文件 📌重要
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 读取文本文件
|
||||||
|
const uri = vscode.Uri.file('/path/to/file.txt');
|
||||||
|
const content = await vscode.workspace.fs.readFile(uri);
|
||||||
|
const text = Buffer.from(content).toString('utf8');
|
||||||
|
|
||||||
|
// 使用 TextDocument API
|
||||||
|
const document = await vscode.workspace.openTextDocument(uri);
|
||||||
|
const text = document.getText();
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.2 写入文件
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 写入文件
|
||||||
|
const uri = vscode.Uri.file('/path/to/file.txt');
|
||||||
|
const content = Buffer.from('Hello World', 'utf8');
|
||||||
|
await vscode.workspace.fs.writeFile(uri, content);
|
||||||
|
|
||||||
|
// 使用 WorkspaceEdit
|
||||||
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
edit.createFile(uri, { overwrite: true });
|
||||||
|
edit.insert(uri, new vscode.Position(0, 0), 'Hello World');
|
||||||
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.3 文件监听
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 监听文件变化
|
||||||
|
const watcher = vscode.workspace.createFileSystemWatcher('**/*.js');
|
||||||
|
|
||||||
|
watcher.onDidCreate(uri => {
|
||||||
|
console.log('File created:', uri.fsPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
watcher.onDidChange(uri => {
|
||||||
|
console.log('File changed:', uri.fsPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
watcher.onDidDelete(uri => {
|
||||||
|
console.log('File deleted:', uri.fsPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
context.subscriptions.push(watcher);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6.4 工作区操作
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 获取工作区文件夹
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (workspaceFolders) {
|
||||||
|
const rootPath = workspaceFolders[0].uri.fsPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找文件
|
||||||
|
const files = await vscode.workspace.findFiles(
|
||||||
|
'**/*.ts', // include pattern
|
||||||
|
'**/node_modules/**' // exclude pattern
|
||||||
|
);
|
||||||
|
|
||||||
|
// 打开文件
|
||||||
|
const document = await vscode.workspace.openTextDocument(uri);
|
||||||
|
await vscode.window.showTextDocument(document);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 配置和存储 ⭐⭐⭐⭐ **面试重点**
|
||||||
|
|
||||||
|
### 7.1 读取配置 📌重要
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 读取配置
|
||||||
|
const config = vscode.workspace.getConfiguration('myExtension');
|
||||||
|
const value = config.get<string>('settingName', 'defaultValue');
|
||||||
|
|
||||||
|
// 监听配置变化
|
||||||
|
vscode.workspace.onDidChangeConfiguration(e => {
|
||||||
|
if (e.affectsConfiguration('myExtension.settingName')) {
|
||||||
|
console.log('Configuration changed');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.2 更新配置
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const config = vscode.workspace.getConfiguration('myExtension');
|
||||||
|
|
||||||
|
// 更新用户配置(全局)
|
||||||
|
await config.update('settingName', 'newValue', vscode.ConfigurationTarget.Global);
|
||||||
|
|
||||||
|
// 更新工作区配置
|
||||||
|
await config.update('settingName', 'newValue', vscode.ConfigurationTarget.Workspace);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.3 状态存储 🔥必考
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 工作区状态(仅当前工作区)
|
||||||
|
await context.workspaceState.update('key', 'value');
|
||||||
|
const value = context.workspaceState.get('key');
|
||||||
|
|
||||||
|
// 全局状态(跨工作区)
|
||||||
|
await context.globalState.update('key', 'value');
|
||||||
|
const value = context.globalState.get('key');
|
||||||
|
|
||||||
|
// 存储对象
|
||||||
|
await context.globalState.update('userData', { name: 'John', age: 30 });
|
||||||
|
```
|
||||||
|
|
||||||
|
### 7.4 敏感信息存储 🔥必考(Token 管理)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 存储密码、Token 等敏感信息
|
||||||
|
await context.secrets.store('apiToken', 'secret-token-value');
|
||||||
|
|
||||||
|
// 读取
|
||||||
|
const token = await context.secrets.get('apiToken');
|
||||||
|
|
||||||
|
// 删除
|
||||||
|
await context.secrets.delete('apiToken');
|
||||||
|
|
||||||
|
// 监听变化
|
||||||
|
context.secrets.onDidChange(e => {
|
||||||
|
console.log('Secret changed:', e.key);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 消息通知
|
||||||
|
|
||||||
|
### 8.1 信息提示
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 普通信息
|
||||||
|
vscode.window.showInformationMessage('Operation completed!');
|
||||||
|
|
||||||
|
// 警告
|
||||||
|
vscode.window.showWarningMessage('This action may cause issues');
|
||||||
|
|
||||||
|
// 错误
|
||||||
|
vscode.window.showErrorMessage('Operation failed!');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.2 带按钮的提示
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const result = await vscode.window.showInformationMessage(
|
||||||
|
'Do you want to continue?',
|
||||||
|
'Yes',
|
||||||
|
'No',
|
||||||
|
'Cancel'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result === 'Yes') {
|
||||||
|
// 用户点击了 Yes
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.3 输入框
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 简单输入
|
||||||
|
const input = await vscode.window.showInputBox({
|
||||||
|
prompt: 'Enter your name',
|
||||||
|
placeHolder: 'John Doe',
|
||||||
|
validateInput: (value) => {
|
||||||
|
return value.length < 3 ? 'Name too short' : null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 快速选择
|
||||||
|
const selected = await vscode.window.showQuickPick(
|
||||||
|
['Option 1', 'Option 2', 'Option 3'],
|
||||||
|
{
|
||||||
|
placeHolder: 'Select an option',
|
||||||
|
canPickMany: false
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 8.4 进度提示
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await vscode.window.withProgress(
|
||||||
|
{
|
||||||
|
location: vscode.ProgressLocation.Notification,
|
||||||
|
title: 'Processing...',
|
||||||
|
cancellable: true
|
||||||
|
},
|
||||||
|
async (progress, token) => {
|
||||||
|
token.onCancellationRequested(() => {
|
||||||
|
console.log('User canceled');
|
||||||
|
});
|
||||||
|
|
||||||
|
progress.report({ increment: 0, message: 'Starting...' });
|
||||||
|
await doWork1();
|
||||||
|
|
||||||
|
progress.report({ increment: 50, message: 'Half done...' });
|
||||||
|
await doWork2();
|
||||||
|
|
||||||
|
progress.report({ increment: 100, message: 'Complete!' });
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 语言特性支持
|
||||||
|
|
||||||
|
### 9.1 代码补全
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const provider = vscode.languages.registerCompletionItemProvider(
|
||||||
|
'javascript',
|
||||||
|
{
|
||||||
|
provideCompletionItems(document, position) {
|
||||||
|
const item = new vscode.CompletionItem('myFunction');
|
||||||
|
item.kind = vscode.CompletionItemKind.Function;
|
||||||
|
item.detail = 'My custom function';
|
||||||
|
item.documentation = 'This is a custom function';
|
||||||
|
item.insertText = new vscode.SnippetString('myFunction($1)$0');
|
||||||
|
|
||||||
|
return [item];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'.' // 触发字符
|
||||||
|
);
|
||||||
|
|
||||||
|
context.subscriptions.push(provider);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.2 悬停提示
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const provider = vscode.languages.registerHoverProvider('javascript', {
|
||||||
|
provideHover(document, position) {
|
||||||
|
const range = document.getWordRangeAtPosition(position);
|
||||||
|
const word = document.getText(range);
|
||||||
|
|
||||||
|
return new vscode.Hover([
|
||||||
|
`**${word}**`,
|
||||||
|
'This is a hover tooltip'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.3 诊断(错误提示)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const diagnosticCollection = vscode.languages.createDiagnosticCollection('myExtension');
|
||||||
|
context.subscriptions.push(diagnosticCollection);
|
||||||
|
|
||||||
|
function updateDiagnostics(document: vscode.TextDocument) {
|
||||||
|
const diagnostics: vscode.Diagnostic[] = [];
|
||||||
|
|
||||||
|
const text = document.getText();
|
||||||
|
const regex = /TODO/g;
|
||||||
|
let match;
|
||||||
|
|
||||||
|
while ((match = regex.exec(text))) {
|
||||||
|
const range = new vscode.Range(
|
||||||
|
document.positionAt(match.index),
|
||||||
|
document.positionAt(match.index + match[0].length)
|
||||||
|
);
|
||||||
|
|
||||||
|
const diagnostic = new vscode.Diagnostic(
|
||||||
|
range,
|
||||||
|
'TODO found',
|
||||||
|
vscode.DiagnosticSeverity.Warning
|
||||||
|
);
|
||||||
|
|
||||||
|
diagnostics.push(diagnostic);
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnosticCollection.set(document.uri, diagnostics);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. 调试和诊断
|
||||||
|
|
||||||
|
### 10.1 输出通道
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const outputChannel = vscode.window.createOutputChannel('My Extension');
|
||||||
|
context.subscriptions.push(outputChannel);
|
||||||
|
|
||||||
|
outputChannel.appendLine('Extension activated');
|
||||||
|
outputChannel.show(); // 显示输出面板
|
||||||
|
```
|
||||||
|
|
||||||
|
### 10.2 日志记录
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 使用 LogOutputChannel(带时间戳)
|
||||||
|
const logger = vscode.window.createOutputChannel('My Extension', { log: true });
|
||||||
|
|
||||||
|
logger.trace('Trace message');
|
||||||
|
logger.debug('Debug message');
|
||||||
|
logger.info('Info message');
|
||||||
|
logger.warn('Warning message');
|
||||||
|
logger.error('Error message');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 10.3 错误处理
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
try {
|
||||||
|
await riskyOperation();
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
vscode.window.showErrorMessage(`Error: ${error.message}`);
|
||||||
|
logger.error(error.stack || error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 最佳实践总结
|
||||||
|
|
||||||
|
### ✅ 推荐做法
|
||||||
|
|
||||||
|
1. **资源管理**:所有 disposable 对象都放入 `context.subscriptions`
|
||||||
|
2. **延迟激活**:使用 `onStartupFinished` 而不是 `*`
|
||||||
|
3. **异步操作**:使用 `async/await` 处理异步操作
|
||||||
|
4. **错误处理**:捕获异常并给用户友好提示
|
||||||
|
5. **类型安全**:充分利用 TypeScript 类型系统
|
||||||
|
6. **状态持久化**:使用 `globalState`/`workspaceState` 保存状态
|
||||||
|
7. **敏感信息**:使用 `secrets` API 存储 Token、密码等
|
||||||
|
|
||||||
|
### ❌ 避免做法
|
||||||
|
|
||||||
|
1. 不要在 `activate` 中执行耗时操作
|
||||||
|
2. 不要忘记清理资源(监听器、Webview 等)
|
||||||
|
3. 不要在 Webview 中直接访问文件系统
|
||||||
|
4. 不要在配置中存储敏感信息
|
||||||
|
5. 不要阻塞主线程(使用 Worker 或异步操作)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 参考资源
|
||||||
|
|
||||||
|
- [VS Code Extension API 官方文档](https://code.visualstudio.com/api)
|
||||||
|
- [Extension Samples](https://github.com/microsoft/vscode-extension-samples)
|
||||||
|
- [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
|
||||||
45
docs/code-changes-feature.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# 代码变更审查功能
|
||||||
|
|
||||||
|
## 功能概述
|
||||||
|
|
||||||
|
AI 修改文件后,会在输入框上方显示"代码变更"面板,用户可以查看所有修改并选择采纳或拒绝。
|
||||||
|
|
||||||
|
## 核心文件
|
||||||
|
|
||||||
|
### 1. 数据结构
|
||||||
|
- `src/types/fileChanges.ts` - 变更数据类型定义
|
||||||
|
|
||||||
|
### 2. 服务层
|
||||||
|
- `src/services/changeTracker.ts` - 变更追踪服务(单例)
|
||||||
|
- `trackChange()` - 记录文件变更
|
||||||
|
- `acceptChange()` - 采纳变更(保存文件)
|
||||||
|
- `rejectChange()` - 拒绝变更(恢复旧内容)
|
||||||
|
|
||||||
|
### 3. UI 组件
|
||||||
|
- `src/views/changePanel.ts` - 变更面板 UI
|
||||||
|
- `src/utils/diffRenderer.ts` - Diff 可视化渲染
|
||||||
|
|
||||||
|
### 4. 集成点
|
||||||
|
- `src/utils/messageHandler.ts` - 消息处理
|
||||||
|
- `trackFileChange()` - 记录变更
|
||||||
|
- `handleAcceptChange()` - 处理采纳
|
||||||
|
- `handleRejectChange()` - 处理拒绝
|
||||||
|
- `sendChangesToWebview()` - 发送变更到前端
|
||||||
|
|
||||||
|
- `src/services/toolExecutor.ts` - 工具执行器
|
||||||
|
- 在 `executeFileWrite()` 中记录变更
|
||||||
|
|
||||||
|
## 使用流程
|
||||||
|
|
||||||
|
1. **开始对话** - 调用 `startChangeSession(sessionId)`
|
||||||
|
2. **修改文件** - 自动调用 `trackFileChange()`
|
||||||
|
3. **对话结束** - 调用 `sendChangesToWebview()` 显示变更面板
|
||||||
|
4. **用户操作** - 点击采纳/拒绝按钮
|
||||||
|
5. **处理结果** - 保存或恢复文件内容
|
||||||
|
|
||||||
|
## 待完成工作
|
||||||
|
|
||||||
|
1. 在 ICHelperPanel 中集成消息处理(监听 acceptChange/rejectChange 命令)
|
||||||
|
2. 在对话结束时调用 `sendChangesToWebview()`
|
||||||
|
3. 在 Webview 中实现变更列表的动态渲染
|
||||||
|
4. 处理前端的采纳/拒绝响应
|
||||||
42
docs/code-to-chat-feature.md
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# 代码快速添加到对话功能
|
||||||
|
|
||||||
|
## 功能说明
|
||||||
|
|
||||||
|
选中代码后,通过右键菜单/小灯泡/快捷键(Ctrl+Shift+I),将代码作为上下文添加到聊天面板输入框上方。
|
||||||
|
|
||||||
|
## 实现方式
|
||||||
|
|
||||||
|
### 1. Code Action Provider
|
||||||
|
`src/providers/codeActionProvider.ts` - 提供小灯泡菜单选项
|
||||||
|
|
||||||
|
### 2. 命令注册
|
||||||
|
`src/extension.ts` - 注册 `ic-coder.addCodeToChat` 命令,发送消息到 webview
|
||||||
|
|
||||||
|
### 3. 全局引用
|
||||||
|
`src/panels/ICHelperPanel.ts` - 保存 panel 到 `(global as any).currentICHelperPanel`
|
||||||
|
|
||||||
|
### 4. 上下文显示
|
||||||
|
`src/views/contextDisplay.ts` - 添加 `code` 类型支持和 `addCodeContext` 消息处理
|
||||||
|
|
||||||
|
### 5. 配置
|
||||||
|
`package.json` - 配置命令、右键菜单、快捷键
|
||||||
|
|
||||||
|
## 用户体验
|
||||||
|
|
||||||
|
1. 选中代码
|
||||||
|
2. 右键/小灯泡/Ctrl+Shift+I
|
||||||
|
3. 代码显示为上下文项:`文件名.v:10-25` 📄
|
||||||
|
4. 输入问题发送(代码自动作为上下文)
|
||||||
|
|
||||||
|
## 数据结构
|
||||||
|
|
||||||
|
代码上下文存储为 JSON:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"fileName": "路径",
|
||||||
|
"startLine": 10,
|
||||||
|
"endLine": 25,
|
||||||
|
"code": "代码内容",
|
||||||
|
"languageId": "verilog"
|
||||||
|
}
|
||||||
|
```
|
||||||
294
docs/delete-file-confirmation.md
Normal file
@ -0,0 +1,294 @@
|
|||||||
|
# 删除文件确认功能实现文档
|
||||||
|
|
||||||
|
## 1. 功能概述
|
||||||
|
|
||||||
|
在 AI 返回删除文件命令时,前端拦截并弹出确认对话框,用户确认后才执行删除操作。
|
||||||
|
|
||||||
|
## 2. 架构设计
|
||||||
|
|
||||||
|
### 2.1 消息流程
|
||||||
|
|
||||||
|
```
|
||||||
|
AI 后端 → 删除文件工具调用 → 前端拦截 → 用户确认对话框
|
||||||
|
↓
|
||||||
|
确定/取消
|
||||||
|
↓
|
||||||
|
执行删除/返回取消结果
|
||||||
|
↓
|
||||||
|
返回 TOOL_EXECUTION_RESULT
|
||||||
|
↓
|
||||||
|
AI 后端
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 关键原则
|
||||||
|
|
||||||
|
**前端必须返回结果**:无论用户选择什么,前端都必须向后端返回 `TOOL_EXECUTION_RESULT`,否则后端会等待超时。
|
||||||
|
|
||||||
|
## 3. 实现方案
|
||||||
|
|
||||||
|
### 3.1 修改位置
|
||||||
|
|
||||||
|
文件:`src/utils/messageHandler.ts`
|
||||||
|
|
||||||
|
在处理工具调用的函数中,找到删除文件的工具处理逻辑。
|
||||||
|
|
||||||
|
### 3.2 核心代码实现
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 处理删除文件工具调用(带用户确认)
|
||||||
|
*/
|
||||||
|
async function handleDeleteFileTool(
|
||||||
|
toolCall: any,
|
||||||
|
panel: vscode.WebviewPanel
|
||||||
|
): Promise<ToolExecutionResult> {
|
||||||
|
const filePath = toolCall.arguments.filePath; // 根据实际参数名调整
|
||||||
|
|
||||||
|
// 弹出确认对话框
|
||||||
|
const confirmed = await vscode.window.showWarningMessage(
|
||||||
|
`确定要删除文件吗?\n\n${filePath}`,
|
||||||
|
{
|
||||||
|
modal: true, // 模态对话框,阻止其他操作
|
||||||
|
detail: '此操作不可撤销'
|
||||||
|
},
|
||||||
|
'确定删除',
|
||||||
|
'取消'
|
||||||
|
);
|
||||||
|
|
||||||
|
// 用户确认删除
|
||||||
|
if (confirmed === '确定删除') {
|
||||||
|
try {
|
||||||
|
// 执行删除操作
|
||||||
|
const uri = vscode.Uri.file(filePath);
|
||||||
|
await vscode.workspace.fs.delete(uri, {
|
||||||
|
recursive: false, // 如果是目录需要设置为 true
|
||||||
|
useTrash: true // 移到回收站而非永久删除(推荐)
|
||||||
|
});
|
||||||
|
|
||||||
|
// 返回成功结果
|
||||||
|
return {
|
||||||
|
type: 'TOOL_EXECUTION_RESULT',
|
||||||
|
toolCallId: toolCall.id,
|
||||||
|
result: JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
message: `文件已删除: ${filePath}`
|
||||||
|
})
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
// 删除失败
|
||||||
|
return {
|
||||||
|
type: 'TOOL_EXECUTION_RESULT',
|
||||||
|
toolCallId: toolCall.id,
|
||||||
|
result: JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
error: `删除失败: ${error.message}`
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户取消或关闭对话框
|
||||||
|
return {
|
||||||
|
type: 'TOOL_EXECUTION_RESULT',
|
||||||
|
toolCallId: toolCall.id,
|
||||||
|
result: JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
message: '用户取消了删除操作'
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 集成到消息处理流程
|
||||||
|
|
||||||
|
在 `messageHandler.ts` 的工具调用处理逻辑中:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 示例:在处理工具调用的地方
|
||||||
|
async function handleToolCall(toolCall: any, panel: vscode.WebviewPanel) {
|
||||||
|
switch (toolCall.name) {
|
||||||
|
case 'deleteFile': // 根据实际工具名称调整
|
||||||
|
return await handleDeleteFileTool(toolCall, panel);
|
||||||
|
|
||||||
|
case 'deleteDirectory': // 如果有删除目录的工具
|
||||||
|
return await handleDeleteDirectoryTool(toolCall, panel);
|
||||||
|
|
||||||
|
// ... 其他工具
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. 用户体验优化
|
||||||
|
|
||||||
|
### 4.1 对话框样式
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const confirmed = await vscode.window.showWarningMessage(
|
||||||
|
`确定要删除文件吗?\n\n📄 ${path.basename(filePath)}\n📁 ${path.dirname(filePath)}`,
|
||||||
|
{
|
||||||
|
modal: true,
|
||||||
|
detail: '⚠️ 文件将被移到回收站,可以恢复'
|
||||||
|
},
|
||||||
|
'确定删除',
|
||||||
|
'取消'
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 批量删除优化
|
||||||
|
|
||||||
|
如果 AI 一次返回多个删除操作:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 方案 1:逐个确认
|
||||||
|
for (const file of filesToDelete) {
|
||||||
|
await handleDeleteFileTool(file, panel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 方案 2:批量确认(推荐)
|
||||||
|
const confirmed = await vscode.window.showWarningMessage(
|
||||||
|
`确定要删除以下 ${filesToDelete.length} 个文件吗?\n\n${filesToDelete.join('\n')}`,
|
||||||
|
{ modal: true },
|
||||||
|
'全部删除',
|
||||||
|
'取消'
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 5. 安全考虑
|
||||||
|
|
||||||
|
### 5.1 使用回收站
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await vscode.workspace.fs.delete(uri, {
|
||||||
|
useTrash: true // 移到回收站,可恢复
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 路径验证
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 防止删除工作区外的文件
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders) {
|
||||||
|
return { success: false, error: '未打开工作区' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const isInWorkspace = workspaceFolders.some(folder =>
|
||||||
|
filePath.startsWith(folder.uri.fsPath)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isInWorkspace) {
|
||||||
|
return { success: false, error: '只能删除工作区内的文件' };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.3 敏感文件保护
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const protectedFiles = [
|
||||||
|
'package.json',
|
||||||
|
'tsconfig.json',
|
||||||
|
'.git',
|
||||||
|
'node_modules'
|
||||||
|
];
|
||||||
|
|
||||||
|
const fileName = path.basename(filePath);
|
||||||
|
if (protectedFiles.includes(fileName)) {
|
||||||
|
vscode.window.showErrorMessage(`不允许删除系统文件: ${fileName}`);
|
||||||
|
return { success: false, error: '受保护的文件' };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6. 错误处理
|
||||||
|
|
||||||
|
### 6.1 常见错误
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
try {
|
||||||
|
await vscode.workspace.fs.delete(uri, { useTrash: true });
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'FileNotFound') {
|
||||||
|
return { success: false, error: '文件不存在' };
|
||||||
|
}
|
||||||
|
if (error.code === 'NoPermissions') {
|
||||||
|
return { success: false, error: '没有删除权限' };
|
||||||
|
}
|
||||||
|
return { success: false, error: error.message };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7. 测试场景
|
||||||
|
|
||||||
|
### 7.1 基本测试
|
||||||
|
|
||||||
|
- [ ] 用户点击"确定删除" → 文件被删除
|
||||||
|
- [ ] 用户点击"取消" → 文件保留,返回取消消息
|
||||||
|
- [ ] 用户关闭对话框 → 文件保留,返回取消消息
|
||||||
|
- [ ] 文件不存在 → 返回错误消息
|
||||||
|
- [ ] 没有删除权限 → 返回错误消息
|
||||||
|
|
||||||
|
### 7.2 边界测试
|
||||||
|
|
||||||
|
- [ ] 删除工作区外的文件 → 拒绝
|
||||||
|
- [ ] 删除受保护文件 → 拒绝
|
||||||
|
- [ ] 批量删除 → 正确处理
|
||||||
|
- [ ] 后端收到取消消息后继续对话 → 流程正常
|
||||||
|
|
||||||
|
## 8. 配置选项(可选)
|
||||||
|
|
||||||
|
可以添加用户设置来控制行为:
|
||||||
|
|
||||||
|
```json
|
||||||
|
// package.json
|
||||||
|
"configuration": {
|
||||||
|
"properties": {
|
||||||
|
"ic-coder.confirmDelete": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"description": "删除文件前是否需要确认"
|
||||||
|
},
|
||||||
|
"ic-coder.useTrash": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true,
|
||||||
|
"description": "删除文件时移到回收站而非永久删除"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
读取配置:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const config = vscode.workspace.getConfiguration('ic-coder');
|
||||||
|
const needConfirm = config.get<boolean>('confirmDelete', true);
|
||||||
|
const useTrash = config.get<boolean>('useTrash', true);
|
||||||
|
|
||||||
|
if (needConfirm) {
|
||||||
|
// 弹出确认对话框
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 9. 总结
|
||||||
|
|
||||||
|
### 9.1 后端是否需要修改?
|
||||||
|
|
||||||
|
**不需要**。后端继续返回删除工具调用,前端负责:
|
||||||
|
1. 拦截工具调用
|
||||||
|
2. 弹出确认对话框
|
||||||
|
3. 执行或取消删除
|
||||||
|
4. **必须返回结果给后端**
|
||||||
|
|
||||||
|
### 9.2 关键要点
|
||||||
|
|
||||||
|
- ✅ 前端必须返回 `TOOL_EXECUTION_RESULT`
|
||||||
|
- ✅ 使用 `useTrash: true` 提高安全性
|
||||||
|
- ✅ 验证文件路径在工作区内
|
||||||
|
- ✅ 保护敏感文件
|
||||||
|
- ✅ 提供清晰的错误消息
|
||||||
|
|
||||||
|
### 9.3 下一步
|
||||||
|
|
||||||
|
1. 在 `messageHandler.ts` 中找到工具调用处理逻辑
|
||||||
|
2. 实现 `handleDeleteFileTool` 函数
|
||||||
|
3. 集成到现有流程
|
||||||
|
4. 测试各种场景
|
||||||
|
5. 考虑添加用户配置选项
|
||||||
50
docs/integration-guide.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# 代码变更审查功能 - 使用说明
|
||||||
|
|
||||||
|
## 功能说明
|
||||||
|
|
||||||
|
AI 修改文件后,会在输入框上方显示"代码变更"面板,用户可以:
|
||||||
|
- 查看所有修改的文件列表
|
||||||
|
- 点击文件查看 diff 对比
|
||||||
|
- 采纳变更(保存文件)
|
||||||
|
- 拒绝变更(恢复旧内容)
|
||||||
|
|
||||||
|
## 已完成的集成
|
||||||
|
|
||||||
|
### 1. 后端集成
|
||||||
|
- ✅ 在 `ICHelperPanel.ts` 中添加了消息监听(acceptChange/rejectChange)
|
||||||
|
- ✅ 在发送消息时启动变更追踪会话
|
||||||
|
- ✅ 在文件操作时自动记录变更(messageHandler.ts、toolExecutor.ts)
|
||||||
|
|
||||||
|
### 2. 前端集成
|
||||||
|
- ✅ 在 `webviewContent.ts` 中添加了消息处理(showChanges/changeAccepted/changeRejected)
|
||||||
|
- ✅ 在 `changePanel.ts` 中实现了完整的 UI 交互逻辑
|
||||||
|
|
||||||
|
### 3. 核心功能
|
||||||
|
- ✅ 变更追踪服务(changeTracker.ts)
|
||||||
|
- ✅ Diff 可视化渲染(diffRenderer.ts)
|
||||||
|
- ✅ 采纳/拒绝变更逻辑
|
||||||
|
|
||||||
|
## 待完成工作
|
||||||
|
|
||||||
|
需要在对话结束时调用 `sendChangesToWebview(panel)` 来显示变更面板。
|
||||||
|
|
||||||
|
建议在以下位置添加:
|
||||||
|
1. 在 `handleUserMessage` 函数中,对话流结束时
|
||||||
|
2. 或在 `dialogManager` 的对话完成回调中
|
||||||
|
|
||||||
|
示例代码:
|
||||||
|
```typescript
|
||||||
|
// 对话结束时
|
||||||
|
import { sendChangesToWebview } from '../utils/messageHandler';
|
||||||
|
|
||||||
|
// 在对话完成的地方调用
|
||||||
|
sendChangesToWebview(panel);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 测试步骤
|
||||||
|
|
||||||
|
1. 启动插件(F5)
|
||||||
|
2. 发送消息让 AI 修改文件
|
||||||
|
3. 对话结束后,输入框上方应显示"代码变更"面板
|
||||||
|
4. 点击文件查看 diff
|
||||||
|
5. 点击"采纳"或"拒绝"按钮测试功能
|
||||||
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
|
||||||
161
docs/personal-rules-mvp-requirements.md
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
# 个人规则功能需求文档(方案 A:本地 `.md` 注入)
|
||||||
|
|
||||||
|
## 1. 文档目标
|
||||||
|
|
||||||
|
在不改动现有核心对话模式的前提下,实现“个人规则(Personal Rules)”能力:
|
||||||
|
用户可在插件内维护个人规则文本,插件保存到本地 `.md` 文件;每次发起对话时自动读取并随请求传递给后端,由后端注入模型上下文,影响回答风格与行为。
|
||||||
|
|
||||||
|
## 2. 范围定义
|
||||||
|
|
||||||
|
### 2.1 本期范围(MVP)
|
||||||
|
|
||||||
|
1. 支持用户编辑、保存、启用/停用个人规则。
|
||||||
|
2. 本地落盘为 `.md` 文件。
|
||||||
|
3. 发消息时自动加载规则并传给后端。
|
||||||
|
4. 后端接收结构化字段并注入提示词。
|
||||||
|
5. 基础异常处理和可观测提示。
|
||||||
|
|
||||||
|
### 2.2 非本期范围
|
||||||
|
|
||||||
|
1. 云端同步、多设备同步。
|
||||||
|
2. 规则版本历史/回滚。
|
||||||
|
3. 多规则集合管理(仅单份个人规则文本)。
|
||||||
|
4. 团队共享规则。
|
||||||
|
|
||||||
|
## 3. 术语与核心概念
|
||||||
|
|
||||||
|
1. `Personal Rules`:用户个人偏好与约束文本。
|
||||||
|
2. `Rules File`:本地规则文件,Markdown 格式。
|
||||||
|
3. `Rules Enabled`:规则开关;关闭时不注入。
|
||||||
|
4. `Rules Injection`:请求时将规则传后端并参与模型上下文构建。
|
||||||
|
|
||||||
|
## 4. 用户故事
|
||||||
|
|
||||||
|
1. 作为用户,我希望在插件里写下“回答风格、代码习惯、语言偏好”等个人规则。
|
||||||
|
2. 作为用户,我希望规则保存在本地可见文件中。
|
||||||
|
3. 作为用户,我希望发消息时自动生效,无需每次重复输入。
|
||||||
|
4. 作为用户,我希望可以一键关闭规则,临时不生效。
|
||||||
|
|
||||||
|
## 5. 功能需求(前端/Webview + 扩展端)
|
||||||
|
|
||||||
|
### 5.1 规则管理界面
|
||||||
|
|
||||||
|
1. 提供“个人规则”入口。
|
||||||
|
2. 提供多行编辑框(显示当前规则内容)。
|
||||||
|
3. 提供“保存”按钮。
|
||||||
|
4. 提供“启用/停用”开关。
|
||||||
|
5. 显示当前状态:
|
||||||
|
6. 规则是否启用。
|
||||||
|
7. 规则字数/长度。
|
||||||
|
8. 最近保存时间(可选)。
|
||||||
|
|
||||||
|
### 5.2 本地文件存储
|
||||||
|
|
||||||
|
1. 规则内容保存到本地 `.md`。
|
||||||
|
2. 推荐文件名:`personal-rules.md`。
|
||||||
|
3. 推荐路径(优先):插件全局存储目录下固定子路径。
|
||||||
|
4. 文件不存在时可自动创建。
|
||||||
|
5. 用户可通过“打开规则文件”查看(可选)。
|
||||||
|
|
||||||
|
### 5.3 对话发送前处理
|
||||||
|
|
||||||
|
1. 用户点击发送消息。
|
||||||
|
2. 扩展端检查规则开关:
|
||||||
|
3. 关闭:不读取规则,不传后端。
|
||||||
|
4. 开启:读取 `.md` 内容。
|
||||||
|
5. 读取成功且非空时,将规则文本附加到对话请求结构化字段。
|
||||||
|
6. 读取失败时:提示告警,但不阻断正常对话。
|
||||||
|
|
||||||
|
### 5.4 限制与防护
|
||||||
|
|
||||||
|
1. 规则长度上限(例如 4000 字符,可配置)。
|
||||||
|
2. 超限时保存被拒绝,提示用户缩短。
|
||||||
|
3. 空白内容视为“无规则”。
|
||||||
|
4. 不允许二进制或非文本写入。
|
||||||
|
|
||||||
|
## 6. 功能需求(后端)
|
||||||
|
|
||||||
|
### 6.1 请求协议扩展
|
||||||
|
|
||||||
|
在现有对话请求结构中增加字段:
|
||||||
|
|
||||||
|
1. `personalRules`:字符串,可选。
|
||||||
|
2. `rulesEnabled`:布尔,可选(便于追踪)。
|
||||||
|
3. `rulesMeta`:可选元信息(长度、来源)。
|
||||||
|
|
||||||
|
### 6.2 注入策略
|
||||||
|
|
||||||
|
1. 后端收到 `personalRules` 后,将其注入系统提示层(而非用户消息层)。
|
||||||
|
2. 注入顺序建议:
|
||||||
|
3. 系统安全与平台策略。
|
||||||
|
4. 产品默认系统提示。
|
||||||
|
5. 用户个人规则。
|
||||||
|
6. 用户输入。
|
||||||
|
7. 若 `personalRules` 为空或开关关闭,则跳过注入。
|
||||||
|
|
||||||
|
### 6.3 风险控制
|
||||||
|
|
||||||
|
1. 规则文本不允许覆盖平台安全策略。
|
||||||
|
2. 记录本次是否注入规则(日志字段即可)。
|
||||||
|
3. 异常不应导致整次对话失败(可降级为无规则对话)。
|
||||||
|
|
||||||
|
## 7. 前后端对接设计
|
||||||
|
|
||||||
|
### 7.1 消息链路
|
||||||
|
|
||||||
|
1. Webview 触发 `sendMessage`。
|
||||||
|
2. 扩展端 `messageHandler` 统一处理发送。
|
||||||
|
3. `messageHandler` 在调用 `dialogService.sendMessage` 前读取个人规则。
|
||||||
|
4. `dialogService` 组装 `DialogRequest`,带上 `personalRules`。
|
||||||
|
5. `sseHandler` 发起流式请求。
|
||||||
|
6. 后端注入规则后进入模型推理。
|
||||||
|
7. 正常走现有 SSE 回传流程。
|
||||||
|
|
||||||
|
### 7.2 职责边界
|
||||||
|
|
||||||
|
1. Webview:展示与编辑,不直接拼接最终请求。
|
||||||
|
2. 扩展端:规则文件读写、开关状态管理、请求组装。
|
||||||
|
3. 后端:规则注入、优先级控制、审计日志。
|
||||||
|
|
||||||
|
## 8. 数据与状态设计
|
||||||
|
|
||||||
|
### 8.1 本地文件
|
||||||
|
|
||||||
|
1. 文件格式:Markdown 纯文本。
|
||||||
|
2. 内容约定:无强制模板,允许自由文本。
|
||||||
|
3. 编码:UTF-8。
|
||||||
|
|
||||||
|
### 8.2 本地配置状态
|
||||||
|
|
||||||
|
1. `personalRulesEnabled`:是否启用。
|
||||||
|
2. `personalRulesPath`:规则文件路径(可固定也可配置)。
|
||||||
|
3. `lastSavedAt`:最近保存时间(可选)。
|
||||||
|
|
||||||
|
## 9. 异常与降级
|
||||||
|
|
||||||
|
1. 文件不存在:自动创建空文件,视为无规则。
|
||||||
|
2. 文件读取失败:弹出提示,继续无规则发送。
|
||||||
|
3. 文件写入失败:保存失败提示,不更新状态。
|
||||||
|
4. 后端字段不识别:请求兼容,后端忽略新字段。
|
||||||
|
5. 后端注入失败:降级为普通对话,记录日志。
|
||||||
|
|
||||||
|
## 10. 安全与合规要求
|
||||||
|
|
||||||
|
1. 个人规则属于用户本地数据,不主动上传除非发起对话。
|
||||||
|
2. 日志中避免完整打印规则正文(最多打印长度和哈希)。
|
||||||
|
3. 后端注入时必须确保平台安全策略优先级更高。
|
||||||
|
|
||||||
|
## 11. 验收标准(UAT)
|
||||||
|
|
||||||
|
1. 用户保存规则后,本地存在 `personal-rules.md` 且内容一致。
|
||||||
|
2. 开启规则发送消息时,请求中可观测到 `personalRules` 字段。
|
||||||
|
3. 关闭规则发送消息时,请求中不含该字段或为空。
|
||||||
|
4. 规则文件损坏/读取失败时,不影响正常聊天。
|
||||||
|
5. 超过长度上限时,前端保存被拒绝且提示明确。
|
||||||
|
6. 后端日志可确认“本次是否注入个人规则”。
|
||||||
|
|
||||||
|
## 12. 迭代建议(下一阶段)
|
||||||
|
|
||||||
|
1. 规则模板(代码风格、语言风格、测试偏好)。
|
||||||
|
2. 项目规则与个人规则合并策略。
|
||||||
|
3. 云端同步(按 `userId`),多端一致。
|
||||||
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` - 侧边栏加载时检查
|
||||||
|
|
||||||
55
docs/webpack-optimization.md
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# Webpack 打包优化说明
|
||||||
|
|
||||||
|
## 优化内容
|
||||||
|
|
||||||
|
### 1. 自动模式切换
|
||||||
|
- 开发模式:保持源码可读性
|
||||||
|
- 生产模式:自动压缩代码
|
||||||
|
|
||||||
|
### 2. 性能优化
|
||||||
|
- **Tree Shaking**:移除未使用的代码
|
||||||
|
- **transpileOnly**:跳过类型检查,加快编译速度
|
||||||
|
- **自动清理**:每次打包自动删除旧文件
|
||||||
|
|
||||||
|
### 3. 体积监控
|
||||||
|
- 单文件超过 2MB 会发出警告
|
||||||
|
- 帮助及时发现打包体积问题
|
||||||
|
|
||||||
|
## 使用方法
|
||||||
|
|
||||||
|
### 开发模式
|
||||||
|
```bash
|
||||||
|
# 编译(不压缩)
|
||||||
|
pnpm run compile
|
||||||
|
|
||||||
|
# 监听模式(自动重新编译)
|
||||||
|
pnpm run watch
|
||||||
|
```
|
||||||
|
|
||||||
|
### 生产模式
|
||||||
|
```bash
|
||||||
|
# Windows
|
||||||
|
set NODE_ENV=production && pnpm run package
|
||||||
|
|
||||||
|
# macOS/Linux
|
||||||
|
NODE_ENV=production pnpm run package
|
||||||
|
```
|
||||||
|
|
||||||
|
## 打包结果
|
||||||
|
|
||||||
|
- **输出目录**:`dist/`
|
||||||
|
- **入口文件**:`dist/extension.js`
|
||||||
|
- **静态资源**:`dist/assets/`
|
||||||
|
|
||||||
|
## 性能对比
|
||||||
|
|
||||||
|
| 模式 | 体积 | 编译速度 | Source Map |
|
||||||
|
|------|------|----------|------------|
|
||||||
|
| 开发 | 较大 | 快 | 完整 |
|
||||||
|
| 生产 | 小 | 较慢 | 隐藏 |
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
1. 开发时使用 `pnpm run watch`,修改代码自动重新编译
|
||||||
|
2. 发布前必须使用生产模式打包
|
||||||
|
3. 如果打包体积超过 2MB,检查是否引入了不必要的依赖
|
||||||
783
docs/插件试用用户功能实现方案.md
Normal file
@ -0,0 +1,783 @@
|
|||||||
|
# 插件试用用户功能实现方案
|
||||||
|
|
||||||
|
## 1. 方案概述
|
||||||
|
|
||||||
|
**核心思路:**
|
||||||
|
- Web 登录成功后只返回 token(保持现状)
|
||||||
|
- 插件调用 `getUserInfo(token)` 时,后端返回的数据里包含标识字段
|
||||||
|
- 前端根据该字段判断是否是插件试用用户
|
||||||
|
- 插件试用用户:显示欢迎弹窗,不显示邀请码弹窗
|
||||||
|
- 正式用户:显示邀请码弹窗(现有逻辑)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 后端需要做什么
|
||||||
|
|
||||||
|
### 2.1 在用户信息接口中添加字段
|
||||||
|
|
||||||
|
**接口:** `GET /system/user/getInfo`
|
||||||
|
|
||||||
|
**现有响应:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"userId": "xxx",
|
||||||
|
"username": "testuser",
|
||||||
|
"nickname": "测试用户",
|
||||||
|
"email": "test@example.com"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**新增字段:**
|
||||||
|
|
||||||
|
**方案 :添加 isPluginTrial 字段**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"userId": "xxx",
|
||||||
|
"username": "testuser",
|
||||||
|
"nickname": "测试用户",
|
||||||
|
"email": "test@example.com",
|
||||||
|
"isPluginTrial": true, // ← 新增:是否是插件试用用户
|
||||||
|
"pluginTrialExpiresAt": 1709654400000 // ← 新增:试用到期时间(毫秒时间戳)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 后端逻辑说明
|
||||||
|
|
||||||
|
**判断逻辑:**
|
||||||
|
```javascript
|
||||||
|
// 伪代码
|
||||||
|
function getUserInfo(userId) {
|
||||||
|
const user = db.users.findById(userId);
|
||||||
|
|
||||||
|
// 判断是否是插件试用用户(后端自己的逻辑)
|
||||||
|
const isPluginTrial = checkIfPluginTrialUser(user);
|
||||||
|
|
||||||
|
return {
|
||||||
|
userId: user.id,
|
||||||
|
username: user.username,
|
||||||
|
nickname: user.nickname,
|
||||||
|
email: user.email,
|
||||||
|
isPluginTrial: isPluginTrial,
|
||||||
|
pluginTrialExpiresAt: isPluginTrial ? user.trial_expires_at : null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Token 过期时间:**
|
||||||
|
- 插件试用用户:JWT Token 设置 15 天过期
|
||||||
|
- 正式用户:JWT Token 设置 30 天过期(或现有逻辑)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 前端需要修改的地方
|
||||||
|
|
||||||
|
### 3.1 修改 UserInfo 接口
|
||||||
|
|
||||||
|
**文件:** `src/services/userService.ts`
|
||||||
|
|
||||||
|
**现有接口:**
|
||||||
|
```typescript
|
||||||
|
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?: number;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**新增字段:**
|
||||||
|
```typescript
|
||||||
|
interface UserInfo {
|
||||||
|
// ... 现有字段
|
||||||
|
isPluginTrial?: boolean; // ← 新增:是否是插件试用用户
|
||||||
|
pluginTrialExpiresAt?: number; // ← 新增:试用到期时间(毫秒时间戳)
|
||||||
|
membership?: {
|
||||||
|
tierCode: string;
|
||||||
|
tierName: string;
|
||||||
|
tierLevel: number;
|
||||||
|
remainingDays?: number;
|
||||||
|
monthlyCredits?: number;
|
||||||
|
};
|
||||||
|
credits?: number;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 3.2 修改 onTokenReceived() 方法
|
||||||
|
|
||||||
|
**文件:** `src/services/userService.ts`
|
||||||
|
|
||||||
|
**现有代码位置:** 约 200 行左右
|
||||||
|
|
||||||
|
**修改内容:**
|
||||||
|
```typescript
|
||||||
|
async onTokenReceived(token: string) {
|
||||||
|
// 现有逻辑:并行获取三类信息
|
||||||
|
const [userInfo, membershipInfo, credits] = await Promise.all([
|
||||||
|
getUserInfo(token),
|
||||||
|
getMembershipInfo(token),
|
||||||
|
fetchBalanceWithToken(token)
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 合并数据
|
||||||
|
const fullUserInfo = {
|
||||||
|
...userInfo,
|
||||||
|
membership: membershipInfo,
|
||||||
|
credits: credits
|
||||||
|
};
|
||||||
|
|
||||||
|
// 保存到 globalState
|
||||||
|
await this.context.globalState.update('icCoderUserInfo', fullUserInfo);
|
||||||
|
|
||||||
|
// ========== 新增逻辑 ==========
|
||||||
|
// 判断是否是插件试用用户
|
||||||
|
if (fullUserInfo.isPluginTrial === true) {
|
||||||
|
// 插件试用用户:显示欢迎弹窗,不显示邀请码弹窗
|
||||||
|
await this.showWelcomePanel();
|
||||||
|
// 标记为已显示欢迎弹窗(避免重复显示)
|
||||||
|
await this.context.globalState.update('pluginTrialWelcomed', true);
|
||||||
|
} else {
|
||||||
|
// 正式用户:显示邀请码弹窗(现有逻辑)
|
||||||
|
await this.checkAndShowInvitationModal();
|
||||||
|
}
|
||||||
|
// ========== 新增逻辑结束 ==========
|
||||||
|
|
||||||
|
return fullUserInfo;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 3.3 新增欢迎弹窗面板
|
||||||
|
|
||||||
|
**新建文件:** `src/panels/WelcomePanel.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 欢迎引导面板
|
||||||
|
* 功能:插件试用用户首次登录显示使用教程
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export class WelcomePanel {
|
||||||
|
public static currentPanel: WelcomePanel | undefined;
|
||||||
|
private readonly _panel: vscode.WebviewPanel;
|
||||||
|
private _disposables: vscode.Disposable[] = [];
|
||||||
|
|
||||||
|
private constructor(panel: vscode.WebviewPanel) {
|
||||||
|
this._panel = panel;
|
||||||
|
this._panel.webview.html = this.getHtmlContent();
|
||||||
|
|
||||||
|
// 监听关闭事件
|
||||||
|
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static render(context: vscode.ExtensionContext) {
|
||||||
|
// 避免重复显示
|
||||||
|
if (WelcomePanel.currentPanel) {
|
||||||
|
WelcomePanel.currentPanel._panel.reveal(vscode.ViewColumn.One);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const panel = vscode.window.createWebviewPanel(
|
||||||
|
'icCoderWelcome',
|
||||||
|
'欢迎使用 IC Coder',
|
||||||
|
vscode.ViewColumn.One,
|
||||||
|
{
|
||||||
|
enableScripts: true,
|
||||||
|
retainContextWhenHidden: true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
WelcomePanel.currentPanel = new WelcomePanel(panel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHtmlContent(): string {
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>欢迎使用 IC Coder</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
padding: 40px;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
background-color: var(--vscode-editor-background);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: var(--vscode-textLink-foreground);
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.welcome-message {
|
||||||
|
font-size: 16px;
|
||||||
|
margin-bottom: 30px;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
}
|
||||||
|
.step {
|
||||||
|
margin: 20px 0;
|
||||||
|
padding: 20px;
|
||||||
|
background: var(--vscode-editor-inactiveSelectionBackground);
|
||||||
|
border-radius: 8px;
|
||||||
|
border-left: 4px solid var(--vscode-textLink-foreground);
|
||||||
|
}
|
||||||
|
.step h3 {
|
||||||
|
margin-top: 0;
|
||||||
|
color: var(--vscode-textLink-foreground);
|
||||||
|
}
|
||||||
|
.step p {
|
||||||
|
margin: 10px 0;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
padding: 12px 24px;
|
||||||
|
background: var(--vscode-button-background);
|
||||||
|
color: var(--vscode-button-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background: var(--vscode-button-hoverBackground);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>🎉 欢迎使用 IC Coder!</h1>
|
||||||
|
<p class="welcome-message">
|
||||||
|
您已成功激活 15 天试用期,让我们开始探索 IC Coder 的强大功能吧!
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="step">
|
||||||
|
<h3>📝 步骤 1:打开聊天面板</h3>
|
||||||
|
<p>点击侧边栏的 IC Coder 图标,或使用命令面板搜索 "IC Coder: Open Chat"</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="step">
|
||||||
|
<h3>💬 步骤 2:输入您的需求</h3>
|
||||||
|
<p>描述您想要生成的 Verilog 代码或需要帮助的问题,AI 将为您提供专业的解决方案</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="step">
|
||||||
|
<h3>🔬 步骤 3:运行仿真</h3>
|
||||||
|
<p>使用 "生成 VCD" 命令运行 iverilog 仿真,并通过波形查看器查看仿真结果</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button class="button" onclick="close()">开始使用</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function close() {
|
||||||
|
// 通知 VS Code 关闭面板
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
public dispose() {
|
||||||
|
WelcomePanel.currentPanel = undefined;
|
||||||
|
this._panel.dispose();
|
||||||
|
while (this._disposables.length) {
|
||||||
|
const disposable = this._disposables.pop();
|
||||||
|
if (disposable) {
|
||||||
|
disposable.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 3.4 新增过期提醒面板
|
||||||
|
|
||||||
|
**新建文件:** `src/panels/ExpiredPanel.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 试用期到期提醒面板
|
||||||
|
* 功能:试用期到期时显示续费提示
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export class ExpiredPanel {
|
||||||
|
public static render() {
|
||||||
|
const panel = vscode.window.createWebviewPanel(
|
||||||
|
'icCoderExpired',
|
||||||
|
'试用期已到期',
|
||||||
|
vscode.ViewColumn.One,
|
||||||
|
{ enableScripts: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
panel.webview.html = this.getHtmlContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getHtmlContent(): string {
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
padding: 60px 40px;
|
||||||
|
text-align: center;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
background-color: var(--vscode-editor-background);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: var(--vscode-errorForeground);
|
||||||
|
font-size: 28px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.6;
|
||||||
|
margin: 15px 0;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
padding: 12px 30px;
|
||||||
|
background: var(--vscode-button-background);
|
||||||
|
color: var(--vscode-button-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 16px;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background: var(--vscode-button-hoverBackground);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>⏰ 您的试用期已到期</h1>
|
||||||
|
<p>感谢您使用 IC Coder!您的 15 天试用期已结束。</p>
|
||||||
|
<p>如需继续使用,请联系我们获取正式版本。</p>
|
||||||
|
|
||||||
|
<button class="button" onclick="contact()">联系我们</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function contact() {
|
||||||
|
// 可以打开联系页面或发送邮件
|
||||||
|
window.open('https://iccoder.com/contact', '_blank');
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 3.5 新增过期检测服务
|
||||||
|
|
||||||
|
**新建文件:** `src/services/trialExpirationService.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
/**
|
||||||
|
* 试用期过期检测服务
|
||||||
|
* 功能:检查插件试用用户是否过期
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import { getUserInfo } from './userService';
|
||||||
|
import { ExpiredPanel } from '../panels/ExpiredPanel';
|
||||||
|
|
||||||
|
export class TrialExpirationService {
|
||||||
|
private context: vscode.ExtensionContext;
|
||||||
|
|
||||||
|
constructor(context: vscode.ExtensionContext) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否过期
|
||||||
|
* @returns true=已过期,false=未过期
|
||||||
|
*/
|
||||||
|
public async checkExpiration(): Promise<boolean> {
|
||||||
|
const userInfo = await getUserInfo();
|
||||||
|
|
||||||
|
// 不是插件试用用户,不需要检查
|
||||||
|
if (!userInfo?.isPluginTrial) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 没有过期时间,不检查
|
||||||
|
if (!userInfo.pluginTrialExpiresAt) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否过期
|
||||||
|
const now = Date.now();
|
||||||
|
if (now >= userInfo.pluginTrialExpiresAt) {
|
||||||
|
// 已过期
|
||||||
|
await this.handleExpired();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理过期逻辑
|
||||||
|
*/
|
||||||
|
private async handleExpired(): Promise<void> {
|
||||||
|
// 显示过期弹窗
|
||||||
|
ExpiredPanel.render();
|
||||||
|
|
||||||
|
// 清除本地数据(可选)
|
||||||
|
// await this.context.globalState.update('icCoderUserInfo', undefined);
|
||||||
|
// await this.context.globalState.update('icCoderSessions', undefined);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 3.6 在消息发送前检查过期
|
||||||
|
|
||||||
|
**文件:** `src/utils/messageHandler.ts`
|
||||||
|
|
||||||
|
**修改位置:** 在发送消息给后端之前添加过期检查
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { TrialExpirationService } from '../services/trialExpirationService';
|
||||||
|
|
||||||
|
// 在 handleUserMessage 或类似的消息处理函数中添加
|
||||||
|
async function handleUserMessage(message: string, context: vscode.ExtensionContext) {
|
||||||
|
// ========== 新增:检查试用期是否过期 ==========
|
||||||
|
const trialService = new TrialExpirationService(context);
|
||||||
|
const isExpired = await trialService.checkExpiration();
|
||||||
|
|
||||||
|
if (isExpired) {
|
||||||
|
// 已过期,禁止使用
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: '您的试用期已到期,请联系我们获取正式版本'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// ========== 新增结束 ==========
|
||||||
|
|
||||||
|
// 现有的消息处理逻辑
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 完整的实现流程
|
||||||
|
|
||||||
|
### 4.1 登录流程(带过期检查)
|
||||||
|
|
||||||
|
```
|
||||||
|
1. 用户点击登录
|
||||||
|
↓
|
||||||
|
2. 打开浏览器,Web 端登录
|
||||||
|
↓
|
||||||
|
3. 重定向回插件:http://localhost:{port}/callback?token={token}
|
||||||
|
↓
|
||||||
|
4. 插件调用 onTokenReceived(token)
|
||||||
|
↓
|
||||||
|
5. 并行获取:getUserInfo + getMembershipInfo + Credits
|
||||||
|
↓
|
||||||
|
6. 后端返回 userInfo(包含 isPluginTrial 和 pluginTrialExpiresAt)
|
||||||
|
↓
|
||||||
|
7. 判断 isPluginTrial === true?
|
||||||
|
├─ 是:显示欢迎弹窗,不显示邀请码弹窗
|
||||||
|
└─ 否:显示邀请码弹窗(现有逻辑)
|
||||||
|
↓
|
||||||
|
8. 保存用户信息到 globalState
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 使用功能时的过期检查
|
||||||
|
|
||||||
|
```
|
||||||
|
1. 用户发送消息/使用功能
|
||||||
|
↓
|
||||||
|
2. 调用 trialService.checkExpiration()
|
||||||
|
↓
|
||||||
|
3. 获取 userInfo,检查 isPluginTrial
|
||||||
|
↓
|
||||||
|
4. 如果是插件试用用户,检查 Date.now() >= pluginTrialExpiresAt?
|
||||||
|
├─ 是:显示过期弹窗,禁止使用,返回 true
|
||||||
|
└─ 否:允许使用,返回 false
|
||||||
|
↓
|
||||||
|
5. 继续正常的消息处理流程
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. ⚠️ 潜在 Bug 和注意事项
|
||||||
|
|
||||||
|
### 5.1 时间同步问题
|
||||||
|
|
||||||
|
**问题:** 前端使用 `Date.now()` 判断过期,如果用户本地时间不准确会导致误判
|
||||||
|
|
||||||
|
**场景:**
|
||||||
|
- 用户本地时间快了 1 天 → 提前显示过期弹窗
|
||||||
|
- 用户本地时间慢了 1 天 → 过期后仍可使用
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
```typescript
|
||||||
|
// 方案 1:每次使用前调用后端验证(推荐)
|
||||||
|
async checkExpiration(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
// 调用后端接口验证 Token 是否过期
|
||||||
|
const response = await fetch(`${API_BASE}/auth/verify`, {
|
||||||
|
headers: { 'Authorization': `Bearer ${token}` }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status === 401) {
|
||||||
|
// Token 过期
|
||||||
|
await this.handleExpired();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (error) {
|
||||||
|
// 网络错误,使用本地时间判断
|
||||||
|
const userInfo = await getUserInfo();
|
||||||
|
return Date.now() >= (userInfo?.pluginTrialExpiresAt || 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 5.2 欢迎弹窗重复显示
|
||||||
|
|
||||||
|
**问题:** 每次登录都显示欢迎弹窗,用户体验不好
|
||||||
|
|
||||||
|
**场景:**
|
||||||
|
- 试用用户第一次登录显示欢迎弹窗 ✅
|
||||||
|
- 用户关闭插件后重新打开,又显示欢迎弹窗 ❌
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
```typescript
|
||||||
|
async onTokenReceived(token: string) {
|
||||||
|
// ... 获取用户信息
|
||||||
|
|
||||||
|
if (fullUserInfo.isPluginTrial === true) {
|
||||||
|
// 检查是否已经显示过欢迎弹窗
|
||||||
|
const hasWelcomed = this.context.globalState.get('pluginTrialWelcomed');
|
||||||
|
|
||||||
|
if (!hasWelcomed) {
|
||||||
|
await this.showWelcomePanel();
|
||||||
|
await this.context.globalState.update('pluginTrialWelcomed', true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await this.checkAndShowInvitationModal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 5.3 isPluginTrial 字段类型不一致
|
||||||
|
|
||||||
|
**问题:** 后端可能返回 `true`、`false`、`null`、`undefined`,前端判断时需要严格处理
|
||||||
|
|
||||||
|
**场景:**
|
||||||
|
```typescript
|
||||||
|
// ❌ 错误写法
|
||||||
|
if (userInfo.isPluginTrial) {
|
||||||
|
// undefined 会被判断为 false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ 正确写法
|
||||||
|
if (userInfo.isPluginTrial === true) {
|
||||||
|
// 插件试用用户
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**建议:**
|
||||||
|
- 后端统一返回 `true` 或 `false`,不要返回 `null`
|
||||||
|
- 前端使用严格相等 `===` 判断
|
||||||
|
|
||||||
|
|
||||||
|
### 5.4 Token 过期但前端未清除
|
||||||
|
|
||||||
|
**问题:** Token 在后端已过期,但前端仍保存着过期的 Token
|
||||||
|
|
||||||
|
**场景:**
|
||||||
|
- 用户 15 天后打开插件
|
||||||
|
- 前端尝试调用 API,后端返回 401
|
||||||
|
- 前端没有处理 401,导致功能异常
|
||||||
|
|
||||||
|
**解决方案:**
|
||||||
|
```typescript
|
||||||
|
// 在 apiClient.ts 中统一处理 401
|
||||||
|
async function apiCall(url: string, options: any) {
|
||||||
|
const response = await fetch(url, options);
|
||||||
|
|
||||||
|
if (response.status === 401) {
|
||||||
|
// Token 过期,清除本地数据
|
||||||
|
await clearAllData();
|
||||||
|
ExpiredPanel.render();
|
||||||
|
throw new Error('Token expired');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 前后端对接清单
|
||||||
|
|
||||||
|
### 6.1 后端需要提供
|
||||||
|
|
||||||
|
**1. 修改 `GET /system/user/getInfo` 接口**
|
||||||
|
- 新增字段:`isPluginTrial` (boolean)
|
||||||
|
- 新增字段:`pluginTrialExpiresAt` (number, 毫秒时间戳)
|
||||||
|
|
||||||
|
**2. Token 过期时间设置**
|
||||||
|
- 插件试用用户:JWT Token 设置 15 天过期
|
||||||
|
- 正式用户:保持现有逻辑
|
||||||
|
|
||||||
|
**3. 测试账号**
|
||||||
|
- 提供 1-2 个插件试用用户账号用于测试
|
||||||
|
|
||||||
|
### 6.2 前端需要修改
|
||||||
|
|
||||||
|
**1. 修改文件:**
|
||||||
|
- `src/services/userService.ts` - 修改 UserInfo 接口和 onTokenReceived()
|
||||||
|
- `src/utils/messageHandler.ts` - 添加过期检查
|
||||||
|
|
||||||
|
**2. 新增文件:**
|
||||||
|
- `src/panels/WelcomePanel.ts` - 欢迎弹窗
|
||||||
|
- `src/panels/ExpiredPanel.ts` - 过期提醒弹窗
|
||||||
|
- `src/services/trialExpirationService.ts` - 过期检测服务
|
||||||
|
|
||||||
|
**3. globalState 新增存储键:**
|
||||||
|
- `pluginTrialWelcomed` (boolean) - 是否已显示欢迎弹窗
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 测试计划
|
||||||
|
|
||||||
|
### 7.1 登录流程测试
|
||||||
|
|
||||||
|
**测试用例 1:插件试用用户登录**
|
||||||
|
- 使用插件试用用户账号登录
|
||||||
|
- 验证是否显示欢迎弹窗
|
||||||
|
- 验证是否不显示邀请码弹窗
|
||||||
|
- 验证 userInfo 中 isPluginTrial === true
|
||||||
|
|
||||||
|
**测试用例 2:正式用户登录**
|
||||||
|
- 使用正式用户账号登录
|
||||||
|
- 验证是否显示邀请码弹窗
|
||||||
|
- 验证是否不显示欢迎弹窗
|
||||||
|
- 验证 userInfo 中 isPluginTrial !== true
|
||||||
|
|
||||||
|
**测试用例 3:欢迎弹窗不重复显示**
|
||||||
|
- 插件试用用户登录后显示欢迎弹窗
|
||||||
|
- 关闭插件重新打开
|
||||||
|
- 验证不再显示欢迎弹窗
|
||||||
|
|
||||||
|
|
||||||
|
### 7.2 过期检测测试
|
||||||
|
|
||||||
|
**测试用例 4:未过期用户正常使用**
|
||||||
|
- 插件试用用户登录(未过期)
|
||||||
|
- 发送消息使用功能
|
||||||
|
- 验证功能正常使用
|
||||||
|
|
||||||
|
**测试用例 5:已过期用户禁止使用**
|
||||||
|
- 修改本地时间到 15 天后(或修改 pluginTrialExpiresAt)
|
||||||
|
- 尝试发送消息
|
||||||
|
- 验证显示过期弹窗
|
||||||
|
- 验证功能被禁用
|
||||||
|
|
||||||
|
**测试用例 6:Token 过期处理**
|
||||||
|
- 使用过期的 Token 调用 API
|
||||||
|
- 验证后端返回 401
|
||||||
|
- 验证前端显示过期弹窗
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 总结
|
||||||
|
|
||||||
|
### 8.1 核心改动点
|
||||||
|
|
||||||
|
**后端(最小改动):**
|
||||||
|
1. `GET /system/user/getInfo` 接口新增 2 个字段
|
||||||
|
2. JWT Token 根据用户类型设置不同过期时间
|
||||||
|
|
||||||
|
**前端(主要改动):**
|
||||||
|
1. 修改 UserInfo 接口定义
|
||||||
|
2. 修改 onTokenReceived() 添加判断逻辑
|
||||||
|
3. 新增 3 个文件(欢迎面板、过期面板、过期检测服务)
|
||||||
|
4. 在消息发送前添加过期检查
|
||||||
|
|
||||||
|
### 8.2 关键判断逻辑
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 登录后判断
|
||||||
|
if (userInfo.isPluginTrial === true) {
|
||||||
|
showWelcomePanel(); // 显示欢迎弹窗
|
||||||
|
} else {
|
||||||
|
showInvitationModal(); // 显示邀请码弹窗
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用前判断
|
||||||
|
if (Date.now() >= userInfo.pluginTrialExpiresAt) {
|
||||||
|
showExpiredPanel(); // 显示过期弹窗
|
||||||
|
return; // 禁止使用
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 8.3 必须注意的问题
|
||||||
|
|
||||||
|
1. **时间同步问题** - 用户本地时间不准确导致误判,建议调用后端验证
|
||||||
|
2. **isPluginTrial 严格判断** - 必须使用 `=== true` 判断
|
||||||
|
3. **欢迎弹窗重复显示** - 使用 globalState 标记避免重复
|
||||||
|
4. **Token 过期处理** - 在 apiClient 中统一处理 401 响应
|
||||||
|
|
||||||
|
### 8.4 实现优先级
|
||||||
|
|
||||||
|
**P0(必须实现):**
|
||||||
|
1. 后端接口新增字段
|
||||||
|
2. 前端 UserInfo 接口修改
|
||||||
|
3. 前端 onTokenReceived() 判断逻辑
|
||||||
|
4. 过期检测逻辑
|
||||||
|
|
||||||
|
**P1(重要):**
|
||||||
|
1. 欢迎弹窗
|
||||||
|
2. 过期提醒弹窗
|
||||||
|
|
||||||
|
**P2(优化):**
|
||||||
|
1. 后端验证过期(避免时间同步问题)
|
||||||
|
2. 更友好的过期提示
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**文档完成!** 基于你现有的代码架构,这个方案改动最小,实现最简单。
|
||||||
|
|
||||||
1027
docs/数据流程详解.md
Normal file
251
media/USER_MANUAL.md
Normal file
@ -0,0 +1,251 @@
|
|||||||
|
# IC Coder 插件端用户手册
|
||||||
|
|
||||||
|
欢迎**宁德时代新能源科技股份有限公司**的各位专家使用 IC Coder 企业版!
|
||||||
|
|
||||||
|
本手册旨在为贵公司提供插件的安装、配置及使用指导,帮助您快速上手并充分发挥工具的功能。
|
||||||
|
|
||||||
|
在使用过程中,如遇任何功能异常、性能问题或操作疑问,欢迎随时联系我们,我们将在第一时间为您提供支持。
|
||||||
|
|
||||||
|
若贵司有特定的业务场景或个性化功能需求,也可直接与我们沟通,我们会为贵司进行定制化开发。
|
||||||
|
|
||||||
|
| 功能 | 说明 |
|
||||||
|
| ----------------------- | ---------------------------------------------------- |
|
||||||
|
| 自研微调模型 | IC Coder 自研顶尖 Max 微调模型,专为 FPGA 开发 |
|
||||||
|
| 高质量 Verilog 代码生成 | 根据自然语言描述生成符合规范的 Verilog 代码 |
|
||||||
|
| 自动语法检查 | 自动检测代码语法错误并自动修复 |
|
||||||
|
| 自动仿真 | 内置编译器,自动编译和仿真 |
|
||||||
|
| 波形检查工具 | 内置 VCD 波形查看器,自动波形解析 |
|
||||||
|
| 自主检查迭代 | AI 自动检查代码问题并迭代优化 |
|
||||||
|
| Diff 功能 | 快速比较代码差异,追踪 AI 修改,可一键确认和撤销修改 |
|
||||||
|
| Code Action 快捷操作 | Ctrl+L 快捷键或右键菜单快速添加代码到对话 |
|
||||||
|
| 支持上下文连续对话 | 多轮对话,AI 记住之前的交互内容 |
|
||||||
|
| 会话历史管理 | 自动保存对话记录,随时恢复历史会话 |
|
||||||
|
|
||||||
|
## IC Coder 快速入门指南
|
||||||
|
|
||||||
|
### 系统要求
|
||||||
|
|
||||||
|
- **Visual Studio Code**: 版本 >= 1.60.0
|
||||||
|
|
||||||
|
各位专家也可以通过这个链接下载VSCode [下载链接](https://code.visualstudio.com/)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 安装步骤
|
||||||
|
|
||||||
|
#### 步骤 1:通过 VSIX 文件安装(推荐)
|
||||||
|
|
||||||
|
1. **获取安装包**
|
||||||
|
- 确保您已经获得 `iccoder-Trial-1.0.vsix` 文件
|
||||||
|
|
||||||
|
2. **打开 VS Code**
|
||||||
|
- 启动 Visual Studio Code
|
||||||
|
|
||||||
|
3. **安装插件**
|
||||||
|
|
||||||
|
有以下三种安装方式:
|
||||||
|
|
||||||
|
**方式 A:通过命令面板**
|
||||||
|
- 按 `Ctrl+Shift+P` (Windows/Linux) 或 `Cmd+Shift+P` (macOS) 打开命令面板
|
||||||
|
- 输入 `Extensions: Install from VSIX...`
|
||||||
|
- 选择 `iccoder-Trial-1.0.vsix` 文件
|
||||||
|
- 等待安装完成
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**方式 B:通过扩展视图**
|
||||||
|
- 点击左侧活动栏的扩展图标(或按 `Ctrl+Shift+X`)
|
||||||
|
- 点击扩展视图右上角的 `...` (更多操作)
|
||||||
|
- 选择 `从 VSIX 安装...`
|
||||||
|
- 选择 `iccoder-Trial-1.0.vsix` 文件
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**方式 C:通过命令行**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
code --install-extension iccoder-Trial-1.0.vsix
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **重启 VS Code**
|
||||||
|
- 安装完成后,建议重启 VS Code 以确保插件正常加载
|
||||||
|
|
||||||
|
#### 步骤 2:打开 IC Coder 界面
|
||||||
|
|
||||||
|
**登录后会自动打开**,手动打开也有以下几种方式:
|
||||||
|
|
||||||
|
**方式 1:通过侧边栏**
|
||||||
|
|
||||||
|
- 点击左侧活动栏的 IC Coder 图标
|
||||||
|
- 侧边栏会显示 IC Coder 聊天界面
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**方式 2:通过命令面板**
|
||||||
|
|
||||||
|
- 按 `Ctrl+Shift+P` (Windows/Linux) 或 `Cmd+Shift+P` (macOS)
|
||||||
|
- 输入以下命令之一:
|
||||||
|
- `IC Coder: 打开聊天` - 打开聊天界面
|
||||||
|
- `打开 IC Coder 助手` - 打开助手面板
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 步骤 3:开始使用
|
||||||
|
|
||||||
|
插件已预配置好后端服务,安装后即可直接使用,无需手动配置。
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 故障排除
|
||||||
|
|
||||||
|
#### 问题 :插件无法安装
|
||||||
|
|
||||||
|
**症状**:安装 VSIX 文件时报错
|
||||||
|
|
||||||
|
#### 解决方案:
|
||||||
|
|
||||||
|
- 确认 VS Code 版本 >= 1.60.0
|
||||||
|
|
||||||
|
- 检查 VSIX 文件是否完整(未损坏)
|
||||||
|
|
||||||
|
- 尝试以管理员权限运行 VS Code
|
||||||
|
|
||||||
|
- 清除 VS Code 缓存后重试
|
||||||
|
|
||||||
|
### 完整使用流程示例
|
||||||
|
|
||||||
|
下面通过一个完整的案例,展示如何使用 IC Coder 从需求到代码生成的全过程。
|
||||||
|
|
||||||
|
#### 步骤 1:输入设计需求
|
||||||
|
|
||||||
|
在对话框中输入设计需求,例如:
|
||||||
|
|
||||||
|
```
|
||||||
|
我需要设计一个 8 位加法器,要求有进位输入和进位输出
|
||||||
|
```
|
||||||
|
|
||||||
|
点击**发送**按钮。
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 步骤 2:AI 询问补充信息
|
||||||
|
|
||||||
|
AI 会根据需求,询问一些关键的设计细节。例如:
|
||||||
|
|
||||||
|
- 是否需要溢出检测?
|
||||||
|
- 时钟频率要求是多少?
|
||||||
|
- 是否需要流水线设计?
|
||||||
|
|
||||||
|
您只需要根据实际需求**选择相应的选项或者直接输入需求**即可,AI 会根据您的选择生成最合适的设计方案。
|
||||||
|
|
||||||
|
#### 步骤 3:确认 AI 生成的任务列表
|
||||||
|
|
||||||
|
AI 会根据您的需求和补充信息,生成一个详细的任务列表(Todo List)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
仔细查看任务列表,确认无误后点击**确认**按钮,AI 将开始执行。
|
||||||
|
|
||||||
|
#### 步骤 4:观察 AI 执行过程
|
||||||
|
|
||||||
|
AI 开始工作后,您可以在对话框中实时看到所有执行步骤:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
每个步骤完成后,任务列表中对应的项目会被标记为完成状态。
|
||||||
|
|
||||||
|
#### 步骤 5:仿真运行与结果查看
|
||||||
|
|
||||||
|
当 AI 完成代码生成后,会自动运行仿真验证:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 步骤 6:查看生成的文件
|
||||||
|
|
||||||
|
所有生成的文件会自动保存到您的工作目录中:
|
||||||
|
|
||||||
|
```
|
||||||
|
project/
|
||||||
|
├── src/
|
||||||
|
│ └── tb_adder_8bit.v # RTL 设计文件
|
||||||
|
├── sim/
|
||||||
|
│ └── tb_adder_8bit # 测试平台文件
|
||||||
|
└── tb_adder_8bit.vcd # 波形文件
|
||||||
|
```
|
||||||
|
|
||||||
|
您可以在 VS Code 的文件资源管理器中直接打开这些文件进行查看或修改。
|
||||||
|
|
||||||
|
#### 步骤8:继续对话
|
||||||
|
|
||||||
|
如果您对给出的结果不太满意,您可以告诉IC Coder您想具体修改的地方或者文件
|
||||||
|
|
||||||
|
#### 使用流程总结
|
||||||
|
|
||||||
|
整个使用流程可以概括为:
|
||||||
|
|
||||||
|
- **输入需求** → 在对话框中描述您的设计需求
|
||||||
|
- **回答问题** → 根据 AI 的询问选择合适的选项
|
||||||
|
- **确认任务** → 查看并确认 AI 生成的任务列表
|
||||||
|
- **观察执行** → 实时查看 AI 的所有执行步骤
|
||||||
|
- **查看结果** → 仿真成功后查看生成的文件
|
||||||
|
|
||||||
|
#### 使用提示
|
||||||
|
|
||||||
|
**如何描述需求更准确?**
|
||||||
|
|
||||||
|
- **明确功能**:清楚说明模块要实现什么功能
|
||||||
|
- **指定参数**:说明位宽、时钟频率等关键参数
|
||||||
|
- **特殊要求**:如果有特殊的时序要求或接口规范,请明确说明
|
||||||
|
|
||||||
|
**示例:**
|
||||||
|
|
||||||
|
```
|
||||||
|
好的描述:设计一个 16 位的 FIFO,深度为 256,支持异步读写
|
||||||
|
不够清晰:帮我写一个 FIFO
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 常见问题
|
||||||
|
|
||||||
|
**Q: 仿真失败了怎么办?**
|
||||||
|
A: AI 会根据错误自动修复代码并重新仿真。
|
||||||
|
|
||||||
|
**Q: 可以修改生成的代码吗?**
|
||||||
|
A: 可以,可以直接编辑文件,然后告诉 AI 重新运行仿真。
|
||||||
|
|
||||||
|
**Q: 可以导入已有的代码吗?**
|
||||||
|
A: 可以,在工作区中打开对应的代码文件夹,然后直接在对话中告诉 AI 您要修改或优化哪个文件,AI 会读取并进行修改。
|
||||||
|
|
||||||
|
**Q: 如何查看 AI 的思考过程?**
|
||||||
|
A: 在执行过程中,AI 会实时显示每一步的操作和决策依据。
|
||||||
|
|
||||||
|
**Q: 如何保存对话历史?**
|
||||||
|
A: 对话历史会自动保存在本地,可以点击历史对话查看历史会话记录。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 卸载插件
|
||||||
|
|
||||||
|
如需卸载插件:
|
||||||
|
|
||||||
|
1. 打开扩展视图
|
||||||
|
2. 找到 "IC Coder" 插件
|
||||||
|
3. 点击卸载按钮
|
||||||
|
4. 重启 VS Code
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 注意事项
|
||||||
|
|
||||||
|
1. **需提前打开一个文件夹作为工作区**,否则会准确的为您服务
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
2. **开箱即用**
|
||||||
|
- 插件已预配置后端服务,无需手动设置
|
||||||
|
- 安装后即可直接使用所有功能
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**祝您使用愉快!如有问题欢迎反馈。**
|
||||||
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 |
|
Before Width: | Height: | Size: 160 KiB After Width: | Height: | Size: 252 KiB |
BIN
media/manual/仿真运行结果.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
media/manual/侧边栏打开.png
Normal file
|
After Width: | Height: | Size: 155 KiB |
BIN
media/manual/命令面板打开.png
Normal file
|
After Width: | Height: | Size: 30 KiB |
BIN
media/manual/安装方式1.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
media/manual/安装方式2.png
Normal file
|
After Width: | Height: | Size: 142 KiB |
BIN
media/manual/打开文件夹.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
media/manual/确认任务.png
Normal file
|
After Width: | Height: | Size: 47 KiB |
BIN
media/manual/聊天界面.png
Normal file
|
After Width: | Height: | Size: 202 KiB |
BIN
media/manual/观察执行过程.png
Normal file
|
After Width: | Height: | Size: 104 KiB |
BIN
media/manual/输入需求.png
Normal file
|
After Width: | Height: | Size: 62 KiB |
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);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
84
package.json
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "iccoder",
|
"name": "iccoder",
|
||||||
"displayName": "IC Coder",
|
"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.12",
|
||||||
"publisher": "ICCoder",
|
"publisher": "ICCoderAgenticVerilogPlatform",
|
||||||
"engines": {
|
"engines": {
|
||||||
"vscode": "^1.80.0"
|
"vscode": "^1.80.0"
|
||||||
},
|
},
|
||||||
@ -21,9 +21,13 @@
|
|||||||
"assistant"
|
"assistant"
|
||||||
],
|
],
|
||||||
"license": "SEE LICENSE IN LICENSE",
|
"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.mainView",
|
||||||
"onLanguage:verilog",
|
"onLanguage:verilog",
|
||||||
"onLanguage:vhdl",
|
"onLanguage:vhdl",
|
||||||
"onStartupFinished"
|
"onStartupFinished"
|
||||||
@ -45,6 +49,33 @@
|
|||||||
"command": "ic-coder.openVCDViewer",
|
"command": "ic-coder.openVCDViewer",
|
||||||
"title": "打开 VCD 波形查看器",
|
"title": "打开 VCD 波形查看器",
|
||||||
"category": "IC Coder"
|
"category": "IC Coder"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "ic-coder.testNotification",
|
||||||
|
"title": "测试系统通知",
|
||||||
|
"category": "IC Coder"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "ic-coder.addCodeToChat",
|
||||||
|
"title": "添加到 IC Coder 对话",
|
||||||
|
"category": "IC Coder"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"menus": {
|
||||||
|
"editor/context": [
|
||||||
|
{
|
||||||
|
"command": "ic-coder.addCodeToChat",
|
||||||
|
"when": "editorHasSelection",
|
||||||
|
"group": "9_cutcopypaste"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"keybindings": [
|
||||||
|
{
|
||||||
|
"command": "ic-coder.addCodeToChat",
|
||||||
|
"key": "ctrl+l",
|
||||||
|
"mac": "cmd+l",
|
||||||
|
"when": "editorTextFocus && editorHasSelection"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"viewsContainers": {
|
"viewsContainers": {
|
||||||
@ -70,7 +101,41 @@
|
|||||||
"id": "iccoder",
|
"id": "iccoder",
|
||||||
"label": "IC Coder"
|
"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": {
|
||||||
"vscode:prepublish": "pnpm run package",
|
"vscode:prepublish": "pnpm run package",
|
||||||
@ -87,10 +152,12 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/mocha": "^10.0.10",
|
"@types/mocha": "^10.0.10",
|
||||||
"@types/node": "22.x",
|
"@types/node": "22.x",
|
||||||
|
"@types/node-notifier": "^8.0.5",
|
||||||
"@types/vscode": "^1.80.0",
|
"@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",
|
"@vscode/vsce": "^3.7.1",
|
||||||
|
"copy-webpack-plugin": "^14.0.0",
|
||||||
"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",
|
||||||
@ -98,16 +165,11 @@
|
|||||||
"webpack": "^5.103.0",
|
"webpack": "^5.103.0",
|
||||||
"webpack-cli": "^6.0.1"
|
"webpack-cli": "^6.0.1"
|
||||||
},
|
},
|
||||||
"files": [
|
|
||||||
"dist",
|
|
||||||
"media",
|
|
||||||
"tools",
|
|
||||||
"src/assets"
|
|
||||||
],
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@wavedrom/doppler": "^1.14.0",
|
"@wavedrom/doppler": "^1.14.0",
|
||||||
"eventsource-parser": "^3.0.6",
|
"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",
|
||||||
|
|||||||
74
pnpm-lock.yaml
generated
@ -17,6 +17,9 @@ importers:
|
|||||||
iconv-lite:
|
iconv-lite:
|
||||||
specifier: ^0.7.1
|
specifier: ^0.7.1
|
||||||
version: 0.7.1
|
version: 0.7.1
|
||||||
|
node-notifier:
|
||||||
|
specifier: ^10.0.1
|
||||||
|
version: 10.0.1
|
||||||
onml:
|
onml:
|
||||||
specifier: ^2.1.0
|
specifier: ^2.1.0
|
||||||
version: 2.1.0
|
version: 2.1.0
|
||||||
@ -39,6 +42,9 @@ importers:
|
|||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: 22.x
|
specifier: 22.x
|
||||||
version: 22.19.2
|
version: 22.19.2
|
||||||
|
'@types/node-notifier':
|
||||||
|
specifier: ^8.0.5
|
||||||
|
version: 8.0.5
|
||||||
'@types/vscode':
|
'@types/vscode':
|
||||||
specifier: ^1.80.0
|
specifier: ^1.80.0
|
||||||
version: 1.107.0
|
version: 1.107.0
|
||||||
@ -51,6 +57,9 @@ importers:
|
|||||||
'@vscode/vsce':
|
'@vscode/vsce':
|
||||||
specifier: ^3.7.1
|
specifier: ^3.7.1
|
||||||
version: 3.7.1
|
version: 3.7.1
|
||||||
|
copy-webpack-plugin:
|
||||||
|
specifier: ^14.0.0
|
||||||
|
version: 14.0.0(webpack@5.103.0)
|
||||||
eslint:
|
eslint:
|
||||||
specifier: ^9.39.1
|
specifier: ^9.39.1
|
||||||
version: 9.39.1
|
version: 9.39.1
|
||||||
@ -349,6 +358,9 @@ packages:
|
|||||||
'@types/mocha@10.0.10':
|
'@types/mocha@10.0.10':
|
||||||
resolution: {integrity: sha512-xPyYSz1cMPnJQhl0CLMH68j3gprKZaTjG3s5Vi+fDgx+uhG9NOXwbVt52eFS8ECyXhyKcjDLCBEqBExKuiZb7Q==}
|
resolution: {integrity: sha512-xPyYSz1cMPnJQhl0CLMH68j3gprKZaTjG3s5Vi+fDgx+uhG9NOXwbVt52eFS8ECyXhyKcjDLCBEqBExKuiZb7Q==}
|
||||||
|
|
||||||
|
'@types/node-notifier@8.0.5':
|
||||||
|
resolution: {integrity: sha512-LX7+8MtTsv6szumAp6WOy87nqMEdGhhry/Qfprjm1Ma6REjVzeF7SCyvPtp5RaF6IkXCS9V4ra8g5fwvf2ZAYg==}
|
||||||
|
|
||||||
'@types/node@22.19.2':
|
'@types/node@22.19.2':
|
||||||
resolution: {integrity: sha512-LPM2G3Syo1GLzXLGJAKdqoU35XvrWzGJ21/7sgZTUpbkBaOasTj8tjwn6w+hCkqaa1TfJ/w67rJSwYItlJ2mYw==}
|
resolution: {integrity: sha512-LPM2G3Syo1GLzXLGJAKdqoU35XvrWzGJ21/7sgZTUpbkBaOasTj8tjwn6w+hCkqaa1TfJ/w67rJSwYItlJ2mYw==}
|
||||||
|
|
||||||
@ -814,6 +826,12 @@ packages:
|
|||||||
convert-source-map@2.0.0:
|
convert-source-map@2.0.0:
|
||||||
resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==}
|
resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==}
|
||||||
|
|
||||||
|
copy-webpack-plugin@14.0.0:
|
||||||
|
resolution: {integrity: sha512-3JLW90aBGeaTLpM7mYQKpnVdgsUZRExY55giiZgLuX/xTQRUs1dOCwbBnWnvY6Q6rfZoXMNwzOQJCSZPppfqXA==}
|
||||||
|
engines: {node: '>= 20.9.0'}
|
||||||
|
peerDependencies:
|
||||||
|
webpack: ^5.1.0
|
||||||
|
|
||||||
core-util-is@1.0.3:
|
core-util-is@1.0.3:
|
||||||
resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==}
|
resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==}
|
||||||
|
|
||||||
@ -1185,6 +1203,9 @@ packages:
|
|||||||
graceful-fs@4.2.11:
|
graceful-fs@4.2.11:
|
||||||
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
|
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
|
||||||
|
|
||||||
|
growly@1.3.0:
|
||||||
|
resolution: {integrity: sha512-+xGQY0YyAWCnqy7Cd++hc2JqMYzlm0dG30Jd0beaA64sROr8C4nt8Yc9V5Ro3avlSUDTN0ulqP/VBKi1/lLygw==}
|
||||||
|
|
||||||
has-flag@4.0.0:
|
has-flag@4.0.0:
|
||||||
resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
|
resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
@ -1284,6 +1305,11 @@ packages:
|
|||||||
resolution: {integrity: sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==}
|
resolution: {integrity: sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==}
|
||||||
engines: {node: '>= 0.4'}
|
engines: {node: '>= 0.4'}
|
||||||
|
|
||||||
|
is-docker@2.2.1:
|
||||||
|
resolution: {integrity: sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==}
|
||||||
|
engines: {node: '>=8'}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
is-docker@3.0.0:
|
is-docker@3.0.0:
|
||||||
resolution: {integrity: sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==}
|
resolution: {integrity: sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==}
|
||||||
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
|
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
|
||||||
@ -1342,6 +1368,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ==}
|
resolution: {integrity: sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ==}
|
||||||
engines: {node: '>=18'}
|
engines: {node: '>=18'}
|
||||||
|
|
||||||
|
is-wsl@2.2.0:
|
||||||
|
resolution: {integrity: sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==}
|
||||||
|
engines: {node: '>=8'}
|
||||||
|
|
||||||
is-wsl@3.1.0:
|
is-wsl@3.1.0:
|
||||||
resolution: {integrity: sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw==}
|
resolution: {integrity: sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw==}
|
||||||
engines: {node: '>=16'}
|
engines: {node: '>=16'}
|
||||||
@ -1622,6 +1652,9 @@ packages:
|
|||||||
node-addon-api@4.3.0:
|
node-addon-api@4.3.0:
|
||||||
resolution: {integrity: sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==}
|
resolution: {integrity: sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==}
|
||||||
|
|
||||||
|
node-notifier@10.0.1:
|
||||||
|
resolution: {integrity: sha512-YX7TSyDukOZ0g+gmzjB6abKu+hTGvO8+8+gIFDsRCU2t8fLV/P2unmt+LGFaIa4y64aX98Qksa97rgz4vMNeLQ==}
|
||||||
|
|
||||||
node-releases@2.0.27:
|
node-releases@2.0.27:
|
||||||
resolution: {integrity: sha512-nmh3lCkYZ3grZvqcCH+fjmQ7X+H0OeZgP40OierEaAptX4XofMh5kwNbWh7lBduUzCcV/8kZ+NDLCwm2iorIlA==}
|
resolution: {integrity: sha512-nmh3lCkYZ3grZvqcCH+fjmQ7X+H0OeZgP40OierEaAptX4XofMh5kwNbWh7lBduUzCcV/8kZ+NDLCwm2iorIlA==}
|
||||||
|
|
||||||
@ -1904,6 +1937,10 @@ packages:
|
|||||||
serialize-javascript@6.0.2:
|
serialize-javascript@6.0.2:
|
||||||
resolution: {integrity: sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==}
|
resolution: {integrity: sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==}
|
||||||
|
|
||||||
|
serialize-javascript@7.0.4:
|
||||||
|
resolution: {integrity: sha512-DuGdB+Po43Q5Jxwpzt1lhyFSYKryqoNjQSA9M92tyw0lyHIOur+XCalOUe0KTJpyqzT8+fQ5A0Jf7vCx/NKmIg==}
|
||||||
|
engines: {node: '>=20.0.0'}
|
||||||
|
|
||||||
setimmediate@1.0.5:
|
setimmediate@1.0.5:
|
||||||
resolution: {integrity: sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==}
|
resolution: {integrity: sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==}
|
||||||
|
|
||||||
@ -1919,6 +1956,9 @@ packages:
|
|||||||
resolution: {integrity: sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==}
|
resolution: {integrity: sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
|
|
||||||
|
shellwords@0.1.1:
|
||||||
|
resolution: {integrity: sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==}
|
||||||
|
|
||||||
side-channel-list@1.0.0:
|
side-channel-list@1.0.0:
|
||||||
resolution: {integrity: sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==}
|
resolution: {integrity: sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==}
|
||||||
engines: {node: '>= 0.4'}
|
engines: {node: '>= 0.4'}
|
||||||
@ -2702,6 +2742,10 @@ snapshots:
|
|||||||
|
|
||||||
'@types/mocha@10.0.10': {}
|
'@types/mocha@10.0.10': {}
|
||||||
|
|
||||||
|
'@types/node-notifier@8.0.5':
|
||||||
|
dependencies:
|
||||||
|
'@types/node': 22.19.2
|
||||||
|
|
||||||
'@types/node@22.19.2':
|
'@types/node@22.19.2':
|
||||||
dependencies:
|
dependencies:
|
||||||
undici-types: 6.21.0
|
undici-types: 6.21.0
|
||||||
@ -3266,6 +3310,15 @@ snapshots:
|
|||||||
|
|
||||||
convert-source-map@2.0.0: {}
|
convert-source-map@2.0.0: {}
|
||||||
|
|
||||||
|
copy-webpack-plugin@14.0.0(webpack@5.103.0):
|
||||||
|
dependencies:
|
||||||
|
glob-parent: 6.0.2
|
||||||
|
normalize-path: 3.0.0
|
||||||
|
schema-utils: 4.3.3
|
||||||
|
serialize-javascript: 7.0.4
|
||||||
|
tinyglobby: 0.2.15
|
||||||
|
webpack: 5.103.0(webpack-cli@6.0.1)
|
||||||
|
|
||||||
core-util-is@1.0.3: {}
|
core-util-is@1.0.3: {}
|
||||||
|
|
||||||
cross-spawn@7.0.6:
|
cross-spawn@7.0.6:
|
||||||
@ -3646,6 +3699,8 @@ snapshots:
|
|||||||
|
|
||||||
graceful-fs@4.2.11: {}
|
graceful-fs@4.2.11: {}
|
||||||
|
|
||||||
|
growly@1.3.0: {}
|
||||||
|
|
||||||
has-flag@4.0.0: {}
|
has-flag@4.0.0: {}
|
||||||
|
|
||||||
has-symbols@1.1.0: {}
|
has-symbols@1.1.0: {}
|
||||||
@ -3737,6 +3792,8 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
hasown: 2.0.2
|
hasown: 2.0.2
|
||||||
|
|
||||||
|
is-docker@2.2.1: {}
|
||||||
|
|
||||||
is-docker@3.0.0: {}
|
is-docker@3.0.0: {}
|
||||||
|
|
||||||
is-extglob@2.1.1: {}
|
is-extglob@2.1.1: {}
|
||||||
@ -3771,6 +3828,10 @@ snapshots:
|
|||||||
|
|
||||||
is-unicode-supported@2.1.0: {}
|
is-unicode-supported@2.1.0: {}
|
||||||
|
|
||||||
|
is-wsl@2.2.0:
|
||||||
|
dependencies:
|
||||||
|
is-docker: 2.2.1
|
||||||
|
|
||||||
is-wsl@3.1.0:
|
is-wsl@3.1.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
is-inside-container: 1.0.0
|
is-inside-container: 1.0.0
|
||||||
@ -4074,6 +4135,15 @@ snapshots:
|
|||||||
node-addon-api@4.3.0:
|
node-addon-api@4.3.0:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
node-notifier@10.0.1:
|
||||||
|
dependencies:
|
||||||
|
growly: 1.3.0
|
||||||
|
is-wsl: 2.2.0
|
||||||
|
semver: 7.7.3
|
||||||
|
shellwords: 0.1.1
|
||||||
|
uuid: 8.3.2
|
||||||
|
which: 2.0.2
|
||||||
|
|
||||||
node-releases@2.0.27: {}
|
node-releases@2.0.27: {}
|
||||||
|
|
||||||
node-sarif-builder@3.3.1:
|
node-sarif-builder@3.3.1:
|
||||||
@ -4383,6 +4453,8 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
randombytes: 2.1.0
|
randombytes: 2.1.0
|
||||||
|
|
||||||
|
serialize-javascript@7.0.4: {}
|
||||||
|
|
||||||
setimmediate@1.0.5: {}
|
setimmediate@1.0.5: {}
|
||||||
|
|
||||||
shallow-clone@3.0.1:
|
shallow-clone@3.0.1:
|
||||||
@ -4395,6 +4467,8 @@ snapshots:
|
|||||||
|
|
||||||
shebang-regex@3.0.0: {}
|
shebang-regex@3.0.0: {}
|
||||||
|
|
||||||
|
shellwords@0.1.1: {}
|
||||||
|
|
||||||
side-channel-list@1.0.0:
|
side-channel-list@1.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
es-errors: 1.3.0
|
es-errors: 1.3.0
|
||||||
|
|||||||
BIN
rustup-init.exe
Normal file
BIN
src/assets/QRCode/wx.png
Normal file
|
After Width: | Height: | Size: 119 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();
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
@ -8,40 +8,68 @@ import * as vscode from "vscode";
|
|||||||
type Environment = "dev" | "test" | "prod";
|
type Environment = "dev" | "test" | "prod";
|
||||||
|
|
||||||
/** 当前环境 - 修改这里切换环境 */
|
/** 当前环境 - 修改这里切换环境 */
|
||||||
const CURRENT_ENV: Environment = "test";
|
const CURRENT_ENV: Environment = "prod";
|
||||||
|
|
||||||
|
/** 服务等级类型 */
|
||||||
|
export type ServiceTier = "lite" | "syntaxic" | "max" | "auto";
|
||||||
|
|
||||||
/** 配置项接口 */
|
/** 配置项接口 */
|
||||||
export interface IccoderConfig {
|
export interface IccoderConfig {
|
||||||
/** 后端服务地址 */
|
/** 后端服务地址 */
|
||||||
backendUrl: string;
|
backendUrl: string;
|
||||||
|
/** 登录页面地址 */
|
||||||
|
loginUrl: string;
|
||||||
|
/** 后端服务地址(strangeLoop) */
|
||||||
|
backendUrlStrongeLoop: string;
|
||||||
/** 请求超时时间(毫秒) */
|
/** 请求超时时间(毫秒) */
|
||||||
timeout: number;
|
timeout: number;
|
||||||
/** 用户ID(临时使用,后续对接认证) */
|
/** 用户ID(临时使用,后续对接认证) */
|
||||||
userId: string;
|
userId: string;
|
||||||
|
/** 服务等级 */
|
||||||
|
serviceTier: ServiceTier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 自定义配置缓存 */
|
||||||
|
let customConfig: Partial<IccoderConfig> | null = null;
|
||||||
|
|
||||||
/** 环境配置 */
|
/** 环境配置 */
|
||||||
const ENV_CONFIG: Record<Environment, IccoderConfig> = {
|
const ENV_CONFIG: Record<Environment, IccoderConfig> = {
|
||||||
/** 本地开发环境 */
|
/** 本地开发环境 - 通过 Gateway 路由 */
|
||||||
dev: {
|
dev: {
|
||||||
backendUrl: "http://localhost:2233",
|
backendUrl: "http://localhost:8080/iccoder",
|
||||||
timeout: 60000,
|
backendUrlStrongeLoop: "http://localhost:8080",
|
||||||
|
loginUrl: "http://localhost/login",
|
||||||
|
timeout: 300000,
|
||||||
userId: "default-user",
|
userId: "default-user",
|
||||||
|
serviceTier: "max",
|
||||||
},
|
},
|
||||||
/** 测试服务器环境 */
|
/** 测试服务器环境 - 通过 Gateway 路由 */
|
||||||
test: {
|
test: {
|
||||||
backendUrl: "http://192.168.1.108:2233",
|
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,
|
timeout: 60000,
|
||||||
userId: "default-user",
|
userId: "default-user",
|
||||||
|
serviceTier: "max",
|
||||||
},
|
},
|
||||||
/** 生产环境 */
|
/** 生产环境 - 通过 Gateway 路由 */
|
||||||
prod: {
|
prod: {
|
||||||
backendUrl: "https://api.iccoder.com", // TODO: 替换为实际生产地址
|
backendUrl: "https://api.iccoder.com",
|
||||||
|
backendUrlStrongeLoop: "http://192.168.1.115:2029",
|
||||||
|
loginUrl: "https://iccoder.com/login",
|
||||||
timeout: 60000,
|
timeout: 60000,
|
||||||
userId: "default-user",
|
userId: "default-user",
|
||||||
|
serviceTier: "auto",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置自定义配置
|
||||||
|
*/
|
||||||
|
export function setCustomConfig(config: Partial<IccoderConfig>) {
|
||||||
|
customConfig = config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前环境
|
* 获取当前环境
|
||||||
*/
|
*/
|
||||||
@ -53,7 +81,14 @@ export function getCurrentEnv(): Environment {
|
|||||||
* 获取配置项
|
* 获取配置项
|
||||||
*/
|
*/
|
||||||
export function getConfig(): IccoderConfig {
|
export function getConfig(): IccoderConfig {
|
||||||
return { ...ENV_CONFIG[CURRENT_ENV] };
|
const baseConfig = { ...ENV_CONFIG[CURRENT_ENV] };
|
||||||
|
|
||||||
|
// 合并自定义配置(空字符串表示使用默认)
|
||||||
|
if (customConfig?.backendUrl && customConfig.backendUrl !== '') {
|
||||||
|
baseConfig.backendUrl = customConfig.backendUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return baseConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,3 +102,15 @@ export function getApiUrl(path: string): string {
|
|||||||
const apiPath = path.startsWith("/") ? path : `/${path}`;
|
const apiPath = path.startsWith("/") ? path : `/${path}`;
|
||||||
return `${baseUrl}${apiPath}`;
|
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}`;
|
||||||
|
}
|
||||||
|
|||||||
335
src/extension.ts
@ -1,32 +1,109 @@
|
|||||||
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 { UserManualPanel } from "./panels/UserManualPanel";
|
||||||
import { ChatHistoryManager } from "./utils/chatHistoryManager";
|
import { ChatHistoryManager } from "./utils/chatHistoryManager";
|
||||||
import { ICCoderAuthenticationProvider } from "./services/icCoderAuthProvider";
|
import { ICCoderAuthenticationProvider } from "./services/icCoderAuthProvider";
|
||||||
|
import { VCDFileServer } from "./services/vcdFileServer";
|
||||||
|
import { isTokenExpired } from "./utils/jwtUtils";
|
||||||
|
import { NotificationService } from "./services/notificationService";
|
||||||
|
import { InvitationService } from "./services/invitationService";
|
||||||
|
import { ICCoderCodeActionProvider } from "./providers/codeActionProvider";
|
||||||
|
import { setCustomConfig } from "./config/settings";
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export async function activate(context: vscode.ExtensionContext) {
|
||||||
console.log("🎉 IC Coder 插件已激活!");
|
console.log("🎉 IC Coder 插件已激活!");
|
||||||
|
|
||||||
// 注册 Authentication Provider
|
// 加载保存的配置
|
||||||
const authProvider = new ICCoderAuthenticationProvider(context);
|
const savedSettings = context.globalState.get('generalSettings') as any;
|
||||||
|
if (savedSettings?.backendUrl) {
|
||||||
|
setCustomConfig({
|
||||||
|
backendUrl: savedSettings.backendUrl,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建装饰类型(代码旁边的提示)
|
||||||
|
const decorationType = vscode.window.createTextEditorDecorationType({
|
||||||
|
after: {
|
||||||
|
contentText: ' Ctrl+L 添加到 IC Coder 对话',
|
||||||
|
color: '#888',
|
||||||
|
fontStyle: 'italic',
|
||||||
|
margin: '0 0 0 1em'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 更新装饰
|
||||||
|
const updateDecorations = () => {
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
if (!editor.selection.isEmpty) {
|
||||||
|
const range = new vscode.Range(editor.selection.end, editor.selection.end);
|
||||||
|
const decoration = { range };
|
||||||
|
editor.setDecorations(decorationType, [decoration]);
|
||||||
|
} else {
|
||||||
|
editor.setDecorations(decorationType, []);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.authentication.registerAuthenticationProvider(
|
vscode.window.onDidChangeTextEditorSelection(updateDecorations),
|
||||||
"iccoder",
|
vscode.window.onDidChangeActiveTextEditor(updateDecorations)
|
||||||
"IC Coder",
|
|
||||||
authProvider
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 检查登录状态,如果已登录则自动打开聊天面板
|
updateDecorations();
|
||||||
vscode.authentication.getSession("iccoder", [], { createIfNone: false })
|
|
||||||
.then((session) => {
|
// 初始化通知服务
|
||||||
if (session) {
|
const notificationService = NotificationService.getInstance(context);
|
||||||
vscode.commands.executeCommand("ic-coder.openChat");
|
console.log('[Extension] 通知服务已初始化');
|
||||||
}
|
|
||||||
}, () => {
|
// 【已禁用】登录和 token 验证 - 无需登录即可使用
|
||||||
// 未登录,不做任何操作
|
// 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) {
|
||||||
|
// await context.globalState.update('icCoderSessions', []);
|
||||||
|
// await context.globalState.update('icCoderUserInfo', undefined);
|
||||||
|
// console.log('[Extension] Token 已过期,已清除所有登录状态');
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 初始化 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 注册 - 无需登录
|
||||||
|
const authProvider = new ICCoderAuthenticationProvider(context);
|
||||||
|
// context.subscriptions.push(
|
||||||
|
// vscode.authentication.registerAuthenticationProvider(
|
||||||
|
// "iccoder",
|
||||||
|
// "IC Coder",
|
||||||
|
// authProvider
|
||||||
|
// )
|
||||||
|
// );
|
||||||
|
|
||||||
|
// 【已禁用】登录状态检查 - 直接打开聊天面板
|
||||||
|
vscode.commands.executeCommand("ic-coder.openChat");
|
||||||
|
|
||||||
// 注册命令:打开助手面板
|
// 注册命令:打开助手面板
|
||||||
const openPanelCommand = vscode.commands.registerCommand(
|
const openPanelCommand = vscode.commands.registerCommand(
|
||||||
@ -68,16 +145,80 @@ 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 openUserManualCommand = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.openUserManual",
|
||||||
|
() => {
|
||||||
|
UserManualPanel.render(context.extensionUri);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 注册命令:用户登录
|
// 注册命令:用户登录
|
||||||
const loginCommand = vscode.commands.registerCommand(
|
const loginCommand = vscode.commands.registerCommand(
|
||||||
"ic-coder.login",
|
"ic-coder.login",
|
||||||
async () => {
|
async (options?: { forceReauth?: boolean }) => {
|
||||||
try {
|
try {
|
||||||
await vscode.authentication.getSession("iccoder", [], { createIfNone: true });
|
const forceReauth = options?.forceReauth === true;
|
||||||
|
const session = await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
createIfNone: false,
|
||||||
|
});
|
||||||
|
const expired = session?.accessToken
|
||||||
|
? isTokenExpired(session.accessToken)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
// 会话仍有效时,直接打开聊天面板
|
||||||
|
if (session && expired === false && !forceReauth) {
|
||||||
|
vscode.commands.executeCommand("ic-coder.openChat");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1) 清空当前登录状态信息
|
||||||
|
await authProvider.clearSessionsForRelogin();
|
||||||
|
await context.globalState.update("icCoderSessions", []);
|
||||||
|
await context.globalState.update("icCoderUserInfo", undefined);
|
||||||
|
|
||||||
|
// 2) 重新登录(强制新会话)
|
||||||
|
await vscode.authentication.getSession("iccoder", [], {
|
||||||
|
clearSessionPreference: true,
|
||||||
|
forceNewSession: true,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
vscode.window.showErrorMessage(`登录失败: ${error}`);
|
vscode.window.showErrorMessage(`登录失败: ${error}`);
|
||||||
}
|
}
|
||||||
@ -91,12 +232,10 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
try {
|
try {
|
||||||
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
|
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
|
||||||
if (session) {
|
if (session) {
|
||||||
// 通过创建新会话并清除偏好来实现登出
|
// 调用 authProvider 的 removeSession 方法
|
||||||
await vscode.authentication.getSession("iccoder", [], {
|
await authProvider.removeSession(session.id);
|
||||||
clearSessionPreference: true,
|
// 清除邀请码验证状态
|
||||||
forceNewSession: true
|
await InvitationService.clearVerificationStatus(context);
|
||||||
});
|
|
||||||
vscode.window.showInformationMessage("已退出登录");
|
|
||||||
} else {
|
} else {
|
||||||
vscode.window.showInformationMessage("当前未登录");
|
vscode.window.showInformationMessage("当前未登录");
|
||||||
}
|
}
|
||||||
@ -106,6 +245,120 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 注册命令:更换邀请码
|
||||||
|
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] 测试通知命令执行完成');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册命令:将选中代码添加到对话
|
||||||
|
const addCodeToChat = vscode.commands.registerCommand(
|
||||||
|
"ic-coder.addCodeToChat",
|
||||||
|
async () => {
|
||||||
|
console.log('[addCodeToChat] 命令触发');
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (!editor) {
|
||||||
|
console.log('[addCodeToChat] 没有活动编辑器');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const selection = editor.selection;
|
||||||
|
const selectedText = editor.document.getText(selection);
|
||||||
|
|
||||||
|
if (!selectedText) {
|
||||||
|
vscode.window.showWarningMessage("请先选择代码");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileName = editor.document.fileName;
|
||||||
|
const startLine = selection.start.line + 1;
|
||||||
|
const endLine = selection.end.line + 1;
|
||||||
|
|
||||||
|
// 检查是否已有打开的面板
|
||||||
|
let panel = (global as any).currentICHelperPanel;
|
||||||
|
let needCreatePanel = false;
|
||||||
|
|
||||||
|
if (!panel) {
|
||||||
|
needCreatePanel = true;
|
||||||
|
} else {
|
||||||
|
// 尝试访问 webview,如果抛出异常说明已销毁
|
||||||
|
try {
|
||||||
|
const _ = panel.webview;
|
||||||
|
} catch (e) {
|
||||||
|
needCreatePanel = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[addCodeToChat] 需要创建面板:', needCreatePanel);
|
||||||
|
|
||||||
|
if (needCreatePanel) {
|
||||||
|
console.log('[addCodeToChat] 正在打开面板...');
|
||||||
|
await showICHelperPanel(context);
|
||||||
|
panel = (global as any).currentICHelperPanel;
|
||||||
|
console.log('[addCodeToChat] 面板打开后状态:', panel ? '成功' : '失败');
|
||||||
|
|
||||||
|
// 如果面板仍未创建(如未登录),直接返回
|
||||||
|
if (!panel) {
|
||||||
|
console.log('[addCodeToChat] 面板创建失败,退出');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送代码上下文
|
||||||
|
console.log('[addCodeToChat] 准备发送代码到面板');
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
if (panel?.webview) {
|
||||||
|
console.log('[addCodeToChat] 发送 addCodeContext 消息');
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: 'addCodeContext',
|
||||||
|
fileName,
|
||||||
|
startLine,
|
||||||
|
endLine,
|
||||||
|
code: selectedText,
|
||||||
|
languageId: editor.document.languageId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log('[addCodeToChat] 发送消息失败:', e);
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// 注册命令:查看会话历史
|
// 注册命令:查看会话历史
|
||||||
// TODO: 这些命令需要根据新的任务架构重新实现
|
// TODO: 这些命令需要根据新的任务架构重新实现
|
||||||
// 暂时注释掉,等待重新实现
|
// 暂时注释掉,等待重新实现
|
||||||
@ -157,7 +410,22 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
const viewProvider = new ICViewProvider(context.extensionUri, context);
|
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,
|
||||||
|
{
|
||||||
|
webviewOptions: {
|
||||||
|
retainContextWhenHidden: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 注册 VCD 自定义编辑器
|
||||||
|
const vcdEditorProvider = VCDViewerEditorProvider.register(context, vcdFileServer);
|
||||||
|
|
||||||
|
// 注册 Code Action Provider
|
||||||
|
const codeActionProvider = vscode.languages.registerCodeActionsProvider(
|
||||||
|
{ scheme: 'file' },
|
||||||
|
new ICCoderCodeActionProvider(),
|
||||||
|
{ providedCodeActionKinds: [vscode.CodeActionKind.RefactorRewrite] }
|
||||||
);
|
);
|
||||||
|
|
||||||
// 添加到订阅
|
// 添加到订阅
|
||||||
@ -165,8 +433,15 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
openPanelCommand,
|
openPanelCommand,
|
||||||
openChatCommand,
|
openChatCommand,
|
||||||
openVCDViewerCommand,
|
openVCDViewerCommand,
|
||||||
|
openVCDViewerInBrowserCommand,
|
||||||
|
openUserManualCommand,
|
||||||
loginCommand,
|
loginCommand,
|
||||||
logoutCommand,
|
logoutCommand,
|
||||||
|
changeInvitationCodeCommand,
|
||||||
|
testNotificationCommand,
|
||||||
|
addCodeToChat,
|
||||||
|
// testTrialUserCommand,
|
||||||
|
// testExpiredUserCommand,
|
||||||
// TODO: 等待重新实现这些命令
|
// TODO: 等待重新实现这些命令
|
||||||
// viewHistoryCommand,
|
// viewHistoryCommand,
|
||||||
// newSessionCommand,
|
// newSessionCommand,
|
||||||
@ -174,7 +449,9 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
// deleteSessionCommand,
|
// deleteSessionCommand,
|
||||||
// clearHistoryCommand,
|
// clearHistoryCommand,
|
||||||
// searchSessionCommand,
|
// searchSessionCommand,
|
||||||
viewRegistration
|
viewRegistration,
|
||||||
|
vcdEditorProvider,
|
||||||
|
codeActionProvider
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
78
src/panels/ExpiredPanel.ts
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/**
|
||||||
|
* 试用期到期提醒面板
|
||||||
|
* 功能:试用期到期时显示续费提示
|
||||||
|
* 依赖:vscode
|
||||||
|
* 使用场景:试用用户到期时显示
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export class ExpiredPanel {
|
||||||
|
public static render() {
|
||||||
|
const panel = vscode.window.createWebviewPanel(
|
||||||
|
'icCoderExpired',
|
||||||
|
'试用期已到期',
|
||||||
|
vscode.ViewColumn.One,
|
||||||
|
{ enableScripts: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
panel.webview.html = this.getHtmlContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getHtmlContent(): string {
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
padding: 60px 40px;
|
||||||
|
text-align: center;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
background-color: var(--vscode-editor-background);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: var(--vscode-errorForeground);
|
||||||
|
font-size: 28px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.6;
|
||||||
|
margin: 15px 0;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
padding: 12px 30px;
|
||||||
|
background: var(--vscode-button-background);
|
||||||
|
color: var(--vscode-button-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 16px;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background: var(--vscode-button-hoverBackground);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>⏰ 您的试用期已到期</h1>
|
||||||
|
<p>感谢您使用 IC Coder!您的 15 天试用期已结束。</p>
|
||||||
|
<p>如需继续使用,请联系我们获取正式版本。</p>
|
||||||
|
|
||||||
|
<button class="button" onclick="contact()">联系我们</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function contact() {
|
||||||
|
window.open('https://iccoder.com/contact', '_blank');
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,48 +9,58 @@ import {
|
|||||||
handleReplaceInFile,
|
handleReplaceInFile,
|
||||||
handleUserAnswer,
|
handleUserAnswer,
|
||||||
abortCurrentDialog,
|
abortCurrentDialog,
|
||||||
|
handleOptimizePrompt,
|
||||||
handlePlanAction,
|
handlePlanAction,
|
||||||
setPendingPlanExecution,
|
|
||||||
getCurrentTaskId,
|
getCurrentTaskId,
|
||||||
setLastTaskId,
|
setLastTaskId,
|
||||||
|
handleAcceptChange,
|
||||||
|
handleRejectChange,
|
||||||
|
startChangeSession,
|
||||||
|
handleOpenFileDiff,
|
||||||
} from "../utils/messageHandler";
|
} from "../utils/messageHandler";
|
||||||
|
import { setCustomConfig } from "../config/settings";
|
||||||
import { compactDialog } from "../services/apiClient";
|
import { compactDialog } from "../services/apiClient";
|
||||||
import { VCDViewerPanel } from "./VCDViewerPanel";
|
import { VCDViewerPanel } from "./VCDViewerPanel";
|
||||||
import { ChatHistoryManager } from "../utils/chatHistoryManager";
|
import { ChatHistoryManager } from "../utils/chatHistoryManager";
|
||||||
import { MessageType } from "../types/chatHistory";
|
import { MessageType } from "../types/chatHistory";
|
||||||
|
import { isTokenExpired } from "../utils/jwtUtils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建并显示 IC 助手面板
|
* 创建并显示 IC 助手面板
|
||||||
*/
|
*/
|
||||||
export async function showICHelperPanel(
|
export async function showICHelperPanel(
|
||||||
context: vscode.ExtensionContext,
|
context: vscode.ExtensionContext,
|
||||||
viewColumn?: vscode.ViewColumn
|
viewColumn?: vscode.ViewColumn,
|
||||||
) {
|
) {
|
||||||
// 检查用户是否已登录
|
// 创建WebView面板
|
||||||
try {
|
// try {
|
||||||
const session = await vscode.authentication.getSession("iccoder", [], {
|
// const session = await vscode.authentication.getSession("iccoder", [], {
|
||||||
createIfNone: false,
|
// createIfNone: false,
|
||||||
});
|
// });
|
||||||
if (!session) {
|
// if (!session) {
|
||||||
vscode.window
|
// vscode.window
|
||||||
.showWarningMessage("请先登录后再使用 IC Coder", "立即登录")
|
// .showWarningMessage("请先登录后再使用 IC Coder", "立即登录")
|
||||||
.then((selection) => {
|
// .then((selection) => {
|
||||||
if (selection === "立即登录") {
|
// if (selection === "立即登录") {
|
||||||
vscode.commands.executeCommand("ic-coder.login");
|
// vscode.commands.executeCommand("ic-coder.login", {
|
||||||
}
|
// forceReauth: true,
|
||||||
});
|
// });
|
||||||
return;
|
// }
|
||||||
}
|
// });
|
||||||
} catch (error) {
|
// return;
|
||||||
vscode.window
|
// }
|
||||||
.showWarningMessage("请先登录后再使用 IC Coder", "立即登录")
|
// } catch (error) {
|
||||||
.then((selection) => {
|
// vscode.window
|
||||||
if (selection === "立即登录") {
|
// .showWarningMessage("请先登录后再使用 IC Coder", "立即登录")
|
||||||
vscode.commands.executeCommand("ic-coder.login");
|
// .then((selection) => {
|
||||||
}
|
// if (selection === "立即登录") {
|
||||||
});
|
// vscode.commands.executeCommand("ic-coder.login", {
|
||||||
return;
|
// forceReauth: true,
|
||||||
}
|
// });
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
// 创建WebView面板
|
// 创建WebView面板
|
||||||
const panel = vscode.window.createWebviewPanel(
|
const panel = vscode.window.createWebviewPanel(
|
||||||
@ -62,41 +72,85 @@ export async function showICHelperPanel(
|
|||||||
retainContextWhenHidden: true,
|
retainContextWhenHidden: true,
|
||||||
localResourceRoots: [
|
localResourceRoots: [
|
||||||
vscode.Uri.joinPath(context.extensionUri, "media"),
|
vscode.Uri.joinPath(context.extensionUri, "media"),
|
||||||
vscode.Uri.joinPath(context.extensionUri, "src", "assets")
|
vscode.Uri.joinPath(context.extensionUri, "dist", "assets"),
|
||||||
],
|
],
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 保存 panel 引用到全局
|
||||||
|
(global as any).currentICHelperPanel = panel;
|
||||||
|
|
||||||
// 为面板生成唯一ID
|
// 为面板生成唯一ID
|
||||||
const panelId = `panel_${Date.now()}_${Math.random()
|
const panelId = `panel_${Date.now()}_${Math.random()
|
||||||
.toString(36)
|
.toString(36)
|
||||||
.substr(2, 9)}`;
|
.substr(2, 9)}`;
|
||||||
(panel as any).__uniqueId = panelId;
|
(panel as any).__uniqueId = panelId;
|
||||||
|
(panel as any).__context = context;
|
||||||
|
|
||||||
// 设置标签页图标
|
// 设置标签页图标
|
||||||
panel.iconPath = vscode.Uri.joinPath(
|
panel.iconPath = vscode.Uri.joinPath(
|
||||||
context.extensionUri,
|
context.extensionUri,
|
||||||
"media",
|
"media",
|
||||||
"icon.png"
|
"icon.png",
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取页面内图标URI
|
// 获取页面内图标URI
|
||||||
const iconUri = panel.webview.asWebviewUri(
|
const iconUri = panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(context.extensionUri, "media", "icon.png")
|
vscode.Uri.joinPath(context.extensionUri, "media", "icon.png"),
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取模型图标URI
|
// 获取模型图标URI
|
||||||
const autoIconUri = panel.webview.asWebviewUri(
|
const autoIconUri = panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "Auto.png")
|
vscode.Uri.joinPath(
|
||||||
|
context.extensionUri,
|
||||||
|
"dist",
|
||||||
|
"assets",
|
||||||
|
"model",
|
||||||
|
"Auto.png",
|
||||||
|
),
|
||||||
);
|
);
|
||||||
const liteIconUri = panel.webview.asWebviewUri(
|
const liteIconUri = panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "lite.png")
|
vscode.Uri.joinPath(
|
||||||
|
context.extensionUri,
|
||||||
|
"dist",
|
||||||
|
"assets",
|
||||||
|
"model",
|
||||||
|
"lite.png",
|
||||||
|
),
|
||||||
);
|
);
|
||||||
const syIconUri = panel.webview.asWebviewUri(
|
const syIconUri = panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "Sy.png")
|
vscode.Uri.joinPath(
|
||||||
|
context.extensionUri,
|
||||||
|
"dist",
|
||||||
|
"assets",
|
||||||
|
"model",
|
||||||
|
"Sy.png",
|
||||||
|
),
|
||||||
);
|
);
|
||||||
const maxIconUri = panel.webview.asWebviewUri(
|
const maxIconUri = panel.webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(context.extensionUri, "src", "assets", "model", "Max.png")
|
vscode.Uri.joinPath(
|
||||||
|
context.extensionUri,
|
||||||
|
"dist",
|
||||||
|
"assets",
|
||||||
|
"model",
|
||||||
|
"Max.png",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 获取二维码图片URI
|
||||||
|
const qrCodeUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(
|
||||||
|
context.extensionUri,
|
||||||
|
"dist",
|
||||||
|
"assets",
|
||||||
|
"QRCode",
|
||||||
|
"wx.png",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 获取Logo URI
|
||||||
|
const logoUri = panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(context.extensionUri, "media", "homepage-logo.png"),
|
||||||
);
|
);
|
||||||
|
|
||||||
// 设置HTML内容
|
// 设置HTML内容
|
||||||
@ -105,9 +159,30 @@ export async function showICHelperPanel(
|
|||||||
autoIconUri.toString(),
|
autoIconUri.toString(),
|
||||||
liteIconUri.toString(),
|
liteIconUri.toString(),
|
||||||
syIconUri.toString(),
|
syIconUri.toString(),
|
||||||
maxIconUri.toString()
|
maxIconUri.toString(),
|
||||||
|
qrCodeUri.toString(),
|
||||||
|
logoUri.toString(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 检查是否有待发送的消息
|
||||||
|
const pendingMessage = context.globalState.get("pendingMessage") as any;
|
||||||
|
if (pendingMessage) {
|
||||||
|
console.log("[ICHelperPanel] 检测到待发送消息,准备自动发送");
|
||||||
|
|
||||||
|
// 清除待发送消息
|
||||||
|
await context.globalState.update("pendingMessage", undefined);
|
||||||
|
|
||||||
|
// 延迟发送,确保面板已完全初始化
|
||||||
|
setTimeout(() => {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "autoSendMessage",
|
||||||
|
text: pendingMessage.text,
|
||||||
|
mode: pendingMessage.mode,
|
||||||
|
serviceTier: pendingMessage.serviceTier,
|
||||||
|
});
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
|
||||||
// 处理消息
|
// 处理消息
|
||||||
panel.webview.onDidReceiveMessage(
|
panel.webview.onDidReceiveMessage(
|
||||||
async (message) => {
|
async (message) => {
|
||||||
@ -125,12 +200,12 @@ export async function showICHelperPanel(
|
|||||||
try {
|
try {
|
||||||
const taskMeta = await historyManager.createTask(
|
const taskMeta = await historyManager.createTask(
|
||||||
workspacePath,
|
workspacePath,
|
||||||
"新对话"
|
"新对话",
|
||||||
);
|
);
|
||||||
historyManager.setPanelTask(
|
historyManager.setPanelTask(
|
||||||
panelId,
|
panelId,
|
||||||
taskMeta.taskId,
|
taskMeta.taskId,
|
||||||
workspacePath
|
workspacePath,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("创建任务失败:", error);
|
console.error("创建任务失败:", error);
|
||||||
@ -141,14 +216,20 @@ export async function showICHelperPanel(
|
|||||||
// 切换到当前面板的任务上下文
|
// 切换到当前面板的任务上下文
|
||||||
historyManager.switchToPanelTask(panelId);
|
historyManager.switchToPanelTask(panelId);
|
||||||
|
|
||||||
|
// 启动变更追踪会话
|
||||||
|
const sessionId = `session_${panelId}_${Date.now()}`;
|
||||||
|
startChangeSession(sessionId);
|
||||||
|
|
||||||
// 显示进度条
|
// 显示进度条
|
||||||
panel.webview.postMessage({ type: 'showProgress' });
|
panel.webview.postMessage({ type: "showProgress" });
|
||||||
|
|
||||||
handleUserMessage(
|
handleUserMessage(
|
||||||
panel,
|
panel,
|
||||||
message.text,
|
message.text,
|
||||||
context.extensionPath,
|
context.extensionPath,
|
||||||
message.mode
|
message.mode,
|
||||||
|
message.model, // 传递服务等级
|
||||||
|
message.contextItems, // 传递上下文项
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case "readFile":
|
case "readFile":
|
||||||
@ -165,7 +246,7 @@ export async function showICHelperPanel(
|
|||||||
panel,
|
panel,
|
||||||
message.filePath,
|
message.filePath,
|
||||||
message.searchText,
|
message.searchText,
|
||||||
message.replaceText
|
message.replaceText,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case "insertCode":
|
case "insertCode":
|
||||||
@ -175,11 +256,11 @@ export async function showICHelperPanel(
|
|||||||
vscode.window.showInformationMessage(message.text);
|
vscode.window.showInformationMessage(message.text);
|
||||||
break;
|
break;
|
||||||
case "openWaveformViewer":
|
case "openWaveformViewer":
|
||||||
// 打开波形查看器
|
// 在新列中打开波形查看器
|
||||||
if (message.vcdFilePath) {
|
if (message.vcdFilePath) {
|
||||||
VCDViewerPanel.createOrShow(
|
vscode.commands.executeCommand(
|
||||||
context.extensionUri,
|
"ic-coder.openVCDViewer",
|
||||||
message.vcdFilePath
|
message.vcdFilePath,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -198,7 +279,7 @@ export async function showICHelperPanel(
|
|||||||
loadConversationHistory(
|
loadConversationHistory(
|
||||||
panel,
|
panel,
|
||||||
message.offset || 0,
|
message.offset || 0,
|
||||||
message.limit || 10
|
message.limit || 10,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case "selectConversation":
|
case "selectConversation":
|
||||||
@ -207,16 +288,17 @@ export async function showICHelperPanel(
|
|||||||
selectConversation(
|
selectConversation(
|
||||||
panel,
|
panel,
|
||||||
message.conversationId,
|
message.conversationId,
|
||||||
context.extensionPath
|
context.extensionPath,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// 新增:处理用户回答
|
// 新增:处理用户回答
|
||||||
case "submitAnswer":
|
case "submitAnswer":
|
||||||
handleUserAnswer(
|
void handleUserAnswer(
|
||||||
message.askId,
|
message.askId,
|
||||||
message.selected,
|
message.selected,
|
||||||
message.customInput
|
message.customInput,
|
||||||
|
message.answers
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
// 新增:中止对话
|
// 新增:中止对话
|
||||||
@ -256,29 +338,188 @@ export async function showICHelperPanel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "optimizePrompt":
|
||||||
|
if (typeof message.prompt === "string") {
|
||||||
|
void handleOptimizePrompt(panel, message.prompt);
|
||||||
|
} else {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "optimizeResult",
|
||||||
|
success: false,
|
||||||
|
error: "提示词为空或格式错误",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "logout":
|
||||||
|
// 退出登录
|
||||||
|
vscode.commands.executeCommand("ic-coder.logout");
|
||||||
|
break;
|
||||||
|
case "openFile":
|
||||||
|
// 打开文件
|
||||||
|
if (message.filePath) {
|
||||||
|
const path = require('path');
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
const fullPath = path.isAbsolute(message.filePath) || !workspaceFolder
|
||||||
|
? message.filePath
|
||||||
|
: vscode.Uri.joinPath(workspaceFolder.uri, message.filePath).fsPath;
|
||||||
|
vscode.workspace.openTextDocument(fullPath).then(doc => {
|
||||||
|
vscode.window.showTextDocument(doc);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "openFileWithSelection":
|
||||||
|
// 打开文件并选中代码
|
||||||
|
if (message.filePath) {
|
||||||
|
const path = require('path');
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
const fullPath = path.isAbsolute(message.filePath) || !workspaceFolder
|
||||||
|
? message.filePath
|
||||||
|
: vscode.Uri.joinPath(workspaceFolder.uri, message.filePath).fsPath;
|
||||||
|
vscode.workspace.openTextDocument(fullPath).then(doc => {
|
||||||
|
vscode.window.showTextDocument(doc).then(editor => {
|
||||||
|
const start = new vscode.Position(message.startLine - 1, 0);
|
||||||
|
const end = new vscode.Position(message.endLine - 1, doc.lineAt(message.endLine - 1).text.length);
|
||||||
|
editor.selection = new vscode.Selection(start, end);
|
||||||
|
editor.revealRange(new vscode.Range(start, end));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "openFilePathTag":
|
||||||
|
// 打开文件路径标签(智能查找)
|
||||||
|
if (message.filePath) {
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
|
||||||
|
let fullPath = message.filePath;
|
||||||
|
|
||||||
|
// 如果是相对路径且工作区存在
|
||||||
|
if (!path.isAbsolute(message.filePath) && workspaceFolder) {
|
||||||
|
const candidatePath = vscode.Uri.joinPath(workspaceFolder.uri, message.filePath).fsPath;
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (fs.existsSync(candidatePath)) {
|
||||||
|
fullPath = candidatePath;
|
||||||
|
} else {
|
||||||
|
// 尝试在工作区中搜索该文件
|
||||||
|
const fileName = path.basename(message.filePath);
|
||||||
|
const files = await vscode.workspace.findFiles(`**/${fileName}`, '**/node_modules/**', 1);
|
||||||
|
if (files.length > 0) {
|
||||||
|
fullPath = files[0].fsPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.startLine && message.endLine) {
|
||||||
|
vscode.workspace.openTextDocument(fullPath).then(doc => {
|
||||||
|
vscode.window.showTextDocument(doc).then(editor => {
|
||||||
|
const start = new vscode.Position(message.startLine - 1, 0);
|
||||||
|
const end = new vscode.Position(message.endLine - 1, doc.lineAt(message.endLine - 1).text.length);
|
||||||
|
editor.selection = new vscode.Selection(start, end);
|
||||||
|
editor.revealRange(new vscode.Range(start, end));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
vscode.workspace.openTextDocument(fullPath).then(doc => {
|
||||||
|
vscode.window.showTextDocument(doc);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "acceptChange":
|
||||||
|
// 采纳变更
|
||||||
|
if (message.changeId) {
|
||||||
|
await handleAcceptChange(panel, message.changeId);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "rejectChange":
|
||||||
|
// 拒绝变更
|
||||||
|
if (message.changeId) {
|
||||||
|
await handleRejectChange(panel, message.changeId);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "openFileDiff":
|
||||||
|
// 打开文件 diff
|
||||||
|
if (message.changeId) {
|
||||||
|
await handleOpenFileDiff(panel, message.changeId);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "checkInvitationCode":
|
||||||
|
// 【已禁用】检查邀请码验证状态 - 现在所有用户都可以直接使用
|
||||||
|
{
|
||||||
|
// 直接返回已验证,无需登录和邀请码
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "invitationCodeStatus",
|
||||||
|
verified: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "checkWelcomeModal":
|
||||||
|
// 【已禁用】检查是否需要显示欢迎弹窗 - 无需登录,不显示欢迎弹窗
|
||||||
|
break;
|
||||||
|
case "checkTrialExpiration":
|
||||||
|
// 检查试用期是否过期
|
||||||
|
{
|
||||||
|
console.log("[ICHelperPanel] 收到 checkTrialExpiration 消息");
|
||||||
|
const {
|
||||||
|
TrialExpirationService,
|
||||||
|
} = require("../services/trialExpirationService");
|
||||||
|
const trialService = new TrialExpirationService(context, panel);
|
||||||
|
const isExpired = await trialService.checkExpiration();
|
||||||
|
console.log("[ICHelperPanel] 试用期过期状态:", isExpired);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "verifyInvitationCode":
|
||||||
|
// 【已禁用】验证邀请码 - 无需邀请码验证
|
||||||
|
{
|
||||||
|
// 直接返回验证成功
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "invitationCodeVerified",
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "openICCoder":
|
||||||
|
// 跳转到 IC Coder 官网
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse("https://www.iccoder.com"));
|
||||||
|
break;
|
||||||
|
case "openTutorial":
|
||||||
|
// 打开使用教程
|
||||||
|
vscode.env.openExternal(
|
||||||
|
vscode.Uri.parse(
|
||||||
|
"https://www.iccoder.com/guides/quick-start/first-task-plugin",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "openUserManual":
|
||||||
|
// 打开用户手册
|
||||||
|
vscode.commands.executeCommand("ic-coder.openUserManual");
|
||||||
|
break;
|
||||||
|
case "openUserFeedback":
|
||||||
|
// 打开用户反馈二维码弹窗
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "showFeedbackQRCode",
|
||||||
|
});
|
||||||
|
break;
|
||||||
// 处理计划操作(只做模式切换,响应已通过 submitAnswer 发送)
|
// 处理计划操作(只做模式切换,响应已通过 submitAnswer 发送)
|
||||||
case "planAction":
|
case "planAction":
|
||||||
if (message.action === "confirm") {
|
if (message.action === "confirm") {
|
||||||
// 确认执行:切换到 Agent 模式
|
// 确认执行:切换到 Agent 模式(UI 切换)
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "switchMode",
|
command: "switchMode",
|
||||||
mode: "agent",
|
mode: "agent",
|
||||||
});
|
});
|
||||||
// 获取当前会话的 taskId,用于复用知识图谱数据
|
// 注意:不再设置待执行计划;后端 LLM 会在同一对话中自动执行计划
|
||||||
const taskId = getCurrentTaskId();
|
} else if (
|
||||||
if (taskId) {
|
message.action === "modify" ||
|
||||||
// 设置待执行的计划,对话结束后自动执行(复用 taskId)
|
message.action === "cancel"
|
||||||
setPendingPlanExecution(
|
) {
|
||||||
panel,
|
void handlePlanAction(
|
||||||
message.planTitle || "计划",
|
panel,
|
||||||
context.extensionPath,
|
message.action,
|
||||||
taskId
|
message.planTitle || "",
|
||||||
);
|
context.extensionPath,
|
||||||
} else {
|
message.model,
|
||||||
console.warn(
|
);
|
||||||
"[ICHelperPanel] 无法获取当前 taskId,知识图谱数据可能丢失"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// 添加文件上下文 - 显示工作区文件列表
|
// 添加文件上下文 - 显示工作区文件列表
|
||||||
@ -293,7 +534,7 @@ export async function showICHelperPanel(
|
|||||||
// 获取工作区所有文件
|
// 获取工作区所有文件
|
||||||
const files = await vscode.workspace.findFiles(
|
const files = await vscode.workspace.findFiles(
|
||||||
"**/*",
|
"**/*",
|
||||||
"**/node_modules/**"
|
"**/node_modules/**",
|
||||||
);
|
);
|
||||||
|
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
@ -323,7 +564,11 @@ export async function showICHelperPanel(
|
|||||||
try {
|
try {
|
||||||
const items = fs.readdirSync(dir, { withFileTypes: true });
|
const items = fs.readdirSync(dir, { withFileTypes: true });
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
if (item.isDirectory() && item.name !== "node_modules" && !item.name.startsWith(".")) {
|
if (
|
||||||
|
item.isDirectory() &&
|
||||||
|
item.name !== "node_modules" &&
|
||||||
|
!item.name.startsWith(".")
|
||||||
|
) {
|
||||||
const fullPath = path.join(dir, item.name);
|
const fullPath = path.join(dir, item.name);
|
||||||
const relativePath = path.relative(baseDir, fullPath);
|
const relativePath = path.relative(baseDir, fullPath);
|
||||||
folders.push({ path: fullPath, relativePath });
|
folders.push({ path: fullPath, relativePath });
|
||||||
@ -352,7 +597,7 @@ export async function showICHelperPanel(
|
|||||||
canSelectMany: true,
|
canSelectMany: true,
|
||||||
openLabel: "选择图片",
|
openLabel: "选择图片",
|
||||||
filters: {
|
filters: {
|
||||||
"图片文件": ["png", "jpg", "jpeg", "gif", "bmp", "svg", "webp"],
|
图片文件: ["png", "jpg", "jpeg", "gif", "bmp", "svg", "webp"],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (imageUris && imageUris.length > 0) {
|
if (imageUris && imageUris.length > 0) {
|
||||||
@ -372,8 +617,8 @@ export async function showICHelperPanel(
|
|||||||
canSelectMany: true,
|
canSelectMany: true,
|
||||||
openLabel: "选择文档",
|
openLabel: "选择文档",
|
||||||
filters: {
|
filters: {
|
||||||
"文档文件": ["pdf", "doc", "docx", "txt", "md"],
|
文档文件: ["pdf", "doc", "docx", "txt", "md"],
|
||||||
"所有文件": ["*"],
|
所有文件: ["*"],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (docUris && docUris.length > 0) {
|
if (docUris && docUris.length > 0) {
|
||||||
@ -384,6 +629,23 @@ export async function showICHelperPanel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
// 打开文件
|
||||||
|
case "openFile":
|
||||||
|
{
|
||||||
|
let filePath = message.filePath;
|
||||||
|
if (filePath) {
|
||||||
|
// 如果是相对路径,转换为绝对路径
|
||||||
|
if (!require("path").isAbsolute(filePath)) {
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
if (workspaceFolder) {
|
||||||
|
filePath = require("path").join(workspaceFolder.uri.fsPath, filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const uri = vscode.Uri.file(filePath);
|
||||||
|
vscode.window.showTextDocument(uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
// 新增:检查工作区状态
|
// 新增:检查工作区状态
|
||||||
case "checkWorkspace":
|
case "checkWorkspace":
|
||||||
const hasWorkspace = !!(
|
const hasWorkspace = !!(
|
||||||
@ -395,7 +657,7 @@ export async function showICHelperPanel(
|
|||||||
vscode.window
|
vscode.window
|
||||||
.showWarningMessage(
|
.showWarningMessage(
|
||||||
"请先打开一个文件夹作为工作区,这样我就能更好地为您服务了 😊",
|
"请先打开一个文件夹作为工作区,这样我就能更好地为您服务了 😊",
|
||||||
"打开文件夹"
|
"打开文件夹",
|
||||||
)
|
)
|
||||||
.then((selection) => {
|
.then((selection) => {
|
||||||
if (selection === "打开文件夹") {
|
if (selection === "打开文件夹") {
|
||||||
@ -409,10 +671,39 @@ export async function showICHelperPanel(
|
|||||||
hasWorkspace: hasWorkspace,
|
hasWorkspace: hasWorkspace,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
case "openExternalUrl":
|
||||||
|
// 打开外部链接
|
||||||
|
if (message.url) {
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse(message.url));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "openICCoder":
|
||||||
|
// 打开 IC Coder 官网
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse("https://www.iccoder.com"));
|
||||||
|
break;
|
||||||
|
case "logout":
|
||||||
|
// 退出登录(前端已有确认对话框)
|
||||||
|
vscode.commands.executeCommand("ic-coder.logout");
|
||||||
|
break;
|
||||||
|
case "saveGeneralSettings":
|
||||||
|
// 保存通用设置
|
||||||
|
context.globalState.update('generalSettings', message.settings);
|
||||||
|
// 更新运行时配置(包括清空)
|
||||||
|
setCustomConfig({ backendUrl: message.settings.backendUrl || '' });
|
||||||
|
vscode.window.showInformationMessage('设置已保存');
|
||||||
|
break;
|
||||||
|
case "loadGeneralSettings":
|
||||||
|
// 加载通用设置
|
||||||
|
const settings = context.globalState.get('generalSettings');
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: 'loadedGeneralSettings',
|
||||||
|
settings: settings
|
||||||
|
});
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
undefined,
|
undefined,
|
||||||
context.subscriptions
|
context.subscriptions,
|
||||||
);
|
);
|
||||||
|
|
||||||
// 面板关闭时清理任务映射
|
// 面板关闭时清理任务映射
|
||||||
@ -423,7 +714,7 @@ export async function showICHelperPanel(
|
|||||||
historyManager.removePanelTask(panelId);
|
historyManager.removePanelTask(panelId);
|
||||||
},
|
},
|
||||||
undefined,
|
undefined,
|
||||||
context.subscriptions
|
context.subscriptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -433,7 +724,7 @@ export async function showICHelperPanel(
|
|||||||
async function getVCDFileInfo(
|
async function getVCDFileInfo(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
vcdFilePath: string,
|
vcdFilePath: string,
|
||||||
containerId: string
|
containerId: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
@ -571,7 +862,7 @@ function parseVCDSignals(content: string, maxSignals: number = 3) {
|
|||||||
if (signalDef.width === 1) {
|
if (signalDef.width === 1) {
|
||||||
// 单比特信号
|
// 单比特信号
|
||||||
const singleBitMatch = trimmedLine.match(
|
const singleBitMatch = trimmedLine.match(
|
||||||
new RegExp(`^([01xz])${signalDef.identifier}$`)
|
new RegExp(`^([01xz])${signalDef.identifier}$`),
|
||||||
);
|
);
|
||||||
if (singleBitMatch) {
|
if (singleBitMatch) {
|
||||||
values.push({ time: currentTime, value: singleBitMatch[1] });
|
values.push({ time: currentTime, value: singleBitMatch[1] });
|
||||||
@ -579,7 +870,7 @@ function parseVCDSignals(content: string, maxSignals: number = 3) {
|
|||||||
} else {
|
} else {
|
||||||
// 多比特信号
|
// 多比特信号
|
||||||
const multiBitMatch = trimmedLine.match(
|
const multiBitMatch = trimmedLine.match(
|
||||||
new RegExp(`^b([01xz]+)\\s+${signalDef.identifier}$`)
|
new RegExp(`^b([01xz]+)\\s+${signalDef.identifier}$`),
|
||||||
);
|
);
|
||||||
if (multiBitMatch) {
|
if (multiBitMatch) {
|
||||||
values.push({ time: currentTime, value: multiBitMatch[1] });
|
values.push({ time: currentTime, value: multiBitMatch[1] });
|
||||||
@ -612,7 +903,7 @@ function parseVCDSignals(content: string, maxSignals: number = 3) {
|
|||||||
async function loadConversationHistory(
|
async function loadConversationHistory(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
offset: number = 0,
|
offset: number = 0,
|
||||||
limit: number = 10
|
limit: number = 10,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const historyManager = ChatHistoryManager.getInstance();
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
@ -633,7 +924,7 @@ async function loadConversationHistory(
|
|||||||
const result = await historyManager.getConversationHistoryList(
|
const result = await historyManager.getConversationHistoryList(
|
||||||
workspacePath,
|
workspacePath,
|
||||||
offset,
|
offset,
|
||||||
limit
|
limit,
|
||||||
);
|
);
|
||||||
|
|
||||||
// 发送会话历史到前端
|
// 发送会话历史到前端
|
||||||
@ -661,7 +952,7 @@ async function loadConversationHistory(
|
|||||||
async function selectConversation(
|
async function selectConversation(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
taskId: string,
|
taskId: string,
|
||||||
extensionPath: string
|
extensionPath: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const historyManager = ChatHistoryManager.getInstance();
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
@ -675,12 +966,12 @@ async function selectConversation(
|
|||||||
// 加载任务会话
|
// 加载任务会话
|
||||||
const taskSession = await historyManager.loadTaskSession(
|
const taskSession = await historyManager.loadTaskSession(
|
||||||
workspacePath,
|
workspacePath,
|
||||||
taskId
|
taskId,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!taskSession) {
|
if (!taskSession) {
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
`加载任务 ${taskId} 失败: 任务不存在或数据损坏`
|
`加载任务 ${taskId} 失败: 任务不存在或数据损坏`,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -841,7 +1132,7 @@ async function selectConversation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(
|
||||||
`已加载会话: ${taskSession.meta.taskName}`
|
`已加载会话: ${taskSession.meta.taskName}`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("选择会话失败:", error);
|
console.error("选择会话失败:", error);
|
||||||
|
|||||||
181
src/panels/UserManualPanel.ts
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
/**
|
||||||
|
* 用户手册只读预览面板
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
|
export class UserManualPanel {
|
||||||
|
public static currentPanel: UserManualPanel | undefined;
|
||||||
|
private readonly _panel: vscode.WebviewPanel;
|
||||||
|
private _disposables: vscode.Disposable[] = [];
|
||||||
|
|
||||||
|
private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
|
||||||
|
this._panel = panel;
|
||||||
|
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
|
||||||
|
this._update(extensionUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static render(extensionUri: vscode.Uri) {
|
||||||
|
if (UserManualPanel.currentPanel) {
|
||||||
|
UserManualPanel.currentPanel._panel.reveal(vscode.ViewColumn.One);
|
||||||
|
} else {
|
||||||
|
const panel = vscode.window.createWebviewPanel(
|
||||||
|
"userManual",
|
||||||
|
"IC Coder 用户手册",
|
||||||
|
vscode.ViewColumn.One,
|
||||||
|
{
|
||||||
|
enableScripts: true,
|
||||||
|
localResourceRoots: [vscode.Uri.joinPath(extensionUri, "media")],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
UserManualPanel.currentPanel = new UserManualPanel(panel, extensionUri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async _update(extensionUri: vscode.Uri) {
|
||||||
|
const manualPath = vscode.Uri.joinPath(
|
||||||
|
extensionUri,
|
||||||
|
"media",
|
||||||
|
"USER_MANUAL.md",
|
||||||
|
);
|
||||||
|
const markdown = await vscode.workspace.fs.readFile(manualPath);
|
||||||
|
const content = Buffer.from(markdown).toString("utf-8");
|
||||||
|
this._panel.webview.html = await this._getHtmlContent(
|
||||||
|
content,
|
||||||
|
extensionUri,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async _getHtmlContent(
|
||||||
|
markdown: string,
|
||||||
|
extensionUri: vscode.Uri,
|
||||||
|
): Promise<string> {
|
||||||
|
let inCodeBlock = false;
|
||||||
|
let inTable = false;
|
||||||
|
let tableRows: string[] = [];
|
||||||
|
const lines: string[] = [];
|
||||||
|
|
||||||
|
// 先处理图片
|
||||||
|
markdown = markdown.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_, alt, src) => {
|
||||||
|
const imgUri = this._panel.webview.asWebviewUri(
|
||||||
|
vscode.Uri.joinPath(extensionUri, "media", src),
|
||||||
|
);
|
||||||
|
return `<img src="${imgUri}" alt="${alt}">`;
|
||||||
|
});
|
||||||
|
|
||||||
|
markdown.split("\n").forEach((line) => {
|
||||||
|
// 代码块
|
||||||
|
if (line.startsWith("```")) {
|
||||||
|
if (inCodeBlock) {
|
||||||
|
lines.push("</code></pre>");
|
||||||
|
inCodeBlock = false;
|
||||||
|
} else {
|
||||||
|
lines.push("<pre><code>");
|
||||||
|
inCodeBlock = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (inCodeBlock) {
|
||||||
|
lines.push(line);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 表格
|
||||||
|
if (line.startsWith("|")) {
|
||||||
|
if (!inTable) inTable = true;
|
||||||
|
tableRows.push(line);
|
||||||
|
return;
|
||||||
|
} else if (inTable) {
|
||||||
|
// 表格结束
|
||||||
|
const headers = tableRows[0]
|
||||||
|
.split("|")
|
||||||
|
.filter((c) => c.trim())
|
||||||
|
.map((h) => `<th>${h.trim()}</th>`)
|
||||||
|
.join("");
|
||||||
|
const body = tableRows
|
||||||
|
.slice(2)
|
||||||
|
.map(
|
||||||
|
(r) =>
|
||||||
|
"<tr>" +
|
||||||
|
r
|
||||||
|
.split("|")
|
||||||
|
.filter((c) => c.trim())
|
||||||
|
.map((c) => `<td>${c.trim()}</td>`)
|
||||||
|
.join("") +
|
||||||
|
"</tr>",
|
||||||
|
)
|
||||||
|
.join("");
|
||||||
|
lines.push(
|
||||||
|
`<table><thead><tr>${headers}</tr></thead><tbody>${body}</tbody></table>`,
|
||||||
|
);
|
||||||
|
tableRows = [];
|
||||||
|
inTable = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他行
|
||||||
|
if (line === "---") lines.push("<hr>");
|
||||||
|
else if (line.startsWith("#### "))
|
||||||
|
lines.push(`<h4>${line.slice(5)}</h4>`);
|
||||||
|
else if (line.startsWith("### ")) lines.push(`<h3>${line.slice(4)}</h3>`);
|
||||||
|
else if (line.startsWith("## ")) lines.push(`<h2>${line.slice(3)}</h2>`);
|
||||||
|
else if (line.startsWith("# ")) lines.push(`<h1>${line.slice(2)}</h1>`);
|
||||||
|
else if (line.startsWith("- "))
|
||||||
|
lines.push(
|
||||||
|
`<li>${line.slice(2).replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>")}</li>`,
|
||||||
|
);
|
||||||
|
else if (line.trim() === "") lines.push("<p></p>");
|
||||||
|
else
|
||||||
|
lines.push(
|
||||||
|
`<p>${line.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>").replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2">$1</a>')}</p>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const html = lines
|
||||||
|
.join("\n")
|
||||||
|
.replace(/((?:<li>.*<\/li>\n?)+)/g, "<ul>$1</ul>");
|
||||||
|
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
padding: 40px;
|
||||||
|
line-height: 1.8;
|
||||||
|
font-size: 16px;
|
||||||
|
max-width: 1000px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
h1 { font-size: 2em; border-bottom: 3px solid #ddd; padding-bottom: 15px; margin: 30px 0 20px; }
|
||||||
|
h2 { font-size: 1.6em; margin-top: 40px; border-bottom: 2px solid #eee; padding-bottom: 10px; }
|
||||||
|
h3 { font-size: 1.3em; margin-top: 30px; }
|
||||||
|
h4 { font-size: 1.1em; margin-top: 20px; font-weight: 600; }
|
||||||
|
p { margin: 15px 0; }
|
||||||
|
img { display: block; margin: 30px auto; max-width: 100%; border: 1px solid #ddd; border-radius: 6px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
|
||||||
|
table { border-collapse: collapse; width: 100%; margin: 25px 0; font-size: 15px; }
|
||||||
|
th, td { border: 1px solid #ddd; padding: 12px 16px; text-align: left; }
|
||||||
|
th { background: #636363; font-weight: 600; }
|
||||||
|
tr:hover { background: #636363; }
|
||||||
|
ul { margin: 15px 0; padding-left: 30px; }
|
||||||
|
li { margin: 8px 0; margin-left: 40px;}
|
||||||
|
pre { background: #2f2f2f; padding: 20px; border-radius: 6px; overflow-x: auto; margin: 20px 0; border: 1px solid #e0e0e0; }
|
||||||
|
code { font-family: 'Consolas', 'Monaco', monospace; font-size: 14px; line-height: 1.6; }
|
||||||
|
hr { border: none; border-top: 2px solid #e0e0e0; margin: 30px 0; }
|
||||||
|
a { color: #0066cc; text-decoration: none; }
|
||||||
|
a:hover { text-decoration: underline; }
|
||||||
|
strong { font-weight: 600; color: #e5e5e5; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>${html}</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
public dispose() {
|
||||||
|
UserManualPanel.currentPanel = undefined;
|
||||||
|
this._panel.dispose();
|
||||||
|
while (this._disposables.length) {
|
||||||
|
this._disposables.pop()?.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,19 +1,86 @@
|
|||||||
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,24 +91,40 @@ 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,
|
||||||
this._disposables
|
this._disposables,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建或显示 VCD 查看器面板
|
* 创建或显示 VCD 查看器面板
|
||||||
*/
|
*/
|
||||||
public static createOrShow(extensionUri: vscode.Uri, vcdFilePath?: string) {
|
public static createOrShow(
|
||||||
const column = vscode.ViewColumn.One;
|
extensionUri: vscode.Uri,
|
||||||
|
vcdFilePath?: string,
|
||||||
|
vcdFileServer?: VCDFileServer,
|
||||||
|
) {
|
||||||
|
// 在当前活动编辑器旁边打开新列
|
||||||
|
const column = vscode.ViewColumn.Beside;
|
||||||
|
|
||||||
// 如果已经有面板打开,则显示它
|
// 如果已经有面板打开,则显示它
|
||||||
if (VCDViewerPanel.currentPanel) {
|
if (VCDViewerPanel.currentPanel) {
|
||||||
@ -61,10 +144,14 @@ export class VCDViewerPanel {
|
|||||||
enableScripts: true,
|
enableScripts: true,
|
||||||
retainContextWhenHidden: true,
|
retainContextWhenHidden: true,
|
||||||
localResourceRoots: [extensionUri],
|
localResourceRoots: [extensionUri],
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
VCDViewerPanel.currentPanel = new VCDViewerPanel(panel, extensionUri);
|
VCDViewerPanel.currentPanel = new VCDViewerPanel(
|
||||||
|
panel,
|
||||||
|
extensionUri,
|
||||||
|
vcdFileServer,
|
||||||
|
);
|
||||||
|
|
||||||
// 如果提供了 VCD 文件路径,加载它
|
// 如果提供了 VCD 文件路径,加载它
|
||||||
if (vcdFilePath) {
|
if (vcdFilePath) {
|
||||||
@ -72,26 +159,145 @@ 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 = `波形查看器 - ${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 : "未知错误"}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 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 +369,249 @@ 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>波形查看器</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>`;
|
||||||
|
|||||||
153
src/panels/WelcomePanel.ts
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
/**
|
||||||
|
* 欢迎引导面板
|
||||||
|
* 功能:插件试用用户首次登录显示使用教程
|
||||||
|
* 依赖:vscode
|
||||||
|
* 使用场景:试用用户首次登录时显示
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export class WelcomePanel {
|
||||||
|
public static currentPanel: WelcomePanel | undefined;
|
||||||
|
private readonly _panel: vscode.WebviewPanel;
|
||||||
|
private _disposables: vscode.Disposable[] = [];
|
||||||
|
|
||||||
|
private constructor(panel: vscode.WebviewPanel) {
|
||||||
|
this._panel = panel;
|
||||||
|
this._panel.webview.html = this.getHtmlContent();
|
||||||
|
|
||||||
|
// 监听来自 webview 的消息
|
||||||
|
this._panel.webview.onDidReceiveMessage(
|
||||||
|
(message) => {
|
||||||
|
if (message.command === 'close') {
|
||||||
|
this._panel.dispose();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
this._disposables
|
||||||
|
);
|
||||||
|
|
||||||
|
// 监听关闭事件
|
||||||
|
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static render(context: vscode.ExtensionContext) {
|
||||||
|
// 避免重复显示
|
||||||
|
if (WelcomePanel.currentPanel) {
|
||||||
|
WelcomePanel.currentPanel._panel.reveal(vscode.ViewColumn.One);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const panel = vscode.window.createWebviewPanel(
|
||||||
|
'icCoderWelcome',
|
||||||
|
'欢迎使用 IC Coder',
|
||||||
|
vscode.ViewColumn.One,
|
||||||
|
{
|
||||||
|
enableScripts: true,
|
||||||
|
retainContextWhenHidden: true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
WelcomePanel.currentPanel = new WelcomePanel(panel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private getHtmlContent(): string {
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>欢迎使用 IC Coder</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
padding: 40px;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: var(--vscode-foreground);
|
||||||
|
background-color: var(--vscode-editor-background);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: var(--vscode-textLink-foreground);
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.welcome-message {
|
||||||
|
font-size: 16px;
|
||||||
|
margin-bottom: 30px;
|
||||||
|
color: var(--vscode-descriptionForeground);
|
||||||
|
}
|
||||||
|
.step {
|
||||||
|
margin: 20px 0;
|
||||||
|
padding: 20px;
|
||||||
|
background: var(--vscode-editor-inactiveSelectionBackground);
|
||||||
|
border-radius: 8px;
|
||||||
|
border-left: 4px solid var(--vscode-textLink-foreground);
|
||||||
|
}
|
||||||
|
.step h3 {
|
||||||
|
margin-top: 0;
|
||||||
|
color: var(--vscode-textLink-foreground);
|
||||||
|
}
|
||||||
|
.step p {
|
||||||
|
margin: 10px 0;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
padding: 12px 24px;
|
||||||
|
background: var(--vscode-button-background);
|
||||||
|
color: var(--vscode-button-foreground);
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background: var(--vscode-button-hoverBackground);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>🎉 欢迎使用 IC Coder!</h1>
|
||||||
|
<p class="welcome-message">
|
||||||
|
您已成功激活 15 天试用期,让我们开始探索 IC Coder 的强大功能吧!
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="step">
|
||||||
|
<h3>📝 步骤 1:打开聊天面板</h3>
|
||||||
|
<p>点击侧边栏的 IC Coder 图标,或使用命令面板搜索 "IC Coder: Open Chat"</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="step">
|
||||||
|
<h3>💬 步骤 2:输入您的需求</h3>
|
||||||
|
<p>描述您想要生成的 Verilog 代码或需要帮助的问题,AI 将为您提供专业的解决方案</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="step">
|
||||||
|
<h3>🔬 步骤 3:运行仿真</h3>
|
||||||
|
<p>使用 "生成 VCD" 命令运行 iverilog 仿真,并通过波形查看器查看仿真结果</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button class="button" onclick="closePanel()">开始使用</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const vscode = acquireVsCodeApi();
|
||||||
|
|
||||||
|
function closePanel() {
|
||||||
|
vscode.postMessage({ command: 'close' });
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
public dispose() {
|
||||||
|
WelcomePanel.currentPanel = undefined;
|
||||||
|
this._panel.dispose();
|
||||||
|
while (this._disposables.length) {
|
||||||
|
const disposable = this._disposables.pop();
|
||||||
|
if (disposable) {
|
||||||
|
disposable.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/providers/codeActionProvider.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* Code Action Provider - 为选中代码提供快捷操作
|
||||||
|
* 功能:在小灯泡菜单中显示"添加到 IC Coder 对话"选项
|
||||||
|
*/
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export class ICCoderCodeActionProvider implements vscode.CodeActionProvider {
|
||||||
|
provideCodeActions(
|
||||||
|
document: vscode.TextDocument,
|
||||||
|
range: vscode.Range
|
||||||
|
): vscode.CodeAction[] {
|
||||||
|
const selectedText = document.getText(range);
|
||||||
|
if (!selectedText) return [];
|
||||||
|
|
||||||
|
const action = new vscode.CodeAction(
|
||||||
|
'💬 添加到 IC Coder 对话',
|
||||||
|
vscode.CodeActionKind.RefactorRewrite
|
||||||
|
);
|
||||||
|
action.command = {
|
||||||
|
command: 'ic-coder.addCodeToChat',
|
||||||
|
title: '添加到对话'
|
||||||
|
};
|
||||||
|
|
||||||
|
return [action];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,22 +2,47 @@
|
|||||||
* API 客户端
|
* API 客户端
|
||||||
* 封装与后端的 HTTP 通信
|
* 封装与后端的 HTTP 通信
|
||||||
*/
|
*/
|
||||||
import * as https from 'https';
|
import * as vscode from "vscode";
|
||||||
import * as http from 'http';
|
import * as https from "https";
|
||||||
import { URL } from 'url';
|
import * as http from "http";
|
||||||
import { getApiUrl, getConfig } from '../config/settings';
|
import { URL } from "url";
|
||||||
import type { ToolCallResult, AnswerRequest, ToolResultResponse, AnswerResponse, ToolConfirmResponse } from '../types/api';
|
import { getApiUrl, getConfig } from "../config/settings";
|
||||||
|
import type {
|
||||||
|
ToolCallResult,
|
||||||
|
AnswerRequest,
|
||||||
|
ToolResultResponse,
|
||||||
|
AnswerResponse,
|
||||||
|
ToolConfirmResponse,
|
||||||
|
UserInfoResponse,
|
||||||
|
InvitationVerifyRequest,
|
||||||
|
InvitationVerifyResponse,
|
||||||
|
InvitationStatusResponse,
|
||||||
|
} from "../types/api";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP 请求选项
|
* HTTP 请求选项
|
||||||
*/
|
*/
|
||||||
interface RequestOptions {
|
interface RequestOptions {
|
||||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
method: "GET" | "POST" | "PUT" | "DELETE";
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
body?: unknown;
|
body?: unknown;
|
||||||
timeout?: number;
|
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 请求
|
* 发送 HTTP 请求
|
||||||
*/
|
*/
|
||||||
@ -25,7 +50,10 @@ async function request<T>(path: string, options: RequestOptions): Promise<T> {
|
|||||||
const url = new URL(getApiUrl(path));
|
const url = new URL(getApiUrl(path));
|
||||||
const { timeout } = getConfig();
|
const { timeout } = getConfig();
|
||||||
|
|
||||||
const isHttps = url.protocol === 'https:';
|
// 自动获取 Token
|
||||||
|
const token = await getAuthToken();
|
||||||
|
|
||||||
|
const isHttps = url.protocol === "https:";
|
||||||
const httpModule = isHttps ? https : http;
|
const httpModule = isHttps ? https : http;
|
||||||
|
|
||||||
const requestOptions: http.RequestOptions = {
|
const requestOptions: http.RequestOptions = {
|
||||||
@ -34,48 +62,84 @@ async function request<T>(path: string, options: RequestOptions): Promise<T> {
|
|||||||
path: url.pathname + url.search,
|
path: url.pathname + url.search,
|
||||||
method: options.method,
|
method: options.method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
...options.headers
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
|
...options.headers,
|
||||||
},
|
},
|
||||||
timeout: options.timeout || timeout
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = httpModule.request(requestOptions, (res) => {
|
const req = httpModule.request(requestOptions, (res) => {
|
||||||
let data = '';
|
let data = "";
|
||||||
|
|
||||||
res.on('data', (chunk) => {
|
// console.log('[HTTP] 响应状态码:', res.statusCode);
|
||||||
|
// console.log('[HTTP] 响应头:', res.headers);
|
||||||
|
|
||||||
|
res.on("data", (chunk) => {
|
||||||
data += chunk;
|
data += chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
res.on('end', () => {
|
res.on("end", () => {
|
||||||
|
console.log("[HTTP] 响应体:", data);
|
||||||
try {
|
try {
|
||||||
const json = JSON.parse(data);
|
const json = JSON.parse(data);
|
||||||
|
// console.log('[HTTP] 解析后的响应:', JSON.stringify(json, null, 2));
|
||||||
|
|
||||||
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
console.log("[HTTP] 请求成功");
|
||||||
resolve(json as T);
|
resolve(json as T);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(json.error || json.message || `HTTP ${res.statusCode}`));
|
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) {
|
} catch (e) {
|
||||||
|
// console.error('[HTTP] 解析响应失败:', e);
|
||||||
|
// console.error('[HTTP] 原始响应:', data);
|
||||||
reject(new Error(`解析响应失败: ${data}`));
|
reject(new Error(`解析响应失败: ${data}`));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', (error) => {
|
req.on("error", (error) => {
|
||||||
|
// console.error('[HTTP] 请求错误:', error);
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('timeout', () => {
|
req.on("timeout", () => {
|
||||||
|
// console.error('[HTTP] 请求超时');
|
||||||
req.destroy();
|
req.destroy();
|
||||||
reject(new Error('请求超时'));
|
reject(new Error("请求超时"));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (options.body) {
|
if (options.body) {
|
||||||
req.write(JSON.stringify(options.body));
|
const bodyStr = JSON.stringify(options.body);
|
||||||
|
// console.log('[HTTP] 发送请求体:', bodyStr);
|
||||||
|
req.write(bodyStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
|
// console.log('[HTTP] 请求已发送');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,11 +147,13 @@ async function request<T>(path: string, options: RequestOptions): Promise<T> {
|
|||||||
* 提交工具执行结果
|
* 提交工具执行结果
|
||||||
* POST /api/tool/result
|
* POST /api/tool/result
|
||||||
*/
|
*/
|
||||||
export async function submitToolResult(result: ToolCallResult): Promise<ToolResultResponse> {
|
export async function submitToolResult(
|
||||||
|
result: ToolCallResult,
|
||||||
|
): Promise<ToolResultResponse> {
|
||||||
console.log(`[API] 提交工具结果: callId=${result.id}`);
|
console.log(`[API] 提交工具结果: callId=${result.id}`);
|
||||||
return request<ToolResultResponse>('/api/tool/result', {
|
return request<ToolResultResponse>("/api/tool/result", {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
body: result
|
body: result,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,11 +161,13 @@ export async function submitToolResult(result: ToolCallResult): Promise<ToolResu
|
|||||||
* 提交用户回答
|
* 提交用户回答
|
||||||
* POST /api/task/answer
|
* POST /api/task/answer
|
||||||
*/
|
*/
|
||||||
export async function submitAnswer(answer: AnswerRequest): Promise<AnswerResponse> {
|
export async function submitAnswer(
|
||||||
|
answer: AnswerRequest,
|
||||||
|
): Promise<AnswerResponse> {
|
||||||
console.log(`[API] 提交用户回答: askId=${answer.askId}`);
|
console.log(`[API] 提交用户回答: askId=${answer.askId}`);
|
||||||
return request<AnswerResponse>('/api/task/answer', {
|
return request<AnswerResponse>("/api/task/answer", {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
body: answer
|
body: answer,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,11 +175,15 @@ export async function submitAnswer(answer: AnswerRequest): Promise<AnswerRespons
|
|||||||
* 提交工具确认响应(Ask 模式)
|
* 提交工具确认响应(Ask 模式)
|
||||||
* POST /api/tool/confirm
|
* POST /api/tool/confirm
|
||||||
*/
|
*/
|
||||||
export async function submitToolConfirm(response: ToolConfirmResponse): Promise<ToolResultResponse> {
|
export async function submitToolConfirm(
|
||||||
console.log(`[API] 提交工具确认: confirmId=${response.confirmId}, approved=${response.approved}`);
|
response: ToolConfirmResponse,
|
||||||
return request<ToolResultResponse>('/api/tool/confirm', {
|
): Promise<ToolResultResponse> {
|
||||||
method: 'POST',
|
console.log(
|
||||||
body: response
|
`[API] 提交工具确认: confirmId=${response.confirmId}, approved=${response.approved}`,
|
||||||
|
);
|
||||||
|
return request<ToolResultResponse>("/api/tool/confirm", {
|
||||||
|
method: "POST",
|
||||||
|
body: response,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,9 +192,9 @@ export async function submitToolConfirm(response: ToolConfirmResponse): Promise<
|
|||||||
* GET /api/dialog/health
|
* GET /api/dialog/health
|
||||||
*/
|
*/
|
||||||
export async function healthCheck(): Promise<{ status: string }> {
|
export async function healthCheck(): Promise<{ status: string }> {
|
||||||
return request<{ status: string }>('/api/dialog/health', {
|
return request<{ status: string }>("/api/dialog/health", {
|
||||||
method: 'GET',
|
method: "GET",
|
||||||
timeout: 5000
|
timeout: 5000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,9 +221,9 @@ export interface StopDialogResponse {
|
|||||||
*/
|
*/
|
||||||
export async function stopDialog(taskId: string): Promise<StopDialogResponse> {
|
export async function stopDialog(taskId: string): Promise<StopDialogResponse> {
|
||||||
console.log(`[API] 停止对话: taskId=${taskId}`);
|
console.log(`[API] 停止对话: taskId=${taskId}`);
|
||||||
return request<StopDialogResponse>('/api/dialog/stop', {
|
return request<StopDialogResponse>("/api/dialog/stop", {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
body: { taskId }
|
body: { taskId },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,11 +239,13 @@ export interface CompactDialogResponse {
|
|||||||
* 手动压缩对话历史
|
* 手动压缩对话历史
|
||||||
* POST /api/dialog/compact
|
* POST /api/dialog/compact
|
||||||
*/
|
*/
|
||||||
export async function compactDialog(taskId: string): Promise<CompactDialogResponse> {
|
export async function compactDialog(
|
||||||
|
taskId: string,
|
||||||
|
): Promise<CompactDialogResponse> {
|
||||||
console.log(`[API] 压缩对话: taskId=${taskId}`);
|
console.log(`[API] 压缩对话: taskId=${taskId}`);
|
||||||
return request<CompactDialogResponse>('/api/dialog/compact', {
|
return request<CompactDialogResponse>("/api/dialog/compact", {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
body: { taskId }
|
body: { taskId },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,36 +254,142 @@ export async function compactDialog(taskId: string): Promise<CompactDialogRespon
|
|||||||
*/
|
*/
|
||||||
export function createSuccessResult(id: number, text: string): ToolCallResult {
|
export function createSuccessResult(id: number, text: string): ToolCallResult {
|
||||||
return {
|
return {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: {
|
result: {
|
||||||
content: [{ type: 'text', text }],
|
content: [{ type: "text", text }],
|
||||||
isError: false
|
isError: false,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建业务错误的工具结果(如编译失败)
|
* 创建业务错误的工具结果(如编译失败)
|
||||||
*/
|
*/
|
||||||
export function createBusinessErrorResult(id: number, errorMessage: string): ToolCallResult {
|
export function createBusinessErrorResult(
|
||||||
|
id: number,
|
||||||
|
errorMessage: string,
|
||||||
|
): ToolCallResult {
|
||||||
return {
|
return {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: {
|
result: {
|
||||||
content: [{ type: 'text', text: errorMessage }],
|
content: [{ type: "text", text: errorMessage }],
|
||||||
isError: true
|
isError: true,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建系统错误的工具结果
|
* 创建系统错误的工具结果
|
||||||
*/
|
*/
|
||||||
export function createSystemErrorResult(id: number, code: number, message: string): ToolCallResult {
|
export function createSystemErrorResult(
|
||||||
|
id: number,
|
||||||
|
code: number,
|
||||||
|
message: string,
|
||||||
|
): ToolCallResult {
|
||||||
return {
|
return {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
error: { code, message }
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
210
src/services/changeTracker.ts
Normal file
@ -0,0 +1,210 @@
|
|||||||
|
/**
|
||||||
|
* 文件变更追踪服务
|
||||||
|
* 功能:收集和管理 AI 修改文件的变更记录
|
||||||
|
* 依赖:types/fileChanges
|
||||||
|
* 使用场景:在文件操作时记录变更,供用户审查
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { FileChange, ChangeSession } from '../types/fileChanges';
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
class ChangeTrackerService {
|
||||||
|
private currentSession: ChangeSession | null = null;
|
||||||
|
private changeListeners: Array<(session: ChangeSession) => void> = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始新的变更会话
|
||||||
|
*/
|
||||||
|
startSession(sessionId: string): void {
|
||||||
|
// 如果已有 session(无论状态),重用并重置为 active
|
||||||
|
if (this.currentSession) {
|
||||||
|
this.currentSession.status = 'active';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.currentSession = {
|
||||||
|
sessionId,
|
||||||
|
startTime: Date.now(),
|
||||||
|
changes: [],
|
||||||
|
status: 'active'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录文件变更
|
||||||
|
*/
|
||||||
|
trackChange(filePath: string, oldContent: string, newContent: string): string {
|
||||||
|
if (!this.currentSession) {
|
||||||
|
this.startSession(`session_${Date.now()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const changeId = `change_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||||
|
|
||||||
|
// 判断变更类型
|
||||||
|
let changeType: 'create' | 'modify' | 'delete';
|
||||||
|
if (oldContent === '' && newContent !== '') {
|
||||||
|
changeType = 'create';
|
||||||
|
} else if (oldContent !== '' && newContent === '') {
|
||||||
|
changeType = 'delete';
|
||||||
|
} else {
|
||||||
|
changeType = 'modify';
|
||||||
|
}
|
||||||
|
|
||||||
|
const change: FileChange = {
|
||||||
|
filePath,
|
||||||
|
oldContent,
|
||||||
|
newContent,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
changeType,
|
||||||
|
changeId
|
||||||
|
};
|
||||||
|
|
||||||
|
this.currentSession!.changes.push(change);
|
||||||
|
this.notifyListeners();
|
||||||
|
|
||||||
|
return changeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 结束当前会话
|
||||||
|
*/
|
||||||
|
endSession(): ChangeSession | null {
|
||||||
|
if (this.currentSession && this.currentSession.changes.length > 0) {
|
||||||
|
this.currentSession.status = 'completed';
|
||||||
|
const session = this.currentSession;
|
||||||
|
this.notifyListeners();
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前会话
|
||||||
|
*/
|
||||||
|
getCurrentSession(): ChangeSession | null {
|
||||||
|
return this.currentSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空当前会话
|
||||||
|
*/
|
||||||
|
clearSession(): void {
|
||||||
|
this.currentSession = null;
|
||||||
|
this.notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除指定的变更
|
||||||
|
*/
|
||||||
|
removeChange(changeId: string): boolean {
|
||||||
|
if (!this.currentSession) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = this.currentSession.changes.findIndex(c => c.changeId === changeId);
|
||||||
|
if (index !== -1) {
|
||||||
|
this.currentSession.changes.splice(index, 1);
|
||||||
|
this.notifyListeners();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听变更
|
||||||
|
*/
|
||||||
|
onChangeUpdate(listener: (session: ChangeSession) => void): void {
|
||||||
|
this.changeListeners.push(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知所有监听器
|
||||||
|
*/
|
||||||
|
private notifyListeners(): void {
|
||||||
|
if (this.currentSession) {
|
||||||
|
this.changeListeners.forEach(listener => listener(this.currentSession!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 采纳变更(保存文件)
|
||||||
|
*/
|
||||||
|
async acceptChange(changeId: string): Promise<boolean> {
|
||||||
|
if (!this.currentSession) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const change = this.currentSession.changes.find(c => c.changeId === changeId);
|
||||||
|
if (!change) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
if (!workspaceFolder) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const absolutePath = path.join(workspaceFolder.uri.fsPath, change.filePath);
|
||||||
|
|
||||||
|
// 如果是删除操作,删除文件
|
||||||
|
if (change.changeType === 'delete') {
|
||||||
|
if (fs.existsSync(absolutePath)) {
|
||||||
|
await fs.promises.unlink(absolutePath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 创建或修改文件
|
||||||
|
await fs.promises.writeFile(absolutePath, change.newContent, 'utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.removeChange(changeId);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[ChangeTracker] 采纳变更失败:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拒绝变更(恢复旧内容)
|
||||||
|
*/
|
||||||
|
async rejectChange(changeId: string): Promise<boolean> {
|
||||||
|
if (!this.currentSession) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const change = this.currentSession.changes.find(c => c.changeId === changeId);
|
||||||
|
if (!change) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
if (!workspaceFolder) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const absolutePath = path.join(workspaceFolder.uri.fsPath, change.filePath);
|
||||||
|
|
||||||
|
// 如果是新建文件,删除它
|
||||||
|
if (change.changeType === 'create') {
|
||||||
|
if (fs.existsSync(absolutePath)) {
|
||||||
|
await fs.promises.unlink(absolutePath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 恢复旧内容
|
||||||
|
await fs.promises.writeFile(absolutePath, change.oldContent, 'utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.removeChange(changeId);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[ChangeTracker] 拒绝变更失败:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const changeTracker = new ChangeTrackerService();
|
||||||
269
src/services/creditsService.ts
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
/**
|
||||||
|
* 资源点余额管理服务
|
||||||
|
* 负责缓存余额、主动查询、发送前检测
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/** 余额更新回调函数 */
|
||||||
|
let onBalanceUpdateCallback: ((balance: number) => void) | 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置余额更新回调
|
||||||
|
*/
|
||||||
|
export function setBalanceUpdateCallback(callback: (balance: number) => void): void {
|
||||||
|
onBalanceUpdateCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存余额到持久化存储
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
// 通知前端更新余额显示
|
||||||
|
if (onBalanceUpdateCallback) {
|
||||||
|
onBalanceUpdateCallback(balance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存的余额
|
||||||
|
*/
|
||||||
|
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] 余额缓存已清除');
|
||||||
|
}
|
||||||
@ -2,6 +2,8 @@ import * as vscode from "vscode";
|
|||||||
import * as http from "http";
|
import * as http from "http";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
import { getConfig } from "../config/settings";
|
||||||
|
import { resetInvitationVerification } from "./apiClient";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IC Coder Authentication Provider
|
* IC Coder Authentication Provider
|
||||||
@ -12,7 +14,6 @@ export class ICCoderAuthenticationProvider
|
|||||||
{
|
{
|
||||||
private static readonly AUTH_TYPE = "iccoder";
|
private static readonly AUTH_TYPE = "iccoder";
|
||||||
private static readonly AUTH_NAME = "IC Coder";
|
private static readonly AUTH_NAME = "IC Coder";
|
||||||
private static readonly LOGIN_URL = "http://192.168.1.108:2005/login";
|
|
||||||
private static loginServer: http.Server | null = null;
|
private static loginServer: http.Server | null = null;
|
||||||
private static currentPort: number | null = null;
|
private static currentPort: number | null = null;
|
||||||
|
|
||||||
@ -23,8 +24,23 @@ export class ICCoderAuthenticationProvider
|
|||||||
private _sessions: vscode.AuthenticationSession[] = [];
|
private _sessions: vscode.AuthenticationSession[] = [];
|
||||||
|
|
||||||
constructor(private readonly context: vscode.ExtensionContext) {
|
constructor(private readonly context: vscode.ExtensionContext) {
|
||||||
// 从存储中恢复会话
|
// 从存储中恢复会话(同步执行)
|
||||||
this.loadSessions();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,7 +57,9 @@ export class ICCoderAuthenticationProvider
|
|||||||
* 保存会话到存储
|
* 保存会话到存储
|
||||||
*/
|
*/
|
||||||
private async saveSessions(): Promise<void> {
|
private async saveSessions(): Promise<void> {
|
||||||
|
console.log("[AuthProvider] 保存 sessions, 数量:", this._sessions.length);
|
||||||
await this.context.globalState.update("icCoderSessions", this._sessions);
|
await this.context.globalState.update("icCoderSessions", this._sessions);
|
||||||
|
console.log("[AuthProvider] sessions 已保存到 globalState");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,6 +68,7 @@ export class ICCoderAuthenticationProvider
|
|||||||
async getSessions(
|
async getSessions(
|
||||||
scopes?: readonly string[]
|
scopes?: readonly string[]
|
||||||
): Promise<vscode.AuthenticationSession[]> {
|
): Promise<vscode.AuthenticationSession[]> {
|
||||||
|
console.log("[AuthProvider] getSessions 被调用, 当前 sessions 数量:", this._sessions.length);
|
||||||
return [...this._sessions];
|
return [...this._sessions];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,15 +79,32 @@ export class ICCoderAuthenticationProvider
|
|||||||
scopes: readonly string[]
|
scopes: readonly string[]
|
||||||
): Promise<vscode.AuthenticationSession> {
|
): Promise<vscode.AuthenticationSession> {
|
||||||
try {
|
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();
|
const token = await this.login();
|
||||||
|
|
||||||
|
// 获取到 token 后立即调用用户信息接口
|
||||||
|
// const userInfo = await onTokenReceived(token);
|
||||||
|
|
||||||
// 创建会话
|
// 创建会话
|
||||||
const session: vscode.AuthenticationSession = {
|
const session: vscode.AuthenticationSession = {
|
||||||
id: this.generateSessionId(),
|
id: this.generateSessionId(),
|
||||||
accessToken: token,
|
accessToken: token,
|
||||||
account: {
|
account: {
|
||||||
id: "iccoder-user",
|
id: "user",
|
||||||
label: "IC Coder 用户",
|
label: "IC Coder User",
|
||||||
},
|
},
|
||||||
scopes: [...scopes],
|
scopes: [...scopes],
|
||||||
};
|
};
|
||||||
@ -106,10 +142,24 @@ export class ICCoderAuthenticationProvider
|
|||||||
const sessionIndex = this._sessions.findIndex((s) => s.id === sessionId);
|
const sessionIndex = this._sessions.findIndex((s) => s.id === sessionId);
|
||||||
if (sessionIndex > -1) {
|
if (sessionIndex > -1) {
|
||||||
const session = this._sessions[sessionIndex];
|
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);
|
this._sessions.splice(sessionIndex, 1);
|
||||||
await this.saveSessions();
|
await this.saveSessions();
|
||||||
|
|
||||||
// 触发会话变化事件
|
// 3. 清除用户信息缓存
|
||||||
|
// await clearUserInfo();
|
||||||
|
|
||||||
|
// 4. 触发会话变化事件
|
||||||
this._onDidChangeSessions.fire({
|
this._onDidChangeSessions.fire({
|
||||||
added: [],
|
added: [],
|
||||||
removed: [session],
|
removed: [session],
|
||||||
@ -125,6 +175,28 @@ export class ICCoderAuthenticationProvider
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear local authentication state without window reload.
|
||||||
|
* Used by re-login flow when session is expired.
|
||||||
|
*/
|
||||||
|
async clearSessionsForRelogin(): Promise<void> {
|
||||||
|
if (this._sessions.length === 0) {
|
||||||
|
// await clearUserInfo();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const removed = [...this._sessions];
|
||||||
|
this._sessions = [];
|
||||||
|
await this.saveSessions();
|
||||||
|
// await clearUserInfo();
|
||||||
|
|
||||||
|
this._onDidChangeSessions.fire({
|
||||||
|
added: [],
|
||||||
|
removed,
|
||||||
|
changed: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成会话 ID
|
* 生成会话 ID
|
||||||
*/
|
*/
|
||||||
@ -149,9 +221,8 @@ export class ICCoderAuthenticationProvider
|
|||||||
|
|
||||||
// 构建登录 URL
|
// 构建登录 URL
|
||||||
const callbackUrl = `http://localhost:${port}/callback`;
|
const callbackUrl = `http://localhost:${port}/callback`;
|
||||||
const loginUrl = `${
|
const config = getConfig();
|
||||||
ICCoderAuthenticationProvider.LOGIN_URL
|
const loginUrl = `${config.loginUrl}?redirect_uri=${encodeURIComponent(callbackUrl)}`;
|
||||||
}?redirect_uri=${encodeURIComponent(callbackUrl)}`;
|
|
||||||
|
|
||||||
console.log("🔐 登录服务器已启动,监听端口:", port);
|
console.log("🔐 登录服务器已启动,监听端口:", port);
|
||||||
console.log("🌐 登录 URL:", loginUrl);
|
console.log("🌐 登录 URL:", loginUrl);
|
||||||
|
|||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
270
src/services/notificationService.ts
Normal file
@ -0,0 +1,270 @@
|
|||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
// 尝试加载 node-notifier,如果失败则使用 null
|
||||||
|
let notifier: any = null;
|
||||||
|
try {
|
||||||
|
notifier = require('node-notifier');
|
||||||
|
console.log('[NotificationService] node-notifier 加载成功');
|
||||||
|
} catch (error) {
|
||||||
|
console.log('[NotificationService] node-notifier 加载失败,将只使用 VS Code 内置通知');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知类型枚举
|
||||||
|
*/
|
||||||
|
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 不可用,直接使用 VS Code 内置通知
|
||||||
|
if (!notifier) {
|
||||||
|
console.log('[NotificationService] node-notifier 不可用,使用 VS Code 内置通知');
|
||||||
|
this.showVSCodeNotification(title, message, type, onClick);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 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();
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -28,7 +28,8 @@ import type {
|
|||||||
AgentProgressEvent,
|
AgentProgressEvent,
|
||||||
AgentCompleteEvent,
|
AgentCompleteEvent,
|
||||||
AgentErrorEvent,
|
AgentErrorEvent,
|
||||||
ContextUsageEvent
|
ContextUsageEvent,
|
||||||
|
CreditUpdateEvent
|
||||||
} from '../types/api';
|
} from '../types/api';
|
||||||
import type { MemoryCompactedEvent } from '../types/memory';
|
import type { MemoryCompactedEvent } from '../types/memory';
|
||||||
|
|
||||||
@ -44,6 +45,16 @@ export interface SSECallbacks {
|
|||||||
onToolConfirm?: (data: ToolConfirmEvent) => void;
|
onToolConfirm?: (data: ToolConfirmEvent) => void;
|
||||||
/** 收到计划确认请求(Plan 模式) */
|
/** 收到计划确认请求(Plan 模式) */
|
||||||
onPlanConfirm?: (data: PlanConfirmEvent) => void;
|
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;
|
onToolStart?: (data: ToolStartEvent) => void;
|
||||||
/** 工具执行完成 */
|
/** 工具执行完成 */
|
||||||
@ -74,6 +85,8 @@ export interface SSECallbacks {
|
|||||||
onMemoryCompacted?: (data: MemoryCompactedEvent) => void;
|
onMemoryCompacted?: (data: MemoryCompactedEvent) => void;
|
||||||
/** 上下文使用量更新 */
|
/** 上下文使用量更新 */
|
||||||
onContextUsage?: (data: ContextUsageEvent) => void;
|
onContextUsage?: (data: ContextUsageEvent) => void;
|
||||||
|
/** 资源点余额更新 */
|
||||||
|
onCreditUpdate?: (data: CreditUpdateEvent) => void;
|
||||||
/** 连接打开 */
|
/** 连接打开 */
|
||||||
onOpen?: () => void;
|
onOpen?: () => void;
|
||||||
/** 连接关闭 */
|
/** 连接关闭 */
|
||||||
@ -148,7 +161,16 @@ export async function startStreamDialog(
|
|||||||
|
|
||||||
const body = JSON.stringify(request);
|
const body = JSON.stringify(request);
|
||||||
|
|
||||||
console.log(`[SSE] 开始流式对话: taskId=${request.taskId}, mode=${request.mode}, url=${urlString}`);
|
console.log('[SSE] 请求详情:', {
|
||||||
|
url: urlString,
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'text/event-stream',
|
||||||
|
hasToken: !!request.token,
|
||||||
|
},
|
||||||
|
body: request
|
||||||
|
});
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const options: http.RequestOptions = {
|
const options: http.RequestOptions = {
|
||||||
@ -160,7 +182,8 @@ export async function startStreamDialog(
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'text/event-stream',
|
'Accept': 'text/event-stream',
|
||||||
'Cache-Control': 'no-cache',
|
'Cache-Control': 'no-cache',
|
||||||
'Content-Length': Buffer.byteLength(body)
|
'Content-Length': Buffer.byteLength(body),
|
||||||
|
...(request.token ? { 'Authorization': `Bearer ${request.token}` } : {})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -170,9 +193,20 @@ export async function startStreamDialog(
|
|||||||
let errorBody = '';
|
let errorBody = '';
|
||||||
res.on('data', chunk => errorBody += chunk);
|
res.on('data', chunk => errorBody += chunk);
|
||||||
res.on('end', () => {
|
res.on('end', () => {
|
||||||
const error = new Error(`SSE 连接失败: ${res.statusCode} ${errorBody}`);
|
// 检测是否是登录状态过期
|
||||||
callbacks.onError?.({ message: error.message });
|
const isLoginExpired = errorBody.includes('登录状态已过期') ||
|
||||||
reject(error);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
@ -213,6 +247,25 @@ export async function startStreamDialog(
|
|||||||
res.on('data', (chunk: string) => {
|
res.on('data', (chunk: string) => {
|
||||||
if (!controller.aborted) {
|
if (!controller.aborted) {
|
||||||
console.log('[SSE] 收到原始数据块:', chunk.substring(0, 200));
|
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);
|
parser.feed(chunk);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -286,6 +339,21 @@ function dispatchEvent(
|
|||||||
case 'plan_confirm':
|
case 'plan_confirm':
|
||||||
callbacks.onPlanConfirm?.(data as PlanConfirmEvent);
|
callbacks.onPlanConfirm?.(data as PlanConfirmEvent);
|
||||||
break;
|
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':
|
case 'tool_start':
|
||||||
callbacks.onToolStart?.(data as ToolStartEvent);
|
callbacks.onToolStart?.(data as ToolStartEvent);
|
||||||
break;
|
break;
|
||||||
@ -331,6 +399,14 @@ function dispatchEvent(
|
|||||||
case 'context_usage':
|
case 'context_usage':
|
||||||
callbacks.onContextUsage?.(data as ContextUsageEvent);
|
callbacks.onContextUsage?.(data as ContextUsageEvent);
|
||||||
break;
|
break;
|
||||||
|
case 'credit_update':
|
||||||
|
callbacks.onCreditUpdate?.(data as CreditUpdateEvent);
|
||||||
|
break;
|
||||||
|
case 'heartbeat':
|
||||||
|
// 心跳事件:仅用于保持连接,不需要特殊处理
|
||||||
|
// Node.js req.setTimeout 会在收到数据时自动重置计时器
|
||||||
|
console.log('[SSE] 收到心跳');
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.log(`[SSE] 未知事件类型: ${eventType}`, data);
|
console.log(`[SSE] 未知事件类型: ${eventType}`, data);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,19 +2,31 @@
|
|||||||
* 工具执行器
|
* 工具执行器
|
||||||
* 接收后端的 tool_call 事件,执行本地工具,返回结果
|
* 接收后端的 tool_call 事件,执行本地工具,返回结果
|
||||||
*/
|
*/
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from "vscode";
|
||||||
import * as path from 'path';
|
import * as path from "path";
|
||||||
import * as os from 'os';
|
import * as os from "os";
|
||||||
import * as fs from 'fs';
|
import * as fs from "fs";
|
||||||
import { readFileContent, readDirectory } from '../utils/readFiles';
|
import { readFileContent, readDirectory, listDirectory } from "../utils/readFiles";
|
||||||
import { createOrOverwriteFile } from '../utils/createFiles';
|
import { createOrOverwriteFile } from "../utils/createFiles";
|
||||||
import { generateVCD, checkIverilogAvailable } from '../utils/iverilogRunner';
|
import { resolveWorkspaceFilePath, showFileDiff } from "../utils/fileDiff";
|
||||||
|
import { changeTracker } from "./changeTracker";
|
||||||
|
import {
|
||||||
|
generateVCD,
|
||||||
|
checkIverilogAvailable,
|
||||||
|
generateMultiVCD,
|
||||||
|
DumpModule,
|
||||||
|
} from "../utils/iverilogRunner";
|
||||||
|
import { analyzeVcdFile } from "../utils/vcdParser";
|
||||||
|
import {
|
||||||
|
executeWaveformTrace,
|
||||||
|
WaveformTraceArgs,
|
||||||
|
} from "../utils/waveformTracer";
|
||||||
import {
|
import {
|
||||||
submitToolResult,
|
submitToolResult,
|
||||||
createSuccessResult,
|
createSuccessResult,
|
||||||
createBusinessErrorResult,
|
createBusinessErrorResult,
|
||||||
createSystemErrorResult
|
createSystemErrorResult,
|
||||||
} from './apiClient';
|
} from "./apiClient";
|
||||||
import type {
|
import type {
|
||||||
ToolCallRequest,
|
ToolCallRequest,
|
||||||
ToolName,
|
ToolName,
|
||||||
@ -23,11 +35,12 @@ import type {
|
|||||||
FileDeleteArgs,
|
FileDeleteArgs,
|
||||||
FileListArgs,
|
FileListArgs,
|
||||||
SyntaxCheckArgs,
|
SyntaxCheckArgs,
|
||||||
|
IverilogArgs,
|
||||||
SimulationArgs,
|
SimulationArgs,
|
||||||
WaveformSummaryArgs,
|
WaveformSummaryArgs,
|
||||||
KnowledgeSaveArgs,
|
KnowledgeSaveArgs,
|
||||||
KnowledgeLoadArgs
|
KnowledgeLoadArgs,
|
||||||
} from '../types/api';
|
} from "../types/api";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工具执行器上下文
|
* 工具执行器上下文
|
||||||
@ -46,7 +59,7 @@ export interface ToolExecutorContext {
|
|||||||
*/
|
*/
|
||||||
export async function executeToolCall(
|
export async function executeToolCall(
|
||||||
request: ToolCallRequest,
|
request: ToolCallRequest,
|
||||||
context: ToolExecutorContext
|
context: ToolExecutorContext,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const toolName = request.params.name as ToolName;
|
const toolName = request.params.name as ToolName;
|
||||||
const args = request.params.arguments;
|
const args = request.params.arguments;
|
||||||
@ -58,31 +71,53 @@ export async function executeToolCall(
|
|||||||
let resultText: string;
|
let resultText: string;
|
||||||
|
|
||||||
switch (toolName) {
|
switch (toolName) {
|
||||||
case 'file_read':
|
case "file_read":
|
||||||
resultText = await executeFileRead(args as unknown as FileReadArgs);
|
resultText = await executeFileRead(args as unknown as FileReadArgs);
|
||||||
break;
|
break;
|
||||||
case 'file_write':
|
case "file_write":
|
||||||
resultText = await executeFileWrite(args as unknown as FileWriteArgs);
|
resultText = await executeFileWrite(args as unknown as FileWriteArgs);
|
||||||
break;
|
break;
|
||||||
case 'file_delete':
|
case "file_delete":
|
||||||
resultText = await executeFileDelete(args as unknown as FileDeleteArgs);
|
resultText = await executeFileDelete(args as unknown as FileDeleteArgs);
|
||||||
break;
|
break;
|
||||||
case 'file_list':
|
case "file_list":
|
||||||
resultText = await executeFileList(args as unknown as FileListArgs);
|
resultText = await executeFileList(args as unknown as FileListArgs);
|
||||||
break;
|
break;
|
||||||
case 'syntax_check':
|
case "syntax_check":
|
||||||
resultText = await executeSyntaxCheck(args as unknown as SyntaxCheckArgs, context);
|
resultText = await executeSyntaxCheck(
|
||||||
|
args as unknown as SyntaxCheckArgs,
|
||||||
|
context,
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case 'simulation':
|
case "iverilog":
|
||||||
resultText = await executeSimulation(args as unknown as SimulationArgs, context);
|
resultText = await executeIverilog(
|
||||||
|
args as unknown as IverilogArgs,
|
||||||
|
context,
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case 'waveform_summary':
|
case "simulation":
|
||||||
resultText = await executeWaveformSummary(args as unknown as WaveformSummaryArgs);
|
resultText = await executeSimulation(
|
||||||
|
args as unknown as SimulationArgs,
|
||||||
|
context,
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case 'knowledge_save':
|
case "waveform_summary":
|
||||||
resultText = await executeKnowledgeSave(args as unknown as KnowledgeSaveArgs);
|
resultText = await executeWaveformSummary(
|
||||||
|
args as unknown as WaveformSummaryArgs,
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case 'knowledge_load':
|
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();
|
resultText = await executeKnowledgeLoad();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -91,16 +126,31 @@ export async function executeToolCall(
|
|||||||
|
|
||||||
// 提交成功结果
|
// 提交成功结果
|
||||||
const result = createSuccessResult(callId, resultText);
|
const result = createSuccessResult(callId, resultText);
|
||||||
|
console.log(`[ToolExecutor] 准备提交结果: ${toolName}, callId=${callId}`);
|
||||||
await submitToolResult(result);
|
await submitToolResult(result);
|
||||||
console.log(`[ToolExecutor] 工具执行成功: ${toolName}, callId=${callId}`);
|
console.log(`[ToolExecutor] 结果提交成功: ${toolName}, callId=${callId}`);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||||||
console.error(`[ToolExecutor] 工具执行失败: ${toolName}, callId=${callId}`, error);
|
console.error(
|
||||||
|
`[ToolExecutor] 工具执行失败: ${toolName}, callId=${callId}`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
|
||||||
// 提交错误结果
|
// 提交错误结果
|
||||||
const result = createBusinessErrorResult(callId, errorMessage);
|
try {
|
||||||
await submitToolResult(result);
|
const result = createBusinessErrorResult(callId, errorMessage);
|
||||||
|
console.log(`[ToolExecutor] 准备提交错误结果: ${toolName}, callId=${callId}`);
|
||||||
|
await submitToolResult(result);
|
||||||
|
console.log(`[ToolExecutor] 错误结果提交成功: ${toolName}, callId=${callId}`);
|
||||||
|
} catch (submitError) {
|
||||||
|
console.error(
|
||||||
|
`[ToolExecutor] 提交错误结果失败: ${toolName}, callId=${callId}`,
|
||||||
|
submitError,
|
||||||
|
);
|
||||||
|
throw submitError;
|
||||||
|
}
|
||||||
|
// 重新抛出原始错误,让调用方知道工具执行失败
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,10 +166,21 @@ async function executeFileRead(args: FileReadArgs): Promise<string> {
|
|||||||
* 执行 file_write 工具
|
* 执行 file_write 工具
|
||||||
*/
|
*/
|
||||||
async function executeFileWrite(args: FileWriteArgs): Promise<string> {
|
async function executeFileWrite(args: FileWriteArgs): Promise<string> {
|
||||||
|
const absolutePath = resolveWorkspaceFilePath(args.path);
|
||||||
|
const existedBeforeWrite = fs.existsSync(absolutePath);
|
||||||
|
const oldContent = existedBeforeWrite ? await readFileContent(args.path) : "";
|
||||||
|
|
||||||
await createOrOverwriteFile(args.path, args.content);
|
await createOrOverwriteFile(args.path, args.content);
|
||||||
|
|
||||||
|
// 记录文件变更
|
||||||
|
try {
|
||||||
|
changeTracker.trackChange(args.path, oldContent, args.content);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[ToolExecutor] 记录文件变更失败:", error);
|
||||||
|
}
|
||||||
|
|
||||||
// Verilog 文件添加知识图谱提示
|
// Verilog 文件添加知识图谱提示
|
||||||
const isVerilogFile = args.path.endsWith('.v') || args.path.endsWith('.sv');
|
const isVerilogFile = args.path.endsWith(".v") || args.path.endsWith(".sv");
|
||||||
if (isVerilogFile) {
|
if (isVerilogFile) {
|
||||||
return `文件已写入: ${args.path}\n\n[提示] 如有新信号或规则,请更新知识图谱`;
|
return `文件已写入: ${args.path}\n\n[提示] 如有新信号或规则,请更新知识图谱`;
|
||||||
}
|
}
|
||||||
@ -129,7 +190,7 @@ async function executeFileWrite(args: FileWriteArgs): Promise<string> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行 file_delete 工具
|
* 执行 file_delete 工具
|
||||||
* 删除指定路径的文件
|
* 删除指定路径的文件(带用户确认)
|
||||||
*/
|
*/
|
||||||
async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
|
async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
|
||||||
const filePath = args.path;
|
const filePath = args.path;
|
||||||
@ -137,7 +198,7 @@ async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
|
|||||||
// 获取工作区路径
|
// 获取工作区路径
|
||||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
if (!workspaceFolders || workspaceFolders.length === 0) {
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
throw new Error('请先打开一个工作区');
|
throw new Error("请先打开一个工作区");
|
||||||
}
|
}
|
||||||
|
|
||||||
const workspacePath = workspaceFolders[0].uri.fsPath;
|
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||||
@ -158,11 +219,60 @@ async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
|
|||||||
throw new Error(`不能删除目录,请指定文件路径: ${filePath}`);
|
throw new Error(`不能删除目录,请指定文件路径: ${filePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除文件
|
// 验证文件路径在工作区内
|
||||||
fs.unlinkSync(absolutePath);
|
const isInWorkspace = workspaceFolders.some((folder) =>
|
||||||
|
absolutePath.startsWith(folder.uri.fsPath),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isInWorkspace) {
|
||||||
|
throw new Error("只能删除工作区内的文件");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保护敏感文件
|
||||||
|
const protectedFiles = [
|
||||||
|
"package.json",
|
||||||
|
"tsconfig.json",
|
||||||
|
".git",
|
||||||
|
"node_modules",
|
||||||
|
];
|
||||||
|
|
||||||
|
const fileName = path.basename(absolutePath);
|
||||||
|
if (protectedFiles.includes(fileName)) {
|
||||||
|
throw new Error(`不允许删除系统文件: ${fileName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 弹出确认对话框
|
||||||
|
const confirmed = await vscode.window.showWarningMessage(
|
||||||
|
`确定要删除文件吗?\n\n📄 ${path.basename(filePath)}\n📁 ${path.dirname(filePath)}`,
|
||||||
|
{
|
||||||
|
modal: true, // 模态对话框,阻止其他操作
|
||||||
|
detail: "⚠️ 文件将被移到回收站,可以恢复",
|
||||||
|
},
|
||||||
|
"确定删除",
|
||||||
|
"取消",
|
||||||
|
);
|
||||||
|
|
||||||
|
// 用户取消或关闭对话框
|
||||||
|
if (confirmed !== "确定删除") {
|
||||||
|
throw new Error("用户取消了删除操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取文件内容用于变更追踪
|
||||||
|
const oldContent = fs.readFileSync(absolutePath, "utf-8");
|
||||||
|
|
||||||
|
// 记录删除变更
|
||||||
|
const relativePath = path.relative(workspacePath, absolutePath);
|
||||||
|
changeTracker.trackChange(relativePath, oldContent, "");
|
||||||
|
|
||||||
|
// 删除文件(移到回收站)
|
||||||
|
const uri = vscode.Uri.file(absolutePath);
|
||||||
|
await vscode.workspace.fs.delete(uri, {
|
||||||
|
recursive: false, // 不是目录,设为 false
|
||||||
|
useTrash: true, // 移到回收站而非永久删除
|
||||||
|
});
|
||||||
|
|
||||||
// Verilog 文件添加知识图谱提示
|
// Verilog 文件添加知识图谱提示
|
||||||
const isVerilogFile = filePath.endsWith('.v') || filePath.endsWith('.sv');
|
const isVerilogFile = filePath.endsWith(".v") || filePath.endsWith(".sv");
|
||||||
if (isVerilogFile) {
|
if (isVerilogFile) {
|
||||||
return `文件已删除: ${filePath}\n\n[提示] 请删除知识图谱中相关节点`;
|
return `文件已删除: ${filePath}\n\n[提示] 请删除知识图谱中相关节点`;
|
||||||
}
|
}
|
||||||
@ -174,13 +284,11 @@ async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
|
|||||||
* 执行 file_list 工具
|
* 执行 file_list 工具
|
||||||
*/
|
*/
|
||||||
async function executeFileList(args: FileListArgs): Promise<string> {
|
async function executeFileList(args: FileListArgs): Promise<string> {
|
||||||
const dirPath = args.path || '.';
|
const dirPath = args.path || ".";
|
||||||
const extensions = args.extension ? [args.extension] : undefined;
|
const extensions = args.extension ? [args.extension] : undefined;
|
||||||
|
|
||||||
const files = await readDirectory(dirPath, extensions);
|
const files = await listDirectory(dirPath, extensions);
|
||||||
const fileList = files.map(f => f.path).join('\n');
|
return files.join("\n") || "(目录为空)";
|
||||||
|
|
||||||
return fileList || '(目录为空)';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -189,12 +297,12 @@ async function executeFileList(args: FileListArgs): Promise<string> {
|
|||||||
*/
|
*/
|
||||||
async function executeSyntaxCheck(
|
async function executeSyntaxCheck(
|
||||||
args: SyntaxCheckArgs,
|
args: SyntaxCheckArgs,
|
||||||
context: ToolExecutorContext
|
context: ToolExecutorContext,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// 检查 iverilog 是否可用
|
// 检查 iverilog 是否可用
|
||||||
const iverilogCheck = await checkIverilogAvailable(context.extensionPath);
|
const iverilogCheck = await checkIverilogAvailable(context.extensionPath);
|
||||||
if (!iverilogCheck.available) {
|
if (!iverilogCheck.available) {
|
||||||
throw new Error(`iverilog 不可用: ${iverilogCheck.message}`);
|
throw new Error(`IC Coder编译器不可用: ${iverilogCheck.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建临时文件
|
// 创建临时文件
|
||||||
@ -203,33 +311,33 @@ async function executeSyntaxCheck(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 写入代码到临时文件
|
// 写入代码到临时文件
|
||||||
fs.writeFileSync(tempFile, args.code, 'utf-8');
|
fs.writeFileSync(tempFile, args.code, "utf-8");
|
||||||
|
|
||||||
// 调用 iverilog 进行语法检查
|
// 调用 iverilog 进行语法检查
|
||||||
const { spawn } = require('child_process');
|
const { spawn } = require("child_process");
|
||||||
const iverilogPath = getIverilogPath(context.extensionPath);
|
const iverilogPath = getIverilogPath(context.extensionPath);
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const child = spawn(iverilogPath, ['-t', 'null', tempFile], {
|
const child = spawn(iverilogPath, ["-t", "null", tempFile], {
|
||||||
cwd: tempDir,
|
cwd: tempDir,
|
||||||
env: {
|
env: {
|
||||||
...process.env,
|
...process.env,
|
||||||
IVERILOG_ROOT: path.join(context.extensionPath, 'tools', 'iverilog')
|
IVERILOG_ROOT: path.join(context.extensionPath, "tools", "iverilog"),
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
let stdout = '';
|
let stdout = "";
|
||||||
let stderr = '';
|
let stderr = "";
|
||||||
|
|
||||||
child.stdout.on('data', (data: Buffer) => {
|
child.stdout.on("data", (data: Buffer) => {
|
||||||
stdout += data.toString();
|
stdout += data.toString();
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stderr.on('data', (data: Buffer) => {
|
child.stderr.on("data", (data: Buffer) => {
|
||||||
stderr += data.toString();
|
stderr += data.toString();
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on('close', (code: number) => {
|
child.on("close", (code: number) => {
|
||||||
// 清理临时文件
|
// 清理临时文件
|
||||||
try {
|
try {
|
||||||
fs.unlinkSync(tempFile);
|
fs.unlinkSync(tempFile);
|
||||||
@ -238,13 +346,13 @@ async function executeSyntaxCheck(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
resolve('语法检查通过,无错误。');
|
resolve("语法检查通过,无错误。");
|
||||||
} else {
|
} else {
|
||||||
resolve(`语法检查发现错误:\n${stderr || stdout}`);
|
resolve(`语法检查发现错误:\n${stderr || stdout}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on('error', (error: Error) => {
|
child.on("error", (error: Error) => {
|
||||||
try {
|
try {
|
||||||
fs.unlinkSync(tempFile);
|
fs.unlinkSync(tempFile);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -253,7 +361,6 @@ async function executeSyntaxCheck(
|
|||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 确保清理临时文件
|
// 确保清理临时文件
|
||||||
try {
|
try {
|
||||||
@ -265,22 +372,113 @@ async function executeSyntaxCheck(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 iverilog 工具
|
||||||
|
* 直接执行 iverilog 命令
|
||||||
|
*/
|
||||||
|
async function executeIverilog(
|
||||||
|
args: IverilogArgs,
|
||||||
|
context: ToolExecutorContext,
|
||||||
|
): Promise<string> {
|
||||||
|
// 检查 iverilog 是否可用
|
||||||
|
const iverilogCheck = await checkIverilogAvailable(context.extensionPath);
|
||||||
|
if (!iverilogCheck.available) {
|
||||||
|
throw new Error(`IC Coder编译器不可用: ${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 工具
|
* 执行 simulation 工具
|
||||||
*/
|
*/
|
||||||
async function executeSimulation(
|
async function executeSimulation(
|
||||||
args: SimulationArgs,
|
args: SimulationArgs,
|
||||||
context: ToolExecutorContext
|
context: ToolExecutorContext,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// 获取工作区路径
|
// 获取工作区路径
|
||||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
if (!workspaceFolders || workspaceFolders.length === 0) {
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
throw new Error('请先打开一个工作区');
|
throw new Error("请先打开一个工作区");
|
||||||
}
|
}
|
||||||
|
|
||||||
const projectPath = workspaceFolders[0].uri.fsPath;
|
const projectPath = workspaceFolders[0].uri.fsPath;
|
||||||
|
|
||||||
// 调用现有的 generateVCD 函数
|
// 检查是否有 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);
|
const result = await generateVCD(projectPath, context.extensionPath);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
@ -299,13 +497,50 @@ async function executeSimulation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行 waveform_summary 工具
|
* 解析 dumpModules 参数
|
||||||
* TODO: 实现 VCD 波形分析
|
* 格式:name:path,name:path
|
||||||
*/
|
*/
|
||||||
async function executeWaveformSummary(args: WaveformSummaryArgs): Promise<string> {
|
function parseDumpModules(dumpModules: string): DumpModule[] {
|
||||||
// TODO: 使用 vcdrom/vcd-stream 解析 VCD 文件
|
return dumpModules.split(",").map((item) => {
|
||||||
// 目前返回一个占位响应
|
const [name, modulePath] = item.trim().split(":");
|
||||||
return `波形分析功能暂未实现。\n请求参数:\n- VCD文件: ${args.vcdPath}\n- 信号: ${args.signals}\n- 检查点: ${args.checkpoints || '无'}`;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -315,17 +550,20 @@ async function executeWaveformSummary(args: WaveformSummaryArgs): Promise<string
|
|||||||
async function executeKnowledgeSave(args: KnowledgeSaveArgs): Promise<string> {
|
async function executeKnowledgeSave(args: KnowledgeSaveArgs): Promise<string> {
|
||||||
const workspaceFolder = getWorkspaceFolder();
|
const workspaceFolder = getWorkspaceFolder();
|
||||||
if (!workspaceFolder) {
|
if (!workspaceFolder) {
|
||||||
throw new Error('请先打开一个工作区');
|
throw new Error("请先打开一个工作区");
|
||||||
}
|
}
|
||||||
|
|
||||||
const iccoderDirUri = vscode.Uri.joinPath(workspaceFolder.uri, '.iccoder');
|
const iccoderDirUri = vscode.Uri.joinPath(workspaceFolder.uri, ".iccoder");
|
||||||
const knowledgeUri = vscode.Uri.joinPath(iccoderDirUri, 'knowledge.json');
|
const knowledgeUri = vscode.Uri.joinPath(iccoderDirUri, "knowledge.json");
|
||||||
|
|
||||||
// 确保 .iccoder 目录存在(兼容远程/虚拟工作区)
|
// 确保 .iccoder 目录存在(兼容远程/虚拟工作区)
|
||||||
await vscode.workspace.fs.createDirectory(iccoderDirUri);
|
await vscode.workspace.fs.createDirectory(iccoderDirUri);
|
||||||
|
|
||||||
// 写入知识图谱(UTF-8)
|
// 写入知识图谱(UTF-8)
|
||||||
await vscode.workspace.fs.writeFile(knowledgeUri, Buffer.from(args.data || '', 'utf-8'));
|
await vscode.workspace.fs.writeFile(
|
||||||
|
knowledgeUri,
|
||||||
|
Buffer.from(args.data || "", "utf-8"),
|
||||||
|
);
|
||||||
|
|
||||||
return `知识图谱已保存: .iccoder/knowledge.json`;
|
return `知识图谱已保存: .iccoder/knowledge.json`;
|
||||||
}
|
}
|
||||||
@ -337,20 +575,33 @@ async function executeKnowledgeSave(args: KnowledgeSaveArgs): Promise<string> {
|
|||||||
async function executeKnowledgeLoad(): Promise<string> {
|
async function executeKnowledgeLoad(): Promise<string> {
|
||||||
const workspaceFolder = getWorkspaceFolder();
|
const workspaceFolder = getWorkspaceFolder();
|
||||||
if (!workspaceFolder) {
|
if (!workspaceFolder) {
|
||||||
throw new Error('请先打开一个工作区');
|
throw new Error("请先打开一个工作区");
|
||||||
}
|
}
|
||||||
|
|
||||||
const knowledgeUri = vscode.Uri.joinPath(workspaceFolder.uri, '.iccoder', 'knowledge.json');
|
const knowledgeUri = vscode.Uri.joinPath(
|
||||||
|
workspaceFolder.uri,
|
||||||
|
".iccoder",
|
||||||
|
"knowledge.json",
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const bytes = await vscode.workspace.fs.readFile(knowledgeUri);
|
const bytes = await vscode.workspace.fs.readFile(knowledgeUri);
|
||||||
const content = Buffer.from(bytes).toString('utf-8');
|
const content = Buffer.from(bytes).toString("utf-8");
|
||||||
return content;
|
return content;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 文件不存在:返回空图谱
|
// 文件不存在:返回空图谱
|
||||||
if (error instanceof vscode.FileSystemError && error.code === 'FileNotFound') {
|
if (
|
||||||
|
error instanceof vscode.FileSystemError &&
|
||||||
|
error.code === "FileNotFound"
|
||||||
|
) {
|
||||||
// 与后端 KnowledgeGraph 结构保持一致(nodes/edges + nodeClass 多态字段)
|
// 与后端 KnowledgeGraph 结构保持一致(nodes/edges + nodeClass 多态字段)
|
||||||
return JSON.stringify({ taskId: '', version: 1, module: null, nodes: [], edges: [] });
|
return JSON.stringify({
|
||||||
|
taskId: "",
|
||||||
|
version: 1,
|
||||||
|
module: null,
|
||||||
|
nodes: [],
|
||||||
|
edges: [],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@ -363,7 +614,9 @@ function getWorkspaceFolder(): vscode.WorkspaceFolder | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const activeUri = vscode.window.activeTextEditor?.document?.uri;
|
const activeUri = vscode.window.activeTextEditor?.document?.uri;
|
||||||
const activeFolder = activeUri ? vscode.workspace.getWorkspaceFolder(activeUri) : undefined;
|
const activeFolder = activeUri
|
||||||
|
? vscode.workspace.getWorkspaceFolder(activeUri)
|
||||||
|
: undefined;
|
||||||
return activeFolder ?? folders[0];
|
return activeFolder ?? folders[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,22 +625,24 @@ function getWorkspaceFolder(): vscode.WorkspaceFolder | undefined {
|
|||||||
*/
|
*/
|
||||||
function getIverilogPath(extensionPath: string): string {
|
function getIverilogPath(extensionPath: string): string {
|
||||||
const platform = process.platform;
|
const platform = process.platform;
|
||||||
if (platform === 'win32') {
|
if (platform === "win32") {
|
||||||
return path.join(extensionPath, 'tools', 'iverilog', 'bin', 'iverilog.exe');
|
return path.join(extensionPath, "tools", "iverilog", "bin", "iverilog.exe");
|
||||||
} else {
|
} else {
|
||||||
return path.join(extensionPath, 'tools', 'iverilog', 'bin', 'iverilog');
|
return path.join(extensionPath, "tools", "iverilog", "bin", "iverilog");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建工具执行器上下文
|
* 创建工具执行器上下文
|
||||||
*/
|
*/
|
||||||
export function createToolExecutorContext(extensionPath: string): ToolExecutorContext {
|
export function createToolExecutorContext(
|
||||||
|
extensionPath: string,
|
||||||
|
): ToolExecutorContext {
|
||||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
const workspacePath = workspaceFolders?.[0]?.uri.fsPath || '';
|
const workspacePath = workspaceFolders?.[0]?.uri.fsPath || "";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
extensionPath,
|
extensionPath,
|
||||||
workspacePath
|
workspacePath,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/services/trialExpirationService.ts
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* 试用期过期检测服务
|
||||||
|
* 功能:检查插件试用用户是否过期
|
||||||
|
* 依赖:vscode, userService
|
||||||
|
* 使用场景:用户使用功能前检查是否过期
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import { getCachedUserInfo } from './userService';
|
||||||
|
|
||||||
|
export class TrialExpirationService {
|
||||||
|
private context: vscode.ExtensionContext;
|
||||||
|
private panel?: vscode.WebviewPanel;
|
||||||
|
|
||||||
|
constructor(context: vscode.ExtensionContext, panel?: vscode.WebviewPanel) {
|
||||||
|
this.context = context;
|
||||||
|
this.panel = panel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否过期
|
||||||
|
* @returns true=已过期,false=未过期
|
||||||
|
*/
|
||||||
|
public async checkExpiration(): Promise<boolean> {
|
||||||
|
const userInfo = getCachedUserInfo();
|
||||||
|
|
||||||
|
// 不是插件试用用户,不需要检查
|
||||||
|
if (!userInfo?.isPluginTrial) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 没有过期时间,不检查
|
||||||
|
if (!userInfo.pluginTrialExpiresAt) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否过期
|
||||||
|
const now = Date.now();
|
||||||
|
if (now >= userInfo.pluginTrialExpiresAt) {
|
||||||
|
// 已过期
|
||||||
|
await this.handleExpired();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理过期逻辑
|
||||||
|
*/
|
||||||
|
private async handleExpired(): Promise<void> {
|
||||||
|
// 通知前端显示过期弹窗
|
||||||
|
if (this.panel) {
|
||||||
|
this.panel.webview.postMessage({
|
||||||
|
command: 'showExpiredModal'
|
||||||
|
});
|
||||||
|
console.log('[TrialExpirationService] 已通知前端显示过期弹窗');
|
||||||
|
} else {
|
||||||
|
console.warn('[TrialExpirationService] panel 未提供,无法显示过期弹窗');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { submitAnswer, submitToolConfirm } from './apiClient';
|
import { submitAnswer, submitToolConfirm } from './apiClient';
|
||||||
import type { AskUserEvent, AnswerRequest } from '../types/api';
|
import type { AskUserEvent, AnswerRequest, QuestionItem } from '../types/api';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 待处理的用户问题
|
* 待处理的用户问题
|
||||||
@ -12,8 +12,7 @@ import type { AskUserEvent, AnswerRequest } from '../types/api';
|
|||||||
interface PendingQuestion {
|
interface PendingQuestion {
|
||||||
askId: string;
|
askId: string;
|
||||||
taskId: string;
|
taskId: string;
|
||||||
question: string;
|
questions: QuestionItem[];
|
||||||
options: string[];
|
|
||||||
resolve: (answer: string) => void;
|
resolve: (answer: string) => void;
|
||||||
reject: (error: Error) => void;
|
reject: (error: Error) => void;
|
||||||
}
|
}
|
||||||
@ -45,9 +44,9 @@ export class UserInteractionManager {
|
|||||||
* @param taskId 当前任务ID
|
* @param taskId 当前任务ID
|
||||||
*/
|
*/
|
||||||
async handleAskUser(event: AskUserEvent, taskId: string): Promise<void> {
|
async handleAskUser(event: AskUserEvent, taskId: string): Promise<void> {
|
||||||
const { askId, question, options } = event;
|
const { askId, questions } = event;
|
||||||
|
|
||||||
console.log(`[UserInteraction] 收到问题: askId=${askId}, question=${question}`);
|
console.log(`[UserInteraction] 收到问题: askId=${askId}, count=${questions.length}`);
|
||||||
|
|
||||||
// 注意:问题显示已经通过 dialogService 的 onSegmentUpdate 统一处理
|
// 注意:问题显示已经通过 dialogService 的 onSegmentUpdate 统一处理
|
||||||
// 这里不再单独发送 showQuestion 命令,避免重复显示
|
// 这里不再单独发送 showQuestion 命令,避免重复显示
|
||||||
@ -57,8 +56,7 @@ export class UserInteractionManager {
|
|||||||
this.pendingQuestions.set(askId, {
|
this.pendingQuestions.set(askId, {
|
||||||
askId,
|
askId,
|
||||||
taskId,
|
taskId,
|
||||||
question,
|
questions,
|
||||||
options,
|
|
||||||
resolve: (answer: string) => {
|
resolve: (answer: string) => {
|
||||||
this.submitUserAnswer(askId, taskId, answer)
|
this.submitUserAnswer(askId, taskId, answer)
|
||||||
.then(() => resolve())
|
.then(() => resolve())
|
||||||
@ -80,22 +78,43 @@ export class UserInteractionManager {
|
|||||||
/**
|
/**
|
||||||
* 处理用户提交的回答(从 WebView 调用)
|
* 处理用户提交的回答(从 WebView 调用)
|
||||||
* @param askId 问题ID
|
* @param askId 问题ID
|
||||||
* @param selected 选中的选项
|
* @param selected 选中的选项(旧格式)
|
||||||
* @param customInput 自定义输入
|
* @param customInput 自定义输入(旧格式)
|
||||||
|
* @param answers 新格式:按问题索引的答案
|
||||||
|
* @param fallbackTaskId 当问题不存在时使用的 taskId(用于直接发送到后端)
|
||||||
*/
|
*/
|
||||||
async receiveAnswer(
|
async receiveAnswer(
|
||||||
askId: string,
|
askId: string,
|
||||||
selected?: string[],
|
selected?: string[],
|
||||||
customInput?: string
|
customInput?: string,
|
||||||
|
answers?: { [questionIndex: string]: string[] },
|
||||||
|
fallbackTaskId?: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const pending = this.pendingQuestions.get(askId);
|
const pending = this.pendingQuestions.get(askId);
|
||||||
if (!pending) {
|
|
||||||
console.warn(`[UserInteraction] 问题不存在或已超时: askId=${askId}`);
|
// 构建答案字符串
|
||||||
return;
|
let answer = '';
|
||||||
|
if (answers && Object.keys(answers).length > 0) {
|
||||||
|
// 新格式:多问题答案
|
||||||
|
answer = Object.entries(answers)
|
||||||
|
.sort(([a], [b]) => parseInt(a) - parseInt(b))
|
||||||
|
.map(([_, vals]) => vals.join('; '))
|
||||||
|
.join(' | ');
|
||||||
|
} else {
|
||||||
|
// 旧格式:单问题答案
|
||||||
|
answer = customInput || selected?.join(', ') || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建答案
|
if (!pending) {
|
||||||
const answer = customInput || selected?.join(', ') || '';
|
// 问题不存在(可能是页面刷新或会话切换后),尝试直接发送到后端
|
||||||
|
if (fallbackTaskId) {
|
||||||
|
console.log(`[UserInteraction] 问题不在 pendingQuestions 中,直接发送到后端: askId=${askId}, taskId=${fallbackTaskId}`);
|
||||||
|
await this.submitUserAnswer(askId, fallbackTaskId, answer, answers);
|
||||||
|
} else {
|
||||||
|
console.warn(`[UserInteraction] 问题不存在且无 fallbackTaskId: askId=${askId}`);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`[UserInteraction] 收到用户回答: askId=${askId}, answer=${answer}`);
|
console.log(`[UserInteraction] 收到用户回答: askId=${askId}, answer=${answer}`);
|
||||||
|
|
||||||
@ -112,7 +131,8 @@ export class UserInteractionManager {
|
|||||||
private async submitUserAnswer(
|
private async submitUserAnswer(
|
||||||
askId: string,
|
askId: string,
|
||||||
taskId: string,
|
taskId: string,
|
||||||
answer: string
|
answer: string,
|
||||||
|
answers?: { [questionIndex: string]: string[] }
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// 检查是否是工具确认类型的问题
|
// 检查是否是工具确认类型的问题
|
||||||
if (askId.startsWith('tool_confirm_')) {
|
if (askId.startsWith('tool_confirm_')) {
|
||||||
@ -141,7 +161,8 @@ export class UserInteractionManager {
|
|||||||
const request: AnswerRequest = {
|
const request: AnswerRequest = {
|
||||||
askId,
|
askId,
|
||||||
taskId,
|
taskId,
|
||||||
customInput: answer
|
answers: answers,
|
||||||
|
customInput: answers ? undefined : answer
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -173,6 +194,13 @@ export class UserInteractionManager {
|
|||||||
hasPendingQuestions(): boolean {
|
hasPendingQuestions(): boolean {
|
||||||
return this.pendingQuestions.size > 0;
|
return this.pendingQuestions.size > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查特定问题是否存在
|
||||||
|
*/
|
||||||
|
hasPendingQuestion(askId: string): boolean {
|
||||||
|
return this.pendingQuestions.has(askId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 全局实例
|
// 全局实例
|
||||||
|
|||||||
416
src/services/userService.ts
Normal file
@ -0,0 +1,416 @@
|
|||||||
|
/**
|
||||||
|
* 用户服务
|
||||||
|
* 管理用户信息和认证相关的 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;
|
||||||
|
// 插件试用用户标识(从 JWT token 中提取)
|
||||||
|
isPluginTrial?: boolean;
|
||||||
|
// 试用到期时间(毫秒时间戳)
|
||||||
|
pluginTrialExpiresAt?: 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;
|
||||||
|
const userInfo: UserInfo = {
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
// 从接口响应中获取企业试用标识
|
||||||
|
if (response.isPluginTrial === true) {
|
||||||
|
userInfo.isPluginTrial = true;
|
||||||
|
console.log('[UserService] 从 getInfo 接口获取到 isPluginTrial: true');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取试用到期时间(null 表示长期有效)
|
||||||
|
if (response.enterpriseTrialExpires !== undefined) {
|
||||||
|
userInfo.pluginTrialExpiresAt = response.enterpriseTrialExpires;
|
||||||
|
if (response.enterpriseTrialExpires === null) {
|
||||||
|
console.log('[UserService] 试用长期有效');
|
||||||
|
} else {
|
||||||
|
console.log('[UserService] 试用到期时间:', new Date(response.enterpriseTrialExpires).toLocaleString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 【已禁用】试用用户和欢迎弹窗逻辑 - 无需登录
|
||||||
|
// if (userInfo.isPluginTrial === true && userInfo.pluginTrialExpiresAt !== null && userInfo.pluginTrialExpiresAt !== undefined) {
|
||||||
|
// const now = Date.now();
|
||||||
|
// const isExpired = now >= userInfo.pluginTrialExpiresAt;
|
||||||
|
// if (isExpired) {
|
||||||
|
// console.log('[UserService] 试用已过期,将显示邀请码弹窗');
|
||||||
|
// } else {
|
||||||
|
// const hasWelcomed = extensionContext?.globalState.get('pluginTrialWelcomed');
|
||||||
|
// if (!hasWelcomed && extensionContext) {
|
||||||
|
// await extensionContext.globalState.update('showWelcomeModal', true);
|
||||||
|
// await extensionContext.globalState.update('pluginTrialWelcomed', true);
|
||||||
|
// console.log('[UserService] ✅ 已设置欢迎弹窗标记 showWelcomeModal=true');
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
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] 用户信息已清除');
|
||||||
|
}
|
||||||
508
src/services/vcdFileServer.ts
Normal file
@ -0,0 +1,508 @@
|
|||||||
|
import * as http from "http";
|
||||||
|
import * as fs from "fs";
|
||||||
|
import * as path from "path";
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VCD 文件 HTTP 服务器
|
||||||
|
* 用于为 波形查看器提供 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>波形查看器 - ${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>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
345
src/types/api.ts
@ -3,7 +3,7 @@
|
|||||||
* 对应后端 IC Coder Backend 的接口格式
|
* 对应后端 IC Coder Backend 的接口格式
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { CompactedMemory, CompactedMessage } from './memory';
|
import { CompactedMemory, CompactedMessage } from "./memory";
|
||||||
|
|
||||||
// ============== 对话请求/响应 ==============
|
// ============== 对话请求/响应 ==============
|
||||||
|
|
||||||
@ -14,7 +14,16 @@ import { CompactedMemory, CompactedMessage } from './memory';
|
|||||||
* - agent: 智能体自主(默认)
|
* - agent: 智能体自主(默认)
|
||||||
* - auto: 完全自动
|
* - auto: 完全自动
|
||||||
*/
|
*/
|
||||||
export type RunMode = 'plan' | 'ask' | 'agent' | 'auto';
|
export type RunMode = "plan" | "ask" | "agent" | "auto";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务等级类型
|
||||||
|
* - lite: 轻量级
|
||||||
|
* - syntaxic: 语法级
|
||||||
|
* - max: 最大性能
|
||||||
|
* - auto: 自动选择
|
||||||
|
*/
|
||||||
|
export type ServiceTier = "lite" | "syntaxic" | "max" | "auto";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对话请求
|
* 对话请求
|
||||||
@ -29,6 +38,10 @@ export interface DialogRequest {
|
|||||||
userId: string;
|
userId: string;
|
||||||
/** 运行模式 */
|
/** 运行模式 */
|
||||||
mode: RunMode;
|
mode: RunMode;
|
||||||
|
/** 服务等级 */
|
||||||
|
serviceTier?: ServiceTier;
|
||||||
|
/** JWT Token(用于认证和扣费) */
|
||||||
|
token?: string;
|
||||||
/** 压缩后的记忆数据(用于后端重启后恢复) */
|
/** 压缩后的记忆数据(用于后端重启后恢复) */
|
||||||
compactedData?: CompactedMemory;
|
compactedData?: CompactedMemory;
|
||||||
/** 压缩后产生的新消息 */
|
/** 压缩后产生的新消息 */
|
||||||
@ -41,25 +54,32 @@ export interface DialogRequest {
|
|||||||
|
|
||||||
/** SSE 事件类型枚举 */
|
/** SSE 事件类型枚举 */
|
||||||
export type SSEEventType =
|
export type SSEEventType =
|
||||||
| 'text_delta' // 文本增量
|
| "text_delta" // 文本增量
|
||||||
| 'tool_call' // 客户端工具调用请求
|
| "tool_call" // 客户端工具调用请求
|
||||||
| 'tool_confirm' // 工具确认请求(Ask 模式)
|
| "tool_confirm" // 工具确认请求(Ask 模式)
|
||||||
| 'plan_confirm' // 计划确认请求(Plan 模式)
|
| "plan_confirm" // 计划确认请求(Plan 模式)
|
||||||
| 'tool_start' // 工具开始执行
|
| "phase_progress" // 阶段进度更新
|
||||||
| 'tool_complete' // 工具执行完成
|
| "plan_step_add" // 添加计划步骤
|
||||||
| 'tool_error' // 工具执行错误
|
| "plan_step_remove" // 删除计划步骤
|
||||||
| 'ask_user' // 向用户提问
|
| "plan_step_update" // 更新计划步骤
|
||||||
| 'agent_start' // 子智能体启动
|
| "plan_summary_update" // 更新计划摘要
|
||||||
| 'agent_progress' // 子智能体进度
|
| "tool_start" // 工具开始执行
|
||||||
| 'agent_complete' // 子智能体完成
|
| "tool_complete" // 工具执行完成
|
||||||
| 'agent_error' // 子智能体错误
|
| "tool_error" // 工具执行错误
|
||||||
| 'memory_compacted' // 记忆压缩完成
|
| "ask_user" // 向用户提问
|
||||||
| 'context_usage' // 上下文使用量
|
| "agent_start" // 子智能体启动
|
||||||
| 'complete' // 对话完成
|
| "agent_progress" // 子智能体进度
|
||||||
| 'error' // 错误
|
| "agent_complete" // 子智能体完成
|
||||||
| 'warning' // 警告
|
| "agent_error" // 子智能体错误
|
||||||
| 'notification' // 通知
|
| "memory_compacted" // 记忆压缩完成
|
||||||
| 'depth_update'; // 深度更新
|
| "context_usage" // 上下文使用量
|
||||||
|
| "credit_update" // 资源点余额更新
|
||||||
|
| "complete" // 对话完成
|
||||||
|
| "error" // 错误
|
||||||
|
| "warning" // 警告
|
||||||
|
| "notification" // 通知
|
||||||
|
| "depth_update" // 深度更新
|
||||||
|
| "heartbeat"; // 心跳
|
||||||
|
|
||||||
/** text_delta 事件数据 */
|
/** text_delta 事件数据 */
|
||||||
export interface TextDeltaEvent {
|
export interface TextDeltaEvent {
|
||||||
@ -76,6 +96,7 @@ export interface ToolStartEvent {
|
|||||||
export interface ToolCompleteEvent {
|
export interface ToolCompleteEvent {
|
||||||
tool_name: string;
|
tool_name: string;
|
||||||
result: string;
|
result: string;
|
||||||
|
description?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** tool_error 事件数据 */
|
/** tool_error 事件数据 */
|
||||||
@ -96,25 +117,94 @@ export interface ToolConfirmEvent {
|
|||||||
timestamp: number;
|
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 模式计划确认) */
|
/** plan_confirm 事件数据(Plan 模式计划确认) */
|
||||||
export interface PlanConfirmEvent {
|
export interface PlanConfirmEvent {
|
||||||
/** 确认ID */
|
/** 确认ID */
|
||||||
confirmId: number;
|
confirmId: number;
|
||||||
/** 计划标题 */
|
/** 计划标题 */
|
||||||
title: string;
|
title: string;
|
||||||
/** 执行步骤列表 */
|
/** 四阶段计划列表(新格式) */
|
||||||
steps: string[];
|
phases?: PlanPhase[];
|
||||||
|
/** 执行步骤列表(旧格式,兼容) */
|
||||||
|
steps?: string[];
|
||||||
/** 计划摘要 */
|
/** 计划摘要 */
|
||||||
summary: string;
|
summary: string;
|
||||||
/** 时间戳 */
|
/** 时间戳 */
|
||||||
timestamp: number;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 单个问题项 */
|
||||||
|
export interface QuestionItem {
|
||||||
|
question: string;
|
||||||
|
options: string[];
|
||||||
|
multiSelect?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/** ask_user 事件数据 */
|
/** ask_user 事件数据 */
|
||||||
export interface AskUserEvent {
|
export interface AskUserEvent {
|
||||||
askId: string;
|
askId: string;
|
||||||
question: string;
|
questions: QuestionItem[];
|
||||||
options: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** complete 事件数据 */
|
/** complete 事件数据 */
|
||||||
@ -161,7 +251,7 @@ export interface AgentProgressEvent {
|
|||||||
toolName: string;
|
toolName: string;
|
||||||
toolInput?: unknown;
|
toolInput?: unknown;
|
||||||
toolResult?: string;
|
toolResult?: string;
|
||||||
status: 'running' | 'completed' | 'error';
|
status: "running" | "completed" | "error";
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,6 +279,12 @@ export interface ContextUsageEvent {
|
|||||||
percentage: number;
|
percentage: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** credit_update 事件数据 */
|
||||||
|
export interface CreditUpdateEvent {
|
||||||
|
deductedCredits: number;
|
||||||
|
remainingCredits: number;
|
||||||
|
}
|
||||||
|
|
||||||
// ============== 工具调用协议 (MCP 格式) ==============
|
// ============== 工具调用协议 (MCP 格式) ==============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -197,11 +293,11 @@ export interface ContextUsageEvent {
|
|||||||
*/
|
*/
|
||||||
export interface ToolCallRequest {
|
export interface ToolCallRequest {
|
||||||
/** JSON-RPC版本,固定为"2.0" */
|
/** JSON-RPC版本,固定为"2.0" */
|
||||||
jsonrpc: '2.0';
|
jsonrpc: "2.0";
|
||||||
/** 请求ID,用于匹配响应 */
|
/** 请求ID,用于匹配响应 */
|
||||||
id: number;
|
id: number;
|
||||||
/** 方法名,固定为"tools/call" */
|
/** 方法名,固定为"tools/call" */
|
||||||
method: 'tools/call';
|
method: "tools/call";
|
||||||
/** 调用参数 */
|
/** 调用参数 */
|
||||||
params: {
|
params: {
|
||||||
/** 工具名称 */
|
/** 工具名称 */
|
||||||
@ -217,7 +313,7 @@ export interface ToolCallRequest {
|
|||||||
*/
|
*/
|
||||||
export interface ToolCallResult {
|
export interface ToolCallResult {
|
||||||
/** JSON-RPC版本 */
|
/** JSON-RPC版本 */
|
||||||
jsonrpc: '2.0';
|
jsonrpc: "2.0";
|
||||||
/** 请求ID,与ToolCallRequest.id对应 */
|
/** 请求ID,与ToolCallRequest.id对应 */
|
||||||
id: number;
|
id: number;
|
||||||
/** 执行结果(与error互斥) */
|
/** 执行结果(与error互斥) */
|
||||||
@ -261,10 +357,12 @@ export interface AnswerRequest {
|
|||||||
askId: string;
|
askId: string;
|
||||||
/** 任务ID */
|
/** 任务ID */
|
||||||
taskId: string;
|
taskId: string;
|
||||||
/** 选中的选项列表 */
|
/** 选中的选项列表(旧格式,兼容) */
|
||||||
selected?: string[];
|
selected?: string[];
|
||||||
/** 自定义输入内容 */
|
/** 自定义输入内容(旧格式,兼容) */
|
||||||
customInput?: string;
|
customInput?: string;
|
||||||
|
/** 新格式:按问题索引的答案 */
|
||||||
|
answers?: { [questionIndex: string]: string[] };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 用户回答响应 */
|
/** 用户回答响应 */
|
||||||
@ -298,19 +396,116 @@ export interface ToolConfirmResponse {
|
|||||||
approved: boolean;
|
approved: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============== 用户信息 ==============
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息响应
|
||||||
|
* GET /system/user/getInfo
|
||||||
|
*/
|
||||||
|
export interface UserInfoResponse {
|
||||||
|
/** 响应消息 */
|
||||||
|
msg: string;
|
||||||
|
/** 响应代码 (200 表示成功) */
|
||||||
|
code: number;
|
||||||
|
/** 权限列表 */
|
||||||
|
permissions: string[];
|
||||||
|
/** 角色列表 */
|
||||||
|
roles: string[];
|
||||||
|
/** 是否默认修改密码 */
|
||||||
|
isDefaultModifyPwd: boolean;
|
||||||
|
/** 密码是否过期 */
|
||||||
|
isPasswordExpired: boolean;
|
||||||
|
/** 是否为插件试用用户 */
|
||||||
|
isPluginTrial?: boolean;
|
||||||
|
/** 企业试用到期时间(毫秒时间戳) */
|
||||||
|
enterpriseTrialExpires?: number;
|
||||||
|
/** 用户信息 */
|
||||||
|
user: {
|
||||||
|
userId: number;
|
||||||
|
userName: string;
|
||||||
|
nickName: string;
|
||||||
|
email?: string;
|
||||||
|
phonenumber?: string;
|
||||||
|
sex?: string;
|
||||||
|
avatar?: string;
|
||||||
|
status?: string;
|
||||||
|
createTime?: string;
|
||||||
|
loginDate?: string;
|
||||||
|
remark?: 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 =
|
export type ToolName =
|
||||||
| 'file_read'
|
| "file_read"
|
||||||
| 'file_write'
|
| "file_write"
|
||||||
| 'file_delete'
|
| "file_delete"
|
||||||
| 'file_list'
|
| "file_list"
|
||||||
| 'syntax_check'
|
| "syntax_check"
|
||||||
| 'simulation'
|
| "iverilog"
|
||||||
| 'waveform_summary'
|
| "simulation"
|
||||||
| 'knowledge_save'
|
| "waveform_summary"
|
||||||
| 'knowledge_load';
|
| "waveform_trace"
|
||||||
|
| "knowledge_save"
|
||||||
|
| "knowledge_load";
|
||||||
|
|
||||||
/** file_read 工具参数 */
|
/** file_read 工具参数 */
|
||||||
export interface FileReadArgs {
|
export interface FileReadArgs {
|
||||||
@ -340,11 +535,21 @@ export interface SyntaxCheckArgs {
|
|||||||
code: string;
|
code: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** iverilog 工具参数 */
|
||||||
|
export interface IverilogArgs {
|
||||||
|
args: string;
|
||||||
|
workDir?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/** simulation 工具参数 */
|
/** simulation 工具参数 */
|
||||||
export interface SimulationArgs {
|
export interface SimulationArgs {
|
||||||
rtlPath: string;
|
rtlPath: string;
|
||||||
tbPath: string;
|
tbPath: string;
|
||||||
duration?: string;
|
duration?: string;
|
||||||
|
/** 要dump的模块列表,格式:name:path,name:path */
|
||||||
|
dumpModules?: string;
|
||||||
|
/** VCD输出目录,默认'vcd' */
|
||||||
|
vcdDir?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** waveform_summary 工具参数 */
|
/** waveform_summary 工具参数 */
|
||||||
@ -354,6 +559,18 @@ export interface WaveformSummaryArgs {
|
|||||||
checkpoints?: string;
|
checkpoints?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** waveform_trace 工具参数 */
|
||||||
|
export interface WaveformTraceArgs {
|
||||||
|
/** Verilog 源文件路径(相对于项目根目录) */
|
||||||
|
verilogPath: string;
|
||||||
|
/** VCD 波形文件路径(相对于项目根目录) */
|
||||||
|
vcdPath: string;
|
||||||
|
/** 仿真工具的输出字符串(包含 mismatch 信息) */
|
||||||
|
simOutput: string;
|
||||||
|
/** BFS 回溯层数,默认 2 */
|
||||||
|
traceLevel?: number;
|
||||||
|
}
|
||||||
|
|
||||||
/** knowledge_save 工具参数 */
|
/** knowledge_save 工具参数 */
|
||||||
export interface KnowledgeSaveArgs {
|
export interface KnowledgeSaveArgs {
|
||||||
/** 知识图谱 JSON 数据 */
|
/** 知识图谱 JSON 数据 */
|
||||||
@ -372,7 +589,55 @@ export type ToolArgs =
|
|||||||
| FileDeleteArgs
|
| FileDeleteArgs
|
||||||
| FileListArgs
|
| FileListArgs
|
||||||
| SyntaxCheckArgs
|
| SyntaxCheckArgs
|
||||||
|
| IverilogArgs
|
||||||
| SimulationArgs
|
| SimulationArgs
|
||||||
| WaveformSummaryArgs
|
| WaveformSummaryArgs
|
||||||
|
| WaveformTraceArgs
|
||||||
| KnowledgeSaveArgs
|
| KnowledgeSaveArgs
|
||||||
| KnowledgeLoadArgs;
|
| 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
47
src/types/fileChanges.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* 文件变更追踪类型定义
|
||||||
|
* 功能:定义代码变更的数据结构
|
||||||
|
* 依赖:无
|
||||||
|
* 使用场景:AI 修改文件后的变更审查
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单个文件的变更记录
|
||||||
|
*/
|
||||||
|
export interface FileChange {
|
||||||
|
/** 文件相对路径 */
|
||||||
|
filePath: string;
|
||||||
|
/** 修改前的内容 */
|
||||||
|
oldContent: string;
|
||||||
|
/** 修改后的内容 */
|
||||||
|
newContent: string;
|
||||||
|
/** 变更时间戳 */
|
||||||
|
timestamp: number;
|
||||||
|
/** 变更类型 */
|
||||||
|
changeType: 'create' | 'modify' | 'delete';
|
||||||
|
/** 变更 ID(唯一标识) */
|
||||||
|
changeId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 变更会话(一次对话的所有变更)
|
||||||
|
*/
|
||||||
|
export interface ChangeSession {
|
||||||
|
/** 会话 ID */
|
||||||
|
sessionId: string;
|
||||||
|
/** 会话开始时间 */
|
||||||
|
startTime: number;
|
||||||
|
/** 所有文件变更 */
|
||||||
|
changes: FileChange[];
|
||||||
|
/** 会话状态 */
|
||||||
|
status: 'active' | 'completed';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 变更操作结果
|
||||||
|
*/
|
||||||
|
export interface ChangeActionResult {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
changeId: string;
|
||||||
|
}
|
||||||
@ -715,6 +715,10 @@ export class ChatHistoryManager {
|
|||||||
|
|
||||||
if (!projectPath) {
|
if (!projectPath) {
|
||||||
console.error('[ChatHistoryManager] 无法保存压缩数据:projectPath 为空');
|
console.error('[ChatHistoryManager] 无法保存压缩数据:projectPath 为空');
|
||||||
|
// 通知用户压缩数据保存失败
|
||||||
|
vscode.window.showWarningMessage(
|
||||||
|
'对话历史压缩数据保存失败:无法确定项目路径。后端重启后可能无法恢复完整对话历史。'
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -731,6 +735,19 @@ export class ChatHistoryManager {
|
|||||||
// 文件不存在,使用空数组
|
// 文件不存在,使用空数组
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 版本检查:防止旧版本覆盖新版本(从尾部扫描,与加载逻辑一致)
|
||||||
|
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 = {
|
const summaryMessage: CompactionSummaryMessage = {
|
||||||
type: MessageType.COMPACTION_SUMMARY,
|
type: MessageType.COMPACTION_SUMMARY,
|
||||||
@ -893,4 +910,14 @@ export class ChatHistoryManager {
|
|||||||
content: text
|
content: text
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 追踪新消息(工具执行结果)
|
||||||
|
*/
|
||||||
|
public trackToolResult(toolName: string, result: string): void {
|
||||||
|
this.newMessagesSinceCompaction.push({
|
||||||
|
type: 'TOOL_RESULT',
|
||||||
|
content: `[${toolName}] ${result}`
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
203
src/utils/diffRenderer.ts
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
/**
|
||||||
|
* Diff 渲染工具
|
||||||
|
* 功能:生成代码差异的 HTML 展示
|
||||||
|
* 依赖:无
|
||||||
|
* 使用场景:在变更面板中展示文件修改的 diff
|
||||||
|
*/
|
||||||
|
|
||||||
|
interface DiffLine {
|
||||||
|
type: 'add' | 'remove' | 'context';
|
||||||
|
content: string;
|
||||||
|
oldLineNumber?: number;
|
||||||
|
newLineNumber?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 简单的 diff 算法(基于行)
|
||||||
|
*/
|
||||||
|
export function generateDiff(oldContent: string, newContent: string): DiffLine[] {
|
||||||
|
const oldLines = oldContent.split('\n');
|
||||||
|
const newLines = newContent.split('\n');
|
||||||
|
const result: DiffLine[] = [];
|
||||||
|
|
||||||
|
let oldIndex = 0;
|
||||||
|
let newIndex = 0;
|
||||||
|
|
||||||
|
while (oldIndex < oldLines.length || newIndex < newLines.length) {
|
||||||
|
const oldLine = oldLines[oldIndex];
|
||||||
|
const newLine = newLines[newIndex];
|
||||||
|
|
||||||
|
if (oldIndex >= oldLines.length) {
|
||||||
|
// 只剩新行
|
||||||
|
result.push({
|
||||||
|
type: 'add',
|
||||||
|
content: newLine,
|
||||||
|
newLineNumber: newIndex + 1
|
||||||
|
});
|
||||||
|
newIndex++;
|
||||||
|
} else if (newIndex >= newLines.length) {
|
||||||
|
// 只剩旧行
|
||||||
|
result.push({
|
||||||
|
type: 'remove',
|
||||||
|
content: oldLine,
|
||||||
|
oldLineNumber: oldIndex + 1
|
||||||
|
});
|
||||||
|
oldIndex++;
|
||||||
|
} else if (oldLine === newLine) {
|
||||||
|
// 相同行
|
||||||
|
result.push({
|
||||||
|
type: 'context',
|
||||||
|
content: oldLine,
|
||||||
|
oldLineNumber: oldIndex + 1,
|
||||||
|
newLineNumber: newIndex + 1
|
||||||
|
});
|
||||||
|
oldIndex++;
|
||||||
|
newIndex++;
|
||||||
|
} else {
|
||||||
|
// 不同行,标记为删除和添加
|
||||||
|
result.push({
|
||||||
|
type: 'remove',
|
||||||
|
content: oldLine,
|
||||||
|
oldLineNumber: oldIndex + 1
|
||||||
|
});
|
||||||
|
result.push({
|
||||||
|
type: 'add',
|
||||||
|
content: newLine,
|
||||||
|
newLineNumber: newIndex + 1
|
||||||
|
});
|
||||||
|
oldIndex++;
|
||||||
|
newIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 diff 结果渲染为 HTML
|
||||||
|
*/
|
||||||
|
export function renderDiffHtml(diffLines: DiffLine[]): string {
|
||||||
|
let html = '<div class="diff-viewer">';
|
||||||
|
|
||||||
|
for (const line of diffLines) {
|
||||||
|
const lineClass = `diff-line diff-line-${line.type}`;
|
||||||
|
const oldNum = line.oldLineNumber ? `<span class="line-num">${line.oldLineNumber}</span>` : '<span class="line-num"></span>';
|
||||||
|
const newNum = line.newLineNumber ? `<span class="line-num">${line.newLineNumber}</span>` : '<span class="line-num"></span>';
|
||||||
|
const prefix = line.type === 'add' ? '+' : line.type === 'remove' ? '-' : ' ';
|
||||||
|
const escapedContent = escapeHtml(line.content);
|
||||||
|
|
||||||
|
html += `
|
||||||
|
<div class="${lineClass}">
|
||||||
|
${oldNum}
|
||||||
|
${newNum}
|
||||||
|
<span class="line-prefix">${prefix}</span>
|
||||||
|
<span class="line-content">${escapedContent}</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
html += '</div>';
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转义 HTML 特殊字符
|
||||||
|
*/
|
||||||
|
function escapeHtml(text: string): string {
|
||||||
|
const map: Record<string, string> = {
|
||||||
|
'&': '&',
|
||||||
|
'<': '<',
|
||||||
|
'>': '>',
|
||||||
|
'"': '"',
|
||||||
|
"'": '''
|
||||||
|
};
|
||||||
|
return text.replace(/[&<>"']/g, m => map[m]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 diff 样式
|
||||||
|
*/
|
||||||
|
export function getDiffStyles(): string {
|
||||||
|
return `
|
||||||
|
.diff-viewer {
|
||||||
|
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
|
||||||
|
font-size: 13px;
|
||||||
|
line-height: 1.6;
|
||||||
|
background: var(--vscode-editor-background);
|
||||||
|
border: 1px solid var(--vscode-panel-border);
|
||||||
|
border-radius: 6px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
padding: 4px 0;
|
||||||
|
white-space: pre;
|
||||||
|
transition: background 0.15s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line:hover {
|
||||||
|
background: var(--vscode-list-hoverBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-add {
|
||||||
|
background: rgba(40, 167, 69, 0.2);
|
||||||
|
border-left: 3px solid #28a745;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-add:hover {
|
||||||
|
background: rgba(40, 167, 69, 0.25);
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-remove {
|
||||||
|
background: rgba(220, 53, 69, 0.2);
|
||||||
|
border-left: 3px solid #dc3545;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-remove:hover {
|
||||||
|
background: rgba(220, 53, 69, 0.25);
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-context {
|
||||||
|
background: transparent;
|
||||||
|
border-left: 3px solid transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.line-num {
|
||||||
|
display: inline-block;
|
||||||
|
width: 45px;
|
||||||
|
text-align: right;
|
||||||
|
padding: 0 10px;
|
||||||
|
color: var(--vscode-editorLineNumber-foreground);
|
||||||
|
user-select: none;
|
||||||
|
flex-shrink: 0;
|
||||||
|
font-size: 11px;
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.line-prefix {
|
||||||
|
display: inline-block;
|
||||||
|
width: 24px;
|
||||||
|
text-align: center;
|
||||||
|
font-weight: bold;
|
||||||
|
flex-shrink: 0;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-add .line-prefix {
|
||||||
|
color: #28a745;
|
||||||
|
}
|
||||||
|
|
||||||
|
.diff-line-remove .line-prefix {
|
||||||
|
color: #dc3545;
|
||||||
|
}
|
||||||
|
|
||||||
|
.line-content {
|
||||||
|
flex: 1;
|
||||||
|
padding: 0 12px 0 8px;
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
51
src/utils/fileDiff.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import * as vscode from "vscode";
|
||||||
|
import * as path from "path";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将相对路径解析为工作区绝对路径
|
||||||
|
*/
|
||||||
|
export function resolveWorkspaceFilePath(filePath: string): string {
|
||||||
|
if (path.isAbsolute(filePath)) {
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error("请先打开一个文件夹作为工作区,这样我就能为您修改文件了");
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 VS Code 原生 diff 视图展示文件修改前后对比
|
||||||
|
*/
|
||||||
|
export async function showFileDiff(
|
||||||
|
filePath: string,
|
||||||
|
oldContent: string,
|
||||||
|
title?: string
|
||||||
|
): Promise<void> {
|
||||||
|
const absolutePath = resolveWorkspaceFilePath(filePath);
|
||||||
|
const fileUri = vscode.Uri.file(absolutePath);
|
||||||
|
const newBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||||
|
const newContent = Buffer.from(newBytes).toString("utf-8");
|
||||||
|
|
||||||
|
if (oldContent === newContent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const language = (await vscode.workspace.openTextDocument(fileUri)).languageId;
|
||||||
|
const oldDoc = await vscode.workspace.openTextDocument({
|
||||||
|
content: oldContent,
|
||||||
|
language,
|
||||||
|
});
|
||||||
|
|
||||||
|
const diffTitle = title || `${path.basename(absolutePath)} (修改前 <-> 修改后)`;
|
||||||
|
await vscode.commands.executeCommand(
|
||||||
|
"vscode.diff",
|
||||||
|
oldDoc.uri,
|
||||||
|
fileUri,
|
||||||
|
diffTitle,
|
||||||
|
{ preview: false }
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -7,7 +7,7 @@ import { promisify } from "util";
|
|||||||
function execCommand(
|
function execCommand(
|
||||||
command: string,
|
command: string,
|
||||||
args: string[],
|
args: string[],
|
||||||
options: { cwd: string; env?: any }
|
options: { cwd: string; env?: any },
|
||||||
): Promise<{ stdout: string; stderr: string }> {
|
): Promise<{ stdout: string; stderr: string }> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// 在 Windows 上,如果路径包含空格,不使用 shell,直接用 spawn
|
// 在 Windows 上,如果路径包含空格,不使用 shell,直接用 spawn
|
||||||
@ -23,25 +23,25 @@ function execCommand(
|
|||||||
let stderr = "";
|
let stderr = "";
|
||||||
|
|
||||||
// 在 Windows 上使用 GBK 编码解码输出
|
// 在 Windows 上使用 GBK 编码解码输出
|
||||||
const encoding = process.platform === 'win32' ? 'gbk' : 'utf8';
|
const encoding = process.platform === "win32" ? "gbk" : "utf8";
|
||||||
|
|
||||||
child.stdout.on("data", (data) => {
|
child.stdout.on("data", (data) => {
|
||||||
try {
|
try {
|
||||||
// 尝试使用 iconv-lite 解码(如果可用)
|
// 尝试使用 iconv-lite 解码(如果可用)
|
||||||
const iconv = require('iconv-lite');
|
const iconv = require("iconv-lite");
|
||||||
stdout += iconv.decode(data, encoding);
|
stdout += iconv.decode(data, encoding);
|
||||||
} catch {
|
} catch {
|
||||||
// 如果 iconv-lite 不可用,使用默认解码
|
// 如果 iconv-lite 不可用,使用默认解码
|
||||||
stdout += data.toString('utf8');
|
stdout += data.toString("utf8");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stderr.on("data", (data) => {
|
child.stderr.on("data", (data) => {
|
||||||
try {
|
try {
|
||||||
const iconv = require('iconv-lite');
|
const iconv = require("iconv-lite");
|
||||||
stderr += iconv.decode(data, encoding);
|
stderr += iconv.decode(data, encoding);
|
||||||
} catch {
|
} catch {
|
||||||
stderr += data.toString('utf8');
|
stderr += data.toString("utf8");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ export interface VCDGenerationResult {
|
|||||||
* 检查项目中的 Verilog 文件完整性
|
* 检查项目中的 Verilog 文件完整性
|
||||||
*/
|
*/
|
||||||
export async function checkVerilogProject(
|
export async function checkVerilogProject(
|
||||||
projectPath: string
|
projectPath: string,
|
||||||
): Promise<VerilogProjectCheck> {
|
): Promise<VerilogProjectCheck> {
|
||||||
const result: VerilogProjectCheck = {
|
const result: VerilogProjectCheck = {
|
||||||
isComplete: false,
|
isComplete: false,
|
||||||
@ -164,7 +164,7 @@ export async function checkVerilogProject(
|
|||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
result.errors.push(
|
result.errors.push(
|
||||||
`检查项目时出错: ${error instanceof Error ? error.message : "未知错误"}`
|
`检查项目时出错: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
);
|
);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -201,6 +201,33 @@ async function findVerilogFiles(dir: string): Promise<string[]> {
|
|||||||
return verilogFiles;
|
return verilogFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归查找目录下所有 VCD 文件
|
||||||
|
*/
|
||||||
|
async function findVcdFilesRecursive(dir: string): Promise<string[]> {
|
||||||
|
const vcdFiles: string[] = [];
|
||||||
|
|
||||||
|
async function searchDir(currentDir: string) {
|
||||||
|
const dirUri = vscode.Uri.file(currentDir);
|
||||||
|
const entries = await vscode.workspace.fs.readDirectory(dirUri);
|
||||||
|
|
||||||
|
for (const [fileName, fileType] of entries) {
|
||||||
|
const filePath = path.join(currentDir, fileName);
|
||||||
|
|
||||||
|
if (fileType === vscode.FileType.Directory) {
|
||||||
|
if (!fileName.startsWith(".") && fileName !== "node_modules") {
|
||||||
|
await searchDir(filePath);
|
||||||
|
}
|
||||||
|
} else if (fileType === vscode.FileType.File && fileName.endsWith(".vcd")) {
|
||||||
|
vcdFiles.push(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await searchDir(dir);
|
||||||
|
return vcdFiles;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 iverilog 可执行文件路径
|
* 获取 iverilog 可执行文件路径
|
||||||
*/
|
*/
|
||||||
@ -209,12 +236,30 @@ async function getIverilogPath(extensionPath: string): Promise<string> {
|
|||||||
let iverilogBin = "";
|
let iverilogBin = "";
|
||||||
|
|
||||||
if (platform === "win32") {
|
if (platform === "win32") {
|
||||||
iverilogBin = path.join(extensionPath, "tools", "iverilog", "bin", "iverilog.exe");
|
iverilogBin = path.join(
|
||||||
|
extensionPath,
|
||||||
|
"tools",
|
||||||
|
"iverilog",
|
||||||
|
"bin",
|
||||||
|
"iverilog.exe",
|
||||||
|
);
|
||||||
} else if (platform === "darwin") {
|
} else if (platform === "darwin") {
|
||||||
iverilogBin = path.join(extensionPath, "tools", "iverilog", "bin", "iverilog");
|
iverilogBin = path.join(
|
||||||
|
extensionPath,
|
||||||
|
"tools",
|
||||||
|
"iverilog",
|
||||||
|
"bin",
|
||||||
|
"iverilog",
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// Linux
|
// Linux
|
||||||
iverilogBin = path.join(extensionPath, "tools", "iverilog", "bin", "iverilog");
|
iverilogBin = path.join(
|
||||||
|
extensionPath,
|
||||||
|
"tools",
|
||||||
|
"iverilog",
|
||||||
|
"bin",
|
||||||
|
"iverilog",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果插件包中没有,尝试使用系统安装的 iverilog
|
// 如果插件包中没有,尝试使用系统安装的 iverilog
|
||||||
@ -258,7 +303,7 @@ async function getVvpPath(extensionPath: string): Promise<string> {
|
|||||||
*/
|
*/
|
||||||
export async function generateVCD(
|
export async function generateVCD(
|
||||||
projectPath: string,
|
projectPath: string,
|
||||||
extensionPath: string
|
extensionPath: string,
|
||||||
): Promise<VCDGenerationResult> {
|
): Promise<VCDGenerationResult> {
|
||||||
try {
|
try {
|
||||||
// 1. 检查项目完整性
|
// 1. 检查项目完整性
|
||||||
@ -286,8 +331,8 @@ export async function generateVCD(
|
|||||||
IVERILOG_ROOT: path.join(extensionPath, "tools", "iverilog"),
|
IVERILOG_ROOT: path.join(extensionPath, "tools", "iverilog"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// 5. 构建 iverilog 编译参数
|
// 5. 构建 iverilog 编译参数(启用 SystemVerilog 2012 标准)
|
||||||
const compileArgs = ["-o", outputFile, ...projectCheck.allVerilogFiles];
|
const compileArgs = ["-g2012", "-o", outputFile, ...projectCheck.allVerilogFiles];
|
||||||
|
|
||||||
console.log("执行编译命令:", iverilogPath, compileArgs.join(" "));
|
console.log("执行编译命令:", iverilogPath, compileArgs.join(" "));
|
||||||
console.log("IVERILOG_ROOT:", env.IVERILOG_ROOT);
|
console.log("IVERILOG_ROOT:", env.IVERILOG_ROOT);
|
||||||
@ -299,15 +344,97 @@ export async function generateVCD(
|
|||||||
cwd: projectPath,
|
cwd: projectPath,
|
||||||
env: env,
|
env: env,
|
||||||
});
|
});
|
||||||
|
console.log("编译成功,stdout:", compileResult.stdout);
|
||||||
|
console.log("编译成功,stderr:", compileResult.stderr);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.error("编译失败:", error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: `iverilog 编译失败:\n${error.message}`,
|
message: `IC Coder编译器编译失败:\n${error.message}`,
|
||||||
stderr: error.stderr,
|
stderr: error.stderr,
|
||||||
stdout: error.stdout,
|
stdout: error.stdout,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 6.1 检查 .vvp 文件是否生成
|
||||||
|
const fs = require("fs");
|
||||||
|
if (!fs.existsSync(outputFile)) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `编译未生成 .vvp 文件: ${outputFile}`,
|
||||||
|
stderr: compileResult.stderr,
|
||||||
|
stdout: compileResult.stdout,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
console.log("已生成 .vvp 文件:", outputFile);
|
||||||
|
|
||||||
|
// 6.5. 删除 shebang 行(修复 Windows 上的 vvp 解析错误)
|
||||||
|
try {
|
||||||
|
const vvpContent = fs.readFileSync(outputFile, "utf8");
|
||||||
|
const lines = vvpContent.split("\n");
|
||||||
|
|
||||||
|
if (lines.length > 0 && lines[0].startsWith("#!")) {
|
||||||
|
const cleanedContent = lines.slice(1).join("\n");
|
||||||
|
fs.writeFileSync(outputFile, cleanedContent, "utf8");
|
||||||
|
console.log("已删除 .vvp 文件的 shebang 行");
|
||||||
|
} else {
|
||||||
|
console.log(".vvp 文件无 shebang 行,跳过");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("删除 shebang 失败:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `处理 .vvp 文件失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6.6. 检查并创建 VCD 输出目录,并处理 Windows 路径问题
|
||||||
|
try {
|
||||||
|
const tbPath = projectCheck.testbenchFile;
|
||||||
|
if (tbPath && fs.existsSync(tbPath)) {
|
||||||
|
const tbContent = fs.readFileSync(tbPath, "utf8");
|
||||||
|
const dumpfileMatch = tbContent.match(/\$dumpfile\s*\(\s*["']([^"']+)["']\s*\)/);
|
||||||
|
if (dumpfileMatch) {
|
||||||
|
const vcdPath = dumpfileMatch[1];
|
||||||
|
const vcdDir = path.dirname(vcdPath);
|
||||||
|
console.log(`testbench 中的 VCD 路径: ${vcdPath}`);
|
||||||
|
|
||||||
|
if (vcdDir && vcdDir !== "." && vcdDir !== "") {
|
||||||
|
const vcdDirPath = path.join(projectPath, vcdDir);
|
||||||
|
console.log(`检查 VCD 目录: ${vcdDirPath}`);
|
||||||
|
if (!fs.existsSync(vcdDirPath)) {
|
||||||
|
fs.mkdirSync(vcdDirPath, { recursive: true });
|
||||||
|
console.log(`已创建 VCD 输出目录: ${vcdDirPath}`);
|
||||||
|
} else {
|
||||||
|
console.log(`VCD 目录已存在: ${vcdDirPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Windows 兼容性:修改 .vvp 文件中的路径,将正斜杠替换为反斜杠
|
||||||
|
if (process.platform === "win32" && vcdPath.includes("/")) {
|
||||||
|
const vvpContent = fs.readFileSync(outputFile, "utf8");
|
||||||
|
const windowsPath = vcdPath.replace(/\//g, "\\\\");
|
||||||
|
const modifiedContent = vvpContent.replace(
|
||||||
|
new RegExp(`"${vcdPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`, 'g'),
|
||||||
|
`"${windowsPath}"`
|
||||||
|
);
|
||||||
|
fs.writeFileSync(outputFile, modifiedContent, "utf8");
|
||||||
|
console.log(`已修正 VCD 路径: ${vcdPath} -> ${windowsPath}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("VCD 文件在根目录,无需创建子目录");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.warn("testbench 中未找到 $dumpfile 语句");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("处理 VCD 路径失败:", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `处理 VCD 路径失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// 7. 执行仿真生成 VCD
|
// 7. 执行仿真生成 VCD
|
||||||
const simArgs = [outputFile];
|
const simArgs = [outputFile];
|
||||||
console.log("执行仿真命令:", vvpPath, simArgs.join(" "));
|
console.log("执行仿真命令:", vvpPath, simArgs.join(" "));
|
||||||
@ -318,7 +445,11 @@ export async function generateVCD(
|
|||||||
cwd: projectPath,
|
cwd: projectPath,
|
||||||
env: env,
|
env: env,
|
||||||
});
|
});
|
||||||
|
console.log("仿真执行完成");
|
||||||
|
console.log("仿真 stdout:", simResult.stdout);
|
||||||
|
console.log("仿真 stderr:", simResult.stderr);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.error("仿真失败:", error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: `VVP 仿真失败:\n${error.message}`,
|
message: `VVP 仿真失败:\n${error.message}`,
|
||||||
@ -328,23 +459,46 @@ export async function generateVCD(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 8. 查找生成的 VCD 文件
|
// 8. 查找生成的 VCD 文件
|
||||||
const projectUri = vscode.Uri.file(projectPath);
|
let vcdFile: string | null = null;
|
||||||
const entries = await vscode.workspace.fs.readDirectory(projectUri);
|
|
||||||
const vcdFiles = entries
|
|
||||||
.filter(([fileName, fileType]) => fileType === vscode.FileType.File && fileName.endsWith('.vcd'))
|
|
||||||
.map(([fileName]) => fileName);
|
|
||||||
|
|
||||||
if (vcdFiles.length === 0) {
|
// 8.1 尝试从 testbench 中提取 VCD 路径
|
||||||
|
try {
|
||||||
|
const fs = require("fs");
|
||||||
|
const tbPath = projectCheck.testbenchFile;
|
||||||
|
if (tbPath && fs.existsSync(tbPath)) {
|
||||||
|
const tbContent = fs.readFileSync(tbPath, "utf8");
|
||||||
|
const dumpfileMatch = tbContent.match(/\$dumpfile\s*\(\s*["']([^"']+)["']\s*\)/);
|
||||||
|
if (dumpfileMatch) {
|
||||||
|
const vcdPath = dumpfileMatch[1];
|
||||||
|
const absoluteVcdPath = path.join(projectPath, vcdPath);
|
||||||
|
if (fs.existsSync(absoluteVcdPath)) {
|
||||||
|
vcdFile = absoluteVcdPath;
|
||||||
|
console.log(`找到 VCD 文件(从 testbench): ${vcdFile}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("从 testbench 提取 VCD 路径失败:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8.2 如果未找到,递归搜索项目目录
|
||||||
|
if (!vcdFile) {
|
||||||
|
const foundFiles = await findVcdFilesRecursive(projectPath);
|
||||||
|
if (foundFiles.length > 0) {
|
||||||
|
vcdFile = foundFiles[0];
|
||||||
|
console.log(`找到 VCD 文件(递归搜索): ${vcdFile}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!vcdFile) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: "VCD 文件未生成。请确保 testbench 中包含 $dumpfile 和 $dumpvars 语句。",
|
message:
|
||||||
|
"VCD 文件未生成。请确保 testbench 中包含 $dumpfile 和 $dumpvars 语句。",
|
||||||
stdout: simResult.stdout,
|
stdout: simResult.stdout,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用找到的第一个 VCD 文件
|
|
||||||
const vcdFile = path.join(projectPath, vcdFiles[0]);
|
|
||||||
|
|
||||||
// 9. 清理中间文件
|
// 9. 清理中间文件
|
||||||
try {
|
try {
|
||||||
const outputUri = vscode.Uri.file(outputFile);
|
const outputUri = vscode.Uri.file(outputFile);
|
||||||
@ -373,7 +527,7 @@ export async function generateVCD(
|
|||||||
* 检查 iverilog 是否可用
|
* 检查 iverilog 是否可用
|
||||||
*/
|
*/
|
||||||
export async function checkIverilogAvailable(
|
export async function checkIverilogAvailable(
|
||||||
extensionPath: string
|
extensionPath: string,
|
||||||
): Promise<{ available: boolean; version?: string; message: string }> {
|
): Promise<{ available: boolean; version?: string; message: string }> {
|
||||||
try {
|
try {
|
||||||
const iverilogPath = await getIverilogPath(extensionPath);
|
const iverilogPath = await getIverilogPath(extensionPath);
|
||||||
@ -385,7 +539,7 @@ export async function checkIverilogAvailable(
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
available: false,
|
available: false,
|
||||||
message: `iverilog 不可用。未找到文件: ${iverilogPath}`,
|
message: `IC Coder编译器不可用。未找到文件: ${iverilogPath}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,12 +558,215 @@ export async function checkIverilogAvailable(
|
|||||||
return {
|
return {
|
||||||
available: true,
|
available: true,
|
||||||
version: version,
|
version: version,
|
||||||
message: `iverilog 可用: ${version}`,
|
message: `IC Coder编译器可用: ${version}`,
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
available: false,
|
available: false,
|
||||||
message: `iverilog 执行失败: ${error.message}\n${error.stderr || ""}`,
|
message: `IC Coder编译器执行失败: ${error.message}\n${error.stderr || ""}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 要 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 {
|
||||||
|
// 编译(带宏定义,启用 SystemVerilog 2012 标准)
|
||||||
|
const compileArgs = [
|
||||||
|
"-g2012",
|
||||||
|
`-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 : "未知错误"}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
126
src/utils/jwtUtils.ts
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/**
|
||||||
|
* JWT 工具函数
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT Payload 接口
|
||||||
|
*/
|
||||||
|
export interface JwtPayload {
|
||||||
|
sub?: string; // subject (通常是 userId)
|
||||||
|
userId?: number; // 用户ID (驼峰命名)
|
||||||
|
user_id?: number; // 用户ID (下划线命名)
|
||||||
|
exp?: number; // 过期时间
|
||||||
|
iat?: number; // 签发时间
|
||||||
|
ispluginTrial?: boolean; // 是否是插件试用用户
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 JWT token 中获取 ispluginTrial 标识
|
||||||
|
* @param token JWT token
|
||||||
|
* @returns true=插件试用用户,false=正式用户,null=无法判断
|
||||||
|
*/
|
||||||
|
export function getIsPluginTrialFromToken(token: string): boolean | null {
|
||||||
|
const payload = parseJwtPayload(token);
|
||||||
|
if (!payload) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查 ispluginTrial 字段
|
||||||
|
if (payload.ispluginTrial !== undefined) {
|
||||||
|
console.log("[JWT] 从 token 中获取到 ispluginTrial:", payload.ispluginTrial);
|
||||||
|
return payload.ispluginTrial === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[JWT] token 中没有 ispluginTrial 字段,判定为正式用户");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
@ -18,8 +18,15 @@ import { ChatHistoryManager } from "./chatHistoryManager";
|
|||||||
import { dialogManager, DialogSession } from "../services/dialogService";
|
import { dialogManager, DialogSession } from "../services/dialogService";
|
||||||
import { userInteractionManager } from "../services/userInteraction";
|
import { userInteractionManager } from "../services/userInteraction";
|
||||||
import { healthCheck } from "../services/apiClient";
|
import { healthCheck } from "../services/apiClient";
|
||||||
|
import { isTokenExpired } from "./jwtUtils";
|
||||||
|
import { optimizePrompt } from "../services/promptOptimizeService";
|
||||||
|
import { NotificationService } from "../services/notificationService";
|
||||||
|
import { TrialExpirationService } from "../services/trialExpirationService";
|
||||||
|
import { showFileDiff } from "./fileDiff";
|
||||||
|
import { changeTracker } from "../services/changeTracker";
|
||||||
|
import { generateDiff, renderDiffHtml } from "./diffRenderer";
|
||||||
|
|
||||||
import type { RunMode } from "../types/api";
|
import type { RunMode, ServiceTier } from "../types/api";
|
||||||
|
|
||||||
/** 是否使用后端服务(可通过配置控制) */
|
/** 是否使用后端服务(可通过配置控制) */
|
||||||
let useBackendService = true;
|
let useBackendService = true;
|
||||||
@ -30,25 +37,19 @@ let currentSession: DialogSession | null = null;
|
|||||||
/** 最后一个活跃的 taskId(用于压缩等操作) */
|
/** 最后一个活跃的 taskId(用于压缩等操作) */
|
||||||
let lastTaskId: string | null = null;
|
let lastTaskId: string | null = null;
|
||||||
|
|
||||||
/** 待执行的计划(Plan 模式确认后自动执行) */
|
/** 离线模式仿真模拟标志(防止重复触发) */
|
||||||
let pendingPlanExecution: {
|
let offlineSimulationTriggered = false;
|
||||||
panel: vscode.WebviewPanel;
|
|
||||||
planTitle: string;
|
|
||||||
extensionPath: string;
|
|
||||||
taskId: string; // 保存 taskId 以便复用
|
|
||||||
} | null = null;
|
|
||||||
|
|
||||||
/**
|
async function trackFileChange(
|
||||||
* 设置待执行的计划(由 ICHelperPanel 调用)
|
filePath: string,
|
||||||
*/
|
oldContent: string,
|
||||||
export function setPendingPlanExecution(
|
newContent: string,
|
||||||
panel: vscode.WebviewPanel,
|
): Promise<void> {
|
||||||
planTitle: string,
|
try {
|
||||||
extensionPath: string,
|
changeTracker.trackChange(filePath, oldContent, newContent);
|
||||||
taskId: string
|
} catch (error) {
|
||||||
): void {
|
console.warn("[MessageHandler] 记录文件变更失败:", error);
|
||||||
pendingPlanExecution = { panel, planTitle, extensionPath, taskId };
|
}
|
||||||
console.log("[MessageHandler] 设置待执行计划:", planTitle, "taskId:", taskId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,10 +59,112 @@ export async function handleUserMessage(
|
|||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
text: string,
|
text: string,
|
||||||
extensionPath?: string,
|
extensionPath?: string,
|
||||||
mode?: RunMode
|
mode?: RunMode,
|
||||||
|
serviceTier?: ServiceTier, // 服务等级参数
|
||||||
|
contextItems?: Array<{ id: number; type: string; path: string }>, // 上下文项参数
|
||||||
) {
|
) {
|
||||||
console.log("收到用户消息:", text);
|
console.log("收到用户消息:", text);
|
||||||
|
|
||||||
|
// 【已禁用】检查 token 是否过期 - 无需登录
|
||||||
|
const context = (panel as any).__context;
|
||||||
|
if (false && 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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", {
|
||||||
|
forceReauth: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token && isTokenExpired(token as string)) {
|
||||||
|
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", {
|
||||||
|
forceReauth: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查试用期是否过期
|
||||||
|
const trialService = new TrialExpirationService(context, panel);
|
||||||
|
const isExpired = await trialService.checkExpiration();
|
||||||
|
if (isExpired) {
|
||||||
|
console.warn("[MessageHandler] 试用期已过期,阻止发送");
|
||||||
|
|
||||||
|
// 恢复输入状态
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "updateSegments",
|
||||||
|
segments: [],
|
||||||
|
isComplete: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 记录用户消息到历史(允许失败,不阻塞主流程)
|
// 记录用户消息到历史(允许失败,不阻塞主流程)
|
||||||
try {
|
try {
|
||||||
const historyManager = ChatHistoryManager.getInstance();
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
@ -90,13 +193,21 @@ export async function handleUserMessage(
|
|||||||
// 尝试使用后端服务
|
// 尝试使用后端服务
|
||||||
if (useBackendService && extensionPath) {
|
if (useBackendService && extensionPath) {
|
||||||
try {
|
try {
|
||||||
await handleUserMessageWithBackend(panel, text, extensionPath, mode);
|
await handleUserMessageWithBackend(
|
||||||
|
panel,
|
||||||
|
text,
|
||||||
|
extensionPath,
|
||||||
|
mode,
|
||||||
|
undefined,
|
||||||
|
serviceTier,
|
||||||
|
contextItems,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("后端服务不可用:", error);
|
console.error("处理用户消息失败:", error);
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "updateStatus",
|
command: "updateStatus",
|
||||||
text: "后端服务不可用",
|
text: "处理用户消息失败,请稍后重试",
|
||||||
type: "error",
|
type: "error",
|
||||||
});
|
});
|
||||||
// 恢复输入状态
|
// 恢复输入状态
|
||||||
@ -125,19 +236,39 @@ async function handleUserMessageWithBackend(
|
|||||||
text: string,
|
text: string,
|
||||||
extensionPath: string,
|
extensionPath: string,
|
||||||
mode?: RunMode,
|
mode?: RunMode,
|
||||||
reuseTaskId?: string // 可选,复用现有 taskId(用于 Plan 模式确认后继续执行)
|
reuseTaskId?: string, // 可选,复用现有 taskId(用于 Plan 模式确认后继续执行)
|
||||||
|
serviceTier?: ServiceTier, // 服务等级参数
|
||||||
|
contextItems?: Array<{ id: number; type: string; path: string }>, // 上下文项参数
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// 创建或复用会话
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
if (!currentSession || !currentSession.active) {
|
|
||||||
currentSession = dialogManager.createSession(extensionPath, reuseTaskId);
|
// 处理上下文项:在消息前附加文件/文件夹路径
|
||||||
// 保存 taskId 用于后续操作(如压缩)
|
let enhancedText = text;
|
||||||
lastTaskId = currentSession.getTaskId();
|
if (contextItems && contextItems.length > 0) {
|
||||||
if (reuseTaskId) {
|
console.log("[MessageHandler] 处理上下文项:", contextItems.length);
|
||||||
console.log("[MessageHandler] 复用 taskId 创建会话:", reuseTaskId);
|
const paths = contextItems.map((item) => item.path).join("\n");
|
||||||
}
|
enhancedText = `${paths}\n\n${text}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
// 重置离线模式仿真标志(新会话开始)
|
||||||
|
offlineSimulationTriggered = false;
|
||||||
|
console.log(
|
||||||
|
"[MessageHandler] 创建会话: taskId=",
|
||||||
|
lastTaskId,
|
||||||
|
"来源=",
|
||||||
|
taskIdToUse ? "historyManager" : "新生成",
|
||||||
|
);
|
||||||
|
|
||||||
// 显示状态栏
|
// 显示状态栏
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
@ -148,18 +279,65 @@ async function handleUserMessageWithBackend(
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
currentSession!.sendMessage(
|
currentSession!.sendMessage(
|
||||||
text,
|
enhancedText,
|
||||||
{
|
{
|
||||||
onText: (fullText, isStreaming) => {
|
onText: (fullText, isStreaming) => {
|
||||||
// 不再单独处理文本,统一通过 onSegmentUpdate 处理
|
// 不再单独处理文本,统一通过 onSegmentUpdate 处理
|
||||||
},
|
},
|
||||||
|
|
||||||
onSegmentUpdate: (segments) => {
|
onSegmentUpdate: (segments) => {
|
||||||
|
// 过滤掉包含 [调用工具:xxx] 的段落
|
||||||
|
const filteredSegments = segments.filter(seg => {
|
||||||
|
if (seg.type === 'text' && typeof seg.content === 'string') {
|
||||||
|
return !/\[调用工具:.+?\]/.test(seg.content);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
// 实时发送段落更新,按后端返回顺序展示
|
// 实时发送段落更新,按后端返回顺序展示
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "updateSegments",
|
command: "updateSegments",
|
||||||
segments: segments,
|
segments: filteredSegments,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 【离线部署模式】检测代码生成完成消息,模拟仿真流程
|
||||||
|
if (!offlineSimulationTriggered) {
|
||||||
|
const hasCompletionMessage = segments.some(seg =>
|
||||||
|
seg.type === 'text' &&
|
||||||
|
seg.content?.includes('【代码生成完成】') &&
|
||||||
|
seg.content?.includes('语法检查:已通过')
|
||||||
|
);
|
||||||
|
|
||||||
|
if (hasCompletionMessage) {
|
||||||
|
offlineSimulationTriggered = true;
|
||||||
|
console.log('[离线模式] 检测到代码生成完成,开始模拟仿真流程');
|
||||||
|
|
||||||
|
// 立即点亮 Simulation 阶段
|
||||||
|
panel.webview.postMessage({
|
||||||
|
type: "updateProgress",
|
||||||
|
step: "simulation"
|
||||||
|
});
|
||||||
|
|
||||||
|
// 随机延时 8-13 秒后完成仿真
|
||||||
|
const simulationDelay = 8000 + Math.random() * 5000;
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('[离线模式] 模拟仿真完成,进入 Done 阶段');
|
||||||
|
// Simulation 完成,进入 Done
|
||||||
|
panel.webview.postMessage({
|
||||||
|
type: "updateProgress",
|
||||||
|
step: "done"
|
||||||
|
});
|
||||||
|
|
||||||
|
// 再延时 1 秒完成所有步骤
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('[离线模式] 所有阶段完成');
|
||||||
|
panel.webview.postMessage({
|
||||||
|
type: "completeProgress"
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
}, simulationDelay);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onToolStart: (toolName) => {
|
onToolStart: (toolName) => {
|
||||||
@ -179,7 +357,10 @@ async function handleUserMessageWithBackend(
|
|||||||
// 工具错误,不需要单独处理,通过 onSegmentUpdate 统一更新
|
// 工具错误,不需要单独处理,通过 onSegmentUpdate 统一更新
|
||||||
},
|
},
|
||||||
|
|
||||||
onQuestion: (askId, question, options) => {
|
onQuestion: (
|
||||||
|
askId: string,
|
||||||
|
questions: import("../types/api").QuestionItem[],
|
||||||
|
) => {
|
||||||
// 只更新状态栏,问题显示由 onSegmentUpdate 统一处理
|
// 只更新状态栏,问题显示由 onSegmentUpdate 统一处理
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "updateStatus",
|
command: "updateStatus",
|
||||||
@ -189,26 +370,9 @@ async function handleUserMessageWithBackend(
|
|||||||
},
|
},
|
||||||
|
|
||||||
onComplete: async (segments) => {
|
onComplete: async (segments) => {
|
||||||
// 隐藏状态栏
|
|
||||||
panel.webview.postMessage({
|
|
||||||
command: "hideStatus",
|
|
||||||
});
|
|
||||||
|
|
||||||
// 最后一次发送完整的段落
|
|
||||||
console.log("[MessageHandler] 对话完成, 段落数:", segments.length);
|
console.log("[MessageHandler] 对话完成, 段落数:", segments.length);
|
||||||
console.log(
|
|
||||||
"[MessageHandler] segments 内容:",
|
|
||||||
JSON.stringify(segments)
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = await panel.webview.postMessage({
|
// 先保存到历史记录(优先级最高,确保数据不丢失)
|
||||||
command: "updateSegments",
|
|
||||||
segments: segments,
|
|
||||||
isComplete: true,
|
|
||||||
});
|
|
||||||
console.log("[MessageHandler] postMessage 返回值:", result);
|
|
||||||
|
|
||||||
// 保存完整的 segments 到历史记录
|
|
||||||
try {
|
try {
|
||||||
// 将完整的 segments 保存到一条 AI 消息中
|
// 将完整的 segments 保存到一条 AI 消息中
|
||||||
// 这样加载时可以完整还原对话样式
|
// 这样加载时可以完整还原对话样式
|
||||||
@ -218,41 +382,48 @@ async function handleUserMessageWithBackend(
|
|||||||
.join("\n");
|
.join("\n");
|
||||||
|
|
||||||
await historyManager.addAiMessage(textContent, undefined, segments);
|
await historyManager.addAiMessage(textContent, undefined, segments);
|
||||||
|
console.log("[MessageHandler] AI响应已保存到历史记录");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("保存AI响应历史失败:", error);
|
console.error("[MessageHandler] 保存AI响应历史失败:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否有待执行的计划(Plan 模式确认后自动执行)
|
// 尝试更新面板(如果面板已关闭,这些操作会失败,但不影响数据保存)
|
||||||
if (pendingPlanExecution) {
|
try {
|
||||||
const {
|
// 隐藏状态栏
|
||||||
panel: execPanel,
|
panel.webview.postMessage({
|
||||||
planTitle,
|
command: "hideStatus",
|
||||||
extensionPath: execPath,
|
});
|
||||||
taskId: reuseTaskId,
|
|
||||||
} = pendingPlanExecution;
|
// 发送完成标记(不再重复发送 segments,避免内容重复显示)
|
||||||
pendingPlanExecution = null;
|
panel.webview.postMessage({
|
||||||
console.log(
|
command: "updateSegments",
|
||||||
"[MessageHandler] 自动执行计划:",
|
segments: [],
|
||||||
planTitle,
|
isComplete: true,
|
||||||
"复用 taskId:",
|
});
|
||||||
reuseTaskId
|
|
||||||
|
// 发送任务完成消息
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "taskComplete",
|
||||||
|
});
|
||||||
|
|
||||||
|
// 发送系统通知 - AI 响应完成
|
||||||
|
const notificationService = NotificationService.getInstance();
|
||||||
|
notificationService.success(
|
||||||
|
"IC Coder - AI 响应完成",
|
||||||
|
"您的问题已得到回复,点击查看详情",
|
||||||
|
() => {
|
||||||
|
// 点击通知时聚焦到面板
|
||||||
|
panel.reveal();
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// 延迟一小段时间确保当前对话完全结束
|
// 发送代码变更到前端
|
||||||
setTimeout(async () => {
|
sendChangesToWebview(panel);
|
||||||
try {
|
} catch (error) {
|
||||||
// 复用 taskId 创建新会话,确保知识图谱数据不丢失
|
console.warn(
|
||||||
await handleUserMessageWithBackend(
|
"[MessageHandler] 更新面板失败(面板可能已关闭):",
|
||||||
execPanel,
|
error,
|
||||||
`请按照刚才的计划执行:${planTitle}`,
|
);
|
||||||
execPath,
|
|
||||||
"agent",
|
|
||||||
reuseTaskId // 复用 Plan 模式的 taskId
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error("[MessageHandler] 自动执行计划失败:", err);
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
@ -264,7 +435,7 @@ async function handleUserMessageWithBackend(
|
|||||||
});
|
});
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "receiveMessage",
|
command: "receiveMessage",
|
||||||
text: `❌ 错误: ${message}`,
|
text: `错误: ${message}`,
|
||||||
});
|
});
|
||||||
// 恢复输入状态
|
// 恢复输入状态
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
@ -288,8 +459,39 @@ async function handleUserMessageWithBackend(
|
|||||||
percentage: data.percentage,
|
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
|
mode,
|
||||||
|
serviceTier, // 传递服务等级
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -300,10 +502,11 @@ async function handleUserMessageWithBackend(
|
|||||||
export async function handleUserAnswer(
|
export async function handleUserAnswer(
|
||||||
askId: string,
|
askId: string,
|
||||||
selected?: string[],
|
selected?: string[],
|
||||||
customInput?: string
|
customInput?: string,
|
||||||
|
answers?: { [questionIndex: string]: string[] },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (currentSession) {
|
if (currentSession) {
|
||||||
await currentSession.submitAnswer(askId, selected, customInput);
|
await currentSession.submitAnswer(askId, selected, customInput, answers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,9 +572,17 @@ export async function handlePlanAction(
|
|||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
action: string,
|
action: string,
|
||||||
planTitle: string,
|
planTitle: string,
|
||||||
extensionPath: string
|
extensionPath: string,
|
||||||
|
serviceTier?: ServiceTier,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log("[handlePlanAction] action:", action, "planTitle:", planTitle);
|
console.log(
|
||||||
|
"[handlePlanAction] action:",
|
||||||
|
action,
|
||||||
|
"planTitle:",
|
||||||
|
planTitle,
|
||||||
|
"serviceTier:",
|
||||||
|
serviceTier,
|
||||||
|
);
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "confirm":
|
case "confirm":
|
||||||
@ -385,7 +596,8 @@ export async function handlePlanAction(
|
|||||||
panel,
|
panel,
|
||||||
`请按照刚才的计划执行:${planTitle}`,
|
`请按照刚才的计划执行:${planTitle}`,
|
||||||
extensionPath,
|
extensionPath,
|
||||||
"agent"
|
"agent",
|
||||||
|
serviceTier,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -401,7 +613,8 @@ export async function handlePlanAction(
|
|||||||
panel,
|
panel,
|
||||||
`请根据以下建议修改计划:${modification}`,
|
`请根据以下建议修改计划:${modification}`,
|
||||||
extensionPath,
|
extensionPath,
|
||||||
"plan"
|
"plan",
|
||||||
|
serviceTier,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -456,7 +669,7 @@ function parseFileOperation(text: string): {
|
|||||||
|
|
||||||
// 匹配重命名文件:将 xxx.ts 重命名为 yyy.ts 或 把 xxx.ts 改名为 yyy.ts(优先匹配,避免被修改匹配)
|
// 匹配重命名文件:将 xxx.ts 重命名为 yyy.ts 或 把 xxx.ts 改名为 yyy.ts(优先匹配,避免被修改匹配)
|
||||||
const renameMatch = lowerText.match(
|
const renameMatch = lowerText.match(
|
||||||
/(?:将|把)\s*(.+?\.\w+)\s*(?:重命名|改名)\s*(?:为|成)\s*(.+?\.\w+)/
|
/(?:将|把)\s*(.+?\.\w+)\s*(?:重命名|改名)\s*(?:为|成)\s*(.+?\.\w+)/,
|
||||||
);
|
);
|
||||||
if (renameMatch) {
|
if (renameMatch) {
|
||||||
const oldPath = renameMatch[1].trim();
|
const oldPath = renameMatch[1].trim();
|
||||||
@ -473,7 +686,7 @@ function parseFileOperation(text: string): {
|
|||||||
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
||||||
// 格式3: 将 xxx.ts 文件 'aaa' 替换为 'bbb'
|
// 格式3: 将 xxx.ts 文件 'aaa' 替换为 'bbb'
|
||||||
const replaceMatch1 = lowerText.match(
|
const replaceMatch1 = lowerText.match(
|
||||||
/在\s*(.+?\.\w+)\s*(?:文件)?中?\s*(?:将|把)\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/
|
/在\s*(.+?\.\w+)\s*(?:文件)?中?\s*(?:将|把)\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/,
|
||||||
);
|
);
|
||||||
if (replaceMatch1) {
|
if (replaceMatch1) {
|
||||||
const filePath = replaceMatch1[1].trim();
|
const filePath = replaceMatch1[1].trim();
|
||||||
@ -489,7 +702,7 @@ function parseFileOperation(text: string): {
|
|||||||
|
|
||||||
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
// 格式2: 将 xxx.ts 文件 "aaa" 替换为 "bbb"
|
||||||
const replaceMatch2 = lowerText.match(
|
const replaceMatch2 = lowerText.match(
|
||||||
/(?:将|把)\s*(.+?\.\w+)\s*(?:文件)?\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/
|
/(?:将|把)\s*(.+?\.\w+)\s*(?:文件)?\s*["'](.+?)["']\s*替换\s*(?:为|成)\s*["'](.+?)["']/,
|
||||||
);
|
);
|
||||||
if (replaceMatch2) {
|
if (replaceMatch2) {
|
||||||
const filePath = replaceMatch2[1].trim();
|
const filePath = replaceMatch2[1].trim();
|
||||||
@ -538,7 +751,7 @@ async function handleFileOperation(
|
|||||||
newPath?: string;
|
newPath?: string;
|
||||||
searchText?: string;
|
searchText?: string;
|
||||||
replaceText?: string;
|
replaceText?: string;
|
||||||
}
|
},
|
||||||
) {
|
) {
|
||||||
const historyManager = ChatHistoryManager.getInstance();
|
const historyManager = ChatHistoryManager.getInstance();
|
||||||
|
|
||||||
@ -554,7 +767,7 @@ async function handleFileOperation(
|
|||||||
text: responseText,
|
text: responseText,
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(
|
||||||
`文件创建成功: ${operation.filePath}`
|
`文件创建成功: ${operation.filePath}`,
|
||||||
);
|
);
|
||||||
await historyManager.addAiMessage(responseText);
|
await historyManager.addAiMessage(responseText);
|
||||||
break;
|
break;
|
||||||
@ -567,7 +780,7 @@ async function handleFileOperation(
|
|||||||
text: responseText,
|
text: responseText,
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(
|
||||||
`文件删除成功: ${operation.filePath}`
|
`文件删除成功: ${operation.filePath}`,
|
||||||
);
|
);
|
||||||
await historyManager.addAiMessage(responseText);
|
await historyManager.addAiMessage(responseText);
|
||||||
break;
|
break;
|
||||||
@ -603,7 +816,7 @@ async function handleFileOperation(
|
|||||||
text: responseText,
|
text: responseText,
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(
|
||||||
`文件重命名成功: ${operation.filePath} → ${operation.newPath}`
|
`文件重命名成功: ${operation.filePath} → ${operation.newPath}`,
|
||||||
);
|
);
|
||||||
await historyManager.addAiMessage(responseText);
|
await historyManager.addAiMessage(responseText);
|
||||||
break;
|
break;
|
||||||
@ -612,10 +825,21 @@ async function handleFileOperation(
|
|||||||
if (!operation.searchText || !operation.replaceText) {
|
if (!operation.searchText || !operation.replaceText) {
|
||||||
throw new Error("缺少替换内容");
|
throw new Error("缺少替换内容");
|
||||||
}
|
}
|
||||||
|
const oldContentBeforeReplace = await readFileContent(
|
||||||
|
operation.filePath,
|
||||||
|
);
|
||||||
await replaceFile(
|
await replaceFile(
|
||||||
operation.filePath,
|
operation.filePath,
|
||||||
operation.searchText,
|
operation.searchText,
|
||||||
operation.replaceText
|
operation.replaceText,
|
||||||
|
);
|
||||||
|
const newContentAfterReplace = await readFileContent(
|
||||||
|
operation.filePath,
|
||||||
|
);
|
||||||
|
await trackFileChange(
|
||||||
|
operation.filePath,
|
||||||
|
oldContentBeforeReplace,
|
||||||
|
newContentAfterReplace,
|
||||||
);
|
);
|
||||||
responseText = `✅ 文件内容替换成功: ${operation.filePath}`;
|
responseText = `✅ 文件内容替换成功: ${operation.filePath}`;
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
@ -623,7 +847,7 @@ async function handleFileOperation(
|
|||||||
text: responseText,
|
text: responseText,
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(
|
||||||
`文件内容替换成功: ${operation.filePath}`
|
`文件内容替换成功: ${operation.filePath}`,
|
||||||
);
|
);
|
||||||
await historyManager.addAiMessage(responseText);
|
await historyManager.addAiMessage(responseText);
|
||||||
break;
|
break;
|
||||||
@ -683,7 +907,7 @@ function getDefaultContent(filePath: string): string {
|
|||||||
*/
|
*/
|
||||||
export async function handleReadFile(
|
export async function handleReadFile(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
filePath: string
|
filePath: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const content = await readFileContent(filePath);
|
const content = await readFileContent(filePath);
|
||||||
@ -707,7 +931,7 @@ export async function handleCreateFile(
|
|||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
filePath: string,
|
filePath: string,
|
||||||
content: string,
|
content: string,
|
||||||
overwrite: boolean = false //是否覆盖
|
overwrite: boolean = false, //是否覆盖
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
if (overwrite) {
|
if (overwrite) {
|
||||||
@ -722,13 +946,26 @@ 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",
|
||||||
error: error instanceof Error ? error.message : "创建文件失败",
|
error: error instanceof Error ? error.message : "创建文件失败",
|
||||||
});
|
});
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
`创建文件失败: ${error instanceof Error ? error.message : "未知错误"}`
|
`创建文件失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -739,23 +976,32 @@ export async function handleCreateFile(
|
|||||||
export async function handleUpdateFile(
|
export async function handleUpdateFile(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
filePath: string,
|
filePath: string,
|
||||||
content: string
|
content: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
const oldContent = await readFileContent(filePath);
|
||||||
await updateFile(filePath, content);
|
await updateFile(filePath, content);
|
||||||
|
await trackFileChange(filePath, oldContent, content);
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "fileUpdated",
|
command: "fileUpdated",
|
||||||
filePath: filePath,
|
filePath: filePath,
|
||||||
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",
|
||||||
error: error instanceof Error ? error.message : "更新文件失败",
|
error: error instanceof Error ? error.message : "更新文件失败",
|
||||||
});
|
});
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
`更新文件失败: ${error instanceof Error ? error.message : "未知错误"}`
|
`更新文件失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -766,7 +1012,7 @@ export async function handleUpdateFile(
|
|||||||
export async function handleRenameFile(
|
export async function handleRenameFile(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
oldPath: string,
|
oldPath: string,
|
||||||
newPath: string
|
newPath: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await renameFile(oldPath, newPath);
|
await renameFile(oldPath, newPath);
|
||||||
@ -777,7 +1023,7 @@ export async function handleRenameFile(
|
|||||||
message: "文件重命名成功",
|
message: "文件重命名成功",
|
||||||
});
|
});
|
||||||
vscode.window.showInformationMessage(
|
vscode.window.showInformationMessage(
|
||||||
`文件重命名成功: ${oldPath} → ${newPath}`
|
`文件重命名成功: ${oldPath} → ${newPath}`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
@ -785,7 +1031,7 @@ export async function handleRenameFile(
|
|||||||
error: error instanceof Error ? error.message : "重命名文件失败",
|
error: error instanceof Error ? error.message : "重命名文件失败",
|
||||||
});
|
});
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
`重命名文件失败: ${error instanceof Error ? error.message : "未知错误"}`
|
`重命名文件失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -797,10 +1043,13 @@ export async function handleReplaceInFile(
|
|||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
filePath: string,
|
filePath: string,
|
||||||
searchText: string,
|
searchText: string,
|
||||||
replaceText: string
|
replaceText: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
const oldContent = await readFileContent(filePath);
|
||||||
await replaceFile(filePath, searchText, replaceText);
|
await replaceFile(filePath, searchText, replaceText);
|
||||||
|
const newContent = await readFileContent(filePath);
|
||||||
|
await trackFileChange(filePath, oldContent, newContent);
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "fileReplaced",
|
command: "fileReplaced",
|
||||||
filePath: filePath,
|
filePath: filePath,
|
||||||
@ -813,7 +1062,7 @@ export async function handleReplaceInFile(
|
|||||||
error: error instanceof Error ? error.message : "替换文件内容失败",
|
error: error instanceof Error ? error.message : "替换文件内容失败",
|
||||||
});
|
});
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
`替换文件内容失败: ${error instanceof Error ? error.message : "未知错误"}`
|
`替换文件内容失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -858,7 +1107,7 @@ function isVCDGenerationCommand(text: string): boolean {
|
|||||||
*/
|
*/
|
||||||
async function handleVCDGeneration(
|
async function handleVCDGeneration(
|
||||||
panel: vscode.WebviewPanel,
|
panel: vscode.WebviewPanel,
|
||||||
extensionPath: string
|
extensionPath: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// 获取当前工作区路径
|
// 获取当前工作区路径
|
||||||
@ -885,7 +1134,7 @@ async function handleVCDGeneration(
|
|||||||
if (!iverilogCheck.available) {
|
if (!iverilogCheck.available) {
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: "receiveMessage",
|
command: "receiveMessage",
|
||||||
text: `❌ ${iverilogCheck.message}\n\n请参考插件文档安装 iverilog 工具。`,
|
text: `❌ ${iverilogCheck.message}。`,
|
||||||
});
|
});
|
||||||
vscode.window.showErrorMessage(iverilogCheck.message);
|
vscode.window.showErrorMessage(iverilogCheck.message);
|
||||||
return;
|
return;
|
||||||
@ -956,6 +1205,20 @@ async function handleVCDGeneration(
|
|||||||
});
|
});
|
||||||
|
|
||||||
vscode.window.showInformationMessage(`VCD 文件生成成功: ${fileName}`);
|
vscode.window.showInformationMessage(`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({
|
||||||
command: "receiveMessage",
|
command: "receiveMessage",
|
||||||
@ -979,6 +1242,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 文件时出错: ${
|
||||||
@ -991,5 +1265,214 @@ 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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理采纳变更
|
||||||
|
*/
|
||||||
|
export async function handleAcceptChange(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
changeId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const success = await changeTracker.acceptChange(changeId);
|
||||||
|
if (success) {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "changeAccepted",
|
||||||
|
changeId: changeId,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "changeAccepted",
|
||||||
|
changeId: changeId,
|
||||||
|
success: false,
|
||||||
|
error: "采纳变更失败",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[MessageHandler] 采纳变更失败:", error);
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "changeAccepted",
|
||||||
|
changeId: changeId,
|
||||||
|
success: false,
|
||||||
|
error: String(error),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理拒绝变更
|
||||||
|
*/
|
||||||
|
export async function handleRejectChange(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
changeId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const success = await changeTracker.rejectChange(changeId);
|
||||||
|
if (success) {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "changeRejected",
|
||||||
|
changeId: changeId,
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "changeRejected",
|
||||||
|
changeId: changeId,
|
||||||
|
success: false,
|
||||||
|
error: "拒绝变更失败",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[MessageHandler] 拒绝变更失败:", error);
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "changeRejected",
|
||||||
|
changeId: changeId,
|
||||||
|
success: false,
|
||||||
|
error: String(error),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在对话结束时发送变更列表到前端
|
||||||
|
*/
|
||||||
|
export function sendChangesToWebview(panel: vscode.WebviewPanel) {
|
||||||
|
const session = changeTracker.endSession();
|
||||||
|
if (session && session.changes.length > 0) {
|
||||||
|
const changesWithDiff = session.changes.map((change) => {
|
||||||
|
const diffLines = generateDiff(change.oldContent, change.newContent);
|
||||||
|
const diffHtml = renderDiffHtml(diffLines);
|
||||||
|
return {
|
||||||
|
...change,
|
||||||
|
diffHtml,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
panel.webview.postMessage({
|
||||||
|
command: "showChanges",
|
||||||
|
changes: changesWithDiff,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始新的变更会话
|
||||||
|
*/
|
||||||
|
export function startChangeSession(sessionId: string) {
|
||||||
|
changeTracker.startSession(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开文件 diff 编辑器
|
||||||
|
*/
|
||||||
|
export async function handleOpenFileDiff(
|
||||||
|
panel: vscode.WebviewPanel,
|
||||||
|
changeId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const session = changeTracker.getCurrentSession();
|
||||||
|
if (!session) {
|
||||||
|
vscode.window.showErrorMessage("没有找到变更会话");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const change = session.changes.find((c) => c.changeId === changeId);
|
||||||
|
if (!change) {
|
||||||
|
vscode.window.showErrorMessage("没有找到该变更");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
|
||||||
|
if (!workspaceFolder) {
|
||||||
|
vscode.window.showErrorMessage("没有打开的工作区");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建临时文件用于对比
|
||||||
|
const filePath = change.filePath;
|
||||||
|
const absolutePath = vscode.Uri.file(
|
||||||
|
path.join(workspaceFolder.uri.fsPath, filePath),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 创建虚拟文档显示旧内容
|
||||||
|
const oldUri = vscode.Uri.parse(
|
||||||
|
`ic-coder-diff:${filePath}.old?${changeId}`,
|
||||||
|
).with({ scheme: "ic-coder-diff" });
|
||||||
|
|
||||||
|
// 注册文档内容提供者(如果还没注册)
|
||||||
|
if (!(global as any).__diffProviderRegistered) {
|
||||||
|
const provider = new (class
|
||||||
|
implements vscode.TextDocumentContentProvider
|
||||||
|
{
|
||||||
|
provideTextDocumentContent(uri: vscode.Uri): string {
|
||||||
|
const changeId = uri.query;
|
||||||
|
const session = changeTracker.getCurrentSession();
|
||||||
|
const change = session?.changes.find((c) => c.changeId === changeId);
|
||||||
|
return change?.oldContent || "";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
vscode.workspace.registerTextDocumentContentProvider(
|
||||||
|
"ic-coder-diff",
|
||||||
|
provider,
|
||||||
|
);
|
||||||
|
(global as any).__diffProviderRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打开 diff 编辑器
|
||||||
|
await vscode.commands.executeCommand(
|
||||||
|
"vscode.diff",
|
||||||
|
oldUri,
|
||||||
|
absolutePath,
|
||||||
|
`${filePath} (变更对比)`,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[MessageHandler] 打开 diff 失败:", error);
|
||||||
|
vscode.window.showErrorMessage(`打开 diff 失败: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -129,6 +129,62 @@ export async function readDirectory(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 列出目录下的文件和文件夹(不读取内容,仅返回路径)
|
||||||
|
*/
|
||||||
|
export async function listDirectory(
|
||||||
|
dirPath: string,
|
||||||
|
extensions?: string[]
|
||||||
|
): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
// 如果是相对路径,转换为绝对路径
|
||||||
|
let absolutePath = dirPath;
|
||||||
|
if (!path.isAbsolute(dirPath)) {
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||||
|
absolutePath = path.join(workspaceFolders[0].uri.fsPath, dirPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dirUri = vscode.Uri.file(absolutePath);
|
||||||
|
|
||||||
|
// 检查目录是否存在
|
||||||
|
try {
|
||||||
|
const stat = await vscode.workspace.fs.stat(dirUri);
|
||||||
|
if (stat.type !== vscode.FileType.Directory) {
|
||||||
|
throw new Error(`路径不是目录: ${absolutePath}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`目录不存在: ${absolutePath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取目录内容
|
||||||
|
const entries = await vscode.workspace.fs.readDirectory(dirUri);
|
||||||
|
const results: string[] = [];
|
||||||
|
|
||||||
|
for (const [fileName, fileType] of entries) {
|
||||||
|
if (fileType === vscode.FileType.Directory) {
|
||||||
|
results.push(fileName + '/');
|
||||||
|
} else if (fileType === vscode.FileType.File) {
|
||||||
|
// 扩展名过滤
|
||||||
|
if (extensions && extensions.length > 0) {
|
||||||
|
const ext = path.extname(fileName);
|
||||||
|
// 规范化扩展名(支持 "v" 和 ".v" 两种格式)
|
||||||
|
const normalizedExts = extensions.map(e => e.startsWith('.') ? e : '.' + e);
|
||||||
|
if (!normalizedExts.includes(ext)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
results.push(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件信息
|
* 获取文件信息
|
||||||
*/
|
*/
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||