@synapsor/client 0.1.2 → 0.1.4

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/synapsor.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { spawn } from "node:child_process";
2
+ import { randomUUID } from "node:crypto";
2
3
  import { existsSync } from "node:fs";
3
4
  import net from "node:net";
4
5
  import { dirname, join, resolve } from "node:path";
@@ -19,11 +20,26 @@ export {
19
20
  };
20
21
 
21
22
  export class SynapsorError extends Error {
22
- constructor(message, { status = undefined, payload = undefined } = {}) {
23
+ constructor(
24
+ message,
25
+ {
26
+ status = undefined,
27
+ payload = undefined,
28
+ code = undefined,
29
+ requestId = undefined,
30
+ retryable = undefined,
31
+ } = {},
32
+ ) {
23
33
  super(message);
24
34
  this.name = "SynapsorError";
25
35
  this.status = status;
26
36
  this.payload = payload;
37
+ const payloadObject = payload && typeof payload === "object" ? payload : {};
38
+ this.code = code ?? payloadObject.code ?? payloadObject.error;
39
+ this.errorCode = this.code;
40
+ this.requestId = requestId ?? payloadObject.request_id ?? payloadObject.response_request_id;
41
+ this.request_id = this.requestId;
42
+ this.retryable = retryable;
27
43
  }
28
44
  }
29
45
 
@@ -64,6 +80,9 @@ export class Synapsor {
64
80
  this.apiKey = apiKey;
65
81
  this.process = process;
66
82
  this.timeoutMs = timeoutMs;
83
+ this.agentRuns = new AgentRunsNamespace(this);
84
+ this.externalActions = new ExternalActionsNamespace(this);
85
+ this.evals = new AgentEvalsNamespace(this);
67
86
  }
68
87
 
69
88
  static async connect(target, options = {}) {
@@ -176,6 +195,19 @@ export class Synapsor {
176
195
  });
177
196
  }
178
197
 
198
+ async setAdminTenantBudget(tenantId, limits) {
199
+ return this.request("POST", "/v1/admin/tenant-budgets", {
200
+ tenant_id: String(tenantId),
201
+ limits: { ...limits },
202
+ });
203
+ }
204
+
205
+ async clearAdminTenantBudget(tenantId) {
206
+ return this.request("POST", "/v1/admin/tenant-budgets/clear", {
207
+ tenant_id: String(tenantId),
208
+ });
209
+ }
210
+
179
211
  async shutdown() {
180
212
  return this.request("POST", "/v1/admin/shutdown", {});
181
213
  }
@@ -249,6 +281,10 @@ export class Synapsor {
249
281
  include_audit_trail = undefined,
250
282
  settlementPolicy = undefined,
251
283
  settlement_policy = undefined,
284
+ runId = undefined,
285
+ run_id = undefined,
286
+ stepKey = undefined,
287
+ step_key = undefined,
252
288
  } = {}) {
253
289
  const payload = {
254
290
  capability,
@@ -260,6 +296,8 @@ export class Synapsor {
260
296
  const envelope = responseEnvelope ?? response_envelope;
261
297
  const audit = includeAuditTrail ?? include_audit_trail;
262
298
  const policy = settlementPolicy ?? settlement_policy;
299
+ const workflowRun = runId ?? run_id;
300
+ const workflowStep = stepKey ?? step_key;
263
301
  if (trace !== undefined) {
264
302
  payload.trace_id = trace;
265
303
  }
@@ -278,9 +316,467 @@ export class Synapsor {
278
316
  if (policy !== undefined) {
279
317
  payload.settlement_policy = policy;
280
318
  }
319
+ if (workflowRun !== undefined) {
320
+ payload.run_id = workflowRun;
321
+ }
322
+ if (workflowStep !== undefined) {
323
+ payload.step_key = workflowStep;
324
+ }
281
325
  return this.request("POST", "/v1/agent/invoke", payload);
282
326
  }
283
327
 
328
+ async createAgentWorkflow(workflow, {
329
+ version = "v1",
330
+ description = undefined,
331
+ entryCapability = undefined,
332
+ entry_capability = undefined,
333
+ maxSteps = undefined,
334
+ max_steps = undefined,
335
+ maxToolCalls = undefined,
336
+ max_tool_calls = undefined,
337
+ maxInputTokens = undefined,
338
+ max_input_tokens = undefined,
339
+ maxOutputTokens = undefined,
340
+ max_output_tokens = undefined,
341
+ evidenceRequired = undefined,
342
+ evidence_required = undefined,
343
+ checkpointEveryStep = undefined,
344
+ checkpoint_every_step = undefined,
345
+ autoBranchOnProposal = undefined,
346
+ auto_branch_on_proposal = undefined,
347
+ branchPolicy = undefined,
348
+ branch_policy = undefined,
349
+ riskPolicy = undefined,
350
+ risk_policy = undefined,
351
+ errorPolicy = undefined,
352
+ error_policy = undefined,
353
+ retentionPolicy = undefined,
354
+ retention_policy = undefined,
355
+ sessionRequirements = undefined,
356
+ session_requirements = undefined,
357
+ allowedCapabilities = undefined,
358
+ allowed_capabilities = undefined,
359
+ allowedActions = undefined,
360
+ allowed_actions = undefined,
361
+ session = undefined,
362
+ } = {}) {
363
+ const payload = {
364
+ workflow,
365
+ version,
366
+ session: this.requireSession(session),
367
+ };
368
+ const entry = entryCapability ?? entry_capability;
369
+ const steps = maxSteps ?? max_steps;
370
+ const toolCalls = maxToolCalls ?? max_tool_calls;
371
+ const inputTokens = maxInputTokens ?? max_input_tokens;
372
+ const outputTokens = maxOutputTokens ?? max_output_tokens;
373
+ const evidence = evidenceRequired ?? evidence_required;
374
+ const checkpoint = checkpointEveryStep ?? checkpoint_every_step;
375
+ const autoBranch = autoBranchOnProposal ?? auto_branch_on_proposal;
376
+ const branch = branchPolicy ?? branch_policy;
377
+ const risk = riskPolicy ?? risk_policy;
378
+ const error = errorPolicy ?? error_policy;
379
+ const retention = retentionPolicy ?? retention_policy;
380
+ const requiredSession = sessionRequirements ?? session_requirements;
381
+ const capabilities = allowedCapabilities ?? allowed_capabilities;
382
+ const actions = allowedActions ?? allowed_actions;
383
+ for (const [key, value] of [
384
+ ["description", description],
385
+ ["entry_capability", entry],
386
+ ["max_steps", steps],
387
+ ["max_tool_calls", toolCalls],
388
+ ["max_input_tokens", inputTokens],
389
+ ["max_output_tokens", outputTokens],
390
+ ["evidence_required", evidence],
391
+ ["checkpoint_every_step", checkpoint],
392
+ ["auto_branch_on_proposal", autoBranch],
393
+ ["branch_policy", branch],
394
+ ["risk_policy", risk],
395
+ ["error_policy", error],
396
+ ["retention_policy", retention],
397
+ ["session_requirements", requiredSession],
398
+ ["allowed_capabilities", capabilities],
399
+ ["allowed_actions", actions],
400
+ ]) {
401
+ if (value !== undefined) {
402
+ payload[key] = value;
403
+ }
404
+ }
405
+ return this.request("POST", "/v1/agent/workflows", payload);
406
+ }
407
+
408
+ async beginAgentRun(workflow, { version = "v1", input = {}, session = undefined } = {}) {
409
+ return this.request("POST", "/v1/agent/runs", {
410
+ workflow,
411
+ version,
412
+ input,
413
+ session: this.requireSession(session),
414
+ });
415
+ }
416
+
417
+ async recordAgentStep(runId, stepKey, {
418
+ kind = "external_action",
419
+ status = "completed",
420
+ input = {},
421
+ output = {},
422
+ session = undefined,
423
+ } = {}) {
424
+ return this.request("POST", "/v1/agent/runs/steps", {
425
+ run_id: runId,
426
+ step_key: stepKey,
427
+ kind,
428
+ status,
429
+ input,
430
+ output,
431
+ session: this.requireSession(session),
432
+ });
433
+ }
434
+
435
+ async checkpointAgentRun(runId, stepKey, {
436
+ payload = {},
437
+ session = undefined,
438
+ } = {}) {
439
+ return this.request("POST", "/v1/agent/runs/checkpoints", {
440
+ run_id: runId,
441
+ step_key: stepKey,
442
+ payload,
443
+ session: this.requireSession(session),
444
+ });
445
+ }
446
+
447
+ async interruptAgentRun(runId, stepKey, {
448
+ reason,
449
+ payload = {},
450
+ session = undefined,
451
+ } = {}) {
452
+ return this.request("POST", "/v1/agent/runs/interrupts", {
453
+ run_id: runId,
454
+ step_key: stepKey,
455
+ reason,
456
+ payload,
457
+ session: this.requireSession(session),
458
+ });
459
+ }
460
+
461
+ async handoffAgentRun(runId, stepKey, {
462
+ target,
463
+ reason = undefined,
464
+ payload = {},
465
+ session = undefined,
466
+ } = {}) {
467
+ const body = {
468
+ run_id: runId,
469
+ step_key: stepKey,
470
+ target,
471
+ payload,
472
+ session: this.requireSession(session),
473
+ };
474
+ if (reason !== undefined) {
475
+ body.reason = reason;
476
+ }
477
+ return this.request("POST", "/v1/agent/runs/handoffs", body);
478
+ }
479
+
480
+ async recordGuardrailEvent(runId, stepKey, {
481
+ status = "completed",
482
+ payload = {},
483
+ session = undefined,
484
+ } = {}) {
485
+ return this.request("POST", "/v1/agent/runs/guardrail-events", {
486
+ run_id: runId,
487
+ step_key: stepKey,
488
+ status,
489
+ payload,
490
+ session: this.requireSession(session),
491
+ });
492
+ }
493
+
494
+ async endAgentRun(runId, { status = "completed", output = {}, session = undefined } = {}) {
495
+ return this.request("POST", "/v1/agent/runs/complete", {
496
+ run_id: runId,
497
+ status,
498
+ output,
499
+ session: this.requireSession(session),
500
+ });
501
+ }
502
+
503
+ async explainAgentRun(runId, { session = undefined } = {}) {
504
+ return this.request("POST", "/v1/agent/runs/explain", {
505
+ run_id: runId,
506
+ session: this.requireSession(session),
507
+ });
508
+ }
509
+
510
+ async settleAgentRun(runId, {
511
+ settlementPolicy = undefined,
512
+ settlement_policy = undefined,
513
+ policy = undefined,
514
+ idempotencyKey = undefined,
515
+ idempotency_key = undefined,
516
+ session = undefined,
517
+ } = {}) {
518
+ const selectedPolicy = settlementPolicy ?? settlement_policy ?? policy;
519
+ if (selectedPolicy === undefined || selectedPolicy === "") {
520
+ throw new Error("settleAgentRun requires settlementPolicy");
521
+ }
522
+ const payload = {
523
+ run_id: runId,
524
+ settlement_policy: selectedPolicy,
525
+ session: this.requireSession(session),
526
+ };
527
+ const idem = idempotencyKey ?? idempotency_key;
528
+ if (idem !== undefined) {
529
+ payload.idempotency_key = idem;
530
+ }
531
+ return this.request("POST", "/v1/agent/runs/settle", payload);
532
+ }
533
+
534
+ async createAgentEval(evalName, {
535
+ assertions = [],
536
+ description = undefined,
537
+ sourceType = undefined,
538
+ source_type = undefined,
539
+ sourceWorkflow = undefined,
540
+ source_workflow = undefined,
541
+ sourceTable = undefined,
542
+ source_table = undefined,
543
+ sourceStatus = undefined,
544
+ source_status = undefined,
545
+ replayMode = "frozen",
546
+ replay_mode = undefined,
547
+ session = undefined,
548
+ } = {}) {
549
+ const table = sourceTable ?? source_table;
550
+ const resolvedSourceType = source_type ?? sourceType ?? (table !== undefined ? "table" : "runs");
551
+ const payload = {
552
+ eval: evalName,
553
+ source_type: resolvedSourceType,
554
+ replay_mode: replay_mode ?? replayMode,
555
+ assertions,
556
+ session: this.requireSession(session),
557
+ };
558
+ const workflow = sourceWorkflow ?? source_workflow;
559
+ const status = sourceStatus ?? source_status;
560
+ if (description !== undefined) {
561
+ payload.description = description;
562
+ }
563
+ if (workflow !== undefined) {
564
+ payload.source_workflow = workflow;
565
+ }
566
+ if (table !== undefined) {
567
+ payload.source_table = table;
568
+ }
569
+ if (status !== undefined) {
570
+ payload.source_status = status;
571
+ }
572
+ return this.request("POST", "/v1/agent/evals", payload);
573
+ }
574
+
575
+ async runAgentEval(evalName, {
576
+ targetBranch = undefined,
577
+ target_branch = undefined,
578
+ replayMode = undefined,
579
+ replay_mode = undefined,
580
+ session = undefined,
581
+ } = {}) {
582
+ const payload = {
583
+ eval: evalName,
584
+ session: this.requireSession(session),
585
+ };
586
+ const branch = targetBranch ?? target_branch;
587
+ const mode = replayMode ?? replay_mode;
588
+ if (branch !== undefined) {
589
+ payload.target_branch = branch;
590
+ }
591
+ if (mode !== undefined) {
592
+ payload.replay_mode = mode;
593
+ }
594
+ return this.request("POST", "/v1/agent/evals/run", payload);
595
+ }
596
+
597
+ async agentEvalFailures(evalRunId, { session = undefined } = {}) {
598
+ return this.request("POST", "/v1/agent/evals/failures", {
599
+ eval_run_id: evalRunId,
600
+ session: this.requireSession(session),
601
+ });
602
+ }
603
+
604
+ async diffAgentEval(baselineEvalRunId, candidateEvalRunId, { session = undefined } = {}) {
605
+ return this.request("POST", "/v1/agent/evals/diff", {
606
+ baseline_eval_run_id: baselineEvalRunId,
607
+ candidate_eval_run_id: candidateEvalRunId,
608
+ session: this.requireSession(session),
609
+ });
610
+ }
611
+
612
+ async createAgentAdapter(adapter, {
613
+ type,
614
+ tools,
615
+ resources = [],
616
+ description = undefined,
617
+ session = undefined,
618
+ } = {}) {
619
+ const payload = {
620
+ adapter,
621
+ type,
622
+ tools,
623
+ resources,
624
+ session: this.requireSession(session),
625
+ };
626
+ if (description !== undefined) {
627
+ payload.description = description;
628
+ }
629
+ return this.request("POST", "/v1/agent/adapters", payload);
630
+ }
631
+
632
+ async adapterTools(adapter, { session = undefined } = {}) {
633
+ return this.request("POST", "/v1/agent/adapters/tools", {
634
+ adapter,
635
+ session: this.requireSession(session),
636
+ });
637
+ }
638
+
639
+ async callAdapterTool(adapter, tool, {
640
+ arguments: args = {},
641
+ runId = undefined,
642
+ run_id = undefined,
643
+ stepKey = undefined,
644
+ step_key = undefined,
645
+ session = undefined,
646
+ } = {}) {
647
+ const payload = {
648
+ adapter,
649
+ tool,
650
+ arguments: args,
651
+ session: this.requireSession(session),
652
+ };
653
+ const run = runId ?? run_id;
654
+ const step = stepKey ?? step_key;
655
+ if (run !== undefined) {
656
+ payload.run_id = run;
657
+ }
658
+ if (step !== undefined) {
659
+ payload.step_key = step;
660
+ }
661
+ return this.request("POST", "/v1/agent/adapters/call-tool", payload);
662
+ }
663
+
664
+ async traceAgentRun(runId, { session = undefined } = {}) {
665
+ return this.request("POST", "/v1/agent/runs/trace", {
666
+ run_id: runId,
667
+ session: this.requireSession(session),
668
+ });
669
+ }
670
+
671
+ async createObservabilitySink({
672
+ name,
673
+ type,
674
+ endpoint = "",
675
+ export: exportList = [],
676
+ redact = {},
677
+ sampleRateMilli = undefined,
678
+ sample_rate_milli = undefined,
679
+ session = undefined,
680
+ } = {}) {
681
+ const payload = {
682
+ name,
683
+ type,
684
+ endpoint,
685
+ export: exportList,
686
+ redact,
687
+ session: this.requireSession(session),
688
+ };
689
+ const sampleRate = sampleRateMilli ?? sample_rate_milli;
690
+ if (sampleRate !== undefined) {
691
+ payload.sample_rate_milli = sampleRate;
692
+ }
693
+ return this.request("POST", "/v1/observability/sinks", payload);
694
+ }
695
+
696
+ async proposeExternalAction(action, {
697
+ runId = undefined,
698
+ run_id = undefined,
699
+ stepKey = undefined,
700
+ step_key = undefined,
701
+ arguments: args = {},
702
+ idempotencyKey = undefined,
703
+ idempotency_key = undefined,
704
+ session = undefined,
705
+ } = {}) {
706
+ const payload = {
707
+ action,
708
+ run_id: runId ?? run_id,
709
+ step_key: stepKey ?? step_key,
710
+ arguments: args,
711
+ session: this.requireSession(session),
712
+ };
713
+ const idem = idempotencyKey ?? idempotency_key;
714
+ if (idem !== undefined) {
715
+ payload.idempotency_key = idem;
716
+ }
717
+ return this.request("POST", "/v1/agent/external-actions/propose", payload);
718
+ }
719
+
720
+ async approveExternalAction(actionInstanceId, {
721
+ note = undefined,
722
+ approvalNote = undefined,
723
+ approval_note = undefined,
724
+ session = undefined,
725
+ } = {}) {
726
+ const payload = {};
727
+ const selectedNote = note ?? approvalNote ?? approval_note;
728
+ if (selectedNote !== undefined) {
729
+ payload.note = selectedNote;
730
+ }
731
+ if (session !== undefined || Object.keys(this.session).length > 0) {
732
+ payload.session = this.requireSession(session);
733
+ }
734
+ return this.request("POST", `/v1/agent/external-actions/${actionInstanceId}/approve`, payload);
735
+ }
736
+
737
+ async claimExternalAction({
738
+ queue,
739
+ workerId = undefined,
740
+ worker_id = undefined,
741
+ lockSeconds = 60,
742
+ lock_seconds = undefined,
743
+ session = undefined,
744
+ } = {}) {
745
+ const payload = {
746
+ queue,
747
+ worker_id: workerId ?? worker_id,
748
+ lock_seconds: lock_seconds ?? lockSeconds,
749
+ };
750
+ if (session !== undefined || Object.keys(this.session).length > 0) {
751
+ payload.session = this.requireSession(session);
752
+ }
753
+ return this.request("POST", "/v1/agent/external-actions/claim", payload);
754
+ }
755
+
756
+ async confirmExternalAction(actionInstanceId, {
757
+ status,
758
+ providerRequestId = undefined,
759
+ provider_request_id = undefined,
760
+ response = {},
761
+ errorCode = undefined,
762
+ error_code = undefined,
763
+ session = undefined,
764
+ } = {}) {
765
+ const payload = { status, response };
766
+ const provider = providerRequestId ?? provider_request_id;
767
+ const error = errorCode ?? error_code;
768
+ if (provider !== undefined) {
769
+ payload.provider_request_id = provider;
770
+ }
771
+ if (error !== undefined) {
772
+ payload.error_code = error;
773
+ }
774
+ if (session !== undefined || Object.keys(this.session).length > 0) {
775
+ payload.session = this.requireSession(session);
776
+ }
777
+ return this.request("POST", `/v1/agent/external-actions/${actionInstanceId}/confirm`, payload);
778
+ }
779
+
284
780
  async listCapabilities(query = "", { session = undefined } = {}) {
285
781
  return this.request("POST", "/v1/agent/capabilities", {
286
782
  query,
@@ -299,6 +795,40 @@ export class Synapsor {
299
795
  return this.memoryCommand(factCommand("agent_memory.remember_fact", options), options);
300
796
  }
301
797
 
798
+ async createMemoryPolicy(options) {
799
+ const command = {
800
+ kind: "agent_memory.create_policy",
801
+ name: options.name,
802
+ scope: options.scope,
803
+ subject: options.subject,
804
+ require_evidence: Boolean(options.requireEvidence ?? options.require_evidence ?? false),
805
+ };
806
+ for (const [from, to] of [
807
+ ["description", "description"],
808
+ ["approvalMinTrustMilli", "approval_min_trust_milli"],
809
+ ["approval_min_trust_milli", "approval_min_trust_milli"],
810
+ ["maxActiveFactsPerSubject", "max_active_facts_per_subject"],
811
+ ["max_active_facts_per_subject", "max_active_facts_per_subject"],
812
+ ["maxRecallFactsPerCapability", "max_recall_facts_per_capability"],
813
+ ["max_recall_facts_per_capability", "max_recall_facts_per_capability"],
814
+ ["forgetMode", "forget_mode"],
815
+ ["forget_mode", "forget_mode"],
816
+ ["conflictStrategy", "conflict_strategy"],
817
+ ["conflict_strategy", "conflict_strategy"],
818
+ ["supersessionStrategy", "supersession_strategy"],
819
+ ["supersession_strategy", "supersession_strategy"],
820
+ ["decayStrategy", "decay_strategy"],
821
+ ["decay_strategy", "decay_strategy"],
822
+ ["retentionStrategy", "retention_strategy"],
823
+ ["retention_strategy", "retention_strategy"],
824
+ ]) {
825
+ if (options[from] !== undefined) {
826
+ command[to] = options[from];
827
+ }
828
+ }
829
+ return this.memoryCommand(command, options);
830
+ }
831
+
302
832
  async proposeMemoryFact(options) {
303
833
  const command = factCommand("agent_memory.propose_fact", options);
304
834
  command.replace_subject = Boolean(options.replaceSubject ?? options.replace_subject ?? false);
@@ -348,6 +878,12 @@ export class Synapsor {
348
878
  }
349
879
  for (const [from, to] of [
350
880
  ["asOf", "as_of"],
881
+ ["as_of", "as_of"],
882
+ ["asOfAgentRunId", "as_of_agent_run_id"],
883
+ ["as_of_agent_run_id", "as_of_agent_run_id"],
884
+ ["policy", "policy"],
885
+ ["policyName", "policy_name"],
886
+ ["policy_name", "policy_name"],
351
887
  ["budgetTokens", "budget_tokens"],
352
888
  ["profile", "profile"],
353
889
  ["inlineEvidence", "inline_evidence"],
@@ -361,6 +897,36 @@ export class Synapsor {
361
897
  return this.memoryCommand(command, options);
362
898
  }
363
899
 
900
+ async recallMemoryAsOfAgentRun(runId, options = {}) {
901
+ return this.recallMemory({ ...options, asOfAgentRunId: runId });
902
+ }
903
+
904
+ async showMemoryConflicts(options = {}) {
905
+ const command = {
906
+ kind: "agent_memory.show_conflicts",
907
+ subject: typedRef(options.subject),
908
+ };
909
+ if (options.scope !== undefined) {
910
+ command.scope = typedRef(options.scope);
911
+ }
912
+ if (options.policy !== undefined) {
913
+ command.policy = options.policy;
914
+ }
915
+ if (options.policyName !== undefined) {
916
+ command.policy_name = options.policyName;
917
+ }
918
+ return this.memoryCommand(command, options);
919
+ }
920
+
921
+ async supersedeMemoryFact(oldMemoryId, newMemoryId, reason, options = {}) {
922
+ return this.memoryCommand({
923
+ kind: "agent_memory.supersede_fact",
924
+ old_memory_id: oldMemoryId,
925
+ new_memory_id: newMemoryId,
926
+ reason,
927
+ }, options);
928
+ }
929
+
364
930
  async retireFact(memoryId, { validTo, reason, session = undefined }) {
365
931
  return this.memoryCommand({
366
932
  kind: "agent_memory.retire_fact",
@@ -455,11 +1021,28 @@ export class Synapsor {
455
1021
  }
456
1022
 
457
1023
  async replayAgentRun(runId, options = {}) {
458
- return this.request("POST", "/v1/agent/runs/replay", {
1024
+ const payload = {
459
1025
  run_id: Number(runId),
460
1026
  session: this.requireSession(options.session),
461
1027
  include_sensitive_memory: Boolean(options.includeSensitiveMemory ?? options.include_sensitive_memory ?? false),
462
- });
1028
+ };
1029
+ const mode = options.mode ?? options.replayMode ?? options.replay_mode;
1030
+ const version = options.version ?? options.commitVersion ?? options.commit_version;
1031
+ const timestamp = options.timestamp;
1032
+ const branchName = options.branchName ?? options.branch_name;
1033
+ if (mode !== undefined) {
1034
+ payload.mode = mode;
1035
+ }
1036
+ if (version !== undefined) {
1037
+ payload.version = Number(version);
1038
+ }
1039
+ if (timestamp !== undefined) {
1040
+ payload.timestamp = timestamp;
1041
+ }
1042
+ if (branchName !== undefined) {
1043
+ payload.branch_name = branchName;
1044
+ }
1045
+ return this.request("POST", "/v1/agent/runs/replay", payload);
463
1046
  }
464
1047
 
465
1048
  async proposalLifecycle(action, proposal, { session = undefined, promoteBranch = undefined, promote_branch = undefined, targetBranch = undefined, target_branch = undefined, settlementPolicy = undefined, settlement_policy = undefined } = {}) {
@@ -494,7 +1077,8 @@ export class Synapsor {
494
1077
  }
495
1078
 
496
1079
  async request(method, path, payload = undefined) {
497
- const headers = { accept: "application/json" };
1080
+ const requestId = `sdk-${randomUUID()}`;
1081
+ const headers = { accept: "application/json", "x-request-id": requestId };
498
1082
  const init = { method, headers };
499
1083
  if (payload !== undefined) {
500
1084
  headers["content-type"] = "application/json";
@@ -515,9 +1099,14 @@ export class Synapsor {
515
1099
  }
516
1100
  if (!response.ok) {
517
1101
  const ErrorType = ERROR_BY_STATUS.get(response.status) ?? SynapsorError;
518
- throw new ErrorType(body.error ?? `Synapsor HTTP ${response.status}`, {
1102
+ const responseRequestId = response.headers.get("x-request-id") ?? body.request_id ?? requestId;
1103
+ const retryableHeader = response.headers.get("x-synapsor-retryable");
1104
+ throw new ErrorType(body.message ?? body.error ?? `Synapsor HTTP ${response.status}`, {
519
1105
  status: response.status,
520
1106
  payload: body,
1107
+ code: body.code ?? body.error,
1108
+ requestId: responseRequestId,
1109
+ retryable: retryableHeader === null ? undefined : retryableHeader === "true",
521
1110
  });
522
1111
  }
523
1112
  return body;
@@ -542,6 +1131,212 @@ export class Synapsor {
542
1131
  }
543
1132
  }
544
1133
 
1134
+ class AgentRunsNamespace {
1135
+ constructor(client) {
1136
+ this.client = client;
1137
+ }
1138
+
1139
+ async start({ workflow, version = "v1", input = {}, session = undefined } = {}) {
1140
+ const response = await this.client.beginAgentRun(workflow, { version, input, session });
1141
+ return new AgentRun(this.client, response);
1142
+ }
1143
+
1144
+ async explain(runId, options = {}) {
1145
+ return this.client.explainAgentRun(runId, options);
1146
+ }
1147
+ }
1148
+
1149
+ class AgentRun {
1150
+ constructor(client, response) {
1151
+ Object.assign(this, response);
1152
+ this.client = client;
1153
+ this.response = { ...response };
1154
+ this.runId = response.run_id ?? response.id;
1155
+ if (this.runId === undefined || this.runId === "") {
1156
+ throw new Error("agent run response did not include run_id");
1157
+ }
1158
+ }
1159
+
1160
+ toJSON() {
1161
+ return { ...this.response };
1162
+ }
1163
+
1164
+ async invokeCapability(capability, {
1165
+ stepKey = undefined,
1166
+ step_key = undefined,
1167
+ arguments: args = {},
1168
+ mode = undefined,
1169
+ autoBranch = undefined,
1170
+ auto_branch = undefined,
1171
+ responseEnvelope = undefined,
1172
+ response_envelope = undefined,
1173
+ includeAuditTrail = undefined,
1174
+ include_audit_trail = undefined,
1175
+ settlementPolicy = undefined,
1176
+ settlement_policy = undefined,
1177
+ session = undefined,
1178
+ } = {}) {
1179
+ const step = stepKey ?? step_key;
1180
+ if (step === undefined || step === "") {
1181
+ throw new Error("run.invokeCapability requires stepKey");
1182
+ }
1183
+ return this.client.invokeAgentCapability(capability, args, {
1184
+ runId: this.runId,
1185
+ stepKey: step,
1186
+ mode,
1187
+ autoBranch: autoBranch ?? auto_branch,
1188
+ responseEnvelope: responseEnvelope ?? response_envelope,
1189
+ includeAuditTrail: includeAuditTrail ?? include_audit_trail,
1190
+ settlementPolicy: settlementPolicy ?? settlement_policy,
1191
+ session,
1192
+ });
1193
+ }
1194
+
1195
+ async proposeExternalAction(action, {
1196
+ stepKey = undefined,
1197
+ step_key = undefined,
1198
+ arguments: args = {},
1199
+ idempotencyKey = undefined,
1200
+ idempotency_key = undefined,
1201
+ session = undefined,
1202
+ } = {}) {
1203
+ const step = stepKey ?? step_key;
1204
+ if (step === undefined || step === "") {
1205
+ throw new Error("run.proposeExternalAction requires stepKey");
1206
+ }
1207
+ return this.client.proposeExternalAction(action, {
1208
+ runId: this.runId,
1209
+ stepKey: step,
1210
+ arguments: args,
1211
+ idempotencyKey: idempotencyKey ?? idempotency_key,
1212
+ session,
1213
+ });
1214
+ }
1215
+
1216
+ async recordStep(stepKey, options = {}) {
1217
+ return this.client.recordAgentStep(this.runId, stepKey, options);
1218
+ }
1219
+
1220
+ async checkpoint(stepKey, options = {}) {
1221
+ return this.client.checkpointAgentRun(this.runId, stepKey, options);
1222
+ }
1223
+
1224
+ async recordGuardrailEvent(stepKey, options = {}) {
1225
+ return this.client.recordGuardrailEvent(this.runId, stepKey, options);
1226
+ }
1227
+
1228
+ async handoff(stepKey, options = {}) {
1229
+ return this.client.handoffAgentRun(this.runId, stepKey, options);
1230
+ }
1231
+
1232
+ async interrupt(stepKey, options = {}) {
1233
+ return this.client.interruptAgentRun(this.runId, stepKey, options);
1234
+ }
1235
+
1236
+ async complete(output = {}, { status = "completed", session = undefined } = {}) {
1237
+ return this.client.endAgentRun(this.runId, { status, output, session });
1238
+ }
1239
+
1240
+ async settle({
1241
+ settlementPolicy = undefined,
1242
+ settlement_policy = undefined,
1243
+ policy = undefined,
1244
+ idempotencyKey = undefined,
1245
+ idempotency_key = undefined,
1246
+ session = undefined,
1247
+ } = {}) {
1248
+ return this.client.settleAgentRun(this.runId, {
1249
+ settlementPolicy: settlementPolicy ?? settlement_policy ?? policy,
1250
+ idempotencyKey: idempotencyKey ?? idempotency_key,
1251
+ session,
1252
+ });
1253
+ }
1254
+
1255
+ async explain(options = {}) {
1256
+ return this.client.explainAgentRun(this.runId, options);
1257
+ }
1258
+ }
1259
+
1260
+ class ExternalActionsNamespace {
1261
+ constructor(client) {
1262
+ this.client = client;
1263
+ }
1264
+
1265
+ async propose(action, options = {}) {
1266
+ return this.client.proposeExternalAction(action, options);
1267
+ }
1268
+
1269
+ async claim(options = {}) {
1270
+ return this.client.claimExternalAction(options);
1271
+ }
1272
+
1273
+ async approve(actionInstanceId, options = {}) {
1274
+ return this.client.approveExternalAction(actionInstanceId, options);
1275
+ }
1276
+
1277
+ async confirm(actionInstanceId, options = {}) {
1278
+ return this.client.confirmExternalAction(actionInstanceId, options);
1279
+ }
1280
+ }
1281
+
1282
+ class AgentEvalsNamespace {
1283
+ constructor(client) {
1284
+ this.client = client;
1285
+ }
1286
+
1287
+ async create(evalName, options = {}) {
1288
+ return this.client.createAgentEval(evalName, options);
1289
+ }
1290
+
1291
+ async run(evalName, {
1292
+ againstBranch = undefined,
1293
+ targetBranch = undefined,
1294
+ target_branch = undefined,
1295
+ replayMode = undefined,
1296
+ replay_mode = undefined,
1297
+ session = undefined,
1298
+ } = {}) {
1299
+ const response = await this.client.runAgentEval(evalName, {
1300
+ targetBranch: targetBranch ?? target_branch ?? againstBranch,
1301
+ replayMode: replayMode ?? replay_mode,
1302
+ session,
1303
+ });
1304
+ return new AgentEvalRun(this.client, response);
1305
+ }
1306
+
1307
+ async failures(evalRunId, options = {}) {
1308
+ return this.client.agentEvalFailures(evalRunId, options);
1309
+ }
1310
+
1311
+ async diff(baselineEvalRunId, candidateEvalRunId, options = {}) {
1312
+ return this.client.diffAgentEval(baselineEvalRunId, candidateEvalRunId, options);
1313
+ }
1314
+ }
1315
+
1316
+ class AgentEvalRun {
1317
+ constructor(client, response) {
1318
+ Object.assign(this, response);
1319
+ this.client = client;
1320
+ this.response = { ...response };
1321
+ this.evalRunId = response.eval_run_id ?? response.id;
1322
+ if (this.evalRunId === undefined || this.evalRunId === "") {
1323
+ throw new Error("agent eval response did not include eval_run_id");
1324
+ }
1325
+ }
1326
+
1327
+ toJSON() {
1328
+ return { ...this.response };
1329
+ }
1330
+
1331
+ async failures(options = {}) {
1332
+ return this.client.agentEvalFailures(this.evalRunId, options);
1333
+ }
1334
+
1335
+ async diffAgainst(baselineEvalRunId, options = {}) {
1336
+ return this.client.diffAgentEval(baselineEvalRunId, this.evalRunId, options);
1337
+ }
1338
+ }
1339
+
545
1340
  function factCommand(kind, options) {
546
1341
  const command = {
547
1342
  kind,
@@ -568,6 +1363,21 @@ function factCommand(kind, options) {
568
1363
  if (options.valid_to !== undefined) {
569
1364
  command.valid_to = options.valid_to;
570
1365
  }
1366
+ for (const [from, to] of [
1367
+ ["policy", "policy"],
1368
+ ["policyName", "policy_name"],
1369
+ ["policy_name", "policy_name"],
1370
+ ["evidenceBundleId", "evidence_bundle_id"],
1371
+ ["evidence_bundle_id", "evidence_bundle_id"],
1372
+ ["runId", "run_id"],
1373
+ ["run_id", "run_id"],
1374
+ ["stepKey", "step_key"],
1375
+ ["step_key", "step_key"],
1376
+ ]) {
1377
+ if (options[from] !== undefined) {
1378
+ command[to] = options[from];
1379
+ }
1380
+ }
571
1381
  return command;
572
1382
  }
573
1383