95 lines
3.0 KiB
Rust
95 lines
3.0 KiB
Rust
//! Extension prompt placement with content and producer attribution kept together.
|
|
|
|
use akode_context_fragments::AnnotatedContent;
|
|
use akode_context_fragments::ContextualUserFragment;
|
|
use akode_context_fragments::RenderedFragment;
|
|
use akode_protocol::models::ContentItem;
|
|
use akode_protocol::models::ContentItemKind;
|
|
use akode_protocol::models::ContentItemMetadata;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub enum PromptSlot {
|
|
DeveloperPolicy,
|
|
DeveloperCapabilities,
|
|
/// Content inside the context-window message, supplied by `contribute_thread_context`.
|
|
ContextWindow,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct PromptFragment {
|
|
slot: PromptSlot,
|
|
content: AnnotatedContent,
|
|
}
|
|
|
|
impl PromptFragment {
|
|
/// Places a contextual fragment in the prompt, preserving its content, kind, and source.
|
|
/// The slot determines placement within the developer message, regardless of its original role.
|
|
pub fn from_fragment(slot: PromptSlot, fragment: &impl ContextualUserFragment) -> Self {
|
|
Self {
|
|
slot,
|
|
content: fragment.render_fragment().into_parts().1,
|
|
}
|
|
}
|
|
|
|
/// Creates harness-provided context for the given slot.
|
|
pub fn new(slot: PromptSlot, text: impl Into<String>, content_kind: ContentItemKind) -> Self {
|
|
Self {
|
|
slot,
|
|
content: AnnotatedContent::text(text, content_kind, ContentItemMetadata::harness()),
|
|
}
|
|
}
|
|
|
|
/// Creates tool-supplied context without changing its prompt placement.
|
|
pub fn tool(
|
|
slot: PromptSlot,
|
|
text: impl Into<String>,
|
|
content_kind: ContentItemKind,
|
|
namespace: String,
|
|
) -> Self {
|
|
Self {
|
|
slot,
|
|
content: AnnotatedContent::text(
|
|
text,
|
|
content_kind,
|
|
ContentItemMetadata::tool(Some(namespace.into())),
|
|
),
|
|
}
|
|
}
|
|
|
|
/// Creates a developer-policy prompt fragment.
|
|
pub fn developer_policy(text: impl Into<String>, content_kind: ContentItemKind) -> Self {
|
|
Self::new(PromptSlot::DeveloperPolicy, text, content_kind)
|
|
}
|
|
|
|
/// Creates a developer-capabilities prompt fragment.
|
|
pub fn developer_capability(text: impl Into<String>, content_kind: ContentItemKind) -> Self {
|
|
Self::new(PromptSlot::DeveloperCapabilities, text, content_kind)
|
|
}
|
|
|
|
/// Returns the target prompt slot.
|
|
pub fn slot(&self) -> PromptSlot {
|
|
self.slot
|
|
}
|
|
|
|
/// Returns the model-visible content.
|
|
pub fn content(&self) -> &ContentItem {
|
|
self.content.content()
|
|
}
|
|
|
|
/// Returns the producer-owned classification of the model-visible content.
|
|
pub fn content_kind(&self) -> &ContentItemKind {
|
|
self.content.kind()
|
|
}
|
|
|
|
/// Consumes the placement wrapper when assembling a shared context message.
|
|
pub fn into_content(self) -> AnnotatedContent {
|
|
self.content
|
|
}
|
|
}
|
|
|
|
impl From<PromptFragment> for RenderedFragment {
|
|
fn from(fragment: PromptFragment) -> Self {
|
|
Self::new("developer", fragment.content)
|
|
}
|
|
}
|