2026-01-14 01:08:15 +00:00
|
|
|
import type { StreamFn } from "@mariozechner/pi-agent-core";
|
2026-01-31 16:06:39 +09:00
|
|
|
import type { SimpleStreamOptions } from "@mariozechner/pi-ai";
|
2026-01-14 01:08:15 +00:00
|
|
|
import { streamSimple } from "@mariozechner/pi-ai";
|
2026-01-30 03:15:10 +01:00
|
|
|
import type { OpenClawConfig } from "../../config/config.js";
|
2026-01-14 01:08:15 +00:00
|
|
|
import { log } from "./logger.js";
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-16 22:25:51 +00:00
|
|
|
* Resolve provider-specific extra params from model config.
|
|
|
|
|
* Used to pass through stream params like temperature/maxTokens.
|
2026-01-14 01:08:15 +00:00
|
|
|
*
|
|
|
|
|
* @internal Exported for testing only
|
|
|
|
|
*/
|
|
|
|
|
export function resolveExtraParams(params: {
|
2026-01-30 03:15:10 +01:00
|
|
|
cfg: OpenClawConfig | undefined;
|
2026-01-14 01:08:15 +00:00
|
|
|
provider: string;
|
|
|
|
|
modelId: string;
|
|
|
|
|
}): Record<string, unknown> | undefined {
|
|
|
|
|
const modelKey = `${params.provider}/${params.modelId}`;
|
|
|
|
|
const modelConfig = params.cfg?.agents?.defaults?.models?.[modelKey];
|
2026-01-16 22:25:51 +00:00
|
|
|
return modelConfig?.params ? { ...modelConfig.params } : undefined;
|
2026-01-14 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-01 09:50:52 +01:00
|
|
|
type CacheRetention = "none" | "short" | "long";
|
2026-01-20 15:07:46 +00:00
|
|
|
|
2026-02-01 09:50:52 +01:00
|
|
|
/**
|
|
|
|
|
* Resolve cacheRetention from extraParams, supporting both new `cacheRetention`
|
|
|
|
|
* and legacy `cacheControlTtl` values for backwards compatibility.
|
|
|
|
|
*
|
|
|
|
|
* Mapping: "5m" → "short", "1h" → "long"
|
|
|
|
|
*
|
|
|
|
|
* Only applies to Anthropic provider (OpenRouter uses openai-completions API
|
|
|
|
|
* with hardcoded cache_control, not the cacheRetention stream option).
|
|
|
|
|
*/
|
|
|
|
|
function resolveCacheRetention(
|
2026-01-20 15:07:46 +00:00
|
|
|
extraParams: Record<string, unknown> | undefined,
|
|
|
|
|
provider: string,
|
2026-02-01 09:50:52 +01:00
|
|
|
): CacheRetention | undefined {
|
|
|
|
|
if (provider !== "anthropic") {
|
2026-01-31 16:19:20 +09:00
|
|
|
return undefined;
|
|
|
|
|
}
|
2026-02-01 09:50:52 +01:00
|
|
|
|
|
|
|
|
// Prefer new cacheRetention if present
|
|
|
|
|
const newVal = extraParams?.cacheRetention;
|
|
|
|
|
if (newVal === "none" || newVal === "short" || newVal === "long") {
|
|
|
|
|
return newVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to legacy cacheControlTtl with mapping
|
|
|
|
|
const legacy = extraParams?.cacheControlTtl;
|
|
|
|
|
if (legacy === "5m") {
|
|
|
|
|
return "short";
|
2026-01-31 16:19:20 +09:00
|
|
|
}
|
2026-02-01 09:50:52 +01:00
|
|
|
if (legacy === "1h") {
|
|
|
|
|
return "long";
|
2026-01-31 16:19:20 +09:00
|
|
|
}
|
2026-01-20 15:07:46 +00:00
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 01:08:15 +00:00
|
|
|
function createStreamFnWithExtraParams(
|
|
|
|
|
baseStreamFn: StreamFn | undefined,
|
|
|
|
|
extraParams: Record<string, unknown> | undefined,
|
2026-01-20 15:07:46 +00:00
|
|
|
provider: string,
|
2026-01-14 01:08:15 +00:00
|
|
|
): StreamFn | undefined {
|
|
|
|
|
if (!extraParams || Object.keys(extraParams).length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-01 09:50:52 +01:00
|
|
|
const streamParams: Partial<SimpleStreamOptions> = {};
|
2026-01-14 01:08:15 +00:00
|
|
|
if (typeof extraParams.temperature === "number") {
|
|
|
|
|
streamParams.temperature = extraParams.temperature;
|
|
|
|
|
}
|
|
|
|
|
if (typeof extraParams.maxTokens === "number") {
|
|
|
|
|
streamParams.maxTokens = extraParams.maxTokens;
|
|
|
|
|
}
|
2026-02-01 09:50:52 +01:00
|
|
|
const cacheRetention = resolveCacheRetention(extraParams, provider);
|
|
|
|
|
if (cacheRetention) {
|
|
|
|
|
streamParams.cacheRetention = cacheRetention;
|
2026-01-20 15:07:46 +00:00
|
|
|
}
|
2026-01-14 01:08:15 +00:00
|
|
|
|
|
|
|
|
if (Object.keys(streamParams).length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 14:31:43 +00:00
|
|
|
log.debug(`creating streamFn wrapper with params: ${JSON.stringify(streamParams)}`);
|
2026-01-14 01:08:15 +00:00
|
|
|
|
|
|
|
|
const underlying = baseStreamFn ?? streamSimple;
|
|
|
|
|
const wrappedStreamFn: StreamFn = (model, context, options) =>
|
2026-01-31 16:03:28 +09:00
|
|
|
underlying(model, context, {
|
2026-01-14 01:08:15 +00:00
|
|
|
...streamParams,
|
|
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return wrappedStreamFn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Apply extra params (like temperature) to an agent's streamFn.
|
|
|
|
|
*
|
|
|
|
|
* @internal Exported for testing
|
|
|
|
|
*/
|
|
|
|
|
export function applyExtraParamsToAgent(
|
|
|
|
|
agent: { streamFn?: StreamFn },
|
2026-01-30 03:15:10 +01:00
|
|
|
cfg: OpenClawConfig | undefined,
|
2026-01-14 01:08:15 +00:00
|
|
|
provider: string,
|
|
|
|
|
modelId: string,
|
2026-01-20 07:35:29 +00:00
|
|
|
extraParamsOverride?: Record<string, unknown>,
|
2026-01-14 01:08:15 +00:00
|
|
|
): void {
|
|
|
|
|
const extraParams = resolveExtraParams({
|
|
|
|
|
cfg,
|
|
|
|
|
provider,
|
|
|
|
|
modelId,
|
|
|
|
|
});
|
2026-01-20 07:35:29 +00:00
|
|
|
const override =
|
|
|
|
|
extraParamsOverride && Object.keys(extraParamsOverride).length > 0
|
|
|
|
|
? Object.fromEntries(
|
|
|
|
|
Object.entries(extraParamsOverride).filter(([, value]) => value !== undefined),
|
|
|
|
|
)
|
|
|
|
|
: undefined;
|
|
|
|
|
const merged = Object.assign({}, extraParams, override);
|
2026-02-01 09:50:52 +01:00
|
|
|
const wrappedStreamFn = createStreamFnWithExtraParams(agent.streamFn, merged, provider);
|
2026-01-14 01:08:15 +00:00
|
|
|
|
|
|
|
|
if (wrappedStreamFn) {
|
2026-01-14 14:31:43 +00:00
|
|
|
log.debug(`applying extraParams to agent streamFn for ${provider}/${modelId}`);
|
2026-01-14 01:08:15 +00:00
|
|
|
agent.streamFn = wrappedStreamFn;
|
|
|
|
|
}
|
|
|
|
|
}
|