Skip to content

Commit 40836e9

Browse files
committed
fix: fix the itemId stripping logic, this time it should fix that id issue w/ gpt models fr
1 parent 9a48f8e commit 40836e9

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

packages/opencode/src/provider/transform.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@ export namespace ProviderTransform {
2424
// Strip openai itemId metadata following what codex does
2525
if (model.api.npm === "@ai-sdk/openai" || options.store === false) {
2626
msgs = msgs.map((msg) => {
27-
if (msg.providerOptions?.openai) {
28-
delete msg.providerOptions.openai["itemId"]
27+
if (msg.providerOptions) {
28+
for (const options of Object.values(msg.providerOptions)) {
29+
if (options && typeof options === "object") {
30+
delete options["itemId"]
31+
}
32+
}
2933
}
3034
if (!Array.isArray(msg.content)) {
3135
return msg
3236
}
3337
const content = msg.content.map((part) => {
34-
if (part.providerOptions?.openai) {
35-
delete part.providerOptions.openai["itemId"]
38+
if (part.providerOptions) {
39+
for (const options of Object.values(part.providerOptions)) {
40+
if (options && typeof options === "object") {
41+
delete options["itemId"]
42+
}
43+
}
3644
}
3745
return part
3846
})

packages/opencode/test/provider/transform.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,82 @@ describe("ProviderTransform.message - strip openai metadata when store=false", (
805805
expect(result[0].content[0].providerOptions?.openai?.itemId).toBeUndefined()
806806
})
807807

808+
test("strips metadata using providerID key when store is false", () => {
809+
const opencodeModel = {
810+
...openaiModel,
811+
providerID: "opencode",
812+
api: {
813+
id: "opencode-test",
814+
url: "https://api.opencode.ai",
815+
npm: "@ai-sdk/openai-compatible",
816+
},
817+
}
818+
const msgs = [
819+
{
820+
role: "assistant",
821+
content: [
822+
{
823+
type: "text",
824+
text: "Hello",
825+
providerOptions: {
826+
opencode: {
827+
itemId: "msg_123",
828+
otherOption: "value",
829+
},
830+
},
831+
},
832+
],
833+
},
834+
] as any[]
835+
836+
const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
837+
838+
expect(result[0].content[0].providerOptions?.opencode?.itemId).toBeUndefined()
839+
expect(result[0].content[0].providerOptions?.opencode?.otherOption).toBe("value")
840+
})
841+
842+
test("strips itemId across all providerOptions keys", () => {
843+
const opencodeModel = {
844+
...openaiModel,
845+
providerID: "opencode",
846+
api: {
847+
id: "opencode-test",
848+
url: "https://api.opencode.ai",
849+
npm: "@ai-sdk/openai-compatible",
850+
},
851+
}
852+
const msgs = [
853+
{
854+
role: "assistant",
855+
providerOptions: {
856+
openai: { itemId: "msg_root" },
857+
opencode: { itemId: "msg_opencode" },
858+
extra: { itemId: "msg_extra" },
859+
},
860+
content: [
861+
{
862+
type: "text",
863+
text: "Hello",
864+
providerOptions: {
865+
openai: { itemId: "msg_openai_part" },
866+
opencode: { itemId: "msg_opencode_part" },
867+
extra: { itemId: "msg_extra_part" },
868+
},
869+
},
870+
],
871+
},
872+
] as any[]
873+
874+
const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
875+
876+
expect(result[0].providerOptions?.openai?.itemId).toBeUndefined()
877+
expect(result[0].providerOptions?.opencode?.itemId).toBeUndefined()
878+
expect(result[0].providerOptions?.extra?.itemId).toBeUndefined()
879+
expect(result[0].content[0].providerOptions?.openai?.itemId).toBeUndefined()
880+
expect(result[0].content[0].providerOptions?.opencode?.itemId).toBeUndefined()
881+
expect(result[0].content[0].providerOptions?.extra?.itemId).toBeUndefined()
882+
})
883+
808884
test("does not strip metadata for non-openai packages when store is not false", () => {
809885
const anthropicModel = {
810886
...openaiModel,

0 commit comments

Comments
 (0)