Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/contextforge-data-plane-cpex/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,12 @@ mod tests {
}

impl HookHandler<CmfHook> for TestPlugin {
async fn handle(
fn handle(
&self,
payload: &MessagePayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> PluginResult<MessagePayload> {
) -> impl std::future::Future<Output = PluginResult<MessagePayload>> {
let is_post = payload.message.role == Role::Tool;
let mut observations = self.observations.lock().expect("observations lock poisoned");
if is_post {
Expand All @@ -529,7 +529,7 @@ mod tests {
}
drop(observations);

if is_post {
std::future::ready(if is_post {
match self.post_behavior {
PostBehavior::Allow => PluginResult::allow(),
PostBehavior::Rewrite => PluginResult::modify_payload(payload.clone()),
Expand Down Expand Up @@ -604,7 +604,7 @@ mod tests {
PluginResult::allow()
},
}
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ impl ServerHandler for PaginatingServer {
.with_protocol_version(ProtocolVersion::V_2024_11_05)
}

async fn list_tools(
fn list_tools(
&self,
request: Option<PaginatedRequestParams>,
_: RequestContext<RoleServer>,
) -> Result<ListToolsResult, McpError> {
if request.as_ref().and_then(|r| r.cursor.as_deref()) == Some(PAGE2_CURSOR) {
) -> impl std::future::Future<Output = Result<ListToolsResult, McpError>> {
std::future::ready(if request.as_ref().and_then(|r| r.cursor.as_deref()) == Some(PAGE2_CURSOR) {
Ok(ListToolsResult::with_all_items(Self::page2_tools()))
} else {
let mut result = ListToolsResult::with_all_items(Self::page1_tools());
result.next_cursor = Some(PAGE2_CURSOR.to_owned());
Ok(result)
}
})
}
}
22 changes: 11 additions & 11 deletions crates/contextforge-data-plane-lib/tests/support/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ impl Plugin for TestPlugin {
}

impl HookHandler<CmfHook> for TestPlugin {
async fn handle(
fn handle(
&self,
payload: &MessagePayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> PluginResult<MessagePayload> {
) -> impl std::future::Future<Output = PluginResult<MessagePayload>> {
let is_post = payload.message.role == Role::Tool;
let mut observations = self.observations.lock().expect("observations lock poisoned");
if is_post {
Expand All @@ -180,7 +180,7 @@ impl HookHandler<CmfHook> for TestPlugin {
}
drop(observations);

if is_post {
std::future::ready(if is_post {
match self.post_behavior {
PostBehavior::Allow => PluginResult::allow(),
PostBehavior::Rewrite => {
Expand All @@ -190,7 +190,7 @@ impl HookHandler<CmfHook> for TestPlugin {
modified.message.content.iter_mut().find(|part| matches!(part, ContentPart::ToolResult { .. }))
{
if !is_tool_result_content(&content.content) {
return PluginResult::allow();
return std::future::ready(PluginResult::allow());
}
content.content = serde_json::to_value(CallToolResult::success(vec![ContentBlock::text(
format!("post:{result_text}"),
Expand All @@ -205,7 +205,7 @@ impl HookHandler<CmfHook> for TestPlugin {
modified.message.content.iter_mut().find(|part| matches!(part, ContentPart::ToolResult { .. }))
{
if !is_tool_result_content(&content.content) {
return PluginResult::allow();
return std::future::ready(PluginResult::allow());
}
content.content = json!("raw-post");
}
Expand All @@ -220,7 +220,7 @@ impl HookHandler<CmfHook> for TestPlugin {
{
progress.message = progress.message.map(|message| format!("plugin:{message}"));
content.content = serde_json::to_value(progress).expect("progress serializes");
return PluginResult::modify_payload(modified);
return std::future::ready(PluginResult::modify_payload(modified));
}
PluginResult::allow()
},
Expand Down Expand Up @@ -279,7 +279,7 @@ impl HookHandler<CmfHook> for TestPlugin {
PluginResult::allow()
},
}
}
})
}
}

Expand Down Expand Up @@ -492,17 +492,17 @@ impl Plugin for PromptTestPlugin {
}

impl HookHandler<CmfHook> for PromptTestPlugin {
async fn handle(
fn handle(
&self,
payload: &MessagePayload,
_extensions: &Extensions,
ctx: &mut PluginContext,
) -> PluginResult<MessagePayload> {
if payload.message.get_prompt_results().is_empty() {
) -> impl std::future::Future<Output = PluginResult<MessagePayload>> {
std::future::ready(if payload.message.get_prompt_results().is_empty() {
self.handle_pre(payload, ctx)
} else {
self.handle_post(payload, ctx)
}
})
}
}

Expand Down
24 changes: 15 additions & 9 deletions crates/contextforge-data-plane-lib/tests/support/plugin_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,22 @@ struct TestBackend {
}

impl ServerHandler for TestBackend {
async fn initialize(
fn initialize(
&self,
_request: InitializeRequestParams,
_cx: RequestContext<RoleServer>,
) -> Result<InitializeResult, ErrorData> {
Ok(InitializeResult::new(ServerCapabilities::builder().enable_tools().enable_prompts().build())
.with_server_info(Implementation::new("test-backend", "0.1.0")))
) -> impl std::future::Future<Output = Result<InitializeResult, ErrorData>> {
std::future::ready(Ok(InitializeResult::new(
ServerCapabilities::builder().enable_tools().enable_prompts().build(),
)
.with_server_info(Implementation::new("test-backend", "0.1.0"))))
}

async fn get_prompt(
fn get_prompt(
&self,
request: GetPromptRequestParams,
_cx: RequestContext<RoleServer>,
) -> Result<GetPromptResponse, ErrorData> {
) -> impl std::future::Future<Output = Result<GetPromptResponse, ErrorData>> {
self.state
.prompts
.lock()
Expand All @@ -87,18 +89,22 @@ impl ServerHandler for TestBackend {
.and_then(Value::as_str)
.unwrap_or("nothing");
if request.name == "review_bundle" {
return Ok(GetPromptResult::new(vec![
return std::future::ready(Ok(GetPromptResult::new(vec![
PromptMessage::new_text(Role::User, format!("review of {topic}")),
PromptMessage::new(
Role::User,
ContentBlock::resource(ResourceContents::text(BACKEND_PROMPT_RESOURCE, "file:///app.env")),
),
PromptMessage::new(Role::Assistant, ContentBlock::image(BACKEND_PROMPT_IMAGE, "image/png")),
])
.into());
.into()));
}

Ok(GetPromptResult::new(vec![PromptMessage::new_text(Role::User, format!("review of {topic}"))]).into())
std::future::ready(Ok(GetPromptResult::new(vec![PromptMessage::new_text(
Role::User,
format!("review of {topic}"),
)])
.into()))
}

async fn call_tool(
Expand Down
10 changes: 5 additions & 5 deletions crates/plugins/cpex-secrets-detection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,33 @@ impl Plugin for StageHandler {
}

impl HookHandler<CmfHook> for StageHandler {
async fn handle(
fn handle(
&self,
payload: &MessagePayload,
extensions: &Extensions,
_ctx: &mut PluginContext,
) -> PluginResult<MessagePayload> {
) -> impl std::future::Future<Output = PluginResult<MessagePayload>> {
let scan = self.scan_payload(payload);

if self.core.should_block(scan.count) {
let violation = build_violation(self.stage, scan.count, &scan.findings);
let mut result = PluginResult::deny(violation);
result.modified_payload = scan.modified_payload.or_else(|| Some(payload.clone()));
attach_metrics(&mut result, extensions, scan.count, &scan.findings, DetectionOutcome::Blocked);
return result;
return std::future::ready(result);
}

if let Some(modified_payload) = scan.modified_payload {
let mut result = PluginResult::modify_payload(modified_payload);
attach_metrics(&mut result, extensions, scan.count, &scan.findings, DetectionOutcome::Masked);
return result;
return std::future::ready(result);
}

let mut result = PluginResult::allow();
if scan.count > 0 {
attach_metrics(&mut result, extensions, scan.count, &scan.findings, DetectionOutcome::None);
}
result
std::future::ready(result)
}
}

Expand Down