diff --git a/packages/tools/src/vercel/index.ts b/packages/tools/src/vercel/index.ts index 54bc5dcf..e87f31b6 100644 --- a/packages/tools/src/vercel/index.ts +++ b/packages/tools/src/vercel/index.ts @@ -216,7 +216,12 @@ const wrapVercelLanguageModel = ( const wrappedModel = new Proxy(model, { get(target, prop, _receiver) { - if (typeof prop === "string" && prop in overrides) { + // Use hasOwnProperty to avoid matching inherited Object.prototype props + // (e.g. 'constructor', 'toString') which would shadow them on the proxy. + if ( + typeof prop === "string" && + Object.prototype.hasOwnProperty.call(overrides, prop) + ) { return overrides[prop] } // Delegate to the original model so that `this` inside prototype diff --git a/packages/tools/test/with-supermemory/unit.test.ts b/packages/tools/test/with-supermemory/unit.test.ts index 4a7695f4..f625a95b 100644 --- a/packages/tools/test/with-supermemory/unit.test.ts +++ b/packages/tools/test/with-supermemory/unit.test.ts @@ -216,6 +216,24 @@ describe("Unit: withSupermemory", () => { }) }) + describe("Proxy override correctness", () => { + it("should not shadow Object.prototype inherited props (e.g. constructor) with undefined overrides", () => { + process.env.SUPERMEMORY_API_KEY = "test-key" + + // 'constructor', 'toString', 'hasOwnProperty' etc are in Object.prototype. + // Using `prop in overrides` (instead of hasOwnProperty) would match these + // inherited props and return undefined from the overrides map, shadowing + // the real implementations on the model/target. + const mockModel = createMockV3LanguageModel() + const wrappedModel = withSupermemory(mockModel, TEST_CONFIG.containerTag) + + // constructor should point to the model's class, not Object + expect((wrappedModel as object).constructor).toBe(MockLanguageModelV3) + // toString should be callable and not undefined + expect(typeof (wrappedModel as object).toString).toBe("function") + }) + }) + describe("Memory caching", () => { let fetchMock: ReturnType