From 7ccafdfbabb82ea1a63238c233d128cdddc59eb4 Mon Sep 17 00:00:00 2001 From: jeff-ang92 <330780721+jeff-ang92@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:49:56 +0700 Subject: [PATCH 1/3] feat(plugin): add "Sign in with ChatGPT" LLM provider Adds a `chatgpt` provider to memos-local-plugin that runs summarizer and skill-evolver calls on the user's ChatGPT plan via OpenAI's Sign in with ChatGPT (OAuth PKCE, Responses API with store:false and stream:true). Falls back to the block's openai_compatible settings on usage limits, eligibility, auth or network failures. - core/llm/chatgpt-auth.ts: PKCE sign-in, ID token verification, owner-only token storage, lock-guarded refresh, stable host ID - core/llm/providers/chatgpt.ts: Responses streaming, 401 refresh, bounded 5xx retry, pause windows, fallback - scripts/chatgpt-login.mjs: sign in, --status, --models - config schema, client registry, skillEvolver wiring, log channel, model-test route, viewer dropdowns, docs, vitest coverage Co-Authored-By: Claude Opus 5.5 --- apps/memos-local-plugin/core/config/schema.ts | 26 + apps/memos-local-plugin/core/llm/README.md | 5 +- .../core/llm/chatgpt-auth.ts | 641 ++++++++++++++++++ apps/memos-local-plugin/core/llm/client.ts | 3 + apps/memos-local-plugin/core/llm/index.ts | 2 + .../core/llm/providers/chatgpt.ts | 480 +++++++++++++ apps/memos-local-plugin/core/llm/types.ts | 22 +- .../core/logger/channels.ts | 1 + .../core/pipeline/memory-core.ts | 13 +- .../docs/CONFIG-ADVANCED.md | 38 ++ apps/memos-local-plugin/package.json | 1 + .../scripts/chatgpt-login.mjs | 48 ++ .../server/routes/models.ts | 3 + .../tests/unit/llm/chatgpt-auth.test.ts | 153 +++++ .../tests/unit/llm/chatgpt-provider.test.ts | 317 +++++++++ .../pipeline/bootstrap-llm-config.test.ts | 62 ++ .../viewer/src/stores/i18n.ts | 4 + .../viewer/src/views/SettingsView.tsx | 9 + 18 files changed, 1822 insertions(+), 6 deletions(-) create mode 100644 apps/memos-local-plugin/core/llm/chatgpt-auth.ts create mode 100644 apps/memos-local-plugin/core/llm/providers/chatgpt.ts create mode 100644 apps/memos-local-plugin/scripts/chatgpt-login.mjs create mode 100644 apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts create mode 100644 apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts diff --git a/apps/memos-local-plugin/core/config/schema.ts b/apps/memos-local-plugin/core/config/schema.ts index 49c6ad6e1..660230913 100644 --- a/apps/memos-local-plugin/core/config/schema.ts +++ b/apps/memos-local-plugin/core/config/schema.ts @@ -95,6 +95,23 @@ const ReasoningSchema = Type.Object({ maxTokens: Type.Optional(Type.Number({ minimum: 1 })), }, { default: {} }); +/** + * Settings for `provider: chatgpt` (Sign in with ChatGPT, see + * `core/llm/providers/chatgpt.ts`). Only read when the provider is + * "chatgpt"; `endpoint` / `apiKey` / `model` in the same block stay the + * openai_compatible fallback. + */ +const ChatGptSchema = Type.Object({ + /** Model slug from GET /v1/models. Blank = provider default. */ + model: Type.Optional(Type.String()), + /** Fall back to the openai_compatible settings on failure. Default true. */ + fallback: Type.Optional(Type.Boolean()), + /** Credential file. Default ~/.config/memos-local/chatgpt-oauth.json. */ + tokenFile: Type.Optional(Type.String()), + /** Optional Responses API reasoning effort (e.g. "low"). */ + reasoningEffort: Type.Optional(Type.String()), +}); + const LlmSchema = Type.Object({ provider: Type.Union([ Type.Literal(""), @@ -104,6 +121,7 @@ const LlmSchema = Type.Object({ Type.Literal("anthropic"), Type.Literal("bedrock"), Type.Literal("host"), + Type.Literal("chatgpt"), ], { default: "" }), endpoint: StringWithDefault(""), model: StringWithDefault(""), @@ -127,6 +145,8 @@ const LlmSchema = Type.Object({ maxTokens: NumberInRange(1024, 100, 131072), /** Extra HTTP headers for the provider request. */ headers: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })), + /** Only read when provider = "chatgpt" (see ChatGptSchema). */ + chatgpt: Type.Optional(ChatGptSchema), }, { default: {} }); /** @@ -145,6 +165,7 @@ const SkillEvolverSchema = Type.Object({ Type.Literal("openai_compatible"), Type.Literal("gemini"), Type.Literal("anthropic"), + Type.Literal("chatgpt"), ], { default: "" }), endpoint: StringWithDefault(""), model: StringWithDefault(""), @@ -163,6 +184,11 @@ const SkillEvolverSchema = Type.Object({ maxTokens: NumberInRange(1024, 100, 131072), /** Extra HTTP headers for the provider request. */ headers: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })), + /** + * Only read when provider = "chatgpt". A blank endpoint / apiKey here + * inherits the main `llm` block's for the openai_compatible fallback. + */ + chatgpt: Type.Optional(ChatGptSchema), }, { default: {} }); const StorageSchema = Type.Object({ diff --git a/apps/memos-local-plugin/core/llm/README.md b/apps/memos-local-plugin/core/llm/README.md index 2ffea8e06..ade89c0fa 100644 --- a/apps/memos-local-plugin/core/llm/README.md +++ b/apps/memos-local-plugin/core/llm/README.md @@ -10,7 +10,7 @@ facade — providers are not exported outside this directory. ## 1. Introduction -Five real providers + two sentinels, all behind one interface: +Six real providers + two sentinels, all behind one interface: | provider | native JSON mode | native stream | notes | | ------------------- | ---------------- | ------------- | ----------------------------- | @@ -18,6 +18,7 @@ Five real providers + two sentinels, all behind one interface: | `anthropic` | ❌ (hint-based) | ✅ (SSE) | Messages API. | | `gemini` | ✅ (`responseMimeType`) | ✅ (SSE) | Google generateContent. | | `bedrock` | ❌ (hint-based) | ❌ | Converse API. SigV4 is the caller's problem. | +| `chatgpt` | ✅ (`json_object`, hint fallback) | ✅ (SSE) | User's ChatGPT plan via Sign in with ChatGPT (Responses API, `store:false`). Falls back to the block's openai_compatible settings. See `docs/CONFIG-ADVANCED.md`. | | `host` | ❌ (hint-based) | ❌ (faked) | Delegates to the host agent (OpenClaw) via `HostLlmBridge`. | | `local_only` | ─ | ─ | Always throws `LLM_UNAVAILABLE`. Use to explicitly disable LLM. | @@ -161,7 +162,7 @@ Current prompts: - `llm.json` — malformed-JSON retry warnings - `llm.prompts` — prompt registry lookups (Phase 12 onward) - `llm.openai_compatible`, `llm.anthropic`, `llm.gemini`, `llm.bedrock`, - `llm.host`, `llm.local_only` — per-provider HTTP attempts + duration + `llm.host`, `llm.local_only`, `llm.chatgpt` — per-provider HTTP attempts + duration + status Every successful call also produces an `llm(...)` payload: diff --git a/apps/memos-local-plugin/core/llm/chatgpt-auth.ts b/apps/memos-local-plugin/core/llm/chatgpt-auth.ts new file mode 100644 index 000000000..0d91adf34 --- /dev/null +++ b/apps/memos-local-plugin/core/llm/chatgpt-auth.ts @@ -0,0 +1,641 @@ +/** + * "Sign in with ChatGPT" credentials for the `chatgpt` LLM provider. + * + * Sign in (from the plugin folder, after `npm run build`): + * node scripts/chatgpt-login.mjs + * + * Flow (developers.openai.com/siwc/token-sharing-open-source/sign-in): + * - OAuth authorization code + PKCE (S256), loopback callback on 127.0.0.1. + * - First sign-in uses client_id=dynamic_agent_client; the issued client_id + * is saved and reused. No client secret, no API key. + * - Scopes: openid profile email offline_access resource.invoke + * chatgpt.tokens.use.direct; resource=https://api.openai.com/v1. + * - Access token ~1h; refresh token rotates on every refresh. + * + * Secrets rule: nothing in this file logs, prints or throws a token. Error + * messages carry OAuth error codes only. + */ + +import { createHash, createPublicKey, randomBytes, randomUUID, verify as cryptoVerify } from "node:crypto"; +import { execFile } from "node:child_process"; +import { promises as fsp } from "node:fs"; +import * as http from "node:http"; +import * as os from "node:os"; +import * as path from "node:path"; + +export const CHATGPT_ISSUER = "https://auth.openai.com"; +export const CHATGPT_AUTHORIZE_URL = "https://auth.openai.com/api/accounts/authorize"; +export const CHATGPT_TOKEN_URL = "https://auth.openai.com/api/accounts/oauth/token"; +export const CHATGPT_JWKS_URL = "https://auth.openai.com/.well-known/jwks.json"; +export const CHATGPT_RESOURCE = "https://api.openai.com/v1"; +export const CHATGPT_API_BASE = "https://api.openai.com/v1"; +export const CHATGPT_SCOPES = [ + "openid", + "profile", + "email", + "offline_access", + "resource.invoke", + "chatgpt.tokens.use.direct", +]; +export const REQUIRED_PLAN_SCOPE = "chatgpt.tokens.use.direct"; +const DYNAMIC_CLIENT_ID = "dynamic_agent_client"; +const AGENT_NAME_HINT = "MemOS local memory"; +const CALLBACK_PATH = "/auth/callback"; +const DEFAULT_CALLBACK_PORT = 1455; +/** Refresh when fewer than this many ms remain on the access token. */ +export const REFRESH_MARGIN_MS = 5 * 60_000; +const LOCK_STALE_MS = 60_000; +const LOCK_WAIT_MS = 20_000; + +/** Refresh-token errors that mean "sign in again" (errors-and-recovery). */ +const REAUTH_ERRORS = new Set([ + "invalid_grant", + "invalid_refresh_token", + "token_expired", + "refresh_token_expired", + "refresh_token_invalidated", + "refresh_token_reused", +]); + +export interface ChatGptCredentials { + version: 1; + issuer: string; + client_id: string; + ext_agent_host_id: string; + subject?: string; + email?: string; + id_token?: string; + access_token: string; + refresh_token: string; + token_type?: string; + /** Epoch ms when the access token expires. */ + expires_at: number; + scopes: string[]; + saved_at: string; + /** Set when a refresh failed terminally; tokens are cleared. */ + reauth_required?: boolean; +} + +export type ChatGptAuthErrorKind = + | "not_signed_in" + | "reauth_required" + | "missing_scope" + | "invalid_client" + | "transient" + | "lock_timeout"; + +export class ChatGptAuthError extends Error { + constructor( + readonly kind: ChatGptAuthErrorKind, + message: string, + ) { + super(message); + this.name = "ChatGptAuthError"; + } +} + +// ─── Paths & storage ───────────────────────────────────────────────────────── + +export function defaultTokenFile(): string { + const env = process.env.MEMOS_CHATGPT_TOKEN_FILE; + if (env && env.trim().length > 0) return env.trim(); + return path.join(os.homedir(), ".config", "memos-local", "chatgpt-oauth.json"); +} + +export async function readCredentials(file: string): Promise { + let raw: string; + try { + raw = await fsp.readFile(file, "utf8"); + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "ENOENT") return null; + throw err; + } + const parsed = JSON.parse(raw) as ChatGptCredentials; + if (!parsed || typeof parsed !== "object" || typeof parsed.client_id !== "string") { + throw new ChatGptAuthError("not_signed_in", "ChatGPT credential file is malformed"); + } + return parsed; +} + +/** Atomic owner-only write: temp file in the same folder, then rename. */ +export async function writeCredentials(file: string, creds: ChatGptCredentials): Promise { + const dir = path.dirname(file); + await fsp.mkdir(dir, { recursive: true, mode: 0o700 }); + const tmp = path.join(dir, `.${path.basename(file)}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`); + const handle = await fsp.open(tmp, "wx", 0o600); + try { + await handle.writeFile(JSON.stringify(creds, null, 2), "utf8"); + await handle.sync(); + } finally { + await handle.close(); + } + try { + await fsp.rename(tmp, file); + } catch (err) { + await fsp.rm(tmp, { force: true }); + throw err; + } + await restrictToOwner(file); +} + +/** + * Owner-only permissions. POSIX: chmod 600. Windows: drop inherited ACEs and + * grant only the current user full control (icacls). + */ +export async function restrictToOwner(target: string): Promise { + if (process.platform !== "win32") { + await fsp.chmod(target, 0o600).catch(() => undefined); + return; + } + const user = os.userInfo().username; + const domain = process.env.USERDOMAIN; + const principal = domain ? `${domain}\\${user}` : user; + const isDir = (await fsp.stat(target).catch(() => null))?.isDirectory() === true; + const icacls = (args: string[]) => + new Promise((resolve) => { + // best effort; never fail a token write on ACL tooling + execFile("icacls", [target, ...args], { windowsHide: true }, () => resolve()); + }); + // /reset drops explicit ACEs a new file gets from the process token + // (SYSTEM, logon session); then keep only the owner. Folders grant the + // owner with (OI)(CI) so files created inside inherit owner-only access. + await icacls(["/reset"]); + await icacls(["/inheritance:r", "/grant:r", isDir ? `${principal}:(OI)(CI)F` : `${principal}:F`]); +} + +// ─── Lock (serialise refreshes across MemOS processes) ─────────────────────── + +async function withFileLock(file: string, fn: () => Promise): Promise { + const lock = `${file}.lock`; + const started = Date.now(); + for (;;) { + try { + const h = await fsp.open(lock, "wx", 0o600); + await h.writeFile(String(process.pid)); + await h.close(); + break; + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err; + try { + const st = await fsp.stat(lock); + if (Date.now() - st.mtimeMs > LOCK_STALE_MS) { + await fsp.rm(lock, { force: true }); + continue; + } + } catch { + continue; // lock vanished between open and stat + } + if (Date.now() - started > LOCK_WAIT_MS) { + throw new ChatGptAuthError("lock_timeout", "timed out waiting for the ChatGPT token lock"); + } + await new Promise((r) => setTimeout(r, 150)); + } + } + try { + return await fn(); + } finally { + await fsp.rm(lock, { force: true }).catch(() => undefined); + } +} + +// ─── Token use & refresh ───────────────────────────────────────────────────── + +export interface TokenOptions { + file?: string; + /** Refresh even if the token is not near expiry (after a 401). */ + force?: boolean; + fetchImpl?: typeof fetch; + now?: () => number; + /** Only used with `force`: skip refresh if another process already rotated past this token. */ + staleAccessToken?: string; +} + +export interface AccessToken { + accessToken: string; + expiresAt: number; + email?: string; +} + +/** + * Return a usable access token, refreshing (under a cross-process lock) when + * it is within REFRESH_MARGIN_MS of expiry or when `force` is set. + */ +export async function getAccessToken(opts: TokenOptions = {}): Promise { + const file = opts.file ?? defaultTokenFile(); + const now = opts.now ?? Date.now; + const creds = await readCredentials(file); + assertUsable(creds); + if (!opts.force && creds.expires_at - now() > REFRESH_MARGIN_MS) { + return { accessToken: creds.access_token, expiresAt: creds.expires_at, email: creds.email }; + } + return withFileLock(file, async () => { + // Another process may have refreshed while we waited for the lock. + const fresh = await readCredentials(file); + assertUsable(fresh); + const rotatedByPeer = opts.force + ? opts.staleAccessToken !== undefined && fresh.access_token !== opts.staleAccessToken + : fresh.expires_at - now() > REFRESH_MARGIN_MS; + if (rotatedByPeer) { + return { accessToken: fresh.access_token, expiresAt: fresh.expires_at, email: fresh.email }; + } + const updated = await refreshCredentials(fresh, file, opts.fetchImpl ?? fetch, now); + return { accessToken: updated.access_token, expiresAt: updated.expires_at, email: updated.email }; + }); +} + +function assertUsable(creds: ChatGptCredentials | null): asserts creds is ChatGptCredentials { + if (!creds) { + throw new ChatGptAuthError("not_signed_in", "not signed in to ChatGPT (run scripts/chatgpt-login.mjs)"); + } + if (creds.reauth_required || !creds.refresh_token || !creds.access_token) { + throw new ChatGptAuthError("reauth_required", "ChatGPT sign-in expired (run scripts/chatgpt-login.mjs)"); + } + if (!creds.scopes.includes(REQUIRED_PLAN_SCOPE)) { + throw new ChatGptAuthError("missing_scope", `ChatGPT grant lacks ${REQUIRED_PLAN_SCOPE}; sign in again`); + } +} + +interface TokenEndpointResponse { + access_token?: string; + refresh_token?: string; + id_token?: string; + token_type?: string; + expires_in?: number; + scope?: string; + error?: string; + error_description?: string; +} + +async function refreshCredentials( + creds: ChatGptCredentials, + file: string, + fetchImpl: typeof fetch, + now: () => number, +): Promise { + let resp: Response; + try { + resp = await fetchImpl(CHATGPT_TOKEN_URL, { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" }, + body: new URLSearchParams({ + grant_type: "refresh_token", + client_id: creds.client_id, + refresh_token: creds.refresh_token, + resource: CHATGPT_RESOURCE, + }).toString(), + signal: AbortSignal.timeout(30_000), + }); + } catch (err) { + throw new ChatGptAuthError("transient", `ChatGPT token refresh failed: ${(err as Error).name}`); + } + const body = (await resp.json().catch(() => ({}))) as TokenEndpointResponse; + if (!resp.ok || !body.access_token) { + const code = body.error ?? `http_${resp.status}`; + if (REAUTH_ERRORS.has(code)) { + // Docs: clear the unusable tokens, keep the issued client_id for re-sign-in. + await writeCredentials(file, { + ...creds, + access_token: "", + refresh_token: "", + id_token: undefined, + reauth_required: true, + saved_at: new Date(now()).toISOString(), + }); + throw new ChatGptAuthError("reauth_required", `ChatGPT refresh rejected (${code}); sign in again`); + } + if (code === "invalid_client") { + throw new ChatGptAuthError("invalid_client", "ChatGPT refresh rejected (invalid_client)"); + } + // 5xx / network / unknown: keep credentials untouched. + throw new ChatGptAuthError("transient", `ChatGPT token refresh failed (${code})`); + } + const updated: ChatGptCredentials = { + ...creds, + access_token: body.access_token, + refresh_token: body.refresh_token ?? creds.refresh_token, + id_token: body.id_token ?? creds.id_token, + token_type: body.token_type ?? creds.token_type, + expires_at: now() + (body.expires_in ?? 3600) * 1000, + scopes: body.scope ? body.scope.split(/\s+/).filter(Boolean) : creds.scopes, + saved_at: new Date(now()).toISOString(), + reauth_required: false, + }; + await writeCredentials(file, updated); + return updated; +} + +// ─── Models ────────────────────────────────────────────────────────────────── + +export interface ChatGptModel { + slug: string; + displayName?: string; +} + +/** GET /v1/models, keeping entries with visibility "list". */ +export async function listChatGptModels(accessToken: string, fetchImpl: typeof fetch = fetch): Promise { + const resp = await fetchImpl(`${CHATGPT_API_BASE}/models`, { + headers: { Authorization: `Bearer ${accessToken}`, Accept: "application/json" }, + signal: AbortSignal.timeout(30_000), + }); + const body = (await resp.json().catch(() => ({}))) as { + models?: Array<{ slug?: string; id?: string; display_name?: string; visibility?: string }>; + data?: Array<{ slug?: string; id?: string; display_name?: string; visibility?: string }>; + error?: { code?: string; message?: string }; + }; + if (!resp.ok) { + throw new Error(`model list failed: HTTP ${resp.status} ${body.error?.code ?? ""}`.trim()); + } + const rows = body.models ?? body.data ?? []; + return rows + .filter((m) => m.visibility === "list") + .map((m) => ({ slug: m.slug ?? m.id ?? "", displayName: m.display_name })) + .filter((m) => m.slug.length > 0); +} + +// ─── Interactive sign-in ───────────────────────────────────────────────────── + +export interface LoginOptions { + file?: string; + port?: number; + /** Prints status lines (never tokens). */ + print?: (line: string) => void; + openBrowser?: (url: string) => void; + timeoutMs?: number; +} + +export interface LoginResult { + email?: string; + subject?: string; + scopes: string[]; + expiresAt: number; + file: string; + newRegistration: boolean; +} + +export async function runLogin(opts: LoginOptions = {}): Promise { + const file = opts.file ?? defaultTokenFile(); + const print = opts.print ?? ((l: string) => console.log(l)); + const existing = await readCredentials(file).catch(() => null); + const savedClientId = existing?.client_id && existing.client_id !== DYNAMIC_CLIENT_ID ? existing.client_id : null; + // Host ID is chosen and persisted BEFORE the browser opens (docs: overview page). + const extAgentHostId = await getOrCreateHostId(file, existing?.ext_agent_host_id); + + const verifier = b64url(randomBytes(48)); + const challenge = b64url(createHash("sha256").update(verifier).digest()); + const state = b64url(randomBytes(24)); + const nonce = b64url(randomBytes(24)); + + const { server, port } = await listenLoopback(opts.port ?? DEFAULT_CALLBACK_PORT); + const redirectUri = `http://127.0.0.1:${port}${CALLBACK_PATH}`; + + const params = new URLSearchParams({ + response_type: "code", + client_id: savedClientId ?? DYNAMIC_CLIENT_ID, + redirect_uri: redirectUri, + scope: CHATGPT_SCOPES.join(" "), + resource: CHATGPT_RESOURCE, + state, + nonce, + code_challenge: challenge, + code_challenge_method: "S256", + ext_agent_host_id: extAgentHostId, + }); + if (!savedClientId) params.set("agent_name_hint", AGENT_NAME_HINT); + if (savedClientId && existing?.id_token) params.set("id_token_hint", existing.id_token); + const authorizeUrl = `${CHATGPT_AUTHORIZE_URL}?${params.toString()}`; + + print("Opening your browser to sign in with ChatGPT."); + print("If it does not open, paste this address into your browser:"); + print(authorizeUrl); + (opts.openBrowser ?? openInBrowser)(authorizeUrl); + + let callback: URLSearchParams; + try { + callback = await waitForCallback(server, opts.timeoutMs ?? 15 * 60_000); + } finally { + server.close(); + } + + if (callback.get("state") !== state) throw new Error("sign-in failed: state mismatch"); + const oauthErr = callback.get("error"); + if (oauthErr) throw new Error(`sign-in failed: ${oauthErr}`); + const code = callback.get("code"); + if (!code) throw new Error("sign-in failed: no authorization code returned"); + const returnedClientId = callback.get("client_id"); + let clientId: string; + if (savedClientId) { + if (returnedClientId && returnedClientId !== savedClientId) { + throw new Error("sign-in failed: callback returned a different client_id"); + } + clientId = savedClientId; + } else { + if (!returnedClientId || returnedClientId === DYNAMIC_CLIENT_ID) { + throw new Error("sign-in failed: registration incomplete (no issued client_id)"); + } + clientId = returnedClientId; + } + + const tokenResp = await fetch(CHATGPT_TOKEN_URL, { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" }, + body: new URLSearchParams({ + grant_type: "authorization_code", + client_id: clientId, + code, + code_verifier: verifier, + redirect_uri: redirectUri, + resource: CHATGPT_RESOURCE, + }).toString(), + signal: AbortSignal.timeout(30_000), + }); + const tok = (await tokenResp.json().catch(() => ({}))) as TokenEndpointResponse; + if (!tokenResp.ok || !tok.access_token) { + throw new Error(`token exchange failed: ${tok.error ?? `HTTP ${tokenResp.status}`}`); + } + if (!tok.refresh_token) throw new Error("token exchange returned no refresh token (offline_access not granted)"); + const scopes = (tok.scope ?? callback.get("scope") ?? "").split(/\s+/).filter(Boolean); + + let subject: string | undefined; + let email: string | undefined; + if (tok.id_token) { + const claims = await verifyIdToken(tok.id_token, clientId, nonce); + subject = claims.sub; + email = typeof claims.email === "string" ? claims.email : undefined; + if (existing?.subject && savedClientId && existing.subject !== subject) { + throw new Error("sign-in failed: a different ChatGPT account signed in for this registration"); + } + } + + const creds: ChatGptCredentials = { + version: 1, + issuer: CHATGPT_ISSUER, + client_id: clientId, + ext_agent_host_id: extAgentHostId, + subject, + email, + id_token: tok.id_token, + access_token: tok.access_token, + refresh_token: tok.refresh_token, + token_type: tok.token_type, + expires_at: Date.now() + (tok.expires_in ?? 3600) * 1000, + scopes, + saved_at: new Date().toISOString(), + }; + await fsp.mkdir(path.dirname(file), { recursive: true, mode: 0o700 }); + await restrictToOwner(path.dirname(file)); + await writeCredentials(file, creds); + return { email, subject, scopes, expiresAt: creds.expires_at, file, newRegistration: !savedClientId }; +} + +/** Status without secrets. */ +export async function describeCredentials(file = defaultTokenFile()): Promise> { + const c = await readCredentials(file); + if (!c) return { signedIn: false, file }; + return { + signedIn: !c.reauth_required && Boolean(c.refresh_token), + file, + email: c.email, + planScope: c.scopes.includes(REQUIRED_PLAN_SCOPE), + scopes: c.scopes, + accessTokenExpires: new Date(c.expires_at).toISOString(), + reauthRequired: Boolean(c.reauth_required), + savedAt: c.saved_at, + }; +} + +// ─── Host ID ───────────────────────────────────────────────────────────────── + +/** + * Accepted `ext_agent_host_id` formats (developers.openai.com/siwc/ + * token-sharing-open-source): JWK-thumbprint URN, `urn:uuid:`, or + * `did:key:`. Opaque — never an email or user id. One per host, stable. + */ +export function isValidHostId(id: string | undefined): id is string { + if (!id) return false; + return ( + /^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(id) || + id.startsWith("urn:ietf:params:oauth:jwk-thumbprint:") || + id.startsWith("did:key:") + ); +} + +export function hostIdFile(tokenFile: string): string { + return path.join(path.dirname(tokenFile), "host-id"); +} + +/** + * Return this host's persisted ID, creating `urn:uuid:` once if none + * exists. Order: valid ID already in the credential record → `host-id` file + * → new. Written owner-only before sign-in starts. + */ +export async function getOrCreateHostId(tokenFile: string, fromCredentials?: string): Promise { + if (isValidHostId(fromCredentials)) return fromCredentials; + const f = hostIdFile(tokenFile); + try { + const saved = (await fsp.readFile(f, "utf8")).trim(); + if (isValidHostId(saved)) return saved; + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err; + } + const id = `urn:uuid:${randomUUID()}`; // randomUUID() is v4 + const dir = path.dirname(f); + await fsp.mkdir(dir, { recursive: true, mode: 0o700 }); + await restrictToOwner(dir); + const tmp = path.join(dir, `.host-id.${process.pid}.${randomBytes(4).toString("hex")}.tmp`); + await fsp.writeFile(tmp, id, { encoding: "utf8", mode: 0o600, flag: "wx" }); + await fsp.rename(tmp, f); + await restrictToOwner(f); + return id; +} + +// ─── Helpers ───────────────────────────────────────────────────────────────── + +function b64url(buf: Buffer): string { + return buf.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); +} + +function listenLoopback(preferred: number): Promise<{ server: http.Server; port: number }> { + const tryPort = (port: number) => + new Promise<{ server: http.Server; port: number }>((resolve, reject) => { + const server = http.createServer(); + server.once("error", reject); + server.listen(port, "127.0.0.1", () => { + const addr = server.address(); + resolve({ server, port: typeof addr === "object" && addr ? addr.port : port }); + }); + }); + return tryPort(preferred).catch(() => tryPort(0)); +} + +function waitForCallback(server: http.Server, timeoutMs: number): Promise { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(new Error("sign-in timed out")), timeoutMs); + server.on("request", (req, res) => { + const url = new URL(req.url ?? "/", "http://127.0.0.1"); + if (url.pathname !== CALLBACK_PATH) { + res.writeHead(404).end(); + return; + } + res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); + res.end( + "MemOS

" + + "MemOS received the ChatGPT sign-in. You can close this tab.

", + ); + clearTimeout(timer); + resolve(url.searchParams); + }); + }); +} + +function openInBrowser(url: string): void { + if (process.platform === "win32") { + // rundll32 avoids cmd.exe treating '&' in the URL as a command separator. + execFile("rundll32", ["url.dll,FileProtocolHandler", url], { windowsHide: true }, () => undefined); + } else if (process.platform === "darwin") { + execFile("open", [url], () => undefined); + } else { + execFile("xdg-open", [url], () => undefined); + } +} + +interface IdTokenClaims { + iss?: string; + aud?: string | string[]; + exp?: number; + nonce?: string; + sub?: string; + email?: unknown; +} + +/** RS256 signature + iss/aud/exp/nonce checks against OpenAI's JWKS. */ +export async function verifyIdToken( + idToken: string, + clientId: string, + nonce: string, + fetchImpl: typeof fetch = fetch, +): Promise { + const parts = idToken.split("."); + if (parts.length !== 3) throw new Error("id_token malformed"); + const header = JSON.parse(Buffer.from(parts[0], "base64url").toString("utf8")) as { alg?: string; kid?: string }; + const claims = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8")) as IdTokenClaims; + if (header.alg !== "RS256") throw new Error(`id_token alg ${header.alg} not accepted`); + const jwksResp = await fetchImpl(CHATGPT_JWKS_URL, { signal: AbortSignal.timeout(30_000) }); + const jwks = (await jwksResp.json()) as { keys?: Array> }; + const keys = jwks.keys ?? []; + const jwk = keys.find((k) => k.kid === header.kid) ?? (keys.length === 1 ? keys[0] : undefined); + if (!jwk) throw new Error("id_token signing key not found in JWKS"); + const key = createPublicKey({ key: jwk as never, format: "jwk" }); + const ok = cryptoVerify( + "RSA-SHA256", + Buffer.from(`${parts[0]}.${parts[1]}`), + key, + Buffer.from(parts[2], "base64url"), + ); + if (!ok) throw new Error("id_token signature invalid"); + if (claims.iss !== CHATGPT_ISSUER) throw new Error("id_token issuer mismatch"); + const aud = Array.isArray(claims.aud) ? claims.aud : [claims.aud]; + if (!aud.includes(clientId)) throw new Error("id_token audience mismatch"); + if (typeof claims.exp !== "number" || claims.exp * 1000 < Date.now() - 60_000) throw new Error("id_token expired"); + if (claims.nonce !== nonce) throw new Error("id_token nonce mismatch"); + return claims; +} diff --git a/apps/memos-local-plugin/core/llm/client.ts b/apps/memos-local-plugin/core/llm/client.ts index 7e19f25ca..8e4e1d6db 100644 --- a/apps/memos-local-plugin/core/llm/client.ts +++ b/apps/memos-local-plugin/core/llm/client.ts @@ -23,6 +23,7 @@ import { getHostLlmBridge } from "./host-bridge.js"; import { buildJsonSystemHint, parseLlmJson } from "./json-mode.js"; import { AnthropicLlmProvider } from "./providers/anthropic.js"; import { BedrockLlmProvider } from "./providers/bedrock.js"; +import { ChatGptLlmProvider } from "./providers/chatgpt.js"; import { GeminiLlmProvider } from "./providers/gemini.js"; import { HostLlmProvider } from "./providers/host.js"; import { LocalOnlyLlmProvider } from "./providers/local-only.js"; @@ -761,6 +762,8 @@ export function makeProviderFor(name: LlmProviderName): LlmProvider { return new HostLlmProvider(); case "local_only": return new LocalOnlyLlmProvider(); + case "chatgpt": + return new ChatGptLlmProvider(); default: throw new MemosError(ERROR_CODES.UNSUPPORTED, `Unknown llm provider: ${String(name)}`, { provider: name, diff --git a/apps/memos-local-plugin/core/llm/index.ts b/apps/memos-local-plugin/core/llm/index.ts index a295cd4a4..08695d94e 100644 --- a/apps/memos-local-plugin/core/llm/index.ts +++ b/apps/memos-local-plugin/core/llm/index.ts @@ -25,8 +25,10 @@ export { GeminiLlmProvider } from "./providers/gemini.js"; export { BedrockLlmProvider } from "./providers/bedrock.js"; export { HostLlmProvider } from "./providers/host.js"; export { LocalOnlyLlmProvider } from "./providers/local-only.js"; +export { ChatGptLlmProvider } from "./providers/chatgpt.js"; export * from "./prompts/index.js"; export type { + ChatGptProviderConfig, LlmCallOptions, LlmCircuitBreakerConfig, LlmCompleteJsonOptions, diff --git a/apps/memos-local-plugin/core/llm/providers/chatgpt.ts b/apps/memos-local-plugin/core/llm/providers/chatgpt.ts new file mode 100644 index 000000000..543a87cad --- /dev/null +++ b/apps/memos-local-plugin/core/llm/providers/chatgpt.ts @@ -0,0 +1,480 @@ +/** + * `chatgpt` provider — runs MemOS LLM calls on the user's ChatGPT plan via + * "Sign in with ChatGPT" (OpenAI Responses API), with automatic fallback to + * the existing openai_compatible settings (OpenRouter) in the same `llm` + * block. + * + * Sign in once (from the plugin folder): node scripts/chatgpt-login.mjs + * + * Endpoint: POST https://api.openai.com/v1/responses with store:false and + * stream:true. Success only on `response.completed`. `temperature`, + * `max_output_tokens` and system-role input items are not accepted on this + * route, so system text goes into `instructions` and temperature/maxTokens + * are ignored. + * + * Config (all optional except provider): + * llm: + * provider: chatgpt + * endpoint/apiKey/model: OpenRouter settings, used for fallback + * chatgpt: + * model: + * fallback: true # default true + * tokenFile: # default ~/.config/memos-local/chatgpt-oauth.json + * reasoningEffort: low # omitted unless set + */ + +import { ERROR_CODES, MemosError } from "../../../agent-contract/errors.js"; +import { ChatGptAuthError, CHATGPT_API_BASE, getAccessToken, type TokenOptions, type AccessToken } from "../chatgpt-auth.js"; +import { decodeSse } from "../fetcher.js"; +import { OpenAiLlmProvider } from "./openai.js"; +import type { + ChatGptProviderConfig, + LlmMessage, + LlmProvider, + LlmProviderCtx, + LlmProviderName, + LlmStreamChunk, + LlmUsage, + ProviderCallInput, + ProviderCompletion, +} from "../types.js"; + +const RESPONSES_URL = `${CHATGPT_API_BASE}/responses`; +export const DEFAULT_CHATGPT_MODEL = "gpt-6.1-sol"; + +/** How long to skip ChatGPT after a failure of each kind (ms). */ +export const PAUSE_MS = { + usageLimit: 30 * 60_000, + notEligible: 6 * 60 * 60_000, + auth: 10 * 60_000, +} as const; + +const RETRYABLE_STATUS = new Set([500, 502, 503, 504]); +const BACKOFF_MS = [1_000, 3_000]; + +/** Shared across provider instances in one process (llm / skillEvolver / l3). */ +const pauseState = new Map(); + +export function resetChatGptPauseState(): void { + pauseState.clear(); +} + +export class ChatGptFailure extends Error { + constructor( + readonly code: string, + readonly status: number | null, + message: string, + readonly param?: string, + ) { + super(message); + this.name = "ChatGptFailure"; + } +} + +export interface ChatGptDeps { + fetchImpl?: typeof fetch; + getToken?: (opts: TokenOptions) => Promise; + now?: () => number; + sleep?: (ms: number) => Promise; + fallback?: LlmProvider; +} + +interface StreamPiece { + delta?: string; + done?: { usage?: LlmUsage }; +} + +export class ChatGptLlmProvider implements LlmProvider { + readonly name: LlmProviderName = "chatgpt"; + private readonly fetchImpl: typeof fetch; + private readonly getToken: (opts: TokenOptions) => Promise; + private readonly now: () => number; + private readonly sleep: (ms: number) => Promise; + private readonly fallbackProvider: LlmProvider; + private jsonFormatUnsupported = false; + + constructor(deps: ChatGptDeps = {}) { + this.fetchImpl = deps.fetchImpl ?? ((...a: Parameters) => fetch(...a)); + this.getToken = deps.getToken ?? getAccessToken; + this.now = deps.now ?? Date.now; + this.sleep = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))); + this.fallbackProvider = deps.fallback ?? new OpenAiLlmProvider(); + } + + async complete( + messages: LlmMessage[], + opts: ProviderCallInput, + ctx: LlmProviderCtx, + ): Promise { + const cg = chatgptConfig(ctx); + const paused = this.pausedReason(cg); + if (paused) return this.fallbackComplete(messages, opts, ctx, cg, paused, null); + const start = this.now(); + try { + let text = ""; + let usage: LlmUsage | undefined; + for await (const piece of this.streamChatGpt(messages, opts, ctx, cg)) { + if (piece.delta) text += piece.delta; + if (piece.done) usage = piece.done.usage; + } + return { text, finishReason: "stop", usage, durationMs: this.now() - start }; + } catch (err) { + return this.fallbackComplete(messages, opts, ctx, cg, null, err); + } + } + + async *stream( + messages: LlmMessage[], + opts: ProviderCallInput, + ctx: LlmProviderCtx, + ): AsyncGenerator { + const cg = chatgptConfig(ctx); + const paused = this.pausedReason(cg); + if (paused) { + yield* this.fallbackStream(messages, opts, ctx, cg, paused, null); + return; + } + let emitted = false; + try { + for await (const piece of this.streamChatGpt(messages, opts, ctx, cg)) { + if (piece.delta) { + emitted = true; + yield { delta: piece.delta, done: false }; + } + if (piece.done) yield { delta: "", done: true, finishReason: "stop", usage: piece.done.usage }; + } + } catch (err) { + // Text already reached the caller: a silent switch would duplicate it. + if (emitted) throw this.toMemosError(err, cg, ctx); + yield* this.fallbackStream(messages, opts, ctx, cg, null, err); + } + } + + // ─── ChatGPT request ─────────────────────────────────────────────────────── + + private async *streamChatGpt( + messages: LlmMessage[], + opts: ProviderCallInput, + ctx: LlmProviderCtx, + cg: ChatGptProviderConfig, + ): AsyncGenerator { + const tokenFile = cg.tokenFile && cg.tokenFile.length > 0 ? cg.tokenFile : undefined; + let token = await this.getToken({ file: tokenFile }); + let refreshedAfter401 = false; + let droppedJsonFormat = false; + let attempt = 0; + + for (;;) { + const body = this.buildBody(messages, opts, cg); + const signal = mergeSignals(ctx.signal, ctx.config.timeoutMs, ctx.deadlineAt, this.now()); + let resp: Response; + try { + resp = await this.fetchImpl(RESPONSES_URL, { + method: "POST", + headers: { + Authorization: `Bearer ${token.accessToken}`, + "Content-Type": "application/json", + Accept: "text/event-stream", + }, + body: JSON.stringify(body), + signal, + }); + } catch (err) { + const name = (err as Error)?.name ?? "Error"; + if (ctx.signal?.aborted) throw new ChatGptFailure("cancelled", null, "ChatGPT request was cancelled"); + if (name !== "TimeoutError" && attempt < BACKOFF_MS.length && this.fitsDeadline(ctx, BACKOFF_MS[attempt])) { + await this.sleep(BACKOFF_MS[attempt++]); + continue; + } + throw new ChatGptFailure(name === "TimeoutError" ? "timeout" : "network_error", null, `ChatGPT request failed (${name})`); + } + + if (!resp.ok) { + const failure = await readHttpFailure(resp); + if (resp.status === 401 && !refreshedAfter401) { + refreshedAfter401 = true; + token = await this.getToken({ file: tokenFile, force: true, staleAccessToken: token.accessToken }); + continue; + } + if ( + failure.code === "subscription_sharing_unsupported_capability" && + opts.jsonMode && + !droppedJsonFormat && + !this.jsonFormatUnsupported && + (!failure.param || failure.param.startsWith("text")) + ) { + // JSON still enforced by the facade's prompt hint + parser. + this.jsonFormatUnsupported = true; + droppedJsonFormat = true; + ctx.log.warn("chatgpt.json_format_unsupported", { param: failure.param }); + continue; + } + const retryable = + RETRYABLE_STATUS.has(resp.status) && + failure.code !== "subscription_sharing_usage_limit_exceeded"; + if (retryable && attempt < BACKOFF_MS.length && this.fitsDeadline(ctx, BACKOFF_MS[attempt])) { + await this.sleep(BACKOFF_MS[attempt++]); + continue; + } + throw failure; + } + if (!resp.body) throw new ChatGptFailure("empty_body", resp.status, "ChatGPT returned an empty stream"); + + yield* parseResponsesStream(resp.body); + return; + } + } + + private buildBody( + messages: LlmMessage[], + opts: ProviderCallInput, + cg: ChatGptProviderConfig, + ): Record { + const system = messages.filter((m) => m.role === "system").map((m) => m.content).join("\n\n"); + let input = messages + .filter((m) => m.role !== "system") + .map((m) => ({ role: m.role, content: m.content })); + let instructions = system; + if (input.length === 0) { + input = [{ role: "user", content: system }]; + instructions = ""; + } + const body: Record = { + model: cg.model && cg.model.length > 0 ? cg.model : DEFAULT_CHATGPT_MODEL, + input, + store: false, + stream: true, + }; + if (instructions) body.instructions = instructions; + if (opts.jsonMode && !this.jsonFormatUnsupported) body.text = { format: { type: "json_object" } }; + if (cg.reasoningEffort) body.reasoning = { effort: cg.reasoningEffort }; + return body; + } + + // ─── Fallback & errors ───────────────────────────────────────────────────── + + private pausedReason(cg: ChatGptProviderConfig): string | null { + const p = pauseState.get(pauseKey(cg)); + if (!p) return null; + if (this.now() >= p.until) { + pauseState.delete(pauseKey(cg)); + return null; + } + return p.reason; + } + + private notePause(err: unknown, cg: ChatGptProviderConfig): void { + const ms = pauseFor(err); + if (ms > 0) pauseState.set(pauseKey(cg), { until: this.now() + ms, reason: failureCode(err) }); + } + + private async fallbackComplete( + messages: LlmMessage[], + opts: ProviderCallInput, + ctx: LlmProviderCtx, + cg: ChatGptProviderConfig, + pausedReason: string | null, + err: unknown, + ): Promise { + if (err) this.notePause(err, cg); + if (cg.fallback === false || ctx.signal?.aborted) { + throw this.toMemosError(err ?? pausedErr(pausedReason), cg, ctx); + } + ctx.log.warn("chatgpt.fallback", { + to: "openai_compatible", + reason: pausedReason ? `paused:${pausedReason}` : failureCode(err), + status: err instanceof ChatGptFailure ? err.status : undefined, + }); + return this.fallbackProvider.complete(messages, opts, ctx); + } + + private async *fallbackStream( + messages: LlmMessage[], + opts: ProviderCallInput, + ctx: LlmProviderCtx, + cg: ChatGptProviderConfig, + pausedReason: string | null, + err: unknown, + ): AsyncGenerator { + if (err) this.notePause(err, cg); + if (cg.fallback === false || ctx.signal?.aborted) { + throw this.toMemosError(err ?? pausedErr(pausedReason), cg, ctx); + } + ctx.log.warn("chatgpt.fallback", { + to: "openai_compatible", + reason: pausedReason ? `paused:${pausedReason}` : failureCode(err), + stream: true, + }); + if (typeof this.fallbackProvider.stream === "function") { + yield* this.fallbackProvider.stream(messages, opts, ctx); + return; + } + const res = await this.fallbackProvider.complete(messages, opts, ctx); + yield { delta: res.text, done: false }; + yield { delta: "", done: true, finishReason: res.finishReason, usage: res.usage }; + } + + private toMemosError(err: unknown, cg: ChatGptProviderConfig, ctx: LlmProviderCtx): MemosError { + if (err instanceof MemosError) return err; + const code = failureCode(err); + const status = err instanceof ChatGptFailure ? err.status : null; + const memosCode = + code === "subscription_sharing_usage_limit_exceeded" || status === 429 + ? ERROR_CODES.LLM_RATE_LIMITED + : code === "timeout" || code === "cancelled" + ? ERROR_CODES.LLM_TIMEOUT + : ERROR_CODES.LLM_UNAVAILABLE; + ctx.log.warn("chatgpt.failed", { code, status }); + return new MemosError(memosCode, `chatgpt: ${code}${status ? ` (HTTP ${status})` : ""}`, { + provider: "chatgpt", + model: cg.model, + status: status ?? undefined, + chatgptCode: code, + }); + } + + private fitsDeadline(ctx: LlmProviderCtx, delayMs: number): boolean { + return ctx.deadlineAt === undefined || this.now() + delayMs < ctx.deadlineAt; + } +} + +// ─── Stream parsing ────────────────────────────────────────────────────────── + +interface ResponsesUsage { + input_tokens?: number; + output_tokens?: number; + total_tokens?: number; +} + +interface ResponsesEvent { + type?: string; + delta?: string; + code?: string; + message?: string; + param?: string; + error?: { code?: string; message?: string; param?: string }; + response?: { + status?: string; + usage?: ResponsesUsage; + error?: { code?: string; message?: string } | null; + incomplete_details?: { reason?: string } | null; + output?: Array<{ type?: string; content?: Array<{ type?: string; text?: string }> }>; + }; +} + +export async function* parseResponsesStream(body: ReadableStream): AsyncGenerator { + let sawText = false; + for await (const payload of decodeSse(body)) { + if (payload === "[DONE]") break; + let ev: ResponsesEvent; + try { + ev = JSON.parse(payload) as ResponsesEvent; + } catch { + continue; // keepalive / non-JSON line + } + switch (ev.type) { + case "response.output_text.delta": + if (ev.delta) { + sawText = true; + yield { delta: ev.delta }; + } + break; + case "response.completed": { + if (!sawText) { + const text = (ev.response?.output ?? []) + .flatMap((o) => o.content ?? []) + .filter((c) => c.type === "output_text" && typeof c.text === "string") + .map((c) => c.text as string) + .join(""); + if (text) yield { delta: text }; + } + yield { done: { usage: mapUsage(ev.response?.usage) } }; + return; + } + case "response.failed": { + const code = ev.response?.error?.code ?? "unknown_error"; + throw new ChatGptFailure(code, null, `ChatGPT response failed (${code})`); + } + case "response.incomplete": { + const reason = ev.response?.incomplete_details?.reason ?? "unknown"; + throw new ChatGptFailure(`incomplete:${reason}`, null, `ChatGPT response incomplete (${reason})`); + } + case "error": { + const code = ev.code ?? ev.error?.code ?? "stream_error"; + throw new ChatGptFailure(code, null, `ChatGPT stream error (${code})`, ev.param ?? ev.error?.param); + } + default: + break; + } + } + throw new ChatGptFailure("stream_interrupted", null, "ChatGPT stream ended before response.completed"); +} + +function mapUsage(u: ResponsesUsage | undefined): LlmUsage | undefined { + if (!u) return undefined; + return { promptTokens: u.input_tokens, completionTokens: u.output_tokens, totalTokens: u.total_tokens }; +} + +// ─── Helpers ───────────────────────────────────────────────────────────────── + +function chatgptConfig(ctx: LlmProviderCtx): ChatGptProviderConfig { + return ctx.config.chatgpt ?? {}; +} + +function pauseKey(cg: ChatGptProviderConfig): string { + return cg.tokenFile && cg.tokenFile.length > 0 ? cg.tokenFile : "default"; +} + +function pausedErr(reason: string | null): ChatGptFailure { + return new ChatGptFailure(reason ?? "paused", null, `ChatGPT paused (${reason ?? "unknown"})`); +} + +function failureCode(err: unknown): string { + if (err instanceof ChatGptFailure) return err.code; + if (err instanceof ChatGptAuthError) return `auth:${err.kind}`; + if (err instanceof MemosError) return err.code; + return (err as Error)?.name ?? "unknown_error"; +} + +function pauseFor(err: unknown): number { + if (err instanceof ChatGptAuthError) { + return err.kind === "transient" || err.kind === "lock_timeout" ? 0 : PAUSE_MS.auth; + } + if (err instanceof ChatGptFailure) { + if (err.code === "subscription_sharing_usage_limit_exceeded") return PAUSE_MS.usageLimit; + if (err.code === "subscription_sharing_user_not_eligible") return PAUSE_MS.notEligible; + if (err.status === 401 || err.status === 403) return PAUSE_MS.auth; + } + return 0; +} + +async function readHttpFailure(resp: Response): Promise { + let code = `http_${resp.status}`; + let param: string | undefined; + try { + const j = (await resp.json()) as { error?: { code?: string; type?: string; param?: string } | string; code?: string }; + if (j && typeof j.error === "object" && j.error) { + code = j.error.code ?? j.error.type ?? code; + param = j.error.param ?? undefined; + } else if (typeof j?.error === "string") { + code = j.error; + } else if (typeof j?.code === "string") { + code = j.code; + } + } catch { + /* non-JSON body: keep http_ */ + } + return new ChatGptFailure(code, resp.status, `ChatGPT HTTP ${resp.status} (${code})`, param); +} + +function mergeSignals( + outer: AbortSignal | undefined, + timeoutMs: number, + deadlineAt: number | undefined, + nowMs: number, +): AbortSignal { + const budget = deadlineAt === undefined ? timeoutMs : Math.max(1, Math.min(timeoutMs, deadlineAt - nowMs)); + const signals: AbortSignal[] = [AbortSignal.timeout(budget)]; + if (outer) signals.push(outer); + return AbortSignal.any(signals); +} diff --git a/apps/memos-local-plugin/core/llm/types.ts b/apps/memos-local-plugin/core/llm/types.ts index ef61c439f..9aeca574e 100644 --- a/apps/memos-local-plugin/core/llm/types.ts +++ b/apps/memos-local-plugin/core/llm/types.ts @@ -16,10 +16,28 @@ export type LlmProviderName = | "anthropic" | "gemini" | "bedrock" - | "host"; + | "host" + | "chatgpt"; export type ReasoningConfig = ConfigReasoningConfig; +/** + * Settings for the `chatgpt` provider (`llm.chatgpt`, `skillEvolver.chatgpt`). + * The provider runs calls on the user's ChatGPT plan via "Sign in with + * ChatGPT"; `endpoint` / `apiKey` / `model` in the same block stay the + * openai_compatible fallback. + */ +export interface ChatGptProviderConfig { + /** Model slug from GET /v1/models (visibility "list"). */ + model?: string; + /** Fall back to the openai_compatible settings in the same block. Default true. */ + fallback?: boolean; + /** Credential file. Default ~/.config/memos-local/chatgpt-oauth.json. */ + tokenFile?: string; + /** Optional Responses API reasoning effort. Omitted when unset. */ + reasoningEffort?: string; +} + /** * Resolved LLM config, post-defaults. Subset of `ResolvedConfig.llm` so * the client is unit-testable without the whole config object. @@ -70,6 +88,8 @@ export interface LlmConfig { * (issue #1897) for the full state machine. */ circuitBreaker?: LlmCircuitBreakerConfig; + /** Only read by the `chatgpt` provider. */ + chatgpt?: ChatGptProviderConfig; } export interface LlmCircuitBreakerConfig { diff --git a/apps/memos-local-plugin/core/logger/channels.ts b/apps/memos-local-plugin/core/logger/channels.ts index 6adb843e2..2cface796 100644 --- a/apps/memos-local-plugin/core/logger/channels.ts +++ b/apps/memos-local-plugin/core/logger/channels.ts @@ -93,6 +93,7 @@ export const CHANNELS = [ "llm.bedrock", "llm.local_only", "llm.host", + "llm.chatgpt", "llm.json", "llm.prompts", diff --git a/apps/memos-local-plugin/core/pipeline/memory-core.ts b/apps/memos-local-plugin/core/pipeline/memory-core.ts index 4f9ad2c01..a219fe8a5 100644 --- a/apps/memos-local-plugin/core/pipeline/memory-core.ts +++ b/apps/memos-local-plugin/core/pipeline/memory-core.ts @@ -98,7 +98,7 @@ import { registerHostLlmBridge, type HostLlmBridge, } from "../llm/host-bridge.js"; -import type { ReasoningConfig } from "../llm/types.js"; +import type { ChatGptProviderConfig, ReasoningConfig } from "../llm/types.js"; import { createPipeline } from "./orchestrator.js"; import { RECOVERY_REASONS } from "./recovery-constants.js"; @@ -147,6 +147,8 @@ type DedicatedLlmConfig = { maxTokens?: number; /** Extra HTTP headers for the provider request. */ headers?: Record; + /** Only read when provider = "chatgpt". */ + chatgpt?: ChatGptProviderConfig; }; export interface BootstrapOptions { @@ -440,11 +442,15 @@ export async function bootstrapMemoryCoreFull( const evolverModel = (evolver?.model ?? "").trim(); const evolverProvider = (evolver?.provider ?? "").trim(); if (evolverModel && evolverProvider) { + // A chatgpt skill evolver with no endpoint/apiKey of its own uses the + // main llm block's for its openai_compatible fallback. + const mainLlm = (config as { llm?: { endpoint?: string; apiKey?: string } }).llm; + const inheritMain = evolverProvider === "chatgpt"; reflectLlm = createLlmClient({ provider: evolverProvider, model: evolverModel, - endpoint: evolver?.endpoint ?? "", - apiKey: evolver?.apiKey ?? "", + endpoint: evolver?.endpoint || (inheritMain ? mainLlm?.endpoint ?? "" : ""), + apiKey: evolver?.apiKey || (inheritMain ? mainLlm?.apiKey ?? "" : ""), temperature: evolver?.temperature ?? 0, timeoutMs: evolver?.timeoutMs ?? 60_000, providerIgnore: evolver?.providerIgnore, @@ -453,6 +459,7 @@ export async function bootstrapMemoryCoreFull( reasoning: evolver?.reasoning, maxTokens: evolver?.maxTokens, headers: evolver?.headers, + chatgpt: evolver?.chatgpt, maxRetries: 3, // V7 §0.x — when the user's dedicated skill-evolver model is // down (auth, model name typo, server outage), prefer falling diff --git a/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md b/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md index e25743e34..4014d2dbf 100644 --- a/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md +++ b/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md @@ -47,6 +47,44 @@ llm: maxRetries: 3 ``` +#### Sign in with ChatGPT (`provider: chatgpt`) + +Runs MemOS LLM calls on the user's own ChatGPT plan through OpenAI's +[Sign in with ChatGPT](https://developers.openai.com/siwc/token-sharing-open-source/sign-in) +(OAuth with PKCE, no API key). Calls go to the Responses API with +`store: false` and `stream: true`. On failure (usage limit, not eligible, +signed out, network) the call falls back to the OpenAI-compatible settings in +the same block, so `endpoint` / `apiKey` / `model` there stay useful. + +Sign in once from the plugin folder (opens a browser; tokens are saved +owner-only and refresh automatically): + +```bash +node scripts/chatgpt-login.mjs # sign in +node scripts/chatgpt-login.mjs --status # show sign-in state (never prints tokens) +node scripts/chatgpt-login.mjs --models # list models your plan can use +``` + +```yaml +llm: + provider: chatgpt + endpoint: https://openrouter.ai/api/v1 # fallback + model: google/gemini-2.5-flash-lite # fallback model + apiKey: sk-or-v1-... # fallback key + chatgpt: + model: "" # slug from --models; blank = provider default + fallback: true # false = fail instead of using the fallback + tokenFile: "" # default ~/.config/memos-local/chatgpt-oauth.json + # reasoningEffort: low +``` + +`skillEvolver` accepts the same `provider: chatgpt` and `chatgpt:` block. A +chatgpt skill evolver with a blank `endpoint` / `apiKey` reuses the main +`llm` block's for its fallback. The Responses route ignores `temperature` +and `maxTokens`. After a usage-limit error ChatGPT is skipped for 30 minutes, +after "not eligible" for 6 hours, and after an auth failure for 10 minutes; +those calls go straight to the fallback. + ### `storage` ```yaml storage: diff --git a/apps/memos-local-plugin/package.json b/apps/memos-local-plugin/package.json index b0d879531..a66c29fb0 100644 --- a/apps/memos-local-plugin/package.json +++ b/apps/memos-local-plugin/package.json @@ -39,6 +39,7 @@ "templates/README.user.md", "viewer/dist", "scripts/postinstall.cjs", + "scripts/chatgpt-login.mjs", "scripts/migrate-dsh-preset-profile.mjs", "scripts/sync-hermes-version.cjs", "install.sh", diff --git a/apps/memos-local-plugin/scripts/chatgpt-login.mjs b/apps/memos-local-plugin/scripts/chatgpt-login.mjs new file mode 100644 index 000000000..3404aa839 --- /dev/null +++ b/apps/memos-local-plugin/scripts/chatgpt-login.mjs @@ -0,0 +1,48 @@ +#!/usr/bin/env node +// Sign in with ChatGPT for the MemOS `chatgpt` LLM provider. +// Run from the plugin folder after `npm run build`: +// +// node scripts/chatgpt-login.mjs sign in (opens your browser) +// node scripts/chatgpt-login.mjs --status show sign-in status (no tokens) +// node scripts/chatgpt-login.mjs --models list models your plan can use +// node scripts/chatgpt-login.mjs --file= use a non-default token file +// +// Never prints access, refresh or ID tokens. + +import { + describeCredentials, + getAccessToken, + listChatGptModels, + runLogin, +} from "../dist/core/llm/chatgpt-auth.js"; + +const args = process.argv.slice(2); +const fileArg = args.find((a) => a.startsWith("--file=")); +const file = fileArg ? fileArg.slice("--file=".length) : undefined; + +async function main() { + if (args.includes("--status")) { + console.log(JSON.stringify(await describeCredentials(file), null, 2)); + return; + } + if (args.includes("--models")) { + const { accessToken } = await getAccessToken({ file }); + const models = await listChatGptModels(accessToken); + if (models.length === 0) console.log("No listed models returned."); + for (const m of models) console.log(`${m.slug}${m.displayName ? ` (${m.displayName})` : ""}`); + return; + } + const res = await runLogin({ file }); + console.log(""); + console.log("Signed in to ChatGPT for MemOS."); + console.log(` account: ${res.email ?? "(no email claim)"}`); + console.log(` plan scope: ${res.scopes.includes("chatgpt.tokens.use.direct") ? "granted" : "MISSING"}`); + console.log(` token file: ${res.file}`); + console.log(` access expires: ${new Date(res.expiresAt).toISOString()} (refreshes automatically)`); + console.log(` registration: ${res.newRegistration ? "new" : "reused"}`); +} + +main().catch((err) => { + console.error(`chatgpt-login: ${err instanceof Error ? err.message : String(err)}`); + process.exitCode = 1; +}); diff --git a/apps/memos-local-plugin/server/routes/models.ts b/apps/memos-local-plugin/server/routes/models.ts index 620fdc325..db0f0b543 100644 --- a/apps/memos-local-plugin/server/routes/models.ts +++ b/apps/memos-local-plugin/server/routes/models.ts @@ -366,6 +366,9 @@ async function probeChat(req: TestRequest): Promise { case "bedrock": case "host": case "local_only": + // chatgpt authenticates with the Sign in with ChatGPT token file, not an + // API key, so there is nothing to probe from the settings form. + case "chatgpt": return 0; default: throw new Error(`unsupported chat provider: ${provider}`); diff --git a/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts b/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts new file mode 100644 index 000000000..10892523c --- /dev/null +++ b/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts @@ -0,0 +1,153 @@ +/** + * Sign in with ChatGPT credentials: refresh, rotation, cross-process lock, + * recovery and host ID. Synthetic token files in a temp folder only. + */ + +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { afterEach, describe, expect, it } from "vitest"; + +import { + ChatGptAuthError, + getAccessToken, + getOrCreateHostId, + hostIdFile, + isValidHostId, +} from "../../../core/llm/chatgpt-auth.js"; + +function jsonResponse(obj: unknown, status: number): Response { + return new Response(JSON.stringify(obj), { status, headers: { "Content-Type": "application/json" } }); +} + +const NOW = 5_000_000; +const dirs: string[] = []; + +async function tokenFixture(overrides: Record = {}) { + const dir = await mkdtemp(join(tmpdir(), "memos-chatgpt-test-")); + dirs.push(dir); + const file = join(dir, "chatgpt-oauth.json"); + await writeFile( + file, + JSON.stringify({ + version: 1, + issuer: "https://auth.openai.com", + client_id: "oaiapp_test", + ext_agent_host_id: "urn:uuid:11111111-1111-4111-8111-111111111111", + access_token: "synthetic-access-1", + refresh_token: "synthetic-refresh-1", + expires_at: NOW + 60_000, // inside the 5-minute refresh margin + scopes: ["openid", "offline_access", "resource.invoke", "chatgpt.tokens.use.direct"], + saved_at: "2026-01-01T00:00:00.000Z", + ...overrides, + }), + ); + return { dir, file }; +} + +afterEach(async () => { + await Promise.all(dirs.splice(0).map((d) => rm(d, { recursive: true, force: true }))); +}); + +describe("chatgpt-auth getAccessToken", () => { + it("refreshes near expiry, rotates both tokens, and parallel callers refresh only once", async () => { + const { file } = await tokenFixture(); + let refreshes = 0; + const fetchImpl = (async (_url: string, init: RequestInit) => { + refreshes++; + const form = new URLSearchParams(String(init.body)); + expect(form.get("grant_type")).toBe("refresh_token"); + expect(form.get("client_id")).toBe("oaiapp_test"); + expect(form.get("resource")).toBe("https://api.openai.com/v1"); + expect(form.get("scope")).toBeNull(); + await new Promise((r) => setTimeout(r, 50)); + return jsonResponse( + { access_token: "synthetic-access-2", refresh_token: "synthetic-refresh-2", expires_in: 3600 }, + 200, + ); + }) as unknown as typeof fetch; + const [a, b] = await Promise.all([ + getAccessToken({ file, fetchImpl, now: () => NOW }), + getAccessToken({ file, fetchImpl, now: () => NOW }), + ]); + expect(refreshes).toBe(1); + expect(a.accessToken).toBe("synthetic-access-2"); + expect(b.accessToken).toBe("synthetic-access-2"); + const saved = JSON.parse(await readFile(file, "utf8")); + expect(saved.refresh_token).toBe("synthetic-refresh-2"); + expect(saved.expires_at).toBe(NOW + 3_600_000); + }); + + it("returns a fresh token without any network call", async () => { + const { file } = await tokenFixture({ expires_at: NOW + 3_000_000 }); + const fetchImpl = (async () => { + throw new Error("no fetch expected"); + }) as unknown as typeof fetch; + const t = await getAccessToken({ file, fetchImpl, now: () => NOW }); + expect(t.accessToken).toBe("synthetic-access-1"); + }); + + it("clears tokens and asks for sign-in on invalid_grant", async () => { + const { file } = await tokenFixture(); + await expect( + getAccessToken({ + file, + now: () => NOW, + fetchImpl: (async () => jsonResponse({ error: "invalid_grant" }, 400)) as unknown as typeof fetch, + }), + ).rejects.toSatisfy((e: unknown) => e instanceof ChatGptAuthError && e.kind === "reauth_required"); + const saved = JSON.parse(await readFile(file, "utf8")); + expect(saved.reauth_required).toBe(true); + expect(saved.refresh_token).toBe(""); + expect(saved.client_id).toBe("oaiapp_test"); // kept for re-sign-in + }); + + it("leaves the token file untouched on a transient refresh error", async () => { + const { file } = await tokenFixture(); + const before = await readFile(file, "utf8"); + await expect( + getAccessToken({ + file, + now: () => NOW, + fetchImpl: (async () => jsonResponse({}, 502)) as unknown as typeof fetch, + }), + ).rejects.toSatisfy((e: unknown) => e instanceof ChatGptAuthError && e.kind === "transient"); + expect(await readFile(file, "utf8")).toBe(before); + }); + + it("reports not_signed_in when the token file is missing", async () => { + await expect( + getAccessToken({ file: join(tmpdir(), "memos-chatgpt-missing-token-file.json") }), + ).rejects.toSatisfy((e: unknown) => e instanceof ChatGptAuthError && e.kind === "not_signed_in"); + }); + + it("rejects a grant without the plan scope", async () => { + const { file } = await tokenFixture({ scopes: ["openid", "offline_access"], expires_at: NOW + 3_000_000 }); + await expect(getAccessToken({ file, now: () => NOW })).rejects.toSatisfy( + (e: unknown) => e instanceof ChatGptAuthError && e.kind === "missing_scope", + ); + }); +}); + +describe("chatgpt-auth host ID", () => { + it("creates a urn:uuid v4 once, persists it, reuses it, and replaces an invalid legacy value", async () => { + const dir = await mkdtemp(join(tmpdir(), "memos-chatgpt-hostid-")); + dirs.push(dir); + const file = join(dir, "chatgpt-oauth.json"); + const a = await getOrCreateHostId(file); + expect(a).toMatch(/^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); + expect((await readFile(hostIdFile(file), "utf8")).trim()).toBe(a); + expect(await getOrCreateHostId(file)).toBe(a); + expect(await getOrCreateHostId(file, "memos-local-old-format")).toBe(a); + const given = "urn:uuid:22222222-2222-4222-8222-222222222222"; + expect(await getOrCreateHostId(file, given)).toBe(given); + }); + + it("accepts only the documented opaque formats", () => { + expect(isValidHostId("did:key:z6Mk")).toBe(true); + expect(isValidHostId("urn:ietf:params:oauth:jwk-thumbprint:sha-256:abc")).toBe(true); + expect(isValidHostId("someone@example.com")).toBe(false); + expect(isValidHostId(undefined)).toBe(false); + }); +}); diff --git a/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts b/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts new file mode 100644 index 000000000..caeafaa87 --- /dev/null +++ b/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts @@ -0,0 +1,317 @@ +/** + * `chatgpt` provider (Sign in with ChatGPT, Responses API). + * + * Mocked fetch + mocked SSE streams only: no network, no real tokens. + */ + +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { Value } from "@sinclair/typebox/value"; +import { beforeEach, describe, expect, it } from "vitest"; + +import { ConfigSchema } from "../../../core/config/schema.js"; +import { createLlmClientWithProvider, makeProviderFor } from "../../../core/llm/client.js"; +import { + ChatGptLlmProvider, + PAUSE_MS, + resetChatGptPauseState, +} from "../../../core/llm/providers/chatgpt.js"; +import type { AccessToken, TokenOptions } from "../../../core/llm/chatgpt-auth.js"; +import type { + ChatGptProviderConfig, + LlmConfig, + LlmMessage, + LlmProvider, + LlmProviderCtx, + LlmProviderLogger, + LlmStreamChunk, + ProviderCallInput, +} from "../../../core/llm/types.js"; + +const noopLog: LlmProviderLogger = { + trace: () => {}, + debug: () => {}, + info: () => {}, + warn: () => {}, + error: () => {}, +}; + +type SseEvent = { type: string } & Record; + +function sse(events: SseEvent[]): string { + return events.map((e) => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(""); +} +function streamResponse(text: string, status = 200): Response { + return new Response(text, { status, headers: { "Content-Type": "text/event-stream" } }); +} +function jsonResponse(obj: unknown, status: number): Response { + return new Response(JSON.stringify(obj), { status, headers: { "Content-Type": "application/json" } }); +} +function okEvents( + parts: string[], + usage = { input_tokens: 7, output_tokens: 3, total_tokens: 10 }, +): SseEvent[] { + return [ + { type: "response.created", response: { status: "in_progress" } }, + ...parts.map((delta) => ({ type: "response.output_text.delta", delta })), + { type: "response.completed", response: { status: "completed", usage } }, + ]; +} + +function makeFallback() { + const calls = { complete: 0, stream: 0 }; + const provider: LlmProvider = { + name: "openai_compatible", + async complete() { + calls.complete++; + return { text: "from-fallback", finishReason: "stop", durationMs: 1 }; + }, + async *stream() { + calls.stream++; + yield { delta: "from-fallback", done: false }; + yield { delta: "", done: true }; + }, + }; + return { calls, provider }; +} + +function baseConfig(chatgpt: ChatGptProviderConfig): LlmConfig { + return { + provider: "chatgpt", + model: "openai/fallback-model", + temperature: 0, + fallbackToHost: false, + timeoutMs: 5_000, + maxRetries: 0, + openRouter: true, + chatgpt, + }; +} + +type QueuedResponse = Response | (() => Response); + +function setup({ + responses, + chatgpt = {}, + token = "tok-A", +}: { responses: QueuedResponse[]; chatgpt?: ChatGptProviderConfig; token?: string }) { + const requests: Array<{ url: string; init: RequestInit; body: Record }> = []; + const tokenCalls: TokenOptions[] = []; + let clock = 1_000_000; + const queue = [...responses]; + const fb = makeFallback(); + const provider = new ChatGptLlmProvider({ + fetchImpl: (async (url: string, init: RequestInit) => { + requests.push({ url, init, body: JSON.parse(String(init.body)) }); + const next = queue.shift(); + if (!next) throw new Error("unexpected extra fetch"); + return typeof next === "function" ? next() : next; + }) as unknown as typeof fetch, + getToken: async (o: TokenOptions): Promise => { + tokenCalls.push(o); + return { accessToken: o.force ? "tok-B" : token, expiresAt: clock + 3_600_000 }; + }, + now: () => clock, + sleep: async () => {}, + fallback: fb.provider, + }); + const ctx = { config: baseConfig(chatgpt), log: noopLog } as LlmProviderCtx; + return { + provider, + ctx, + requests, + tokenCalls, + fb, + advance: (ms: number) => { + clock += ms; + }, + }; +} + +const call: ProviderCallInput = { temperature: 0, maxTokens: 512, jsonMode: false }; +const msgs: LlmMessage[] = [ + { role: "system", content: "SYS" }, + { role: "user", content: "hello" }, +]; +const headers = (init: RequestInit) => init.headers as Record; + +async function collect(it: AsyncIterable): Promise { + const out: LlmStreamChunk[] = []; + for await (const c of it) out.push(c); + return out; +} + +describe("ChatGptLlmProvider", () => { + beforeEach(() => resetChatGptPauseState()); + + it("streams deltas, completes only on response.completed, and sends the required body", async () => { + const s = setup({ responses: [streamResponse(sse(okEvents(["Hel", "lo"])))], chatgpt: { model: "m-test" } }); + const out = await s.provider.complete(msgs, call, s.ctx); + expect(out.text).toBe("Hello"); + expect(out.usage).toEqual({ promptTokens: 7, completionTokens: 3, totalTokens: 10 }); + const r = s.requests[0]!; + expect(r.url).toBe("https://api.openai.com/v1/responses"); + expect(headers(r.init).Authorization).toBe("Bearer tok-A"); + expect(r.body.store).toBe(false); + expect(r.body.stream).toBe(true); + expect(r.body.model).toBe("m-test"); + expect(r.body.instructions).toBe("SYS"); + expect(r.body.input).toEqual([{ role: "user", content: "hello" }]); + for (const banned of ["temperature", "max_output_tokens", "text"]) expect(r.body).not.toHaveProperty(banned); + expect(s.fb.calls.complete).toBe(0); + }); + + it("falls back and pauses ChatGPT when the usage limit arrives mid-stream as response.failed", async () => { + const failed: SseEvent[] = [ + { type: "response.output_text.delta", delta: "partial" }, + { + type: "response.failed", + response: { status: "failed", error: { code: "subscription_sharing_usage_limit_exceeded" } }, + }, + ]; + const s = setup({ responses: [streamResponse(sse(failed))] }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + expect(s.fb.calls.complete).toBe(1); + // Inside the pause window: straight to the fallback, no ChatGPT fetch. + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + expect(s.requests).toHaveLength(1); + // After the pause ChatGPT is tried again (empty queue → fetch throws → retries → fallback). + s.advance(PAUSE_MS.usageLimit + 1); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + expect(s.requests.length).toBeGreaterThan(1); + }); + + it("throws llm_rate_limited for an HTTP 429 usage limit when fallback is disabled", async () => { + const s = setup({ + responses: [jsonResponse({ error: { code: "subscription_sharing_usage_limit_exceeded" } }, 429)], + chatgpt: { fallback: false }, + }); + await expect(s.provider.complete(msgs, call, s.ctx)).rejects.toMatchObject({ + code: "llm_rate_limited", + message: expect.stringMatching(/usage_limit/), + }); + expect(s.fb.calls.complete).toBe(0); + }); + + it("treats a stream that ends without response.completed as a failure", async () => { + const s = setup({ responses: [streamResponse(sse([{ type: "response.output_text.delta", delta: "cut" }]))] }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + }); + + it("treats response.incomplete as a failure", async () => { + const s = setup({ + responses: [ + streamResponse( + sse([{ type: "response.incomplete", response: { incomplete_details: { reason: "content_filter" } } }]), + ), + ], + }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + }); + + it("refreshes once after a 401 and retries with the new token", async () => { + const s = setup({ + responses: [jsonResponse({ error: { code: "invalid_token" } }, 401), streamResponse(sse(okEvents(["ok"])))], + }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("ok"); + expect(s.tokenCalls).toHaveLength(2); + expect(s.tokenCalls[1]).toMatchObject({ force: true, staleAccessToken: "tok-A" }); + expect(headers(s.requests[1]!.init).Authorization).toBe("Bearer tok-B"); + }); + + it("retries a 503 with bounded backoff, then falls back", async () => { + const busy = () => jsonResponse({ error: { code: "subscription_sharing_usage_unavailable" } }, 503); + const s = setup({ responses: [busy, busy, busy] }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + expect(s.requests).toHaveLength(3); + }); + + it("asks for json_object in JSON mode and drops text.format once if the route rejects it", async () => { + const s = setup({ + responses: [ + jsonResponse({ error: { code: "subscription_sharing_unsupported_capability", param: "text.format" } }, 400), + streamResponse(sse(okEvents(['{"a":', "1}"]))), + ], + }); + const out = await s.provider.complete(msgs, { ...call, jsonMode: true }, s.ctx); + expect(out.text).toBe('{"a":1}'); + expect(s.requests[0]!.body.text).toEqual({ format: { type: "json_object" } }); + expect(s.requests[1]!.body).not.toHaveProperty("text"); + }); + + it("works through the real LlmClient facade (completeJson)", async () => { + const s = setup({ responses: [streamResponse(sse(okEvents(['{"score":', " 0.5}"])))] }); + const client = createLlmClientWithProvider(s.ctx.config, s.provider); + const res = await client.completeJson<{ score: number }>([{ role: "user", content: "rate this" }], { + op: "test.json", + }); + expect(res.value).toEqual({ score: 0.5 }); + expect(res.servedBy).toBe("chatgpt"); + // The facade's JSON hint must arrive as instructions, not as a system input item. + const body = s.requests[0]!.body as { instructions: string; input: Array<{ role: string }> }; + expect(body.instructions.length).toBeGreaterThan(0); + expect(body.input.every((i) => i.role !== "system")).toBe(true); + }); + + it("stream() yields ChatGPT deltas, and falls back when nothing was emitted yet", async () => { + const ok = setup({ responses: [streamResponse(sse(okEvents(["a", "b"])))] }); + const got = await collect(ok.provider.stream(msgs, call, ok.ctx)); + expect(got.filter((c) => !c.done).map((c) => c.delta).join("")).toBe("ab"); + expect(got.at(-1)?.done).toBe(true); + + const bad = setup({ + responses: [jsonResponse({ error: { code: "subscription_sharing_user_not_eligible" } }, 403)], + }); + const got2 = await collect(bad.provider.stream(msgs, call, bad.ctx)); + expect(got2[0]?.delta).toBe("from-fallback"); + expect(bad.fb.calls.stream).toBe(1); + }); + + it("falls back when no token file exists (not signed in)", async () => { + const fb = makeFallback(); + const p = new ChatGptLlmProvider({ + fallback: fb.provider, + fetchImpl: (async () => { + throw new Error("no fetch expected"); + }) as unknown as typeof fetch, + }); + const ctx = { + config: { + ...baseConfig({ tokenFile: join(tmpdir(), "memos-chatgpt-does-not-exist.json") }), + openRouter: false, + }, + log: noopLog, + } as LlmProviderCtx; + expect((await p.complete(msgs, call, ctx)).text).toBe("from-fallback"); + expect(fb.calls.complete).toBe(1); + }); +}); + +describe("chatgpt provider registration", () => { + it("makeProviderFor('chatgpt') returns the new provider; existing names are unchanged", () => { + expect(makeProviderFor("chatgpt").name).toBe("chatgpt"); + expect(makeProviderFor("openai_compatible").name).toBe("openai_compatible"); + }); + + it("config schema accepts llm.provider chatgpt with an llm.chatgpt block", () => { + const cfg = Value.Default(ConfigSchema, { + llm: { + provider: "chatgpt", + endpoint: "https://openrouter.ai/api/v1", + model: "x", + chatgpt: { model: "m", fallback: true }, + }, + }); + expect([...Value.Errors(ConfigSchema, cfg)].map((e) => `${e.path}: ${e.message}`)).toEqual([]); + }); + + it("config schema accepts skillEvolver.provider chatgpt; unknown providers still fail", () => { + const ok = Value.Default(ConfigSchema, { + skillEvolver: { provider: "chatgpt", model: "openai/x", chatgpt: { model: "x", fallback: true } }, + }); + expect([...Value.Errors(ConfigSchema, ok)].map((e) => `${e.path}: ${e.message}`)).toEqual([]); + const bad = Value.Default(ConfigSchema, { skillEvolver: { provider: "not_a_provider", model: "x" } }); + expect([...Value.Errors(ConfigSchema, bad)].some((e) => e.path.startsWith("/skillEvolver/provider"))).toBe(true); + }); +}); diff --git a/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts b/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts index fd62c903d..e8a58ee25 100644 --- a/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts +++ b/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts @@ -174,4 +174,66 @@ l3Llm: expect(capturedLlmConfigs.find((cfg) => cfg.model === "skill-model")?.openRouter).toBe(false); expect(capturedLlmConfigs.find((cfg) => cfg.model === "l3-model")?.openRouter).toBe(false); }); + + it("passes skillEvolver.chatgpt through and inherits the main llm endpoint/key for its fallback", async () => { + const { bootstrapMemoryCore } = await import("../../../core/pipeline/memory-core.js"); + home = await makeTmpHome({ + agent: "openclaw", + configYaml: ` +llm: + provider: openai_compatible + endpoint: https://openrouter.ai/api/v1 + apiKey: sk-main + model: main +skillEvolver: + provider: chatgpt + model: skill-fallback-model + chatgpt: + model: chatgpt-model + fallback: true +`, + }); + + core = await bootstrapMemoryCore({ + agent: "openclaw", + home: home.home, + config: home.config, + pkgVersion: "bootstrap-test", + }); + + expect(capturedLlmConfigs.find((cfg) => cfg.model === "skill-fallback-model")).toMatchObject({ + provider: "chatgpt", + endpoint: "https://openrouter.ai/api/v1", + apiKey: "sk-main", + chatgpt: { model: "chatgpt-model", fallback: true }, + }); + }); + + it("does not inherit the main llm endpoint/key for non-chatgpt skill evolvers", async () => { + const { bootstrapMemoryCore } = await import("../../../core/pipeline/memory-core.js"); + home = await makeTmpHome({ + agent: "openclaw", + configYaml: ` +llm: + provider: openai_compatible + endpoint: https://openrouter.ai/api/v1 + apiKey: sk-main + model: main +skillEvolver: + provider: openai_compatible + model: skill-model +`, + }); + + core = await bootstrapMemoryCore({ + agent: "openclaw", + home: home.home, + config: home.config, + pkgVersion: "bootstrap-test", + }); + + const evolver = capturedLlmConfigs.find((cfg) => cfg.model === "skill-model"); + expect(evolver?.endpoint).toBe(""); + expect(evolver?.apiKey).toBe(""); + }); }); diff --git a/apps/memos-local-plugin/viewer/src/stores/i18n.ts b/apps/memos-local-plugin/viewer/src/stores/i18n.ts index cf5564050..e6ca1bab8 100644 --- a/apps/memos-local-plugin/viewer/src/stores/i18n.ts +++ b/apps/memos-local-plugin/viewer/src/stores/i18n.ts @@ -920,6 +920,8 @@ const en = { "Model that turns your conversations into short task summaries and the takeaways the agent keeps.", "settings.summarizer.inherit": "Currently using the agent's model. Select a provider to override.", + "settings.chatgpt.hint": + "Runs on your ChatGPT plan. Sign in once with `node scripts/chatgpt-login.mjs`. The model, endpoint and API key below are the OpenAI-compatible fallback; set the ChatGPT model under `chatgpt.model` in config.yaml.", "settings.model.tip.title": "Model selection tips", "settings.model.tip.embedding": "Embedding — the built-in model is small. For better recall, configure a dedicated model such as bge-m3 or text-embedding-3-large.", @@ -1797,6 +1799,8 @@ const zh: Record = { "settings.summarizer.desc": "把原始对话压缩成任务摘要和要点的模型。", "settings.summarizer.inherit": "当前使用 agent 的模型。选择 Provider 即可覆盖。", + "settings.chatgpt.hint": + "使用你的 ChatGPT 套餐运行。先运行一次 `node scripts/chatgpt-login.mjs` 登录。下方的模型、Endpoint 和 API Key 是 OpenAI 兼容的回退设置;ChatGPT 模型请在 config.yaml 的 `chatgpt.model` 中设置。", "settings.model.tip.title": "模型选择提示", "settings.model.tip.embedding": "嵌入模型:插件内置模型较小。为获得更精准的记忆检索,建议配置 bge-m3、text-embedding-3-large 等专业嵌入模型。", diff --git a/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx b/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx index 43d2efb1a..d86684605 100644 --- a/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx +++ b/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx @@ -110,6 +110,7 @@ const LLM_PROVIDERS = [ "openai_compatible", "gemini", "anthropic", + "chatgpt", ]; const SKILL_PROVIDERS = [ @@ -117,6 +118,7 @@ const SKILL_PROVIDERS = [ "openai_compatible", "gemini", "anthropic", + "chatgpt", ]; export function SettingsView({ initialTab }: { initialTab?: Tab } = {}) { @@ -473,6 +475,13 @@ function ModelCard({ {inheritsLabel} )} + {block.provider === "chatgpt" && ( +
+ {t("settings.chatgpt.hint")} +
+ )} {isLocal && localHint && (
Date: Thu, 12 Nov 2026 02:26:20 +0700 Subject: [PATCH 2/3] fix(plugin): address review findings on the chatgpt provider - auth: tolerate corrupt token files, owner-checked refresh lock, folders chmod 700 (files 600), warn when owner-only access fails, JWKS status + strict kid matching, typed JWK, host-id temp cleanup, never print the ID token in the sign-in URL, keep models without a visibility field - provider: no pause for local sign-in states, no fallback without a fallback target, shared fallback gate, error mapping helper - wiring: l3Llm gets chatgpt + fallback inheritance; inherit the main llm endpoint/key only from OpenAI-compatible providers - schema: ChatGptProviderConfig derived from the schema; reasoningEffort reuses the reasoning effort union - settings: Test checks the ChatGPT sign-in; per-card hint; shared hint style; login script rejects unknown args and exits 1 without the plan scope - tests for each behaviour change Co-Authored-By: Claude Opus 5.5 --- apps/memos-local-plugin/core/config/schema.ts | 29 +++-- .../core/llm/chatgpt-auth.ts | 109 ++++++++++++----- .../core/llm/providers/chatgpt.ts | 69 +++++++---- apps/memos-local-plugin/core/llm/types.ts | 24 ++-- .../core/pipeline/memory-core.ts | 36 ++++-- .../docs/CONFIG-ADVANCED.md | 21 +++- .../scripts/chatgpt-login.mjs | 21 +++- .../server/routes/models.ts | 15 ++- .../tests/unit/llm/chatgpt-auth.test.ts | 110 +++++++++++++++++- .../tests/unit/llm/chatgpt-provider.test.ts | 61 +++++++++- .../pipeline/bootstrap-llm-config.test.ts | 62 ++++++++++ .../viewer/src/stores/i18n.ts | 4 +- .../viewer/src/views/SettingsView.tsx | 23 ++-- 13 files changed, 474 insertions(+), 110 deletions(-) diff --git a/apps/memos-local-plugin/core/config/schema.ts b/apps/memos-local-plugin/core/config/schema.ts index 660230913..503e3f0bd 100644 --- a/apps/memos-local-plugin/core/config/schema.ts +++ b/apps/memos-local-plugin/core/config/schema.ts @@ -75,6 +75,17 @@ const EmbeddingSchema = Type.Object({ }, { default: {} }), }, { default: {} }); +/** Reasoning effort levels accepted by `reasoning.effort` and `chatgpt.reasoningEffort`. */ +const ReasoningEffortSchema = Type.Union([ + Type.Literal("minimal"), + Type.Literal("none"), + Type.Literal("low"), + Type.Literal("medium"), + Type.Literal("high"), + Type.Literal("xhigh"), + Type.Literal("max"), +]); + const ReasoningSchema = Type.Object({ /** * OpenRouter-compatible reasoning toggle. Omit the whole block to keep @@ -82,15 +93,7 @@ const ReasoningSchema = Type.Object({ */ enabled: Type.Optional(Type.Boolean()), /** Optional provider effort hint for reasoning-capable models. */ - effort: Type.Optional(Type.Union([ - Type.Literal("minimal"), - Type.Literal("none"), - Type.Literal("low"), - Type.Literal("medium"), - Type.Literal("high"), - Type.Literal("xhigh"), - Type.Literal("max"), - ])), + effort: Type.Optional(ReasoningEffortSchema), /** Optional token budget for reasoning-capable providers. */ maxTokens: Type.Optional(Type.Number({ minimum: 1 })), }, { default: {} }); @@ -108,8 +111,8 @@ const ChatGptSchema = Type.Object({ fallback: Type.Optional(Type.Boolean()), /** Credential file. Default ~/.config/memos-local/chatgpt-oauth.json. */ tokenFile: Type.Optional(Type.String()), - /** Optional Responses API reasoning effort (e.g. "low"). */ - reasoningEffort: Type.Optional(Type.String()), + /** Optional Responses API reasoning effort (e.g. "low"). Omitted when unset. */ + reasoningEffort: Type.Optional(ReasoningEffortSchema), }); const LlmSchema = Type.Object({ @@ -186,7 +189,8 @@ const SkillEvolverSchema = Type.Object({ headers: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })), /** * Only read when provider = "chatgpt". A blank endpoint / apiKey here - * inherits the main `llm` block's for the openai_compatible fallback. + * inherits the main `llm` block's for the openai_compatible fallback + * when the main provider is openai_compatible or chatgpt. */ chatgpt: Type.Optional(ChatGptSchema), }, { default: {} }); @@ -735,4 +739,5 @@ export const ConfigSchema = Type.Object({ }, { default: {} }); export type ReasoningConfig = Static; +export type ChatGptConfig = Static; export type ResolvedConfig = Static; diff --git a/apps/memos-local-plugin/core/llm/chatgpt-auth.ts b/apps/memos-local-plugin/core/llm/chatgpt-auth.ts index 0d91adf34..5451cd850 100644 --- a/apps/memos-local-plugin/core/llm/chatgpt-auth.ts +++ b/apps/memos-local-plugin/core/llm/chatgpt-auth.ts @@ -16,7 +16,14 @@ * messages carry OAuth error codes only. */ -import { createHash, createPublicKey, randomBytes, randomUUID, verify as cryptoVerify } from "node:crypto"; +import { + createHash, + createPublicKey, + randomBytes, + randomUUID, + verify as cryptoVerify, + type JsonWebKey, +} from "node:crypto"; import { execFile } from "node:child_process"; import { promises as fsp } from "node:fs"; import * as http from "node:http"; @@ -110,15 +117,29 @@ export async function readCredentials(file: string): Promise { +/** + * Atomic owner-only write: temp file in the same folder, then rename. + * Resolves false when owner-only permissions could not be applied. + */ +export async function writeCredentials(file: string, creds: ChatGptCredentials): Promise { const dir = path.dirname(file); await fsp.mkdir(dir, { recursive: true, mode: 0o700 }); const tmp = path.join(dir, `.${path.basename(file)}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`); @@ -135,43 +156,54 @@ export async function writeCredentials(file: string, creds: ChatGptCredentials): await fsp.rm(tmp, { force: true }); throw err; } - await restrictToOwner(file); + return restrictToOwner(file); } /** - * Owner-only permissions. POSIX: chmod 600. Windows: drop inherited ACEs and - * grant only the current user full control (icacls). + * Owner-only permissions. POSIX: chmod 700 for folders, 600 for files. + * Windows: drop inherited ACEs and grant only the current user full + * control (icacls). Best effort: never throws, returns false when the + * permissions could not be applied so callers can warn. */ -export async function restrictToOwner(target: string): Promise { +export async function restrictToOwner(target: string): Promise { + const isDir = (await fsp.stat(target).catch(() => null))?.isDirectory() === true; if (process.platform !== "win32") { - await fsp.chmod(target, 0o600).catch(() => undefined); - return; + return fsp.chmod(target, isDir ? 0o700 : 0o600).then( + () => true, + () => false, + ); } const user = os.userInfo().username; const domain = process.env.USERDOMAIN; const principal = domain ? `${domain}\\${user}` : user; - const isDir = (await fsp.stat(target).catch(() => null))?.isDirectory() === true; const icacls = (args: string[]) => - new Promise((resolve) => { - // best effort; never fail a token write on ACL tooling - execFile("icacls", [target, ...args], { windowsHide: true }, () => resolve()); + new Promise((resolve) => { + execFile("icacls", [target, ...args], { windowsHide: true }, (err) => resolve(!err)); }); // /reset drops explicit ACEs a new file gets from the process token // (SYSTEM, logon session); then keep only the owner. Folders grant the // owner with (OI)(CI) so files created inside inherit owner-only access. - await icacls(["/reset"]); - await icacls(["/inheritance:r", "/grant:r", isDir ? `${principal}:(OI)(CI)F` : `${principal}:F`]); + const reset = await icacls(["/reset"]); + const granted = await icacls([ + "/inheritance:r", + "/grant:r", + isDir ? `${principal}:(OI)(CI)F` : `${principal}:F`, + ]); + return reset && granted; } // ─── Lock (serialise refreshes across MemOS processes) ─────────────────────── async function withFileLock(file: string, fn: () => Promise): Promise { const lock = `${file}.lock`; + // Unique per acquisition, so a holder never removes a lock that a peer + // re-created after treating this one as stale. + const owner = `${process.pid}.${randomBytes(8).toString("hex")}`; const started = Date.now(); for (;;) { try { const h = await fsp.open(lock, "wx", 0o600); - await h.writeFile(String(process.pid)); + await h.writeFile(owner); await h.close(); break; } catch (err) { @@ -194,7 +226,8 @@ async function withFileLock(file: string, fn: () => Promise): Promise { try { return await fn(); } finally { - await fsp.rm(lock, { force: true }).catch(() => undefined); + const current = await fsp.readFile(lock, "utf8").catch(() => null); + if (current === owner) await fsp.rm(lock, { force: true }).catch(() => undefined); } } @@ -347,7 +380,9 @@ export async function listChatGptModels(accessToken: string, fetchImpl: typeof f } const rows = body.models ?? body.data ?? []; return rows - .filter((m) => m.visibility === "list") + // ChatGPT-plan listings mark usable models "list"; a plain OpenAI-style + // listing has no visibility field, so keep those rows too. + .filter((m) => m.visibility === undefined || m.visibility === "list") .map((m) => ({ slug: m.slug ?? m.id ?? "", displayName: m.display_name })) .filter((m) => m.slug.length > 0); } @@ -401,12 +436,15 @@ export async function runLogin(opts: LoginOptions = {}): Promise { ext_agent_host_id: extAgentHostId, }); if (!savedClientId) params.set("agent_name_hint", AGENT_NAME_HINT); + // The printed address never carries the ID token: `id_token_hint` is an + // optional hint, so it only goes into the URL handed to the browser. + const printableUrl = `${CHATGPT_AUTHORIZE_URL}?${params.toString()}`; if (savedClientId && existing?.id_token) params.set("id_token_hint", existing.id_token); const authorizeUrl = `${CHATGPT_AUTHORIZE_URL}?${params.toString()}`; print("Opening your browser to sign in with ChatGPT."); print("If it does not open, paste this address into your browser:"); - print(authorizeUrl); + print(printableUrl); (opts.openBrowser ?? openInBrowser)(authorizeUrl); let callback: URLSearchParams; @@ -482,8 +520,11 @@ export async function runLogin(opts: LoginOptions = {}): Promise { saved_at: new Date().toISOString(), }; await fsp.mkdir(path.dirname(file), { recursive: true, mode: 0o700 }); - await restrictToOwner(path.dirname(file)); - await writeCredentials(file, creds); + const dirRestricted = await restrictToOwner(path.dirname(file)); + const fileRestricted = await writeCredentials(file, creds); + if (!dirRestricted || !fileRestricted) { + print(`Warning: could not restrict ${file} to your user account; check its permissions.`); + } return { email, subject, scopes, expiresAt: creds.expires_at, file, newRegistration: !savedClientId }; } @@ -543,7 +584,12 @@ export async function getOrCreateHostId(tokenFile: string, fromCredentials?: str await restrictToOwner(dir); const tmp = path.join(dir, `.host-id.${process.pid}.${randomBytes(4).toString("hex")}.tmp`); await fsp.writeFile(tmp, id, { encoding: "utf8", mode: 0o600, flag: "wx" }); - await fsp.rename(tmp, f); + try { + await fsp.rename(tmp, f); + } catch (err) { + await fsp.rm(tmp, { force: true }); + throw err; + } await restrictToOwner(f); return id; } @@ -620,11 +666,18 @@ export async function verifyIdToken( const claims = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8")) as IdTokenClaims; if (header.alg !== "RS256") throw new Error(`id_token alg ${header.alg} not accepted`); const jwksResp = await fetchImpl(CHATGPT_JWKS_URL, { signal: AbortSignal.timeout(30_000) }); - const jwks = (await jwksResp.json()) as { keys?: Array> }; - const keys = jwks.keys ?? []; - const jwk = keys.find((k) => k.kid === header.kid) ?? (keys.length === 1 ? keys[0] : undefined); + if (!jwksResp.ok) throw new Error(`id_token JWKS fetch failed: HTTP ${jwksResp.status}`); + const jwks = (await jwksResp.json().catch(() => ({}))) as { keys?: JsonWebKey[] }; + const keys = Array.isArray(jwks.keys) ? jwks.keys : []; + let jwk: JsonWebKey | undefined; + if (header.kid) { + jwk = keys.find((k) => k.kid === header.kid); + } else if (keys.length === 1) { + // A single key is only accepted when the token names no key. + jwk = keys[0]; + } if (!jwk) throw new Error("id_token signing key not found in JWKS"); - const key = createPublicKey({ key: jwk as never, format: "jwk" }); + const key = createPublicKey({ key: jwk, format: "jwk" }); const ok = cryptoVerify( "RSA-SHA256", Buffer.from(`${parts[0]}.${parts[1]}`), diff --git a/apps/memos-local-plugin/core/llm/providers/chatgpt.ts b/apps/memos-local-plugin/core/llm/providers/chatgpt.ts index 543a87cad..c2ebae06a 100644 --- a/apps/memos-local-plugin/core/llm/providers/chatgpt.ts +++ b/apps/memos-local-plugin/core/llm/providers/chatgpt.ts @@ -23,7 +23,7 @@ * reasoningEffort: low # omitted unless set */ -import { ERROR_CODES, MemosError } from "../../../agent-contract/errors.js"; +import { ERROR_CODES, MemosError, type ErrorCode } from "../../../agent-contract/errors.js"; import { ChatGptAuthError, CHATGPT_API_BASE, getAccessToken, type TokenOptions, type AccessToken } from "../chatgpt-auth.js"; import { decodeSse } from "../fetcher.js"; import { OpenAiLlmProvider } from "./openai.js"; @@ -268,23 +268,42 @@ export class ChatGptLlmProvider implements LlmProvider { if (ms > 0) pauseState.set(pauseKey(cg), { until: this.now() + ms, reason: failureCode(err) }); } - private async fallbackComplete( - messages: LlmMessage[], - opts: ProviderCallInput, + /** + * Shared gate for both fallback paths: records a pause for the failure, + * then throws the ChatGPT error when fallback is off, the call was + * cancelled, or the block has no fallback target (no apiKey and no + * endpoint), so the caller sees the actionable ChatGPT reason instead of + * the fallback provider's configuration error. + */ + private beginFallback( ctx: LlmProviderCtx, cg: ChatGptProviderConfig, pausedReason: string | null, err: unknown, - ): Promise { + stream: boolean, + ): void { if (err) this.notePause(err, cg); - if (cg.fallback === false || ctx.signal?.aborted) { + const hasTarget = Boolean(ctx.config.apiKey) || Boolean(ctx.config.endpoint); + if (cg.fallback === false || ctx.signal?.aborted || !hasTarget) { throw this.toMemosError(err ?? pausedErr(pausedReason), cg, ctx); } ctx.log.warn("chatgpt.fallback", { to: "openai_compatible", reason: pausedReason ? `paused:${pausedReason}` : failureCode(err), status: err instanceof ChatGptFailure ? err.status : undefined, + stream, }); + } + + private async fallbackComplete( + messages: LlmMessage[], + opts: ProviderCallInput, + ctx: LlmProviderCtx, + cg: ChatGptProviderConfig, + pausedReason: string | null, + err: unknown, + ): Promise { + this.beginFallback(ctx, cg, pausedReason, err, false); return this.fallbackProvider.complete(messages, opts, ctx); } @@ -296,15 +315,7 @@ export class ChatGptLlmProvider implements LlmProvider { pausedReason: string | null, err: unknown, ): AsyncGenerator { - if (err) this.notePause(err, cg); - if (cg.fallback === false || ctx.signal?.aborted) { - throw this.toMemosError(err ?? pausedErr(pausedReason), cg, ctx); - } - ctx.log.warn("chatgpt.fallback", { - to: "openai_compatible", - reason: pausedReason ? `paused:${pausedReason}` : failureCode(err), - stream: true, - }); + this.beginFallback(ctx, cg, pausedReason, err, true); if (typeof this.fallbackProvider.stream === "function") { yield* this.fallbackProvider.stream(messages, opts, ctx); return; @@ -318,12 +329,7 @@ export class ChatGptLlmProvider implements LlmProvider { if (err instanceof MemosError) return err; const code = failureCode(err); const status = err instanceof ChatGptFailure ? err.status : null; - const memosCode = - code === "subscription_sharing_usage_limit_exceeded" || status === 429 - ? ERROR_CODES.LLM_RATE_LIMITED - : code === "timeout" || code === "cancelled" - ? ERROR_CODES.LLM_TIMEOUT - : ERROR_CODES.LLM_UNAVAILABLE; + const memosCode = memosCodeFor(code, status); ctx.log.warn("chatgpt.failed", { code, status }); return new MemosError(memosCode, `chatgpt: ${code}${status ? ` (HTTP ${status})` : ""}`, { provider: "chatgpt", @@ -436,9 +442,28 @@ function failureCode(err: unknown): string { return (err as Error)?.name ?? "unknown_error"; } +/** Maps a ChatGPT failure code / HTTP status to the facade's error code. */ +function memosCodeFor(code: string, status: number | null): ErrorCode { + if (code === "subscription_sharing_usage_limit_exceeded" || status === 429) { + return ERROR_CODES.LLM_RATE_LIMITED; + } + if (code === "timeout" || code === "cancelled") return ERROR_CODES.LLM_TIMEOUT; + return ERROR_CODES.LLM_UNAVAILABLE; +} + +/** + * Auth problems found by reading the local token file (not signed in, plan + * scope missing, sign-in expired) are not paused: the check is cheap and + * the user fixes them by signing in, which should take effect at once. + * Transient refresh failures and lock timeouts are retryable, so they are + * not paused either. Only an auth rejection from the server + * (invalid_client, HTTP 401/403) pauses ChatGPT. + */ +const UNPAUSED_AUTH_KINDS = new Set(["not_signed_in", "missing_scope", "reauth_required", "transient", "lock_timeout"]); + function pauseFor(err: unknown): number { if (err instanceof ChatGptAuthError) { - return err.kind === "transient" || err.kind === "lock_timeout" ? 0 : PAUSE_MS.auth; + return UNPAUSED_AUTH_KINDS.has(err.kind) ? 0 : PAUSE_MS.auth; } if (err instanceof ChatGptFailure) { if (err.code === "subscription_sharing_usage_limit_exceeded") return PAUSE_MS.usageLimit; diff --git a/apps/memos-local-plugin/core/llm/types.ts b/apps/memos-local-plugin/core/llm/types.ts index 9aeca574e..c6dab5caa 100644 --- a/apps/memos-local-plugin/core/llm/types.ts +++ b/apps/memos-local-plugin/core/llm/types.ts @@ -5,7 +5,10 @@ * Providers stay internal to `core/llm/`. */ -import type { ReasoningConfig as ConfigReasoningConfig } from "../config/schema.js"; +import type { + ChatGptConfig as ConfigChatGptConfig, + ReasoningConfig as ConfigReasoningConfig, +} from "../config/schema.js"; import type { RetryDiagnosticDetails } from "../util/retry-after.js"; // ─── Providers & config ────────────────────────────────────────────────────── @@ -22,21 +25,12 @@ export type LlmProviderName = export type ReasoningConfig = ConfigReasoningConfig; /** - * Settings for the `chatgpt` provider (`llm.chatgpt`, `skillEvolver.chatgpt`). - * The provider runs calls on the user's ChatGPT plan via "Sign in with - * ChatGPT"; `endpoint` / `apiKey` / `model` in the same block stay the - * openai_compatible fallback. + * Settings for the `chatgpt` provider (`llm.chatgpt`, `skillEvolver.chatgpt`, + * `l3Llm.chatgpt`). The provider runs calls on the user's ChatGPT plan via + * "Sign in with ChatGPT"; `endpoint` / `apiKey` / `model` in the same block + * stay the openai_compatible fallback. Derived from `ChatGptSchema`. */ -export interface ChatGptProviderConfig { - /** Model slug from GET /v1/models (visibility "list"). */ - model?: string; - /** Fall back to the openai_compatible settings in the same block. Default true. */ - fallback?: boolean; - /** Credential file. Default ~/.config/memos-local/chatgpt-oauth.json. */ - tokenFile?: string; - /** Optional Responses API reasoning effort. Omitted when unset. */ - reasoningEffort?: string; -} +export type ChatGptProviderConfig = ConfigChatGptConfig; /** * Resolved LLM config, post-defaults. Subset of `ResolvedConfig.llm` so diff --git a/apps/memos-local-plugin/core/pipeline/memory-core.ts b/apps/memos-local-plugin/core/pipeline/memory-core.ts index a219fe8a5..6b39d0b55 100644 --- a/apps/memos-local-plugin/core/pipeline/memory-core.ts +++ b/apps/memos-local-plugin/core/pipeline/memory-core.ts @@ -151,6 +151,27 @@ type DedicatedLlmConfig = { chatgpt?: ChatGptProviderConfig; }; +/** + * Endpoint / apiKey for a dedicated LLM slot (skillEvolver, l3Llm). A + * `chatgpt` slot with a blank endpoint or apiKey reuses the main `llm` + * block's for its OpenAI-compatible fallback, but only when the main block + * is itself OpenAI-compatible (`openai_compatible` or `chatgpt`), so a key + * meant for another provider is never sent to an OpenAI-compatible endpoint. + */ +function dedicatedFallbackTarget( + slot: DedicatedLlmConfig | undefined, + provider: string, + mainLlm: ResolvedConfig["llm"], +): { endpoint: string; apiKey: string } { + const inherit = + provider === "chatgpt" && + (mainLlm.provider === "openai_compatible" || mainLlm.provider === "chatgpt"); + return { + endpoint: slot?.endpoint || (inherit ? mainLlm.endpoint : ""), + apiKey: slot?.apiKey || (inherit ? mainLlm.apiKey : ""), + }; +} + export interface BootstrapOptions { agent: AgentKind; namespace?: RuntimeNamespace; @@ -442,15 +463,12 @@ export async function bootstrapMemoryCoreFull( const evolverModel = (evolver?.model ?? "").trim(); const evolverProvider = (evolver?.provider ?? "").trim(); if (evolverModel && evolverProvider) { - // A chatgpt skill evolver with no endpoint/apiKey of its own uses the - // main llm block's for its openai_compatible fallback. - const mainLlm = (config as { llm?: { endpoint?: string; apiKey?: string } }).llm; - const inheritMain = evolverProvider === "chatgpt"; + const evolverTarget = dedicatedFallbackTarget(evolver, evolverProvider, config.llm); reflectLlm = createLlmClient({ provider: evolverProvider, model: evolverModel, - endpoint: evolver?.endpoint || (inheritMain ? mainLlm?.endpoint ?? "" : ""), - apiKey: evolver?.apiKey || (inheritMain ? mainLlm?.apiKey ?? "" : ""), + endpoint: evolverTarget.endpoint, + apiKey: evolverTarget.apiKey, temperature: evolver?.temperature ?? 0, timeoutMs: evolver?.timeoutMs ?? 60_000, providerIgnore: evolver?.providerIgnore, @@ -511,11 +529,12 @@ export async function bootstrapMemoryCoreFull( const l3Model = (l3c?.model ?? "").trim(); const l3Provider = (l3c?.provider ?? "").trim(); if (l3Model && l3Provider) { + const l3Target = dedicatedFallbackTarget(l3c, l3Provider, config.llm); l3Llm = createLlmClient({ provider: l3Provider, model: l3Model, - endpoint: l3c?.endpoint ?? "", - apiKey: l3c?.apiKey ?? "", + endpoint: l3Target.endpoint, + apiKey: l3Target.apiKey, temperature: l3c?.temperature ?? 0, timeoutMs: l3c?.timeoutMs ?? 60_000, providerIgnore: l3c?.providerIgnore, @@ -524,6 +543,7 @@ export async function bootstrapMemoryCoreFull( reasoning: l3c?.reasoning, maxTokens: l3c?.maxTokens, headers: l3c?.headers, + chatgpt: l3c?.chatgpt, maxRetries: 3, fallbackToHost: true, onError: (d: { provider: string; model: string; message: string; code?: string; at?: number }) => diff --git a/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md b/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md index 4014d2dbf..d2b1c06ca 100644 --- a/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md +++ b/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md @@ -78,12 +78,21 @@ llm: # reasoningEffort: low ``` -`skillEvolver` accepts the same `provider: chatgpt` and `chatgpt:` block. A -chatgpt skill evolver with a blank `endpoint` / `apiKey` reuses the main -`llm` block's for its fallback. The Responses route ignores `temperature` -and `maxTokens`. After a usage-limit error ChatGPT is skipped for 30 minutes, -after "not eligible" for 6 hours, and after an auth failure for 10 minutes; -those calls go straight to the fallback. +`skillEvolver` and `l3Llm` accept the same `provider: chatgpt` and +`chatgpt:` block (their `model` must be non-empty, as for any provider). A +chatgpt `skillEvolver` / `l3Llm` with a blank `endpoint` / `apiKey` reuses +the main `llm` block's for its fallback, but only when the main block is +`openai_compatible` or `chatgpt`, so another provider's key is never sent to +an OpenAI-compatible endpoint. With no fallback target at all (no `apiKey` +and no `endpoint`), ChatGPT errors are reported as they are instead of +falling back. + +The Responses route ignores `temperature` and `maxTokens`. After a +usage-limit error ChatGPT is skipped for 30 minutes, after "not eligible" +for 6 hours, and after the server rejects the sign-in for 10 minutes; those +calls go straight to the fallback. "Not signed in" is not paused, so signing +in takes effect on the next call. The Settings **Test** button checks the +sign-in (and refreshes the token if needed) without spending plan usage. ### `storage` ```yaml diff --git a/apps/memos-local-plugin/scripts/chatgpt-login.mjs b/apps/memos-local-plugin/scripts/chatgpt-login.mjs index 3404aa839..ca79e5195 100644 --- a/apps/memos-local-plugin/scripts/chatgpt-login.mjs +++ b/apps/memos-local-plugin/scripts/chatgpt-login.mjs @@ -13,14 +13,24 @@ import { describeCredentials, getAccessToken, listChatGptModels, + REQUIRED_PLAN_SCOPE, runLogin, } from "../dist/core/llm/chatgpt-auth.js"; const args = process.argv.slice(2); +const KNOWN_FLAGS = new Set(["--status", "--models"]); +// Reject typos up front: an unknown flag must not fall through to an +// interactive sign-in that writes the default token file. +const unknown = args.filter((a) => !KNOWN_FLAGS.has(a) && !a.startsWith("--file=")); const fileArg = args.find((a) => a.startsWith("--file=")); -const file = fileArg ? fileArg.slice("--file=".length) : undefined; +const file = fileArg ? fileArg.slice("--file=".length).trim() || undefined : undefined; async function main() { + if (unknown.length > 0) { + throw new Error( + `unknown argument(s): ${unknown.join(" ")} (use --status, --models or --file=)`, + ); + } if (args.includes("--status")) { console.log(JSON.stringify(await describeCredentials(file), null, 2)); return; @@ -36,10 +46,17 @@ async function main() { console.log(""); console.log("Signed in to ChatGPT for MemOS."); console.log(` account: ${res.email ?? "(no email claim)"}`); - console.log(` plan scope: ${res.scopes.includes("chatgpt.tokens.use.direct") ? "granted" : "MISSING"}`); + const planScope = res.scopes.includes(REQUIRED_PLAN_SCOPE); + console.log(` plan scope: ${planScope ? "granted" : "MISSING"}`); console.log(` token file: ${res.file}`); console.log(` access expires: ${new Date(res.expiresAt).toISOString()} (refreshes automatically)`); console.log(` registration: ${res.newRegistration ? "new" : "reused"}`); + if (!planScope) { + console.error( + "chatgpt-login: the grant lacks the ChatGPT plan scope, so MemOS cannot use it. Sign in again and approve access to your plan.", + ); + process.exitCode = 1; + } } main().catch((err) => { diff --git a/apps/memos-local-plugin/server/routes/models.ts b/apps/memos-local-plugin/server/routes/models.ts index db0f0b543..4728d6380 100644 --- a/apps/memos-local-plugin/server/routes/models.ts +++ b/apps/memos-local-plugin/server/routes/models.ts @@ -21,6 +21,7 @@ */ import { createEmbedder, type Embedder } from "../../core/embedding/index.js"; import type { EmbeddingConfig } from "../../core/embedding/types.js"; +import { getAccessToken } from "../../core/llm/chatgpt-auth.js"; import type { ServerDeps } from "../types.js"; import { parseJson, writeError, type Routes } from "./registry.js"; @@ -31,6 +32,8 @@ interface TestRequest { model?: string; apiKey?: string; local?: EmbeddingConfig["local"]; + /** provider = "chatgpt" only: which credential file to check. */ + chatgpt?: { tokenFile?: string }; } export function registerModelsRoutes(routes: Routes, deps: ServerDeps): void { @@ -363,12 +366,18 @@ async function probeChat(req: TestRequest): Promise { .join(""); return text.length; } + case "chatgpt": { + // Checks the Sign in with ChatGPT credentials (file present, plan + // scope granted, access token usable; refreshes when near expiry) + // without spending plan usage on a completion. Fails with the + // "not signed in … run scripts/chatgpt-login.mjs" message otherwise. + const tokenFile = req.chatgpt?.tokenFile?.trim(); + await getAccessToken({ file: tokenFile ? tokenFile : undefined }); + return 0; + } case "bedrock": case "host": case "local_only": - // chatgpt authenticates with the Sign in with ChatGPT token file, not an - // API key, so there is nothing to probe from the settings form. - case "chatgpt": return 0; default: throw new Error(`unsupported chat provider: ${provider}`); diff --git a/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts b/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts index 10892523c..e700d0deb 100644 --- a/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts +++ b/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts @@ -3,7 +3,7 @@ * recovery and host ID. Synthetic token files in a temp folder only. */ -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -15,6 +15,10 @@ import { getOrCreateHostId, hostIdFile, isValidHostId, + listChatGptModels, + restrictToOwner, + runLogin, + verifyIdToken, } from "../../../core/llm/chatgpt-auth.js"; function jsonResponse(obj: unknown, status: number): Response { @@ -122,6 +126,36 @@ describe("chatgpt-auth getAccessToken", () => { ).rejects.toSatisfy((e: unknown) => e instanceof ChatGptAuthError && e.kind === "not_signed_in"); }); + it("treats a corrupt or incomplete token file as not signed in", async () => { + const { file } = await tokenFixture(); + await writeFile(file, "{ not json"); + await expect(getAccessToken({ file, now: () => NOW })).rejects.toSatisfy( + (e: unknown) => e instanceof ChatGptAuthError && e.kind === "not_signed_in", + ); + await writeFile(file, JSON.stringify({ client_id: "oaiapp_test", expires_at: NOW })); + await expect(getAccessToken({ file, now: () => NOW })).rejects.toSatisfy( + (e: unknown) => e instanceof ChatGptAuthError && e.kind === "not_signed_in", + ); + }); + + it("releases its own lock, but never a lock a peer re-created meanwhile", async () => { + const { file } = await tokenFixture(); + const ok = (async () => + jsonResponse({ access_token: "synthetic-access-2", refresh_token: "synthetic-refresh-2", expires_in: 3600 }, 200) + ) as unknown as typeof fetch; + await getAccessToken({ file, fetchImpl: ok, now: () => NOW }); + await expect(stat(`${file}.lock`)).rejects.toMatchObject({ code: "ENOENT" }); + + const second = await tokenFixture(); + const peer = (async () => { + // A peer treated our lock as stale and took it over. + await writeFile(`${second.file}.lock`, "peer-owner"); + return jsonResponse({ access_token: "a", refresh_token: "r", expires_in: 3600 }, 200); + }) as unknown as typeof fetch; + await getAccessToken({ file: second.file, fetchImpl: peer, now: () => NOW }); + expect(await readFile(`${second.file}.lock`, "utf8")).toBe("peer-owner"); + }); + it("rejects a grant without the plan scope", async () => { const { file } = await tokenFixture({ scopes: ["openid", "offline_access"], expires_at: NOW + 3_000_000 }); await expect(getAccessToken({ file, now: () => NOW })).rejects.toSatisfy( @@ -151,3 +185,77 @@ describe("chatgpt-auth host ID", () => { expect(isValidHostId(undefined)).toBe(false); }); }); + +describe("chatgpt-auth permissions", () => { + it("restrictToOwner keeps a folder usable by its owner (directories stay traversable)", async () => { + const dir = await mkdtemp(join(tmpdir(), "memos-chatgpt-perm-")); + dirs.push(dir); + expect(typeof (await restrictToOwner(dir))).toBe("boolean"); + await writeFile(join(dir, "inside.txt"), "ok"); + expect(await readFile(join(dir, "inside.txt"), "utf8")).toBe("ok"); + }); +}); + +describe("chatgpt-auth model list", () => { + it("keeps rows without a visibility field and drops hidden ones", async () => { + const fetchImpl = (async () => + jsonResponse( + { + data: [ + { id: "plain-model" }, + { slug: "listed", visibility: "list", display_name: "Listed" }, + { slug: "hidden", visibility: "hide" }, + ], + }, + 200, + )) as unknown as typeof fetch; + const models = await listChatGptModels("synthetic-access", fetchImpl); + expect(models.map((m) => m.slug)).toEqual(["plain-model", "listed"]); + }); +}); + +describe("chatgpt-auth id_token verification", () => { + const b64 = (o: unknown) => Buffer.from(JSON.stringify(o)).toString("base64url"); + const token = (kid?: string) => `${b64({ alg: "RS256", ...(kid ? { kid } : {}) })}.${b64({ sub: "s" })}.c2ln`; + + it("fails clearly when the JWKS endpoint errors", async () => { + const fetchImpl = (async () => new Response("oops", { status: 500 })) as unknown as typeof fetch; + await expect(verifyIdToken(token("k1"), "client", "nonce", fetchImpl)).rejects.toThrow( + "id_token JWKS fetch failed: HTTP 500", + ); + }); + + it("does not fall back to a lone key whose kid differs from the token's", async () => { + const fetchImpl = (async () => + jsonResponse({ keys: [{ kty: "RSA", kid: "other", n: "AQAB", e: "AQAB" }] }, 200)) as unknown as typeof fetch; + await expect(verifyIdToken(token("k1"), "client", "nonce", fetchImpl)).rejects.toThrow( + "id_token signing key not found in JWKS", + ); + }); +}); + +describe("chatgpt-auth runLogin", () => { + it("never prints the ID token: id_token_hint goes only to the browser URL", async () => { + const { file } = await tokenFixture({ id_token: "synthetic.id.token" }); + const printed: string[] = []; + let browserUrl = ""; + const login = runLogin({ + file, + port: 0, + print: (l) => printed.push(l), + openBrowser: (url) => { + browserUrl = url; + const u = new URL(url); + const redirect = new URL(u.searchParams.get("redirect_uri")!); + redirect.searchParams.set("state", u.searchParams.get("state")!); + redirect.searchParams.set("error", "access_denied"); + void fetch(redirect).catch(() => undefined); + }, + timeoutMs: 10_000, + }); + await expect(login).rejects.toThrow("sign-in failed: access_denied"); + expect(new URL(browserUrl).searchParams.get("id_token_hint")).toBe("synthetic.id.token"); + expect(printed.join("\n")).not.toContain("synthetic.id.token"); + expect(printed.join("\n")).not.toContain("id_token_hint"); + }); +}); diff --git a/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts b/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts index caeafaa87..9e0ca9945 100644 --- a/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts +++ b/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts @@ -17,7 +17,7 @@ import { PAUSE_MS, resetChatGptPauseState, } from "../../../core/llm/providers/chatgpt.js"; -import type { AccessToken, TokenOptions } from "../../../core/llm/chatgpt-auth.js"; +import { ChatGptAuthError, type AccessToken, type TokenOptions } from "../../../core/llm/chatgpt-auth.js"; import type { ChatGptProviderConfig, LlmConfig, @@ -85,6 +85,8 @@ function baseConfig(chatgpt: ChatGptProviderConfig): LlmConfig { timeoutMs: 5_000, maxRetries: 0, openRouter: true, + endpoint: "https://openrouter.ai/api/v1", + apiKey: "sk-fallback-test", chatgpt, }; } @@ -286,6 +288,56 @@ describe("ChatGptLlmProvider", () => { expect((await p.complete(msgs, call, ctx)).text).toBe("from-fallback"); expect(fb.calls.complete).toBe(1); }); + + it("does not pause after not_signed_in, so a sign-in takes effect on the next call", async () => { + const fb = makeFallback(); + let signedIn = false; + const requests: string[] = []; + const p = new ChatGptLlmProvider({ + fallback: fb.provider, + getToken: async () => { + if (!signedIn) throw new ChatGptAuthError("not_signed_in", "not signed in"); + return { accessToken: "tok-A", expiresAt: Date.now() + 3_600_000 }; + }, + fetchImpl: (async (url: string) => { + requests.push(url); + return streamResponse(sse(okEvents(["ok"]))); + }) as unknown as typeof fetch, + sleep: async () => {}, + }); + const ctx = { config: baseConfig({}), log: noopLog } as LlmProviderCtx; + expect((await p.complete(msgs, call, ctx)).text).toBe("from-fallback"); + signedIn = true; + expect((await p.complete(msgs, call, ctx)).text).toBe("ok"); + expect(requests).toHaveLength(1); + }); + + it("reports the ChatGPT error instead of falling back when the block has no fallback target", async () => { + const fb = makeFallback(); + const p = new ChatGptLlmProvider({ + fallback: fb.provider, + getToken: async () => { + throw new ChatGptAuthError("not_signed_in", "not signed in"); + }, + }); + const ctx = { + config: { ...baseConfig({}), endpoint: "", apiKey: "" }, + log: noopLog, + } as LlmProviderCtx; + await expect(p.complete(msgs, call, ctx)).rejects.toMatchObject({ + code: "llm_unavailable", + message: expect.stringMatching(/auth:not_signed_in/), + }); + await expect(collect(p.stream(msgs, call, ctx))).rejects.toMatchObject({ code: "llm_unavailable" }); + expect(fb.calls).toEqual({ complete: 0, stream: 0 }); + }); + + it("still pauses after the server rejects the sign-in (HTTP 403)", async () => { + const s = setup({ responses: [jsonResponse({ error: { code: "forbidden" } }, 403)] }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + expect(s.requests).toHaveLength(1); + }); }); describe("chatgpt provider registration", () => { @@ -314,4 +366,11 @@ describe("chatgpt provider registration", () => { const bad = Value.Default(ConfigSchema, { skillEvolver: { provider: "not_a_provider", model: "x" } }); expect([...Value.Errors(ConfigSchema, bad)].some((e) => e.path.startsWith("/skillEvolver/provider"))).toBe(true); }); + + it("config schema rejects an unknown chatgpt.reasoningEffort", () => { + const ok = Value.Default(ConfigSchema, { llm: { provider: "chatgpt", chatgpt: { reasoningEffort: "low" } } }); + expect([...Value.Errors(ConfigSchema, ok)]).toEqual([]); + const bad = Value.Default(ConfigSchema, { llm: { provider: "chatgpt", chatgpt: { reasoningEffort: "loww" } } }); + expect([...Value.Errors(ConfigSchema, bad)].some((e) => e.path.startsWith("/llm/chatgpt/reasoningEffort"))).toBe(true); + }); }); diff --git a/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts b/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts index e8a58ee25..78ddeb8b5 100644 --- a/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts +++ b/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts @@ -236,4 +236,66 @@ skillEvolver: expect(evolver?.endpoint).toBe(""); expect(evolver?.apiKey).toBe(""); }); + + it("never copies a non-OpenAI-compatible main llm key into a chatgpt slot", async () => { + const { bootstrapMemoryCore } = await import("../../../core/pipeline/memory-core.js"); + home = await makeTmpHome({ + agent: "openclaw", + configYaml: ` +llm: + provider: anthropic + endpoint: https://api.anthropic.com + apiKey: sk-ant-main + model: main +skillEvolver: + provider: chatgpt + model: skill-fallback-model +`, + }); + + core = await bootstrapMemoryCore({ + agent: "openclaw", + home: home.home, + config: home.config, + pkgVersion: "bootstrap-test", + }); + + const evolver = capturedLlmConfigs.find((cfg) => cfg.model === "skill-fallback-model"); + expect(evolver?.provider).toBe("chatgpt"); + expect(evolver?.endpoint).toBe(""); + expect(evolver?.apiKey).toBe(""); + }); + + it("wires l3Llm.chatgpt and inherits the main llm endpoint/key like skillEvolver", async () => { + const { bootstrapMemoryCore } = await import("../../../core/pipeline/memory-core.js"); + home = await makeTmpHome({ + agent: "openclaw", + configYaml: ` +llm: + provider: openai_compatible + endpoint: https://openrouter.ai/api/v1 + apiKey: sk-main + model: main +l3Llm: + provider: chatgpt + model: l3-fallback-model + chatgpt: + model: chatgpt-l3-model +`, + }); + + core = await bootstrapMemoryCore({ + agent: "openclaw", + home: home.home, + config: home.config, + pkgVersion: "bootstrap-test", + }); + + expect(capturedLlmConfigs.find((cfg) => cfg.model === "l3-fallback-model")).toMatchObject({ + provider: "chatgpt", + endpoint: "https://openrouter.ai/api/v1", + apiKey: "sk-main", + chatgpt: { model: "chatgpt-l3-model" }, + }); + }); }); diff --git a/apps/memos-local-plugin/viewer/src/stores/i18n.ts b/apps/memos-local-plugin/viewer/src/stores/i18n.ts index e6ca1bab8..ee00d573e 100644 --- a/apps/memos-local-plugin/viewer/src/stores/i18n.ts +++ b/apps/memos-local-plugin/viewer/src/stores/i18n.ts @@ -921,7 +921,7 @@ const en = { "settings.summarizer.inherit": "Currently using the agent's model. Select a provider to override.", "settings.chatgpt.hint": - "Runs on your ChatGPT plan. Sign in once with `node scripts/chatgpt-login.mjs`. The model, endpoint and API key below are the OpenAI-compatible fallback; set the ChatGPT model under `chatgpt.model` in config.yaml.", + "Runs on your ChatGPT plan. Sign in once with `node scripts/chatgpt-login.mjs`; Test checks the sign-in. The model, endpoint and API key below are the OpenAI-compatible fallback. Set the ChatGPT model as `{path}` in config.yaml. For the Skill Evolver, the model field must not be blank, and a blank endpoint or API key reuses the Summarizer's when that one is OpenAI-compatible.", "settings.model.tip.title": "Model selection tips", "settings.model.tip.embedding": "Embedding — the built-in model is small. For better recall, configure a dedicated model such as bge-m3 or text-embedding-3-large.", @@ -1800,7 +1800,7 @@ const zh: Record = { "settings.summarizer.inherit": "当前使用 agent 的模型。选择 Provider 即可覆盖。", "settings.chatgpt.hint": - "使用你的 ChatGPT 套餐运行。先运行一次 `node scripts/chatgpt-login.mjs` 登录。下方的模型、Endpoint 和 API Key 是 OpenAI 兼容的回退设置;ChatGPT 模型请在 config.yaml 的 `chatgpt.model` 中设置。", + "使用你的 ChatGPT 套餐运行。先运行一次 `node scripts/chatgpt-login.mjs` 登录;“测试”会检查登录状态。下方的模型、Endpoint 和 API Key 是 OpenAI 兼容的回退设置。ChatGPT 模型请在 config.yaml 的 `{path}` 中设置。技能进化模型的模型字段不能留空;其 Endpoint 或 API Key 留空时,若摘要模型为 OpenAI 兼容,则沿用摘要模型的设置。", "settings.model.tip.title": "模型选择提示", "settings.model.tip.embedding": "嵌入模型:插件内置模型较小。为获得更精准的记忆检索,建议配置 bge-m3、text-embedding-3-large 等专业嵌入模型。", diff --git a/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx b/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx index d86684605..b2add095e 100644 --- a/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx +++ b/apps/memos-local-plugin/viewer/src/views/SettingsView.tsx @@ -46,6 +46,8 @@ interface ProviderBlock { offline?: boolean; loadTimeoutMs?: number; }; + /** provider = "chatgpt" only (edited in config.yaml). */ + chatgpt?: { model?: string; tokenFile?: string }; } interface AlgorithmBlock { @@ -105,6 +107,10 @@ const EMBEDDING_PROVIDERS = [ "gemini", ]; +/** Dashed note shown above a model card's fields. */ +const HINT_STYLE = + "font-size:var(--fs-xs);color:var(--fg-muted);margin-bottom:var(--sp-3);padding:var(--sp-2) var(--sp-3);background:var(--bg-canvas);border-radius:var(--radius-sm);border:1px dashed var(--border)"; + const LLM_PROVIDERS = [ "", // inherit from agent "openai_compatible", @@ -419,6 +425,7 @@ function ModelCard({ model: block.model, local: block.local, apiKey: sanitizeApiKey(block.apiKey), + chatgpt: block.provider === "chatgpt" ? { tokenFile: block.chatgpt?.tokenFile } : undefined, }); setResult(r); } catch (err) { @@ -469,23 +476,19 @@ function ModelCard({
{inherits && inheritsLabel && ( -
+
{inheritsLabel}
)} {block.provider === "chatgpt" && ( -
- {t("settings.chatgpt.hint")} +
+ {t("settings.chatgpt.hint", { + path: type === "skillEvolver" ? "skillEvolver.chatgpt.model" : "llm.chatgpt.model", + })}
)} {isLocal && localHint && ( -
+
{localHint}
)} From 7543d33c235138096a81d962d445844179dddda4 Mon Sep 17 00:00:00 2001 From: jeff-ang92 <330780721+jeff-ang92@users.noreply.github.com> Date: Thu, 12 Nov 2026 04:58:00 +0700 Subject: [PATCH 3/3] fix(plugin): address second review round on the chatgpt provider - wiring: inherit the main llm endpoint/key only as a pair - config: chatgpt defaults so llm/l3Llm/skillEvolver.chatgpt load without "unknown config key" warnings - provider: fall back only on ChatGPT outcomes (local bugs surface), map body read errors to ChatGPT failures, pause after a failure that follows streamed text - auth: ignore callbacks without this sign-in's state, default scopes when the token response omits them, report a busy callback port, bounded lock wait, document the id_token_hint argv limit - login script: reject empty --file= and --status with --models - tests for each behaviour change Co-Authored-By: Claude Opus 5.5 --- .../core/config/defaults.ts | 4 + apps/memos-local-plugin/core/config/schema.ts | 7 +- .../core/llm/chatgpt-auth.ts | 51 +++++++++---- .../core/llm/providers/chatgpt.ts | 27 ++++++- .../core/pipeline/memory-core.ts | 19 +++-- .../docs/CONFIG-ADVANCED.md | 8 +- .../scripts/chatgpt-login.mjs | 11 ++- .../tests/unit/llm/chatgpt-auth.test.ts | 75 +++++++++++++++++-- .../tests/unit/llm/chatgpt-provider.test.ts | 55 ++++++++++++++ .../pipeline/bootstrap-llm-config.test.ts | 29 +++++++ .../viewer/src/stores/i18n.ts | 4 +- 11 files changed, 244 insertions(+), 46 deletions(-) diff --git a/apps/memos-local-plugin/core/config/defaults.ts b/apps/memos-local-plugin/core/config/defaults.ts index 093e06035..fb8c1201a 100644 --- a/apps/memos-local-plugin/core/config/defaults.ts +++ b/apps/memos-local-plugin/core/config/defaults.ts @@ -62,6 +62,8 @@ export const DEFAULT_CONFIG: ResolvedConfig = { openRouter: false, maxTokens: 1024, headers: {}, + // Only read when provider = "chatgpt" (Sign in with ChatGPT). + chatgpt: { model: "", fallback: true, tokenFile: "" }, }, l3Llm: { // Empty by default — falls back to the shared `llm` settings. @@ -80,6 +82,7 @@ export const DEFAULT_CONFIG: ResolvedConfig = { openRouter: false, maxTokens: 4096, headers: {}, + chatgpt: { model: "", fallback: true, tokenFile: "" }, }, skillEvolver: { // Empty by default — falls back to the shared `llm` settings. @@ -96,6 +99,7 @@ export const DEFAULT_CONFIG: ResolvedConfig = { openRouter: false, maxTokens: 4096, headers: {}, + chatgpt: { model: "", fallback: true, tokenFile: "" }, }, storage: { ftsTokenizer: "trigram", diff --git a/apps/memos-local-plugin/core/config/schema.ts b/apps/memos-local-plugin/core/config/schema.ts index 503e3f0bd..f734001ba 100644 --- a/apps/memos-local-plugin/core/config/schema.ts +++ b/apps/memos-local-plugin/core/config/schema.ts @@ -188,9 +188,10 @@ const SkillEvolverSchema = Type.Object({ /** Extra HTTP headers for the provider request. */ headers: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })), /** - * Only read when provider = "chatgpt". A blank endpoint / apiKey here - * inherits the main `llm` block's for the openai_compatible fallback - * when the main provider is openai_compatible or chatgpt. + * Only read when provider = "chatgpt". When this block's endpoint and + * apiKey are both blank, the openai_compatible fallback uses the main + * `llm` block's endpoint and apiKey, provided the main provider is + * openai_compatible or chatgpt. */ chatgpt: Type.Optional(ChatGptSchema), }, { default: {} }); diff --git a/apps/memos-local-plugin/core/llm/chatgpt-auth.ts b/apps/memos-local-plugin/core/llm/chatgpt-auth.ts index 5451cd850..a7c31da5d 100644 --- a/apps/memos-local-plugin/core/llm/chatgpt-auth.ts +++ b/apps/memos-local-plugin/core/llm/chatgpt-auth.ts @@ -13,7 +13,11 @@ * - Access token ~1h; refresh token rotates on every refresh. * * Secrets rule: nothing in this file logs, prints or throws a token. Error - * messages carry OAuth error codes only. + * messages carry OAuth error codes only. One known limit: on a re-sign-in + * the browser is opened with an `id_token_hint` (the previous ID token, + * which identifies the account but grants no API access) as a command-line + * argument to the OS URL opener, which other local users can see in the + * process list for that short moment. The printed address never carries it. */ import { @@ -201,6 +205,10 @@ async function withFileLock(file: string, fn: () => Promise): Promise { const owner = `${process.pid}.${randomBytes(8).toString("hex")}`; const started = Date.now(); for (;;) { + // Checked on every pass, so no path below can spin past the wait limit. + if (Date.now() - started > LOCK_WAIT_MS) { + throw new ChatGptAuthError("lock_timeout", "timed out waiting for the ChatGPT token lock"); + } try { const h = await fsp.open(lock, "wx", 0o600); await h.writeFile(owner); @@ -208,17 +216,11 @@ async function withFileLock(file: string, fn: () => Promise): Promise { break; } catch (err) { if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err; - try { - const st = await fsp.stat(lock); - if (Date.now() - st.mtimeMs > LOCK_STALE_MS) { - await fsp.rm(lock, { force: true }); - continue; - } - } catch { - continue; // lock vanished between open and stat - } - if (Date.now() - started > LOCK_WAIT_MS) { - throw new ChatGptAuthError("lock_timeout", "timed out waiting for the ChatGPT token lock"); + const st = await fsp.stat(lock).catch(() => null); + if (!st) continue; // lock vanished between open and stat: retry at once + if (Date.now() - st.mtimeMs > LOCK_STALE_MS) { + await fsp.rm(lock, { force: true }); + continue; } await new Promise((r) => setTimeout(r, 150)); } @@ -420,8 +422,15 @@ export async function runLogin(opts: LoginOptions = {}): Promise { const state = b64url(randomBytes(24)); const nonce = b64url(randomBytes(24)); - const { server, port } = await listenLoopback(opts.port ?? DEFAULT_CALLBACK_PORT); + const preferredPort = opts.port ?? DEFAULT_CALLBACK_PORT; + const { server, port } = await listenLoopback(preferredPort); const redirectUri = `http://127.0.0.1:${port}${CALLBACK_PATH}`; + if (preferredPort !== 0 && port !== preferredPort) { + print(`Port ${preferredPort} is busy, so the sign-in callback uses ${redirectUri} instead.`); + if (savedClientId) { + print(`If sign-in fails with a redirect error, free port ${preferredPort} and run this again.`); + } + } const params = new URLSearchParams({ response_type: "code", @@ -449,7 +458,7 @@ export async function runLogin(opts: LoginOptions = {}): Promise { let callback: URLSearchParams; try { - callback = await waitForCallback(server, opts.timeoutMs ?? 15 * 60_000); + callback = await waitForCallback(server, state, opts.timeoutMs ?? 15 * 60_000); } finally { server.close(); } @@ -491,7 +500,8 @@ export async function runLogin(opts: LoginOptions = {}): Promise { throw new Error(`token exchange failed: ${tok.error ?? `HTTP ${tokenResp.status}`}`); } if (!tok.refresh_token) throw new Error("token exchange returned no refresh token (offline_access not granted)"); - const scopes = (tok.scope ?? callback.get("scope") ?? "").split(/\s+/).filter(Boolean); + // RFC 6749 §5.1: `scope` may be omitted when the grant equals the request. + const scopes = (tok.scope ?? callback.get("scope") ?? CHATGPT_SCOPES.join(" ")).split(/\s+/).filter(Boolean); let subject: string | undefined; let email: string | undefined; @@ -613,7 +623,12 @@ function listenLoopback(preferred: number): Promise<{ server: http.Server; port: return tryPort(preferred).catch(() => tryPort(0)); } -function waitForCallback(server: http.Server, timeoutMs: number): Promise { +/** + * Resolves on the first callback that carries this sign-in's `state`. + * Stray requests (browser prefetch, retries, other local processes) get a + * 400 and the listener keeps waiting, so they cannot end the sign-in. + */ +function waitForCallback(server: http.Server, state: string, timeoutMs: number): Promise { return new Promise((resolve, reject) => { const timer = setTimeout(() => reject(new Error("sign-in timed out")), timeoutMs); server.on("request", (req, res) => { @@ -622,6 +637,10 @@ function waitForCallback(server: http.Server, timeoutMs: number): PromiseMemOS

" + diff --git a/apps/memos-local-plugin/core/llm/providers/chatgpt.ts b/apps/memos-local-plugin/core/llm/providers/chatgpt.ts index c2ebae06a..73ea0f8b9 100644 --- a/apps/memos-local-plugin/core/llm/providers/chatgpt.ts +++ b/apps/memos-local-plugin/core/llm/providers/chatgpt.ts @@ -145,7 +145,12 @@ export class ChatGptLlmProvider implements LlmProvider { } } catch (err) { // Text already reached the caller: a silent switch would duplicate it. - if (emitted) throw this.toMemosError(err, cg, ctx); + // Still record the pause, as complete() does, so a repeatable failure + // (usage limit, not eligible, auth) is not retried on every call. + if (emitted) { + this.notePause(err, cg); + throw this.toMemosError(err, cg, ctx); + } yield* this.fallbackStream(messages, opts, ctx, cg, null, err); } } @@ -220,7 +225,19 @@ export class ChatGptLlmProvider implements LlmProvider { } if (!resp.body) throw new ChatGptFailure("empty_body", resp.status, "ChatGPT returned an empty stream"); - yield* parseResponsesStream(resp.body); + try { + yield* parseResponsesStream(resp.body); + } catch (err) { + if (err instanceof ChatGptFailure) throw err; + // A dropped connection or timeout while reading the body. + const name = (err as Error)?.name ?? "Error"; + if (ctx.signal?.aborted) throw new ChatGptFailure("cancelled", null, "ChatGPT request was cancelled"); + throw new ChatGptFailure( + name === "TimeoutError" ? "timeout" : "stream_interrupted", + null, + `ChatGPT stream failed (${name})`, + ); + } return; } } @@ -284,7 +301,11 @@ export class ChatGptLlmProvider implements LlmProvider { ): void { if (err) this.notePause(err, cg); const hasTarget = Boolean(ctx.config.apiKey) || Boolean(ctx.config.endpoint); - if (cg.fallback === false || ctx.signal?.aborted || !hasTarget) { + // Only ChatGPT outcomes (HTTP / stream / sign-in failures) fall back. + // Anything else is a local bug: surface it rather than re-sending the + // prompt to the fallback endpoint. + const chatgptOutcome = err instanceof ChatGptFailure || err instanceof ChatGptAuthError; + if (cg.fallback === false || ctx.signal?.aborted || !hasTarget || (err && !chatgptOutcome)) { throw this.toMemosError(err ?? pausedErr(pausedReason), cg, ctx); } ctx.log.warn("chatgpt.fallback", { diff --git a/apps/memos-local-plugin/core/pipeline/memory-core.ts b/apps/memos-local-plugin/core/pipeline/memory-core.ts index 6b39d0b55..3c37b4dbc 100644 --- a/apps/memos-local-plugin/core/pipeline/memory-core.ts +++ b/apps/memos-local-plugin/core/pipeline/memory-core.ts @@ -153,23 +153,26 @@ type DedicatedLlmConfig = { /** * Endpoint / apiKey for a dedicated LLM slot (skillEvolver, l3Llm). A - * `chatgpt` slot with a blank endpoint or apiKey reuses the main `llm` - * block's for its OpenAI-compatible fallback, but only when the main block - * is itself OpenAI-compatible (`openai_compatible` or `chatgpt`), so a key - * meant for another provider is never sent to an OpenAI-compatible endpoint. + * `chatgpt` slot with BOTH endpoint and apiKey blank reuses the main `llm` + * block's pair for its OpenAI-compatible fallback, and only when the main + * block is itself OpenAI-compatible (`openai_compatible` or `chatgpt`). + * The pair is inherited together or not at all, so a key is never sent to + * an endpoint it was not configured for. */ function dedicatedFallbackTarget( slot: DedicatedLlmConfig | undefined, provider: string, mainLlm: ResolvedConfig["llm"], ): { endpoint: string; apiKey: string } { + const endpoint = slot?.endpoint ?? ""; + const apiKey = slot?.apiKey ?? ""; const inherit = provider === "chatgpt" && + !endpoint && + !apiKey && (mainLlm.provider === "openai_compatible" || mainLlm.provider === "chatgpt"); - return { - endpoint: slot?.endpoint || (inherit ? mainLlm.endpoint : ""), - apiKey: slot?.apiKey || (inherit ? mainLlm.apiKey : ""), - }; + if (inherit) return { endpoint: mainLlm.endpoint, apiKey: mainLlm.apiKey }; + return { endpoint, apiKey }; } export interface BootstrapOptions { diff --git a/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md b/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md index d2b1c06ca..620a5d068 100644 --- a/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md +++ b/apps/memos-local-plugin/docs/CONFIG-ADVANCED.md @@ -80,10 +80,10 @@ llm: `skillEvolver` and `l3Llm` accept the same `provider: chatgpt` and `chatgpt:` block (their `model` must be non-empty, as for any provider). A -chatgpt `skillEvolver` / `l3Llm` with a blank `endpoint` / `apiKey` reuses -the main `llm` block's for its fallback, but only when the main block is -`openai_compatible` or `chatgpt`, so another provider's key is never sent to -an OpenAI-compatible endpoint. With no fallback target at all (no `apiKey` +chatgpt `skillEvolver` / `l3Llm` with both `endpoint` and `apiKey` blank +reuses the main `llm` block's pair for its fallback, but only when the main +block is `openai_compatible` or `chatgpt`. The pair is inherited together or +not at all, so a key is never sent to an endpoint it was not set up for. With no fallback target at all (no `apiKey` and no `endpoint`), ChatGPT errors are reported as they are instead of falling back. diff --git a/apps/memos-local-plugin/scripts/chatgpt-login.mjs b/apps/memos-local-plugin/scripts/chatgpt-login.mjs index ca79e5195..30789d18a 100644 --- a/apps/memos-local-plugin/scripts/chatgpt-login.mjs +++ b/apps/memos-local-plugin/scripts/chatgpt-login.mjs @@ -23,7 +23,9 @@ const KNOWN_FLAGS = new Set(["--status", "--models"]); // interactive sign-in that writes the default token file. const unknown = args.filter((a) => !KNOWN_FLAGS.has(a) && !a.startsWith("--file=")); const fileArg = args.find((a) => a.startsWith("--file=")); -const file = fileArg ? fileArg.slice("--file=".length).trim() || undefined : undefined; +const file = fileArg === undefined ? undefined : fileArg.slice("--file=".length).trim(); +const wantStatus = args.includes("--status"); +const wantModels = args.includes("--models"); async function main() { if (unknown.length > 0) { @@ -31,11 +33,14 @@ async function main() { `unknown argument(s): ${unknown.join(" ")} (use --status, --models or --file=)`, ); } - if (args.includes("--status")) { + // An explicit but empty --file= must not fall back to the default file. + if (fileArg !== undefined && !file) throw new Error("--file= needs a non-empty path"); + if (wantStatus && wantModels) throw new Error("use either --status or --models, not both"); + if (wantStatus) { console.log(JSON.stringify(await describeCredentials(file), null, 2)); return; } - if (args.includes("--models")) { + if (wantModels) { const { accessToken } = await getAccessToken({ file }); const models = await listChatGptModels(accessToken); if (models.length === 0) console.log("No listed models returned."); diff --git a/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts b/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts index e700d0deb..0dbbcbd1a 100644 --- a/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts +++ b/apps/memos-local-plugin/tests/unit/llm/chatgpt-auth.test.ts @@ -4,10 +4,12 @@ */ import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; +import { createServer, get as httpGet } from "node:http"; +import type { AddressInfo } from "node:net"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { ChatGptAuthError, @@ -235,27 +237,86 @@ describe("chatgpt-auth id_token verification", () => { }); describe("chatgpt-auth runLogin", () => { - it("never prints the ID token: id_token_hint goes only to the browser URL", async () => { + afterEach(() => { + vi.unstubAllGlobals(); + }); + + /** GET a loopback URL with node:http (independent of a stubbed global fetch). */ + function hit(url: URL): Promise { + return new Promise((resolve, reject) => { + httpGet(url, (res) => { + res.resume(); + resolve(res.statusCode ?? 0); + }).on("error", reject); + }); + } + + /** Callback URL for the authorize URL handed to the browser. */ + function callbackFor(authorizeUrl: string, extra: Record): URL { + const u = new URL(authorizeUrl); + const redirect = new URL(u.searchParams.get("redirect_uri")!); + redirect.searchParams.set("state", u.searchParams.get("state")!); + for (const [k, v] of Object.entries(extra)) redirect.searchParams.set(k, v); + return redirect; + } + + it("never prints the ID token, and ignores stray callbacks without this sign-in's state", async () => { const { file } = await tokenFixture({ id_token: "synthetic.id.token" }); const printed: string[] = []; let browserUrl = ""; + let strayStatus = 0; const login = runLogin({ file, port: 0, print: (l) => printed.push(l), openBrowser: (url) => { browserUrl = url; - const u = new URL(url); - const redirect = new URL(u.searchParams.get("redirect_uri")!); - redirect.searchParams.set("state", u.searchParams.get("state")!); - redirect.searchParams.set("error", "access_denied"); - void fetch(redirect).catch(() => undefined); + void (async () => { + const stray = callbackFor(url, { error: "access_denied" }); + stray.searchParams.set("state", "not-this-sign-in"); + strayStatus = await hit(stray); + await hit(callbackFor(url, { error: "access_denied" })); + })(); }, timeoutMs: 10_000, }); await expect(login).rejects.toThrow("sign-in failed: access_denied"); + expect(strayStatus).toBe(400); expect(new URL(browserUrl).searchParams.get("id_token_hint")).toBe("synthetic.id.token"); expect(printed.join("\n")).not.toContain("synthetic.id.token"); expect(printed.join("\n")).not.toContain("id_token_hint"); }); + + it("keeps the requested scopes when the token response omits `scope`, and reports a busy port", async () => { + const dir = await mkdtemp(join(tmpdir(), "memos-chatgpt-login-")); + dirs.push(dir); + const file = join(dir, "chatgpt-oauth.json"); + const blocker = createServer(); + await new Promise((r) => blocker.listen(0, "127.0.0.1", () => r())); + const busyPort = (blocker.address() as AddressInfo).port; + vi.stubGlobal( + "fetch", + vi.fn(async () => + jsonResponse({ access_token: "synthetic-access", refresh_token: "synthetic-refresh", expires_in: 3600 }, 200), + ), + ); + const printed: string[] = []; + try { + const res = await runLogin({ + file, + port: busyPort, + print: (l) => printed.push(l), + openBrowser: (url) => { + void hit(callbackFor(url, { code: "synthetic-code", client_id: "oaiapp_test" })); + }, + timeoutMs: 10_000, + }); + expect(res.scopes).toContain("chatgpt.tokens.use.direct"); + expect(printed.some((l) => l.includes(`Port ${busyPort} is busy`))).toBe(true); + const saved = JSON.parse(await readFile(file, "utf8")); + expect(saved.scopes).toContain("chatgpt.tokens.use.direct"); + } finally { + blocker.close(); + } + }); }); diff --git a/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts b/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts index 9e0ca9945..3cb784714 100644 --- a/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts +++ b/apps/memos-local-plugin/tests/unit/llm/chatgpt-provider.test.ts @@ -10,6 +10,7 @@ import { join } from "node:path"; import { Value } from "@sinclair/typebox/value"; import { beforeEach, describe, expect, it } from "vitest"; +import { resolveConfig } from "../../../core/config/index.js"; import { ConfigSchema } from "../../../core/config/schema.js"; import { createLlmClientWithProvider, makeProviderFor } from "../../../core/llm/client.js"; import { @@ -332,6 +333,47 @@ describe("ChatGptLlmProvider", () => { expect(fb.calls).toEqual({ complete: 0, stream: 0 }); }); + it("stream(): a failure after the first delta is thrown, and still pauses ChatGPT", async () => { + const failed: SseEvent[] = [ + { type: "response.output_text.delta", delta: "partial" }, + { + type: "response.failed", + response: { status: "failed", error: { code: "subscription_sharing_usage_limit_exceeded" } }, + }, + ]; + const s = setup({ responses: [streamResponse(sse(failed))] }); + await expect(collect(s.provider.stream(msgs, call, s.ctx))).rejects.toMatchObject({ code: "llm_rate_limited" }); + expect(s.fb.calls.stream).toBe(0); + // Paused: the next call goes straight to the fallback without a ChatGPT request. + const next = await collect(s.provider.stream(msgs, call, s.ctx)); + expect(next[0]?.delta).toBe("from-fallback"); + expect(s.requests).toHaveLength(1); + }); + + it("surfaces unexpected local errors instead of re-sending the prompt to the fallback", async () => { + const fb = makeFallback(); + const p = new ChatGptLlmProvider({ + fallback: fb.provider, + getToken: async () => { + throw new TypeError("bug in local code"); + }, + }); + const ctx = { config: baseConfig({}), log: noopLog } as LlmProviderCtx; + await expect(p.complete(msgs, call, ctx)).rejects.toMatchObject({ code: "llm_unavailable" }); + expect(fb.calls.complete).toBe(0); + }); + + it("treats a connection dropped while reading the stream as a ChatGPT failure (falls back)", async () => { + const broken = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode(sse([{ type: "response.output_text.delta", delta: "x" }]))); + controller.error(new TypeError("terminated")); + }, + }); + const s = setup({ responses: [new Response(broken, { status: 200 })] }); + expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); + }); + it("still pauses after the server rejects the sign-in (HTTP 403)", async () => { const s = setup({ responses: [jsonResponse({ error: { code: "forbidden" } }, 403)] }); expect((await s.provider.complete(msgs, call, s.ctx)).text).toBe("from-fallback"); @@ -367,6 +409,19 @@ describe("chatgpt provider registration", () => { expect([...Value.Errors(ConfigSchema, bad)].some((e) => e.path.startsWith("/skillEvolver/provider"))).toBe(true); }); + it("loading a chatgpt block raises no 'unknown config key' warning", () => { + const warnings: string[] = []; + resolveConfig( + { + llm: { provider: "chatgpt", chatgpt: { model: "m", fallback: true, tokenFile: "/tmp/t.json" } }, + skillEvolver: { provider: "chatgpt", model: "x", chatgpt: { model: "m" } }, + l3Llm: { provider: "chatgpt", model: "x", chatgpt: { fallback: false } }, + }, + warnings, + ); + expect(warnings.filter((w) => w.includes("chatgpt"))).toEqual([]); + }); + it("config schema rejects an unknown chatgpt.reasoningEffort", () => { const ok = Value.Default(ConfigSchema, { llm: { provider: "chatgpt", chatgpt: { reasoningEffort: "low" } } }); expect([...Value.Errors(ConfigSchema, ok)]).toEqual([]); diff --git a/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts b/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts index 78ddeb8b5..61b6e481f 100644 --- a/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts +++ b/apps/memos-local-plugin/tests/unit/pipeline/bootstrap-llm-config.test.ts @@ -266,6 +266,35 @@ skillEvolver: expect(evolver?.apiKey).toBe(""); }); + it("inherits the main endpoint/key only as a pair (a slot endpoint alone gets no main key)", async () => { + const { bootstrapMemoryCore } = await import("../../../core/pipeline/memory-core.js"); + home = await makeTmpHome({ + agent: "openclaw", + configYaml: ` +llm: + provider: openai_compatible + endpoint: https://openrouter.ai/api/v1 + apiKey: sk-main + model: main +skillEvolver: + provider: chatgpt + model: skill-fallback-model + endpoint: http://127.0.0.1:8080/v1 +`, + }); + + core = await bootstrapMemoryCore({ + agent: "openclaw", + home: home.home, + config: home.config, + pkgVersion: "bootstrap-test", + }); + + const evolver = capturedLlmConfigs.find((cfg) => cfg.model === "skill-fallback-model"); + expect(evolver?.endpoint).toBe("http://127.0.0.1:8080/v1"); + expect(evolver?.apiKey).toBe(""); + }); + it("wires l3Llm.chatgpt and inherits the main llm endpoint/key like skillEvolver", async () => { const { bootstrapMemoryCore } = await import("../../../core/pipeline/memory-core.js"); home = await makeTmpHome({ diff --git a/apps/memos-local-plugin/viewer/src/stores/i18n.ts b/apps/memos-local-plugin/viewer/src/stores/i18n.ts index ee00d573e..e1f90ca80 100644 --- a/apps/memos-local-plugin/viewer/src/stores/i18n.ts +++ b/apps/memos-local-plugin/viewer/src/stores/i18n.ts @@ -921,7 +921,7 @@ const en = { "settings.summarizer.inherit": "Currently using the agent's model. Select a provider to override.", "settings.chatgpt.hint": - "Runs on your ChatGPT plan. Sign in once with `node scripts/chatgpt-login.mjs`; Test checks the sign-in. The model, endpoint and API key below are the OpenAI-compatible fallback. Set the ChatGPT model as `{path}` in config.yaml. For the Skill Evolver, the model field must not be blank, and a blank endpoint or API key reuses the Summarizer's when that one is OpenAI-compatible.", + "Runs on your ChatGPT plan. Sign in once with `node scripts/chatgpt-login.mjs`; Test checks the sign-in. The model, endpoint and API key below are the OpenAI-compatible fallback. Set the ChatGPT model as `{path}` in config.yaml. For the Skill Evolver, the model field must not be blank; leave both endpoint and API key blank to reuse the Summarizer's pair when that one is OpenAI-compatible.", "settings.model.tip.title": "Model selection tips", "settings.model.tip.embedding": "Embedding — the built-in model is small. For better recall, configure a dedicated model such as bge-m3 or text-embedding-3-large.", @@ -1800,7 +1800,7 @@ const zh: Record = { "settings.summarizer.inherit": "当前使用 agent 的模型。选择 Provider 即可覆盖。", "settings.chatgpt.hint": - "使用你的 ChatGPT 套餐运行。先运行一次 `node scripts/chatgpt-login.mjs` 登录;“测试”会检查登录状态。下方的模型、Endpoint 和 API Key 是 OpenAI 兼容的回退设置。ChatGPT 模型请在 config.yaml 的 `{path}` 中设置。技能进化模型的模型字段不能留空;其 Endpoint 或 API Key 留空时,若摘要模型为 OpenAI 兼容,则沿用摘要模型的设置。", + "使用你的 ChatGPT 套餐运行。先运行一次 `node scripts/chatgpt-login.mjs` 登录;“测试”会检查登录状态。下方的模型、Endpoint 和 API Key 是 OpenAI 兼容的回退设置。ChatGPT 模型请在 config.yaml 的 `{path}` 中设置。技能进化模型的模型字段不能留空;若摘要模型为 OpenAI 兼容,将 Endpoint 和 API Key 都留空即可沿用摘要模型的这两项设置。", "settings.model.tip.title": "模型选择提示", "settings.model.tip.embedding": "嵌入模型:插件内置模型较小。为获得更精准的记忆检索,建议配置 bge-m3、text-embedding-3-large 等专业嵌入模型。",