| 1 | use crate::rpc::{ |
| 2 | ThreadLoadedListParams, ThreadLoadedListResponse, ThreadSetNameParams, ThreadSetNameResponse, |
| 3 | }; |
| 4 | use crate::runtime::*; |
| 5 | use schemars::generate::SchemaSettings; |
| 6 | use serde_json::{Value, json}; |
| 7 | use std::collections::BTreeMap; |
| 8 | |
| 9 | fn output_references(value: &mut Value) { |
| 10 | match value { |
| 11 | Value::Object(object) => { |
| 12 | if object.get("$ref").and_then(Value::as_str) == Some("#/$defs/DynamicToolSpec") { |
| 13 | object.insert( |
| 14 | "$ref".into(), |
| 15 | Value::String("#/$defs/DynamicToolSpecOutput".into()), |
| 16 | ); |
| 17 | } |
| 18 | for child in object.values_mut() { |
| 19 | output_references(child); |
| 20 | } |
| 21 | } |
| 22 | Value::Array(values) => { |
| 23 | for child in values { |
| 24 | output_references(child); |
| 25 | } |
| 26 | } |
| 27 | _ => {} |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | pub fn runtime_schema() -> Value { |
| 32 | let mut input = SchemaSettings::draft2020_12() |
| 33 | .for_deserialize() |
| 34 | .into_generator(); |
| 35 | let _ = input.subschema_for::<RuntimeImageInput>(); |
| 36 | let _ = input.subschema_for::<ThreadLoadedListParams>(); |
| 37 | let _ = input.subschema_for::<ThreadSetNameParams>(); |
| 38 | let _ = input.subschema_for::<DynamicToolSpec>(); |
| 39 | let _ = input.subschema_for::<DynamicToolItemStatus>(); |
| 40 | let _ = input.subschema_for::<DynamicToolCallResult>(); |
| 41 | let _ = input.subschema_for::<DynamicToolCallContent>(); |
| 42 | let _ = input.subschema_for::<TurnEnvironmentParams>(); |
| 43 | let mut output = SchemaSettings::draft2020_12() |
| 44 | .for_serialize() |
| 45 | .into_generator(); |
| 46 | let _ = output.subschema_for::<RuntimeEventEnvelope>(); |
| 47 | let _ = output.subschema_for::<ThreadLoadedListResponse>(); |
| 48 | let _ = output.subschema_for::<ThreadSetNameResponse>(); |
| 49 | let _ = output.subschema_for::<RuntimeCapabilities>(); |
| 50 | let _ = output.subschema_for::<ApprovalKind>(); |
| 51 | let _ = output.subschema_for::<FleetProfileDocument>(); |
| 52 | let _ = output.subschema_for::<RuntimeExperimentalCapabilities>(); |
| 53 | let _ = output.subschema_for::<DynamicToolCallParams>(); |
| 54 | let _ = output.subschema_for::<DynamicToolSpec>(); |
| 55 | let _ = output.subschema_for::<TurnEnvironmentParams>(); |
| 56 | let _ = output.subschema_for::<DynamicToolItemStatus>(); |
| 57 | let mut definitions = input.take_definitions(false); |
| 58 | for (name, mut schema) in output.take_definitions(false) { |
| 59 | output_references(&mut schema); |
| 60 | let name = if name == "DynamicToolSpec" { |
| 61 | "DynamicToolSpecOutput".to_string() |
| 62 | } else { |
| 63 | name |
| 64 | }; |
| 65 | if let Some(previous) = definitions.get(&name) { |
| 66 | assert_eq!(previous, &schema, "input/output schema conflict for {name}"); |
| 67 | } else { |
| 68 | definitions.insert(name, schema); |
| 69 | } |
| 70 | } |
| 71 | json!({"$schema": "https://json-schema.org/draft/2020-12/schema", "$defs": definitions}) |
| 72 | } |
| 73 | |
| 74 | pub fn runtime_typescript(schema: &Value) -> String { |
| 75 | let definitions: BTreeMap<String, crate::schema::Def> = schema["$defs"] |
| 76 | .as_object() |
| 77 | .expect("Runtime schema definitions") |
| 78 | .iter() |
| 79 | .map(|(name, schema)| { |
| 80 | ( |
| 81 | name.clone(), |
| 82 | crate::schema::parse_def(name, schema, crate::schema::SchemaMode::Runtime), |
| 83 | ) |
| 84 | }) |
| 85 | .collect(); |
| 86 | crate::schema::typescript_defs(&definitions, crate::schema::SchemaMode::Runtime) |
| 87 | } |
| 88 |