Web version, stage 1: the interface as a library over a platform layer; apps/web
apps/desktop becomes a library (chesspenguin_app: the interface) and a thin binary; apps/web starts the same interface in a page's canvas (eframe's WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser. The platform layer (apps/desktop/src/platform): the program's folders, files, background work, the files the user opens or saves, the clipboard and what the platform offers (CAPS). On a computer it is the system as before; in a browser the program's data lives in IndexedDB behind an in-memory file tree loaded before the start, the files the user opens are read into it for the session (only read), background jobs run on the page's thread after the frame, the clipboard is read through the browser's API, sounds play through audio elements, a game is saved as a download. What a browser does not have is not built for it: updates, the online games window and its probe, engine processes (analysis starts a Web Worker there; the review and preparation jobs, Rodent and the judge are native), blocking requests (engine, Maia and Rodent downloads report no download for the platform, translation reports that it makes no requests). The desktop program behaves as before; its build finds unused code (the web build compiles parts it never shows). apps/web: the entry point, the page /app/ (device check before anything large is loaded: phones and tablets get the page about the apps in the site's nine languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile web), a static server and headless Firefox checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a4445d2c15
commit
3e69a5d57c
61 changed files with 2628 additions and 552 deletions
|
|
@ -12,7 +12,8 @@ use std::sync::Arc;
|
|||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{Receiver, channel};
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
use std::time::Duration;
|
||||
use web_time::{Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
||||
|
|
@ -20,7 +21,9 @@ use serde::{Deserialize, Serialize};
|
|||
use serde_json::{Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::translate::{Failure, agent, update_credentials, with_waits};
|
||||
use crate::translate::{Failure, update_credentials};
|
||||
#[cfg(not(web))]
|
||||
use crate::translate::{agent, with_waits};
|
||||
|
||||
const AUTHORIZE: &str = "https://auth.openai.com/api/accounts/authorize";
|
||||
const TOKEN: &str = "https://auth.openai.com/api/accounts/oauth/token";
|
||||
|
|
@ -148,6 +151,13 @@ pub struct SignIn {
|
|||
}
|
||||
|
||||
impl SignIn {
|
||||
/// The web version does not sign in to ChatGPT (it would listen on a port).
|
||||
#[cfg(web)]
|
||||
pub fn start(_ctx: &egui::Context, _host_id: &str, _client_id: &str) -> Result<SignIn, String> {
|
||||
Err("the web version does not sign in to ChatGPT".into())
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// Starts listening for the callback and returns the address to open in the browser.
|
||||
pub fn start(ctx: &egui::Context, host_id: &str, client_id: &str) -> Result<SignIn, String> {
|
||||
let listener = PORTS
|
||||
|
|
@ -217,6 +227,7 @@ impl Drop for SignIn {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// The page the browser tab shows after the sign-in.
|
||||
fn page(ok: bool) -> String {
|
||||
let (title, text) = if ok {
|
||||
|
|
@ -240,6 +251,7 @@ fn page(ok: bool) -> String {
|
|||
)
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
fn reply(stream: &mut TcpStream, status: &str, body: &str) {
|
||||
let _ = write!(
|
||||
stream,
|
||||
|
|
@ -248,6 +260,7 @@ fn reply(stream: &mut TcpStream, status: &str, body: &str) {
|
|||
);
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// Waits for the browser to come back to the callback: (code, issued client id).
|
||||
fn wait_for_callback(
|
||||
listener: &TcpListener,
|
||||
|
|
@ -310,6 +323,7 @@ fn wait_for_callback(
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
fn token_request(body: &str) -> Result<Value, (u16, String)> {
|
||||
let mut resp = agent(Duration::from_secs(30))
|
||||
.post(TOKEN)
|
||||
|
|
@ -340,6 +354,7 @@ fn tokens_from(v: &Value, client_id: &str, old_refresh: &str) -> Tokens {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
fn exchange(
|
||||
code: &str,
|
||||
verifier: &str,
|
||||
|
|
@ -380,9 +395,11 @@ fn exchange(
|
|||
})
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// Refreshes are one at a time: the refresh token rotates.
|
||||
static REFRESH: Mutex<()> = Mutex::new(());
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// A valid access token, refreshed when it is about to expire.
|
||||
fn access_token() -> Result<String, Failure> {
|
||||
let _one = REFRESH.lock().unwrap_or_else(|e| e.into_inner());
|
||||
|
|
@ -398,6 +415,7 @@ fn access_token() -> Result<String, Failure> {
|
|||
refresh(&t)
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
fn refresh(t: &Tokens) -> Result<String, Failure> {
|
||||
let body = form(&[
|
||||
("grant_type", "refresh_token"),
|
||||
|
|
@ -459,11 +477,15 @@ pub fn sign_out() {
|
|||
return;
|
||||
};
|
||||
update_credentials(|c| c.chatgpt = None);
|
||||
#[cfg(not(web))]
|
||||
let _ = std::thread::Builder::new()
|
||||
.name("chatgpt-sign-out".into())
|
||||
.spawn(move || revoke(&t));
|
||||
#[cfg(web)]
|
||||
let _ = t;
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
fn revoke(t: &Tokens) -> bool {
|
||||
let body = form(&[
|
||||
("token", &t.refresh),
|
||||
|
|
@ -489,6 +511,22 @@ fn revoke(t: &Tokens) -> bool {
|
|||
|
||||
// --- Requests ------------------------------------------------------------------------------
|
||||
|
||||
#[cfg(web)]
|
||||
pub fn models() -> Result<Vec<String>, Failure> {
|
||||
Err(crate::translate::no_requests())
|
||||
}
|
||||
|
||||
#[cfg(web)]
|
||||
pub fn respond(
|
||||
_model: &str,
|
||||
_system: &str,
|
||||
_user: &str,
|
||||
_cancel: &AtomicBool,
|
||||
) -> Result<String, Failure> {
|
||||
Err(crate::translate::no_requests())
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// The models this account may use (`visibility: list`).
|
||||
pub fn models() -> Result<Vec<String>, Failure> {
|
||||
let token = access_token()?;
|
||||
|
|
@ -594,6 +632,7 @@ fn failure(status: u16, body: &str) -> Failure {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// One answer of the model, read from the event stream.
|
||||
pub fn respond(
|
||||
model: &str,
|
||||
|
|
@ -655,6 +694,7 @@ pub fn respond(
|
|||
})
|
||||
}
|
||||
|
||||
#[cfg(not(web))]
|
||||
/// The text of a Responses event stream: the deltas until `response.completed`.
|
||||
fn read_stream(reader: impl BufRead, cancel: &AtomicBool) -> Result<String, Failure> {
|
||||
let mut text = String::new();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue