@huaqiu/hardware-artifacts 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.
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@huaqiu/hardware-artifacts",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.7.13",
4
+ "type": "module",
5
+ "description": "Hardware Engineering host capability: deterministic artifact discovery, installation resolution, native application launch and compact agent context.",
6
+ "license": "MIT",
7
+ "main": "./lib/index.mjs",
8
+ "types": "./lib/index.d.mts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./lib/index.d.mts",
12
+ "default": "./lib/index.mjs"
13
+ },
14
+ "./cordis.patch.yml": "./cordis.patch.yml",
15
+ "./package.json": "./package.json"
16
+ },
17
+ "dsh": {
18
+ "bundle": {
19
+ "patch": "./cordis.patch.yml"
20
+ }
21
+ },
22
+ "engines": {
23
+ "dsh": "^0.2.2-rc.1"
24
+ },
25
+ "dependencies": {
26
+ "@deepseek-ai/schemastery": "^3.18.1"
27
+ },
28
+ "peerDependencies": {
29
+ "@deepseek-ai/cordis": "^4.0.1",
30
+ "@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.2"
31
+ },
32
+ "devDependencies": {
33
+ "@deepseek-ai/cordis": "^4.0.2",
34
+ "@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.2",
35
+ "@types/node": "^24.0.0",
36
+ "tsdown": "^0.22.14",
37
+ "typescript": "^5.9.0",
38
+ "vitest": "^4.1.0"
39
+ },
40
+ "files": [
41
+ "lib",
42
+ "src",
43
+ "cordis.patch.yml",
44
+ "README.md"
45
+ ],
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "typecheck": "tsc --noEmit",
51
+ "build": "tsdown",
52
+ "test": "vitest run"
53
+ }
6
54
  }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * `@huaqiu/hardware-artifacts` — Hardware Engineering host capability.
3
+ *
4
+ * Provides `hardwareArtifacts`, mounts same-origin read/export/launch routes, and
5
+ * contributes a compact validated installation block to the agent prompt.
6
+ * Nothing here injects hqEdge, opens a shell, or mutates global process env.
7
+ */
8
+ import type { Context } from '@deepseek-ai/cordis'
9
+ import type {} from '@deepseek-ai/dsh-host-webserver'
10
+ import Schema from '@deepseek-ai/schemastery'
11
+ import { createHardwareHandler, HARDWARE_ROUTE_PREFIX } from './routes.js'
12
+ import { HardwareArtifactService } from './service.js'
13
+
14
+ export type {
15
+ ArtifactKind,
16
+ DiscoveryResult,
17
+ HardwareArtifact,
18
+ HardwareProject,
19
+ HardwareArtifactsOptions,
20
+ Installations,
21
+ InstallationInfo,
22
+ NativeApplication,
23
+ OpenArtifactRequest,
24
+ OpenArtifactResult,
25
+ } from './service.js'
26
+ export { HardwareArtifactService } from './service.js'
27
+ export { createHardwareHandler, HARDWARE_ROUTE_PREFIX } from './routes.js'
28
+
29
+ export const name = '@huaqiu/hardware-artifacts'
30
+ export const inject = ['webServer', 'systemPrompt'] as const
31
+
32
+ /** User-scoped installation settings persisted by DSH's host-backed config. */
33
+ export const Config = Schema.object({
34
+ kicadRoot: Schema.string().default(''),
35
+ solidcraftRoot: Schema.string().default(''),
36
+ })
37
+
38
+ export interface HardwareArtifactsPluginConfig {
39
+ readonly kicadRoot?: string
40
+ readonly solidcraftRoot?: string
41
+ }
42
+
43
+ declare module '@deepseek-ai/cordis' {
44
+ interface Context {
45
+ hardwareArtifacts: HardwareArtifactService
46
+ }
47
+ }
48
+
49
+ interface SystemPromptFace {
50
+ context(entry: {
51
+ name: string
52
+ order: number
53
+ text: string | (() => string)
54
+ interpolate?: boolean
55
+ required?: boolean
56
+ }): () => void
57
+ }
58
+
59
+ export function apply(ctx: Context, config: HardwareArtifactsPluginConfig = {}): void {
60
+ const service = new HardwareArtifactService({
61
+ ...(config.kicadRoot ? { kicadRoot: config.kicadRoot } : {}),
62
+ ...(config.solidcraftRoot ? { solidcraftRoot: config.solidcraftRoot } : {}),
63
+ })
64
+ ctx.effect(() => ctx.provide('hardwareArtifacts', service))
65
+
66
+ if (ctx.webServer !== undefined) {
67
+ ctx.effect(() => ctx.webServer.register({
68
+ kind: 'prefix',
69
+ path: HARDWARE_ROUTE_PREFIX,
70
+ handler: createHardwareHandler(service),
71
+ }), 'hardware-artifacts: local read/launch routes')
72
+ }
73
+
74
+ const prompt = (ctx as Context & { systemPrompt?: SystemPromptFace }).systemPrompt
75
+ if (prompt !== undefined && typeof prompt.context === 'function') {
76
+ ctx.effect(() => prompt.context({
77
+ name: 'hardware-environment',
78
+ order: 42,
79
+ interpolate: false,
80
+ text: () => service.environmentText(),
81
+ }), 'hardware-artifacts: compact agent environment')
82
+ }
83
+ }
package/src/routes.ts ADDED
@@ -0,0 +1,195 @@
1
+ /** Read-only HTTP surface for the Hardware Engineering client. */
2
+ import type { IncomingMessage, ServerResponse } from 'node:http'
3
+ import { readFileSync } from 'node:fs'
4
+ import type { ArtifactKind, DiscoveryResult, HardwareArtifactService, NativeApplication } from './service.js'
5
+
6
+ export const HARDWARE_ROUTE_PREFIX = '/api/v1/hardware'
7
+
8
+ function sendJson(res: ServerResponse, status: number, body: unknown): void {
9
+ const text = JSON.stringify(body)
10
+ res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(text) })
11
+ res.end(text)
12
+ }
13
+
14
+ function isTrustedRequest(req: IncomingMessage): boolean {
15
+ const host = String(req.headers.host ?? '')
16
+ if (!/^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(host)) return false
17
+ const origin = req.headers.origin
18
+ return origin === undefined || /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(String(origin))
19
+ }
20
+
21
+ async function readJson(req: IncomingMessage, maxBytes = 64 * 1024): Promise<unknown> {
22
+ const chunks: Buffer[] = []
23
+ let size = 0
24
+ for await (const chunk of req) {
25
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as string)
26
+ size += buffer.byteLength
27
+ if (size > maxBytes) throw Object.assign(new Error('request body too large'), { status: 413 })
28
+ chunks.push(buffer)
29
+ }
30
+ return JSON.parse(Buffer.concat(chunks).toString('utf8'))
31
+ }
32
+
33
+ function asString(value: unknown): string | undefined {
34
+ return typeof value === 'string' && value.length > 0 ? value : undefined
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
+
51
+ export function createHardwareHandler(service: HardwareArtifactService): (req: IncomingMessage, res: ServerResponse) => void {
52
+ let lastDiscovery: DiscoveryResult | undefined
53
+ return (req, res) => {
54
+ if (!isTrustedRequest(req)) {
55
+ sendJson(res, 403, { ok: false, error: { code: 'forbidden', message: 'forbidden' } })
56
+ return
57
+ }
58
+ void (async () => {
59
+ try {
60
+ const url = new URL(req.url ?? '/', 'http://dsh.internal')
61
+ const p = url.pathname
62
+ if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/projects`) {
63
+ const result = service.discover(asString(url.searchParams.get('cwd') ?? undefined) ?? process.cwd(), asString(url.searchParams.get('project') ?? undefined))
64
+ lastDiscovery = result
65
+ sendJson(res, 200, { ok: true, ...result })
66
+ return
67
+ }
68
+ if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/installations`) {
69
+ sendJson(res, 200, { ok: true, ...service.installations() })
70
+ return
71
+ }
72
+ if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/environment`) {
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
+ }
159
+ return
160
+ }
161
+ if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/open`) {
162
+ const body = await readJson(req) as Record<string, unknown>
163
+ const application = asString(body.application)
164
+ const artifactPath = asString(body.artifactPath)
165
+ const kind = asString(body.kind)
166
+ if (application !== 'kicad') {
167
+ sendJson(res, 400, { error: { code: 'invalid-application', message: 'application must be kicad' }, ok: false })
168
+ return
169
+ }
170
+ if (artifactPath === undefined || kind === undefined) {
171
+ sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'artifactPath and kind are required' } })
172
+ return
173
+ }
174
+ try {
175
+ const opened = service.open({
176
+ application: application as NativeApplication,
177
+ artifactPath,
178
+ kind: kind as ArtifactKind,
179
+ ...(asString(body.projectPath) === undefined ? {} : { projectPath: asString(body.projectPath) as string }),
180
+ })
181
+ sendJson(res, 200, { ok: true, ...opened })
182
+ } catch (error) {
183
+ const { status, body } = failure(error)
184
+ sendJson(res, status, body)
185
+ }
186
+ return
187
+ }
188
+ sendJson(res, 404, { ok: false, error: { code: 'not-found', message: 'unknown hardware route' } })
189
+ } catch (error) {
190
+ const { status, body } = failure(error)
191
+ sendJson(res, status, body)
192
+ }
193
+ })()
194
+ }
195
+ }