Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/server/src/provider/Drivers/SazabiDriver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it } from "@effect/vitest";

import { SazabiDriver } from "./SazabiDriver.ts";

describe("SazabiDriver", () => {
it("registers the sazabi driver kind with multi-instance support", () => {
expect(SazabiDriver.driverKind).toBe("sazabi");
expect(SazabiDriver.metadata.displayName).toBe("Sazabi");
expect(SazabiDriver.metadata.supportsMultipleInstances).toBe(true);
});

it("produces a disabled, credential-free default config", () => {
const config = SazabiDriver.defaultConfig();
expect(config.enabled).toBe(false);
expect(config.apiBaseUrl).toBe("");
expect(config.projectId).toBe("");
expect(config.binaryPath).toBe("");
expect(config.customModels).toEqual([]);
});

it("decodes a populated instance config through its schema", () => {
const decode = SazabiDriver.configSchema;
expect(decode).toBeDefined();
const config = SazabiDriver.defaultConfig();
// The token is never part of the persisted config — only connection hints.
expect(config).not.toHaveProperty("apiToken");
expect(config).not.toHaveProperty("token");
});
});
167 changes: 167 additions & 0 deletions apps/server/src/provider/Drivers/SazabiDriver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* SazabiDriver — `ProviderDriver` for the Sazabi **cloud** provider.
*
* Scaffold only (PR T1). Mirrors the Grok / OpenCode drivers: a plain value
* whose `create()` bundles `snapshot` / `adapter` / `textGeneration` closures
* over the per-instance `SazabiSettings`.
*
* Sazabi is Path A (cloud): the adapter will talk to the Sazabi public API
* over HTTP/SSE. This scaffold wires up an availability probe (token/env or an
* optional `sazabi whoami`) and an "empty" adapter whose `streamEvents` PubSub
* is ready for PR T2 to fill in with real streaming + cancel. The driver is
* fully constructable at boot so the provider surfaces in the catalog + settings
* and reports a clear reason when unauthenticated.
*
* @module provider/Drivers/SazabiDriver
*/
import { SazabiSettings, ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { ChildProcessSpawner } from "effect/unstable/process";

import { ServerSettingsService } from "../../serverSettings.ts";
import { makeSazabiTextGeneration } from "../../textGeneration/SazabiTextGeneration.ts";
import { ProviderDriverError } from "../Errors.ts";
import { makeSazabiAdapter } from "../Layers/SazabiAdapter.ts";
import {
buildInitialSazabiProviderSnapshot,
checkSazabiProviderStatus,
} from "../Layers/SazabiProvider.ts";
import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
import {
defaultProviderContinuationIdentity,
type ProviderDriver,
type ProviderInstance,
} from "../ProviderDriver.ts";
import type { ServerProviderDraft } from "../providerSnapshot.ts";
import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
import {
makeManualOnlyProviderMaintenanceCapabilities,
makeStaticProviderMaintenanceResolver,
resolveProviderMaintenanceCapabilitiesEffect,
} from "../providerMaintenance.ts";
import {
haveProviderSnapshotSettingsChanged,
makeProviderSnapshotSettingsSource,
type ProviderSnapshotSettings,
} from "../providerUpdateSettings.ts";

const decodeSazabiSettings = Schema.decodeSync(SazabiSettings);

const DRIVER_KIND = ProviderDriverKind.make("sazabi");
const SNAPSHOT_REFRESH_INTERVAL = Duration.minutes(5);

// Cloud provider: no local package/binary to self-update. Manual only.
const UPDATE = makeStaticProviderMaintenanceResolver(
makeManualOnlyProviderMaintenanceCapabilities({
provider: DRIVER_KIND,
packageName: null,
}),
);

export type SazabiDriverEnv =
| ChildProcessSpawner.ChildProcessSpawner
| FileSystem.FileSystem
| Path.Path
| ProviderEventLoggers
| ServerSettingsService;

const withInstanceIdentity =
(input: {
readonly instanceId: ProviderInstance["instanceId"];
readonly displayName: string | undefined;
readonly accentColor: string | undefined;
readonly continuationGroupKey: string;
}) =>
(snapshot: ServerProviderDraft): ServerProvider => ({
...snapshot,
instanceId: input.instanceId,
driver: DRIVER_KIND,
...(input.displayName ? { displayName: input.displayName } : {}),
...(input.accentColor ? { accentColor: input.accentColor } : {}),
continuation: { groupKey: input.continuationGroupKey },
});

export const SazabiDriver: ProviderDriver<SazabiSettings, SazabiDriverEnv> = {
driverKind: DRIVER_KIND,
metadata: {
displayName: "Sazabi",
supportsMultipleInstances: true,
},
configSchema: SazabiSettings,
defaultConfig: (): SazabiSettings => decodeSazabiSettings({}),
create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const serverSettings = yield* ServerSettingsService;
const eventLoggers = yield* ProviderEventLoggers;
const processEnv = mergeProviderInstanceEnvironment(environment);
const continuationIdentity = defaultProviderContinuationIdentity({
driverKind: DRIVER_KIND,
instanceId,
});
const stampIdentity = withInstanceIdentity({
instanceId,
displayName,
accentColor,
continuationGroupKey: continuationIdentity.continuationKey,
});
const effectiveConfig = { ...config, enabled } satisfies SazabiSettings;
const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
binaryPath: effectiveConfig.binaryPath,
env: processEnv,
});

const adapter = yield* makeSazabiAdapter(effectiveConfig, {
environment: processEnv,
...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
instanceId,
});
const textGeneration = yield* makeSazabiTextGeneration(effectiveConfig, processEnv);

const checkProvider = checkSazabiProviderStatus(effectiveConfig, processEnv).pipe(
Effect.map(stampIdentity),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
);

const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<SazabiSettings>>({
maintenanceCapabilities,
getSettings: snapshotSettings.getSettings,
streamSettings: snapshotSettings.streamSettings,
haveSettingsChanged: haveProviderSnapshotSettingsChanged,
initialSnapshot: (settings) =>
buildInitialSazabiProviderSnapshot(settings.provider, processEnv).pipe(
Effect.map(stampIdentity),
),
checkProvider,
refreshInterval: SNAPSHOT_REFRESH_INTERVAL,
}).pipe(
Effect.mapError(
(cause) =>
new ProviderDriverError({
driver: DRIVER_KIND,
instanceId,
detail: `Failed to build Sazabi snapshot: ${cause.message ?? String(cause)}`,
cause,
}),
),
);

return {
instanceId,
driverKind: DRIVER_KIND,
continuationIdentity,
displayName,
accentColor,
enabled,
snapshot,
adapter,
textGeneration,
} satisfies ProviderInstance;
}),
};
131 changes: 131 additions & 0 deletions apps/server/src/provider/Layers/SazabiAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import {
ApprovalRequestId,
ProviderDriverKind,
ProviderInstanceId,
SazabiSettings,
ThreadId,
} from "@t3tools/contracts";

import type { SazabiAdapterShape } from "../Services/SazabiAdapter.ts";
import { makeSazabiAdapter, SAZABI_ADAPTER_NOT_IMPLEMENTED_DETAIL } from "./SazabiAdapter.ts";

const decodeSazabiSettings = Schema.decodeSync(SazabiSettings);

const withAdapter = <A, E>(use: (adapter: SazabiAdapterShape) => Effect.Effect<A, E>) =>
Effect.scoped(
Effect.gen(function* () {
const adapter = yield* makeSazabiAdapter(decodeSazabiSettings({ enabled: true }), {
instanceId: ProviderInstanceId.make("sazabi"),
});
return yield* use(adapter);
}),
);

const THREAD = ThreadId.make("sazabi-scaffold-thread");

describe("makeSazabiAdapter (scaffold)", () => {
it.effect("advertises the sazabi provider with model switching unsupported", () =>
withAdapter((adapter) =>
Effect.sync(() => {
expect(adapter.provider).toBe("sazabi");
expect(adapter.capabilities.sessionModelSwitch).toBe("unsupported");
}),
),
);

it.effect("fails startSession with a clear not-implemented error", () =>
withAdapter((adapter) =>
Effect.gen(function* () {
const error = yield* Effect.flip(
adapter.startSession({
threadId: THREAD,
provider: ProviderDriverKind.make("sazabi"),
cwd: process.cwd(),
runtimeMode: "full-access",
modelSelection: {
instanceId: ProviderInstanceId.make("sazabi"),
model: "sazabi-default",
},
}),
);
expect(error._tag).toBe("ProviderAdapterRequestError");
if (error._tag === "ProviderAdapterRequestError") {
expect(error.method).toBe("session/start");
expect(error.detail).toContain(SAZABI_ADAPTER_NOT_IMPLEMENTED_DETAIL);
}
}),
),
);

it.effect("fails sendTurn with a clear not-implemented error", () =>
withAdapter((adapter) =>
Effect.gen(function* () {
const error = yield* Effect.flip(
adapter.sendTurn({ threadId: THREAD, input: "hello sazabi", attachments: [] }),
);
expect(error._tag).toBe("ProviderAdapterRequestError");
if (error._tag === "ProviderAdapterRequestError") {
expect(error.method).toBe("session/prompt");
expect(error.detail).toContain(SAZABI_ADAPTER_NOT_IMPLEMENTED_DETAIL);
}
}),
),
);

it.effect("fails readThread and rollbackThread as not implemented", () =>
withAdapter((adapter) =>
Effect.gen(function* () {
const readError = yield* Effect.flip(adapter.readThread(THREAD));
expect(readError._tag).toBe("ProviderAdapterRequestError");
if (readError._tag === "ProviderAdapterRequestError") {
expect(readError.method).toBe("thread/read");
}

const rollbackError = yield* Effect.flip(adapter.rollbackThread(THREAD, 1));
expect(rollbackError._tag).toBe("ProviderAdapterRequestError");
if (rollbackError._tag === "ProviderAdapterRequestError") {
expect(rollbackError.method).toBe("thread/rollback");
}
}),
),
);

it.effect("fails interactive responses as not implemented", () =>
withAdapter((adapter) =>
Effect.gen(function* () {
const approvalError = yield* Effect.flip(
adapter.respondToRequest(THREAD, ApprovalRequestId.make("req-1"), "accept"),
);
expect(approvalError._tag).toBe("ProviderAdapterRequestError");
if (approvalError._tag === "ProviderAdapterRequestError") {
expect(approvalError.method).toBe("session/request_permission");
}

const inputError = yield* Effect.flip(
adapter.respondToUserInput(THREAD, ApprovalRequestId.make("req-2"), {}),
);
expect(inputError._tag).toBe("ProviderAdapterRequestError");
if (inputError._tag === "ProviderAdapterRequestError") {
expect(inputError.method).toBe("session/user_input");
}
}),
),
);

it.effect("treats interrupt/stop lifecycle operations as safe no-ops", () =>
withAdapter((adapter) =>
Effect.gen(function* () {
yield* adapter.interruptTurn(THREAD);
yield* adapter.stopSession(THREAD);
yield* adapter.stopAll();
const sessions = yield* adapter.listSessions();
const hasSession = yield* adapter.hasSession(THREAD);
expect(sessions).toEqual([]);
expect(hasSession).toBe(false);
}),
),
);
});
Loading
Loading