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
6 changes: 3 additions & 3 deletions apps/server/src/environment/ServerEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("ServerEnvironmen
it.effect.each([
{ platform: "darwin", architecture: "arm64", supported: true },
{ platform: "linux", architecture: "x64", supported: true },
{ platform: "linux", architecture: "x64", libc: "musl", supported: false },
{ platform: "win32", architecture: "arm64", supported: false },
{ platform: "linux", architecture: "arm", supported: false },
{ platform: "linux", architecture: "x64", libc: "musl", supported: true },
{ platform: "win32", architecture: "arm64", supported: true },
{ platform: "linux", architecture: "arm", supported: true },
] as const)("advertises voice transcription on $platform-$architecture: $supported", (host) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
Expand Down
5 changes: 2 additions & 3 deletions apps/server/src/environment/ServerEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { resolveServiceLauncherMode } from "../cloud/serviceLauncherClient.ts";
import * as ServerConfig from "../config.ts";
import { databaseMigrationHead } from "../persistence/databaseMigrationHead.ts";
import * as ProcessRunner from "../processRunner.ts";
import { isSpeechPlatformSupported } from "../speech/platform.ts";
import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
import { detectServerEnvironmentMachineKind } from "./ServerEnvironmentMachine.ts";

Expand Down Expand Up @@ -190,7 +189,6 @@ export const make = Effect.gen(function* () {
const identity = yield* ServerEnvironmentIdentity;
const hostPlatform = yield* HostProcess.Platform;
const hostArchitecture = yield* HostProcess.Architecture;
const hostLibc = hostPlatform === "linux" ? yield* HostProcess.LinuxLibc : undefined;
const environmentId = yield* identity.getEnvironmentId;
const cwdBaseName = path.basename(serverConfig.cwd).trim();
const label = yield* resolveServerEnvironmentLabel({ cwdBaseName });
Expand Down Expand Up @@ -273,7 +271,8 @@ export const make = Effect.gen(function* () {
freshContext: true,
scheduledTaskThreadModes: true,
npmReleaseLookup: true,
voiceTranscription: isSpeechPlatformSupported(hostPlatform, hostArchitecture, hostLibc),
// Custom speech endpoints work even where the native speech engine is unavailable.
voiceTranscription: true,
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
...(serverInstallation === null ? {} : { serverInstallation }),
...(serverSelfUpdate === "boot-service" || desktopAppUpdate
Expand Down
41 changes: 40 additions & 1 deletion apps/server/src/serverSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1311,15 +1311,21 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
);

it.effect(
"uses the same secret lifecycle for provider config, environment, host tokens, and quota keys",
"uses the same secret lifecycle for provider config, environment, host tokens, quota keys, and speech",
() =>
Effect.gen(function* () {
const service = yield* ServerSettingsModule.ServerSettingsService;
const config = yield* ServerConfig.ServerConfig;
const fs = yield* FileSystem.FileSystem;
const store = yield* ServerSecretStore.ServerSecretStore;
const instanceId = ProviderInstanceId.make("antigravity_secrets");
const speechEndpoint = {
url: `https://speech.example.test/${"a".repeat(300)}/transcribe`,
model: "whisper",
apiKey: "speech-secret",
};
const saved = yield* service.updateSettings({
speechEndpoint,
providerInstances: {
[instanceId]: {
driver: ProviderDriverKind.make("antigravity"),
Expand Down Expand Up @@ -1363,6 +1369,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
"gitcafe-secret",
"quota-secret",
"expo-secret",
"speech-secret",
];
const client = ServerSettingsModule.redactServerSettingsForClient(saved, {
secretReferences: true,
Expand All @@ -1380,8 +1387,29 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
sourceControlHosts: client.sourceControlHosts,
usageLimitSources: client.usageLimitSources,
easDeviceHosts: client.easDeviceHosts,
speechEndpoint: client.speechEndpoint,
});
for (const value of values) assert.include(JSON.stringify(echoed), value);
// An echoed placeholder must never carry another destination's credentials.
const moved = yield* service.updateSettings({
speechEndpoint: {
...client.speechEndpoint!,
url: "https://other.example.test/transcribe",
},
});
assert.equal(moved.speechEndpoint?.apiKey, "");
for (const apiKey of ["replacement-speech-secret", ""]) {
const changed = yield* service.updateSettings({
speechEndpoint: { ...speechEndpoint, apiKey },
});
assert.equal(changed.speechEndpoint?.apiKey, apiKey);
assert.equal(
ServerSettingsModule.redactServerSettingsForClient(changed, { secretReferences: true })
.speechEndpoint?.apiKey,
apiKey ? "••••••" : "",
);
}
yield* service.updateSettings({ speechEndpoint });
const renamed = yield* service.updateProviderInstance({
operation: "upsert",
instanceId,
Expand Down Expand Up @@ -1432,6 +1460,16 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
sourceControlHostSecretName({ kind: "gitcafe", field: "token", serverHost: null }),
"usage-limit-source-aHVi",
];
const files = yield* fs.readDirectory(config.secretsDir);
secretNames.push(
...files
.filter((name) => name.startsWith("speech-endpoint-"))
.map((name) => name.replace(/\.bin$/, "")),
);
assert.lengthOf(
secretNames.filter((name) => name.startsWith("speech-endpoint-")),
1,
);
for (const name of secretNames) {
assert.isTrue(Option.isSome(yield* store.get(name)), name);
}
Expand Down Expand Up @@ -1463,6 +1501,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
gitcafe: { token: "" },
},
usageLimitSources: { [UsageLimitSourceId.make("hub")]: null },
speechEndpoint: null,
});
for (const name of secretNames) {
assert.isTrue(Option.isNone(yield* store.get(name)));
Expand Down
13 changes: 13 additions & 0 deletions apps/server/src/settingsSecrets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @effect-diagnostics-next-line nodeBuiltinImport:off - Secret slot mapping is synchronous; URL hashes keep filenames bounded.
import * as NodeCrypto from "node:crypto";
import {
AntigravitySettings,
ClaudeSettings,
Expand All @@ -7,6 +9,7 @@ import {
type ServerSettings,
type SettingsSecretValue,
SimlockDeviceHostConfig,
SpeechEndpointConfig,
UsageLimitSourceConfig,
isSecretReference,
secretSettingsFields,
Expand Down Expand Up @@ -47,6 +50,7 @@ const providerSecretFields = new Map([
const usageLimitSecretFields = secretSettingsFields(UsageLimitSourceConfig);
const easDeviceHostSecretFields = secretSettingsFields(EasDeviceHostConfig);
const simlockDeviceHostSecretFields = secretSettingsFields(SimlockDeviceHostConfig);
const speechEndpointSecretFields = secretSettingsFields(SpeechEndpointConfig);
const encode = (value: string) => Buffer.from(value, "utf8").toString("base64url");

export interface SettingsSecret {
Expand Down Expand Up @@ -232,6 +236,15 @@ function mapSecretFieldGroups(

return {
...settings,
speechEndpoint: settings.speechEndpoint
? (mapFields(
settings.speechEndpoint,
speechEndpointSecretFields,
// Bind credentials to this destination without exceeding filesystem name limits.
(field) =>
`speech-endpoint-${NodeCrypto.createHash("sha256").update(settings.speechEndpoint!.url).digest("hex")}-${encode(field)}`,
) as SpeechEndpointConfig)
: null,
providerInstances,
sourceControlHosts,
usageLimitSources,
Expand Down
112 changes: 112 additions & 0 deletions apps/server/src/speech/SpeechService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,118 @@ it("rejects empty, misaligned, non-finite, and oversized PCM as invalid audio",
}
});

it.effect(
"transcribes through an endpoint without a native engine and preserves dictionary corrections",
() =>
Effect.gen(function* () {
const speech = yield* SpeechService.SpeechService;
const settings = yield* ServerSettings.ServerSettingsService;
const endpoint = {
url: "http://localhost:8000/v1/audio/transcriptions",
model: "parakeet",
apiKey: "test-key",
};
yield* settings.updateSettings({ speechEndpoint: endpoint });
using network = vi
.spyOn(globalThis, "fetch")
.mockResolvedValue(Response.json({ text: "hello tay three" }));
expect(yield* speech.status).toMatchObject({
supported: true,
state: "ready",
endpointUrl: endpoint.url,
supportsStreaming: false,
supportsBufferedStreaming: false,
});
expect(yield* speech.models(false)).toEqual({ models: [] });
yield* speech.prepareModel;
expect(
yield* speech.transcribe(pcm(), {
...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
speechEndpointUrl: endpoint.url,
speechCustomWords: [{ term: "T3", aliases: ["tay three"] }],
}),
).toBe("hello T3");
expect(network).toHaveBeenCalledTimes(1);
expect(loadNative).not.toHaveBeenCalled();
expect(download).not.toHaveBeenCalled();
yield* settings.updateSettings({ speechEndpoint: null });
expect(yield* speech.status).toMatchObject({ supported: false });
}).pipe(
Effect.provide(layer),
Effect.provideService(HostProcess.Platform, "win32"),
Effect.provideService(HostProcess.Architecture, "arm64"),
),
);

it.effect(
"rejects absent or stale endpoint consent before forwarding audio, including streaming",
() =>
Effect.gen(function* () {
const speech = yield* SpeechService.SpeechService;
const settings = yield* ServerSettings.ServerSettingsService;
const endpoint = {
url: "https://speech.example.test/v1/audio/transcriptions",
model: "whisper",
apiKey: "",
};
using network = vi
.spyOn(globalThis, "fetch")
.mockResolvedValue(Response.json({ text: "unused" }));
yield* settings.updateSettings({ speechEndpoint: endpoint });
for (const speechEndpointUrl of [undefined, "https://previous.example.test/transcribe"]) {
const options = {
...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
...(speechEndpointUrl ? { speechEndpointUrl } : {}),
};
const result = yield* Effect.result(speech.transcribe(pcm(), options));
expect(Result.isFailure(result) && result.failure).toMatchObject({
_tag: "SpeechDestinationChangedError",
});
}
const options = { ...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, speechEndpointUrl: endpoint.url };
const stream = yield* Effect.result(speech.startStream(options).pipe(Effect.scoped));
expect(Result.isFailure(stream) && stream.failure).toMatchObject({
_tag: "SpeechDestinationChangedError",
});
yield* settings.updateSettings({ speechEndpoint: null });
const local = yield* Effect.result(speech.transcribe(pcm(), options));
expect(Result.isFailure(local) && local.failure).toMatchObject({
_tag: "SpeechDestinationChangedError",
});
expect(network).not.toHaveBeenCalled();
expect(loadNative).not.toHaveBeenCalled();
}).pipe(Effect.provide(layer)),
);

it.effect("reports an endpoint failure without falling back to native transcription", () =>
Effect.gen(function* () {
const speech = yield* SpeechService.SpeechService;
const settings = yield* ServerSettings.ServerSettingsService;
const endpoint = {
url: "https://speech.example.test/transcribe",
model: "whisper",
apiKey: "",
};
yield* settings.updateSettings({ speechEndpoint: endpoint });
using network = vi
.spyOn(globalThis, "fetch")
.mockResolvedValue(new Response(null, { status: 503 }));
const result = yield* Effect.result(
speech.transcribe(pcm(), {
...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
speechEndpointUrl: endpoint.url,
}),
);
expect(Result.isFailure(result) && result.failure).toMatchObject({
_tag: "SpeechOperationError",
operation: "endpoint transcription",
});
expect(network).toHaveBeenCalledTimes(1);
expect(loadNative).not.toHaveBeenCalled();
expect(yield* speech.status).toMatchObject({ state: "ready" });
}).pipe(Effect.provide(layer)),
);

it.effect("prepares a batch model before audio arrives and reuses it for transcription", () =>
Effect.gen(function* () {
const speech = yield* SpeechService.SpeechService;
Expand Down
Loading
Loading