diff --git a/package.json b/package.json index 96167d2..8dafee3 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ ], "license": "ISC", "dependencies": { - "@abearxiong/auth": "1.0.0-alpha.5", + "@abearxiong/auth": "1.0.1", "@abearxiong/router": "0.0.1-alpha.38", "@abearxiong/use-config": "^0.0.2", "@babel/core": "^7.25.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 63a8d45..2287eef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,8 +14,8 @@ importers: .: dependencies: '@abearxiong/auth': - specifier: 1.0.0-alpha.5 - version: 1.0.0-alpha.5(@abearxiong/router@0.0.1-alpha.38) + specifier: 1.0.1 + version: 1.0.1(@abearxiong/router@0.0.1-alpha.38) '@abearxiong/router': specifier: 0.0.1-alpha.38 version: 0.0.1-alpha.38 @@ -222,8 +222,8 @@ importers: packages: - '@abearxiong/auth@1.0.0-alpha.5': - resolution: {integrity: sha512-/DPlwvWN0zLQ7X3D/zAhtHRTVWP/Odn3lZeyllUzjn8PFV9E5pCVXbLV9fBqmP400icZkG9a3fplSbUrVIIpOA==, tarball: https://npm.pkg.github.com/download/@abearxiong/auth/1.0.0-alpha.5/9d149a4e10bde7fd51b102b00849d75da85dd282} + '@abearxiong/auth@1.0.1': + resolution: {integrity: sha512-KWolCGZorF/h+IyBToQyom/7iNUYN1V4ZsVSb8saAhnkQZ52CVT0P/8zBBq6WvbU8ZlJGCFk5mm7Jtaq9AE5kw==, tarball: https://npm.pkg.github.com/download/@abearxiong/auth/1.0.1/41c1a8845ad5fe34168bfbc209c8cb6e242de590} peerDependencies: '@abearxiong/router': ^0.0.1-alpha.38 @@ -3371,7 +3371,7 @@ packages: snapshots: - '@abearxiong/auth@1.0.0-alpha.5(@abearxiong/router@0.0.1-alpha.38)': + '@abearxiong/auth@1.0.1(@abearxiong/router@0.0.1-alpha.38)': dependencies: '@abearxiong/router': 0.0.1-alpha.38 diff --git a/src/app.ts b/src/app.ts index cb94dc1..6d4602a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -22,6 +22,15 @@ export const app = new App<{ import: any; emit: typeof emit }>({ import: dynamicImport, emit, }, + // routerHandle(res) { + // console.log('routerHandle', res.query); + // const { code, data, message } = res; + // return { + // code, + // data, + // message, + // }; + // }, }); const clients = []; diff --git a/src/models/chat-history.ts b/src/models/chat-history.ts new file mode 100644 index 0000000..8d40fd0 --- /dev/null +++ b/src/models/chat-history.ts @@ -0,0 +1,58 @@ +import { chat } from '@/modules/ollama.ts'; +import { sequelize } from '../modules/sequelize.ts'; +import { DataTypes, Model } from 'sequelize'; + +/** + * chat 回话记录 + * 有一些内容是预置的。 + */ +export class ChatHistory extends Model { + declare id: string; + declare data: string; + declare root: boolean; + declare show: boolean; + declare uid: string; +} + +ChatHistory.init( + { + id: { + type: DataTypes.UUID, + primaryKey: true, + defaultValue: DataTypes.UUIDV4, + }, + data: { + type: DataTypes.JSON, + allowNull: true, + }, + chatId: { + type: DataTypes.UUID, // 历史属于哪一条会话 + allowNull: true, + }, + chatPromptId: { + type: DataTypes.UUID, // 属于哪一个prompt + allowNull: true, + }, + root: { + type: DataTypes.BOOLEAN, // 是否是根节点 + defaultValue: false, + }, + show: { + type: DataTypes.BOOLEAN, // 当创建返回的时候,配置是否显示 + defaultValue: true, + }, + uid: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize, // 传入 Sequelize 实例 + modelName: 'chat_history', // 模型名称 + }, +); + +// force 只能run一次,否则会删除表 +ChatHistory.sync({ alter: true, force: true, logging: false }).catch((e) => { + console.error('History sync error', e); +}); diff --git a/src/models/chat-prompt.ts b/src/models/chat-prompt.ts new file mode 100644 index 0000000..d2fd59a --- /dev/null +++ b/src/models/chat-prompt.ts @@ -0,0 +1,61 @@ +import { sequelize } from '../modules/sequelize.ts'; +import { DataTypes, Model } from 'sequelize'; +import { Variable } from '@kevisual/ai-graph'; + +export type ChatPromptData = { + // 使用那个agent, 必须要有 + aiAgentId: string; + // 使用那个初始化的prompt,如果不存在则纯粹的白对话。 + promptId?: string; +}; +/** + * chat绑定就的agent和prompt + * 有一些内容是预置的。 + */ +export class ChatPrompt extends Model { + declare id: string; + declare title: string; + declare description: string; + declare uid: string; + declare key: string; + declare data: string; +} + +ChatPrompt.init( + { + id: { + type: DataTypes.UUID, + primaryKey: true, + defaultValue: DataTypes.UUIDV4, + }, + title: { + type: DataTypes.STRING, + allowNull: false, + }, + description: { + type: DataTypes.TEXT, + allowNull: true, + }, + data: { + type: DataTypes.JSON, + allowNull: true, + }, + key: { + type: DataTypes.STRING, // 页面属于 /container/edit/list + allowNull: false, + }, + uid: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize, // 传入 Sequelize 实例 + modelName: 'chat_prompt', // 模型名称 + }, +); + +// force 只能run一次,否则会删除表 +ChatPrompt.sync({ alter: true, force: true, logging: false }).catch((e) => { + console.error('Prompt sync error', e); +}); diff --git a/src/routes/container/list.ts b/src/routes/container/list.ts index 398ef43..f78ef84 100644 --- a/src/routes/container/list.ts +++ b/src/routes/container/list.ts @@ -5,10 +5,16 @@ import semver from 'semver'; const list = app.route({ path: 'container', key: 'list', + middleware: ['auth'] }); + list.run = async (ctx) => { + const tokenUser = ctx.state.tokenUser; const list = await ContainerModel.findAll({ order: [['updatedAt', 'DESC']], + where: { + uid: tokenUser.id, + }, }); ctx.body = list; return ctx; @@ -42,10 +48,7 @@ add.run = async (ctx) => { title: '', description: '', code: '', - source: '', type: '', - sourceType: '', - data: {}, }; const container = { ..._data, @@ -67,6 +70,8 @@ add.run = async (ctx) => { try { containerModel = await ContainerModel.create({ ...container, + source: '', + sourceType: '', }); } catch (e) { console.log('error', e); diff --git a/src/routes/page/list.ts b/src/routes/page/list.ts index 5df2bda..6c76cf7 100644 --- a/src/routes/page/list.ts +++ b/src/routes/page/list.ts @@ -29,10 +29,15 @@ app .route({ path: 'page', key: 'list', + middleware: ['auth'], }) .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser; ctx.body = await PageModel.findAll({ order: [['updatedAt', 'DESC']], + where: { + uid: tokenUser.id, + }, }); return ctx; }) diff --git a/src/routes/resource/list.ts b/src/routes/resource/list.ts index 9f7cb63..94fe215 100644 --- a/src/routes/resource/list.ts +++ b/src/routes/resource/list.ts @@ -6,10 +6,15 @@ app .route({ path: 'resource', key: 'list', + middleware: ['auth'], }) .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser; const list = await ResourceModel.findAll({ order: [['updatedAt', 'DESC']], + where: { + uid: tokenUser.id, + }, }); ctx.body = list; return ctx;