From 40d335951c5f9a1ff5a5fb904b115930a99fde2d Mon Sep 17 00:00:00 2001 From: Ilya Shcherbak Date: Thu, 19 Nov 2026 21:17:01 +0300 Subject: [PATCH 1/5] Add stateless video MCP tools and bundled Codex skill --- Cargo.lock | 247 +++++++- Cargo.toml | 2 + README.md | 31 + crates/wizard-core/Cargo.toml | 7 +- crates/wizard-core/src/agents/codex.rs | 106 +++- crates/wizard-core/src/hub/mod.rs | 4 + crates/wizard-core/src/lib.rs | 3 + crates/wizard-core/src/video/install.rs | 171 ++++++ crates/wizard-core/src/video/mod.rs | 418 +++++++++++++ crates/wizard-core/src/video/server.rs | 195 ++++++ crates/wizard-core/src/video/tests.rs | 570 ++++++++++++++++++ crates/wizard-core/tests/fixtures/blue.mp4 | Bin 0 -> 1591 bytes crates/wizard-core/tests/video_clients.py | 110 ++++ crates/wizard-gui/Cargo.toml | 1 + crates/wizard-gui/src/agents.rs | 7 +- crates/wizard-gui/src/hub_tokens.rs | 21 +- crates/wizard-gui/src/lib.rs | 6 + crates/wizard-gui/src/video.rs | 315 ++++++++++ .../wizard-ui/src/agents/AgentPage.tsx | 2 + .../wizard-ui/src/agents/VideoConnection.tsx | 80 +++ crates/wizard-settings/src/lib.rs | 30 + skills/coreinfra-video/SKILL.md | 14 + skills/coreinfra-video/agents/openai.yaml | 10 + 23 files changed, 2326 insertions(+), 24 deletions(-) create mode 100644 crates/wizard-core/src/video/install.rs create mode 100644 crates/wizard-core/src/video/mod.rs create mode 100644 crates/wizard-core/src/video/server.rs create mode 100644 crates/wizard-core/src/video/tests.rs create mode 100644 crates/wizard-core/tests/fixtures/blue.mp4 create mode 100644 crates/wizard-core/tests/video_clients.py create mode 100644 crates/wizard-gui/src/video.rs create mode 100644 crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx create mode 100644 skills/coreinfra-video/SKILL.md create mode 100644 skills/coreinfra-video/agents/openai.yaml diff --git a/Cargo.lock b/Cargo.lock index 29b475a..137c952 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -348,6 +348,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "block2" version = "0.6.2" @@ -537,6 +546,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core", +] + [[package]] name = "chrono" version = "0.4.45" @@ -614,6 +634,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + [[package]] name = "cookie" version = "0.18.2" @@ -673,6 +699,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.5.1" @@ -707,6 +742,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + [[package]] name = "cssparser" version = "0.36.0" @@ -753,9 +797,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.2.17", "curve25519-dalek-derive", - "digest", + "digest 0.10.7", "fiat-crypto", "rustc_version", "subtle", @@ -884,8 +928,19 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid", + "crypto-common 0.2.2", ] [[package]] @@ -1058,7 +1113,7 @@ checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" dependencies = [ "curve25519-dalek", "ed25519", - "sha2", + "sha2 0.10.9", "subtle", ] @@ -1291,6 +1346,21 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.34" @@ -1298,6 +1368,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -1365,6 +1436,7 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -1516,6 +1588,7 @@ dependencies = [ "cfg-if", "libc", "r-efi 6.0.0", + "rand_core", ] [[package]] @@ -1757,6 +1830,15 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.11.1" @@ -2809,6 +2891,12 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "pastey" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" + [[package]] name = "percent-encoding" version = "2.3.2" @@ -3072,6 +3160,23 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f6fa277c67e6ea590466f04c3fe82cb509f0864fe470643e5570de9514a9b11" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -3211,6 +3316,42 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rmcp" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "391f153431ab68664d946513ea0cb3edbca78eaf99c5a983036cacab6220d85b" +dependencies = [ + "async-trait", + "base64 0.23.1", + "bytes", + "chrono", + "futures", + "http", + "http-body", + "http-body-util", + "indexmap 2.14.2", + "libc", + "pastey", + "percent-encoding", + "pin-project-lite", + "rand", + "schemars 1.2.2", + "serde", + "serde_json", + "sha2 0.11.1", + "sse-stream", + "thiserror 2.0.20", + "tokio", + "tokio-stream", + "tokio-util", + "tower-service", + "tracing", + "unicode-general-category", + "unicode-normalization", + "uuid", +] + [[package]] name = "rustc-hash" version = "2.1.3" @@ -3344,7 +3485,7 @@ checksum = "3fbf2ae1b8bc8e02df939598064d22402220cd5bbcca1c76f7d6a310974d5615" dependencies = [ "dyn-clone", "indexmap 1.9.3", - "schemars_derive", + "schemars_derive 0.8.22", "serde", "serde_json", "url", @@ -3369,8 +3510,10 @@ version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "687274d293b6cdc6e73e0fee520bf2049650090d7164f87672d212a3c530cf4a" dependencies = [ + "chrono", "dyn-clone", "ref-cast", + "schemars_derive 1.2.2", "serde", "serde_json", ] @@ -3383,10 +3526,22 @@ checksum = "32e265784ad618884abaea0600a9adf15393368d840e0222d101a072f3f7534d" dependencies = [ "proc-macro2", "quote", - "serde_derive_internals", + "serde_derive_internals 0.29.1", "syn 2.0.119", ] +[[package]] +name = "schemars_derive" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98c67716b46af2f0b8cf752abc930f6f9aecfbf671ecfb531db8a31dbe4e2ba" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals 0.30.0", + "syn 3.0.5", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -3498,6 +3653,17 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "serde_derive_internals" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f852137cce035d6a4df67ccce505ff6b3e9fd3a10e3e52b24dc71e650bb1a9bd" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + [[package]] name = "serde_json" version = "1.0.151" @@ -3612,8 +3778,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -3736,6 +3913,19 @@ dependencies = [ "system-deps", ] +[[package]] +name = "sse-stream" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c25ac7aff0abd1dbc474536e40416e1102c7dd9bfba0b9861c6d357f835dcfb4" +dependencies = [ + "bytes", + "futures-util", + "http-body", + "http-body-util", + "pin-project-lite", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -4011,7 +4201,7 @@ dependencies = [ "semver", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "syn 2.0.119", "tauri-utils", "thiserror 2.0.20", @@ -4390,6 +4580,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-util" version = "0.7.19" @@ -4399,6 +4600,7 @@ dependencies = [ "bytes", "futures-core", "futures-sink", + "libc", "pin-project-lite", "tokio", ] @@ -4694,12 +4896,27 @@ dependencies = [ "unic-common", ] +[[package]] +name = "unicode-general-category" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b993bddc193ae5bd0d623b49ec06ac3e9312875fdae725a975c51db1cc1677f" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-normalization" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cd3fb03d5288b3ba9a1ee103ac7e9cbb59f0b03d64f8ddfcc04dd42758a85d" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-segmentation" version = "1.13.3" @@ -5512,6 +5729,9 @@ dependencies = [ "cookie", "dirs 6.0.0", "ed25519-dalek", + "http-body-util", + "hyper", + "hyper-util", "jsonc-parser", "libc", "log", @@ -5522,11 +5742,13 @@ dependencies = [ "quick-xml", "regex", "reqwest", + "rmcp", "rustix", + "schemars 1.2.2", "semver", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "tempfile", "tokio", "tokio-rustls", @@ -5553,6 +5775,7 @@ dependencies = [ "tauri-plugin-single-instance", "tauri-plugin-updater", "tokio", + "uuid", "wizard-core", "wizard-proxy", "wizard-proxy-daemon", @@ -5659,7 +5882,7 @@ dependencies = [ "once_cell", "percent-encoding", "raw-window-handle", - "sha2", + "sha2 0.10.9", "soup3", "tao-macros", "thiserror 2.0.20", diff --git a/Cargo.toml b/Cargo.toml index d663e95..66b40ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,9 +70,11 @@ windows = "0.61.3" plist = "1.10.1" quick-xml = "0.42.0" regex = "1.13.1" +rmcp = { version = "3.11.0", default-features = false, features = ["server", "transport-streamable-http-server"] } reqwest = { version = "0.13.5", default-features = false, features = ["rustls-no-provider"] } rustls-native-certs = "0.8.4" semver = "1.0.28" +schemars = "1.2.2" sha2 = "0.10.9" serde = { version = "1", features = ["derive"] } serde_json = "1.0.151" diff --git a/README.md b/README.md index d2688ef..476833f 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,37 @@ An application for configuring agents to use CoreInfra. +## Video in ChatGPT Work and Codex + +In Wizard, sign in, select a Hub, then open **ChatGPT / Codex → Видеомодели CoreInfra**. Select the API key by its Hub name and click **Подключить видеомодели**. Matching names show a short fingerprint. Keep Wizard running and ask for a video in a new chat, or invoke `$coreinfra-video`. + +Wizard installs the bundled skill under the user's Codex skills directory and registers `coreinfra_video` at `http://127.0.0.1:33789/mcp` in the same Codex profile. Wizard refreshes its bundled files on startup. **Отключить** revokes local access and removes the managed registration and unmodified skill files. Updates preserve user edits and report file conflicts. Registration works with the profile selected by `CODEX_HOME`. + +`video_models` returns the public catalog and prices in RUB per second. `video_create` submits one paid job. Save the full returned `job` object and pass it to `video_result` to poll, including after restarting Wizard. The original Hub and key must still be available. A completed download returns a local MP4 in **Downloads/CoreInfra Video**. Use `download: false` to poll without creating a file. fal also returns its signed result URL in this mode. Repeating a submission after a network timeout can incur another charge. + +The local bearer in the Codex MCP registration grants access to video operations for the selected Hub key. Wizard resolves that key in the signed-in account for each authenticated operation. Job references contain the Hub ID, key fingerprint, provider, model and upstream job ID. Wizard keeps no job table or MCP session. A Python MCP client with permission to reach loopback can use the same URL and local `http_headers`. ChatGPT Work can use the registered tools with shell network access restricted. + +### Custom video fields + +Put provider-specific JSON in `fields`. Wizard merges its entries into the outgoing JSON body and preserves nested objects, arrays, numbers, booleans and nulls. For clients that cannot express open-ended object arguments, `fields_json` accepts the same object as a JSON string. Use one representation per request. `model` and `prompt` are reserved tool arguments. The destination, authentication and HTTP headers are controlled by Wizard. + +```json +{ + "model": "blackforestlabs/flux-3-max/image-to-video", + "prompt": "The paper boat drifts downstream", + "fields": { + "image_url": "data:image/png;base64,", + "duration": 5, + "seed": 0, + "generate_audio": false + } +} +``` + +For OpenRouter, put per-provider options under `fields.provider.options` and inline frame images under `fields.frame_images`. The Hub strips other OpenRouter routing options and `callback_url`. fal accepts inline `image_url` and `end_image_url` and uses polling for job status. The MCP request body limit is 32 MiB including base64 encoding. The Hub and model determine which fields they honor. [OpenRouter media](https://coreinfra.ru/docs/use/openrouter) · [fal queue](https://coreinfra.ru/docs/use/fal) + +The [client compatibility test](crates/wizard-core/tests/video_clients.py) includes a Python SDK 2.x example using `streamable_http_client`. To repeat the end-to-end test, set `WIZARD_VIDEO_TEST_PYTHON` to a Python with `mcp` 2.x installed and `WIZARD_VIDEO_TEST_CODEX` to a Codex executable. Run `cargo test -p wizard-core native_and_python_clients -- --ignored --nocapture`. All submissions in this test go to its temporary local Hub. + ## How to build 1. Install Rust and Node.js: diff --git a/crates/wizard-core/Cargo.toml b/crates/wizard-core/Cargo.toml index 931f6cf..d6d47c5 100644 --- a/crates/wizard-core/Cargo.toml +++ b/crates/wizard-core/Cargo.toml @@ -16,18 +16,23 @@ anyhow.workspace = true base64.workspace = true chrono.workspace = true cookie.workspace = true +http-body-util.workspace = true +hyper = { workspace = true, features = ["server", "http1"] } +hyper-util = { workspace = true, features = ["tokio"] } dirs.workspace = true ed25519-dalek.workspace = true log.workspace = true regex.workspace = true +rmcp.workspace = true quick-xml.workspace = true reqwest.workspace = true serde.workspace = true serde_json = { workspace = true, features = ["preserve_order"] } # keep key order in user files semver.workspace = true +schemars.workspace = true sha2.workspace = true jsonc-parser.workspace = true -tokio = { workspace = true, features = ["rt"] } +tokio = { workspace = true, features = ["rt", "fs", "macros"] } toml_edit = { workspace = true, features = ["serde"] } tempfile.workspace = true tokio-rustls.workspace = true diff --git a/crates/wizard-core/src/agents/codex.rs b/crates/wizard-core/src/agents/codex.rs index d5dd666..73a4b04 100644 --- a/crates/wizard-core/src/agents/codex.rs +++ b/crates/wizard-core/src/agents/codex.rs @@ -72,7 +72,7 @@ pub(super) fn detect(settings: &Settings) -> AgentDetection { }) } -fn config_path() -> Result { +pub(crate) fn config_path() -> Result { if let Some(home) = env_var_not_empty("CODEX_HOME") { return Ok(PathBuf::from(home).join("config.toml")); } @@ -112,8 +112,11 @@ pub fn get_backups() -> Result> { /// which may interrupt work. pub fn restore_backup(id: u32, settings: &Settings) -> Result<()> { let _guard = PreparedAgent::Codex.lock()?; - BackupManager::new(AgentKind::Codex)?.load_backup(id)?; let path = std::path::absolute(config_path()?)?; + let backups = BackupManager::new(AgentKind::Codex)?; + let mut changes = backups.restore_changes(id)?; + protect_restored_configs(&mut changes, &path, settings)?; + backups.apply_changes(&changes)?; // Backups hold files only; the restored mode decides whether ChatGPT updates itself. #[cfg(target_os = "macos")] super::chatgpt_updates::reconcile( @@ -129,6 +132,28 @@ pub fn restore_backup(id: u32, settings: &Settings) -> Result<()> { Ok(()) } +fn protect_restored_configs( + changes: &mut Vec, + active: &Path, + settings: &Settings, +) -> Result<()> { + if !changes.iter().any(|c| c.path() == active) { + changes.push(FileChange::read(active.to_owned())?); + } + for change in changes { + if change + .path() + .file_name() + .is_some_and(|n| n == "config.toml") + { + let connection = settings.video.as_ref().filter(|_| change.path() == active); + config_files::toml::update(&mut change.after, exclude_hub_key)?; + crate::video::install::update_config(&mut change.after, None, connection)?; + } + } + Ok(()) +} + /// Call from a Tokio blocking worker. Restart only if the version probe finds a managed daemon. /// A restart may interrupt active work; this does not change routing configuration. pub fn restart_daemon(executable: &Path, codex_home: &Path) -> Result<()> { @@ -473,12 +498,7 @@ fn daemon_executable(settings: &Settings) -> Result> { ) } -const API_FILTERS: &[&str] = &[ - "COREINFRA_API_KEY", - "COREINFRA_PROXY_URL", - "HTTPS_PROXY", - "NO_PROXY", -]; +const API_FILTERS: &[&str] = &["COREINFRA_PROXY_URL", "HTTPS_PROXY", "NO_PROXY"]; fn remove_inactive_routing_settings( doc: &mut DocumentMut, @@ -504,6 +524,7 @@ fn remove_inactive_routing_settings( } fn write_routing_settings(doc: &mut DocumentMut, mode: RoutingMode) -> Result<()> { + exclude_hub_key(doc)?; let (provider, name, base_url) = match mode { RoutingMode::Disabled => return Ok(()), RoutingMode::Hub => ( @@ -573,6 +594,27 @@ fn write_routing_settings(doc: &mut DocumentMut, mode: RoutingMode) -> Result<() Ok(()) } +pub(crate) fn exclude_hub_key(doc: &mut DocumentMut) -> Result<()> { + config_files::toml::set_value( + doc, + &["shell_environment_policy", "filters", "COREINFRA_API_KEY"], + "exclude", + ) +} + +/// Upgrade older Wizard-managed API profiles at startup, including installations without video. +pub fn migrate_hub_key_filter() -> Result<()> { + let _guard = PreparedAgent::Codex.lock()?; + let mut config = FileChange::read(config_path()?)?; + config_files::toml::update(&mut config.after, |doc| { + if mode_from_config(doc) != RoutingMode::Disabled { + exclude_hub_key(doc)?; + } + Ok(()) + })?; + FileChange::apply_all(&[config]) +} + fn remove_inactive_routing_env( after: &mut FileSnapshot, current_mode: RoutingMode, @@ -620,6 +662,54 @@ mod tests { use super::*; use serde_json::json; + #[test] + fn excludes_hub_key_across_modes_and_old_backup_restores() -> Result<()> { + let mut doc: DocumentMut = + "[shell_environment_policy.filters]\nKEEP='include'\n".parse()?; + for mode in [ + RoutingMode::Sub, + RoutingMode::Hub, + RoutingMode::Disabled, + RoutingMode::Hub, + ] { + let before = mode_from_config(&doc); + remove_inactive_routing_settings(&mut doc, before)?; + write_routing_settings(&mut doc, mode)?; + for (key, value) in [("KEEP", "include"), ("COREINFRA_API_KEY", "exclude")] { + assert_eq!( + config_files::toml::get_string( + &doc, + &["shell_environment_policy", "filters", key] + ), + Some(value) + ); + } + } + let dir = tempfile::tempdir()?; + let active = dir.path().join("config.toml"); + let old = dir.path().join("previous-home/config.toml"); + let mut backup = FileChange::read(old.clone())?; + backup.after = FileSnapshot::new("model_provider='coreinfra'\n".into(), 0o644); + let settings = Settings { + video: Some(wizard_settings::VideoConnection { + account_id: "p".into(), + hub_id: "h".into(), + key_name: "video".into(), + key_id: "id".into(), + local_bearer: "fixture-local-grant".into(), + }), + ..Settings::default() + }; + let mut changes = vec![backup]; + protect_restored_configs(&mut changes, &active, &settings)?; + FileChange::apply_all(&changes)?; + let old_config = std::fs::read_to_string(old)?; + assert!(old_config.contains("exclude")); + assert!(!old_config.contains("fixture-local-grant")); + assert!(std::fs::read_to_string(active)?.contains("fixture-local-grant")); + Ok(()) + } + #[test] fn chatgpt_can_be_configured_before_its_first_launch() -> Result<()> { let directory = tempfile::tempdir()?; diff --git a/crates/wizard-core/src/hub/mod.rs b/crates/wizard-core/src/hub/mod.rs index 4d977f1..a5f379c 100644 --- a/crates/wizard-core/src/hub/mod.rs +++ b/crates/wizard-core/src/hub/mod.rs @@ -260,6 +260,10 @@ pub struct AgentToken { pub struct AgentTokens(Vec); impl AgentTokens { + pub fn iter(&self) -> impl Iterator { + self.0.iter() + } + /// Retains only the tokens that satisfy the predicate. pub fn retain(&mut self, predicate: impl FnMut(&AgentToken) -> bool) { self.0.retain(predicate); diff --git a/crates/wizard-core/src/lib.rs b/crates/wizard-core/src/lib.rs index 087baf1..d79b700 100644 --- a/crates/wizard-core/src/lib.rs +++ b/crates/wizard-core/src/lib.rs @@ -1,3 +1,5 @@ +extern crate alloc; + use anyhow::ensure; use regex::Regex; use std::sync::LazyLock; @@ -7,6 +9,7 @@ mod config_files; pub mod hub; mod platform; pub mod prepared_plan; +pub mod video; pub fn validate_token(token: &str) -> anyhow::Result<()> { static TOKEN_REGEX: LazyLock = LazyLock::new(|| { diff --git a/crates/wizard-core/src/video/install.rs b/crates/wizard-core/src/video/install.rs new file mode 100644 index 0000000..2421e0f --- /dev/null +++ b/crates/wizard-core/src/video/install.rs @@ -0,0 +1,171 @@ +//! Reconcile only the Wizard-owned MCP registration and bundled skill documents. +use crate::{ + agents::codex, + config_files::{ + changes::{FileChange, FileSnapshot}, + toml, + }, + prepared_plan::PreparedAgent, +}; +use alloc::collections::BTreeMap; +use anyhow::{Context as _, Result, ensure}; +use std::path::Path; +use toml_edit::DocumentMut; +use wizard_settings::VideoConnection; + +const SERVER: &str = "coreinfra_video"; +const SKILL: &[(&str, &str)] = &[ + ( + "SKILL.md", + include_str!("../../../../skills/coreinfra-video/SKILL.md"), + ), + ( + "agents/openai.yaml", + include_str!("../../../../skills/coreinfra-video/agents/openai.yaml"), + ), +]; + +pub fn sync(previous: Option<&VideoConnection>, next: Option<&VideoConnection>) -> Result<()> { + let _guard = PreparedAgent::Codex.lock()?; + sync_at(&codex::config_path()?, previous, next) +} + +pub(super) fn sync_at( + path: &Path, + previous: Option<&VideoConnection>, + next: Option<&VideoConnection>, +) -> Result<()> { + let mut config = FileChange::read(path.to_owned())?; + update_config(&mut config.after, previous, next)?; + if next.is_none() { + // Revoke the registration even when a user-edited skill cannot be cleaned up. + FileChange::apply_all(core::slice::from_ref(&config))?; + } + let root = path + .parent() + .context("Missing Codex home")? + .join("skills/coreinfra-video"); + for dir in [&root, &root.join("agents")] { + ensure!( + !dir.is_symlink(), + "Video skill directory is a symlink: {}", + dir.display() + ); + } + let mut manifest = FileChange::read(root.join(".wizard-managed.json"))?; + let old: BTreeMap = manifest + .before() + .content() + .map(serde_json::from_str) + .transpose() + .context("Invalid Wizard skill manifest")? + .unwrap_or_default(); + let mut new = BTreeMap::new(); + let mut changes = Vec::new(); + for &(name, text) in SKILL { + let mut file = FileChange::read(root.join(name))?; + let owned = file + .before() + .content() + .is_some_and(|v| old.get(name) == Some(&super::fingerprint(v))); + if next.is_some() { + ensure!( + owned + || file.before().content().is_none() + || (old.contains_key(name) && file.before().content() == Some(text)), + "Файл скилла изменён пользователем: {}", + file.path().display() + ); + file.after = FileSnapshot::new(text.into(), 0o644); + new.insert(name, super::fingerprint(text)); + } else if owned { + file.after = FileSnapshot::Missing; + } + changes.push(file); + } + manifest.after = if next.is_some() { + FileSnapshot::new(serde_json::to_string_pretty(&new)?, 0o600) + } else { + FileSnapshot::Missing + }; + if next.is_some() && manifest.before().content().is_none() { + // A retry can recognize documents written before a first install was interrupted. + changes.insert(0, manifest); + } else { + changes.push(manifest); + } + if next.is_some() { + changes.push(config); + } + FileChange::apply_all(&changes) +} + +pub(crate) fn update_config( + snapshot: &mut FileSnapshot, + previous: Option<&VideoConnection>, + next: Option<&VideoConnection>, +) -> Result<()> { + toml::update(snapshot, |doc| { + registration(doc, previous, next)?; + if next.is_some() { + codex::exclude_hub_key(doc)?; + } + Ok(()) + })?; + #[cfg(unix)] + if next.is_some() + && let FileSnapshot::Present { mode, .. } = snapshot + { + *mode &= 0o600; + } + Ok(()) +} + +fn registration( + doc: &mut DocumentMut, + previous: Option<&VideoConnection>, + next: Option<&VideoConnection>, +) -> Result<()> { + let key = ["mcp_servers", SERVER]; + let table = doc.get("mcp_servers").and_then(|i| i.get(SERVER)); + let needs_timeout = table.and_then(|s| s.get("tool_timeout_sec")).is_none(); + if table.is_some() { + let url = toml::get_string(doc, &["mcp_servers", SERVER, "url"]); + let auth = toml::get_string( + doc, + &["mcp_servers", SERVER, "http_headers", "Authorization"], + ); + let ours = url == Some(super::URL) + && auth.is_some_and(|value| { + value.strip_prefix("Bearer ").is_some_and(|bearer| { + previous + .into_iter() + .chain(next) + .any(|c| c.local_bearer == bearer) + || bearer + .strip_prefix("wizard-video-") + .is_some_and(|id| uuid::Uuid::parse_str(id).is_ok()) + }) + }); + if !ours { + ensure!( + next.is_none(), + "Имя MCP-сервера coreinfra_video уже занято в config.toml" + ); + return Ok(()); + } + } + if let Some(connection) = next { + toml::set_value(doc, &["mcp_servers", SERVER, "url"], super::URL)?; + if needs_timeout { + toml::set_value(doc, &["mcp_servers", SERVER, "tool_timeout_sec"], 240)?; + } + toml::set_value( + doc, + &["mcp_servers", SERVER, "http_headers", "Authorization"], + format!("Bearer {}", connection.local_bearer), + ) + } else { + toml::remove(doc, &key) + } +} diff --git a/crates/wizard-core/src/video/mod.rs b/crates/wizard-core/src/video/mod.rs new file mode 100644 index 0000000..9c9169d --- /dev/null +++ b/crates/wizard-core/src/video/mod.rs @@ -0,0 +1,418 @@ +//! Stateless video tools. Job references are carried by the caller and contain no credential. +pub mod install; +pub mod server; + +use alloc::collections::BTreeMap; +use anyhow::{Context as _, Result, anyhow, bail, ensure}; +use core::{future::Future, time::Duration}; +use reqwest::{Client, RequestBuilder, Response, Url}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value, json}; +use sha2::{Digest as _, Sha256}; +use std::path::{Path, PathBuf}; +use tokio::io::AsyncWriteExt as _; +use wizard_settings::VideoConnection; + +pub const ADDRESS: &str = "127.0.0.1:33789"; +pub const URL: &str = "http://127.0.0.1:33789/mcp"; +const OPENROUTER: &str = "/openrouter/api/v1/videos"; +const FAL: &str = "/fal/api"; + +#[must_use] +pub fn fingerprint(secret: &str) -> String { + format!("{:x}", Sha256::digest(secret.as_bytes())) +} + +#[derive(Clone, Copy, Deserialize, Serialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +enum Provider { + OpenRouter, + Fal, +} + +#[derive(Serialize)] +struct Model { + id: String, + provider: Provider, + name: Option, + min_rub_per_second: Option, + max_rub_per_second: Option, +} + +#[derive(Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +struct Create { + model: String, + /// Video description, when the selected model requires one. + prompt: Option, + /// Extra top-level fields of the provider's JSON body, including inline image inputs. + fields: Option>, + /// Alternative for clients that require fixed object schemas. A JSON object string. + fields_json: Option, +} + +impl Create { + fn into_body(self) -> Result<(String, Map)> { + let mut body = match (self.fields, self.fields_json) { + (Some(_), Some(_)) => bail!("Use either fields or fields_json"), + (Some(fields), None) => fields, + (None, Some(text)) => serde_json::from_str(&text) + .map_err(|_| anyhow!("fields_json must encode a JSON object"))?, + (None, None) => Map::new(), + }; + ensure!( + !body.contains_key("model") && !body.contains_key("prompt"), + "Put model and prompt in the named tool arguments" + ); + ensure!( + self.prompt.as_ref().is_some_and(|p| !p.trim().is_empty()) || !body.is_empty(), + "Provide a prompt or model-specific inputs" + ); + if let Some(prompt) = self.prompt { + body.insert("prompt".into(), prompt.into()); + } + Ok((self.model, body)) + } +} + +#[derive(Serialize, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +struct Job { + provider: Provider, + model: String, + id: String, + hub_id: String, + key_id: String, +} + +#[derive(Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +struct GetResult { + /// Copy the entire job object returned by `video_create`. + job: Job, + /// Save a completed MP4 to the user's Downloads folder. Defaults to true. + download: Option, + /// Zero-based `OpenRouter` output index. fal has one output. + #[serde(default)] + index: u32, +} + +#[derive(Clone)] +pub struct VideoHub { + client: Client, + origin: Url, + output_dir: Option, +} + +impl VideoHub { + pub fn new() -> Result { + Ok(Self { + client: Client::builder() + .user_agent(wizard_proxy::user_agent()) + .redirect(reqwest::redirect::Policy::none()) + .retry(reqwest::retry::never()) + .connect_timeout(Duration::from_secs(10)) + .timeout(Duration::from_secs(180)) + .build() + .map_err(|_| anyhow!("Could not initialize the video HTTP client"))?, + origin: Url::parse(crate::hub::HUB_ORIGIN)?, + output_dir: dirs::download_dir() + .or_else(|| dirs::home_dir().map(|dir| dir.join("Downloads"))) + .map(|dir| dir.join("CoreInfra Video")), + }) + } + + async fn models(&self) -> Result> { + #[derive(Deserialize)] + struct Prices { + providers: BTreeMap, + } + #[derive(Deserialize)] + struct Group { + models: BTreeMap, + } + #[derive(Deserialize)] + struct Entry { + category: String, + endpoints: Vec, + display_name: Option, + prices: Rates, + lowest_prices: Option, + } + #[derive(Deserialize)] + struct Rates { + video_seconds: Option, + } + let value = read_json(self.client.get(self.origin.join("/hub/api/prices")?)).await?; + let prices: Prices = serde_json::from_value(value).context("Invalid Hub price catalog")?; + let mut models = BTreeMap::new(); + for (id, entry) in prices + .providers + .into_values() + .flat_map(|group| group.models) + { + if entry.category != "videos" { + continue; + } + let Some(provider) = entry.endpoints.iter().find_map(|path| match path.as_str() { + OPENROUTER => Some(Provider::OpenRouter), + FAL => Some(Provider::Fal), + _ => None, + }) else { + continue; + }; + let max = entry.prices.video_seconds.map(|p| p / 1_000_000.0); + let min = entry + .lowest_prices + .and_then(|p| p.video_seconds) + .map(|p| p / 1_000_000.0) + .or(max); + models.insert( + id.clone(), + Model { + id, + provider, + name: entry.display_name, + min_rub_per_second: min, + max_rub_per_second: max, + }, + ); + } + Ok(models) + } + + async fn create( + &self, + input: Create, + connection: &VideoConnection, + resolve_token: impl Future>, + ) -> Result { + let (model_id, mut body) = input.into_body()?; + let model = self + .models() + .await? + .remove(&model_id) + .context("Unknown video model. Call video_models for current IDs")?; + let url = match model.provider { + Provider::OpenRouter => { + body.insert("model".into(), model_id.into()); + self.origin.join(OPENROUTER)? + } + Provider::Fal => self + .origin + .join(&format!("{FAL}/{}", fal_model(&model.id)?))?, + }; + let token = resolve_token.await?; + let req = self + .client + .post(url) + .bearer_auth(&token) + .header("content-type", "application/json") + .body(serde_json::to_vec(&body)?); + let response = read_json(req).await.context("Video submission failed. If the outcome is uncertain, do not submit again automatically")?; + let id_field = if model.provider == Provider::Fal { + "request_id" + } else { + "id" + }; + let id = response[id_field].as_str().filter(|id| safe_segment(id)) + .context("Hub accepted the submission but returned no usable job ID; do not submit again automatically")?; + let job = Job { + provider: model.provider, + model: model.id, + id: id.to_owned(), + hub_id: connection.hub_id.clone(), + key_id: connection.key_id.clone(), + }; + Ok(json!({"job":job, "status":response["status"].as_str().unwrap_or("pending")})) + } + + async fn result( + &self, + input: GetResult, + connection: &VideoConnection, + resolve_token: impl Future>, + ) -> Result { + let job = &input.job; + ensure!( + job.key_id == connection.key_id && job.hub_id == connection.hub_id, + "Reconnect the original Hub and video key in Wizard to read this job" + ); + ensure!(safe_segment(&job.id), "Invalid job ID"); + let path = match job.provider { + Provider::OpenRouter => format!("{OPENROUTER}/{}", job.id), + Provider::Fal => { + ensure!(input.index == 0, "fal has only output index 0"); + let model = fal_model(&job.model)?; + let (owner, rest) = model.split_once('/').context("Invalid fal model")?; + let app = rest.split('/').next().context("Invalid fal model")?; + format!("{FAL}/{owner}/{app}/requests/{}", job.id) + } + }; + let token = resolve_token.await?; + let url = self.origin.join(&path)?; + let status_url = if job.provider == Provider::Fal { + self.origin.join(&format!("{path}/status"))? + } else { + url.clone() + }; + let status = read_json(self.client.get(status_url).bearer_auth(&token)).await?; + let state = status["status"] + .as_str() + .context("Hub response missing job status")?; + let mut result = json!({"job":job, "status":state}); + if let Some(cost) = status.pointer("/usage/cost_rub").and_then(Value::as_number) { + result["cost_rub"] = json!(cost); + } + if !state.eq_ignore_ascii_case("completed") { + return Ok(result); + } + + let download = input.download.unwrap_or(true); + let request = match job.provider { + Provider::OpenRouter => { + if !download { + return Ok(result); + } + let mut content = self.origin.join(&format!("{path}/content"))?; + content + .query_pairs_mut() + .append_pair("index", &input.index.to_string()); + self.client.get(content).bearer_auth(&token) + } + Provider::Fal => { + let completed = read_json(self.client.get(url).bearer_auth(&token)).await?; + let signed = completed + .pointer("/video/url") + .and_then(Value::as_str) + .context("Hub response missing video URL")?; + let content = Url::parse(signed).context("Invalid video URL")?; + ensure!( + content.origin() == self.origin.origin() + && content.path() == format!("{path}/content") + && content.username().is_empty() + && content.password().is_none() + && content.fragment().is_none() + && content + .query_pairs() + .any(|(name, value)| name == "t" && !value.is_empty()), + "Invalid Hub video download URL" + ); + if !download { + result["url"] = json!(content.as_str()); + return Ok(result); + } + self.client.get(content) + } + }; + let directory = self + .output_dir + .as_deref() + .context("Downloads directory unavailable")?; + let response = send(request).await?; + result["path"] = json!(save_video(directory, response).await?); + Ok(result) + } +} + +fn safe_segment(part: &str) -> bool { + !part.is_empty() + && part.len() <= 256 + && part != "." + && part != ".." + && part + .bytes() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, b'.' | b'-' | b'_')) +} + +fn fal_model(model: &str) -> Result<&str> { + ensure!( + model.contains('/') && model.split('/').all(safe_segment), + "Invalid fal video model" + ); + Ok(model) +} + +async fn send(request: RequestBuilder) -> Result { + let response = request + .send() + .await + .map_err(|_| anyhow!("Could not reach CoreInfra Hub"))?; + let status = response.status(); + if !status.is_success() { + if let Some(seconds) = response + .headers() + .get(reqwest::header::RETRY_AFTER) + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.parse::().ok()) + { + bail!( + "CoreInfra Hub returned HTTP {} (Retry-After: {seconds}s)", + status.as_u16() + ); + } + bail!("CoreInfra Hub returned HTTP {}", status.as_u16()); + } + Ok(response) +} + +async fn read_json(request: RequestBuilder) -> Result { + let mut response = send(request).await?; + let mut bytes = Vec::new(); + while let Some(chunk) = response + .chunk() + .await + .map_err(|_| anyhow!("Incomplete Hub response"))? + { + ensure!( + bytes.len() + chunk.len() <= 8 * 1024 * 1024, + "Hub JSON response is too large" + ); + bytes.extend_from_slice(&chunk); + } + serde_json::from_slice(&bytes).map_err(|_| anyhow!("Invalid JSON from CoreInfra Hub")) +} + +async fn save_video(dir: &Path, mut response: Response) -> Result { + if let Some(mime) = response.headers().get(reqwest::header::CONTENT_TYPE) { + let mime = mime + .to_str() + .unwrap_or("") + .split(';') + .next() + .unwrap_or("") + .trim(); + ensure!( + ["video/mp4", "application/mp4", "application/octet-stream"] + .iter() + .any(|t| mime.eq_ignore_ascii_case(t)), + "Hub returned a non-MP4 response" + ); + } + tokio::fs::create_dir_all(dir).await?; + let file = tempfile::Builder::new() + .prefix("video-") + .suffix(".part") + .tempfile_in(dir)?; + let (file, path) = file.into_parts(); + let mut file = tokio::fs::File::from_std(file); + let mut size = 0_u64; + while let Some(chunk) = response + .chunk() + .await + .map_err(|_| anyhow!("Incomplete video download"))? + { + size += chunk.len() as u64; + file.write_all(&chunk).await?; + } + ensure!(size > 0, "Empty video from CoreInfra Hub"); + file.flush().await?; + drop(file); + let output = path.with_extension("mp4"); + path.persist_noclobber(&output)?; + Ok(output) +} + +#[cfg(test)] +mod tests; diff --git a/crates/wizard-core/src/video/server.rs b/crates/wizard-core/src/video/server.rs new file mode 100644 index 0000000..9f373dc --- /dev/null +++ b/crates/wizard-core/src/video/server.rs @@ -0,0 +1,195 @@ +use super::{Create, GetResult, VideoHub}; +use alloc::sync::Arc; +use anyhow::{Context as _, Result, bail, ensure}; +use core::{convert::Infallible, future::Future}; +use http_body_util::{BodyExt as _, Empty, combinators::BoxBody}; +use hyper::{ + Request, Response, StatusCode, + body::{Bytes, Incoming}, + header::AUTHORIZATION, + server::conn::http1, + service::service_fn, +}; +use hyper_util::rt::TokioIo; +use rmcp::{ + ErrorData, RoleServer, ServerHandler, + handler::server::common::schema_for_type, + model::{ + CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, Implementation, + ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerConfig, Tool, + ToolAnnotations, + }, + service::RequestContext, + transport::streamable_http_server::{ + StreamableHttpServerConfig, StreamableHttpService, session::never::NeverSessionManager, + }, +}; +use schemars::JsonSchema; +use serde::Deserialize; +use serde_json::{Value, json}; +use tokio::{net::TcpListener, task::JoinSet}; +use wizard_settings::VideoConnection; + +/// The GUI owns the grant and the signed-in Hub account. No upstream key is cached here. +pub trait VideoAccess: Send + Sync + 'static { + fn connection(&self) -> Option; + fn token(&self, connection: &VideoConnection) -> impl Future> + Send; +} + +struct Tools { + hub: VideoHub, + access: Arc, +} + +#[derive(Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +struct NoArgs {} + +impl Tools { + async fn call(&self, name: &str, args: Value, connection: &VideoConnection) -> Result { + match name { + "video_models" => { + serde_json::from_value::(args)?; + let models = self.hub.models().await?.into_values().collect::>(); + Ok(json!({"models":models})) + } + "video_create" => { + let input: Create = + serde_json::from_value(args).context("Invalid video_create arguments")?; + self.hub + .create(input, connection, self.access.token(connection)) + .await + } + "video_result" => { + let input: GetResult = + serde_json::from_value(args).context("Invalid video_result arguments")?; + self.hub + .result(input, connection, self.access.token(connection)) + .await + } + _ => bail!("Unknown video tool"), + } + } +} + +impl ServerHandler for Tools { + fn get_info(&self) -> ServerConfig { + ServerConfig::new(ServerCapabilities::builder().enable_tools().build()).with_server_info( + Implementation::new("CoreInfra Wizard Video", env!("CARGO_PKG_VERSION")), + ) + } + + fn list_tools( + &self, + _: Option, + _: RequestContext, + ) -> impl Future> { + core::future::ready(Ok(ListToolsResult::with_all_items(vec![ + Tool::new("video_models", "List current Hub video model IDs, providers and price ranges in RUB per second.", schema_for_type::()) + .with_annotations(ToolAnnotations::new().read_only(true)), + Tool::new("video_create", "Submit one paid video generation. Keep the returned job to poll or resume. Never retry a submission with an uncertain outcome automatically.", schema_for_type::()) + .with_annotations(ToolAnnotations::new().read_only(false).idempotent(false)), + Tool::new("video_result", "Poll an existing job using its original connection. A completed download returns an absolute MP4 path.", schema_for_type::()), + ]))) + } + + async fn call_tool( + &self, + request: CallToolRequestParams, + ctx: RequestContext, + ) -> Result { + let connection = ctx + .extensions + .get::() + .and_then(|parts| parts.extensions.get::()) + .ok_or_else(|| ErrorData::internal_error("Missing video grant", None))?; + let args = Value::Object(request.arguments.unwrap_or_default()); + let response = match self.call(&request.name, args, connection).await { + Ok(value) => CallToolResult::structured(value), + Err(error) => CallToolResult::error(vec![ContentBlock::text(format!("{error:#}"))]), + }; + Ok(response.into()) + } +} + +fn empty(status: StatusCode) -> Response> { + let mut response = Response::new(Empty::::new().boxed()); + *response.status_mut() = status; + response +} + +async fn handle( + mut request: Request, + service: StreamableHttpService, NeverSessionManager>, + access: Arc, +) -> Result>, Infallible> { + if request.uri().path() != "/mcp" || request.uri().query().is_some() { + return Ok(empty(StatusCode::NOT_FOUND)); + } + let bearer = request + .headers() + .get(AUTHORIZATION) + .and_then(|value| value.to_str().ok()) + .and_then(|value| value.split_once(' ')) + .filter(|(scheme, _)| scheme.eq_ignore_ascii_case("bearer")) + .map(|(_, token)| token); + let connection = access + .connection() + .filter(|c| !c.local_bearer.is_empty() && bearer == Some(c.local_bearer.as_str())); + let Some(connection) = connection else { + let mut response = empty(StatusCode::UNAUTHORIZED); + response.headers_mut().insert( + hyper::header::WWW_AUTHENTICATE, + hyper::header::HeaderValue::from_static("Bearer"), + ); + return Ok(response); + }; + request.headers_mut().remove(AUTHORIZATION); + request.extensions_mut().insert(connection); + Ok(service.handle(request).await) +} + +pub async fn serve(listener: TcpListener, hub: VideoHub, access: impl VideoAccess) -> Result<()> { + let addr = listener.local_addr()?; + ensure!(addr.ip().is_loopback(), "Video MCP must bind to loopback"); + let access = Arc::new(access); + let factory_access = Arc::clone(&access); + let config = StreamableHttpServerConfig::default() + .with_allowed_hosts([addr.to_string()]) + .enforce_origin_validation() + .with_legacy_session_mode(false) + .with_json_response(true) + .with_max_request_body_bytes(32 * 1024 * 1024); + let service = StreamableHttpService::new( + move || { + Ok(Tools { + hub: hub.clone(), + access: Arc::clone(&factory_access), + }) + }, + Arc::new(NeverSessionManager::default()), + config, + ); + let mut clients = JoinSet::new(); + loop { + tokio::select! { + result = listener.accept() => { + let (socket, _) = match result { + Ok(connection) => connection, + Err(error) => { + log::warn!("video listener accept failed: {error}"); + tokio::time::sleep(core::time::Duration::from_millis(500)).await; + continue; + } + }; + let service = service.clone(); + let access = Arc::clone(&access); + clients.spawn(async move { + let handler = service_fn(move |req| handle(req, service.clone(), Arc::clone(&access))); + let _ = http1::Builder::new().serve_connection(TokioIo::new(socket), handler).await; + }); + } + _ = clients.join_next(), if !clients.is_empty() => {} + } + } +} diff --git a/crates/wizard-core/src/video/tests.rs b/crates/wizard-core/src/video/tests.rs new file mode 100644 index 0000000..46e2271 --- /dev/null +++ b/crates/wizard-core/src/video/tests.rs @@ -0,0 +1,570 @@ +use super::*; +use crate::video::server::{VideoAccess, serve}; +use alloc::sync::Arc; +use core::convert::Infallible; +use http_body_util::{BodyExt as _, Full}; +use hyper::{ + Request, Response as HttpResponse, + body::{Bytes, Incoming}, + server::conn::http1, + service::service_fn, +}; +use hyper_util::rt::TokioIo; +use reqwest::StatusCode; +use std::sync::{Mutex, Once}; +use tokio::{net::TcpListener, task::JoinHandle}; + +const OR_MODEL: &str = "example/cinema"; +const FAL_MODEL: &str = "example/flux/image-to-video"; +const KEY: &str = "fixture-upstream-secret"; +const MP4: &[u8] = include_bytes!("../../tests/fixtures/blue.mp4"); + +fn connection() -> VideoConnection { + VideoConnection { + account_id: "personal".into(), + hub_id: "team".into(), + key_id: fingerprint(KEY), + key_name: "studio".into(), + local_bearer: "fixture-local-grant".into(), + } +} + +struct Access(VideoConnection); +impl VideoAccess for Access { + fn connection(&self) -> Option { + Some(self.0.clone()) + } + fn token(&self, _: &VideoConnection) -> impl Future> { + core::future::ready(Ok(KEY.into())) + } +} + +struct Seen { + method: String, + path: String, + auth: Option, + body: Value, +} + +struct Fixture { + hub: VideoHub, + dir: tempfile::TempDir, + requests: Arc>>, + fake_hub: JoinHandle<()>, +} + +fn catalog() -> Value { + json!({"providers":{"test":{"models":{ + OR_MODEL:{"category":"videos","display_name":"Cinema","endpoints":[OPENROUTER],"prices":{"video_seconds":2_500_000},"lowest_prices":{"video_seconds":1_000_000}}, + FAL_MODEL:{"category":"videos","endpoints":[FAL],"prices":{"video_seconds":12_000_000}}, + "ignored":{"category":"text","endpoints":["/text"],"prices":{}} + }}}}) +} + +impl Fixture { + async fn new() -> Result { + static INIT: Once = Once::new(); + INIT.call_once(|| { + let _ = crate::hub::install_crypto_provider(); + }); + let listener = TcpListener::bind("127.0.0.1:0").await?; + let origin = format!("http://{}", listener.local_addr()?); + let dir = tempfile::tempdir()?; + let mut hub = VideoHub::new()?; + hub.origin = Url::parse(&origin)?; + hub.output_dir = Some(dir.path().join("downloads")); + let requests = Arc::new(Mutex::new(Vec::new())); + let observed = Arc::clone(&requests); + let fake_hub = tokio::spawn(async move { + while let Ok((stream, _)) = listener.accept().await { + let requests = Arc::clone(&observed); + let origin = origin.clone(); + tokio::spawn(async move { + let handle = service_fn(move |req: Request| { + let requests = Arc::clone(&requests); + let origin = origin.clone(); + async move { + let (head, body) = req.into_parts(); + let path = head.uri.to_string(); + let auth = head + .headers + .get("authorization") + .and_then(|v| v.to_str().ok()) + .map(str::to_owned); + let bytes = body.collect().await.expect("body").to_bytes(); + let body = + serde_json::from_slice::(&bytes).unwrap_or(Value::Null); + let mut requests = requests.lock().expect("requests"); + let polls = requests.iter().filter(|r: &&Seen| r.path == path).count(); + requests.push(Seen { + method: head.method.to_string(), + path: path.clone(), + auth: auth.clone(), + body: body.clone(), + }); + let mut code = 200; + let mut redirect = false; + let answer = if path == "/hub/api/prices" && auth.is_none() { + catalog() + } else if path.ends_with("/content?t=fixture") && auth.is_none() + || path.ends_with("/content?index=1") + && auth.as_deref() == Some("Bearer fixture-upstream-secret") + { + return Ok::<_, Infallible>(HttpResponse::new(Full::new( + Bytes::from_static(MP4), + ))); + } else if auth.as_deref() != Some("Bearer fixture-upstream-secret") { + code = 401; + json!({}) + } else if path == OPENROUTER && head.method == "POST" { + match body["prompt"].as_str() { + Some("redirect") => { + code = 307; + redirect = true; + json!({}) + } + Some("reject") => { + code = 422; + json!({"error":KEY}) + } + _ => json!({"id":"job-1","status":"pending"}), + } + } else if path == "/fal/api/example/flux/image-to-video" + && head.method == "POST" + { + json!({"request_id":"f-1","status":"IN_QUEUE","status_url":"https://untrusted.invalid/status"}) + } else if path == format!("{OPENROUTER}/job-1") { + json!({"status":if polls==0 {"in_progress"} else {"completed"},"usage":{"cost_rub":6.5}}) + } else if path.ends_with("/f-1/status") + || path.ends_with("/bad-link/status") + { + json!({"status":"COMPLETED"}) + } else if path.ends_with("/f-1") { + json!({"video":{"url":format!("{origin}{path}/content?t=fixture")}}) + } else if path.ends_with("/bad-link") { + json!({"video":{"url":"https://untrusted.invalid/content?t=fixture"}}) + } else if path.ends_with("/failed") { + json!({"status":"failed"}) + } else { + code = 404; + json!({}) + }; + let mut response = HttpResponse::builder() + .status(code) + .header("content-type", "application/json"); + if redirect { + response = + response.header("location", format!("{origin}/forbidden")); + } + Ok(response + .body(Full::new(Bytes::from(answer.to_string()))) + .expect("response")) + } + }); + let _ = http1::Builder::new() + .serve_connection(TokioIo::new(stream), handle) + .await; + }); + } + }); + Ok(Self { + hub, + dir, + requests, + fake_hub, + }) + } + + async fn start(&self, config: VideoConnection) -> Result<(String, JoinHandle>)> { + let local = TcpListener::bind("127.0.0.1:0").await?; + let url = format!("http://{}/mcp", local.local_addr()?); + let task = tokio::spawn(serve(local, self.hub.clone(), Access(config))); + Ok((url, task)) + } + + fn request(&self, url: &str, method: &str, params: &Value) -> RequestBuilder { + self.hub + .client + .post(url) + .header("Content-Type", "application/json") + .header("Accept", "application/json, text/event-stream") + .header("MCP-Protocol-Version", "2025-11-25") + .body(json!({"jsonrpc":"2.0","id":1,"method":method,"params":params}).to_string()) + } + + async fn call(&self, url: &str, tool: &str, args: Value) -> Result { + let response = self + .request(url, "tools/call", &json!({"name":tool,"arguments":args})) + .bearer_auth(connection().local_bearer) + .send() + .await?; + ensure!( + response.status() == StatusCode::OK, + "MCP returned {}", + response.status() + ); + ensure!( + !response.headers().contains_key("mcp-session-id"), + "Unexpected MCP session" + ); + let data: Value = serde_json::from_slice(&response.bytes().await?)?; + ensure!(!data.to_string().contains(KEY), "Leaked Hub credential"); + ensure!(data.get("error").is_none(), "MCP error: {data}"); + Ok(data["result"].clone()) + } +} + +impl Drop for Fixture { + fn drop(&mut self) { + self.fake_hub.abort(); + } +} + +#[tokio::test] +async fn both_providers_fields_and_resume_across_restart() -> Result<()> { + let fixture = Fixture::new().await?; + let (url, first) = fixture.start(connection()).await?; + let catalog = fixture.call(&url, "video_models", json!({})).await?; + let models = catalog["structuredContent"]["models"] + .as_array() + .context("models")?; + assert_eq!(models.len(), 2); + let model = models + .iter() + .find(|m| m["id"] == OR_MODEL) + .context("model")?; + assert_eq!(model["min_rub_per_second"], 1.0); + assert_eq!(model["max_rub_per_second"], 2.5); + let fields = json!({"duration":5,"generate_audio":false,"seed":0,"nested":[null,{"a":[1,2.25]}], + "provider":{"options":{"example":{"guidance_scale":4.5}}},"frame_images":[{"image_url":{"url":"data:image/png;base64,AAAA"},"frame_type":"first_frame","type":"image_url"}]}); + let first_job = fixture + .call( + &url, + "video_create", + json!({"model":OR_MODEL,"prompt":"river","fields":fields}), + ) + .await?["structuredContent"]["job"] + .clone(); + let fal_fields = json!({"image_url":"data:image/png;base64,AAAA","extra":{"a":null},"seed":0,"generate_audio":false}); + let fal_job = fixture + .call( + &url, + "video_create", + json!({"model":FAL_MODEL,"prompt":"waves","fields_json":fal_fields.to_string()}), + ) + .await?["structuredContent"]["job"] + .clone(); + assert_eq!(first_job["id"], "job-1"); + assert_eq!(fal_job["id"], "f-1"); + let pending = fixture + .call(&url, "video_result", json!({"job":first_job})) + .await?; + assert_eq!(pending["structuredContent"]["status"], "in_progress"); + first.abort(); + let _ = first.await; + let (url, _second) = fixture.start(connection()).await?; + let completed = fixture + .call(&url, "video_result", json!({"job":first_job,"index":1})) + .await?["structuredContent"] + .clone(); + assert_eq!(completed["cost_rub"], 6.5); + let path = Path::new(completed["path"].as_str().context("OpenRouter file")?); + assert!(path.starts_with(fixture.dir.path())); + assert_eq!(std::fs::read(path)?, MP4); + let fal = fixture + .call(&url, "video_result", json!({"job":fal_job})) + .await?["structuredContent"] + .clone(); + assert_eq!( + std::fs::read(fal["path"].as_str().context("fal file")?)?, + MP4 + ); + let link = fixture + .call( + &url, + "video_result", + json!({"job":fal_job,"download":false}), + ) + .await?; + assert!( + link["structuredContent"]["url"] + .as_str() + .context("signed URL")? + .ends_with("/content?t=fixture") + ); + let records = fixture.requests.lock().expect("requests"); + let submitted: Vec<_> = records.iter().filter(|r| r.method == "POST").collect(); + assert_eq!(submitted.len(), 2); + let mut expected = fields; + expected["model"] = json!(OR_MODEL); + expected["prompt"] = json!("river"); + assert_eq!(submitted[0].body, expected); + let mut expected = fal_fields; + expected["prompt"] = json!("waves"); + assert_eq!(submitted[1].body, expected); + assert!( + submitted + .iter() + .all(|r| r.auth.as_deref() == Some("Bearer fixture-upstream-secret")) + ); + Ok(()) +} + +#[tokio::test] +async fn validates_before_billing_and_confines_credentials() -> Result<()> { + let fixture = Fixture::new().await?; + let (url, _server) = fixture.start(connection()).await?; + let unauthorized = fixture.hub.client.post(&url).send().await?; + assert_eq!(unauthorized.status(), 401); + for (header, value) in [ + ("Origin", "https://elsewhere.example"), + ("Host", "hub.coreinfra.ai"), + ] { + assert!( + fixture + .request(&url, "tools/list", &json!({})) + .bearer_auth(connection().local_bearer) + .header(header, value) + .send() + .await? + .status() + .is_client_error() + ); + } + assert!(fixture.requests.lock().expect("requests").is_empty()); + for extra in [ + json!({"fields":{"prompt":"changed"}}), + json!({"fields":{"model":"other"}}), + json!({"fields_json":"[]"}), + json!({"fields":{},"fields_json":"{}"}), + json!({"fields_json":"{"}), + json!({"url":"https://elsewhere.example"}), + ] { + let mut args = json!({"model":OR_MODEL,"prompt":"river"}); + args.as_object_mut() + .expect("args") + .extend(extra.as_object().expect("extra").clone()); + assert_eq!( + fixture.call(&url, "video_create", args).await?["isError"], + true + ); + } + assert!( + !fixture + .requests + .lock() + .expect("requests") + .iter() + .any(|r| r.method == "POST") + ); + for prompt in ["redirect", "reject"] { + assert_eq!( + fixture + .call( + &url, + "video_create", + json!({"model":OR_MODEL,"prompt":prompt}) + ) + .await?["isError"], + true + ); + } + let mut job = json!({"id":"f-1","provider":"fal","model":FAL_MODEL,"hub_id":"team","key_id":fingerprint(KEY)}); + for (k, v) in [ + ("hub_id", "another"), + ("key_id", "different"), + ("model", "../hub/account"), + ("id", "../admins"), + ] { + let mut invalid = job.clone(); + invalid[k] = json!(v); + assert_eq!( + fixture + .call(&url, "video_result", json!({"job":invalid})) + .await?["isError"], + true + ); + } + job["id"] = json!("bad-link"); + assert_eq!( + fixture + .call(&url, "video_result", json!({"job":job})) + .await?["isError"], + true + ); + let records = fixture.requests.lock().expect("requests"); + assert_eq!(records.iter().filter(|r| r.method == "POST").count(), 2); + assert!( + !records + .iter() + .any(|r| r.path.contains("forbidden") || r.path.contains("admins")) + ); + Ok(()) +} + +#[test] +fn install_upgrade_disable_and_collisions() -> Result<()> { + use std::fs; + let dir = tempfile::tempdir()?; + let config = dir.path().join("config.toml"); + let skill = dir.path().join("skills/coreinfra-video"); + let source = skill.join("SKILL.md"); + fs::write( + &config, + "# keep this\n[mcp_servers.other]\nurl = 'https://elsewhere.example'\n", + )?; + let c = connection(); + install::sync_at(&config, None, Some(&c))?; + let initial = fs::read_to_string(&config)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + assert_eq!(fs::metadata(&config)?.permissions().mode() & 0o077, 0); + } + assert!(initial.contains("# keep this") && initial.contains("https://elsewhere.example")); + assert!(initial.contains("exclude") && !initial.contains(KEY)); + let bundled = fs::read_to_string(&source)?; + fs::write(skill.join("my-notes"), "custom")?; + install::sync_at(&config, Some(&c), Some(&c))?; + assert_eq!(fs::read_to_string(&config)?, initial); + let manifest = skill.join(".wizard-managed.json"); + let mut hashes: Value = serde_json::from_str(&fs::read_to_string(&manifest)?)?; + fs::write(&source, "old bundled file")?; + hashes["SKILL.md"] = json!(fingerprint("old bundled file")); + fs::write(manifest, hashes.to_string())?; + install::sync_at(&config, Some(&c), Some(&c))?; + assert_eq!(fs::read_to_string(&source)?, bundled); + fs::write(&source, "user changes")?; + assert!(install::sync_at(&config, Some(&c), Some(&c)).is_err()); + install::sync_at(&config, Some(&c), None)?; + assert_eq!(fs::read_to_string(&source)?, "user changes"); + assert!(skill.join("my-notes").exists()); + assert!(!fs::read_to_string(&config)?.contains(&c.local_bearer)); + let other_dir = tempfile::tempdir()?; + let config = other_dir.path().join("config.toml"); + let foreign = "[mcp_servers.coreinfra_video]\nurl='https://other.example'\n"; + fs::write(&config, foreign)?; + assert!(install::sync_at(&config, None, Some(&c)).is_err()); + assert_eq!(fs::read_to_string(config)?, foreign); + assert!(!other_dir.path().join("skills").exists()); + let fresh = tempfile::tempdir()?; + let config = fresh.path().join("config.toml"); + install::sync_at(&config, None, Some(&c))?; + fs::write( + fresh + .path() + .join("skills/coreinfra-video/.wizard-managed.json"), + "invalid", + )?; + assert!(install::sync_at(&config, Some(&c), None).is_err()); + assert!(!fs::read_to_string(config)?.contains(&c.local_bearer)); + Ok(()) +} + +#[tokio::test] +async fn rotated_connection_and_interrupted_download_fail_closed() -> Result<()> { + let fixture = Fixture::new().await?; + let rotated = VideoConnection { + key_id: fingerprint("replacement"), + local_bearer: "rotated-grant".into(), + ..connection() + }; + let (url, _server) = fixture.start(rotated.clone()).await?; + assert_eq!( + fixture + .request(&url, "tools/list", &json!({})) + .bearer_auth(connection().local_bearer) + .send() + .await? + .status(), + 401 + ); + let original = json!({"id":"f-1","provider":"fal","model":FAL_MODEL,"hub_id":"team","key_id":fingerprint(KEY)}); + let response = fixture + .request( + &url, + "tools/call", + &json!({"name":"video_result","arguments":{"job":original}}), + ) + .bearer_auth(&rotated.local_bearer) + .send() + .await?; + let data: Value = serde_json::from_slice(&response.bytes().await?)?; + assert_eq!(data["result"]["isError"], true); + assert!(fixture.requests.lock().expect("requests").is_empty()); + + let listener = TcpListener::bind("127.0.0.1:0").await?; + let addr = listener.local_addr()?; + let broken = tokio::spawn(async move { + use tokio::io::AsyncReadExt as _; + let (mut stream, _) = listener.accept().await?; + let mut buf = [0u8; 1024]; + let count = stream.read(&mut buf).await?; + assert!(count > 0); + stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: video/mp4\r\nContent-Length: 1000\r\n\r\npartial").await?; + stream.shutdown().await + }); + let response = fixture + .hub + .client + .get(format!("http://{addr}/content")) + .send() + .await?; + let dir = fixture.dir.path().join("truncated-download"); + assert!(save_video(&dir, response).await.is_err()); + broken.await??; + assert_eq!(std::fs::read_dir(dir)?.count(), 0); + Ok(()) +} + +#[tokio::test] +#[ignore = "requires WIZARD_VIDEO_TEST_PYTHON with mcp and WIZARD_VIDEO_TEST_CODEX pointing to a Codex app-server binary"] +async fn native_and_python_clients() -> Result<()> { + let fixture = Fixture::new().await?; + let (url, _task) = fixture.start(connection()).await?; + let profile = fixture.dir.path().join("profile"); + let path = profile.join("config.toml"); + install::sync_at(&path, None, Some(&connection()))?; + std::fs::write(&path, std::fs::read_to_string(&path)?.replace(URL, &url))?; + let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/video_clients.py"); + let status = tokio::process::Command::new(std::env::var("WIZARD_VIDEO_TEST_PYTHON")?) + .arg(script) + .arg(&profile) + .current_dir(fixture.dir.path()) + .status() + .await?; + ensure!(status.success(), "external client test failed"); + let requests = fixture.requests.lock().expect("requests"); + let bodies: Vec<_> = requests + .iter() + .filter(|r| r.method == "POST") + .map(|r| &r.body) + .collect(); + assert_eq!(bodies.len(), 2); + assert_eq!( + bodies[0], + &json!({"model":OR_MODEL,"prompt":"native client","provider":{"options":{"test":{"seed":0,"flag":false,"values":[null,2.75]}}}}) + ); + assert_eq!( + bodies[1], + &json!({"prompt":"python client","image_url":"data:image/png;base64,AAAA","extra":[false,null,3]}) + ); + Ok(()) +} + +#[tokio::test] +#[ignore = "contacts the public read-only CoreInfra Hub price catalog"] +async fn public_video_catalog() -> Result<()> { + let _ = crate::hub::install_crypto_provider(); + let models = VideoHub::new()?.models().await?; + ensure!(!models.is_empty(), "Hub returned no video models"); + let fal = models + .values() + .filter(|m| m.provider == Provider::Fal) + .count(); + println!( + "Live public catalog: {} OpenRouter and {fal} fal video models", + models.len() - fal + ); + Ok(()) +} diff --git a/crates/wizard-core/tests/fixtures/blue.mp4 b/crates/wizard-core/tests/fixtures/blue.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..7b9687688d6e672c975c59b0e9f03e7a253d8976 GIT binary patch literal 1591 zcmZuxy>A>v6rZz6gpdmtR!HGu99-Jx-M#ZASZrj=M7RKn1`7mYncbbeUES`^cxG(h zrHDYH;4etQe-H_1p$SSsmog1TNCY9FAP`hYp&-Za?Vatz%G1rf-@LDRA9KqX8{V%yjLl8#2729qdWzlq4_7!KVdCKzjONE0pbhi?Q_fLp*iI7 zzB6I~sc0W2L=P0-o2SJR1ZIW>}O zh)BP1Xi7f8t((@xCGfY_+0%iL%~v!uaH83m6UidV-UFmX)LMEI_PtKqZ~LCl%OV~I zz3xOe*{_br09{z^wo>-e_-FQVAN-4x4LZ{K=jy~}s@cVU^z1d3gyN2Sah4?M549(chfgt>EiYh&a1 z__$54PnDu>E7RYgTiUs+O4umv3RT-Jo`_f^q0jNc!VXVmT&iRc`di+X$3-p5k(Hr0 z>}+{M-|z5BhPfPadl&;<=h}|oiaj!E=(jyMVdGUXlxai}eRxDh)O{KHJ)Y!7RU(3` z&z+HFSy-SO!y9Sh0Fr|$^pJu`Z>w5{fxqVaJQLPM+71d$7Lx!6I#O9?r3=>s?s5YU z3REgJ5IGLfxy+VDf?Q4o9Y0%!o=_G%Jqu2)IZ?%qkM8&{Rbn#f7kRNnvDD*Rd@6xr?DwTGmltX=o=x4cCDj zA$AySdQ%wTlK{C*WG$0J7j`|$(g^CPkv507NuslANpF%cF&VG$*OF8f4q?z~d;EZE z3B7g?P*dE7i(%M%6$0y|4!gY2m=WFsMu=GA0OO}&vXAkAugK3nWlQ5{fBbOg()ES? YfBt^7a{qDb6MEV^pRbfV8_yp92Zgh1uK)l5 literal 0 HcmV?d00001 diff --git a/crates/wizard-core/tests/video_clients.py b/crates/wizard-core/tests/video_clients.py new file mode 100644 index 0000000..f31e93e --- /dev/null +++ b/crates/wizard-core/tests/video_clients.py @@ -0,0 +1,110 @@ +"""Opt-in compatibility test, launched by the Rust native_and_python_clients fixture.""" +import asyncio +import json +import os +from pathlib import Path +import queue +import subprocess +import sys +import threading +import time +import tomllib + +import httpx2 +from mcp import Client +from mcp.client.streamable_http import streamable_http_client + + +MP4 = (Path(__file__).parent / "fixtures/blue.mp4").read_bytes() + + +def native_client(profile): + config = profile / "config.toml" + with config.open("a") as file: + file.write('\n[model_providers.fixture]\nname="Offline fixture"\n' + 'base_url="http://127.0.0.1:1/v1"\nwire_api="responses"\n') + env = {k: os.environ[k] for k in ("PATH", "HOME", "TMPDIR", "LANG", "USER") if k in os.environ} + env.update(CODEX_HOME=str(profile), HTTP_PROXY="http://127.0.0.1:1", + HTTPS_PROXY="http://127.0.0.1:1", ALL_PROXY="http://127.0.0.1:1", + NO_PROXY="127.0.0.1,localhost", COREINFRA_API_KEY="exclude-this-test-value") + binary = os.environ["WIZARD_VIDEO_TEST_CODEX"] + server = subprocess.Popen([binary, "app-server", "--listen", "stdio://", + "-c", 'model_provider="fixture"', "-c", 'model="offline"'], + cwd=profile.parent, env=env, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1) + replies = queue.Queue() + def read(): + for line in server.stdout: + replies.put(json.loads(line)) + replies.put(None) + threading.Thread(target=read, daemon=True).start() + def send(message): + server.stdin.write(json.dumps(message) + "\n") + server.stdin.flush() + seq = 0 + def call(method, params): + nonlocal seq + seq += 1 + send({"jsonrpc":"2.0", "id":seq, "method":method, "params":params}) + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + msg = replies.get(timeout=max(.01, deadline - time.monotonic())) + if msg is None: + raise RuntimeError("test app-server exited") + if msg.get("id") == seq and "method" not in msg: + assert "error" not in msg, (method, msg.get("error")) + return msg["result"] + raise TimeoutError(method) + try: + call("initialize", {"clientInfo":{"name":"wizard_fixture","version":"1"}, + "capabilities":{"experimentalApi":True}}) + send({"jsonrpc":"2.0","method":"initialized"}) + thread = call("thread/start", {"cwd":str(profile.parent),"ephemeral":True,"sandbox":"workspace-write"})["thread"]["id"] + skills = call("skills/list", {"cwds":[str(profile.parent)],"forceReload":True}) + assert any(s["name"] == "coreinfra-video" for group in skills["data"] for s in group["skills"]) + inventory = call("mcpServerStatus/list", {"threadId":thread,"serverName":"coreinfra_video"}) + tools = next(s["tools"] for s in inventory["data"] if s["name"] == "coreinfra_video") + assert set(tools) == {"video_models","video_create","video_result"} + shell = call("command/exec", {"command":[sys.executable,"-I","-c", + "import os; print('COREINFRA_API_KEY' in os.environ)"], + "cwd":str(profile.parent), "sandboxPolicy":{"type":"workspaceWrite","networkAccess":False}}) + assert shell["exitCode"] == 0 and shell["stdout"].strip() == "False", shell + result = call("mcpServer/tool/call", {"threadId":thread,"server":"coreinfra_video", + "tool":"video_create","arguments":{"model":"example/cinema","prompt":"native client", + "fields":{"provider":{"options":{"test":{"seed":0,"flag":False,"values":[None,2.75]}}}}}}) + assert not result.get("isError"), result + return result["structuredContent"]["job"] + finally: + server.stdin.close() + try: + server.wait(timeout=4) + except subprocess.TimeoutExpired: + server.terminate() + server.wait(timeout=4) + + +async def python_client(profile, existing_job): + with (profile / "config.toml").open("rb") as file: + local = tomllib.load(file)["mcp_servers"]["coreinfra_video"] + async with httpx2.AsyncClient(headers=local["http_headers"], trust_env=False) as http: + async with Client(streamable_http_client(local["url"], http_client=http)) as wizard: + assert len((await wizard.list_tools()).tools) == 3 + models = await wizard.call_tool("video_models", {}) + assert len(models.structured_content["models"]) == 2 + for _ in range(2): + result = await wizard.call_tool("video_result", {"job": existing_job, "index": 1}) + assert Path(result.structured_content["path"]).read_bytes() == MP4 + submitted = await wizard.call_tool("video_create", { + "model": "example/flux/image-to-video", "prompt": "python client", + "fields_json": json.dumps({"image_url":"data:image/png;base64,AAAA","extra":[False,None,3]})}) + job = submitted.structured_content["job"] + async with Client(streamable_http_client(local["url"], http_client=http)) as resumed: + final = await resumed.call_tool("video_result", {"job":job}) + assert Path(final.structured_content["path"]).read_bytes() == MP4 + + +if __name__ == "__main__": + test_profile = Path(sys.argv[1]).resolve() + existing = native_client(test_profile) + asyncio.run(python_client(test_profile, existing)) + print("Native ChatGPT skill, credential exclusion, MCP tools and Python SDK round trip passed.") diff --git a/crates/wizard-gui/Cargo.toml b/crates/wizard-gui/Cargo.toml index e34a23b..a37218f 100644 --- a/crates/wizard-gui/Cargo.toml +++ b/crates/wizard-gui/Cargo.toml @@ -33,6 +33,7 @@ tauri-plugin-log.workspace = true tauri-plugin-single-instance.workspace = true tauri-plugin-updater.workspace = true tokio.workspace = true +uuid.workspace = true wizard-core.workspace = true wizard-proxy.workspace = true wizard-proxy-daemon.workspace = true diff --git a/crates/wizard-gui/src/agents.rs b/crates/wizard-gui/src/agents.rs index e365ad0..80da8ba 100644 --- a/crates/wizard-gui/src/agents.rs +++ b/crates/wizard-gui/src/agents.rs @@ -394,9 +394,12 @@ pub(crate) async fn apply_direct_change( return apply_agent_plan(plan, app).await; } DirectChange::RestoreBackup { agent, id } => { - let settings = settings::get_state(&app).map_err(|error| format!("{error:#}"))?; + let restore_app = app.clone(); let result = tauri::async_runtime::spawn_blocking(move || match agent { - BackupAgent::Codex => codex::restore_backup(id, &settings), + BackupAgent::Codex => { + let _guard = settings::lock_state()?; + codex::restore_backup(id, &settings::get_state(&restore_app)?) + } BackupAgent::Claude => claude::restore_backup(id), BackupAgent::ClaudeDesktop => unreachable!(), }) diff --git a/crates/wizard-gui/src/hub_tokens.rs b/crates/wizard-gui/src/hub_tokens.rs index 21caddb..033aadf 100644 --- a/crates/wizard-gui/src/hub_tokens.rs +++ b/crates/wizard-gui/src/hub_tokens.rs @@ -11,7 +11,7 @@ use serde::Serialize; use tauri::Manager as _; use wizard_core::hub::{self, AgentTokens, Client, HubError, SessionUpdate}; -const PROXY_TOKEN_NAME: &str = "coreinfra-ai-wizard"; +pub(crate) const PROXY_TOKEN_NAME: &str = "coreinfra-ai-wizard"; #[derive(Clone, Default, Serialize)] pub(crate) struct HubTokens { @@ -24,6 +24,25 @@ pub(crate) struct HubTokens { generation: u64, } +pub(crate) fn video_keys(app: &tauri::AppHandle) -> (Option, Vec) { + let snapshot = app.state::>().current().state; + let proxy = settings::get_state(app).ok().and_then(|s| s.proxy_token); + let keys = snapshot + .tokens + .iter() + .filter(|t| crate::video::selectable(t, proxy.as_deref())) + .map(crate::video::VideoKey::from) + .collect(); + (snapshot.hub_id, keys) +} + +pub(crate) fn generation(app: &tauri::AppHandle) -> u64 { + app.state::>() + .current() + .state + .generation +} + #[derive(Clone, Copy, Debug)] enum TokensError { /// Hub returned a profile without `personal_hub_id`. diff --git a/crates/wizard-gui/src/lib.rs b/crates/wizard-gui/src/lib.rs index fce162d..8005cb3 100644 --- a/crates/wizard-gui/src/lib.rs +++ b/crates/wizard-gui/src/lib.rs @@ -23,6 +23,7 @@ mod system_proxy; #[cfg(any(target_os = "macos", target_os = "windows"))] mod tray; mod updater; +mod video; const MAIN_WINDOW_NAME: &str = "main"; static MAIN_WINDOW_LOCK: Mutex<()> = Mutex::new(()); @@ -89,6 +90,7 @@ pub fn run_application() -> Result<()> { .plugin(tauri_plugin_updater::Builder::new().build()) .manage(desktop_install::InstallManagers::default()) .manage(proxy::ProxyState::default()) + .manage(video::State::default()) // Proxy startup can sign out a rejected session, which clears the token list. .manage(revision_signal::RevisionSignal::new( hub_tokens::HubTokens::default(), @@ -136,6 +138,8 @@ pub fn run_application() -> Result<()> { hub_tokens::get_hub_tokens_state, hub_tokens::get_configured_token_info, hub_tokens::refresh_hub_tokens, + video::get_video_connection, + video::set_video_connection, proxy::get_proxy_state, agents::get_agent_state, agents::launch_agent, @@ -160,6 +164,7 @@ pub fn run_application() -> Result<()> { #[cfg(target_os = "macos")] tauri::RunEvent::Reopen { .. } => request_main_window(app), tauri::RunEvent::Exit => { + tauri::async_runtime::block_on(video::shutdown(app)); system_proxy::stop(app); proxy::stop_observer(app); } @@ -173,6 +178,7 @@ fn setup(app: &mut tauri::App, minimized: bool) -> Result<(), Box>, + error: Option, +} + +pub(crate) type State = Mutex; + +pub(crate) fn selectable(token: &AgentToken, proxy: Option<&str>) -> bool { + !token.name.trim().is_empty() + && token.name != hub_tokens::PROXY_TOKEN_NAME + && Some(token.token.as_str()) != proxy +} + +#[derive(Serialize)] +pub(crate) struct VideoKey { + name: String, + id: String, +} + +impl From<&AgentToken> for VideoKey { + fn from(t: &AgentToken) -> Self { + Self { + name: t.name.clone(), + id: video::fingerprint(&t.token), + } + } +} + +#[derive(Deserialize)] +pub(crate) struct Selection { + hub_id: String, + name: String, + id: Option, +} + +fn choose<'a>( + list: &'a AgentTokens, + selection: &Selection, + proxy: Option<&str>, +) -> Result<&'a AgentToken> { + let mut matches = list.iter().filter(|t| { + selectable(t, proxy) + && t.name == selection.name + && selection + .id + .as_ref() + .is_none_or(|id| *id == video::fingerprint(&t.token)) + }); + let token = matches + .next() + .context("Выбранный ключ отсутствует в хабе")?; + ensure!( + matches.next().is_none(), + "В хабе несколько ключей с таким именем. Выберите конкретный ключ" + ); + Ok(token) +} + +async fn list(app: &tauri::AppHandle, hub_id: String) -> Result { + hub_tokens::with_session(app, |client, session| async move { + hub::list_agent_tokens(&client, &session, &hub_id).await + }) + .await + .map_err(|_| anyhow!("Не удалось получить ключи выбранного хаба")) +} + +struct Access(tauri::AppHandle); + +impl VideoAccess for Access { + fn connection(&self) -> Option { + let state = settings::get_state(&self.0).ok()?; + if state.hub_session.is_empty() { + None + } else { + state.video + } + } + + async fn token(&self, connection: &VideoConnection) -> Result { + let generation = hub_tokens::generation(&self.0); + let profile = hub_auth::load_profile(&self.0) + .await + .map_err(|_| anyhow!("Войдите в CoreInfra Hub в Wizard"))?; + ensure!( + profile.personal_hub_id == connection.account_id, + "Войдите в исходный аккаунт CoreInfra Hub" + ); + let tokens = list(&self.0, connection.hub_id.clone()).await?; + let state = settings::get_state(&self.0)?; + ensure!( + !state.hub_session.is_empty() + && state.video.as_ref() == Some(connection) + && generation == hub_tokens::generation(&self.0), + "Подключение видео изменилось" + ); + tokens + .iter() + .find(|t| { + selectable(t, state.proxy_token.as_deref()) + && video::fingerprint(&t.token) == connection.key_id + }) + .map(|t| t.token.clone()) + .context("Исходный ключ видео отозван или недоступен") + } +} + +impl Runtime { + async fn start(&mut self, app: &tauri::AppHandle) -> Result<()> { + if self.listener.as_ref().is_some_and(|t| !t.is_finished()) { + return Ok(()); + } + let hub = VideoHub::new()?; + let listener = TcpListener::bind(video::ADDRESS) + .await + .context("Не удалось открыть локальный порт видео 33789")?; + let access = Access(app.clone()); + self.listener = Some(tokio::spawn(async move { + if let Err(e) = video::server::serve(listener, hub, access).await { + log::warn!("video MCP listener stopped: {e}"); + } + })); + Ok(()) + } + + async fn stop(&mut self) { + if let Some(task) = self.listener.take() { + task.abort(); + let _ = task.await; + } + } +} + +pub(crate) async fn initialize(app: &tauri::AppHandle) { + let state = app.state::(); + let mut runtime = state.lock().await; + let result = async { + let config = settings::get_state(app)?; + wizard_core::agents::codex::migrate_hub_key_filter()?; + if let Some(current) = config.video { + runtime.start(app).await?; + video::install::sync(Some(¤t), Some(¤t))?; + } + anyhow::Ok(()) + } + .await; + if let Err(error) = result { + log::warn!("video setup: {error:#}"); + runtime.error = Some(format!("{error:#}")); + } +} + +pub(crate) async fn shutdown(app: &tauri::AppHandle) { + app.state::().lock().await.stop().await; +} + +#[derive(Serialize)] +pub(crate) struct Status { + hub_id: Option, + key_id: Option, + key_name: Option, + keys_hub_id: Option, + keys: Vec, + running: bool, + error: Option, +} + +#[tauri::command] +pub(crate) async fn get_video_connection(app: tauri::AppHandle) -> Result { + let state = app.state::(); + let runtime = state.lock().await; + let current = settings::get_state(&app) + .map_err(|_| "Настройки недоступны")? + .video; + let (keys_hub_id, keys) = hub_tokens::video_keys(&app); + Ok(Status { + hub_id: current.as_ref().map(|c| c.hub_id.clone()), + key_id: current.as_ref().map(|c| c.key_id.clone()), + key_name: current.map(|c| c.key_name), + keys_hub_id, + keys, + running: runtime.listener.as_ref().is_some_and(|t| !t.is_finished()), + error: runtime.error.clone(), + }) +} + +async fn configure( + app: &tauri::AppHandle, + runtime: &mut Runtime, + selection: Option, +) -> Result<()> { + let current = settings::get_state(app)?.video; + let Some(selection) = selection else { + settings::update_state(app, |s| s.video = None).await?; + runtime.stop().await; + return video::install::sync(current.as_ref(), None); + }; + let generation = hub_tokens::generation(app); + let profile = hub_auth::load_profile(app) + .await + .map_err(|_| anyhow!("Войдите в CoreInfra Hub"))?; + ensure!( + selection.hub_id == profile.active_hub_id(&settings::get_state(app)?.active_hub_id), + "Выбранный хаб изменился" + ); + let tokens = list(app, selection.hub_id.clone()).await?; + let snapshot = settings::get_state(app)?; + ensure!( + selection.hub_id == profile.active_hub_id(&snapshot.active_hub_id), + "Выбранный хаб изменился" + ); + let token = choose(&tokens, &selection, snapshot.proxy_token.as_deref())?; + let key_id = video::fingerprint(&token.token); + let next = VideoConnection { + local_bearer: current + .as_ref() + .filter(|c| { + c.account_id == profile.personal_hub_id + && c.hub_id == selection.hub_id + && c.key_id == key_id + }) + .map_or_else( + || format!("wizard-video-{}", uuid::Uuid::new_v4()), + |c| c.local_bearer.clone(), + ), + account_id: profile.personal_hub_id, + hub_id: selection.hub_id, + key_name: token.name.clone(), + key_id, + }; + runtime.start(app).await?; + let handle = app.clone(); + let result = tauri::async_runtime::spawn_blocking(move || { + let guard = settings::lock_state()?; + let now = settings::get_state(&handle)?; + ensure!( + !now.hub_session.is_empty() + && now.hub_session == snapshot.hub_session + && now.active_hub_id == snapshot.active_hub_id + && generation == hub_tokens::generation(&handle), + "Хаб или аккаунт изменился. Повторите подключение" + ); + video::install::sync(current.as_ref(), Some(&next))?; + if let Err(e) = + settings::update_state_locked(&handle, &guard, |s| s.video = Some(next.clone())) + { + video::install::sync(Some(&next), current.as_ref()) + .context("Не удалось восстановить регистрацию видео")?; + return Err(e); + } + Ok(()) + }) + .await + .context("Video configuration task failed")?; + if result.is_err() && settings::get_state(app)?.video.is_none() { + runtime.stop().await; + } + result +} + +#[tauri::command] +pub(crate) async fn set_video_connection( + app: tauri::AppHandle, + selection: Option, +) -> Result<(), String> { + let state = app.state::(); + let mut runtime = state.lock().await; + let result = configure(&app, &mut runtime, selection) + .await + .map_err(|e| format!("{e:#}")); + runtime.error = result.as_ref().err().cloned(); + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names_are_hub_scoped_and_duplicates_require_the_selected_fingerprint() -> Result<()> { + let tokens: AgentTokens = serde_json::from_value(serde_json::json!([ + {"name":"studio", "token":"a", "issued_at":1}, {"name":"studio", "token":"b", "issued_at":1}, + {"name":"work", "token":"c", "issued_at":1}, {"name":"proxy-alias", "token":"proxy", "issued_at":1}, + {"name":"coreinfra-ai-wizard", "token":"reserved", "issued_at":1} + ]))?; + let mut choice = Selection { + hub_id: "h".into(), + name: "studio".into(), + id: None, + }; + assert!(choose(&tokens, &choice, None).is_err()); + choice.id = Some(video::fingerprint("b")); + assert_eq!(choose(&tokens, &choice, None)?.token, "b"); + choice.name = "work".into(); + assert!(choose(&tokens, &choice, None).is_err()); + choice.id = None; + assert_eq!(choose(&tokens, &choice, None)?.token, "c"); + for name in ["missing", "coreinfra-ai-wizard", "proxy-alias"] { + choice.name = name.into(); + assert!(choose(&tokens, &choice, Some("proxy")).is_err()); + } + Ok(()) + } +} diff --git a/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx b/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx index 4c5aa6e..5bf3df9 100644 --- a/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx +++ b/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx @@ -18,6 +18,7 @@ import { import { PreparedPlanDialog } from "./PreparedPlanDialog"; import { BackupDialog } from "./BackupDialog"; import { DesktopVersion } from "./DesktopInstall"; +import { VideoConnection } from "./VideoConnection"; import { MissingAgent } from "./MissingAgent"; import { agentPageNames } from "./navigation"; import { useHubTokens } from "../settings/hub_tokens"; @@ -251,6 +252,7 @@ export function AgentPage({ onPrepare={prepare} /> )} + {(codex.status === "not_found" || (codex.status === "found" && codex.data.cli.status === "not_found")) && ( )} diff --git a/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx b/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx new file mode 100644 index 0000000..bf3950b --- /dev/null +++ b/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx @@ -0,0 +1,80 @@ +import { invoke } from "@tauri-apps/api/core"; +import { useEffect, useState } from "react"; +import type { Hub } from "./AgentConfiguration"; +import { Button } from "../components/catalyst/button"; +import { Field, Fieldset, Label, Legend } from "../components/catalyst/fieldset"; +import { Select } from "../components/catalyst/select"; +import { Text } from "../components/catalyst/text"; +import { ErrorText } from "../components/ErrorText"; +import { refreshHubTokens } from "../settings/hub_tokens"; + +type VideoKey = { name: string; id: string }; +type Status = { + hub_id: string | null; key_id: string | null; key_name: string | null; + keys_hub_id: string | null; keys: VideoKey[]; running: boolean; error: string | null; +}; + +export function VideoConnection({ hub, hubId, disabled }: { hub: Hub; hubId?: string | null; disabled: boolean }) { + const [status, setStatus] = useState(); + const [draft, setDraft] = useState(); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(); + const [revision, setRevision] = useState(0); + const loadedTokens = hub.status === "ready" ? hub.tokens : undefined; + useEffect(() => { + let active = true; + invoke("get_video_connection").then(result => { + if (active) setStatus(result); + }).catch(cause => { if (active) setError(String(cause)); }); + return () => { active = false; }; + }, [hubId, loadedTokens, revision]); + + const keys = hub.signedIn && loadedTokens && hubId === status?.keys_hub_id ? status?.keys ?? [] : []; + const selected = keys.find(key => key.id === draft) ?? keys.find(key => key.id === status?.key_id) + ?? (keys.length === 1 ? keys[0] : undefined); + async function apply(key: VideoKey | null) { + setBusy(true); + setError(undefined); + try { + await invoke("set_video_connection", { selection: key && { hub_id: hubId, name: key.name, id: key.id } }); + setDraft(undefined); + } catch (cause) { + setError(String(cause)); + } finally { + setRevision(value => value + 1); + setBusy(false); + } + } + + return
+ Видеомодели CoreInfra + {status?.key_name && Ключ «{status.key_name}» · {status.hub_id}} + {hub.signedIn ? + + + : Войдите в CoreInfra Hub, чтобы выбрать ключ для видео.} + {hub.signedIn && hub.status === "ready" && status?.keys_hub_id === hubId && !keys.length && + Создайте именованный API-ключ в выбранном хабе.} + {hub.signedIn && hub.status === "error" && {hub.error}} + {hub.signedIn && {status?.key_id + ? "Оставьте Wizard запущенным и попросите ChatGPT сгенерировать видео в новом чате." + : "Wizard установит скилл для ChatGPT / Codex. Для генерации оставьте Wizard запущенным."}} +
+ + {(status?.key_id || status?.error) && } + {hub.signedIn && } +
+ {status?.key_id && !status.running && !status.error && Сервер видео остановлен. Примените подключение повторно.} + {(error || status?.error) && {error || status?.error}} +
; +} diff --git a/crates/wizard-settings/src/lib.rs b/crates/wizard-settings/src/lib.rs index e79a4a3..2ffd8dd 100644 --- a/crates/wizard-settings/src/lib.rs +++ b/crates/wizard-settings/src/lib.rs @@ -32,12 +32,25 @@ pub struct Settings { /// Random id that tells Wizard installations apart in Hub update metrics. Not a secret. #[serde(skip_serializing_if = "String::is_empty")] pub install_id: String, + /// One local video client grant. The upstream agent token is resolved in Hub. + #[serde(skip_serializing_if = "Option::is_none")] + pub video: Option, /// Application lifecycle preferences, independent of proxy demand. pub application: ApplicationSettings, /// Kept last: it serializes as a TOML table, written after plain keys. pub last_paths: LastPaths, } +// No Debug: local_bearer authenticates paid video requests on loopback. +#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct VideoConnection { + pub account_id: String, + pub hub_id: String, + pub key_name: String, + pub key_id: String, + pub local_bearer: String, +} + #[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] #[serde(default)] pub struct ApplicationSettings { @@ -202,6 +215,23 @@ pub fn update_file(edit: impl FnOnce(&mut Settings)) -> Result<()> { mod tests { use super::*; + #[test] + fn video_connection_round_trips_in_existing_installations() -> Result<()> { + let mut settings: Settings = toml_edit::de::from_str("install_id = 'abcdefgh27'")?; + assert!(settings.video.is_none()); + settings.video = Some(VideoConnection { + account_id: "account".into(), + hub_id: "team".into(), + key_name: "studio".into(), + key_id: "fingerprint".into(), + local_bearer: "local-grant".into(), + }); + let saved = toml_edit::ser::to_string_pretty(&settings)?; + let restored: Settings = toml_edit::de::from_str(&saved)?; + assert!(settings == restored); + Ok(()) + } + #[test] fn application_defaults_and_partial_tables() { for text in ["", "[last_paths]\n", "[application]\n"] { diff --git a/skills/coreinfra-video/SKILL.md b/skills/coreinfra-video/SKILL.md new file mode 100644 index 0000000..0211717 --- /dev/null +++ b/skills/coreinfra-video/SKILL.md @@ -0,0 +1,14 @@ +--- +name: coreinfra-video +description: Generate video from a prompt or image through CoreInfra Hub, list video models and prices, and retrieve existing video jobs. Use for requests such as «сгенерируй видео», «оживи фото» or «забери готовый ролик» in a Wizard-connected workspace. +--- + +Use the tools on the `coreinfra_video` MCP server. Wizard supplies the credential for the video key selected in its ChatGPT / Codex settings. If the server is offline, open Wizard and check the video connection. + +- Use `video_models` for current model IDs, providers and public RUB-per-second rates. A request for models, a quote or a prompt only calls for a read. +- To generate a requested video, call `video_create` once with `model`, `prompt` and any model-specific JSON in `fields`. Keep the entire returned `job` object with the conversation or the user's script. If the submission outcome is uncertain, report it. A second submission can incur another charge. +- Call `video_result` with that job. Poll pending or in-progress jobs with a delay. On completion, present the returned absolute `path` as a video. To check status without saving a file, pass `download: false`. For another OpenRouter output, pass its zero-based `index`. After interruption, resume with the same job. If the connection changed, reselect its original Hub and key in Wizard. +- The MCP object `fields` is merged into the **top level** of the upstream JSON body. Preserve the requested types and provider nesting. For example, OpenRouter accepts `{"duration":5,"provider":{"options":{"":{"
{ @@ -46,7 +50,7 @@ struct Tools { struct NoArgs {} impl Tools { - async fn call(&self, name: &str, args: Value, connection: &VideoConnection) -> Result { + async fn call(&self, name: &str, args: Value) -> Result { match name { "video_models" => { serde_json::from_value::(args)?; @@ -56,16 +60,12 @@ impl Tools { "video_create" => { let input: Create = serde_json::from_value(args).context("Invalid video_create arguments")?; - self.hub - .create(input, connection, self.access.token(connection)) - .await + self.hub.create(input, self.access.as_ref()).await } "video_result" => { let input: GetResult = serde_json::from_value(args).context("Invalid video_result arguments")?; - self.hub - .result(input, connection, self.access.token(connection)) - .await + self.hub.result(input, self.access.as_ref()).await } _ => bail!("Unknown video tool"), } @@ -96,15 +96,10 @@ impl ServerHandler for Tools { async fn call_tool( &self, request: CallToolRequestParams, - ctx: RequestContext, + _: RequestContext, ) -> Result { - let connection = ctx - .extensions - .get::() - .and_then(|parts| parts.extensions.get::()) - .ok_or_else(|| ErrorData::internal_error("Missing video grant", None))?; let args = Value::Object(request.arguments.unwrap_or_default()); - let response = match self.call(&request.name, args, connection).await { + let response = match self.call(&request.name, args).await { Ok(value) => CallToolResult::structured(value), Err(error) => CallToolResult::error(vec![ContentBlock::text(format!("{error:#}"))]), }; @@ -121,7 +116,7 @@ fn empty(status: StatusCode) -> Response> { async fn handle( mut request: Request, service: StreamableHttpService, NeverSessionManager>, - access: Arc, + local_bearer: Arc, ) -> Result>, Infallible> { if request.uri().path() != "/mcp" || request.uri().query().is_some() { return Ok(empty(StatusCode::NOT_FOUND)); @@ -133,27 +128,47 @@ async fn handle( .and_then(|value| value.split_once(' ')) .filter(|(scheme, _)| scheme.eq_ignore_ascii_case("bearer")) .map(|(_, token)| token); - let connection = access - .connection() - .filter(|c| !c.local_bearer.is_empty() && bearer == Some(c.local_bearer.as_str())); - let Some(connection) = connection else { + if bearer != Some(local_bearer.as_ref()) { let mut response = empty(StatusCode::UNAUTHORIZED); response.headers_mut().insert( hyper::header::WWW_AUTHENTICATE, hyper::header::HeaderValue::from_static("Bearer"), ); return Ok(response); - }; + } request.headers_mut().remove(AUTHORIZATION); - request.extensions_mut().insert(connection); Ok(service.handle(request).await) } -pub async fn serve(listener: TcpListener, hub: VideoHub, access: impl VideoAccess) -> Result<()> { +/// Publish the new listener's grant before it accepts requests. A failed registration +/// drops the socket and leaves no running service. +pub async fn start( + listener: TcpListener, + hub: VideoHub, + access: impl VideoAccess, + register: impl FnOnce(&str) -> Result<()> + Send + 'static, +) -> Result> { let addr = listener.local_addr()?; ensure!(addr.ip().is_loopback(), "Video MCP must bind to loopback"); + let bearer = tokio::task::spawn_blocking(move || { + let bearer = format!("wizard-video-{}", uuid::Uuid::new_v4()); + register(&bearer)?; + anyhow::Ok(bearer) + }) + .await + .context("Video installation task failed")??; + Ok(tokio::spawn(serve(listener, addr, hub, bearer, access))) +} + +async fn serve( + listener: TcpListener, + addr: SocketAddr, + hub: VideoHub, + local_bearer: String, + access: impl VideoAccess, +) { + let local_bearer: Arc = local_bearer.into(); let access = Arc::new(access); - let factory_access = Arc::clone(&access); let config = StreamableHttpServerConfig::default() .with_allowed_hosts([addr.to_string()]) .enforce_origin_validation() @@ -164,7 +179,7 @@ pub async fn serve(listener: TcpListener, hub: VideoHub, access: impl VideoAcces move || { Ok(Tools { hub: hub.clone(), - access: Arc::clone(&factory_access), + access: Arc::clone(&access), }) }, Arc::new(NeverSessionManager::default()), @@ -183,9 +198,9 @@ pub async fn serve(listener: TcpListener, hub: VideoHub, access: impl VideoAcces } }; let service = service.clone(); - let access = Arc::clone(&access); + let bearer = Arc::clone(&local_bearer); clients.spawn(async move { - let handler = service_fn(move |req| handle(req, service.clone(), Arc::clone(&access))); + let handler = service_fn(move |req| handle(req, service.clone(), Arc::clone(&bearer))); let _ = http1::Builder::new().serve_connection(TokioIo::new(socket), handler).await; }); } diff --git a/crates/wizard-core/src/video/tests.rs b/crates/wizard-core/src/video/tests.rs index 3027665..70d33bb 100644 --- a/crates/wizard-core/src/video/tests.rs +++ b/crates/wizard-core/src/video/tests.rs @@ -1,7 +1,7 @@ use super::*; -use crate::video::server::{VideoAccess, serve}; +use crate::{config_files::toml, video::server::VideoAccess}; use alloc::sync::Arc; -use core::convert::Infallible; +use core::{convert::Infallible, future::Future}; use http_body_util::{BodyExt as _, Full}; use hyper::{ Request, Response as HttpResponse, @@ -17,25 +17,19 @@ use tokio::{net::TcpListener, task::JoinHandle}; const OR_MODEL: &str = "example/cinema"; const FAL_MODEL: &str = "example/flux/image-to-video"; const KEY: &str = "fixture-upstream-secret"; +const LOCAL: &str = "fixture-local-grant"; const MP4: &[u8] = include_bytes!("../../tests/fixtures/blue.mp4"); -fn connection() -> VideoConnection { - VideoConnection { - account_id: "personal".into(), - hub_id: "team".into(), - key_id: fingerprint(KEY), - key_name: "studio".into(), - local_bearer: "fixture-local-grant".into(), - } -} - -struct Access(VideoConnection); +struct Access(&'static str); impl VideoAccess for Access { - fn connection(&self) -> Option { - Some(self.0.clone()) - } - fn token(&self, _: &VideoConnection) -> impl Future> { - core::future::ready(Ok(KEY.into())) + fn token<'a>( + &'a self, + _: Option<&'a Job>, + ) -> impl Future> + Send + 'a { + core::future::ready(Ok(VideoKey { + hub_id: "team".into(), + token: self.0.into(), + })) } } @@ -53,6 +47,12 @@ struct Fixture { fake_hub: JoinHandle<()>, } +struct RunningServer { + url: String, + bearer: String, + task: JoinHandle<()>, +} + fn catalog() -> Value { json!({"providers":{"test":{"models":{ OR_MODEL:{"category":"videos","display_name":"Cinema","endpoints":[OPENROUTER],"prices":{"video_seconds":2_500_000},"lowest_prices":{"video_seconds":1_000_000}}, @@ -175,11 +175,29 @@ impl Fixture { }) } - async fn start(&self, config: VideoConnection) -> Result<(String, JoinHandle>)> { + async fn start(&self, access: impl VideoAccess) -> Result { + let path = self.dir.path().join("profile/config.toml"); + let config = path.clone(); let local = TcpListener::bind("127.0.0.1:0").await?; let url = format!("http://{}/mcp", local.local_addr()?); - let task = tokio::spawn(serve(local, self.hub.clone(), Access(config))); - Ok((url, task)) + let task = server::start(local, self.hub.clone(), access, move |bearer| { + install::sync_at(&config, None, Some(bearer)) + }) + .await?; + let doc = toml::read(&path)?; + let bearer = toml::get_string( + &doc, + &[ + "mcp_servers", + "coreinfra_video", + "http_headers", + "Authorization", + ], + ) + .and_then(|header| header.strip_prefix("Bearer ")) + .context("Installed MCP authorization")? + .to_owned(); + Ok(RunningServer { url, bearer, task }) } fn request(&self, url: &str, method: &str, params: &Value) -> RequestBuilder { @@ -192,10 +210,14 @@ impl Fixture { .body(json!({"jsonrpc":"2.0","id":1,"method":method,"params":params}).to_string()) } - async fn call(&self, url: &str, tool: &str, args: Value) -> Result { + async fn call(&self, server: &RunningServer, tool: &str, args: Value) -> Result { let response = self - .request(url, "tools/call", &json!({"name":tool,"arguments":args})) - .bearer_auth(connection().local_bearer) + .request( + &server.url, + "tools/call", + &json!({"name":tool,"arguments":args}), + ) + .bearer_auth(&server.bearer) .send() .await?; ensure!( @@ -223,8 +245,8 @@ impl Drop for Fixture { #[tokio::test] async fn both_providers_fields_and_resume_across_restart() -> Result<()> { let fixture = Fixture::new().await?; - let (url, first) = fixture.start(connection()).await?; - let catalog = fixture.call(&url, "video_models", json!({})).await?; + let first = fixture.start(Access(KEY)).await?; + let catalog = fixture.call(&first, "video_models", json!({})).await?; let models = catalog["structuredContent"]["models"] .as_array() .context("models")?; @@ -239,7 +261,7 @@ async fn both_providers_fields_and_resume_across_restart() -> Result<()> { "provider":{"options":{"example":{"guidance_scale":4.5}}},"frame_images":[{"image_url":{"url":"data:image/png;base64,AAAA"},"frame_type":"first_frame","type":"image_url"}]}); let first_job = fixture .call( - &url, + &first, "video_create", json!({"model":OR_MODEL,"prompt":"river","fields":fields}), ) @@ -248,7 +270,7 @@ async fn both_providers_fields_and_resume_across_restart() -> Result<()> { let fal_fields = json!({"image_url":"data:image/png;base64,AAAA","extra":{"a":null},"seed":0,"generate_audio":false}); let fal_job = fixture .call( - &url, + &first, "video_create", json!({"model":FAL_MODEL,"prompt":"waves","fields_json":fal_fields.to_string()}), ) @@ -257,14 +279,24 @@ async fn both_providers_fields_and_resume_across_restart() -> Result<()> { assert_eq!(first_job["id"], "job-1"); assert_eq!(fal_job["id"], "f-1"); let pending = fixture - .call(&url, "video_result", json!({"job":first_job})) + .call(&first, "video_result", json!({"job":first_job})) .await?; assert_eq!(pending["structuredContent"]["status"], "in_progress"); - first.abort(); - let _ = first.await; - let (url, _second) = fixture.start(connection()).await?; + first.task.abort(); + let _ = first.task.await; + let second = fixture.start(Access(KEY)).await?; + assert_ne!(first.bearer, second.bearer); + assert_eq!( + fixture + .request(&second.url, "tools/list", &json!({})) + .bearer_auth(&first.bearer) + .send() + .await? + .status(), + StatusCode::UNAUTHORIZED + ); let completed = fixture - .call(&url, "video_result", json!({"job":first_job,"index":1})) + .call(&second, "video_result", json!({"job":first_job,"index":1})) .await?["structuredContent"] .clone(); assert_eq!(completed["cost_rub"], 6.5); @@ -272,7 +304,7 @@ async fn both_providers_fields_and_resume_across_restart() -> Result<()> { assert!(path.starts_with(fixture.dir.path())); assert_eq!(std::fs::read(path)?, MP4); let fal = fixture - .call(&url, "video_result", json!({"job":fal_job})) + .call(&second, "video_result", json!({"job":fal_job})) .await?["structuredContent"] .clone(); assert_eq!( @@ -281,7 +313,7 @@ async fn both_providers_fields_and_resume_across_restart() -> Result<()> { ); let link = fixture .call( - &url, + &second, "video_result", json!({"job":fal_job,"download":false}), ) @@ -313,8 +345,8 @@ async fn both_providers_fields_and_resume_across_restart() -> Result<()> { #[tokio::test] async fn validates_before_billing_and_confines_credentials() -> Result<()> { let fixture = Fixture::new().await?; - let (url, _server) = fixture.start(connection()).await?; - let unauthorized = fixture.hub.client.post(&url).send().await?; + let server = fixture.start(Access(KEY)).await?; + let unauthorized = fixture.hub.client.post(&server.url).send().await?; assert_eq!(unauthorized.status(), 401); for (header, value) in [ ("Origin", "https://elsewhere.example"), @@ -322,8 +354,8 @@ async fn validates_before_billing_and_confines_credentials() -> Result<()> { ] { assert!( fixture - .request(&url, "tools/list", &json!({})) - .bearer_auth(connection().local_bearer) + .request(&server.url, "tools/list", &json!({})) + .bearer_auth(&server.bearer) .header(header, value) .send() .await? @@ -345,7 +377,7 @@ async fn validates_before_billing_and_confines_credentials() -> Result<()> { .expect("args") .extend(extra.as_object().expect("extra").clone()); assert_eq!( - fixture.call(&url, "video_create", args).await?["isError"], + fixture.call(&server, "video_create", args).await?["isError"], true ); } @@ -361,7 +393,7 @@ async fn validates_before_billing_and_confines_credentials() -> Result<()> { assert_eq!( fixture .call( - &url, + &server, "video_create", json!({"model":OR_MODEL,"prompt":prompt}) ) @@ -380,7 +412,7 @@ async fn validates_before_billing_and_confines_credentials() -> Result<()> { invalid[k] = json!(v); assert_eq!( fixture - .call(&url, "video_result", json!({"job":invalid})) + .call(&server, "video_result", json!({"job":invalid})) .await?["isError"], true ); @@ -388,7 +420,7 @@ async fn validates_before_billing_and_confines_credentials() -> Result<()> { job["id"] = json!("bad-link"); assert_eq!( fixture - .call(&url, "video_result", json!({"job":job})) + .call(&server, "video_result", json!({"job":job})) .await?["isError"], true ); @@ -415,8 +447,7 @@ fn install_upgrade_disable_and_collisions() -> Result<()> { "[mcp_servers.other]\nurl = 'https://elsewhere.example'\n", ); fs::write(&config, original)?; - let c = connection(); - install::sync_at(&config, None, Some(&c))?; + install::sync_at(&config, None, Some(LOCAL))?; let initial = fs::read_to_string(&config)?; #[cfg(unix)] { @@ -427,18 +458,18 @@ fn install_upgrade_disable_and_collisions() -> Result<()> { assert!(!initial.contains("COREINFRA_API_KEY") && !initial.contains(KEY)); let bundled = fs::read_to_string(&source)?; fs::write(skill.join("my-notes"), "custom")?; - install::sync_at(&config, Some(&c), Some(&c))?; + install::sync_at(&config, Some(LOCAL), Some(LOCAL))?; assert_eq!(fs::read_to_string(&config)?, initial); let manifest = skill.join(".wizard-managed.json"); let mut hashes: Value = serde_json::from_str(&fs::read_to_string(&manifest)?)?; fs::write(&source, "old bundled file")?; hashes["SKILL.md"] = json!(fingerprint("old bundled file")); fs::write(manifest, hashes.to_string())?; - install::sync_at(&config, Some(&c), Some(&c))?; + install::sync_at(&config, Some(LOCAL), Some(LOCAL))?; assert_eq!(fs::read_to_string(&source)?, bundled); fs::write(&source, "user changes")?; - assert!(install::sync_at(&config, Some(&c), Some(&c)).is_err()); - install::sync_at(&config, Some(&c), None)?; + assert!(install::sync_at(&config, Some(LOCAL), Some(LOCAL)).is_err()); + install::sync_at(&config, Some(LOCAL), None)?; assert_eq!(fs::read_to_string(&source)?, "user changes"); assert!(skill.join("my-notes").exists()); assert_eq!(fs::read_to_string(&config)?, original); @@ -446,12 +477,12 @@ fn install_upgrade_disable_and_collisions() -> Result<()> { let config = other_dir.path().join("config.toml"); let foreign = "[mcp_servers.coreinfra_video]\nurl='https://other.example'\n"; fs::write(&config, foreign)?; - assert!(install::sync_at(&config, None, Some(&c)).is_err()); + assert!(install::sync_at(&config, None, Some(LOCAL)).is_err()); assert_eq!(fs::read_to_string(config)?, foreign); assert!(!other_dir.path().join("skills").exists()); let fresh = tempfile::tempdir()?; let config = fresh.path().join("config.toml"); - install::sync_at(&config, None, Some(&c))?; + install::sync_at(&config, None, Some(LOCAL))?; assert!(!fs::read_to_string(&config)?.contains("shell_environment_policy")); fs::write( fresh @@ -459,41 +490,20 @@ fn install_upgrade_disable_and_collisions() -> Result<()> { .join("skills/coreinfra-video/.wizard-managed.json"), "invalid", )?; - assert!(install::sync_at(&config, Some(&c), None).is_err()); - assert!(!fs::read_to_string(config)?.contains(&c.local_bearer)); + assert!(install::sync_at(&config, Some(LOCAL), None).is_err()); + assert!(!fs::read_to_string(config)?.contains(LOCAL)); Ok(()) } #[tokio::test] async fn rotated_connection_and_interrupted_download_fail_closed() -> Result<()> { let fixture = Fixture::new().await?; - let rotated = VideoConnection { - key_id: fingerprint("replacement"), - local_bearer: "rotated-grant".into(), - ..connection() - }; - let (url, _server) = fixture.start(rotated.clone()).await?; - assert_eq!( - fixture - .request(&url, "tools/list", &json!({})) - .bearer_auth(connection().local_bearer) - .send() - .await? - .status(), - 401 - ); + let server = fixture.start(Access("replacement-key")).await?; let original = json!({"id":"f-1","provider":"fal","model":FAL_MODEL,"hub_id":"team","key_id":fingerprint(KEY)}); let response = fixture - .request( - &url, - "tools/call", - &json!({"name":"video_result","arguments":{"job":original}}), - ) - .bearer_auth(&rotated.local_bearer) - .send() + .call(&server, "video_result", json!({"job":original})) .await?; - let data: Value = serde_json::from_slice(&response.bytes().await?)?; - assert_eq!(data["result"]["isError"], true); + assert_eq!(response["isError"], true); assert!(fixture.requests.lock().expect("requests").is_empty()); let listener = TcpListener::bind("127.0.0.1:0").await?; @@ -520,28 +530,134 @@ async fn rotated_connection_and_interrupted_download_fail_closed() -> Result<()> Ok(()) } +#[tokio::test] +async fn installation_and_catalog_work_without_hub_credentials() -> Result<()> { + use core::sync::atomic::{AtomicUsize, Ordering}; + struct SignedOut(Arc); + impl VideoAccess for SignedOut { + fn token<'a>( + &'a self, + _: Option<&'a Job>, + ) -> impl Future> + Send + 'a { + core::future::poll_fn(|_| { + self.0.fetch_add(1, Ordering::SeqCst); + core::task::Poll::Ready(Err(anyhow!("Sign in to Wizard"))) + }) + } + } + + let fixture = Fixture::new().await?; + let lookups = Arc::new(AtomicUsize::new(0)); + let server = fixture.start(SignedOut(Arc::clone(&lookups))).await?; + assert!( + fixture + .dir + .path() + .join("profile/skills/coreinfra-video/SKILL.md") + .is_file() + ); + let catalog = fixture.call(&server, "video_models", json!({})).await?; + assert_eq!( + catalog["structuredContent"]["models"] + .as_array() + .context("models")? + .len(), + 2 + ); + let missing = fixture + .call( + &server, + "video_create", + json!({"model":"unknown","prompt":"river"}), + ) + .await?; + assert_eq!(missing["isError"], true); + assert_eq!(lookups.load(Ordering::SeqCst), 0); + let generation = fixture + .call( + &server, + "video_create", + json!({"model":OR_MODEL,"prompt":"river"}), + ) + .await?; + assert_eq!(generation["isError"], true); + assert_eq!(lookups.load(Ordering::SeqCst), 1); + assert!( + fixture + .requests + .lock() + .expect("requests") + .iter() + .all(|r| r.method == "GET" && r.auth.is_none()) + ); + Ok(()) +} + +#[tokio::test] +async fn failed_registration_releases_the_listener_and_preserves_a_foreign_server() -> Result<()> { + let fixture = Fixture::new().await?; + let config = fixture.dir.path().join("config.toml"); + let foreign = "[mcp_servers.coreinfra_video]\nurl = 'https://other.example'\n"; + std::fs::write(&config, foreign)?; + let register_path = config.clone(); + let listener = TcpListener::bind("127.0.0.1:0").await?; + let addr = listener.local_addr()?; + let result = server::start(listener, fixture.hub.clone(), Access(KEY), move |bearer| { + install::sync_at(®ister_path, None, Some(bearer)) + }) + .await; + assert!(result.is_err()); + let _new_listener = TcpListener::bind(addr).await?; + assert_eq!(std::fs::read_to_string(config)?, foreign); + assert!(!fixture.dir.path().join("skills").exists()); + Ok(()) +} + #[tokio::test] #[ignore = "requires WIZARD_VIDEO_TEST_PYTHON with mcp and WIZARD_VIDEO_TEST_CODEX pointing to a Codex app-server binary"] async fn native_and_python_clients() -> Result<()> { + use std::process::Stdio; + use tokio::io::{AsyncBufReadExt as _, BufReader}; let fixture = Fixture::new().await?; - let (url, _task) = fixture.start(connection()).await?; let profile = fixture.dir.path().join("profile"); let path = profile.join("config.toml"); std::fs::create_dir(&profile)?; + let dotenv = "COREINFRA_API_KEY=unrelated-codex-fixture-key\n"; + std::fs::write(profile.join(".env"), dotenv)?; std::fs::write( &path, - "[shell_environment_policy]\nexclude = ['WIZARD_VIDEO_TEST_FILTER']\n", + "[shell_environment_policy]\nexclude = ['WIZARD_VIDEO_TEST_FILTER', 'COREINFRA_API_KEY']\n", )?; - install::sync_at(&path, None, Some(&connection()))?; - std::fs::write(&path, std::fs::read_to_string(&path)?.replace(URL, &url))?; let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/video_clients.py"); - let status = tokio::process::Command::new(std::env::var("WIZARD_VIDEO_TEST_PYTHON")?) + let mut clients = tokio::process::Command::new(std::env::var("WIZARD_VIDEO_TEST_PYTHON")?) .arg(script) .arg(&profile) .current_dir(fixture.dir.path()) - .status() + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .kill_on_drop(true) + .spawn()?; + let mut output = BufReader::new(clients.stdout.take().context("client output")?).lines(); + let ready = tokio::time::timeout(Duration::from_secs(60), output.next_line()).await??; + ensure!( + ready.as_deref() == Some("codex-ready"), + "Codex did not start" + ); + + // Install while Codex is running. Only the test listener's port is ephemeral. + let server = fixture.start(Access(KEY)).await?; + let config = std::fs::read_to_string(&path)?; + assert!(config.contains(URL) && !config.contains(KEY)); + std::fs::write(&path, config.replace(URL, &server.url))?; + clients + .stdin + .take() + .context("client input")? + .write_all(b"installed\n") .await?; + let status = tokio::time::timeout(Duration::from_secs(90), clients.wait()).await??; ensure!(status.success(), "external client test failed"); + assert_eq!(std::fs::read_to_string(profile.join(".env"))?, dotenv); let requests = fixture.requests.lock().expect("requests"); let bodies: Vec<_> = requests .iter() diff --git a/crates/wizard-core/tests/video_clients.py b/crates/wizard-core/tests/video_clients.py index 6bdb0df..fe099c4 100644 --- a/crates/wizard-core/tests/video_clients.py +++ b/crates/wizard-core/tests/video_clients.py @@ -26,7 +26,8 @@ def native_client(profile): env = {k: os.environ[k] for k in ("PATH", "HOME", "TMPDIR", "LANG", "USER") if k in os.environ} env.update(CODEX_HOME=str(profile), HTTP_PROXY="http://127.0.0.1:1", HTTPS_PROXY="http://127.0.0.1:1", ALL_PROXY="http://127.0.0.1:1", - NO_PROXY="127.0.0.1,localhost", WIZARD_VIDEO_TEST_FILTER="fixture") + NO_PROXY="127.0.0.1,localhost", WIZARD_VIDEO_TEST_FILTER="fixture", + COREINFRA_API_KEY="unrelated-process-fixture-key") binary = os.environ["WIZARD_VIDEO_TEST_CODEX"] server = subprocess.Popen([binary, "app-server", "--listen", "stdio://", "-c", 'model_provider="fixture"', "-c", 'model="offline"'], @@ -59,14 +60,23 @@ def call(method, params): call("initialize", {"clientInfo":{"name":"wizard_fixture","version":"1"}, "capabilities":{"experimentalApi":True}}) send({"jsonrpc":"2.0","method":"initialized"}) - thread = call("thread/start", {"cwd":str(profile.parent),"ephemeral":True,"sandbox":"workspace-write"})["thread"]["id"] - skills = call("skills/list", {"cwds":[str(profile.parent)],"forceReload":True}) - assert any(s["name"] == "coreinfra-video" for group in skills["data"] for s in group["skills"]) + options = {"cwd":str(profile.parent),"ephemeral":True,"sandbox":"workspace-write"} + old_thread = call("thread/start", options)["thread"]["id"] + before = call("mcpServerStatus/list", {"threadId":old_thread}) + assert all(s["name"] != "coreinfra_video" for s in before["data"]) + skill_query = {"cwds":[str(profile.parent)]} + previous_skills = call("skills/list", skill_query) + assert not any(s["name"] == "coreinfra-video" for group in previous_skills["data"] for s in group["skills"]) + print("codex-ready", flush=True) + assert sys.stdin.readline() == "installed\n" + thread = call("thread/start", options)["thread"]["id"] + skills = call("skills/list", skill_query) + assert any(s["name"] == "coreinfra-video" and s["enabled"] for group in skills["data"] for s in group["skills"]) inventory = call("mcpServerStatus/list", {"threadId":thread,"serverName":"coreinfra_video"}) tools = next(s["tools"] for s in inventory["data"] if s["name"] == "coreinfra_video") assert set(tools) == {"video_models","video_create","video_result"} shell = call("command/exec", {"command":[sys.executable,"-I","-c", - "import os; print('WIZARD_VIDEO_TEST_FILTER' in os.environ)"], + "import os; print(any(k in os.environ for k in ('WIZARD_VIDEO_TEST_FILTER', 'COREINFRA_API_KEY')))"], "cwd":str(profile.parent), "sandboxPolicy":{"type":"workspaceWrite","networkAccess":False}}) assert shell["exitCode"] == 0 and shell["stdout"].strip() == "False", shell result = call("mcpServer/tool/call", {"threadId":thread,"server":"coreinfra_video", @@ -107,4 +117,4 @@ async def python_client(profile, existing_job): test_profile = Path(sys.argv[1]).resolve() existing = native_client(test_profile) asyncio.run(python_client(test_profile, existing)) - print("Native ChatGPT skill, existing shell policy, MCP tools and Python SDK round trip passed.") + print("Native Codex discovered the installed MCP in a new chat, applied the shell policy and shared jobs with the Python SDK.", file=sys.stderr) diff --git a/crates/wizard-gui/Cargo.toml b/crates/wizard-gui/Cargo.toml index a37218f..e34a23b 100644 --- a/crates/wizard-gui/Cargo.toml +++ b/crates/wizard-gui/Cargo.toml @@ -33,7 +33,6 @@ tauri-plugin-log.workspace = true tauri-plugin-single-instance.workspace = true tauri-plugin-updater.workspace = true tokio.workspace = true -uuid.workspace = true wizard-core.workspace = true wizard-proxy.workspace = true wizard-proxy-daemon.workspace = true diff --git a/crates/wizard-gui/src/agents.rs b/crates/wizard-gui/src/agents.rs index 80da8ba..5171242 100644 --- a/crates/wizard-gui/src/agents.rs +++ b/crates/wizard-gui/src/agents.rs @@ -159,7 +159,7 @@ pub(crate) async fn get_agent_backups(agent: BackupAgent) -> Result, .map_err(|error| format!("{error:#}")) } -/// Token for an enabled mode: an existing value, or a new Hub token named after the agent. +/// Use an existing value or provision the agent's default Hub token for an enabled mode. // No Debug: the value is a credential. #[derive(Deserialize)] #[serde(tag = "kind", rename_all = "snake_case")] @@ -211,8 +211,8 @@ impl AgentChange { } /// Enabled modes require `token`, except Claude Desktop subscription which uses -/// Wizard's shared proxy credential. Never log tokens. `Create` issues the Hub token -/// before preparation: cancelling or failed preparation keeps it in the Hub list. +/// Wizard's shared proxy credential. Never log tokens. Newly issued defaults remain +/// in the Hub if preparation fails or is cancelled. #[tauri::command] pub(crate) async fn prepare_agent_change( change: AgentChange, @@ -237,6 +237,9 @@ pub(crate) async fn prepare_agent_change( ) { match token { Some(TokenChoice::Existing { token }) if !token.is_empty() => token, + Some(TokenChoice::Create) if matches!(change, AgentChange::CodexSetRouting(_)) => { + hub_tokens::codex_token(&app, None).await?.token + } Some(TokenChoice::Create) => { hub_tokens::issue(&app, change.token_agent().token_name()).await? } diff --git a/crates/wizard-gui/src/hub_tokens.rs b/crates/wizard-gui/src/hub_tokens.rs index 033aadf..7392367 100644 --- a/crates/wizard-gui/src/hub_tokens.rs +++ b/crates/wizard-gui/src/hub_tokens.rs @@ -9,7 +9,11 @@ use crate::{ use core::future::Future; use serde::Serialize; use tauri::Manager as _; -use wizard_core::hub::{self, AgentTokens, Client, HubError, SessionUpdate}; +use wizard_core::{ + agents::TokenAgent, + hub::{self, AgentTokens, Client, HubError, SessionUpdate}, + video::{Job, VideoKey, fingerprint}, +}; pub(crate) const PROXY_TOKEN_NAME: &str = "coreinfra-ai-wizard"; @@ -24,19 +28,7 @@ pub(crate) struct HubTokens { generation: u64, } -pub(crate) fn video_keys(app: &tauri::AppHandle) -> (Option, Vec) { - let snapshot = app.state::>().current().state; - let proxy = settings::get_state(app).ok().and_then(|s| s.proxy_token); - let keys = snapshot - .tokens - .iter() - .filter(|t| crate::video::selectable(t, proxy.as_deref())) - .map(crate::video::VideoKey::from) - .collect(); - (snapshot.hub_id, keys) -} - -pub(crate) fn generation(app: &tauri::AppHandle) -> u64 { +fn generation(app: &tauri::AppHandle) -> u64 { app.state::>() .current() .state @@ -277,13 +269,13 @@ pub(crate) async fn proxy_token(app: &tauri::AppHandle) -> Result( +async fn accept_token_response( app: &tauri::AppHandle, session: &mut String, (update, result): (SessionUpdate, Result), + failure: &'static str, ) -> Result { let next = match &update { SessionUpdate::Refreshed(refreshed) => refreshed.clone(), @@ -322,11 +315,110 @@ async fn accept_proxy_response( } *session = next; result.map_err(|error| { - log::error!("Wizard proxy Hub request failed: {error:?}"); - "Не удалось получить токен Wizard в личном Hub".into() + log::error!("Wizard Hub token request failed: {error:?}"); + failure.into() }) } +const PROXY_LOOKUP_ERROR: &str = "Не удалось получить токен Wizard в личном Hub"; +const CODEX_LOOKUP_ERROR: &str = "Не удалось получить ключ Codex в выбранном хабе"; +static CODEX_TOKEN_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); + +/// Both automatic Codex setup and video use the same default in Wizard's selected Hub. +/// Capture the selection now, even when the caller awaits this after other I/O. +pub(crate) fn codex_token<'a>( + app: &'a tauri::AppHandle, + job: Option<&'a Job>, +) -> impl Future> + Send + 'a { + let revision = generation(app); + let selected = settings::get_state(app) + .map_err(|_| "Настройки Wizard недоступны") + .and_then(|s| { + if s.hub_session.is_empty() { + Err("Войдите в CoreInfra Hub в Wizard") + } else { + Ok(s.active_hub_id) + } + }); + async move { + let selected = selected?; + let current = || { + let state = settings::get_state(app).map_err(|_| "Настройки Wizard недоступны")?; + if state.hub_session.is_empty() + || state.active_hub_id != selected + || generation(app) != revision + { + return Err("Хаб или аккаунт изменился во время запроса"); + } + Ok(state) + }; + current()?; + let profile = hub_auth::load_profile(app) + .await + .map_err(|_| CODEX_LOOKUP_ERROR)?; + let hub_id = profile.active_hub_id(&selected).to_owned(); + if job.is_some_and(|j| j.hub_id != hub_id) { + return Err("Выберите в Wizard исходный хаб этого видеозадания".into()); + } + let client = hub::client().map_err(|_| CODEX_LOOKUP_ERROR)?; + let list = async { + let state = current()?; + let mut session = state.hub_session; + let response = hub::list_agent_tokens(&client, &session, &hub_id).await; + let mut tokens = + accept_token_response(app, &mut session, response, CODEX_LOOKUP_ERROR).await?; + tokens.retain(|t| Some(t.token.as_str()) != state.proxy_token.as_deref()); + Ok((tokens, session)) + }; + let token = + resolve_codex_token(job.map(|j| j.key_id.as_str()), list, async |mut session| { + current()?; + let response = hub::issue_agent_token( + &client, + &session, + &hub_id, + TokenAgent::Codex.token_name(), + ) + .await; + let issued = + accept_token_response(app, &mut session, response, CODEX_LOOKUP_ERROR).await?; + HubTokensManager::refresh(app).await; + Ok(issued) + }) + .await?; + current()?; + Ok(VideoKey { hub_id, token }) + } +} + +async fn resolve_codex_token( + original_key_id: Option<&str>, + list: impl Future>, + issue: impl AsyncFnOnce(String) -> Result, +) -> Result { + // New jobs and Codex setup share this list-and-issue operation. Polls never issue. + let _guard = if original_key_id.is_none() { + Some(CODEX_TOKEN_LOCK.lock().await) + } else { + None + }; + let (tokens, session) = list.await?; + let name = TokenAgent::Codex.token_name(); + if let Some(id) = original_key_id { + return tokens + .iter() + .find(|t| t.name == name && fingerprint(&t.token) == id) + .map(|t| t.token.clone()) + .ok_or_else(|| { + "Исходный ключ Codex для этого видеозадания отозван или недоступен".into() + }); + } + if let Some(token) = tokens.named(name) { + return Ok(token.token.clone()); + } + issue(session).await +} + /// Creates a token named `name` in the selected hub, then reloads the list so the token shows /// with Hub's own data before its value is returned. A failed reload is published as the list /// error and does not fail the result. Never log the value. @@ -443,6 +535,82 @@ mod tests { .expect("test token list should deserialize") } + #[test] + fn codex_and_video_share_the_oldest_default_and_poll_with_the_original_key() { + tauri::async_runtime::block_on(async { + let tokens: AgentTokens = serde_json::from_value(serde_json::json!([ + {"name":"agent-codex", "token":"recent", "issued_at":3}, + {"name":"studio", "token":"custom", "issued_at":1}, + {"name":"coreinfra-ai-wizard", "token":"proxy", "issued_at":1}, + {"name":"agent-codex", "token":"original", "issued_at":2} + ])) + .expect("fixture"); + let resolve = |id| { + resolve_codex_token(id, ready(Ok((tokens.clone(), String::new()))), async |_| { + panic!("existing defaults and job lookups must not issue keys") + }) + }; + assert_eq!(resolve(None).await.as_deref(), Ok("original")); + assert_eq!( + resolve(Some(&fingerprint("recent"))).await.as_deref(), + Ok("recent") + ); + for missing in ["revoked", "custom", "proxy"] { + assert!( + resolve_codex_token( + Some(&fingerprint(missing)), + ready(Ok((tokens.clone(), String::new()))), + async |_| { panic!("polling must never issue a replacement") } + ) + .await + .is_err() + ); + } + assert!( + resolve_codex_token(None, ready(Err("Hub unavailable".into())), async |_| { + panic!("failed discovery must not issue a key") + }) + .await + .is_err() + ); + }); + } + + #[test] + fn simultaneous_first_requests_issue_one_codex_key() { + use core::sync::atomic::{AtomicUsize, Ordering}; + let issues = AtomicUsize::new(0); + tauri::async_runtime::block_on(async { + let request = || { + resolve_codex_token( + None, + async { + let tokens = if issues.load(Ordering::SeqCst) == 0 { + AgentTokens::default() + } else { + serde_json::from_value(serde_json::json!([ + {"name":"agent-codex","token":"issued","issued_at":1} + ])) + .expect("fixture") + }; + tokio::task::yield_now().await; + Ok((tokens, "refreshed-session".into())) + }, + async |session| { + assert_eq!(session, "refreshed-session"); + tokio::task::yield_now().await; + issues.fetch_add(1, Ordering::SeqCst); + Ok("issued".into()) + }, + ) + }; + let (codex, video) = tokio::join!(request(), request()); + assert_eq!(codex.as_deref(), Ok("issued")); + assert_eq!(video, codex); + assert_eq!(issues.load(Ordering::SeqCst), 1); + }); + } + #[test] fn wizard_operations_reuse_only_the_common_token() { let tokens = serde_json::from_value(serde_json::json!([ diff --git a/crates/wizard-gui/src/video.rs b/crates/wizard-gui/src/video.rs index 9c34fa0..d26a9c9 100644 --- a/crates/wizard-gui/src/video.rs +++ b/crates/wizard-gui/src/video.rs @@ -1,13 +1,10 @@ -use crate::{hub_auth, hub_tokens, settings}; -use anyhow::{Context as _, Result, anyhow, ensure}; -use serde::{Deserialize, Serialize}; +use crate::{hub_tokens, settings}; +use anyhow::{Context as _, Result}; +use core::future::Future; +use serde::Serialize; use tauri::Manager as _; use tokio::{net::TcpListener, sync::Mutex, task::JoinHandle}; -use wizard_core::{ - hub::{self, AgentToken, AgentTokens}, - video::{self, VideoHub, server::VideoAccess}, -}; -use wizard_settings::VideoConnection; +use wizard_core::video::{self, Job, VideoHub, VideoKey, server::VideoAccess}; #[derive(Default)] pub(crate) struct Runtime { @@ -17,142 +14,85 @@ pub(crate) struct Runtime { pub(crate) type State = Mutex; -pub(crate) fn selectable(token: &AgentToken, proxy: Option<&str>) -> bool { - !token.name.trim().is_empty() - && token.name != hub_tokens::PROXY_TOKEN_NAME - && Some(token.token.as_str()) != proxy -} - -#[derive(Serialize)] -pub(crate) struct VideoKey { - name: String, - id: String, -} +struct Access(tauri::AppHandle); -impl From<&AgentToken> for VideoKey { - fn from(t: &AgentToken) -> Self { - Self { - name: t.name.clone(), - id: video::fingerprint(&t.token), - } +impl VideoAccess for Access { + fn token<'a>( + &'a self, + job: Option<&'a Job>, + ) -> impl Future> + Send + 'a { + let key = hub_tokens::codex_token(&self.0, job); + async move { key.await.map_err(anyhow::Error::msg) } } } -#[derive(Deserialize)] -pub(crate) struct Selection { - hub_id: String, - name: String, - id: Option, -} - -fn choose<'a>( - list: &'a AgentTokens, - selection: &Selection, - proxy: Option<&str>, -) -> Result<&'a AgentToken> { - let mut matches = list.iter().filter(|t| { - selectable(t, proxy) - && t.name == selection.name - && selection - .id - .as_ref() - .is_none_or(|id| *id == video::fingerprint(&t.token)) - }); - let token = matches - .next() - .context("Выбранный ключ отсутствует в хабе")?; - ensure!( - matches.next().is_none(), - "В хабе несколько ключей с таким именем. Выберите конкретный ключ" - ); - Ok(token) -} - -async fn list(app: &tauri::AppHandle, hub_id: String) -> Result { - hub_tokens::with_session(app, |client, session| async move { - hub::list_agent_tokens(&client, &session, &hub_id).await - }) - .await - .map_err(|_| anyhow!("Не удалось получить ключи выбранного хаба")) -} - -struct Access(tauri::AppHandle); +impl Runtime { + fn running(&self) -> bool { + self.listener + .as_ref() + .is_some_and(|task| !task.is_finished()) + } -impl VideoAccess for Access { - fn connection(&self) -> Option { - let state = settings::get_state(&self.0).ok()?; - if state.hub_session.is_empty() { - None - } else { - state.video + async fn stop(&mut self) { + if let Some(task) = self.listener.take() { + task.abort(); + let _ = task.await; } } - async fn token(&self, connection: &VideoConnection) -> Result { - let generation = hub_tokens::generation(&self.0); - let profile = hub_auth::load_profile(&self.0) - .await - .map_err(|_| anyhow!("Войдите в CoreInfra Hub в Wizard"))?; - ensure!( - profile.personal_hub_id == connection.account_id, - "Войдите в исходный аккаунт CoreInfra Hub" - ); - let tokens = list(&self.0, connection.hub_id.clone()).await?; - let state = settings::get_state(&self.0)?; - ensure!( - !state.hub_session.is_empty() - && state.video.as_ref() == Some(connection) - && generation == hub_tokens::generation(&self.0), - "Подключение видео изменилось" - ); - tokens - .iter() - .find(|t| { - selectable(t, state.proxy_token.as_deref()) - && video::fingerprint(&t.token) == connection.key_id + async fn configure(&mut self, app: &tauri::AppHandle, enabled: bool) -> Result<()> { + if !enabled { + let previous = settings::get_state(app)?.video.local_bearer; + settings::update_state(app, |s| { + s.video.enabled = false; + s.video.local_bearer = None; }) - .map(|t| t.token.clone()) - .context("Исходный ключ видео отозван или недоступен") - } -} - -impl Runtime { - async fn start(&mut self, app: &tauri::AppHandle) -> Result<()> { - if self.listener.as_ref().is_some_and(|t| !t.is_finished()) { + .await?; + self.stop().await; + return tauri::async_runtime::spawn_blocking(move || { + video::install::sync(previous.as_deref(), None) + }) + .await + .context("Video cleanup task failed")?; + } + if self.running() { return Ok(()); } let hub = VideoHub::new()?; let listener = TcpListener::bind(video::ADDRESS) .await .context("Не удалось открыть локальный порт видео 33789")?; + let handle = app.clone(); let access = Access(app.clone()); - self.listener = Some(tokio::spawn(async move { - if let Err(e) = video::server::serve(listener, hub, access).await { - log::warn!("video MCP listener stopped: {e}"); - } - })); + self.listener = Some( + video::server::start(listener, hub, access, move |bearer| { + let guard = settings::lock_state()?; + let previous = settings::get_state(&handle)?.video.local_bearer; + video::install::sync(previous.as_deref(), Some(bearer))?; + if let Err(error) = settings::update_state_locked(&handle, &guard, |s| { + s.video.enabled = true; + s.video.local_bearer = Some(bearer.to_owned()); + }) { + video::install::sync(Some(bearer), previous.as_deref()) + .context("Не удалось восстановить регистрацию видео")?; + return Err(error); + } + Ok(()) + }) + .await?, + ); Ok(()) } - - async fn stop(&mut self) { - if let Some(task) = self.listener.take() { - task.abort(); - let _ = task.await; - } - } } pub(crate) async fn initialize(app: &tauri::AppHandle) { + let enabled = settings::get_state(app).map(|s| s.video.enabled); let state = app.state::(); let mut runtime = state.lock().await; - let result = async { - if let Some(current) = settings::get_state(app)?.video { - runtime.start(app).await?; - video::install::sync(Some(¤t), Some(¤t))?; - } - anyhow::Ok(()) - } - .await; + let result = match enabled { + Ok(enabled) => runtime.configure(app, enabled).await, + Err(error) => Err(error), + }; if let Err(error) = result { log::warn!("video setup: {error:#}"); runtime.error = Some(format!("{error:#}")); @@ -165,11 +105,7 @@ pub(crate) async fn shutdown(app: &tauri::AppHandle) { #[derive(Serialize)] pub(crate) struct Status { - hub_id: Option, - key_id: Option, - key_name: Option, - keys_hub_id: Option, - keys: Vec, + enabled: bool, running: bool, error: Option, } @@ -178,136 +114,27 @@ pub(crate) struct Status { pub(crate) async fn get_video_connection(app: tauri::AppHandle) -> Result { let state = app.state::(); let runtime = state.lock().await; - let current = settings::get_state(&app) - .map_err(|_| "Настройки недоступны")? - .video; - let (keys_hub_id, keys) = hub_tokens::video_keys(&app); Ok(Status { - hub_id: current.as_ref().map(|c| c.hub_id.clone()), - key_id: current.as_ref().map(|c| c.key_id.clone()), - key_name: current.map(|c| c.key_name), - keys_hub_id, - keys, - running: runtime.listener.as_ref().is_some_and(|t| !t.is_finished()), + enabled: settings::get_state(&app) + .map_err(|_| "Настройки недоступны")? + .video + .enabled, + running: runtime.running(), error: runtime.error.clone(), }) } -async fn configure( - app: &tauri::AppHandle, - runtime: &mut Runtime, - selection: Option, -) -> Result<()> { - let current = settings::get_state(app)?.video; - let Some(selection) = selection else { - settings::update_state(app, |s| s.video = None).await?; - runtime.stop().await; - return video::install::sync(current.as_ref(), None); - }; - let generation = hub_tokens::generation(app); - let profile = hub_auth::load_profile(app) - .await - .map_err(|_| anyhow!("Войдите в CoreInfra Hub"))?; - ensure!( - selection.hub_id == profile.active_hub_id(&settings::get_state(app)?.active_hub_id), - "Выбранный хаб изменился" - ); - let tokens = list(app, selection.hub_id.clone()).await?; - let snapshot = settings::get_state(app)?; - ensure!( - selection.hub_id == profile.active_hub_id(&snapshot.active_hub_id), - "Выбранный хаб изменился" - ); - let token = choose(&tokens, &selection, snapshot.proxy_token.as_deref())?; - let key_id = video::fingerprint(&token.token); - let next = VideoConnection { - local_bearer: current - .as_ref() - .filter(|c| { - c.account_id == profile.personal_hub_id - && c.hub_id == selection.hub_id - && c.key_id == key_id - }) - .map_or_else( - || format!("wizard-video-{}", uuid::Uuid::new_v4()), - |c| c.local_bearer.clone(), - ), - account_id: profile.personal_hub_id, - hub_id: selection.hub_id, - key_name: token.name.clone(), - key_id, - }; - runtime.start(app).await?; - let handle = app.clone(); - let result = tauri::async_runtime::spawn_blocking(move || { - let guard = settings::lock_state()?; - let now = settings::get_state(&handle)?; - ensure!( - !now.hub_session.is_empty() - && now.hub_session == snapshot.hub_session - && now.active_hub_id == snapshot.active_hub_id - && generation == hub_tokens::generation(&handle), - "Хаб или аккаунт изменился. Повторите подключение" - ); - video::install::sync(current.as_ref(), Some(&next))?; - if let Err(e) = - settings::update_state_locked(&handle, &guard, |s| s.video = Some(next.clone())) - { - video::install::sync(Some(&next), current.as_ref()) - .context("Не удалось восстановить регистрацию видео")?; - return Err(e); - } - Ok(()) - }) - .await - .context("Video configuration task failed")?; - if result.is_err() && settings::get_state(app)?.video.is_none() { - runtime.stop().await; - } - result -} - #[tauri::command] pub(crate) async fn set_video_connection( app: tauri::AppHandle, - selection: Option, + enabled: bool, ) -> Result<(), String> { let state = app.state::(); let mut runtime = state.lock().await; - let result = configure(&app, &mut runtime, selection) + let result = runtime + .configure(&app, enabled) .await .map_err(|e| format!("{e:#}")); runtime.error = result.as_ref().err().cloned(); result } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn names_are_hub_scoped_and_duplicates_require_the_selected_fingerprint() -> Result<()> { - let tokens: AgentTokens = serde_json::from_value(serde_json::json!([ - {"name":"studio", "token":"a", "issued_at":1}, {"name":"studio", "token":"b", "issued_at":1}, - {"name":"work", "token":"c", "issued_at":1}, {"name":"proxy-alias", "token":"proxy", "issued_at":1}, - {"name":"coreinfra-ai-wizard", "token":"reserved", "issued_at":1} - ]))?; - let mut choice = Selection { - hub_id: "h".into(), - name: "studio".into(), - id: None, - }; - assert!(choose(&tokens, &choice, None).is_err()); - choice.id = Some(video::fingerprint("b")); - assert_eq!(choose(&tokens, &choice, None)?.token, "b"); - choice.name = "work".into(); - assert!(choose(&tokens, &choice, None).is_err()); - choice.id = None; - assert_eq!(choose(&tokens, &choice, None)?.token, "c"); - for name in ["missing", "coreinfra-ai-wizard", "proxy-alias"] { - choice.name = name.into(); - assert!(choose(&tokens, &choice, Some("proxy")).is_err()); - } - Ok(()) - } -} diff --git a/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx b/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx index 5bf3df9..56df378 100644 --- a/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx +++ b/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx @@ -252,7 +252,7 @@ export function AgentPage({ onPrepare={prepare} /> )} - + {(codex.status === "not_found" || (codex.status === "found" && codex.data.cli.status === "not_found")) && ( )} diff --git a/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx b/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx index bf3950b..9297b3d 100644 --- a/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx +++ b/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx @@ -1,43 +1,30 @@ import { invoke } from "@tauri-apps/api/core"; import { useEffect, useState } from "react"; -import type { Hub } from "./AgentConfiguration"; import { Button } from "../components/catalyst/button"; -import { Field, Fieldset, Label, Legend } from "../components/catalyst/fieldset"; -import { Select } from "../components/catalyst/select"; +import { Fieldset, Legend } from "../components/catalyst/fieldset"; import { Text } from "../components/catalyst/text"; import { ErrorText } from "../components/ErrorText"; -import { refreshHubTokens } from "../settings/hub_tokens"; -type VideoKey = { name: string; id: string }; -type Status = { - hub_id: string | null; key_id: string | null; key_name: string | null; - keys_hub_id: string | null; keys: VideoKey[]; running: boolean; error: string | null; -}; +type Status = { enabled: boolean; running: boolean; error: string | null }; -export function VideoConnection({ hub, hubId, disabled }: { hub: Hub; hubId?: string | null; disabled: boolean }) { +export function VideoConnection({ signedIn, disabled }: { signedIn: boolean; disabled: boolean }) { const [status, setStatus] = useState(); - const [draft, setDraft] = useState(); const [busy, setBusy] = useState(false); const [error, setError] = useState(); const [revision, setRevision] = useState(0); - const loadedTokens = hub.status === "ready" ? hub.tokens : undefined; useEffect(() => { let active = true; invoke("get_video_connection").then(result => { if (active) setStatus(result); }).catch(cause => { if (active) setError(String(cause)); }); return () => { active = false; }; - }, [hubId, loadedTokens, revision]); + }, [revision]); - const keys = hub.signedIn && loadedTokens && hubId === status?.keys_hub_id ? status?.keys ?? [] : []; - const selected = keys.find(key => key.id === draft) ?? keys.find(key => key.id === status?.key_id) - ?? (keys.length === 1 ? keys[0] : undefined); - async function apply(key: VideoKey | null) { + async function apply(enabled: boolean) { setBusy(true); setError(undefined); try { - await invoke("set_video_connection", { selection: key && { hub_id: hubId, name: key.name, id: key.id } }); - setDraft(undefined); + await invoke("set_video_connection", { enabled }); } catch (cause) { setError(String(cause)); } finally { @@ -48,33 +35,16 @@ export function VideoConnection({ hub, hubId, disabled }: { hub: Hub; hubId?: st return
Видеомодели CoreInfra - {status?.key_name && Ключ «{status.key_name}» · {status.hub_id}} - {hub.signedIn ? - - - : Войдите в CoreInfra Hub, чтобы выбрать ключ для видео.} - {hub.signedIn && hub.status === "ready" && status?.keys_hub_id === hubId && !keys.length && - Создайте именованный API-ключ в выбранном хабе.} - {hub.signedIn && hub.status === "error" && {hub.error}} - {hub.signedIn && {status?.key_id - ? "Оставьте Wizard запущенным и попросите ChatGPT сгенерировать видео в новом чате." - : "Wizard установит скилл для ChatGPT / Codex. Для генерации оставьте Wizard запущенным."}} -
- - {(status?.key_id || status?.error) && } - {hub.signedIn && } -
- {status?.key_id && !status.running && !status.error && Сервер видео остановлен. Примените подключение повторно.} + {status?.running && {signedIn + ? "Попросите ChatGPT сгенерировать видео в новом чате. Wizard использует ключ agent-codex выбранного хаба." + : "Скилл установлен. Для генерации войдите в CoreInfra Hub в Wizard."}} + {status && !status.enabled && Видеомодели отключены.} {(error || status?.error) && {error || status?.error}} + {status &&
+ {!status.running && } + {status.enabled && } +
}
; } diff --git a/crates/wizard-settings/src/lib.rs b/crates/wizard-settings/src/lib.rs index 2ffd8dd..ab8b3b8 100644 --- a/crates/wizard-settings/src/lib.rs +++ b/crates/wizard-settings/src/lib.rs @@ -32,9 +32,7 @@ pub struct Settings { /// Random id that tells Wizard installations apart in Hub update metrics. Not a secret. #[serde(skip_serializing_if = "String::is_empty")] pub install_id: String, - /// One local video client grant. The upstream agent token is resolved in Hub. - #[serde(skip_serializing_if = "Option::is_none")] - pub video: Option, + pub video: VideoSettings, /// Application lifecycle preferences, independent of proxy demand. pub application: ApplicationSettings, /// Kept last: it serializes as a TOML table, written after plain keys. @@ -43,12 +41,22 @@ pub struct Settings { // No Debug: local_bearer authenticates paid video requests on loopback. #[derive(Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct VideoConnection { - pub account_id: String, - pub hub_id: String, - pub key_name: String, - pub key_id: String, - pub local_bearer: String, +#[serde(default)] +pub struct VideoSettings { + /// Install the managed skill and MCP registration when Wizard starts. + pub enabled: bool, + /// Current listener's local grant, also used when restoring Codex backups. + #[serde(skip_serializing_if = "Option::is_none")] + pub local_bearer: Option, +} + +impl Default for VideoSettings { + fn default() -> Self { + Self { + enabled: true, + local_bearer: None, + } + } } #[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] @@ -218,17 +226,19 @@ mod tests { #[test] fn video_connection_round_trips_in_existing_installations() -> Result<()> { let mut settings: Settings = toml_edit::de::from_str("install_id = 'abcdefgh27'")?; - assert!(settings.video.is_none()); - settings.video = Some(VideoConnection { - account_id: "account".into(), - hub_id: "team".into(), - key_name: "studio".into(), - key_id: "fingerprint".into(), - local_bearer: "local-grant".into(), - }); + assert!(settings.video.enabled && settings.video.local_bearer.is_none()); + settings = toml_edit::de::from_str( + "[video]\naccount_id='account'\nhub_id='team'\nkey_name='studio'\nkey_id='fingerprint'\nlocal_bearer='local-grant'\n", + )?; let saved = toml_edit::ser::to_string_pretty(&settings)?; + assert!(settings.video.enabled); + assert!(saved.contains("local-grant") && !saved.contains("studio")); let restored: Settings = toml_edit::de::from_str(&saved)?; assert!(settings == restored); + let opted_out: Settings = toml_edit::de::from_str("[video]\nenabled = false\n")?; + let saved = toml_edit::ser::to_string_pretty(&opted_out)?; + let restored: Settings = toml_edit::de::from_str(&saved)?; + assert!(!restored.video.enabled && restored.video.local_bearer.is_none()); Ok(()) } diff --git a/skills/coreinfra-video/SKILL.md b/skills/coreinfra-video/SKILL.md index 0211717..1d49f83 100644 --- a/skills/coreinfra-video/SKILL.md +++ b/skills/coreinfra-video/SKILL.md @@ -3,11 +3,11 @@ name: coreinfra-video description: Generate video from a prompt or image through CoreInfra Hub, list video models and prices, and retrieve existing video jobs. Use for requests such as «сгенерируй видео», «оживи фото» or «забери готовый ролик» in a Wizard-connected workspace. --- -Use the tools on the `coreinfra_video` MCP server. Wizard supplies the credential for the video key selected in its ChatGPT / Codex settings. If the server is offline, open Wizard and check the video connection. +Use the tools on the `coreinfra_video` MCP server. Wizard installs it automatically and supplies the default `agent-codex` key from its selected CoreInfra Hub. If the server is offline, open Wizard and check the video connection in ChatGPT / Codex settings. - Use `video_models` for current model IDs, providers and public RUB-per-second rates. A request for models, a quote or a prompt only calls for a read. - To generate a requested video, call `video_create` once with `model`, `prompt` and any model-specific JSON in `fields`. Keep the entire returned `job` object with the conversation or the user's script. If the submission outcome is uncertain, report it. A second submission can incur another charge. -- Call `video_result` with that job. Poll pending or in-progress jobs with a delay. On completion, present the returned absolute `path` as a video. To check status without saving a file, pass `download: false`. For another OpenRouter output, pass its zero-based `index`. After interruption, resume with the same job. If the connection changed, reselect its original Hub and key in Wizard. +- Call `video_result` with that job. Poll pending or in-progress jobs with a delay. On completion, present the returned absolute `path` as a video. To check status without saving a file, pass `download: false`. For another OpenRouter output, pass its zero-based `index`. After interruption, resume with the same job. If the active Hub changed, select the job's original Hub in Wizard. - The MCP object `fields` is merged into the **top level** of the upstream JSON body. Preserve the requested types and provider nesting. For example, OpenRouter accepts `{"duration":5,"provider":{"options":{"":{"