@huaqiu/dsh-hardware-ui 0.0.0-stage → 0.7.13

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.
@@ -0,0 +1,123 @@
1
+ /**
2
+ * One store handle shared by the navigation, viewer and settings
3
+ * registrations. It follows DSH's declared-store contract: a spec (init +
4
+ * complete write set) plus a per-scope instance factory. The handle is created
5
+ * inside `apply()`, never at module scope, so a plugin reload cannot resurrect
6
+ * an old snapshot.
7
+ */
8
+ import type {
9
+ HardwareActions, HardwareDestination, HardwareState, DiscoveryInfo, InstallationsInfo,
10
+ } from './model.js'
11
+
12
+ type Mutable<T> = { -readonly [K in keyof T]: T[K] }
13
+
14
+ type Mutator = (draft: Mutable<HardwareState>, ...params: unknown[]) => void
15
+
16
+ export interface HardwareStoreInstance {
17
+ readonly actions: HardwareActions
18
+ getSnapshot(): HardwareState
19
+ subscribe(listener: () => void): () => void
20
+ clearPersisted(): void
21
+ }
22
+
23
+ export interface HardwareStoreHandle {
24
+ readonly spec: {
25
+ readonly init: () => HardwareState
26
+ readonly actions: Record<string, Mutator>
27
+ }
28
+ create(scopeKey?: string): HardwareStoreInstance
29
+ }
30
+
31
+ function initial(): HardwareState {
32
+ return { activeDestination: 'schematic', revision: 0 }
33
+ }
34
+
35
+ /** A selected project resets artifact selections to its own defaults. */
36
+ function adoptProject(draft: Mutable<HardwareState>, project: DiscoveryInfo['selected']): void {
37
+ draft.projectPath = project?.path
38
+ const schematic = project?.sheetPath ?? project?.schematic?.path ?? project?.rootSheet?.path
39
+ if (schematic === undefined) delete draft.schematicPath
40
+ else draft.schematicPath = schematic
41
+ if (schematic === undefined) delete draft.sheetPath
42
+ else draft.sheetPath = schematic
43
+ const board = project?.defaultBoard ?? project?.boards?.[0]?.path
44
+ if (board === undefined) delete draft.pcbPath
45
+ else draft.pcbPath = board
46
+ draft.bomPath = project?.bom?.path
47
+ }
48
+
49
+ const mutators = {
50
+ setActive(draft: Mutable<HardwareState>, destination: HardwareDestination): void {
51
+ draft.activeDestination = destination
52
+ draft.revision += 1
53
+ },
54
+ setWorkspaceRoot(draft: Mutable<HardwareState>, root: string): void {
55
+ draft.workspaceRoot = root
56
+ },
57
+ setDiscovery(draft: Mutable<HardwareState>, discovery: DiscoveryInfo): void {
58
+ draft.discovery = discovery
59
+ draft.workspaceRoot = discovery.cwd ?? draft.workspaceRoot
60
+ adoptProject(draft, discovery.selected)
61
+ draft.revision += 1
62
+ },
63
+ selectProject(draft: Mutable<HardwareState>, path: string): void {
64
+ const project = draft.discovery?.projects?.find(candidate => candidate.path === path)
65
+ adoptProject(draft, project)
66
+ draft.revision += 1
67
+ },
68
+ selectArtifact(draft: Mutable<HardwareState>, path: string, kind: 'schematic' | 'pcb'): void {
69
+ if (kind === 'schematic') {
70
+ draft.schematicPath = path
71
+ draft.sheetPath = path
72
+ } else {
73
+ draft.pcbPath = path
74
+ }
75
+ draft.revision += 1
76
+ },
77
+ setInstallations(draft: Mutable<HardwareState>, installations: InstallationsInfo): void {
78
+ draft.installations = installations
79
+ draft.revision += 1
80
+ },
81
+ setNotice(draft: Mutable<HardwareState>, notice?: string): void {
82
+ if (notice === undefined) delete draft.notice
83
+ else draft.notice = notice
84
+ },
85
+ refresh(draft: Mutable<HardwareState>): void {
86
+ draft.revision += 1
87
+ },
88
+ }
89
+
90
+ /** Create the shared handle. Call once inside `apply()`. */
91
+ export function createHardwareStore(): HardwareStoreHandle {
92
+ return {
93
+ spec: { init: initial, actions: mutators as unknown as Record<string, Mutator> },
94
+ create(): HardwareStoreInstance {
95
+ let snapshot = initial()
96
+ const listeners = new Set<() => void>()
97
+ const commit = (mutate: (draft: Mutable<HardwareState>) => void): void => {
98
+ const draft: Mutable<HardwareState> = { ...snapshot }
99
+ mutate(draft)
100
+ snapshot = draft
101
+ for (const listener of [...listeners]) listener()
102
+ }
103
+ return {
104
+ actions: {
105
+ setActive: destination => commit(draft => mutators.setActive(draft, destination)),
106
+ setWorkspaceRoot: root => commit(draft => { draft.workspaceRoot = root }),
107
+ setDiscovery: discovery => commit(draft => mutators.setDiscovery(draft, discovery)),
108
+ selectProject: path => commit(draft => mutators.selectProject(draft, path)),
109
+ selectArtifact: (path, kind) => commit(draft => mutators.selectArtifact(draft, path, kind)),
110
+ setInstallations: installations => commit(draft => mutators.setInstallations(draft, installations)),
111
+ setNotice: notice => commit(draft => mutators.setNotice(draft, notice)),
112
+ refresh: () => commit(draft => mutators.refresh(draft)),
113
+ },
114
+ getSnapshot: () => snapshot,
115
+ subscribe(listener) {
116
+ listeners.add(listener)
117
+ return () => { listeners.delete(listener) }
118
+ },
119
+ clearPersisted() {},
120
+ }
121
+ },
122
+ }
123
+ }
@@ -0,0 +1,135 @@
1
+ /** Same-origin calls to the `@huaqiu/hardware-artifacts` Host routes. */
2
+ import type {
3
+ DiscoveryInfo, HardwareProjectInfo, InstallationsInfo, NativeApplication,
4
+ } from './model.js'
5
+
6
+ const PREFIX = '/api/v1/hardware'
7
+
8
+ interface Envelope<T> {
9
+ readonly ok?: boolean
10
+ readonly error?: { readonly code?: string; readonly message?: string }
11
+ readonly data?: T
12
+ readonly [key: string]: unknown
13
+ }
14
+
15
+ async function request<T>(path: string, init?: RequestInit): Promise<T> {
16
+ const response = await fetch(`${PREFIX}${path}`, init)
17
+ const body = await response.json().catch(() => undefined) as Envelope<T> | undefined
18
+ if (!response.ok || body?.ok === false) {
19
+ const detail = body?.error?.message ?? `HTTP ${response.status}`
20
+ throw new Error(body?.error?.code === undefined ? detail : `${body.error.code}: ${detail}`)
21
+ }
22
+ return body as T
23
+ }
24
+
25
+ function query(cwd: string | undefined, project?: string): string {
26
+ const params = new URLSearchParams()
27
+ if (cwd !== undefined && cwd !== '') params.set('cwd', cwd)
28
+ if (project !== undefined && project !== '') params.set('project', project)
29
+ const text = params.toString()
30
+ return text === '' ? '' : `?${text}`
31
+ }
32
+
33
+ export async function loadProjects(cwd?: string, project?: string): Promise<DiscoveryInfo> {
34
+ const body = await request<DiscoveryInfo>(`/projects${query(cwd, project)}`)
35
+ const fallback: DiscoveryInfo = {
36
+ cwd: body.cwd,
37
+ projects: Array.isArray(body.projects) ? body.projects : [],
38
+ ...(body.selected === undefined ? {} : { selected: body.selected as HardwareProjectInfo }),
39
+ ...(body.issue === undefined ? {} : { issue: body.issue }),
40
+ }
41
+ return fallback
42
+ }
43
+
44
+ export async function loadInstallations(): Promise<InstallationsInfo> {
45
+ return request<InstallationsInfo>('/installations')
46
+ }
47
+
48
+ export async function environment(): Promise<{ text?: string; childEnv?: Record<string, string> }> {
49
+ return request<{ text?: string; childEnv?: Record<string, string> }>('/environment')
50
+ }
51
+
52
+ export async function openArtifact(requestBody: {
53
+ application: NativeApplication
54
+ artifactPath: string
55
+ kind: string
56
+ projectPath?: string
57
+ }): Promise<void> {
58
+ await request('/open', {
59
+ method: 'POST',
60
+ headers: { 'content-type': 'application/json' },
61
+ body: JSON.stringify(requestBody),
62
+ })
63
+ }
64
+
65
+ /** Saved text of one authorized `.kicad_sch` or `.kicad_pcb`. */
66
+ export interface ArtifactSource {
67
+ readonly path: string
68
+ readonly kind: 'schematic' | 'pcb'
69
+ readonly revision: string
70
+ readonly bytes: number
71
+ readonly mtimeMs?: number
72
+ readonly text: string
73
+ }
74
+
75
+ export async function loadArtifactSource(
76
+ artifactPath: string,
77
+ signal?: AbortSignal,
78
+ expectedRevision?: string,
79
+ ): Promise<ArtifactSource> {
80
+ const params = new URLSearchParams({ path: artifactPath })
81
+ if (expectedRevision !== undefined && expectedRevision !== '') params.set('revision', expectedRevision)
82
+ return request<ArtifactSource>(`/source?${params.toString()}`, { signal })
83
+ }
84
+
85
+ /** Generated PCB GLB for one authorized `.kicad_pcb`. */
86
+ export interface GlbArtifact {
87
+ readonly url: string
88
+ readonly path: string
89
+ readonly revision: string
90
+ readonly bytes: number
91
+ readonly generatedAt: string
92
+ readonly cached: boolean
93
+ }
94
+
95
+ /** Grouped BOM rows produced by the host from the saved root schematic. */
96
+ export interface BomRecord {
97
+ readonly references: readonly string[]
98
+ readonly quantity: number
99
+ readonly value: string
100
+ readonly footprint: string
101
+ readonly dnp: boolean
102
+ readonly fields: Readonly<Record<string, string>>
103
+ }
104
+
105
+ export interface BomProjection {
106
+ readonly path: string
107
+ readonly revision: string
108
+ readonly generatedAt: string
109
+ readonly cached: boolean
110
+ readonly rows: readonly BomRecord[]
111
+ readonly source: string
112
+ }
113
+
114
+ export async function loadBom(artifactPath: string, signal?: AbortSignal): Promise<BomProjection> {
115
+ return request<BomProjection>('/bom', {
116
+ method: 'POST',
117
+ headers: { 'content-type': 'application/json' },
118
+ body: JSON.stringify({ path: artifactPath }),
119
+ signal,
120
+ })
121
+ }
122
+
123
+ /** Prepare the GLB; the returned `url` is served by the same host route. */
124
+ export async function prepareBoardGlb(
125
+ artifactPath: string,
126
+ cwd?: string,
127
+ signal?: AbortSignal,
128
+ ): Promise<GlbArtifact & { readonly url: string }> {
129
+ return request<GlbArtifact & { readonly url: string }>('/glb', {
130
+ method: 'POST',
131
+ headers: { 'content-type': 'application/json' },
132
+ body: JSON.stringify({ path: artifactPath, ...(cwd === undefined ? {} : { cwd }) }),
133
+ signal,
134
+ })
135
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Node half of `@huaqiu/dsh-hardware-ui`.
3
+ *
4
+ * All product UI is browser-side. This entry exists so the package is a normal
5
+ * installable plugin in the DSH bundle; it intentionally provides no service
6
+ * and injects nothing. Host operations live in `@huaqiu/hardware-artifacts`.
7
+ */
8
+ export const name = '@huaqiu/dsh-hardware-ui'
9
+
10
+ /** Browser-only plugin: the node Loader seat intentionally does nothing. */
11
+ export function apply(): void {}
12
+
13
+ /** The tab kind owned by this product UI. */
14
+ export const HARDWARE_TAB_KIND = 'hardware-engineering'
15
+
16
+ /** Slots this product UI occupies. Exported for contract tests and readers. */
17
+ export const HARDWARE_UI_SLOTS = Object.freeze([
18
+ 'conversation.header.leading',
19
+ 'conversation.session.header.corner',
20
+ 'sidebar.right.pane.tab',
21
+ 'settings.section',
22
+ ] as const)