//
// OpenRocky — Voice-first AI Agent
// https://github.com/openrocky
//
// Developed by everettjf with the assistance of Claude Code and Codex.
// Date: 2026-04-25
// Copyright (c) 2026 everettjf. All rights reserved.
//

import Foundation
@preconcurrency import SwiftOpenAI

/// `conversation.item.truncate` — tells the server "the user only heard up to this
/// point of the assistant's audio". The server then rewrites that conversation item
/// to that prefix before the next response.
///
/// Required for clean barge-in: without it the server thinks the user heard the full
/// assistant reply and conditions the next turn on words the user never heard.
///
/// `audioEndMs` is computed by `OpenRockyRealtimeVoiceBridge` from wall-clock
/// time since the first audio chunk for the response was queued. AudioController
/// adds a few tens of ms of buffering so the figure trails actual playback by
/// that much — close enough to keep the server's transcript aligned with what
/// the user remembers hearing.
///
/// https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/truncate
@RealtimeActor
struct OpenRockyRealtimeConversationItemTruncate: Encodable {
    let type = "conversation.item.truncate"
    let itemID: String
    let contentIndex: Int
    let audioEndMs: Int

    init(itemID: String, contentIndex: Int = 0, audioEndMs: Int = 0) {
        self.itemID = itemID
        self.contentIndex = contentIndex
        self.audioEndMs = audioEndMs
    }

    enum CodingKeys: String, CodingKey {
        case type
        case itemID = "item_id"
        case contentIndex = "content_index"
        case audioEndMs = "audio_end_ms"
    }
}
