@usabledev/usable-chat 1.236.2 → 1.237.0
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/cli.js +144 -35
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -26790,6 +26790,32 @@ var init_dist7 = __esm({
|
|
|
26790
26790
|
// src/lib/logger.ts
|
|
26791
26791
|
import { appendFileSync, mkdirSync as mkdirSync2 } from "fs";
|
|
26792
26792
|
import { join as join3 } from "path";
|
|
26793
|
+
function serializeLogEntry(entry) {
|
|
26794
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
26795
|
+
return JSON.stringify(entry, (_key, value) => {
|
|
26796
|
+
if (value instanceof Error) {
|
|
26797
|
+
const serialized = {
|
|
26798
|
+
name: value.name,
|
|
26799
|
+
message: value.message,
|
|
26800
|
+
...value.stack ? { stack: value.stack } : {}
|
|
26801
|
+
};
|
|
26802
|
+
if ("cause" in value && value.cause !== void 0) {
|
|
26803
|
+
serialized.cause = value.cause;
|
|
26804
|
+
}
|
|
26805
|
+
for (const [prop, propValue] of Object.entries(value)) {
|
|
26806
|
+
if (!(prop in serialized)) {
|
|
26807
|
+
serialized[prop] = propValue;
|
|
26808
|
+
}
|
|
26809
|
+
}
|
|
26810
|
+
return serialized;
|
|
26811
|
+
}
|
|
26812
|
+
if (typeof value === "object" && value !== null) {
|
|
26813
|
+
if (seen.has(value)) return "[Circular]";
|
|
26814
|
+
seen.add(value);
|
|
26815
|
+
}
|
|
26816
|
+
return value;
|
|
26817
|
+
});
|
|
26818
|
+
}
|
|
26793
26819
|
var LOG_LEVEL_PRIORITY, Logger, logger, orchestrationLogger, streamLogger;
|
|
26794
26820
|
var init_logger = __esm({
|
|
26795
26821
|
"src/lib/logger.ts"() {
|
|
@@ -26871,7 +26897,7 @@ var init_logger = __esm({
|
|
|
26871
26897
|
break;
|
|
26872
26898
|
}
|
|
26873
26899
|
} else {
|
|
26874
|
-
const logLine =
|
|
26900
|
+
const logLine = serializeLogEntry(entry);
|
|
26875
26901
|
switch (entry.level) {
|
|
26876
26902
|
case "debug":
|
|
26877
26903
|
case "info":
|
|
@@ -26893,7 +26919,7 @@ var init_logger = __esm({
|
|
|
26893
26919
|
try {
|
|
26894
26920
|
const logFileName = this.getLogFileName(entry.category);
|
|
26895
26921
|
const logPath = join3(this.logDir, logFileName);
|
|
26896
|
-
const logLine =
|
|
26922
|
+
const logLine = serializeLogEntry(entry) + "\n";
|
|
26897
26923
|
appendFileSync(logPath, logLine, "utf-8");
|
|
26898
26924
|
} catch (error42) {
|
|
26899
26925
|
console.error("Failed to write to log file:", error42);
|
|
@@ -53353,7 +53379,7 @@ var init_model_registry = __esm({
|
|
|
53353
53379
|
var version2;
|
|
53354
53380
|
var init_package = __esm({
|
|
53355
53381
|
"package.json"() {
|
|
53356
|
-
version2 = "1.
|
|
53382
|
+
version2 = "1.237.0";
|
|
53357
53383
|
}
|
|
53358
53384
|
});
|
|
53359
53385
|
|
|
@@ -252758,8 +252784,9 @@ async function coordinateAccessTokenRefresh(token, refresh2) {
|
|
|
252758
252784
|
);
|
|
252759
252785
|
return null;
|
|
252760
252786
|
}
|
|
252787
|
+
const { error: _previousError, ...restToken } = token;
|
|
252761
252788
|
return {
|
|
252762
|
-
...
|
|
252789
|
+
...restToken,
|
|
252763
252790
|
accessToken: result2.accessToken,
|
|
252764
252791
|
refreshToken: result2.refreshToken,
|
|
252765
252792
|
accessTokenExpires: result2.accessTokenExpires,
|
|
@@ -252788,34 +252815,61 @@ async function coordinateAccessTokenRefresh(token, refresh2) {
|
|
|
252788
252815
|
throw new Error("refresh_rotation_chain_limit");
|
|
252789
252816
|
}
|
|
252790
252817
|
const owner = randomUUID5();
|
|
252791
|
-
if (!await redis2.set(key3, owner, "EX",
|
|
252818
|
+
if (!await redis2.set(key3, owner, "EX", IN_FLIGHT_LOCK_TTL, "NX")) {
|
|
252792
252819
|
for (let attempt = 0; attempt < 20; attempt++) {
|
|
252793
252820
|
await new Promise((resolve9) => setTimeout(resolve9, 250));
|
|
252794
|
-
const
|
|
252795
|
-
if (
|
|
252821
|
+
const completed2 = await read2();
|
|
252822
|
+
if (completed2) return completed2;
|
|
252823
|
+
if (!await redis2.get(key3)) break;
|
|
252796
252824
|
}
|
|
252825
|
+
const completed = await read2();
|
|
252826
|
+
if (completed) return completed;
|
|
252797
252827
|
throw new Error("refresh_outcome_unconfirmed");
|
|
252798
252828
|
}
|
|
252799
|
-
const
|
|
252800
|
-
|
|
252801
|
-
|
|
252802
|
-
|
|
252803
|
-
|
|
252804
|
-
|
|
252805
|
-
|
|
252806
|
-
|
|
252807
|
-
|
|
252808
|
-
|
|
252809
|
-
|
|
252810
|
-
|
|
252829
|
+
const releaseLock = async () => {
|
|
252830
|
+
await redis2.eval(
|
|
252831
|
+
"if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end",
|
|
252832
|
+
1,
|
|
252833
|
+
key3,
|
|
252834
|
+
owner
|
|
252835
|
+
).catch(() => {
|
|
252836
|
+
});
|
|
252837
|
+
};
|
|
252838
|
+
let refreshed;
|
|
252839
|
+
try {
|
|
252840
|
+
refreshed = await refresh2();
|
|
252841
|
+
} catch (error42) {
|
|
252842
|
+
await releaseLock();
|
|
252843
|
+
throw error42;
|
|
252844
|
+
}
|
|
252845
|
+
if (refreshed.error) {
|
|
252846
|
+
await releaseLock();
|
|
252847
|
+
return refreshed;
|
|
252848
|
+
}
|
|
252849
|
+
let result;
|
|
252850
|
+
try {
|
|
252851
|
+
result = await (0, import_jwt.encode)({
|
|
252852
|
+
secret,
|
|
252853
|
+
maxAge: RESULT_TTL,
|
|
252854
|
+
token: {
|
|
252855
|
+
accessToken: refreshed.accessToken,
|
|
252856
|
+
refreshToken: refreshed.refreshToken,
|
|
252857
|
+
accessTokenExpires: refreshed.accessTokenExpires,
|
|
252858
|
+
idToken: refreshed.idToken
|
|
252859
|
+
}
|
|
252860
|
+
});
|
|
252861
|
+
} catch (error42) {
|
|
252862
|
+
await releaseLock();
|
|
252863
|
+
throw error42;
|
|
252864
|
+
}
|
|
252811
252865
|
const committed = await redis2.eval(
|
|
252812
|
-
"if redis.call('GET', KEYS[1]) ~= ARGV[1] then return 0 end redis.call('SET', KEYS[2], ARGV[2], 'EX', ARGV[3]) return 1",
|
|
252866
|
+
"if redis.call('GET', KEYS[1]) ~= ARGV[1] then return 0 end redis.call('SET', KEYS[2], ARGV[2], 'EX', ARGV[3]) redis.call('EXPIRE', KEYS[1], ARGV[3]) return 1",
|
|
252813
252867
|
2,
|
|
252814
252868
|
key3,
|
|
252815
252869
|
`${key3}:result`,
|
|
252816
252870
|
owner,
|
|
252817
252871
|
result,
|
|
252818
|
-
|
|
252872
|
+
RESULT_TTL
|
|
252819
252873
|
);
|
|
252820
252874
|
if (committed !== 1) throw new Error("refresh_rotation_conflict");
|
|
252821
252875
|
return refreshed;
|
|
@@ -252830,7 +252884,7 @@ async function compressAccessTokenRotation(original2, refreshed) {
|
|
|
252830
252884
|
if (Number(prior?.accessTokenExpires ?? 0) > Number(refreshed.accessTokenExpires ?? 0)) return;
|
|
252831
252885
|
const sealed = await (0, import_jwt.encode)({
|
|
252832
252886
|
secret,
|
|
252833
|
-
maxAge:
|
|
252887
|
+
maxAge: RESULT_TTL,
|
|
252834
252888
|
token: {
|
|
252835
252889
|
accessToken: refreshed.accessToken,
|
|
252836
252890
|
refreshToken: refreshed.refreshToken,
|
|
@@ -252844,16 +252898,17 @@ async function compressAccessTokenRotation(original2, refreshed) {
|
|
|
252844
252898
|
key3,
|
|
252845
252899
|
previous,
|
|
252846
252900
|
sealed,
|
|
252847
|
-
|
|
252901
|
+
RESULT_TTL
|
|
252848
252902
|
);
|
|
252849
252903
|
}
|
|
252850
|
-
var import_jwt,
|
|
252904
|
+
var import_jwt, RESULT_TTL, IN_FLIGHT_LOCK_TTL;
|
|
252851
252905
|
var init_auth_refresh_coordination = __esm({
|
|
252852
252906
|
"src/lib/auth-refresh-coordination.ts"() {
|
|
252853
252907
|
"use strict";
|
|
252854
252908
|
import_jwt = __toESM(require_jwt());
|
|
252855
252909
|
init_redis();
|
|
252856
|
-
|
|
252910
|
+
RESULT_TTL = 30 * 24 * 60 * 60;
|
|
252911
|
+
IN_FLIGHT_LOCK_TTL = 45;
|
|
252857
252912
|
}
|
|
252858
252913
|
});
|
|
252859
252914
|
|
|
@@ -252971,29 +253026,61 @@ async function refreshAccessToken(token, rotationDepth = 0) {
|
|
|
252971
253026
|
client_secret: config2.auth.clientSecret,
|
|
252972
253027
|
grant_type: "refresh_token",
|
|
252973
253028
|
refresh_token: token.refreshToken,
|
|
252974
|
-
scope: USABLE_AUTH_SCOPE,
|
|
252975
253029
|
// Request same audience for consistency
|
|
252976
253030
|
audience: config2.auth.clientId
|
|
252977
253031
|
})
|
|
252978
253032
|
});
|
|
252979
|
-
const
|
|
253033
|
+
const rawBody = await response.text();
|
|
253034
|
+
let refreshedTokens = {};
|
|
253035
|
+
if (rawBody) {
|
|
253036
|
+
try {
|
|
253037
|
+
const parsed = JSON.parse(rawBody);
|
|
253038
|
+
if (parsed && typeof parsed === "object") {
|
|
253039
|
+
refreshedTokens = parsed;
|
|
253040
|
+
}
|
|
253041
|
+
} catch {
|
|
253042
|
+
}
|
|
253043
|
+
}
|
|
252980
253044
|
if (!response.ok) {
|
|
252981
|
-
|
|
253045
|
+
const oauthError = typeof refreshedTokens.error === "string" ? refreshedTokens.error : void 0;
|
|
253046
|
+
const errorDescription = typeof refreshedTokens.error_description === "string" ? refreshedTokens.error_description : void 0;
|
|
253047
|
+
const detail = oauthError || response.statusText || "unknown_error";
|
|
253048
|
+
throw new TokenRefreshError(
|
|
253049
|
+
`Token refresh failed (${response.status}): ${detail}${errorDescription ? ` - ${errorDescription}` : ""}`,
|
|
253050
|
+
{
|
|
253051
|
+
status: response.status,
|
|
253052
|
+
oauthError,
|
|
253053
|
+
errorDescription,
|
|
253054
|
+
bodyPreview: oauthError ? void 0 : rawBody.slice(0, 200) || void 0
|
|
253055
|
+
}
|
|
253056
|
+
);
|
|
253057
|
+
}
|
|
253058
|
+
if (typeof refreshedTokens.access_token !== "string" || !refreshedTokens.access_token) {
|
|
253059
|
+
throw new TokenRefreshError("Token refresh response missing access_token", {
|
|
253060
|
+
status: response.status
|
|
253061
|
+
});
|
|
252982
253062
|
}
|
|
252983
253063
|
const missingScopes = missingRequiredAuthorizationScopes(refreshedTokens.access_token);
|
|
252984
253064
|
if (missingScopes.length > 0) {
|
|
252985
|
-
throw new
|
|
253065
|
+
throw new TokenRefreshError(
|
|
253066
|
+
`Refreshed token is missing required scopes: ${missingScopes.join(", ")}`,
|
|
253067
|
+
{ status: response.status, missingScopes }
|
|
253068
|
+
);
|
|
252986
253069
|
}
|
|
252987
253070
|
logger.info("Auth", "Token refreshed successfully");
|
|
253071
|
+
const expiresInSeconds = typeof refreshedTokens.expires_in === "number" ? refreshedTokens.expires_in : 3600;
|
|
253072
|
+
const nextRefreshToken = typeof refreshedTokens.refresh_token === "string" ? refreshedTokens.refresh_token : token.refreshToken;
|
|
253073
|
+
const nextIdToken = typeof refreshedTokens.id_token === "string" ? refreshedTokens.id_token : token.idToken;
|
|
253074
|
+
const { error: _previousError, ...restToken } = token;
|
|
252988
253075
|
return {
|
|
252989
|
-
...
|
|
253076
|
+
...restToken,
|
|
252990
253077
|
accessToken: refreshedTokens.access_token,
|
|
252991
|
-
accessTokenExpires: Date.now() +
|
|
252992
|
-
refreshToken:
|
|
253078
|
+
accessTokenExpires: Date.now() + expiresInSeconds * 1e3,
|
|
253079
|
+
refreshToken: nextRefreshToken,
|
|
252993
253080
|
// Use new refresh token if provided
|
|
252994
253081
|
// Preserve existing ID token (refresh typically doesn't return a new one)
|
|
252995
253082
|
// If refresh does return a new ID token, update it
|
|
252996
|
-
idToken:
|
|
253083
|
+
idToken: nextIdToken
|
|
252997
253084
|
};
|
|
252998
253085
|
});
|
|
252999
253086
|
if (typeof refreshed.accessTokenExpires === "number" && refreshed.accessTokenExpires <= Date.now() + 6e4 && refreshed.refreshToken && refreshed.refreshToken !== token.refreshToken) {
|
|
@@ -253003,14 +253090,24 @@ async function refreshAccessToken(token, rotationDepth = 0) {
|
|
|
253003
253090
|
}
|
|
253004
253091
|
return refreshed;
|
|
253005
253092
|
} catch (error42) {
|
|
253006
|
-
logger.error("Auth", "Error refreshing access token", {
|
|
253093
|
+
logger.error("Auth", "Error refreshing access token", {
|
|
253094
|
+
error: error42,
|
|
253095
|
+
rotationDepth,
|
|
253096
|
+
...error42 instanceof TokenRefreshError ? {
|
|
253097
|
+
status: error42.status,
|
|
253098
|
+
oauthError: error42.oauthError,
|
|
253099
|
+
errorDescription: error42.errorDescription,
|
|
253100
|
+
missingScopes: error42.missingScopes,
|
|
253101
|
+
bodyPreview: error42.bodyPreview
|
|
253102
|
+
} : {}
|
|
253103
|
+
});
|
|
253007
253104
|
return {
|
|
253008
253105
|
...token,
|
|
253009
253106
|
error: "RefreshAccessTokenError"
|
|
253010
253107
|
};
|
|
253011
253108
|
}
|
|
253012
253109
|
}
|
|
253013
|
-
var import_keycloak, USABLE_REQUIRED_AUTH_SCOPES, USABLE_OPTIONAL_AUTH_SCOPES, USABLE_AUTH_SCOPE, AUTH_SESSION_COOKIE_VERSION, authOptions;
|
|
253110
|
+
var import_keycloak, USABLE_REQUIRED_AUTH_SCOPES, USABLE_OPTIONAL_AUTH_SCOPES, USABLE_AUTH_SCOPE, AUTH_SESSION_COOKIE_VERSION, TokenRefreshError, authOptions;
|
|
253014
253111
|
var init_auth6 = __esm({
|
|
253015
253112
|
"src/lib/auth.ts"() {
|
|
253016
253113
|
"use strict";
|
|
@@ -253030,6 +253127,17 @@ var init_auth6 = __esm({
|
|
|
253030
253127
|
...USABLE_OPTIONAL_AUTH_SCOPES
|
|
253031
253128
|
].join(" ");
|
|
253032
253129
|
AUTH_SESSION_COOKIE_VERSION = "v3";
|
|
253130
|
+
TokenRefreshError = class extends Error {
|
|
253131
|
+
constructor(message2, details = {}) {
|
|
253132
|
+
super(message2);
|
|
253133
|
+
this.name = "TokenRefreshError";
|
|
253134
|
+
this.status = details.status;
|
|
253135
|
+
this.oauthError = details.oauthError;
|
|
253136
|
+
this.errorDescription = details.errorDescription;
|
|
253137
|
+
this.missingScopes = details.missingScopes;
|
|
253138
|
+
this.bodyPreview = details.bodyPreview;
|
|
253139
|
+
}
|
|
253140
|
+
};
|
|
253033
253141
|
authOptions = {
|
|
253034
253142
|
providers: [
|
|
253035
253143
|
(0, import_keycloak.default)({
|
|
@@ -253133,6 +253241,7 @@ var init_auth6 = __esm({
|
|
|
253133
253241
|
const metadataExpiresAt = account.expires_at ? account.expires_at * 1e3 : Date.now() + 3600 * 1e3;
|
|
253134
253242
|
const expiresAt = resolveAccessTokenExpiresAt(account.access_token, metadataExpiresAt);
|
|
253135
253243
|
token.accessTokenExpires = expiresAt;
|
|
253244
|
+
token.error = void 0;
|
|
253136
253245
|
const missingScopes = missingRequiredAuthorizationScopes(account.access_token);
|
|
253137
253246
|
if (missingScopes.length > 0) {
|
|
253138
253247
|
logger.error("Auth", "New access token is missing required Usable scopes", {
|