@distri/core 0.2.5 → 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/README.md +60 -1
- package/dist/index.d.mts +45 -36
- package/dist/index.d.ts +45 -36
- package/dist/index.js +65 -68
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -68
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -121,7 +121,7 @@ function isDistriEvent(event) {
|
|
|
121
121
|
return "type" in event && "data" in event;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
//
|
|
124
|
+
// ../../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
|
|
125
125
|
var A2AClient = class {
|
|
126
126
|
/**
|
|
127
127
|
* Constructs an A2AClient instance.
|
|
@@ -860,21 +860,29 @@ var _DistriClient = class _DistriClient {
|
|
|
860
860
|
this.tokenRefreshSkewMs = config.tokenRefreshSkewMs ?? 6e4;
|
|
861
861
|
this.onTokenRefresh = config.onTokenRefresh;
|
|
862
862
|
this.config = {
|
|
863
|
-
baseUrl: config.baseUrl
|
|
863
|
+
baseUrl: config.baseUrl?.replace(/\/$/, "") || DEFAULT_BASE_URL,
|
|
864
864
|
apiVersion: config.apiVersion || "v1",
|
|
865
|
-
timeout: config.timeout
|
|
866
|
-
retryAttempts: config.retryAttempts
|
|
867
|
-
retryDelay: config.retryDelay
|
|
868
|
-
debug: config.debug
|
|
865
|
+
timeout: config.timeout ?? 3e4,
|
|
866
|
+
retryAttempts: config.retryAttempts ?? 3,
|
|
867
|
+
retryDelay: config.retryDelay ?? 1e3,
|
|
868
|
+
debug: config.debug ?? false,
|
|
869
869
|
headers,
|
|
870
|
-
interceptor: config.interceptor
|
|
870
|
+
interceptor: config.interceptor ?? (async (init) => Promise.resolve(init)),
|
|
871
|
+
onTokenRefresh: config.onTokenRefresh,
|
|
872
|
+
clientId: config.clientId
|
|
871
873
|
};
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Get the configured client ID.
|
|
877
|
+
*/
|
|
878
|
+
get clientId() {
|
|
879
|
+
return this.config.clientId;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Set the client ID for embed token issuance.
|
|
883
|
+
*/
|
|
884
|
+
set clientId(value) {
|
|
885
|
+
this.config.clientId = value;
|
|
878
886
|
}
|
|
879
887
|
/**
|
|
880
888
|
* Create a client with default cloud configuration.
|
|
@@ -907,7 +915,7 @@ var _DistriClient = class _DistriClient {
|
|
|
907
915
|
if (expiry) {
|
|
908
916
|
body.expiry = typeof expiry === "string" ? expiry : expiry.toISOString();
|
|
909
917
|
}
|
|
910
|
-
const resp = await this.fetch(`/
|
|
918
|
+
const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values`, {
|
|
911
919
|
method: "POST",
|
|
912
920
|
headers: {
|
|
913
921
|
"Content-Type": "application/json",
|
|
@@ -924,7 +932,7 @@ var _DistriClient = class _DistriClient {
|
|
|
924
932
|
* Session store: get a single value
|
|
925
933
|
*/
|
|
926
934
|
async getSessionValue(sessionId, key) {
|
|
927
|
-
const resp = await this.fetch(`/
|
|
935
|
+
const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values/${encodeURIComponent(key)}`, {
|
|
928
936
|
method: "GET",
|
|
929
937
|
headers: {
|
|
930
938
|
...this.config.headers
|
|
@@ -941,7 +949,7 @@ var _DistriClient = class _DistriClient {
|
|
|
941
949
|
* Session store: get all values in a session
|
|
942
950
|
*/
|
|
943
951
|
async getSessionValues(sessionId) {
|
|
944
|
-
const resp = await this.fetch(`/
|
|
952
|
+
const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values`, {
|
|
945
953
|
method: "GET",
|
|
946
954
|
headers: {
|
|
947
955
|
...this.config.headers
|
|
@@ -958,7 +966,7 @@ var _DistriClient = class _DistriClient {
|
|
|
958
966
|
* Session store: delete a single key
|
|
959
967
|
*/
|
|
960
968
|
async deleteSessionValue(sessionId, key) {
|
|
961
|
-
const resp = await this.fetch(`/
|
|
969
|
+
const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}/values/${encodeURIComponent(key)}`, {
|
|
962
970
|
method: "DELETE",
|
|
963
971
|
headers: {
|
|
964
972
|
...this.config.headers
|
|
@@ -973,7 +981,7 @@ var _DistriClient = class _DistriClient {
|
|
|
973
981
|
* Session store: clear all keys in a session
|
|
974
982
|
*/
|
|
975
983
|
async clearSession(sessionId) {
|
|
976
|
-
const resp = await this.fetch(`/
|
|
984
|
+
const resp = await this.fetch(`/sessions/${encodeURIComponent(sessionId)}`, {
|
|
977
985
|
method: "DELETE",
|
|
978
986
|
headers: {
|
|
979
987
|
...this.config.headers
|
|
@@ -984,30 +992,6 @@ var _DistriClient = class _DistriClient {
|
|
|
984
992
|
throw new ApiError(errorData.error || "Failed to clear session", resp.status);
|
|
985
993
|
}
|
|
986
994
|
}
|
|
987
|
-
/**
|
|
988
|
-
* Set additional user message parts for the next agent iteration.
|
|
989
|
-
* These parts will be appended to the user message in the prompt.
|
|
990
|
-
* @param sessionId - The thread/session ID
|
|
991
|
-
* @param parts - Array of DistriPart objects to append to user message
|
|
992
|
-
*/
|
|
993
|
-
async setAdditionalUserParts(sessionId, parts) {
|
|
994
|
-
await this.setSessionValue(sessionId, _DistriClient.ADDITIONAL_PARTS_KEY, parts);
|
|
995
|
-
}
|
|
996
|
-
/**
|
|
997
|
-
* Get the current additional user message parts.
|
|
998
|
-
* @param sessionId - The thread/session ID
|
|
999
|
-
* @returns Array of DistriPart objects or null if not set
|
|
1000
|
-
*/
|
|
1001
|
-
async getAdditionalUserParts(sessionId) {
|
|
1002
|
-
return this.getSessionValue(sessionId, _DistriClient.ADDITIONAL_PARTS_KEY);
|
|
1003
|
-
}
|
|
1004
|
-
/**
|
|
1005
|
-
* Clear/delete the additional user message parts.
|
|
1006
|
-
* @param sessionId - The thread/session ID
|
|
1007
|
-
*/
|
|
1008
|
-
async clearAdditionalUserParts(sessionId) {
|
|
1009
|
-
await this.deleteSessionValue(sessionId, _DistriClient.ADDITIONAL_PARTS_KEY);
|
|
1010
|
-
}
|
|
1011
995
|
/**
|
|
1012
996
|
* Issue an access token + refresh token for temporary authentication.
|
|
1013
997
|
* Requires an existing authenticated session (bearer token).
|
|
@@ -1037,7 +1021,7 @@ var _DistriClient = class _DistriClient {
|
|
|
1037
1021
|
if (!tokens?.access_token || !tokens?.refresh_token || typeof tokens?.expires_at !== "number") {
|
|
1038
1022
|
throw new ApiError("Invalid token response", response.status);
|
|
1039
1023
|
}
|
|
1040
|
-
this.applyTokens(tokens.access_token, tokens.refresh_token
|
|
1024
|
+
this.applyTokens(tokens.access_token, tokens.refresh_token);
|
|
1041
1025
|
return tokens;
|
|
1042
1026
|
}
|
|
1043
1027
|
/**
|
|
@@ -1050,13 +1034,18 @@ var _DistriClient = class _DistriClient {
|
|
|
1050
1034
|
* Update the access/refresh tokens in memory.
|
|
1051
1035
|
*/
|
|
1052
1036
|
setTokens(tokens) {
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
}
|
|
1056
|
-
if (tokens.refreshToken !== void 0) {
|
|
1037
|
+
this.accessToken = tokens.accessToken;
|
|
1038
|
+
if (tokens.refreshToken) {
|
|
1057
1039
|
this.refreshToken = tokens.refreshToken;
|
|
1058
1040
|
}
|
|
1059
1041
|
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Reset all authentication tokens.
|
|
1044
|
+
*/
|
|
1045
|
+
resetTokens() {
|
|
1046
|
+
this.accessToken = void 0;
|
|
1047
|
+
this.refreshToken = void 0;
|
|
1048
|
+
}
|
|
1060
1049
|
/**
|
|
1061
1050
|
* Start streaming speech-to-text transcription via WebSocket
|
|
1062
1051
|
*/
|
|
@@ -1472,15 +1461,17 @@ var _DistriClient = class _DistriClient {
|
|
|
1472
1461
|
get baseUrl() {
|
|
1473
1462
|
return this.config.baseUrl;
|
|
1474
1463
|
}
|
|
1475
|
-
applyTokens(accessToken, refreshToken
|
|
1464
|
+
applyTokens(accessToken, refreshToken) {
|
|
1476
1465
|
this.accessToken = accessToken;
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
this.onTokenRefresh({ accessToken, refreshToken });
|
|
1466
|
+
if (refreshToken) {
|
|
1467
|
+
this.refreshToken = refreshToken;
|
|
1480
1468
|
}
|
|
1481
1469
|
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Ensure access token is valid, refreshing if necessary
|
|
1472
|
+
*/
|
|
1482
1473
|
async ensureAccessToken() {
|
|
1483
|
-
if (!this.refreshToken) {
|
|
1474
|
+
if (!this.refreshToken && !this.onTokenRefresh) {
|
|
1484
1475
|
return;
|
|
1485
1476
|
}
|
|
1486
1477
|
if (!this.accessToken || this.isTokenExpiring(this.accessToken)) {
|
|
@@ -1492,7 +1483,7 @@ var _DistriClient = class _DistriClient {
|
|
|
1492
1483
|
}
|
|
1493
1484
|
}
|
|
1494
1485
|
async refreshTokens() {
|
|
1495
|
-
if (!this.refreshToken) {
|
|
1486
|
+
if (!this.refreshToken && !this.onTokenRefresh) {
|
|
1496
1487
|
return;
|
|
1497
1488
|
}
|
|
1498
1489
|
if (!this.refreshPromise) {
|
|
@@ -1503,6 +1494,17 @@ var _DistriClient = class _DistriClient {
|
|
|
1503
1494
|
return this.refreshPromise;
|
|
1504
1495
|
}
|
|
1505
1496
|
async performTokenRefresh() {
|
|
1497
|
+
if (this.onTokenRefresh) {
|
|
1498
|
+
this.accessToken = void 0;
|
|
1499
|
+
const newToken = await this.onTokenRefresh();
|
|
1500
|
+
if (newToken) {
|
|
1501
|
+
this.applyTokens(newToken);
|
|
1502
|
+
return;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
if (!this.refreshToken) {
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1506
1508
|
const response = await this.fetchAbsolute(
|
|
1507
1509
|
`${this.config.baseUrl}/token`,
|
|
1508
1510
|
{
|
|
@@ -1526,7 +1528,7 @@ var _DistriClient = class _DistriClient {
|
|
|
1526
1528
|
if (!tokens?.access_token || !tokens?.refresh_token) {
|
|
1527
1529
|
throw new ApiError("Invalid token response", response.status);
|
|
1528
1530
|
}
|
|
1529
|
-
this.applyTokens(tokens.access_token, tokens.refresh_token
|
|
1531
|
+
this.applyTokens(tokens.access_token, tokens.refresh_token);
|
|
1530
1532
|
}
|
|
1531
1533
|
isTokenExpiring(token) {
|
|
1532
1534
|
const expiresAt = this.getTokenExpiry(token);
|
|
@@ -1621,7 +1623,7 @@ var _DistriClient = class _DistriClient {
|
|
|
1621
1623
|
headers
|
|
1622
1624
|
});
|
|
1623
1625
|
clearTimeout(timeoutId);
|
|
1624
|
-
if (!skipAuth && retryOnAuth && response.status === 401 && this.refreshToken) {
|
|
1626
|
+
if (!skipAuth && retryOnAuth && response.status === 401 && (this.refreshToken || this.onTokenRefresh)) {
|
|
1625
1627
|
const refreshed = await this.refreshTokens().then(() => true).catch(() => false);
|
|
1626
1628
|
if (refreshed) {
|
|
1627
1629
|
return this.fetchAbsolute(url, initialInit, { skipAuth, retryOnAuth: false });
|
|
@@ -1639,7 +1641,8 @@ var _DistriClient = class _DistriClient {
|
|
|
1639
1641
|
throw lastError;
|
|
1640
1642
|
}
|
|
1641
1643
|
/**
|
|
1642
|
-
* Enhanced fetch with retry logic
|
|
1644
|
+
* Enhanced fetch with retry logic and auth headers.
|
|
1645
|
+
* Exposed publicly for extensions like DistriHomeClient.
|
|
1643
1646
|
*/
|
|
1644
1647
|
async fetch(input, initialInit) {
|
|
1645
1648
|
const url = `${this.config.baseUrl}${input}`;
|
|
@@ -1712,13 +1715,6 @@ var _DistriClient = class _DistriClient {
|
|
|
1712
1715
|
}
|
|
1713
1716
|
};
|
|
1714
1717
|
// ============================================================
|
|
1715
|
-
// Additional User Message Parts API
|
|
1716
|
-
// ============================================================
|
|
1717
|
-
// These methods allow external tools to append parts (text, images)
|
|
1718
|
-
// to the user message in the next agent iteration.
|
|
1719
|
-
// The parts are stored under the key "__additional_user_parts".
|
|
1720
|
-
_DistriClient.ADDITIONAL_PARTS_KEY = "__additional_user_parts";
|
|
1721
|
-
// ============================================================
|
|
1722
1718
|
// Token API
|
|
1723
1719
|
// ============================================================
|
|
1724
1720
|
// Issue access + refresh tokens for temporary authentication (e.g., frontend use)
|
|
@@ -1783,7 +1779,7 @@ var Agent = class _Agent {
|
|
|
1783
1779
|
return this.agentDefinition.description;
|
|
1784
1780
|
}
|
|
1785
1781
|
get agentType() {
|
|
1786
|
-
return this.agentDefinition.agent_type
|
|
1782
|
+
return this.agentDefinition.agent_type;
|
|
1787
1783
|
}
|
|
1788
1784
|
get iconUrl() {
|
|
1789
1785
|
return this.agentDefinition.icon_url;
|
|
@@ -1820,7 +1816,7 @@ var Agent = class _Agent {
|
|
|
1820
1816
|
const enhancedParams = this.enhanceParamsWithTools(params, tools);
|
|
1821
1817
|
const a2aStream = this.client.sendMessageStream(this.agentDefinition.id, enhancedParams);
|
|
1822
1818
|
const self = this;
|
|
1823
|
-
return
|
|
1819
|
+
return async function* () {
|
|
1824
1820
|
for await (const event of a2aStream) {
|
|
1825
1821
|
const converted = decodeA2AStreamEvent(event);
|
|
1826
1822
|
if (converted && converted.type === "inline_hook_requested") {
|
|
@@ -1841,7 +1837,7 @@ var Agent = class _Agent {
|
|
|
1841
1837
|
yield converted;
|
|
1842
1838
|
}
|
|
1843
1839
|
}
|
|
1844
|
-
}
|
|
1840
|
+
}();
|
|
1845
1841
|
}
|
|
1846
1842
|
/**
|
|
1847
1843
|
* Validate that required external tools are registered before invoking.
|
|
@@ -1933,12 +1929,13 @@ var Agent = class _Agent {
|
|
|
1933
1929
|
*/
|
|
1934
1930
|
static async create(agentIdOrDef, client) {
|
|
1935
1931
|
const agentDefinition = typeof agentIdOrDef === "string" ? await client.getAgent(agentIdOrDef) : agentIdOrDef;
|
|
1932
|
+
const tools = agentDefinition?.resolved_tools || [];
|
|
1936
1933
|
console.log("\u{1F916} Agent definition loaded:", {
|
|
1937
1934
|
id: agentDefinition.id,
|
|
1938
1935
|
name: agentDefinition.name,
|
|
1939
|
-
tools:
|
|
1936
|
+
tools: tools.map((t) => ({
|
|
1940
1937
|
name: t.name,
|
|
1941
|
-
type:
|
|
1938
|
+
type: "function"
|
|
1942
1939
|
})) || [],
|
|
1943
1940
|
toolCount: agentDefinition.tools?.length || 0
|
|
1944
1941
|
});
|