-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: improve communication with new Selection Command Hub via background_script #398
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
Merged
ujiro99
merged 19 commits into
main
from
copilot/improve-communication-with-selection-hub
May 10, 2026
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b77d410
Initial plan
Copilot 43d5dc9
feat: improve hub communication via background_script
Copilot 27e7907
fix: address code review feedback on useCommandHubBridge and backgrou…
Copilot 5216e19
fix: add error handling for async calls in HubBridge and background_s…
Copilot 9763e21
feat: use externally_connectable and onMessageExternal for hub messaging
Copilot 161005d
fix: add error handling for onMessageExternal IPC calls and removeLis…
Copilot 1d355bb
Merge branch 'main' into copilot/improve-communication-with-selection…
Copilot 9779896
Fix: Enable adding and deleting commands to work correctly.
ujiro99 6c34838
Update: Refactoring.
ujiro99 773351d
Update: Enable sharing commands via port.postMessage.
ujiro99 6245089
Update: Separate HUB-related logic into a different file.
ujiro99 307b775
fix: use let tabId to avoid ReferenceError in onPortConnect and fix r…
Copilot ded9a56
fix: address code review feedback on background.ts and background_scr…
Copilot 54eafae
fix: address code review - remove non-null assertion and add error ha…
Copilot 4b39fef
fix: rename install_id to client_id and clarify portConnect vs onPort…
Copilot bd6d42f
Fix: Property name.
ujiro99 cefad1b
Update: Remove localhost from `externally_connectable.matches`.
ujiro99 9a92e95
fix: fix Ipc.callListener type, hubOrigin calculation, and hubShare e…
Copilot 9b3a15f
test: add SH-08 error case test for shareCommandToHub catch handler
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,3 @@ | ||
| import { useCommandHubBridge } from "@/hooks/useCommandHubBridge" | ||
|
|
||
| export const CommandHub = (): JSX.Element => { | ||
| useCommandHubBridge() | ||
| return <></> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
packages/extension/src/components/option/editor/commandChangedDetector.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { describe, it, expect } from "vitest" | ||
| import { hasCommandChanged } from "./commandChangedDetector" | ||
| import { OPEN_MODE, PAGE_ACTION_OPEN_MODE } from "@/const" | ||
| import type { | ||
| SearchCommand, | ||
| PageActionCommand, | ||
| AiPromptCommand, | ||
| PageActionOption, | ||
| } from "@/types" | ||
|
|
||
| const makeSearchCommand = ( | ||
| overrides: Partial<SearchCommand> = {}, | ||
| ): SearchCommand => ({ | ||
| id: "cmd-1", | ||
| title: "Google", | ||
| iconUrl: "https://example.com/icon.png", | ||
| openMode: OPEN_MODE.POPUP, | ||
| searchUrl: "https://example.com/search?q=%s", | ||
| ...overrides, | ||
| }) | ||
|
|
||
| const makePageActionOption = ( | ||
| overrides: Partial<PageActionOption> = {}, | ||
| ): PageActionOption => ({ | ||
| startUrl: "https://example.com", | ||
| openMode: PAGE_ACTION_OPEN_MODE.POPUP, | ||
| steps: [], | ||
| ...overrides, | ||
| }) | ||
|
|
||
| const makePageActionCommand = ( | ||
| overrides: Partial<Omit<PageActionCommand, "pageActionOption">> & { | ||
| pageActionOption?: Partial<PageActionOption> | ||
| } = {}, | ||
| ): PageActionCommand => { | ||
| const { pageActionOption, ...rest } = overrides | ||
| return { | ||
| id: "cmd-2", | ||
| title: "Example Action", | ||
| iconUrl: "https://example.com/icon.png", | ||
| openMode: OPEN_MODE.PAGE_ACTION, | ||
| pageActionOption: makePageActionOption(pageActionOption), | ||
| ...rest, | ||
| } | ||
| } | ||
|
|
||
| const makeAiPromptCommand = ( | ||
| overrides: Partial<AiPromptCommand> = {}, | ||
| ): AiPromptCommand => ({ | ||
| id: "cmd-3", | ||
| title: "AI Summarize", | ||
| iconUrl: "https://example.com/icon.png", | ||
| openMode: OPEN_MODE.AI_PROMPT, | ||
| aiPromptOption: { | ||
| serviceId: "chatgpt", | ||
| prompt: "Summarize: {text}", | ||
| openMode: OPEN_MODE.POPUP, | ||
| }, | ||
| ...overrides, | ||
| }) | ||
|
|
||
| describe("hasCommandChanged", () => { | ||
| describe("SearchCommand", () => { | ||
| it("returns false when searchUrl is unchanged", () => { | ||
| const cmd = makeSearchCommand() | ||
| expect(hasCommandChanged(cmd, cmd.searchUrl!, undefined, "")).toBe(false) | ||
| }) | ||
|
|
||
| it("returns true when searchUrl is changed", () => { | ||
| const cmd = makeSearchCommand() | ||
| expect( | ||
| hasCommandChanged(cmd, "https://bing.com/search?q=%s", undefined, ""), | ||
| ).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe("PageActionCommand", () => { | ||
| it("returns false when pageActionOption is unchanged", () => { | ||
| const cmd = makePageActionCommand() | ||
| expect(hasCommandChanged(cmd, "", { ...cmd.pageActionOption }, "")).toBe( | ||
| false, | ||
| ) | ||
| }) | ||
|
|
||
| it("returns true when startUrl is changed", () => { | ||
| const cmd = makePageActionCommand() | ||
| expect( | ||
| hasCommandChanged( | ||
| cmd, | ||
| "", | ||
| { ...cmd.pageActionOption, startUrl: "https://other.com" }, | ||
| "", | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it("returns true when pageUrl is changed", () => { | ||
| const cmd = makePageActionCommand() | ||
| expect( | ||
| hasCommandChanged( | ||
| cmd, | ||
| "", | ||
| { ...cmd.pageActionOption, pageUrl: "https://example.com/*" }, | ||
| "", | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it("returns false when only openMode is changed", () => { | ||
| const cmd = makePageActionCommand() | ||
| expect( | ||
| hasCommandChanged( | ||
| cmd, | ||
| "", | ||
| { ...cmd.pageActionOption, openMode: PAGE_ACTION_OPEN_MODE.TAB }, | ||
| "", | ||
| ), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it("does not throw when currentPageActionOption is null, returns true (differs from saved)", () => { | ||
| const cmd = makePageActionCommand() | ||
| expect(hasCommandChanged(cmd, "", null, "")).toBe(true) | ||
| }) | ||
|
|
||
| it("does not throw when currentPageActionOption is undefined, returns true (differs from saved)", () => { | ||
| const cmd = makePageActionCommand() | ||
| expect(hasCommandChanged(cmd, "", undefined, "")).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe("AiPromptCommand", () => { | ||
| it("returns false when prompt is unchanged", () => { | ||
| const cmd = makeAiPromptCommand() | ||
| expect( | ||
| hasCommandChanged(cmd, "", undefined, cmd.aiPromptOption.prompt), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it("returns true when prompt is changed", () => { | ||
| const cmd = makeAiPromptCommand() | ||
| expect(hasCommandChanged(cmd, "", undefined, "Translate: {text}")).toBe( | ||
| true, | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("other command types", () => { | ||
| it("returns false for CopyCommand", () => { | ||
| const cmd = makeSearchCommand({ openMode: OPEN_MODE.COPY }) | ||
| expect(hasCommandChanged(cmd, "", undefined, "")).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
27 changes: 27 additions & 0 deletions
27
packages/extension/src/components/option/editor/commandChangedDetector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { isSearchType, isPageActionType, isAiPromptType } from "@/types/schema" | ||
| import type { SelectionCommand, PageActionOption } from "@/types" | ||
|
|
||
| /** | ||
| * Returns true when the current form values differ from the saved command, | ||
| * ignoring pageActionOption.openMode (which represents display preference, | ||
| * not the command's core behavior). | ||
| */ | ||
| export function hasCommandChanged( | ||
| command: SelectionCommand, | ||
| currentSearchUrl: string, | ||
| currentPageActionOption: Partial<PageActionOption> | null | undefined, | ||
| currentAiPromptPrompt: string, | ||
| ): boolean { | ||
| if (isSearchType(command)) { | ||
| return currentSearchUrl !== command.searchUrl | ||
| } | ||
| if (isPageActionType(command)) { | ||
| const { openMode: _a, ...pao } = currentPageActionOption ?? {} | ||
| const { openMode: _b, ...cmdPao } = command.pageActionOption | ||
| return JSON.stringify(pao) !== JSON.stringify(cmdPao) | ||
| } | ||
| if (isAiPromptType(command)) { | ||
| return currentAiPromptPrompt !== command.aiPromptOption.prompt | ||
| } | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.