@huaqiu/hardware-artifacts 0.0.0-stage → 0.7.12
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/LICENSE +21 -0
- package/README.md +10 -2
- package/cordis.patch.yml +10 -0
- package/lib/index.d.mts +27 -0
- package/lib/index.mjs +32 -0
- package/lib/routes.d.mts +7 -0
- package/lib/routes.mjs +139 -0
- package/lib/service.d.mts +99 -0
- package/lib/service.mjs +377 -0
- package/package.json +51 -3
- package/src/index.ts +83 -0
- package/src/routes.ts +96 -0
- package/src/service.ts +423 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 深圳华秋智联股份有限公司
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @huaqiu/hardware-artifacts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Hardware Engineering host capability. It is a local Cordis plugin, not a daemon and not an HQ Edge dependency.
|
|
4
|
+
|
|
5
|
+
- Deterministically discovers one selected `.kicad_pro`, its schematic hierarchy, boards and mechanical models. Multiple projects/boards require explicit selection; the package never silently takes the first match.
|
|
6
|
+
- Resolves configured KiCad and SolidCraft installations from user-scoped settings. KiCad CLI and GUI editors are resolved from one installation root; SolidCraft stays disabled when unconfigured.
|
|
7
|
+
- Launches the exact displayed artifact with explicit `argv`, the artifact's parent as cwd, no shell, and a detached process.
|
|
8
|
+
- Exposes a compact `<hardware_environment>` prompt context and `HUAQIU_*` child-process environment values without mutating global `process.env`.
|
|
9
|
+
- Serves read-only same-origin routes under `/api/v1/hardware`.
|
|
10
|
+
|
|
11
|
+
Native KiCad files remain authoritative. The service stores path pointers only.
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# dsh bundle patch: Hardware Engineering host capability.
|
|
2
|
+
#
|
|
3
|
+
# The row waits on the local web server and the prompt-context service it
|
|
4
|
+
# contributes to. Filesystem, settings and subprocess access stay defensive.
|
|
5
|
+
# No row here injects hqEdge: installation resolution and native launch are
|
|
6
|
+
# local and shell-free by construction.
|
|
7
|
+
- insert:
|
|
8
|
+
- id: hardware-artifacts
|
|
9
|
+
name: '@huaqiu/hardware-artifacts'
|
|
10
|
+
inject: ['webServer', 'systemPrompt']
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ArtifactKind, DiscoveryResult, HardwareArtifact, HardwareArtifactService, HardwareArtifactsOptions, HardwareProject, InstallationInfo, Installations, NativeApplication, OpenArtifactRequest, OpenArtifactResult } from "./service.mjs";
|
|
2
|
+
import { HARDWARE_ROUTE_PREFIX, createHardwareHandler } from "./routes.mjs";
|
|
3
|
+
import Schema from "@deepseek-ai/schemastery";
|
|
4
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
declare const name = "@huaqiu/hardware-artifacts";
|
|
7
|
+
declare const inject: readonly ["webServer", "systemPrompt"];
|
|
8
|
+
/** User-scoped installation settings persisted by DSH's host-backed config. */
|
|
9
|
+
declare const Config: Schema<Schemastery.ObjectS<NoInfer<{
|
|
10
|
+
kicadRoot: Schema<string, string, "defined">;
|
|
11
|
+
solidcraftRoot: Schema<string, string, "defined">;
|
|
12
|
+
}>>, Schemastery.ObjectT<NoInfer<{
|
|
13
|
+
kicadRoot: Schema<string, string, "defined">;
|
|
14
|
+
solidcraftRoot: Schema<string, string, "defined">;
|
|
15
|
+
}>>, "plain">;
|
|
16
|
+
interface HardwareArtifactsPluginConfig {
|
|
17
|
+
readonly kicadRoot?: string;
|
|
18
|
+
readonly solidcraftRoot?: string;
|
|
19
|
+
}
|
|
20
|
+
declare module '@deepseek-ai/cordis' {
|
|
21
|
+
interface Context {
|
|
22
|
+
hardwareArtifacts: HardwareArtifactService;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
declare function apply(ctx: Context, config?: HardwareArtifactsPluginConfig): void;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { type ArtifactKind, Config, type DiscoveryResult, HARDWARE_ROUTE_PREFIX, type HardwareArtifact, HardwareArtifactService, type HardwareArtifactsOptions, HardwareArtifactsPluginConfig, type HardwareProject, type InstallationInfo, type Installations, type NativeApplication, type OpenArtifactRequest, type OpenArtifactResult, apply, createHardwareHandler, inject, name };
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { HARDWARE_ROUTE_PREFIX, createHardwareHandler } from "./routes.mjs";
|
|
2
|
+
import { HardwareArtifactService } from "./service.mjs";
|
|
3
|
+
import Schema from "@deepseek-ai/schemastery";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const name = "@huaqiu/hardware-artifacts";
|
|
6
|
+
const inject = ["webServer", "systemPrompt"];
|
|
7
|
+
/** User-scoped installation settings persisted by DSH's host-backed config. */
|
|
8
|
+
const Config = Schema.object({
|
|
9
|
+
kicadRoot: Schema.string().default(""),
|
|
10
|
+
solidcraftRoot: Schema.string().default("")
|
|
11
|
+
});
|
|
12
|
+
function apply(ctx, config = {}) {
|
|
13
|
+
const service = new HardwareArtifactService({
|
|
14
|
+
...config.kicadRoot ? { kicadRoot: config.kicadRoot } : {},
|
|
15
|
+
...config.solidcraftRoot ? { solidcraftRoot: config.solidcraftRoot } : {}
|
|
16
|
+
});
|
|
17
|
+
ctx.effect(() => ctx.provide("hardwareArtifacts", service));
|
|
18
|
+
if (ctx.webServer !== void 0) ctx.effect(() => ctx.webServer.register({
|
|
19
|
+
kind: "prefix",
|
|
20
|
+
path: HARDWARE_ROUTE_PREFIX,
|
|
21
|
+
handler: createHardwareHandler(service)
|
|
22
|
+
}), "hardware-artifacts: local read/launch routes");
|
|
23
|
+
const prompt = ctx.systemPrompt;
|
|
24
|
+
if (prompt !== void 0 && typeof prompt.context === "function") ctx.effect(() => prompt.context({
|
|
25
|
+
name: "hardware-environment",
|
|
26
|
+
order: 42,
|
|
27
|
+
interpolate: false,
|
|
28
|
+
text: () => service.environmentText()
|
|
29
|
+
}), "hardware-artifacts: compact agent environment");
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { Config, HARDWARE_ROUTE_PREFIX, HardwareArtifactService, apply, createHardwareHandler, inject, name };
|
package/lib/routes.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HardwareArtifactService } from "./service.mjs";
|
|
2
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
//#region src/routes.d.ts
|
|
4
|
+
declare const HARDWARE_ROUTE_PREFIX = "/api/v1/hardware";
|
|
5
|
+
declare function createHardwareHandler(service: HardwareArtifactService): (req: IncomingMessage, res: ServerResponse) => void;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { HARDWARE_ROUTE_PREFIX, createHardwareHandler };
|
package/lib/routes.mjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
//#region src/routes.ts
|
|
2
|
+
const HARDWARE_ROUTE_PREFIX = "/api/v1/hardware";
|
|
3
|
+
function sendJson(res, status, body) {
|
|
4
|
+
const text = JSON.stringify(body);
|
|
5
|
+
res.writeHead(status, {
|
|
6
|
+
"content-type": "application/json; charset=utf-8",
|
|
7
|
+
"content-length": Buffer.byteLength(text)
|
|
8
|
+
});
|
|
9
|
+
res.end(text);
|
|
10
|
+
}
|
|
11
|
+
function isTrustedRequest(req) {
|
|
12
|
+
const host = String(req.headers.host ?? "");
|
|
13
|
+
if (!/^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(host)) return false;
|
|
14
|
+
const origin = req.headers.origin;
|
|
15
|
+
return origin === void 0 || /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(String(origin));
|
|
16
|
+
}
|
|
17
|
+
async function readJson(req, maxBytes = 65536) {
|
|
18
|
+
const chunks = [];
|
|
19
|
+
let size = 0;
|
|
20
|
+
for await (const chunk of req) {
|
|
21
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
22
|
+
size += buffer.byteLength;
|
|
23
|
+
if (size > maxBytes) throw Object.assign(/* @__PURE__ */ new Error("request body too large"), { status: 413 });
|
|
24
|
+
chunks.push(buffer);
|
|
25
|
+
}
|
|
26
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
27
|
+
}
|
|
28
|
+
function asString(value) {
|
|
29
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
30
|
+
}
|
|
31
|
+
function createHardwareHandler(service) {
|
|
32
|
+
let lastDiscovery;
|
|
33
|
+
return (req, res) => {
|
|
34
|
+
if (!isTrustedRequest(req)) {
|
|
35
|
+
sendJson(res, 403, {
|
|
36
|
+
ok: false,
|
|
37
|
+
error: {
|
|
38
|
+
code: "forbidden",
|
|
39
|
+
message: "forbidden"
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
(async () => {
|
|
45
|
+
try {
|
|
46
|
+
const url = new URL(req.url ?? "/", "http://dsh.internal");
|
|
47
|
+
const p = url.pathname;
|
|
48
|
+
if (req.method === "GET" && p === `/api/v1/hardware/projects`) {
|
|
49
|
+
const result = service.discover(asString(url.searchParams.get("cwd") ?? void 0) ?? process.cwd(), asString(url.searchParams.get("project") ?? void 0));
|
|
50
|
+
lastDiscovery = result;
|
|
51
|
+
sendJson(res, 200, {
|
|
52
|
+
ok: true,
|
|
53
|
+
...result
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (req.method === "GET" && p === `/api/v1/hardware/installations`) {
|
|
58
|
+
sendJson(res, 200, {
|
|
59
|
+
ok: true,
|
|
60
|
+
...service.installations()
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (req.method === "GET" && p === `/api/v1/hardware/environment`) {
|
|
65
|
+
sendJson(res, 200, {
|
|
66
|
+
ok: true,
|
|
67
|
+
text: service.environmentText(lastDiscovery),
|
|
68
|
+
childEnv: service.childEnvironment()
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (req.method === "POST" && p === `/api/v1/hardware/open`) {
|
|
73
|
+
const body = await readJson(req);
|
|
74
|
+
const application = asString(body.application);
|
|
75
|
+
const artifactPath = asString(body.artifactPath);
|
|
76
|
+
const kind = asString(body.kind);
|
|
77
|
+
if (application !== "kicad" && application !== "solidcraft") {
|
|
78
|
+
sendJson(res, 400, {
|
|
79
|
+
ok: false,
|
|
80
|
+
error: {
|
|
81
|
+
code: "invalid-application",
|
|
82
|
+
message: "application must be kicad or solidcraft"
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (artifactPath === void 0 || kind === void 0) {
|
|
88
|
+
sendJson(res, 400, {
|
|
89
|
+
ok: false,
|
|
90
|
+
error: {
|
|
91
|
+
code: "invalid-request",
|
|
92
|
+
message: "artifactPath and kind are required"
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
sendJson(res, 200, {
|
|
99
|
+
ok: true,
|
|
100
|
+
...service.open({
|
|
101
|
+
application,
|
|
102
|
+
artifactPath,
|
|
103
|
+
kind,
|
|
104
|
+
...asString(body.projectPath) === void 0 ? {} : { projectPath: asString(body.projectPath) }
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
} catch (error) {
|
|
108
|
+
const code = typeof error.code === "string" ? String(error.code) : "launch-failed";
|
|
109
|
+
sendJson(res, typeof error.status === "number" ? Number(error.status) : 500, {
|
|
110
|
+
ok: false,
|
|
111
|
+
error: {
|
|
112
|
+
code,
|
|
113
|
+
message: error instanceof Error ? error.message : String(error)
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
sendJson(res, 404, {
|
|
120
|
+
ok: false,
|
|
121
|
+
error: {
|
|
122
|
+
code: "not-found",
|
|
123
|
+
message: "unknown hardware route"
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
} catch (error) {
|
|
127
|
+
sendJson(res, typeof error.status === "number" ? Number(error.status) : 500, {
|
|
128
|
+
ok: false,
|
|
129
|
+
error: {
|
|
130
|
+
code: "internal",
|
|
131
|
+
message: error instanceof Error ? error.message : String(error)
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
})();
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { HARDWARE_ROUTE_PREFIX, createHardwareHandler };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { ChildProcess } from "node:child_process";
|
|
2
|
+
//#region src/service.d.ts
|
|
3
|
+
type ArtifactKind = 'schematic' | 'pcb' | 'pcb-3d' | 'bom' | 'mechanical-3d';
|
|
4
|
+
type NativeApplication = 'kicad' | 'solidcraft';
|
|
5
|
+
interface HardwareArtifact {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
readonly kind: ArtifactKind;
|
|
8
|
+
readonly path: string;
|
|
9
|
+
readonly projectPath?: string;
|
|
10
|
+
/** BOM keeps its schematic; PCB 3D keeps its board; sheets keep their root. */
|
|
11
|
+
readonly sourcePath?: string;
|
|
12
|
+
readonly title: string;
|
|
13
|
+
readonly format: string;
|
|
14
|
+
}
|
|
15
|
+
interface HardwareProject {
|
|
16
|
+
readonly path: string;
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly schematic?: HardwareArtifact;
|
|
19
|
+
/** Semantic views; both keep the identity of their source artifact. */
|
|
20
|
+
readonly bom?: HardwareArtifact;
|
|
21
|
+
readonly pcb3d?: HardwareArtifact;
|
|
22
|
+
readonly sheets: readonly HardwareArtifact[];
|
|
23
|
+
readonly boards: readonly HardwareArtifact[];
|
|
24
|
+
readonly mechanical: readonly HardwareArtifact[];
|
|
25
|
+
readonly selectionRequired: boolean;
|
|
26
|
+
}
|
|
27
|
+
interface DiscoveryResult {
|
|
28
|
+
readonly cwd: string;
|
|
29
|
+
readonly manifestPath?: string;
|
|
30
|
+
readonly projects: readonly HardwareProject[];
|
|
31
|
+
readonly selected?: HardwareProject;
|
|
32
|
+
readonly issue?: {
|
|
33
|
+
readonly code: string;
|
|
34
|
+
readonly message: string;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
interface InstallationInfo {
|
|
38
|
+
readonly configured: boolean;
|
|
39
|
+
readonly root?: string;
|
|
40
|
+
readonly cli?: string;
|
|
41
|
+
readonly projectManager?: string;
|
|
42
|
+
readonly pcbEditor?: string;
|
|
43
|
+
readonly schematicEditor?: string;
|
|
44
|
+
readonly executable?: string;
|
|
45
|
+
readonly version?: string;
|
|
46
|
+
readonly error?: string;
|
|
47
|
+
}
|
|
48
|
+
interface Installations {
|
|
49
|
+
readonly kicad: InstallationInfo;
|
|
50
|
+
readonly solidcraft: InstallationInfo;
|
|
51
|
+
}
|
|
52
|
+
interface OpenArtifactRequest {
|
|
53
|
+
readonly application: NativeApplication;
|
|
54
|
+
readonly artifactPath: string;
|
|
55
|
+
readonly kind: ArtifactKind;
|
|
56
|
+
readonly projectPath?: string;
|
|
57
|
+
}
|
|
58
|
+
interface OpenArtifactResult {
|
|
59
|
+
readonly launched: true;
|
|
60
|
+
readonly command: string;
|
|
61
|
+
readonly args: readonly string[];
|
|
62
|
+
readonly cwd: string;
|
|
63
|
+
readonly pid?: number;
|
|
64
|
+
}
|
|
65
|
+
interface HardwareArtifactsOptions {
|
|
66
|
+
readonly kicadRoot?: string;
|
|
67
|
+
readonly solidcraftRoot?: string;
|
|
68
|
+
readonly probeVersion?: (executable: string) => string | undefined;
|
|
69
|
+
readonly spawnProcess?: (command: string, args: readonly string[], options: {
|
|
70
|
+
cwd: string;
|
|
71
|
+
detached: boolean;
|
|
72
|
+
stdio: 'ignore';
|
|
73
|
+
windowsHide: boolean;
|
|
74
|
+
shell: false;
|
|
75
|
+
}) => Pick<ChildProcess, 'pid'>;
|
|
76
|
+
}
|
|
77
|
+
/** Small, immutable host capability implementation. */
|
|
78
|
+
declare class HardwareArtifactService {
|
|
79
|
+
private readonly config;
|
|
80
|
+
private readonly options;
|
|
81
|
+
constructor(config?: HardwareArtifactsOptions);
|
|
82
|
+
/** Resolve configured installations from validated absolute paths only. */
|
|
83
|
+
installations(): Installations;
|
|
84
|
+
/** Paths passed to agent child processes. Never assign these globally. */
|
|
85
|
+
childEnvironment(): Record<string, string>;
|
|
86
|
+
/** Compact agent context: validated paths only, no project inventory dump. */
|
|
87
|
+
environmentText(current?: DiscoveryResult): string;
|
|
88
|
+
/**
|
|
89
|
+
* Deterministic discovery. An explicit project or manifest wins; otherwise
|
|
90
|
+
* exactly one `.kicad_pro` may be selected automatically. Ambiguity is
|
|
91
|
+
* returned as a selection requirement, never resolved by taking the first.
|
|
92
|
+
*/
|
|
93
|
+
discover(cwdInput: string, explicitProject?: string): DiscoveryResult;
|
|
94
|
+
private projectFrom;
|
|
95
|
+
/** Validate and launch a native application without a shell. */
|
|
96
|
+
open(request: OpenArtifactRequest): OpenArtifactResult;
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
export { ArtifactKind, DiscoveryResult, HardwareArtifact, HardwareArtifactService, HardwareArtifactsOptions, HardwareProject, InstallationInfo, Installations, NativeApplication, OpenArtifactRequest, OpenArtifactResult };
|