Skip to content

Commit

Permalink
refactor: improve code organization and fix voice input bug
Browse files Browse the repository at this point in the history
This commit refactors the frontend chat components to improve code organization and readability. It also fixes a bug in the voice input functionality where the transcript was not being properly cleared when stopping voice input.
  • Loading branch information
Sma1lboy committed Oct 26, 2024
1 parent d90bf48 commit 72209ba
Show file tree
Hide file tree
Showing 10 changed files with 10,366 additions and 8,102 deletions.
4 changes: 4 additions & 0 deletions .vscode/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
12 changes: 12 additions & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import 'reflect-metadata';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: '*',
credentials: true,
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Accept',
'Authorization',
'Access-Control-Allow-Origin',
'Access-Control-Allow-Credentials',
],
});
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@hookform/resolvers": "^3.9.0",
"@langchain/community": "^0.3.1",
"@langchain/core": "^0.3.3",
"@nestjs/common": "^10.4.6",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/HomeContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ export default function HomeContent() {

if (chunk.choices[0]?.finish_reason === 'stop') {
setLoadingSubmit(false);
// 保存到本地存储
localStorage.setItem(`chat_${chatId}`, JSON.stringify(messages));
window.dispatchEvent(new Event('storage'));
}
Expand Down
19 changes: 18 additions & 1 deletion frontend/src/lib/model-helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { ModelTags } from '@/graphql/type';
import client from '@/utils/client';
import { gql } from '@apollo/client';

export function getSelectedModel(): string {
if (typeof window !== 'undefined') {
client
.query<ModelTags>({
query: gql`
query {
modelTags {
tags
}
}
`,
})
.then((result) => {
console.log(result.data.tags);
});
const storedModel = localStorage.getItem('selectedModel');
return storedModel || 'gemma:2b';
} else {
// Default model
return 'gemma:2b';
return 'default';
}
}
36 changes: 34 additions & 2 deletions frontend/src/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,33 @@ import {
HttpLink,
ApolloLink,
concat,
from,
} from '@apollo/client';
import { onError } from '@apollo/client/link/error';

const httpLink = new HttpLink({
uri: process.env.NEXT_PUBLIC_GRAPHQL_URL,
headers: {
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin': '*',
},
});
const requestLoggingMiddleware = new ApolloLink((operation, forward) => {
console.log('GraphQL Request:', {
operationName: operation.operationName,
variables: operation.variables,
query: operation.query.loc?.source.body,
});

return forward(operation).map((response) => {
console.log('GraphQL Response:', response.data);
return response;
});
});
const authMiddleware = new ApolloLink((operation, forward) => {
// Get the authentication token from local storage if it exists
if (typeof window === 'undefined') {
return forward(operation);
}
const token = localStorage.getItem('token');
// Use the setContext method to set the HTTP headers.
if (token) {
Expand All @@ -23,8 +42,21 @@ const authMiddleware = new ApolloLink((operation, forward) => {
}
return forward(operation);
});
const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path }) => {
console.error(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
);
});
}
if (networkError) {
console.error(`[Network error]: ${networkError}`);
}
});

const client = new ApolloClient({
link: concat(authMiddleware, httpLink),
link: from([errorLink, requestLoggingMiddleware, authMiddleware, httpLink]),
cache: new InMemoryCache(),
});

Expand Down
1 change: 0 additions & 1 deletion frontend/src/utils/requests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApolloClient, gql, TypedDocumentNode } from '@apollo/client';
import type { DocumentNode } from 'graphql';

// 定义具体的查询类型
export type GetUserProjectsQuery = { __typename?: 'Query' } & {
getUserProjects: Array<
{ __typename?: 'Project' } & {
Expand Down
1 change: 1 addition & 0 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"sourceMap": true,
"incremental": true,
"plugins": [
{
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"packageManager": "[email protected]",
"engines": {
"node": ">=18"
},
"dependencies": {
"typescript": "5.6.2"
}
}
Loading

0 comments on commit 72209ba

Please sign in to comment.