Module: Legion::LLM::API::Namespaces::OpenAI::Conversations
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/namespaces/openai/conversations.rb,
lib/legion/llm/api/namespaces/openai/conversations/items.rb
Defined Under Namespace
Modules: Items
Class Method Summary collapse
-
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
Class Method Details
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/legion/llm/api/namespaces/openai/conversations.rb', line 16 def self.registered(app) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength log.debug('[llm][api][namespaces][openai][conversations] registering routes') app.post '/v1/conversations' do require_llm! body = parse_request_body conv_id = "conv_#{SecureRandom.hex(16)}" model = body[:model].to_s title = body.dig(:metadata, :title) = body[:metadata] || {} Inference::Conversation.create_conversation(conv_id) Inference::Conversation.(conv_id, title: title, model: model.empty? ? nil : model) log.debug("[llm][api][openai][conversations] action=create conv_id=#{conv_id} model=#{model}") content_type :json status 200 Legion::JSON.dump({ id: conv_id, object: 'conversation', created_at: Time.now.to_i, model: model.empty? ? nil : model, metadata: }.compact) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.conversations.create') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end app.get '/v1/conversations/:id' do require_llm! conv_id = params[:id] unless Inference::Conversation.conversation_exists?(conv_id) halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.", type: 'invalid_request_error', code: 'conversation_not_found' } }) end = Inference::Conversation.(conv_id) || {} # Tombstone check — conversation was deleted via DELETE /:id if [:title] == '__deleted__' halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.", type: 'invalid_request_error', code: 'conversation_not_found' } }) end msg_count = Inference::Conversation.(conv_id).size log.debug("[llm][api][openai][conversations] action=get conv_id=#{conv_id} messages=#{msg_count}") content_type :json status 200 Legion::JSON.dump({ id: conv_id, object: 'conversation', created_at: nil, model: [:model], title: [:title], message_count: msg_count }.compact) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.conversations.get') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end app.post '/v1/conversations/:id' do require_llm! conv_id = params[:id] body = parse_request_body unless Inference::Conversation.conversation_exists?(conv_id) halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.", type: 'invalid_request_error', code: 'conversation_not_found' } }) end = body[:metadata] || {} title = [:title] model = [:model] || body[:model] Inference::Conversation.(conv_id, title: title, model: model) log.debug("[llm][api][openai][conversations] action=update conv_id=#{conv_id}") content_type :json status 200 Legion::JSON.dump( { id: conv_id, object: 'conversation', metadata: } ) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.conversations.update') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end app.delete '/v1/conversations/:id' do require_llm! conv_id = params[:id] unless Inference::Conversation.conversation_exists?(conv_id) halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.", type: 'invalid_request_error', code: 'conversation_not_found' } }) end # Tombstone strategy: mark this specific conversation as deleted. # GET /:id checks for tombstone and treats it as gone. Inference::Conversation.(conv_id, title: '__deleted__') log.debug("[llm][api][openai][conversations] action=delete conv_id=#{conv_id}") content_type :json status 200 Legion::JSON.dump({ id: conv_id, object: 'conversation', deleted: true }) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.conversations.delete') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end log.debug('[llm][api][namespaces][openai][conversations] routes registered') rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.conversations.register') end |