ghostlink-client 0.0.0-stage → 2.6.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ghostlink contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,136 @@
1
- # Temporary Holding Version
1
+ # ghostlink-client
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ JavaScript/TypeScript client for a [Ghostlink](https://github.com/rwilliamspbg-ops/Ghostlink) Studio API server — the OpenAI-compatible REST endpoints (`/v1/chat/completions`, `/v1/completions`, `/v1/embeddings`, `/v1/models`) plus Ghostlink's native `/api/*` surface (workers, sessions, settings, metrics, and real token-by-token streaming chat).
4
+
5
+ This is the JS/TS counterpart of `sdks/python` — same method names, same nested structure (`client.chat.completions.create`, `client.workers.connect`, ...), same endpoints. Built on the native `fetch`/`ReadableStream` APIs, so it has no runtime dependencies and works in Node 18+ and modern browsers alike.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install ghostlink-client
11
+ ```
12
+
13
+ (Not yet published to npm — install from a local checkout or a Git URL until then.)
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import { GhostlinkClient } from "ghostlink-client";
19
+
20
+ const client = new GhostlinkClient("http://127.0.0.1:8003", { apiKey: "<your api key>" });
21
+
22
+ const resp = await client.chat.completions.create({
23
+ model: "llama3.2:3b",
24
+ messages: [{ role: "user", content: "Say hello in one sentence." }],
25
+ });
26
+ console.log(resp.content);
27
+ ```
28
+
29
+ The API key is the one Ghostlink generates on first run (printed at startup, persisted to `api_key.txt` by default — see `GHOSTLINK_API_KEY_PATH`).
30
+
31
+ ## OpenAI-compatible endpoints
32
+
33
+ ```ts
34
+ // Chat
35
+ const resp = await client.chat.completions.create({ model: "llama3.2:3b", messages: [...] });
36
+
37
+ // Legacy text completion
38
+ const cmpl = await client.completions.create({ model: "llama3.2:3b", prompt: "Once upon a time" });
39
+ console.log(cmpl.text);
40
+
41
+ // Embeddings
42
+ const emb = await client.embeddings.create({ model: "nomic-embed-text", input: "hello world" });
43
+
44
+ // List models
45
+ for (const m of await client.listModels()) {
46
+ console.log(m.id);
47
+ }
48
+ ```
49
+
50
+ ## Real streaming chat
51
+
52
+ `/v1/chat/completions` accepts a `stream` field but Ghostlink doesn't act on it yet — real token-by-token streaming is only available today through Ghostlink Studio's native chat endpoint:
53
+
54
+ ```ts
55
+ for await (const chunk of client.streamChat("Tell me a short story")) {
56
+ if (chunk.error) {
57
+ console.log(`\n[error: ${chunk.token}]`);
58
+ } else if (!chunk.done) {
59
+ process.stdout.write(chunk.token);
60
+ }
61
+ }
62
+ ```
63
+
64
+ ## Cluster, metrics, settings
65
+
66
+ ```ts
67
+ await client.workers.list();
68
+ await client.workers.discover(); // UDP broadcast + mDNS sweep
69
+ await client.workers.connect("192.168.1.42", 8003);
70
+
71
+ await client.metrics(); // JSON snapshot
72
+ await client.metricsPrometheus(); // Prometheus exposition format
73
+
74
+ await client.settings.get();
75
+ await client.settings.update({ inference_backend: "ollama" });
76
+ ```
77
+
78
+ ## Error handling
79
+
80
+ ```ts
81
+ import { GhostlinkAPIError, GhostlinkAuthError, GhostlinkConnectionError } from "ghostlink-client";
82
+
83
+ try {
84
+ await client.chat.completions.create({ model: "x", messages: [] });
85
+ } catch (err) {
86
+ if (err instanceof GhostlinkAuthError) {
87
+ console.log("bad or missing API key:", err.message);
88
+ } else if (err instanceof GhostlinkAPIError) {
89
+ console.log(`server rejected the request: HTTP ${err.statusCode}: ${err.message}`);
90
+ } else if (err instanceof GhostlinkConnectionError) {
91
+ console.log("could not reach the server:", err.message);
92
+ } else {
93
+ throw err;
94
+ }
95
+ }
96
+ ```
97
+
98
+ ## Custom inference backends
99
+
100
+ If the server has a custom backend plugin registered (see `backend_plugin.rs` / `GHOSTLINK_OPENAI_COMPAT_BASE_URL` in the main project), select it the same way as any built-in backend:
101
+
102
+ ```ts
103
+ await client.settings.update({ inference_backend: "my_custom_backend" });
104
+ const resp = await client.chat.completions.create({ model: "anything", messages: [...] });
105
+ ```
106
+
107
+ ## API surface at a glance
108
+
109
+ | Python (`sdks/python`) | JS/TS (this package) |
110
+ | -------------------------------------- | ---------------------------------------- |
111
+ | `client.chat.completions.create(...)` | `client.chat.completions.create({...})` |
112
+ | `client.completions.create(...)` | `client.completions.create({...})` |
113
+ | `client.embeddings.create(...)` | `client.embeddings.create({...})` |
114
+ | `client.models.list()` | `client.models.list()` |
115
+ | `client.list_models()` | `client.listModels()` |
116
+ | `client.workers.list()` / `.discover()` / `.connect()` / `.add()` / `.disconnect()` | same, unchanged |
117
+ | `client.sessions.list()` | `client.sessions.list()` |
118
+ | `client.settings.get()` / `.update()` / `.reset()` | same, unchanged |
119
+ | `client.health()` / `.metrics()` / `.metrics_prometheus()` | `client.health()` / `.metrics()` / `.metricsPrometheus()` |
120
+ | `client.studio_chat(...)` | `client.studioChat(...)` |
121
+ | `client.stream_chat(...)` (generator) | `client.streamChat(...)` (async generator) |
122
+
123
+ Method and namespace names match the Python client one-for-one; only casing follows each language's convention (`snake_case` in Python, `camelCase` in JS/TS) and multi-argument calls take a single options object instead of keyword arguments.
124
+
125
+ ## Node vs. browser
126
+
127
+ This SDK is built on the standard `fetch`, `ReadableStream`, and `TextDecoder` APIs — no HTTP client dependency (no axios) — so it runs unmodified in Node 18+ and in any modern browser. The one caveat: calling it directly from a browser page means the API key ships in that page's JS, so for browser use put this behind your own backend (or a proxy that injects the key) rather than embedding a Ghostlink API key in client-side code.
128
+
129
+ ## Development
130
+
131
+ ```bash
132
+ npm install
133
+ npm run build # emits dist/ (ESM + CJS + .d.ts)
134
+ npm run type-check
135
+ npm test
136
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,507 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ChatCompletion: () => ChatCompletion,
24
+ Completion: () => Completion,
25
+ DEFAULT_TIMEOUT_MS: () => DEFAULT_TIMEOUT_MS,
26
+ GhostlinkAPIError: () => GhostlinkAPIError,
27
+ GhostlinkAuthError: () => GhostlinkAuthError,
28
+ GhostlinkClient: () => GhostlinkClient,
29
+ GhostlinkConnectionError: () => GhostlinkConnectionError,
30
+ GhostlinkError: () => GhostlinkError,
31
+ Model: () => Model,
32
+ StreamChunk: () => StreamChunk,
33
+ VERSION: () => VERSION
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/sse.ts
38
+ async function* streamLines(body) {
39
+ const reader = body.getReader();
40
+ const decoder = new TextDecoder("utf-8");
41
+ let buffer = "";
42
+ try {
43
+ for (; ; ) {
44
+ const { done, value } = await reader.read();
45
+ if (done) break;
46
+ buffer += decoder.decode(value, { stream: true });
47
+ let newlineIndex;
48
+ while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
49
+ let line = buffer.slice(0, newlineIndex);
50
+ buffer = buffer.slice(newlineIndex + 1);
51
+ if (line.endsWith("\r")) line = line.slice(0, -1);
52
+ yield line;
53
+ }
54
+ }
55
+ buffer += decoder.decode();
56
+ if (buffer.length > 0) {
57
+ yield buffer.endsWith("\r") ? buffer.slice(0, -1) : buffer;
58
+ }
59
+ } finally {
60
+ reader.releaseLock();
61
+ }
62
+ }
63
+ async function* iterSseJson(lines) {
64
+ const dataLines = [];
65
+ function flush() {
66
+ if (dataLines.length === 0) return null;
67
+ const payload = dataLines.join("\n");
68
+ dataLines.length = 0;
69
+ let decoded2;
70
+ try {
71
+ decoded2 = JSON.parse(payload);
72
+ } catch {
73
+ return null;
74
+ }
75
+ return decoded2 !== null && typeof decoded2 === "object" && !Array.isArray(decoded2) ? decoded2 : null;
76
+ }
77
+ for await (const line of lines) {
78
+ if (line === "") {
79
+ const decoded2 = flush();
80
+ if (decoded2 !== null) yield decoded2;
81
+ continue;
82
+ }
83
+ if (line.startsWith(":")) continue;
84
+ if (line.startsWith("data:")) {
85
+ dataLines.push(line.slice("data:".length).replace(/^ /, ""));
86
+ }
87
+ }
88
+ const decoded = flush();
89
+ if (decoded !== null) yield decoded;
90
+ }
91
+
92
+ // src/exceptions.ts
93
+ var GhostlinkError = class extends Error {
94
+ constructor(message) {
95
+ super(message);
96
+ this.name = "GhostlinkError";
97
+ Object.setPrototypeOf(this, new.target.prototype);
98
+ }
99
+ };
100
+ var GhostlinkConnectionError = class extends GhostlinkError {
101
+ constructor(message) {
102
+ super(message);
103
+ this.name = "GhostlinkConnectionError";
104
+ Object.setPrototypeOf(this, new.target.prototype);
105
+ }
106
+ };
107
+ var GhostlinkAPIError = class extends GhostlinkError {
108
+ constructor(statusCode, message, body) {
109
+ super(`HTTP ${statusCode}: ${message}`);
110
+ this.name = "GhostlinkAPIError";
111
+ this.statusCode = statusCode;
112
+ this.body = body;
113
+ Object.setPrototypeOf(this, new.target.prototype);
114
+ }
115
+ };
116
+ var GhostlinkAuthError = class extends GhostlinkAPIError {
117
+ constructor(statusCode, message, body) {
118
+ super(statusCode, message, body);
119
+ this.name = "GhostlinkAuthError";
120
+ Object.setPrototypeOf(this, new.target.prototype);
121
+ }
122
+ };
123
+
124
+ // src/models.ts
125
+ function asString(value, fallback = "") {
126
+ return typeof value === "string" ? value : fallback;
127
+ }
128
+ function asNumber(value, fallback = 0) {
129
+ return typeof value === "number" ? value : fallback;
130
+ }
131
+ function asBool(value, fallback = false) {
132
+ return typeof value === "boolean" ? value : Boolean(value ?? fallback);
133
+ }
134
+ function chatChoiceFromDict(data) {
135
+ return {
136
+ index: asNumber(data.index, 0),
137
+ message: data.message ?? {},
138
+ finishReason: data.finish_reason ?? null
139
+ };
140
+ }
141
+ var ChatCompletion = class _ChatCompletion {
142
+ constructor(fields) {
143
+ this.id = fields.id;
144
+ this.object = fields.object;
145
+ this.created = fields.created;
146
+ this.model = fields.model;
147
+ this.choices = fields.choices;
148
+ this.raw = fields.raw;
149
+ }
150
+ /** The first choice's assistant text, or `""` if there are none. */
151
+ get content() {
152
+ if (this.choices.length === 0) return "";
153
+ const content = this.choices[0].message?.content;
154
+ return typeof content === "string" ? content : "";
155
+ }
156
+ static fromDict(data) {
157
+ return new _ChatCompletion({
158
+ id: asString(data.id),
159
+ object: asString(data.object),
160
+ created: asNumber(data.created),
161
+ model: asString(data.model),
162
+ choices: Array.isArray(data.choices) ? data.choices.map(chatChoiceFromDict) : [],
163
+ raw: data
164
+ });
165
+ }
166
+ };
167
+ var Completion = class _Completion {
168
+ constructor(fields) {
169
+ this.id = fields.id;
170
+ this.object = fields.object;
171
+ this.created = fields.created;
172
+ this.model = fields.model;
173
+ this.choices = fields.choices;
174
+ this.raw = fields.raw;
175
+ }
176
+ get text() {
177
+ return this.choices.length > 0 ? this.choices[0].text : "";
178
+ }
179
+ static fromDict(data) {
180
+ return new _Completion({
181
+ id: asString(data.id),
182
+ object: asString(data.object),
183
+ created: asNumber(data.created),
184
+ model: asString(data.model),
185
+ choices: Array.isArray(data.choices) ? data.choices.map((c) => ({
186
+ text: asString(c.text),
187
+ index: asNumber(c.index),
188
+ finishReason: c.finish_reason ?? null
189
+ })) : [],
190
+ raw: data
191
+ });
192
+ }
193
+ };
194
+ var Model = class _Model {
195
+ constructor(fields) {
196
+ this.id = fields.id;
197
+ this.raw = fields.raw;
198
+ }
199
+ static fromDict(data) {
200
+ return new _Model({ id: asString(data.id), raw: data });
201
+ }
202
+ };
203
+ var StreamChunk = class _StreamChunk {
204
+ constructor(fields) {
205
+ this.token = fields.token;
206
+ this.requestId = fields.requestId;
207
+ this.sessionId = fields.sessionId;
208
+ this.error = fields.error;
209
+ this.done = fields.done;
210
+ this.truncated = fields.truncated;
211
+ this.raw = fields.raw;
212
+ }
213
+ static fromDict(data) {
214
+ return new _StreamChunk({
215
+ token: asString(data.token),
216
+ requestId: asString(data.request_id),
217
+ sessionId: asString(data.session_id),
218
+ error: asBool(data.error),
219
+ done: asBool(data.done),
220
+ truncated: asBool(data.truncated),
221
+ raw: data
222
+ });
223
+ }
224
+ };
225
+
226
+ // src/client.ts
227
+ var DEFAULT_TIMEOUT_MS = 3e4;
228
+ function addOptional(payload, fields) {
229
+ for (const [key, value] of Object.entries(fields)) {
230
+ if (value !== void 0) payload[key] = value;
231
+ }
232
+ }
233
+ function samplingParamsToPayload(params) {
234
+ const payload = {};
235
+ addOptional(payload, {
236
+ temperature: params.temperature,
237
+ top_p: params.topP,
238
+ top_k: params.topK,
239
+ penalty: params.penalty,
240
+ max_tokens: params.maxTokens
241
+ });
242
+ return payload;
243
+ }
244
+ function extractErrorMessage(body) {
245
+ if (body && typeof body === "object") {
246
+ const error = body.error;
247
+ if (error && typeof error === "object") {
248
+ const message = error.message;
249
+ if (typeof message === "string") return message;
250
+ }
251
+ if (typeof error === "string") return error;
252
+ }
253
+ return void 0;
254
+ }
255
+ var ChatCompletions = class {
256
+ constructor(client) {
257
+ this.client = client;
258
+ }
259
+ /** `POST /v1/chat/completions` — OpenAI-compatible, non-streaming. */
260
+ async create(params) {
261
+ const payload = {
262
+ model: params.model,
263
+ messages: params.messages,
264
+ ...samplingParamsToPayload(params)
265
+ };
266
+ const data = await this.client._request("POST", "/v1/chat/completions", payload);
267
+ return ChatCompletion.fromDict(data);
268
+ }
269
+ };
270
+ var Chat = class {
271
+ constructor(client) {
272
+ this.completions = new ChatCompletions(client);
273
+ }
274
+ };
275
+ var Completions = class {
276
+ constructor(client) {
277
+ this.client = client;
278
+ }
279
+ /** `POST /v1/completions` — OpenAI's legacy completions endpoint. */
280
+ async create(params) {
281
+ const payload = {
282
+ model: params.model,
283
+ prompt: params.prompt,
284
+ ...samplingParamsToPayload(params)
285
+ };
286
+ const data = await this.client._request("POST", "/v1/completions", payload);
287
+ return Completion.fromDict(data);
288
+ }
289
+ };
290
+ var Embeddings = class {
291
+ constructor(client) {
292
+ this.client = client;
293
+ }
294
+ /** `POST /v1/embeddings`. */
295
+ async create(params) {
296
+ return this.client._request("POST", "/v1/embeddings", {
297
+ model: params.model,
298
+ input: params.input
299
+ });
300
+ }
301
+ };
302
+ var Models = class {
303
+ constructor(client) {
304
+ this.client = client;
305
+ }
306
+ /** `GET /v1/models`. */
307
+ async list() {
308
+ const data = await this.client._request("GET", "/v1/models");
309
+ const items = Array.isArray(data.data) ? data.data : [];
310
+ return items.map((m) => Model.fromDict(m));
311
+ }
312
+ };
313
+ var Workers = class {
314
+ constructor(client) {
315
+ this.client = client;
316
+ }
317
+ async list() {
318
+ const data = await this.client._request("GET", "/api/workers");
319
+ return Array.isArray(data.workers) ? data.workers : [];
320
+ }
321
+ /**
322
+ * Triggers a UDP-broadcast + mDNS discovery sweep and registers any peers
323
+ * found. See `GET /api/workers/discover`.
324
+ */
325
+ async discover() {
326
+ return this.client._request("GET", "/api/workers/discover");
327
+ }
328
+ async connect(host, port) {
329
+ return this.client._request("POST", "/api/workers/connect", { host, port });
330
+ }
331
+ async add(fields) {
332
+ return this.client._request("POST", "/api/workers/add", fields);
333
+ }
334
+ async disconnect(workerId) {
335
+ return this.client._request("POST", `/api/workers/${workerId}/disconnect`);
336
+ }
337
+ };
338
+ var Sessions = class {
339
+ constructor(client) {
340
+ this.client = client;
341
+ }
342
+ async list() {
343
+ const data = await this.client._request("GET", "/api/sessions");
344
+ return Array.isArray(data.sessions) ? data.sessions : [];
345
+ }
346
+ };
347
+ var Settings = class {
348
+ constructor(client) {
349
+ this.client = client;
350
+ }
351
+ async get() {
352
+ return this.client._request("GET", "/api/settings");
353
+ }
354
+ async update(fields) {
355
+ return this.client._request("POST", "/api/settings", fields);
356
+ }
357
+ async reset() {
358
+ return this.client._request("POST", "/api/settings/reset");
359
+ }
360
+ };
361
+ var GhostlinkClient = class {
362
+ constructor(baseUrl, options = {}) {
363
+ let normalizedBaseUrl = baseUrl;
364
+ while (normalizedBaseUrl.endsWith("/")) {
365
+ normalizedBaseUrl = normalizedBaseUrl.slice(0, -1);
366
+ }
367
+ this.baseUrl = normalizedBaseUrl;
368
+ this.apiKey = options.apiKey;
369
+ this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
370
+ this.fetchImpl = options.fetchImpl ?? fetch;
371
+ this.chat = new Chat(this);
372
+ this.completions = new Completions(this);
373
+ this.embeddings = new Embeddings(this);
374
+ this.models = new Models(this);
375
+ this.workers = new Workers(this);
376
+ this.sessions = new Sessions(this);
377
+ this.settings = new Settings(this);
378
+ }
379
+ headers() {
380
+ const headers = { "Content-Type": "application/json" };
381
+ if (this.apiKey) headers["Authorization"] = `Bearer ${this.apiKey}`;
382
+ return headers;
383
+ }
384
+ /** Fetches with a total-duration timeout on the connect/response-headers phase. */
385
+ async fetchWithTimeout(url, init) {
386
+ const controller = new AbortController();
387
+ const timeoutId = setTimeout(() => controller.abort(), this.timeoutMs);
388
+ try {
389
+ return await this.fetchImpl(url, { ...init, signal: controller.signal });
390
+ } catch (err) {
391
+ throw new GhostlinkConnectionError(
392
+ `failed to reach ${url}: ${err instanceof Error ? err.message : String(err)}`
393
+ );
394
+ } finally {
395
+ clearTimeout(timeoutId);
396
+ }
397
+ }
398
+ async raiseForStatus(resp) {
399
+ if (resp.status < 400) return;
400
+ const text = await resp.text();
401
+ let body = text;
402
+ try {
403
+ body = text ? JSON.parse(text) : void 0;
404
+ } catch {
405
+ }
406
+ const message = extractErrorMessage(body) ?? resp.statusText ?? "request failed";
407
+ if (resp.status === 401 || resp.status === 403) {
408
+ throw new GhostlinkAuthError(resp.status, message, body);
409
+ }
410
+ throw new GhostlinkAPIError(resp.status, message, body);
411
+ }
412
+ /** @internal used by the nested resource classes. */
413
+ async _request(method, path, json) {
414
+ const url = `${this.baseUrl}${path}`;
415
+ const resp = await this.fetchWithTimeout(url, {
416
+ method,
417
+ headers: this.headers(),
418
+ body: json !== void 0 ? JSON.stringify(json) : void 0
419
+ });
420
+ await this.raiseForStatus(resp);
421
+ const text = await resp.text();
422
+ if (!text) return {};
423
+ return JSON.parse(text);
424
+ }
425
+ /** `GET /health` — unauthenticated liveness/version/backend info. */
426
+ async health() {
427
+ return this._request("GET", "/health");
428
+ }
429
+ /**
430
+ * `GET /api/metrics` — JSON metrics snapshot. See `metricsPrometheus` for
431
+ * the Prometheus-exposition-format sibling.
432
+ */
433
+ async metrics() {
434
+ return this._request("GET", "/api/metrics");
435
+ }
436
+ /**
437
+ * `GET /metrics` — the same snapshot as `metrics()`, formatted for a
438
+ * Prometheus scrape config instead of JSON.
439
+ */
440
+ async metricsPrometheus() {
441
+ const url = `${this.baseUrl}/metrics`;
442
+ const resp = await this.fetchWithTimeout(url, { method: "GET", headers: this.headers() });
443
+ await this.raiseForStatus(resp);
444
+ return resp.text();
445
+ }
446
+ /** Convenience alias for `client.models.list()`. */
447
+ async listModels() {
448
+ return this.models.list();
449
+ }
450
+ /**
451
+ * Non-streaming call to Ghostlink Studio's native chat endpoint
452
+ * (`POST /api/inference/chat`) — richer than the OpenAI-compatible routes
453
+ * (session bookkeeping, MCP tool results, live metrics in the response),
454
+ * but Ghostlink-specific rather than portable. Use `streamChat` for real
455
+ * token-by-token streaming.
456
+ */
457
+ async studioChat(message, params = {}) {
458
+ const payload = { message };
459
+ if (params.messages !== void 0) payload.messages = params.messages;
460
+ if (params.model !== void 0) payload.model = params.model;
461
+ Object.assign(payload, samplingParamsToPayload(params));
462
+ return this._request("POST", "/api/inference/chat", payload);
463
+ }
464
+ /**
465
+ * Real token-by-token streaming via `POST /api/inference/chat`
466
+ * (`stream: true`) over Server-Sent Events. Yields one `StreamChunk` per
467
+ * token as the model produces it; the final chunk has `done: true`. This
468
+ * is the only endpoint with genuine incremental streaming today — the
469
+ * OpenAI-compatible `/v1/chat/completions` accepts a `stream` field but
470
+ * does not yet act on it.
471
+ *
472
+ * Note: unlike the other request methods, the client's timeout is only
473
+ * applied to the connect/response-headers phase here, not the full
474
+ * duration of the stream — a long-running token stream would otherwise be
475
+ * truncated by a total-duration timeout meant for quick JSON calls.
476
+ *
477
+ * @example
478
+ * ```ts
479
+ * for await (const chunk of client.streamChat("tell me a story")) {
480
+ * if (!chunk.done) process.stdout.write(chunk.token);
481
+ * }
482
+ * ```
483
+ */
484
+ async *streamChat(message, params = {}) {
485
+ const payload = { message, stream: true };
486
+ if (params.messages !== void 0) payload.messages = params.messages;
487
+ if (params.model !== void 0) payload.model = params.model;
488
+ Object.assign(payload, samplingParamsToPayload(params));
489
+ const url = `${this.baseUrl}/api/inference/chat`;
490
+ const resp = await this.fetchWithTimeout(url, {
491
+ method: "POST",
492
+ headers: this.headers(),
493
+ body: JSON.stringify(payload)
494
+ });
495
+ await this.raiseForStatus(resp);
496
+ if (!resp.body) {
497
+ throw new GhostlinkConnectionError(`no response body from ${url}`);
498
+ }
499
+ for await (const chunkData of iterSseJson(streamLines(resp.body))) {
500
+ yield StreamChunk.fromDict(chunkData);
501
+ }
502
+ }
503
+ };
504
+
505
+ // src/index.ts
506
+ var VERSION = "0.1.0";
507
+ //# sourceMappingURL=index.cjs.map