diff options
| l---------[-rw-r--r--] | contrib/pi/atuin.ts | 88 | ||||
| -rw-r--r-- | crates/atuin/contrib/pi/atuin.ts | 87 | ||||
| -rw-r--r-- | crates/atuin/src/command/client/hook.rs | 2 |
3 files changed, 89 insertions, 88 deletions
diff --git a/contrib/pi/atuin.ts b/contrib/pi/atuin.ts index 55c17cb8..d5e7a533 100644..120000 --- a/contrib/pi/atuin.ts +++ b/contrib/pi/atuin.ts @@ -1,87 +1 @@ -/** - * Atuin extension for pi. - * - * Tracks bash commands executed by pi in Atuin history with author `pi`. - * - * Install with: - * atuin hook install pi - * - * Then restart pi or run /reload. - */ - -import type { BashOperations, ExtensionAPI } from "@mariozechner/pi-coding-agent"; -import { createBashTool, createLocalBashOperations } from "@mariozechner/pi-coding-agent"; - -const ATUIN_AUTHOR = "pi"; -const ATUIN_TIMEOUT_MS = 10_000; - -async function startHistory( - pi: ExtensionAPI, - cwd: string, - command: string, -): Promise<string | undefined> { - try { - const result = await pi.exec( - "atuin", - ["history", "start", "--author", ATUIN_AUTHOR, "--", command], - { cwd, timeout: ATUIN_TIMEOUT_MS }, - ); - - if (result.code !== 0) return undefined; - - const id = result.stdout.trim(); - return id.length > 0 ? id : undefined; - } catch { - return undefined; - } -} - -async function endHistory( - pi: ExtensionAPI, - cwd: string, - historyId: string, - exitCode: number, -): Promise<void> { - try { - await pi.exec( - "atuin", - ["history", "end", historyId, "--exit", String(exitCode)], - { cwd, timeout: ATUIN_TIMEOUT_MS }, - ); - } catch { - // Ignore Atuin failures so command execution is never blocked. - } -} - -export default function atuinPiExtension(pi: ExtensionAPI) { - const cwd = process.cwd(); - const local = createLocalBashOperations(); - - const trackedOperations: BashOperations = { - async exec(command, commandCwd, options) { - const historyId = await startHistory(pi, commandCwd, command); - let exitCode: number | null = null; - - try { - const result = await local.exec(command, commandCwd, options); - exitCode = result.exitCode; - return result; - } finally { - if (historyId) { - await endHistory( - pi, - commandCwd, - historyId, - exitCode ?? (options.signal?.aborted ? 130 : 1), - ); - } - } - }, - }; - - pi.registerTool( - createBashTool(cwd, { - operations: trackedOperations, - }), - ); -} +../../crates/atuin/contrib/pi/atuin.ts
\ No newline at end of file diff --git a/crates/atuin/contrib/pi/atuin.ts b/crates/atuin/contrib/pi/atuin.ts new file mode 100644 index 00000000..55c17cb8 --- /dev/null +++ b/crates/atuin/contrib/pi/atuin.ts @@ -0,0 +1,87 @@ +/** + * Atuin extension for pi. + * + * Tracks bash commands executed by pi in Atuin history with author `pi`. + * + * Install with: + * atuin hook install pi + * + * Then restart pi or run /reload. + */ + +import type { BashOperations, ExtensionAPI } from "@mariozechner/pi-coding-agent"; +import { createBashTool, createLocalBashOperations } from "@mariozechner/pi-coding-agent"; + +const ATUIN_AUTHOR = "pi"; +const ATUIN_TIMEOUT_MS = 10_000; + +async function startHistory( + pi: ExtensionAPI, + cwd: string, + command: string, +): Promise<string | undefined> { + try { + const result = await pi.exec( + "atuin", + ["history", "start", "--author", ATUIN_AUTHOR, "--", command], + { cwd, timeout: ATUIN_TIMEOUT_MS }, + ); + + if (result.code !== 0) return undefined; + + const id = result.stdout.trim(); + return id.length > 0 ? id : undefined; + } catch { + return undefined; + } +} + +async function endHistory( + pi: ExtensionAPI, + cwd: string, + historyId: string, + exitCode: number, +): Promise<void> { + try { + await pi.exec( + "atuin", + ["history", "end", historyId, "--exit", String(exitCode)], + { cwd, timeout: ATUIN_TIMEOUT_MS }, + ); + } catch { + // Ignore Atuin failures so command execution is never blocked. + } +} + +export default function atuinPiExtension(pi: ExtensionAPI) { + const cwd = process.cwd(); + const local = createLocalBashOperations(); + + const trackedOperations: BashOperations = { + async exec(command, commandCwd, options) { + const historyId = await startHistory(pi, commandCwd, command); + let exitCode: number | null = null; + + try { + const result = await local.exec(command, commandCwd, options); + exitCode = result.exitCode; + return result; + } finally { + if (historyId) { + await endHistory( + pi, + commandCwd, + historyId, + exitCode ?? (options.signal?.aborted ? 130 : 1), + ); + } + } + }, + }; + + pi.registerTool( + createBashTool(cwd, { + operations: trackedOperations, + }), + ); +} diff --git a/crates/atuin/src/command/client/hook.rs b/crates/atuin/src/command/client/hook.rs index 6aa2bb28..0abae575 100644 --- a/crates/atuin/src/command/client/hook.rs +++ b/crates/atuin/src/command/client/hook.rs @@ -10,7 +10,7 @@ use serde_json::Value; use super::history; const HOOK_EVENT_TYPES: &[&str] = &["PreToolUse", "PostToolUse", "PostToolUseFailure"]; -const PI_EXTENSION_SOURCE: &str = include_str!("../../../../../contrib/pi/atuin.ts"); +const PI_EXTENSION_SOURCE: &str = include_str!("../../../contrib/pi/atuin.ts"); enum InstallKind { JsonHooks { |
