mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Replace BrowserCodex with app-server style wasm wrapper
This commit is contained in:
Generated
+1
@@ -2913,6 +2913,7 @@ name = "codex-wasm-harness"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"codex-app-server-protocol",
|
||||
"codex-code-mode",
|
||||
"codex-core",
|
||||
"codex-exec-server",
|
||||
|
||||
@@ -3234,6 +3234,12 @@ impl Session {
|
||||
"Overwriting existing pending elicitation for server_name: {server_name}, request_id: {request_id}"
|
||||
);
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let id = match request_id.clone() {
|
||||
RequestId::String(value) => codex_protocol::mcp::RequestId::String(value.to_string()),
|
||||
RequestId::Number(value) => codex_protocol::mcp::RequestId::Integer(value),
|
||||
};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let id = request_id.clone();
|
||||
let event = EventMsg::ElicitationRequest(ElicitationRequestEvent {
|
||||
turn_id: params.turn_id,
|
||||
@@ -4644,6 +4650,8 @@ mod handlers {
|
||||
use codex_protocol::request_user_input::RequestUserInputResponse;
|
||||
|
||||
use crate::context_manager::is_user_turn_boundary;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use crate::mcp_types::RequestId;
|
||||
use codex_protocol::config_types::CollaborationMode;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::Settings;
|
||||
@@ -4836,6 +4844,12 @@ mod handlers {
|
||||
content,
|
||||
meta,
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let request_id = match request_id {
|
||||
ProtocolRequestId::String(value) => RequestId::String(value.into()),
|
||||
ProtocolRequestId::Integer(value) => RequestId::Number(value),
|
||||
};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let request_id = request_id;
|
||||
if let Err(err) = sess
|
||||
.resolve_elicitation(server_name, request_id, response)
|
||||
|
||||
@@ -137,7 +137,14 @@ impl NetworkProxySpec {
|
||||
None => builder.policy_decider(|_request| async {
|
||||
// In restricted sandbox modes, allowlist misses should ask for
|
||||
// explicit network approval instead of hard-denying.
|
||||
Ok(NetworkDecision::ask("not_allowed"))
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
NetworkDecision::ask("not_allowed")
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
Ok(NetworkDecision::ask("not_allowed"))
|
||||
}
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -794,7 +794,7 @@ async fn maybe_request_mcp_tool_approval(
|
||||
mcp_tool_approval_question_text(question.question, monitor_reason.as_deref());
|
||||
if tool_call_mcp_elicitation_enabled {
|
||||
let request_id =
|
||||
RequestId::String(format!("{MCP_TOOL_APPROVAL_QUESTION_ID_PREFIX}_{call_id}"));
|
||||
RequestId::String(format!("{MCP_TOOL_APPROVAL_QUESTION_ID_PREFIX}_{call_id}").into());
|
||||
let params = build_mcp_tool_approval_elicitation_request(
|
||||
sess.as_ref(),
|
||||
turn_context.as_ref(),
|
||||
|
||||
@@ -545,11 +545,11 @@ pub(crate) fn build_network_policy_decider(
|
||||
let network_policy_decider_session = Arc::clone(&network_policy_decider_session);
|
||||
async move {
|
||||
let Some(session) = network_policy_decider_session.read().await.upgrade() else {
|
||||
return Ok(NetworkDecision::ask("not_allowed"));
|
||||
return NetworkDecision::ask("not_allowed");
|
||||
};
|
||||
Ok(network_approval
|
||||
network_approval
|
||||
.handle_inline_policy_request(session, request)
|
||||
.await)
|
||||
.await
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
async-trait = { workspace = true }
|
||||
codex-app-server-protocol = { workspace = true }
|
||||
codex-code-mode = { workspace = true }
|
||||
codex-core = { workspace = true }
|
||||
codex-exec-server = { workspace = true }
|
||||
@@ -22,7 +23,7 @@ codex-protocol = { workspace = true }
|
||||
js-sys = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
tokio = { workspace = true, features = ["rt"] }
|
||||
tokio = { workspace = true, features = ["rt", "sync"] }
|
||||
wasm-bindgen = { workspace = true }
|
||||
wasm-bindgen-futures = { workspace = true }
|
||||
web-sys = { workspace = true, features = [
|
||||
|
||||
@@ -4,9 +4,11 @@ This crate is the first browser-facing seam for a Codex harness prototype.
|
||||
|
||||
It exposes one browser-facing layer:
|
||||
|
||||
- `BrowserCodex`: a `wasm_bindgen` adapter that starts a real
|
||||
`codex-core::CodexThread`, calls `CodexThread::submit(Op::UserTurn { ... })`,
|
||||
and streams real Codex protocol events back to JavaScript.
|
||||
- `BrowserAppServer`: a `wasm_bindgen` adapter that exposes an app-server-shaped
|
||||
browser boundary. It keeps a live `codex-core::CodexThread` behind a
|
||||
request/event interface, accepts app-server request envelopes such as
|
||||
`thread/start` and `turn/start`, and streams notifications plus raw core
|
||||
events back to JavaScript.
|
||||
|
||||
The demo page passes the user's API key into the WASM facade so Rust can make
|
||||
browser `fetch` requests to the Responses API through the wasm-compatible
|
||||
@@ -17,9 +19,12 @@ The API key field is for local prototype use only: it stores the key in browser
|
||||
integration should use a proxy or an ephemeral-token flow instead of persisting
|
||||
long-lived API keys in the page origin.
|
||||
|
||||
The remaining work is to replace the current browser-only host shims with more
|
||||
complete browser implementations for persistence, richer tools, and other
|
||||
host-heavy services.
|
||||
This replaces the earlier `BrowserCodex.submit_turn(...)` prototype. The
|
||||
browser example now uses the same high-level control plane as native Codex:
|
||||
create or reuse a thread, start turns against it, and listen for async events.
|
||||
The remaining work is to swap the direct core bridge under this wrapper for a
|
||||
deeper reuse of the in-process app-server runtime once the storage/runtime
|
||||
dependencies are wasm-compatible.
|
||||
|
||||
## Library Boundary
|
||||
|
||||
@@ -27,7 +32,7 @@ The intended downstream integration point is the crate itself. Downstream
|
||||
webapps can:
|
||||
|
||||
- depend on `codex-wasm-harness` from a Git branch or local path;
|
||||
- construct `BrowserCodex` from JavaScript or wrap the crate with their own
|
||||
- construct `BrowserAppServer` from JavaScript or wrap the crate with their own
|
||||
browser bindings;
|
||||
- provide session-scoped prompt inputs such as `cwd`, developer instructions,
|
||||
and user/project-doc instructions;
|
||||
@@ -45,22 +50,39 @@ The intended boundary is:
|
||||
Example:
|
||||
|
||||
```js
|
||||
const codex = new BrowserCodex(apiKey);
|
||||
codex.setSessionOptions({
|
||||
const app = new BrowserAppServer(apiKey);
|
||||
app.setSessionOptions({
|
||||
cwd: "/workspace",
|
||||
instructions: {
|
||||
developer: "Code runs inside AppKernel. OPFS and network APIs are available.",
|
||||
user: "# AGENTS.md instructions for /workspace\n\n<INSTRUCTIONS>\n...\n</INSTRUCTIONS>",
|
||||
},
|
||||
});
|
||||
app.setEventHandler((event) => console.log(event));
|
||||
|
||||
const thread = await app.request({
|
||||
method: "thread/start",
|
||||
id: 1,
|
||||
params: {},
|
||||
});
|
||||
|
||||
await app.request({
|
||||
method: "turn/start",
|
||||
id: 2,
|
||||
params: {
|
||||
threadId: thread.thread.id,
|
||||
input: [{ type: "text", text: "Say hello." }],
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
## Current Limitations
|
||||
|
||||
This is a minimal browser port of the Codex turn loop, not full desktop Codex.
|
||||
This is a minimal browser port of the Codex app-server boundary, not full
|
||||
desktop Codex.
|
||||
|
||||
- It uses the real `CodexThread` turn path, but many host-heavy services are
|
||||
still compiled into degraded wasm implementations.
|
||||
- It keeps real Codex threads alive across turns, but many host-heavy services
|
||||
are still compiled into degraded wasm implementations.
|
||||
- It currently relies on a single injected browser code executor for code mode.
|
||||
- Native shell, PTY, MCP, plugin runtime, and filesystem-backed persistence are
|
||||
not available in the browser prototype.
|
||||
|
||||
@@ -202,9 +202,11 @@
|
||||
<section id="events" aria-live="polite"></section>
|
||||
</main>
|
||||
<script type="module">
|
||||
import init, { BrowserCodex } from "../pkg/codex_wasm_harness.js?v=20260414s";
|
||||
import init, {
|
||||
BrowserAppServer,
|
||||
} from "../pkg/codex_wasm_harness.js?v=20260416a";
|
||||
|
||||
await init("../pkg/codex_wasm_harness_bg.wasm?v=20260414s");
|
||||
await init("../pkg/codex_wasm_harness_bg.wasm?v=20260416a");
|
||||
|
||||
const form = document.querySelector("#prompt-form");
|
||||
const input = document.querySelector("#prompt");
|
||||
@@ -213,8 +215,10 @@
|
||||
const apiKeyStatus = document.querySelector("#api-key-status");
|
||||
const clearApiKeyButton = document.querySelector("#clear-api-key");
|
||||
const API_KEY_STORAGE_KEY = "codexWasmHarness.apiKey";
|
||||
const codex = new BrowserCodex(readStoredApiKey());
|
||||
const app = new BrowserAppServer(readStoredApiKey());
|
||||
const sandbox = createSandboxKernel();
|
||||
let nextRequestId = 1;
|
||||
let threadId = null;
|
||||
const browserSessionOptions = {
|
||||
cwd: "/workspace",
|
||||
instructions: {
|
||||
@@ -235,23 +239,27 @@
|
||||
|
||||
apiKeyInput.value = readStoredApiKey();
|
||||
updateApiKeyStatus();
|
||||
codex.setSessionOptions(browserSessionOptions);
|
||||
codex.set_code_executor(async (requestJson) =>
|
||||
app.setSessionOptions(browserSessionOptions);
|
||||
app.setEventHandler(handleServerEvent);
|
||||
app.set_code_executor(async (requestJson) =>
|
||||
sandbox.execute(requestJson),
|
||||
);
|
||||
await app.notify({ method: "initialized" });
|
||||
renderEvent({ type: "page_ready" });
|
||||
|
||||
apiKeyInput.addEventListener("input", () => {
|
||||
const apiKey = apiKeyInput.value.trim();
|
||||
writeStoredApiKey(apiKey);
|
||||
codex.set_api_key(apiKey);
|
||||
threadId = null;
|
||||
app.set_api_key(apiKey);
|
||||
updateApiKeyStatus();
|
||||
});
|
||||
|
||||
clearApiKeyButton.addEventListener("click", () => {
|
||||
apiKeyInput.value = "";
|
||||
writeStoredApiKey("");
|
||||
codex.set_api_key("");
|
||||
threadId = null;
|
||||
app.set_api_key("");
|
||||
updateApiKeyStatus();
|
||||
});
|
||||
|
||||
@@ -260,7 +268,11 @@
|
||||
events.replaceChildren();
|
||||
renderEvent({ type: "submit_clicked" });
|
||||
try {
|
||||
await codex.submit_turn(input.value, renderEvent);
|
||||
const currentThreadId = await ensureThread();
|
||||
await request("turn/start", {
|
||||
threadId: currentThreadId,
|
||||
input: [{ type: "text", text: input.value }],
|
||||
});
|
||||
} catch (error) {
|
||||
renderEvent({
|
||||
type: "page_error",
|
||||
@@ -321,11 +333,35 @@
|
||||
: "No API key stored. Enter a key to run the real Codex harness.";
|
||||
}
|
||||
|
||||
async function ensureThread() {
|
||||
if (threadId) {
|
||||
return threadId;
|
||||
}
|
||||
const response = await request("thread/start", {});
|
||||
threadId = response.thread.id;
|
||||
return threadId;
|
||||
}
|
||||
|
||||
async function request(method, params) {
|
||||
const payload = { method, id: nextRequestId++, params };
|
||||
renderEvent({ type: "client_request", payload });
|
||||
const response = await app.request(payload);
|
||||
renderEvent({ type: "client_response", method, response });
|
||||
return response;
|
||||
}
|
||||
|
||||
function handleServerEvent(event) {
|
||||
if (event?.method === "thread/started") {
|
||||
threadId = event.params?.thread?.id ?? threadId;
|
||||
}
|
||||
renderEvent(event);
|
||||
}
|
||||
|
||||
function renderEvent(event) {
|
||||
const card = document.createElement("article");
|
||||
card.className = "event";
|
||||
const title = document.createElement("strong");
|
||||
title.textContent = event.type;
|
||||
title.textContent = event.method ?? event.type ?? "event";
|
||||
const body = document.createElement("pre");
|
||||
body.textContent = JSON.stringify(event, null, 2);
|
||||
card.append(title, body);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
mod browser;
|
||||
mod error;
|
||||
|
||||
pub use browser::BrowserCodex;
|
||||
pub use browser::BrowserAppServer;
|
||||
pub use error::HarnessError;
|
||||
|
||||
Reference in New Issue
Block a user