Skip to content

Commit

Permalink
test(frontend): fix mock data
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 committed Sep 25, 2024
1 parent 6ef9b44 commit 0b54548
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 60 deletions.
18 changes: 0 additions & 18 deletions frontend/app/src/api/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { zodJsonDate } from '@/lib/zod';
import { parseStreamPart } from 'ai';
import { z, type ZodType } from 'zod';

const mockChat = !!process.env.NEXT_PUBLIC_MOCKING_CHAT;

type ClientEngineOptions = Omit<ChatEngineOptions, 'post_verification_token'>;

export interface Chat {
Expand Down Expand Up @@ -151,22 +149,6 @@ export async function getChatMessageSubgraph (chatMessageId: number): Promise<Kn
}

export async function* chat ({ chat_id, chat_engine, content, headers: headersInit, signal }: PostChatParams, onResponse?: (response: Response) => void) {
if (mockChat) {
const res = await fetch('/chats.mock.txt');
const text = await res.text();
for (let line of text.split('\n')) {
if (line.startsWith('0:')) {
await new Promise(resolve => setTimeout(resolve, 0));
} else {
await new Promise(resolve => setTimeout(resolve, 1000));
}
if (line) {
yield parseStreamPart(line + '\n');
}
}
return;
}

const headers = new Headers(headersInit);
headers.set('Content-Type', 'application/json');

Expand Down
67 changes: 28 additions & 39 deletions frontend/app/src/app/(main)/c/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import Link from 'next/link';
import { notFound } from 'next/navigation';
import { cache } from 'react';

const mockChat = !!process.env.NEXT_PUBLIC_MOCKING_CHAT;

const cachedGetChat = cache((id: string) => getChat(id)
.then(res => {
return res;
Expand All @@ -33,41 +31,37 @@ export default async function ChatDetailPage ({ params }: { params: { id: string
let chat: Chat | undefined;
let messages: ChatMessage[];

if (mockChat && id === '00000000-0000-0000-0000-00000000000') {
messages = [];
} else {
try {
const detail = await cachedGetChat(id);
chat = detail.chat;
messages = detail.messages;
} catch (error) {
if (isServerError(error, 403)) {
return (
<div className="h-screen flex items-center justify-center xl:pr-side bg-accent">
<ErrorCard
title="Access denied"
message="This chat is private"
>
<div className="flex gap-2 items-center mt-8">
{!me && (
<Button asChild>
<Link href="/auth/login">
Login to continue
</Link>
</Button>
)}
<Button variant="ghost" asChild>
<Link href="/">
Back to homepage
try {
const detail = await cachedGetChat(id);
chat = detail.chat;
messages = detail.messages;
} catch (error) {
if (isServerError(error, 403)) {
return (
<div className="h-screen flex items-center justify-center xl:pr-side bg-accent">
<ErrorCard
title="Access denied"
message="This chat is private"
>
<div className="flex gap-2 items-center mt-8">
{!me && (
<Button asChild>
<Link href="/auth/login">
Login to continue
</Link>
</Button>
</div>
</ErrorCard>
</div>
);
}
throw error;
)}
<Button variant="ghost" asChild>
<Link href="/">
Back to homepage
</Link>
</Button>
</div>
</ErrorCard>
</div>
);
}
throw error;
}

const shouldOpen = me
Expand All @@ -91,11 +85,6 @@ export default async function ChatDetailPage ({ params }: { params: { id: string
}

export async function generateMetadata ({ params }: { params: { id: string } }): Promise<Metadata> {
if (mockChat && params.id === '00000000-0000-0000-0000-00000000000') {
return {
title: 'Mocking chat... | tidb.ai',
};
}
try {
const chat = await cachedGetChat(params.id);

Expand Down
4 changes: 3 additions & 1 deletion frontend/app/src/components/chat/chat-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const exampleData = {
updated_at: new Date,
browser_id: null,
deleted_at: null,
origin: "None",
engine_options: {
llm: {
condense_question_prompt: '',
Expand Down Expand Up @@ -58,7 +59,7 @@ const exampleData = {
trace_url: '',
updated_at: new Date(),
user_id: '0',

post_verification_result_url: null,
},
user_message: {
chat_id: 'mock',
Expand All @@ -73,6 +74,7 @@ const exampleData = {
trace_url: '',
updated_at: new Date(),
user_id: '0',
post_verification_result_url: null,
},
} satisfies ChatInitialData;

Expand Down
2 changes: 1 addition & 1 deletion frontend/app/src/components/chat/chat-stream.state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ it('pass', () => {
'id': '019091e3-5cfc-74a3-b5e0-653a73f52af2',
'title': 'What is TiDB?',
'engine_id': 1,
'engine_options': '{"llm":{"provider":"openai","openai_chat_model":"gpt-4o","gemini_chat_model":"models/gemini-1.5-flash","reranker_provider":"jinaai","reranker_top_k":10,"intent_graph_knowledge":"Given a list of relationships of a knowledge graph as follows. When there is a conflict in meaning between knowledge relationships, the relationship with the higher `weight` and newer `last_modified_at` value takes precedence.\\n\\nKnowledge sub-queries:\\n\\n{% for sub_query, data in sub_queries.items() %}\\n\\nSub-query: {{ sub_query }}\\n\\n - Entities:\\n\\n{% for entity in data[\'entities\'] %}\\n\\n - Name: {{ entity.name }}\\n - Description: {{ entity.description }}\\n\\n{% endfor %}\\n\\n - Relationships:\\n\\n{% for relationship in data[\'relationships\'] %}\\n\\n - Description: {{ relationship.rag_description }}\\n - Last Modified At: {{ relationship.last_modified_at }}\\n - Meta: {{ relationship.meta | tojson(indent=2) }}\\n\\n{% endfor %}\\n\\n{% endfor %}\\n","normal_graph_knowledge":"Given a list of relationships of a knowledge graph as follows. When there is a conflict in meaning between knowledge relationships, the relationship with the higher `weight` and newer `last_modified_at` value takes precedence.\\n\\n---------------------\\nEntities:\\n\\n{% for entity in entities %}\\n\\n- Name: {{ entity.name }}\\n- Description: {{ entity.description }}\\n\\n{% endfor %}\\n\\n---------------------\\n\\nKnowledge relationships:\\n\\n{% for relationship in relationships %}\\n\\n- Description: {{ relationship.rag_description }}\\n- Weight: {{ relationship.weight }}\\n- Last Modified At: {{ relationship.last_modified_at }}\\n- Meta: {{ relationship.meta | tojson(indent=2) }}\\n\\n{% endfor %}\\n"},"knowledge_graph":{"enabled":true,"depth":2,"include_meta":true,"with_degree":false,"using_intent_search":true}}',
'engine_options': {"llm":{"provider":"openai","openai_chat_model":"gpt-4o","gemini_chat_model":"models/gemini-1.5-flash","reranker_provider":"jinaai","reranker_top_k":10,"intent_graph_knowledge":"Given a list of relationships of a knowledge graph as follows. When there is a conflict in meaning between knowledge relationships, the relationship with the higher `weight` and newer `last_modified_at` value takes precedence.\\n\\nKnowledge sub-queries:\\n\\n{% for sub_query, data in sub_queries.items() %}\\n\\nSub-query: {{ sub_query }}\\n\\n - Entities:\\n\\n{% for entity in data[\'entities\'] %}\\n\\n - Name: {{ entity.name }}\\n - Description: {{ entity.description }}\\n\\n{% endfor %}\\n\\n - Relationships:\\n\\n{% for relationship in data[\'relationships\'] %}\\n\\n - Description: {{ relationship.rag_description }}\\n - Last Modified At: {{ relationship.last_modified_at }}\\n - Meta: {{ relationship.meta | tojson(indent=2) }}\\n\\n{% endfor %}\\n\\n{% endfor %}\\n","normal_graph_knowledge":"Given a list of relationships of a knowledge graph as follows. When there is a conflict in meaning between knowledge relationships, the relationship with the higher `weight` and newer `last_modified_at` value takes precedence.\\n\\n---------------------\\nEntities:\\n\\n{% for entity in entities %}\\n\\n- Name: {{ entity.name }}\\n- Description: {{ entity.description }}\\n\\n{% endfor %}\\n\\n---------------------\\n\\nKnowledge relationships:\\n\\n{% for relationship in relationships %}\\n\\n- Description: {{ relationship.rag_description }}\\n- Weight: {{ relationship.weight }}\\n- Last Modified At: {{ relationship.last_modified_at }}\\n- Meta: {{ relationship.meta | tojson(indent=2) }}\\n\\n{% endfor %}\\n"},"knowledge_graph":{"enabled":true,"depth":2,"include_meta":true,"with_degree":false,"using_intent_search":true}},
'user_id': null,
'browser_id': null,
'created_at': '2024-07-08T10:28:39',
Expand Down
1 change: 0 additions & 1 deletion frontend/packages/widget-react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default defineConfig({
'process.env.BASE_URL': process.env.NODE_ENV === 'development' ? '"https://tidbai-dev.htapdb.com"' : '""',
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.NEXT_PUBLIC_DISABLE_DEBUG_PANEL': 'false',
'process.env.NEXT_PUBLIC_MOCKING_CHAT': 'false',
'process.env.NEXT_PUBLIC_IS_WIDGET': 'true',
'process.env.__NEXT_ROUTER_BASEPATH': '""',
},
Expand Down

0 comments on commit 0b54548

Please sign in to comment.