fix(cohere): translate system prompts · openclaw/openclaw@4132ce1
vincentkoc
·
2026-06-17
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -32,11 +32,14 @@ function captureCoherePayload(context: Context): Record<string, unknown> {
|
32 | 32 | { maxTokens: 2048 } as never, |
33 | 33 | ); |
34 | 34 | options?.onPayload?.(payload, model); |
35 | | -captured = payload; |
36 | 35 | return {} as ReturnType<StreamFn>; |
37 | 36 | }; |
38 | 37 | |
39 | | -void createCohereCompletionsWrapper(baseStreamFn)(requireCohereModel(), context, {}); |
| 38 | +void createCohereCompletionsWrapper(baseStreamFn)(requireCohereModel(), context, { |
| 39 | +onPayload: (payload) => { |
| 40 | +captured = payload as Record<string, unknown>; |
| 41 | +}, |
| 42 | +}); |
40 | 43 | if (!captured) { |
41 | 44 | throw new Error("Cohere payload was not captured"); |
42 | 45 | } |
@@ -104,5 +107,11 @@ describe("Cohere provider plugin", () => {
|
104 | 107 | expect(params).not.toHaveProperty("store"); |
105 | 108 | expect(params).not.toHaveProperty("stream_options"); |
106 | 109 | expect(params).not.toHaveProperty("tool_choice"); |
| 110 | +expect(params.messages).toEqual( |
| 111 | +expect.arrayContaining([expect.objectContaining({ role: "developer", content: "system" })]), |
| 112 | +); |
| 113 | +expect(params.messages).not.toEqual( |
| 114 | +expect.arrayContaining([expect.objectContaining({ role: "system", content: "system" })]), |
| 115 | +); |
107 | 116 | }); |
108 | 117 | }); |
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry"; |
2 | 2 | import { createPayloadPatchStreamWrapper } from "openclaw/plugin-sdk/provider-stream-shared"; |
3 | 3 | |
| 4 | +function patchCoherePayload(payload: Record<string, unknown>): void { |
| 5 | +// Cohere's Compatibility API uses developer, not system, for instructions. |
| 6 | +if (Array.isArray(payload.messages)) { |
| 7 | +payload.messages = payload.messages.map((message) => |
| 8 | +message && |
| 9 | +typeof message === "object" && |
| 10 | +(message as Record<string, unknown>).role === "system" |
| 11 | + ? { ...(message as Record<string, unknown>), role: "developer" } |
| 12 | + : message, |
| 13 | +); |
| 14 | +} |
| 15 | + |
| 16 | +// Cohere lets tool-capable models choose a tool when tool_choice is omitted. |
| 17 | +delete payload.tool_choice; |
| 18 | +} |
| 19 | + |
4 | 20 | export function createCohereCompletionsWrapper( |
5 | 21 | baseStreamFn: ProviderWrapStreamFnContext["streamFn"], |
6 | 22 | ): ProviderWrapStreamFnContext["streamFn"] { |
7 | | -return createPayloadPatchStreamWrapper( |
8 | | -baseStreamFn, |
9 | | -({ payload }) => { |
10 | | -// Cohere lets tool-capable models choose a tool when tool_choice is omitted. |
11 | | -delete payload.tool_choice; |
12 | | -}, |
13 | | -{ |
14 | | -shouldPatch: ({ model }) => model.provider === "cohere" && model.api === "openai-completions", |
15 | | -}, |
| 23 | +return createPayloadPatchStreamWrapper(baseStreamFn, ({ payload }) => |
| 24 | +patchCoherePayload(payload), |
16 | 25 | ); |
17 | 26 | } |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。