agnt5-sdk-core 0.1.2

High-performance Rust core for AGNT5 SDK
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use crate::error::Result;
use opentelemetry::trace::TraceContextExt;
use opentelemetry::Context;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{oneshot, Mutex};

#[derive(Debug, Clone)]
pub struct InvocationRequest {
    pub run_id: String,
    pub service_name: String,
    pub handler_name: String,
    pub input_data: Vec<u8>,
    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Clone)]
pub struct InvocationResponse {
    pub run_id: String,
    pub output_data: Vec<u8>,
    pub success: bool,
    pub error_message: Option<String>,
    pub metadata: HashMap<String, String>,
}

pub struct StreamingInvocationRequest {}

pub struct StreamingInvocationResponse {}

#[async_trait::async_trait]
pub trait StateManager: Send + Sync {
    async fn get(&self, key: String) -> Result<Vec<u8>>;
    async fn set(&self, key: String, value: Vec<u8>) -> Result<()>;
    async fn delete(&self, key: String) -> Result<()>;
}

pub struct RuntimeContext {
    pub run_id: String,
    pub service_name: String,
    pub component_name: String,
    pub tenant_id: String,
    pub deployment_id: String,
    pub metadata: HashMap<String, String>,
    pub state_manager: Arc<dyn StateManager>,

    // OpenTelemetry context for trace propagation
    pub otel_context: Option<Context>,

    // Pre-extracted correlation IDs for easy access in logging
    pub trace_id: Option<String>,
    pub span_id: Option<String>,

    // Whether this is a streaming request (for real-time SSE journal export)
    pub is_streaming: bool,
}

impl RuntimeContext {
    /// Create a new RuntimeContext with OpenTelemetry trace context
    pub fn with_trace_context(
        run_id: String,
        service_name: String,
        component_name: String,
        tenant_id: String,
        deployment_id: String,
        metadata: HashMap<String, String>,
        otel_context: Context,
        state_manager: Arc<dyn StateManager>,
        is_streaming: bool,
    ) -> Self {
        // Extract trace_id and span_id for logging correlation
        let (trace_id, span_id) = extract_trace_ids(&otel_context);

        Self {
            run_id,
            service_name,
            component_name,
            tenant_id,
            deployment_id,
            metadata,
            state_manager,
            otel_context: Some(otel_context),
            trace_id,
            span_id,
            is_streaming,
        }
    }

    /// Create a basic RuntimeContext without OpenTelemetry context
    pub fn new(
        run_id: String,
        service_name: String,
        component_name: String,
        tenant_id: String,
        deployment_id: String,
        metadata: HashMap<String, String>,
        state_manager: Arc<dyn StateManager>,
    ) -> Self {
        Self {
            run_id,
            service_name,
            component_name,
            tenant_id,
            deployment_id,
            metadata,
            state_manager,
            otel_context: None,
            trace_id: None,
            span_id: None,
            is_streaming: false,
        }
    }
}

/// Extract trace_id and span_id from OpenTelemetry context for logging
fn extract_trace_ids(ctx: &Context) -> (Option<String>, Option<String>) {
    let span = ctx.span();
    let span_ctx = span.span_context();

    if span_ctx.is_valid() {
        (
            Some(span_ctx.trace_id().to_string()),
            Some(span_ctx.span_id().to_string()),
        )
    } else {
        (None, None)
    }
}

pub struct RuntimeCapabilities {
    pub supports_websockets: bool,
    pub supports_sse: bool,
    pub supports_bidirectional: bool,
    pub max_payload_size: usize,
    pub timeout_seconds: u32,
}

#[async_trait::async_trait]
pub trait RuntimeAdapter: Send + Sync {
    async fn handle_request(
        &self,
        ctx: &RuntimeContext,
        request: InvocationRequest,
    ) -> Result<InvocationResponse>;

    async fn handle_stream(
        &self,
        ctx: &RuntimeContext,
        request: StreamingInvocationRequest,
    ) -> Result<StreamingInvocationResponse>;
}

/// Dummy state manager for basic testing
pub struct DummyStateManager;

#[async_trait::async_trait]
impl StateManager for DummyStateManager {
    async fn get(&self, _key: String) -> Result<Vec<u8>> {
        Err(crate::error::SdkError::Other(anyhow::anyhow!(
            "State management not implemented"
        )))
    }

    async fn set(&self, _key: String, _value: Vec<u8>) -> Result<()> {
        Err(crate::error::SdkError::Other(anyhow::anyhow!(
            "State management not implemented"
        )))
    }

    async fn delete(&self, _key: String) -> Result<()> {
        Err(crate::error::SdkError::Other(anyhow::anyhow!(
            "State management not implemented"
        )))
    }
}

/// Entity state load result
#[derive(Debug, Clone)]
pub struct EntityStateLoadResult {
    pub found: bool,
    pub state_json: Vec<u8>,
    pub version: i64,
}

/// Entity state save result
#[derive(Debug, Clone)]
pub struct EntityStateSaveResult {
    pub new_version: i64,
}

/// Entity state manager that communicates with Worker Coordinator via gRPC
/// Implements bulk load/save pattern for entity state operations
pub struct EntityStateManager {
    stream_sender: flume::Sender<crate::pb::ServiceMessage>,
    pending_requests:
        Arc<Mutex<HashMap<String, oneshot::Sender<crate::pb::RuntimeServiceResponse>>>>,
    _tenant_id: String,
    session_id: String,
}

impl EntityStateManager {
    /// Create a new EntityStateManager with stream access
    pub fn new(
        stream_sender: flume::Sender<crate::pb::ServiceMessage>,
        tenant_id: String,
        session_id: String,
    ) -> Self {
        Self {
            stream_sender,
            pending_requests: Arc::new(Mutex::new(HashMap::new())),
            _tenant_id: tenant_id,
            session_id,
        }
    }

    /// Handle RuntimeServiceResponse from the worker stream
    /// This should be called by the worker when it receives a RuntimeServiceResponse
    pub async fn handle_response(&self, response: crate::pb::RuntimeServiceResponse) {
        let request_id = response.request_id.clone();

        let mut pending = self.pending_requests.lock().await;
        if let Some(sender) = pending.remove(&request_id) {
            let _ = sender.send(response);
        }
    }

    /// Load entire entity state from platform
    pub async fn load_state(
        &self,
        entity_type: String,
        entity_key: String,
    ) -> Result<EntityStateLoadResult> {
        use crate::pb::{
            runtime_service_request, EntityStateLoadRequest, RuntimeServiceRequest, ServiceMessage,
        };

        // Generate unique request ID
        let request_id = uuid::Uuid::new_v4().to_string();

        // Create oneshot channel for response
        let (response_tx, response_rx) = oneshot::channel();

        // Store in pending requests
        self.pending_requests
            .lock()
            .await
            .insert(request_id.clone(), response_tx);

        // Create RuntimeServiceRequest
        let request = RuntimeServiceRequest {
            request_id: request_id.clone(),
            session_id: self.session_id.clone(),
            operation: Some(runtime_service_request::Operation::EntityStateLoad(
                EntityStateLoadRequest {
                    entity_type,
                    entity_key,
                    scope: String::new(),    // Default to global scope
                    scope_id: String::new(), // Empty for global scope
                },
            )),
        };

        // Send via worker stream
        let message = ServiceMessage {
            worker_id: String::new(), // Will be filled by worker
            metadata: HashMap::new(),
            message_type: Some(crate::pb::service_message::MessageType::RuntimeService(
                request,
            )),
        };

        self.stream_sender.send_async(message).await.map_err(|e| {
            crate::error::SdkError::Connection {
                message: format!("Failed to send load request: {}", e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            }
        })?;

        // Wait for response with timeout
        let response = tokio::time::timeout(std::time::Duration::from_secs(10), response_rx)
            .await
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Entity state load timeout"))
            })?
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Response channel closed"))
            })?;

        // Check if request succeeded
        if !response.success {
            return Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Entity state load failed: {}",
                response.error_message
            )));
        }

        // Extract result
        match response.result {
            Some(crate::pb::runtime_service_response::Result::EntityStateLoad(result)) => {
                Ok(EntityStateLoadResult {
                    found: result.found,
                    state_json: result.state_json,
                    version: result.version,
                })
            }
            _ => Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Unexpected response type for entity state load"
            ))),
        }
    }

    /// Save entire entity state to platform with optimistic locking
    pub async fn save_state(
        &self,
        entity_type: String,
        entity_key: String,
        state_json: Vec<u8>,
        expected_version: i64,
    ) -> Result<EntityStateSaveResult> {
        use crate::pb::{
            runtime_service_request, EntityStateSaveRequest, RuntimeServiceRequest, ServiceMessage,
        };

        // Generate unique request ID
        let request_id = uuid::Uuid::new_v4().to_string();

        // Create oneshot channel for response
        let (response_tx, response_rx) = oneshot::channel();

        // Store in pending requests
        self.pending_requests
            .lock()
            .await
            .insert(request_id.clone(), response_tx);

        // Create RuntimeServiceRequest
        let request = RuntimeServiceRequest {
            request_id: request_id.clone(),
            session_id: self.session_id.clone(),
            operation: Some(runtime_service_request::Operation::EntityStateSave(
                EntityStateSaveRequest {
                    entity_type,
                    entity_key,
                    state_json,
                    expected_version,
                    scope: String::new(),    // Default to global scope
                    scope_id: String::new(), // Empty for global scope
                },
            )),
        };

        // Send via worker stream
        let message = ServiceMessage {
            worker_id: String::new(), // Will be filled by worker
            metadata: HashMap::new(),
            message_type: Some(crate::pb::service_message::MessageType::RuntimeService(
                request,
            )),
        };

        self.stream_sender.send_async(message).await.map_err(|e| {
            crate::error::SdkError::Connection {
                message: format!("Failed to send save request: {}", e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            }
        })?;

        // Wait for response with timeout
        let response = tokio::time::timeout(std::time::Duration::from_secs(10), response_rx)
            .await
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Entity state save timeout"))
            })?
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Response channel closed"))
            })?;

        // Check if request succeeded
        if !response.success {
            return Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Entity state save failed: {}",
                response.error_message
            )));
        }

        // Extract result
        match response.result {
            Some(crate::pb::runtime_service_response::Result::EntityStateSave(result)) => {
                Ok(EntityStateSaveResult {
                    new_version: result.new_version,
                })
            }
            _ => Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Unexpected response type for entity state save"
            ))),
        }
    }

    // -------------------------------------------------------------------
    // Session / Message operations
    // -------------------------------------------------------------------

    /// Send a message to a conversation thread
    pub async fn send_message(
        &self,
        correlation_id: String,
        message_type: String,
        payload: Vec<u8>,
        from_service: String,
    ) -> Result<String> {
        use crate::pb::{
            runtime_service_request, MessageSendRequest, RuntimeServiceRequest, ServiceMessage,
        };

        let request_id = uuid::Uuid::new_v4().to_string();
        let (response_tx, response_rx) = oneshot::channel();
        self.pending_requests
            .lock()
            .await
            .insert(request_id.clone(), response_tx);

        let request = RuntimeServiceRequest {
            request_id: request_id.clone(),
            session_id: self.session_id.clone(),
            operation: Some(runtime_service_request::Operation::MessageSend(
                MessageSendRequest {
                    correlation_id,
                    message_type,
                    payload,
                    from_service,
                },
            )),
        };

        let message = ServiceMessage {
            worker_id: String::new(),
            metadata: HashMap::new(),
            message_type: Some(crate::pb::service_message::MessageType::RuntimeService(
                request,
            )),
        };

        self.stream_sender.send_async(message).await.map_err(|e| {
            crate::error::SdkError::Connection {
                message: format!("Failed to send message request: {}", e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            }
        })?;

        let response = tokio::time::timeout(std::time::Duration::from_secs(10), response_rx)
            .await
            .map_err(|_| crate::error::SdkError::Other(anyhow::anyhow!("Message send timeout")))?
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Response channel closed"))
            })?;

        if !response.success {
            return Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Message send failed: {}",
                response.error_message
            )));
        }

        match response.result {
            Some(crate::pb::runtime_service_response::Result::MessageSend(result)) => {
                Ok(result.message_id)
            }
            _ => Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Unexpected response type for message send"
            ))),
        }
    }

    /// List messages in a conversation by correlation ID
    pub async fn list_messages(&self, correlation_id: String, limit: i32) -> Result<Vec<Vec<u8>>> {
        use crate::pb::{
            runtime_service_request, MessageListRequest, RuntimeServiceRequest, ServiceMessage,
        };

        let request_id = uuid::Uuid::new_v4().to_string();
        let (response_tx, response_rx) = oneshot::channel();
        self.pending_requests
            .lock()
            .await
            .insert(request_id.clone(), response_tx);

        let request = RuntimeServiceRequest {
            request_id: request_id.clone(),
            session_id: self.session_id.clone(),
            operation: Some(runtime_service_request::Operation::MessageList(
                MessageListRequest {
                    correlation_id,
                    limit,
                    after_message_id: String::new(),
                },
            )),
        };

        let message = ServiceMessage {
            worker_id: String::new(),
            metadata: HashMap::new(),
            message_type: Some(crate::pb::service_message::MessageType::RuntimeService(
                request,
            )),
        };

        self.stream_sender.send_async(message).await.map_err(|e| {
            crate::error::SdkError::Connection {
                message: format!("Failed to send message list request: {}", e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            }
        })?;

        let response = tokio::time::timeout(std::time::Duration::from_secs(10), response_rx)
            .await
            .map_err(|_| crate::error::SdkError::Other(anyhow::anyhow!("Message list timeout")))?
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Response channel closed"))
            })?;

        if !response.success {
            return Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Message list failed: {}",
                response.error_message
            )));
        }

        match response.result {
            Some(crate::pb::runtime_service_response::Result::MessageList(result)) => {
                Ok(result.messages)
            }
            _ => Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Unexpected response type for message list"
            ))),
        }
    }

    /// Create a session
    pub async fn create_session(
        &self,
        session_id: String,
        component_name: String,
        session_type: String,
    ) -> Result<String> {
        use crate::pb::{
            runtime_service_request, RuntimeServiceRequest, ServiceMessage, SessionCreateRequest,
        };

        let request_id = uuid::Uuid::new_v4().to_string();
        let (response_tx, response_rx) = oneshot::channel();
        self.pending_requests
            .lock()
            .await
            .insert(request_id.clone(), response_tx);

        let request = RuntimeServiceRequest {
            request_id: request_id.clone(),
            session_id: self.session_id.clone(),
            operation: Some(runtime_service_request::Operation::SessionCreate(
                SessionCreateRequest {
                    session_id,
                    component_name,
                    session_type,
                    state: vec![],
                    metadata: vec![],
                    expires_at_ns: 0,
                },
            )),
        };

        let message = ServiceMessage {
            worker_id: String::new(),
            metadata: HashMap::new(),
            message_type: Some(crate::pb::service_message::MessageType::RuntimeService(
                request,
            )),
        };

        self.stream_sender.send_async(message).await.map_err(|e| {
            crate::error::SdkError::Connection {
                message: format!("Failed to send session create request: {}", e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            }
        })?;

        let response = tokio::time::timeout(std::time::Duration::from_secs(10), response_rx)
            .await
            .map_err(|_| crate::error::SdkError::Other(anyhow::anyhow!("Session create timeout")))?
            .map_err(|_| {
                crate::error::SdkError::Other(anyhow::anyhow!("Response channel closed"))
            })?;

        if !response.success {
            return Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Session create failed: {}",
                response.error_message
            )));
        }

        match response.result {
            Some(crate::pb::runtime_service_response::Result::SessionCreate(result)) => {
                Ok(result.session_id)
            }
            _ => Err(crate::error::SdkError::Other(anyhow::anyhow!(
                "Unexpected response type for session create"
            ))),
        }
    }
}