diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9aa5d3d..e81e8a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,6 +54,21 @@ jobs: - name: Run CI checks run: cargo x ci + - name: Set up Python for the video skill E2E + uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0 + with: + python-version: '3.13' + + - name: Install Codex for the video skill E2E + run: npm install --prefix "${{ runner.temp }}/video-codex" --no-save --no-package-lock --no-audit --no-fund "@openai/codex@0.160.0" + + - name: Verify the installed skill can list live video models + timeout-minutes: 5 + env: + WIZARD_VIDEO_TEST_CODEX: ${{ runner.temp }}/video-codex/node_modules/@openai/codex/bin/codex.js + WIZARD_VIDEO_TEST_PYTHON: python + run: cargo test --locked -p wizard-core --lib video::tests::codex_skill_lists_public_video_models -- --exact --ignored --nocapture + frontend: name: Frontend runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index 29b475a..8388418 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", @@ -5659,7 +5881,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..bf0a357 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,39 @@ An application for configuring agents to use CoreInfra. +## Video in ChatGPT Work and Codex + +Start Wizard, sign in to CoreInfra Hub and select the Hub to charge. Keep Wizard running and ask for a video in a new ChatGPT Work or Codex chat, or invoke `$coreinfra-video`. Wizard automatically installs the skill and registers its MCP server at startup. + +The bundled skill and the `coreinfra_video` MCP registration live in the profile selected by `CODEX_HOME`. Wizard listens at `http://127.0.0.1:33789/mcp` and publishes a new local bearer on each start. **ChatGPT / Codex → Видеомодели CoreInfra → Отключить** revokes local access and removes the managed registration and unmodified skill files. The opt-out persists across restarts. + +`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. + +For new jobs Wizard uses the oldest active key named `agent-codex` in the selected Hub, the same default that Wizard provisions for Codex. If it is missing, Wizard issues one as part of the generation request. Only Wizard adds the Hub key to the outgoing `Authorization` header. A job records its Hub ID, key fingerprint, provider, model and upstream ID so Wizard can poll with the original key. Wizard keeps no job table or MCP session. A Python MCP client with permission to reach loopback can use the URL and current local `http_headers` from the registration. + +### 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. + +CI also runs a catalog E2E on Linux, macOS and Windows. Wizard installs the bundled skill and MCP registration into a fresh profile. A native Codex turn uses the installed skill to call `video_models` and receives model IDs from the public Hub. A local Responses fixture requests that single read-only tool call. To run it locally, set `WIZARD_VIDEO_TEST_CODEX` to a Codex executable or its npm `bin/codex.js` launcher and run `cargo test -p wizard-core codex_skill_lists_public_video_models -- --ignored --nocapture` with Python 3 available and port 33789 free. + ## 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..cbe5691 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)?; + restore_video_registration(&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,31 @@ pub fn restore_backup(id: u32, settings: &Settings) -> Result<()> { Ok(()) } +fn restore_video_registration( + 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 bearer = settings + .video + .local_bearer + .as_deref() + .filter(|_| settings.video.enabled && change.path() == active); + crate::video::install::update_config(&mut change.after, None, bearer)?; + } + } + 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<()> { @@ -620,6 +648,59 @@ mod tests { use super::*; use serde_json::json; + #[test] + fn hub_key_filter_follows_subscription_routing() -> 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)?; + let key_filter = (mode == RoutingMode::Sub).then_some("exclude"); + for (key, expected) in [("KEEP", Some("include")), ("COREINFRA_API_KEY", key_filter)] { + assert_eq!( + config_files::toml::get_string( + &doc, + &["shell_environment_policy", "filters", key] + ), + expected + ); + } + } + Ok(()) + } + + #[test] + fn restoring_video_registration_preserves_shell_policy() -> Result<()> { + 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())?; + let backed_up = + "model_provider='coreinfra'\n[shell_environment_policy]\nexclude=['PRIVATE_SETTING']\n"; + backup.after = FileSnapshot::new(backed_up.into(), 0o644); + let settings = Settings { + video: wizard_settings::VideoSettings { + local_bearer: Some("fixture-local-grant".into()), + ..Default::default() + }, + ..Settings::default() + }; + let mut changes = vec![backup]; + restore_video_registration(&mut changes, &active, &settings)?; + FileChange::apply_all(&changes)?; + assert_eq!(std::fs::read_to_string(old)?, backed_up); + let active_config = std::fs::read_to_string(active)?; + assert!(active_config.contains("fixture-local-grant")); + assert!(!active_config.contains("shell_environment_policy")); + 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..32db4a9 --- /dev/null +++ b/crates/wizard-core/src/video/install.rs @@ -0,0 +1,154 @@ +//! 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; + +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<&str>, next: Option<&str>) -> Result<()> { + let _guard = PreparedAgent::Codex.lock()?; + sync_at(&codex::config_path()?, previous, next) +} + +pub(super) fn sync_at(path: &Path, previous: Option<&str>, next: Option<&str>) -> 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<&str>, + next: Option<&str>, +) -> Result<()> { + toml::update(snapshot, |doc| registration(doc, previous, next))?; + #[cfg(unix)] + if next.is_some() + && let FileSnapshot::Present { mode, .. } = snapshot + { + *mode &= 0o600; + } + Ok(()) +} + +fn registration(doc: &mut DocumentMut, previous: Option<&str>, next: Option<&str>) -> 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 == Some(bearer) + || next == Some(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(bearer) = 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 {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..f8873e1 --- /dev/null +++ b/crates/wizard-core/src/video/mod.rs @@ -0,0 +1,417 @@ +//! 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::time::Duration; +use reqwest::{Client, RequestBuilder, Response, Url}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value, json}; +use server::VideoAccess; +use sha2::{Digest as _, Sha256}; +use std::path::{Path, PathBuf}; +use tokio::io::AsyncWriteExt as _; + +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)] +pub struct Job { + provider: Provider, + model: String, + id: String, + pub hub_id: String, + pub key_id: String, +} + +/// Resolved by Wizard for one operation. The token is never serialized or logged. +pub struct VideoKey { + pub hub_id: String, + pub token: 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, access: &impl VideoAccess) -> Result { + let (model_id, mut body) = input.into_body()?; + // Capture the caller's Hub before catalog I/O. Issue a key only for a known model. + let credentials = access.token(None); + 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 key = credentials.await?; + let req = self + .client + .post(url) + .bearer_auth(&key.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: key.hub_id, + key_id: fingerprint(&key.token), + }; + Ok(json!({"job":job, "status":response["status"].as_str().unwrap_or("pending")})) + } + + async fn result(&self, input: GetResult, access: &impl VideoAccess) -> Result { + let job = &input.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 key = access.token(Some(job)).await?; + ensure!( + job.hub_id == key.hub_id && job.key_id == fingerprint(&key.token), + "This video job requires its original Hub and API key" + ); + let token = key.token; + 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..18c2a67 --- /dev/null +++ b/crates/wizard-core/src/video/server.rs @@ -0,0 +1,210 @@ +use super::{Create, GetResult, Job, VideoHub, VideoKey}; +use alloc::sync::Arc; +use anyhow::{Context as _, Result, bail, ensure}; +use core::{convert::Infallible, future::Future, net::SocketAddr}; +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::{JoinHandle, JoinSet}, +}; + +/// Wizard resolves its Codex key for each operation. Polling must never issue a key. +pub trait VideoAccess: Send + Sync + 'static { + fn token<'a>( + &'a self, + job: Option<&'a Job>, + ) -> impl Future> + Send + 'a; +} + +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) -> 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, self.access.as_ref()).await + } + "video_result" => { + let input: GetResult = + serde_json::from_value(args).context("Invalid video_result arguments")?; + self.hub.result(input, self.access.as_ref()).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, + _: RequestContext, + ) -> Result { + let args = Value::Object(request.arguments.unwrap_or_default()); + let response = match self.call(&request.name, args).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>, + local_bearer: 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); + 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); + Ok(service.handle(request).await) +} + +/// 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 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(&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 bearer = Arc::clone(&local_bearer); + clients.spawn(async move { + let handler = service_fn(move |req| handle(req, service.clone(), Arc::clone(&bearer))); + 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..237a6ab --- /dev/null +++ b/crates/wizard-core/src/video/tests.rs @@ -0,0 +1,741 @@ +use super::*; +use crate::{config_files::toml, video::server::VideoAccess}; +use alloc::sync::Arc; +use core::{ + convert::Infallible, + future::Future, + sync::atomic::{AtomicUsize, Ordering}, +}; +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 LOCAL: &str = "fixture-local-grant"; +const MP4: &[u8] = include_bytes!("../../tests/fixtures/blue.mp4"); + +struct Access(&'static str); +impl VideoAccess for Access { + 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(), + })) + } +} + +#[derive(Default, Clone)] +struct SignedOut { + lookups: Arc, +} + +impl VideoAccess for SignedOut { + fn token<'a>( + &'a self, + _: Option<&'a Job>, + ) -> impl Future> + Send + 'a { + core::future::poll_fn(|_| { + self.lookups.fetch_add(1, Ordering::SeqCst); + core::task::Poll::Ready(Err(anyhow!("Sign in to Wizard"))) + }) + } +} + +struct Seen { + method: String, + path: String, + auth: Option, + body: Value, +} + +struct Fixture { + hub: VideoHub, + dir: tempfile::TempDir, + requests: Arc>>, + 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}}, + 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, 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 = 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 { + 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, server: &RunningServer, tool: &str, args: Value) -> Result { + let response = self + .request( + &server.url, + "tools/call", + &json!({"name":tool,"arguments":args}), + ) + .bearer_auth(&server.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 first = fixture.start(Access(KEY)).await?; + let catalog = fixture.call(&first, "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( + &first, + "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( + &first, + "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(&first, "video_result", json!({"job":first_job})) + .await?; + assert_eq!(pending["structuredContent"]["status"], "in_progress"); + 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(&second, "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(&second, "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( + &second, + "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 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"), + ("Host", "hub.coreinfra.ai"), + ] { + assert!( + fixture + .request(&server.url, "tools/list", &json!({})) + .bearer_auth(&server.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(&server, "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( + &server, + "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(&server, "video_result", json!({"job":invalid})) + .await?["isError"], + true + ); + } + job["id"] = json!("bad-link"); + assert_eq!( + fixture + .call(&server, "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"); + let original = concat!( + "# keep this\n[shell_environment_policy.filters]\n", + "coreinfra_api_key = 'exclude'\n\n", + "[mcp_servers.other]\nurl = 'https://elsewhere.example'\n", + ); + fs::write(&config, original)?; + install::sync_at(&config, None, Some(LOCAL))?; + 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.starts_with(original)); + 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(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(LOCAL), Some(LOCAL))?; + assert_eq!(fs::read_to_string(&source)?, bundled); + fs::write(&source, "user changes")?; + 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); + 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(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(LOCAL))?; + assert!(!fs::read_to_string(&config)?.contains("shell_environment_policy")); + fs::write( + fresh + .path() + .join("skills/coreinfra-video/.wizard-managed.json"), + "invalid", + )?; + 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 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 + .call(&server, "video_result", json!({"job":original})) + .await?; + assert_eq!(response["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] +async fn installation_and_catalog_work_without_hub_credentials() -> Result<()> { + let fixture = Fixture::new().await?; + let access = SignedOut::default(); + let server = fixture.start(access.clone()).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!(access.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!(access.lookups.load(Ordering::SeqCst), 1); + assert!( + fixture + .requests + .lock() + .expect("requests") + .iter() + .all(|r| r.method == "GET" && r.auth.is_none()) + ); + Ok(()) +} + +#[tokio::test] +#[ignore = "requires WIZARD_VIDEO_TEST_CODEX, Python and access to the public Hub catalog"] +async fn codex_skill_lists_public_video_models() -> Result<()> { + let _ = crate::hub::install_crypto_provider(); + let dir = tempfile::tempdir()?; + let profile = dir.path().join("codex"); + let config = profile.join("config.toml"); + let access = SignedOut::default(); + let listener = TcpListener::bind(ADDRESS) + .await + .with_context(|| format!("bind the Wizard video MCP port at {ADDRESS}"))?; + let server = server::start(listener, VideoHub::new()?, access.clone(), move |bearer| { + install::sync_at(&config, None, Some(bearer)) + }) + .await?; + let python = std::env::var_os("WIZARD_VIDEO_TEST_PYTHON") + .unwrap_or_else(|| if cfg!(windows) { "python" } else { "python3" }.into()); + let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/video_catalog_e2e.py"); + let mut client = tokio::process::Command::new(python) + .args(["-E", "-B"]) + .arg(script) + .arg(&profile) + .current_dir(dir.path()) + .kill_on_drop(true) + .spawn()?; + let status = tokio::time::timeout(Duration::from_secs(120), client.wait()).await??; + server.abort(); + let _ = server.await; + ensure!( + status.success(), + "Codex skill/catalog E2E failed ({status})" + ); + assert_eq!( + access.lookups.load(Ordering::SeqCst), + 0, + "catalog requested a Hub key" + ); + 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 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', 'COREINFRA_API_KEY']\n", + )?; + let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/video_clients.py"); + let mut clients = tokio::process::Command::new(std::env::var("WIZARD_VIDEO_TEST_PYTHON")?) + .arg(script) + .arg(&profile) + .current_dir(fixture.dir.path()) + .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() + .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 0000000..7b96876 Binary files /dev/null and b/crates/wizard-core/tests/fixtures/blue.mp4 differ diff --git a/crates/wizard-core/tests/video_catalog_e2e.py b/crates/wizard-core/tests/video_catalog_e2e.py new file mode 100644 index 0000000..350f6c8 --- /dev/null +++ b/crates/wizard-core/tests/video_catalog_e2e.py @@ -0,0 +1,106 @@ +"""Run one Codex turn against Wizard's installed skill and the public Hub catalog.""" +from http.server import BaseHTTPRequestHandler, HTTPServer +import json +from pathlib import Path +import sys +import threading +import time + +from video_clients import codex_session, receive + + +class CatalogTurn(BaseHTTPRequestHandler): + """A deterministic Responses provider. Codex executes the tool and sends its output back.""" + + def do_POST(self): + if self.path != "/v1/responses": + self.send_error(404) + return + request = json.loads(self.rfile.read(int(self.headers["Content-Length"]))) + output = next((item for item in request["input"] if item.get("type") == "function_call_output" + and item.get("call_id") == "call_catalog"), None) + offered = any(tool.get("name") == "mcp__coreinfra_video" and + any(f["name"] == "video_models" for f in tool.get("tools", [])) + for tool in request["tools"]) + if output is None and offered: + item = {"type": "function_call", "id": "fc_catalog", "call_id": "call_catalog", + "namespace": "mcp__coreinfra_video", "name": "video_models", "arguments": "{}"} + else: + # Echo only what Codex delivered to the model on its next inference request. + text = json.dumps(output["output"], ensure_ascii=False) if output else "video_models unavailable" + item = {"type": "message", "id": "msg_catalog", "role": "assistant", + "content": [{"type": "output_text", "text": text}]} + response = {"id": "resp_" + item["id"], "status": "completed", "output": [item]} + events = [ + {"type": "response.created", "response": {"id": response["id"], "status": "in_progress"}}, + {"type": "response.output_item.done", "output_index": 0, "item": item}, + {"type": "response.completed", "response": response}, + ] + data = "".join("data: " + json.dumps(event) + "\n\n" for event in events).encode() + self.send_response(200) + self.send_header("Content-Type", "text/event-stream") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + def log_message(self, *_): + pass + + +def run(profile, model): + with codex_session(profile, f"http://127.0.0.1:{model.server_port}/v1") as (call, events): + cwd = str(profile.parent) + thread = call("thread/start", {"cwd": cwd, "ephemeral": True, "sandbox": "read-only", + "approvalPolicy": "never"})["thread"]["id"] + installed = profile / "skills" / "coreinfra-video" / "SKILL.md" + groups = call("skills/list", {"cwds": [cwd]})["data"] + skills = [s for group in groups for s in group["skills"] + if s["name"] == "coreinfra-video" and s["enabled"] and Path(s["path"]).samefile(installed)] + assert len(skills) == 1, "Codex must discover the enabled skill in the isolated profile" + skill = skills[0] + dependencies = (skill.get("dependencies") or {}).get("tools", []) + assert any(d["type"] == "mcp" and d["value"] == "coreinfra_video" for d in dependencies), ( + "Codex did not read the bundled MCP dependency from agents/openai.yaml" + ) + turn = call("turn/start", {"threadId": thread, "input": [ + {"type": "skill", "name": skill["name"], "path": skill["path"]}, + {"type": "text", "text": "List the video models available in CoreInfra Hub."}, + ]})["turn"]["id"] + + models = [] + answer = "" + deadline = time.monotonic() + 85 + while time.monotonic() < deadline: + message = receive(events, deadline - time.monotonic(), "Codex catalog turn") + params = message.get("params", {}) + if params.get("threadId") != thread: + continue + if message.get("method") == "item/completed" and params.get("turnId") == turn: + item = params["item"] + if item["type"] == "mcpToolCall": + assert (item["server"], item["tool"]) == ("coreinfra_video", "video_models"), item + assert item["status"] == "completed" and not item.get("error"), item + models = item["result"]["structuredContent"]["models"] + elif item["type"] == "agentMessage": + answer = item["text"] + if message.get("method") == "turn/completed" and params["turn"]["id"] == turn: + assert params["turn"]["status"] == "completed", params["turn"].get("error") + break + else: + raise TimeoutError("Codex catalog turn") + + assert isinstance(models, list) and models, "Codex received an empty video catalog" + assert all(isinstance(m.get("id"), str) and m["id"].strip() for m in models), "Missing video model ID" + assert models[0]["id"] in answer, "The final agent reply did not contain a returned model ID" + print(f"Codex discovered {skill['name']} and received {len(models)} video models.") + + +if __name__ == "__main__": + with HTTPServer(("127.0.0.1", 0), CatalogTurn) as provider: + worker = threading.Thread(target=provider.serve_forever, daemon=True) + worker.start() + try: + run(Path(sys.argv[1]).resolve(), provider) + finally: + provider.shutdown() + worker.join() diff --git a/crates/wizard-core/tests/video_clients.py b/crates/wizard-core/tests/video_clients.py new file mode 100644 index 0000000..72fc61a --- /dev/null +++ b/crates/wizard-core/tests/video_clients.py @@ -0,0 +1,138 @@ +"""Codex app-server test client and the opt-in Python MCP compatibility test.""" +import asyncio +from contextlib import contextmanager +import json +import os +from pathlib import Path +import queue +import subprocess +import sys +import threading + + +def receive(channel, timeout, operation): + try: + message = channel.get(timeout=max(0, timeout)) + except queue.Empty: + raise TimeoutError(f"Timed out waiting for {operation}") from None + if message is None: + raise RuntimeError(f"Codex exited during {operation}") + return message + + +@contextmanager +def codex_session(profile, model_url="http://127.0.0.1:1/v1", extra_env=None): + config = profile / "config.toml" + with config.open("a", encoding="utf-8") as file: + file.write('\n[model_providers.fixture]\nname="Offline fixture"\n' + f'base_url="{model_url}"\nwire_api="responses"\n') + system_vars = ("PATH", "HOME", "TMPDIR", "LANG", "USER", "SYSTEMROOT", "WINDIR", + "SYSTEMDRIVE", "USERPROFILE", "LOCALAPPDATA", "APPDATA", "PROGRAMDATA", + "COMSPEC", "PATHEXT", "TEMP", "TMP") + env = {k: os.environ[k] for k in system_vars 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") + env.update(extra_env or {}) + binary = os.environ["WIZARD_VIDEO_TEST_CODEX"] + launcher = ["node", binary] if binary.endswith(".js") else [binary] + server = subprocess.Popen([*launcher, "app-server", "--listen", "stdio://", + "-c", 'model_provider="fixture"', "-c", 'model="offline"'], + cwd=profile.parent, env=env, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=sys.stderr, encoding="utf-8", bufsize=1) + replies = queue.Queue() + events = queue.Queue() + def read(): + try: + for line in server.stdout: + message = json.loads(line) + (events if "method" in message else replies).put(message) + finally: + replies.put(None) + events.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}) + msg = receive(replies, 30, method) + assert msg["id"] == seq and "error" not in msg, (method, msg.get("error")) + return msg["result"] + try: + call("initialize", {"clientInfo":{"name":"wizard_fixture","version":"1"}, + "capabilities":{"experimentalApi":True}}) + send({"jsonrpc":"2.0","method":"initialized"}) + yield call, events + finally: + server.stdin.close() + try: + server.wait(timeout=4) + except subprocess.TimeoutExpired: + server.terminate() + server.wait(timeout=4) + + +def native_client(profile): + shell_env = {"WIZARD_VIDEO_TEST_FILTER": "fixture", "COREINFRA_API_KEY": "unrelated-process-fixture-key"} + with codex_session(profile, extra_env=shell_env) as (call, _): + 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(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", + "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"] + + +async def python_client(profile, existing_job): + 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() + 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 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/src/agents.rs b/crates/wizard-gui/src/agents.rs index e365ad0..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? } @@ -394,9 +397,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..7392367 100644 --- a/crates/wizard-gui/src/hub_tokens.rs +++ b/crates/wizard-gui/src/hub_tokens.rs @@ -9,9 +9,13 @@ 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}, +}; -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 +28,13 @@ pub(crate) struct HubTokens { generation: u64, } +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`. @@ -258,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(), @@ -303,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. @@ -424,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/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; + +struct Access(tauri::AppHandle); + +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) } + } +} + +impl Runtime { + fn running(&self) -> bool { + self.listener + .as_ref() + .is_some_and(|task| !task.is_finished()) + } + + async fn stop(&mut self) { + if let Some(task) = self.listener.take() { + task.abort(); + let _ = task.await; + } + } + + 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; + }) + .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( + 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(()) + } +} + +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 = 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:#}")); + } +} + +pub(crate) async fn shutdown(app: &tauri::AppHandle) { + app.state::().lock().await.stop().await; +} + +#[derive(Serialize)] +pub(crate) struct Status { + enabled: bool, + 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; + Ok(Status { + enabled: settings::get_state(&app) + .map_err(|_| "Настройки недоступны")? + .video + .enabled, + running: runtime.running(), + error: runtime.error.clone(), + }) +} + +#[tauri::command] +pub(crate) async fn set_video_connection( + app: tauri::AppHandle, + enabled: bool, +) -> Result<(), String> { + let state = app.state::(); + let mut runtime = state.lock().await; + let result = runtime + .configure(&app, enabled) + .await + .map_err(|e| format!("{e:#}")); + runtime.error = result.as_ref().err().cloned(); + result +} diff --git a/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx b/crates/wizard-gui/wizard-ui/src/agents/AgentPage.tsx index 4c5aa6e..56df378 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..9297b3d --- /dev/null +++ b/crates/wizard-gui/wizard-ui/src/agents/VideoConnection.tsx @@ -0,0 +1,50 @@ +import { invoke } from "@tauri-apps/api/core"; +import { useEffect, useState } from "react"; +import { Button } from "../components/catalyst/button"; +import { Fieldset, Legend } from "../components/catalyst/fieldset"; +import { Text } from "../components/catalyst/text"; +import { ErrorText } from "../components/ErrorText"; + +type Status = { enabled: boolean; running: boolean; error: string | null }; + +export function VideoConnection({ signedIn, disabled }: { signedIn: boolean; disabled: boolean }) { + const [status, setStatus] = useState(); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(); + const [revision, setRevision] = useState(0); + useEffect(() => { + let active = true; + invoke("get_video_connection").then(result => { + if (active) setStatus(result); + }).catch(cause => { if (active) setError(String(cause)); }); + return () => { active = false; }; + }, [revision]); + + async function apply(enabled: boolean) { + setBusy(true); + setError(undefined); + try { + await invoke("set_video_connection", { enabled }); + } catch (cause) { + setError(String(cause)); + } finally { + setRevision(value => value + 1); + setBusy(false); + } + } + + return
+ Видеомодели CoreInfra + {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-gui/wizard-ui/src/preview/mocks.ts b/crates/wizard-gui/wizard-ui/src/preview/mocks.ts index f26e521..3c75c7c 100644 --- a/crates/wizard-gui/wizard-ui/src/preview/mocks.ts +++ b/crates/wizard-gui/wizard-ui/src/preview/mocks.ts @@ -206,6 +206,7 @@ export function createMockEnvironment(scenario: Scenario = "update", options: Mo balance_rub: ["balance_empty", "balance_guest", "onboarding_merge"].includes(scenario) ? 0 : 2500, }); const proxy = new Snapshots({ enabled: !onboarding }); + let videoEnabled = true; const update = new Snapshots( scenario === "wizard_update" ? { status: "available", version: "1.0.6" } : scenario === "checking" || scenario === "startup" @@ -486,6 +487,11 @@ export function createMockEnvironment(scenario: Scenario = "update", options: Mo return configured.read(revision); case "get_proxy_state": return proxy.read(revision); + case "get_video_connection": + return { enabled: videoEnabled, running: videoEnabled, error: null }; + case "set_video_connection": + videoEnabled = args.enabled === true; + return; case "get_update_state": return update.read(revision); case "get_application_settings": diff --git a/crates/wizard-gui/wizard-ui/src/test/flows.test.tsx b/crates/wizard-gui/wizard-ui/src/test/flows.test.tsx index 4eacc60..30a8c8d 100644 --- a/crates/wizard-gui/wizard-ui/src/test/flows.test.tsx +++ b/crates/wizard-gui/wizard-ui/src/test/flows.test.tsx @@ -43,6 +43,17 @@ afterEach(() => { clearMocks(); }); +it("shows the automatic video connection on the Codex page and lets the user reconnect it", async () => { + const user = userEvent.setup(); + launch("current", "/agents/codex"); + const video = within(await screen.findByRole("group", { name: "Видеомодели CoreInfra" })); + expect(await video.findByText(/ключ agent-codex/)).toBeVisible(); + expect(environment.calls).not.toContain("set_video_connection"); + await user.click(video.getByRole("button", { name: "Отключить" })); + await user.click(await video.findByRole("button", { name: "Включить видеомодели" })); + expect(await video.findByRole("button", { name: "Отключить" })).toBeVisible(); +}); + describe("shared installation flow", () => { it("keeps a download through Back and Escape, then cancels and starts again from zero", async () => { const user = userEvent.setup(); diff --git a/crates/wizard-settings/src/lib.rs b/crates/wizard-settings/src/lib.rs index e79a4a3..ab8b3b8 100644 --- a/crates/wizard-settings/src/lib.rs +++ b/crates/wizard-settings/src/lib.rs @@ -32,12 +32,33 @@ 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, + 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. pub last_paths: LastPaths, } +// No Debug: local_bearer authenticates paid video requests on loopback. +#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)] +#[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)] #[serde(default)] pub struct ApplicationSettings { @@ -202,6 +223,25 @@ 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.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(()) + } + #[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..1d49f83 --- /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 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 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":{"":{"