@hivemagnus/ardent-client 3.2.0 → 3.2.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/README.md CHANGED
@@ -48,12 +48,21 @@ const ardent = createArdentClient({
48
48
  });
49
49
  ```
50
50
 
51
+ `baseUrl` is the Magnus origin; the client adds `/api/v1`. Behind a proxy that
52
+ serves Magnus's `/api/v1` under another path, such as a Hive console's
53
+ `/api/magnus`, give that path as `apiBase` instead (exactly one of the two):
54
+
55
+ ```ts
56
+ viaMagnus({ apiBase: 'https://console.example.com/api/magnus', nodeId: 'node-1', profile: 'assistant', getToken: () => session.token });
57
+ ```
58
+
51
59
  `client/examples/cli.mjs` in the Ardent repository runs submit, follow and
52
60
  download end to end:
53
61
 
54
62
  ```bash
55
63
  ARDENT_TOKEN=... node examples/cli.mjs --url http://127.0.0.1:9910 --prompt "Write the report" --out ./downloads
56
64
  MAGNUS_TOKEN=... node examples/cli.mjs --magnus https://magnus.example.com --node node-1 --profile assistant --prompt "Write the report"
65
+ MAGNUS_TOKEN=... node examples/cli.mjs --magnus-api https://console.example.com/api/magnus --node node-1 --profile assistant --prompt "Write the report"
57
66
  ```
58
67
 
59
68
  ## API
@@ -61,7 +70,7 @@ MAGNUS_TOKEN=... node examples/cli.mjs --magnus https://magnus.example.com --nod
61
70
  ```ts
62
71
  createArdentClient({ transport, execution?, signal?, retryPolicy? }): ArdentClient
63
72
  direct({ baseUrl, getToken, headers?, fetch? }): ArdentTransport
64
- viaMagnus({ baseUrl, nodeId, profile, getToken, headers?, fetch? }): ArdentTransport
73
+ viaMagnus({ baseUrl | apiBase, nodeId, profile, getToken, headers?, fetch? }): ArdentTransport
65
74
 
66
75
  ardent.tasks.submit({ prompt, idempotencyKey?, sessionId?, execution?, signal? }): Promise<TaskAccepted>
67
76
  ardent.tasks.get(id, { signal? }): Promise<Task>
@@ -236,7 +245,8 @@ report.
236
245
  | `artifacts.import` | `POST /api/v1/artifacts/imports` | action `artifact-import`, from Magnus 3.3.0 |
237
246
  | `status`, `capabilities` | `GET /api/v1/status` | read `status`, plus `GET /api/v1/ardent/installations/{nodeId}` |
238
247
 
239
- Magnus reads are `GET {baseUrl}/api/v1/ardent/installations/{nodeId}/profiles/{profile}/{route}`
248
+ Magnus reads are `GET {apiBase}/ardent/installations/{nodeId}/profiles/{profile}/{route}`
249
+ (`apiBase` is `{baseUrl}/api/v1` when `baseUrl` is given)
240
250
  and actions `POST …/action` with `{ action, target?, body? }`; the client
241
251
  unwraps Magnus's `{ success, data }` envelope. A route Magnus refuses (`403`,
242
252
  `404`, or `400` for an action it does not know, while it does not advertise the
package/dist/client.d.ts CHANGED
@@ -5,7 +5,7 @@ import { type FollowOptions } from './follow.js';
5
5
  import { type ArtifactDownload, type DownloadOptions } from './download.js';
6
6
  import type { ArtifactImport, ArtifactImportAccepted, ArtifactImportRequest, ArtifactRecord, ArtifactRecordV2, ArtifactRendition, Lineage, OperationalStatus, Recovery, RecoveryAction, Session, SessionContext, SessionMessage, Task, TaskAccepted, TaskAdmission, TaskEvent } from './types.js';
7
7
  export interface ArdentClientOptions {
8
- /** `direct({ baseUrl, getToken })` or `viaMagnus({ baseUrl, nodeId, profile, getToken })`. */
8
+ /** `direct({ baseUrl, getToken })` or `viaMagnus({ baseUrl | apiBase, nodeId, profile, getToken })`. */
9
9
  readonly transport: ArdentTransport;
10
10
  /** Execution context v1 sent with every submission (attribution only; it never grants access). */
11
11
  readonly execution?: ExecutionContext;
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  export { createArdentClient } from './client.js';
7
7
  export type { ArdentCapabilities, ArdentClient, ArdentClientOptions, CallOptions, FollowTaskOptions, RecoveryDecision, SubmitInput, } from './client.js';
8
8
  export { direct, viaMagnus } from './transport.js';
9
- export type { ArdentTransport, DirectTransportOptions, FetchLike, MagnusTransportOptions, TokenProvider } from './transport.js';
9
+ export type { ArdentTransport, DirectTransportOptions, FetchLike, MagnusTransportOptions, ResolvedMagnusTransportOptions, TokenProvider } from './transport.js';
10
10
  export type { FollowOptions } from './follow.js';
11
11
  export type { ArtifactDownload, DownloadOptions, DownloadProgress, ResumeSource } from './download.js';
12
12
  export { ArdentClientError, ConflictError, DigestMismatchError, NotFoundError, TransportError, UnauthorizedError, UnsupportedCapabilityError, } from './errors.js';
@@ -15,25 +15,47 @@ export interface DirectTransportOptions {
15
15
  /** Defaults to the global `fetch`. */
16
16
  readonly fetch?: FetchLike;
17
17
  }
18
- export interface MagnusTransportOptions extends DirectTransportOptions {
19
- /** The Magnus Control API origin, e.g. `https://magnus.example.com`. */
20
- readonly baseUrl: string;
18
+ interface MagnusTarget {
21
19
  /** The managed node that runs the profile. */
22
20
  readonly nodeId: string;
23
21
  /** The Ardent profile on that node. */
24
22
  readonly profile: string;
23
+ readonly getToken: TokenProvider;
24
+ /** Extra headers for every request. */
25
+ readonly headers?: Readonly<Record<string, string>>;
26
+ /** Defaults to the global `fetch`. */
27
+ readonly fetch?: FetchLike;
25
28
  }
29
+ /**
30
+ * Where Magnus is: either its origin (`baseUrl`; the client adds `/api/v1`) or the API root itself
31
+ * (`apiBase`, e.g. a Hive console's `https://console.example.com/api/magnus`, where the console
32
+ * serves Magnus's `/api/v1`). Give exactly one.
33
+ */
34
+ export type MagnusTransportOptions = MagnusTarget & ({
35
+ readonly baseUrl: string;
36
+ readonly apiBase?: undefined;
37
+ } | {
38
+ readonly apiBase: string;
39
+ readonly baseUrl?: undefined;
40
+ });
41
+ /** The options a Magnus transport carries once `viaMagnus()` has checked them: `apiBase` is always set. */
42
+ export type ResolvedMagnusTransportOptions = MagnusTarget & {
43
+ readonly baseUrl?: string | undefined;
44
+ readonly apiBase: string;
45
+ };
26
46
  /** How the client reaches a profile: built by `direct()` or `viaMagnus()`. */
27
47
  export interface ArdentTransport {
28
48
  readonly kind: 'direct' | 'magnus';
29
- readonly options: DirectTransportOptions | MagnusTransportOptions;
49
+ readonly options: DirectTransportOptions | ResolvedMagnusTransportOptions;
30
50
  }
31
51
  /** Talk to a standalone Ardent management API. */
32
52
  export declare function direct(options: DirectTransportOptions): ArdentTransport;
33
53
  /**
34
54
  * Talk to a Magnus-managed profile through the Magnus Control API
35
- * (`/api/v1/ardent/installations/:nodeId/profiles/:profile/...`). Only routes Magnus allows are
36
- * available; the others raise `UnsupportedCapabilityError`.
55
+ * (`/api/v1/ardent/installations/:nodeId/profiles/:profile/...`). Give Magnus's origin as
56
+ * `baseUrl`, or, behind a proxy that serves Magnus's `/api/v1` under another path (a Hive
57
+ * console's `/api/magnus`), that path as `apiBase`. Only routes Magnus allows are available;
58
+ * the others raise `UnsupportedCapabilityError`.
37
59
  */
38
60
  export declare function viaMagnus(options: MagnusTransportOptions): ArdentTransport;
39
61
  /** Routes that need a capability Magnus or an older Ardent may lack. */
package/dist/transport.js CHANGED
@@ -7,27 +7,35 @@ export function direct(options) {
7
7
  }
8
8
  /**
9
9
  * Talk to a Magnus-managed profile through the Magnus Control API
10
- * (`/api/v1/ardent/installations/:nodeId/profiles/:profile/...`). Only routes Magnus allows are
11
- * available; the others raise `UnsupportedCapabilityError`.
10
+ * (`/api/v1/ardent/installations/:nodeId/profiles/:profile/...`). Give Magnus's origin as
11
+ * `baseUrl`, or, behind a proxy that serves Magnus's `/api/v1` under another path (a Hive
12
+ * console's `/api/magnus`), that path as `apiBase`. Only routes Magnus allows are available;
13
+ * the others raise `UnsupportedCapabilityError`.
12
14
  */
13
15
  export function viaMagnus(options) {
14
16
  for (const [name, value] of [['nodeId', options.nodeId], ['profile', options.profile]])
15
17
  if (!/^[A-Za-z0-9._-]{1,128}$/.test(value))
16
18
  throw new ArdentClientError('invalid_request', `Invalid Magnus ${name}`, { status: 400 });
17
- return { kind: 'magnus', options: { ...options, baseUrl: checkedBase(options.baseUrl) } };
19
+ const { baseUrl, apiBase } = options;
20
+ if ((baseUrl === undefined) === (apiBase === undefined))
21
+ throw new ArdentClientError('invalid_request', 'Give viaMagnus either baseUrl (the Magnus origin) or apiBase (the Magnus API root), not both', { status: 400 });
22
+ if (apiBase !== undefined)
23
+ return { kind: 'magnus', options: { ...options, apiBase: checkedBase(apiBase, 'apiBase') } };
24
+ const origin = checkedBase(baseUrl, 'baseUrl');
25
+ return { kind: 'magnus', options: { ...options, baseUrl: origin, apiBase: `${origin}/api/v1` } };
18
26
  }
19
- function checkedBase(value) {
27
+ function checkedBase(value, name = 'baseUrl') {
20
28
  let url;
21
29
  try {
22
- url = new URL(value);
30
+ url = new URL(String(value));
23
31
  }
24
32
  catch {
25
- throw new ArdentClientError('invalid_request', 'baseUrl must be an absolute http(s) URL', { status: 400 });
33
+ throw new ArdentClientError('invalid_request', `${name} must be an absolute http(s) URL`, { status: 400 });
26
34
  }
27
- if (url.protocol !== 'http:' && url.protocol !== 'https:')
28
- throw new ArdentClientError('invalid_request', 'baseUrl must be an absolute http(s) URL', { status: 400 });
35
+ if (typeof value !== 'string' || (url.protocol !== 'http:' && url.protocol !== 'https:'))
36
+ throw new ArdentClientError('invalid_request', `${name} must be an absolute http(s) URL`, { status: 400 });
29
37
  if (url.username || url.password)
30
- throw new ArdentClientError('invalid_request', 'baseUrl must not carry credentials; use getToken', { status: 400 });
38
+ throw new ArdentClientError('invalid_request', `${name} must not carry credentials; use getToken`, { status: 400 });
31
39
  url.search = '';
32
40
  url.hash = '';
33
41
  return url.toString().replace(/\/+$/, '');
@@ -66,6 +74,8 @@ class NetworkFailure extends Error {
66
74
  export class Connection {
67
75
  kind;
68
76
  #options;
77
+ /** The API root: `<baseUrl>/api/v1` directly, Magnus's API root through Magnus. */
78
+ #api;
69
79
  #magnus;
70
80
  #fetch;
71
81
  #signal;
@@ -76,7 +86,9 @@ export class Connection {
76
86
  constructor(transport, options = {}) {
77
87
  this.kind = transport.kind;
78
88
  this.#options = transport.options;
79
- this.#magnus = 'nodeId' in transport.options ? { nodeId: transport.options.nodeId, profile: transport.options.profile } : undefined;
89
+ const resolved = transport.options;
90
+ this.#magnus = 'nodeId' in resolved ? { nodeId: resolved.nodeId, profile: resolved.profile } : undefined;
91
+ this.#api = 'apiBase' in resolved ? resolved.apiBase : `${resolved.baseUrl}/api/v1`;
80
92
  const custom = transport.options.fetch;
81
93
  this.#fetch = custom ?? ((input, init) => globalThis.fetch(input, init));
82
94
  this.#signal = options.signal;
@@ -88,7 +100,7 @@ export class Connection {
88
100
  get fetcher() { return this.#fetch; }
89
101
  /** The URL of an Ardent path below `/api/v1/` on this transport. */
90
102
  url(path, query = {}) {
91
- const base = this.kind === 'direct' ? `${this.#options.baseUrl}/api/v1/${path}` : `${this.#profileBase()}/${path}`;
103
+ const base = this.kind === 'direct' ? `${this.#api}/${path}` : `${this.#profileBase()}/${path}`;
92
104
  const search = new URLSearchParams();
93
105
  for (const [key, value] of Object.entries(query))
94
106
  if (value !== undefined)
@@ -98,7 +110,7 @@ export class Connection {
98
110
  }
99
111
  #profileBase() {
100
112
  const magnus = this.#magnus ?? { nodeId: '', profile: '' };
101
- return `${this.#options.baseUrl}/api/v1/ardent/installations/${encodeURIComponent(magnus.nodeId)}/profiles/${encodeURIComponent(magnus.profile)}`;
113
+ return `${this.#api}/ardent/installations/${encodeURIComponent(magnus.nodeId)}/profiles/${encodeURIComponent(magnus.profile)}`;
102
114
  }
103
115
  async headers(refresh) {
104
116
  const token = await this.#options.getToken({ refresh });
@@ -247,7 +259,7 @@ export class Connection {
247
259
  this.#reportLoad ??= (async () => {
248
260
  const magnus = this.#magnus ?? { nodeId: '', profile: '' };
249
261
  try {
250
- const response = await this.send({ url: `${this.#options.baseUrl}/api/v1/ardent/installations/${encodeURIComponent(magnus.nodeId)}`, method: 'GET', effect: 'none' });
262
+ const response = await this.send({ url: `${this.#api}/ardent/installations/${encodeURIComponent(magnus.nodeId)}`, method: 'GET', effect: 'none' });
251
263
  const data = await this.#json(response);
252
264
  const profiles = Array.isArray(data['profiles']) ? data['profiles'] : [];
253
265
  const profile = profiles.filter(isRecord).find(item => item['id'] === magnus.profile);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hivemagnus/ardent-client",
3
- "version": "3.2.0",
3
+ "version": "3.2.1",
4
4
  "description": "Typed client for the Ardent management API: tasks, live streams, recovery, lineage and resumable artifact downloads, direct or through Magnus",
5
5
  "keywords": [
6
6
  "ardent",