minutes-core 0.27.0

Core library for minutes — audio capture, transcription, and meeting memory
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
// Included in selection::native to reuse its bounded AX readers and ownership.
use super::super::shared_context::{Observations, Request as ContextRequest};
use crossbeam_channel::{Receiver as ContextReceiver, Sender as ContextSender};
use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool as ContextFlag, Ordering as ContextOrdering};
use std::sync::Arc as ContextArc;

#[link(name = "ApplicationServices", kind = "framework")]
extern "C" {
    fn AXObserverCreate(
        pid: i32,
        callback: unsafe extern "C" fn(Ref, Ref, Ref, *mut c_void),
        observer: *mut Ref,
    ) -> i32;
    fn AXObserverAddNotification(
        observer: Ref,
        element: Ref,
        notification: Ref,
        context: *mut c_void,
    ) -> i32;
    fn AXObserverGetRunLoopSource(observer: Ref) -> *mut c_void;
}
#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
    fn CFRetain(value: Ref) -> Ref;
    fn CFArrayGetTypeID() -> usize;
    fn CFArrayGetCount(array: Ref) -> isize;
    fn CFArrayGetValueAtIndex(array: Ref, index: isize) -> Ref;
    fn CFRunLoopGetCurrent() -> *mut c_void;
    fn CFRunLoopAddSource(run_loop: *mut c_void, source: *mut c_void, mode: Ref);
    fn CFRunLoopRemoveSource(run_loop: *mut c_void, source: *mut c_void, mode: Ref);
    fn CFNumberGetTypeID() -> usize;
    fn CFNumberGetValue(number: Ref, kind: isize, value: *mut c_void) -> bool;
    fn CFRunLoopRunInMode(mode: Ref, seconds: f64, return_after_source: bool) -> i32;
    static kCFRunLoopDefaultMode: Ref;
    fn CFDictionaryGetTypeID() -> usize;
    fn CFDictionaryGetValue(dictionary: Ref, key: Ref) -> Ref;
}

#[link(name = "CoreGraphics", kind = "framework")]
extern "C" {
    fn CGWindowListCopyWindowInfo(options: u32, relative_to: u32) -> Ref;
    static kCGWindowOwnerPID: Ref;
    static kCGWindowName: Ref;
    static kCGWindowNumber: Ref;
}

unsafe fn window_number(pid: i32, title: &str) -> Result<u32, String> {
    let title = std::ffi::CString::new(title).map_err(|_| "Invalid window title")?;
    let title = owned(CFStringCreateWithCString(ptr::null(), title.as_ptr(), UTF8))?;
    let windows = owned(CGWindowListCopyWindowInfo(1 | 16, 0))?;
    if CFGetTypeID(windows.0) != CFArrayGetTypeID() || CFArrayGetCount(windows.0) > 2048 {
        return Err("Window identity budget exceeded; no screenshot taken.".into());
    }
    let number = |dictionary: Ref, key: Ref| -> Option<i64> {
        let value = CFDictionaryGetValue(dictionary, key);
        let mut result = 0i64;
        if !value.is_null()
            && CFGetTypeID(value) == CFNumberGetTypeID()
            && CFNumberGetValue(value, 4, std::ptr::from_mut(&mut result).cast())
        {
            Some(result)
        } else {
            None
        }
    };
    let mut ids = Vec::new();
    for index in 0..CFArrayGetCount(windows.0) {
        let window = CFArrayGetValueAtIndex(windows.0, index);
        if window.is_null()
            || CFGetTypeID(window) != CFDictionaryGetTypeID()
            || number(window, kCGWindowOwnerPID) != Some(i64::from(pid))
        {
            continue;
        }
        let name = CFDictionaryGetValue(window, kCGWindowName);
        if !name.is_null() && CFEqual(name, title.0) != 0 {
            if let Some(id) = number(window, kCGWindowNumber)
                .and_then(|n| u32::try_from(n).ok())
                .filter(|n| *n > 0)
            {
                ids.push(id);
            }
        }
    }
    if ids.len() == 1 {
        Ok(ids[0])
    } else {
        Err("Cannot bind one exact visible window; no screenshot taken and no full-screen fallback.".into())
    }
}

fn bounded_capture_command(command: &mut std::process::Command) -> Result<(), String> {
    let mut child = command
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .map_err(|e| e.to_string())?;
    let began = Instant::now();
    loop {
        match child.try_wait() {
            Ok(Some(status)) => {
                return if status.success() {
                    Ok(())
                } else {
                    Err(format!("Window capture helper failed ({status})"))
                }
            }
            Ok(None) if began.elapsed() < Duration::from_secs(1) => {
                std::thread::sleep(Duration::from_millis(20))
            }
            result => {
                let _ = child.kill();
                let _ = child.wait();
                return Err(format!(
                    "Window capture helper timed out or failed: {result:?}"
                ));
            }
        }
    }
}

unsafe fn capture_context_window(
    app: &Owned,
    window: &Owned,
    pid: i32,
    title: &str,
    document: &str,
) -> Result<Vec<u8>, String> {
    if !crate::screen::check_screen_permission() {
        return Err(
            "Screen Recording permission is unavailable; accessibility context remains available."
                .into(),
        );
    }
    same_context(app, window, pid, title, document)?;
    let id = window_number(pid, title)?;
    let temp = tempfile::tempdir().map_err(|e| e.to_string())?;
    let path = temp.path().join("window.png");
    bounded_capture_command(
        crate::engine_process::command("/usr/sbin/screencapture")
            .args(["-x", "-o", "-l", &id.to_string(), "-t", "png"])
            .arg(&path),
    )?;
    same_context(app, window, pid, title, document)?;
    if window_number(pid, title)? != id {
        return Err("Window identity changed during capture; image discarded.".into());
    }
    bounded_capture_command(
        crate::engine_process::command("/usr/bin/sips")
            .args(["--resampleWidth", "1440"])
            .arg(&path),
    )?;
    if std::fs::metadata(&path).map_err(|e| e.to_string())?.len() > 5_000_000 {
        return Err("Window image exceeds the sharing budget.".into());
    }
    std::fs::read(path).map_err(|e| e.to_string())
}

unsafe extern "C" fn context_changed(
    _observer: Ref,
    _element: Ref,
    _notification: Ref,
    context: *mut c_void,
) {
    if !context.is_null() {
        unsafe { &*context.cast::<ContextFlag>() }.store(true, ContextOrdering::Release);
    }
}

struct ContextObserver {
    observer: Owned,
    source: *mut c_void,
    run_loop: *mut c_void,
}
impl Drop for ContextObserver {
    fn drop(&mut self) {
        unsafe {
            CFRunLoopRemoveSource(self.run_loop, self.source, kCFRunLoopDefaultMode);
        }
    }
}

unsafe fn subscribe_context(
    observer: &Owned,
    element: &Owned,
    name: &CStr,
    dirty: &mut ContextFlag,
) -> bool {
    let Ok(notification) = owned(CFStringCreateWithCString(ptr::null(), name.as_ptr(), UTF8))
    else {
        return false;
    };
    matches!(
        AXObserverAddNotification(
            observer.0,
            element.0,
            notification.0,
            std::ptr::from_mut(dirty).cast()
        ),
        0 | -25209
    )
}

unsafe fn same_context(
    app: &Owned,
    window: &Owned,
    pid: i32,
    title: &str,
    document: &str,
) -> Result<(), String> {
    if NSWorkspace::sharedWorkspace()
        .frontmostApplication()
        .is_none_or(|a| a.processIdentifier() != pid)
    {
        return Err(
            "Shared app lost focus. Sharing ended; explicitly share the desired window again."
                .into(),
        );
    }
    let current = element(app, c"AXFocusedWindow")?;
    if CFEqual(window.0, current.0) == 0
        || string(window, c"AXTitle")? != title
        || string(window, c"AXDocument").unwrap_or_default() != document
    {
        return Err(
            "Shared window or document changed. Sharing ended; old references are invalid.".into(),
        );
    }
    Ok(())
}

unsafe fn context_snapshot(
    app: &Owned,
    window: &Owned,
    bundle: &str,
) -> Result<(Value, Vec<Owned>), String> {
    let began = Instant::now();
    let focused = element(app, c"AXFocusedUIElement")?;
    let role = string(&focused, c"AXRole")?;
    let subrole = string(&focused, c"AXSubrole").unwrap_or_default();
    if role == "AXSecureTextField"
        || subrole == "AXSecureTextField"
        || (role == "AXTextField" && subrole.is_empty())
    {
        return Err("Secure or unverifiable focused field; no shared context captured.".into());
    }
    let selected: String = string(&focused, c"AXSelectedText")
        .unwrap_or_default()
        .chars()
        .take(512)
        .collect();
    let title = string(window, c"AXTitle")?;
    let document = string(window, c"AXDocument").unwrap_or_default();
    let mut queue = VecDeque::from([(owned(CFRetain(window.0))?, 0)]);
    let mut nodes = Vec::new();
    let mut elements = Vec::new();
    let mut remaining = 4096usize;
    while let Some((node, depth)) = queue.pop_front() {
        if nodes.len() >= 24 || began.elapsed() >= Duration::from_millis(800) || remaining < 512 {
            break;
        }
        let role = string(&node, c"AXRole").unwrap_or_default();
        let subrole = string(&node, c"AXSubrole").unwrap_or_default();
        if role == "AXSecureTextField"
            || subrole == "AXSecureTextField"
            || (role == "AXTextField" && subrole.is_empty())
        {
            continue;
        }
        let label = string(&node, c"AXTitle")
            .or_else(|_| string(&node, c"AXDescription"))
            .unwrap_or_default();
        // Editable documents are represented by their selection, never their
        // entire AXValue. Static labels and scalar controls remain bounded.
        let value = if matches!(
            role.as_str(),
            "AXStaticText" | "AXSlider" | "AXCheckBox" | "AXPopUpButton"
        ) {
            string(&node, c"AXValue").unwrap_or_else(|_| {
                let Ok(value) = attribute(&node, c"AXValue") else {
                    return String::new();
                };
                if CFGetTypeID(value.0) == CFBooleanGetTypeID() {
                    return (CFBooleanGetValue(value.0) != 0).to_string();
                }
                let mut number = 0.0f64;
                if CFGetTypeID(value.0) == CFNumberGetTypeID()
                    && CFNumberGetValue(value.0, 13, std::ptr::from_mut(&mut number).cast())
                    && number.is_finite()
                {
                    number.to_string()
                } else {
                    String::new()
                }
            })
        } else {
            String::new()
        };
        let label: String = label.chars().take(80).collect();
        let value: String = value.chars().take(120).collect();
        remaining = remaining.saturating_sub(label.len() + value.len() + role.len());
        elements.push(json!({"role":role,"label":label,"value":value}));
        if depth < 7 {
            if let Ok(children) = attribute(&node, c"AXChildren") {
                if CFGetTypeID(children.0) == CFArrayGetTypeID() {
                    for index in 0..CFArrayGetCount(children.0).clamp(0, 32) {
                        if queue.len() + nodes.len() >= 64 {
                            break;
                        }
                        let child = CFArrayGetValueAtIndex(children.0, index);
                        if !child.is_null() && CFGetTypeID(child) == AXUIElementGetTypeID() {
                            queue.push_back((owned(CFRetain(child))?, depth + 1));
                        }
                    }
                }
            }
        }
        nodes.push(node);
    }
    nodes.push(focused);
    Ok((
        json!({"bundle_id":bundle,"window_title":title.chars().take(160).collect::<String>(),"document":document.chars().take(256).collect::<String>(),
        "focused_role":role,"selected_text":selected,"elements":elements,"bounded":true}),
        nodes,
    ))
}

pub fn observe_window(
    target: &str,
    duration: Duration,
    grant: u64,
    stop: ContextArc<ContextFlag>,
    requests: ContextReceiver<ContextRequest>,
    ready: ContextSender<Result<Value, String>>,
) {
    let started = Instant::now();
    let revoked = || {
        stop.load(ContextOrdering::Acquire) || super::super::shared_context::generation() != grant
    };
    let result: Result<(), String> = autoreleasepool(|_| unsafe {
        if !AXIsProcessTrusted() {
            return Err(
                "Accessibility permission is unavailable; no shared window started.".to_string(),
            );
        }
        let application = NSWorkspace::sharedWorkspace()
            .frontmostApplication()
            .ok_or("No frontmost app")?;
        let bundle = application
            .bundleIdentifier()
            .ok_or("App has no stable identity")?
            .to_string();
        let name = application
            .localizedName()
            .map(|v| v.to_string())
            .unwrap_or_default();
        if bundle != target && !name.eq_ignore_ascii_case(target) {
            return Err("The named app must be frontmost before sharing its window.".into());
        }
        let pid = application.processIdentifier();
        let app = owned(AXUIElementCreateApplication(pid))?;
        AXUIElementSetMessagingTimeout(app.0, 0.1);
        let window = element(&app, c"AXFocusedWindow")?;
        let title = string(&window, c"AXTitle")?;
        let document = string(&window, c"AXDocument").unwrap_or_default();
        let (initial, nodes) = context_snapshot(&app, &window, &bundle)?;
        same_context(&app, &window, pid, &title, &document)?;
        let mut dirty = Box::new(ContextFlag::new(false));
        let mut observer = ptr::null();
        let status = AXObserverCreate(pid, context_changed, &mut observer);
        if status != 0 {
            return Err(format!(
                "Accessibility event observer unavailable ({status}); no ongoing sharing."
            ));
        }
        let observer = owned(observer)?;
        let source = AXObserverGetRunLoopSource(observer.0);
        if source.is_null() {
            return Err("Accessibility observer has no event source".into());
        }
        let run_loop = CFRunLoopGetCurrent();
        CFRunLoopAddSource(run_loop, source, kCFRunLoopDefaultMode);
        let observer = ContextObserver {
            observer,
            source,
            run_loop,
        };
        let mut subscribed = 0;
        for node in std::iter::once(&app)
            .chain(std::iter::once(&window))
            .chain(nodes.iter())
        {
            for notification in [
                c"AXFocusedWindowChanged",
                c"AXFocusedUIElementChanged",
                c"AXSelectedTextChanged",
                c"AXValueChanged",
                c"AXTitleChanged",
                c"AXUIElementDestroyed",
            ] {
                if subscribe_context(&observer.observer, node, notification, &mut dirty) {
                    subscribed += 1;
                }
            }
        }
        if subscribed == 0 {
            return Err(
                "This app does not expose accessibility change notifications; no ongoing sharing."
                    .into(),
            );
        }
        same_context(&app, &window, pid, &title, &document)?;
        if revoked() || started.elapsed() >= duration {
            return Err(
                "Sharing expired or was cancelled during setup; no context released.".into(),
            );
        }
        let mut observations = Observations::new(initial);
        ready
            .send(Ok(observations.receipt(
                duration.saturating_sub(started.elapsed()).as_secs(),
            )))
            .map_err(|_| "Shared-window request was cancelled")?;
        while !revoked() && started.elapsed() < duration {
            CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
            if revoked() {
                break;
            }
            // Metadata-only target checks do not read other apps or their text.
            same_context(&app, &window, pid, &title, &document)?;
            if dirty.swap(false, ContextOrdering::AcqRel) {
                let (next, _) = context_snapshot(&app, &window, &bundle)?;
                same_context(&app, &window, pid, &title, &document)?;
                observations.update(next, "accessibility_event");
            }
            for request in requests.try_iter() {
                if revoked() || started.elapsed() >= duration {
                    break;
                }
                match request {
                    ContextRequest::Inspect(reply) => {
                        let (next, _) = context_snapshot(&app, &window, &bundle)?;
                        same_context(&app, &window, pid, &title, &document)?;
                        if revoked() || started.elapsed() >= duration {
                            let _ = reply.send(Err(
                                "Sharing ended during inspection; context withheld.".into(),
                            ));
                            break;
                        }
                        observations.update(next, "explicit_inspection");
                        let _ = reply.send(Ok(observations
                            .receipt(duration.saturating_sub(started.elapsed()).as_secs())));
                    }
                    ContextRequest::Capture(reply) => {
                        let image = capture_context_window(&app, &window, pid, &title, &document);
                        let result = image.and_then(|bytes| {
                            if revoked() || started.elapsed() >= duration {
                                return Err("Sharing ended during capture; image discarded.".into());
                            }
                            same_context(&app, &window, pid, &title, &document)?;
                            let (next, _) = context_snapshot(&app, &window, &bundle)?;
                            same_context(&app, &window, pid, &title, &document)?;
                            if revoked() || started.elapsed() >= duration {
                                return Err(
                                    "Sharing ended during capture inspection; image discarded."
                                        .into(),
                                );
                            }
                            observations.update(next, "explicit_targeted_vision");
                            Ok((
                                observations
                                    .receipt(duration.saturating_sub(started.elapsed()).as_secs()),
                                bytes,
                            ))
                        });
                        let _ = reply.send(result);
                    }
                }
            }
        }
        Err("Shared-window grant stopped or expired; share the window again if needed.".into())
    });
    stop.store(true, ContextOrdering::Release);
    if let Err(reason) = result {
        super::super::shared_context::invalidate_if(grant);
        let _ = ready.try_send(Err(reason.clone()));
        super::super::shared_context::reject_pending(&requests, &reason);
    }
}