Skip to content

Commit

Permalink
chore: fix up v1 -> v2 API migration for AI endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Jan 16, 2025
1 parent 6ec9abf commit 43c88f6
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 166 deletions.
24 changes: 11 additions & 13 deletions modules/fundamental/src/ai/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod test;

use crate::{
ai::{
model::{AiFlags, AiTool, ChatState, Conversation, ConversationSummary},
model::{AiFlags, AiTool, ChatMessage, ChatState, Conversation, ConversationSummary},
service::AiService,
},
Error,
Expand All @@ -13,15 +13,13 @@ use actix_web::{
http::header::{self, ETag, EntityTag, IfMatch},
post, put, web, HttpResponse, Responder,
};

use crate::ai::model::ChatMessage;
use itertools::Itertools;
use time::OffsetDateTime;
use trustify_auth::authenticator::user::UserDetails;
use trustify_auth::{authorizer::Require, Ai};
use trustify_common::db::query::Query;
use trustify_common::db::Database;
use trustify_common::model::{Paginated, PaginatedResults};
use trustify_auth::{authenticator::user::UserDetails, authorizer::Require, Ai};
use trustify_common::{
db::{query::Query, Database},
model::{Paginated, PaginatedResults},
};
use uuid::Uuid;

pub fn configure(config: &mut utoipa_actix_web::service_config::ServiceConfig, db: Database) {
Expand Down Expand Up @@ -149,7 +147,7 @@ pub async fn tool_call(
(status = 404, description = "The AI service is not enabled")
)
)]
#[post("/v1/ai/conversations")]
#[post("/v2/ai/conversations")]
pub async fn create_conversation(_: Require<Ai>) -> actix_web::Result<impl Responder, Error> {
// generate an assistant response
let uuid = Uuid::now_v7();
Expand Down Expand Up @@ -187,7 +185,7 @@ fn to_offset_date_time(uuid: Uuid) -> Result<OffsetDateTime, Error> {
(status = 404, description = "The AI service is not enabled or the conversation was not found")
)
)]
#[put("/v1/ai/conversations/{id}")]
#[put("/v2/ai/conversations/{id}")]
pub async fn update_conversation(
service: web::Data<AiService>,
db: web::Data<Database>,
Expand Down Expand Up @@ -232,7 +230,7 @@ pub async fn update_conversation(
(status = 404, description = "The AI service is not enabled")
)
)]
#[get("/v1/ai/conversations")]
#[get("/v2/ai/conversations")]
// Gets the list of the user's previous conversations
pub async fn list_conversations(
service: web::Data<AiService>,
Expand Down Expand Up @@ -278,7 +276,7 @@ pub async fn list_conversations(
(status = 404, description = "The AI service is not enabled")
)
)]
#[get("/v1/ai/conversations/{id}")]
#[get("/v2/ai/conversations/{id}")]
pub async fn get_conversation(
service: web::Data<AiService>,
db: web::Data<Database>,
Expand Down Expand Up @@ -338,7 +336,7 @@ pub async fn get_conversation(
(status = 404, description = "The AI service is not enabled or the conversation was not found")
)
)]
#[delete("/v1/ai/conversations/{id}")]
#[delete("/v2/ai/conversations/{id}")]
pub async fn delete_conversation(
service: web::Data<AiService>,
db: web::Data<Database>,
Expand Down
306 changes: 153 additions & 153 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,159 +24,6 @@ paths:
type: object
version:
type: string
/api/v1/ai/conversations:
get:
tags:
- ai
operationId: listConversations
parameters:
- name: q
in: query
required: false
schema:
type: string
- name: sort
in: query
required: false
schema:
type: string
- name: offset
in: query
description: |-
The first item to return, skipping all that come before it.
NOTE: The order of items is defined by the API being called.
required: false
schema:
type: integer
format: int64
minimum: 0
- name: limit
in: query
description: |-
The maximum number of entries to return.
Zero means: no limit
required: false
schema:
type: integer
format: int64
minimum: 0
responses:
'200':
description: The resulting list of conversation summaries
content:
application/json:
schema:
$ref: '#/components/schemas/PaginatedResults_ConversationSummary'
'404':
description: The AI service is not enabled
post:
tags:
- ai
operationId: createConversation
responses:
'200':
description: The resulting conversation
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled
/api/v1/ai/conversations/{id}:
get:
tags:
- ai
operationId: getConversation
parameters:
- name: id
in: path
description: Opaque ID of the conversation
required: true
schema:
type: string
format: uuid
responses:
'200':
description: The resulting conversation
headers:
etag:
schema:
type: string
description: Sequence ID
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled
put:
tags:
- ai
operationId: updateConversation
parameters:
- name: id
in: path
description: Opaque ID of the conversation
required: true
schema:
type: string
format: uuid
- name: if-match
in: header
description: The revision to update
required: false
schema:
type:
- string
- 'null'
requestBody:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ChatMessage'
required: true
responses:
'200':
description: The resulting conversation
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled or the conversation was not found
delete:
tags:
- ai
operationId: deleteConversation
parameters:
- name: id
in: path
description: Opaque ID of the conversation
required: true
schema:
type: string
format: uuid
responses:
'200':
description: The resulting conversation
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled or the conversation was not found
/api/v2/advisory:
get:
tags:
Expand Down Expand Up @@ -401,6 +248,159 @@ paths:
description: The request was invalid
'404':
description: The AI service is not enabled
/api/v2/ai/conversations:
get:
tags:
- ai
operationId: listConversations
parameters:
- name: q
in: query
required: false
schema:
type: string
- name: sort
in: query
required: false
schema:
type: string
- name: offset
in: query
description: |-
The first item to return, skipping all that come before it.
NOTE: The order of items is defined by the API being called.
required: false
schema:
type: integer
format: int64
minimum: 0
- name: limit
in: query
description: |-
The maximum number of entries to return.
Zero means: no limit
required: false
schema:
type: integer
format: int64
minimum: 0
responses:
'200':
description: The resulting list of conversation summaries
content:
application/json:
schema:
$ref: '#/components/schemas/PaginatedResults_ConversationSummary'
'404':
description: The AI service is not enabled
post:
tags:
- ai
operationId: createConversation
responses:
'200':
description: The resulting conversation
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled
/api/v2/ai/conversations/{id}:
get:
tags:
- ai
operationId: getConversation
parameters:
- name: id
in: path
description: Opaque ID of the conversation
required: true
schema:
type: string
format: uuid
responses:
'200':
description: The resulting conversation
headers:
etag:
schema:
type: string
description: Sequence ID
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled
put:
tags:
- ai
operationId: updateConversation
parameters:
- name: id
in: path
description: Opaque ID of the conversation
required: true
schema:
type: string
format: uuid
- name: if-match
in: header
description: The revision to update
required: false
schema:
type:
- string
- 'null'
requestBody:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ChatMessage'
required: true
responses:
'200':
description: The resulting conversation
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled or the conversation was not found
delete:
tags:
- ai
operationId: deleteConversation
parameters:
- name: id
in: path
description: Opaque ID of the conversation
required: true
schema:
type: string
format: uuid
responses:
'200':
description: The resulting conversation
content:
application/json:
schema:
$ref: '#/components/schemas/Conversation'
'400':
description: The request was invalid
'404':
description: The AI service is not enabled or the conversation was not found
/api/v2/ai/flags:
get:
tags:
Expand Down

0 comments on commit 43c88f6

Please sign in to comment.