feat: 集成 AI 对话与只读查询工具

This commit is contained in:
2026-07-23 14:24:40 +08:00
parent 302dbe0621
commit f3b59935d6
52 changed files with 4942 additions and 4 deletions

View File

@@ -0,0 +1,46 @@
import { DataSource } from 'typeorm';
import { AddAiChat1784780000000 } from '../migrations/1784780000000-AddAiChat';
describe('AddAiChat1784780000000', () => {
let dataSource: DataSource;
beforeEach(async () => {
dataSource = new DataSource({
type: 'better-sqlite3',
database: ':memory:',
migrations: [AddAiChat1784780000000],
});
await dataSource.initialize();
await dataSource.query(
'CREATE TABLE users (id integer PRIMARY KEY AUTOINCREMENT, username varchar(100) NOT NULL)',
);
});
afterEach(async () => {
if (dataSource.isInitialized) await dataSource.destroy();
});
it('创建会话、消息和工具记录表,并按会话级联删除', async () => {
await dataSource.runMigrations();
for (const table of ['ai_conversations', 'ai_messages', 'ai_tool_runs']) {
expect(await dataSource.createQueryRunner().hasTable(table)).toBe(true);
}
await dataSource.query("INSERT INTO users (username) VALUES ('tester')");
await dataSource.query(
"INSERT INTO ai_conversations (user_id, title) VALUES (1, '测试会话')",
);
await dataSource.query(
"INSERT INTO ai_messages (conversation_id, role, content) VALUES (1, 'assistant', '回答')",
);
await dataSource.query(
"INSERT INTO ai_tool_runs (message_id, tool_call_id, tool_name, status) VALUES (1, 'call_1', 'search_students', 'success')",
);
await dataSource.query('DELETE FROM ai_conversations WHERE id = 1');
expect(await dataSource.query('SELECT id FROM ai_messages')).toEqual([]);
expect(await dataSource.query('SELECT id FROM ai_tool_runs')).toEqual([]);
});
});