Base: codex-main originale (prima del fork aKode)

This commit is contained in:
Andrea
2026-11-09 10:37:42 +01:00
commit 7874033c81
10166 changed files with 2728937 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
use codex_utils_absolute_path::AbsolutePathBuf;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use ts_rs::TS;
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
#[serde(tag = "type", rename_all = "camelCase")]
#[ts(tag = "type", rename_all = "camelCase")]
pub enum ImageGenerationFailure {
UsageLimitExceeded {
#[serde(rename = "limitId")]
#[ts(rename = "limitId")]
limit_id: String,
#[serde(rename = "resetsAt")]
#[ts(rename = "resetsAt")]
#[ts(type = "number | null")]
resets_at: Option<i64>,
},
}
/// A local image file successfully read for this exact image-generation request.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct ImageGenerationInputImage {
pub path: AbsolutePathBuf,
/// Lowercase SHA-256 of the file bytes read before image normalization.
pub sha256: String,
}
// Standalone image-generation item owned by the image extension. This is also
// the field-level representation exposed by app-server; core and rollout
// persistence only carry it inside an ExtensionItem envelope.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct ImageGenerationItem {
pub id: String,
pub status: String,
pub revised_prompt: Option<String>,
#[serde(flatten)]
pub image: GeneratedImageRef,
#[serde(default)]
#[ts(optional)]
pub transparent_background: Option<bool>,
#[serde(default)]
pub failure: Option<ImageGenerationFailure>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub saved_path: Option<AbsolutePathBuf>,
/// Observed local inputs for a completed generation, absent on older items.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[ts(as = "Option<_>", optional)]
pub input_images: Vec<ImageGenerationInputImage>,
/// Exact nested ImageGen request ID retained only for in-process analytics.
///
/// This is deliberately excluded from the extension/app-server wire shape
/// and rollout persistence because it is not a client-facing item field.
#[serde(skip)]
#[schemars(skip)]
#[ts(skip)]
pub imagegen_request_id: Option<String>,
/// Image generation identifier for the generated image, when provided by the image backend.
#[serde(default)]
pub generation_id: Option<String>,
}
/// Either inline generated-image data or its durable attachment-store identity.
///
/// `File` must remain first so rollouts written during the migration, which contain both an empty
/// `result` and a `fileId`, retain the file reference when deserialized.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
#[serde(untagged)]
#[ts(untagged)]
pub enum GeneratedImageRef {
File {
#[serde(rename = "fileId")]
#[ts(rename = "fileId")]
file_id: String,
},
Inline {
result: String,
},
}

View File

@@ -0,0 +1,61 @@
//! Typed display items owned by Codex extensions.
//!
//! This crate intentionally sits below `codex-protocol` so core can carry
//! extension items without owning each extension's display schema.
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use ts_rs::TS;
pub mod image_generation;
pub mod sleep;
pub mod web_search;
/// Canonical extension-owned turn item carried through core lifecycle events.
///
/// The item is serialized as a flattened, namespaced envelope:
///
/// ```json
/// {
/// "kind": "image_gen.generation",
/// "id": "call-id",
/// "status": "completed",
/// "revisedPrompt": "A blue square",
/// "result": "cG5n",
/// "savedPath": "/tmp/image.png"
/// }
/// ```
///
/// `kind` values follow `<extension_namespace>.<item_kind>`. Adding a variant
/// also requires app-server to add its typed public wrapper.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
#[serde(tag = "kind")]
#[ts(tag = "kind")]
pub enum ExtensionItem {
#[serde(rename = "image_gen.generation")]
#[ts(rename = "image_gen.generation")]
ImageGeneration(image_generation::ImageGenerationItem),
#[serde(rename = "clock.sleep")]
#[ts(rename = "clock.sleep")]
Sleep(sleep::SleepItem),
#[serde(rename = "web.search")]
#[ts(rename = "web.search")]
WebSearch(web_search::WebSearchItem),
}
impl ExtensionItem {
/// Returns the stable item identifier without exposing variant fields to
/// core or rollout persistence.
pub fn id(&self) -> &str {
match self {
Self::ImageGeneration(item) => &item.id,
Self::Sleep(item) => &item.id,
Self::WebSearch(item) => &item.id,
}
}
}
#[cfg(test)]
#[path = "tests.rs"]
mod tests;

View File

@@ -0,0 +1,14 @@
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use ts_rs::TS;
/// Display item emitted by the interruptible `clock.sleep` tool.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct SleepItem {
pub id: String,
#[ts(type = "number")]
pub duration_ms: u64,
}

View File

@@ -0,0 +1,314 @@
use pretty_assertions::assert_eq;
use serde_json::json;
use ts_rs::TS;
use super::ExtensionItem;
use super::image_generation::GeneratedImageRef;
use super::image_generation::ImageGenerationInputImage;
use super::image_generation::ImageGenerationItem;
use super::sleep::SleepItem;
use super::web_search::WebSearchAction;
use super::web_search::WebSearchItem;
use codex_utils_absolute_path::AbsolutePathBuf;
fn completed_image_generation_item() -> ExtensionItem {
ExtensionItem::ImageGeneration(ImageGenerationItem {
id: "image-1".to_string(),
status: "completed".to_string(),
revised_prompt: Some("A blue square".to_string()),
image: GeneratedImageRef::Inline {
result: "cG5n".to_string(),
},
transparent_background: None,
failure: None,
saved_path: None,
input_images: Vec::new(),
imagegen_request_id: None,
generation_id: None,
})
}
#[test]
fn image_generation_item_preserves_stable_wire_shape() {
let item = completed_image_generation_item();
let value = serde_json::to_value(&item).expect("serialize extension item");
assert_eq!(
value,
json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"result": "cG5n",
"transparentBackground": null,
"failure": null,
"generationId": null,
})
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(value).expect("deserialize extension item"),
item
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"result": "cG5n",
}))
.expect("deserialize legacy image-generation item without transparency metadata"),
item
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"result": "cG5n",
"fileId": null,
}))
.expect("deserialize transitional inline image-generation item"),
item
);
}
/// Transitional rollouts containing both fields must retain the durable file reference.
#[test]
fn image_generation_item_reads_transitional_file_wire_shape() {
let item = ExtensionItem::ImageGeneration(ImageGenerationItem {
id: "image-1".to_string(),
status: "completed".to_string(),
revised_prompt: Some("A blue square".to_string()),
image: GeneratedImageRef::File {
file_id: "file-1".to_string(),
},
transparent_background: None,
failure: None,
saved_path: None,
input_images: Vec::new(),
imagegen_request_id: None,
generation_id: None,
});
assert_eq!(
serde_json::from_value::<ExtensionItem>(json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"result": "",
"fileId": "file-1",
}))
.expect("deserialize transitional file-backed image-generation item"),
item
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"result": "stale-inline-bytes",
"fileId": "file-1",
}))
.expect("prefer file ID when both image representations are populated"),
item
);
assert_eq!(
serde_json::to_value(&item).expect("serialize file-backed extension item"),
json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"fileId": "file-1",
"transparentBackground": null,
"failure": null,
"generationId": null,
})
);
}
#[test]
fn image_generation_input_images_survive_serialization() {
let ExtensionItem::ImageGeneration(mut image) = completed_image_generation_item() else {
panic!("expected image-generation item");
};
let path = AbsolutePathBuf::current_dir()
.expect("absolute cwd")
.join("reference.png");
image.input_images = vec![ImageGenerationInputImage {
path: path.clone(),
sha256: "ab".repeat(32),
}];
let item = ExtensionItem::ImageGeneration(image);
let value = serde_json::to_value(&item).expect("serialize image generation");
assert_eq!(
value["inputImages"],
json!([{"path": path, "sha256": "ab".repeat(32)}])
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(value).expect("deserialize image generation"),
item
);
}
#[test]
fn image_generation_item_preserves_authoritative_transparency() {
let ExtensionItem::ImageGeneration(mut image) = completed_image_generation_item() else {
panic!("expected image-generation item");
};
image.transparent_background = Some(true);
let item = ExtensionItem::ImageGeneration(image);
let value = serde_json::to_value(&item).expect("serialize extension item");
assert_eq!(
value,
json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
"revisedPrompt": "A blue square",
"result": "cG5n",
"transparentBackground": true,
"failure": null,
"generationId": null,
})
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(value).expect("deserialize extension item"),
item
);
}
#[test]
fn image_generation_typescript_fields_preserve_wire_contracts() {
assert!(
ImageGenerationItem::inline().contains("{ fileId: string, } | { result: string, }")
|| ImageGenerationItem::inline().contains("{ result: string, } | { fileId: string, }"),
"v2 image-generation items must require exactly one image reference"
);
assert!(
ImageGenerationItem::inline().contains("transparentBackground?: boolean"),
"image-generation transparency must remain optional for existing TypeScript clients"
);
}
#[test]
fn image_generation_request_id_stays_internal() {
let ExtensionItem::ImageGeneration(mut image) = completed_image_generation_item() else {
panic!("expected image-generation item");
};
image.imagegen_request_id = Some("req-imagegen-123".to_string());
image.generation_id = Some("gen-image-123".to_string());
let item = ExtensionItem::ImageGeneration(image);
let value = serde_json::to_value(&item).expect("serialize extension item");
assert!(value.get("imagegenRequestId").is_none());
assert_eq!(value["generationId"], json!("gen-image-123"));
let ExtensionItem::ImageGeneration(round_tripped) =
serde_json::from_value::<ExtensionItem>(value).expect("deserialize extension item")
else {
panic!("expected image-generation item");
};
assert_eq!(round_tripped.imagegen_request_id, None);
assert_eq!(
round_tripped.generation_id.as_deref(),
Some("gen-image-123")
);
assert!(!ImageGenerationItem::inline().contains("imagegenRequestId"));
assert!(ImageGenerationItem::inline().contains("generationId: string | null"));
}
#[test]
fn web_search_item_preserves_stable_wire_shape() {
let item = ExtensionItem::WebSearch(WebSearchItem {
id: "search-1".to_string(),
query: "docs".to_string(),
action: Some(WebSearchAction::Search {
query: Some("docs".to_string()),
queries: None,
}),
results: None,
});
let value = serde_json::to_value(&item).expect("serialize extension item");
assert_eq!(
value,
json!({
"kind": "web.search",
"id": "search-1",
"query": "docs",
"action": {
"type": "search",
"query": "docs",
"queries": null,
},
"results": null,
})
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(value).expect("deserialize extension item"),
item
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(json!({
"kind": "web.search",
"id": "search-1",
"query": "docs",
"action": {
"type": "search",
"query": "docs",
"queries": null,
},
}))
.expect("deserialize legacy extension item without results"),
item
);
}
#[test]
fn sleep_item_preserves_stable_wire_shape() {
let item = ExtensionItem::Sleep(SleepItem {
id: "sleep-1".to_string(),
duration_ms: 1_000,
});
let value = serde_json::to_value(&item).expect("serialize extension item");
assert_eq!(
value,
json!({
"kind": "clock.sleep",
"id": "sleep-1",
"durationMs": 1_000,
})
);
assert_eq!(
serde_json::from_value::<ExtensionItem>(value).expect("deserialize extension item"),
item
);
}
#[test]
fn unknown_extension_kind_is_rejected() {
let value = json!({
"kind": "image_gen.unknown",
"id": "image-1",
});
assert!(serde_json::from_value::<ExtensionItem>(value).is_err());
}
#[test]
fn malformed_known_extension_payload_is_rejected() {
let value = json!({
"kind": "image_gen.generation",
"id": "image-1",
"status": "completed",
});
assert!(serde_json::from_value::<ExtensionItem>(value).is_err());
}

View File

@@ -0,0 +1,46 @@
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value as JsonValue;
use ts_rs::TS;
// Standalone web-search item owned by the web extension. This is also the
// field-level representation exposed by app-server; core and rollout
// persistence only carry it inside an ExtensionItem envelope.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct WebSearchItem {
pub id: String,
pub query: String,
pub action: Option<WebSearchAction>,
/// Structured search results returned out-of-band by standalone web search.
///
/// These stay as opaque JSON at the extension/app-server boundary so new
/// result fields and result types can pass through without a Codex release.
#[serde(default)]
pub results: Option<Vec<JsonValue>>,
}
// App-server-facing description of the action performed by standalone web search.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
#[serde(tag = "type", rename_all = "camelCase")]
#[ts(tag = "type", rename_all = "camelCase")]
// Keep app-server's existing v2 TS path. The root WebSearchAction name is
// already used by the snake_case Responses API action type.
#[ts(export_to = "v2/")]
pub enum WebSearchAction {
Search {
query: Option<String>,
queries: Option<Vec<String>>,
},
OpenPage {
url: Option<String>,
},
FindInPage {
url: Option<String>,
pattern: Option<String>,
},
#[serde(other)]
Other,
}