dbmd-core 0.13.0

Reference library for db.md, the open standard for databases in plain files. Parsing, store walk, wiki-link graph, validation, query, and write-through indexes. Zero AI dependencies.
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
// SPDX-License-Identifier: Apache-2.0

//! The ChatGPT (Codex) backend adapter — the OpenAI **Responses** wire format
//! against `https://chatgpt.com/backend-api/codex/responses`, driven by a
//! subscription token from [`super::oauth`]. Ported from pi's
//! `openai-codex-responses` provider.
//!
//! This is a third wire protocol, deliberately scoped: it exists only because
//! the ChatGPT backend speaks Responses rather than Chat Completions, and it
//! is reached only by an explicit `dbmd login codex`. The two neutral
//! protocols ([`super::openai`], [`super::anthropic`]) remain the general
//! path for API keys and local servers.
//!
//! Request shape (pi's `buildRequestBody`): `{model, store: false, stream:
//! true, instructions: <system prompt>, input: [...], tools: [...],
//! tool_choice: "auto", parallel_tool_calls: true, include:
//! ["reasoning.encrypted_content"], text: {verbosity}}`. The system prompt
//! rides as `instructions`, never as an input message.
//!
//! Headers: `Authorization: Bearer <access>`, `chatgpt-account-id` from the
//! token's JWT claim, `OpenAI-Beta: responses=experimental`, a `session_id`,
//! and `originator: dbmd` — this toolkit identifies itself honestly and never
//! poses as another vendor's client.
//!
//! Stream events consumed (`response.*`): `output_item.added` opens a
//! reasoning / message / function_call item, `output_text.delta` and
//! `reasoning_summary_text.delta` carry text, `function_call_arguments.delta`
//! accumulates tool arguments, `output_item.done` finalizes each item, and
//! `response.completed` carries usage. Unknown event types are ignored by
//! design, exactly as with the other adapters.

use serde_json::{json, Value};

use super::openai::streaming_agent;
use super::sse::read_events;
use super::tools::ToolSpec;
use super::{Block, Event, HarnessError, Msg, Provider, Role, RunOptions, Stop, Turn};

/// The ChatGPT backend base (overridable for tests via the provider's
/// `base_url`).
pub const DEFAULT_BASE_URL: &str = "https://chatgpt.com/backend-api";

/// Resolve the responses endpoint from a base URL, tolerating a base that
/// already names `/codex` or `/codex/responses` (pi's `resolveCodexUrl`).
pub fn responses_url(base_url: &str) -> String {
    let raw = if base_url.trim().is_empty() {
        DEFAULT_BASE_URL
    } else {
        base_url
    };
    let normalized = raw.trim_end_matches('/');
    if normalized.ends_with("/codex/responses") {
        normalized.to_string()
    } else if normalized.ends_with("/codex") {
        format!("{normalized}/responses")
    } else {
        format!("{normalized}/codex/responses")
    }
}

/// Build the `input` array: user text, assistant text, tool calls, and tool
/// results in the Responses item vocabulary.
fn wire_input(messages: &[Msg]) -> Vec<Value> {
    let mut input: Vec<Value> = Vec::new();
    for msg in messages {
        match msg.role {
            Role::User => {
                for block in &msg.blocks {
                    match block {
                        Block::Text(text) => input.push(json!({
                            "type": "message",
                            "role": "user",
                            "content": [{ "type": "input_text", "text": text }],
                        })),
                        Block::ToolResult {
                            id,
                            content,
                            is_error,
                            ..
                        } => {
                            // The engine's call id is the Responses `call_id`.
                            let output = if *is_error {
                                format!("ERROR: {content}")
                            } else {
                                content.clone()
                            };
                            input.push(json!({
                                "type": "function_call_output",
                                "call_id": id,
                                "output": output,
                            }));
                        }
                        _ => {}
                    }
                }
            }
            Role::Assistant => {
                for block in &msg.blocks {
                    match block {
                        Block::Text(text) if !text.is_empty() => input.push(json!({
                            "type": "message",
                            "role": "assistant",
                            "content": [{ "type": "output_text", "text": text, "annotations": [] }],
                        })),
                        Block::ToolUse {
                            id,
                            name,
                            args,
                            raw_args,
                        } => input.push(json!({
                            "type": "function_call",
                            "call_id": id,
                            "name": name,
                            "arguments": raw_args.clone().unwrap_or_else(|| args.to_string()),
                        })),
                        // Reasoning items are not replayed: their encrypted
                        // content is bound to the response that produced it.
                        _ => {}
                    }
                }
            }
        }
    }
    input
}

fn wire_tools(tools: &[ToolSpec]) -> Vec<Value> {
    tools
        .iter()
        .map(|tool| {
            json!({
                "type": "function",
                "name": tool.name,
                "description": tool.description,
                "parameters": tool.parameters,
                "strict": false,
            })
        })
        .collect()
}

/// One in-flight output item, keyed by its stream `output_index`.
enum Pending {
    Text(String),
    Thinking(String),
    ToolCall {
        call_id: String,
        name: String,
        args_buf: String,
    },
    Other,
}

/// Stream one assistant turn from the ChatGPT backend.
pub fn stream_turn(
    provider: &Provider,
    opts: &RunOptions,
    system: &str,
    messages: &[Msg],
    tools: &[ToolSpec],
    emit: &mut dyn FnMut(Event),
) -> Result<Turn, HarnessError> {
    let access = provider.key.as_deref().ok_or_else(|| {
        HarnessError::Config(
            "no ChatGPT credentials — run `dbmd login codex` to use your subscription".to_string(),
        )
    })?;
    let account = super::oauth::account_id(access).ok_or_else(|| {
        HarnessError::Config(
            "the stored ChatGPT token carries no account id — run `dbmd login codex` again"
                .to_string(),
        )
    })?;
    let session_id = super::oauth::create_state()?;

    let mut body = serde_json::Map::new();
    body.insert("model".into(), json!(provider.model));
    body.insert("store".into(), json!(false));
    body.insert("stream".into(), json!(true));
    body.insert("instructions".into(), json!(system));
    body.insert("input".into(), json!(wire_input(messages)));
    body.insert("include".into(), json!(["reasoning.encrypted_content"]));
    body.insert("text".into(), json!({ "verbosity": "low" }));
    body.insert("tool_choice".into(), json!("auto"));
    body.insert("parallel_tool_calls".into(), json!(true));
    body.insert("prompt_cache_key".into(), json!(session_id));
    if !tools.is_empty() {
        body.insert("tools".into(), json!(wire_tools(tools)));
    }
    // `max_tokens` has no Responses equivalent the backend accepts here; the
    // engine's turn cap and the account's own limits bound a run instead.
    let _ = opts.max_tokens;
    // Reasoning depth. `summary: "auto"` is what makes the backend stream
    // reasoning summaries at all — without it the thinking deltas this
    // adapter already parses simply never arrive.
    if let Some(effort) = opts.effort {
        body.insert(
            "reasoning".into(),
            json!({ "effort": effort.codex(), "summary": "auto" }),
        );
    }

    let url = responses_url(&provider.base_url);
    let agent = streaming_agent();
    let request = agent
        .post(&url)
        .set("authorization", &format!("Bearer {access}"))
        .set("chatgpt-account-id", &account)
        .set("originator", super::oauth::ORIGINATOR)
        .set("openai-beta", "responses=experimental")
        .set("session_id", &session_id)
        .set("x-client-request-id", &session_id)
        .set("accept", "text/event-stream")
        .set("content-type", "application/json")
        .set(
            "user-agent",
            &format!(
                "dbmd/{} ({} {})",
                env!("CARGO_PKG_VERSION"),
                std::env::consts::OS,
                std::env::consts::ARCH
            ),
        );
    let response = match request.send_string(&Value::Object(body).to_string()) {
        Ok(response) => response,
        Err(ureq::Error::Status(status, response)) => {
            let mut detail = response.into_string().unwrap_or_default();
            detail.truncate(600);
            let hint = match status {
                400 if detail.contains("reasoning") || detail.contains("effort") => {
                    " — this model or plan does not accept that reasoning \
                     effort; try `--effort high` or drop `--effort`"
                }
                400 if detail.contains("not supported") => {
                    " — pick one your plan exposes with `--model` (or \
                     `llm_model_codex` in .dbmd/config); `codex --help` and \
                     ~/.codex/config.toml name what this account uses"
                }
                401 => " — the login may have expired; run `dbmd login codex` again",
                403 => " — this ChatGPT account may not include Codex access",
                429 => " — the account's rate or usage limit was reached",
                _ => "",
            };
            return Err(HarnessError::Provider(format!(
                "ChatGPT backend returned HTTP {status}{hint}: {detail}"
            )));
        }
        Err(error) => {
            return Err(HarnessError::Provider(format!(
                "cannot reach the ChatGPT backend: {error}"
            )))
        }
    };

    let mut open: Vec<(u64, Pending)> = Vec::new();
    let mut blocks: Vec<Block> = Vec::new();
    let mut usage: Option<(u64, u64)> = None;
    let mut stream_error: Option<String> = None;
    let mut incomplete = false;

    read_events(response.into_reader(), |event| {
        let Ok(data) = serde_json::from_str::<Value>(&event.data) else {
            return true;
        };
        let kind = data
            .get("type")
            .and_then(|t| t.as_str())
            .or(event.event.as_deref())
            .unwrap_or("");
        let index = data
            .get("output_index")
            .and_then(|i| i.as_u64())
            .unwrap_or(0);
        match kind {
            "response.output_item.added" => {
                let item = data.get("item").unwrap_or(&Value::Null);
                let pending = match item.get("type").and_then(|t| t.as_str()) {
                    Some("reasoning") => Pending::Thinking(String::new()),
                    Some("message") => Pending::Text(String::new()),
                    Some("function_call") => Pending::ToolCall {
                        call_id: item
                            .get("call_id")
                            .and_then(|c| c.as_str())
                            .unwrap_or("")
                            .to_string(),
                        name: item
                            .get("name")
                            .and_then(|n| n.as_str())
                            .unwrap_or("")
                            .to_string(),
                        args_buf: String::new(),
                    },
                    _ => Pending::Other,
                };
                open.push((index, pending));
            }
            "response.output_text.delta"
            | "response.reasoning_summary_text.delta"
            | "response.reasoning_text.delta" => {
                let Some(fragment) = data.get("delta").and_then(|d| d.as_str()) else {
                    return true;
                };
                if fragment.is_empty() {
                    return true;
                }
                match open.iter_mut().rev().find(|(i, _)| *i == index) {
                    Some((_, Pending::Text(buffer))) => {
                        buffer.push_str(fragment);
                        emit(Event::TextDelta {
                            text: fragment.to_string(),
                        });
                    }
                    Some((_, Pending::Thinking(buffer))) => {
                        buffer.push_str(fragment);
                        emit(Event::ThinkingDelta {
                            text: fragment.to_string(),
                        });
                    }
                    _ => {}
                }
            }
            "response.function_call_arguments.delta" => {
                if let Some(fragment) = data.get("delta").and_then(|d| d.as_str()) {
                    if let Some((_, Pending::ToolCall { args_buf, .. })) =
                        open.iter_mut().rev().find(|(i, _)| *i == index)
                    {
                        args_buf.push_str(fragment);
                    }
                }
            }
            "response.function_call_arguments.done" => {
                // The terminal event carries the COMPLETE argument string.
                if let Some(complete) = data.get("arguments").and_then(|a| a.as_str()) {
                    if let Some((_, Pending::ToolCall { args_buf, .. })) =
                        open.iter_mut().rev().find(|(i, _)| *i == index)
                    {
                        *args_buf = complete.to_string();
                    }
                }
            }
            "response.output_item.done" => {
                let item = data.get("item").unwrap_or(&Value::Null);
                let position = open.iter().rposition(|(i, _)| *i == index);
                let pending = position.map(|position| open.remove(position).1);
                match pending {
                    Some(Pending::Text(buffer)) => {
                        // The done item is authoritative over the deltas.
                        let text = item
                            .get("content")
                            .and_then(|c| c.as_array())
                            .map(|parts| {
                                parts
                                    .iter()
                                    .filter_map(|part| {
                                        part.get("text")
                                            .or_else(|| part.get("refusal"))
                                            .and_then(|t| t.as_str())
                                    })
                                    .collect::<Vec<_>>()
                                    .join("")
                            })
                            .filter(|text| !text.is_empty())
                            .unwrap_or(buffer);
                        if !text.is_empty() {
                            blocks.push(Block::Text(text));
                        }
                    }
                    Some(Pending::Thinking(buffer)) => {
                        let summary = item
                            .get("summary")
                            .and_then(|s| s.as_array())
                            .map(|parts| {
                                parts
                                    .iter()
                                    .filter_map(|part| part.get("text").and_then(|t| t.as_str()))
                                    .collect::<Vec<_>>()
                                    .join("\n\n")
                            })
                            .filter(|text| !text.is_empty())
                            .unwrap_or(buffer);
                        if !summary.is_empty() {
                            blocks.push(Block::Thinking {
                                text: summary,
                                signature: None,
                            });
                        }
                    }
                    Some(Pending::ToolCall {
                        call_id,
                        name,
                        args_buf,
                    }) => {
                        let call_id = if call_id.is_empty() {
                            item.get("call_id")
                                .and_then(|c| c.as_str())
                                .unwrap_or("")
                                .to_string()
                        } else {
                            call_id
                        };
                        let name = if name.is_empty() {
                            item.get("name")
                                .and_then(|n| n.as_str())
                                .unwrap_or("")
                                .to_string()
                        } else {
                            name
                        };
                        let raw = if args_buf.trim().is_empty() {
                            item.get("arguments")
                                .and_then(|a| a.as_str())
                                .unwrap_or("")
                                .to_string()
                        } else {
                            args_buf
                        };
                        if !name.is_empty() {
                            let args = if raw.trim().is_empty() {
                                json!({})
                            } else {
                                serde_json::from_str(&raw).unwrap_or(json!({}))
                            };
                            blocks.push(Block::ToolUse {
                                id: call_id,
                                name,
                                args,
                                raw_args: if raw.trim().is_empty() {
                                    None
                                } else {
                                    Some(raw)
                                },
                            });
                        }
                    }
                    Some(Pending::Other) | None => {}
                }
            }
            "response.completed" | "response.incomplete" => {
                let response = data.get("response").unwrap_or(&Value::Null);
                let input = response
                    .pointer("/usage/input_tokens")
                    .and_then(|t| t.as_u64())
                    .unwrap_or(0);
                let output = response
                    .pointer("/usage/output_tokens")
                    .and_then(|t| t.as_u64())
                    .unwrap_or(0);
                if input > 0 || output > 0 {
                    usage = Some((input, output));
                }
                if kind == "response.incomplete" {
                    incomplete = true;
                }
                return false;
            }
            "response.failed" | "error" => {
                let message = data
                    .pointer("/response/error/message")
                    .or_else(|| data.pointer("/error/message"))
                    .and_then(|m| m.as_str())
                    .unwrap_or("the ChatGPT backend reported a stream error");
                stream_error = Some(message.to_string());
                return false;
            }
            _ => {}
        }
        true
    })
    .map_err(|error| HarnessError::Provider(format!("stream read failed: {error}")))?;

    if let Some(message) = stream_error {
        return Err(HarnessError::Provider(format!(
            "ChatGPT backend error: {message}"
        )));
    }
    if blocks.is_empty() {
        return Err(HarnessError::Provider(
            "the ChatGPT backend returned no content".to_string(),
        ));
    }

    let has_calls = blocks.iter().any(|b| matches!(b, Block::ToolUse { .. }));
    let stop = if has_calls {
        Stop::ToolUse
    } else if incomplete {
        Stop::Length
    } else {
        Stop::EndTurn
    };
    Ok(Turn {
        blocks,
        stop,
        usage,
    })
}

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

    #[test]
    fn endpoint_resolution_tolerates_every_base_shape() {
        assert_eq!(
            responses_url(""),
            "https://chatgpt.com/backend-api/codex/responses"
        );
        assert_eq!(
            responses_url("https://chatgpt.com/backend-api"),
            "https://chatgpt.com/backend-api/codex/responses"
        );
        assert_eq!(
            responses_url("http://127.0.0.1:9/codex"),
            "http://127.0.0.1:9/codex/responses"
        );
        assert_eq!(
            responses_url("http://127.0.0.1:9/codex/responses/"),
            "http://127.0.0.1:9/codex/responses"
        );
    }

    #[test]
    fn tool_results_ride_as_function_call_output() {
        let input = wire_input(&[
            Msg::user("hi"),
            Msg {
                role: Role::Assistant,
                blocks: vec![Block::ToolUse {
                    id: "call_1".into(),
                    name: "query".into(),
                    args: json!({"type": "todo"}),
                    raw_args: Some("{\"type\":\"todo\"}".into()),
                }],
            },
            Msg {
                role: Role::User,
                blocks: vec![Block::ToolResult {
                    id: "call_1".into(),
                    name: "query".into(),
                    content: "[]".into(),
                    is_error: false,
                }],
            },
        ]);
        assert_eq!(input[0]["type"], "message");
        assert_eq!(input[0]["content"][0]["type"], "input_text");
        assert_eq!(input[1]["type"], "function_call");
        assert_eq!(input[1]["call_id"], "call_1");
        assert_eq!(input[2]["type"], "function_call_output");
        assert_eq!(input[2]["call_id"], "call_1");
    }

    #[test]
    fn error_results_are_marked_for_the_model() {
        let input = wire_input(&[Msg {
            role: Role::User,
            blocks: vec![Block::ToolResult {
                id: "c".into(),
                name: "write".into(),
                content: "unknown tool".into(),
                is_error: true,
            }],
        }]);
        assert!(input[0]["output"]
            .as_str()
            .expect("output")
            .starts_with("ERROR:"));
    }
}