-
Notifications
You must be signed in to change notification settings - Fork 5k
feat(omp): add extension-based rewrite integration for Oh My Pi #1365
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ff8eb32
feat(omp): add extension-based rewrite integration for Oh My Pi
makoMakoGo d2127c5
fix(omp): address extension review feedback
makoMakoGo 6d13b37
fix(omp): keep missing rtk warning visible
makoMakoGo 5988886
fix(omp): report missing rtk via status line
makoMakoGo 32dd7e6
refactor(init): model omp as agent target
makoMakoGo 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
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
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,12 @@ | ||
| # Oh My Pi Hooks | ||
|
|
||
| > Part of [`hooks/`](../README.md) — see also [`src/hooks/`](../../src/hooks/README.md) for installation code | ||
|
|
||
| ## Specifics | ||
|
|
||
| - TypeScript extension module, not a shell hook or rules file | ||
| - Sets its OMP extension display label to `RTK` | ||
| - Installs to `./.omp/extensions/rtk.ts` with `rtk init --agent omp`, or to `~/.omp/agent/extensions/rtk.ts` with `rtk init -g --agent omp` | ||
| - Intercepts OMP `tool_call` events for the `bash` tool and delegates rewrite decisions to `rtk rewrite` | ||
| - Requires Bun runtime (uses `Bun.which` and `Bun.spawn`); OMP currently ships with Bun | ||
| - Multi-extension chaining: OMP dispatches `tool_call` handlers sequentially. Downstream handlers observe the RTK-rewritten `event.input.command` only when RTK actually rewrites it |
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,101 @@ | ||
| // RTK - Rust Token Killer | ||
| // OMP extension: rewrite bash tool calls through `rtk rewrite`. | ||
| // | ||
| // This is a thin delegating extension. All rewrite logic lives in RTK's Rust | ||
| // registry via `rtk rewrite`, which remains the single source of truth. | ||
|
|
||
| type BashToolCallEvent = { | ||
| toolName: string; | ||
| input: { command: string }; | ||
| }; | ||
|
|
||
| type ExtensionContext = { | ||
| ui: { | ||
| setStatus(key: string, text: string | undefined): void; | ||
| }; | ||
| }; | ||
|
|
||
| type ExtensionAPI = { | ||
| setLabel(label: string): void; | ||
| on( | ||
| event: "session_start", | ||
| handler: ( | ||
| event: unknown, | ||
| ctx: ExtensionContext, | ||
| ) => Promise<void> | void, | ||
| ): void; | ||
| on( | ||
| event: "tool_call", | ||
| handler: ( | ||
| event: BashToolCallEvent, | ||
| ) => Promise<void> | void, | ||
| ): void; | ||
| }; | ||
|
|
||
| type RewriteDecision = { kind: "rewrite"; rewritten: string } | { kind: "skip" }; | ||
|
|
||
| function readText(stream: ReadableStream<Uint8Array> | null | undefined, name: string): Promise<string> { | ||
| if (!stream) { | ||
| throw new Error(`rtk rewrite ${name} stream was unavailable`); | ||
| } | ||
| return new Response(stream).text().then((text) => text.trim()); | ||
| } | ||
|
|
||
| async function rewriteWithRtk(command: string): Promise<RewriteDecision> { | ||
| const proc = Bun.spawn(["rtk", "rewrite", command], { | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [exitCode, stdout] = await Promise.all([ | ||
| proc.exited, | ||
| readText(proc.stdout, "stdout"), | ||
| proc.stderr?.cancel(), | ||
| ]); | ||
|
|
||
| switch (exitCode) { | ||
| case 0: | ||
| case 3: | ||
| if (!stdout || stdout === command) { | ||
| return { kind: "skip" }; | ||
| } | ||
| return { kind: "rewrite", rewritten: stdout }; | ||
| default: | ||
| return { kind: "skip" }; | ||
| } | ||
| } | ||
|
|
||
| export default function rtkOmpExtension(pi: ExtensionAPI) { | ||
| pi.setLabel("RTK"); | ||
|
|
||
| const hasRtk = Boolean(Bun.which("rtk")); | ||
|
|
||
| if (!hasRtk) { | ||
| pi.on("session_start", (_event, ctx) => { | ||
| ctx.ui.setStatus("rtk", "RTK extension disabled: rtk binary not found in PATH."); | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| pi.on("tool_call", async (event) => { | ||
| if (event.toolName !== "bash") { | ||
| return; | ||
| } | ||
|
|
||
| const original = event.input.command; | ||
| if (original.trim() === "") { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const decision = await rewriteWithRtk(original); | ||
| if (decision.kind === "skip") { | ||
| return; | ||
| } | ||
|
|
||
| event.input.command = decision.rewritten; | ||
| } catch { | ||
| 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
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.
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.
nitpick, why not just skip registering the
tool_callhook if RTK isn't available?How could we alert the user about that?
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.
Got it. I moved the RTK availability check to extension setup: when
rtkis missing, the extension now registers only asession_startwarning via OMP's UI and skips thetool_callhandler; whenrtkis available, it registers the rewrite hook as before.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.
I initially tried
ctx.ui.notify(..., "warning"), butsession_startnotifications did not appear reliably during OMP startup, likely because the initial chat container is rebuilt afterward.So I chose the persistent hook status line instead, as shown in the screenshots below.
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.
I tried to align this with the OpenCode plugin behavior: when
rtkis missing, don't register the tool hook and surface a warning.For OMP, it's a little werid.
mode.init() -> UI start -> initHooksAndCustomTools() -> emit session_start -> extension: ctx.ui.notify(...) -> showWarning -->chatContainer main.ts -> mode.renderInitialMessages() -> chatContainer.clear() -> warning clear (not show)As a result, I cant see any warning expected.
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.
Does persistent hook status works flawlessly? It seems enough. I'm not sure to understand your latest message.