@stacklink/sdk 0.3.15 → 0.6.2

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 (34) hide show
  1. package/README.md +26 -0
  2. package/compatibility.json +1 -1
  3. package/dist/src/domain/billing-plans.d.ts +827 -0
  4. package/dist/src/domain/billing-plans.d.ts.map +1 -0
  5. package/dist/src/domain/billing-plans.js +893 -0
  6. package/dist/src/domain/billing-plans.js.map +1 -0
  7. package/dist/src/domain/bot-avatar.d.ts +46 -0
  8. package/dist/src/domain/bot-avatar.d.ts.map +1 -0
  9. package/dist/src/domain/bot-avatar.js +436 -0
  10. package/dist/src/domain/bot-avatar.js.map +1 -0
  11. package/dist/src/domain/bot-contract.d.ts +1472 -700
  12. package/dist/src/domain/bot-contract.d.ts.map +1 -1
  13. package/dist/src/domain/bot-contract.js +133 -10
  14. package/dist/src/domain/bot-contract.js.map +1 -1
  15. package/dist/src/domain/memory-contract.d.ts +275 -275
  16. package/dist/src/domain/platform-approval-contract.d.ts +10 -4
  17. package/dist/src/domain/platform-approval-contract.d.ts.map +1 -1
  18. package/dist/src/domain/platform-approval-contract.js.map +1 -1
  19. package/dist/src/domain/provider-branding.d.ts.map +1 -1
  20. package/dist/src/domain/provider-branding.js +3 -0
  21. package/dist/src/domain/provider-branding.js.map +1 -1
  22. package/dist/src/domain/stacklink-meta-tools.d.ts +21 -5
  23. package/dist/src/domain/stacklink-meta-tools.d.ts.map +1 -1
  24. package/dist/src/domain/stacklink-meta-tools.js +73 -45
  25. package/dist/src/domain/stacklink-meta-tools.js.map +1 -1
  26. package/dist/src/domain/workflow-contract.d.ts +24338 -0
  27. package/dist/src/domain/workflow-contract.d.ts.map +1 -0
  28. package/dist/src/domain/workflow-contract.js +95 -0
  29. package/dist/src/domain/workflow-contract.js.map +1 -0
  30. package/dist/src/sdk/index.d.ts +329 -30
  31. package/dist/src/sdk/index.d.ts.map +1 -1
  32. package/dist/src/sdk/index.js +194 -47
  33. package/dist/src/sdk/index.js.map +1 -1
  34. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  import { botApiSignaturePayload } from '../domain/bot-contract.js';
2
2
  import { materializeStacklinkWorkbenchExecutionResponse, } from '../domain/stacklink-meta-tools.js';
3
3
  export { StacklinkMemoryLifecycle, StacklinkMemoryLifecycleError, createStacklinkMemoryLifecycle, serializeCompletedConversationTurn, } from './memory-lifecycle.js';
4
- export const stacklinkTypeScriptSdkVersion = '0.3.15';
4
+ export const stacklinkTypeScriptSdkVersion = '0.6.2';
5
5
  const workbenchReusableRunComparisonWaitMs = 120_000;
6
6
  const workbenchReusableRunComparisonPollMs = 1_000;
7
7
  export class StacklinkApiError extends Error {
@@ -9,12 +9,14 @@ export class StacklinkApiError extends Error {
9
9
  code;
10
10
  details;
11
11
  retryAfterMs;
12
- constructor(message, statusCode, code, details, retryAfterMs) {
12
+ failureId;
13
+ constructor(message, statusCode, code, details, retryAfterMs, failureId) {
13
14
  super(message);
14
15
  this.statusCode = statusCode;
15
16
  this.code = code;
16
17
  this.details = details;
17
18
  this.retryAfterMs = retryAfterMs;
19
+ this.failureId = failureId;
18
20
  this.name = 'StacklinkApiError';
19
21
  }
20
22
  }
@@ -231,6 +233,7 @@ export class Stacklink {
231
233
  timeoutMs;
232
234
  maxAttempts;
233
235
  bots;
236
+ workflows;
234
237
  approvals;
235
238
  webhooks;
236
239
  triggers;
@@ -259,6 +262,7 @@ export class Stacklink {
259
262
  maxEntries: options.cache?.maxEntries ?? 500,
260
263
  };
261
264
  this.bots = new StacklinkBotsClient(this);
265
+ this.workflows = new StacklinkWorkflowsClient(this);
262
266
  this.approvals = new StacklinkApprovalsClient(this);
263
267
  this.webhooks = new StacklinkWebhooksClient(this);
264
268
  this.triggers = new StacklinkTriggersClient(this);
@@ -540,16 +544,25 @@ export class Stacklink {
540
544
  body: input,
541
545
  });
542
546
  }
547
+ async resumeWorkbenchExecution(input, options = {}) {
548
+ return this.request('/v1/workbench/resume', { method: 'POST', body: input, signal: options.signal });
549
+ }
543
550
  async workbenchModels() {
544
551
  return this.request('/v1/workbench/models');
545
552
  }
546
553
  async runWorkbench(input, options = {}) {
547
554
  const response = await this.request('/v1/workbench/run', {
548
555
  method: 'POST',
556
+ idempotencyKey: typeof input.idempotencyKey === 'string' ? input.idempotencyKey : undefined,
549
557
  body: input,
550
558
  signal: options.signal,
551
559
  });
552
560
  this.attachAbortSignalCancellation(input, response, options.signal);
561
+ if (isSdkRecord(response.operation) && typeof response.operation.operationId === 'string' && typeof response.operation.workbenchSessionId === 'string' && typeof input.sessionId === 'string') {
562
+ const { operation, ...accepted } = response;
563
+ const completed = await this.waitForWorkbenchOperation({ sessionId: input.sessionId, workbenchSessionId: response.operation.workbenchSessionId, operationId: response.operation.operationId }, options);
564
+ return { ...accepted, ...completed };
565
+ }
553
566
  if (input.operation === 'run_code' && input.background !== true) {
554
567
  return this.waitForWorkbenchResponse(input, response, options);
555
568
  }
@@ -561,35 +574,83 @@ export class Stacklink {
561
574
  body: input,
562
575
  });
563
576
  }
564
- async deleteWorkbenchSession(input) {
565
- return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}`, {
566
- method: 'DELETE',
567
- query: { sessionId: input.sessionId },
577
+ async getWorkbenchOperation(input, options = {}) {
578
+ return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/operations/${input.operationId}`, { query: { sessionId: input.sessionId }, ...options });
579
+ }
580
+ async waitForWorkbenchOperation(input, options = {}) {
581
+ const deadline = Date.now() + (options.timeoutMs ?? 300_000);
582
+ while (Date.now() < deadline) {
583
+ options.signal?.throwIfAborted();
584
+ let operation;
585
+ try {
586
+ ({ operation } = await this.getWorkbenchOperation(input, { signal: options.signal, timeoutMs: Math.min(15_000, Math.max(1, deadline - Date.now())) }));
587
+ }
588
+ catch (error) {
589
+ throw new StacklinkApiError('Observation stopped. Resume the same Workbench operation.', error instanceof StacklinkApiError ? error.statusCode : 408, 'workbench_operation_observation_stopped', { ...input, cause: error instanceof StacklinkApiError ? error.code : undefined });
590
+ }
591
+ if (operation.status === 'completed')
592
+ return operation.result;
593
+ if (operation.status === 'failed')
594
+ throw new StacklinkApiError('The Workbench operation needs attention.', 409, 'workbench_operation_failed', { ...input, operation });
595
+ const delayMs = Math.min(Math.max(100, operation.retryAfterMs), Math.max(0, deadline - Date.now()));
596
+ await new Promise((resolve, reject) => {
597
+ const abort = () => { clearTimeout(timer); reject(options.signal?.reason); };
598
+ const timer = setTimeout(() => { options.signal?.removeEventListener('abort', abort); resolve(); }, delayMs);
599
+ options.signal?.addEventListener('abort', abort, { once: true });
600
+ });
601
+ }
602
+ throw new StacklinkApiError('The Workbench operation is still running. Resume with its operation ID.', 408, 'workbench_operation_pending', input);
603
+ }
604
+ async deleteWorkbenchSession(input, options = {}) {
605
+ const result = await this.request(`/v1/workbench/sessions/${input.workbenchSessionId}`, {
606
+ method: 'DELETE', query: { sessionId: input.sessionId }, signal: options.signal,
568
607
  });
608
+ return result.deleted ? result : this.waitForWorkbenchOperation({ ...input, operationId: result.operation.operationId }, options);
569
609
  }
570
610
  async listWorkbenchFiles(input) {
571
- return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files`, { query: { sessionId: input.sessionId } });
611
+ return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files`, { query: { sessionId: input.sessionId, cursor: input.cursor, limit: input.limit } });
572
612
  }
573
613
  async readWorkbenchFile(input) {
574
614
  return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files`, { query: { sessionId: input.sessionId, path: input.path } });
575
615
  }
576
616
  async uploadWorkbenchFile(input) {
577
- return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files`, {
578
- method: 'PUT',
579
- body: {
580
- sessionId: input.sessionId,
581
- path: input.path,
582
- contentBase64: base64Encode(typeof input.content === 'string' ? utf8(input.content) : input.content),
583
- contentType: input.contentType,
584
- metadata: input.metadata,
585
- },
586
- });
617
+ const uploadId = input.uploadId ?? crypto.randomUUID();
618
+ try {
619
+ const result = await this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files`, {
620
+ method: 'PUT',
621
+ body: {
622
+ sessionId: input.sessionId,
623
+ uploadId,
624
+ path: input.path,
625
+ contentBase64: base64Encode(typeof input.content === 'string' ? utf8(input.content) : input.content),
626
+ contentType: input.contentType,
627
+ metadata: input.metadata,
628
+ },
629
+ });
630
+ return ('file' in result) ? result : await this.waitForWorkbenchOperation({ sessionId: input.sessionId, workbenchSessionId: input.workbenchSessionId, operationId: result.operation.operationId });
631
+ }
632
+ catch (error) {
633
+ if (error instanceof StacklinkApiError)
634
+ throw new StacklinkApiError(error.message, error.statusCode, error.code, { ...isSdkRecord(error.details) ? error.details : {}, uploadId, workbenchSessionId: input.workbenchSessionId, sessionId: input.sessionId }, error.retryAfterMs, error.failureId);
635
+ throw error;
636
+ }
587
637
  }
588
638
  async prepareWorkbenchFileUpload(input) {
589
- return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files/upload-url`, { method: 'POST', body: input });
639
+ const { workbenchSessionId, ...body } = input;
640
+ const uploadId = input.uploadId ?? crypto.randomUUID();
641
+ try {
642
+ return await this.request(`/v1/workbench/sessions/${workbenchSessionId}/files/upload-url`, { method: 'POST', body: { ...body, uploadId } });
643
+ }
644
+ catch (error) {
645
+ if (error instanceof StacklinkApiError)
646
+ throw new StacklinkApiError(error.message, error.statusCode, error.code, { ...isSdkRecord(error.details) ? error.details : {}, uploadId, workbenchSessionId, sessionId: input.sessionId }, error.retryAfterMs, error.failureId);
647
+ throw error;
648
+ }
590
649
  }
591
- async finalizeWorkbenchFileUpload(input) {
592
- return this.request(`/v1/workbench/sessions/${input.workbenchSessionId}/files/finalize-upload`, { method: 'POST', body: input });
650
+ async finalizeWorkbenchFileUpload(input, options = {}) {
651
+ const { workbenchSessionId, ...body } = input;
652
+ const result = await this.request(`/v1/workbench/sessions/${workbenchSessionId}/files/finalize-upload`, { method: 'POST', body, signal: options.signal });
653
+ return ('file' in result) ? result : this.waitForWorkbenchOperation({ sessionId: input.sessionId, workbenchSessionId, operationId: result.operation.operationId }, options);
593
654
  }
594
655
  async uploadWorkbenchFileDirect(input) {
595
656
  const content = typeof input.content === 'string' ? utf8(input.content) : input.content;
@@ -599,15 +660,23 @@ export class Stacklink {
599
660
  workbenchSessionId: input.workbenchSessionId,
600
661
  path: input.path,
601
662
  contentType,
602
- sizeBytes: content.byteLength,
603
- });
604
- const response = await this.fetchImpl(prepared.upload.uploadUrl, {
605
- method: 'PUT',
606
- headers: prepared.upload.requiredHeaders,
607
- body: content,
663
+ sizeBytes: content instanceof Blob ? content.size : content.byteLength,
664
+ uploadId: input.uploadId ?? crypto.randomUUID(),
608
665
  });
609
- if (!response.ok) {
610
- throw new StacklinkApiError(`Workbench file upload failed with ${response.status}`, response.status, 'workbench_file_direct_upload_failed');
666
+ let response;
667
+ try {
668
+ response = await this.fetchImpl(prepared.upload.uploadUrl, {
669
+ method: 'PUT',
670
+ headers: prepared.upload.requiredHeaders,
671
+ body: content,
672
+ signal: input.signal ? AbortSignal.any([input.signal, AbortSignal.timeout(input.transferTimeoutMs ?? 300_000)]) : AbortSignal.timeout(input.transferTimeoutMs ?? 300_000),
673
+ });
674
+ }
675
+ catch (error) {
676
+ throw new StacklinkApiError("Upload outcome is unknown. Finalize the same uploadId to reconcile; do not create another upload.", 408, "workbench_upload_outcome_unknown", { uploadId: prepared.upload.uploadId, workbenchSessionId: input.workbenchSessionId, sessionId: input.sessionId });
677
+ }
678
+ if (!response.ok && response.status !== 412) {
679
+ throw new StacklinkApiError(`Workbench file upload failed with ${response.status}`, response.status, 'workbench_file_direct_upload_failed', { uploadId: prepared.upload.uploadId, workbenchSessionId: input.workbenchSessionId, sessionId: input.sessionId });
611
680
  }
612
681
  return this.finalizeWorkbenchFileUpload({
613
682
  sessionId: input.sessionId,
@@ -621,7 +690,7 @@ export class Stacklink {
621
690
  }
622
691
  async downloadWorkbenchFile(input) {
623
692
  const signed = await this.getWorkbenchFileDownload(input);
624
- const response = await this.fetchImpl(signed.downloadUrl);
693
+ const response = await this.fetchImpl(signed.downloadUrl, { headers: signed.requiredHeaders ?? {}, signal: AbortSignal.timeout(300_000) });
625
694
  if (!response.ok) {
626
695
  throw new StacklinkApiError(`Workbench file download failed with ${response.status}`, response.status, 'workbench_file_download_failed');
627
696
  }
@@ -725,6 +794,7 @@ export class Stacklink {
725
794
  async runWorkbenchBash(input, options = {}) {
726
795
  const response = await this.request('/v1/workbench/bash', {
727
796
  method: 'POST',
797
+ idempotencyKey: typeof input.idempotencyKey === 'string' ? input.idempotencyKey : undefined,
728
798
  body: input,
729
799
  signal: options.signal,
730
800
  });
@@ -735,12 +805,12 @@ export class Stacklink {
735
805
  return response;
736
806
  }
737
807
  async waitForWorkbenchResponse(input, accepted, options = {}) {
738
- const sessionId = typeof input.sessionId === 'string' ? input.sessionId : undefined;
808
+ const sessionId = workbenchExecutionSessionId(accepted, typeof input.sessionId === 'string' ? input.sessionId : undefined);
739
809
  const executionId = typeof accepted.executionId === 'string' ? accepted.executionId : undefined;
740
810
  const executionKind = accepted.executionKind === 'workbench_code' || accepted.executionKind === 'workbench_bash'
741
811
  ? accepted.executionKind
742
812
  : undefined;
743
- if (!sessionId || !executionId || !executionKind || accepted.status === 'waiting_for_approval')
813
+ if (!sessionId || !executionId || !executionKind || ['waiting_for_approval', 'waiting_saved', 'waiting_ready'].includes(String(accepted.status)))
744
814
  return accepted;
745
815
  while (true) {
746
816
  const waited = await this.waitForExecution({
@@ -750,7 +820,7 @@ export class Stacklink {
750
820
  waitTimeoutMs: 10_000,
751
821
  }, options);
752
822
  const execution = isSdkRecord(waited.execution) ? waited.execution : undefined;
753
- if (execution && (isTerminalSdkExecution(execution) || execution.status === 'waiting_for_approval')) {
823
+ if (execution && (isTerminalSdkExecution(execution) || execution.nextAction === 'wait_for_approval' || ['waiting_for_approval', 'waiting_saved', 'waiting_ready'].includes(String(execution.status)))) {
754
824
  return materializeStacklinkWorkbenchExecutionResponse(accepted, execution);
755
825
  }
756
826
  }
@@ -1190,10 +1260,21 @@ export class Stacklink {
1190
1260
  return this.requestRaw(`/v1/workbench/artifacts/${artifactId}/download`);
1191
1261
  }
1192
1262
  async promoteWorkbenchArtifact(input) {
1193
- return this.request('/v1/workbench/artifacts', {
1194
- method: 'POST',
1195
- body: input,
1196
- });
1263
+ const artifactRef = input.artifactRef ?? `wba_${crypto.randomUUID()}`;
1264
+ try {
1265
+ const result = await this.request('/v1/workbench/artifacts', {
1266
+ method: 'POST',
1267
+ body: { ...input, artifactRef },
1268
+ });
1269
+ if (!result.operation)
1270
+ return result;
1271
+ return await this.waitForWorkbenchOperation({ sessionId: input.sessionId, workbenchSessionId: input.workbenchSessionId, operationId: result.operation.operationId });
1272
+ }
1273
+ catch (error) {
1274
+ if (error instanceof StacklinkApiError)
1275
+ throw new StacklinkApiError(error.message, error.statusCode, error.code, { ...isSdkRecord(error.details) ? error.details : {}, artifactRef, workbenchSessionId: input.workbenchSessionId, sessionId: input.sessionId }, error.retryAfterMs, error.failureId);
1276
+ throw error;
1277
+ }
1197
1278
  }
1198
1279
  async deleteWorkbenchArtifact(input) {
1199
1280
  return this.request(`/v1/workbench/artifacts/${input.artifactId}`, {
@@ -1447,7 +1528,7 @@ export class Stacklink {
1447
1528
  attachAbortSignalCancellation(input, response, signal) {
1448
1529
  if (!signal)
1449
1530
  return;
1450
- const sessionId = typeof input.sessionId === 'string' ? input.sessionId : undefined;
1531
+ const sessionId = workbenchExecutionSessionId(response, typeof input.sessionId === 'string' ? input.sessionId : undefined);
1451
1532
  const executionId = typeof response.executionId === 'string'
1452
1533
  ? response.executionId
1453
1534
  : isSdkRecord(response.executionLog) && typeof response.executionLog.id === 'string'
@@ -1494,7 +1575,7 @@ export class Stacklink {
1494
1575
  const details = body && typeof body === 'object' && 'details' in body
1495
1576
  ? body.details
1496
1577
  : undefined;
1497
- throw new StacklinkApiError(message, response.status, code, details);
1578
+ throw new StacklinkApiError(message, response.status, code, details, undefined, response.headers.get('x-stacklink-failure-id') ?? (isSdkRecord(body) && typeof body.failureId === 'string' ? body.failureId : undefined));
1498
1579
  }
1499
1580
  return body;
1500
1581
  }
@@ -1509,7 +1590,7 @@ export class Stacklink {
1509
1590
  catch {
1510
1591
  parsed = null;
1511
1592
  }
1512
- throw new StacklinkApiError(isSdkRecord(parsed) && typeof parsed.message === 'string' ? parsed.message : `Stacklink request failed with ${response.status}`, response.status, isSdkRecord(parsed) && typeof parsed.code === 'string' ? parsed.code : undefined, isSdkRecord(parsed) ? parsed.details : undefined, response.headers.has('retry-after') ? sdkRetryDelayMs(response, 1) : undefined);
1593
+ throw new StacklinkApiError(isSdkRecord(parsed) && typeof parsed.message === 'string' ? parsed.message : `Stacklink request failed with ${response.status}`, response.status, isSdkRecord(parsed) && typeof parsed.code === 'string' ? parsed.code : undefined, isSdkRecord(parsed) ? parsed.details : undefined, response.headers.has('retry-after') ? sdkRetryDelayMs(response, 1) : undefined, response.headers.get('x-stacklink-failure-id') ?? undefined);
1513
1594
  }
1514
1595
  return response;
1515
1596
  }
@@ -1772,6 +1853,7 @@ export class Stacklink {
1772
1853
  ...extraHeaders,
1773
1854
  'x-stacklink-client-channel': 'ts_sdk',
1774
1855
  'x-stacklink-client-name': 'stacklink-ts',
1856
+ 'x-stacklink-workbench-protocol': '2',
1775
1857
  'x-stacklink-client-version': stacklinkTypeScriptSdkVersion,
1776
1858
  };
1777
1859
  }
@@ -1781,6 +1863,7 @@ export class Stacklink {
1781
1863
  ...extraHeaders,
1782
1864
  'x-stacklink-client-channel': 'ts_sdk',
1783
1865
  'x-stacklink-client-name': 'stacklink-ts',
1866
+ 'x-stacklink-workbench-protocol': '2',
1784
1867
  'x-stacklink-client-version': stacklinkTypeScriptSdkVersion,
1785
1868
  };
1786
1869
  }
@@ -2403,6 +2486,12 @@ export class StacklinkSessionMemoryClient {
2403
2486
  });
2404
2487
  }
2405
2488
  }
2489
+ function workbenchExecutionSessionId(response, fallback) {
2490
+ const workspace = response.workbenchSession;
2491
+ return isSdkRecord(workspace) && typeof workspace.platformSessionId === 'string'
2492
+ ? workspace.platformSessionId
2493
+ : fallback;
2494
+ }
2406
2495
  export class StacklinkWorkbenchRun {
2407
2496
  client;
2408
2497
  platformSessionId;
@@ -2413,6 +2502,7 @@ export class StacklinkWorkbenchRun {
2413
2502
  this.client = client;
2414
2503
  this.platformSessionId = platformSessionId;
2415
2504
  this.response = response;
2505
+ this.platformSessionId = workbenchExecutionSessionId(response, platformSessionId) ?? platformSessionId;
2416
2506
  this.executionId = response.executionId;
2417
2507
  this.executionKind = response.executionKind;
2418
2508
  }
@@ -2456,8 +2546,9 @@ export class StacklinkWorkbenchFiles {
2456
2546
  this.platformSessionId = platformSessionId;
2457
2547
  this.workbenchSessionId = workbenchSessionId;
2458
2548
  }
2459
- async list() {
2549
+ async list(options = {}) {
2460
2550
  return this.client.listWorkbenchFiles({
2551
+ ...options,
2461
2552
  sessionId: this.platformSessionId,
2462
2553
  workbenchSessionId: await this.workbenchSessionId(),
2463
2554
  });
@@ -2606,6 +2697,9 @@ export class StacklinkSessionSandbox {
2606
2697
  this.runs = new StacklinkWorkbenchRuns(client, platformSessionId, () => this.ensureWorkbenchSession());
2607
2698
  this.artifacts = new StacklinkWorkbenchArtifacts(client, platformSessionId, () => this.ensureWorkbenchSession());
2608
2699
  }
2700
+ resume(executionId, expectedWaitRevision, checkpointId) {
2701
+ return this.client.resumeWorkbenchExecution({ sessionId: this.platformSessionId, executionId, expectedWaitRevision, checkpointId });
2702
+ }
2609
2703
  async use(workbenchSessionId, input = {}) {
2610
2704
  const response = await this.client.useWorkbenchSession({
2611
2705
  sessionId: this.platformSessionId,
@@ -2623,7 +2717,9 @@ export class StacklinkSessionSandbox {
2623
2717
  workbenchSessionId,
2624
2718
  operation: 'run_code',
2625
2719
  }, options);
2626
- this.activeWorkbenchSessionId = response.workbenchSession.id;
2720
+ if (!response.workbenchSession.platformSessionId || response.workbenchSession.platformSessionId === this.platformSessionId) {
2721
+ this.activeWorkbenchSessionId = response.workbenchSession.id;
2722
+ }
2627
2723
  return new StacklinkWorkbenchRun(this.client, this.platformSessionId, response);
2628
2724
  }
2629
2725
  async bash(input, options = {}) {
@@ -2633,7 +2729,9 @@ export class StacklinkSessionSandbox {
2633
2729
  sessionId: this.platformSessionId,
2634
2730
  workbenchSessionId,
2635
2731
  }, options);
2636
- this.activeWorkbenchSessionId = response.workbenchSession.id;
2732
+ if (!response.workbenchSession.platformSessionId || response.workbenchSession.platformSessionId === this.platformSessionId) {
2733
+ this.activeWorkbenchSessionId = response.workbenchSession.id;
2734
+ }
2637
2735
  return new StacklinkWorkbenchRun(this.client, this.platformSessionId, response);
2638
2736
  }
2639
2737
  async destroy() {
@@ -2743,6 +2841,7 @@ export class ToolRouterSession {
2743
2841
  body: {
2744
2842
  connectionRef,
2745
2843
  oauthScopeProfile: input.oauthScopeProfile,
2844
+ amazonSellerRegion: input.amazonSellerRegion,
2746
2845
  metadata: input.metadata ?? {},
2747
2846
  },
2748
2847
  });
@@ -3121,7 +3220,7 @@ function unwrapMcpToolResult(response) {
3121
3220
  const code = typeof response.error.code === 'string' || typeof response.error.code === 'number'
3122
3221
  ? String(response.error.code)
3123
3222
  : undefined;
3124
- throw new StacklinkApiError(message, 400, code, response.error.data);
3223
+ throw new StacklinkApiError(message, 400, code, response.error.data, undefined, isSdkRecord(response.error.data) && typeof response.error.data.failureId === 'string' ? response.error.data.failureId : undefined);
3125
3224
  }
3126
3225
  const result = response.result;
3127
3226
  if (!isSdkRecord(result))
@@ -3134,7 +3233,8 @@ function unwrapMcpToolResult(response) {
3134
3233
  .map((item) => String(item.text))
3135
3234
  .join('\n')
3136
3235
  : '';
3137
- throw new StacklinkApiError(errorText || 'Stacklink MCP tool call failed.', 400, 'mcp_tool_error', result);
3236
+ const failure = isSdkRecord(result.structuredContent) && isSdkRecord(result.structuredContent.error) ? result.structuredContent.error : undefined;
3237
+ throw new StacklinkApiError(errorText || 'Stacklink MCP tool call failed.', 400, 'mcp_tool_error', result, undefined, typeof failure?.failureId === 'string' ? failure.failureId : undefined);
3138
3238
  }
3139
3239
  if ('structuredContent' in result)
3140
3240
  return result.structuredContent;
@@ -3197,13 +3297,14 @@ export class StacklinkBotsClient {
3197
3297
  delete(botId) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}`, { method: 'DELETE' }); }
3198
3298
  deleteCustomerData(botId, sessionId) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/customer-data/delete`, { method: 'POST', body: { sessionId } }); }
3199
3299
  setState(botId, input) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/state`, { method: 'POST', body: input }); }
3200
- list() { return this.client.request('/v1/bots'); }
3201
- create(name) { return this.client.request('/v1/bots', { method: 'POST', body: { name } }); }
3300
+ list(query) { return this.client.request('/v1/bots', { query }); }
3301
+ create(name, identity) { return this.client.request('/v1/bots', { method: 'POST', body: { name, ...identity } }); }
3202
3302
  get(botId) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}`); }
3203
3303
  update(botId, revision, patch) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}`, { method: 'PATCH', body: { revision, patch } }); }
3204
3304
  publish(botId, revision, channels) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/publish`, { method: 'POST', body: { revision, channels } }); }
3205
3305
  run(botId, input) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/runs`, { method: 'POST', body: { ...input, runId: input.runId ?? globalThis.crypto.randomUUID() } }); }
3206
3306
  getRun(botId, runId) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/runs/${encodeURIComponent(runId)}`); }
3307
+ continueRun(botId, runId) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/runs/${encodeURIComponent(runId)}/continue`, { method: 'POST', body: {} }); }
3207
3308
  /** Observe the same durable run. Returns when finished or when a person must act. */
3208
3309
  async wait(botId, runId, options = {}) {
3209
3310
  const deadline = Date.now() + positiveSdkInteger(options.totalTimeoutMs ?? 300_000, 'totalTimeoutMs');
@@ -3211,7 +3312,7 @@ export class StacklinkBotsClient {
3211
3312
  if (options.signal?.aborted)
3212
3313
  throw options.signal.reason;
3213
3314
  const run = await this.client.request(`/v1/bots/${encodeURIComponent(botId)}/runs/${encodeURIComponent(runId)}`, { signal: options.signal, timeoutMs: Math.min(30_000, deadline - Date.now()) });
3214
- if (['completed', 'failed', 'stopped', 'cancelled', 'waiting_for_approval', 'waiting_for_connection'].includes(run.status))
3315
+ if (['completed', 'failed', 'stopped', 'cancelled', 'waiting_for_approval', 'waiting_for_connection'].includes(run.status) || run.waitingOperation?.kind === 'funding')
3215
3316
  return run;
3216
3317
  await sdkDelay(Math.min(1000, Math.max(1, deadline - Date.now())), options.signal ?? AbortSignal.timeout(Math.max(1, deadline - Date.now())));
3217
3318
  }
@@ -3221,4 +3322,50 @@ export class StacklinkBotsClient {
3221
3322
  stream(botId, runId, options = {}) { return this.client.streamBotEvents(botId, runId, options); }
3222
3323
  cancel(botId, runId) { return this.client.request(`/v1/bots/${encodeURIComponent(botId)}/runs/${encodeURIComponent(runId)}/stop`, { method: 'POST', body: {} }); }
3223
3324
  }
3325
+ /** Visual and code-defined workflows use the same immutable graph and run API. */
3326
+ export class StacklinkWorkflowsClient {
3327
+ client;
3328
+ context;
3329
+ constructor(client, context) {
3330
+ this.client = client;
3331
+ this.context = context;
3332
+ }
3333
+ /** Backend administration of one customer; never expose the project key in the browser. */
3334
+ forCustomer(customerId) { return new StacklinkWorkflowsClient(this.client, { customerId }); }
3335
+ /** Person-scoped data/decision calls from your authenticated backend. Same workflow methods. */
3336
+ withEmbeddedAccess(projectId, accessToken) { return new StacklinkWorkflowsClient(this.client, { projectId, accessToken }); }
3337
+ request(path, init = {}) { return this.client.request(path, { ...init, headers: { ...init.headers, ...(this.context?.customerId ? { 'X-Stacklink-Customer': this.context.customerId } : {}), ...(this.context?.accessToken ? { authorization: `Bearer ${this.context.accessToken}` } : {}) } }); }
3338
+ path(id) { return `${this.context?.projectId ? `/embedded/api/projects/${encodeURIComponent(this.context.projectId)}/workflows` : '/v1/workflows'}${id ? `/${encodeURIComponent(id)}` : ''}`; }
3339
+ list(query) { return this.request(this.path(), { query }); }
3340
+ create(input) { return this.request(this.path(), { method: 'POST', body: input }); }
3341
+ get(id) { return this.request(this.path(id)); }
3342
+ update(id, input) { return this.request(this.path(id), { method: 'PATCH', body: input }); }
3343
+ validate(definition) { return this.request(`${this.path()}/validate`, { method: 'POST', body: { definition } }); }
3344
+ publish(id, revision) { return this.request(`${this.path(id)}/publish`, { method: 'POST', body: { revision } }); }
3345
+ setState(id, paused) { return this.request(`${this.path(id)}/state`, { method: 'POST', body: { paused } }); }
3346
+ versions(id) { return this.request(`${this.path(id)}/versions`); }
3347
+ run(id, input) { return this.request(`${this.path(id)}/runs`, { method: 'POST', body: input, idempotencyKey: input.jobKey }); }
3348
+ runs(id, query) { return this.request(`${this.path(id)}/runs`, { query }); }
3349
+ getRun(id, runId) { return this.request(`${this.path(id)}/runs/${encodeURIComponent(runId)}`); }
3350
+ events(id, runId, query) { return this.request(`${this.path(id)}/runs/${encodeURIComponent(runId)}/events`, { query }); }
3351
+ action(id, runId, input) { return this.request(`${this.path(id)}/runs/${encodeURIComponent(runId)}/actions`, { method: 'POST', body: input }); }
3352
+ triggers(id) { return this.request(`${this.path(id)}/triggers`); }
3353
+ saveTrigger(id, input) { return this.request(`${this.path(id)}/triggers`, { method: 'POST', body: input }); }
3354
+ deleteTrigger(id, triggerId, expectedRevision) { return this.request(`${this.path(id)}/triggers/${encodeURIComponent(triggerId)}`, { method: 'DELETE', query: { expectedRevision } }); }
3355
+ invokeTrigger(id, triggerId, requestId, input) { return this.request(`${this.path(id)}/triggers/${encodeURIComponent(triggerId)}/invoke`, { method: 'POST', body: { requestId, input }, idempotencyKey: requestId }); }
3356
+ previewSchedule(cron, timezone) { return this.request(`${this.path()}/schedule/preview`, { method: 'POST', body: { cron, timezone } }); }
3357
+ suggest(id, description, revision, sessionId) { return this.request(`${this.path(id)}/suggest`, { method: 'POST', body: { description, revision, sessionId }, timeoutMs: 60_000 }); }
3358
+ issueEmbeddedAccess(input) { return this.client.request('/v1/workflows/embed/access', { method: 'POST', body: input }); }
3359
+ exchangeEmbeddedAccess(bootstrapToken, origin) { return this.client.request('/v1/workflows/embed/exchange', { method: 'POST', body: { bootstrapToken, origin } }); }
3360
+ embedSettings() { return this.request(`${this.path()}/embed`); }
3361
+ saveEmbedSettings(input) { return this.request(`${this.path()}/embed`, { method: 'PUT', body: input }); }
3362
+ customers(query) { return this.request(`${this.path()}/embed/customers`, { query }); }
3363
+ customer(id) { return this.request(`${this.path()}/embed/customers/${encodeURIComponent(id)}`); }
3364
+ setCustomerAutomations(id, enabled) { return this.request(`${this.path()}/embed/customers/${encodeURIComponent(id)}/automations`, { method: 'POST', body: { enabled } }); }
3365
+ setCustomerPersonAccess(customerId, personId, action) { return this.request(`${this.path()}/embed/customers/${encodeURIComponent(customerId)}/people/${encodeURIComponent(personId)}/access`, { method: 'POST', body: { action } }); }
3366
+ analytics(query) { return this.request(`${this.path()}/analytics`, { query }); }
3367
+ activity(query) { return this.request(`${this.path()}/activity`, { query }); }
3368
+ approvals(query) { return this.request(`${this.path()}/approvals`, { query }); }
3369
+ decideApproval(id, input) { return this.request(`${this.path()}/approvals/${encodeURIComponent(id)}/decide`, { method: 'POST', body: input }); }
3370
+ }
3224
3371
  //# sourceMappingURL=index.js.map