mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 03:49:44 +08:00
Maintenance release on top of v1.5.5, finishing the Codex sign-in it shipped and adding a connected knowledge engine. - Tencent IMA joins as a connected knowledge engine: a KB bound to `ima` is a connection pointer, not an index, following the lightrag-server shape. Documents are added in IMA and retrieval calls IMA's OpenAPI; credentials (client_id + api_key) and the library id live per-KB, so each user on a shared deployment retrieves through their own account. - The activity panel splits what a conversation produced from what you uploaded: generated files get their own card above attachments, with a size and a hover title derived from the artifact URL, so no absolute server path is persisted or sent to the browser. - Remote Codex OAuth completes (#716): the loopback listener stays private while the exact callback path is forwarded through the Web origin to a state-validating broker, with the real callback address, the SSH tunnel command, an authorize action, and a reload-surviving countdown surfaced in the settings card and CLI. - That callback is delivered by OAuth state rather than the request's user, since the browser arrives through the user's own tunnel with no session cookie -- without it remote sign-in returned login_not_active for every non-administrator. Partner scopes resolve Codex credentials to the owner's root (#713) instead of the partner's synthetic workspace. - parse_language() no longer collapses every non-English language to Chinese (#712), which is why a Japanese book came back with Chinese quizzes; prompt fallbacks now try [code, base locale, "en"]. - Assorted: book creation and block regeneration run over WebSocket to dodge proxy timeouts with non-streaming spine synthesis (#707, routed through the agent seam so image attachments, trace metadata, and per-agent routing survive), deeptutor start installs web/node_modules when missing (#710), admin user management is localized (#718), and the service lazy loader uses literal import paths (#717). README prose, the ten translated READMEs, release notes, and the version bump ride along in this commit.
210 lines
6.1 KiB
TypeScript
210 lines
6.1 KiB
TypeScript
import { apiFetch, apiUrl } from "@/lib/api";
|
|
|
|
export type CodexOAuthStatus = {
|
|
connection: "disconnected" | "authorizing" | "connected" | "error";
|
|
operation_id: string | null;
|
|
operation_state:
|
|
| "waiting"
|
|
| "exchanging"
|
|
| "fetching_models"
|
|
| "completed"
|
|
| "cancelled"
|
|
| "expired"
|
|
| "failed"
|
|
| null;
|
|
authorize_url: string | null;
|
|
expires_in: number | null;
|
|
callback_port: number | null;
|
|
callback_forward_port: number | null;
|
|
redirect_uri: string | null;
|
|
model_count: number;
|
|
catalog_source:
|
|
| "live"
|
|
| "fresh-cache"
|
|
| "revalidated-cache"
|
|
| "stale-cache"
|
|
| null;
|
|
catalog_fetched_at: number | null;
|
|
active_model: string | null;
|
|
activated: boolean;
|
|
error_code: string | null;
|
|
};
|
|
|
|
export type CodexLoginStart = {
|
|
operation_id: string;
|
|
authorize_url: string;
|
|
expires_in: number;
|
|
callback_port: number;
|
|
callback_forward_port: number;
|
|
redirect_uri: string;
|
|
ssh_forward_command: string;
|
|
};
|
|
|
|
export type CodexRemoteGuidance = Omit<CodexLoginStart, "ssh_forward_command">;
|
|
|
|
export class CodexOAuthApiError extends Error {
|
|
code: string;
|
|
|
|
constructor(code: string, message: string) {
|
|
super(message);
|
|
this.name = "CodexOAuthApiError";
|
|
this.code = code;
|
|
}
|
|
}
|
|
|
|
const BASE = "/api/v1/settings/providers/openai-codex";
|
|
|
|
export function isLoopbackHostname(hostname: string): boolean {
|
|
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
|
|
if (
|
|
normalized === "localhost" ||
|
|
normalized.endsWith(".localhost") ||
|
|
normalized === "::1" ||
|
|
normalized === "[::1]"
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
const octets = normalized.split(".");
|
|
return (
|
|
octets.length === 4 &&
|
|
octets[0] === "127" &&
|
|
octets.every(
|
|
(octet) =>
|
|
/^\d+$/.test(octet) && Number(octet) >= 0 && Number(octet) <= 255,
|
|
)
|
|
);
|
|
}
|
|
|
|
export function buildSshForwardCommand(
|
|
callbackPort: number,
|
|
hostname: string,
|
|
forwardPort: number,
|
|
): string {
|
|
const serverHost = hostname.trim() || "<server-host>";
|
|
return `ssh -N -L ${callbackPort}:127.0.0.1:${forwardPort} <ssh-user>@${serverHost}`;
|
|
}
|
|
|
|
export function codexRemoteGuidance(
|
|
status: CodexOAuthStatus | null,
|
|
loginStart: CodexLoginStart | null,
|
|
): CodexRemoteGuidance | null {
|
|
if (status?.operation_state !== "waiting" || !status.operation_id) {
|
|
return null;
|
|
}
|
|
const matchingStart =
|
|
loginStart?.operation_id === status.operation_id ? loginStart : null;
|
|
const authorizeUrl = status.authorize_url ?? matchingStart?.authorize_url;
|
|
const expiresIn = status.expires_in ?? matchingStart?.expires_in;
|
|
const callbackPort = status.callback_port ?? matchingStart?.callback_port;
|
|
const callbackForwardPort =
|
|
status.callback_forward_port ?? matchingStart?.callback_forward_port;
|
|
const redirectUri = status.redirect_uri ?? matchingStart?.redirect_uri;
|
|
if (
|
|
!authorizeUrl ||
|
|
expiresIn == null ||
|
|
callbackPort == null ||
|
|
callbackForwardPort == null ||
|
|
!redirectUri
|
|
) {
|
|
return null;
|
|
}
|
|
return {
|
|
operation_id: status.operation_id,
|
|
authorize_url: authorizeUrl,
|
|
expires_in: expiresIn,
|
|
callback_port: callbackPort,
|
|
callback_forward_port: callbackForwardPort,
|
|
redirect_uri: redirectUri,
|
|
};
|
|
}
|
|
|
|
export async function requestCodex<T>(
|
|
path: string,
|
|
method: "GET" | "POST",
|
|
fetchImpl: typeof apiFetch = apiFetch,
|
|
): Promise<T> {
|
|
const response = await fetchImpl(apiUrl(`${BASE}${path}`), {
|
|
method,
|
|
skipAuthRedirect: true,
|
|
});
|
|
if (response.ok) {
|
|
try {
|
|
return (await response.json()) as T;
|
|
} catch {
|
|
throw new CodexOAuthApiError(
|
|
"invalid_response",
|
|
"DeepTutor returned an invalid Codex OAuth response.",
|
|
);
|
|
}
|
|
}
|
|
|
|
let code = `http_${response.status}`;
|
|
let message = "Codex request failed.";
|
|
try {
|
|
const payload = (await response.json()) as {
|
|
detail?: { code?: string; message?: string };
|
|
};
|
|
if (payload.detail?.code) code = payload.detail.code;
|
|
if (payload.detail?.message) message = payload.detail.message;
|
|
} catch {
|
|
// The UI renders only the stable code mapping below.
|
|
}
|
|
throw new CodexOAuthApiError(code, message);
|
|
}
|
|
|
|
export function getCodexStatus(): Promise<CodexOAuthStatus> {
|
|
return requestCodex<CodexOAuthStatus>("/oauth/status", "GET");
|
|
}
|
|
|
|
export function startCodexLogin(): Promise<CodexLoginStart> {
|
|
return requestCodex<CodexLoginStart>("/oauth/start", "POST");
|
|
}
|
|
|
|
export function cancelCodexLogin(): Promise<CodexOAuthStatus> {
|
|
return requestCodex<CodexOAuthStatus>("/oauth/cancel", "POST");
|
|
}
|
|
|
|
export function refreshCodexModels(): Promise<CodexOAuthStatus> {
|
|
return requestCodex<CodexOAuthStatus>("/models/refresh", "POST");
|
|
}
|
|
|
|
export function logoutCodex(): Promise<CodexOAuthStatus> {
|
|
return requestCodex<CodexOAuthStatus>("/oauth/logout", "POST");
|
|
}
|
|
|
|
export function shouldPollCodexStatus(status: CodexOAuthStatus): boolean {
|
|
return (
|
|
status.operation_state === "waiting" ||
|
|
status.operation_state === "exchanging" ||
|
|
status.operation_state === "fetching_models"
|
|
);
|
|
}
|
|
|
|
export function codexErrorMessageKey(code: string | null): string {
|
|
if (code === "catalog_unavailable" || code === "catalog_invalid") {
|
|
return "codex.oauth.catalogFailed";
|
|
}
|
|
if (code === "inference_in_progress") {
|
|
return "codex.oauth.inferenceActive";
|
|
}
|
|
if (code === "login_timeout") return "codex.oauth.callbackMissing";
|
|
if (code === "callback_unavailable") {
|
|
return "codex.oauth.callbackUnavailable";
|
|
}
|
|
if (code === "invalid_response") return "codex.oauth.invalidResponse";
|
|
if (code === "login_cancelled") return "codex.oauth.cancelled";
|
|
if (code === "authorization_denied") return "codex.oauth.denied";
|
|
return "codex.oauth.requestFailed";
|
|
}
|
|
|
|
export function codexStatusMessageKey(status: CodexOAuthStatus): string {
|
|
if (status.error_code) return codexErrorMessageKey(status.error_code);
|
|
if (shouldPollCodexStatus(status)) return "codex.oauth.waiting";
|
|
if (status.activated && status.active_model) return "codex.oauth.activated";
|
|
if (status.connection === "connected") return "codex.oauth.connected";
|
|
if (status.operation_state === "cancelled") return "codex.oauth.cancelled";
|
|
if (status.operation_state === "expired") return "codex.oauth.expired";
|
|
return "codex.oauth.disconnected";
|
|
}
|