File size: 6,483 Bytes
e38a84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad6142
 
 
 
 
 
 
 
 
e38a84f
 
 
 
 
 
 
 
 
8ad6142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e38a84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env node

import path from "node:path";
import { fetchModelData, modelDisplayName, normalizeBaseUrl, probeServedModelId } from "./hf.js";
import { buildAgentArtifacts } from "./agents.js";
import { writeArtifactBundle } from "./fs.js";
import { buildRuntimeArtifacts, inferServedModelId, listSupportedRuntimes, resolveRuntime } from "./runtimes.js";

function usage() {
  console.log(`hf-launch

Usage:
  hf-launch inspect <org/model[:variant]>
  hf-launch scaffold <agent> <org/model[:variant]> [options]

Agents:
  pi | openclaw | opencode | codex | claude

Options for scaffold:
  --runtime <key>        Force a runtime (llama.cpp, vllm, sglang, tgi, mlx-lm, docker-model-runner, responses-gateway, anthropic-gateway)
  --base-url <url>       Override the runtime base URL
  --served-model <id>    Override the model id the agent should send to the backend
  --probe-model          Probe <base-url>/v1/models and use the first returned model id
  --out-dir <path>       Output directory (default: ./out/<agent>)
  --hf-token <token>     Hugging Face token for gated/private models
`);
}

function parseArgs(argv) {
  const positional = [];
  const options = {};

  for (let index = 0; index < argv.length; index += 1) {
    const token = argv[index];
    if (!token.startsWith("--")) {
      positional.push(token);
      continue;
    }

    const key = token.slice(2);
    const next = argv[index + 1];
    if (!next || next.startsWith("--")) {
      options[key] = true;
      continue;
    }

    options[key] = next;
    index += 1;
  }

  return { positional, options };
}

function extractFirstCommand(content) {
  return (
    String(content ?? "")
      .split(/\r?\n/)
      .map((line) => line.trim())
      .find((line) => line && !line.startsWith("#")) ?? null
  );
}

function printInspect(modelData, runtimes) {
  const lines = [
    `model: ${modelDisplayName(modelData)}`,
    `pipeline: ${modelData.pipeline_tag ?? "unknown"}`,
    `library: ${modelData.library_name ?? "unknown"}`,
    `tags: ${(modelData.tags ?? []).join(", ") || "none"}`,
    `gguf context: ${modelData.gguf?.context_length ?? "n/a"}`,
    `supported runtimes: ${runtimes.join(", ") || "none"}`
  ];

  if (runtimes.includes("llama.cpp")) {
    const runtimeArtifacts = buildRuntimeArtifacts(modelData, "llama.cpp");
    if (runtimeArtifacts.selectedGgufQuant) {
      lines.push(`llama.cpp quant: ${runtimeArtifacts.selectedGgufQuant}`);
    }
    if (runtimeArtifacts.selectedGgufPath) {
      lines.push(`llama.cpp file: ${runtimeArtifacts.selectedGgufPath}`);
    }
    const serverCommand = extractFirstCommand(runtimeArtifacts.cliCommands[0]?.content);
    if (serverCommand) {
      lines.push(`llama.cpp server: ${serverCommand}`);
    }
  }

  console.log(lines.join("\n"));
}

async function runInspect(modelRef, options) {
  const modelData = await fetchModelData(modelRef, { token: options["hf-token"] ?? process.env.HF_TOKEN });
  const runtimes = listSupportedRuntimes(modelData);
  printInspect(modelData, runtimes);
}

async function runScaffold(agent, modelRef, options) {
  const outDir = path.resolve(options["out-dir"] ?? path.join("out", agent));
  const modelData = await fetchModelData(modelRef, { token: options["hf-token"] ?? process.env.HF_TOKEN });
  const runtime = resolveRuntime(modelData, options.runtime);
  const runtimeArtifacts = buildRuntimeArtifacts(modelData, runtime.key);

  let baseUrl = normalizeBaseUrl(options["base-url"] ?? runtime.defaultBaseUrl);
  let servedModelId = options["served-model"];

  if (!servedModelId && options["probe-model"]) {
    if (!baseUrl) {
      throw new Error("--probe-model requires a base URL.");
    }
    servedModelId = await probeServedModelId(baseUrl);
  }

  servedModelId = servedModelId ?? inferServedModelId(runtime, modelData, null);
  const agentArtifacts = buildAgentArtifacts({
    agent,
    runtime,
    baseUrl,
    servedModelId,
    modelData
  });

  const warnings = [...runtimeArtifacts.warnings];
  if (servedModelId.startsWith("__")) {
    warnings.push(
      `The served model id is still "${servedModelId}". Start the runtime and rerun with --probe-model (or pass --served-model) before copying the generated config into place.`
    );
  }

  const manifest = {
    model: modelData.repoId,
    variant: modelData.variant,
    agent,
    runtime: runtime.key,
    apiFormat: runtime.apiFormat,
    baseUrl,
    servedModelId,
    targetFiles: agentArtifacts.files.map((artifact) => ({
      name: artifact.name,
      targetPath: artifact.targetPath
    })),
    warnings
  };

  const cliCommands = [...runtimeArtifacts.cliCommands, ...agentArtifacts.cliCommands];
  const artifacts = [
    {
      name: "runtime.md",
      targetPath: "n/a (generated reference)",
      content: runtimeArtifacts.notesMarkdown
    },
    ...agentArtifacts.files,
    {
      name: "manifest.json",
      targetPath: "n/a (generated metadata)",
      content: `${JSON.stringify(manifest, null, 2)}\n`
    }
  ];

  await writeArtifactBundle(outDir, artifacts);

  console.log(`wrote ${artifacts.length} files to ${outDir}`);
  console.log(`agent: ${agent}`);
  console.log(`model: ${modelData.repoId}${modelData.variant ? `:${modelData.variant}` : ""}`);
  console.log(`runtime: ${runtime.key}`);
  console.log(`api format: ${runtime.apiFormat}`);
  console.log(`base url: ${baseUrl ?? "not set"}`);
  console.log(`served model id: ${servedModelId}`);
  if (cliCommands.length > 0) {
    console.log("\ncommands:");
    for (const command of cliCommands) {
      console.log(`\n# ${command.title}`);
      console.log(command.content);
    }
  }
  if (warnings.length > 0) {
    console.log("\nwarnings:");
    for (const warning of warnings) {
      console.log(`- ${warning}`);
    }
  }
}

async function main() {
  const { positional, options } = parseArgs(process.argv.slice(2));
  const [command, arg1, arg2] = positional;

  if (!command || command === "--help" || command === "-h") {
    usage();
    return;
  }

  if (command === "inspect") {
    if (!arg1) {
      usage();
      process.exitCode = 1;
      return;
    }
    await runInspect(arg1, options);
    return;
  }

  if (command === "scaffold") {
    if (!arg1 || !arg2) {
      usage();
      process.exitCode = 1;
      return;
    }
    await runScaffold(arg1, arg2, options);
    return;
  }

  usage();
  process.exitCode = 1;
}

main().catch((error) => {
  console.error(error.message);
  process.exitCode = 1;
});