@drsamonline/sdk 0.0.0-stage → 0.12.3

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.
Files changed (3) hide show
  1. package/package.json +26 -4
  2. package/src/index.js +153 -0
  3. package/README.md +0 -3
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@drsamonline/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.12.3",
4
+ "description": "JavaScript/TypeScript client for the gateway.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "main": "./src/index.js",
8
+ "exports": {
9
+ ".": "./src/index.js"
10
+ },
11
+ "files": [
12
+ "src",
13
+ "web",
14
+ "bin",
15
+ "LICENSE"
16
+ ],
17
+ "types": "./src/index.d.ts",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git://github.com/drsamonline/sui.git",
21
+ "directory": "packages/sdk"
22
+ },
23
+ "homepage": "https://github.com/drsamonline/sui#readme",
24
+ "publishConfig": {
25
+ "access": "public",
26
+ "registry": "https://registry.npmjs.org/"
27
+ }
28
+ }
package/src/index.js ADDED
@@ -0,0 +1,153 @@
1
+ /**
2
+ * @drsamonline/sdk — a small client for the gateway.
3
+ *
4
+ * No dependencies, works in Node 20+ and modern browsers. Every response keeps
5
+ * the `sui` provenance block, because a client that cannot say who answered is
6
+ * a client that cannot explain an outage.
7
+ */
8
+
9
+ export class SuiClientError extends Error {
10
+ /**
11
+ * @param {string} message
12
+ * @param {{type?: string, status?: number, attempts?: any[]}} [opts]
13
+ */
14
+ constructor(message, opts = {}) {
15
+ super(message);
16
+ this.name = 'SuiClientError';
17
+ this.type = opts.type ?? 'client_error';
18
+ this.status = opts.status ?? 0;
19
+ this.attempts = opts.attempts ?? [];
20
+ }
21
+ }
22
+
23
+ export class SuiClient {
24
+ /**
25
+ * @param {{baseUrl?: string, apiKey?: string, fetchImpl?: typeof fetch, timeoutMs?: number}} [opts]
26
+ */
27
+ constructor(opts = {}) {
28
+ this.baseUrl = String(opts.baseUrl ?? 'http://127.0.0.1:8787').replace(/\/+$/, '');
29
+ this.apiKey = opts.apiKey ?? null;
30
+ this.fetchImpl = opts.fetchImpl ?? globalThis.fetch;
31
+ this.timeoutMs = opts.timeoutMs ?? 120000;
32
+ }
33
+
34
+ /** @param {string} path @param {RequestInit} [init] */
35
+ async #request(path, init = {}) {
36
+ const headers = {
37
+ 'content-type': 'application/json',
38
+ ...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
39
+ ...(init.headers ?? {}),
40
+ };
41
+ const res = await this.fetchImpl(`${this.baseUrl}${path}`, {
42
+ ...init,
43
+ headers,
44
+ signal: init.signal ?? AbortSignal.timeout(this.timeoutMs),
45
+ });
46
+ if (!res.ok) {
47
+ const body = await res.json().catch(() => null);
48
+ throw new SuiClientError(
49
+ body?.error?.message ?? `Gateway returned HTTP ${res.status}.`,
50
+ { type: body?.error?.type, status: res.status, attempts: body?.error?.attempts },
51
+ );
52
+ }
53
+ return res;
54
+ }
55
+
56
+ /**
57
+ * One chat turn.
58
+ * @param {{model: string, messages: Array<{role: string, content: string}>, temperature?: number, max_tokens?: number, top_p?: number, stop?: string|string[]}} req
59
+ * @returns {Promise<{text: string, usage: any, provenance: any, raw: any}>}
60
+ */
61
+ async chat(req) {
62
+ const res = await this.#request('/v1/chat/completions', {
63
+ method: 'POST',
64
+ body: JSON.stringify({ ...req, stream: false }),
65
+ });
66
+ const json = await res.json();
67
+ return {
68
+ text: json.choices?.[0]?.message?.content ?? '',
69
+ usage: json.usage ?? null,
70
+ provenance: json.sui ?? null,
71
+ raw: json,
72
+ };
73
+ }
74
+
75
+ /**
76
+ * Streaming turn. Yields content deltas; the last value is
77
+ * `{done: true, provenance}`.
78
+ * @param {{model: string, messages: Array<{role: string, content: string}>, signal?: AbortSignal}} req
79
+ */
80
+ async *stream(req) {
81
+ const res = await this.#request('/v1/chat/completions', {
82
+ method: 'POST',
83
+ body: JSON.stringify({ ...req, stream: true }),
84
+ signal: req.signal,
85
+ });
86
+
87
+ const reader = res.body.getReader();
88
+ const decoder = new TextDecoder();
89
+ let buffer = '';
90
+ let provenance = null;
91
+
92
+ while (true) {
93
+ const { done, value } = await reader.read();
94
+ if (done) break;
95
+ buffer += decoder.decode(value, { stream: true });
96
+ let idx;
97
+ while ((idx = buffer.indexOf('\n\n')) !== -1) {
98
+ const block = buffer.slice(0, idx);
99
+ buffer = buffer.slice(idx + 2);
100
+ for (const line of block.split('\n')) {
101
+ if (!line.startsWith('data:')) continue;
102
+ const data = line.slice(5).trim();
103
+ if (!data || data === '[DONE]') continue;
104
+ let payload;
105
+ try {
106
+ payload = JSON.parse(data);
107
+ } catch {
108
+ continue;
109
+ }
110
+ if (payload.sui) provenance = payload.sui;
111
+ const delta = payload.choices?.[0]?.delta?.content;
112
+ if (delta) yield { done: false, delta, provenance };
113
+ }
114
+ }
115
+ }
116
+ yield { done: true, provenance };
117
+ }
118
+
119
+ /** @param {{all?: boolean}} [opts] */
120
+ async models(opts = {}) {
121
+ const res = await this.#request(`/v1/models${opts.all ? '?all=1' : ''}`);
122
+ return (await res.json()).data ?? [];
123
+ }
124
+
125
+ async status() {
126
+ return (await (await this.#request('/api/status')).json());
127
+ }
128
+
129
+ /** @param {{since?: number|string}} [opts] */
130
+ async metrics(opts = {}) {
131
+ const q = opts.since != null ? `?since=${encodeURIComponent(String(opts.since))}` : '';
132
+ return (await (await this.#request(`/api/metrics${q}`)).json());
133
+ }
134
+
135
+ /** Ask whether a dashboard toggle would be safe to commit. */
136
+ async validateToggle(patch) {
137
+ const res = await this.fetchImpl(`${this.baseUrl}/api/config/validate`, {
138
+ method: 'POST',
139
+ headers: {
140
+ 'content-type': 'application/json',
141
+ ...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
142
+ },
143
+ body: JSON.stringify(patch),
144
+ });
145
+ return res.json();
146
+ }
147
+
148
+ async health() {
149
+ return (await (await this.#request('/healthz')).json());
150
+ }
151
+ }
152
+
153
+ export default SuiClient;
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
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.