dsh-cad-studio 0.2.3 → 0.2.4

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/client.js +69 -58
  2. package/index.js +14 -4
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -970,7 +970,6 @@ window.__ModuleLoader__.load({
970
970
  /** Host config: 'session' | 'always' | 'off' — see index.js DEFAULTS.autoOpen. */
971
971
  let cadAutoOpen = 'session'
972
972
  /** Memo for "does this session own CAD work", keyed by session id. */
973
- let ownsCache = { id: '', owns: false, at: 0 }
974
973
 
975
974
  function Viewport({ payload, visibility, options, handleRef: externalHandle }) {
976
975
  const hostRef = useRef(null)
@@ -2525,40 +2524,11 @@ const FILE_EXTS = ['step', 'stp', 'iges', 'igs', 'brep', 'stl']
2525
2524
  if (!id) id = readDomSession()
2526
2525
  if (typeof id === 'string' && id && id !== panelSession) {
2527
2526
  panelSession = id
2528
- ownsCache = { id: '', owns: false, at: 0 }
2529
2527
  }
2530
2528
  return panelSession
2531
2529
  }
2530
+ readSession()
2532
2531
 
2533
- // The host owns the policy and both "did CAD happen here" signals, so a
2534
- // single probe settles it: /state carries the mode plus the last session
2535
- // that published, /scenes carries who owns the older ones.
2536
- const sessionOwnsCad = async () => {
2537
- const id = panelSession || ''
2538
- if (id && ownsCache.id === id && Date.now() - ownsCache.at < 15000) return ownsCache.owns
2539
- let owns = false
2540
- try {
2541
- const [resState, resScenes] = await Promise.all([
2542
- fetch(`${ROUTE}/state`, { headers: { accept: 'application/json' } }),
2543
- fetch(`${ROUTE}/scenes`, { headers: { accept: 'application/json' } }),
2544
- ])
2545
- if (resState.ok) {
2546
- const st = await resState.json()
2547
- const mode = st && st.config && st.config.autoOpen
2548
- if (['session', 'always', 'off'].includes(mode)) cadAutoOpen = mode
2549
- if (id && st && st.lastSession === id) owns = true
2550
- }
2551
- if (!owns && id && resScenes.ok) {
2552
- const data = await resScenes.json()
2553
- owns = (data.scenes || []).some((sc) => sc && sc.session === id)
2554
- }
2555
- } catch (err) { /* a failed probe must not open the bar */ }
2556
- if (cadAutoOpen === 'off') return false
2557
- if (cadAutoOpen === 'always') return true
2558
- if (!id) return false
2559
- if (id) ownsCache = { id, owns, at: Date.now() }
2560
- return owns
2561
- }
2562
2532
  const AUTO_OPEN_DELAYS = [300, 900, 1800, 3000, 5000]
2563
2533
  const openPane = (index) => {
2564
2534
  const ctrl = ctx.get?.('sidebarRight')
@@ -2573,49 +2543,90 @@ const FILE_EXTS = ['step', 'stp', 'iges', 'igs', 'brep', 'stl']
2573
2543
  }
2574
2544
  // Debug surface: `window.__cadStudioAutoOpen` answers "why did the pane
2575
2545
  // open / not open" without digging through the bundle.
2576
- const autoOpenTrace = { mode: '', session: '', owns: null, opened: 0, why: 'boot' }
2546
+ const autoOpenTrace = { mode: '', session: '', opened: 0, why: 'boot' }
2577
2547
  try {
2578
2548
  Object.defineProperty(window, '__cadStudioAutoOpen', {
2579
2549
  configurable: true,
2580
2550
  get: () => ({ ...autoOpenTrace, mode: cadAutoOpen, session: panelSession }),
2581
2551
  })
2582
2552
  } catch (err) { /* a stale definition from a previous apply is fine */ }
2583
- const maybeOpen = () => {
2553
+
2554
+ // Policy: never open because a session "has used CAD before", and never
2555
+ // on a conversation switch. The single trigger is live work — this
2556
+ // session publishes something while the pane is off screen — and then
2557
+ // it opens on the live (activity) tab. Everything else stays closed.
2558
+ const paneMounted = () => Boolean(document.querySelector('[data-cad-panel-root]'))
2559
+ let watchStamp = ''
2560
+ let watchBusy = false
2561
+ let ranRecently = false
2562
+ const watchOnce = async () => {
2563
+ readSession()
2564
+ if (cadAutoOpen === 'off') { autoOpenTrace.why = 'off'; return }
2565
+ if (paneMounted()) { watchStamp = ''; autoOpenTrace.why = 'shown'; return }
2566
+ const id = panelSession || ''
2567
+ if (!id || watchBusy) return
2568
+ watchBusy = true
2569
+ try {
2570
+ const res = await fetch(`${ROUTE}/state`, { headers: { accept: 'application/json' } })
2571
+ if (res.ok) {
2572
+ const st = await res.json()
2573
+ // A tool is executing for this session right now: that is the
2574
+ // trigger the policy asks for, so raise the pane on the live tab.
2575
+ const runningNow = Number(st && st.running) > 0
2576
+ && (!st.runningSession || st.runningSession === id)
2577
+ if (runningNow && !ranRecently) {
2578
+ ranRecently = true
2579
+ autoOpenTrace.opened += 1
2580
+ autoOpenTrace.why = 'running'
2581
+ openPane(0)
2582
+ return
2583
+ }
2584
+ if (!runningNow) ranRecently = false
2585
+ const latest = st && st.latest
2586
+ if (latest && latest.session === id) {
2587
+ const stamp = `${latest.id}@${latest.at || ''}`
2588
+ const prev = watchStamp
2589
+ watchStamp = stamp
2590
+ if (prev && prev !== stamp) {
2591
+ autoOpenTrace.opened += 1
2592
+ autoOpenTrace.why = 'activity'
2593
+ openPane(0)
2594
+ } else if (!prev) {
2595
+ autoOpenTrace.why = 'armed'
2596
+ }
2597
+ } else {
2598
+ watchStamp = ''
2599
+ autoOpenTrace.why = 'idle'
2600
+ }
2601
+ }
2602
+ } catch (err) {
2603
+ autoOpenTrace.why = 'probe-failed'
2604
+ } finally {
2605
+ watchBusy = false
2606
+ }
2607
+ }
2608
+ watchOnce()
2609
+ const watchTimer = setInterval(watchOnce, 3000)
2610
+ ctx.effect?.(() => () => clearInterval(watchTimer))
2611
+ // The session id still has to be tracked (the watcher only opens for the
2612
+ // session that is on screen), but a change merely re-arms the baseline.
2613
+ const rebase = () => {
2584
2614
  readSession()
2585
- autoOpenTrace.why = 'probe'
2586
- sessionOwnsCad().then((owns) => {
2587
- autoOpenTrace.owns = owns
2588
- if (owns) { autoOpenTrace.opened += 1; autoOpenTrace.why = 'opened'; openPane(0) } else autoOpenTrace.why = 'gated'
2589
- }).catch(() => { autoOpenTrace.why = 'probe-failed' })
2615
+ watchStamp = ''
2616
+ autoOpenTrace.session = panelSession
2590
2617
  }
2591
- maybeOpen()
2592
- // Every session carries its OWN rightbar layout, so entering a session
2593
- // replaces the bar with that session's tabs. Re-place ours whenever the
2594
- // route or the current session changes; a slow caught-up retry covers
2595
- // hosts that notify neither way.
2596
- const kick = () => maybeOpen()
2597
- const kickSoon = () => setTimeout(kick, 400)
2598
- try {
2599
- window.addEventListener('hashchange', kickSoon)
2600
- window.addEventListener('popstate', kickSoon)
2601
- ctx.effect?.(() => () => {
2602
- window.removeEventListener('hashchange', kickSoon)
2603
- window.removeEventListener('popstate', kickSoon)
2604
- })
2605
- } catch (err) { /* no window: nothing to stage against */ }
2606
- for (const delay of [2000, 5000, 9000, 14000]) setTimeout(kick, delay)
2607
2618
  if (current != null && typeof current.subscribe === 'function' && typeof current.getSnapshot === 'function') {
2608
2619
  try {
2609
- const unsubscribe = current.subscribe(() => { readSession(); maybeOpen() })
2620
+ const unsubscribe = current.subscribe(() => rebase())
2610
2621
  ctx.effect?.(() => unsubscribe)
2611
- } catch (err) { /* no session store on this host: boot-time placement is all */ }
2622
+ } catch (err) { /* no session store on this host */ }
2612
2623
  // Switching conversations rewrites that attribute without always
2613
2624
  // firing a store event, so watch it directly; the observer is cheap.
2614
2625
  try {
2615
- let lastSeen = ''
2626
+ let lastSeen = readDomSession()
2616
2627
  const observer = new MutationObserver(() => {
2617
2628
  const next = readDomSession()
2618
- if (next && next !== lastSeen) { lastSeen = next; readSession(); maybeOpen() }
2629
+ if (next && next !== lastSeen) { lastSeen = next; rebase() }
2619
2630
  })
2620
2631
  observer.observe(document.documentElement, {
2621
2632
  subtree: true,
package/index.js CHANGED
@@ -1012,8 +1012,18 @@ __out = {
1012
1012
  const tool = (definition) => ctx.effect(() => ctx.tools.register({
1013
1013
  ...definition,
1014
1014
  execute: async (args, exec) => {
1015
- bridge.currentSession = sessionOf(exec)
1016
- return definition.execute(args, exec)
1015
+ const session = sessionOf(exec)
1016
+ bridge.currentSession = session
1017
+ // A tool is working right now: the panel watches this to show itself on
1018
+ // the live tab (and only for the session that is actually running).
1019
+ bridge.running = (bridge.running || 0) + 1
1020
+ bridge.runningSession = session
1021
+ try {
1022
+ return await definition.execute(args, exec)
1023
+ } finally {
1024
+ bridge.running = Math.max(0, (bridge.running || 1) - 1)
1025
+ if (!bridge.running) bridge.runningSession = ''
1026
+ }
1017
1027
  },
1018
1028
  }))
1019
1029
 
@@ -2069,10 +2079,10 @@ if _TARGET:
2069
2079
  try {
2070
2080
  const parsed = JSON.parse(await readFile(join(dir, 'state.json'), 'utf8'))
2071
2081
  const setup = setupStatus(bridge, { withVersion: false })
2072
- return send(200, JSON.stringify({ runtime, setup, config: { autoOpen: resolved.autoOpen }, latest: parsed.latest ?? null, lastSession: parsed.lastSession ?? '', assemblies: parsed.assemblies ?? [] }))
2082
+ return send(200, JSON.stringify({ runtime, setup, config: { autoOpen: resolved.autoOpen }, latest: parsed.latest ?? null, lastSession: parsed.lastSession ?? '', assemblies: parsed.assemblies ?? [], running: bridge.running || 0, runningSession: bridge.runningSession || '' }))
2073
2083
  } catch {
2074
2084
  const setup = setupStatus(bridge, { withVersion: false })
2075
- return send(200, JSON.stringify({ runtime, setup, config: { autoOpen: resolved.autoOpen }, latest: null, lastSession: '' }))
2085
+ return send(200, JSON.stringify({ runtime, setup, config: { autoOpen: resolved.autoOpen }, latest: null, lastSession: '', running: bridge.running || 0, runningSession: bridge.runningSession || '' }))
2076
2086
  }
2077
2087
  }
2078
2088
  if (url.pathname === `${ROUTE_PREFIX}/scenes`) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {