norns-sdk 0.2.0 → 0.3.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/dist/index.d.cts CHANGED
@@ -264,6 +264,12 @@ declare class NornsClient {
264
264
  delete: (agent: AgentRef, key: string) => Promise<void>;
265
265
  };
266
266
  }
267
+ /**
268
+ * A key issued by `POST /api/v1/api_keys` with a worker scope (`nrnw_...`).
269
+ * It opens the worker socket and nothing else, so a worker holding one must
270
+ * not try the REST API.
271
+ */
272
+ declare function isScopedKey(apiKey: string): boolean;
267
273
 
268
274
  /**
269
275
  * Where the SDK writes what it is doing. Anything with these four methods
@@ -301,7 +307,11 @@ interface NornsWorkerOptions {
301
307
  agents: AgentDefinition | AgentDefinition[];
302
308
  /** The Norns base URL. Defaults to `NORNS_URL`. */
303
309
  url?: string;
304
- /** The tenant's bearer token. Defaults to `NORNS_API_KEY`. */
310
+ /**
311
+ * The bearer token. Defaults to `NORNS_API_KEY`. A scoped worker key
312
+ * (`nrnw_...`) joins without registering the agents, since it cannot reach
313
+ * the REST API; register the definitions with a tenant key.
314
+ */
305
315
  apiKey?: string;
306
316
  /**
307
317
  * How a model name on an LLM task becomes a model to call: a function, a
@@ -582,6 +592,10 @@ declare class JoinError extends NornsError {
582
592
  declare class GardDestroyedError extends NornsError {
583
593
  constructor(gard: string | number | undefined);
584
594
  }
595
+ /** The scoped key this worker joined on was revoked while it was connected. */
596
+ declare class KeyRevokedError extends NornsError {
597
+ constructor();
598
+ }
585
599
  /** A stream's socket closed before the run reached a terminal event. */
586
600
  declare class StreamClosedError extends NornsError {
587
601
  readonly reason: unknown;
@@ -675,5 +689,5 @@ type ToolResult = {
675
689
  error: string;
676
690
  };
677
691
 
678
- export { DEFAULT_MODEL, GardDestroyedError, JoinError, NornsApiError, NornsClient, NornsError, NornsTimeoutError, NornsWorker, NotFoundError, StreamClosedError, consoleLogger, defineAgent, inferProvider, silentLogger, tool };
692
+ export { DEFAULT_MODEL, GardDestroyedError, JoinError, KeyRevokedError, NornsApiError, NornsClient, NornsError, NornsTimeoutError, NornsWorker, NotFoundError, StreamClosedError, consoleLogger, defineAgent, inferProvider, isScopedKey, silentLogger, tool };
679
693
  export type { AgentDefinition, AgentInfo, AgentMode, AgentOptions, AgentRef, Capability, CheckpointPolicy, ContextPolicy, ContextStrategy, Conversation, InferToolInput, ListRunsOptions, LlmResult, LlmTask, Logger, MessageResult, ModelResolver, ModelSource, ModelsOption, NeutralMessage, NeutralToolCall, NornsClientOptions, NornsWorkerOptions, OnFailure, RegisterPortOptions, RegisteredPort, Run, RunEvent, RunStatus, SendMessageOptions, StreamEvent, StreamOptions, SubagentConversation, SubagentPolicy, ToolContext, ToolDefinition, ToolInputSchema, ToolOptions, ToolResult, ToolTask, WaitingFor, WebSocketConstructor };
package/dist/index.d.ts CHANGED
@@ -264,6 +264,12 @@ declare class NornsClient {
264
264
  delete: (agent: AgentRef, key: string) => Promise<void>;
265
265
  };
266
266
  }
267
+ /**
268
+ * A key issued by `POST /api/v1/api_keys` with a worker scope (`nrnw_...`).
269
+ * It opens the worker socket and nothing else, so a worker holding one must
270
+ * not try the REST API.
271
+ */
272
+ declare function isScopedKey(apiKey: string): boolean;
267
273
 
268
274
  /**
269
275
  * Where the SDK writes what it is doing. Anything with these four methods
@@ -301,7 +307,11 @@ interface NornsWorkerOptions {
301
307
  agents: AgentDefinition | AgentDefinition[];
302
308
  /** The Norns base URL. Defaults to `NORNS_URL`. */
303
309
  url?: string;
304
- /** The tenant's bearer token. Defaults to `NORNS_API_KEY`. */
310
+ /**
311
+ * The bearer token. Defaults to `NORNS_API_KEY`. A scoped worker key
312
+ * (`nrnw_...`) joins without registering the agents, since it cannot reach
313
+ * the REST API; register the definitions with a tenant key.
314
+ */
305
315
  apiKey?: string;
306
316
  /**
307
317
  * How a model name on an LLM task becomes a model to call: a function, a
@@ -582,6 +592,10 @@ declare class JoinError extends NornsError {
582
592
  declare class GardDestroyedError extends NornsError {
583
593
  constructor(gard: string | number | undefined);
584
594
  }
595
+ /** The scoped key this worker joined on was revoked while it was connected. */
596
+ declare class KeyRevokedError extends NornsError {
597
+ constructor();
598
+ }
585
599
  /** A stream's socket closed before the run reached a terminal event. */
586
600
  declare class StreamClosedError extends NornsError {
587
601
  readonly reason: unknown;
@@ -675,5 +689,5 @@ type ToolResult = {
675
689
  error: string;
676
690
  };
677
691
 
678
- export { DEFAULT_MODEL, GardDestroyedError, JoinError, NornsApiError, NornsClient, NornsError, NornsTimeoutError, NornsWorker, NotFoundError, StreamClosedError, consoleLogger, defineAgent, inferProvider, silentLogger, tool };
692
+ export { DEFAULT_MODEL, GardDestroyedError, JoinError, KeyRevokedError, NornsApiError, NornsClient, NornsError, NornsTimeoutError, NornsWorker, NotFoundError, StreamClosedError, consoleLogger, defineAgent, inferProvider, isScopedKey, silentLogger, tool };
679
693
  export type { AgentDefinition, AgentInfo, AgentMode, AgentOptions, AgentRef, Capability, CheckpointPolicy, ContextPolicy, ContextStrategy, Conversation, InferToolInput, ListRunsOptions, LlmResult, LlmTask, Logger, MessageResult, ModelResolver, ModelSource, ModelsOption, NeutralMessage, NeutralToolCall, NornsClientOptions, NornsWorkerOptions, OnFailure, RegisterPortOptions, RegisteredPort, Run, RunEvent, RunStatus, SendMessageOptions, StreamEvent, StreamOptions, SubagentConversation, SubagentPolicy, ToolContext, ToolDefinition, ToolInputSchema, ToolOptions, ToolResult, ToolTask, WaitingFor, WebSocketConstructor };
package/dist/index.js CHANGED
@@ -206,6 +206,12 @@ var GardDestroyedError = class extends NornsError {
206
206
  this.name = "GardDestroyedError";
207
207
  }
208
208
  };
209
+ var KeyRevokedError = class extends NornsError {
210
+ constructor() {
211
+ super("the worker's scoped key was revoked");
212
+ this.name = "KeyRevokedError";
213
+ }
214
+ };
209
215
  var StreamClosedError = class extends NornsError {
210
216
  reason;
211
217
  constructor(reason) {
@@ -534,6 +540,9 @@ function resolveApiKey(apiKey) {
534
540
  if (!value) throw new NornsError("Norns API key missing: pass `apiKey` or set NORNS_API_KEY");
535
541
  return value;
536
542
  }
543
+ function isScopedKey(apiKey) {
544
+ return apiKey.startsWith("nrnw_");
545
+ }
537
546
  function wsBase(url) {
538
547
  return url.replace(/^http:\/\//, "ws://").replace(/^https:\/\//, "wss://");
539
548
  }
@@ -1247,9 +1256,15 @@ var NornsWorker = class {
1247
1256
  return this.#starting;
1248
1257
  }
1249
1258
  async #start() {
1250
- for (const agent of this.#agents) {
1251
- const info = await this.#client.agents.ensure(agent);
1252
- this.#logger.info(`Registered agent '${agent.name}' (id=${info.id})`);
1259
+ if (isScopedKey(this.#apiKey)) {
1260
+ this.#logger.info(
1261
+ "Worker key is scoped (nrnw_): skipping agent registration. Register the agent definitions with a tenant key."
1262
+ );
1263
+ } else {
1264
+ for (const agent of this.#agents) {
1265
+ const info = await this.#client.agents.ensure(agent);
1266
+ this.#logger.info(`Registered agent '${agent.name}' (id=${info.id})`);
1267
+ }
1253
1268
  }
1254
1269
  if (this.#capabilities.includes("llm")) {
1255
1270
  for (const model of new Set(this.#agents.map((a) => a.model))) {
@@ -1300,6 +1315,10 @@ var NornsWorker = class {
1300
1315
  this.#logger.error("This worker's gard was destroyed; shutting down");
1301
1316
  this.#fail(new GardDestroyedError(this.gard));
1302
1317
  });
1318
+ channel.on("key_revoked", () => {
1319
+ this.#logger.error("This worker's key was revoked; shutting down");
1320
+ this.#fail(new KeyRevokedError());
1321
+ });
1303
1322
  channel.onError((reason) => {
1304
1323
  if (this.#state === "running")
1305
1324
  this.#logger.warn(`Channel error (${errorMessage(reason)}); rejoining`);
@@ -1612,6 +1631,7 @@ export {
1612
1631
  DEFAULT_MODEL,
1613
1632
  GardDestroyedError,
1614
1633
  JoinError,
1634
+ KeyRevokedError,
1615
1635
  NornsApiError,
1616
1636
  NornsClient,
1617
1637
  NornsError,
@@ -1622,6 +1642,7 @@ export {
1622
1642
  consoleLogger,
1623
1643
  defineAgent,
1624
1644
  inferProvider,
1645
+ isScopedKey,
1625
1646
  jsonSchema3 as jsonSchema,
1626
1647
  silentLogger,
1627
1648
  tool