context.rs 2.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//! Context for /v1/responses endpoint handlers
//!
//! Bundles all dependencies needed by responses handlers to avoid passing
//! 10+ parameters to every function (fixes clippy::too_many_arguments).

use std::{collections::HashMap, sync::Arc};

use tokio::sync::RwLock;

use super::types::BackgroundTaskInfo;
use crate::{
    core::WorkerRegistry,
13
    data_connector::{ConversationItemStorage, ConversationStorage, ResponseStorage},
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    mcp::McpManager,
    routers::grpc::{context::SharedComponents, pipeline::RequestPipeline},
};

/// Context for /v1/responses endpoint
///
/// All fields are Arc/shared references, so cloning this context is cheap.
#[derive(Clone)]
pub struct ResponsesContext {
    /// Chat pipeline for executing requests
    pub pipeline: Arc<RequestPipeline>,

    /// Shared components (tokenizer, parsers, worker_registry)
    pub components: Arc<SharedComponents>,

    /// Worker registry for validation
    pub worker_registry: Arc<WorkerRegistry>,

    /// Response storage backend
33
    pub response_storage: Arc<dyn ResponseStorage>,
34
35

    /// Conversation storage backend
36
    pub conversation_storage: Arc<dyn ConversationStorage>,
37
38

    /// Conversation item storage backend
39
    pub conversation_item_storage: Arc<dyn ConversationItemStorage>,
40
41
42
43
44
45
46
47
48
49
50
51
52
53

    /// MCP manager for tool support
    pub mcp_manager: Arc<McpManager>,

    /// Background task handles for cancellation support
    pub background_tasks: Arc<RwLock<HashMap<String, BackgroundTaskInfo>>>,
}

impl ResponsesContext {
    /// Create a new responses context
    pub fn new(
        pipeline: Arc<RequestPipeline>,
        components: Arc<SharedComponents>,
        worker_registry: Arc<WorkerRegistry>,
54
55
56
        response_storage: Arc<dyn ResponseStorage>,
        conversation_storage: Arc<dyn ConversationStorage>,
        conversation_item_storage: Arc<dyn ConversationItemStorage>,
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        mcp_manager: Arc<McpManager>,
    ) -> Self {
        Self {
            pipeline,
            components,
            worker_registry,
            response_storage,
            conversation_storage,
            conversation_item_storage,
            mcp_manager,
            background_tasks: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}