Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

driver.ts

210 lines7.8 KB

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