cognis-core 0.2.0

Core traits and types for the Cognis LLM framework
Documentation
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
448
449
450
451
452
453
454
455
456
457
458
459
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// A citation annotation referencing a source document.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Citation {
    /// URL of the document source.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    /// Source document title.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Start index of the response text.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start_index: Option<usize>,
    /// End index of the response text.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end_index: Option<usize>,
    /// Excerpt of source text being cited.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cited_text: Option<String>,
    /// Provider-specific metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extras: Option<Value>,
}

/// An annotation on a content block.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Annotation {
    /// A citation annotation.
    Citation(Citation),
    /// A provider-specific annotation.
    NonStandardAnnotation {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        value: Value,
    },
}

/// Standard content block types for LLM I/O.
///
/// Each variant corresponds to a typed content block from the Python
/// `langchain_core.messages.content` module. All multimodal data blocks
/// (Image, Video, Audio, File, PlainText) support URL, base64, and file_id
/// sources.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlock {
    /// Plain text content from a user or model.
    Text {
        text: String,
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// Citations and other annotations.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        annotations: Option<Vec<Annotation>>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Image data (URL, base64, or file reference).
    Image {
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// URL of the image.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        url: Option<String>,
        /// Base64-encoded image data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        base64: Option<String>,
        /// Reference to an image in an external file storage system.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// MIME type of the image. Required for base64 data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mime_type: Option<String>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        // Legacy fields for backward compatibility with old-style blocks.
        /// Legacy: inline image data (base64 or URL).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        image: Option<String>,
        /// Legacy: source type ("url", "base64", "id").
        #[serde(default, skip_serializing_if = "Option::is_none")]
        source_type: Option<String>,
        /// Legacy: media type alias.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// OpenAI-style image_url block with optional detail level.
    ImageUrl {
        /// The image URL info (url, detail).
        image_url: ImageUrlInfo,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Audio data (URL, base64, or file reference).
    Audio {
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// URL of the audio.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        url: Option<String>,
        /// Base64-encoded audio data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        base64: Option<String>,
        /// Reference to an audio file in an external file storage system.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// MIME type of the audio. Required for base64 data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mime_type: Option<String>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        // Legacy fields for backward compatibility.
        /// Legacy: inline audio data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        audio: Option<String>,
        /// Legacy: source type.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        source_type: Option<String>,
        /// Legacy: media type alias.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Video data (URL, base64, or file reference).
    Video {
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// URL of the video.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        url: Option<String>,
        /// Base64-encoded video data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        base64: Option<String>,
        /// Reference to a video file in an external file storage system.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// MIME type of the video. Required for base64 data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mime_type: Option<String>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        // Legacy fields for backward compatibility.
        /// Legacy: inline video data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        video: Option<String>,
        /// Legacy: source type.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        source_type: Option<String>,
        /// Legacy: media type alias.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// File data (PDFs, documents, etc. — not images/audio/video).
    File {
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// URL of the file.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        url: Option<String>,
        /// Base64-encoded file data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        base64: Option<String>,
        /// Reference to the file in an external file storage system.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// MIME type of the file. Required for base64 data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mime_type: Option<String>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        // Legacy fields.
        /// Legacy: inline file data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file: Option<String>,
        /// Legacy: source type.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        source_type: Option<String>,
        /// Legacy: media type alias.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Plain text document data (e.g., `.txt` or `.md`).
    #[serde(rename = "text-plain")]
    PlainText {
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// URL of the plaintext.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        url: Option<String>,
        /// Base64-encoded data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        base64: Option<String>,
        /// Reference to the file in an external file storage system.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// MIME type. Should be "text/plain".
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mime_type: Option<String>,
        /// Plaintext content.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        text: Option<String>,
        /// Title of the text data.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Context for the text (description or summary).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        context: Option<String>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Chain-of-thought reasoning output from a model.
    Reasoning {
        /// The reasoning text (thought summary or raw reasoning).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        reasoning: Option<String>,
        /// Optional unique identifier for this block.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// Index of block in aggregate response (streaming).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Redacted thinking/reasoning block (e.g., Anthropic's redacted thinking).
    RedactedThinking {
        /// Opaque data representing the redacted thinking.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        data: Option<String>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Cache control marker (e.g., Anthropic's cache control).
    CacheControl {
        /// Cache control type (e.g., "ephemeral").
        #[serde(default, skip_serializing_if = "Option::is_none")]
        cache_type: Option<String>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// An AI's request to call a tool.
    ToolCall {
        name: String,
        args: Value,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// A chunk of a tool call (yielded during streaming).
    ToolCallChunk {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        args: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// A tool call that failed to parse.
    InvalidToolCall {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        args: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        error: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// A tool call that is executed server-side.
    ServerToolCall {
        name: String,
        args: Value,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// A chunk of a server-side tool call (streaming).
    ServerToolCallChunk {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        args: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Result of a server-side tool call.
    ServerToolResult {
        /// ID of the corresponding server tool call.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        tool_call_id: Option<String>,
        /// Execution status ("success" or "error").
        #[serde(default, skip_serializing_if = "Option::is_none")]
        status: Option<String>,
        /// Output of the executed tool.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        output: Option<Value>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Tool result content (tool_use_id based, e.g., for Anthropic).
    ToolResult {
        /// The tool_use_id this result corresponds to.
        tool_use_id: String,
        /// The result content.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        content: Option<Value>,
        /// Whether this result represents an error.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
        /// Provider-specific metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Arbitrary data payload.
    Data {
        data: Value,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extras: Option<Value>,
    },

    /// Provider-specific content that does not fit standard types.
    #[serde(rename = "non_standard")]
    NonStandard {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        value: Value,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        index: Option<usize>,
    },
}

/// Image URL info for OpenAI-style image_url blocks.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImageUrlInfo {
    /// The URL of the image.
    pub url: String,
    /// Detail level ("auto", "low", "high").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

impl ContentBlock {
    /// Create a text-only content block with no metadata.
    pub fn text_only(text: impl Into<String>) -> Self {
        Self::Text {
            text: text.into(),
            id: None,
            annotations: None,
            index: None,
            extras: None,
        }
    }
}

/// Known content block type strings.
pub const KNOWN_BLOCK_TYPES: &[&str] = &[
    "text",
    "reasoning",
    "redacted_thinking",
    "cache_control",
    "tool_call",
    "invalid_tool_call",
    "tool_call_chunk",
    "tool_result",
    "image",
    "image_url",
    "audio",
    "file",
    "text-plain",
    "video",
    "server_tool_call",
    "server_tool_call_chunk",
    "server_tool_result",
    "data",
    "non_standard",
];

/// Check if a content block type string is a known data content block type.
pub fn is_data_content_block_type(block_type: &str) -> bool {
    matches!(
        block_type,
        "image" | "video" | "audio" | "text-plain" | "file"
    )
}