longbridge 4.4.3

Longbridge OpenAPI SDK for Rust
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
use std::{sync::Arc, time::Duration};

use futures_util::{Stream, StreamExt};
use longbridge_httpcli::{HttpClient, Json, Method};
use serde::{Deserialize, Serialize};
use tracing::{Subscriber, dispatcher, instrument::WithSubscriber};

use crate::{Config, Result, agent::types::*};

/// The shared httpclient default (30s) is tuned for fast REST calls and is
/// too tight here: in blocking mode the server holds the connection silent
/// until the whole LLM turn is done, and that can legitimately take longer.
/// Only agent calls get this longer budget — every other domain keeps the
/// 30s default.
const AGENT_REQUEST_TIMEOUT: Duration = Duration::from_secs(120);

struct InnerAgentContext {
    http_cli: HttpClient,
    log_subscriber: Arc<dyn Subscriber + Send + Sync>,
}

impl Drop for InnerAgentContext {
    fn drop(&mut self) {
        dispatcher::with_default(&self.log_subscriber.clone().into(), || {
            tracing::info!("agent context dropped");
        });
    }
}

/// AI Agent conversation context.
///
/// Reference: <https://open.longbridge.com/en/docs/ai/chat/conversation>
#[derive(Clone)]
pub struct AgentContext(Arc<InnerAgentContext>);

#[derive(Debug, Deserialize)]
struct SseEnvelope {
    event: String,
    #[serde(default)]
    data: serde_json::Value,
    /// Only present on `plan_changed`, as a sibling of `data` rather than a
    /// field inside it — see [`PlanChangedPayload::tool_name`].
    #[serde(default)]
    tool_name: Option<String>,
}

/// Parse one raw SSE frame into a [`ConversationStreamEvent`], threading the
/// `chat_uid`/`message_id` captured from an earlier `chat_started` event (the
/// `workflow_finished` event doesn't repeat them) through `started`.
fn map_conversation_event(
    item: longbridge_httpcli::HttpClientResult<longbridge_httpcli::SseEvent>,
    started: &mut Option<(String, String)>,
) -> Result<ConversationStreamEvent> {
    let event = item?;
    let envelope: SseEnvelope = serde_json::from_str(&event.data)?;
    Ok(match envelope.event.as_str() {
        "chat_started" => {
            let payload: ChatStartedPayload = serde_json::from_value(envelope.data)?;
            *started = Some((payload.chat_uid.clone(), payload.message_id.clone()));
            ConversationStreamEvent::ChatStarted(payload)
        }
        "message" => ConversationStreamEvent::Message(serde_json::from_value(envelope.data)?),
        "workflow_started" => {
            ConversationStreamEvent::WorkflowStarted(serde_json::from_value(envelope.data)?)
        }
        "ping" => ConversationStreamEvent::Ping,
        "thinking_started" => {
            ConversationStreamEvent::ThinkingStarted(serde_json::from_value(envelope.data)?)
        }
        "thinking_finished" => {
            ConversationStreamEvent::ThinkingFinished(serde_json::from_value(envelope.data)?)
        }
        "node_tool_use_started" => {
            ConversationStreamEvent::NodeToolUseStarted(serde_json::from_value(envelope.data)?)
        }
        "node_tool_use_finished" => {
            ConversationStreamEvent::NodeToolUseFinished(serde_json::from_value(envelope.data)?)
        }
        "subagent_started" => {
            ConversationStreamEvent::SubagentStarted(serde_json::from_value(envelope.data)?)
        }
        "subagent_progress" => {
            ConversationStreamEvent::SubagentProgress(serde_json::from_value(envelope.data)?)
        }
        "subagent_finished" => {
            ConversationStreamEvent::SubagentFinished(serde_json::from_value(envelope.data)?)
        }
        "agent_tool_started" => {
            ConversationStreamEvent::AgentToolStarted(serde_json::from_value(envelope.data)?)
        }
        "agent_tool_progress" => {
            ConversationStreamEvent::AgentToolProgress(serde_json::from_value(envelope.data)?)
        }
        "agent_tool_finished" => {
            ConversationStreamEvent::AgentToolFinished(serde_json::from_value(envelope.data)?)
        }
        "human_interaction_required" => {
            let interrupt: Interrupt = serde_json::from_value(envelope.data)?;
            ConversationStreamEvent::HumanInteractionRequired(
                ConversationResponse::from_stream_interrupt(started.clone(), interrupt),
            )
        }
        "query_masked" => {
            ConversationStreamEvent::QueryMasked(serde_json::from_value(envelope.data)?)
        }
        "plan_changed" => {
            let mut payload: PlanChangedPayload = serde_json::from_value(envelope.data)?;
            payload.tool_name = envelope.tool_name.clone().unwrap_or_default();
            ConversationStreamEvent::PlanChanged(payload)
        }
        "context_compress_started" => {
            ConversationStreamEvent::ContextCompressStarted(serde_json::from_value(envelope.data)?)
        }
        "context_compress_finished" => {
            ConversationStreamEvent::ContextCompressFinished(serde_json::from_value(envelope.data)?)
        }
        "chat_finished" => {
            ConversationStreamEvent::ChatFinished(serde_json::from_value(envelope.data)?)
        }
        "chat_title_updated" => {
            ConversationStreamEvent::ChatTitleUpdated(serde_json::from_value(envelope.data)?)
        }
        "workflow_finished" => {
            let payload: WorkflowFinishedPayload = serde_json::from_value(envelope.data)?;
            ConversationStreamEvent::WorkflowFinished(ConversationResponse::from_stream_parts(
                started.clone(),
                payload,
            ))
        }
        _ => ConversationStreamEvent::Other {
            event: envelope.event,
            data: envelope.data,
        },
    })
}

impl AgentContext {
    /// Create an [`AgentContext`]
    pub fn new(config: Arc<Config>) -> Self {
        let log_subscriber = config.create_log_subscriber("agent");
        dispatcher::with_default(&log_subscriber.clone().into(), || {
            tracing::info!(language = ?config.language, "creating agent context");
        });
        let ctx = Self(Arc::new(InnerAgentContext {
            http_cli: config.create_http_client(),
            log_subscriber,
        }));
        dispatcher::with_default(&ctx.0.log_subscriber.clone().into(), || {
            tracing::info!("agent context created");
        });
        ctx
    }

    /// Returns the log subscriber
    #[inline]
    pub fn log_subscriber(&self) -> Arc<dyn Subscriber + Send + Sync> {
        self.0.log_subscriber.clone()
    }

    /// List the Workspaces the current account belongs to.
    ///
    /// Path: `GET /v1/ai/workspaces`
    pub async fn workspaces(&self) -> Result<WorkspacesResponse> {
        Ok(self
            .0
            .http_cli
            .request(Method::GET, "/v1/ai/workspaces")
            .response::<Json<WorkspacesResponse>>()
            .send()
            .with_subscriber(self.0.log_subscriber.clone())
            .await?
            .0)
    }

    /// List the Agents in the specified Workspace.
    ///
    /// Path: `GET /v1/ai/workspaces/{id}/agents`
    pub async fn agents(
        &self,
        workspace_id: impl Into<String>,
        opts: impl Into<Option<GetAgentsOptions>>,
    ) -> Result<AgentsResponse> {
        let workspace_id = workspace_id.into();
        Ok(self
            .0
            .http_cli
            .request(
                Method::GET,
                format!("/v1/ai/workspaces/{workspace_id}/agents"),
            )
            .query_params(opts.into().unwrap_or_default())
            .response::<Json<AgentsResponse>>()
            .send()
            .with_subscriber(self.0.log_subscriber.clone())
            .await?
            .0)
    }

    /// Start a conversation with the specified Agent, blocking until the run
    /// succeeds, is interrupted, or fails.
    ///
    /// Path: `POST /v1/ai/agents/{id}/conversations`
    pub async fn conversation(
        &self,
        agent_id: impl Into<String>,
        query: impl Into<String>,
        chat_uid: impl Into<Option<String>>,
    ) -> Result<ConversationResponse> {
        #[derive(Debug, Serialize)]
        struct Body {
            query: String,
            #[serde(skip_serializing_if = "Option::is_none")]
            chat_uid: Option<String>,
        }

        let agent_id = agent_id.into();
        Ok(self
            .0
            .http_cli
            .request(
                Method::POST,
                format!("/v1/ai/agents/{agent_id}/conversations"),
            )
            .header("Accept", "application/json")
            .body(Json(Body {
                query: query.into(),
                chat_uid: chat_uid.into(),
            }))
            .timeout(AGENT_REQUEST_TIMEOUT)
            .response::<Json<ConversationResponse>>()
            .send()
            .with_subscriber(self.0.log_subscriber.clone())
            .await?
            .0)
    }

    /// Resume an interrupted conversation, blocking until the run succeeds, is
    /// interrupted again, or fails.
    ///
    /// Path: `POST
    /// /v1/ai/agents/{id}/conversations/{chat_uid}/messages/{message_id}/
    /// continue`
    pub async fn continue_conversation(
        &self,
        agent_id: impl Into<String>,
        chat_uid: impl Into<String>,
        message_id: impl Into<String>,
        answers: AnswersByToolCall,
    ) -> Result<ConversationResponse> {
        #[derive(Debug, Serialize)]
        struct Body {
            answers_by_tool_call: AnswersByToolCall,
        }

        let agent_id = agent_id.into();
        let chat_uid = chat_uid.into();
        let message_id = message_id.into();
        Ok(self
            .0
            .http_cli
            .request(
                Method::POST,
                format!(
                    "/v1/ai/agents/{agent_id}/conversations/{chat_uid}/messages/{message_id}/continue"
                ),
            )
            .header("Accept", "application/json")
            .body(Json(Body {
                answers_by_tool_call: answers,
            }))
            .timeout(AGENT_REQUEST_TIMEOUT)
            .response::<Json<ConversationResponse>>()
            .send()
            .with_subscriber(self.0.log_subscriber.clone())
            .await?
            .0)
    }

    /// Start a conversation with the specified Agent, returning a [`Stream`] of
    /// run-progress events over SSE. The run's outcome is carried by a
    /// [`ConversationStreamEvent::WorkflowFinished`] event (succeeded, failed,
    /// or stopped) or, if the Agent needs more input from you, a
    /// [`ConversationStreamEvent::HumanInteractionRequired`] event instead —
    /// an interrupted run never emits `WorkflowFinished`. Neither is
    /// necessarily the last item — the server may still emit a few more
    /// housekeeping events (e.g.
    /// [`ConversationStreamEvent::ChatTitleUpdated`]) before actually closing
    /// the connection, so keep draining the stream until it ends rather than
    /// stopping as soon as you see one.
    ///
    /// Path: `POST /v1/ai/agents/{id}/conversations` (`Accept:
    /// text/event-stream`)
    pub async fn conversation_streamed(
        &self,
        agent_id: impl Into<String>,
        query: impl Into<String>,
        chat_uid: impl Into<Option<String>>,
    ) -> Result<impl Stream<Item = Result<ConversationStreamEvent>> + Send + 'static> {
        #[derive(Debug, Serialize)]
        struct Body {
            query: String,
            #[serde(skip_serializing_if = "Option::is_none")]
            chat_uid: Option<String>,
        }

        let agent_id = agent_id.into();
        let raw = self
            .0
            .http_cli
            .request(
                Method::POST,
                format!("/v1/ai/agents/{agent_id}/conversations"),
            )
            .body(Json(Body {
                query: query.into(),
                chat_uid: chat_uid.into(),
            }))
            .timeout(AGENT_REQUEST_TIMEOUT)
            .send_events()
            .with_subscriber(self.0.log_subscriber.clone())
            .await?;

        let mut started: Option<(String, String)> = None;
        Ok(raw.map(move |item| map_conversation_event(item, &mut started)))
    }

    /// Resume an interrupted conversation, returning a [`Stream`] of
    /// run-progress events over SSE.
    ///
    /// Path: `POST
    /// /v1/ai/agents/{id}/conversations/{chat_uid}/messages/{message_id}/
    /// continue` (`Accept: text/event-stream`)
    pub async fn continue_conversation_streamed(
        &self,
        agent_id: impl Into<String>,
        chat_uid: impl Into<String>,
        message_id: impl Into<String>,
        answers: AnswersByToolCall,
    ) -> Result<impl Stream<Item = Result<ConversationStreamEvent>> + Send + 'static> {
        #[derive(Debug, Serialize)]
        struct Body {
            answers_by_tool_call: AnswersByToolCall,
        }

        let agent_id = agent_id.into();
        let chat_uid = chat_uid.into();
        let message_id = message_id.into();
        // We already know chat_uid/message_id from the caller (unlike a brand-new
        // conversation) — seed `started` so the final ConversationResponse carries
        // them even if the server doesn't re-emit a `chat_started` event here.
        let mut started = Some((chat_uid.clone(), message_id.clone()));
        let raw = self
            .0
            .http_cli
            .request(
                Method::POST,
                format!(
                    "/v1/ai/agents/{agent_id}/conversations/{chat_uid}/messages/{message_id}/continue"
                ),
            )
            .body(Json(Body {
                answers_by_tool_call: answers,
            }))
            .timeout(AGENT_REQUEST_TIMEOUT)
            .send_events()
            .with_subscriber(self.0.log_subscriber.clone())
            .await?;

        Ok(raw.map(move |item| map_conversation_event(item, &mut started)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // The `data:` payloads of the three example SSE frames from
    // https://open.longbridge.com/en/docs/ai/chat/conversation
    const CHAT_STARTED: &str = r#"{"event":"chat_started","workflow_run_id":"wr_1","data":{"chat_uid":"ct_9f2c1a5b","message_id":42}}"#;
    const MESSAGE: &str = r#"{"event":"message","workflow_run_id":"wr_1","data":{"text":"Tesla"}}"#;
    const WORKFLOW_FINISHED: &str = r#"{"event":"workflow_finished","workflow_run_id":"wr_1","data":{"status":"succeeded","elapsed_time":3.21,"outputs":{"answer":"Tesla (TSLA.US) recently..."}}}"#;

    // The four event types below aren't in the docs — captured verbatim from
    // real traffic during manual live testing (see conversation history).
    const WORKFLOW_STARTED: &str = r#"{"event":"workflow_started","workflow_run_id":"wr_1","data":{"hit_cache":false,"inputs":{"chat_id":834552,"chat_uid":"ct_9f2c1a5b","message_id":42,"query":"How has Tesla stock performed recently?"},"started_at":1784545150,"workflow_id":176476}}"#;
    const PING: &str = r#"{"event":"ping","workflow_run_id":"wr_1","data":null}"#;
    const CHAT_FINISHED: &str = r#"{"event":"chat_finished","workflow_run_id":"wr_1","data":{"chat_id":834552,"chat_uid":"ct_9f2c1a5b","error":"","error_message":"","message_id":42}}"#;
    const CHAT_TITLE_UPDATED: &str = r#"{"event":"chat_title_updated","workflow_run_id":"wr_1","data":{"chat_id":834552,"chat_uid":"ct_9f2c1a5b","source":"ai_generated","title":"Tesla stock performance","updated_at":1784546957}}"#;

    fn sse(data: &str) -> longbridge_httpcli::HttpClientResult<longbridge_httpcli::SseEvent> {
        Ok(longbridge_httpcli::SseEvent {
            event: "message".to_string(),
            data: data.to_string(),
            id: String::new(),
            retry: None,
        })
    }

    #[test]
    fn map_conversation_event_full_sequence() {
        let mut started = None;

        match map_conversation_event(sse(CHAT_STARTED), &mut started).unwrap() {
            ConversationStreamEvent::ChatStarted(payload) => {
                assert_eq!(payload.chat_uid, "ct_9f2c1a5b");
                assert_eq!(payload.message_id, "42");
            }
            other => panic!("unexpected event: {other:?}"),
        }
        assert_eq!(started, Some(("ct_9f2c1a5b".to_string(), "42".to_string())));

        // The real event stream is richer than the docs' three-event example
        // — this exercises the fuller, real-world sequence, including
        // `chat_title_updated` arriving *after* `workflow_finished` (observed
        // live; see the "drain to the stream's natural end" fix).
        match map_conversation_event(sse(WORKFLOW_STARTED), &mut started).unwrap() {
            ConversationStreamEvent::WorkflowStarted(payload) => {
                assert!(!payload.hit_cache);
                assert_eq!(payload.inputs.chat_uid, "ct_9f2c1a5b");
                assert_eq!(payload.inputs.message_id, "42");
                assert_eq!(payload.workflow_id, 176476);
            }
            other => panic!("unexpected event: {other:?}"),
        }

        match map_conversation_event(sse(MESSAGE), &mut started).unwrap() {
            ConversationStreamEvent::Message(payload) => assert_eq!(payload.text, "Tesla"),
            other => panic!("unexpected event: {other:?}"),
        }

        match map_conversation_event(sse(PING), &mut started).unwrap() {
            ConversationStreamEvent::Ping => {}
            other => panic!("unexpected event: {other:?}"),
        }

        match map_conversation_event(sse(CHAT_FINISHED), &mut started).unwrap() {
            ConversationStreamEvent::ChatFinished(payload) => {
                assert_eq!(payload.chat_uid, "ct_9f2c1a5b");
                assert_eq!(payload.message_id, "42");
                assert_eq!(payload.error, "");
                assert_eq!(payload.error_message, "");
            }
            other => panic!("unexpected event: {other:?}"),
        }

        match map_conversation_event(sse(WORKFLOW_FINISHED), &mut started).unwrap() {
            ConversationStreamEvent::WorkflowFinished(resp) => {
                assert_eq!(resp.chat_uid, "ct_9f2c1a5b");
                assert_eq!(resp.message_id, "42");
                assert_eq!(resp.status, ConversationStatus::Succeeded);
                assert_eq!(resp.answer, "Tesla (TSLA.US) recently...");
            }
            other => panic!("unexpected event: {other:?}"),
        }

        // Arrives *after* workflow_finished in this (real, observed) ordering.
        match map_conversation_event(sse(CHAT_TITLE_UPDATED), &mut started).unwrap() {
            ConversationStreamEvent::ChatTitleUpdated(payload) => {
                assert_eq!(payload.chat_uid, "ct_9f2c1a5b");
                assert_eq!(payload.source, "ai_generated");
                assert_eq!(payload.title, "Tesla stock performance");
            }
            other => panic!("unexpected event: {other:?}"),
        }
    }

    #[test]
    fn map_conversation_event_unknown_type_falls_back_to_other() {
        let mut started = None;
        let json = r#"{"event":"some_future_event","data":{"foo":"bar"}}"#;
        match map_conversation_event(sse(json), &mut started).unwrap() {
            ConversationStreamEvent::Other { event, data } => {
                assert_eq!(event, "some_future_event");
                assert_eq!(data["foo"], "bar");
            }
            other => panic!("unexpected event: {other:?}"),
        }
    }

    // https://github.com/longbridge/developers/pull/1176 — an interrupted
    // run's stream never emits `workflow_finished`; `human_interaction_required`
    // is the terminal event instead.
    const HUMAN_INTERACTION_REQUIRED: &str = r#"{"event":"human_interaction_required","workflow_run_id":"wr_1","data":{"node_id":"n_ask_human","tool_call_id":"call_abc123","questions":[{"question":"Which time range would you like to check?","options":[{"description":"Past week"},{"description":"Past month"}],"multi_select":false}],"message_id":43,"chat_id":1001}}"#;

    #[test]
    fn map_conversation_event_interrupted_sequence_has_no_workflow_finished() {
        let mut started = None;
        map_conversation_event(sse(CHAT_STARTED), &mut started).unwrap();
        map_conversation_event(sse(WORKFLOW_STARTED), &mut started).unwrap();

        match map_conversation_event(sse(HUMAN_INTERACTION_REQUIRED), &mut started).unwrap() {
            ConversationStreamEvent::HumanInteractionRequired(resp) => {
                assert_eq!(resp.chat_uid, "ct_9f2c1a5b");
                assert_eq!(resp.message_id, "42");
                assert_eq!(resp.status, ConversationStatus::Interrupted);
                let interrupt = resp.interrupt.expect("interrupt");
                assert_eq!(interrupt.node_id, "n_ask_human");
                assert_eq!(interrupt.tool_call_id, "call_abc123");
            }
            other => panic!("unexpected event: {other:?}"),
        }

        // The stream still ends with `chat_finished`, just never emits
        // `workflow_finished`.
        match map_conversation_event(sse(CHAT_FINISHED), &mut started).unwrap() {
            ConversationStreamEvent::ChatFinished(_) => {}
            other => panic!("unexpected event: {other:?}"),
        }
    }

    #[test]
    fn map_conversation_event_plan_changed_picks_up_sibling_tool_name() {
        let mut started = None;
        // `tool_name` sits outside `data`, as a sibling of `event`/`data` in
        // the raw envelope.
        let json = r#"{"event":"plan_changed","workflow_run_id":"wr_1","tool_name":"planner","data":{"node_id":"n_plan","started_at":1752048000}}"#;
        match map_conversation_event(sse(json), &mut started).unwrap() {
            ConversationStreamEvent::PlanChanged(payload) => {
                assert_eq!(payload.node_id, "n_plan");
                assert_eq!(payload.tool_name, "planner");
            }
            other => panic!("unexpected event: {other:?}"),
        }
    }
}