refactor(cohere): flatten stream payload wrapper

This commit is contained in:
Amp
2026-08-12 15:20:42 +00:00
committed by Peter Steinberger
parent a85cd94dde
commit 22d437def9
3 changed files with 27 additions and 27 deletions
+9 -3
View File
@@ -7,7 +7,7 @@ import { describe, expect, it } from "vitest";
import plugin from "./index.js";
import manifest from "./openclaw.plugin.json" with { type: "json" };
import { COHERE_LIVE_MODEL_DISCOVERY } from "./provider-catalog.js";
import { createCohereCompletionsWrapper } from "./stream.js";
import { wrapCohereProviderStream } from "./stream.js";
const COHERE_COMMAND_A_PLUS_MODEL_ID = "command-a-plus-05-2026";
const COHERE_COMMAND_A_REASONING_MODEL_ID = "command-a-reasoning-08-2025";
@@ -50,11 +50,17 @@ function captureCoherePayload(
return {} as ReturnType<StreamFn>;
};
const wrappedStreamFn = createCohereCompletionsWrapper(baseStreamFn);
const model = requireCohereModel(settings?.modelId);
const wrappedStreamFn = wrapCohereProviderStream({
provider: "cohere",
modelId: model.id,
model,
streamFn: baseStreamFn,
});
if (!wrappedStreamFn) {
throw new Error("Cohere wrapper did not return a stream function");
}
void wrappedStreamFn(requireCohereModel(settings?.modelId), context, {
void wrappedStreamFn(model, context, {
onPayload: (payload) => {
captured = payload as Record<string, unknown>;
},
+3 -3
View File
@@ -3,7 +3,7 @@ import { isModernCohereModelId } from "./models.js";
import { applyCohereConfig } from "./onboard.js";
import manifest from "./openclaw.plugin.json" with { type: "json" };
import { COHERE_LIVE_MODEL_DISCOVERY } from "./provider-catalog.js";
import { createCohereCompletionsWrapper } from "./stream.js";
import { wrapCohereProviderStream } from "./stream.js";
export default defineSingleProviderPluginEntry({
id: "cohere",
@@ -17,8 +17,8 @@ export default defineSingleProviderPluginEntry({
catalog: {
liveModelDiscovery: COHERE_LIVE_MODEL_DISCOVERY,
},
wrapStreamFn: (ctx) => createCohereCompletionsWrapper(ctx.streamFn),
wrapSimpleCompletionStreamFn: (ctx) => createCohereCompletionsWrapper(ctx.streamFn),
wrapStreamFn: wrapCohereProviderStream,
wrapSimpleCompletionStreamFn: wrapCohereProviderStream,
isModernModelRef: ({ modelId }) => isModernCohereModelId(modelId),
},
});
+15 -21
View File
@@ -1,26 +1,20 @@
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
import { createPayloadPatchStreamWrapper } from "openclaw/plugin-sdk/provider-stream-shared";
function patchCoherePayload(payload: Record<string, unknown>): void {
// Cohere's Compatibility API uses developer, not system, for instructions.
if (Array.isArray(payload.messages)) {
payload.messages = payload.messages.map((message) =>
message &&
typeof message === "object" &&
(message as Record<string, unknown>).role === "system"
? { ...(message as Record<string, unknown>), role: "developer" }
: message,
);
}
export function wrapCohereProviderStream(ctx: ProviderWrapStreamFnContext) {
return createPayloadPatchStreamWrapper(ctx.streamFn, ({ payload }) => {
// Cohere's Compatibility API uses developer, not system, for instructions.
if (Array.isArray(payload.messages)) {
payload.messages = payload.messages.map((message) =>
message &&
typeof message === "object" &&
(message as Record<string, unknown>).role === "system"
? { ...(message as Record<string, unknown>), role: "developer" }
: message,
);
}
// Cohere lets tool-capable models choose a tool when tool_choice is omitted.
delete payload.tool_choice;
}
export function createCohereCompletionsWrapper(
baseStreamFn: ProviderWrapStreamFnContext["streamFn"],
): ProviderWrapStreamFnContext["streamFn"] {
return createPayloadPatchStreamWrapper(baseStreamFn, ({ payload }) =>
patchCoherePayload(payload),
);
// Cohere lets tool-capable models choose a tool when tool_choice is omitted.
delete payload.tool_choice;
});
}