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

PiDriver.ts

188 lines7.1 KB

1/**
2 * PiDriver — `ProviderDriver` for the Pi coding agent, composing the RPC
3 * adapter (`PiAdapter`), the snapshot/probe layer (`PiProvider`), and
4 * 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 } from "@t3tools/contracts";
10import * as Crypto from "effect/Crypto";
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/unstable/http";
16import { ChildProcessSpawner } from "effect/unstable/process";
17​
18import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
19import { ServerConfig } from "../../config.ts";
20import { ServerSettingsService } from "../../serverSettings.ts";
21import { makePiTextGeneration } from "../../textGeneration/PiTextGeneration.ts";
22import { ProviderDriverError } from "../Errors.ts";
23import { makePiAdapter } from "../Layers/PiAdapter.ts";
24import {
25 buildInitialPiProviderSnapshot,
26 checkPiProviderStatus,
27 discoverPiWorkspaceCommands,
28 enrichPiSnapshot,
29} from "../Layers/PiProvider.ts";
30import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
31import {
32 defaultProviderContinuationIdentity,
33 type ProviderDriver,
34 type ProviderInstance,
35} from "../ProviderDriver.ts";
36import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
37import {
38 makeCachedProviderMaintenanceResolution,
39 makePackageManagedProviderMaintenanceResolver,
40 resolveProviderMaintenanceCapabilitiesEffect,
41} from "../providerMaintenance.ts";
42import {
43 haveProviderSnapshotSettingsChanged,
44 makeProviderSnapshotSettingsSource,
45 type ProviderSnapshotSettings,
46} from "../providerUpdateSettings.ts";
47import { withInstanceIdentity } from "./instanceIdentity.ts";
48​
49const decodePiSettings = Schema.decodeSync(PiSettings);
50​
51const DRIVER_KIND = ProviderDriverKind.make("pi");
52const UPDATE = makePackageManagedProviderMaintenanceResolver({
53 provider: DRIVER_KIND,
54 npmPackageName: "@earendil-works/pi-coding-agent",
55 nativeUpdate: null,
56});
57​
58export type PiDriverEnv =
59 | BackgroundPolicy.BackgroundPolicy
60 | ChildProcessSpawner.ChildProcessSpawner
61 | Crypto.Crypto
62 | FileSystem.FileSystem
63 | HttpClient.HttpClient
64 | Path.Path
65 | ServerConfig
66 | ServerSettingsService;
67​
68export const PiDriver: ProviderDriver<PiSettings, PiDriverEnv> = {
69 driverKind: DRIVER_KIND,
70 metadata: {
71 displayName: "Pi",
72 supportsMultipleInstances: true,
73 },
74 configSchema: PiSettings,
75 defaultConfig: (): PiSettings => decodePiSettings({}),
76 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
77 Effect.gen(function* () {
78 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
79 const fileSystem = yield* FileSystem.FileSystem;
80 const pathService = yield* Path.Path;
81 const httpClient = yield* HttpClient.HttpClient;
82 const { cwd } = yield* ServerConfig;
83 const serverSettings = yield* ServerSettingsService;
84 const processEnv = mergeProviderInstanceEnvironment(environment);
85 const continuationIdentity = defaultProviderContinuationIdentity({
86 driverKind: DRIVER_KIND,
87 instanceId,
88 });
89 const stampIdentity = withInstanceIdentity({
90 instanceId,
91 driverKind: DRIVER_KIND,
92 displayName,
93 accentColor,
94 continuationGroupKey: continuationIdentity.continuationKey,
95 });
96 const effectiveConfig = { ...config, enabled } satisfies PiSettings;
97 const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
98 resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
99 binaryPath: effectiveConfig.binaryPath,
100 env: processEnv,
101 }).pipe(
102 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
103 Effect.provideService(FileSystem.FileSystem, fileSystem),
104 Effect.provideService(Path.Path, pathService),
105 ),
106 );
107​
108 const adapter = yield* makePiAdapter(effectiveConfig, {
109 environment: processEnv,
110 instanceId,
111 });
112 const textGeneration = yield* makePiTextGeneration(effectiveConfig, processEnv);
113​
114 const checkProvider = checkPiProviderStatus(effectiveConfig, processEnv, cwd).pipe(
115 Effect.map(stampIdentity),
116 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
117 );
118​
119 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
120 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<PiSettings>>({
121 resolveMaintenance,
122 getSettings: snapshotSettings.getSettings,
123 streamSettings: snapshotSettings.streamSettings,
124 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
125 initialSnapshot: (settings) =>
126 buildInitialPiProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
127 checkProvider,
128 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
129 resolveMaintenance().pipe(
130 Effect.flatMap((maintenanceCapabilities) =>
131 enrichPiSnapshot({
132 snapshot: currentSnapshot,
133 maintenanceCapabilities,
134 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
135 publishSnapshot,
136 }),
137 ),
138 Effect.provideService(HttpClient.HttpClient, httpClient),
139 ),
140 }).pipe(
141 Effect.mapError(
142 (cause) =>
143 new ProviderDriverError({
144 driver: DRIVER_KIND,
145 instanceId,
146 detail: "Failed to build Pi snapshot.",
147 cause,
148 }),
149 ),
150 );
151​
152 // Project skills live in the workspace, so each project gets its own
153 // command and skill list on top of the machine-wide snapshot.
154 const snapshotForCwd = (workspaceCwd: string) =>
155 !effectiveConfig.enabled
156 ? snapshot.getSnapshot
157 : Effect.all([
158 snapshot.getSnapshot,
159 discoverPiWorkspaceCommands(effectiveConfig, processEnv, workspaceCwd).pipe(
160 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
161 Effect.mapError(
162 (cause) =>
163 new ProviderDriverError({
164 driver: DRIVER_KIND,
165 instanceId,
166 detail: "Failed to discover Pi commands and skills for the workspace.",
167 cause,
168 }),
169 ),
170 ),
171 ]).pipe(
172 Effect.map(([machineSnapshot, commands]) => ({ ...machineSnapshot, ...commands })),
173 );
174​
175 return {
176 instanceId,
177 driverKind: DRIVER_KIND,
178 continuationIdentity,
179 displayName,
180 accentColor,
181 enabled,
182 snapshot,
183 snapshotForCwd,
184 adapter,
185 textGeneration,
186 } satisfies ProviderInstance;
187 }),
188};