Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add x-speakeasy-sse-sentinel to OpenAPI documentation for chat … #146

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions atoma-proxy/docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ paths:
text/event-stream:
schema:
$ref: '#/components/schemas/ChatCompletionStreamResponse'
x-speakeasy-sse-sentinel: '[DONE]'
'400':
description: Bad request
'401':
Expand Down
44 changes: 44 additions & 0 deletions atoma-proxy/src/server/components/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,50 @@ pub fn openapi_routes() -> Router {
let spec =
serde_yaml::to_string(&ApiDoc::openapi()).expect("Failed to serialize OpenAPI spec");

// Parse the YAML into a serde_yaml::Value for modification
let mut spec_value: serde_yaml::Value =
serde_yaml::from_str(&spec).expect("Failed to parse OpenAPI spec");

// Add x-speakeasy-sse-sentinel to the chat completions stream endpoint
if let serde_yaml::Value::Mapping(ref mut paths) = spec_value["paths"] {
if let Some(serde_yaml::Value::Mapping(ref mut endpoint)) = paths.get_mut(
&serde_yaml::Value::String("/v1/chat/completions#stream".to_string()),
) {
if let Some(serde_yaml::Value::Mapping(ref mut post)) =
endpoint.get_mut(&serde_yaml::Value::String("post".to_string()))
{
if let Some(serde_yaml::Value::Mapping(ref mut responses)) =
post.get_mut(&serde_yaml::Value::String("responses".to_string()))
{
if let Some(serde_yaml::Value::Mapping(ref mut ok_response)) =
responses.get_mut(&serde_yaml::Value::String("200".to_string()))
{
if let Some(serde_yaml::Value::Mapping(ref mut content)) = ok_response
.get_mut(&serde_yaml::Value::String("content".to_string()))
{
if let Some(serde_yaml::Value::Mapping(ref mut event_stream)) =
content.get_mut(&serde_yaml::Value::String(
"text/event-stream".to_string(),
))
{
event_stream.insert(
serde_yaml::Value::String(
"x-speakeasy-sse-sentinel".to_string(),
),
serde_yaml::Value::String("[DONE]".to_string()),
);
}
}
}
}
}
}
}

// Convert back to YAML string
let spec =
serde_yaml::to_string(&spec_value).expect("Failed to serialize modified OpenAPI spec");

// Ensure the docs directory exists
let docs_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("docs");
fs::create_dir_all(&docs_dir).expect("Failed to create docs directory");
Expand Down