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
8 changes: 6 additions & 2 deletions __tests__/api/chat/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ vi.mock('@sentry/nextjs', () => ({
}));

vi.mock('@ai-sdk/google', () => ({
createGoogleGenerativeAI: () =>
// AI SDK v7 renamed the provider factory from createGoogleGenerativeAI to createGoogle.
createGoogle: () =>
Object.assign(
vi.fn(() => 'gemini-model'),
{
Expand All @@ -50,10 +51,13 @@ vi.mock('@ai-sdk/google', () => ({
),
}));

// AI SDK v7 returns the stream through createUIMessageStreamResponse({ stream: toUIMessageStream(...) })
// rather than result.toUIMessageStreamResponse(). Mock the surface the route actually calls.
vi.mock('ai', () => ({
convertToModelMessages: vi.fn((messages) => messages),
toUIMessageStream: vi.fn(() => 'ui-message-stream'),
createUIMessageStreamResponse: vi.fn(() => new Response('stream', { status: 200 })),
streamText: vi.fn(() => ({
toUIMessageStreamResponse: vi.fn(() => new Response('stream', { status: 200 })),
totalUsage: Promise.resolve({}),
request: Promise.resolve({ body: null }),
})),
Expand Down
67 changes: 66 additions & 1 deletion __tests__/lib/ai/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { describe, expect, it } from 'vitest';

import { extractDocumentIdFromFilename, extractRelevantSources } from '@/lib/ai/utils';
import {
extractDocumentIdFromFilename,
extractRelevantSources,
extractRequestMetadata,
} from '@/lib/ai/utils';

describe('ai/utils', () => {
describe('extractRelevantSources', () => {
Expand Down Expand Up @@ -97,4 +101,65 @@ describe('ai/utils', () => {
).toBe('550E8400-E29B-41D4-A716-446655440000');
});
});
describe('extractRequestMetadata', () => {
// AI SDK v7 hands back an already-parsed object. This is the shape the
// chat route actually receives, and `request.body` is typed `unknown`, so
// only a test can catch a regression here.
it('should read generationConfig and systemInstruction from an object body', () => {
expect(
extractRequestMetadata({
generationConfig: { temperature: 0.4, topP: 0.9 },
systemInstruction: { parts: [{ text: 'You are a helpful assistant.' }] },
contents: [{ role: 'user', parts: [{ text: 'hi' }] }],
})
).toEqual({
generationConfig: '{"temperature":0.4,"topP":0.9}',
systemPrompt: 'You are a helpful assistant.',
});
});

it('should still read a JSON string body (the pre-v7 shape)', () => {
expect(
extractRequestMetadata(
JSON.stringify({
generationConfig: { temperature: 0.2 },
systemInstruction: { parts: [{ text: 'Be concise.' }] },
})
)
).toEqual({
generationConfig: '{"temperature":0.2}',
systemPrompt: 'Be concise.',
});
});

it('should return nulls when the body carries neither field', () => {
expect(extractRequestMetadata({ contents: [] })).toEqual({
generationConfig: null,
systemPrompt: null,
});
});

it('should serialize an empty generationConfig rather than dropping it', () => {
// Google sends `generationConfig: {}` when no sampling settings are set.
expect(extractRequestMetadata({ generationConfig: {} })).toEqual({
generationConfig: '{}',
systemPrompt: null,
});
});

it('should return nulls for unusable bodies', () => {
expect(extractRequestMetadata(undefined)).toEqual({
generationConfig: null,
systemPrompt: null,
});
expect(extractRequestMetadata(null)).toEqual({
generationConfig: null,
systemPrompt: null,
});
expect(extractRequestMetadata('not json')).toEqual({
generationConfig: null,
systemPrompt: null,
});
});
});
});
2 changes: 1 addition & 1 deletion app/(logged-in)/org/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default async function OrgLayout({
params,
}: {
children: React.ReactNode;
params: { slug: string };
params: Promise<{ slug: string }>;
}) {
const { slug } = await params;
const caller = await createCaller();
Expand Down
Loading