Open History to see the last commit
1/**
2 * PiDriver — v1 `ProviderDriver` for the Pi coding agent, composing the
3 * orchestrator-v2 adapter (`PiAdapterV2`), the snapshot/probe layer
4 * (`PiProvider`), and Pi-backed text generation.
5 *
6 * Pi state (sessions, settings, extensions, auth) lives in the user's own
7 * `~/.pi/agent`, so continuation identity uses the default instance grouping.
8 */
9import { PiSettings, ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13import * as Schema from "effect/Schema";
14import { HttpClient } from "effect/http";
15import { ChildProcessSpawner } from "effect/process";
16
17import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
18import * as ServerConfig from "../../config.ts";
19import * as ServerSettings from "../../serverSettings.ts";
20import { makePiTextGeneration } from "../../textGeneration/PiTextGeneration.ts";
21import {
22 PiAdapterV2Driver,
23 type PiAdapterV2DriverEnv,
24} from "../../orchestration-v2/Adapters/PiAdapterV2.ts";
25import { ProviderDriverError } from "../Errors.ts";
26import {
27 buildInitialPiProviderSnapshot,
28 checkPiProviderStatus,
29 discoverPiCommandsForCwd,
30 enrichPiSnapshot,
31} from "../PiProvider.ts";
32import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
33import {
34 defaultProviderContinuationIdentity,
35 type ProviderDriver,
36 type ProviderInstance,
37} from "../ProviderDriver.ts";
38import type { ServerProviderDraft } from "../providerSnapshot.ts";
39import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
40import {
41 makeCachedProviderMaintenanceResolution,
42 makePackageManagedProviderMaintenanceResolver,
43 resolveProviderMaintenanceCapabilitiesEffect,
44} from "../providerMaintenance.ts";
45import {
46 haveProviderSnapshotSettingsChanged,
47 makeProviderSnapshotSettingsSource,
48 type ProviderSnapshotSettings,
49} from "../providerUpdateSettings.ts";
50
51const decodePiSettings = Schema.decodeSync(PiSettings);
52
53const DRIVER_KIND = ProviderDriverKind.make("pi");
54const UPDATE = makePackageManagedProviderMaintenanceResolver({
55 provider: DRIVER_KIND,
56 npmPackageName: "@earendil-works/pi-coding-agent",
57 // Pi's updater covers its own installer and npm, pnpm, yarn, and bun globals.
58 nativeUpdate: { args: ["update", "--self"] },
59});
60
61export type PiDriverEnv =
62 | PiAdapterV2DriverEnv
63 | BackgroundPolicy.BackgroundPolicy
64 | ChildProcessSpawner.ChildProcessSpawner
65 | FileSystem.FileSystem
66 | HttpClient.HttpClient
67 | Path.Path
68 | ServerConfig.ServerConfig
69 | ServerSettings.ServerSettingsService;
70
71const withInstanceIdentity =
72 (input: {
73 readonly instanceId: ProviderInstance["instanceId"];
74 readonly displayName: string | undefined;
75 readonly accentColor: string | undefined;
76 readonly continuationGroupKey: string;
77 }) =>
78 (snapshot: ServerProviderDraft): ServerProvider => ({
79 ...snapshot,
80 instanceId: input.instanceId,
81 driver: DRIVER_KIND,
82 ...(input.displayName ? { displayName: input.displayName } : {}),
83 ...(input.accentColor ? { accentColor: input.accentColor } : {}),
84 continuation: { groupKey: input.continuationGroupKey },
85 });
86
87export const PiDriver: ProviderDriver<PiSettings, PiDriverEnv> = {
88 driverKind: DRIVER_KIND,
89 metadata: {
90 displayName: "Pi",
91 supportsMultipleInstances: true,
92 },
93 configSchema: PiSettings,
94 defaultConfig: (): PiSettings => decodePiSettings({}),
95 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
96 Effect.gen(function* () {
97 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
98 const fileSystem = yield* FileSystem.FileSystem;
99 const pathService = yield* Path.Path;
100 const httpClient = yield* HttpClient.HttpClient;
101 const { cwd } = yield* ServerConfig.ServerConfig;
102 const serverSettings = yield* ServerSettings.ServerSettingsService;
103 const processEnv = mergeProviderInstanceEnvironment(environment);
104 const continuationIdentity = defaultProviderContinuationIdentity({
105 driverKind: DRIVER_KIND,
106 instanceId,
107 });
108 const stampIdentity = withInstanceIdentity({
109 instanceId,
110 displayName,
111 accentColor,
112 continuationGroupKey: continuationIdentity.continuationKey,
113 });
114 const effectiveConfig = { ...config, enabled } satisfies PiSettings;
115 const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
116 resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
117 binaryPath: effectiveConfig.binaryPath,
118 env: processEnv,
119 }).pipe(
120 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
121 Effect.provideService(FileSystem.FileSystem, fileSystem),
122 Effect.provideService(Path.Path, pathService),
123 ),
124 );
125
126 const orchestrationAdapter = yield* PiAdapterV2Driver.create({
127 instanceId,
128 displayName,
129 accentColor,
130 environment,
131 enabled,
132 config,
133 }).pipe(
134 Effect.mapError(
135 (cause) =>
136 new ProviderDriverError({
137 driver: DRIVER_KIND,
138 instanceId,
139 detail: "Failed to build Pi orchestration adapter.",
140 cause,
141 }),
142 ),
143 );
144 const textGeneration = yield* makePiTextGeneration(effectiveConfig, processEnv);
145
146 const checkProvider = checkPiProviderStatus(effectiveConfig, processEnv, cwd).pipe(
147 Effect.map(stampIdentity),
148 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
149 );
150
151 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
152 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<PiSettings>>({
153 resolveMaintenance,
154 getSettings: snapshotSettings.getSettings,
155 streamSettings: snapshotSettings.streamSettings,
156 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
157 initialSnapshot: (settings) =>
158 buildInitialPiProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
159 checkProvider,
160 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
161 resolveMaintenance().pipe(
162 Effect.flatMap((maintenanceCapabilities) =>
163 enrichPiSnapshot({
164 snapshot: currentSnapshot,
165 maintenanceCapabilities,
166 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
167 publishSnapshot,
168 httpClient,
169 }),
170 ),
171 ),
172 }).pipe(
173 Effect.mapError(
174 (cause) =>
175 new ProviderDriverError({
176 driver: DRIVER_KIND,
177 instanceId,
178 detail: "Failed to build Pi snapshot.",
179 cause,
180 }),
181 ),
182 );
183
184 return {
185 instanceId,
186 driverKind: DRIVER_KIND,
187 continuationIdentity,
188 displayName,
189 accentColor,
190 enabled,
191 snapshot,
192 snapshotForCwd: (workspaceCwd) =>
193 !effectiveConfig.enabled
194 ? snapshot.getSnapshot
195 : Effect.all([
196 snapshot.getSnapshot,
197 discoverPiCommandsForCwd(effectiveConfig, processEnv, workspaceCwd).pipe(
198 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
199 Effect.mapError(
200 (cause) =>
201 new ProviderDriverError({
202 driver: DRIVER_KIND,
203 instanceId,
204 detail: "Failed to discover Pi workspace commands.",
205 cause,
206 }),
207 ),
208 ),
209 ]).pipe(
210 Effect.map(([machineSnapshot, commands]) => ({ ...machineSnapshot, ...commands })),
211 ),
212 orchestrationAdapter,
213 textGeneration,
214 } satisfies ProviderInstance;
215 }),
216};