@atbash/sdk 0.3.11-dev.12 → 0.3.11-dev.13

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.cjs CHANGED
@@ -61,6 +61,7 @@ __export(index_exports, {
61
61
  loadUserConfig: () => loadUserConfig,
62
62
  normalizeForMatching: () => normalizeForMatching,
63
63
  resolve: () => resolve,
64
+ resolveChainForOrg: () => resolveChainForOrg,
64
65
  resolveKeyPath: () => resolveKeyPath,
65
66
  saveUserConfig: () => saveUserConfig,
66
67
  scanMemory: () => scanMemory,
@@ -320,8 +321,8 @@ async function _checkAgentExists(pubkey, opts, chainOpts) {
320
321
  throw err;
321
322
  }
322
323
  }
323
- async function checkAgentExists(pubkey, opts) {
324
- return _checkAgentExists(pubkey, opts);
324
+ async function checkAgentExists(pubkey, opts, chainOpts) {
325
+ return _checkAgentExists(pubkey, opts, chainOpts);
325
326
  }
326
327
  async function logToolCall(action, context, auth, chainOpts, extra, clientOpts) {
327
328
  const start = performance.now();
@@ -395,7 +396,9 @@ function enrichError(status, body, statusText, opts) {
395
396
  return new Error(message);
396
397
  }
397
398
  async function postJson(url, body, opts) {
398
- const headers = { "Content-Type": "application/json" };
399
+ const headers = {
400
+ "Content-Type": "application/json"
401
+ };
399
402
  if (opts?.auth) {
400
403
  headers["Authorization"] = `Bearer ${await getOrCreateAuthBearer(opts.auth)}`;
401
404
  }
@@ -461,9 +464,14 @@ async function judgeAction(action, context = "", auth, opts) {
461
464
  }
462
465
  try {
463
466
  let chainOpts = opts?.chainOpts;
464
- if (opts?.orgName && !chainOpts?.blockchainRid) {
465
- const resolved = await resolveChainForOrg(opts.orgName, opts);
466
- chainOpts = { ...chainOpts, network: resolved.network };
467
+ if (opts?.orgName) {
468
+ const mapNetwork = await getActiveNetworkForOrg(opts.orgName, opts);
469
+ if (mapNetwork) {
470
+ chainOpts = { network: mapNetwork };
471
+ } else if (!chainOpts?.blockchainRid) {
472
+ const resolved = await resolveChainForOrg(opts.orgName, opts);
473
+ chainOpts = { ...chainOpts, network: resolved.network };
474
+ }
467
475
  }
468
476
  const logResult = await logToolCall(
469
477
  action,
@@ -492,7 +500,9 @@ async function judgeAction(action, context = "", auth, opts) {
492
500
  agent_pubkey: auth.pubkey,
493
501
  action,
494
502
  signed_log_tool_call: logResult.signedHex,
495
- ...signedJudgeActionHex && { signed_judge_action: signedJudgeActionHex },
503
+ ...signedJudgeActionHex && {
504
+ signed_judge_action: signedJudgeActionHex
505
+ },
496
506
  ...context && { context },
497
507
  ...opts?.provider && { provider: opts.provider },
498
508
  ...opts?.toolName && { tool_name: opts.toolName },
@@ -537,9 +547,10 @@ async function getJudgmentStatus(judgmentId, agentPubkey, opts) {
537
547
  throw err;
538
548
  }
539
549
  }
540
- function riskEngineUrl(action, params, opts) {
550
+ function riskEngineUrl(action, params, opts, network) {
541
551
  const url = new URL(`${baseUrl(opts)}/api/risk-engine`);
542
552
  url.searchParams.set("action", action);
553
+ if (network) url.searchParams.set("network", network);
543
554
  for (const [k, v] of Object.entries(params)) {
544
555
  if (v) url.searchParams.set(k, v);
545
556
  }
@@ -644,12 +655,14 @@ function coerceOrgSubscription(row, orgName) {
644
655
  is_active: Boolean(r.is_active)
645
656
  };
646
657
  }
647
- async function getOrgSubscription(orgName, opts) {
658
+ async function getOrgSubscription(orgName, opts, network) {
648
659
  const start = performance.now();
649
660
  recordCall("getOrgSubscription");
650
661
  try {
662
+ const params = { org: orgName };
663
+ if (network) params.network = network;
651
664
  const result = await getJson(
652
- riskEngineUrl("org-subscription", { org: orgName }, opts),
665
+ riskEngineUrl("org-subscription", params, opts),
653
666
  opts
654
667
  );
655
668
  recordDuration("getOrgSubscription", performance.now() - start, "success");
@@ -659,13 +672,47 @@ async function getOrgSubscription(orgName, opts) {
659
672
  throw err;
660
673
  }
661
674
  }
675
+ async function getActiveNetworkForOrg(orgName, opts) {
676
+ try {
677
+ const url = `${baseUrl(opts)}/api/org-network?org=${encodeURIComponent(orgName)}`;
678
+ const data = await getJson(url, opts);
679
+ if (data?.network === "public" || data?.network === "private") {
680
+ return data.network;
681
+ }
682
+ return null;
683
+ } catch {
684
+ return null;
685
+ }
686
+ }
662
687
  var _chainCache = /* @__PURE__ */ new Map();
663
688
  async function resolveChainForOrg(orgName, opts) {
664
689
  const cached = _chainCache.get(orgName);
665
690
  if (cached) return cached;
691
+ const mapNetwork = await getActiveNetworkForOrg(orgName, opts);
692
+ if (mapNetwork) {
693
+ const chain = mapNetwork === "private" ? PRIVATE_CHAIN : PUBLIC_CHAIN;
694
+ _chainCache.set(orgName, chain);
695
+ return chain;
696
+ }
666
697
  try {
667
- const sub = await getOrgSubscription(orgName, opts);
668
- if (sub?.is_private_blockchain) {
698
+ const [pubSub, privSub] = await Promise.all([
699
+ getOrgSubscription(orgName, opts, "public").catch(() => null),
700
+ getOrgSubscription(orgName, opts, "private").catch(() => null)
701
+ ]);
702
+ if (pubSub?.is_private_blockchain) {
703
+ _chainCache.set(orgName, PRIVATE_CHAIN);
704
+ return PRIVATE_CHAIN;
705
+ }
706
+ if (pubSub && privSub) {
707
+ const chain = privSub.assigned_at > pubSub.assigned_at ? PRIVATE_CHAIN : PUBLIC_CHAIN;
708
+ _chainCache.set(orgName, chain);
709
+ return chain;
710
+ }
711
+ if (pubSub) {
712
+ _chainCache.set(orgName, PUBLIC_CHAIN);
713
+ return PUBLIC_CHAIN;
714
+ }
715
+ if (privSub?.is_private_blockchain) {
669
716
  _chainCache.set(orgName, PRIVATE_CHAIN);
670
717
  return PRIVATE_CHAIN;
671
718
  }
@@ -686,7 +733,11 @@ async function getPendingHeldActions(orgName, maxCount, opts) {
686
733
  ),
687
734
  opts
688
735
  );
689
- recordDuration("getPendingHeldActions", performance.now() - start, "success");
736
+ recordDuration(
737
+ "getPendingHeldActions",
738
+ performance.now() - start,
739
+ "success"
740
+ );
690
741
  return raw.map((h) => ({ ...h, verdict: normalizeVerdict(h.verdict) }));
691
742
  } catch (err) {
692
743
  recordDuration("getPendingHeldActions", performance.now() - start, "error");
@@ -705,22 +756,36 @@ async function getHeldActionReviews(orgName, maxCount, opts) {
705
756
  ),
706
757
  opts
707
758
  );
708
- recordDuration("getHeldActionReviews", performance.now() - start, "success");
759
+ recordDuration(
760
+ "getHeldActionReviews",
761
+ performance.now() - start,
762
+ "success"
763
+ );
709
764
  return result;
710
765
  } catch (err) {
711
766
  recordDuration("getHeldActionReviews", performance.now() - start, "error");
712
767
  throw err;
713
768
  }
714
769
  }
715
- function riskEnginePostUrl(opts) {
716
- return `${baseUrl(opts)}/api/risk-engine`;
770
+ function riskEnginePostUrl(opts, network) {
771
+ let url = `${baseUrl(opts)}/api/risk-engine`;
772
+ if (network) {
773
+ url += `?network=${encodeURIComponent(network)}`;
774
+ }
775
+ return url;
717
776
  }
718
- async function getAgentDetail(agentPubkey, opts) {
777
+ async function getAgentDetail(agentPubkey, opts, chainOpts) {
719
778
  const start = performance.now();
720
779
  recordCall("getAgentDetail", void 0, agentPubkey);
721
780
  try {
781
+ let resolvedChainOpts = chainOpts;
782
+ if (opts?.orgName && !resolvedChainOpts?.blockchainRid) {
783
+ const resolved = await resolveChainForOrg(opts.orgName, opts);
784
+ resolvedChainOpts = { ...resolvedChainOpts, network: resolved.network };
785
+ }
786
+ const network = resolvedChainOpts?.network;
722
787
  const result = await postJson(
723
- riskEnginePostUrl(opts),
788
+ riskEnginePostUrl(opts, network),
724
789
  { action: "agent-detail-batch", agent: agentPubkey },
725
790
  opts
726
791
  );
@@ -731,12 +796,18 @@ async function getAgentDetail(agentPubkey, opts) {
731
796
  throw err;
732
797
  }
733
798
  }
734
- async function getAgentPolicy(agentPubkey, opts) {
799
+ async function getAgentPolicy(agentPubkey, opts, chainOpts) {
735
800
  const start = performance.now();
736
801
  recordCall("getAgentPolicy", void 0, agentPubkey);
737
802
  try {
803
+ let resolvedChainOpts = chainOpts;
804
+ if (opts?.orgName && !resolvedChainOpts?.blockchainRid) {
805
+ const resolved = await resolveChainForOrg(opts.orgName, opts);
806
+ resolvedChainOpts = { ...resolvedChainOpts, network: resolved.network };
807
+ }
808
+ const network = resolvedChainOpts?.network;
738
809
  const result = await postJson(
739
- riskEnginePostUrl(opts),
810
+ riskEnginePostUrl(opts, network),
740
811
  { action: "agent-policy-batch", agent: agentPubkey },
741
812
  opts
742
813
  );
@@ -1604,6 +1675,7 @@ function deduplicateAnomalies(anomalies) {
1604
1675
  loadUserConfig,
1605
1676
  normalizeForMatching,
1606
1677
  resolve,
1678
+ resolveChainForOrg,
1607
1679
  resolveKeyPath,
1608
1680
  saveUserConfig,
1609
1681
  scanMemory,
package/dist/index.d.cts CHANGED
@@ -5,6 +5,12 @@ type PubkeyValue = string | Buffer | {
5
5
  data: number[];
6
6
  };
7
7
  type JudgmentStatusState = "pending" | "answered" | "error";
8
+ type Network = "public" | "private";
9
+ interface ChainConfig {
10
+ network: Network;
11
+ blockchainRid: string;
12
+ nodeUrls: string[];
13
+ }
8
14
  interface Subscription {
9
15
  subscription_name: string;
10
16
  agent_number: number;
@@ -31,6 +37,7 @@ interface ClientOpts {
31
37
  interface ChainOpts {
32
38
  nodeUrls?: string[];
33
39
  blockchainRid?: string;
40
+ network?: Network;
34
41
  }
35
42
  interface JudgeResult {
36
43
  verdict: Verdict;
@@ -206,7 +213,7 @@ declare function generateKeyPair(): {
206
213
  };
207
214
  declare function loadAgent(privkey: string): AgentAuth;
208
215
  declare function toPubkeyHex(val: unknown): string;
209
- declare function checkAgentExists(pubkey: string, opts?: ClientOpts): Promise<boolean>;
216
+ declare function checkAgentExists(pubkey: string, opts?: ClientOpts, chainOpts?: ChainOpts): Promise<boolean>;
210
217
  declare function judgeAction(action: string, context: string | undefined, auth: AgentAuth, opts?: JudgeOptions): Promise<JudgeResult>;
211
218
  declare function getJudgmentStatus(judgmentId: string, agentPubkey: string, opts?: ClientOpts): Promise<JudgmentStatus>;
212
219
  declare function getToolCalls(maxCount: number, opts?: ClientOpts): Promise<ToolCallRecord[]>;
@@ -214,11 +221,16 @@ declare function getOrgToolCalls(orgName: string, maxCount: number, opts?: Clien
214
221
  declare function getAgentToolCalls(agentPubkey: string, maxCount: number, opts?: ClientOpts): Promise<ToolCallRecord[]>;
215
222
  declare function getToolCallCount(opts?: ClientOpts): Promise<number>;
216
223
  declare function getToolCallFull(toolCallId: string, opts?: ClientOpts): Promise<ToolCallFull | null>;
217
- declare function getOrgSubscription(orgName: string, opts?: ClientOpts): Promise<OrgSubscription | null>;
224
+ declare function getOrgSubscription(orgName: string, opts?: ClientOpts, network?: Network): Promise<OrgSubscription | null>;
225
+ declare function resolveChainForOrg(orgName: string, opts?: ClientOpts): Promise<ChainConfig>;
218
226
  declare function getPendingHeldActions(orgName: string, maxCount: number, opts?: ClientOpts): Promise<HeldAction[]>;
219
227
  declare function getHeldActionReviews(orgName: string, maxCount: number, opts?: ClientOpts): Promise<HeldActionReview[]>;
220
- declare function getAgentDetail(agentPubkey: string, opts?: ClientOpts): Promise<Record<string, unknown>>;
221
- declare function getAgentPolicy(agentPubkey: string, opts?: ClientOpts): Promise<AgentPolicy>;
228
+ declare function getAgentDetail(agentPubkey: string, opts?: ClientOpts & {
229
+ orgName?: string;
230
+ }, chainOpts?: ChainOpts): Promise<Record<string, unknown>>;
231
+ declare function getAgentPolicy(agentPubkey: string, opts?: ClientOpts & {
232
+ orgName?: string;
233
+ }, chainOpts?: ChainOpts): Promise<AgentPolicy>;
222
234
  declare function getSafetyStats(opts?: ClientOpts): Promise<Record<string, unknown>>;
223
235
 
224
236
  interface AtbashClient {
@@ -336,4 +348,4 @@ declare function normalizeForMatching(input: string): string;
336
348
  */
337
349
  declare function containsEvasionCharacters(input: string): boolean;
338
350
 
339
- export { type ActionType, type AgentAuth, type AgentPolicy, type AnomalySeverity, type AnomalyType, type AtbashClient, type AtbashClientConfig, type AtbashUserConfig, type ClientOpts, type ClientSource, DEFAULT_BLOCKCHAIN_RID, DEFAULT_CHROMIA_NODE_URLS, DEFAULT_ENDPOINT, type Decision, type DecisionVerdict, type HeldAction, type HeldActionReview, type JudgeEndpointConfig, type JudgeOptions, type JudgeResult, type JudgmentStatus, type JudgmentStatusState, type MemoryAnomaly, type MemoryDiffResult, type MemoryEntry, type MemoryScanOptions, type MemoryScanResult, type MemoryScanVerdict, type MemorySnapshot, type OrgSubscription, type Provider, type PubkeyValue, type TelemetryConfig, type ToolCallFull, type ToolCallInput, type ToolCallRecord, type ValidatedEndpoint, type Verdict, checkAgentExists, containsEvasionCharacters, createAtbashClient, createMemorySnapshot, derivePublicKey, diffMemorySnapshots, generateKeyPair, getAgentDetail, getAgentPolicy, getAgentToolCalls, getConfigDir, getConfigPath, getHeldActionReviews, getJudgmentStatus, getOrgSubscription, getOrgToolCalls, getPendingHeldActions, getSafetyStats, getToolCallCount, getToolCallFull, getToolCalls, isValidPrivateKey, judgeAction, loadAgent, loadAgentFromFile, loadUserConfig, normalizeForMatching, resolve, resolveKeyPath, saveUserConfig, scanMemory, scanMemoryBatch, setupTelemetry, shutdownTelemetry, toPubkeyHex, validateJudgeEndpoint, verifyJudgeResponseSignature };
351
+ export { type ActionType, type AgentAuth, type AgentPolicy, type AnomalySeverity, type AnomalyType, type AtbashClient, type AtbashClientConfig, type AtbashUserConfig, type ClientOpts, type ClientSource, DEFAULT_BLOCKCHAIN_RID, DEFAULT_CHROMIA_NODE_URLS, DEFAULT_ENDPOINT, type Decision, type DecisionVerdict, type HeldAction, type HeldActionReview, type JudgeEndpointConfig, type JudgeOptions, type JudgeResult, type JudgmentStatus, type JudgmentStatusState, type MemoryAnomaly, type MemoryDiffResult, type MemoryEntry, type MemoryScanOptions, type MemoryScanResult, type MemoryScanVerdict, type MemorySnapshot, type OrgSubscription, type Provider, type PubkeyValue, type TelemetryConfig, type ToolCallFull, type ToolCallInput, type ToolCallRecord, type ValidatedEndpoint, type Verdict, checkAgentExists, containsEvasionCharacters, createAtbashClient, createMemorySnapshot, derivePublicKey, diffMemorySnapshots, generateKeyPair, getAgentDetail, getAgentPolicy, getAgentToolCalls, getConfigDir, getConfigPath, getHeldActionReviews, getJudgmentStatus, getOrgSubscription, getOrgToolCalls, getPendingHeldActions, getSafetyStats, getToolCallCount, getToolCallFull, getToolCalls, isValidPrivateKey, judgeAction, loadAgent, loadAgentFromFile, loadUserConfig, normalizeForMatching, resolve, resolveChainForOrg, resolveKeyPath, saveUserConfig, scanMemory, scanMemoryBatch, setupTelemetry, shutdownTelemetry, toPubkeyHex, validateJudgeEndpoint, verifyJudgeResponseSignature };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,12 @@ type PubkeyValue = string | Buffer | {
5
5
  data: number[];
6
6
  };
7
7
  type JudgmentStatusState = "pending" | "answered" | "error";
8
+ type Network = "public" | "private";
9
+ interface ChainConfig {
10
+ network: Network;
11
+ blockchainRid: string;
12
+ nodeUrls: string[];
13
+ }
8
14
  interface Subscription {
9
15
  subscription_name: string;
10
16
  agent_number: number;
@@ -31,6 +37,7 @@ interface ClientOpts {
31
37
  interface ChainOpts {
32
38
  nodeUrls?: string[];
33
39
  blockchainRid?: string;
40
+ network?: Network;
34
41
  }
35
42
  interface JudgeResult {
36
43
  verdict: Verdict;
@@ -206,7 +213,7 @@ declare function generateKeyPair(): {
206
213
  };
207
214
  declare function loadAgent(privkey: string): AgentAuth;
208
215
  declare function toPubkeyHex(val: unknown): string;
209
- declare function checkAgentExists(pubkey: string, opts?: ClientOpts): Promise<boolean>;
216
+ declare function checkAgentExists(pubkey: string, opts?: ClientOpts, chainOpts?: ChainOpts): Promise<boolean>;
210
217
  declare function judgeAction(action: string, context: string | undefined, auth: AgentAuth, opts?: JudgeOptions): Promise<JudgeResult>;
211
218
  declare function getJudgmentStatus(judgmentId: string, agentPubkey: string, opts?: ClientOpts): Promise<JudgmentStatus>;
212
219
  declare function getToolCalls(maxCount: number, opts?: ClientOpts): Promise<ToolCallRecord[]>;
@@ -214,11 +221,16 @@ declare function getOrgToolCalls(orgName: string, maxCount: number, opts?: Clien
214
221
  declare function getAgentToolCalls(agentPubkey: string, maxCount: number, opts?: ClientOpts): Promise<ToolCallRecord[]>;
215
222
  declare function getToolCallCount(opts?: ClientOpts): Promise<number>;
216
223
  declare function getToolCallFull(toolCallId: string, opts?: ClientOpts): Promise<ToolCallFull | null>;
217
- declare function getOrgSubscription(orgName: string, opts?: ClientOpts): Promise<OrgSubscription | null>;
224
+ declare function getOrgSubscription(orgName: string, opts?: ClientOpts, network?: Network): Promise<OrgSubscription | null>;
225
+ declare function resolveChainForOrg(orgName: string, opts?: ClientOpts): Promise<ChainConfig>;
218
226
  declare function getPendingHeldActions(orgName: string, maxCount: number, opts?: ClientOpts): Promise<HeldAction[]>;
219
227
  declare function getHeldActionReviews(orgName: string, maxCount: number, opts?: ClientOpts): Promise<HeldActionReview[]>;
220
- declare function getAgentDetail(agentPubkey: string, opts?: ClientOpts): Promise<Record<string, unknown>>;
221
- declare function getAgentPolicy(agentPubkey: string, opts?: ClientOpts): Promise<AgentPolicy>;
228
+ declare function getAgentDetail(agentPubkey: string, opts?: ClientOpts & {
229
+ orgName?: string;
230
+ }, chainOpts?: ChainOpts): Promise<Record<string, unknown>>;
231
+ declare function getAgentPolicy(agentPubkey: string, opts?: ClientOpts & {
232
+ orgName?: string;
233
+ }, chainOpts?: ChainOpts): Promise<AgentPolicy>;
222
234
  declare function getSafetyStats(opts?: ClientOpts): Promise<Record<string, unknown>>;
223
235
 
224
236
  interface AtbashClient {
@@ -336,4 +348,4 @@ declare function normalizeForMatching(input: string): string;
336
348
  */
337
349
  declare function containsEvasionCharacters(input: string): boolean;
338
350
 
339
- export { type ActionType, type AgentAuth, type AgentPolicy, type AnomalySeverity, type AnomalyType, type AtbashClient, type AtbashClientConfig, type AtbashUserConfig, type ClientOpts, type ClientSource, DEFAULT_BLOCKCHAIN_RID, DEFAULT_CHROMIA_NODE_URLS, DEFAULT_ENDPOINT, type Decision, type DecisionVerdict, type HeldAction, type HeldActionReview, type JudgeEndpointConfig, type JudgeOptions, type JudgeResult, type JudgmentStatus, type JudgmentStatusState, type MemoryAnomaly, type MemoryDiffResult, type MemoryEntry, type MemoryScanOptions, type MemoryScanResult, type MemoryScanVerdict, type MemorySnapshot, type OrgSubscription, type Provider, type PubkeyValue, type TelemetryConfig, type ToolCallFull, type ToolCallInput, type ToolCallRecord, type ValidatedEndpoint, type Verdict, checkAgentExists, containsEvasionCharacters, createAtbashClient, createMemorySnapshot, derivePublicKey, diffMemorySnapshots, generateKeyPair, getAgentDetail, getAgentPolicy, getAgentToolCalls, getConfigDir, getConfigPath, getHeldActionReviews, getJudgmentStatus, getOrgSubscription, getOrgToolCalls, getPendingHeldActions, getSafetyStats, getToolCallCount, getToolCallFull, getToolCalls, isValidPrivateKey, judgeAction, loadAgent, loadAgentFromFile, loadUserConfig, normalizeForMatching, resolve, resolveKeyPath, saveUserConfig, scanMemory, scanMemoryBatch, setupTelemetry, shutdownTelemetry, toPubkeyHex, validateJudgeEndpoint, verifyJudgeResponseSignature };
351
+ export { type ActionType, type AgentAuth, type AgentPolicy, type AnomalySeverity, type AnomalyType, type AtbashClient, type AtbashClientConfig, type AtbashUserConfig, type ClientOpts, type ClientSource, DEFAULT_BLOCKCHAIN_RID, DEFAULT_CHROMIA_NODE_URLS, DEFAULT_ENDPOINT, type Decision, type DecisionVerdict, type HeldAction, type HeldActionReview, type JudgeEndpointConfig, type JudgeOptions, type JudgeResult, type JudgmentStatus, type JudgmentStatusState, type MemoryAnomaly, type MemoryDiffResult, type MemoryEntry, type MemoryScanOptions, type MemoryScanResult, type MemoryScanVerdict, type MemorySnapshot, type OrgSubscription, type Provider, type PubkeyValue, type TelemetryConfig, type ToolCallFull, type ToolCallInput, type ToolCallRecord, type ValidatedEndpoint, type Verdict, checkAgentExists, containsEvasionCharacters, createAtbashClient, createMemorySnapshot, derivePublicKey, diffMemorySnapshots, generateKeyPair, getAgentDetail, getAgentPolicy, getAgentToolCalls, getConfigDir, getConfigPath, getHeldActionReviews, getJudgmentStatus, getOrgSubscription, getOrgToolCalls, getPendingHeldActions, getSafetyStats, getToolCallCount, getToolCallFull, getToolCalls, isValidPrivateKey, judgeAction, loadAgent, loadAgentFromFile, loadUserConfig, normalizeForMatching, resolve, resolveChainForOrg, resolveKeyPath, saveUserConfig, scanMemory, scanMemoryBatch, setupTelemetry, shutdownTelemetry, toPubkeyHex, validateJudgeEndpoint, verifyJudgeResponseSignature };
package/dist/index.js CHANGED
@@ -245,8 +245,8 @@ async function _checkAgentExists(pubkey, opts, chainOpts) {
245
245
  throw err;
246
246
  }
247
247
  }
248
- async function checkAgentExists(pubkey, opts) {
249
- return _checkAgentExists(pubkey, opts);
248
+ async function checkAgentExists(pubkey, opts, chainOpts) {
249
+ return _checkAgentExists(pubkey, opts, chainOpts);
250
250
  }
251
251
  async function logToolCall(action, context, auth, chainOpts, extra, clientOpts) {
252
252
  const start = performance.now();
@@ -320,7 +320,9 @@ function enrichError(status, body, statusText, opts) {
320
320
  return new Error(message);
321
321
  }
322
322
  async function postJson(url, body, opts) {
323
- const headers = { "Content-Type": "application/json" };
323
+ const headers = {
324
+ "Content-Type": "application/json"
325
+ };
324
326
  if (opts?.auth) {
325
327
  headers["Authorization"] = `Bearer ${await getOrCreateAuthBearer(opts.auth)}`;
326
328
  }
@@ -386,9 +388,14 @@ async function judgeAction(action, context = "", auth, opts) {
386
388
  }
387
389
  try {
388
390
  let chainOpts = opts?.chainOpts;
389
- if (opts?.orgName && !chainOpts?.blockchainRid) {
390
- const resolved = await resolveChainForOrg(opts.orgName, opts);
391
- chainOpts = { ...chainOpts, network: resolved.network };
391
+ if (opts?.orgName) {
392
+ const mapNetwork = await getActiveNetworkForOrg(opts.orgName, opts);
393
+ if (mapNetwork) {
394
+ chainOpts = { network: mapNetwork };
395
+ } else if (!chainOpts?.blockchainRid) {
396
+ const resolved = await resolveChainForOrg(opts.orgName, opts);
397
+ chainOpts = { ...chainOpts, network: resolved.network };
398
+ }
392
399
  }
393
400
  const logResult = await logToolCall(
394
401
  action,
@@ -417,7 +424,9 @@ async function judgeAction(action, context = "", auth, opts) {
417
424
  agent_pubkey: auth.pubkey,
418
425
  action,
419
426
  signed_log_tool_call: logResult.signedHex,
420
- ...signedJudgeActionHex && { signed_judge_action: signedJudgeActionHex },
427
+ ...signedJudgeActionHex && {
428
+ signed_judge_action: signedJudgeActionHex
429
+ },
421
430
  ...context && { context },
422
431
  ...opts?.provider && { provider: opts.provider },
423
432
  ...opts?.toolName && { tool_name: opts.toolName },
@@ -462,9 +471,10 @@ async function getJudgmentStatus(judgmentId, agentPubkey, opts) {
462
471
  throw err;
463
472
  }
464
473
  }
465
- function riskEngineUrl(action, params, opts) {
474
+ function riskEngineUrl(action, params, opts, network) {
466
475
  const url = new URL(`${baseUrl(opts)}/api/risk-engine`);
467
476
  url.searchParams.set("action", action);
477
+ if (network) url.searchParams.set("network", network);
468
478
  for (const [k, v] of Object.entries(params)) {
469
479
  if (v) url.searchParams.set(k, v);
470
480
  }
@@ -569,12 +579,14 @@ function coerceOrgSubscription(row, orgName) {
569
579
  is_active: Boolean(r.is_active)
570
580
  };
571
581
  }
572
- async function getOrgSubscription(orgName, opts) {
582
+ async function getOrgSubscription(orgName, opts, network) {
573
583
  const start = performance.now();
574
584
  recordCall("getOrgSubscription");
575
585
  try {
586
+ const params = { org: orgName };
587
+ if (network) params.network = network;
576
588
  const result = await getJson(
577
- riskEngineUrl("org-subscription", { org: orgName }, opts),
589
+ riskEngineUrl("org-subscription", params, opts),
578
590
  opts
579
591
  );
580
592
  recordDuration("getOrgSubscription", performance.now() - start, "success");
@@ -584,13 +596,47 @@ async function getOrgSubscription(orgName, opts) {
584
596
  throw err;
585
597
  }
586
598
  }
599
+ async function getActiveNetworkForOrg(orgName, opts) {
600
+ try {
601
+ const url = `${baseUrl(opts)}/api/org-network?org=${encodeURIComponent(orgName)}`;
602
+ const data = await getJson(url, opts);
603
+ if (data?.network === "public" || data?.network === "private") {
604
+ return data.network;
605
+ }
606
+ return null;
607
+ } catch {
608
+ return null;
609
+ }
610
+ }
587
611
  var _chainCache = /* @__PURE__ */ new Map();
588
612
  async function resolveChainForOrg(orgName, opts) {
589
613
  const cached = _chainCache.get(orgName);
590
614
  if (cached) return cached;
615
+ const mapNetwork = await getActiveNetworkForOrg(orgName, opts);
616
+ if (mapNetwork) {
617
+ const chain = mapNetwork === "private" ? PRIVATE_CHAIN : PUBLIC_CHAIN;
618
+ _chainCache.set(orgName, chain);
619
+ return chain;
620
+ }
591
621
  try {
592
- const sub = await getOrgSubscription(orgName, opts);
593
- if (sub?.is_private_blockchain) {
622
+ const [pubSub, privSub] = await Promise.all([
623
+ getOrgSubscription(orgName, opts, "public").catch(() => null),
624
+ getOrgSubscription(orgName, opts, "private").catch(() => null)
625
+ ]);
626
+ if (pubSub?.is_private_blockchain) {
627
+ _chainCache.set(orgName, PRIVATE_CHAIN);
628
+ return PRIVATE_CHAIN;
629
+ }
630
+ if (pubSub && privSub) {
631
+ const chain = privSub.assigned_at > pubSub.assigned_at ? PRIVATE_CHAIN : PUBLIC_CHAIN;
632
+ _chainCache.set(orgName, chain);
633
+ return chain;
634
+ }
635
+ if (pubSub) {
636
+ _chainCache.set(orgName, PUBLIC_CHAIN);
637
+ return PUBLIC_CHAIN;
638
+ }
639
+ if (privSub?.is_private_blockchain) {
594
640
  _chainCache.set(orgName, PRIVATE_CHAIN);
595
641
  return PRIVATE_CHAIN;
596
642
  }
@@ -611,7 +657,11 @@ async function getPendingHeldActions(orgName, maxCount, opts) {
611
657
  ),
612
658
  opts
613
659
  );
614
- recordDuration("getPendingHeldActions", performance.now() - start, "success");
660
+ recordDuration(
661
+ "getPendingHeldActions",
662
+ performance.now() - start,
663
+ "success"
664
+ );
615
665
  return raw.map((h) => ({ ...h, verdict: normalizeVerdict(h.verdict) }));
616
666
  } catch (err) {
617
667
  recordDuration("getPendingHeldActions", performance.now() - start, "error");
@@ -630,22 +680,36 @@ async function getHeldActionReviews(orgName, maxCount, opts) {
630
680
  ),
631
681
  opts
632
682
  );
633
- recordDuration("getHeldActionReviews", performance.now() - start, "success");
683
+ recordDuration(
684
+ "getHeldActionReviews",
685
+ performance.now() - start,
686
+ "success"
687
+ );
634
688
  return result;
635
689
  } catch (err) {
636
690
  recordDuration("getHeldActionReviews", performance.now() - start, "error");
637
691
  throw err;
638
692
  }
639
693
  }
640
- function riskEnginePostUrl(opts) {
641
- return `${baseUrl(opts)}/api/risk-engine`;
694
+ function riskEnginePostUrl(opts, network) {
695
+ let url = `${baseUrl(opts)}/api/risk-engine`;
696
+ if (network) {
697
+ url += `?network=${encodeURIComponent(network)}`;
698
+ }
699
+ return url;
642
700
  }
643
- async function getAgentDetail(agentPubkey, opts) {
701
+ async function getAgentDetail(agentPubkey, opts, chainOpts) {
644
702
  const start = performance.now();
645
703
  recordCall("getAgentDetail", void 0, agentPubkey);
646
704
  try {
705
+ let resolvedChainOpts = chainOpts;
706
+ if (opts?.orgName && !resolvedChainOpts?.blockchainRid) {
707
+ const resolved = await resolveChainForOrg(opts.orgName, opts);
708
+ resolvedChainOpts = { ...resolvedChainOpts, network: resolved.network };
709
+ }
710
+ const network = resolvedChainOpts?.network;
647
711
  const result = await postJson(
648
- riskEnginePostUrl(opts),
712
+ riskEnginePostUrl(opts, network),
649
713
  { action: "agent-detail-batch", agent: agentPubkey },
650
714
  opts
651
715
  );
@@ -656,12 +720,18 @@ async function getAgentDetail(agentPubkey, opts) {
656
720
  throw err;
657
721
  }
658
722
  }
659
- async function getAgentPolicy(agentPubkey, opts) {
723
+ async function getAgentPolicy(agentPubkey, opts, chainOpts) {
660
724
  const start = performance.now();
661
725
  recordCall("getAgentPolicy", void 0, agentPubkey);
662
726
  try {
727
+ let resolvedChainOpts = chainOpts;
728
+ if (opts?.orgName && !resolvedChainOpts?.blockchainRid) {
729
+ const resolved = await resolveChainForOrg(opts.orgName, opts);
730
+ resolvedChainOpts = { ...resolvedChainOpts, network: resolved.network };
731
+ }
732
+ const network = resolvedChainOpts?.network;
663
733
  const result = await postJson(
664
- riskEnginePostUrl(opts),
734
+ riskEnginePostUrl(opts, network),
665
735
  { action: "agent-policy-batch", agent: agentPubkey },
666
736
  opts
667
737
  );
@@ -1528,6 +1598,7 @@ export {
1528
1598
  loadUserConfig,
1529
1599
  normalizeForMatching,
1530
1600
  resolve,
1601
+ resolveChainForOrg,
1531
1602
  resolveKeyPath,
1532
1603
  saveUserConfig,
1533
1604
  scanMemory,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atbash/sdk",
3
- "version": "0.3.11-dev.12",
3
+ "version": "0.3.11-dev.13",
4
4
  "description": "Atbash SDK — control boundary before the last irreversible step in an agent workflow",
5
5
  "homepage": "https://atbash.ai",
6
6
  "author": "Atbash",