aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
177
akode-rs/context-fragments/src/fragment.rs
Normal file
177
akode-rs/context-fragments/src/fragment.rs
Normal file
@@ -0,0 +1,177 @@
|
||||
use crate::AnnotatedContent;
|
||||
use crate::message_from_parts;
|
||||
use akode_protocol::models::ContentItem;
|
||||
use akode_protocol::models::ContentItemKind;
|
||||
use akode_protocol::models::ContentItemMetadata;
|
||||
use akode_protocol::models::ResponseItem;
|
||||
use akode_protocol::models::input_text;
|
||||
|
||||
/// A rendered contextual fragment and the role that owns its annotated content.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct RenderedFragment {
|
||||
role: &'static str,
|
||||
content: AnnotatedContent,
|
||||
}
|
||||
|
||||
impl RenderedFragment {
|
||||
/// Creates a rendered fragment without separating its role and annotated content.
|
||||
pub fn new(role: &'static str, content: AnnotatedContent) -> Self {
|
||||
Self { role, content }
|
||||
}
|
||||
|
||||
/// Returns the response role associated with this fragment.
|
||||
pub fn role(&self) -> &'static str {
|
||||
self.role
|
||||
}
|
||||
|
||||
/// Returns this fragment's model-visible content and classification.
|
||||
pub fn annotated_content(&self) -> &AnnotatedContent {
|
||||
&self.content
|
||||
}
|
||||
|
||||
/// Separates the role and annotated content at an API boundary.
|
||||
pub fn into_parts(self) -> (&'static str, AnnotatedContent) {
|
||||
(self.role, self.content)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RenderedFragment> for ResponseItem {
|
||||
fn from(fragment: RenderedFragment) -> Self {
|
||||
let (role, annotated_content) = fragment.into_parts();
|
||||
message_from_parts(role, vec![annotated_content])
|
||||
}
|
||||
}
|
||||
|
||||
/// Context payload that is injected as a message fragment.
|
||||
///
|
||||
/// Implementations own the response role and provide the exact fragment body.
|
||||
/// Marked fragments also provide start/end markers used to recognize injected
|
||||
/// context later. `render()` concatenates markers and `InputText` bodies without
|
||||
/// adding separators, so implementations should include any whitespace they need
|
||||
/// between tags in `body()`. All other content passes through unchanged. Unmarked
|
||||
/// fragments should leave both markers empty and never match arbitrary text.
|
||||
/// Producers must enforce size bounds for all model-visible content before rendering.
|
||||
/// User-role non-text content is currently classified as ordinary user media because
|
||||
/// contextual-user-message recognition relies on text markers.
|
||||
pub trait ContextualUserFragment {
|
||||
fn role(&self) -> &'static str;
|
||||
|
||||
/// Attribution is supplied when the fragment is produced, never inferred from rendered text.
|
||||
fn content_metadata(&self) -> ContentItemMetadata {
|
||||
ContentItemMetadata::harness()
|
||||
}
|
||||
|
||||
/// Binds producer attribution before this fragment is rendered or persisted.
|
||||
fn with_metadata(self, metadata: ContentItemMetadata) -> AttributedFragment<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
AttributedFragment {
|
||||
fragment: self,
|
||||
metadata,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a stable `<feature>.<name>` classification, using `generic` for shared fragments.
|
||||
fn content_kind(&self) -> ContentItemKind;
|
||||
|
||||
fn markers(&self) -> (&'static str, &'static str);
|
||||
|
||||
fn body(&self) -> ContentItem;
|
||||
|
||||
fn type_markers() -> (&'static str, &'static str)
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn matches_text(text: &str) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let (start_marker, end_marker) = Self::type_markers();
|
||||
matches_marked_text(start_marker, end_marker, text)
|
||||
}
|
||||
|
||||
fn render(&self) -> ContentItem {
|
||||
let (start_marker, end_marker) = self.markers();
|
||||
let body = self.body();
|
||||
if start_marker.is_empty() && end_marker.is_empty() {
|
||||
return body;
|
||||
}
|
||||
|
||||
match body {
|
||||
ContentItem::InputText { text } => {
|
||||
input_text(format!("{start_marker}{text}{end_marker}"))
|
||||
}
|
||||
content => content,
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the role, model-visible content, and classification together.
|
||||
fn render_fragment(&self) -> RenderedFragment {
|
||||
RenderedFragment::new(
|
||||
self.role(),
|
||||
AnnotatedContent::from_parts(
|
||||
self.render(),
|
||||
self.content_kind(),
|
||||
self.content_metadata(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn into(self) -> ResponseItem
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
ResponseItem::from(self.render_fragment())
|
||||
}
|
||||
|
||||
fn into_boxed_response_item(self: Box<Self>) -> ResponseItem {
|
||||
ResponseItem::from(self.render_fragment())
|
||||
}
|
||||
}
|
||||
|
||||
/// A contextual fragment with attribution supplied by its producer.
|
||||
pub struct AttributedFragment<F> {
|
||||
fragment: F,
|
||||
metadata: ContentItemMetadata,
|
||||
}
|
||||
|
||||
impl<F: ContextualUserFragment> ContextualUserFragment for AttributedFragment<F> {
|
||||
fn role(&self) -> &'static str {
|
||||
self.fragment.role()
|
||||
}
|
||||
fn content_kind(&self) -> ContentItemKind {
|
||||
self.fragment.content_kind()
|
||||
}
|
||||
fn markers(&self) -> (&'static str, &'static str) {
|
||||
self.fragment.markers()
|
||||
}
|
||||
fn type_markers() -> (&'static str, &'static str) {
|
||||
F::type_markers()
|
||||
}
|
||||
fn body(&self) -> ContentItem {
|
||||
self.fragment.body()
|
||||
}
|
||||
fn render(&self) -> ContentItem {
|
||||
self.fragment.render()
|
||||
}
|
||||
fn content_metadata(&self) -> ContentItemMetadata {
|
||||
self.metadata.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn matches_marked_text(start_marker: &str, end_marker: &str, text: &str) -> bool {
|
||||
if start_marker.is_empty() || end_marker.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let trimmed = text.trim_start();
|
||||
let starts_with_marker = trimmed
|
||||
.get(..start_marker.len())
|
||||
.is_some_and(|candidate| candidate.eq_ignore_ascii_case(start_marker));
|
||||
let trimmed = trimmed.trim_end();
|
||||
let ends_with_marker = trimmed
|
||||
.get(trimmed.len().saturating_sub(end_marker.len())..)
|
||||
.is_some_and(|candidate| candidate.eq_ignore_ascii_case(end_marker));
|
||||
starts_with_marker && ends_with_marker
|
||||
}
|
||||
Reference in New Issue
Block a user