-
Notifications
You must be signed in to change notification settings - Fork 57
feat(harness): integrate pi runtime into desktop app #3469
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: pi-harness-runtime
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { | ||
| PiResumeInput, | ||
| PiRunInput, | ||
| PiRunner, | ||
| } from "@posthog/core/pi-runtime/piRunner"; | ||
| import { resolveService } from "@posthog/di/container"; | ||
| import { | ||
| HOST_TRPC_CLIENT, | ||
| type HostTrpcClient, | ||
| } from "@posthog/host-router/client"; | ||
|
|
||
| function hostClient(): HostTrpcClient { | ||
| return resolveService<HostTrpcClient>(HOST_TRPC_CLIENT); | ||
| } | ||
|
|
||
| export class TrpcPiRunner implements PiRunner { | ||
| async create(input: PiRunInput): Promise<void> { | ||
| await hostClient().piSession.start.mutate(input); | ||
| } | ||
|
|
||
| resume(input: PiResumeInput): Promise<void> { | ||
| return hostClient().piSession.resume.mutate(input); | ||
| } | ||
|
|
||
| stop(taskId: string): Promise<void> { | ||
| return hostClient().piSession.stop.mutate({ taskId }); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,49 @@ | ||
| import type { ChildProcess } from "node:child_process"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { | ||
| RpcClient, | ||
| type RpcClientOptions, | ||
| } from "@earendil-works/pi-coding-agent"; | ||
| import type { PosthogOAuthCredentials } from "@posthog/harness"; | ||
|
|
||
| export type PiRpcClient = RpcClient; | ||
|
|
||
| type RpcClientProcessAccess = { | ||
| process?: ChildProcess; | ||
| }; | ||
|
|
||
| // HACK: Pi RpcClient does not expose its spawned child process or PID publicly. | ||
| export function getPiRpcClientProcess( | ||
| client: PiRpcClient, | ||
| ): ChildProcess | null { | ||
| return (client as unknown as RpcClientProcessAccess).process ?? null; | ||
| } | ||
|
|
||
| export type PiRpcClientOptions = Pick< | ||
| RpcClientOptions, | ||
| "cwd" | "env" | "model" | ||
| > & { | ||
| /** PostHog token passed only to the isolated RPC host process. */ | ||
| apiKey?: string; | ||
| sessionFile?: string; | ||
| oauthCredentials: PosthogOAuthCredentials; | ||
| }; | ||
|
|
||
| /** | ||
| * Create a native Pi RPC client backed by the PostHog harness. | ||
| * | ||
| * The client owns an isolated child process. Call `start()` before sending | ||
| * commands and `stop()` when the run is finished. | ||
| */ | ||
| export function createPiRpcClient( | ||
| options: PiRpcClientOptions = {}, | ||
| ): PiRpcClient { | ||
| const { apiKey, env, ...rpcOptions } = options; | ||
| export function createPiRpcClient(options: PiRpcClientOptions): PiRpcClient { | ||
| const { env, sessionFile, oauthCredentials, ...rpcOptions } = options; | ||
| const args = sessionFile ? ["--session-file", sessionFile] : undefined; | ||
| const cliPath = fileURLToPath(new URL("./rpc-host.js", import.meta.url)); | ||
| const oauthEnvironment: Record<string, string> = { | ||
| POSTHOG_OAUTH_CREDENTIALS: JSON.stringify(oauthCredentials), | ||
| }; | ||
| const childEnvironment: Record<string, string> = { | ||
| ...env, | ||
| ...oauthEnvironment, | ||
| }; | ||
|
|
||
|
Comment on lines
+34
to
41
Contributor
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 child receives a one-time copy of the desktop access and refresh tokens. If the provider rotates refresh tokens and the parent refreshes first, the long-running child retains the invalidated token and its next refresh fails even though the desktop remains signed in, interrupting the Pi session. |
||
| return new RpcClient({ | ||
| ...rpcOptions, | ||
| cliPath: fileURLToPath(new URL("./rpc-host.js", import.meta.url)), | ||
| env: { | ||
| ...env, | ||
| ...(apiKey ? { POSTHOG_API_KEY: apiKey } : {}), | ||
| }, | ||
| args, | ||
| cliPath, | ||
| env: childEnvironment, | ||
| provider: "posthog", | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.