Repository navigation
Expand file tree
/
Copy pathcheaperinference.ts
More file actions
69 lines (64 loc) · 3.04 KB
/
Copy pathcheaperinference.ts
File metadata and controls
69 lines (64 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { BaseExecutor, type ProviderCredentials } from "./base.ts";
import { PROVIDERS } from "../config/constants.ts";
import { getModelTargetFormat, PROVIDER_ID_TO_ALIAS } from "../config/providerModels.ts";
/**
* CheaperInferenceExecutor — api.cheaperinference.com.
*
* The gateway is OpenAI-compatible on both surfaces, so everything else comes from
* BaseExecutor. Two provider-specific facts need handling (both measured against the
* live API on 2026-07-31, not inferred from docs):
*
* 1. `/v1/responses` is STATELESS and REQUIRES `store:false`. Omitting it returns
* HTTP 400 ("This Responses-compatible endpoint is stateless. Send store=false…").
* chatCore.ts deletes `store` for every provider except "openai" — a strip shared
* by ~290 providers that must not be special-cased — so we re-add it here, after
* that strip has run. A client-supplied `store:true` is overwritten rather than
* forwarded: the endpoint cannot honour it, and forwarding would 400.
*
* 2. Chat and Responses live at DIFFERENT URLs (unlike providers that switch on a
* path suffix). The per-model `targetFormat` registry tag is the single source of
* truth for which surface a model uses — the same tag chatCore reads to translate
* the body — so resolving the URL from it keeps URL and payload in lockstep.
* Same pattern as executors/xai.ts (9router#2439).
*/
export class CheaperInferenceExecutor extends BaseExecutor {
constructor(provider = "cheaperinference") {
super(provider, PROVIDERS[provider]);
}
/**
* True when this model is served by the native /v1/responses endpoint.
*
* PROVIDER_MODELS is keyed by provider ALIAS ("cinf"), while PROVIDERS is keyed by
* provider ID ("cheaperinference") — so `this.provider` cannot be passed straight
* through the way executors/xai.ts does (there the alias equals the id, which hides
* the distinction). Resolve the alias first or every lookup silently returns null
* and every Responses request 400s upstream.
*/
private usesResponsesEndpoint(model: string): boolean {
const alias = PROVIDER_ID_TO_ALIAS[this.provider] || this.provider;
return getModelTargetFormat(alias, model) === "openai-responses";
}
buildUrl(model: string, _stream: boolean, _urlIndex = 0): string {
if (this.usesResponsesEndpoint(model)) {
return this.config.responsesBaseUrl || this.config.baseUrl;
}
return this.config.baseUrl;
}
transformRequest(
model: string,
body: unknown,
stream: boolean,
credentials: ProviderCredentials
): unknown {
const cleanedBody = super.transformRequest(model, body, stream, credentials);
if (!cleanedBody || typeof cleanedBody !== "object" || Array.isArray(cleanedBody)) {
return cleanedBody;
}
if (!this.usesResponsesEndpoint(model)) {
// Chat Completions rejects unknown params — never add `store` on that surface.
return cleanedBody;
}
return { ...(cleanedBody as Record<string, unknown>), store: false };
}
}
export default CheaperInferenceExecutor;