@distri/core 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -223,6 +223,14 @@ declare class DistriClient {
223
223
  private refreshPromise?;
224
224
  private agentClients;
225
225
  constructor(config: DistriClientConfig);
226
+ /**
227
+ * Get the configured client ID.
228
+ */
229
+ get clientId(): string | undefined;
230
+ /**
231
+ * Set the client ID for embed token issuance.
232
+ */
233
+ set clientId(value: string | undefined);
226
234
  /**
227
235
  * Create a client with default cloud configuration.
228
236
  *
@@ -296,6 +304,10 @@ declare class DistriClient {
296
304
  accessToken?: string;
297
305
  refreshToken?: string;
298
306
  }): void;
307
+ /**
308
+ * Reset all authentication tokens.
309
+ */
310
+ resetTokens(): void;
299
311
  /**
300
312
  * Start streaming speech-to-text transcription via WebSocket
301
313
  */
@@ -373,6 +385,9 @@ declare class DistriClient {
373
385
  */
374
386
  get baseUrl(): string;
375
387
  private applyTokens;
388
+ /**
389
+ * Ensure access token is valid, refreshing if necessary
390
+ */
376
391
  private ensureAccessToken;
377
392
  private refreshTokens;
378
393
  private performTokenRefresh;
@@ -932,6 +947,11 @@ interface DistriClientConfig {
932
947
  * Request interceptor for modifying requests before sending
933
948
  */
934
949
  interceptor?: (init?: RequestInit) => Promise<RequestInit | undefined>;
950
+ /**
951
+ * Hook to refresh the access token when it expires.
952
+ * Useful for public clients where only an access token is available.
953
+ */
954
+ onTokenRefresh?: () => Promise<string | null>;
935
955
  /**
936
956
  * Access token for bearer auth (optional)
937
957
  */
@@ -945,12 +965,9 @@ interface DistriClientConfig {
945
965
  */
946
966
  tokenRefreshSkewMs?: number;
947
967
  /**
948
- * Callback invoked when tokens are refreshed
968
+ * Client ID from Distri Cloud.
949
969
  */
950
- onTokenRefresh?: (tokens: {
951
- accessToken: string;
952
- refreshToken: string;
953
- }) => void;
970
+ clientId?: string;
954
971
  }
955
972
  interface LLMResponse {
956
973
  finish_reason: string;
package/dist/index.d.ts CHANGED
@@ -223,6 +223,14 @@ declare class DistriClient {
223
223
  private refreshPromise?;
224
224
  private agentClients;
225
225
  constructor(config: DistriClientConfig);
226
+ /**
227
+ * Get the configured client ID.
228
+ */
229
+ get clientId(): string | undefined;
230
+ /**
231
+ * Set the client ID for embed token issuance.
232
+ */
233
+ set clientId(value: string | undefined);
226
234
  /**
227
235
  * Create a client with default cloud configuration.
228
236
  *
@@ -296,6 +304,10 @@ declare class DistriClient {
296
304
  accessToken?: string;
297
305
  refreshToken?: string;
298
306
  }): void;
307
+ /**
308
+ * Reset all authentication tokens.
309
+ */
310
+ resetTokens(): void;
299
311
  /**
300
312
  * Start streaming speech-to-text transcription via WebSocket
301
313
  */
@@ -373,6 +385,9 @@ declare class DistriClient {
373
385
  */
374
386
  get baseUrl(): string;
375
387
  private applyTokens;
388
+ /**
389
+ * Ensure access token is valid, refreshing if necessary
390
+ */
376
391
  private ensureAccessToken;
377
392
  private refreshTokens;
378
393
  private performTokenRefresh;
@@ -932,6 +947,11 @@ interface DistriClientConfig {
932
947
  * Request interceptor for modifying requests before sending
933
948
  */
934
949
  interceptor?: (init?: RequestInit) => Promise<RequestInit | undefined>;
950
+ /**
951
+ * Hook to refresh the access token when it expires.
952
+ * Useful for public clients where only an access token is available.
953
+ */
954
+ onTokenRefresh?: () => Promise<string | null>;
935
955
  /**
936
956
  * Access token for bearer auth (optional)
937
957
  */
@@ -945,12 +965,9 @@ interface DistriClientConfig {
945
965
  */
946
966
  tokenRefreshSkewMs?: number;
947
967
  /**
948
- * Callback invoked when tokens are refreshed
968
+ * Client ID from Distri Cloud.
949
969
  */
950
- onTokenRefresh?: (tokens: {
951
- accessToken: string;
952
- refreshToken: string;
953
- }) => void;
970
+ clientId?: string;
954
971
  }
955
972
  interface LLMResponse {
956
973
  finish_reason: string;
package/dist/index.js CHANGED
@@ -170,7 +170,7 @@ function isDistriEvent(event) {
170
170
  return "type" in event && "data" in event;
171
171
  }
172
172
 
173
- // ../../../node_modules/.pnpm/@a2a-js+sdk@https+++codeload.github.com+v3g42+a2a-js+tar.gz+51444c9/node_modules/@a2a-js/sdk/dist/chunk-CUGIRVQB.js
173
+ // ../../node_modules/.pnpm/@a2a-js+sdk@https+++codeload.github.com+v3g42+a2a-js+tar.gz+51444c9/node_modules/@a2a-js/sdk/dist/chunk-CUGIRVQB.js
174
174
  var A2AClient = class {
175
175
  /**
176
176
  * Constructs an A2AClient instance.
@@ -909,21 +909,29 @@ var _DistriClient = class _DistriClient {
909
909
  this.tokenRefreshSkewMs = config.tokenRefreshSkewMs ?? 6e4;
910
910
  this.onTokenRefresh = config.onTokenRefresh;
911
911
  this.config = {
912
- baseUrl: config.baseUrl.replace(/\/$/, ""),
912
+ baseUrl: config.baseUrl?.replace(/\/$/, "") || DEFAULT_BASE_URL,
913
913
  apiVersion: config.apiVersion || "v1",
914
- timeout: config.timeout || 3e4,
915
- retryAttempts: config.retryAttempts || 3,
916
- retryDelay: config.retryDelay || 1e3,
917
- debug: config.debug || false,
914
+ timeout: config.timeout ?? 3e4,
915
+ retryAttempts: config.retryAttempts ?? 3,
916
+ retryDelay: config.retryDelay ?? 1e3,
917
+ debug: config.debug ?? false,
918
918
  headers,
919
- interceptor: config.interceptor || ((init) => Promise.resolve(init))
919
+ interceptor: config.interceptor ?? (async (init) => Promise.resolve(init)),
920
+ onTokenRefresh: config.onTokenRefresh,
921
+ clientId: config.clientId
920
922
  };
921
- this.debug("DistriClient initialized with config:", {
922
- baseUrl: this.config.baseUrl,
923
- hasAccessToken: !!this.accessToken,
924
- hasRefreshToken: !!this.refreshToken,
925
- timeout: this.config.timeout
926
- });
923
+ }
924
+ /**
925
+ * Get the configured client ID.
926
+ */
927
+ get clientId() {
928
+ return this.config.clientId;
929
+ }
930
+ /**
931
+ * Set the client ID for embed token issuance.
932
+ */
933
+ set clientId(value) {
934
+ this.config.clientId = value;
927
935
  }
928
936
  /**
929
937
  * Create a client with default cloud configuration.
@@ -956,7 +964,7 @@ var _DistriClient = class _DistriClient {
956
964
  if (expiry) {
957
965
  body.expiry = typeof expiry === "string" ? expiry : expiry.toISOString();
958
966
  }
959
- const resp = await this.fetch(`/session/${encodeURIComponent(sessionId)}/values`, {
967
+ const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values`, {
960
968
  method: "POST",
961
969
  headers: {
962
970
  "Content-Type": "application/json",
@@ -973,7 +981,7 @@ var _DistriClient = class _DistriClient {
973
981
  * Session store: get a single value
974
982
  */
975
983
  async getSessionValue(sessionId, key) {
976
- const resp = await this.fetch(`/session/${encodeURIComponent(sessionId)}/values/${encodeURIComponent(key)}`, {
984
+ const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values/${encodeURIComponent(key)}`, {
977
985
  method: "GET",
978
986
  headers: {
979
987
  ...this.config.headers
@@ -990,7 +998,7 @@ var _DistriClient = class _DistriClient {
990
998
  * Session store: get all values in a session
991
999
  */
992
1000
  async getSessionValues(sessionId) {
993
- const resp = await this.fetch(`/session/${encodeURIComponent(sessionId)}/values`, {
1001
+ const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values`, {
994
1002
  method: "GET",
995
1003
  headers: {
996
1004
  ...this.config.headers
@@ -1007,7 +1015,7 @@ var _DistriClient = class _DistriClient {
1007
1015
  * Session store: delete a single key
1008
1016
  */
1009
1017
  async deleteSessionValue(sessionId, key) {
1010
- const resp = await this.fetch(`/session/${encodeURIComponent(sessionId)}/values/${encodeURIComponent(key)}`, {
1018
+ const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values/${encodeURIComponent(key)}`, {
1011
1019
  method: "DELETE",
1012
1020
  headers: {
1013
1021
  ...this.config.headers
@@ -1022,7 +1030,7 @@ var _DistriClient = class _DistriClient {
1022
1030
  * Session store: clear all keys in a session
1023
1031
  */
1024
1032
  async clearSession(sessionId) {
1025
- const resp = await this.fetch(`/session/${encodeURIComponent(sessionId)}`, {
1033
+ const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}`, {
1026
1034
  method: "DELETE",
1027
1035
  headers: {
1028
1036
  ...this.config.headers
@@ -1033,30 +1041,6 @@ var _DistriClient = class _DistriClient {
1033
1041
  throw new ApiError(errorData.error || "Failed to clear session", resp.status);
1034
1042
  }
1035
1043
  }
1036
- /**
1037
- * Set additional user message parts for the next agent iteration.
1038
- * These parts will be appended to the user message in the prompt.
1039
- * @param sessionId - The thread/session ID
1040
- * @param parts - Array of DistriPart objects to append to user message
1041
- */
1042
- async setAdditionalUserParts(sessionId, parts) {
1043
- await this.setSessionValue(sessionId, _DistriClient.ADDITIONAL_PARTS_KEY, parts);
1044
- }
1045
- /**
1046
- * Get the current additional user message parts.
1047
- * @param sessionId - The thread/session ID
1048
- * @returns Array of DistriPart objects or null if not set
1049
- */
1050
- async getAdditionalUserParts(sessionId) {
1051
- return this.getSessionValue(sessionId, _DistriClient.ADDITIONAL_PARTS_KEY);
1052
- }
1053
- /**
1054
- * Clear/delete the additional user message parts.
1055
- * @param sessionId - The thread/session ID
1056
- */
1057
- async clearAdditionalUserParts(sessionId) {
1058
- await this.deleteSessionValue(sessionId, _DistriClient.ADDITIONAL_PARTS_KEY);
1059
- }
1060
1044
  /**
1061
1045
  * Issue an access token + refresh token for temporary authentication.
1062
1046
  * Requires an existing authenticated session (bearer token).
@@ -1086,7 +1070,7 @@ var _DistriClient = class _DistriClient {
1086
1070
  if (!tokens?.access_token || !tokens?.refresh_token || typeof tokens?.expires_at !== "number") {
1087
1071
  throw new ApiError("Invalid token response", response.status);
1088
1072
  }
1089
- this.applyTokens(tokens.access_token, tokens.refresh_token, false);
1073
+ this.applyTokens(tokens.access_token, tokens.refresh_token);
1090
1074
  return tokens;
1091
1075
  }
1092
1076
  /**
@@ -1099,13 +1083,18 @@ var _DistriClient = class _DistriClient {
1099
1083
  * Update the access/refresh tokens in memory.
1100
1084
  */
1101
1085
  setTokens(tokens) {
1102
- if (tokens.accessToken !== void 0) {
1103
- this.accessToken = tokens.accessToken;
1104
- }
1105
- if (tokens.refreshToken !== void 0) {
1086
+ this.accessToken = tokens.accessToken;
1087
+ if (tokens.refreshToken) {
1106
1088
  this.refreshToken = tokens.refreshToken;
1107
1089
  }
1108
1090
  }
1091
+ /**
1092
+ * Reset all authentication tokens.
1093
+ */
1094
+ resetTokens() {
1095
+ this.accessToken = void 0;
1096
+ this.refreshToken = void 0;
1097
+ }
1109
1098
  /**
1110
1099
  * Start streaming speech-to-text transcription via WebSocket
1111
1100
  */
@@ -1521,15 +1510,17 @@ var _DistriClient = class _DistriClient {
1521
1510
  get baseUrl() {
1522
1511
  return this.config.baseUrl;
1523
1512
  }
1524
- applyTokens(accessToken, refreshToken, notify) {
1513
+ applyTokens(accessToken, refreshToken) {
1525
1514
  this.accessToken = accessToken;
1526
- this.refreshToken = refreshToken;
1527
- if (notify && this.onTokenRefresh) {
1528
- this.onTokenRefresh({ accessToken, refreshToken });
1515
+ if (refreshToken) {
1516
+ this.refreshToken = refreshToken;
1529
1517
  }
1530
1518
  }
1519
+ /**
1520
+ * Ensure access token is valid, refreshing if necessary
1521
+ */
1531
1522
  async ensureAccessToken() {
1532
- if (!this.refreshToken) {
1523
+ if (!this.refreshToken && !this.onTokenRefresh) {
1533
1524
  return;
1534
1525
  }
1535
1526
  if (!this.accessToken || this.isTokenExpiring(this.accessToken)) {
@@ -1541,7 +1532,7 @@ var _DistriClient = class _DistriClient {
1541
1532
  }
1542
1533
  }
1543
1534
  async refreshTokens() {
1544
- if (!this.refreshToken) {
1535
+ if (!this.refreshToken && !this.onTokenRefresh) {
1545
1536
  return;
1546
1537
  }
1547
1538
  if (!this.refreshPromise) {
@@ -1552,6 +1543,17 @@ var _DistriClient = class _DistriClient {
1552
1543
  return this.refreshPromise;
1553
1544
  }
1554
1545
  async performTokenRefresh() {
1546
+ if (this.onTokenRefresh) {
1547
+ this.accessToken = void 0;
1548
+ const newToken = await this.onTokenRefresh();
1549
+ if (newToken) {
1550
+ this.applyTokens(newToken);
1551
+ return;
1552
+ }
1553
+ }
1554
+ if (!this.refreshToken) {
1555
+ return;
1556
+ }
1555
1557
  const response = await this.fetchAbsolute(
1556
1558
  `${this.config.baseUrl}/token`,
1557
1559
  {
@@ -1575,7 +1577,7 @@ var _DistriClient = class _DistriClient {
1575
1577
  if (!tokens?.access_token || !tokens?.refresh_token) {
1576
1578
  throw new ApiError("Invalid token response", response.status);
1577
1579
  }
1578
- this.applyTokens(tokens.access_token, tokens.refresh_token, true);
1580
+ this.applyTokens(tokens.access_token, tokens.refresh_token);
1579
1581
  }
1580
1582
  isTokenExpiring(token) {
1581
1583
  const expiresAt = this.getTokenExpiry(token);
@@ -1670,7 +1672,7 @@ var _DistriClient = class _DistriClient {
1670
1672
  headers
1671
1673
  });
1672
1674
  clearTimeout(timeoutId);
1673
- if (!skipAuth && retryOnAuth && response.status === 401 && this.refreshToken) {
1675
+ if (!skipAuth && retryOnAuth && response.status === 401 && (this.refreshToken || this.onTokenRefresh)) {
1674
1676
  const refreshed = await this.refreshTokens().then(() => true).catch(() => false);
1675
1677
  if (refreshed) {
1676
1678
  return this.fetchAbsolute(url, initialInit, { skipAuth, retryOnAuth: false });
@@ -1762,13 +1764,6 @@ var _DistriClient = class _DistriClient {
1762
1764
  }
1763
1765
  };
1764
1766
  // ============================================================
1765
- // Additional User Message Parts API
1766
- // ============================================================
1767
- // These methods allow external tools to append parts (text, images)
1768
- // to the user message in the next agent iteration.
1769
- // The parts are stored under the key "__additional_user_parts".
1770
- _DistriClient.ADDITIONAL_PARTS_KEY = "__additional_user_parts";
1771
- // ============================================================
1772
1767
  // Token API
1773
1768
  // ============================================================
1774
1769
  // Issue access + refresh tokens for temporary authentication (e.g., frontend use)
@@ -1870,7 +1865,7 @@ var Agent = class _Agent {
1870
1865
  const enhancedParams = this.enhanceParamsWithTools(params, tools);
1871
1866
  const a2aStream = this.client.sendMessageStream(this.agentDefinition.id, enhancedParams);
1872
1867
  const self = this;
1873
- return (async function* () {
1868
+ return async function* () {
1874
1869
  for await (const event of a2aStream) {
1875
1870
  const converted = decodeA2AStreamEvent(event);
1876
1871
  if (converted && converted.type === "inline_hook_requested") {
@@ -1891,7 +1886,7 @@ var Agent = class _Agent {
1891
1886
  yield converted;
1892
1887
  }
1893
1888
  }
1894
- })();
1889
+ }();
1895
1890
  }
1896
1891
  /**
1897
1892
  * Validate that required external tools are registered before invoking.