qq-cli 0.1.0

A modular Rust QQ task interface for coding agents
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
614
615
616
617
618
619
620
621
use crate::core::contracts::{
    message::{
        Availability, KnownSegment, Media, Mention, Message as PublicMessage, Node, Segment,
        Sender, UnsupportedKind,
    },
    scalar::No,
};
use std::time::Instant;

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

use crate::core::domain::{Conversation, Scope, decimal_id, identifier};
use crate::core::error::{AppError, Result};
use crate::messaging::session::Session;
use crate::storage::references::References;
use crate::transport::client::{Action, Backend};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Locator {
    pub scope: Scope,
    pub run_id: String,
    pub conversation: Conversation,
    pub backend_id: String,
    pub time_seconds: Option<i64>,
    pub sequence: Option<String>,
    pub sender_id: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct MediaLocator {
    pub message: Locator,
    pub kind: String,
    pub file_id: String,
    pub name: Option<String>,
    pub size: Option<u64>,
    #[serde(default)]
    pub opaque_id: bool,
    #[serde(default)]
    pub segment_index: Option<usize>,
    #[serde(default)]
    pub source_fingerprint: Option<String>,
}

#[derive(Debug)]
pub struct Message {
    pub locator: Locator,
    pub value: PublicMessage,
    pub text: String,
}

pub fn unsigned(value: &Value) -> Option<u64> {
    identifier(value).and_then(|s| s.parse().ok())
}

pub fn normalize(
    raw: &Value,
    scope: &Scope,
    run_id: &str,
    conversation: &Conversation,
    references: &References,
) -> Result<Message> {
    let backend_id = raw
        .get("message_id")
        .and_then(identifier)
        .ok_or_else(AppError::protocol)?;
    validate_peer(raw, scope, conversation)?;
    let sender_id = raw
        .get("user_id")
        .or_else(|| raw.get("sender").and_then(|s| s.get("user_id")))
        .and_then(identifier)
        .and_then(|s| decimal_id(&s).ok())
        .ok_or_else(AppError::protocol)?;
    if raw
        .get("sender")
        .and_then(|s| s.get("user_id"))
        .and_then(identifier)
        .is_some_and(|s| s != sender_id)
    {
        return Err(AppError::protocol());
    }
    let time = raw
        .get("time")
        .and_then(unsigned)
        .filter(|seconds| *seconds <= 253_402_300_799)
        .ok_or_else(AppError::protocol)? as i64;
    let sequence = match raw.get("real_seq") {
        None | Some(Value::Null) => None,
        Some(value) => Some(unsigned(value).ok_or_else(AppError::protocol)?.to_string()),
    };
    let locator = Locator {
        scope: scope.clone(),
        run_id: run_id.into(),
        conversation: conversation.clone(),
        backend_id: backend_id.clone(),
        time_seconds: Some(time),
        sequence,
        sender_id: Some(sender_id.clone()),
    };
    let message_ref = references.issue("m1", &locator)?;
    let mut budget = SegmentBudget { nodes: 0, bytes: 0 };
    let segments = normalize_segments(
        raw.get("message").ok_or_else(AppError::protocol)?,
        Some(&locator),
        references,
        false,
        0,
        &mut budget,
    )?;
    let text = searchable_text(&segments);
    let sender = Sender {
        user_id: sender_id,
        nickname: raw
            .get("sender")
            .and_then(|s| s.get("nickname"))
            .and_then(Value::as_str)
            .map(str::to_owned),
    };
    Ok(Message {
        locator,
        text,
        value: PublicMessage {
            message_ref,
            id: backend_id,
            conversation: conversation.to_string(),
            sender,
            time_seconds: time,
            segments,
        },
    })
}

fn validate_peer(raw: &Value, scope: &Scope, conversation: &Conversation) -> Result<()> {
    if let Some(self_id) = raw.get("self_id")
        && identifier(self_id).as_deref() != Some(&scope.account)
    {
        return Err(AppError::protocol());
    }
    match conversation {
        Conversation::Group(id) => {
            if raw.get("message_type").and_then(Value::as_str) != Some("group")
                || raw.get("group_id").and_then(identifier).as_deref() != Some(id)
            {
                return Err(AppError::protocol());
            }
        }
        Conversation::Private(id) => {
            if raw.get("message_type").and_then(Value::as_str) != Some("private") {
                return Err(AppError::protocol());
            }
            let sender = raw
                .get("user_id")
                .and_then(identifier)
                .ok_or_else(AppError::protocol)?;
            if sender != *id && sender != scope.account {
                return Err(AppError::protocol());
            }
            if let Some(target) = raw.get("target_id") {
                let target = identifier(target).ok_or_else(AppError::protocol)?;
                if target != *id && target != scope.account {
                    return Err(AppError::protocol());
                }
            }
        }
    }
    Ok(())
}

pub fn load_locator(references: &References, token: &str) -> Result<Locator> {
    references.get("m1", token)
}

pub fn validate_locator<B: Backend>(locator: &Locator, session: &Session<'_, B>) -> Result<()> {
    if locator.scope.instance_id != session.instance.id || locator.run_id != session.instance.run_id
    {
        return Err(AppError::new(
            "STALE_REFERENCE",
            "Message reference belongs to another instance or backend run; locate it again in history.",
            4,
        ));
    }
    Ok(())
}

pub fn fetch<B: Backend>(
    locator: &Locator,
    session: &Session<'_, B>,
    references: &References,
    deadline: Instant,
) -> Result<Message> {
    fetch_raw(locator, session, references, deadline).map(|(_, message)| message)
}

pub(crate) fn fetch_raw<B: Backend>(
    locator: &Locator,
    session: &Session<'_, B>,
    references: &References,
    deadline: Instant,
) -> Result<(Value, Message)> {
    validate_locator(locator, session)?;
    let (raw,_) = session.read_in_scope(Some(&locator.conversation),Some(&locator.scope),deadline,|backend| {
        backend.call(Action::Message,&json!({"message_id":locator.backend_id}),deadline)
    }).map_err(|error| if error.code=="BACKEND_REJECTED" {
        AppError::new("STALE_REFERENCE","Backend could not resolve this message; this does not prove it was deleted or never sent.",4)
    } else {error})?;
    let message = normalize(
        &raw,
        &locator.scope,
        &locator.run_id,
        &locator.conversation,
        references,
    )?;
    if message.locator.backend_id != locator.backend_id
        || locator
            .time_seconds
            .is_some_and(|time| message.locator.time_seconds != Some(time))
        || locator
            .sequence
            .as_ref()
            .is_some_and(|seq| message.locator.sequence.as_ref() != Some(seq))
        || locator
            .sender_id
            .as_ref()
            .is_some_and(|id| message.locator.sender_id.as_ref() != Some(id))
    {
        return Err(AppError::new(
            "STALE_REFERENCE",
            "Backend message identity no longer matches the stored locator.",
            4,
        ));
    }
    Ok((raw, message))
}

pub fn get<B: Backend>(
    token: &str,
    expand: bool,
    session: &Session<'_, B>,
    references: &References,
    deadline: Instant,
) -> Result<PublicMessage> {
    let locator = load_locator(references, token)?;
    let (raw, mut result) = fetch_raw(&locator, session, references, deadline)?;
    if expand {
        let mut budget = SegmentBudget { nodes: 0, bytes: 0 };
        let mut segments = normalize_segments(
            raw.get("message").ok_or_else(AppError::protocol)?,
            Some(&locator),
            references,
            true,
            0,
            &mut budget,
        )?;
        ForwardExpansion {
            session,
            references,
            locator: &locator,
            deadline,
            active: std::collections::BTreeSet::new(),
            requests: 0,
            response_bytes: serde_json::to_vec(&raw)
                .map_err(|_| AppError::protocol())?
                .len(),
        }
        .resolve(&mut segments, 0, &mut budget)?;
        result.value.segments = segments;
    }
    Ok(result.value)
}

struct SegmentBudget {
    nodes: usize,
    bytes: usize,
}

struct ForwardExpansion<'a, 's, B: Backend> {
    session: &'a Session<'s, B>,
    references: &'a References,
    locator: &'a Locator,
    deadline: Instant,
    active: std::collections::BTreeSet<String>,
    requests: usize,
    response_bytes: usize,
}

impl<B: Backend> ForwardExpansion<'_, '_, B> {
    fn resolve(
        &mut self,
        segments: &mut [Segment],
        depth: usize,
        budget: &mut SegmentBudget,
    ) -> Result<()> {
        if depth > 4 || self.response_bytes > 4 * 1024 * 1024 {
            return Err(expansion_limit());
        }
        for segment in segments {
            if Instant::now() >= self.deadline {
                return Err(AppError::new(
                    "DEADLINE_EXCEEDED",
                    "Forward expansion exceeded the operation deadline.",
                    3,
                ));
            }
            let Segment::Known(KnownSegment::Forward {
                nodes,
                expanded,
                resource_id,
            }) = segment
            else {
                continue;
            };
            let resource = resource_id.take();
            if let Some(resource) = &resource {
                if depth >= 4 || self.requests >= 100 || !self.active.insert(resource.clone()) {
                    return Err(expansion_limit());
                }
                self.requests += 1;
                let (raw, _) = self.session.read_in_scope(
                    Some(&self.locator.conversation),
                    Some(&self.locator.scope),
                    self.deadline,
                    |backend| backend.call(Action::Forward, &json!({"id":resource}), self.deadline),
                )?;
                self.response_bytes = self.response_bytes.saturating_add(
                    serde_json::to_vec(&raw)
                        .map_err(|_| AppError::protocol())?
                        .len(),
                );
                if self.response_bytes > 4 * 1024 * 1024 {
                    return Err(expansion_limit());
                }
                *nodes = Some(normalize_nodes(
                    raw.get("messages").ok_or_else(AppError::protocol)?,
                    self.references,
                    depth + 1,
                    budget,
                )?);
                *expanded = true;
            }
            if let Some(nodes) = nodes {
                for node in nodes {
                    self.resolve(&mut node.segments, depth + 1, budget)?;
                }
            }
            if let Some(resource) = resource {
                self.active.remove(&resource);
            }
        }
        Ok(())
    }
}

fn normalize_segments(
    value: &Value,
    locator: Option<&Locator>,
    references: &References,
    expand: bool,
    depth: usize,
    budget: &mut SegmentBudget,
) -> Result<Vec<Segment>> {
    if depth > 4 {
        return Err(expansion_limit());
    }
    let raw = value.as_array().ok_or_else(AppError::protocol)?;
    let mut output = Vec::new();
    for (segment_index, segment) in raw.iter().enumerate() {
        budget.nodes += 1;
        if budget.nodes > 1000 {
            return Err(expansion_limit());
        }
        let kind = segment
            .get("type")
            .and_then(Value::as_str)
            .ok_or_else(AppError::protocol)?;
        let data = segment
            .get("data")
            .and_then(Value::as_object)
            .ok_or_else(AppError::protocol)?;
        let normalized = match kind {
            "text" => {
                let text = data
                    .get("text")
                    .and_then(Value::as_str)
                    .ok_or_else(AppError::protocol)?;
                budget.bytes = budget.bytes.saturating_add(text.len());
                if budget.bytes > 4 * 1024 * 1024 {
                    return Err(expansion_limit());
                }
                Segment::Known(KnownSegment::Text { text: text.into() })
            }
            "at" => {
                let user = data
                    .get("qq")
                    .and_then(identifier)
                    .and_then(|id| decimal_id(&id).ok());
                match user {
                    Some(user) => Segment::Known(KnownSegment::Mention {
                        target: Mention::User { user_id: user },
                    }),
                    None => Segment::Known(KnownSegment::Mention {
                        target: Mention::Unsupported { supported: No },
                    }),
                }
            }
            "reply" => {
                let id = data
                    .get("id")
                    .and_then(identifier)
                    .ok_or_else(AppError::protocol)?;
                let mut message_ref = None;
                if let Some(locator) = locator {
                    let mut reply = locator.clone();
                    reply.backend_id = id;
                    reply.time_seconds = None;
                    reply.sequence = None;
                    reply.sender_id = None;
                    message_ref = Some(references.issue("m1", &reply)?);
                }
                Segment::Known(KnownSegment::Reply { message_ref })
            }
            "image" | "file" => media_segment(kind, data, locator, references, segment_index)?,
            "forward" => {
                let mut nodes = None;
                let mut expanded = false;
                let mut resource_id = None;
                if expand {
                    if let Some(content) = data.get("content") {
                        nodes = Some(normalize_nodes(content, references, depth + 1, budget)?);
                        expanded = true;
                    } else if let Some(id) = data.get("id").and_then(Value::as_str) {
                        if id.is_empty() || id.len() > 1024 || id.chars().any(char::is_control) {
                            return Err(AppError::protocol());
                        }
                        resource_id = Some(id.into());
                    }
                }
                Segment::Known(KnownSegment::Forward {
                    expanded,
                    nodes,
                    resource_id,
                })
            }
            _ => Segment::Unsupported {
                kind: UnsupportedKind::new(kind),
                supported: No,
            },
        };
        output.push(normalized);
    }
    Ok(output)
}

fn normalize_nodes(
    value: &Value,
    references: &References,
    depth: usize,
    budget: &mut SegmentBudget,
) -> Result<Vec<Node>> {
    if depth > 4 {
        return Err(expansion_limit());
    }
    let nodes = value.as_array().ok_or_else(AppError::protocol)?;
    if nodes.len() > 100 {
        return Err(expansion_limit());
    }
    nodes
        .iter()
        .map(|node| {
            budget.nodes += 1;
            if budget.nodes > 1000 {
                return Err(expansion_limit());
            }
            let data = if node.get("type").and_then(Value::as_str) == Some("node") {
                node.get("data").ok_or_else(AppError::protocol)?
            } else {
                node
            };
            let content = data
                .get("content")
                .or_else(|| data.get("message"))
                .ok_or_else(AppError::protocol)?;
            let segments = normalize_segments(content, None, references, true, depth, budget)?;
            let sender_id = data.get("user_id").and_then(identifier);
            let nickname = data
                .get("nickname")
                .or_else(|| data.get("sender").and_then(|s| s.get("nickname")))
                .and_then(Value::as_str)
                .map(str::to_owned);
            Ok(Node {
                segments,
                sender_id,
                nickname,
            })
        })
        .collect()
}

fn media_segment(
    kind: &str,
    data: &Map<String, Value>,
    locator: Option<&Locator>,
    references: &References,
    segment_index: usize,
) -> Result<Segment> {
    let mut result = Media {
        availability: Availability::Unavailable {
            downloadable: No,
            media_ref: None,
        },
        name: None,
        size_bytes: None,
    };
    let file = data
        .get("file_id")
        .or_else(|| data.get("file"))
        .and_then(Value::as_str);
    let name = data
        .get("file")
        .and_then(Value::as_str)
        .filter(|name| !name.contains("://") && !name.contains(['?', '#']))
        .and_then(|s| {
            let name = s.rsplit(['/', '\\']).next().unwrap_or_default();
            crate::napcat::install::archive::member_path(name)
                .ok()
                .map(|_| name.to_owned())
        });
    if let Some(name) = &name {
        result.name = Some(name.clone());
    }
    let size = data.get("file_size").and_then(unsigned);
    if let Some(size) = size {
        result.size_bytes = Some(size);
    }
    if let (Some(file_id), Some(locator)) = (file, locator)
        && !file_id.is_empty()
        && file_id.len() <= 1024
        && ((!file_id.contains(['/', '\\', ':', '?', '#'])
            && !file_id.chars().any(char::is_control)
            && crate::napcat::install::archive::member_path(file_id).is_ok())
            || (kind == "file"
                && matches!(locator.conversation, Conversation::Group(_))
                && data.get("file_id").and_then(Value::as_str) == Some(file_id)
                && is_group_file_uuid(file_id)))
    {
        let media_ref = references.issue(
            "f1",
            &MediaLocator {
                message: locator.clone(),
                kind: kind.into(),
                file_id: file_id.into(),
                name,
                size,
                segment_index: Some(segment_index),
                source_fingerprint: if kind == "image" {
                    data.get("url")
                        .and_then(Value::as_str)
                        .and_then(crate::messaging::media::source::image_identity)
                } else {
                    None
                },
                opaque_id: kind == "file"
                    && data
                        .get("file_id")
                        .and_then(Value::as_str)
                        .is_some_and(|id| id == file_id),
            },
        )?;
        let downloadable = (kind == "image"
            && crate::messaging::media::source::can_resolve_image(
                data.get("url").and_then(Value::as_str),
            ))
            || (kind == "file"
                && data
                    .get("file_id")
                    .and_then(Value::as_str)
                    .is_some_and(|id| id == file_id));
        result.reference(media_ref, downloadable);
    }
    Ok(Segment::Known(if kind == "image" {
        KnownSegment::Image { media: result }
    } else {
        KnownSegment::File { media: result }
    }))
}

// QQ group fileUuid is an opaque API identifier, sometimes prefixed with '/'.
// Accept this exact wire shape without interpreting it as a filesystem path.
fn is_group_file_uuid(value: &str) -> bool {
    let Some(uuid) = value.strip_prefix('/') else {
        return false;
    };
    uuid.len() == 36
        && uuid.bytes().enumerate().all(|(index, byte)| {
            if matches!(index, 8 | 13 | 18 | 23) {
                byte == b'-'
            } else {
                byte.is_ascii_hexdigit()
            }
        })
}

fn searchable_text(segments: &[Segment]) -> String {
    segments
        .iter()
        .filter_map(|s| match s {
            Segment::Known(KnownSegment::Text { text }) => Some(text.as_str()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("")
}

fn expansion_limit() -> AppError {
    AppError::new(
        "EXPANSION_LIMIT",
        "Forwarded message exceeds bounded expansion limits.",
        4,
    )
}