Skip to content
Merged
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
52 changes: 52 additions & 0 deletions apps/server/src/speech/CloudSpeech.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @effect-diagnostics processEnvInEffect:off - checks that the host key leaves the live environment.
import { it } from "@effect/vitest";
import {
DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
SecretProviderId,
type SpeechTranscriptionOptions,
} from "@t3tools/contracts";
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -302,3 +304,53 @@ it.effect(
expect(yield* Effect.flip(transcribe(1600))).toMatchObject({ reason: "missing-key" });
}),
);

it.effect(
"a host-injected AssemblyAI key works without settings and never outranks a saved key",
() =>
Effect.gen(function* () {
const upstream = vi.fn<typeof fetch>(async () => Response.json({ text: "hello" }));
process.env.ASSEMBLYAI_API_KEY = "host-fixture";
const service = yield* CloudSpeech.CloudSpeech.pipe(
Effect.provide(
CloudSpeech.layer.pipe(
Layer.provide(FetchHttpClient.layer),
Layer.provide(ServerSettings.ServerSettingsService.layerTest()),
Layer.provide(Layer.mock(SecretResolver.SecretResolver)({})),
Layer.provide(
ConfigProvider.layer(
ConfigProvider.fromEnv({ env: { ASSEMBLYAI_API_KEY: " host-fixture\n" } }),
),
),
),
),
);
// Agent subprocesses must not inherit the key.
expect(process.env.ASSEMBLYAI_API_KEY).toBeUndefined();
const authorization = () =>
new Headers(upstream.mock.calls.at(-1)![1]?.headers).get("authorization");
const transcribe = () =>
service
.transcribe("assemblyai", new Uint8Array(new Float32Array(1600).buffer), {
...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
})
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream));
expect(yield* service.configured).toEqual({
openai: false,
xai: false,
openrouter: false,
assemblyai: true,
hostManaged: ["assemblyai"],
});
expect(yield* transcribe()).toBe("hello");
expect(authorization()).toBe("host-fixture");
yield* service.setKey("assemblyai", Redacted.make("saved-fixture"));
expect((yield* service.configured).hostManaged).toBeUndefined();
yield* transcribe();
expect(authorization()).toBe("saved-fixture");
yield* service.setKey("assemblyai", null);
expect((yield* service.configured).hostManaged).toEqual(["assemblyai"]);
yield* transcribe();
expect(authorization()).toBe("host-fixture");
}),
);
32 changes: 29 additions & 3 deletions apps/server/src/speech/CloudSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import {
} from "@t3tools/contracts";
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import { cloudSpeechLanguages, effectiveSpeechLanguage } from "@t3tools/shared/speech";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Redacted from "effect/Redacted";
import * as Schema from "effect/Schema";
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
Expand All @@ -35,6 +37,11 @@ const endpoints = {
};
const OPENROUTER_CATALOG_URL =
"https://openrouter.ai/api/v1/models?output_modalities=transcription&zdr=true";
/**
* Keys a host injects at launch (for example `infisical run -- t3`), so nobody types or sees them.
* A key saved in settings still wins.
*/
const HOST_KEY_VARIABLES = { assemblyai: "ASSEMBLYAI_API_KEY" } as const;
const isApiKey = Schema.is(CloudSpeechApiKey);
const isModelId = Schema.is(CloudSpeechModelId);
const sameCredential = Schema.toEquivalence(SettingsSecretValue);
Expand Down Expand Up @@ -140,15 +147,34 @@ export const layer = Layer.effect(
const settings = yield* ServerSettings.ServerSettingsService;
const resolver = yield* SecretResolver.SecretResolver;
const http = yield* HttpClient.HttpClient;
const credentials = settings.getSettings.pipe(
const hostAssemblyAi = Option.filter(
Option.map(yield* Config.Redacted(HOST_KEY_VARIABLES.assemblyai).pipe(Config.option), (key) =>
Redacted.value(key).trim(),
),
isApiKey,
);
// Agent subprocesses inherit this environment; only this service should hold the key.
// @effect-diagnostics-next-line processEnvInEffect:off
delete process.env[HOST_KEY_VARIABLES.assemblyai];
const savedCredentials = settings.getSettings.pipe(
Effect.map((value) => value.cloudSpeechCredentials),
);
const configured = credentials.pipe(
const credentials = savedCredentials.pipe(
Effect.map((saved) =>
!saved.assemblyai && Option.isSome(hostAssemblyAi)
? { ...saved, assemblyai: hostAssemblyAi.value }
: saved,
),
);
const configured = savedCredentials.pipe(
Effect.map(({ openai, xai, openrouter, assemblyai }) => ({
openai: Boolean(openai),
xai: Boolean(xai),
openrouter: Boolean(openrouter),
assemblyai: Boolean(assemblyai),
assemblyai: Boolean(assemblyai) || Option.isSome(hostAssemblyAi),
...(!assemblyai && Option.isSome(hostAssemblyAi)
? { hostManaged: ["assemblyai" as const] }
: {}),
})),
);
const execute = (request: HttpClientRequest.HttpClientRequest) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ vi.mock("../../state/environments", async (importOriginal) => ({
{
environmentId: "voice-host",
label: "Laptop",
connection: { phase: "available" },
connection: { phase: "connected" },
serverConfig: {
secretReferences: true,
settings: {
Expand All @@ -46,19 +46,21 @@ const grantKey = "t3code:voice-audio-grant:v1";
async function withVoicePanel(
route: (url: URL, request: RequestInfo | URL, init?: RequestInit) => Promise<Response> | null,
body: (screen: Awaited<ReturnType<typeof render>>) => Promise<void>,
{ connected = true, path = "/" }: { connected?: boolean; path?: string } = {},
) {
const target = new PrimaryConnectionTarget({
environmentId: id,
label: "Laptop",
httpBaseUrl: window.location.origin,
wsBaseUrl: "ws://localhost",
});
const prepared = Option.some({
const connection = Option.some({
...target,
target,
socketUrl: target.wsBaseUrl,
httpAuthorization: null,
});
const prepared = connected ? connection : Option.none();
using cleanup = new DisposableStack();
cleanup.defer(() => chooseAudioDestination(null));
const fetch = globalThis.fetch;
Expand All @@ -76,6 +78,7 @@ async function withVoicePanel(
<SettingsScopeProvider search={{}} onChange={() => {}}>
<VoiceSettingsPanel />
</SettingsScopeProvider>,
{ router: path },
);
await body(screen);
},
Expand All @@ -99,13 +102,13 @@ it("cancelling a provider's setup keeps the current destination and writes nothi
},
async (screen) => {
const destination = screen.getByRole("combobox", { name: "Transcribe with" });
await expect.element(destination).toHaveTextContent("Laptop");
await expect.element(destination).toHaveTextContent("Local model");
await destination.click();
await page.getByRole("option", { name: "OpenAI via Laptop" }).click();
await page.getByRole("option", { name: "OpenAI" }).click();
const dialog = page.getByRole("dialog");
await dialog.getByRole("button", { name: "Cancel" }).click();
await expect.element(dialog).not.toBeInTheDocument();
await expect.element(destination).toHaveTextContent("Laptop");
await expect.element(destination).toHaveTextContent("Local model");
expect(writes).toEqual([]);
},
);
Expand All @@ -132,7 +135,7 @@ it("enables the chosen provider only after a successful setup", async () => {
async (screen) => {
const destination = screen.getByRole("combobox", { name: "Transcribe with" });
await destination.click();
await page.getByRole("option", { name: "OpenAI via Laptop" }).click();
await page.getByRole("option", { name: "OpenAI" }).click();
const dialog = page.getByRole("dialog");
await dialog.getByRole("combobox", { name: "OpenAI API key source" }).click();
await page.getByRole("option", { name: "Work vault" }).click();
Expand All @@ -144,7 +147,7 @@ it("enables the chosen provider only after a successful setup", async () => {
await dialog.getByLabelText("OpenAI API key reference").click();
await userEvent.keyboard("{Enter}");
await expect.element(dialog).not.toBeInTheDocument();
await expect.element(destination).toHaveTextContent("OpenAI via Laptop");
await expect.element(destination).toHaveTextContent("OpenAI");
expect(writes).toEqual([{ key: { provider: "work", reference: "op://Work/Voice/key" } }]);
},
);
Expand Down Expand Up @@ -186,12 +189,12 @@ it("uses a provider's saved key without writing it, and turns dictation off", as
async (screen) => {
const destination = screen.getByRole("combobox", { name: "Transcribe with" });
await destination.click();
await page.getByRole("option", { name: "Grok Voice Transcribe via Laptop" }).click();
await page.getByRole("option", { name: "Grok Voice Transcribe" }).click();
await page
.getByRole("dialog")
.getByRole("button", { name: "Use Grok Voice Transcribe" })
.click();
await expect.element(destination).toHaveTextContent("Grok Voice Transcribe via Laptop");
await expect.element(destination).toHaveTextContent("Grok Voice Transcribe");
await destination.click();
await page.getByRole("option", { name: "Off", exact: true }).click();
await expect.element(destination).toHaveTextContent("Off");
Expand Down Expand Up @@ -230,7 +233,7 @@ it("OpenRouter needs an exact model, and changing it writes no credential", asyn
async (screen) => {
const destination = screen.getByRole("combobox", { name: "Transcribe with" });
await destination.click();
await page.getByRole("option", { name: "OpenRouter via Laptop" }).click();
await page.getByRole("option", { name: "OpenRouter" }).click();
const dialog = page.getByRole("dialog");
const save = dialog.getByRole("button", { name: "Save and enable" });
await expect.element(save).toBeDisabled();
Expand All @@ -246,7 +249,7 @@ it("OpenRouter needs an exact model, and changing it writes no credential", asyn
await key.fill("");
await save.click();
await expect.element(dialog).not.toBeInTheDocument();
await expect.element(destination).toHaveTextContent("OpenRouter via Laptop");
await expect.element(destination).toHaveTextContent("OpenRouter");
expect(grantModel()).toBe("other/listen-2");
expect(writes).toEqual([]);
await screen.getByRole("button", { name: "Manage OpenRouter" }).click();
Expand All @@ -256,9 +259,89 @@ it("OpenRouter needs an exact model, and changing it writes no credential", asyn
await page.getByRole("option", { name: /Acme: Transcribe 1/ }).click();
await dialog.getByRole("button", { name: "Save" }).click();
await expect.element(dialog).not.toBeInTheDocument();
await expect.element(destination).toHaveTextContent("OpenRouter via Laptop");
await expect.element(destination).toHaveTextContent("OpenRouter");
expect(grantModel()).toBe("acme/transcribe-1");
expect(writes).toEqual([]);
},
);
});

/** Every step either offers a real next action or names nothing the user can't find. */
function expectNoDeadEnd(dialog: ReturnType<typeof page.getByRole>) {
const element = dialog.element();
const buttons = [...element.querySelectorAll("button")].map((button) =>
button.textContent.trim(),
);
const forward = buttons.filter((label) => label && !["Cancel", "Close"].includes(label));
expect(forward.length).toBeGreaterThan(0);
for (const [, name] of element.textContent.matchAll(
/\b(?:press|click|tap|hit|open)\s+([A-Z][\w ]*?)(?=[ .,]|$)/g,
))
expect(buttons.some((label) => label.includes(name!))).toBe(true);
if (/\bcontinue\b/i.test(element.textContent)) expect(buttons).toContain("Continue");
}

it("a backend on a disconnected environment points to a real control, never a missing Continue", async () => {
chooseAudioDestination(null);
await withVoicePanel(
() => null,
async (screen) => {
await screen.getByRole("combobox", { name: "Transcribe with" }).click();
await page.getByRole("option", { name: /AssemblyAI/ }).click();
const dialog = page.getByRole("dialog");
await expect
.element(dialog.getByText("Laptop isn’t connected.", { exact: false }))
.toBeVisible();
expectNoDeadEnd(dialog);
await dialog.getByRole("button", { name: "Open Environments" }).click();
await expect.element(dialog).not.toBeInTheDocument();
expect(screen.router?.state.location.pathname).toBe("/settings/connections");
},
{ connected: false },
);
});

it("AssemblyAI with a host-provided key enables in one step, with nothing to type or remove", async () => {
// Never chosen, unlike an explicit Off.
window.localStorage.removeItem(grantKey);
const writes: unknown[] = [];
await withVoicePanel(
(url) => {
if (url.pathname === "/api/voice/cloud/keys")
return Promise.resolve(
Response.json({
openai: false,
xai: false,
assemblyai: true,
hostManaged: ["assemblyai"],
}),
);
if (url.pathname.endsWith("/key")) writes.push(url.pathname);
return null;
},
async (screen) => {
// The composer's mic sends first-time users here; the recommended backend is offered at once.
const dialog = page.getByRole("dialog");
await expect.element(dialog.getByText("Use AssemblyAI for dictation?")).toBeVisible();
await expect
.element(
dialog.getByText("was started with, so there’s nothing to enter", { exact: false }),
)
.toBeVisible();
expect(dialog.element().querySelector("input")).toBeNull();
expectNoDeadEnd(dialog);
await dialog.getByRole("button", { name: "Use AssemblyAI" }).click();
await expect.element(dialog).not.toBeInTheDocument();
const destination = screen.getByRole("combobox", { name: "Transcribe with" });
await expect.element(destination).toHaveTextContent("AssemblyAI");
await screen.getByRole("button", { name: "Manage key" }).click();
await expect.element(dialog).toBeVisible();
expect(dialog.element().querySelector("input")).toBeNull();
await expect
.element(dialog.getByRole("button", { name: "Remove key…" }))
.not.toBeInTheDocument();
expect(writes).toEqual([]);
},
{ path: "/settings/voice#voice-transcription-destination" },
);
});
Loading
Loading