Merge remote-tracking branch 'origin/main' into feat/integration-test
This commit is contained in:
@@ -27,14 +27,18 @@ export class OllamaEmbedder implements Embedder {
|
||||
} catch (err) {
|
||||
logger.error(`Error ensuring model exists: ${err}`);
|
||||
}
|
||||
// Ollama's Go server requires prompt to be a string. Coerce defensively
|
||||
// since callers may pass values parsed from untrusted LLM JSON output.
|
||||
const prompt = typeof text === "string" ? text : JSON.stringify(text);
|
||||
const response = await this.ollama.embeddings({
|
||||
// Coerce defensively since callers may pass values parsed from untrusted LLM JSON output.
|
||||
const input = typeof text === "string" ? text : JSON.stringify(text);
|
||||
const response = await this.ollama.embed({
|
||||
model: this.model,
|
||||
prompt,
|
||||
input,
|
||||
});
|
||||
return response.embedding;
|
||||
if (!response.embeddings || response.embeddings.length === 0) {
|
||||
throw new Error(
|
||||
`Ollama embed() returned no embeddings for model '${this.model}'`,
|
||||
);
|
||||
}
|
||||
return response.embeddings[0];
|
||||
}
|
||||
|
||||
async embedBatch(texts: string[]): Promise<number[][]> {
|
||||
@@ -42,12 +46,21 @@ export class OllamaEmbedder implements Embedder {
|
||||
return response;
|
||||
}
|
||||
|
||||
private static normalizeModelName(name: string): string {
|
||||
return name.includes(":") ? name : `${name}:latest`;
|
||||
}
|
||||
|
||||
private async ensureModelExists(): Promise<boolean> {
|
||||
if (this.initialized) {
|
||||
return true;
|
||||
}
|
||||
const local_models = await this.ollama.list();
|
||||
if (!local_models.models.find((m: any) => m.name === this.model)) {
|
||||
const target = OllamaEmbedder.normalizeModelName(this.model);
|
||||
if (
|
||||
!local_models.models.find(
|
||||
(m: any) => OllamaEmbedder.normalizeModelName(m.name) === target,
|
||||
)
|
||||
) {
|
||||
logger.info(`Pulling model ${this.model}...`);
|
||||
await this.ollama.pull({ model: this.model });
|
||||
}
|
||||
|
||||
@@ -16,25 +16,50 @@ export class GoogleLLM implements LLM {
|
||||
responseFormat?: { type: string },
|
||||
tools?: any[],
|
||||
): Promise<string | LLMResponse> {
|
||||
const completion = await this.google.models.generateContent({
|
||||
contents: messages.map((msg) => ({
|
||||
parts: [
|
||||
{
|
||||
text:
|
||||
typeof msg.content === "string"
|
||||
? msg.content
|
||||
: JSON.stringify(msg.content),
|
||||
},
|
||||
],
|
||||
role: msg.role === "system" ? "model" : "user",
|
||||
})),
|
||||
const contents = messages.map((msg) => ({
|
||||
parts: [
|
||||
{
|
||||
text:
|
||||
typeof msg.content === "string"
|
||||
? msg.content
|
||||
: JSON.stringify(msg.content),
|
||||
},
|
||||
],
|
||||
role: msg.role === "system" ? "model" : "user",
|
||||
}));
|
||||
|
||||
// Build config with tools if provided
|
||||
const config: Record<string, any> = {};
|
||||
if (tools && tools.length > 0) {
|
||||
config.tools = [
|
||||
{
|
||||
functionDeclarations: tools.map((tool) => ({
|
||||
name: tool.function.name,
|
||||
description: tool.function.description,
|
||||
parameters: tool.function.parameters,
|
||||
})),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
const completion = await this.google.models.generateContent({
|
||||
contents,
|
||||
model: this.model,
|
||||
// config: {
|
||||
// responseSchema: {}, // Add response schema if needed
|
||||
// },
|
||||
config,
|
||||
});
|
||||
|
||||
// Handle function call responses
|
||||
if (completion.functionCalls && completion.functionCalls.length > 0) {
|
||||
return {
|
||||
content: completion.text || "",
|
||||
role: "assistant",
|
||||
toolCalls: completion.functionCalls.map((call) => ({
|
||||
name: call.name!,
|
||||
arguments: JSON.stringify(call.args),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const text = completion.text
|
||||
?.replace(/^```json\n/, "")
|
||||
.replace(/\n```$/, "");
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
/// <reference types="jest" />
|
||||
/**
|
||||
* Google LLM — unit tests (mocked @google/genai).
|
||||
*
|
||||
* Regression tests for #4380: tools parameter was ignored, causing graph
|
||||
* memory operations to silently fail with Gemini models.
|
||||
*/
|
||||
|
||||
const mockGenerateContent = jest.fn();
|
||||
|
||||
jest.mock("@google/genai", () => ({
|
||||
GoogleGenAI: jest.fn().mockImplementation(() => ({
|
||||
models: { generateContent: mockGenerateContent },
|
||||
})),
|
||||
}));
|
||||
|
||||
import { GoogleLLM } from "../src/llms/google";
|
||||
|
||||
describe("GoogleLLM (unit)", () => {
|
||||
beforeEach(() => mockGenerateContent.mockClear());
|
||||
|
||||
it("returns text response when no tools are provided", async () => {
|
||||
mockGenerateContent.mockResolvedValueOnce({
|
||||
text: '{"facts": ["fact1"]}',
|
||||
functionCalls: null,
|
||||
});
|
||||
|
||||
const llm = new GoogleLLM({ apiKey: "test-key" });
|
||||
const result = await llm.generateResponse([
|
||||
{ role: "user", content: "Hello" },
|
||||
]);
|
||||
|
||||
expect(mockGenerateContent).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe('{"facts": ["fact1"]}');
|
||||
|
||||
// Verify tools are not in config
|
||||
const callArgs = mockGenerateContent.mock.calls[0][0];
|
||||
expect(callArgs.config.tools).toBeUndefined();
|
||||
});
|
||||
|
||||
it("forwards tools as functionDeclarations to Gemini API", async () => {
|
||||
mockGenerateContent.mockResolvedValueOnce({
|
||||
text: "",
|
||||
functionCalls: [
|
||||
{
|
||||
name: "extract_entities",
|
||||
args: { entities: [{ entity: "Alice", entity_type: "person" }] },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const tools = [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "extract_entities",
|
||||
description: "Extract entities from text",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
entities: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
entity: { type: "string" },
|
||||
entity_type: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ["entities"],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const llm = new GoogleLLM({ apiKey: "test-key" });
|
||||
const result = await llm.generateResponse(
|
||||
[{ role: "user", content: "Alice is a person" }],
|
||||
undefined,
|
||||
tools,
|
||||
);
|
||||
|
||||
// Verify functionDeclarations were passed in config
|
||||
const callArgs = mockGenerateContent.mock.calls[0][0];
|
||||
expect(callArgs.config.tools).toBeDefined();
|
||||
expect(callArgs.config.tools[0].functionDeclarations).toHaveLength(1);
|
||||
expect(callArgs.config.tools[0].functionDeclarations[0].name).toBe(
|
||||
"extract_entities",
|
||||
);
|
||||
|
||||
// Verify toolCalls in response
|
||||
expect(result).toHaveProperty("toolCalls");
|
||||
const response = result as { toolCalls: any[] };
|
||||
expect(response.toolCalls).toHaveLength(1);
|
||||
expect(response.toolCalls[0].name).toBe("extract_entities");
|
||||
expect(JSON.parse(response.toolCalls[0].arguments)).toEqual({
|
||||
entities: [{ entity: "Alice", entity_type: "person" }],
|
||||
});
|
||||
});
|
||||
|
||||
it("returns text when tools are provided but model returns text", async () => {
|
||||
mockGenerateContent.mockResolvedValueOnce({
|
||||
text: "Just a text response",
|
||||
functionCalls: null,
|
||||
});
|
||||
|
||||
const tools = [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "noop",
|
||||
description: "No operation",
|
||||
parameters: { type: "object", properties: {} },
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const llm = new GoogleLLM({ apiKey: "test-key" });
|
||||
const result = await llm.generateResponse(
|
||||
[{ role: "user", content: "Hello" }],
|
||||
undefined,
|
||||
tools,
|
||||
);
|
||||
|
||||
// Should return text, not toolCalls
|
||||
expect(result).toBe("Just a text response");
|
||||
});
|
||||
|
||||
it("strips markdown code fences from text responses", async () => {
|
||||
mockGenerateContent.mockResolvedValueOnce({
|
||||
text: '```json\n{"facts": ["fact1"]}\n```',
|
||||
functionCalls: null,
|
||||
});
|
||||
|
||||
const llm = new GoogleLLM({ apiKey: "test-key" });
|
||||
const result = await llm.generateResponse([
|
||||
{ role: "user", content: "Extract facts" },
|
||||
]);
|
||||
|
||||
expect(result).toBe('{"facts": ["fact1"]}');
|
||||
});
|
||||
|
||||
it("handles multiple function calls in response", async () => {
|
||||
mockGenerateContent.mockResolvedValueOnce({
|
||||
text: "",
|
||||
functionCalls: [
|
||||
{
|
||||
name: "add_graph_memory",
|
||||
args: { source: "Alice", destination: "Bob", relationship: "knows" },
|
||||
},
|
||||
{
|
||||
name: "add_graph_memory",
|
||||
args: {
|
||||
source: "Bob",
|
||||
destination: "Charlie",
|
||||
relationship: "works_with",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const tools = [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "add_graph_memory",
|
||||
description: "Add a graph memory",
|
||||
parameters: { type: "object", properties: {} },
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const llm = new GoogleLLM({ apiKey: "test-key" });
|
||||
const result = await llm.generateResponse(
|
||||
[{ role: "user", content: "Alice knows Bob, Bob works with Charlie" }],
|
||||
undefined,
|
||||
tools,
|
||||
);
|
||||
|
||||
const response = result as { toolCalls: any[] };
|
||||
expect(response.toolCalls).toHaveLength(2);
|
||||
expect(response.toolCalls[0].name).toBe("add_graph_memory");
|
||||
expect(response.toolCalls[1].name).toBe("add_graph_memory");
|
||||
});
|
||||
});
|
||||
@@ -58,7 +58,11 @@ function createMemory(overrides: Partial<MemoryConfig> = {}): Memory {
|
||||
},
|
||||
vectorStore: {
|
||||
provider: "memory",
|
||||
config: { collectionName: `test-add-${Date.now()}`, dimension: 1536 },
|
||||
config: {
|
||||
collectionName: `test-add-${Date.now()}`,
|
||||
dimension: 1536,
|
||||
dbPath: ":memory:",
|
||||
},
|
||||
},
|
||||
llm: {
|
||||
provider: "openai",
|
||||
|
||||
@@ -61,6 +61,7 @@ function createMemory(): Memory {
|
||||
config: {
|
||||
collectionName: `test-crud-${Date.now()}-${Math.random()}`,
|
||||
dimension: 1536,
|
||||
dbPath: ":memory:",
|
||||
},
|
||||
},
|
||||
llm: {
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/// <reference types="jest" />
|
||||
/**
|
||||
* Ollama Embedder — unit tests (mocked Ollama client).
|
||||
*/
|
||||
|
||||
import { OllamaEmbedder } from "../src/embeddings/ollama";
|
||||
|
||||
const mockEmbedding = [0.1, 0.2, 0.3, 0.4, 0.5];
|
||||
const mockEmbed = jest.fn().mockResolvedValue({
|
||||
model: "nomic-embed-text:latest",
|
||||
embeddings: [mockEmbedding],
|
||||
});
|
||||
const mockList = jest.fn().mockResolvedValue({
|
||||
models: [{ name: "nomic-embed-text:latest" }],
|
||||
});
|
||||
const mockPull = jest.fn().mockResolvedValue({});
|
||||
|
||||
jest.mock("ollama", () => ({
|
||||
Ollama: jest.fn().mockImplementation(() => ({
|
||||
embed: mockEmbed,
|
||||
list: mockList,
|
||||
pull: mockPull,
|
||||
})),
|
||||
}));
|
||||
|
||||
describe("OllamaEmbedder (unit)", () => {
|
||||
beforeEach(() => {
|
||||
mockEmbed.mockClear();
|
||||
mockList.mockClear();
|
||||
mockPull.mockClear();
|
||||
});
|
||||
|
||||
it("embed() calls ollama.embed with model and input, returns first embedding", async () => {
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text:latest",
|
||||
});
|
||||
|
||||
const result = await embedder.embed("Sample text to embed.");
|
||||
|
||||
expect(mockEmbed).toHaveBeenCalledTimes(1);
|
||||
expect(mockEmbed.mock.calls[0][0]).toEqual({
|
||||
model: "nomic-embed-text:latest",
|
||||
input: "Sample text to embed.",
|
||||
});
|
||||
expect(result).toEqual(mockEmbedding);
|
||||
});
|
||||
|
||||
it("embed() coerces non-string input to JSON string", async () => {
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text:latest",
|
||||
});
|
||||
|
||||
// Force a non-string through the type boundary
|
||||
await embedder.embed(42 as any);
|
||||
|
||||
expect(mockEmbed.mock.calls[0][0].input).toBe("42");
|
||||
});
|
||||
|
||||
it("embedBatch() returns vectors for multiple inputs", async () => {
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text:latest",
|
||||
});
|
||||
|
||||
const result = await embedder.embedBatch(["text1", "text2"]);
|
||||
|
||||
expect(mockEmbed).toHaveBeenCalledTimes(2);
|
||||
expect(result).toEqual([mockEmbedding, mockEmbedding]);
|
||||
});
|
||||
|
||||
it("ensureModelExists() does not pull when model is already present", async () => {
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text:latest",
|
||||
});
|
||||
|
||||
await embedder.embed("trigger ensureModelExists");
|
||||
|
||||
expect(mockList).toHaveBeenCalled();
|
||||
expect(mockPull).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("ensureModelExists() pulls model when not found locally", async () => {
|
||||
mockList.mockResolvedValueOnce({ models: [] });
|
||||
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text:latest",
|
||||
});
|
||||
|
||||
await embedder.embed("trigger ensureModelExists");
|
||||
|
||||
expect(mockPull).toHaveBeenCalledWith({ model: "nomic-embed-text:latest" });
|
||||
});
|
||||
|
||||
it("ensureModelExists() normalizes model name with :latest tag", async () => {
|
||||
mockList.mockResolvedValue({
|
||||
models: [{ name: "nomic-embed-text:latest" }],
|
||||
});
|
||||
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text",
|
||||
});
|
||||
|
||||
await embedder.embed("trigger ensureModelExists");
|
||||
|
||||
expect(mockPull).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("embed() throws when embeddings array is empty", async () => {
|
||||
mockEmbed.mockResolvedValueOnce({
|
||||
model: "nomic-embed-text:latest",
|
||||
embeddings: [],
|
||||
});
|
||||
|
||||
const embedder = new OllamaEmbedder({
|
||||
model: "nomic-embed-text:latest",
|
||||
});
|
||||
|
||||
await expect(embedder.embed("text")).rejects.toThrow(
|
||||
"Ollama embed() returned no embeddings",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
/**
|
||||
* Drift-prevention test: ensures every peerDependency in package.json
|
||||
* is listed in tsup.config.ts's external array so tsup never bundles
|
||||
* optional provider SDKs into the dist output.
|
||||
*/
|
||||
describe("tsup.config.ts externals", () => {
|
||||
let peerDeps: string[];
|
||||
let directDeps: string[];
|
||||
let externalDeps: string[];
|
||||
|
||||
beforeAll(() => {
|
||||
const pkgPath = path.resolve(__dirname, "../../../package.json");
|
||||
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
||||
// Filter out @types/* packages — they are type-only and not bundled at runtime
|
||||
peerDeps = Object.keys(pkg.peerDependencies || {}).filter(
|
||||
(dep) => !dep.startsWith("@types/"),
|
||||
);
|
||||
directDeps = Object.keys(pkg.dependencies || {});
|
||||
|
||||
const tsupConfigPath = path.resolve(__dirname, "../../../tsup.config.ts");
|
||||
const tsupContent = fs.readFileSync(tsupConfigPath, "utf-8");
|
||||
|
||||
// Extract strings from the external array (supports double, single, and backtick quotes)
|
||||
const externalMatch = tsupContent.match(
|
||||
/const external\s*=\s*\[([\s\S]*?)\];/,
|
||||
);
|
||||
if (!externalMatch) {
|
||||
throw new Error("Could not find external array in tsup.config.ts");
|
||||
}
|
||||
const matches = externalMatch[1].match(/["'`]([^"'`]+)["'`]/g);
|
||||
externalDeps = (matches || []).map((m) => m.replace(/["'`]/g, ""));
|
||||
});
|
||||
|
||||
it("should have every peerDependency in the external array", () => {
|
||||
const missing = peerDeps.filter((dep) => !externalDeps.includes(dep));
|
||||
expect(missing).toEqual([]);
|
||||
});
|
||||
|
||||
it("should not have stale entries that are not in package.json", () => {
|
||||
const allDeps = [...peerDeps, ...directDeps];
|
||||
const stale = externalDeps.filter((dep) => !allDeps.includes(dep));
|
||||
expect(stale).toEqual([]);
|
||||
});
|
||||
|
||||
it("should have peerDependencies defined in package.json", () => {
|
||||
expect(peerDeps.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user