@huaqiu/dsh-hardware-ui 0.7.18 → 0.7.20

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.
@@ -22,15 +22,16 @@ export const DESTINATIONS: readonly HardwareDestination[] = Object.freeze([
22
22
  *
23
23
  * Read from the copy module rather than written here: the same strings are
24
24
  * rendered from the navigation row and the viewer strip, and the two must never
25
- * disagree. Keys stay in destination order because the navigation maps over
26
- * `DESTINATIONS`, not over this object.
25
+ * disagree. Getters, not captured values: a DSH language switch must change the
26
+ * navigation labels without a module reload. Keys stay in destination order
27
+ * because the navigation maps over `DESTINATIONS`, not over this object.
27
28
  */
28
29
  export const DESTINATION_LABELS: Readonly<Record<HardwareDestination, string>> = Object.freeze({
29
- schematic: COPY.destinations.schematic,
30
- pcb: COPY.destinations.pcb,
31
- bom: COPY.destinations.bom,
32
- '3d': COPY.destinations['3d'],
33
- order: COPY.destinations.order,
30
+ get schematic() { return COPY.destinations.schematic },
31
+ get pcb() { return COPY.destinations.pcb },
32
+ get bom() { return COPY.destinations.bom },
33
+ get '3d'() { return COPY.destinations['3d'] },
34
+ get order() { return COPY.destinations.order },
34
35
  })
35
36
 
36
37
  /** Which native application a destination can open, if any. */
@@ -123,8 +124,16 @@ export interface ConfigFormLike<T> {
123
124
  export interface HardwareInstallationConfig {
124
125
  readonly kicadRoot?: string
125
126
  readonly solidcraftRoot?: string
126
- /** Set once the first-run setup wizard has been completed or skipped. */
127
- readonly onboardingComplete?: boolean
127
+ /**
128
+ * What the first-run step was told: `deferred` = skipped but still missing,
129
+ * `complete` = configured (§4.6).
130
+ *
131
+ * This is the single source of truth. The legacy `onboardingComplete` boolean is
132
+ * gone from the client (and from the Host schema) — internal-test builds do not
133
+ * need the compatibility, and two overlapping flags made the state harder to
134
+ * reason about than it was worth.
135
+ */
136
+ readonly toolchainSetup?: 'deferred' | 'complete'
128
137
  }
129
138
 
130
139
  /** Parameters carried by one `sidebar.right.openTab` navigation. */
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Open a placed order's page where the user already is.
3
+ *
4
+ * DSH has a `browser` tab kind (`sidebarRight.openTab('browser', { params: { url } })`),
5
+ * so an order page belongs in the right panel beside the board it was made from.
6
+ * Handing it to the OS browser instead splits the user's attention across two
7
+ * windows and loses the page as soon as the browser is closed.
8
+ *
9
+ * The opener is registered by `apply` because only that scope holds the client
10
+ * context; everything that produces an order URL — the panel's confirm button
11
+ * and the tool-result watcher — goes through this one function, so there is a
12
+ * single place that decides where a page opens. The host route is kept as a
13
+ * last resort for a Host whose client has no Browser tab (the page still opens,
14
+ * just outside DSH), and `window.open` covers a client with no sidebar at all.
15
+ *
16
+ * @module
17
+ */
18
+ import { openOrderUrl } from './transport.js'
19
+
20
+ /** One way to put a URL in front of the user; `true` means it succeeded. */
21
+ export type OrderPageOpener = (url: string) => boolean
22
+
23
+ let opener: OrderPageOpener | undefined
24
+
25
+ /** Install the client's own opener. Called once, from `apply`. */
26
+ export function setOrderPageOpener(next: OrderPageOpener): void {
27
+ opener = next
28
+ }
29
+
30
+ /**
31
+ * Show one order page: in a DSH Browser tab when the client has one.
32
+ * @param url - the order page URL returned by the host.
33
+ */
34
+ export function openOrderPage(url: string): void {
35
+ if (url === '') return
36
+ try {
37
+ if (opener?.(url) === true) return
38
+ } catch {
39
+ // A tab that will not open is not a reason to lose the page.
40
+ }
41
+ void openOrderUrl(url).catch(() => {
42
+ window.open(url, '_blank', 'noopener')
43
+ })
44
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The order page as a DSH tab.
3
+ *
4
+ * The page is a real hqpcb order form that must be finished in a browser; the
5
+ * question is only *which* browser. A separate window loses the page as soon as
6
+ * it is closed and leaves the user without the board they were looking at, so
7
+ * this plugin contributes its own tab type and frames the page inside the panel.
8
+ *
9
+ * Why a tab of our own instead of DSH's `browser` kind: that kind belongs to
10
+ * `@deepseek-ai/dsh-client-ui-sidebar-browser`, which this deployment does not
11
+ * load (it is not in the profile's client batch). `openTab('browser', …)` would
12
+ * target a kind nobody registered — the call would be a silent no-op — while our
13
+ * own kind always exists wherever this plugin is active. The page sends no
14
+ * `X-Frame-Options` and no `frame-ancestors`, so framing it is allowed.
15
+ *
16
+ * The token in the URL is the user's Huaqiu login. It is never rendered: the
17
+ * header shows the order id instead, and the iframe is given
18
+ * `referrerPolicy="no-referrer"` so the query cannot leak to a third party.
19
+ *
20
+ * @module
21
+ */
22
+ import { createElement, type CSSProperties, type ReactNode } from 'react'
23
+ import { COPY, HARDWARE_UI_NS } from './copy.js'
24
+ import { C, MONO } from './theme.js'
25
+
26
+ /** Tab kind `openTab` names for an order page. */
27
+ export const ORDER_TAB_KIND = 'hardware-order'
28
+ /** Implementation identity of the order tab body. */
29
+ export const ORDER_TAB_ID = '@huaqiu/dsh-hardware-ui/order'
30
+
31
+ /** The slice of tab info this body reads. */
32
+ interface TabInfo {
33
+ readonly tab: {
34
+ readonly id: string
35
+ readonly navigation: {
36
+ readonly params?: Readonly<Record<string, unknown>>
37
+ readonly revision?: number
38
+ }
39
+ }
40
+ }
41
+
42
+ export interface OrderTabProps {
43
+ /** Standard kit hook supplied by the pane tab seat. */
44
+ readonly useTabInfo?: () => TabInfo
45
+ /** Direct URL, used by tests and by a tab seeded without navigation params. */
46
+ readonly url?: string
47
+ }
48
+
49
+ const BAR: CSSProperties = {
50
+ display: 'flex', alignItems: 'center', gap: 10, padding: '0 10px', height: 34,
51
+ borderBottom: `1px solid ${C.borderSoft}`, background: C.base, flex: '0 0 auto',
52
+ }
53
+ const LABEL: CSSProperties = { fontSize: 11.5, fontWeight: 600, color: C.text }
54
+ const ADDRESS: CSSProperties = {
55
+ flex: 1, minWidth: 0, fontFamily: MONO, fontSize: 10.5, color: C.text3,
56
+ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
57
+ }
58
+ const LINK: CSSProperties = {
59
+ fontSize: 10.5, color: C.accent, textDecoration: 'none', flex: '0 0 auto',
60
+ }
61
+
62
+ /** The order id, which is the only part of the URL that is safe to show. */
63
+ function orderLabel(url: string): string {
64
+ try {
65
+ const id = new URL(url).searchParams.get('id')
66
+ return id === null ? COPY.order.tabTitle : COPY.order.tabOrder(id)
67
+ } catch {
68
+ return COPY.order.tabTitle
69
+ }
70
+ }
71
+
72
+ /**
73
+ * One order page: a slim header plus the page itself.
74
+ * @param props - tab info from the seat, or a direct URL.
75
+ * @returns the framed order page.
76
+ */
77
+ export function OrderTabBody(props: OrderTabProps): ReactNode {
78
+ const params = props.useTabInfo?.().tab.navigation.params
79
+ const fromParams = typeof params?.url === 'string' ? params.url : undefined
80
+ const url = fromParams ?? props.url
81
+ if (url === undefined || url === '') {
82
+ return createElement('div', { style: { padding: 16, color: C.text3, fontSize: 12 } }, COPY.order.tabMissing)
83
+ }
84
+ return createElement(
85
+ 'div',
86
+ { style: { display: 'flex', flexDirection: 'column', height: '100%', minHeight: 0 } },
87
+ createElement(
88
+ 'div',
89
+ { style: BAR },
90
+ createElement('span', { style: LABEL }, COPY.order.tabTitle),
91
+ createElement('span', { style: ADDRESS, title: COPY.order.tabAddressHint }, orderLabel(url)),
92
+ createElement('a', {
93
+ href: url, target: '_blank', rel: 'noreferrer', style: LINK,
94
+ }, COPY.order.openInSystemBrowser),
95
+ ),
96
+ createElement('iframe', {
97
+ src: url,
98
+ title: COPY.order.tabTitle,
99
+ // A payment page needs scripts, forms, popups and downloads; the framed
100
+ // origin is hqpcb.com and this app is localhost, so same-origin access
101
+ // stays impossible across the boundary.
102
+ sandbox: 'allow-scripts allow-forms allow-same-origin allow-popups allow-downloads allow-modals',
103
+ referrerPolicy: 'no-referrer',
104
+ style: { flex: '1 1 auto', width: '100%', border: 0, background: '#fff' },
105
+ }),
106
+ )
107
+ }
108
+
109
+ /** Registration face for the order tab body. */
110
+ export function orderTabRegistration(): {
111
+ readonly name: string
112
+ readonly key: string
113
+ readonly locale: string
114
+ readonly inject: () => Record<string, never>
115
+ } {
116
+ return { name: 'sidebar.right.pane.tab', key: ORDER_TAB_ID, locale: HARDWARE_UI_NS, inject: () => ({}) }
117
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Put an order page into the right panel as soon as an order tool finishes.
3
+ *
4
+ * The panel's own confirm button can open a tab directly, but an agent-driven
5
+ * order ends inside a tool result, where the host has no client context. DSH's
6
+ * established way to close that gap is a session-scoped slot: this watcher sits
7
+ * in the session header, renders nothing, and reads the transcript the client
8
+ * already has. When a `hardware_place_*_order` result carrying an `orderUrl`
9
+ * appears, it calls the same opener the panel uses, so the page lands in a
10
+ * Browser tab next to the board instead of in the OS browser.
11
+ *
12
+ * Why it does not open historical orders: the first snapshot a watcher sees is
13
+ * the conversation that already existed when it mounted. Those URLs are left
14
+ * alone — a reload or a session switch must not resurrect old order tabs — and
15
+ * only URLs that appear afterwards are opened. `opened` then keeps one order
16
+ * from opening twice when several sessions are mounted at once.
17
+ *
18
+ * @module
19
+ */
20
+ import { useEffect, useRef } from 'react'
21
+ import { openOrderPage } from './order-page.js'
22
+
23
+ /** Transcript hooks the host injects into session-scoped slots. */
24
+ type UseNodes = <T>(selector: (snapshot: { nodes?: unknown[]; legacy?: { nodes?: unknown[] } } | undefined) => T) => T
25
+
26
+ interface WatcherProps {
27
+ readonly useChat?: UseNodes
28
+ readonly useSession?: UseNodes
29
+ }
30
+
31
+ interface Node {
32
+ readonly kind?: string
33
+ readonly call?: { readonly name?: string }
34
+ readonly content?: unknown
35
+ readonly children?: unknown
36
+ readonly nodes?: unknown
37
+ readonly subCalls?: unknown
38
+ }
39
+
40
+ /** Tools whose result carries an order page. */
41
+ const ORDER_TOOLS = new Set(['hardware_place_pcb_order', 'hardware_place_smt_order'])
42
+
43
+ const EMPTY: readonly unknown[] = []
44
+ const MAX_DEPTH = 100
45
+
46
+ /** Every order URL this transcript holds, oldest first. */
47
+ export function orderUrlsIn(nodes: readonly unknown[]): string[] {
48
+ const urls: string[] = []
49
+ const visit = (value: unknown, depth: number): void => {
50
+ if (depth > MAX_DEPTH || value === null || typeof value !== 'object') return
51
+ const node = value as Node
52
+ if (node.kind === 'tool-result' && node.call?.name !== undefined && ORDER_TOOLS.has(node.call.name)) {
53
+ const url = orderUrlOf(node)
54
+ if (url !== undefined) urls.push(url)
55
+ }
56
+ for (const list of [node.children, node.nodes, node.subCalls]) {
57
+ if (Array.isArray(list)) for (const child of list) visit(child, depth + 1)
58
+ }
59
+ }
60
+ for (const node of nodes) visit(node, 0)
61
+ return [...new Set(urls)]
62
+ }
63
+
64
+ /** The `orderUrl` field of a tool result, from whichever text block holds it. */
65
+ function orderUrlOf(node: Node): string | undefined {
66
+ const blocks = Array.isArray(node.content) ? node.content : []
67
+ for (const block of blocks) {
68
+ const text = (block as { type?: string; text?: unknown }).type === 'text'
69
+ ? (block as { text?: unknown }).text
70
+ : undefined
71
+ if (typeof text !== 'string' || !text.includes('orderUrl')) continue
72
+ try {
73
+ const value = JSON.parse(text) as { orderUrl?: unknown }
74
+ if (typeof value.orderUrl === 'string' && value.orderUrl.startsWith('http')) return value.orderUrl
75
+ } catch {
76
+ // A tool result may carry other text blocks; only one of them is JSON.
77
+ }
78
+ }
79
+ return undefined
80
+ }
81
+
82
+ /** Identity of one order, so two mounts cannot open the same page twice. */
83
+ function orderKey(url: string): string {
84
+ try {
85
+ return new URL(url).searchParams.get('id') ?? url
86
+ } catch {
87
+ return url
88
+ }
89
+ }
90
+
91
+ const opened = new Set<string>()
92
+
93
+ /**
94
+ * Render-nothing watcher: opens a Browser tab per newly placed order.
95
+ * @param props - transcript hooks injected by the host.
96
+ * @returns always `null`.
97
+ */
98
+ export function OrderWatcher(props: WatcherProps): null {
99
+ const chat = props.useChat === undefined ? EMPTY : props.useChat(snapshot => snapshot?.legacy?.nodes ?? EMPTY)
100
+ const session = props.useSession === undefined ? EMPTY : props.useSession(snapshot => snapshot?.nodes ?? EMPTY)
101
+ const nodes = chat.length > 0 ? chat : session
102
+ const baselined = useRef(false)
103
+
104
+ useEffect(() => {
105
+ const urls = orderUrlsIn(nodes)
106
+ if (!baselined.current) {
107
+ baselined.current = true
108
+ // Remember what was already on screen: these are history, not new orders.
109
+ for (const url of urls) opened.add(orderKey(url))
110
+ return
111
+ }
112
+ for (const url of urls) {
113
+ const key = orderKey(url)
114
+ if (opened.has(key)) continue
115
+ opened.add(key)
116
+ openOrderPage(url)
117
+ }
118
+ }, [nodes])
119
+
120
+ return null
121
+ }
@@ -10,9 +10,10 @@ import { createElement, useEffect, useMemo, useRef, useState, type CSSProperties
10
10
  import { isAbortError } from './ecad.js'
11
11
  import { COPY } from './copy.js'
12
12
  import {
13
- loadOrderConfig, messageOf, openOrderUrl, placeOrder, prepareFabrication,
13
+ loadOrderConfig, messageOf, placeOrder, prepareFabrication,
14
14
  type FabricationProjection, type OrderConfigInfo, type PlacedOrder,
15
15
  } from './transport.js'
16
+ import { openOrderPage } from './order-page.js'
16
17
  import {
17
18
  buttonStyle, C, cardStyle, disabled, dotStyle, MUTED, MONO, MONO_TEXT, pillStyle, R, SECTION_TITLE, SELECT,
18
19
  } from './theme.js'
@@ -21,12 +22,9 @@ export interface OrderPanelProps {
21
22
  readonly boardPath: string
22
23
  readonly boardTitle: string
23
24
  readonly schematicPath?: string
24
- /**
25
- * Workspace root of the session showing this panel. It is the authorization
26
- * base the host re-checks; the project file is not, and never was.
27
- */
25
+ /** Workspace root of the session showing this panel; the base for relative paths. */
28
26
  readonly workspaceRoot?: string
29
- /** DSH session the panel belongs to; the host authorizes against its workspace. */
27
+ /** DSH session the panel belongs to, used to scope host-side discovery. */
30
28
  readonly session?: string
31
29
  readonly configured: boolean
32
30
  }
@@ -193,6 +191,8 @@ export function OrderPanel(props: OrderPanelProps): ReactNode {
193
191
  setPlaced(result)
194
192
  setConfirming(undefined)
195
193
  setAcknowledged(false)
194
+ // The order exists from here on; show it where the user is working.
195
+ openOrderPage(result.orderUrl)
196
196
  } catch (error) {
197
197
  setMessage(messageOf(error))
198
198
  } finally {
@@ -216,7 +216,7 @@ export function OrderPanel(props: OrderPanelProps): ReactNode {
216
216
  setReopening(true)
217
217
  setMessage(undefined)
218
218
  try {
219
- await openOrderUrl(placed.orderUrl)
219
+ openOrderPage(placed.orderUrl)
220
220
  setPlaced({ ...placed, launched: true, status: 'launched' })
221
221
  } catch (error) {
222
222
  setMessage(messageOf(error))
@@ -341,8 +341,8 @@ export function OrderPanel(props: OrderPanelProps): ReactNode {
341
341
  // kept on the object for tooling, not rendered.
342
342
  createElement('p', { style: MUTED },
343
343
  placed.launched
344
- ? COPY.order.resultLaunchedMessage(placed.kind === 'pcb' ? 'PCB' : '贴片')
345
- : COPY.order.resultReadyMessage(placed.kind === 'pcb' ? 'PCB' : '贴片')),
344
+ ? COPY.order.resultLaunchedMessage(placed.kind === 'pcb' ? 'PCB' : COPY.order.smtShort)
345
+ : COPY.order.resultReadyMessage(placed.kind === 'pcb' ? 'PCB' : COPY.order.smtShort)),
346
346
  createElement('a', {
347
347
  style: LINK, href: placed.orderUrl, target: '_blank', rel: 'noreferrer noopener',
348
348
  'data-hardware-order-url': true,