@huaqiu/hardware-artifacts 0.7.12 → 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.
- package/lib/routes.mjs +150 -20
- package/lib/service.d.mts +129 -2
- package/lib/service.mjs +538 -77
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/routes.ts +108 -9
- package/src/service.ts +639 -54
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huaqiu/hardware-artifacts",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Hardware Engineering host capability: deterministic artifact discovery, installation resolution, native application launch and compact agent context.",
|
|
6
6
|
"license": "MIT",
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `@huaqiu/hardware-artifacts` — Hardware Engineering host capability.
|
|
3
3
|
*
|
|
4
|
-
* Provides `hardwareArtifacts`, mounts same-origin read/launch routes, and
|
|
4
|
+
* Provides `hardwareArtifacts`, mounts same-origin read/export/launch routes, and
|
|
5
5
|
* contributes a compact validated installation block to the agent prompt.
|
|
6
6
|
* Nothing here injects hqEdge, opens a shell, or mutates global process env.
|
|
7
7
|
*/
|
package/src/routes.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Read-only HTTP surface for the Hardware Engineering client. */
|
|
2
2
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
3
|
-
import
|
|
3
|
+
import { readFileSync } from 'node:fs'
|
|
4
|
+
import type { ArtifactKind, DiscoveryResult, HardwareArtifactService, NativeApplication } from './service.js'
|
|
4
5
|
|
|
5
6
|
export const HARDWARE_ROUTE_PREFIX = '/api/v1/hardware'
|
|
6
7
|
|
|
@@ -33,6 +34,20 @@ function asString(value: unknown): string | undefined {
|
|
|
33
34
|
return typeof value === 'string' && value.length > 0 ? value : undefined
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
function failure(error: unknown): { status: number; body: unknown } {
|
|
38
|
+
const code = typeof (error as { code?: unknown }).code === 'string' ? String((error as { code: string }).code) : 'internal'
|
|
39
|
+
const status = typeof (error as { status?: unknown }).status === 'number' ? Number((error as { status: number }).status) : 500
|
|
40
|
+
return { status, body: { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } } }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Roots a request may read: the caller's workspace, then the host's own. */
|
|
44
|
+
function rootsOf(body: unknown): string[] {
|
|
45
|
+
if (body === null || typeof body !== 'object') return []
|
|
46
|
+
const cwd = asString((body as Record<string, unknown>).cwd)
|
|
47
|
+
const workspace = asString((body as Record<string, unknown>).workspace)
|
|
48
|
+
return [cwd, workspace].filter((value): value is string => value !== undefined)
|
|
49
|
+
}
|
|
50
|
+
|
|
36
51
|
export function createHardwareHandler(service: HardwareArtifactService): (req: IncomingMessage, res: ServerResponse) => void {
|
|
37
52
|
let lastDiscovery: DiscoveryResult | undefined
|
|
38
53
|
return (req, res) => {
|
|
@@ -55,7 +70,92 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
55
70
|
return
|
|
56
71
|
}
|
|
57
72
|
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/environment`) {
|
|
58
|
-
sendJson(res, 200, { ok: true, text: service.environmentText(lastDiscovery), childEnv: service.childEnvironment() })
|
|
73
|
+
sendJson(res, 200, { ok: true, text: service.environmentText(lastDiscovery ?? service.discovery()), childEnv: service.childEnvironment() })
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/source`) {
|
|
77
|
+
const artifactPath = asString(url.searchParams.get('path'))
|
|
78
|
+
if (artifactPath === undefined) {
|
|
79
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const cwd = asString(url.searchParams.get('cwd') ?? undefined)
|
|
83
|
+
const revision = asString(url.searchParams.get('revision') ?? undefined)
|
|
84
|
+
try {
|
|
85
|
+
const source = await service.readSource({
|
|
86
|
+
artifactPath,
|
|
87
|
+
...(revision === undefined ? {} : { expectedRevision: revision }),
|
|
88
|
+
...(cwd === undefined ? {} : { roots: [cwd] }),
|
|
89
|
+
})
|
|
90
|
+
sendJson(res, 200, { ok: true, ...source })
|
|
91
|
+
} catch (error) {
|
|
92
|
+
const { status, body } = failure(error)
|
|
93
|
+
sendJson(res, status, body)
|
|
94
|
+
}
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/bom`) {
|
|
98
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
99
|
+
const artifactPath = asString(body.path)
|
|
100
|
+
if (artifactPath === undefined) {
|
|
101
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const bom = await service.exportBom({ artifactPath, roots: rootsOf(body) })
|
|
106
|
+
sendJson(res, 200, { ok: true, ...bom })
|
|
107
|
+
} catch (error) {
|
|
108
|
+
const { status, body } = failure(error)
|
|
109
|
+
sendJson(res, status, body)
|
|
110
|
+
}
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/glb`) {
|
|
114
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
115
|
+
const artifactPath = asString(body.path)
|
|
116
|
+
if (artifactPath === undefined) {
|
|
117
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const glb = await service.exportGlb({ artifactPath, roots: rootsOf(body) })
|
|
122
|
+
const query = new URLSearchParams({ path: glb.path, revision: glb.revision })
|
|
123
|
+
sendJson(res, 200, {
|
|
124
|
+
ok: true,
|
|
125
|
+
path: glb.path,
|
|
126
|
+
revision: glb.revision,
|
|
127
|
+
bytes: glb.bytes,
|
|
128
|
+
generatedAt: glb.generatedAt,
|
|
129
|
+
cached: glb.cached,
|
|
130
|
+
url: `/api/v1/hardware/glb-file?${query.toString()}`,
|
|
131
|
+
})
|
|
132
|
+
} catch (error) {
|
|
133
|
+
const { status, body } = failure(error)
|
|
134
|
+
sendJson(res, status, body)
|
|
135
|
+
}
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/glb-file`) {
|
|
139
|
+
const artifactPath = asString(url.searchParams.get('path'))
|
|
140
|
+
const revision = asString(url.searchParams.get('revision'))
|
|
141
|
+
if (artifactPath === undefined || revision === undefined) {
|
|
142
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path and revision are required' } })
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const file = service.glbFile(artifactPath, revision)
|
|
147
|
+
const bytes = readFileSync(file)
|
|
148
|
+
res.writeHead(200, {
|
|
149
|
+
'content-type': 'model/gltf-binary',
|
|
150
|
+
'content-length': bytes.byteLength,
|
|
151
|
+
'cache-control': 'no-store',
|
|
152
|
+
'x-huaqiu-source-revision': revision,
|
|
153
|
+
})
|
|
154
|
+
res.end(bytes)
|
|
155
|
+
} catch (error) {
|
|
156
|
+
const { status, body } = failure(error)
|
|
157
|
+
sendJson(res, status, body)
|
|
158
|
+
}
|
|
59
159
|
return
|
|
60
160
|
}
|
|
61
161
|
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/open`) {
|
|
@@ -63,8 +163,8 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
63
163
|
const application = asString(body.application)
|
|
64
164
|
const artifactPath = asString(body.artifactPath)
|
|
65
165
|
const kind = asString(body.kind)
|
|
66
|
-
if (application !== 'kicad'
|
|
67
|
-
sendJson(res, 400, {
|
|
166
|
+
if (application !== 'kicad') {
|
|
167
|
+
sendJson(res, 400, { error: { code: 'invalid-application', message: 'application must be kicad' }, ok: false })
|
|
68
168
|
return
|
|
69
169
|
}
|
|
70
170
|
if (artifactPath === undefined || kind === undefined) {
|
|
@@ -80,16 +180,15 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
80
180
|
})
|
|
81
181
|
sendJson(res, 200, { ok: true, ...opened })
|
|
82
182
|
} catch (error) {
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
sendJson(res, status, { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } })
|
|
183
|
+
const { status, body } = failure(error)
|
|
184
|
+
sendJson(res, status, body)
|
|
86
185
|
}
|
|
87
186
|
return
|
|
88
187
|
}
|
|
89
188
|
sendJson(res, 404, { ok: false, error: { code: 'not-found', message: 'unknown hardware route' } })
|
|
90
189
|
} catch (error) {
|
|
91
|
-
const
|
|
92
|
-
sendJson(res, status,
|
|
190
|
+
const { status, body } = failure(error)
|
|
191
|
+
sendJson(res, status, body)
|
|
93
192
|
}
|
|
94
193
|
})()
|
|
95
194
|
}
|