summaryrefslogtreecommitdiffhomepage
path: root/packages/provider-umans/src/reasoning.ts
blob: 9015848ccd6e9ffde66b7d5c88eecea41ac81acd (plain)
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
import type { ProviderStreamOptions, ReasoningEffort } from "@dispatch/kernel";

/**
 * Map a resolved `ReasoningEffort` to Umans' `reasoning_effort` wire value.
 *
 * Umans' OpenAI route accepts `"none"|"low"|"medium"|"high"`; Dispatch's
 * `ReasoningEffort` adds `"xhigh"|"max"`, which Umans caps to `"high"`. An
 * absent effort (`undefined`) maps to `undefined` so the caller emits no
 * `reasoning_effort` field at all — byte-stable when the caller has no
 * preference.
 *
 * Pure: the `transformBody` decision, factored out for direct unit testing.
 */
export function mapReasoningEffort(
	effort: ReasoningEffort | undefined,
): "low" | "medium" | "high" | undefined {
	if (effort === undefined) return undefined;
	if (effort === "xhigh" || effort === "max") return "high";
	return effort;
}

/**
 * Provider-specific body transform handed to `@dispatch/openai-stream`'s
 * `createOpenAICompatProvider`. Returns the extra fields the library merges
 * into the chat-completions body before send. Adds `reasoning_effort` only when
 * a resolved effort is present; returns `{}` (no fields) otherwise —
 * byte-stable for calls with no reasoning preference.
 */
export function transformBody(
	_body: Record<string, unknown>,
	opts: ProviderStreamOptions,
): Record<string, unknown> {
	const mapped = mapReasoningEffort(opts.reasoningEffort);
	if (mapped === undefined) return {};
	return { reasoning_effort: mapped };
}