@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.js
CHANGED
|
@@ -170,7 +170,7 @@ function isDistriEvent(event) {
|
|
|
170
170
|
return "type" in event && "data" in event;
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
//
|
|
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
|
|
912
|
+
baseUrl: config.baseUrl?.replace(/\/$/, "") || DEFAULT_BASE_URL,
|
|
913
913
|
apiVersion: config.apiVersion || "v1",
|
|
914
|
-
timeout: config.timeout
|
|
915
|
-
retryAttempts: config.retryAttempts
|
|
916
|
-
retryDelay: config.retryDelay
|
|
917
|
-
debug: config.debug
|
|
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
|
|
919
|
+
interceptor: config.interceptor ?? (async (init) => Promise.resolve(init)),
|
|
920
|
+
onTokenRefresh: config.onTokenRefresh,
|
|
921
|
+
clientId: config.clientId
|
|
920
922
|
};
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
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(`/
|
|
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(`/
|
|
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(`/
|
|
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(`/
|
|
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(`/
|
|
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
|
|
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
|
-
|
|
1103
|
-
|
|
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
|
|
1513
|
+
applyTokens(accessToken, refreshToken) {
|
|
1525
1514
|
this.accessToken = accessToken;
|
|
1526
|
-
|
|
1527
|
-
|
|
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
|
|
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 });
|
|
@@ -1688,7 +1690,8 @@ var _DistriClient = class _DistriClient {
|
|
|
1688
1690
|
throw lastError;
|
|
1689
1691
|
}
|
|
1690
1692
|
/**
|
|
1691
|
-
* Enhanced fetch with retry logic
|
|
1693
|
+
* Enhanced fetch with retry logic and auth headers.
|
|
1694
|
+
* Exposed publicly for extensions like DistriHomeClient.
|
|
1692
1695
|
*/
|
|
1693
1696
|
async fetch(input, initialInit) {
|
|
1694
1697
|
const url = `${this.config.baseUrl}${input}`;
|
|
@@ -1761,13 +1764,6 @@ var _DistriClient = class _DistriClient {
|
|
|
1761
1764
|
}
|
|
1762
1765
|
};
|
|
1763
1766
|
// ============================================================
|
|
1764
|
-
// Additional User Message Parts API
|
|
1765
|
-
// ============================================================
|
|
1766
|
-
// These methods allow external tools to append parts (text, images)
|
|
1767
|
-
// to the user message in the next agent iteration.
|
|
1768
|
-
// The parts are stored under the key "__additional_user_parts".
|
|
1769
|
-
_DistriClient.ADDITIONAL_PARTS_KEY = "__additional_user_parts";
|
|
1770
|
-
// ============================================================
|
|
1771
1767
|
// Token API
|
|
1772
1768
|
// ============================================================
|
|
1773
1769
|
// Issue access + refresh tokens for temporary authentication (e.g., frontend use)
|
|
@@ -1832,7 +1828,7 @@ var Agent = class _Agent {
|
|
|
1832
1828
|
return this.agentDefinition.description;
|
|
1833
1829
|
}
|
|
1834
1830
|
get agentType() {
|
|
1835
|
-
return this.agentDefinition.agent_type
|
|
1831
|
+
return this.agentDefinition.agent_type;
|
|
1836
1832
|
}
|
|
1837
1833
|
get iconUrl() {
|
|
1838
1834
|
return this.agentDefinition.icon_url;
|
|
@@ -1869,7 +1865,7 @@ var Agent = class _Agent {
|
|
|
1869
1865
|
const enhancedParams = this.enhanceParamsWithTools(params, tools);
|
|
1870
1866
|
const a2aStream = this.client.sendMessageStream(this.agentDefinition.id, enhancedParams);
|
|
1871
1867
|
const self = this;
|
|
1872
|
-
return
|
|
1868
|
+
return async function* () {
|
|
1873
1869
|
for await (const event of a2aStream) {
|
|
1874
1870
|
const converted = decodeA2AStreamEvent(event);
|
|
1875
1871
|
if (converted && converted.type === "inline_hook_requested") {
|
|
@@ -1890,7 +1886,7 @@ var Agent = class _Agent {
|
|
|
1890
1886
|
yield converted;
|
|
1891
1887
|
}
|
|
1892
1888
|
}
|
|
1893
|
-
}
|
|
1889
|
+
}();
|
|
1894
1890
|
}
|
|
1895
1891
|
/**
|
|
1896
1892
|
* Validate that required external tools are registered before invoking.
|
|
@@ -1982,12 +1978,13 @@ var Agent = class _Agent {
|
|
|
1982
1978
|
*/
|
|
1983
1979
|
static async create(agentIdOrDef, client) {
|
|
1984
1980
|
const agentDefinition = typeof agentIdOrDef === "string" ? await client.getAgent(agentIdOrDef) : agentIdOrDef;
|
|
1981
|
+
const tools = agentDefinition?.resolved_tools || [];
|
|
1985
1982
|
console.log("\u{1F916} Agent definition loaded:", {
|
|
1986
1983
|
id: agentDefinition.id,
|
|
1987
1984
|
name: agentDefinition.name,
|
|
1988
|
-
tools:
|
|
1985
|
+
tools: tools.map((t) => ({
|
|
1989
1986
|
name: t.name,
|
|
1990
|
-
type:
|
|
1987
|
+
type: "function"
|
|
1991
1988
|
})) || [],
|
|
1992
1989
|
toolCount: agentDefinition.tools?.length || 0
|
|
1993
1990
|
});
|