Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions openless-all/app/crates/openless-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10624,9 +10624,11 @@ impl OpenLessBackend {
.as_deref()
.filter(|id| !id.trim().is_empty());
// Capture target metadata before any asynchronous host or credential access.
let input_location = (foreground_output && preferences.cursor_context_enabled)
.then(|| host_context.input_location())
.flatten();
let input_location = if foreground_output && preferences.cursor_context_enabled {
host_context.capture_input_location().await
} else {
None
};
let (mut assigned_ids, input_scene_prefix) = input_app
.as_ref()
.map(|app| self.app_styles.selection(app, input_location.as_deref()))
Expand Down Expand Up @@ -15438,6 +15440,70 @@ mod tests {
);
}

#[tokio::test]
async fn frozen_scene_reaches_the_request_and_preserves_the_explicit_style_and_privacy_switch() {
struct SceneHost {
app: crate::app_style_routing::InputApp,
kind: Option<crate::browser_scene::BrowserInputKind>,
reads: Arc<std::sync::atomic::AtomicUsize>,
}
impl HostContextAdapter for SceneHost {
fn input_app(&self) -> Option<crate::app_style_routing::InputApp> { Some(self.app.clone()) }
fn capture_input_location(&self) -> BoxFuture<'static, Option<String>> {
self.reads.fetch_add(1, Ordering::AcqRel);
let location = self.kind.and_then(|kind| kind.location()).map(str::to_owned);
Box::pin(async move { location })
}
fn capture(&self, _: bool) -> BoxFuture<'static, Result<HostContextCapture, BackendError>> {
Box::pin(async { Ok(HostContextCapture::default()) })
}
}
let (backend, _) = backend();
let mut preferences = backend.get_preferences();
preferences.cursor_context_enabled = true;
backend.set_preferences(preferences.clone()).unwrap();
let options = DictationStartOptions {
style_pack_id: Some(preferences.active_style_pack_id.clone()),
..Default::default()
};
let reads = Arc::new(std::sync::atomic::AtomicUsize::new(0));
for (bundle, name, agent) in [
("com.todesktop.230313mzl4w4u92", "Cursor", true),
("com.stablyai.orca", "Orca", true),
("com.microsoft.VSCode", "VS Code", false),
("example.unknown", "Cursor", false),
] {
let host = SceneHost {
app: crate::app_style_routing::InputApp::macos(bundle, Some(name)).unwrap(),
kind: None, reads: reads.clone(),
};
let context = backend.capture_dictation_context_with_host(
&options, DictationContextPurpose::Dictation, &host,
).await.unwrap();
assert_eq!(context.polish.style_pack_id, preferences.active_style_pack_id);
let (system, _) = context.effective_polish_prompts("Fictional rename task");
assert!(system.starts_with(&format!("当前应用:{name}\n应用标识:macos:{bundle}\n")));
assert_eq!(system.matches("当前应用:").count(), 1);
assert_eq!(system.contains("本次输入将交给编程 Agent"), agent);
}
let host = SceneHost {
app: crate::app_style_routing::InputApp::macos("com.google.Chrome", Some("Chrome")).unwrap(),
kind: Some(crate::browser_scene::BrowserInputKind::WebAskAnything), reads: reads.clone(),
};
let context = backend.capture_dictation_context_with_host(
&options, DictationContextPurpose::Dictation, &host,
).await.unwrap();
assert!(context.effective_polish_prompts("Fictional task").0.contains("网页 Ask anything 输入框"));
let before = reads.load(Ordering::Acquire);
preferences.cursor_context_enabled = false;
backend.set_preferences(preferences).unwrap();
let context = backend.capture_dictation_context_with_host(
&options, DictationContextPurpose::Dictation, &host,
).await.unwrap();
assert_eq!(reads.load(Ordering::Acquire), before);
assert!(!context.effective_polish_prompts("Fictional task").0.contains("网页 Ask anything 输入框"));
}

#[tokio::test]
async fn realtime_recording_fault_finishes_once_and_rejects_stale_reports() {
let (backend, _) = backend();
Expand Down
45 changes: 42 additions & 3 deletions openless-all/app/crates/openless-core/src/app_style_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub(crate) fn input_scene_prefix(
guidance: Option<&str>,
location: Option<&str>,
) -> String {
let recognized = app.and_then(crate::input_scene::recognize);
let known = |value: Option<&str>| {
value
.filter(|text| !text.trim().is_empty())
Expand All @@ -123,16 +124,18 @@ pub(crate) fn input_scene_prefix(
};
let name = known(app.map(|app| app.name.as_str()));
let id = known(app.map(|app| app.id.as_str()));
let category = known(category);
let purpose = known(purpose);
let category = known(category.or_else(|| recognized.map(|rule| rule.category.label())));
let purpose = known(purpose.filter(|value| !value.trim().is_empty()).or_else(|| recognized.map(|rule| rule.purpose)));
let location = location
.filter(|text| !text.trim().is_empty())
.unwrap_or("应用内输入,无法正确确定");
let reference = guidance
.filter(|text| !text.trim().is_empty())
.map(|text| format!("分类用途参考:{text}。"))
.unwrap_or_default();
format!("当前应用:{name}\n应用标识:{id}\n应用分类:{category}\n应用主要用途:{purpose}\n当前输入位置:{location}\n本次输入场景:在上述目标应用内输入。{reference}具体输入用途以本次语音为准,仅按已知信息理解术语与对象,按用户所选风格组织内容;不据此增加开发任务、技术方案或回答问题。\n请结合上述信息理解用户的输入场景,再按照后续提供的用户风格要求处理本次语音内容。保持用户的原意,不自行增加需求或推断无法确定的信息。")
let policy = recognized.and_then(|rule| rule.output_policy.guidance())
.map(|guidance| format!("\n{guidance}")).unwrap_or_default();
format!("当前应用:{name}\n应用标识:{id}\n应用分类:{category}\n应用主要用途:{purpose}\n当前输入位置:{location}\n本次输入场景:在上述目标应用内输入。{reference}具体输入用途以本次语音为准,仅按已知信息理解术语与对象,按用户所选风格组织内容;不据此增加开发任务、技术方案或回答问题。\n请结合上述信息理解用户的输入场景,再按照后续提供的用户风格要求处理本次语音内容。保持用户的原意,不自行增加需求或推断无法确定的信息。{policy}")
}

pub(crate) struct AppStyleStore {
Expand Down Expand Up @@ -388,6 +391,42 @@ impl RoutingState {
mod tests {
use super::*;

#[test]
fn shipped_agent_scene_reaches_the_real_routing_prefix_without_changing_style() {
let store = AppStyleStore::in_memory();
for bundle in ["com.todesktop.230313mzl4w4u92", "com.stablyai.orca"] {
let app = InputApp::macos(bundle, Some("untrusted display name")).unwrap();
let (assigned, prefix) = store.selection(&app, None);
assert!(assigned.is_empty());
assert!(prefix.contains("本次输入将交给编程 Agent"));
assert!(prefix.contains("用户所选风格"));
assert!(prefix.starts_with(&format!(
"当前应用:untrusted display name\n应用标识:macos:{bundle}\n"
)));
}
let unknown = InputApp::macos("example.unknown", Some("Cursor")).unwrap();
assert!(!store.selection(&unknown, None).1.contains("本次输入将交给编程 Agent"));
}

#[test]
fn routing_preserves_frozen_editor_names_and_empty_name_fallback() {
let store = AppStyleStore::in_memory();
for (name, expected) in [
("VS Code", "VS Code"),
("Renamed Editor", "Renamed Editor"),
("", "未知"),
(" \t", "未知"),
] {
let app = InputApp { id: "macos:com.microsoft.VSCode".into(), name: name.into() };
let (assigned, prefix) = store.selection(&app, None);
assert!(assigned.is_empty());
assert!(prefix.starts_with(&format!(
"当前应用:{expected}\n应用标识:macos:com.microsoft.VSCode\n应用分类:编程\n"
)));
assert_eq!(prefix.matches("当前应用:").count(), 1);
}
}

#[test]
fn scene_unknowns_do_not_infer_purpose_or_location() {
let unknown = input_scene_prefix(None, None, None, None, None);
Expand Down
11 changes: 11 additions & 0 deletions openless-all/app/crates/openless-core/src/ask_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct AppMetadata {
display_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
app_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
scene: Option<crate::input_scene::ApplicationScene>,
}

impl AppMetadata {
Expand All @@ -47,9 +49,15 @@ impl AppMetadata {
(!value.trim().is_empty()).then_some(value)
}
let app_id = app_id.and_then(bounded);
let scene = app_id.as_deref()
.and_then(|id| crate::app_style_routing::InputApp::macos(id, None))
.as_ref()
.and_then(crate::input_scene::recognize)
.map(crate::input_scene::ApplicationRule::scene);
Some(Self {
display_name: bounded(display_name).or_else(|| app_id.clone())?,
app_id,
scene,
})
}
}
Expand Down Expand Up @@ -176,6 +184,9 @@ pub struct TurnContext {
/// Called at model dispatch, not during input entry. Refresh before another
/// dispatch if Ask authorization could have changed; don't cache these facts.
pub trait ContextProvider: Send + Sync {
/// Bind invocation-scoped authorization to the actual admitted turn. Hosts
/// must discard a previous turn's private references on the next call.
fn begin_turn(&self, _key: crate::ask::AskTurnKey) {}
fn context_for_turn(&self, conversation_id: Uuid) -> TurnContext;
}

Expand Down
Loading
Loading