Open History to see the last commit
1/**
2 * HermesDriver — `ProviderDriver` for the Hermes Agent (`hermes acp`) runtime.
3 *
4 * Hermes exposes an ACP-based CLI. Model discovery happens during the managed
5 * provider status check via ACP session setup; there is no standalone auth
6 * probe, so a successful discovery doubles as proof of authentication.
7 *
8 * Text generation is supported via the ACP runtime — `makeHermesTextGeneration`
9 * drives `runtime.prompt` with a structured-output schema and collects the
10 * agent's `agent_message_chunk` stream into a single JSON blob.
11 *
12 * @module provider/Drivers/HermesDriver
13 */
14import { HermesSettings, ProviderDriverKind } from "@t3tools/contracts";
15import * as Crypto from "effect/Crypto";
16import * as Effect from "effect/Effect";
17import * as FileSystem from "effect/FileSystem";
18import * as Path from "effect/Path";
19import * as Schema from "effect/Schema";
20import { HttpClient } from "effect/unstable/http";
21import { ChildProcessSpawner } from "effect/unstable/process";
22
23import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
24import { ServerConfig } from "../../config.ts";
25import { ServerSettingsService } from "../../serverSettings.ts";
26import { makeHermesTextGeneration } from "../../textGeneration/HermesTextGeneration.ts";
27import { ProviderDriverError } from "../Errors.ts";
28import { withInstanceIdentity } from "./instanceIdentity.ts";
29import { makeHermesAdapter } from "../Layers/HermesAdapter.ts";
30import {
31 buildInitialHermesProviderSnapshot,
32 checkHermesProviderStatus,
33 enrichHermesSnapshot,
34} from "../Layers/HermesProvider.ts";
35import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
36import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
37import {
38 defaultProviderContinuationIdentity,
39 type ProviderDriver,
40 type ProviderInstance,
41} from "../ProviderDriver.ts";
42import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
43import { makeManualOnlyProviderMaintenanceCapabilities } from "../providerMaintenance.ts";
44import {
45 haveProviderSnapshotSettingsChanged,
46 makeProviderSnapshotSettingsSource,
47 type ProviderSnapshotSettings,
48} from "../providerUpdateSettings.ts";
49const decodeHermesSettings = Schema.decodeSync(HermesSettings);
50
51const DRIVER_KIND = ProviderDriverKind.make("hermes");
52const MAINTENANCE_CAPABILITIES = makeManualOnlyProviderMaintenanceCapabilities({
53 provider: DRIVER_KIND,
54 packageName: null,
55});
56
57// `ServerConfig` stays in the union even though `create` never yields it
58// directly: `makeHermesAdapter` reads it for the attachments directory.
59export type HermesDriverEnv =
60 | BackgroundPolicy.BackgroundPolicy
61 | ChildProcessSpawner.ChildProcessSpawner
62 | Crypto.Crypto
63 | FileSystem.FileSystem
64 | HttpClient.HttpClient
65 | Path.Path
66 | ProviderEventLoggers
67 | ServerConfig
68 | ServerSettingsService;
69
70export const HermesDriver: ProviderDriver<HermesSettings, HermesDriverEnv> = {
71 driverKind: DRIVER_KIND,
72 metadata: {
73 displayName: "Hermes",
74 supportsMultipleInstances: true,
75 },
76 configSchema: HermesSettings,
77 defaultConfig: (): HermesSettings => decodeHermesSettings({}),
78 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
79 Effect.gen(function* () {
80 const crypto = yield* Crypto.Crypto;
81 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
82 const httpClient = yield* HttpClient.HttpClient;
83 const serverSettings = yield* ServerSettingsService;
84 const eventLoggers = yield* ProviderEventLoggers;
85 const processEnv = mergeProviderInstanceEnvironment(environment);
86 const continuationIdentity = defaultProviderContinuationIdentity({
87 driverKind: DRIVER_KIND,
88 instanceId,
89 });
90 const stampIdentity = withInstanceIdentity({
91 instanceId,
92 driverKind: DRIVER_KIND,
93 displayName,
94 accentColor,
95 continuationGroupKey: continuationIdentity.continuationKey,
96 });
97 const effectiveConfig = { ...config, enabled } satisfies HermesSettings;
98
99 const adapter = yield* makeHermesAdapter(effectiveConfig, {
100 environment: processEnv,
101 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
102 instanceId,
103 });
104 const textGeneration = yield* makeHermesTextGeneration(effectiveConfig, processEnv);
105
106 const checkProvider = checkHermesProviderStatus(effectiveConfig, processEnv).pipe(
107 Effect.map(stampIdentity),
108 Effect.provideService(Crypto.Crypto, crypto),
109 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
110 );
111
112 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
113 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<HermesSettings>>({
114 resolveMaintenance: () => Effect.succeed(MAINTENANCE_CAPABILITIES),
115 getSettings: snapshotSettings.getSettings,
116 streamSettings: snapshotSettings.streamSettings,
117 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
118 initialSnapshot: (settings) =>
119 buildInitialHermesProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
120 checkProvider,
121 // Model data comes exclusively from ACP session setup during provider
122 // status checks; enrichment only republishes version advisory metadata.
123 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
124 enrichHermesSnapshot({
125 settings: settings.provider,
126 snapshot: currentSnapshot,
127 maintenanceCapabilities: MAINTENANCE_CAPABILITIES,
128 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
129 publishSnapshot,
130 stampIdentity,
131 httpClient,
132 }),
133 }).pipe(
134 Effect.mapError(
135 (cause) =>
136 new ProviderDriverError({
137 driver: DRIVER_KIND,
138 instanceId,
139 detail: `Failed to build Hermes snapshot: ${cause.message ?? String(cause)}`,
140 cause,
141 }),
142 ),
143 );
144
145 return {
146 instanceId,
147 driverKind: DRIVER_KIND,
148 continuationIdentity,
149 displayName,
150 accentColor,
151 enabled,
152 snapshot,
153 adapter,
154 textGeneration,
155 } satisfies ProviderInstance;
156 }),
157};