meshy-dsh 0.3.7 → 0.4.1

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/catalog.js ADDED
@@ -0,0 +1,124 @@
1
+ // What the bundled Meshy CLI says about itself (`meshy resources`): its per-endpoint commands, the
2
+ // kind of each (a `task` resource's `create` spends credits) and the verbs they share. The plugin
3
+ // reads it instead of keeping its own list, so a CLI release with a new or renamed command needs
4
+ // no change here.
5
+
6
+ /** A resource name as the CLI prints them (`image-to-3d`, `creative-lab.figure.build`). */
7
+ const NAME = /^[a-z0-9][a-z0-9-]*(?:\.[a-z0-9][a-z0-9-]*)*$/
8
+ /** A task id as the API issues them; never starts with "-", so it cannot read as an option. */
9
+ const TASK_ID = /^[A-Za-z0-9][A-Za-z0-9-]{7,63}$/
10
+
11
+ /** `meshy creative-lab figure build` → ['creative-lab', 'figure', 'build']. */
12
+ export function commandTokens(command) {
13
+ const parts = String(command ?? '').trim().split(/\s+/).filter(Boolean)
14
+ if (parts[0] === 'meshy' || parts[0] === 'meshy-cli') parts.shift()
15
+ return parts.every((p) => /^[a-z0-9][a-z0-9-]*$/.test(p)) ? parts : []
16
+ }
17
+
18
+ /** The CLI's listing, kept only where each entry has a usable name, kind and command. */
19
+ export function parseResources(envelope) {
20
+ const raw = envelope?.ok ? envelope.result?.items : null
21
+ if (!Array.isArray(raw)) return null
22
+ const items = []
23
+ for (const it of raw) {
24
+ const tokens = commandTokens(it?.command)
25
+ if (typeof it?.name !== 'string' || !NAME.test(it.name) || tokens.length === 0) continue
26
+ items.push({
27
+ name: it.name,
28
+ kind: typeof it.kind === 'string' ? it.kind : 'unknown',
29
+ tokens,
30
+ verbs: Array.isArray(it.verbs) ? it.verbs.filter((v) => typeof v === 'string') : [],
31
+ })
32
+ }
33
+ return items.length > 0 ? items : null
34
+ }
35
+
36
+ /**
37
+ * Whether one CLI command spends credits (`paid`, so the user approves it first) and whether it
38
+ * can run for minutes (`long`, so it runs as a background job). The rule follows the CLI's own
39
+ * contract: `make` (not with `--dry-run`), `create` on a `task` resource, and `api` with a method
40
+ * other than GET. `items` null (the listing could not be read) fails closed: any `create` counts
41
+ * as paid. Help never spends.
42
+ */
43
+ export function classify(argv, items) {
44
+ const help = argv.some((a) => a === '--help' || a === '-h') || argv[0] === 'help'
45
+ if (help) return { paid: false, long: false, resource: null, verb: null }
46
+ if (argv[0] === 'make') {
47
+ const dry = argv.some((a) => a === '--dry-run' || a === '--dry-run=true')
48
+ return { paid: !dry, long: !dry, resource: null, verb: null }
49
+ }
50
+ if (argv[0] === 'api') {
51
+ const method = typeof argv[1] === 'string' ? argv[1].toUpperCase() : ''
52
+ return { paid: method !== 'GET', long: false, resource: null, verb: null }
53
+ }
54
+ if (!items) {
55
+ const create = argv.includes('create')
56
+ return { paid: create, long: create || argv.includes('wait') || argv.includes('stream'), resource: null, verb: null }
57
+ }
58
+ let match = null
59
+ for (const it of items) {
60
+ if (it.tokens.length > argv.length) continue
61
+ if (!it.tokens.every((t, i) => argv[i] === t)) continue
62
+ if (!match || it.tokens.length > match.tokens.length) match = it
63
+ }
64
+ if (!match) return { paid: false, long: false, resource: null, verb: null }
65
+ const verb = argv[match.tokens.length] ?? null
66
+ const task = match.kind === 'task'
67
+ return {
68
+ paid: task && verb === 'create',
69
+ long: task && (verb === 'create' || verb === 'wait' || verb === 'stream'),
70
+ resource: match.name,
71
+ verb,
72
+ }
73
+ }
74
+
75
+ /**
76
+ * The listing read once through `cli(['resources'])` and kept for `ttlMs`; a failed read is
77
+ * retried on the next ask. `taskCommand` is the only way a resource name from a journal record,
78
+ * the asset index or a browser request becomes CLI arguments.
79
+ */
80
+ export function createCatalog({ cli, ttlMs = 30 * 60_000, now = Date.now }) {
81
+ let hit = null
82
+ let loading = null
83
+
84
+ async function items({ signal } = {}) {
85
+ if (hit && now() - hit.at < ttlMs) return hit.items
86
+ loading ??= (async () => {
87
+ try {
88
+ const parsed = parseResources(await cli(['resources'], { timeoutMs: 30_000, signal }))
89
+ if (parsed) hit = { at: now(), items: parsed }
90
+ return parsed ?? hit?.items ?? null
91
+ } catch {
92
+ return hit?.items ?? null
93
+ } finally {
94
+ loading = null
95
+ }
96
+ })()
97
+ return loading
98
+ }
99
+
100
+ /** The command of a `task` resource, or undefined for any other name. */
101
+ async function taskCommand(name, opts) {
102
+ if (typeof name !== 'string' || !NAME.test(name)) return undefined
103
+ const found = (await items(opts))?.find((it) => it.name === name && it.kind === 'task')
104
+ return found ? found.tokens : undefined
105
+ }
106
+
107
+ /** `[...command, verb, taskId]` for one task, or undefined when the resource or id is refused. */
108
+ async function taskArgs(name, verb, taskId, opts) {
109
+ if (typeof taskId !== 'string' || !TASK_ID.test(taskId)) return undefined
110
+ const tokens = await taskCommand(name, opts)
111
+ return tokens ? [...tokens, verb, taskId] : undefined
112
+ }
113
+
114
+ /** Task resources that support `list`. */
115
+ async function listable(opts) {
116
+ return new Set(((await items(opts)) ?? []).filter((it) => it.kind === 'task' && it.verbs.includes('list')).map((it) => it.name))
117
+ }
118
+
119
+ async function classifyArgs(argv, opts) {
120
+ return classify(argv, await items(opts))
121
+ }
122
+
123
+ return { items, taskCommand, taskArgs, listable, classify: classifyArgs }
124
+ }