cloudgpu-mcp 0.6.0 → 0.8.0

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/README.md CHANGED
@@ -10,6 +10,14 @@ It is a local (stdio) MCP server. It works with any MCP client that can start on
10
10
 
11
11
  ## Install
12
12
 
13
+ For the browser-approval flow, add this local MCP server **without** an API key first:
14
+
15
+ ```bash
16
+ claude mcp add cloudgpu -- npx -y cloudgpu-mcp
17
+ ```
18
+
19
+ For another stdio client, use `command: "npx"` and `args: ["-y", "cloudgpu-mcp"]`; omit the environment key. Ask the Agent to call `connect_account`, open its returned CloudGPU link yourself, approve the account, spending limit, GPU concurrency and expiry, then let it call `finish_connect`. Never paste your password or API key into the conversation. The client must support and configure MCP; the approval link alone does not install tools in an arbitrary assistant. Existing API-key setup remains available below.
20
+
13
21
  1. Create an API key at [cloudgpu.app/api](https://cloudgpu.app/api) and add a prepaid balance (card or USDT).
14
22
  2. Add the server to your client. Node.js 18 or newer is required. The key goes in the environment variable `CLOUDGPU_API_KEY`.
15
23
 
@@ -162,7 +170,7 @@ The server also provides a `train_on_cloudgpu` prompt.
162
170
 
163
171
  The key also works with the OpenAI-compatible API at `https://cloudgpu.app/v1`. Call `list_models` (or `GET /v1/models`) to see the available model IDs. Model prices are on [cloudgpu.app/api](https://cloudgpu.app/api).
164
172
 
165
- ## Optional persistent files and Workspaces (development)
173
+ ## Optional persistent files and Workspaces
166
174
 
167
175
  These tools require persistent storage to be explicitly enabled on the server;
168
176
  they return `storage_not_enabled` until it is ready. Discover server capabilities
@@ -261,3 +269,29 @@ MIT, see [LICENSE](LICENSE).
261
269
  Use `list_workspace_instances`, then `get_workspace_application` with the saved `version_id` and the selected `instance_id`. After the user agrees, call `apply_workspace` with `user_confirmed: true`, then poll status. The files appear in a new private project directory; existing files are never overwritten. This does not install dependencies, execute project code or start, stop, or restart a GPU. Rental billing continues during transfer. Budgeted Agent keys can only target instances created with the same key. Workspace requires a funded account.
262
270
 
263
271
  Repeated application of the same version to the same instance returns the existing result, including user edits after success. On `needs_attention`, `failed` or `conflict`, inspect the result rather than automatically retrying. A new saved version creates a separate directory. The feature must be enabled by the server; unavailable storage does not disable ordinary GPU rental or Job tools.
272
+
273
+ ### D13 model preparation (0.8.0)
274
+
275
+ Version 0.8.0 adds three preparation tools; check both the installed tools and live backend capabilities before using them:
276
+
277
+ - `resolve_model_reference`: resolve a public Hugging Face branch/tag and explicit filenames to a commit-addressed manifest, sizes and available LFS SHA-256. No weights are hosted by CloudGPU. To enable verified use of the instance’s existing mirror, up to 8 selected JSON configuration files of at most 64 KiB each may be read from the pinned origin and SHA-256 hashed, within a shared 15-second deadline. Weight files are not fetched by this resolver. Private/gated models are refused.
278
+ - `prepare_job_rerun`: read parameters from a successful owned Job. No new GPU, old quote or old idempotency key. Obtain a fresh quote and explicit budget approval before `run_job`.
279
+ - `prepare_model_deployment`: prepare a setup script for the **existing ComfyUI template**, with direct model downloads and optional UI-format workflow JSON. Use the model name as a category (e.g. `checkpoints`, `loras`, `vae`). Review and approve the script before deploying. Custom nodes are not installed and workflows are not executed automatically. This is preparation support, not a real-GPU-validated template catalog.
280
+
281
+ `run_job` accepts optional `model_references`. A supporting backend is checked before submission; an old backend cannot silently ignore the references. Models are downloaded after Workspace restoration, into `$CLOUDGPU_MODEL_DIR/<name>/<path>`. Download time consumes the same rental budget. File size/checksum, disk and download timeout failures stop the Job and follow the existing release-evidence flow, without provisioning retries. The weights disappear with the GPU unless the user separately persists them; no R2 model cache is added.
282
+
283
+ Agent-first interaction: ask your Agent to run the project again; no separate web Job console or model JSON form is required. Team-authorized keys spend the selected team wallet; personal funds are separate.
284
+
285
+ Suggested workflow: resolve references → review model license/files and compatible environment → get quote → obtain explicit budget/time/GPU approval → run once with a stable idempotency key → poll status/cost → prepare a rerun only after success. Output collection remains the user's command's responsibility.
286
+
287
+ See [`examples/pinned-model-job.mjs`](examples/pinned-model-job.mjs) for a default **dry-run** REST example. Do not enable submission during routine tests. Bounded internal acceptance covered actual MCP, candidate HTTP authentication/MySQL, independent model hashes, two GPU runs and release. A separate ComfyUI image accepted model links and a workflow file. This does not validate arbitrary custom nodes or graph execution.
288
+
289
+ ## Small Job output snapshots and diagnosis (0.7.0)
290
+
291
+ This branch adds `diagnose_job` (read-only) and `save_job_output` (explicit storage write). Check the installed tool list before using them; they are not a claim about the currently published npm release.
292
+
293
+ `save_job_output` stores only the ended Job output tail as a small Artifact. It is not a full log, file collector, disk snapshot or automatic backup. Known credential patterns and queried URLs are refused. Review output for other sensitive data before approving storage. Existing eligibility, quota, retention and explicit paid-storage consent apply; the GPU Job budget does not authorize paid storage. No GPU is created, kept alive or stopped by these tools. Matching snapshots are verified and reused when found; concurrent different client processes may still create duplicate artifacts.
294
+
295
+ `diagnose_job` reports the error category, platform instance status and whether platform charges are final. It does not prove supplier release, infer that command failures are always user errors, or automatically rerun work. `needs_attention` requires resource reconciliation before a new task. A rerun requires a fresh quote, explicit approval and a new idempotency key.
296
+
297
+ Registry configuration allows discovery and connect-link approval without pre-entering a key. A tool-enabled MCP client is still required; a link does not install a client or grant unlimited spending. No new fixed infrastructure or model storage is introduced.
@@ -0,0 +1,22 @@
1
+ // Dry-run by default. No dependencies beyond Node and this repository's REST client.
2
+ // Example: CLOUDGPU_API_KEY=... node examples/pinned-model-job.mjs org/model main config.json
3
+ // To submit, explicitly set CGPU_SUBMIT=YES plus CGPU_GPU, CGPU_BUDGET_USD, CGPU_MAX_MINUTES,
4
+ // CGPU_COMMAND and CGPU_JOB_KEY. Reuse the SAME key only to retry this SAME submission.
5
+ import {resolveModelReference} from '../src/model-references.js';
6
+ import {CloudGpuApi} from '../src/api.js';
7
+ const [repo_id,revision='main',...files]=process.argv.slice(2);
8
+ if(!repo_id||!files.length)throw new Error('Provide repository, revision and explicit model files');
9
+ const model=await resolveModelReference({repo_id,revision,name:'model',files});
10
+ if(process.env.CGPU_SUBMIT!=='YES'){
11
+ console.log(JSON.stringify({model_references:[model],next:'Dry run only. Choose the full model file set, compatible GPU image, command, budget and time limit before explicit submission. Files will be in $CLOUDGPU_MODEL_DIR/model. No GPU created.'},null,2));
12
+ }else{
13
+ const {CGPU_GPU:gpu_model,CGPU_BUDGET_USD:budget,CGPU_MAX_MINUTES:minutes,CGPU_COMMAND:command,CGPU_JOB_KEY:key}=process.env;
14
+ if(!gpu_model||!command||!key||!(Number(budget)>0)||!Number.isInteger(Number(minutes))||Number(minutes)<1)throw new Error('Explicit GPU, command, new submission key, positive budget and integer minutes are required');
15
+ const api=new CloudGpuApi({apiKey:process.env.CLOUDGPU_API_KEY});
16
+ const quote=await api.quote({requirements:{gpu_model,gpu_count:1},estimated_duration_seconds:Number(minutes)*60,max_budget_usd:Number(budget)});
17
+ const body={gpu_model,gpu_count:1,command,max_minutes:Number(minutes),max_spend_usd:Number(budget),quote_id:quote.id,model_references:[model]};
18
+ if(!body.quote_id)throw new Error('Quote did not return quote_id; no Job submitted');
19
+ if(process.env.CGPU_WORKSPACE_VERSION)body.workspace_version_id=process.env.CGPU_WORKSPACE_VERSION;
20
+ const job=await api.createJob(body,key);
21
+ console.log(JSON.stringify({job_id:job.id,status:job.status,next:'Poll get_job until terminal; inspect release evidence and cost. To run again, prepare_job_rerun, obtain a new quote/approval, then use a NEW submission key. Output is not automatically persisted.'},null,2));
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudgpu-mcp",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "mcpName": "io.github.mrhy7176/cloudgpu-mcp",
5
5
  "description": "MCP server and command-line tool for cloudgpu.app: let Claude Code, Cursor, Cline, opencode, goose and other MCP clients (or you, from a terminal) rent a GPU per minute, run a job, fetch the results and shut it down, with spending limits the platform enforces.",
6
6
  "type": "module",
@@ -13,7 +13,8 @@
13
13
  "src",
14
14
  "rules",
15
15
  "README.md",
16
- "LICENSE"
16
+ "LICENSE",
17
+ "examples"
17
18
  ],
18
19
  "engines": {
19
20
  "node": ">=18.3"
package/src/api.js CHANGED
@@ -106,8 +106,16 @@ export class CloudGpuApi {
106
106
  // ── jobs: start a GPU, run one command, destroy the GPU (server-side) ──
107
107
  createJob(body, idempotencyKey) {
108
108
  if (!idempotencyKey) throw new Error("createJob needs an idempotency key");
109
+ if (body.model_references?.length) return this.createModelJob(body,idempotencyKey);
109
110
  return this.request("POST", "/v1/jobs", body, { idempotencyKey });
110
111
  }
112
+ async createModelJob(body,idempotencyKey) {
113
+ const caps=await this.request("GET","/v1/public/compute-capabilities",undefined,{noAuth:true});
114
+ if(caps.model_references!==true) throw new ApiError(503,"model_references_unavailable","Backend has not enabled pinned model loading; no Job was submitted.");
115
+ return this.request("POST", "/v1/jobs", body, { idempotencyKey });
116
+ }
117
+ prepareModelDeployment(body) { return this.request("POST", "/v1/jobs/prepare-model-deployment", body); }
118
+ jobRerunSpec(id) { return this.request("GET", `/v1/jobs/${encodeURIComponent(id)}/rerun-spec`); }
111
119
  job(id) {
112
120
  return this.request("GET", `/v1/jobs/${encodeURIComponent(id)}`);
113
121
  }
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import {resolveModelReference} from "./model-references.js";
2
3
  // CloudGPU MCP server (stdio). Lets an AI coding assistant rent a GPU on cloudgpu.app, run a job,
3
4
  // fetch the results and shut the GPU down.
4
5
  //
@@ -19,6 +20,7 @@ import { SshPool, exec, upload, download, writeFile } from "./ssh.js";
19
20
  import { loadStoredKey, saveStoredKey } from "./credentials.js";
20
21
  import crypto from "node:crypto";
21
22
  import { registerStorageTools } from "./storage.js";
23
+ import { registerJobResultTools } from "./job-results.js";
22
24
  import {
23
25
  checkCommand,
24
26
  sensitiveReason,
@@ -30,7 +32,7 @@ import {
30
32
  shq,
31
33
  } from "./guards.js";
32
34
 
33
- const VERSION = "0.6.0";
35
+ const VERSION = "0.8.0";
34
36
  const HERE = path.dirname(fileURLToPath(import.meta.url));
35
37
  const RULES = fs.readFileSync(path.join(HERE, "..", "rules", "cloudgpu.md"), "utf8");
36
38
 
@@ -64,6 +66,7 @@ The same API key also works for the OpenAI-compatible model API at ${cfg.baseUrl
64
66
 
65
67
  const server = new McpServer({ name: "cloudgpu", version: VERSION }, { instructions: INSTRUCTIONS });
66
68
  registerStorageTools(server, { api, wrap, ok, fail, readOnly: cfg.readOnly, expandHome });
69
+ registerJobResultTools(server, { api, wrap, ok, readOnly: cfg.readOnly });
67
70
 
68
71
  // ── helpers ──────────────────────────────────────────────────────────────
69
72
 
@@ -480,6 +483,21 @@ if (!cfg.readOnly) {
480
483
  }),
481
484
  );
482
485
 
486
+ server.registerTool("resolve_model_reference", {
487
+ title:"Pin a public model reference", description:"Read-only. Resolve a public Hugging Face model revision and explicit file list to immutable commit, sizes and available LFS SHA-256 checksums. Does not download weights, rent a GPU or authenticate to Hugging Face. Review model license and application compatibility separately.",
488
+ inputSchema:{repo_id:z.string(),revision:z.string().default("main"),name:z.string(),files:z.array(z.string()).min(1).max(32)},
489
+ annotations:{readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:true},
490
+ },wrap(async (input)=>ok(await resolveModelReference(input))));
491
+ server.registerTool("prepare_model_deployment", {
492
+ title:"Prepare models and a ComfyUI workflow",description:"Preparation only; no GPU is started. Create a reviewed setup script for the existing ComfyUI template. Downloads go from the public model host directly to the rented GPU, never through CloudGPU storage. Use model names as categories (checkpoints, loras, vae, diffusion_models, text_encoders, clip, controlnet, upscale_models). Does not install custom nodes, execute workflows, accept licenses for the user, or promise compatibility. Pass the setup_script only after explicit approval to the existing deployment flow.",
493
+ inputSchema:{model_references:z.array(z.record(z.unknown())).min(1).max(4),workflow:z.record(z.unknown()).optional()},
494
+ annotations:{readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:false},
495
+ },wrap(async (input)=>ok(await api.prepareModelDeployment(input))));
496
+ server.registerTool("prepare_job_rerun", {
497
+ title:"Prepare a successful job for rerun",description:"Read-only. Retrieve the original command, pinned model references and Workspace without starting anything. Request a fresh quote, check Workspace expiry and obtain explicit budget approval before calling run_job with a NEW idempotency key. Never reuse the old quote or key.",
498
+ inputSchema:{job_id:z.string()},annotations:{readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:true},
499
+ },wrap(async ({job_id})=>ok(await api.jobRerunSpec(job_id))));
500
+
483
501
  server.registerTool(
484
502
  "run_job",
485
503
  {
@@ -496,6 +514,7 @@ if (!cfg.readOnly) {
496
514
  gpu_count: z.number().int().min(1).max(8).default(1),
497
515
  disk_gb: z.number().int().optional().describe("Larger system disk: 100, 200 or 500; omit for the standard disk"),
498
516
  workspace_version_id: z.string().optional().describe("Immutable Workspace version to restore before the command; requires enabled persistent storage"),
517
+ model_references: z.array(z.object({name:z.string(),repo_id:z.string(),revision:z.string().regex(/^[0-9a-f]{40}$/),files:z.array(z.object({path:z.string(),size_bytes:z.number().int().positive(),sha256:z.string().regex(/^[0-9a-f]{64}$/).optional()})).min(1).max(32)})).max(4).optional().describe("Pinned public model references from resolve_model_reference. Downloads into $CLOUDGPU_MODEL_DIR/<name> before the command; download time consumes the same GPU budget. No R2 cache or remote repository code execution."),
499
518
  quote_id: z.string().optional().describe("Server quote from quote_compute, starting quote_; binds configuration and checks price at submission"),
500
519
  max_spend_usd: z.number().positive().describe(`Budget for this job in USD (at most ${cfg.maxSpend})`),
501
520
  max_hours: z.number().positive().describe(`Hard time limit in hours (at most ${cfg.maxHours})`),
@@ -504,7 +523,7 @@ if (!cfg.readOnly) {
504
523
  },
505
524
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: true },
506
525
  },
507
- wrap(async ({ command, gpu_model, template, gpu_count = 1, disk_gb, workspace_version_id, quote_id, max_spend_usd, max_hours, user_confirmed, idempotency_key }) => {
526
+ wrap(async ({ command, gpu_model, template, gpu_count = 1, disk_gb, workspace_version_id, model_references, quote_id, max_spend_usd, max_hours, user_confirmed, idempotency_key }) => {
508
527
  if (user_confirmed !== true) {
509
528
  return fail("Not started: tell the user the GPU, budget and time limit, wait for an explicit yes, then call again with user_confirmed=true.");
510
529
  }
@@ -523,6 +542,7 @@ if (!cfg.readOnly) {
523
542
  if (template) body.template = template;
524
543
  if (disk_gb) body.disk_gb = disk_gb;
525
544
  if (workspace_version_id) body.workspace_version_id = workspace_version_id;
545
+ if (model_references?.length) body.model_references = model_references;
526
546
  if (quote_id) body.quote_id = quote_id;
527
547
  const job = await api.createJob(body, key);
528
548
  return ok({
@@ -772,5 +792,5 @@ process.on("SIGINT", bye);
772
792
  process.on("SIGTERM", bye);
773
793
  process.stdin.on("close", bye);
774
794
  if (!cfg.apiKey) {
775
- console.error("cloudgpu-mcp: CLOUDGPU_API_KEY is not set; tools will return an error until it is. Create a key at https://cloudgpu.app/api");
795
+ console.error("cloudgpu-mcp: no saved key. Public discovery is available; use connect_account and finish_connect for owner-approved access.");
776
796
  }
@@ -0,0 +1,54 @@
1
+ // 轻量 D14:只保存已结束 Job 的有限输出,不访问 GPU、不增加 GPU 等待时间。
2
+ import fs from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import {createHash} from 'node:crypto';
6
+ import {z} from 'zod';
7
+ import {PersistentStorage} from './storage.js';
8
+ const terminal=new Set(['succeeded','failed','cancelled']);
9
+ const idSchema=z.string().regex(/^job_[a-f0-9]{32}$/);
10
+ const locks=new Map();
11
+ function assertId(id){if(!/^job_[a-f0-9]{32}$/.test(id))throw Error('Invalid Job ID.');}
12
+ export function checkedTail(text,apiKey){
13
+ if(typeof text!=='string'||Buffer.byteLength(text)>16384)throw Error('Job output tail exceeds the bounded snapshot limit.');
14
+ const secrets=[/-----BEGIN [^-]*PRIVATE KEY-----/,/\bsk-[A-Za-z0-9_-]{16,}/,/\b(?:password|secret|token|api[_-]?key|authorization)\s*[:=]\s*["']?[^\s"',}]+/i,/https?:\/\/[^\s]+\?[^\s]+/i];
15
+ if((apiKey&&apiKey.length>=4&&text.includes(apiKey))||secrets.some(re=>re.test(text)))throw Error('Output may contain credentials or a signed URL. Sanitize it before saving; nothing was uploaded.');
16
+ return text;
17
+ }
18
+ export async function diagnoseJob(api,id){
19
+ assertId(id);const job=await api.job(id);const code=job.error?.code||null;
20
+ let observed='not_checked';if(job.instance_id){try{const r=await api.instance(job.instance_id);observed=String(r.status);}catch{observed='unavailable';}}
21
+ const groups={model_load_failed:'model_preparation',workspace_restore_failed:'workspace_restore',workspace_unavailable:'workspace_restore',command_failed:'command_execution',instance_unreachable:'connectivity',command_lost:'execution_lost',insufficient_balance:'funding',cannot_create:'provisioning'};
22
+ let category=job.status==='needs_attention'?'release_uncertainty':groups[code]||(job.status==='succeeded'?'completed':'inspect_job');
23
+ return {job_id:job.id,status:job.status,category,error_code:code,observed_instance_status:observed,
24
+ supplier_release_verified:false,cost_usd:job.cost_usd,cost_status:job.cost_status,
25
+ cost_note:job.cost_status==='final'?'Final platform Job charge.':'Unsettled; may include preauthorisation and refunds not yet applied.',
26
+ automatic_retry_allowed:false,next:category==='release_uncertainty'?'Stop and ask support to reconcile resources; do not create another GPU.':job.status==='succeeded'?'Reuse the Workspace and parameters only after a fresh quote and explicit budget approval.':'Inspect the error and remaining resources. Do not blame user code or rerun automatically; obtain a fresh quote and approval for a new Job.'};
27
+ }
28
+ async function saveOne(api,storage,id,acceptCharges){
29
+ const job=await api.job(id);if(!terminal.has(job.status))throw Error('Wait until the Job has ended; no running GPU will be changed.');
30
+ const logs=await api.jobLogs(id);const tail=checkedTail(logs.tail??job.output_tail??'',api.apiKey);
31
+ const content=JSON.stringify({object:'job_output_tail',job_id:job.id,status:job.status,tail_only:true,complete_output:false,output_tail:tail},null,2)+'\n';
32
+ const sha=createHash('sha256').update(content).digest('hex');const name=`${id}-${sha.slice(0,16)}.json`;
33
+ // 同一客户端串行保存并核验已有内容。跨进程不承诺exactly-once。
34
+ const files=(await api.request('GET','/v1/files')).data||[];
35
+ const existing=files.find(f=>f.name===name&&f.purpose==='artifact'&&f.status==='ready'&&Number(f.size_bytes)===Buffer.byteLength(content));
36
+ const dir=await fs.mkdtemp(path.join(os.tmpdir(),'cgpu-job-output-'));
37
+ try{
38
+ const local=path.join(dir,'output.json');
39
+ if(existing){await storage.download(existing.file_id,local);if(createHash('sha256').update(await fs.readFile(local)).digest('hex')!==sha)throw Error('Existing snapshot does not match; inspect it before saving another.');return {file_id:existing.file_id,reused:true,tail_only:true,complete_output:false,sha256:sha};}
40
+ await fs.writeFile(local,content,{mode:0o600});const saved=await storage.upload(local,'artifact',name,acceptCharges===true);
41
+ return {...saved,reused:false,tail_only:true,complete_output:false,sha256:sha};
42
+ }finally{await fs.rm(dir,{recursive:true,force:true});}
43
+ }
44
+ export async function saveJobOutput(api,storage,id,confirmed,acceptCharges=false){
45
+ assertId(id);if(confirmed!==true)throw Error('Ask the user before storing Job output.');
46
+ const previous=locks.get(id)||Promise.resolve();const current=previous.catch(()=>{}).then(()=>saveOne(api,storage,id,acceptCharges));locks.set(id,current);
47
+ try{return await current;}finally{if(locks.get(id)===current)locks.delete(id);}
48
+ }
49
+ export function registerJobResultTools(server,{api,wrap,ok,readOnly}){
50
+ const storage=new PersistentStorage(api);
51
+ server.registerTool('diagnose_job',{title:'Inspect Job failure and cost state',description:'Read-only Job diagnosis and observed platform instance state. Never creates, stops or retries a GPU. Does not claim supplier release verification or classify every command failure as user error.',inputSchema:{job_id:idSchema},annotations:{readOnlyHint:true,openWorldHint:true}},wrap(async({job_id})=>ok(await diagnoseJob(api,job_id))));
52
+ if(readOnly)return;
53
+ server.registerTool('save_job_output',{title:'Save ended Job output tail',description:'With approval, persist the ended Job log tail as a small Artifact using existing storage quota and retention. This is not the full log or binary output collection. Refuses recognizable credentials and queried URLs. Never touches GPU resources. Reuses a matching verified snapshot when found; cross-client concurrent calls may create duplicates.',inputSchema:{job_id:idSchema,user_confirmed:z.boolean(),accept_storage_charges:z.boolean().default(false)},annotations:{readOnlyHint:false,destructiveHint:false,idempotentHint:false,openWorldHint:true}},wrap(async({job_id,user_confirmed,accept_storage_charges})=>ok(await saveJobOutput(api,storage,job_id,user_confirmed,accept_storage_charges))));
54
+ }
@@ -0,0 +1,44 @@
1
+ // 只向 Hugging Face 的固定公开 API 解析引用,不携带 CloudGPU key 或用户 token。
2
+ import {createHash} from 'node:crypto';
3
+ export async function resolveModelReference({repo_id, revision = "main", name, files}, fetchImpl = globalThis.fetch) {
4
+ if (!/^[A-Za-z0-9][A-Za-z0-9_.-]{0,95}\/[A-Za-z0-9][A-Za-z0-9_.-]{0,95}$/.test(repo_id)
5
+ || !/^[A-Za-z0-9][A-Za-z0-9_.-]{0,119}$/.test(revision)
6
+ || !/^[A-Za-z][A-Za-z0-9_-]{0,47}$/.test(name)
7
+ || !Array.isArray(files) || !files.length || files.length > 32 || new Set(files).size !== files.length
8
+ || files.some(p => typeof p !== "string" || p.length > 240 || !/^[A-Za-z0-9_][A-Za-z0-9_./-]*$/.test(p) || p.split("/").some(s => !s || s === "." || s === ".."))) throw new Error("Invalid public model reference");
9
+ const url = `https://huggingface.co/api/models/${repo_id}/revision/${encodeURIComponent(revision)}?blobs=true`;
10
+ const response = await fetchImpl(url, {headers:{"User-Agent":"CloudGPU-model-reference/1"}, redirect:"error", signal:AbortSignal.timeout(15000)});
11
+ if (!response.ok) throw new Error("Public model metadata unavailable; check repository, revision and public access");
12
+ const reader=response.body.getReader();const chunks=[];let bytes=0;
13
+ try {while(true){const r=await reader.read();if(r.done)break;bytes+=r.value.byteLength;if(bytes>2*1024*1024)throw new Error("Model metadata exceeds limit; use a smaller repository or supply a pinned manifest");chunks.push(r.value);}}
14
+ finally {await reader.cancel();}
15
+ let data;try{data=JSON.parse(Buffer.concat(chunks).toString("utf8"));}catch{throw new Error("Invalid public model metadata");}
16
+ if (data.private || data.gated) throw new Error("Private or gated models are not supported; select a public model without an access gate");
17
+ if (!/^[0-9a-f]{40}$/.test(data.sha)) throw new Error("Model revision could not be pinned");
18
+ const selected=files.map(path=>{
19
+ const file=data.siblings?.find(f=>f.rfilename===path);const size=file?.lfs?.size ?? file?.size;
20
+ if(!Number.isSafeInteger(size)||size<1)throw new Error("Requested file metadata missing or empty");
21
+ const sha=file?.lfs?.sha256;
22
+ if(sha!==undefined&&!/^[0-9a-f]{64}$/.test(sha))throw new Error("Invalid file checksum metadata");
23
+ return {path,size_bytes:size,...(sha?{sha256:sha}:{})};
24
+ });
25
+ if(selected.reduce((n,f)=>n+f.size_bytes,0)>100_000_000_000)throw new Error("Model exceeds the first-version 100 GB job limit");
26
+ // Git-backed config files have no LFS SHA. Hash bounded public JSON metadata
27
+ // on the origin so the GPU can verify it even when using its existing mirror.
28
+ const configs=selected.filter(f=>!f.sha256&&f.path.endsWith('.json')&&f.size_bytes<=65536);
29
+ if(configs.length>8)throw new Error('Too many unhashed JSON metadata files; supply an independently verified manifest');
30
+ const deadline=Date.now()+15000;
31
+ for(const file of configs){
32
+ const remaining=deadline-Date.now();
33
+ if(remaining<=0)throw new Error('Model config metadata deadline exceeded');
34
+ const config=await fetchImpl(`https://huggingface.co/${repo_id}/raw/${data.sha}/${file.path.split('/').map(encodeURIComponent).join('/')}`,
35
+ {headers:{'User-Agent':'CloudGPU-model-reference/1'},redirect:'error',signal:AbortSignal.timeout(remaining)});
36
+ if(!config.ok)throw new Error('Public model config metadata unavailable');
37
+ const stream=config.body.getReader();const content=[];let size=0;
38
+ try{while(true){const r=await stream.read();if(r.done)break;size+=r.value.byteLength;if(size>file.size_bytes)throw new Error('Model config metadata size mismatch');content.push(r.value);}}
39
+ finally{await stream.cancel();}
40
+ if(size!==file.size_bytes)throw new Error('Model config metadata size mismatch');
41
+ file.sha256=createHash('sha256').update(Buffer.concat(content)).digest('hex');
42
+ }
43
+ return {name,repo_id,revision:data.sha,files:selected};
44
+ }