-
-
Notifications
You must be signed in to change notification settings - Fork 93
fix: sanitize terminal app accessibility context to prevent garbled transcription #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
506c5fd
38d0498
e2b5c5c
71f20da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,18 @@ import { | |||||||||||||||||
| type CloudErrorResponse, | ||||||||||||||||||
| } from "../../../types/error"; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Strip ANSI escape sequences and control characters from accessibility text. | ||||||||||||||||||
| // Terminal apps expose raw terminal output via the accessibility API which | ||||||||||||||||||
| // contains escape codes that degrade transcription quality. | ||||||||||||||||||
| const ANSI_REGEX = | ||||||||||||||||||
| // eslint-disable-next-line no-control-regex | ||||||||||||||||||
| /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><~]/g; | ||||||||||||||||||
|
|
||||||||||||||||||
| function stripAnsiSequences(text: string | undefined | null): string | undefined { | ||||||||||||||||||
| if (!text) return undefined; | ||||||||||||||||||
| return text.replace(ANSI_REGEX, ""); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+25
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The If this is intentional (empty and absent are equivalent), this is fine. Otherwise, a narrower null-check would preserve empty strings: ♻️ Suggested fix function stripAnsiSequences(text: string | undefined | null): string | undefined {
- if (!text) return undefined;
+ if (text == null) return undefined;
return text.replace(ANSI_REGEX, "");
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| // Type guard to validate error codes from server | ||||||||||||||||||
| const isValidErrorCode = (code: string | undefined): code is ErrorCode => | ||||||||||||||||||
| code !== undefined && Object.values(ErrorCodes).includes(code as ErrorCode); | ||||||||||||||||||
|
|
@@ -283,28 +295,35 @@ export class AmicalCloudProvider implements TranscriptionProvider { | |||||||||||||||||
| enabled: enableFormatting, | ||||||||||||||||||
| }, | ||||||||||||||||||
| sharedContext: this.currentAccessibilityContext | ||||||||||||||||||
| ? { | ||||||||||||||||||
| selectedText: | ||||||||||||||||||
| this.currentAccessibilityContext.context?.textSelection | ||||||||||||||||||
| ?.selectedText, | ||||||||||||||||||
| beforeText: | ||||||||||||||||||
| this.currentAccessibilityContext.context?.textSelection | ||||||||||||||||||
| ?.preSelectionText, | ||||||||||||||||||
| afterText: | ||||||||||||||||||
| this.currentAccessibilityContext.context?.textSelection | ||||||||||||||||||
| ?.postSelectionText, | ||||||||||||||||||
| appType: detectApplicationType( | ||||||||||||||||||
| ? (() => { | ||||||||||||||||||
| const appType = detectApplicationType( | ||||||||||||||||||
| this.currentAccessibilityContext, | ||||||||||||||||||
| ), | ||||||||||||||||||
| appBundleId: | ||||||||||||||||||
| this.currentAccessibilityContext.context?.application | ||||||||||||||||||
| ?.bundleIdentifier, | ||||||||||||||||||
| appName: | ||||||||||||||||||
| this.currentAccessibilityContext.context?.application?.name, | ||||||||||||||||||
| appUrl: | ||||||||||||||||||
| this.currentAccessibilityContext.context?.windowInfo?.url, | ||||||||||||||||||
| surroundingContext: "", // Empty for now, future enhancement | ||||||||||||||||||
| } | ||||||||||||||||||
| ); | ||||||||||||||||||
| const isTerminal = appType === "terminal"; | ||||||||||||||||||
| return { | ||||||||||||||||||
| selectedText: stripAnsiSequences( | ||||||||||||||||||
| this.currentAccessibilityContext!.context?.textSelection | ||||||||||||||||||
| ?.selectedText, | ||||||||||||||||||
| ), | ||||||||||||||||||
| beforeText: stripAnsiSequences( | ||||||||||||||||||
| this.currentAccessibilityContext!.context?.textSelection | ||||||||||||||||||
| ?.preSelectionText, | ||||||||||||||||||
| ), | ||||||||||||||||||
| afterText: stripAnsiSequences( | ||||||||||||||||||
| this.currentAccessibilityContext!.context?.textSelection | ||||||||||||||||||
| ?.postSelectionText, | ||||||||||||||||||
| ), | ||||||||||||||||||
| appType: isTerminal ? "default" : appType, | ||||||||||||||||||
| appBundleId: | ||||||||||||||||||
| this.currentAccessibilityContext!.context?.application | ||||||||||||||||||
| ?.bundleIdentifier, | ||||||||||||||||||
| appName: | ||||||||||||||||||
| this.currentAccessibilityContext!.context?.application?.name, | ||||||||||||||||||
| appUrl: | ||||||||||||||||||
| this.currentAccessibilityContext!.context?.windowInfo?.url, | ||||||||||||||||||
| surroundingContext: "", | ||||||||||||||||||
| }; | ||||||||||||||||||
| })() | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||
| : undefined, | ||||||||||||||||||
| }), | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty-string entries silently suppress default rules/examples for terminal apps.
APP_TYPE_RULES.terminalandAPP_TYPE_EXAMPLES.terminalare"". Because??only falls through onnull/undefined(not empty string), the template on lines 226 and 231 will inject an empty string rather than the default rules/examples:If the intent is for terminal apps to use no app-specific rules (relying on universal rules only), this is fine but worth a brief comment. If the intent is to fall back to
defaultrules, change the values toundefinedor use||instead of??.Also applies to: 110-111
🤖 Prompt for AI Agents