mirror of
https://github.com/openai/codex.git
synced 2026-11-02 19:43:24 +00:00
## What changed - Tell the model that generated images are already displayed to the user and should not be repeated as Markdown images or file links. - Keep generated-image artifact paths and output hints owned by the image generation extension. - Centralize the availability check for the image generation tool across feature, provider, namespace-tool, model modality, and authentication requirements. ## Testing - Verify artifact paths sanitize session and call identifiers. - Verify the model-facing output hint reports that the generated image is already displayed. GitOrigin-RevId: 6c865b6a96d6a71bb2c33246d1518a00e7c8b840
47 lines
1.7 KiB
Rust
47 lines
1.7 KiB
Rust
use std::fmt::Display;
|
|
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
|
|
const GENERATED_IMAGE_ARTIFACTS_DIR: &str = "generated_images";
|
|
const MAX_IMAGE_GENERATION_OUTPUT_HINT_BYTES: usize = 1024;
|
|
|
|
/// Returns the extension-owned artifact path for a generated image.
|
|
pub(crate) fn image_generation_artifact_path(
|
|
save_root: &AbsolutePathBuf,
|
|
session_id: &str,
|
|
call_id: &str,
|
|
) -> AbsolutePathBuf {
|
|
let sanitize = |value: &str| {
|
|
let mut sanitized: String = value
|
|
.chars()
|
|
.map(|ch| {
|
|
if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' {
|
|
ch
|
|
} else {
|
|
'_'
|
|
}
|
|
})
|
|
.collect();
|
|
if sanitized.is_empty() {
|
|
sanitized = "generated_image".to_string();
|
|
}
|
|
sanitized
|
|
};
|
|
|
|
save_root
|
|
.join(GENERATED_IMAGE_ARTIFACTS_DIR)
|
|
.join(sanitize(session_id))
|
|
.join(format!("{}.png", sanitize(call_id)))
|
|
}
|
|
|
|
/// Returns the model-facing generated-image path hint, or omits it if it is too large.
|
|
pub(crate) fn image_generation_output_hint(
|
|
image_output_dir: impl Display,
|
|
image_output_path: impl Display,
|
|
) -> Option<String> {
|
|
let hint = format!(
|
|
"Generated images are saved to {image_output_dir} as {image_output_path} by default.\nIf you need to use a generated image at another path, copy it and leave the original in place unless the user explicitly asks you to delete it.\nThe generated image is already displayed to the user. There is no need to render it in the final response as a Markdown image or file link."
|
|
);
|
|
(hint.len() <= MAX_IMAGE_GENERATION_OUTPUT_HINT_BYTES).then_some(hint)
|
|
}
|