@alter-ai/alter-sdk 0.2.0 → 0.2.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.
package/dist/index.js CHANGED
@@ -144,6 +144,87 @@ var TokenResponse = class _TokenResponse {
144
144
  return this.toString();
145
145
  }
146
146
  };
147
+ var ConnectionInfo = class {
148
+ id;
149
+ providerId;
150
+ attributes;
151
+ scopes;
152
+ accountIdentifier;
153
+ accountDisplayName;
154
+ status;
155
+ expiresAt;
156
+ createdAt;
157
+ lastUsedAt;
158
+ constructor(data) {
159
+ this.id = data.id;
160
+ this.providerId = data.provider_id;
161
+ this.attributes = data.attributes ?? {};
162
+ this.scopes = data.scopes ?? [];
163
+ this.accountIdentifier = data.account_identifier ?? null;
164
+ this.accountDisplayName = data.account_display_name ?? null;
165
+ this.status = data.status;
166
+ this.expiresAt = data.expires_at ?? null;
167
+ this.createdAt = data.created_at;
168
+ this.lastUsedAt = data.last_used_at ?? null;
169
+ Object.freeze(this);
170
+ }
171
+ toJSON() {
172
+ return {
173
+ id: this.id,
174
+ provider_id: this.providerId,
175
+ attributes: this.attributes,
176
+ scopes: this.scopes,
177
+ account_identifier: this.accountIdentifier,
178
+ account_display_name: this.accountDisplayName,
179
+ status: this.status,
180
+ expires_at: this.expiresAt,
181
+ created_at: this.createdAt,
182
+ last_used_at: this.lastUsedAt
183
+ };
184
+ }
185
+ toString() {
186
+ return `ConnectionInfo(id=${this.id}, provider=${this.providerId}, status=${this.status})`;
187
+ }
188
+ };
189
+ var ConnectSession = class {
190
+ sessionToken;
191
+ connectUrl;
192
+ expiresIn;
193
+ expiresAt;
194
+ constructor(data) {
195
+ this.sessionToken = data.session_token;
196
+ this.connectUrl = data.connect_url;
197
+ this.expiresIn = data.expires_in;
198
+ this.expiresAt = data.expires_at;
199
+ Object.freeze(this);
200
+ }
201
+ toJSON() {
202
+ return {
203
+ session_token: this.sessionToken,
204
+ connect_url: this.connectUrl,
205
+ expires_in: this.expiresIn,
206
+ expires_at: this.expiresAt
207
+ };
208
+ }
209
+ toString() {
210
+ return `ConnectSession(url=${this.connectUrl}, expires_in=${this.expiresIn})`;
211
+ }
212
+ };
213
+ var ConnectionListResult = class {
214
+ connections;
215
+ total;
216
+ limit;
217
+ offset;
218
+ hasMore;
219
+ constructor(data) {
220
+ this.connections = data.connections;
221
+ this.total = data.total;
222
+ this.limit = data.limit;
223
+ this.offset = data.offset;
224
+ this.hasMore = data.has_more;
225
+ Object.freeze(this);
226
+ }
227
+ };
147
228
  var SENSITIVE_HEADERS = /* @__PURE__ */ new Set([
148
229
  "authorization",
149
230
  "cookie",
@@ -238,7 +319,7 @@ function _extractAccessToken(token) {
238
319
  return value;
239
320
  }
240
321
  var _fetch;
241
- var SDK_VERSION = "0.2.0";
322
+ var SDK_VERSION = "0.2.2";
242
323
  var SDK_USER_AGENT = `alter-sdk-node/${SDK_VERSION}`;
243
324
  var VALID_ACTOR_TYPES = ["ai_agent", "mcp_server"];
244
325
  var HTTP_FORBIDDEN = 403;
@@ -334,7 +415,6 @@ var AlterVault = class _AlterVault {
334
415
  // Public readonly properties (frozen by Object.freeze)
335
416
  // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
336
417
  baseUrl;
337
- enableAuditLogging;
338
418
  /** Actor tracking configuration (readonly, not secret) */
339
419
  #actorType;
340
420
  #actorIdentifier;
@@ -362,7 +442,6 @@ var AlterVault = class _AlterVault {
362
442
  /\/+$/,
363
443
  ""
364
444
  );
365
- this.enableAuditLogging = options.enableAuditLogging ?? true;
366
445
  const timeoutMs = options.timeout ?? 3e4;
367
446
  this.#actorType = options.actorType;
368
447
  this.#actorIdentifier = options.actorIdentifier;
@@ -591,7 +670,7 @@ var AlterVault = class _AlterVault {
591
670
  * This is a private method. Tokens are NEVER exposed to developers.
592
671
  * Use request() instead, which handles tokens internally.
593
672
  */
594
- async #getToken(providerId, attributes, reason, runId, threadId, toolCallId) {
673
+ async #getToken(providerId, attributes, reason, requestMetadata, runId, threadId, toolCallId) {
595
674
  const actorHeaders = this.#getActorRequestHeaders(
596
675
  runId,
597
676
  threadId,
@@ -603,7 +682,8 @@ var AlterVault = class _AlterVault {
603
682
  json: {
604
683
  provider_id: providerId,
605
684
  attributes,
606
- reason: reason ?? null
685
+ reason: reason ?? null,
686
+ request: requestMetadata ?? null
607
687
  },
608
688
  headers: actorHeaders
609
689
  });
@@ -640,9 +720,6 @@ var AlterVault = class _AlterVault {
640
720
  * if audit logging fails.
641
721
  */
642
722
  async #logApiCall(params) {
643
- if (!this.enableAuditLogging) {
644
- return;
645
- }
646
723
  try {
647
724
  const auditLog = new APICallAuditLog({
648
725
  connectionId: params.connectionId,
@@ -727,7 +804,7 @@ var AlterVault = class _AlterVault {
727
804
  * 1. Fetches an OAuth token from Alter backend (never exposed)
728
805
  * 2. Injects the token as a Bearer header
729
806
  * 3. Calls the provider API
730
- * 4. Logs the call for audit (if enabled, fire-and-forget)
807
+ * 4. Logs the call for audit (fire-and-forget)
731
808
  * 5. Returns the raw response
732
809
  */
733
810
  async request(provider, method, url, options) {
@@ -783,6 +860,7 @@ var AlterVault = class _AlterVault {
783
860
  providerStr,
784
861
  options.user,
785
862
  options.reason,
863
+ { method: methodStr, url },
786
864
  options.runId,
787
865
  options.threadId,
788
866
  options.toolCallId
@@ -863,6 +941,112 @@ var AlterVault = class _AlterVault {
863
941
  }
864
942
  return response;
865
943
  }
944
+ /**
945
+ * List OAuth connections for this app.
946
+ *
947
+ * Returns paginated connection metadata (no tokens).
948
+ * Useful for discovering which services a user has connected.
949
+ */
950
+ async listConnections(options) {
951
+ if (this.#closed) {
952
+ throw new AlterSDKError(
953
+ "SDK instance has been closed. Create a new AlterVault instance to make requests."
954
+ );
955
+ }
956
+ const actorHeaders = this.#getActorRequestHeaders();
957
+ let response;
958
+ try {
959
+ response = await this.#alterClient.post("/oauth/connections/list", {
960
+ json: {
961
+ provider_id: options?.providerId ?? null,
962
+ limit: options?.limit ?? 100,
963
+ offset: options?.offset ?? 0
964
+ },
965
+ headers: actorHeaders
966
+ });
967
+ } catch (error) {
968
+ if (_AlterVault.#isTimeoutOrAbortError(error)) {
969
+ throw new TimeoutError(
970
+ `Request to Alter Vault backend timed out: ${error instanceof Error ? error.message : String(error)}`,
971
+ { base_url: this.baseUrl }
972
+ );
973
+ }
974
+ if (error instanceof TypeError) {
975
+ throw new NetworkError(
976
+ `Failed to connect to Alter Vault backend: ${error.message}`,
977
+ { base_url: this.baseUrl }
978
+ );
979
+ }
980
+ throw new AlterSDKError(
981
+ `Failed to list connections: ${error instanceof Error ? error.message : String(error)}`
982
+ );
983
+ }
984
+ this.#cacheActorIdFromResponse(response);
985
+ await this.#handleErrorResponse(response);
986
+ const data = await response.json();
987
+ const connections = data.connections.map(
988
+ (c) => new ConnectionInfo(
989
+ c
990
+ )
991
+ );
992
+ return new ConnectionListResult({
993
+ connections,
994
+ total: data.total,
995
+ limit: data.limit,
996
+ offset: data.offset,
997
+ has_more: data.has_more
998
+ });
999
+ }
1000
+ /**
1001
+ * Create a Connect session for initiating OAuth flows.
1002
+ *
1003
+ * Returns a URL the user can open in their browser to authorize access.
1004
+ */
1005
+ async createConnectSession(options) {
1006
+ if (this.#closed) {
1007
+ throw new AlterSDKError(
1008
+ "SDK instance has been closed. Create a new AlterVault instance to make requests."
1009
+ );
1010
+ }
1011
+ if (!options.endUser?.id) {
1012
+ throw new AlterSDKError("endUser.id is required");
1013
+ }
1014
+ const actorHeaders = this.#getActorRequestHeaders();
1015
+ let response;
1016
+ try {
1017
+ response = await this.#alterClient.post("/oauth/connect/session", {
1018
+ json: {
1019
+ end_user: options.endUser,
1020
+ attributes: options.attributes ?? null,
1021
+ allowed_providers: options.allowedProviders ?? null,
1022
+ return_url: options.returnUrl ?? null,
1023
+ allowed_origin: options.allowedOrigin ?? null,
1024
+ metadata: options.metadata ?? null
1025
+ },
1026
+ headers: actorHeaders
1027
+ });
1028
+ } catch (error) {
1029
+ if (_AlterVault.#isTimeoutOrAbortError(error)) {
1030
+ throw new TimeoutError(
1031
+ `Request to Alter Vault backend timed out: ${error instanceof Error ? error.message : String(error)}`,
1032
+ { base_url: this.baseUrl }
1033
+ );
1034
+ }
1035
+ if (error instanceof TypeError) {
1036
+ throw new NetworkError(
1037
+ `Failed to connect to Alter Vault backend: ${error.message}`,
1038
+ { base_url: this.baseUrl }
1039
+ );
1040
+ }
1041
+ throw new AlterSDKError(
1042
+ `Failed to create connect session: ${error instanceof Error ? error.message : String(error)}`
1043
+ );
1044
+ }
1045
+ this.#cacheActorIdFromResponse(response);
1046
+ await this.#handleErrorResponse(response);
1047
+ const data = await response.json();
1048
+ return new ConnectSession(data);
1049
+ }
866
1050
  /**
867
1051
  * Close HTTP clients and release resources.
868
1052
  * Waits for any pending audit tasks before closing.
@@ -910,6 +1094,9 @@ export {
910
1094
  APICallAuditLog,
911
1095
  AlterSDKError,
912
1096
  AlterVault,
1097
+ ConnectSession,
1098
+ ConnectionInfo,
1099
+ ConnectionListResult,
913
1100
  ConnectionNotFoundError,
914
1101
  HttpMethod,
915
1102
  NetworkError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alter-ai/alter-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Official TypeScript SDK for Alter Vault — OAuth token management with policy enforcement",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",