kcode-speaker-v3-gemini-protocol 0.1.0

Deterministic Speaker V3 Gemini protocol construction and decoding
Documentation
# Purpose

Constructs and decodes the provider-independent Gemini portion of the Speaker V3 protocol. It owns the frozen transcript and feature prompts, request ordering, packet ordering, target substitution, and textual-candidate extraction. It performs no provider calls, cache management, identity work, persistence, retries, or result limiting.

# Public API

```rust
pub const GEMINI_TRANSCRIPT_PROMPT_REVISION: &str;
pub const GEMINI_FEATURE_PROMPT_ONE_REVISION: &str;
pub const GEMINI_FEATURE_PROMPT_TWO_REVISION: &str;
pub const GEMINI_FEATURE_PROMPT_THREE_REVISION: &str;
pub const GEMINI_FEATURE_PROMPT_REVISIONS: [&str; 3];
pub const GEMINI_TRANSCRIPT_PROMPT: &str;
pub const GEMINI_FEATURE_PROMPT_ONE: &str;
pub const GEMINI_FEATURE_PROMPT_TWO: &str;
pub const GEMINI_FEATURE_PROMPT_THREE: &str;
pub const FEATURE_PACKETS: [FeaturePacket; 3];

pub enum GeminiRequestPart<'a> {
    Audio {
        media_type: &'static str,
        bytes: &'a [u8],
    },
    Text(String),
}

pub enum FeaturePacket {
    One,
    Two,
    Three,
}
impl FeaturePacket {
    pub fn index(self) -> usize;
    pub fn prompt(self) -> &'static str;
    pub fn revision(self) -> &'static str;
}

pub enum GeminiProtocolError {
    InvalidResponse(String),
    TextCandidateCount(usize),
}
impl Display for GeminiProtocolError;
impl Error for GeminiProtocolError;

pub fn gemini_transcript_request(audio: &[u8]) -> [GeminiRequestPart<'_>; 2];
pub fn gemini_feature_cached_prefix<'a>(
    audio: &'a [u8],
    transcript: &str,
) -> [GeminiRequestPart<'a>; 2];
pub fn gemini_feature_suffix(
    packet: FeaturePacket,
    target: kcode_speaker_v3_schema::LocalSpeakerLabel,
) -> String;
pub fn extract_gemini_text(
    response: &serde_json::Value,
) -> Result<String, GeminiProtocolError>;
```

`GeminiRequestPart`, `GeminiProtocolError`, and `FeaturePacket` implement `Debug`, `Clone`, `PartialEq`, and `Eq`. `FeaturePacket` also implements `Copy`, `PartialOrd`, `Ord`, and `Hash`.

# Requests

`gemini_transcript_request` returns borrowed Ogg audio with media type `audio/ogg` followed by the complete frozen transcript prompt. It accepts any byte slice and neither validates nor copies the audio.

`gemini_feature_cached_prefix` returns the same borrowed audio followed by the common feature-prompt prefix through the supplied transcript. It copies transcript bytes exactly without trimming or placeholder substitution. `gemini_feature_suffix` returns the selected packet text after the transcript placeholder, substitutes only the exact target-speaker placeholder, and leaves the target label last. `FEATURE_PACKETS` and packet indices define packet order one, two, three.

# Extraction

`extract_gemini_text` requires a top-level candidate array and a content-parts array for every candidate. It concatenates each candidate's string-valued textual parts in part order without separators or trimming, ignores candidates whose concatenated text is blank, and succeeds only when exactly one nonblank textual candidate remains. Non-text parts are ignored. A present `text` field with a non-string value is invalid. The operation does not inspect other response fields.

# Performance and concurrency

All operations are deterministic and stateless. Independent calls share no state, locks, queue, wait, callback, or serialization and may proceed concurrently. There is no filesystem, network, provider, clock, retry, timeout, cache policy, persistence, or hidden input limit.

Request construction is O(transcript bytes) time and memory while borrowing audio. Suffix construction is O(packet bytes). Extraction is O(response bytes) time and uses O(response plus concatenated text) peak memory. The frozen prompt and packet lookups are constant work.

The reference canary is `cargo test` in the Kennedy hardened rootless Podman validation image on one x86-64 vCPU. Extraction of one 1 MiB textual response and the other local protocol checks complete within 10 seconds. This fixture is a measurement, not an accepted-input limit.