@node-agent/provider 0.0.0-stage → 0.1.6

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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +167 -2
  3. package/dist/i18n/en.json +105 -0
  4. package/dist/i18n/zh-CN.json +105 -0
  5. package/dist/icon.svg +6 -0
  6. package/dist/index.d.ts +21 -0
  7. package/dist/index.js +372 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/mode.d.ts +26 -0
  10. package/dist/mode.js +39 -0
  11. package/dist/mode.js.map +1 -0
  12. package/dist/persistency/history.d.ts +11 -0
  13. package/dist/persistency/history.js +50 -0
  14. package/dist/persistency/history.js.map +1 -0
  15. package/dist/persistency/index.d.ts +23 -0
  16. package/dist/persistency/index.js +33 -0
  17. package/dist/persistency/index.js.map +1 -0
  18. package/dist/persistency/local.d.ts +20 -0
  19. package/dist/persistency/local.js +50 -0
  20. package/dist/persistency/local.js.map +1 -0
  21. package/dist/providers/anthropic.d.ts +28 -0
  22. package/dist/providers/anthropic.js +538 -0
  23. package/dist/providers/anthropic.js.map +1 -0
  24. package/dist/providers/base.d.ts +262 -0
  25. package/dist/providers/base.js +871 -0
  26. package/dist/providers/base.js.map +1 -0
  27. package/dist/providers/deepseek.d.ts +18 -0
  28. package/dist/providers/deepseek.js +73 -0
  29. package/dist/providers/deepseek.js.map +1 -0
  30. package/dist/providers/index.d.ts +13 -0
  31. package/dist/providers/index.js +30 -0
  32. package/dist/providers/index.js.map +1 -0
  33. package/dist/providers/responses.d.ts +28 -0
  34. package/dist/providers/responses.js +310 -0
  35. package/dist/providers/responses.js.map +1 -0
  36. package/dist/serve.d.ts +2 -0
  37. package/dist/serve.js +28 -0
  38. package/dist/serve.js.map +1 -0
  39. package/dist/ui.html +324 -0
  40. package/package.json +193 -4
  41. package/src/icon.svg +6 -0
  42. package/src/ui.html +324 -0
package/dist/index.js ADDED
@@ -0,0 +1,372 @@
1
+ #!/usr/bin/env node
2
+ // provider: the stack's LLM credentials, served to whoever asks.
3
+ //
4
+ // The node keeps no conversation. A caller sends the whole transcript, gets an answer, and the node
5
+ // remembers nothing between requests. Named rows live in history - or, in local mode, in the file
6
+ // `mode.ts` names, because that node serves this stack's own endpoint - and, without either, in this
7
+ // process; each row picks one of three wires: openai, anthropic or responses.
8
+ //
9
+ // providers {action: list | save | delete, name?, config?}
10
+ // chat {name, messages, tools?, max_completion_tokens?, previous_response_id?}
11
+ // start_stream {name, messages, tools?, max_completion_tokens?, previous_response_id?} -> {stream_id, topic}
12
+ // stream_ready {stream_id} -> {started}
13
+ // cancel_stream {stream_id} -> {canceled}
14
+ // ui
15
+ //
16
+ // A stream is three calls, not one, because a bus event is only delivered to a node that was already
17
+ // subscribed when it was published. The caller asks for a stream and is told its id, subscribes to
18
+ // `provider.stream.<id>`, and only then says the stream may start: the model is dialled after
19
+ // `stream_ready`, so not one delta can be published into a room nobody is in yet. The id is the
20
+ // whole address - never the caller's name, which would mix two agents' deltas and outlive the
21
+ // request - and it is forgotten as soon as the stream ends, fails or is cancelled.
22
+ import { randomUUID } from 'node:crypto';
23
+ import { existsSync, readFileSync, realpathSync } from 'node:fs';
24
+ import { fileURLToPath } from 'node:url';
25
+ import { Node, loadResource, paintPage, } from '@node-agent/sdk';
26
+ import {} from './providers/base.js';
27
+ import { buildProvider } from './providers/index.js';
28
+ import { localConfigPath } from './mode.js';
29
+ import { providerStore, validateName } from './persistency/index.js';
30
+ const PANEL = 'ui';
31
+ const MODEL_TIMEOUT = 120;
32
+ const PAGE = readFileSync(new URL('./ui.html', import.meta.url), 'utf8');
33
+ /** The page's words, read from beside the compiled JS (`npm run build` copies src/i18n there). */
34
+ const RESOURCE = loadResource(new URL('./i18n/', import.meta.url));
35
+ let mutation = Promise.resolve();
36
+ /** Serialise provider mutations so two saves never race over one history revision. */
37
+ async function withMutation(work) {
38
+ const previous = mutation;
39
+ let release = () => { };
40
+ mutation = new Promise((resolve) => {
41
+ release = resolve;
42
+ });
43
+ await previous;
44
+ try {
45
+ return await work();
46
+ }
47
+ finally {
48
+ release();
49
+ }
50
+ }
51
+ /** How long a stream id waits for its `stream_ready` before it is dropped: a caller that asked for a
52
+ * stream and never opened it must not leave a row of session state behind forever. */
53
+ export const STREAM_PENDING_MS = 60_000;
54
+ /** The streams this node is serving, keyed by the id it handed out for each one. */
55
+ const activeStreams = new Map();
56
+ function missingProvider(name, known) {
57
+ const others = [...known].sort().join(', ');
58
+ return `no provider "${name}" yet — open the provider page and add one${others ? ` (configured: ${others})` : ''}`;
59
+ }
60
+ function environmentProvider() {
61
+ const url = (process.env.NODE_AGENT_MODEL_URL ?? '').trim();
62
+ const key = (process.env.NODE_AGENT_MODEL_KEY ?? '').trim();
63
+ const model = (process.env.NODE_AGENT_MODEL_NAME ?? '').trim();
64
+ if (!url || !key || !model)
65
+ return null;
66
+ return {
67
+ type: 'openai',
68
+ api_key: key,
69
+ model_name: model,
70
+ base_url: url.replace(/\/$/, '').replace(/\/chat\/completions$/, ''),
71
+ timeout: MODEL_TIMEOUT,
72
+ thinking_effort: null,
73
+ };
74
+ }
75
+ class ProviderManager {
76
+ ctx;
77
+ /** Where the rows live: this bus's history, or this machine's file (see `persistency/`). */
78
+ store;
79
+ providers = {};
80
+ persistence = false;
81
+ constructor(ctx, store) {
82
+ this.ctx = ctx;
83
+ this.store = store;
84
+ }
85
+ state() {
86
+ return {
87
+ providers: Object.entries(this.providers).map(([name, config]) => {
88
+ const row = { ...config };
89
+ const apiKey = row.api_key;
90
+ delete row.api_key;
91
+ return { name, config: row, has_api_key: typeof apiKey === 'string' && apiKey !== '' };
92
+ }),
93
+ persistent: this.persistence,
94
+ };
95
+ }
96
+ /** Load the saved rows, then the environment, then one blank row.
97
+ *
98
+ * Which rows are "saved" is the store's business: history for the shared provider, a file for the
99
+ * local one, whose endpoint and credentials belong to the stack that started it. `persistence` is
100
+ * the store's answer to "will these survive this process", which is what the page shows.
101
+ */
102
+ async load() {
103
+ const saved = await this.store.read();
104
+ this.persistence = this.store.persistent;
105
+ this.providers = saved ?? this.seeded();
106
+ }
107
+ /** What a stack nobody configured starts with: the three model variables, else one blank row. */
108
+ seeded() {
109
+ const env = environmentProvider();
110
+ return env ? { default: env } : { default: { type: 'openai' } };
111
+ }
112
+ /** The rows as the provider's own `dump()`, which is the shape both stores hold. */
113
+ dumped() {
114
+ return Object.fromEntries(Object.entries(this.providers).map(([name, config]) => [
115
+ name,
116
+ buildProvider(config, this.ctx.fetch).dump(),
117
+ ]));
118
+ }
119
+ async persist() {
120
+ await this.store.write(this.dumped());
121
+ }
122
+ async handle(args) {
123
+ await this.load();
124
+ const action = String(args.action ?? 'list');
125
+ if (action === 'list')
126
+ return this.state();
127
+ return await withMutation(async () => {
128
+ await this.load();
129
+ const name = validateName(args.name);
130
+ if (action === 'delete') {
131
+ if (!(name in this.providers))
132
+ throw new Error(`provider "${name}" does not exist`);
133
+ const remaining = { ...this.providers };
134
+ delete remaining[name];
135
+ if (Object.keys(remaining).length === 0)
136
+ throw new Error('cannot delete the last provider');
137
+ this.providers = remaining;
138
+ await this.persist();
139
+ return this.state();
140
+ }
141
+ if (action !== 'save')
142
+ throw new Error(`unknown provider action "${action}"`);
143
+ const raw = args.config;
144
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
145
+ throw new Error('saving a provider needs a config object');
146
+ const config = { ...raw };
147
+ delete config.has_api_key;
148
+ const existing = this.providers[name];
149
+ if (!config.api_key && existing)
150
+ config.api_key = existing['api_key'];
151
+ if (!String(config.model_name ?? '').trim())
152
+ throw new Error('provider model_name must be set');
153
+ if (!config.api_key)
154
+ throw new Error('this provider needs an API key: leave the field blank only to keep the one it has');
155
+ // Rebuild once before saving: an unknown type or a bad timeout is refused here, not on a call.
156
+ buildProvider(config, this.ctx.fetch);
157
+ this.providers = { ...this.providers, [name]: config };
158
+ await this.persist();
159
+ return this.state();
160
+ });
161
+ }
162
+ chatRequest(args) {
163
+ const name = validateName(args.name);
164
+ const config = this.providers[name];
165
+ if (!config)
166
+ throw new Error(missingProvider(name, Object.keys(this.providers)));
167
+ const messages = args.messages;
168
+ if (!Array.isArray(messages))
169
+ throw new Error('provider chat needs a messages array');
170
+ const tools = args.tools;
171
+ if (tools !== undefined && tools !== null && !Array.isArray(tools))
172
+ throw new Error('provider chat tools must be an array or null');
173
+ const limit = args.max_completion_tokens;
174
+ if (limit !== undefined &&
175
+ limit !== null &&
176
+ (!Number.isInteger(limit) || typeof limit === 'boolean'))
177
+ throw new Error('max_completion_tokens must be an integer or null');
178
+ const previous = args.previous_response_id;
179
+ if (previous !== undefined && previous !== null && typeof previous !== 'string')
180
+ throw new Error('previous_response_id must be a string or null');
181
+ return {
182
+ // The row's name travels with the provider: a connection that fails says which row and which
183
+ // host, instead of the SDK's bare "Connection error."
184
+ provider: buildProvider(config, this.ctx.fetch, name),
185
+ messages: messages,
186
+ tools: tools ?? null,
187
+ limit: limit ?? null,
188
+ previous: previous ?? null,
189
+ };
190
+ }
191
+ async chat(args) {
192
+ await this.load();
193
+ const request = this.chatRequest(args);
194
+ try {
195
+ return await request.provider.chat(request.messages, request.tools, request.limit, request.previous);
196
+ }
197
+ finally {
198
+ await request.provider.close();
199
+ }
200
+ }
201
+ /** Ask for a stream: answer with its id, and dial nothing yet.
202
+ *
203
+ * The row, the transcript and the tools are checked here, so a caller that named an unknown
204
+ * provider or a broken tool list is told at once instead of through a stream event. What is
205
+ * deliberately not done here is the request itself: the model must not answer before the caller
206
+ * has subscribed to `provider.stream.<id>` (see `streamReady`). */
207
+ async startStream(args) {
208
+ await this.load();
209
+ const request = this.chatRequest(args);
210
+ const id = randomUUID().replaceAll('-', '');
211
+ const topic = `provider.stream.${id}`;
212
+ const session = {
213
+ id,
214
+ topic,
215
+ provider: request.provider,
216
+ messages: request.messages,
217
+ tools: request.tools,
218
+ limit: request.limit,
219
+ previous: request.previous,
220
+ controller: new AbortController(),
221
+ started: false,
222
+ expiry: null,
223
+ };
224
+ session.expiry = setTimeout(() => {
225
+ void this.#dropStream(session).catch(() => undefined);
226
+ }, STREAM_PENDING_MS);
227
+ session.expiry.unref?.();
228
+ activeStreams.set(id, session);
229
+ return { stream_id: id, topic };
230
+ }
231
+ /** Say the caller is listening: only now is the model dialled, and the answer arrives as events.
232
+ *
233
+ * Answering before the first delta exists is the whole point, so this never throws for a failure
234
+ * of the model: that failure is published as `error` on the stream's own topic, where the caller
235
+ * that is already listening will see it. */
236
+ streamReady(args) {
237
+ const session = this.#streamOf(args.stream_id);
238
+ if (session.started)
239
+ return { started: true };
240
+ session.started = true;
241
+ if (session.expiry !== null) {
242
+ clearTimeout(session.expiry);
243
+ session.expiry = null;
244
+ }
245
+ void this.#runStream(session).catch(() => undefined);
246
+ return { started: true };
247
+ }
248
+ /** Stop the stream the id names, whether it has started or not. */
249
+ cancelStream(args) {
250
+ const id = String(args.stream_id ?? '').trim();
251
+ const session = activeStreams.get(id);
252
+ if (session === undefined)
253
+ return { canceled: false };
254
+ if (session.started) {
255
+ session.controller.abort();
256
+ return { canceled: true };
257
+ }
258
+ void this.#dropStream(session, true).catch(() => undefined);
259
+ return { canceled: true };
260
+ }
261
+ /** The stream one id names, or the refusal that says why there is none. */
262
+ #streamOf(value) {
263
+ const id = String(value ?? '').trim();
264
+ if (id === '')
265
+ throw new Error('a provider stream call needs a stream_id');
266
+ const session = activeStreams.get(id);
267
+ if (session === undefined)
268
+ throw new Error(`no provider stream "${id}"`);
269
+ return session;
270
+ }
271
+ /** Publish one event of a stream, and never let a bus that has gone away turn a finished stream
272
+ * into a crash. These publishes happen after the call that asked for the stream was already
273
+ * answered, so a throw here has no caller to go to: it would end the whole node process, and
274
+ * every other caller's stream with it. */
275
+ #emit(session, value) {
276
+ try {
277
+ this.ctx.publish(session.topic, value);
278
+ }
279
+ catch {
280
+ // The bus is gone; so is every reader of this event.
281
+ }
282
+ }
283
+ /** Publish one stream's events until it ends, then forget it however it ended. */
284
+ async #runStream(session) {
285
+ const { id, controller } = session;
286
+ let canceled = false;
287
+ try {
288
+ const stream = session.provider.chatStream(session.messages, session.tools, session.limit, session.previous, controller.signal);
289
+ for await (const chunk of stream) {
290
+ if (controller.signal.aborted) {
291
+ canceled = true;
292
+ break;
293
+ }
294
+ this.#emit(session, { stream_id: id, event: 'chunk', chunk });
295
+ }
296
+ // An iterator that ends without throwing after a cancel ended it is still a cancelled stream:
297
+ // the caller asked for it to stop, and the `done` must say so.
298
+ if (controller.signal.aborted)
299
+ canceled = true;
300
+ }
301
+ catch (error) {
302
+ if (controller.signal.aborted)
303
+ canceled = true;
304
+ else
305
+ this.#emit(session, {
306
+ stream_id: id,
307
+ event: 'error',
308
+ error: error instanceof Error ? error.message : String(error),
309
+ });
310
+ }
311
+ finally {
312
+ if (activeStreams.get(id) === session)
313
+ activeStreams.delete(id);
314
+ if (session.expiry !== null) {
315
+ clearTimeout(session.expiry);
316
+ session.expiry = null;
317
+ }
318
+ try {
319
+ await session.provider.close();
320
+ }
321
+ catch {
322
+ // Closing the wire is the provider's business; a stream that already ended says no more.
323
+ }
324
+ this.#emit(session, { stream_id: id, event: 'done', canceled });
325
+ }
326
+ }
327
+ /** Forget a stream that never ran: the caller asked for it and never opened it, or cancelled it
328
+ * before the model was dialled. The provider row is closed so the wire does not outlive it. */
329
+ async #dropStream(session, canceled = false) {
330
+ if (session.started)
331
+ return;
332
+ if (activeStreams.get(session.id) === session)
333
+ activeStreams.delete(session.id);
334
+ if (session.expiry !== null) {
335
+ clearTimeout(session.expiry);
336
+ session.expiry = null;
337
+ }
338
+ try {
339
+ await session.provider.close();
340
+ }
341
+ catch {
342
+ // Nothing was in flight: a close that cannot answer changes nothing.
343
+ }
344
+ this.#emit(session, { stream_id: session.id, event: 'done', canceled });
345
+ }
346
+ }
347
+ export class ProviderNode extends Node {
348
+ handlers;
349
+ constructor(ctx) {
350
+ super(ctx);
351
+ const manager = new ProviderManager(ctx, providerStore(ctx, localConfigPath(ctx.nodeId, process.env, ctx.home)));
352
+ this.handlers = {
353
+ providers: (args) => manager.handle(args),
354
+ chat: (args) => manager.chat(args),
355
+ start_stream: (args) => manager.startStream(args),
356
+ stream_ready: (args) => manager.streamReady(args),
357
+ cancel_stream: (args) => manager.cancelStream(args),
358
+ // The registered bus name, not the package id: the page calls back with it.
359
+ // `paintPage` writes the page's words in the language this visit asked for, and leaves the
360
+ // markers the browser half redraws when the user switches language.
361
+ [PANEL]: (args) => paintPage(PAGE, RESOURCE, args).replace('"__NODE__"', () => JSON.stringify(ctx.nodeId)),
362
+ };
363
+ }
364
+ }
365
+ export default ProviderNode;
366
+ // `npx @node-agent/provider` runs this file. Keep the library import side-effect free: only a direct
367
+ // invocation hands over to the process entry, and `--local` is read there from the same argv.
368
+ const invoked = process.argv[1];
369
+ if (invoked && existsSync(invoked) && realpathSync(invoked) === fileURLToPath(import.meta.url)) {
370
+ await import('./serve.js');
371
+ }
372
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,iEAAiE;AACjE,EAAE;AACF,oGAAoG;AACpG,kGAAkG;AAClG,qGAAqG;AACrG,8EAA8E;AAC9E,EAAE;AACF,6DAA6D;AAC7D,iFAAiF;AACjF,+GAA+G;AAC/G,0CAA0C;AAC1C,4CAA4C;AAC5C,OAAO;AACP,EAAE;AACF,qGAAqG;AACrG,mGAAmG;AACnG,8FAA8F;AAC9F,gGAAgG;AAChG,8FAA8F;AAC9F,mFAAmF;AACnF,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EACL,IAAI,EACJ,YAAY,EACZ,SAAS,GAKV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAKN,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,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,IAAI,EAAE,EAAE,CAChB,SAAS,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;SAC1F,CAAC;IACJ,CAAC;CACF;AAED,eAAe,YAAY,CAAC;AAE5B,qGAAqG;AACrG,8FAA8F;AAC9F,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAChC,IAAI,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,IAAI,YAAY,CAAC,OAAO,CAAC,KAAK,aAAa,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;IAC/F,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC;AAC7B,CAAC"}
package/dist/mode.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ export declare const PROVIDER_ID = "provider";
2
+ export declare const LOCAL_PROVIDER_ID = "local-provider";
3
+ /** The environment variable that asks for local mode instead of the flag (a supervisor's way). */
4
+ export declare const LOCAL_ENV = "NODE_AGENT_LOCAL";
5
+ export type ProviderMode = {
6
+ readonly local: boolean;
7
+ /** The id to register under: `local-provider` in local mode, `provider` otherwise. */
8
+ readonly id: string;
9
+ };
10
+ /** Read `--local` (or NODE_AGENT_LOCAL=1) off the command line this process was started with. */
11
+ export declare function providerMode(argv: readonly string[], env?: Record<string, string | undefined>): ProviderMode;
12
+ /** The variable that names the file local mode keeps its rows in, instead of the bus's history. */
13
+ export declare const CONFIG_ENV = "NODE_AGENT_PROVIDER_CONFIG";
14
+ /** The file name inside this node's own directory (`<node-agent-home>/<node-name>/`). */
15
+ export declare const LOCAL_CONFIG_FILE = "local-provider.json";
16
+ /** The file a local provider keeps its provider rows in, or null when this process is the shared one.
17
+ *
18
+ * Local mode is the address, not a second switch: the node that answers as `local-provider` points at
19
+ * the endpoint this stack runs itself, so its credentials belong to that stack, on that machine - a
20
+ * file - while `provider` serves a whole bus and keeps its rows in the bus's history as before. The
21
+ * name is the one the host registered the node under (`ctx.nodeId`), not something read back from
22
+ * the environment. A file
23
+ * that cannot be parsed fails rather than starting over: an empty list would show one blank row and let
24
+ * the page's next save overwrite every row the file held.
25
+ */
26
+ export declare function localConfigPath(id: string, env?: Record<string, string | undefined>, home?: string): string | null;
package/dist/mode.js ADDED
@@ -0,0 +1,39 @@
1
+ // Which name this provider process registers under.
2
+ //
3
+ // One node package, two addresses: `provider` is the shared node a whole bus may use, and
4
+ // `local-provider` is the same node pointed at the endpoint this stack runs itself (a model server
5
+ // on the same host, say). A stack that runs both wants the agent to prefer the near one, so the
6
+ // choice is made here, from the command line, and told to the package by name.
7
+ import { join } from 'node:path';
8
+ import { nodeAgentDir } from '@node-agent/sdk';
9
+ export const PROVIDER_ID = 'provider';
10
+ export const LOCAL_PROVIDER_ID = 'local-provider';
11
+ /** The environment variable that asks for local mode instead of the flag (a supervisor's way). */
12
+ export const LOCAL_ENV = 'NODE_AGENT_LOCAL';
13
+ /** Read `--local` (or NODE_AGENT_LOCAL=1) off the command line this process was started with. */
14
+ export function providerMode(argv, env = process.env) {
15
+ const named = env[LOCAL_ENV]?.trim().toLowerCase();
16
+ const local = argv.includes('--local') || named === '1' || named === 'true';
17
+ return { local, id: local ? LOCAL_PROVIDER_ID : PROVIDER_ID };
18
+ }
19
+ /** The variable that names the file local mode keeps its rows in, instead of the bus's history. */
20
+ export const CONFIG_ENV = 'NODE_AGENT_PROVIDER_CONFIG';
21
+ /** The file name inside this node's own directory (`<node-agent-home>/<node-name>/`). */
22
+ export const LOCAL_CONFIG_FILE = 'local-provider.json';
23
+ /** The file a local provider keeps its provider rows in, or null when this process is the shared one.
24
+ *
25
+ * Local mode is the address, not a second switch: the node that answers as `local-provider` points at
26
+ * the endpoint this stack runs itself, so its credentials belong to that stack, on that machine - a
27
+ * file - while `provider` serves a whole bus and keeps its rows in the bus's history as before. The
28
+ * name is the one the host registered the node under (`ctx.nodeId`), not something read back from
29
+ * the environment. A file
30
+ * that cannot be parsed fails rather than starting over: an empty list would show one blank row and let
31
+ * the page's next save overwrite every row the file held.
32
+ */
33
+ export function localConfigPath(id, env = process.env, home) {
34
+ if (id !== LOCAL_PROVIDER_ID)
35
+ return null;
36
+ const named = env[CONFIG_ENV]?.trim();
37
+ return named ? named : join(home ?? nodeAgentDir(id), LOCAL_CONFIG_FILE);
38
+ }
39
+ //# sourceMappingURL=mode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mode.js","sourceRoot":"","sources":["../src/mode.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,EAAE;AACF,0FAA0F;AAC1F,mGAAmG;AACnG,gGAAgG;AAChG,+EAA+E;AAC/E,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,gBAAgB,CAAC;AAElD,kGAAkG;AAClG,MAAM,CAAC,MAAM,SAAS,GAAG,kBAAkB,CAAC;AAQ5C,iGAAiG;AACjG,MAAM,UAAU,YAAY,CAC1B,IAAuB,EACvB,GAAG,GAAuC,OAAO,CAAC,GAAG;IAErD,MAAM,KAAK,GAAG,GAAG,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACnD,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,KAAK,KAAK,GAAG,IAAI,KAAK,KAAK,MAAM,CAAC;IAC5E,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAChE,CAAC;AAED,mGAAmG;AACnG,MAAM,CAAC,MAAM,UAAU,GAAG,4BAA4B,CAAC;AAEvD,yFAAyF;AACzF,MAAM,CAAC,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;AAEvD;;;;;;;;;GASG;AACH,MAAM,UAAU,eAAe,CAC7B,EAAU,EACV,GAAG,GAAuC,OAAO,CAAC,GAAG,EACrD,IAAa;IAEb,IAAI,EAAE,KAAK,iBAAiB;QAAE,OAAO,IAAI,CAAC;IAC1C,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,CAAC;IACtC,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,YAAY,CAAC,EAAE,CAAC,EAAE,iBAAiB,CAAC,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { NodeContext } from '@node-agent/sdk';
2
+ import { type ProviderRows, type ProviderStore } from './index.js';
3
+ export declare class HistoryStore implements ProviderStore {
4
+ readonly ctx: NodeContext;
5
+ revision: number | null;
6
+ persistent: boolean;
7
+ constructor(ctx: NodeContext);
8
+ private get tenant();
9
+ read(): Promise<ProviderRows | null>;
10
+ write(rows: ProviderRows): Promise<void>;
11
+ }
@@ -0,0 +1,50 @@
1
+ import { rowsIn } from './index.js';
2
+ const HISTORY_KEY = 'providers';
3
+ /** Rows saved while the bus has no history node. One process is one lifetime. */
4
+ const ephemeral = new Map();
5
+ export class HistoryStore {
6
+ ctx;
7
+ revision = null;
8
+ persistent = false;
9
+ constructor(ctx) {
10
+ this.ctx = ctx;
11
+ }
12
+ get tenant() {
13
+ return this.ctx.tenantId || 'default';
14
+ }
15
+ async read() {
16
+ try {
17
+ const reply = await this.ctx.call('history', 'load', {
18
+ unitId: HISTORY_KEY,
19
+ });
20
+ this.persistent = true;
21
+ const unit = reply.unit ?? null;
22
+ if (unit === null)
23
+ return null;
24
+ const rows = rowsIn(unit.meta?.providers);
25
+ this.revision = typeof unit.revision === 'number' ? unit.revision : null;
26
+ return Object.keys(rows).length > 0 ? rows : null;
27
+ }
28
+ catch {
29
+ // No history on this bus (or no unit yet): this process keeps the rows instead.
30
+ this.persistent = false;
31
+ const local = ephemeral.get(this.tenant);
32
+ this.revision = local?.revision ?? null;
33
+ return local && Object.keys(local.rows).length > 0 ? local.rows : null;
34
+ }
35
+ }
36
+ async write(rows) {
37
+ if (!this.persistent) {
38
+ ephemeral.set(this.tenant, { rows, revision: this.revision });
39
+ return;
40
+ }
41
+ const request = { unitId: HISTORY_KEY, meta: { providers: rows } };
42
+ if (this.revision !== null)
43
+ request.expectedRevision = this.revision;
44
+ const reply = await this.ctx.call('history', 'save', request);
45
+ const revision = reply.unit?.revision;
46
+ if (typeof revision === 'number')
47
+ this.revision = revision;
48
+ }
49
+ }
50
+ //# sourceMappingURL=history.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"history.js","sourceRoot":"","sources":["../../src/persistency/history.ts"],"names":[],"mappings":"AAQA,OAAO,EAAyC,MAAM,EAAE,MAAM,YAAY,CAAC;AAE3E,MAAM,WAAW,GAAG,WAAW,CAAC;AAEhC,iFAAiF;AACjF,MAAM,SAAS,GAAG,IAAI,GAAG,EAA2D,CAAC;AAErF,MAAM,OAAO,YAAY;IACd,GAAG,CAAc;IAC1B,QAAQ,GAAkB,IAAI,CAAC;IAC/B,UAAU,GAAG,KAAK,CAAC;IAEnB,YAAY,GAAgB;QAC1B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED,IAAY,MAAM;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,IAAI,SAAS,CAAC;IACxC,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAA+B,SAAS,EAAE,MAAM,EAAE;gBACjF,MAAM,EAAE,WAAW;aACpB,CAAC,CAAC;YACH,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC;YAChC,I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CAAC,QAAQ,CAAC;QACrE,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAC/B,SAAS,EACT,MAAM,EACN,OAAO,CACR,CAAC;QACF,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC;QACtC,IAAI,OAAO,QAAQ,KAAK,QAAQ;YAAE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7D,CAAC;CACF"}
@@ -0,0 +1,23 @@
1
+ import type { NodeContext } from '@node-agent/sdk';
2
+ import type { JsonObject } from '../providers/base.js';
3
+ /** One provider row, exactly as its `dump()` wrote it. */
4
+ export type ProviderRow = JsonObject;
5
+ export type ProviderRows = Record<string, ProviderRow>;
6
+ export interface ProviderStore {
7
+ /** Whether the rows may be expected to survive this process; the page shows it as `persistent`. */
8
+ persistent: boolean;
9
+ /** The rows that were saved, or null when nothing has been saved yet. */
10
+ read(): Promise<ProviderRows | null>;
11
+ /** Replace the saved rows with these. */
12
+ write(rows: ProviderRows): Promise<void>;
13
+ }
14
+ /** A provider name, as a row key and as a caller spells it. */
15
+ export declare function validateName(value: unknown): string;
16
+ /** The rows a saved document holds, refusing anything that is not an object of objects.
17
+ *
18
+ * Shared by both stores: history and the file hold the same document, so a file that was hand-edited
19
+ * into nonsense is refused with the same sentence a history unit would be.
20
+ */
21
+ export declare function rowsIn(configs: unknown): ProviderRows;
22
+ /** The store this process answers with: the file `configPath` names, or the bus's history. */
23
+ export declare function providerStore(ctx: NodeContext, configPath: string | null): ProviderStore;
@@ -0,0 +1,33 @@
1
+ import { HistoryStore } from './history.js';
2
+ import { LocalStore } from './local.js';
3
+ const NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
4
+ /** A provider name, as a row key and as a caller spells it. */
5
+ export function validateName(value) {
6
+ const name = String(value ?? '').trim();
7
+ if (!NAME.test(name))
8
+ throw new Error('provider name must use 1-64 letters, numbers, underscores, or hyphens');
9
+ return name;
10
+ }
11
+ /** The rows a saved document holds, refusing anything that is not an object of objects.
12
+ *
13
+ * Shared by both stores: history and the file hold the same document, so a file that was hand-edited
14
+ * into nonsense is refused with the same sentence a history unit would be.
15
+ */
16
+ export function rowsIn(configs) {
17
+ if (configs !== undefined &&
18
+ configs !== null &&
19
+ (typeof configs !== 'object' || Array.isArray(configs)))
20
+ throw new Error('saved providers must be an object');
21
+ const rows = {};
22
+ for (const [name, config] of Object.entries(configs ?? {})) {
23
+ if (config === null || typeof config !== 'object' || Array.isArray(config))
24
+ throw new Error(`provider "${name}" config must be an object`);
25
+ rows[validateName(name)] = config;
26
+ }
27
+ return rows;
28
+ }
29
+ /** The store this process answers with: the file `configPath` names, or the bus's history. */
30
+ export function providerStore(ctx, configPath) {
31
+ return configPath === null ? new HistoryStore(ctx) : new LocalStore(configPath);
32
+ }
33
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/persistency/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAiBxC,MAAM,IAAI,GAAG,kCAAkC,CAAC;AAEhD,+DAA+D;AAC/D,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACxC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,uEAAuE,CAAC,CAAC;IAC3F,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,OAAgB;IACrC,IACE,OAAO,KAAK,SAAS;QACrB,OAAO,KAAK,IAAI;QAChB,CAAC,OAAO,OAAO,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAEvD,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvD,MAAM,IAAI,GAAiB,EAAE,CAAC;IAC9B,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAE,OAAkC,IAAI,EAAE,CAAC,EAAE,CAAC;QACvF,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;YACxE,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,4BAA4B,CAAC,CAAC;QACjE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,GAAG,MAAqB,CAAC;IACnD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,8FAA8F;AAC9F,MAAM,UAAU,aAAa,CAAC,GAAgB,EAAE,UAAyB;IACvE,OAAO,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC;AAClF,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { type ProviderRows, type ProviderStore } from './index.js';
2
+ export declare class LocalStore implements ProviderStore {
3
+ readonly path: string;
4
+ /** A file survives the process, so the page is told the rows are kept. */
5
+ persistent: boolean;
6
+ constructor(path: string);
7
+ /** The rows this stack saved for itself, or null when it has not saved any yet.
8
+ *
9
+ * A file that is there and cannot be parsed is an error, not an empty list: starting over would
10
+ * show one blank row and let the page's next save overwrite every row it hid.
11
+ */
12
+ read(): Promise<ProviderRows | null>;
13
+ /** Write the rows where that file lives, through a temporary name so a crash leaves the old one.
14
+ *
15
+ * The rows carry API keys, so the file is `0600` and its directory `0700`: unencrypted, but not
16
+ * readable by every other user on the machine. The temporary file is chmodded too - a stale one
17
+ * from an earlier crash would otherwise keep whatever mode it was made with.
18
+ */
19
+ write(rows: ProviderRows): Promise<void>;
20
+ }