Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions lib/agents/generalAgent/__tests__/getGeneralAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { ChatRequestBody } from "@/lib/chat/validateChatRequest";
import { ToolLoopAgent, stepCountIs } from "ai";

// Import after mocks
import getGeneralAgent from "../getGeneralAgent";
import selectAccountEmails from "@/lib/supabase/account_emails/selectAccountEmails";
import { selectAccountInfo } from "@/lib/supabase/account_info/selectAccountInfo";
import { getAccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails";
import { getKnowledgeBaseText } from "@/lib/files/getKnowledgeBaseText";
import { setupToolsForRequest } from "@/lib/chat/setupToolsForRequest";
import { getSystemPrompt } from "@/lib/prompts/getSystemPrompt";
import { extractImageUrlsFromMessages } from "@/lib/messages/extractImageUrlsFromMessages";
import { buildSystemPromptWithImages } from "@/lib/chat/buildSystemPromptWithImages";

// Mock all external dependencies
vi.mock("@/lib/supabase/account_emails/selectAccountEmails", () => ({
default: vi.fn(),
Expand Down Expand Up @@ -35,17 +46,6 @@ vi.mock("@/lib/chat/buildSystemPromptWithImages", () => ({
buildSystemPromptWithImages: vi.fn(),
}));

// Import after mocks
import getGeneralAgent from "../getGeneralAgent";
import selectAccountEmails from "@/lib/supabase/account_emails/selectAccountEmails";
import { selectAccountInfo } from "@/lib/supabase/account_info/selectAccountInfo";
import { getAccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails";
import { getKnowledgeBaseText } from "@/lib/files/getKnowledgeBaseText";
import { setupToolsForRequest } from "@/lib/chat/setupToolsForRequest";
import { getSystemPrompt } from "@/lib/prompts/getSystemPrompt";
import { extractImageUrlsFromMessages } from "@/lib/messages/extractImageUrlsFromMessages";
import { buildSystemPromptWithImages } from "@/lib/chat/buildSystemPromptWithImages";

const mockSelectAccountEmails = vi.mocked(selectAccountEmails);
const mockSelectAccountInfo = vi.mocked(selectAccountInfo);
const mockGetAccountWithDetails = vi.mocked(getAccountWithDetails);
Expand Down Expand Up @@ -465,5 +465,52 @@ describe("getGeneralAgent", () => {
expect(result.stopWhen).toBeDefined();
expect(typeof result.stopWhen).toBe("function");
});

it("creates ToolLoopAgent with providerOptions for thinking/reasoning", async () => {
const body: ChatRequestBody = {
accountId: "account-123",
orgId: null,
messages: [{ id: "1", role: "user", content: "Hello" }],
};

const result = await getGeneralAgent(body);

// providerOptions should be baked into the agent constructor (stored in settings)
const settings = (result.agent as any).settings;
expect(settings.providerOptions).toBeDefined();
expect(settings.providerOptions.anthropic).toEqual(
expect.objectContaining({
thinking: { type: "enabled", budgetTokens: 12000 },
}),
);
expect(settings.providerOptions.google).toEqual(
expect.objectContaining({
thinkingConfig: expect.objectContaining({
thinkingBudget: 8192,
includeThoughts: true,
}),
}),
);
expect(settings.providerOptions.openai).toEqual(
expect.objectContaining({
reasoningEffort: "medium",
reasoningSummary: "detailed",
}),
);
});

it("creates ToolLoopAgent with prepareStep function", async () => {
const body: ChatRequestBody = {
accountId: "account-123",
orgId: null,
messages: [{ id: "1", role: "user", content: "Hello" }],
};

const result = await getGeneralAgent(body);

// prepareStep should be baked into the agent constructor (stored in settings)
const settings = (result.agent as any).settings;
expect(settings.prepareStep).toBeInstanceOf(Function);
});
});
});
24 changes: 24 additions & 0 deletions lib/agents/generalAgent/getGeneralAgent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { stepCountIs, ToolLoopAgent } from "ai";
import { AnthropicProviderOptions } from "@ai-sdk/anthropic";
import { GoogleGenerativeAIProviderOptions } from "@ai-sdk/google";
import { OpenAIResponsesProviderOptions } from "@ai-sdk/openai";
import { DEFAULT_MODEL } from "@/lib/const";
import { RoutingDecision } from "@/lib/chat/types";
import { extractImageUrlsFromMessages } from "@/lib/messages/extractImageUrlsFromMessages";
Expand All @@ -10,6 +13,7 @@ import selectAccountEmails from "@/lib/supabase/account_emails/selectAccountEmai
import { selectAccountInfo } from "@/lib/supabase/account_info/selectAccountInfo";
import { getKnowledgeBaseText } from "@/lib/files/getKnowledgeBaseText";
import { getAccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails";
import getPrepareStepResult from "@/lib/chat/toolChains/getPrepareStepResult";

/**
* Gets the general agent for the chat
Expand Down Expand Up @@ -55,6 +59,26 @@ export default async function getGeneralAgent(body: ChatRequestBody): Promise<Ro
instructions,
tools,
stopWhen,
prepareStep: options => {
const next = getPrepareStepResult(options);
if (next) return { ...options, ...next };
return options;
},
providerOptions: {
anthropic: {
thinking: { type: "enabled", budgetTokens: 12000 },
} satisfies AnthropicProviderOptions,
google: {
thinkingConfig: {
thinkingBudget: 8192,
includeThoughts: true,
},
} satisfies GoogleGenerativeAIProviderOptions,
openai: {
reasoningEffort: "medium",
reasoningSummary: "detailed",
} satisfies OpenAIResponsesProviderOptions,
},
});

return {
Expand Down
Loading