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

driver.ts

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