Skip to content

feat: 跨 App 语气适应(前台 App 注入 LLM context)#117

Merged
appergb merged 1 commit into
mainfrom
feat/front-app-context
May 1, 2026
Merged

feat: 跨 App 语气适应(前台 App 注入 LLM context)#117
appergb merged 1 commit into
mainfrom
feat/front-app-context

Conversation

@appergb
Copy link
Copy Markdown
Collaborator

@appergb appergb commented May 1, 2026

Closes #116

改动

  • coordinator.rs::capture_frontmost_app:macOS NSWorkspace(公开 API,无权限要求)/ Windows GetWindowTextW
  • SessionState.front_app: Option<String> —— begin_session 填充
  • polish.rs::polish/translate_to 新增 front_app: Option<&str> 参数
  • context_premise(working_languages, front_app) 替代原 working_languages_premise,统一组装成 "# 上下文" 块
  • repolish 路径独立调用 capture_frontmost_app

Test plan

  • macOS:在 Mail / 微信 / Xcode 切换录音同一句话,输出风格应不同
  • Windows:window title 注入应不报错
  • 日志能看到 [coord] front_app captured: ...
  • 隐私:日志只有 app 名 + bundle id,无选区

参考 Wispr Flow / Superwhisper Super Mode / Typeless 的"按应用调整语气"
设计:begin_session 捕获前台 app 标签 → polish/translate 调用前作为
"# 上下文\n当前前台应用:…" 拼到 system prompt 头部,让 LLM 自行根据
app 类型调风格,不维护硬编码 app→style 表。

实现
  - coordinator.rs::capture_frontmost_app(macOS NSWorkspace 拿
    localizedName + bundleIdentifier;Windows GetWindowTextW 拿前台
    HWND 标题;Linux/其他平台 None)
  - SessionState 加 front_app: Option<String>,begin_session 填充
  - polish.rs::polish/translate_to 新增 front_app: Option<&str> 参数
  - 原 working_languages_premise 提升为 context_premise,两个字段
    一起拼成 # 上下文 块
  - repolish 路径独立调用 capture_frontmost_app(不沿用原 session 的
    上下文,因为用户重新润色时所处的 app 可能已经不同)

Out of scope(v1)
  - 不读 AX 焦点元素的 selectedText(隐私 + Electron app 兼容性差)
  - 不维护 app→style 映射表(让 LLM 自己理解)
  - Linux 实现跳过

NSWorkspace 是 macOS 公开 API,无需额外权限;Windows 复用现有
foreground HWND 路径,也不增加权限要求。
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @appergb, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3ad57983d8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1549 to +1553
let title = String::from_utf16_lossy(&buf[..copied as usize]);
if title.is_empty() {
None
} else {
Some(title)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace window title with stable app identifier

On Windows, capture_frontmost_app stores the foreground window caption from GetWindowTextW, which commonly contains document/tab names and other user content rather than just the app name. That value is then propagated into LLM context and logs, so sensitive text can be exfiltrated to the provider and persisted in telemetry even though this feature is meant to pass app context. Capture a stable app identity (process/app name) or aggressively reduce the caption to a safe app label before storing it.

Useful? React with 👍 / 👎.

Comment on lines +236 to +239
if let Some(name) = app {
lines.push(format!(
"当前前台应用:{name}。请按这个应用的常见沟通风格调整语气——例如邮件类 app 偏正式、聊天类 app 偏口语、IDE / 文档类 app 偏技术或结构化。\u{4E0D}主动加入与用户原意无关的客套话。"
));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Sanitize front-app text before prompt composition

context_premise interpolates front_app directly into the system prompt. In the Windows path this string is window-title text that can be influenced by untrusted content (for example, a browser page title) and can include newlines/instruction-like payloads, which allows prompt-injection-style overrides of polish/translation behavior. Normalize/escape control characters and quote or constrain this field before embedding it in the prompt.

Useful? React with 👍 / 👎.

@appergb appergb merged commit 5650a9a into main May 1, 2026
2 checks passed
@appergb appergb deleted the feat/front-app-context branch May 1, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] 跨 App 语气适应:把前台 App 注入 LLM polish/translate prompt

1 participant