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

driver.ts

218 lines7.8 KB

1// User-wide ~/.pi/agent state makes continuation identity use default instance grouping.
2import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
3import { PiSettings } from "../settings.ts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as Schema from "effect/Schema";
8import * as HttpClient from "effect/http/HttpClient";
9import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
10​
11import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
12import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
13import { piUsageReader } from "./usage.ts";
14import { makePiTextGeneration } from "./textGeneration.ts";
15import { PiAdapterV2Driver, type PiAdapterV2DriverEnv } from "./adapter.ts";
16import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
17import {
18 buildInitialPiProviderSnapshot,
19 checkPiProviderStatus,
20 discoverPiCommandsForCwd,
21 enrichPiSnapshot,
22} from "./status.ts";
23import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
24import {
25 defaultProviderContinuationIdentity,
26 type ProviderDriver,
27 type ProviderInstance,
28} from "@t3tools/provider-core/server/driver";
29import type { ServerProviderDraft } from "@t3tools/provider-core/server/snapshotProbe";
30import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
31import {
32 makeCachedProviderMaintenanceResolution,
33 makePackageManagedProviderMaintenanceResolver,
34 resolveProviderMaintenanceCapabilitiesEffect,
35} from "@t3tools/provider-core/server/maintenanceResolver";
36import {
37 haveProviderSnapshotSettingsChanged,
38 makeProviderSnapshotSettingsSource,
39 type ProviderSnapshotSettings,
40} from "@t3tools/provider-core/server/snapshotSettings";
41​
42const decodePiSettings = Schema.decodeSync(PiSettings);
43​
44const DRIVER_KIND = ProviderDriverKind.make("pi");
45const UPDATE = makePackageManagedProviderMaintenanceResolver({
46 provider: DRIVER_KIND,
47 npmPackageName: "@earendil-works/pi-coding-agent",
48 nativeUpdate: { args: ["update", "--self"] },
49});
50​
51export type PiDriverEnv =
52 | PiAdapterV2DriverEnv
53 | ProviderHost.ProviderHost
54 | ChildProcessSpawner.ChildProcessSpawner
55 | FileSystem.FileSystem
56 | HttpClient.HttpClient
57 | ProviderLatestVersions.ProviderLatestVersions
58 | Path.Path;
59​
60const withInstanceIdentity =
61 (input: {
62 readonly instanceId: ProviderInstance["instanceId"];
63 readonly displayName: string | undefined;
64 readonly accentColor: string | undefined;
65 readonly continuationGroupKey: string;
66 }) =>
67 (snapshot: ServerProviderDraft): ServerProvider => ({
68 ...snapshot,
69 instanceId: input.instanceId,
70 driver: DRIVER_KIND,
71 ...(input.displayName ? { displayName: input.displayName } : {}),
72 ...(input.accentColor ? { accentColor: input.accentColor } : {}),
73 continuation: { groupKey: input.continuationGroupKey },
74 });
75​
76export const PiDriver: ProviderDriver<PiSettings, PiDriverEnv, Path.Path> = {
77 driverKind: DRIVER_KIND,
78 metadata: {
79 displayName: "Pi",
80 supportsMultipleInstances: true,
81 },
82 usage: piUsageReader,
83 configSchema: PiSettings,
84 defaultConfig: (): PiSettings => decodePiSettings({}),
85 create: Effect.fn("PiDriver.create")(function* ({
86 instanceId,
87 displayName,
88 accentColor,
89 environment,
90 enabled,
91 config,
92 }) {
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]) => ({
209 ...machineSnapshot,
210 ...commands,
211 slashCommandsPending: false,
212 })),
213 ),
214 orchestrationAdapter,
215 textGeneration,
216 } satisfies ProviderInstance;
217 }),
218};