@ariacompute/engine-ts 1.15.1 → 1.17.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
@@ -24,10 +24,12 @@ and then loads it. Dashboard is not used. A Dashboard `sk-` / `bfvk-` token is
24
24
  ignored for hub auth. Token is optional for public models. A valid cached bundle
25
25
  is reused without re-downloading.
26
26
 
27
- Gated files: pass `hfToken` (`.com`) or `modelscopeApiToken` (`.cn`) — same keys as
28
- `aria-engine auth`. If omitted, the SDK reads `~/.ariacompute/config.yml`.
27
+ Gated files: call `eng.setup({ hf_token })` (`.com`) or `eng.setup({ modelscope_api_token, site_url })` (`.cn`) **before** `open`. Same keys as `aria-engine setup`. Instance-only; does not write `engine.yml`. If omitted, the SDK reads `~/.ariacompute/engine.yml`.
29
28
 
30
29
  ```ts
31
30
  const eng = await Engine.open("gemma-4-e2b-it_q4");
32
- const gated = await Engine.open("gemma-4-e2b-it_q4", { hfToken: "hf_..." });
31
+
32
+ const gated = new Engine();
33
+ gated.setup({ hf_token: "hf_..." });
34
+ await gated.open("gemma-4-e2b-it_q4");
33
35
  ```
package/dist/index.d.ts CHANGED
@@ -29,15 +29,30 @@ export interface CompleteResult {
29
29
  export interface OpenOptions {
30
30
  /** Legacy generic hub token. Dashboard sk-/bfvk- keys are ignored. */
31
31
  token?: string;
32
- /** Hugging Face hub token (`.com`). Same field as `aria-engine auth` `hf_token`. */
32
+ /** Hugging Face hub token (`.com`). Same field as `aria-engine setup` `hf_token`. */
33
33
  hfToken?: string;
34
- /** ModelScope hub token (`.cn`). Same field as `aria-engine auth` `modelscope_api_token`. */
34
+ /** ModelScope hub token (`.cn`). Same field as `aria-engine setup` `modelscope_api_token`. */
35
35
  modelscopeApiToken?: string;
36
36
  /** Site used to pick the regional hub. Defaults to https://ariacompute.com (.com → HF, .cn → ModelScope). */
37
37
  site?: string;
38
38
  /** Explicit path to the FFI library. */
39
39
  ffiLib?: string;
40
40
  }
41
+ export declare const INTL_SITE = "https://ariacompute.com";
42
+ export declare const INTL_UPGRADE = "https://github.com/ariacompute";
43
+ export declare const CN_SITE = "https://ariacompute.cn";
44
+ export declare const CN_UPGRADE = "https://gitee.com/ariacompute";
45
+ export interface SetupConfig {
46
+ router: string;
47
+ site_url: string;
48
+ upgrade_url: string;
49
+ compute: string;
50
+ hf_token: string;
51
+ modelscope_api_token: string;
52
+ }
53
+ export declare function defaultSetupConfig(): SetupConfig;
54
+ export declare function fillSetupUrls(cfg: SetupConfig): SetupConfig;
55
+ export declare function applySetup(existing: SetupConfig, updates: Partial<SetupConfig>): SetupConfig;
41
56
  /** Parse `slug`/`quant` from a model name such as `gemma-4-e2b-it_q4`. */
42
57
  export declare function parseBundleName(model: string): {
43
58
  slug: string;
@@ -70,8 +85,18 @@ export declare class Engine {
70
85
  private fnEmbed;
71
86
  private fnTranscribe;
72
87
  private fnLastError;
73
- /** Construct from a local bundle directory. */
74
- constructor(bundle: string, opts?: OpenOptions);
88
+ private cfg;
89
+ private opts;
90
+ /** Empty construct, or a local bundle directory. */
91
+ constructor(bundle?: string, opts?: OpenOptions);
92
+ /** Set Config / Run fields on this instance only. Does not write engine.yml. */
93
+ setup(updates: Partial<SetupConfig>): this;
94
+ setupStatus(): SetupConfig;
95
+ /** Reset instance defaults. Does not delete ~/.ariacompute/engine.yml. */
96
+ setupClear(): this;
97
+ private bindAndInit;
98
+ /** Download (if needed) and load a model using instance setup. */
99
+ open(modelRef: string): Promise<this>;
75
100
  /** Auto-detect: a value containing a separator or already on disk is a local
76
101
  * path; otherwise it is a model name downloaded from the regional public hub. */
77
102
  static open(modelRef: string, opts?: OpenOptions): Promise<Engine>;
package/dist/index.js CHANGED
@@ -33,7 +33,10 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports._internal = exports.Engine = void 0;
36
+ exports._internal = exports.Engine = exports.CN_UPGRADE = exports.CN_SITE = exports.INTL_UPGRADE = exports.INTL_SITE = void 0;
37
+ exports.defaultSetupConfig = defaultSetupConfig;
38
+ exports.fillSetupUrls = fillSetupUrls;
39
+ exports.applySetup = applySetup;
37
40
  exports.parseBundleName = parseBundleName;
38
41
  exports.preferredPublicHub = preferredPublicHub;
39
42
  exports.hubBearer = hubBearer;
@@ -56,6 +59,55 @@ const promises_1 = require("node:stream/promises");
56
59
  const node_stream_1 = require("node:stream");
57
60
  const zlib = __importStar(require("node:zlib"));
58
61
  const DEFAULT_SITE = "https://ariacompute.com";
62
+ exports.INTL_SITE = "https://ariacompute.com";
63
+ exports.INTL_UPGRADE = "https://github.com/ariacompute";
64
+ exports.CN_SITE = "https://ariacompute.cn";
65
+ exports.CN_UPGRADE = "https://gitee.com/ariacompute";
66
+ function defaultSetupConfig() {
67
+ return {
68
+ router: "",
69
+ site_url: "",
70
+ upgrade_url: "",
71
+ compute: "auto",
72
+ hf_token: "",
73
+ modelscope_api_token: "",
74
+ };
75
+ }
76
+ function gatewayRegion(url) {
77
+ const lower = (url || "").toLowerCase();
78
+ if (lower.includes("ariacompute.cn") || lower.includes("gitee.com/ariacompute"))
79
+ return "cn";
80
+ if (lower.includes("ariacompute.com") || lower.includes("github.com/ariacompute"))
81
+ return "intl";
82
+ return undefined;
83
+ }
84
+ function pairUrls(region) {
85
+ return region === "cn" ? [exports.CN_SITE, exports.CN_UPGRADE] : [exports.INTL_SITE, exports.INTL_UPGRADE];
86
+ }
87
+ function fillSetupUrls(cfg) {
88
+ const out = { ...cfg };
89
+ const region = gatewayRegion(out.site_url) || gatewayRegion(out.upgrade_url);
90
+ if (!region)
91
+ return out;
92
+ const [site, upgrade] = pairUrls(region);
93
+ if (!out.site_url)
94
+ out.site_url = site;
95
+ if (!out.upgrade_url)
96
+ out.upgrade_url = upgrade;
97
+ return out;
98
+ }
99
+ function applySetup(existing, updates) {
100
+ const out = { ...existing };
101
+ for (const [k, v] of Object.entries(updates)) {
102
+ if (v === undefined)
103
+ continue;
104
+ out[k] = v;
105
+ }
106
+ if (!["auto", "cpu", "cuda"].includes(out.compute)) {
107
+ throw new Error(`invalid compute: ${out.compute}`);
108
+ }
109
+ return fillSetupUrls(out);
110
+ }
59
111
  const DEFAULT_SDK = "v1.0";
60
112
  const HUB_REQUIRED = ["config.json", "weight.bin"];
61
113
  const HUB_OPTIONAL = [
@@ -132,23 +184,27 @@ function unquoteYaml(v) {
132
184
  return t;
133
185
  }
134
186
  function configYmlScalar(key) {
135
- try {
136
- const raw = fs.readFileSync(path.join(ariaHome(), "config.yml"), "utf8");
137
- for (const line of raw.split("\n")) {
138
- if (line.startsWith(" ") || line.startsWith("\t"))
139
- continue;
140
- const s = line.trim();
141
- if (!s || s.startsWith("#") || !s.includes(":"))
142
- continue;
143
- const idx = s.indexOf(":");
144
- if (s.slice(0, idx).trim() !== key)
145
- continue;
146
- const v = unquoteYaml(s.slice(idx + 1));
147
- return v || undefined;
187
+ const home = ariaHome();
188
+ for (const name of ["engine.yml", "config.yml"]) {
189
+ try {
190
+ const raw = fs.readFileSync(path.join(home, name), "utf8");
191
+ for (const line of raw.split("\n")) {
192
+ if (line.startsWith(" ") || line.startsWith("\t"))
193
+ continue;
194
+ const s = line.trim();
195
+ if (!s || s.startsWith("#") || !s.includes(":"))
196
+ continue;
197
+ const idx = s.indexOf(":");
198
+ if (s.slice(0, idx).trim() !== key)
199
+ continue;
200
+ const v = unquoteYaml(s.slice(idx + 1));
201
+ if (v)
202
+ return v;
203
+ }
204
+ }
205
+ catch {
206
+ continue;
148
207
  }
149
- }
150
- catch {
151
- return undefined;
152
208
  }
153
209
  return undefined;
154
210
  }
@@ -236,7 +292,7 @@ async function fetchHubFile(source, model, file, dest, token, required) {
236
292
  last = e;
237
293
  if (e instanceof HubHttpError && (e.status === 401 || e.status === 403)) {
238
294
  const field = source === "modelscope" ? "modelscope_api_token" : "hf_token";
239
- throw new Error(`auth failed HTTP ${e.status}; set ${field} via aria-engine auth (do not pass a Dashboard sk-/bfvk- key as the hub token)`);
295
+ throw new Error(`auth failed HTTP ${e.status}; set ${field} via aria-engine setup (do not pass a Dashboard sk-/bfvk- key as the hub token)`);
240
296
  }
241
297
  }
242
298
  }
@@ -508,9 +564,38 @@ function isLocalRef(modelRef) {
508
564
  return modelRef.includes("/") || modelRef.includes("\\") || fs.existsSync(modelRef);
509
565
  }
510
566
  class Engine {
511
- /** Construct from a local bundle directory. */
567
+ /** Empty construct, or a local bundle directory. */
512
568
  constructor(bundle, opts = {}) {
513
- this.lib = loadLib(opts.ffiLib, opts.site);
569
+ this.lib = null;
570
+ this.handle = null;
571
+ this.cfg = defaultSetupConfig();
572
+ this.opts = {};
573
+ this.opts = opts;
574
+ if (opts.site || opts.hfToken || opts.modelscopeApiToken) {
575
+ this.setup({
576
+ site_url: opts.site,
577
+ hf_token: opts.hfToken,
578
+ modelscope_api_token: opts.modelscopeApiToken,
579
+ });
580
+ }
581
+ if (bundle)
582
+ this.bindAndInit(bundle, opts.ffiLib);
583
+ }
584
+ /** Set Config / Run fields on this instance only. Does not write engine.yml. */
585
+ setup(updates) {
586
+ this.cfg = applySetup(this.cfg, updates);
587
+ return this;
588
+ }
589
+ setupStatus() {
590
+ return { ...this.cfg };
591
+ }
592
+ /** Reset instance defaults. Does not delete ~/.ariacompute/engine.yml. */
593
+ setupClear() {
594
+ this.cfg = defaultSetupConfig();
595
+ return this;
596
+ }
597
+ bindAndInit(bundle, ffiLib) {
598
+ this.lib = loadLib(ffiLib || this.opts.ffiLib, this.cfg.site_url || this.opts.site);
514
599
  this.fnInit = this.lib.func("aria_model_init", "void*", ["str"]);
515
600
  this.fnDestroy = this.lib.func("aria_model_destroy", "void", ["void*"]);
516
601
  this.fnComplete = this.lib.func("aria_complete", "int", ["void*", "str", "str", "str", "void*", "size_t"]);
@@ -523,14 +608,27 @@ class Engine {
523
608
  throw new Error(err || "init failed");
524
609
  }
525
610
  }
611
+ /** Download (if needed) and load a model using instance setup. */
612
+ async open(modelRef) {
613
+ await ensureFfiLib(this.cfg.site_url || this.opts.site);
614
+ const openOpts = {
615
+ ...this.opts,
616
+ site: this.cfg.site_url || this.opts.site,
617
+ hfToken: this.cfg.hf_token || this.opts.hfToken,
618
+ modelscopeApiToken: this.cfg.modelscope_api_token || this.opts.modelscopeApiToken,
619
+ };
620
+ const bundle = isLocalRef(modelRef) ? modelRef : await downloadModel(modelRef, openOpts);
621
+ if (this.handle)
622
+ this.close();
623
+ this.bindAndInit(bundle, this.opts.ffiLib);
624
+ return this;
625
+ }
526
626
  /** Auto-detect: a value containing a separator or already on disk is a local
527
627
  * path; otherwise it is a model name downloaded from the regional public hub. */
528
628
  static async open(modelRef, opts = {}) {
529
- await ensureFfiLib(opts.site);
530
- if (isLocalRef(modelRef))
531
- return new Engine(modelRef, opts);
532
- const bundle = await downloadModel(modelRef, opts);
533
- return new Engine(bundle, opts);
629
+ const eng = new Engine(undefined, opts);
630
+ await eng.open(modelRef);
631
+ return eng;
534
632
  }
535
633
  complete(messages, opts = {}) {
536
634
  const messagesJson = JSON.stringify(messages);
@@ -569,7 +667,7 @@ class Engine {
569
667
  return buf.toString("utf8").replace(/\0+$/, "");
570
668
  }
571
669
  close() {
572
- if (this.handle)
670
+ if (this.handle && this.fnDestroy)
573
671
  this.fnDestroy(this.handle);
574
672
  this.handle = null;
575
673
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ariacompute/engine-ts",
3
- "version": "1.15.1",
3
+ "version": "1.17.0",
4
4
  "description": "TypeScript/Node SDK for Aria Engine",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",