diff --git a/src/agent/client.ts b/src/agent/client.ts index f7388ad..23fa9c6 100644 --- a/src/agent/client.ts +++ b/src/agent/client.ts @@ -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 }; diff --git a/src/auth/oauth.ts b/src/auth/oauth.ts index a23871a..5ea10a5 100644 --- a/src/auth/oauth.ts +++ b/src/auth/oauth.ts @@ -68,7 +68,7 @@ export async function pollForToken( deviceCode: string, interval: number = 5, expiresIn: number = 900, - deviceId?: string, + deviceId: string, deviceName?: string, ): Promise { 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 { 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 }), }), });