dsh-cad-studio 0.28.0 → 0.29.1

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/index.js +51 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -26,7 +26,7 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
26
26
  | 几何自检 | `cad_studio_check {source}` 在导出/打印前问一次:是否有效实体、外壳是否闭合、每个实体体积多少,返回中文问题清单(零体积 / 外壳未闭合 / 几何无效 / 没有实体) |
27
27
  | 拔模挤出 | `{kind:"extrude", sketch:"S1", h:20, draft:3}` 按 3° 拔模:截面按 `h*tan(3°)` 偏移后 loft 成体,正角度内收、负角度外放 |
28
28
  | 展开图 | `cad_studio_unfold {source, target:"…/net.dxf"}` 把板类零件按共享边摊平成下料图(面片等距铺开,面积与模型一致;孔各自成环;圆柱/圆角等曲面报 `curvedSkipped` 而不是瞎编),可写 DXF 或 SVG |
29
- | 改动记录 | `cad_studio_changes {model}` 看谁改了什么(会话 / 面板 / 外部);每次写都会涨 `rev` 并留一行,面板「信息」里也能看到。别的工具的结果里会顺带一句「自你上次之后,面板改了 N 处」,所以人手动改完,会话下一次动手之前就知道 |
29
+ | 改动记录 | `cad_studio_changes {model}` 看谁改了什么(会话 / 面板 / 外部);每次写都会涨 `rev` 并留一行,面板「信息」里也能看到。别的工具的结果里会顺带一句「自你上次之后,面板改了 N 处」。0.29.0 起「下一次动手」不再等于「先动手再知情」:人改过之后,会话的**写操作会被拒绝一次**(`needsReview`,不给文件动一下),看清了带 `ifRev=<rev>` 重发才算覆盖(或 `force:true` 硬盖并回报 `overwrote`)|
30
30
  | 打印网格 | `cad_studio_mesh {source, target:"…/x.stl"}` 三角化 + 去重合并点/面、修形变、统一法向,可选 `fillHoles`、`targetSize`(减面);回报修复前后的 facets/points,避免「导出成功但网格是碎的」 |
31
31
  | 旋转体与凹槽 | `revolve` 的 `axis` 取草图平面内的轴(`"Y"` 或 `[0,1,0]`)才是实体;垂直轴(XY 草图的 Z)只会扫出零厚度圆环,插件会直接报错。凹槽=`{kind:"cut", with:{kind:"revolve", …}}`,不需要单独 op |
32
32
  | 面板改草图 | 特征抽屉里点「草图 N」展开:数值约束是可编辑输入框(`distance*`/`radius`/`angle` 按度),改完立刻重解并重建;解不出来就回滚并报错 |
package/index.js CHANGED
@@ -1047,7 +1047,11 @@ class FreeCadBridge {
1047
1047
  this.scenes.delete(key)
1048
1048
  for (const [asmId, rec] of [...this.assemblies]) if (rec.sceneId === key) this.assemblies.delete(asmId)
1049
1049
  try { await rm(join(FreeCadBridge.dataDir, 'scenes', `${key}.json`), { force: true }) } catch {}
1050
- if (this.latest && this.latest.id === key) {
1050
+ // Re-point the "latest" pointer whenever it went stale: the deleted row may
1051
+ // be it, and after a fresh process it may be null while the rows still say
1052
+ // otherwise — either way the panel's live tab must not end up pointing at
1053
+ // nothing (it renders an empty viewport and looks like the tool broke).
1054
+ if (!this.latest || this.latest.id === key) {
1051
1055
  const next = this.history[0]
1052
1056
  this.latest = next ? { id: next.id, title: next.title, at: next.at, scene: this.scenes.get(next.id) ?? null } : null
1053
1057
  }
@@ -4208,6 +4212,34 @@ else:
4208
4212
  return (doc.changes || []).filter((c) => c.rev > seen && c.by !== by)
4209
4213
  }
4210
4214
 
4215
+ /**
4216
+ * Read-before-write. A digest attached to the result arrives too late when the
4217
+ * very next call is a write — the damage is done before the agent reads it. So
4218
+ * a write from one side is refused once when the other side changed the model
4219
+ * since that side last looked. The agent re-issues with ifRev (the revision it
4220
+ * just saw) or force, which is the explicit "yes, overwrite that".
4221
+ */
4222
+ const foreignWriteGuard = (doc, by, args) => {
4223
+ const pending = pendingChanges(doc, by)
4224
+ if (!pending.length) return null
4225
+ if (args && args.force === true) return { ok: true, rows: changeRows(pending) }
4226
+ const ifRev = Number(args && args.ifRev)
4227
+ if (Number.isFinite(ifRev) && ifRev === Number(doc.rev)) return { ok: true, rows: changeRows(pending) }
4228
+ return { ok: false, rows: changeRows(pending) }
4229
+ }
4230
+
4231
+ /** The refusal a guarded write returns: no file touched, rows in hand. */
4232
+ const guardRefusal = (doc, guard, by) => {
4233
+ const latest = guard.rows[guard.rows.length - 1]
4234
+ return {
4235
+ ok: false,
4236
+ needsReview: true,
4237
+ model: doc.name,
4238
+ conflict: { rev: Number(doc.rev) || 0, rows: guard.rows },
4239
+ hint: `${latest.by}刚在这个模型上改了 ${guard.rows.length} 处(最新一条:${latest.detail}),这次改动没有执行。看清楚了再重发:带 ifRev=${Number(doc.rev) || 0} 表示在最新版上改,或 force=true 表示认可覆盖。`,
4240
+ }
4241
+ }
4242
+
4211
4243
  /** One line a human can read, so the digest means something without a lookup. */
4212
4244
  const changeRows = (rows) => rows.map((c) => ({
4213
4245
  rev: c.rev,
@@ -4557,6 +4589,8 @@ else:
4557
4589
  to: { type: 'string', description: 'For action=rename / duplicate: the new model name.' },
4558
4590
  sketches: { type: 'object', description: 'For kind="features": named sketches stored in the document, e.g. {S1:{geometry:[{id:"g1",type:"line",p1:[0,0,0],p2:[80,0,0]}, …],constraints:[…]}}. A feature extrudes/revolves/lofts one with {kind:"extrude",sketch:"S1"}.' },
4559
4591
  export: { type: 'string', description: 'Also write the model out as STEP at this path (create / update / show). Saves the caller from re-implementing the feature emitter.' },
4592
+ ifRev: { type: 'number', description: 'Write guard. If someone edited this model in the panel since you last saw it, the write is refused once and tells you the revision; re-issue with ifRev=<that revision> to say "I saw those edits, write on top of them".' },
4593
+ force: { type: 'boolean', description: 'Write guard escape hatch: true means "I accept overwriting the panel edits I was just told about". Prefer ifRev — force skips the review.' },
4560
4594
  },
4561
4595
  required: ['action'],
4562
4596
  additionalProperties: false,
@@ -4710,6 +4744,10 @@ else:
4710
4744
  if (action === 'list') {
4711
4745
  return { ok: true, model: doc.name, kind: doc.kind, features: publicFeatures(doc), sketches: doc.sketches ?? {}, graph: graphView(doc, { errors: [], executed: null }), stats: doc.stats ?? null }
4712
4746
  }
4747
+ // Every other action writes — including undo/redo, which can undo a human's
4748
+ // edit as easily as the agent's own. Same read-before-write rule as above.
4749
+ const guard = by === 'agent' ? foreignWriteGuard(doc, by, args) : null
4750
+ if (guard && guard.ok === false) return guardRefusal(doc, guard, by)
4713
4751
  if (action === 'sketch') {
4714
4752
  // Sketches are document data (geometry + constraints), so editing one is
4715
4753
  // a document edit: same rollback rule as a feature edit, and the sketch
@@ -4854,7 +4892,7 @@ else:
4854
4892
  doc.history = doc.history.slice(-20)
4855
4893
  recordChange(doc, { by, kind: `feature.${action}`, detail: label })
4856
4894
  await writeModel(doc, dir)
4857
- return await withChanges(doc, by, {
4895
+ const out = await withChanges(doc, by, {
4858
4896
  ok: true,
4859
4897
  evaluated: result.ok !== false && (result.errors ?? []).length === 0,
4860
4898
  model: doc.name,
@@ -4875,6 +4913,8 @@ else:
4875
4913
  computed: result.computed ?? null,
4876
4914
  revalidated: result.revalidated === true,
4877
4915
  }, dir)
4916
+ if (guard && guard.rows.length) out.overwrote = guard.rows
4917
+ return out
4878
4918
  }
4879
4919
 
4880
4920
  tool({
@@ -4894,6 +4934,8 @@ else:
4894
4934
  feature: { type: 'object', description: 'add: the feature to append. update/toggle: {id, ...fields to change}. remove/move: {id}.' },
4895
4935
  id: { type: 'string', description: 'Feature id (f1, f2 …); alternative to feature.id.' },
4896
4936
  to: { type: 'integer', description: 'move: target index (0-based). Default: to the end.' },
4937
+ ifRev: { type: 'number', description: 'Write guard: if the panel edited this model since you last saw it, the write is refused once with the revision; re-issue with ifRev=<that revision> to write on top of those edits.' },
4938
+ force: { type: 'boolean', description: 'Write guard escape hatch (accepts overwriting the panel edits you were just told about). Prefer ifRev.' },
4897
4939
  },
4898
4940
  required: ['model', 'action'],
4899
4941
  additionalProperties: false,
@@ -5058,6 +5100,8 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
5058
5100
 
5059
5101
  if (action === 'update') {
5060
5102
  const doc = await readModel(args.name, dir)
5103
+ const guard = by === 'agent' ? foreignWriteGuard(doc, by, args) : null
5104
+ if (guard && guard.ok === false) return guardRefusal(doc, guard, by)
5061
5105
  if (args.vars !== undefined) doc.vars = normalizeVars(args.vars, doc.vars)
5062
5106
  if (args.sketches !== undefined) doc.sketches = normalizeSketches(args.sketches, doc.sketches)
5063
5107
  const before = {
@@ -5091,11 +5135,15 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
5091
5135
  doc.history = doc.history.slice(-20)
5092
5136
  recordChange(doc, { by, kind: 'doc.update', detail: describeDocUpdate(args, doc) })
5093
5137
  await writeModel(doc, dir)
5094
- return await withChanges(doc, by, { ...result, kind: doc.kind, ...(doc.kind === 'features' ? { features: publicFeatures(doc) } : {}) }, dir)
5138
+ const out = await withChanges(doc, by, { ...result, kind: doc.kind, ...(doc.kind === 'features' ? { features: publicFeatures(doc) } : {}) }, dir)
5139
+ if (guard && guard.rows.length) out.overwrote = guard.rows
5140
+ return out
5095
5141
  }
5096
5142
 
5097
5143
  if (action === 'revert') {
5098
5144
  const doc = await readModel(args.name, dir)
5145
+ const guard = by === 'agent' ? foreignWriteGuard(doc, by, args) : null
5146
+ if (guard && guard.ok === false) return guardRefusal(doc, guard, by)
5099
5147
  const last = doc.history.pop()
5100
5148
  if (!last) throw new Error(`${doc.name} 没有可回退的历史`)
5101
5149
  if (last.prev) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.28.0",
3
+ "version": "0.29.1",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {