fix: random logouts on letta code (#272)
Co-authored-by: Shubham Naik <shub@memgpt.ai>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { hostname } from "node:os";
|
||||
import Letta from "@letta-ai/letta-client";
|
||||
import packageJson from "../../package.json";
|
||||
import { LETTA_CLOUD_API_URL, refreshAccessToken } from "../auth/oauth";
|
||||
@@ -20,7 +21,19 @@ export async function getClient() {
|
||||
// Refresh if token expires within 5 minutes
|
||||
if (expiresAt - now < 5 * 60 * 1000) {
|
||||
try {
|
||||
const tokens = await refreshAccessToken(settings.refreshToken);
|
||||
// Get or generate device ID (should always exist, but fallback just in case)
|
||||
let deviceId = settings.deviceId;
|
||||
if (!deviceId) {
|
||||
deviceId = crypto.randomUUID();
|
||||
settingsManager.updateSettings({ deviceId });
|
||||
}
|
||||
const deviceName = hostname();
|
||||
|
||||
const tokens = await refreshAccessToken(
|
||||
settings.refreshToken,
|
||||
deviceId,
|
||||
deviceName,
|
||||
);
|
||||
|
||||
// Update settings with new token
|
||||
const updatedEnv = { ...settings.env };
|
||||
|
||||
@@ -68,7 +68,7 @@ export async function pollForToken(
|
||||
deviceCode: string,
|
||||
interval: number = 5,
|
||||
expiresIn: number = 900,
|
||||
deviceId?: string,
|
||||
deviceId: string,
|
||||
deviceName?: string,
|
||||
): Promise<TokenResponse> {
|
||||
const startTime = Date.now();
|
||||
@@ -88,7 +88,7 @@ export async function pollForToken(
|
||||
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
||||
client_id: OAUTH_CONFIG.clientId,
|
||||
device_code: deviceCode,
|
||||
...(deviceId && { device_id: deviceId }),
|
||||
device_id: deviceId,
|
||||
...(deviceName && { device_name: deviceName }),
|
||||
}),
|
||||
},
|
||||
@@ -138,6 +138,8 @@ export async function pollForToken(
|
||||
*/
|
||||
export async function refreshAccessToken(
|
||||
refreshToken: string,
|
||||
deviceId: string,
|
||||
deviceName?: string,
|
||||
): Promise<TokenResponse> {
|
||||
const response = await fetch(`${OAUTH_CONFIG.authBaseUrl}/api/oauth/token`, {
|
||||
method: "POST",
|
||||
@@ -147,6 +149,8 @@ export async function refreshAccessToken(
|
||||
client_id: OAUTH_CONFIG.clientId,
|
||||
refresh_token: refreshToken,
|
||||
refresh_token_mode: "new",
|
||||
device_id: deviceId,
|
||||
...(deviceName && { device_name: deviceName }),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user