dsh-stm32 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +567 -516
- package/README_EN.md +544 -510
- package/client/client.js +7033 -682
- package/docs/reading-guide.md +921 -859
- package/lib/client.js +7033 -682
- package/lib/commands/register.js +2 -0
- package/lib/data/firmware-packages.js +35 -0
- package/lib/data/firmware-pins.js +7 -0
- package/lib/data/install-guides.js +110 -0
- package/lib/data/ioc-presets.js +118 -0
- package/lib/engine/artifacts.js +123 -0
- package/lib/engine/breakpoint-hits.js +73 -0
- package/lib/engine/build.js +161 -5
- package/lib/engine/channel-tap.js +227 -0
- package/lib/engine/channels.js +48 -0
- package/lib/engine/clocks.js +192 -0
- package/lib/engine/datasheet/auto.js +74 -0
- package/lib/engine/datasheet/digest.js +74 -0
- package/lib/engine/datasheet/lcsc.js +60 -0
- package/lib/engine/datasheet/pinout.js +119 -0
- package/lib/engine/datasheet/price.js +152 -0
- package/lib/engine/datasheet/search.js +147 -0
- package/lib/engine/datasheet/store.js +371 -0
- package/lib/engine/debug-session.js +48 -2
- package/lib/engine/ecad/autobuild.js +156 -0
- package/lib/engine/ecad/bom.js +238 -0
- package/lib/engine/ecad/brief.js +205 -0
- package/lib/engine/ecad/checks.js +239 -0
- package/lib/engine/ecad/design.js +177 -0
- package/lib/engine/ecad/dfm.js +314 -0
- package/lib/engine/ecad/easyeda.js +454 -0
- package/lib/engine/ecad/fabrication.js +104 -0
- package/lib/engine/ecad/gen/footprint.js +159 -0
- package/lib/engine/ecad/gen/symbol.js +103 -0
- package/lib/engine/ecad/import.js +372 -0
- package/lib/engine/ecad/jlcpcb.js +141 -0
- package/lib/engine/ecad/libs.js +316 -0
- package/lib/engine/ecad/models3d.js +89 -0
- package/lib/engine/ecad/netgate.js +286 -0
- package/lib/engine/ecad/normalize.js +182 -0
- package/lib/engine/ecad/package.js +126 -0
- package/lib/engine/ecad/padmerge.js +128 -0
- package/lib/engine/ecad/padprecheck.js +123 -0
- package/lib/engine/ecad/part-import.js +257 -0
- package/lib/engine/ecad/pcb/apply.js +53 -0
- package/lib/engine/ecad/pcb/emc-deep.js +396 -0
- package/lib/engine/ecad/pcb/emc.js +275 -0
- package/lib/engine/ecad/pcb/geometry.js +63 -0
- package/lib/engine/ecad/pcb/gerber.js +499 -0
- package/lib/engine/ecad/pcb/hand-sync.js +242 -0
- package/lib/engine/ecad/pcb/impedance.js +119 -0
- package/lib/engine/ecad/pcb/ipc-open.js +78 -0
- package/lib/engine/ecad/pcb/ipc-script.js +276 -0
- package/lib/engine/ecad/pcb/ipc.js +129 -0
- package/lib/engine/ecad/pcb/krt.js +182 -0
- package/lib/engine/ecad/pcb/links.js +393 -0
- package/lib/engine/ecad/pcb/mech.js +201 -0
- package/lib/engine/ecad/pcb/model-fix.js +178 -0
- package/lib/engine/ecad/pcb/netlist.js +146 -0
- package/lib/engine/ecad/pcb/pcb-svg.js +199 -0
- package/lib/engine/ecad/pcb/placement.js +140 -0
- package/lib/engine/ecad/pcb/power.js +203 -0
- package/lib/engine/ecad/pcb/render3d.js +208 -0
- package/lib/engine/ecad/pcb/route.js +167 -0
- package/lib/engine/ecad/pcb/router.js +469 -0
- package/lib/engine/ecad/pcb/rules.js +122 -0
- package/lib/engine/ecad/pcb/scene.js +513 -0
- package/lib/engine/ecad/pcb/stackup.js +154 -0
- package/lib/engine/ecad/pcb/swap.js +222 -0
- package/lib/engine/ecad/pcb/tracks.js +70 -0
- package/lib/engine/ecad/runtime.js +227 -0
- package/lib/engine/ecad/toolchain.js +146 -0
- package/lib/engine/ecad/tsci.js +362 -0
- package/lib/engine/ecad/zip.js +98 -0
- package/lib/engine/event-bus.js +158 -0
- package/lib/engine/event-log.js +335 -0
- package/lib/engine/fault-hints.js +146 -0
- package/lib/engine/generate-script.js +4 -1
- package/lib/engine/generate.js +145 -5
- package/lib/engine/interrupts.js +247 -0
- package/lib/engine/ioc-diff.js +369 -0
- package/lib/engine/ioc-plan.js +12 -2
- package/lib/engine/ioc-sync.js +58 -0
- package/lib/engine/mcu-info.js +134 -0
- package/lib/engine/msys2-install.js +31 -6
- package/lib/engine/panel-actions.js +103 -0
- package/lib/engine/pdf/curve.js +321 -0
- package/lib/engine/pdf/png.js +83 -0
- package/lib/engine/pdf/reader.js +336 -0
- package/lib/engine/pdf/schematic.js +93 -0
- package/lib/engine/pdf/tables.js +249 -0
- package/lib/engine/pdf/toolchain.js +50 -0
- package/lib/engine/pinout.js +140 -0
- package/lib/engine/probe-info.js +95 -0
- package/lib/engine/probe.js +45 -0
- package/lib/engine/progress.js +99 -0
- package/lib/engine/project-conf.js +254 -0
- package/lib/engine/project-layout.js +198 -0
- package/lib/engine/project-repair.js +205 -0
- package/lib/engine/schematic/checks.js +153 -0
- package/lib/engine/schematic/draw.js +390 -0
- package/lib/engine/schematic/edit.js +229 -0
- package/lib/engine/schematic/kicad-sch.js +225 -0
- package/lib/engine/schematic/model.js +442 -0
- package/lib/engine/schematic/nets.js +219 -0
- package/lib/engine/schematic/pcb.js +144 -0
- package/lib/engine/schematic/review.js +134 -0
- package/lib/engine/schematic/sexpr.js +134 -0
- package/lib/engine/schematic/to-ioc.js +197 -0
- package/lib/engine/schematic/transform.js +25 -0
- package/lib/engine/session-context.js +106 -0
- package/lib/engine/source-slice.js +59 -0
- package/lib/engine/tool-events.js +119 -0
- package/lib/engine/toolchain-gate.js +8 -0
- package/lib/engine/watch-admin.js +101 -0
- package/lib/engine/watch-record.js +39 -0
- package/lib/engine/watch-recorder.js +116 -0
- package/lib/engine/web-api.js +1123 -4
- package/lib/engine/web-routes.js +18 -1
- package/lib/engine/workspace-memo.js +41 -0
- package/lib/index.js +118 -24
- package/lib/skill/stm32-workflow.js +1 -1
- package/lib/tools/build.js +10 -2
- package/lib/tools/code-patch.js +2 -2
- package/lib/tools/datasheet.js +447 -0
- package/lib/tools/debug-session.js +9 -4
- package/lib/tools/debug.js +2 -2
- package/lib/tools/doctor.js +2 -2
- package/lib/tools/ecad.js +903 -0
- package/lib/tools/flash.js +2 -2
- package/lib/tools/gen.js +166 -0
- package/lib/tools/generate.js +7 -5
- package/lib/tools/import-circuit.js +146 -0
- package/lib/tools/ioc-apply.js +11 -2
- package/lib/tools/ioc-inspect.js +2 -2
- package/lib/tools/ioc-plan.js +2 -2
- package/lib/tools/ioc-verify.js +2 -2
- package/lib/tools/layout.js +92 -0
- package/lib/tools/mcu-info.js +163 -0
- package/lib/tools/part-import.js +182 -0
- package/lib/tools/pcb.js +2316 -0
- package/lib/tools/pdf.js +423 -0
- package/lib/tools/project-create.js +28 -0
- package/lib/tools/project-dir.js +25 -1
- package/lib/tools/regs.js +2 -2
- package/lib/tools/route-plan.js +2 -2
- package/lib/tools/rtt.js +10 -2
- package/lib/tools/sch-draw.js +433 -0
- package/lib/tools/sch-edit.js +145 -0
- package/lib/tools/schematic.js +817 -0
- package/lib/tools/serial.js +22 -0
- package/lib/tools/watch.js +24 -2
- package/package.json +1 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话身份与 agent 的对照表(S84 引入,S91 改为按会话索引)。
|
|
3
|
+
*
|
|
4
|
+
* Web API 没有 agent 对象,宿主的 `workingDirectory.ensure()` 因此给不出会话目录。
|
|
5
|
+
* 工具调用与会话事件里一定有 agent,这里按会话 id 记下来,Web API 拿请求里的
|
|
6
|
+
* `session` 去查,只解析这一条会话自己的工作目录。
|
|
7
|
+
*
|
|
8
|
+
* 只记最近的若干条,不做生命周期管理:会话结束后条目留在表里也无害,
|
|
9
|
+
* 缺省容量足够覆盖同时打开的几个会话。
|
|
10
|
+
*/
|
|
11
|
+
/** 最多记住多少条会话;超出时丢掉最早的一条。 */
|
|
12
|
+
const SESSION_LIMIT = 64;
|
|
13
|
+
/** 会话 id → 该会话最近一次带来的 agent。 */
|
|
14
|
+
const agents = new Map();
|
|
15
|
+
/** 会话 id → 该会话的工作目录。agent 一创建就向宿主换一次,面板首次打开即可用。 */
|
|
16
|
+
const dirs = new Map();
|
|
17
|
+
/** 没有会话 id 时记在空键上,仅供工具侧解析(Web API 不读它,避免跨会话串用)。 */
|
|
18
|
+
let lastAgent = undefined;
|
|
19
|
+
/** 取 agent 自带的 id(宿主会话 id 与工具的 `exec.agent.id` 未必同形,两者都记)。 */
|
|
20
|
+
export function agentIdOf(agent) {
|
|
21
|
+
if (agent === null || typeof agent !== "object")
|
|
22
|
+
return "";
|
|
23
|
+
const id = agent["id"];
|
|
24
|
+
return typeof id === "string" ? id.trim() : "";
|
|
25
|
+
}
|
|
26
|
+
function remember(key, agent) {
|
|
27
|
+
const trimmed = key.trim();
|
|
28
|
+
if (trimmed === "")
|
|
29
|
+
return;
|
|
30
|
+
// 宿主与工具层对同一会话的写法不统一:可能带 `session-` 前缀,也可能是裸 UUID。
|
|
31
|
+
// 三种写法都登记,任何一方查都命中;条目仍在容量上限内,不跨会话。
|
|
32
|
+
const bare = trimmed.startsWith("session-") ? trimmed.slice("session-".length) : trimmed;
|
|
33
|
+
const names = new Set([trimmed, bare, "session-" + bare]);
|
|
34
|
+
for (const name of names) {
|
|
35
|
+
if (name === "")
|
|
36
|
+
continue;
|
|
37
|
+
agents.set(name, agent);
|
|
38
|
+
}
|
|
39
|
+
while (agents.size > SESSION_LIMIT) {
|
|
40
|
+
const oldest = agents.keys().next();
|
|
41
|
+
if (oldest.done === true)
|
|
42
|
+
return;
|
|
43
|
+
agents.delete(oldest.value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 记一条会话的 agent;会话 id 为空时只更新兜底引用。
|
|
48
|
+
*
|
|
49
|
+
* 同时按 agent 自己的 id 记一份:调试台的 `conversation.view` 注入的会话 id 与工具层
|
|
50
|
+
* 的 `exec.agent.id` 是同一个值,但 session/event 的 session 上不一定带 header.id。
|
|
51
|
+
* 只记 sessionId 会让 Web API 永远查不到,面板只能显示「没有可用的会话工作目录」。
|
|
52
|
+
*/
|
|
53
|
+
export function noteSessionAgent(sessionId, agent) {
|
|
54
|
+
if (agent === undefined || agent === null)
|
|
55
|
+
return;
|
|
56
|
+
lastAgent = agent;
|
|
57
|
+
remember(sessionId.trim(), agent);
|
|
58
|
+
remember(agentIdOf(agent), agent);
|
|
59
|
+
}
|
|
60
|
+
/** 按会话 id 取 agent;没有记录返回 undefined。 */
|
|
61
|
+
export function agentOfSession(sessionId) {
|
|
62
|
+
const key = sessionId.trim();
|
|
63
|
+
if (key === "")
|
|
64
|
+
return undefined;
|
|
65
|
+
return agents.get(key);
|
|
66
|
+
}
|
|
67
|
+
/** 最近一次工具调用带来的 agent;工具侧解析工程目录时使用。 */
|
|
68
|
+
export function lastSessionAgent() {
|
|
69
|
+
return lastAgent;
|
|
70
|
+
}
|
|
71
|
+
/** 清空对照表(测试用)。 */
|
|
72
|
+
/**
|
|
73
|
+
* 记一条会话的工作目录。
|
|
74
|
+
*
|
|
75
|
+
* 与 `noteSessionAgent` 用同一套写法归一:宿主给的是 `session-<uuid>` 还是裸 UUID 都登记。
|
|
76
|
+
* 目的:面板第一次打开时不必先调用一次 STM32 工具,`/projects` 就能给出默认工程。
|
|
77
|
+
*/
|
|
78
|
+
export function noteSessionDir(sessionId, dir) {
|
|
79
|
+
const trimmed = sessionId.trim();
|
|
80
|
+
if (trimmed === "" || dir.trim() === "")
|
|
81
|
+
return;
|
|
82
|
+
const bare = trimmed.startsWith("session-") ? trimmed.slice("session-".length) : trimmed;
|
|
83
|
+
for (const name of new Set([trimmed, bare, "session-" + bare])) {
|
|
84
|
+
if (name === "")
|
|
85
|
+
continue;
|
|
86
|
+
dirs.set(name, dir);
|
|
87
|
+
}
|
|
88
|
+
while (dirs.size > SESSION_LIMIT) {
|
|
89
|
+
const oldest = dirs.keys().next();
|
|
90
|
+
if (oldest.done === true)
|
|
91
|
+
return;
|
|
92
|
+
dirs.delete(oldest.value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** 按会话 id 取已记下的工作目录;没有记录返回空串。 */
|
|
96
|
+
export function sessionDirOf(sessionId) {
|
|
97
|
+
const trimmed = sessionId.trim();
|
|
98
|
+
if (trimmed === "")
|
|
99
|
+
return "";
|
|
100
|
+
return dirs.get(trimmed) ?? "";
|
|
101
|
+
}
|
|
102
|
+
export function resetSessionAgents() {
|
|
103
|
+
agents.clear();
|
|
104
|
+
dirs.clear();
|
|
105
|
+
lastAgent = undefined;
|
|
106
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 源码片段:给断点命中、编译错误这类「文件:行」补上上下文。
|
|
3
|
+
*
|
|
4
|
+
* 只读,且只读工程目录内的文件:路径先解析成绝对路径,再要求它落在 projectDir 之下,
|
|
5
|
+
* 防止面板把 `../` 或绝对路径当参数读走别处的文件。
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { resolve, sep } from "node:path";
|
|
9
|
+
/** 一次最多带多少行上下文;面板只显示几行,给太多也没人读。 */
|
|
10
|
+
export const SOURCE_CONTEXT_MAX = 20;
|
|
11
|
+
/**
|
|
12
|
+
* 把「文件:行」里的文件参数解析成工程内的绝对路径。
|
|
13
|
+
*
|
|
14
|
+
* 接受相对工程的路径(`Core/Src/main.c`)、带反斜杠的 Windows 路径;
|
|
15
|
+
* 绝对路径只有在工程目录内才放行。越界一律 null。
|
|
16
|
+
*/
|
|
17
|
+
export function resolveSourceFile(projectDir, file) {
|
|
18
|
+
const dir = projectDir.trim();
|
|
19
|
+
const wanted = file.trim();
|
|
20
|
+
if (dir === "" || wanted === "")
|
|
21
|
+
return null;
|
|
22
|
+
const base = resolve(dir);
|
|
23
|
+
const target = resolve(base, wanted);
|
|
24
|
+
if (target === base)
|
|
25
|
+
return null;
|
|
26
|
+
if (!target.startsWith(base + sep))
|
|
27
|
+
return null;
|
|
28
|
+
return target;
|
|
29
|
+
}
|
|
30
|
+
/** 读文件的一行及其上下文;行号越界时夹到首/尾,不报错。 */
|
|
31
|
+
export function readSourceSlice(projectDir, file, line, context = 3) {
|
|
32
|
+
if (typeof line !== "number" || !Number.isFinite(line) || line < 1) {
|
|
33
|
+
return { ok: false, error: "line 必须是 ≥1 的整数" };
|
|
34
|
+
}
|
|
35
|
+
const target = resolveSourceFile(projectDir, file);
|
|
36
|
+
if (target === null)
|
|
37
|
+
return { ok: false, error: "文件必须在工程目录内:" + file };
|
|
38
|
+
let text = "";
|
|
39
|
+
try {
|
|
40
|
+
text = readFileSync(target, "utf8");
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
return { ok: false, error: "读取源码失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
44
|
+
}
|
|
45
|
+
const all = text.split(/\r?\n/u);
|
|
46
|
+
// 末尾换行会切出一个空元素,去掉它,行号才和编辑器一致。
|
|
47
|
+
if (all.length > 1 && all[all.length - 1] === "")
|
|
48
|
+
all.pop();
|
|
49
|
+
const total = all.length;
|
|
50
|
+
const span = Math.max(0, Math.min(SOURCE_CONTEXT_MAX, Math.floor(context)));
|
|
51
|
+
const focus = Math.min(Math.max(1, Math.floor(line)), Math.max(1, total));
|
|
52
|
+
const from = Math.max(1, focus - span);
|
|
53
|
+
const to = Math.min(total, focus + span);
|
|
54
|
+
const lines = [];
|
|
55
|
+
for (let index = from; index <= to; index += 1) {
|
|
56
|
+
lines.push({ line: index, text: all[index - 1] ?? "" });
|
|
57
|
+
}
|
|
58
|
+
return { ok: true, slice: { file, line: focus, from, to, totalLines: total, lines } };
|
|
59
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具调用的统一事件埋点:不逐个工具改 execute,而是在装配时包一层。
|
|
3
|
+
*
|
|
4
|
+
* 做法:给 apply 用的 ctx 套一个 Proxy,只替换 `tools.register`。
|
|
5
|
+
* 其余属性原样转发(绑定到真实 ctx,避免 cordis 内部的 this 语义被破坏)。
|
|
6
|
+
*
|
|
7
|
+
* 每条工具调用产生两条事件:start 与配对的 ok/fail,共享工具名。
|
|
8
|
+
* 摘要是给人看的一行字,完整参数与结果放进 payload,超过 8 KB 时由总线截断。
|
|
9
|
+
*/
|
|
10
|
+
import { emitEvent, setEventSessionKey } from "./event-bus.js";
|
|
11
|
+
import { beginToolCall } from "./progress.js";
|
|
12
|
+
function firstLine(text, limit = 160) {
|
|
13
|
+
const line = text.split(/\r?\n/u)[0] ?? "";
|
|
14
|
+
return line.length > limit ? line.slice(0, limit) + "…" : line;
|
|
15
|
+
}
|
|
16
|
+
function describeError(error) {
|
|
17
|
+
if (error instanceof Error)
|
|
18
|
+
return firstLine(error.message === "" ? error.name : error.message);
|
|
19
|
+
return firstLine(String(error));
|
|
20
|
+
}
|
|
21
|
+
/** 从返回值里挑少量字段进事件,避免把整个产物结构塞进时间线。 */
|
|
22
|
+
function resultDigest(value) {
|
|
23
|
+
if (value === null || typeof value !== "object")
|
|
24
|
+
return undefined;
|
|
25
|
+
const record = value;
|
|
26
|
+
const digest = {};
|
|
27
|
+
for (const key of ["ok", "summary", "error", "message", "elfPath", "hexPath", "binPath", "port", "taskId", "memoryUsage", "warningCount", "projectDir"]) {
|
|
28
|
+
if (record[key] !== undefined)
|
|
29
|
+
digest[key] = record[key];
|
|
30
|
+
}
|
|
31
|
+
// 编译错误最多带 20 条:面板用它跳源码,再多也只是刷屏。
|
|
32
|
+
const errors = record["errors"];
|
|
33
|
+
if (Array.isArray(errors) && errors.length > 0)
|
|
34
|
+
digest["errors"] = errors.slice(0, 20);
|
|
35
|
+
// 编译警告同样截断:多数警告不影响烧录,但「有几条、是什么」要看得见(S75)。
|
|
36
|
+
const warnings = record["warnings"];
|
|
37
|
+
if (Array.isArray(warnings) && warnings.length > 0)
|
|
38
|
+
digest["warnings"] = warnings.slice(0, 20);
|
|
39
|
+
return Object.keys(digest).length === 0 ? undefined : digest;
|
|
40
|
+
}
|
|
41
|
+
function formatDuration(ms) {
|
|
42
|
+
if (ms < 1000)
|
|
43
|
+
return String(Math.round(ms)) + " ms";
|
|
44
|
+
return (ms / 1000).toFixed(1) + " s";
|
|
45
|
+
}
|
|
46
|
+
/** 给单个工具定义加 start / ok / fail 事件。 */
|
|
47
|
+
export function withToolEvents(definition) {
|
|
48
|
+
const original = definition.execute;
|
|
49
|
+
return {
|
|
50
|
+
...definition,
|
|
51
|
+
execute: async (args, exec) => {
|
|
52
|
+
const startedAt = Date.now();
|
|
53
|
+
const owner = exec?.agent?.id;
|
|
54
|
+
if (typeof owner === "string" && owner !== "")
|
|
55
|
+
setEventSessionKey(owner);
|
|
56
|
+
// 登记在途调用:面板能列出「谁在跑」,也能中止(abort 会透传到子进程)。
|
|
57
|
+
const running = beginToolCall({ name: definition.name, session: owner, outer: exec?.signal });
|
|
58
|
+
emitEvent({
|
|
59
|
+
source: "model",
|
|
60
|
+
kind: definition.name,
|
|
61
|
+
status: "start",
|
|
62
|
+
summary: definition.name + " 开始",
|
|
63
|
+
payload: { args: args, callId: running.info.id },
|
|
64
|
+
session: owner,
|
|
65
|
+
});
|
|
66
|
+
try {
|
|
67
|
+
const value = await original(args, exec === undefined ? exec : { ...exec, signal: running.signal });
|
|
68
|
+
emitEvent({
|
|
69
|
+
source: "model",
|
|
70
|
+
kind: definition.name,
|
|
71
|
+
status: "ok",
|
|
72
|
+
summary: definition.name + " 完成(" + formatDuration(Date.now() - startedAt) + ")",
|
|
73
|
+
payload: { durationMs: Date.now() - startedAt, callId: running.info.id, result: resultDigest(value) },
|
|
74
|
+
session: owner,
|
|
75
|
+
});
|
|
76
|
+
running.end();
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
emitEvent({
|
|
81
|
+
source: "model",
|
|
82
|
+
kind: definition.name,
|
|
83
|
+
status: "fail",
|
|
84
|
+
summary: definition.name + " 失败:" + describeError(error),
|
|
85
|
+
payload: { durationMs: Date.now() - startedAt, callId: running.info.id, error: describeError(error) },
|
|
86
|
+
session: owner,
|
|
87
|
+
});
|
|
88
|
+
running.end();
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 返回一个只替换了 `tools` 的 ctx 代理,供 apply 里注册工具使用。
|
|
96
|
+
*
|
|
97
|
+
* 参数:deps 由调用方传 `{}`,留给后续按工具过滤/采样用。
|
|
98
|
+
*/
|
|
99
|
+
export function instrumentContext(ctx, options = {}) {
|
|
100
|
+
const include = options.include ?? ((name) => name.startsWith("stm32_"));
|
|
101
|
+
const tools = {
|
|
102
|
+
register(tool) {
|
|
103
|
+
return ctx.tools.register(include(tool.name) ? withToolEvents(tool) : tool);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
return new Proxy(ctx, {
|
|
107
|
+
get(target, property) {
|
|
108
|
+
if (property === "tools")
|
|
109
|
+
return tools;
|
|
110
|
+
const value = Reflect.get(target, property, target);
|
|
111
|
+
if (typeof value === "function")
|
|
112
|
+
return value.bind(target);
|
|
113
|
+
return value;
|
|
114
|
+
},
|
|
115
|
+
set(target, property, value) {
|
|
116
|
+
return Reflect.set(target, property, value, target);
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
}
|
|
@@ -19,12 +19,18 @@ export const CAPABILITY_COMPONENTS = {
|
|
|
19
19
|
flash: ["openocd"],
|
|
20
20
|
debug: ["openocd", "gdb"],
|
|
21
21
|
generate: ["cubemx", "cubemx-java"],
|
|
22
|
+
// 电路设计:KiCad 提供构建/检查/导出内核,npm 负责拉 tscircuit 依赖。
|
|
23
|
+
ecad: ["kicad", "npm"],
|
|
24
|
+
// PDF 读取只依赖 poppler 的三个命令行工具。
|
|
25
|
+
pdf: ["poppler"],
|
|
22
26
|
};
|
|
23
27
|
const CAPABILITY_LABEL = {
|
|
24
28
|
build: "编译",
|
|
25
29
|
flash: "烧录",
|
|
26
30
|
debug: "调试",
|
|
27
31
|
generate: "生成代码",
|
|
32
|
+
ecad: "电路设计",
|
|
33
|
+
pdf: "PDF 读取",
|
|
28
34
|
};
|
|
29
35
|
const PROBE_CACHE_TTL_MS = 60_000;
|
|
30
36
|
const probeCache = new Map();
|
|
@@ -41,6 +47,8 @@ const CAPABILITY_PRIMARY = {
|
|
|
41
47
|
flash: "openocd",
|
|
42
48
|
debug: "gdb",
|
|
43
49
|
generate: "cubemx-java",
|
|
50
|
+
ecad: "kicad",
|
|
51
|
+
pdf: "poppler",
|
|
44
52
|
};
|
|
45
53
|
/** 单测 / 换机器后清缓存。 */
|
|
46
54
|
export function resetGateCache() {
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 监视登记表的读写入口(面板用)。
|
|
3
|
+
*
|
|
4
|
+
* 工具 stm32_watch 与面板操作的是同一份 `watchState`:面板加一项,下一次 sample 就会读到它。
|
|
5
|
+
* 这里只做登记/注销与列清单,不采样、不发通道事件——采样仍由 `stm32_watch` 触发。
|
|
6
|
+
* 硬件观察点(write/read/access)必须装进 gdb,所以要活动会话;值监视(value)只登记表达式。
|
|
7
|
+
*/
|
|
8
|
+
import { debugSessions } from "./debug-session.js";
|
|
9
|
+
import { registerWatch, watchState, isWatchKind, WATCH_KIND_LABELS, } from "./watch.js";
|
|
10
|
+
function view(entry) {
|
|
11
|
+
return {
|
|
12
|
+
id: entry.id,
|
|
13
|
+
expression: entry.expression,
|
|
14
|
+
kind: entry.kind,
|
|
15
|
+
kindLabel: WATCH_KIND_LABELS[entry.kind],
|
|
16
|
+
lastValue: entry.lastValue,
|
|
17
|
+
lastAt: entry.lastAt,
|
|
18
|
+
lastError: entry.lastError,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function listWatches() {
|
|
22
|
+
return {
|
|
23
|
+
entries: watchState.entries.map(view),
|
|
24
|
+
budget: debugSessions.status().watchpoints,
|
|
25
|
+
canAddWatchpoint: debugSessions.isActive(),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const emptyEdit = (message, errors = []) => ({
|
|
29
|
+
ok: false,
|
|
30
|
+
added: [],
|
|
31
|
+
removed: [],
|
|
32
|
+
errors,
|
|
33
|
+
message,
|
|
34
|
+
});
|
|
35
|
+
/** 加监视项:value 只登记;write/read/access 走 gdb 装硬件观察点。 */
|
|
36
|
+
export async function addWatches(request) {
|
|
37
|
+
const kind = request.kind;
|
|
38
|
+
if (!isWatchKind(kind))
|
|
39
|
+
return emptyEdit("kind 不认识:" + kind + "(只认 value/write/read/access)", ["kind 不认识:" + kind]);
|
|
40
|
+
const wanted = request.expressions.map((item) => item.trim()).filter((item) => item !== "");
|
|
41
|
+
if (wanted.length === 0)
|
|
42
|
+
return emptyEdit("没有表达式", ["没有表达式"]);
|
|
43
|
+
if (kind === "value") {
|
|
44
|
+
const added = [];
|
|
45
|
+
const errors = [];
|
|
46
|
+
for (const expression of wanted) {
|
|
47
|
+
const registered = registerWatch(watchState, expression, "value", Date.now());
|
|
48
|
+
if (registered.entry === null)
|
|
49
|
+
errors.push(registered.error);
|
|
50
|
+
else
|
|
51
|
+
added.push(view(registered.entry));
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
ok: errors.length === 0,
|
|
55
|
+
added,
|
|
56
|
+
removed: [],
|
|
57
|
+
errors,
|
|
58
|
+
message: errors.length === 0
|
|
59
|
+
? "已登记 " + String(added.length) + " 个值监视项"
|
|
60
|
+
: "登记部分失败:" + errors[0],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (!debugSessions.isActive()) {
|
|
64
|
+
return emptyEdit("硬件观察点需要活动调试会话(先 stm32_debug_session)", ["没有调试会话"]);
|
|
65
|
+
}
|
|
66
|
+
const report = await debugSessions.addWatchpoints({ entries: wanted.map((expression) => ({ expression, kind })) });
|
|
67
|
+
return {
|
|
68
|
+
ok: report.ok,
|
|
69
|
+
added: report.added.map(view),
|
|
70
|
+
removed: [],
|
|
71
|
+
errors: report.errors,
|
|
72
|
+
message: report.message,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export async function removeWatches(ids) {
|
|
76
|
+
const wanted = ids.filter((id) => Number.isInteger(id) && id > 0);
|
|
77
|
+
if (wanted.length === 0)
|
|
78
|
+
return emptyEdit("ids 必须是正整数数组", ["ids 必须是正整数数组"]);
|
|
79
|
+
// 硬件观察点住在 gdb 里:交给 DebugSessionManager 删,它同时清登记表、必要时执行 gdb 的 delete。
|
|
80
|
+
const report = await debugSessions.removeWatchpoints(wanted);
|
|
81
|
+
return {
|
|
82
|
+
ok: report.ok,
|
|
83
|
+
added: [],
|
|
84
|
+
removed: report.removed.map(view),
|
|
85
|
+
errors: report.errors,
|
|
86
|
+
message: report.message,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export async function clearWatches() {
|
|
90
|
+
const ids = watchState.entries.map((entry) => entry.id);
|
|
91
|
+
if (ids.length === 0)
|
|
92
|
+
return { ok: true, added: [], removed: [], errors: [], message: "登记表已经是空的" };
|
|
93
|
+
const report = await debugSessions.removeWatchpoints(ids);
|
|
94
|
+
return {
|
|
95
|
+
ok: report.ok,
|
|
96
|
+
added: [],
|
|
97
|
+
removed: report.removed.map(view),
|
|
98
|
+
errors: report.errors,
|
|
99
|
+
message: report.message,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 监视自动采样的接线:录制器 → 真实采样 → 通道事件。
|
|
3
|
+
*
|
|
4
|
+
* 采样本身沿用 `debugSessions.sampleWatches`(目标在跑就先拦停、读完按原状态放回),
|
|
5
|
+
* 这里只负责「什么时候采」和「怎么发」。`channel:watch` 的形状与 stm32_watch
|
|
6
|
+
* 的 sample 一致,面板的历史曲线不用改。
|
|
7
|
+
*/
|
|
8
|
+
import { createWatchRecorder } from "./watch-recorder.js";
|
|
9
|
+
import { debugSessions } from "./debug-session.js";
|
|
10
|
+
import { emitChannel } from "./channels.js";
|
|
11
|
+
/** 样本 → 面板里的一行文本(与 stm32_watch sample 的写法一致)。 */
|
|
12
|
+
export function recordLines(samples) {
|
|
13
|
+
return samples.map((sample) => sample.ok
|
|
14
|
+
? sample.expression + " = " + sample.value + (sample.delta === "" ? "" : "(" + sample.delta + ")")
|
|
15
|
+
: sample.expression + ":" + sample.error);
|
|
16
|
+
}
|
|
17
|
+
/** 进程内唯一的录制器:HTTP 接口与 dispose 都拿同一个。 */
|
|
18
|
+
export const watchRecorder = createWatchRecorder({
|
|
19
|
+
sample: async () => {
|
|
20
|
+
const report = await debugSessions.sampleWatches({});
|
|
21
|
+
return { ok: report.ok, samples: report.samples, errors: report.errors };
|
|
22
|
+
},
|
|
23
|
+
emit: (samples) => {
|
|
24
|
+
emitChannel({
|
|
25
|
+
channel: "watch",
|
|
26
|
+
lines: recordLines(samples),
|
|
27
|
+
meta: {
|
|
28
|
+
action: "record",
|
|
29
|
+
samples: samples.map((sample) => ({
|
|
30
|
+
expression: sample.expression,
|
|
31
|
+
value: sample.value,
|
|
32
|
+
delta: sample.delta,
|
|
33
|
+
ok: sample.ok,
|
|
34
|
+
})),
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
now: () => Date.now(),
|
|
39
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 监视自动采样:按固定间隔调一次 sampleWatches,把结果当通道事件发出去。
|
|
3
|
+
*
|
|
4
|
+
* 复用面板已有的 `channel:watch` 形状,曲线不用改;采样本体仍是 debug-session 的
|
|
5
|
+
* 「临时停下 → 逐项读 → 按原状态放回」,只是把「人手点一次」换成定时器。
|
|
6
|
+
*
|
|
7
|
+
* 默认关:自动采样会在目标运行时反复打断它,联调多久就打断多久,必须显式打开。
|
|
8
|
+
*/
|
|
9
|
+
/** 采样间隔下限:再密就得长时间把目标按在停止态,打断感明显。 */
|
|
10
|
+
export const WATCH_RECORD_MIN_MS = 500;
|
|
11
|
+
/** 采样间隔上限:太稀就看不出变化。 */
|
|
12
|
+
export const WATCH_RECORD_INTERVAL_MAX_MS = 60_000;
|
|
13
|
+
/** 默认间隔。 */
|
|
14
|
+
export const WATCH_RECORD_INTERVAL_MS = 2_000;
|
|
15
|
+
/** 一次录制的时长上限:忘记关也不会一直打断目标。 */
|
|
16
|
+
export const WATCH_RECORD_MAX_MS = 10 * 60_000;
|
|
17
|
+
/** 建一个录制器。抽成工厂是为了单测能注入假采样与假定时器,不碰 gdb。 */
|
|
18
|
+
export function createWatchRecorder(deps) {
|
|
19
|
+
const setTimer = deps.setTimer ?? ((callback, ms) => {
|
|
20
|
+
const timer = setInterval(callback, ms);
|
|
21
|
+
// 定时器不该拖住宿主退出。
|
|
22
|
+
if (typeof timer === "object" && timer !== null && typeof timer.unref === "function") {
|
|
23
|
+
timer.unref();
|
|
24
|
+
}
|
|
25
|
+
return timer;
|
|
26
|
+
});
|
|
27
|
+
const clearTimer = deps.clearTimer ?? ((timer) => {
|
|
28
|
+
clearInterval(timer);
|
|
29
|
+
});
|
|
30
|
+
let timer = null;
|
|
31
|
+
let busy = false;
|
|
32
|
+
let intervalMs = WATCH_RECORD_INTERVAL_MS;
|
|
33
|
+
let startedMs = 0;
|
|
34
|
+
let lastMs = 0;
|
|
35
|
+
let lastError = "";
|
|
36
|
+
let recorded = 0;
|
|
37
|
+
const expired = () => startedMs !== 0 && deps.now() - startedMs >= WATCH_RECORD_MAX_MS;
|
|
38
|
+
const status = () => ({
|
|
39
|
+
active: timer !== null,
|
|
40
|
+
intervalMs: intervalMs,
|
|
41
|
+
recorded: recorded,
|
|
42
|
+
startedAt: startedMs === 0 ? "" : new Date(startedMs).toISOString(),
|
|
43
|
+
lastAt: lastMs === 0 ? "" : new Date(lastMs).toISOString(),
|
|
44
|
+
lastError: lastError,
|
|
45
|
+
remainingMs: startedMs === 0 ? 0 : Math.max(0, WATCH_RECORD_MAX_MS - (deps.now() - startedMs)),
|
|
46
|
+
});
|
|
47
|
+
const halt = (reason) => {
|
|
48
|
+
if (timer !== null)
|
|
49
|
+
clearTimer(timer);
|
|
50
|
+
timer = null;
|
|
51
|
+
lastError = reason;
|
|
52
|
+
return status();
|
|
53
|
+
};
|
|
54
|
+
const tick = async () => {
|
|
55
|
+
if (busy)
|
|
56
|
+
return;
|
|
57
|
+
if (expired()) {
|
|
58
|
+
halt("录制到达上限(" + String(Math.round(WATCH_RECORD_MAX_MS / 60_000)) + " 分钟),已停止");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
busy = true;
|
|
62
|
+
try {
|
|
63
|
+
const result = await deps.sample();
|
|
64
|
+
if (result.ok) {
|
|
65
|
+
recorded += result.samples.length;
|
|
66
|
+
lastMs = deps.now();
|
|
67
|
+
lastError = "";
|
|
68
|
+
if (result.samples.length > 0)
|
|
69
|
+
deps.emit(result.samples);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
lastError = (result.errors ?? []).join(";") === "" ? "采样失败" : (result.errors ?? []).join(";");
|
|
73
|
+
lastMs = deps.now();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
busy = false;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
start(options = {}) {
|
|
85
|
+
const requested = options.intervalMs ?? intervalMs;
|
|
86
|
+
intervalMs = Math.min(WATCH_RECORD_INTERVAL_MAX_MS, Math.max(WATCH_RECORD_MIN_MS, Math.trunc(requested)));
|
|
87
|
+
if (timer !== null) {
|
|
88
|
+
clearTimer(timer);
|
|
89
|
+
timer = null;
|
|
90
|
+
}
|
|
91
|
+
startedMs = deps.now();
|
|
92
|
+
lastMs = 0;
|
|
93
|
+
lastError = "";
|
|
94
|
+
recorded = 0;
|
|
95
|
+
timer = setTimer(() => {
|
|
96
|
+
void tick();
|
|
97
|
+
}, intervalMs);
|
|
98
|
+
return status();
|
|
99
|
+
},
|
|
100
|
+
stop() {
|
|
101
|
+
return halt("");
|
|
102
|
+
},
|
|
103
|
+
status: status,
|
|
104
|
+
reset() {
|
|
105
|
+
if (timer !== null)
|
|
106
|
+
clearTimer(timer);
|
|
107
|
+
timer = null;
|
|
108
|
+
busy = false;
|
|
109
|
+
startedMs = 0;
|
|
110
|
+
lastMs = 0;
|
|
111
|
+
lastError = "";
|
|
112
|
+
recorded = 0;
|
|
113
|
+
intervalMs = WATCH_RECORD_INTERVAL_MS;
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|