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
//! `PromptMiddleware` — what an embedding program says about a user message
//! before it enters the conversation.
//!
//! The Rust counterpart of a `UserPromptSubmit` hook, for rules that live in
//! the embedder's own code: it sees every user message, the one that opens a
//! turn and the ones steered into a running turn at a step boundary, and lets
//! it through, rewrites it, or refuses it. It runs after the hooks configured
//! in settings, in the order the middlewares were added, so the embedder's
//! rules are the last word: nothing configured can rewrite back what they
//! decided.
//!
//! Attachments that came with the message are shown read-only through
//! `inspect_with_attachments`; a middleware that only implements `inspect`
//! never sees them.
use async_trait::async_trait;
use crate::permission::PermissionMode;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptDelivery {
/// The message opens a turn.
Turn,
/// The message joins the running turn at a step boundary.
Steer,
}
impl PromptDelivery {
pub fn as_str(self) -> &'static str {
match self {
Self::Turn => "turn",
Self::Steer => "steer",
}
}
}
#[derive(Debug, Clone)]
pub struct PromptCall<'a> {
pub session_id: &'a str,
pub turn_id: &'a str,
pub content: &'a str,
pub delivery: PromptDelivery,
pub permission_mode: PermissionMode,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PromptDecision {
Continue,
Rewrite(String),
/// A turn ends as a `UserPromptSubmit` block ends it (`stopped_by_hook`,
/// no model call); a steered message is left out and the turn goes on.
Block {
message: Option<String>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptAttachmentKind {
/// A path the runtime reads after screening; its content is not read
/// for the middleware.
File,
/// Text the host already read.
Text,
/// An image the host already decoded.
Image,
/// A document the host already encoded.
Document,
}
impl PromptAttachmentKind {
pub fn as_str(self) -> &'static str {
match self {
Self::File => "file",
Self::Text => "text",
Self::Image => "image",
Self::Document => "document",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct PromptAttachment<'a> {
pub kind: PromptAttachmentKind,
pub name: Option<&'a str>,
pub media_type: Option<&'a str>,
/// Bytes: the file's length on disk, the text's UTF-8 length, the
/// image's decoded length. `None` when a file's metadata can't be read.
pub size: Option<u64>,
pub text: Option<&'a str>,
}
impl<'a> PromptAttachment<'a> {
pub fn file(path: &'a str, media_type: Option<&'a str>, size: Option<u64>) -> Self {
Self {
kind: PromptAttachmentKind::File,
name: Some(path),
media_type,
size,
text: None,
}
}
pub fn text(name: &'a str, content: &'a str) -> Self {
Self {
kind: PromptAttachmentKind::Text,
name: Some(name),
media_type: None,
size: Some(content.len() as u64),
text: Some(content),
}
}
pub fn document(name: Option<&'a str>, media_type: &'a str, size: u64) -> Self {
Self {
kind: PromptAttachmentKind::Document,
name,
media_type: Some(media_type),
size: Some(size),
text: None,
}
}
pub fn image(media_type: &'a str, size: u64) -> Self {
Self {
kind: PromptAttachmentKind::Image,
name: None,
media_type: Some(media_type),
size: Some(size),
text: None,
}
}
}
#[async_trait]
pub trait PromptMiddleware: Send + Sync {
async fn inspect(&self, call: &PromptCall<'_>) -> PromptDecision;
/// What the runtime calls. Attachments can't be rewritten: a `Rewrite`
/// replaces the text only, a `Block` refuses the whole message.
async fn inspect_with_attachments(
&self,
call: &PromptCall<'_>,
attachments: &[PromptAttachment<'_>],
) -> PromptDecision {
let _ = attachments;
self.inspect(call).await
}
}