Files
openclaw/extensions/meta/stream.ts
Peter Steinberger 964c8c84c1 refactor: consolidate coercion ownership (#122299)
* refactor: consolidate coercion ownership

Centralize four canonical coercion helpers, migrate exact core and plugin duplicates through narrow Plugin SDK facades, and enforce declaration and plugin-normalization ownership boundaries.

The sweep adds eight focused SDK exports while deleting more production and tooling code than it adds. User-visible behavior is unchanged except for safer equivalent object and UI parsing at existing boundaries.

* fix: guard integer option ownership

Register resolveIntegerOption with the canonical function owner and extend the declaration-guard fixture so future local duplicates fail validation.

* fix: keep integer helpers on numeric facade

Remove the unshipped duplicate string-coerce exports and route every affected plugin consumer through the existing number-runtime contract.

* fix: point numeric coercion to number runtime

Make boundary and declaration diagnostics recommend the canonical numeric facade, with failing-before coverage for both guidance paths.
2026-08-11 17:14:53 -07:00

42 lines
1.7 KiB
TypeScript

// Meta plugin module implements stream behavior.
import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
import { createPayloadPatchStreamWrapper } from "openclaw/plugin-sdk/provider-stream-shared";
import { filterStringEntries } from "openclaw/plugin-sdk/string-coerce-runtime";
const META_REASONING_ENCRYPTED_CONTENT_INCLUDE = "reasoning.encrypted_content";
function ensureMetaResponsesReplayFields(payloadObj: Record<string, unknown>): void {
const existing = payloadObj.include;
const include = filterStringEntries(existing);
if (!include.includes(META_REASONING_ENCRYPTED_CONTENT_INCLUDE)) {
include.push(META_REASONING_ENCRYPTED_CONTENT_INCLUDE);
}
payloadObj.include = include;
payloadObj.store = false;
}
function createMetaResponsesWrapper(baseStreamFn: StreamFn | undefined): StreamFn {
return createPayloadPatchStreamWrapper(baseStreamFn, ({ payload, model, options }) => {
if (model.provider !== "meta") {
return;
}
// Responses treats zero as an unset caller cap. Restore the catalog limit
// without changing provider-selected behavior when the caller omits the field.
if (options?.maxTokens === 0 && payload.max_output_tokens === undefined) {
payload.max_output_tokens = model.maxTokens;
}
if (!model.reasoning) {
return;
}
ensureMetaResponsesReplayFields(payload);
});
}
export function wrapMetaProviderStream(ctx: ProviderWrapStreamFnContext): StreamFn | undefined {
if (ctx.provider !== "meta" || (ctx.sourceApi ?? ctx.model?.api) !== "openai-responses") {
return undefined;
}
return createMetaResponsesWrapper(ctx.streamFn);
}