@huaqiu/hardware-artifacts 0.7.18 → 0.7.19
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/index.d.mts +61 -8
- package/lib/index.mjs +2610 -6
- package/lib/routes-D78ML_ef.d.mts +438 -0
- package/lib/routes.d.mts +2 -18
- package/lib/routes.mjs +156 -41
- package/lib/{service-CtVn7z8b.d.mts → service-BGecln85.d.mts} +119 -28
- package/lib/{service-Yrc2C_eo.mjs → service-Bo58tzXv.mjs} +570 -167
- package/lib/service.d.mts +1 -1
- package/lib/service.mjs +1 -1
- package/package.json +2 -1
- package/src/index.ts +124 -5
- package/src/order.ts +22 -0
- package/src/package.ts +282 -0
- package/src/routes.ts +127 -48
- package/src/service.ts +1635 -1651
- package/src/toolchain/dialog.ts +240 -0
- package/src/toolchain/discovery.ts +266 -0
- package/src/toolchain/index.ts +36 -0
- package/src/toolchain/install.ts +430 -0
- package/src/toolchain/lzma.ts +384 -0
- package/src/toolchain/manager.ts +594 -0
- package/src/toolchain/manifest.ts +234 -0
- package/src/toolchain/resolve.ts +325 -0
- package/src/toolchain/zip.ts +495 -0
- package/src/tools.ts +470 -0
package/src/tools.ts
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent tools for `@huaqiu/hardware-artifacts`.
|
|
3
|
+
*
|
|
4
|
+
* hardware_projects which projects/boards this workspace has, and
|
|
5
|
+
* whether a `kicad-cli` can export from them
|
|
6
|
+
* hardware_bom the grouped BOM of one root schematic
|
|
7
|
+
* hardware_fabricate gerber + drill (+ assembly inputs), validated
|
|
8
|
+
* hardware_place_pcb_order upload the package and enter the hqpcb order flow
|
|
9
|
+
* hardware_place_smt_order the same for assembly (board zip + BOM + positions)
|
|
10
|
+
*
|
|
11
|
+
* These are the file-based path to fabrication. They take a path — absolute
|
|
12
|
+
* anywhere on this machine, or relative to the session workspace — and run
|
|
13
|
+
* `kicad-cli` in the board's own directory — no HQ Edge, no open PCB editor, no
|
|
14
|
+
* live KiCad IPC, and no workspace authorization step: the user is the owner of
|
|
15
|
+
* the machine, and making them prove which directory their own board is in is
|
|
16
|
+
* how a working order turned into `403 artifact is outside the authorized
|
|
17
|
+
* workspace`.
|
|
18
|
+
*
|
|
19
|
+
* Every name is prefixed with this plugin's own `hardware_` namespace. The
|
|
20
|
+
* order tools deliberately do not reuse `@huaqiu/dsh-tool-quote`'s bare
|
|
21
|
+
* `place_pcb_order`/`place_smt_order`: that plugin drives the EDA host's own
|
|
22
|
+
* action, this one runs the file-based path, and two plugins registering one
|
|
23
|
+
* name would collide in the tool registry if a profile ever mounted both.
|
|
24
|
+
*
|
|
25
|
+
* The two order tools call the same `service.placeOrder` the panel's
|
|
26
|
+
* `/hardware/order/*` routes call, so this one package owns both the route and
|
|
27
|
+
* the agent tool for the same action instead of splitting the order backend
|
|
28
|
+
* across two plugins. Creating a purchase is a human decision, so the tools
|
|
29
|
+
* carry the route's own guard — a required `confirm: true` — and the service
|
|
30
|
+
* refuses a package that failed validation before anything is uploaded.
|
|
31
|
+
*
|
|
32
|
+
* Every tool answers with one `{ ok, ... }` JSON value. A prepared package that
|
|
33
|
+
* fails validation is still a successful call with `ok: false` and the issues:
|
|
34
|
+
* the export ran, the judgement is the finding, and the agent has to relay it
|
|
35
|
+
* rather than retry.
|
|
36
|
+
*
|
|
37
|
+
* @module
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
41
|
+
import { isAbsolute, resolve } from 'node:path'
|
|
42
|
+
import type { HardwareArtifactService, HardwareProject } from './service.js'
|
|
43
|
+
|
|
44
|
+
/** Structural alias of the DSH `JsonValue`. */
|
|
45
|
+
type Json = string | number | boolean | null | Json[] | { [key: string]: Json }
|
|
46
|
+
|
|
47
|
+
function asJson<T>(value: T): Json {
|
|
48
|
+
return JSON.parse(JSON.stringify(value)) as Json
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Execution identity a tool receives; only the fields used here are named. */
|
|
52
|
+
interface ToolExecLike {
|
|
53
|
+
readonly signal?: AbortSignal
|
|
54
|
+
readonly sessionId?: string
|
|
55
|
+
readonly session?: { readonly id?: string }
|
|
56
|
+
readonly context?: { readonly sessionId?: string }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface HardwareToolEnv {
|
|
60
|
+
readonly service: HardwareArtifactService
|
|
61
|
+
/**
|
|
62
|
+
* Base directory for a relative path: the DSH session's workspace.
|
|
63
|
+
*
|
|
64
|
+
* Only a base, never a limit. An absolute path is used exactly as given; this
|
|
65
|
+
* resolves the convenient relative form the panel and the model both use.
|
|
66
|
+
*/
|
|
67
|
+
readonly workspaceOf: (sessionId?: string) => Promise<string | undefined> | string | undefined
|
|
68
|
+
/** Used when the execution names no session (tests, a bare host). */
|
|
69
|
+
readonly defaultCwd?: () => string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The DSH session one execution belongs to, spelled however the host spells it. */
|
|
73
|
+
function sessionIdOf(exec: unknown): string | undefined {
|
|
74
|
+
const e = exec as ToolExecLike | undefined
|
|
75
|
+
return e?.sessionId ?? e?.session?.id ?? e?.context?.sessionId
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Resolve the scan root: the session workspace, then the process directory. */
|
|
79
|
+
async function workspaceOf(env: HardwareToolEnv, exec: unknown): Promise<string> {
|
|
80
|
+
const sessionId = sessionIdOf(exec)
|
|
81
|
+
const declared = await env.workspaceOf(sessionId)
|
|
82
|
+
if (declared !== undefined && declared.trim() !== '') return resolve(declared)
|
|
83
|
+
return resolve(env.defaultCwd?.() ?? process.cwd())
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Resolve a caller-supplied path: absolute as given, relative against the workspace. */
|
|
87
|
+
function resolveUnder(cwd: string, input: unknown): string {
|
|
88
|
+
const text = typeof input === 'string' ? input.trim() : ''
|
|
89
|
+
if (text === '') throw new Error('path is required')
|
|
90
|
+
return isAbsolute(text) ? resolve(text) : resolve(cwd, text)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Compact one project for the model: identity and paths, no per-artifact noise. */
|
|
94
|
+
function projectJson(project: HardwareProject, limit = 20): Json {
|
|
95
|
+
return {
|
|
96
|
+
name: project.name,
|
|
97
|
+
path: project.path,
|
|
98
|
+
...(project.defaultBoard === undefined ? {} : { defaultBoard: project.defaultBoard }),
|
|
99
|
+
...(project.rootSheet === undefined ? {} : { rootSheet: project.rootSheet.path }),
|
|
100
|
+
...(project.schematic === undefined ? {} : { schematic: project.schematic.path }),
|
|
101
|
+
boards: project.boards.slice(0, limit).map(board => board.path),
|
|
102
|
+
...(project.boards.length > limit ? { boardsTruncated: project.boards.length } : {}),
|
|
103
|
+
selectionRequired: project.selectionRequired,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function failure(error: unknown): Json {
|
|
108
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
109
|
+
const code = typeof (error as { code?: unknown }).code === 'string'
|
|
110
|
+
? String((error as { code: string }).code)
|
|
111
|
+
: 'internal'
|
|
112
|
+
const status = (error as { status?: unknown }).status
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
error: {
|
|
116
|
+
code,
|
|
117
|
+
message,
|
|
118
|
+
// The order path distinguishes a missing login (401) from a package the
|
|
119
|
+
// service refused (422), and the caller has to act on which it was.
|
|
120
|
+
...(typeof status === 'number' ? { status } : {}),
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The refusal the order routes and the order tools share. */
|
|
126
|
+
function confirmationRequired(kind: 'pcb' | 'assembly'): Json {
|
|
127
|
+
return {
|
|
128
|
+
ok: false,
|
|
129
|
+
error: {
|
|
130
|
+
code: 'confirmation-required',
|
|
131
|
+
message: kind === 'pcb'
|
|
132
|
+
? 'placing an order uploads the board files and requires explicit confirmation'
|
|
133
|
+
: 'placing an assembly order uploads the board, BOM and positions and requires explicit confirmation',
|
|
134
|
+
},
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Compact one placed order for the model: the flow's outcome, not the upload. */
|
|
139
|
+
function placedOrderJson(placed: {
|
|
140
|
+
readonly kind: 'pcb' | 'smt'
|
|
141
|
+
readonly orderUrl: string
|
|
142
|
+
readonly status: 'launched' | 'ready'
|
|
143
|
+
readonly message: string
|
|
144
|
+
readonly launched: boolean
|
|
145
|
+
readonly endpoint: string
|
|
146
|
+
readonly facts: unknown
|
|
147
|
+
readonly archive: unknown
|
|
148
|
+
readonly validation: unknown
|
|
149
|
+
readonly assembly?: unknown
|
|
150
|
+
}): Json {
|
|
151
|
+
return asJson({
|
|
152
|
+
ok: true,
|
|
153
|
+
kind: placed.kind,
|
|
154
|
+
orderUrl: placed.orderUrl,
|
|
155
|
+
status: placed.status,
|
|
156
|
+
message: placed.message,
|
|
157
|
+
launched: placed.launched,
|
|
158
|
+
endpoint: placed.endpoint,
|
|
159
|
+
facts: placed.facts,
|
|
160
|
+
archive: placed.archive,
|
|
161
|
+
validation: placed.validation,
|
|
162
|
+
...(placed.assembly === undefined ? {} : { assembly: placed.assembly }),
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function createHardwareTools(env: HardwareToolEnv) {
|
|
167
|
+
const { service } = env
|
|
168
|
+
|
|
169
|
+
return [
|
|
170
|
+
defineTool({
|
|
171
|
+
name: 'hardware_projects',
|
|
172
|
+
description:
|
|
173
|
+
'List the hardware projects and boards a directory contains, and whether a KiCad CLI is ' +
|
|
174
|
+
'available to export from them. Convenience only: every other tool takes a path directly, ' +
|
|
175
|
+
'so this is never a required first step. Use it when the user asks what ' +
|
|
176
|
+
'can be manufactured, and before hardware_fabricate when the board path is not already ' +
|
|
177
|
+
'known. Reads the workspace only — no KiCad process is started. Returns project paths, ' +
|
|
178
|
+
'their boards, the deterministic default board (only when ownership is unambiguous), and ' +
|
|
179
|
+
'the resolved kicad-cli path/version. An `issue.code` of "selection-required" means the ' +
|
|
180
|
+
'workspace holds several projects and the user must choose one; never pick for them.',
|
|
181
|
+
parameters: {
|
|
182
|
+
project: {
|
|
183
|
+
type: 'string' as const,
|
|
184
|
+
description:
|
|
185
|
+
'Project (.kicad_pro) path, absolute or relative to the workspace. Omit to discover ' +
|
|
186
|
+
'every project in the workspace.',
|
|
187
|
+
},
|
|
188
|
+
path: {
|
|
189
|
+
type: 'string' as const,
|
|
190
|
+
description: 'Workspace directory to scan. Defaults to the session working directory.',
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
output: { schema: { type: 'json' }, render: renderJson },
|
|
194
|
+
async execute(args: unknown, exec: ToolExecLike): Promise<Json> {
|
|
195
|
+
const a = (args ?? {}) as Record<string, unknown>
|
|
196
|
+
try {
|
|
197
|
+
const cwd = await workspaceOf(env, exec)
|
|
198
|
+
const base = a.path === undefined ? cwd : resolveUnder(cwd, a.path)
|
|
199
|
+
const sessionId = sessionIdOf(exec)
|
|
200
|
+
const discovery = service.discover(
|
|
201
|
+
base,
|
|
202
|
+
typeof a.project === 'string' ? a.project : undefined,
|
|
203
|
+
sessionId,
|
|
204
|
+
)
|
|
205
|
+
const installed = service.installations().kicad
|
|
206
|
+
return asJson({
|
|
207
|
+
ok: true,
|
|
208
|
+
cwd: discovery.cwd,
|
|
209
|
+
...(discovery.issue === undefined ? {} : { issue: discovery.issue }),
|
|
210
|
+
projects: discovery.projects.slice(0, 20).map(project => projectJson(project)),
|
|
211
|
+
...(discovery.projects.length > 20 ? { projectsTruncated: discovery.projects.length } : {}),
|
|
212
|
+
...(discovery.selected === undefined
|
|
213
|
+
? {}
|
|
214
|
+
: { selected: projectJson(discovery.selected) }),
|
|
215
|
+
kicad: {
|
|
216
|
+
configured: installed.configured,
|
|
217
|
+
...(installed.cli === undefined ? {} : { cli: installed.cli }),
|
|
218
|
+
...(installed.version === undefined ? {} : { version: installed.version }),
|
|
219
|
+
...(installed.error === undefined ? {} : { error: installed.error }),
|
|
220
|
+
},
|
|
221
|
+
})
|
|
222
|
+
} catch (error) {
|
|
223
|
+
return failure(error)
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
}),
|
|
227
|
+
defineTool({
|
|
228
|
+
name: 'hardware_bom',
|
|
229
|
+
description:
|
|
230
|
+
'Read the bill of materials of one KiCad root schematic, grouped by part. Uses KiCad\'s own ' +
|
|
231
|
+
'`kicad-cli sch export bom`, so the rows are what KiCad itself reports — references, value, ' +
|
|
232
|
+
'footprint, quantity and DNP, plus any custom fields (MPN, Manufacturer). Use when the user ' +
|
|
233
|
+
'asks what parts a board needs, for a cost estimate, or to sanity-check assembly. Requires ' +
|
|
234
|
+
'a configured KiCad CLI; returns its error otherwise.',
|
|
235
|
+
parameters: {
|
|
236
|
+
path: {
|
|
237
|
+
type: 'string' as const,
|
|
238
|
+
required: true as const,
|
|
239
|
+
description:
|
|
240
|
+
'Root schematic (.kicad_sch) path, absolute or relative to the workspace. A child ' +
|
|
241
|
+
'sheet returns that sheet only.',
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
output: { schema: { type: 'json' }, render: renderJson },
|
|
245
|
+
async execute(args: unknown, exec: ToolExecLike): Promise<Json> {
|
|
246
|
+
try {
|
|
247
|
+
const cwd = await workspaceOf(env, exec)
|
|
248
|
+
const artifactPath = resolveUnder(cwd, (args as Record<string, unknown> | undefined)?.path)
|
|
249
|
+
const sessionId = sessionIdOf(exec)
|
|
250
|
+
const bom = await service.exportBom({
|
|
251
|
+
artifactPath,
|
|
252
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
253
|
+
...(exec.signal === undefined ? {} : { signal: exec.signal }),
|
|
254
|
+
})
|
|
255
|
+
return asJson({
|
|
256
|
+
ok: true,
|
|
257
|
+
path: bom.path,
|
|
258
|
+
revision: bom.revision,
|
|
259
|
+
cached: bom.cached,
|
|
260
|
+
source: bom.source,
|
|
261
|
+
lines: bom.rows.length,
|
|
262
|
+
parts: bom.rows.reduce((total, row) => total + row.quantity, 0),
|
|
263
|
+
rows: bom.rows.map(row => ({
|
|
264
|
+
references: row.references,
|
|
265
|
+
quantity: row.quantity,
|
|
266
|
+
value: row.value,
|
|
267
|
+
footprint: row.footprint,
|
|
268
|
+
dnp: row.dnp,
|
|
269
|
+
fields: row.fields,
|
|
270
|
+
})),
|
|
271
|
+
})
|
|
272
|
+
} catch (error) {
|
|
273
|
+
return failure(error)
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
}),
|
|
277
|
+
defineTool({
|
|
278
|
+
name: 'hardware_fabricate',
|
|
279
|
+
description:
|
|
280
|
+
'Export and validate the fabrication package for one .kicad_pcb: gerbers, Excellon drill, ' +
|
|
281
|
+
'and — when a schematic is given — the assembly inputs (positions CSV + grouped BOM). Runs ' +
|
|
282
|
+
'`kicad-cli` in the board directory; no PCB editor needs to be open and no HQ Edge service ' +
|
|
283
|
+
'is involved. Returns the board facts read from the file (layers, outline size, thickness, ' +
|
|
284
|
+
'min track/hole, copper weight), the archive manifest, and a `validation` with `ok` plus ' +
|
|
285
|
+
'`errors`/`warnings`. `ok: false` means the package must not be ordered (for example a ' +
|
|
286
|
+
'copper layer that was not plotted, or no board outline): report the issues instead of ' +
|
|
287
|
+
'retrying. Pass `output_dir` when the user wants the files on disk.',
|
|
288
|
+
parameters: {
|
|
289
|
+
path: {
|
|
290
|
+
type: 'string' as const,
|
|
291
|
+
required: true as const,
|
|
292
|
+
description:
|
|
293
|
+
'Board (.kicad_pcb) path, absolute or relative to the workspace.',
|
|
294
|
+
},
|
|
295
|
+
schematic: {
|
|
296
|
+
type: 'string' as const,
|
|
297
|
+
description:
|
|
298
|
+
'Root schematic (.kicad_sch) to add assembly inputs (positions + BOM). Required for an ' +
|
|
299
|
+
'SMT order; omit for fabrication only.',
|
|
300
|
+
},
|
|
301
|
+
output_dir: {
|
|
302
|
+
type: 'string' as const,
|
|
303
|
+
description:
|
|
304
|
+
'Directory to write the package into (created when missing): `<board>.zip`, plus ' +
|
|
305
|
+
'positions/BOM CSVs when assembly inputs were produced. Omit to export and validate ' +
|
|
306
|
+
'without writing files.',
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
output: { schema: { type: 'json' }, render: renderJson },
|
|
310
|
+
async execute(args: unknown, exec: ToolExecLike): Promise<Json> {
|
|
311
|
+
const a = (args ?? {}) as Record<string, unknown>
|
|
312
|
+
try {
|
|
313
|
+
const cwd = await workspaceOf(env, exec)
|
|
314
|
+
const boardPath = resolveUnder(cwd, a.path)
|
|
315
|
+
const sessionId = sessionIdOf(exec)
|
|
316
|
+
const directory = a.output_dir === undefined ? undefined : resolveUnder(cwd, a.output_dir)
|
|
317
|
+
const prepared = await service.fabricate({
|
|
318
|
+
boardPath,
|
|
319
|
+
...(typeof a.schematic === 'string' ? { schematicPath: resolveUnder(cwd, a.schematic) } : {}),
|
|
320
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
321
|
+
...(exec.signal === undefined ? {} : { signal: exec.signal }),
|
|
322
|
+
})
|
|
323
|
+
const written = directory === undefined ? undefined : service.writePackage(prepared, directory)
|
|
324
|
+
return asJson({
|
|
325
|
+
ok: prepared.validation.ok,
|
|
326
|
+
boardPath: prepared.boardPath,
|
|
327
|
+
revision: prepared.revision,
|
|
328
|
+
generatedAt: prepared.generatedAt,
|
|
329
|
+
facts: prepared.facts,
|
|
330
|
+
archive: prepared.archive,
|
|
331
|
+
validation: prepared.validation,
|
|
332
|
+
...(prepared.assembly === undefined ? {} : { assembly: prepared.assembly }),
|
|
333
|
+
...(written === undefined ? {} : { written }),
|
|
334
|
+
})
|
|
335
|
+
} catch (error) {
|
|
336
|
+
return failure(error)
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
}),
|
|
340
|
+
defineTool({
|
|
341
|
+
name: 'hardware_place_pcb_order',
|
|
342
|
+
description:
|
|
343
|
+
'Place a bare-PCB (fabrication-only) order: fabricate and validate the package from one ' +
|
|
344
|
+
'.kicad_pcb, upload it to the configured Huaqiu order endpoint, and return the order page ' +
|
|
345
|
+
'URL — the same flow the Hardware panel\'s order action runs, through the same service. ' +
|
|
346
|
+
'Use only when the user has clearly asked to order/prototype this board ("下单", "place ' +
|
|
347
|
+
'order", "打样"): this uploads the board files and creates a real purchase, so `confirm` ' +
|
|
348
|
+
'must be true. Refused before any upload when the user is not signed in to Huaqiu ' +
|
|
349
|
+
'(error code "unauthorized") or when the package fails validation (error code ' +
|
|
350
|
+
'"package-invalid"); relay either instead of retrying. The order origin is this plugin\'s ' +
|
|
351
|
+
'configured setting (default eda.cn) — there is no region parameter. The PCB order uploads ' +
|
|
352
|
+
'the gerber/drill archive only; use hardware_place_smt_order when assembly inputs matter. ' +
|
|
353
|
+
'Returns { orderUrl, status, message, facts, archive, validation }. The DSH client opens ' +
|
|
354
|
+
'orderUrl in its own Browser tab as soon as this result arrives; do not paste the URL or ' +
|
|
355
|
+
'ask the user to click it. `status` is "ready" — the page is prepared, and nothing is ' +
|
|
356
|
+
'bought until the user finishes there.',
|
|
357
|
+
parameters: {
|
|
358
|
+
path: {
|
|
359
|
+
type: 'string' as const,
|
|
360
|
+
required: true as const,
|
|
361
|
+
description: 'Board (.kicad_pcb) path, absolute or relative to the workspace.',
|
|
362
|
+
},
|
|
363
|
+
confirm: {
|
|
364
|
+
type: 'boolean' as const,
|
|
365
|
+
required: true as const,
|
|
366
|
+
description:
|
|
367
|
+
'Must be true. The user\'s explicit request to order this board is the confirmation; ' +
|
|
368
|
+
'passing false (or omitting it) refuses the call without uploading anything.',
|
|
369
|
+
},
|
|
370
|
+
quantity: {
|
|
371
|
+
type: 'number' as const,
|
|
372
|
+
description: 'Board quantity for the order form. Omit to let the endpoint use its default.',
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
output: { schema: { type: 'json' }, render: renderJson },
|
|
376
|
+
async execute(args: unknown, exec: ToolExecLike): Promise<Json> {
|
|
377
|
+
const a = (args ?? {}) as Record<string, unknown>
|
|
378
|
+
try {
|
|
379
|
+
if (a.confirm !== true) return confirmationRequired('pcb')
|
|
380
|
+
const cwd = await workspaceOf(env, exec)
|
|
381
|
+
const sessionId = sessionIdOf(exec)
|
|
382
|
+
const placed = await service.placeOrder({
|
|
383
|
+
kind: 'pcb',
|
|
384
|
+
boardPath: resolveUnder(cwd, a.path),
|
|
385
|
+
...(typeof a.quantity === 'number' ? { quantity: a.quantity } : {}),
|
|
386
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
387
|
+
...(exec.signal === undefined ? {} : { signal: exec.signal }),
|
|
388
|
+
})
|
|
389
|
+
return placedOrderJson(placed)
|
|
390
|
+
} catch (error) {
|
|
391
|
+
return failure(error)
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
}),
|
|
395
|
+
defineTool({
|
|
396
|
+
name: 'hardware_place_smt_order',
|
|
397
|
+
description:
|
|
398
|
+
'Place an SMT assembly order: fabricate the package plus the assembly inputs (board zip, ' +
|
|
399
|
+
'component positions and BOM from the root schematic), upload them to the configured ' +
|
|
400
|
+
'Huaqiu order endpoint, and return the order page URL. Same flow, service and guards as ' +
|
|
401
|
+
'hardware_place_pcb_order, but `schematic` is required — without it there is nothing to assemble ' +
|
|
402
|
+
'from. Requires the user\'s explicit request to order assembly for this board, so ' +
|
|
403
|
+
'`confirm` must be true. Refused before any upload when the user is not signed in to ' +
|
|
404
|
+
'Huaqiu (error code "unauthorized") or when the package fails validation (error code ' +
|
|
405
|
+
'"package-invalid"). The order origin is this plugin\'s configured setting (default ' +
|
|
406
|
+
'eda.cn) — there is no region parameter.',
|
|
407
|
+
parameters: {
|
|
408
|
+
path: {
|
|
409
|
+
type: 'string' as const,
|
|
410
|
+
required: true as const,
|
|
411
|
+
description: 'Board (.kicad_pcb) path, absolute or relative to the workspace.',
|
|
412
|
+
},
|
|
413
|
+
schematic: {
|
|
414
|
+
type: 'string' as const,
|
|
415
|
+
required: true as const,
|
|
416
|
+
description:
|
|
417
|
+
'Root schematic (.kicad_sch) path for the BOM. Required: the assembly endpoint needs ' +
|
|
418
|
+
'the grouped BOM alongside the positions.',
|
|
419
|
+
},
|
|
420
|
+
confirm: {
|
|
421
|
+
type: 'boolean' as const,
|
|
422
|
+
required: true as const,
|
|
423
|
+
description:
|
|
424
|
+
'Must be true. The user\'s explicit request to order assembly for this board is the ' +
|
|
425
|
+
'confirmation; passing false (or omitting it) refuses the call without uploading ' +
|
|
426
|
+
'anything.',
|
|
427
|
+
},
|
|
428
|
+
quantity: {
|
|
429
|
+
type: 'number' as const,
|
|
430
|
+
description: 'Assembled quantity for the order form. Omit to let the endpoint use its default.',
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
output: { schema: { type: 'json' }, render: renderJson },
|
|
434
|
+
async execute(args: unknown, exec: ToolExecLike): Promise<Json> {
|
|
435
|
+
const a = (args ?? {}) as Record<string, unknown>
|
|
436
|
+
try {
|
|
437
|
+
if (a.confirm !== true) return confirmationRequired('assembly')
|
|
438
|
+
const cwd = await workspaceOf(env, exec)
|
|
439
|
+
const sessionId = sessionIdOf(exec)
|
|
440
|
+
const placed = await service.placeOrder({
|
|
441
|
+
kind: 'smt',
|
|
442
|
+
boardPath: resolveUnder(cwd, a.path),
|
|
443
|
+
schematicPath: resolveUnder(cwd, a.schematic),
|
|
444
|
+
...(typeof a.quantity === 'number' ? { quantity: a.quantity } : {}),
|
|
445
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
446
|
+
...(exec.signal === undefined ? {} : { signal: exec.signal }),
|
|
447
|
+
})
|
|
448
|
+
return placedOrderJson(placed)
|
|
449
|
+
} catch (error) {
|
|
450
|
+
return failure(error)
|
|
451
|
+
}
|
|
452
|
+
},
|
|
453
|
+
}),
|
|
454
|
+
]
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** The tool names this package registers; the bundle test asserts them. */
|
|
458
|
+
export function hardwareToolNames(): string[] {
|
|
459
|
+
return [
|
|
460
|
+
'hardware_projects',
|
|
461
|
+
'hardware_bom',
|
|
462
|
+
'hardware_fabricate',
|
|
463
|
+
'hardware_place_pcb_order',
|
|
464
|
+
'hardware_place_smt_order',
|
|
465
|
+
]
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function renderJson(_args: unknown, value: unknown) {
|
|
469
|
+
return [{ type: 'text' as const, text: JSON.stringify(value) }]
|
|
470
|
+
}
|