Selaa lähdekoodia

连接数据库

李富豪 1 vuosi sitten
vanhempi
commit
785402c747
5 muutettua tiedostoa jossa 159 lisäystä ja 89 poistoa
  1. 75 0
      app/api/bigModel/route.ts
  2. 0 29
      app/api/getData/route.ts
  3. 0 58
      app/api/insertData/route.ts
  4. 14 1
      app/client/platforms/bigmodel.ts
  5. 70 1
      yarn.lock

+ 75 - 0
app/api/bigModel/route.ts

@@ -0,0 +1,75 @@
+import { NextResponse } from 'next/server';
+import mysql from 'mysql2/promise';
+
+// 创建全局的 MySQL 连接池
+const pool = mysql.createPool({
+  connectionLimit: 10,
+  host: '192.168.3.89', // 服务器地址
+  user: 'root',
+  password: '123456', // 密码
+  database: 'chat',
+})
+
+interface Request {
+  id: string;
+  messages: {
+    id: string;
+    date: string;
+    role: "system" | "user" | "assistant";
+    content: string;
+  }[];
+}
+
+export async function insertData(req: Request) {
+  try {
+    // 从连接池中获取连接
+    const connection = await pool.getConnection()
+    const id = req.id;
+    const messages = req.messages;
+
+    const [rows, fields] = await connection.query('SELECT * FROM dialog where id = ?', [id]);
+
+    if (!rows) {
+      // 执行 MySQL 添加头表
+      await connection.execute('INSERT INTO dialog (id, create_time) VALUES (?,?)', [req, new Date])
+    }
+
+    messages.forEach(async function (message) {
+      const detailId = message.id;
+      const role = message.role;
+      const createDate = message.date;
+      const content = message.content;
+      console.log(message);
+      const [detailRows, fields] = await connection.query('SELECT * FROM dialog_detail where id = ?', [detailId]);
+      if (!detailRows) {
+        // 执行 MySQL 添加头表
+        await connection.execute('INSERT INTO dialog_detail (id,dialog_id,type,content,create_by, create_time) VALUES (?, ?, ?)', [detailId, id, role, content, role, createDate])
+      }
+    });
+
+    // 释放连接回连接池
+    connection.release()
+    return NextResponse.json({ message: 'ok' }, { status: 200 })
+  } catch (error) {
+    console.error('Error:', error)
+    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
+  }
+}
+
+export async function getAllData() {
+  try {
+    // 从连接池中获取连接
+    const connection = await pool.getConnection()
+
+    // 执行 MySQL 查询
+    const [rows, fields] = await connection.query('SELECT he.id,he.dialog_name,de.id did,de.dialog_id,de.type,de.content FROM dialog he left join dialog_detail de on he.id = de.dialog_id')
+
+    // 释放连接回连接池
+    connection.release()
+
+    return NextResponse.json({ data: rows }, { status: 200 })
+  } catch (error) {
+    console.error('Error:', error)
+    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
+  }
+}

+ 0 - 29
app/api/getData/route.ts

@@ -1,29 +0,0 @@
-import { NextResponse } from 'next/server'
-import mysql from 'mysql2/promise'
- 
-// 创建全局的 MySQL 连接池
-const pool = mysql.createPool({
-  connectionLimit: 10,
-  host: '192.168.3.89', // 服务器地址
-  user: 'root',
-  password: '123456', // 密码
-  database: 'chat',
-})
- 
-export async function GET(request:any) {
-  try {
-    // 从连接池中获取连接
-    const connection = await pool.getConnection()
- 
-    // 执行 MySQL 查询
-    const [rows, fields] = await connection.query('SELECT he.id,he.dialog_name,de.id did,de.dialog_id,de.type,de.content FROM dialog he left join dialog_detail de on he.id = de.dialog_id')
- 
-    // 释放连接回连接池
-    connection.release()
- 
-    return NextResponse.json({ data: rows }, { status: 200 })
-  } catch (error) {
-    console.error('Error:', error)
-    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
-  }
-}

+ 0 - 58
app/api/insertData/route.ts

@@ -1,58 +0,0 @@
-import { NextResponse } from 'next/server'
-import mysql from 'mysql2/promise'
- 
-// 创建全局的 MySQL 连接池
-const pool = mysql.createPool({
-  connectionLimit: 10,
-  host: '192.168.3.89', // 服务器地址
-  user: 'root',
-  password: '123456', // 密码
-  database: 'chat',
-})
-
-interface Request{
-    id: string;
-    messages: {
-        id: string;
-        date: string;
-        role: "system" | "user" | "assistant";
-        content: string;
-    }[];
-}
- 
-export async function InsertData(req:Request,res:any) {
-  try {
-    // 从连接池中获取连接
-    const connection = await pool.getConnection()
-    const id = req.id;
-    const messages = req.messages;
-    
-    const [rows, fields] = await connection.query('SELECT * FROM dialog where id = ?',[id]);
-
-    if(!rows) {
-      // 执行 MySQL 添加头表
-      await connection.execute('INSERT INTO dialog (id, create_time) VALUES (?,?)',[req, new Date])
-    }
-
-    messages.forEach(async function(message) {
-      const detailId = message.id;
-      const role = message.role;
-      const createDate = message.date;
-      const content = message.content;
-      console.log(message);
-      const [detailRows, fields] = await connection.query('SELECT * FROM dialog_detail where id = ?',[detailId]);
-      if(!detailRows) {
-        // 执行 MySQL 添加头表
-        await connection.execute('INSERT INTO dialog_detail (id,dialog_id,type,content,create_by, create_time) VALUES (?, ?, ?)',[detailId,id,role,content,role,createDate])
-      }
-    });
- 
-    // 释放连接回连接池
-    connection.release()
- 
-    return res.status(201).json({ message: 'Data inserted successfully' });
-  } catch (error) {
-    console.error('Error:', error)
-    return res.status(500).json({ error: 'Internal Server Error' });
-  }
-}

+ 14 - 1
app/client/platforms/bigmodel.ts

@@ -14,6 +14,7 @@ import {
 import { prettyObject } from "@/app/utils/format";
 import { getMessageTextContent } from "@/app/utils";
 import { bigModelApiKey } from "../config";
+import { insertData } from "@/app/api/bigModel/route";
 
 export class BigModelApi implements LLMApi {
   path(): string {
@@ -84,7 +85,6 @@ export class BigModelApi implements LLMApi {
         function animateResponseText() {
           if (finished || controller.signal.aborted) {
             responseText += remainText;
-            console.log("[Response Animation] finished");
             if (responseText?.length === 0) {
               options.onError?.(new Error("empty response from server"));
             }
@@ -110,6 +110,19 @@ export class BigModelApi implements LLMApi {
             finished = true;
             options.onFinish(responseText + remainText);
           }
+          const session = useChatStore.getState().sessions[0];
+          const data = {
+            id: session.id,
+            messages: session.messages.map(item => {
+              return {
+                id: item.id,
+                date: item.date,
+                role: item.role,
+                content: item.content,
+              }
+            })
+          }
+          insertData(data as any)
         };
 
         controller.signal.onabort = finish;

+ 70 - 1
yarn.lock

@@ -1832,6 +1832,11 @@ anymatch@~3.1.2:
     normalize-path "^3.0.0"
     picomatch "^2.0.4"
 
+aws-ssl-profiles@^1.1.1:
+  version "1.1.1"
+  resolved "https://registry.npmmirror.com/aws-ssl-profiles/-/aws-ssl-profiles-1.1.1.tgz#21ef8ad77d753927f6c01b144c5ef4cc4f150cdc"
+  integrity sha512-+H+kuK34PfMaI9PNU/NSjBKL5hh/KDM9J72kwYeYEm0A8B1AC4fuCy3qsjnA7lxklgyXsB68yn8Z2xoZEjgwCQ==
+
 babel-plugin-polyfill-corejs2@^0.4.10:
   version "0.4.11"
   resolved "https://registry.npmmirror.com/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.4.11.tgz#30320dfe3ffe1a336c15afdcdafd6fd615b25e33"
@@ -2495,6 +2500,11 @@ delaunator@5:
   dependencies:
     robust-predicates "^3.0.2"
 
+denque@^2.1.0:
+  version "2.1.0"
+  resolved "https://registry.npmmirror.com/denque/-/denque-2.1.0.tgz#e93e1a6569fb5e66f16a3c2a2964617d349d6ab1"
+  integrity sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==
+
 dequal@^2.0.0:
   version "2.0.3"
   resolved "https://registry.npmmirror.com/dequal/-/dequal-2.0.3.tgz#2644214f1997d39ed0ee0ece72335490a7ac67be"
@@ -2778,6 +2788,13 @@ fuse.js@^7.0.0:
   resolved "https://registry.npmmirror.com/fuse.js/-/fuse.js-7.0.0.tgz#6573c9fcd4c8268e403b4fc7d7131ffcf99a9eb2"
   integrity sha512-14F4hBIxqKvD4Zz/XjDc3y94mNZN6pRv3U13Udo0lNLCWRBUsrMv2xwcF/y/Z5sV6+FQW+/ow68cHpm4sunt8Q==
 
+generate-function@^2.3.1:
+  version "2.3.1"
+  resolved "https://registry.npmmirror.com/generate-function/-/generate-function-2.3.1.tgz#f069617690c10c868e73b8465746764f97c3479f"
+  integrity sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==
+  dependencies:
+    is-property "^1.0.2"
+
 gensync@^1.0.0-beta.2:
   version "1.0.0-beta.2"
   resolved "https://registry.npmmirror.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
@@ -2954,7 +2971,7 @@ husky@^8.0.0:
   resolved "https://registry.npmmirror.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184"
   integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==
 
-iconv-lite@0.6:
+iconv-lite@0.6, iconv-lite@^0.6.3:
   version "0.6.3"
   resolved "https://registry.npmmirror.com/iconv-lite/-/iconv-lite-0.6.3.tgz#a52f80bf38da1952eb5c681790719871a1a72501"
   integrity sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==
@@ -3045,6 +3062,11 @@ is-plain-obj@^4.0.0:
   resolved "https://registry.npmmirror.com/is-plain-obj/-/is-plain-obj-4.1.0.tgz#d65025edec3657ce032fd7db63c97883eaed71f0"
   integrity sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==
 
+is-property@^1.0.2:
+  version "1.0.2"
+  resolved "https://registry.npmmirror.com/is-property/-/is-property-1.0.2.tgz#57fe1c4e48474edd65b09911f26b1cd4095dda84"
+  integrity sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==
+
 is-stream@^3.0.0:
   version "3.0.0"
   resolved "https://registry.npmmirror.com/is-stream/-/is-stream-3.0.0.tgz#e6bfd7aa6bef69f4f472ce9bb681e3e57b4319ac"
@@ -3185,6 +3207,11 @@ log-update@^5.0.1:
     strip-ansi "^7.0.1"
     wrap-ansi "^8.0.1"
 
+long@^5.2.1:
+  version "5.2.3"
+  resolved "https://registry.npmmirror.com/long/-/long-5.2.3.tgz#a3ba97f3877cf1d778eccbcb048525ebb77499e1"
+  integrity sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q==
+
 longest-streak@^3.0.0:
   version "3.1.0"
   resolved "https://registry.npmmirror.com/longest-streak/-/longest-streak-3.1.0.tgz#62fa67cd958742a1574af9f39866364102d90cd4"
@@ -3213,6 +3240,16 @@ lru-cache@^5.1.1:
   dependencies:
     yallist "^3.0.2"
 
+lru-cache@^7.14.1:
+  version "7.18.3"
+  resolved "https://registry.npmmirror.com/lru-cache/-/lru-cache-7.18.3.tgz#f793896e0fd0e954a59dfdd82f0773808df6aa89"
+  integrity sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==
+
+lru-cache@^8.0.0:
+  version "8.0.5"
+  resolved "https://registry.npmmirror.com/lru-cache/-/lru-cache-8.0.5.tgz#983fe337f3e176667f8e567cfcce7cb064ea214e"
+  integrity sha512-MhWWlVnuab1RG5/zMRRcVGXZLCXrZTgfwMikgzCegsPnG62yDQo5JnqKkrK4jO5iKqDAZGItAqN5CtKBCBWRUA==
+
 markdown-table@^3.0.0:
   version "3.0.3"
   resolved "https://registry.npmmirror.com/markdown-table/-/markdown-table-3.0.3.tgz#e6331d30e493127e031dd385488b5bd326e4a6bd"
@@ -3750,6 +3787,28 @@ ms@2.1.2:
   resolved "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
   integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
 
+mysql2@^3.11.0:
+  version "3.11.0"
+  resolved "https://registry.npmmirror.com/mysql2/-/mysql2-3.11.0.tgz#2a7bd7c615ab43f8167ed9922063b968f3e48f33"
+  integrity sha512-J9phbsXGvTOcRVPR95YedzVSxJecpW5A5+cQ57rhHIFXteTP10HCs+VBjS7DHIKfEaI1zQ5tlVrquCd64A6YvA==
+  dependencies:
+    aws-ssl-profiles "^1.1.1"
+    denque "^2.1.0"
+    generate-function "^2.3.1"
+    iconv-lite "^0.6.3"
+    long "^5.2.1"
+    lru-cache "^8.0.0"
+    named-placeholders "^1.1.3"
+    seq-queue "^0.0.5"
+    sqlstring "^2.3.2"
+
+named-placeholders@^1.1.3:
+  version "1.1.3"
+  resolved "https://registry.npmmirror.com/named-placeholders/-/named-placeholders-1.1.3.tgz#df595799a36654da55dda6152ba7a137ad1d9351"
+  integrity sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==
+  dependencies:
+    lru-cache "^7.14.1"
+
 nanoid@^3.3.6:
   version "3.3.7"
   resolved "https://registry.npmmirror.com/nanoid/-/nanoid-3.3.7.tgz#d0c301a691bc8d54efa0a2226ccf3fe2fd656bd8"
@@ -4251,6 +4310,11 @@ semver@^6.3.1:
   resolved "https://registry.npmmirror.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
   integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==
 
+seq-queue@^0.0.5:
+  version "0.0.5"
+  resolved "https://registry.npmmirror.com/seq-queue/-/seq-queue-0.0.5.tgz#d56812e1c017a6e4e7c3e3a37a1da6d78dd3c93e"
+  integrity sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==
+
 serialize-javascript@^6.0.1:
   version "6.0.2"
   resolved "https://registry.npmmirror.com/serialize-javascript/-/serialize-javascript-6.0.2.tgz#defa1e055c83bf6d59ea805d8da862254eb6a6c2"
@@ -4321,6 +4385,11 @@ spawn-command@0.0.2:
   resolved "https://registry.npmmirror.com/spawn-command/-/spawn-command-0.0.2.tgz#9544e1a43ca045f8531aac1a48cb29bdae62338e"
   integrity sha512-zC8zGoGkmc8J9ndvml8Xksr1Amk9qBujgbF0JAIWO7kXr43w0h/0GJNM/Vustixu+YE8N/MTrQ7N31FvHUACxQ==
 
+sqlstring@^2.3.2:
+  version "2.3.3"
+  resolved "https://registry.npmmirror.com/sqlstring/-/sqlstring-2.3.3.tgz#2ddc21f03bce2c387ed60680e739922c65751d0c"
+  integrity sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==
+
 stable@^0.1.8:
   version "0.1.8"
   resolved "https://registry.npmmirror.com/stable/-/stable-0.1.8.tgz#836eb3c8382fe2936feaf544631017ce7d47a3cf"