pocketstation 1.0.0

Source-aware desktop audio Session SDK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! SignalSpec — two-level typed signal contract for graph ports.
//!
//! Every port in the graph declares a `SignalSpec`.  The compiler validates
//! `SignalClass` compatibility on edge connections; `SemanticRole` is advisory
//! metadata for humans, tooling, and port-matching hints — the core never
//! enumerates roles.
//!
//! Design constraints (CRATE_OWNERSHIP.md):
//!   - `Custom(SignalId)` uses a stable string identifier, NOT a Rust TypeId.
//!     Rust TypeId is process-local and breaks Python/Swift/Kotlin/Protobuf/remote ops.
//!   - `SemanticRole` is a free-form string, not an enum. Application semantics
//!     belong to the role layer, not the class.
//!   - `SchemaRef` identifies an external schema document; the core never parses it.

use std::borrow::Cow;

/// Opaque identifier for a custom signal type.
///
/// Use a stable reverse-domain identifier such as `"com.example.signal.v1"`.
/// This survives serialization, cross-language use, and remote operator manifests.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SignalId(Cow<'static, str>);

impl SignalId {
    pub fn new(id: impl Into<SignalId>) -> Self {
        id.into()
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&str> for SignalId {
    fn from(s: &str) -> Self {
        Self(Cow::Owned(s.to_owned()))
    }
}

impl From<String> for SignalId {
    fn from(s: String) -> Self {
        Self(Cow::Owned(s))
    }
}

/// Semantic role annotation on a port.
///
/// Free-form dot-namespaced string such as `"result.partial"` or
/// `"state.final"`. The core never matches on role values for routing.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SemanticRole(Cow<'static, str>);

impl SemanticRole {
    pub fn new(role: impl Into<SemanticRole>) -> Self {
        role.into()
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&str> for SemanticRole {
    fn from(s: &str) -> Self {
        Self(Cow::Owned(s.to_owned()))
    }
}

impl From<String> for SemanticRole {
    fn from(s: String) -> Self {
        Self(Cow::Owned(s))
    }
}

/// Reference to an external schema document.
///
/// Identifies the shape of a custom or structured signal — e.g. a Protobuf
/// message name, JSON Schema URI, or Flatbuffers table identifier.
/// The core never parses schema content; this is for tooling and validation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SchemaRef(Cow<'static, str>);

impl SchemaRef {
    pub fn new(schema: impl Into<SchemaRef>) -> Self {
        schema.into()
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&str> for SchemaRef {
    fn from(s: &str) -> Self {
        Self(Cow::Owned(s.to_owned()))
    }
}

impl From<String> for SchemaRef {
    fn from(s: String) -> Self {
        Self(Cow::Owned(s))
    }
}

/// Audio encoding format for `SignalClass::EncodedAudio`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Codec {
    Opus,
    Aac,
    Mp3,
    G711Ulaw,
    G711Alaw,
    WebmOpus,
}

/// Text encoding hint for `SignalClass::Text`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TextFormat {
    Utf8,
    Json,
    Markdown,
}

/// Event structure hint for `SignalClass::Event`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EventFormat {
    Json,
    Protobuf,
    Flatbuffers,
    Cbor,
}

/// Binary encoding hint for `SignalClass::Binary`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BinaryFormat {
    Raw,
    Protobuf,
    Flatbuffers,
    Cbor,
}

/// The fundamental class of data flowing through a port.
///
/// The compiler validates class compatibility between connected ports.
/// `Custom` breaks out of the fixed taxonomy using a stable string identifier.
///
/// Do not add application semantics here; those belong in open roles.
/// Those belong in `SemanticRole` — a free-form label that the core never enumerates.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SignalClass {
    /// Wildcard accepted only at deliberately open graph boundaries.
    Any,
    /// Interleaved PCM audio samples (format described by the edge AudioCaps).
    PcmAudio,
    /// Compressed audio bitstream (Opus packet, AAC frame, …).
    EncodedAudio(Codec),
    /// UTF-8 or structured text.
    Text(TextFormat),
    /// Discrete event payloads.
    Event(EventFormat),
    /// Telemetry and observability counters / gauges.
    Metrics,
    /// Graph control messages (route patches, session lifecycle, mute/unmute).
    Control,
    /// Opaque binary blob.
    Binary(BinaryFormat),
    /// Extension point for community / vendor signals.
    /// Use a stable reverse-domain identifier.
    Custom(SignalId),
}

impl SignalClass {
    /// Returns `true` for classes that carry real-time audio on the hot path.
    pub fn is_audio(self: &SignalClass) -> bool {
        matches!(self, Self::PcmAudio | Self::EncodedAudio(_))
    }

    /// Returns `true` if two signal classes are compatible for edge wiring.
    ///
    /// Compatibility is exact-match for concrete classes.  The compiler uses
    /// this to reject mismatched port wiring at graph validation time.
    pub fn is_compatible_with(&self, other: &SignalClass) -> bool {
        matches!(self, Self::Any) || matches!(other, Self::Any) || self == other
    }
}

/// Full signal contract for a single port.
///
/// ```
/// use pocketstation::{SignalSpec, TextFormat};
///
/// // Mic audio port
/// let mic = SignalSpec::audio().with_role("voice");
///
/// // Application-defined structured result port
/// let result = SignalSpec::text(TextFormat::Utf8).with_role("result.partial");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignalSpec {
    /// What kind of data (class-level compatibility — validated by compiler).
    pub(crate) class: SignalClass,
    /// What the data means (advisory; humans + tooling only).
    pub(crate) role: Option<SemanticRole>,
    /// Schema document reference for structured payloads.
    pub(crate) schema: Option<SchemaRef>,
}

impl SignalSpec {
    pub const fn class(&self) -> &SignalClass {
        &self.class
    }

    pub const fn role(&self) -> Option<&SemanticRole> {
        self.role.as_ref()
    }

    pub const fn schema(&self) -> Option<&SchemaRef> {
        self.schema.as_ref()
    }
    pub fn new(class: SignalClass) -> Self {
        Self {
            class,
            role: None,
            schema: None,
        }
    }

    /// Stable language-neutral identifier for the fundamental wire class.
    /// Semantic role and schema remain separate fields.
    pub fn wire_id(&self) -> &str {
        match &self.class {
            SignalClass::Any => "pks.signal.any.v1",
            SignalClass::PcmAudio => "pks.signal.pcm-audio.v1",
            SignalClass::EncodedAudio(Codec::Opus) => "pks.signal.encoded.opus.v1",
            SignalClass::EncodedAudio(Codec::Aac) => "pks.signal.encoded.aac.v1",
            SignalClass::EncodedAudio(Codec::Mp3) => "pks.signal.encoded.mp3.v1",
            SignalClass::EncodedAudio(Codec::G711Ulaw) => "pks.signal.encoded.g711-ulaw.v1",
            SignalClass::EncodedAudio(Codec::G711Alaw) => "pks.signal.encoded.g711-alaw.v1",
            SignalClass::EncodedAudio(Codec::WebmOpus) => "pks.signal.encoded.webm-opus.v1",
            SignalClass::Text(TextFormat::Utf8) => "pks.signal.text.utf8.v1",
            SignalClass::Text(TextFormat::Json) => "pks.signal.text.json.v1",
            SignalClass::Text(TextFormat::Markdown) => "pks.signal.text.markdown.v1",
            SignalClass::Event(EventFormat::Json) => "pks.signal.event.json.v1",
            SignalClass::Event(EventFormat::Protobuf) => "pks.signal.event.protobuf.v1",
            SignalClass::Event(EventFormat::Flatbuffers) => "pks.signal.event.flatbuffers.v1",
            SignalClass::Event(EventFormat::Cbor) => "pks.signal.event.cbor.v1",
            SignalClass::Metrics => "pks.signal.metrics.v1",
            SignalClass::Control => "pks.signal.control.v1",
            SignalClass::Binary(BinaryFormat::Raw) => "pks.signal.binary.raw.v1",
            SignalClass::Binary(BinaryFormat::Protobuf) => "pks.signal.binary.protobuf.v1",
            SignalClass::Binary(BinaryFormat::Flatbuffers) => "pks.signal.binary.flatbuffers.v1",
            SignalClass::Binary(BinaryFormat::Cbor) => "pks.signal.binary.cbor.v1",
            SignalClass::Custom(id) => id.as_str(),
        }
    }

    /// Convenience constructor for a deliberately open boundary port.
    pub fn any() -> Self {
        Self::new(SignalClass::Any)
    }

    /// Convenience constructor for PCM audio ports.
    pub fn audio() -> Self {
        Self::new(SignalClass::PcmAudio)
    }

    /// Convenience constructor for encoded audio ports.
    pub fn encoded_audio(codec: Codec) -> Self {
        Self::new(SignalClass::EncodedAudio(codec))
    }

    /// Convenience constructor for text ports.
    pub fn text(format: TextFormat) -> Self {
        Self::new(SignalClass::Text(format))
    }

    /// Convenience constructor for event ports.
    pub fn event(format: EventFormat) -> Self {
        Self::new(SignalClass::Event(format))
    }

    /// Convenience constructor for metrics ports.
    pub fn metrics() -> Self {
        Self::new(SignalClass::Metrics)
    }

    /// Convenience constructor for control ports.
    pub fn control() -> Self {
        Self::new(SignalClass::Control)
    }

    /// Convenience constructor for opaque or schema-backed binary ports.
    pub fn binary(format: BinaryFormat) -> Self {
        Self::new(SignalClass::Binary(format))
    }

    /// Convenience constructor for custom / vendor extension ports.
    pub fn custom(id: impl Into<SignalId>) -> Self {
        Self::new(SignalClass::Custom(id.into()))
    }

    /// Attach a semantic role annotation.
    pub fn with_role(mut self, role: impl Into<SemanticRole>) -> Self {
        self.role = Some(role.into());
        self
    }

    /// Attach a schema reference.
    pub fn with_schema(mut self, schema: impl Into<SchemaRef>) -> Self {
        self.schema = Some(schema.into());
        self
    }

    /// Returns `true` if this spec can connect to `other` on an edge.
    ///
    /// Class must match exactly.  Role and schema are informational only
    /// and never block wiring.
    pub fn is_compatible_with(&self, other: &SignalSpec) -> bool {
        self.class.is_compatible_with(&other.class)
    }

    pub fn validate(&self) -> Result<(), SignalSpecError> {
        if matches!(&self.class, SignalClass::Custom(id) if id.as_str().trim().is_empty()) {
            return Err(SignalSpecError::EmptyCustomId);
        }
        if self
            .role
            .as_ref()
            .is_some_and(|role| role.as_str().trim().is_empty())
        {
            return Err(SignalSpecError::EmptyRole);
        }
        if self
            .schema
            .as_ref()
            .is_some_and(|schema| schema.as_str().trim().is_empty())
        {
            return Err(SignalSpecError::EmptySchema);
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum SignalSpecError {
    #[error("custom signal identifier cannot be empty")]
    EmptyCustomId,
    #[error("signal semantic role cannot be empty")]
    EmptyRole,
    #[error("signal schema reference cannot be empty")]
    EmptySchema,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn given_two_pcm_audio_specs_when_compatible_check_then_true() {
        let a = SignalSpec::audio().with_role("voice");
        let b = SignalSpec::audio().with_role("music"); // different role — irrelevant
        assert!(a.is_compatible_with(&b));
    }

    #[test]
    fn given_audio_and_text_specs_when_compatible_check_then_false() {
        let audio = SignalSpec::audio();
        let text = SignalSpec::text(TextFormat::Utf8);
        assert!(!audio.is_compatible_with(&text));
    }

    #[test]
    fn given_pcm_audio_spec_when_is_audio_then_true() {
        assert!(SignalClass::PcmAudio.is_audio());
        assert!(SignalClass::EncodedAudio(Codec::Opus).is_audio());
    }

    #[test]
    fn given_text_spec_when_is_audio_then_false() {
        assert!(!SignalClass::Text(TextFormat::Utf8).is_audio());
        assert!(!SignalClass::Control.is_audio());
        assert!(!SignalClass::Metrics.is_audio());
    }

    #[test]
    fn given_custom_signal_with_role_when_built_then_fields_accessible() {
        let spec = SignalSpec::custom("com.acme.sentiment.v1")
            .with_role("emotion.stress")
            .with_schema("proto:acme.Sentiment");
        assert_eq!(
            spec.class,
            SignalClass::Custom(SignalId::new("com.acme.sentiment.v1"))
        );
        assert_eq!(
            spec.role.as_ref().map(|r| r.as_str()),
            Some("emotion.stress")
        );
        assert_eq!(
            spec.schema.as_ref().map(|s| s.as_str()),
            Some("proto:acme.Sentiment")
        );
    }

    #[test]
    fn given_two_different_custom_ids_when_compatible_check_then_false() {
        let a = SignalSpec::custom("com.acme.foo.v1");
        let b = SignalSpec::custom("com.acme.bar.v1");
        assert!(!a.is_compatible_with(&b));
    }

    #[test]
    fn given_same_custom_ids_when_compatible_check_then_true() {
        let a = SignalSpec::custom("com.acme.foo.v1");
        let b = SignalSpec::custom("com.acme.foo.v1");
        assert!(a.is_compatible_with(&b));
    }

    #[test]
    fn given_signal_id_when_as_str_then_returns_inner() {
        let id = SignalId::new("com.acme.widget.v2");
        assert_eq!(id.as_str(), "com.acme.widget.v2");
    }

    #[test]
    fn given_semantic_role_when_as_str_then_returns_inner() {
        let role = SemanticRole::new("transcript.partial");
        assert_eq!(role.as_str(), "transcript.partial");
    }

    #[test]
    fn given_empty_custom_id_role_or_schema_when_validated_then_rejected() {
        assert_eq!(
            SignalSpec::custom("").validate(),
            Err(SignalSpecError::EmptyCustomId)
        );
        assert_eq!(
            SignalSpec::control().with_role("").validate(),
            Err(SignalSpecError::EmptyRole)
        );
        assert_eq!(
            SignalSpec::binary(BinaryFormat::Raw)
                .with_schema("")
                .validate(),
            Err(SignalSpecError::EmptySchema)
        );
    }
}