Skip to content

Commit 4788b96

Browse files
fix(voice): isolate cleanup catalogs and credential drafts
1 parent 7475530 commit 4788b96

15 files changed

Lines changed: 370 additions & 205 deletions

File tree

‎apps/server/src/speech/SpeechCleanupApi.test.ts‎

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ it.effect.each(["openai", "anthropic", "compatible", "openrouter"] as const)(
5656
const api = yield* SpeechCleanupApi.SpeechCleanupApi.pipe(
5757
Effect.provide(
5858
layer({
59-
speechPostProcessingProvider: provider === "openrouter" ? "compatible" : provider,
59+
speechPostProcessingProvider: "agent",
6060
speechPostProcessingCompatiblePreset:
6161
provider === "openrouter" ? "openrouter" : "custom",
6262
speechPostProcessingApiEndpoints: {
@@ -71,15 +71,19 @@ it.effect.each(["openai", "anthropic", "compatible", "openrouter"] as const)(
7171
}),
7272
),
7373
);
74-
const result = yield* api.models.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream));
74+
const result = yield* api
75+
.models(provider)
76+
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream));
7577
expect(result.models.map((model) => model.id)).toEqual(
7678
provider === "anthropic" ? ["claude-first", "model-a", "model-b"] : ["model-a", "model-b"],
7779
);
7880
if (provider === "anthropic") expect(result.models[0]?.reasoningLevels).toEqual(["low"]);
7981
expect(upstream).toHaveBeenCalledTimes(provider === "anthropic" ? 2 : 1);
8082
upstream.mockResolvedValueOnce(Response.json({ data: "invalid" }));
8183
expect(
82-
yield* Effect.flip(api.models.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream))),
84+
yield* Effect.flip(
85+
api.models(provider).pipe(Effect.provideService(FetchHttpClient.Fetch, upstream)),
86+
),
8387
).toMatchObject({ operation: "models", reason: "response" });
8488
}),
8589
);
@@ -120,37 +124,64 @@ it.effect(
120124
}),
121125
);
122126

123-
it.effect("cleans text in one OpenAI request without sending an STT credential", () =>
124-
Effect.gen(function* () {
125-
const upstream = vi.fn<typeof fetch>(async (url, init) => {
126-
expect(String(url)).toBe("https://api.openai.com/v1/responses");
127-
expect(new Headers(init?.headers).get("authorization")).toBe("Bearer openai-key");
128-
expect(JSON.parse(String(init?.body))).toMatchObject({
129-
model: "gpt-4.1-mini",
130-
input: "Clean this transcript",
131-
store: false,
132-
reasoning: { effort: "low" },
133-
});
134-
return Response.json({
135-
status: "completed",
136-
output: [{ type: "message", content: [{ type: "output_text", text: "Cleaned." }] }],
127+
it.effect.each([null, "low"] as const)(
128+
"cleans text in one OpenAI request with %s effort and no STT credential",
129+
(effort) =>
130+
Effect.gen(function* () {
131+
const upstream = vi.fn<typeof fetch>(async (url, init) => {
132+
expect(String(url)).toBe("https://api.openai.com/v1/responses");
133+
expect(new Headers(init?.headers).get("authorization")).toBe("Bearer openai-key");
134+
expect(JSON.parse(String(init?.body))).toMatchObject({
135+
model: "gpt-4.1-mini",
136+
input: "Clean this transcript",
137+
store: false,
138+
});
139+
expect(JSON.parse(String(init?.body)).reasoning).toEqual(
140+
effort === null ? undefined : { effort },
141+
);
142+
return Response.json({
143+
status: "completed",
144+
output: [{ type: "message", content: [{ type: "output_text", text: "Cleaned." }] }],
145+
});
137146
});
138-
});
139-
const api = yield* SpeechCleanupApi.SpeechCleanupApi.pipe(
140-
Effect.provide(
141-
layer({
142-
voiceApiCredentials: { openaiPostProcessing: "openai-key" },
143-
speechPostProcessingOpenAIReasoning: "low",
144-
}),
145-
),
146-
);
147-
expect(
148-
yield* api
149-
.postProcess("Clean this transcript")
150-
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream)),
151-
).toBe("Cleaned.");
152-
expect(upstream).toHaveBeenCalledTimes(1);
153-
}),
147+
const api = yield* SpeechCleanupApi.SpeechCleanupApi.pipe(
148+
Effect.provide(
149+
layer({
150+
speechPostProcessingProvider: "openai",
151+
voiceApiCredentials: { openaiPostProcessing: "openai-key" },
152+
speechPostProcessingOpenAIReasoning: effort,
153+
}),
154+
),
155+
);
156+
expect(
157+
yield* api
158+
.postProcess("Clean this transcript")
159+
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream)),
160+
).toBe("Cleaned.");
161+
expect(upstream).toHaveBeenCalledTimes(1);
162+
}),
163+
);
164+
165+
it.effect.each(["agent", "opencode"] as const)(
166+
"never sends %s cleanup to OpenAI through the API service",
167+
(provider) =>
168+
Effect.gen(function* () {
169+
const upstream = vi.fn<typeof fetch>();
170+
const api = yield* SpeechCleanupApi.SpeechCleanupApi.pipe(
171+
Effect.provide(
172+
layer({
173+
speechPostProcessingProvider: provider,
174+
voiceApiCredentials: { openaiPostProcessing: "private-openai-key" },
175+
}),
176+
),
177+
);
178+
expect(
179+
yield* api
180+
.postProcess("Clean")
181+
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream), Effect.flip),
182+
).toMatchObject({ reason: "configuration" });
183+
expect(upstream).not.toHaveBeenCalled();
184+
}),
154185
);
155186

156187
it.effect.each(["compatible", "openrouter", "anthropic"] as const)(
@@ -283,5 +314,12 @@ it.effect(
283314
),
284315
).toMatchObject({ operation: "post-processing", reason: "credential" });
285316
expect(upstream).toHaveBeenCalledTimes(1);
286-
}).pipe(Effect.provide(layer({ voiceApiCredentials: { openaiPostProcessing: "key" } }))),
317+
}).pipe(
318+
Effect.provide(
319+
layer({
320+
speechPostProcessingProvider: "openai",
321+
voiceApiCredentials: { openaiPostProcessing: "key" },
322+
}),
323+
),
324+
),
287325
);

‎apps/server/src/speech/SpeechCleanupApi.ts‎

Lines changed: 84 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
isSecretReference,
33
SpeechCleanupReasoning,
44
type SpeechCleanupModels,
5+
type SpeechCleanupApiProvider,
56
} from "@t3tools/contracts";
67
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
78
import * as Context from "effect/Context";
@@ -31,7 +32,9 @@ export class SpeechCleanupApi extends Context.Service<
3132
SpeechCleanupApi,
3233
{
3334
readonly postProcess: (prompt: string) => Effect.Effect<string, SpeechCleanupApiError>;
34-
readonly models: Effect.Effect<SpeechCleanupModels, SpeechCleanupApiError>;
35+
readonly models: (
36+
provider: SpeechCleanupApiProvider,
37+
) => Effect.Effect<SpeechCleanupModels, SpeechCleanupApiError>;
3538
}
3639
>()("t3/speech/SpeechCleanupApi") {}
3740

@@ -163,59 +166,58 @@ const make = Effect.gen(function* () {
163166
),
164167
);
165168

166-
const credential = Effect.fn("SpeechCleanupApi.credential")(function* (
167-
operation: "post-processing" | "models",
169+
const configuration = Effect.fn("SpeechCleanupApi.configuration")(function* (
170+
operation: SpeechCleanupApiError["operation"],
171+
requestedProvider?: SpeechCleanupApiProvider,
168172
) {
169173
const settings = yield* settingsService.getSettings.pipe(
170174
Effect.mapError(
171175
(cause) => new SpeechCleanupApiError({ operation, reason: "configuration", cause }),
172176
),
173177
);
174-
const selected = settings.speechPostProcessingProvider;
178+
const selected = requestedProvider ?? settings.speechPostProcessingProvider;
179+
if (selected === "agent" || selected === "opencode")
180+
return yield* new SpeechCleanupApiError({ operation, reason: "configuration" });
175181
const provider =
176-
selected === "compatible" && settings.speechPostProcessingCompatiblePreset === "openrouter"
182+
requestedProvider === undefined &&
183+
selected === "compatible" &&
184+
settings.speechPostProcessingCompatiblePreset === "openrouter"
177185
? "openrouter"
178186
: selected;
179-
const slot =
180-
provider === "compatible"
181-
? "compatiblePostProcessing"
182-
: provider === "anthropic"
183-
? "anthropicPostProcessing"
184-
: provider === "openrouter"
185-
? "openrouterPostProcessing"
186-
: "openaiPostProcessing";
187+
const endpoint = (
188+
provider === "openai"
189+
? "https://api.openai.com/v1"
190+
: settings.speechPostProcessingApiEndpoints[provider].endpoint
191+
).replace(/\/+$/, "");
192+
const model =
193+
provider === "openai"
194+
? settings.speechPostProcessingOpenAIModel
195+
: settings.speechPostProcessingApiEndpoints[provider].model;
196+
if (!endpoint || (operation === "post-processing" && !model))
197+
return yield* new SpeechCleanupApiError({ operation, reason: "configuration" });
198+
const slot = `${provider}PostProcessing` as const;
187199
const source = settings.voiceApiCredentials[slot];
188200
if (isSecretReference(source)) yield* resolver.invalidate([source]);
189201
const key = yield* requireSettingsSecret(resolver, source).pipe(
190202
Effect.mapError(
191203
(cause) => new SpeechCleanupApiError({ operation, reason: "credential", cause }),
192204
),
193205
);
194-
return { settings, key, provider };
206+
if (!key && provider !== "compatible")
207+
return yield* new SpeechCleanupApiError({ operation, reason: "credential" });
208+
return { settings, key, provider, endpoint, model };
195209
});
196210

197211
return SpeechCleanupApi.of({
198212
postProcess: Effect.fn("SpeechCleanupApi.postProcess")(function* (prompt) {
199-
const { settings, key, provider } = yield* credential("post-processing");
200-
if (!key && provider !== "compatible")
201-
return yield* new SpeechCleanupApiError({
202-
operation: "post-processing",
203-
reason: "credential",
204-
});
213+
const { settings, key, provider, endpoint, model } = yield* configuration("post-processing");
205214
if (provider === "compatible" || provider === "anthropic" || provider === "openrouter") {
206-
const config = settings.speechPostProcessingApiEndpoints[provider];
207-
if (!config.endpoint || !config.model)
208-
return yield* new SpeechCleanupApiError({
209-
operation: "post-processing",
210-
reason: "configuration",
211-
});
212215
const anthropic = provider === "anthropic";
213-
const endpoint = config.endpoint.replace(/\/+$/, "");
214216
let request = HttpClientRequest.post(
215217
`${endpoint}${anthropic ? "/messages" : "/chat/completions"}`,
216218
).pipe(
217219
HttpClientRequest.bodyJsonUnsafe({
218-
model: config.model,
220+
model,
219221
messages: [{ role: "user", content: prompt }],
220222
...(anthropic
221223
? {
@@ -239,10 +241,10 @@ const make = Effect.gen(function* () {
239241
}
240242
return yield* requestText(
241243
"post-processing",
242-
HttpClientRequest.post("https://api.openai.com/v1/responses").pipe(
244+
HttpClientRequest.post(`${endpoint}/responses`).pipe(
243245
HttpClientRequest.bearerToken(Redacted.make(key)),
244246
HttpClientRequest.bodyJsonUnsafe({
245-
model: settings.speechPostProcessingOpenAIModel,
247+
model,
246248
input: prompt,
247249
store: false,
248250
...(settings.speechPostProcessingOpenAIReasoning === null
@@ -255,69 +257,60 @@ const make = Effect.gen(function* () {
255257
"responses",
256258
);
257259
}),
258-
models: Effect.gen(function* () {
259-
const operation = "models";
260-
const { settings, key, provider } = yield* credential(operation);
261-
if (provider === "agent" || provider === "opencode")
262-
return yield* new SpeechCleanupApiError({ operation, reason: "configuration" });
263-
if (!key && provider !== "compatible")
264-
return yield* new SpeechCleanupApiError({ operation, reason: "credential" });
265-
const endpoint =
266-
provider === "openai"
267-
? "https://api.openai.com/v1"
268-
: settings.speechPostProcessingApiEndpoints[provider].endpoint.replace(/\/+$/, "");
269-
if (!endpoint)
270-
return yield* new SpeechCleanupApiError({ operation, reason: "configuration" });
271-
const models = new Map<string, SpeechCleanupModels["models"][number]>();
272-
let cursor: string | undefined;
273-
for (let page = 0; page < 10; page++) {
274-
let request = HttpClientRequest.get(`${endpoint}/models`);
275-
if (provider === "anthropic") {
276-
request = request.pipe(
277-
HttpClientRequest.setHeader("x-api-key", key),
278-
HttpClientRequest.setHeader("anthropic-version", "2023-06-01"),
279-
HttpClientRequest.setUrlParam("limit", "1000"),
260+
models: Effect.fn("SpeechCleanupApi.models")(
261+
function* (provider) {
262+
const operation = "models";
263+
const { key, endpoint } = yield* configuration(operation, provider);
264+
const models = new Map<string, SpeechCleanupModels["models"][number]>();
265+
let cursor: string | undefined;
266+
for (let page = 0; page < 10; page++) {
267+
let request = HttpClientRequest.get(`${endpoint}/models`);
268+
if (provider === "anthropic") {
269+
request = request.pipe(
270+
HttpClientRequest.setHeader("x-api-key", key),
271+
HttpClientRequest.setHeader("anthropic-version", "2023-06-01"),
272+
HttpClientRequest.setUrlParam("limit", "1000"),
273+
);
274+
if (cursor) request = HttpClientRequest.setUrlParam(request, "after_id", cursor);
275+
} else if (key) request = HttpClientRequest.bearerToken(request, Redacted.make(key));
276+
const response = yield* http.execute(request).pipe(
277+
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }),
278+
Effect.provideService(HttpClient.TracerPropagationEnabled, false),
279+
Effect.mapError(() => new SpeechCleanupApiError({ operation, reason: "unavailable" })),
280280
);
281-
if (cursor) request = HttpClientRequest.setUrlParam(request, "after_id", cursor);
282-
} else if (key) request = HttpClientRequest.bearerToken(request, Redacted.make(key));
283-
const response = yield* http.execute(request).pipe(
284-
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }),
285-
Effect.provideService(HttpClient.TracerPropagationEnabled, false),
286-
Effect.mapError(() => new SpeechCleanupApiError({ operation, reason: "unavailable" })),
287-
);
288-
if (response.status < 200 || response.status >= 300)
289-
return yield* new SpeechCleanupApiError({
290-
operation,
291-
reason: "http",
292-
status: response.status,
293-
});
294-
const result = yield* HttpClientResponse.schemaBodyJson(ModelPage)(response).pipe(
295-
Effect.mapError(() => new SpeechCleanupApiError({ operation, reason: "response" })),
296-
);
297-
for (const model of result.data) {
298-
if (!model.id.trim() || model.id.length > 200) continue;
299-
const effort = model.capabilities?.effort;
300-
models.set(model.id, {
301-
id: model.id,
302-
name: model.display_name ?? model.name ?? model.id,
303-
...(effort
304-
? {
305-
reasoningLevels: SpeechCleanupReasoning.literals.filter((level) => {
306-
const capability = effort[level];
307-
return typeof capability === "object" && capability.supported;
308-
}),
309-
}
310-
: {}),
311-
});
281+
if (response.status < 200 || response.status >= 300)
282+
return yield* new SpeechCleanupApiError({
283+
operation,
284+
reason: "http",
285+
status: response.status,
286+
});
287+
const result = yield* HttpClientResponse.schemaBodyJson(ModelPage)(response).pipe(
288+
Effect.mapError(() => new SpeechCleanupApiError({ operation, reason: "response" })),
289+
);
290+
for (const model of result.data) {
291+
const id = model.id.trim();
292+
if (!id || id.length > 200) continue;
293+
const effort = model.capabilities?.effort;
294+
models.set(id, {
295+
id,
296+
name: model.display_name?.trim() || model.name?.trim() || id,
297+
...(effort
298+
? {
299+
reasoningLevels: SpeechCleanupReasoning.literals.filter((level) => {
300+
const capability = effort[level];
301+
return typeof capability === "object" && capability.supported;
302+
}),
303+
}
304+
: {}),
305+
});
306+
}
307+
if (!result.has_more)
308+
return { models: [...models.values()].sort((a, b) => a.id.localeCompare(b.id)) };
309+
if (provider !== "anthropic" || !result.last_id || result.last_id === cursor) break;
310+
cursor = result.last_id;
312311
}
313-
if (!result.has_more)
314-
return { models: [...models.values()].sort((a, b) => a.id.localeCompare(b.id)) };
315-
if (provider !== "anthropic" || !result.last_id || result.last_id === cursor) break;
316-
cursor = result.last_id;
317-
}
318-
return yield* new SpeechCleanupApiError({ operation, reason: "response" });
319-
}).pipe(
320-
Effect.withSpan("SpeechCleanupApi.models"),
312+
return yield* new SpeechCleanupApiError({ operation, reason: "response" });
313+
},
321314
Effect.timeoutOrElse({
322315
duration: "25 seconds",
323316
orElse: () =>

‎apps/server/src/speech/http.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ export const speechHttpApiLayer = HttpApiBuilder.group(
7474
"cleanupModels",
7575
Effect.fn("environment.voice.cleanupModels")(function* (args) {
7676
yield* guard(args.endpoint.name);
77-
return yield* cleanup.models.pipe(
78-
Effect.catch((error) => failEnvironmentInternal("internal_error", error)),
79-
);
77+
return yield* cleanup
78+
.models(args.params.provider)
79+
.pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
8080
}),
8181
)
8282
.handle(

0 commit comments

Comments
 (0)