-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add Pi coding-agent provider #3818
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
c29adba
feat(contracts): add Pi provider schemas and model/runtime wiring
ahmadaccino f27cfaf
feat(server): implement Pi coding-agent provider
ahmadaccino 25e8ab3
feat(web): surface Pi provider in settings, model picker, and icons
ahmadaccino 8e79df1
docs(providers): add Pi guide; list Pi and Grok in README
ahmadaccino ab7cefa
Merge remote-tracking branch 'origin/main' into feat/pi-provider
ahmadaccino 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
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,114 @@ | ||
| #!/usr/bin/env node | ||
| // @effect-diagnostics nodeBuiltinImport:off | ||
| // Fake `pi --mode rpc` for tests; driven by `PI_MOCK_*` env vars. | ||
| import * as NodeReadline from "node:readline"; | ||
|
|
||
| const assistantText = process.env["PI_MOCK_ASSISTANT_TEXT"] ?? '{"title":"Mock title"}'; | ||
| const emitInvalidJson = process.env["PI_MOCK_EMIT_INVALID_JSON"] === "1"; | ||
| const lastTextFails = process.env["PI_MOCK_LAST_TEXT_FAILS"] === "1"; | ||
|
|
||
| const replyText = emitInvalidJson | ||
| ? "Sure — here is the answer, with no JSON at all." | ||
| : assistantText; | ||
| let lastAssistantText: string | null = null; | ||
|
|
||
| function write(obj: unknown): void { | ||
| process.stdout.write(`${JSON.stringify(obj)}\n`); | ||
| } | ||
|
|
||
| const rl = NodeReadline.createInterface({ input: process.stdin }); | ||
|
|
||
| rl.on("line", (line: string) => { | ||
| const trimmed = line.trim(); | ||
| if (!trimmed) return; | ||
| let command: { type?: string; id?: string }; | ||
| try { | ||
| command = JSON.parse(trimmed) as { type?: string; id?: string }; | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| switch (command.type) { | ||
| case "prompt": | ||
| case "steer": | ||
| case "follow_up": { | ||
| write({ type: "agent_start" }); | ||
| write({ type: "turn_start" }); | ||
| write({ | ||
| type: "message_update", | ||
| assistantMessageEvent: { type: "text_delta", delta: replyText }, | ||
| }); | ||
| lastAssistantText = replyText; | ||
| write({ type: "message_end" }); | ||
| write({ type: "turn_end" }); | ||
| write({ type: "agent_end" }); | ||
| return; | ||
| } | ||
| case "get_last_assistant_text": { | ||
| write( | ||
| lastTextFails | ||
| ? { | ||
| type: "response", | ||
| id: command.id, | ||
| command: "get_last_assistant_text", | ||
| success: false, | ||
| error: "no assistant text", | ||
| } | ||
| : { | ||
| type: "response", | ||
| id: command.id, | ||
| command: "get_last_assistant_text", | ||
| success: true, | ||
| data: { text: lastAssistantText }, | ||
| }, | ||
| ); | ||
| return; | ||
| } | ||
| case "get_state": { | ||
| write({ | ||
| type: "response", | ||
| id: command.id, | ||
| command: "get_state", | ||
| success: true, | ||
| data: { | ||
| sessionId: "mock-session", | ||
| sessionFile: "/tmp/pi-mock-session.json", | ||
| thinkingLevel: "off", | ||
| isStreaming: false, | ||
| isCompacting: false, | ||
| steeringMode: "all", | ||
| followUpMode: "all", | ||
| autoCompactionEnabled: false, | ||
| messageCount: 0, | ||
| pendingMessageCount: 0, | ||
| }, | ||
| }); | ||
| return; | ||
| } | ||
| case "get_commands": { | ||
| write({ | ||
| type: "response", | ||
| id: command.id, | ||
| command: "get_commands", | ||
| success: true, | ||
| data: { commands: [] }, | ||
| }); | ||
| return; | ||
| } | ||
| default: { | ||
| if (command.id !== undefined) { | ||
| write({ | ||
| type: "response", | ||
| id: command.id, | ||
| command: command.type ?? "unknown", | ||
| success: true, | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| rl.on("close", () => { | ||
| process.exit(0); | ||
| }); |
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,167 @@ | ||
| import { PiSettings, ProviderDriverKind, type ServerProvider } from "@t3tools/contracts"; | ||
| import * as Crypto from "effect/Crypto"; | ||
| import * as Duration from "effect/Duration"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Path from "effect/Path"; | ||
| import * as Schema from "effect/Schema"; | ||
| import { HttpClient } from "effect/unstable/http"; | ||
| import { ChildProcessSpawner } from "effect/unstable/process"; | ||
|
|
||
| import { ServerConfig } from "../../config.ts"; | ||
| import { ServerSettingsService } from "../../serverSettings.ts"; | ||
| import { makePiTextGeneration } from "../../textGeneration/PiTextGeneration.ts"; | ||
| import { ProviderDriverError } from "../Errors.ts"; | ||
| import { makePiAdapter } from "../Layers/PiAdapter.ts"; | ||
| import { buildInitialPiProviderSnapshot, checkPiProviderStatus } from "../Layers/PiProvider.ts"; | ||
| import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts"; | ||
| import { makeManagedServerProvider } from "../makeManagedServerProvider.ts"; | ||
| import { | ||
| defaultProviderContinuationIdentity, | ||
| type ProviderDriver, | ||
| type ProviderInstance, | ||
| } from "../ProviderDriver.ts"; | ||
| import type { ServerProviderDraft } from "../providerSnapshot.ts"; | ||
| import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts"; | ||
| import { | ||
| enrichProviderSnapshotWithVersionAdvisory, | ||
| makePackageManagedProviderMaintenanceResolver, | ||
| resolveProviderMaintenanceCapabilitiesEffect, | ||
| } from "../providerMaintenance.ts"; | ||
| import { | ||
| haveProviderSnapshotSettingsChanged, | ||
| makeProviderSnapshotSettingsSource, | ||
| type ProviderSnapshotSettings, | ||
| } from "../providerUpdateSettings.ts"; | ||
|
|
||
| const decodePiSettings = Schema.decodeSync(PiSettings); | ||
|
|
||
| const DRIVER_KIND = ProviderDriverKind.make("pi"); | ||
| const SNAPSHOT_REFRESH_INTERVAL = Duration.minutes(5); | ||
|
|
||
| const UPDATE = makePackageManagedProviderMaintenanceResolver({ | ||
| provider: DRIVER_KIND, | ||
| npmPackageName: "@earendil-works/pi-coding-agent", | ||
| homebrewFormula: null, | ||
| nativeUpdate: null, | ||
| }); | ||
|
|
||
| export type PiDriverEnv = | ||
| | ChildProcessSpawner.ChildProcessSpawner | ||
| | Crypto.Crypto | ||
| | FileSystem.FileSystem | ||
| | HttpClient.HttpClient | ||
| | Path.Path | ||
| | ProviderEventLoggers | ||
| | ServerConfig | ||
| | ServerSettingsService; | ||
|
|
||
| const withInstanceIdentity = | ||
| (input: { | ||
| readonly instanceId: ProviderInstance["instanceId"]; | ||
| readonly displayName: string | undefined; | ||
| readonly accentColor: string | undefined; | ||
| readonly continuationGroupKey: string; | ||
| }) => | ||
| (snapshot: ServerProviderDraft): ServerProvider => ({ | ||
| ...snapshot, | ||
| instanceId: input.instanceId, | ||
| driver: DRIVER_KIND, | ||
| ...(input.displayName ? { displayName: input.displayName } : {}), | ||
| ...(input.accentColor ? { accentColor: input.accentColor } : {}), | ||
| continuation: { groupKey: input.continuationGroupKey }, | ||
| }); | ||
|
|
||
| export const PiDriver: ProviderDriver<PiSettings, PiDriverEnv> = { | ||
| driverKind: DRIVER_KIND, | ||
| metadata: { | ||
| displayName: "Pi", | ||
| supportsMultipleInstances: true, | ||
| }, | ||
| configSchema: PiSettings, | ||
| defaultConfig: (): PiSettings => decodePiSettings({}), | ||
| create: ({ instanceId, displayName, accentColor, environment, enabled, config }) => | ||
| Effect.gen(function* () { | ||
| const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | ||
| const httpClient = yield* HttpClient.HttpClient; | ||
| const serverSettings = yield* ServerSettingsService; | ||
| const serverConfig = yield* ServerConfig; | ||
| const processEnv = mergeProviderInstanceEnvironment(environment); | ||
| const continuationIdentity = defaultProviderContinuationIdentity({ | ||
| driverKind: DRIVER_KIND, | ||
| instanceId, | ||
| }); | ||
| const stampIdentity = withInstanceIdentity({ | ||
| instanceId, | ||
| displayName, | ||
| accentColor, | ||
| continuationGroupKey: continuationIdentity.continuationKey, | ||
| }); | ||
| const effectiveConfig = { ...config, enabled } satisfies PiSettings; | ||
| const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, { | ||
| binaryPath: effectiveConfig.binaryPath, | ||
| env: processEnv, | ||
| }); | ||
|
|
||
| const adapter = yield* makePiAdapter(effectiveConfig, { | ||
| instanceId, | ||
| environment: processEnv, | ||
| }); | ||
| const textGeneration = yield* makePiTextGeneration(effectiveConfig, processEnv); | ||
|
|
||
| const checkProvider = checkPiProviderStatus( | ||
| effectiveConfig, | ||
| serverConfig.cwd, | ||
| processEnv, | ||
| ).pipe( | ||
| Effect.map(stampIdentity), | ||
| Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), | ||
| ); | ||
|
|
||
| const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings); | ||
| const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<PiSettings>>({ | ||
| maintenanceCapabilities, | ||
| getSettings: snapshotSettings.getSettings, | ||
| streamSettings: snapshotSettings.streamSettings, | ||
| haveSettingsChanged: haveProviderSnapshotSettingsChanged, | ||
| initialSnapshot: (settings) => | ||
| buildInitialPiProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)), | ||
| checkProvider, | ||
| enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) => | ||
| enrichProviderSnapshotWithVersionAdvisory(currentSnapshot, maintenanceCapabilities, { | ||
| enableProviderUpdateChecks: settings.enableProviderUpdateChecks, | ||
| }).pipe( | ||
| Effect.provideService(HttpClient.HttpClient, httpClient), | ||
| Effect.flatMap((enrichedSnapshot) => publishSnapshot(enrichedSnapshot)), | ||
| Effect.catchCause((cause) => | ||
| Effect.logWarning("Pi version advisory enrichment failed", { cause }).pipe( | ||
| Effect.asVoid, | ||
| ), | ||
| ), | ||
| ), | ||
| refreshInterval: SNAPSHOT_REFRESH_INTERVAL, | ||
| }).pipe( | ||
| Effect.mapError( | ||
| (cause) => | ||
| new ProviderDriverError({ | ||
| driver: DRIVER_KIND, | ||
| instanceId, | ||
| detail: `Failed to build Pi snapshot: ${cause.message ?? String(cause)}`, | ||
| cause, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| return { | ||
| instanceId, | ||
| driverKind: DRIVER_KIND, | ||
| continuationIdentity, | ||
| displayName, | ||
| accentColor, | ||
| enabled, | ||
| snapshot, | ||
| adapter, | ||
| textGeneration, | ||
| } satisfies ProviderInstance; | ||
| }), | ||
| }; |
Oops, something went wrong.
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.