formal-ai 0.251.0

Formal symbolic AI implementation with OpenAI-compatible APIs
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
use std::fmt::Write as _;

use super::finalize_simple;

use crate::coding::contains_cjk;
use crate::engine::{normalize_prompt, SymbolicAnswer};
use crate::event_log::EventLog;
use crate::language::detect as detect_language;
use crate::memory::MemoryEvent;
use crate::seed::{self, Slot, WordForm};
use crate::solver_helpers::{extract_introduced_name, last_user_turn, recall_name_from_history};
use crate::summarization::{
    generate_chat_title, summarize_dialog, DialogTurn, SummarizationConfig, SummarizationMode,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RecallScope {
    Conversation,
    OtherConversations,
}

impl RecallScope {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Conversation => "conversation",
            Self::OtherConversations => "other_conversations",
        }
    }
}

#[derive(Debug)]
struct RecallQuery {
    term: String,
    scope: RecallScope,
}

#[derive(Debug)]
struct RecallMatch {
    turn_index: usize,
    role: &'static str,
    content: String,
}

#[derive(Debug)]
struct MemoryRecallMatch {
    event_index: usize,
    role: String,
    content: String,
    conversation_id: String,
    conversation_title: String,
    sent_at: String,
}

pub fn try_conversation_memory(
    prompt: &str,
    normalized: &str,
    log: &mut EventLog,
) -> Option<SymbolicAnswer> {
    if let Some(answer) = try_recall_name(prompt, normalized, log) {
        return Some(answer);
    }
    if let Some(answer) = try_recall_last_question(prompt, normalized, log) {
        return Some(answer);
    }
    if let Some(answer) = try_conversation_recall(prompt, normalized, log) {
        return Some(answer);
    }
    if let Some(answer) = try_summarize_conversation(prompt, normalized, log) {
        return Some(answer);
    }
    None
}

#[must_use]
pub fn answer_memory_recall(
    prompt: &str,
    events: &[MemoryEvent],
    current_conversation_id: Option<&str>,
) -> Option<SymbolicAnswer> {
    let normalized = normalize_prompt(prompt);
    let mut log = EventLog::new();
    log.append("impulse", prompt.to_owned());
    try_memory_recall(
        prompt,
        &normalized,
        events,
        current_conversation_id,
        &mut log,
    )
}

fn try_recall_name(prompt: &str, normalized: &str, log: &mut EventLog) -> Option<SymbolicAnswer> {
    let asks_name = normalized.contains("what is my name")
        || normalized.contains("what's my name")
        || normalized.contains("do you know my name")
        || normalized.contains("who am i");
    if !asks_name {
        return None;
    }
    let name = recall_name_from_history(log, prompt).or_else(|| extract_introduced_name(prompt))?;
    log.append("filter:user", format!("name={name}"));
    let body = format!("Your name is {name}.");
    Some(finalize_simple(
        prompt,
        log,
        "recall_name",
        "response:recall_name",
        &body,
        0.9,
    ))
}

fn try_recall_last_question(
    prompt: &str,
    normalized: &str,
    log: &mut EventLog,
) -> Option<SymbolicAnswer> {
    let asks = normalized.contains("what did i ask")
        || normalized.contains("what was my last question")
        || normalized.contains("what was my previous question")
        || normalized.contains("repeat my last message");
    if !asks {
        return None;
    }
    let previous = last_user_turn(log)?;
    let body = format!("Your previous message was: \"{previous}\"");
    log.append("filter:user", "previous_turn".to_owned());
    Some(finalize_simple(
        prompt,
        log,
        "recall_last_question",
        "response:recall_last_question",
        &body,
        0.9,
    ))
}

fn try_conversation_recall(
    prompt: &str,
    normalized: &str,
    log: &mut EventLog,
) -> Option<SymbolicAnswer> {
    let query = recognize_recall_query(normalized)?;
    let matches = recall_matches(log, &query.term);
    log.append("filter:memory_query", query.term.clone());
    log.append("filter:memory_scope", query.scope.as_str());
    log.append("filter:memory_matches", matches.len().to_string());
    for matched in &matches {
        log.append(
            "memory_match",
            format!(
                "turn={} role={} content={}",
                matched.turn_index, matched.role, matched.content
            ),
        );
    }
    let language = detect_language(prompt).slug();
    let body = render_recall_report(&query, &matches, language);
    Some(finalize_simple(
        prompt,
        log,
        "conversation_recall",
        "response:conversation_recall",
        &body,
        0.9,
    ))
}

fn try_memory_recall(
    prompt: &str,
    normalized: &str,
    events: &[MemoryEvent],
    current_conversation_id: Option<&str>,
    log: &mut EventLog,
) -> Option<SymbolicAnswer> {
    let query = recognize_recall_query(normalized)?;
    let matches = memory_recall_matches(events, &query, current_conversation_id, prompt);
    let conversation_count = memory_conversation_count(&matches);
    log.append("filter:memory_query", query.term.clone());
    log.append("filter:memory_scope", query.scope.as_str());
    log.append("filter:memory_matches", matches.len().to_string());
    log.append(
        "filter:memory_conversations",
        conversation_count.to_string(),
    );
    for matched in &matches {
        log.append(
            "memory_match",
            format!(
                "event={} conversation={} title={} role={} content={}",
                matched.event_index,
                matched.conversation_id,
                matched.conversation_title,
                matched.role,
                matched.content
            ),
        );
    }
    let language = detect_language(prompt).slug();
    let body = render_memory_recall_report(&query, &matches, language);
    Some(finalize_simple(
        prompt,
        log,
        "conversation_recall",
        "response:conversation_recall",
        &body,
        0.9,
    ))
}

fn recognize_recall_query(normalized: &str) -> Option<RecallQuery> {
    recall_term_for_role(seed::ROLE_CONVERSATION_RECALL_QUERY, normalized)
        .map(|term| RecallQuery {
            term,
            scope: RecallScope::Conversation,
        })
        .or_else(|| {
            recall_term_for_role(seed::ROLE_CONVERSATION_RECALL_OTHER_QUERY, normalized).map(
                |term| RecallQuery {
                    term,
                    scope: RecallScope::OtherConversations,
                },
            )
        })
}

fn recall_term_for_role(role: &str, normalized: &str) -> Option<String> {
    seed::lexicon()
        .role_word_forms(role)
        .iter()
        .filter_map(|form| term_from_form(form, normalized))
        .find(|term| !term.is_empty())
}

fn term_from_form(form: &WordForm, normalized: &str) -> Option<String> {
    let raw = match form.slot() {
        Slot::Prefix => normalized.strip_prefix(form.before_slot())?,
        Slot::Suffix => normalized.strip_suffix(form.after_slot())?,
        Slot::Circumfix => normalized
            .strip_prefix(form.before_slot())?
            .strip_suffix(form.after_slot())?,
        Slot::Bare => return None,
    };
    clean_recall_term(raw)
}

fn clean_recall_term(raw: &str) -> Option<String> {
    let term = raw
        .trim()
        .trim_matches(|ch: char| {
            ch.is_whitespace()
                || matches!(
                    ch,
                    '`' | '"' | '\'' | ':' | '-' | '_' | '.' | ',' | '?' | '!' | '(' | ')'
                )
        })
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ");
    (!term.is_empty()).then_some(term)
}

fn recall_matches(log: &EventLog, term: &str) -> Vec<RecallMatch> {
    let needle = normalize_prompt(term);
    if needle.is_empty() {
        return Vec::new();
    }
    log.events()
        .iter()
        .enumerate()
        .filter_map(|(index, event)| {
            let role = match event.kind {
                "prior_turn:user" => "user",
                "prior_turn:assistant" => "assistant",
                _ => return None,
            };
            let haystack = normalize_prompt(&event.payload);
            haystack.contains(&needle).then(|| RecallMatch {
                turn_index: index + 1,
                role,
                content: event.payload.clone(),
            })
        })
        .collect()
}

fn memory_recall_matches(
    events: &[MemoryEvent],
    query: &RecallQuery,
    current_conversation_id: Option<&str>,
    trigger_text: &str,
) -> Vec<MemoryRecallMatch> {
    let needle = normalize_prompt(&query.term);
    if needle.is_empty() {
        return Vec::new();
    }
    let trigger = normalize_prompt(trigger_text);
    events
        .iter()
        .enumerate()
        .filter_map(|(index, event)| {
            if event.kind.as_deref().is_some_and(|kind| kind != "message") {
                return None;
            }
            let role = event.role.as_deref()?;
            if !role.eq_ignore_ascii_case("user") && !role.eq_ignore_ascii_case("assistant") {
                return None;
            }
            let content = event.content.as_deref()?.trim();
            if content.is_empty() {
                return None;
            }
            let haystack = normalize_prompt(content);
            if !haystack.contains(&needle) {
                return None;
            }
            if !trigger.is_empty() && haystack == trigger {
                return None;
            }
            let conversation_id = event.conversation_id.as_deref().unwrap_or("legacy");
            if query.scope == RecallScope::OtherConversations
                && current_conversation_id.is_some_and(|current| current == conversation_id)
            {
                return None;
            }
            Some(MemoryRecallMatch {
                event_index: index + 1,
                role: role.to_ascii_lowercase(),
                content: content.to_owned(),
                conversation_id: conversation_id.to_owned(),
                conversation_title: event
                    .conversation_title
                    .as_deref()
                    .unwrap_or_default()
                    .to_owned(),
                sent_at: event.sent_at.as_deref().unwrap_or_default().to_owned(),
            })
        })
        .collect()
}

fn memory_conversation_count(matches: &[MemoryRecallMatch]) -> usize {
    let mut ids: Vec<&str> = Vec::new();
    for matched in matches {
        if !ids.contains(&matched.conversation_id.as_str()) {
            ids.push(matched.conversation_id.as_str());
        }
    }
    ids.len()
}

fn render_recall_report(query: &RecallQuery, matches: &[RecallMatch], language: &str) -> String {
    if matches.is_empty() {
        return match language {
            "ru" => format!(
                "Упоминаний \"{}\" в истории разговора не найдено.",
                query.term
            ),
            "zh" => format!("在对话历史中没有找到 \"{}\"", query.term),
            "hi" => format!("बातचीत के इतिहास में \"{}\" नहीं मिला.", query.term),
            _ => format!(
                "No mentions of \"{}\" found in the conversation history.",
                query.term
            ),
        };
    }

    let mut body = match language {
        "ru" => format!(
            "Найдено упоминаний \"{}\" в истории разговора: {}\n",
            query.term,
            matches.len()
        ),
        "zh" => format!(
            "在对话历史中找到 \"{}\" 的记录: {}\n",
            query.term,
            matches.len()
        ),
        "hi" => format!(
            "बातचीत के इतिहास में \"{}\" के उल्लेख मिले: {}\n",
            query.term,
            matches.len()
        ),
        _ => format!(
            "Found {} mention(s) of \"{}\" in the conversation history.\n",
            matches.len(),
            query.term
        ),
    };
    for matched in matches {
        writeln!(
            body,
            "- turn {} {}: {}",
            matched.turn_index, matched.role, matched.content
        )
        .expect("string write is infallible");
    }
    body.trim_end().to_owned()
}

fn render_memory_recall_report(
    query: &RecallQuery,
    matches: &[MemoryRecallMatch],
    language: &str,
) -> String {
    if matches.is_empty() {
        return match language {
            "ru" => format!("Упоминаний \"{}\" в памяти не найдено.", query.term),
            "zh" => format!("在记忆中没有找到 \"{}\"", query.term),
            "hi" => format!("स्मृति में \"{}\" नहीं मिला.", query.term),
            _ => format!("No mentions of \"{}\" found in memory.", query.term),
        };
    }

    let conversation_count = memory_conversation_count(matches);
    let mut body = match language {
        "ru" => format!(
            "Найдено упоминаний \"{}\" в памяти: {} (бесед: {}).\n",
            query.term,
            matches.len(),
            conversation_count
        ),
        "zh" => format!(
            "在记忆中找到 \"{}\" 的记录: {} (对话: {})。\n",
            query.term,
            matches.len(),
            conversation_count
        ),
        "hi" => format!(
            "स्मृति में \"{}\" के उल्लेख मिले: {} (बातचीत: {}).\n",
            query.term,
            matches.len(),
            conversation_count
        ),
        _ => format!(
            "Found {} mention(s) of \"{}\" across {} conversation(s) in memory.\n",
            matches.len(),
            query.term,
            conversation_count
        ),
    };

    let mut conversation_ids: Vec<&str> = Vec::new();
    for matched in matches {
        if !conversation_ids.contains(&matched.conversation_id.as_str()) {
            conversation_ids.push(matched.conversation_id.as_str());
        }
    }
    for conversation_id in conversation_ids {
        let title = matches
            .iter()
            .find(|matched| {
                matched.conversation_id == conversation_id && !matched.conversation_title.is_empty()
            })
            .map_or("", |matched| matched.conversation_title.as_str());
        let label = if title.is_empty() || title == conversation_id {
            conversation_id.to_owned()
        } else {
            format!("{title} ({conversation_id})")
        };
        writeln!(body, "- conversation {label}").expect("string write is infallible");
        for matched in matches
            .iter()
            .filter(|matched| matched.conversation_id == conversation_id)
        {
            let stamp = if matched.sent_at.is_empty() {
                String::new()
            } else {
                format!(" [{}]", matched.sent_at)
            };
            writeln!(body, "  - {}{}: {}", matched.role, stamp, matched.content)
                .expect("string write is infallible");
        }
    }
    body.trim_end().to_owned()
}

/// Recognise a request to summarize the running conversation by composing
/// meaning roles rather than matching raw per-language phrases (issue #386).
///
/// The universal algorithm is identical for every language: the prompt either
/// (a) carries a complete standalone conversation-summary phrasing, (b) carries
/// an objectless courtesy frame asking for a summary, (c) names a summary
/// directive *together with* a conversation reference, or (d) leads with a bare
/// summary directive (`summarize`, `резюме`, `总结`, …). The prompt is
/// re-normalised first so the boundary-aware matcher sees punctuation collapsed
/// to spaces. Mirror of `asksForConversationSummary` in the browser worker.
fn asks_for_conversation_summary(normalized: &str) -> bool {
    let cleaned = normalize_prompt(normalized);
    let lexicon = seed::lexicon();
    lexicon.mentions_role(seed::ROLE_CONVERSATION_SUMMARY_PHRASE, &cleaned)
        || lexicon.mentions_role(seed::ROLE_CONVERSATION_SUMMARY_COURTESY, &cleaned)
        || (lexicon.mentions_role(seed::ROLE_CONVERSATION_SUMMARY_DIRECTIVE, &cleaned)
            && lexicon.mentions_role(seed::ROLE_CONVERSATION_REFERENCE, &cleaned))
        || summary_directive_leads(&cleaned)
}

/// A bare summary directive standing alone is itself a request to summarize the
/// running conversation ("summarize", "резюме", "总结", …).
///
/// For whitespace-delimited scripts the directive must be the *whole* prompt, so
/// "summarize the article" is left for other handlers (a conversation object is
/// required via the directive∧reference arm instead). For CJK (no word spaces) a
/// leading substring suffices — mirroring the worker's historical `^总结` anchor
/// — which also keeps compounds like "工作总结" (a *work* summary) from being
/// mis-claimed. Surface words come from the `conversation_summary_directive`
/// role in the seed lexicon.
fn summary_directive_leads(cleaned: &str) -> bool {
    seed::lexicon()
        .words_for_role(seed::ROLE_CONVERSATION_SUMMARY_DIRECTIVE)
        .iter()
        .any(|word| {
            if contains_cjk(word) {
                cleaned.starts_with(word.as_str())
            } else {
                cleaned == word.as_str()
            }
        })
}

fn try_summarize_conversation(
    prompt: &str,
    normalized: &str,
    log: &mut EventLog,
) -> Option<SymbolicAnswer> {
    if !asks_for_conversation_summary(normalized) {
        return None;
    }
    let turns: Vec<DialogTurn> = log
        .events()
        .iter()
        .filter_map(|event| match event.kind {
            "prior_turn:user" => Some(DialogTurn::user(event.payload.clone())),
            "prior_turn:assistant" => Some(DialogTurn::assistant(event.payload.clone())),
            _ => None,
        })
        .collect();
    let user_turn_count = turns.iter().filter(|t| t.role == "user").count();
    if user_turn_count == 0 {
        return None;
    }
    let language = detect_language(prompt).slug();
    // Standard mode keeps roughly 50% of the highest-weighted statements; with
    // the dialog bias (user +20, assistant -10) the user's questions dominate
    // the output while still keeping room for any assistant prose worth
    // remembering.
    let config = SummarizationConfig::default()
        .with_mode(SummarizationMode::Standard)
        .with_language(language);
    let summary = summarize_dialog(&turns, &config);
    let title = generate_chat_title(&turns, language);
    let user_turns: Vec<&str> = turns
        .iter()
        .filter(|t| t.role == "user")
        .map(|t| t.text.as_str())
        .collect();
    let mut body = match language {
        "ru" => {
            format!("Резюме разговора: {summary}\n\nЗаголовок: {title}\n\nРеплики пользователя:\n")
        }
        "zh" => format!("对话摘要:{summary}\n\n标题:{title}\n\n用户发言:\n"),
        _ => format!("Conversation summary: {summary}\n\nTitle: {title}\n\nUser turns:\n"),
    };
    for (index, turn) in user_turns.iter().enumerate() {
        writeln!(body, "  {}. {turn}", index + 1).expect("string write is infallible");
    }
    log.append("filter:user", "conversation_summary".to_owned());
    log.append("summarization:mode", "standard".to_owned());
    log.append("summarization:language", language.to_owned());
    log.append("chat_title", title);
    Some(finalize_simple(
        prompt,
        log,
        "summarize_conversation",
        "response:summarize_conversation",
        body.trim_end(),
        0.9,
    ))
}