generated from template/vite-react-template
temp
This commit is contained in:
121
template/command/routes.ts
Normal file
121
template/command/routes.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import TurndownService from 'turndown';
|
||||
import { app, message } from '../app';
|
||||
|
||||
// 命令规则
|
||||
// 1. 命令以 ! 开头
|
||||
// 2. 命令和内容之间用空格隔开
|
||||
// 3. 多余的地方不要有!,如果有,使用\! 代替
|
||||
//
|
||||
//
|
||||
// test命令 !a 显示内容 !b 但是会计法 !c 飒短发 !fdsaf s !kong !d d!!的身份 ! 是的! !ene
|
||||
// 7个
|
||||
export function parseCommands(text: string) {
|
||||
//文本以\!的内容都去掉
|
||||
text = text.replace(/\\!/g, '__REPLACE__RETURN__');
|
||||
const result: { command: string; content: string }[] = [];
|
||||
const parts = text.split('!');
|
||||
|
||||
for (let i = 1; i < parts.length; i++) {
|
||||
const part = parts[i].trim();
|
||||
if (part.length === 0) continue; // 忽略空的部分
|
||||
|
||||
const spaceIndex = part.indexOf(' ');
|
||||
const command = '!' + (spaceIndex === -1 ? part : part.slice(0, spaceIndex));
|
||||
let content = spaceIndex === -1 ? '' : part.slice(spaceIndex + 1).trim();
|
||||
if (content.includes('__REPLACE__RETURN__')) {
|
||||
content = content.replace('__REPLACE__RETURN__', '!');
|
||||
}
|
||||
result.push({ command, content });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'command',
|
||||
key: 'handle',
|
||||
description: '处理命令',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { html } = ctx.query;
|
||||
// 解析 文本中的 !command 命令
|
||||
// 1. 当没有命令的时候是保存文本
|
||||
// 2. 当有命令的时候,查询命令,执行
|
||||
// - 当命令不存在,直接返回提示
|
||||
// - 当命令存在,执行命令
|
||||
const turndown = new TurndownService();
|
||||
const markdown = turndown.turndown(html);
|
||||
const commands = parseCommands(markdown);
|
||||
|
||||
if (commands.length === 0) {
|
||||
ctx.body = markdown;
|
||||
const res = await app.call({ path: 'note', key: 'save', payload: { html } });
|
||||
if (res.code !== 200) {
|
||||
message.error(res.message || '保存失败');
|
||||
ctx.throw(400, res.message || '保存失败');
|
||||
}
|
||||
return;
|
||||
}
|
||||
console.log('md', markdown);
|
||||
console.log('commands', commands, commands.length);
|
||||
const res = await app.call({ path: 'command', key: 'list', payload: { commands } });
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'command',
|
||||
key: 'list',
|
||||
description: '命令列表',
|
||||
metadata: {
|
||||
command: 'command-list',
|
||||
prompt: '把当前我的数据中,所有命令列表返回',
|
||||
},
|
||||
validator: {
|
||||
commands: {
|
||||
type: 'any',
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { commands } = ctx.query;
|
||||
const getRouteInfo = (route: any) => {
|
||||
return {
|
||||
path: route.path,
|
||||
key: route.key,
|
||||
description: route.description,
|
||||
metadata: route.metadata,
|
||||
validator: route.validator,
|
||||
};
|
||||
};
|
||||
if (Array.isArray(commands) && commands.length > 0) {
|
||||
const routes = ctx.queryRouter.routes;
|
||||
const commandRoutes = commands.map((command) => {
|
||||
const route = routes.find((route) => route.metadata?.command === command.command);
|
||||
if (!route) {
|
||||
message.error(`命令 ${command.command} 不存在`);
|
||||
ctx.throw(400, `命令 ${command.command} 不存在`);
|
||||
}
|
||||
return {
|
||||
command,
|
||||
route: getRouteInfo(route),
|
||||
};
|
||||
});
|
||||
ctx.body = commandRoutes;
|
||||
} else {
|
||||
ctx.body = ctx.queryRouter.routes
|
||||
.map((route) => ({
|
||||
command: route.metadata?.command,
|
||||
route: getRouteInfo(route),
|
||||
}))
|
||||
.filter((item) => item.command);
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
setTimeout(async () => {
|
||||
const res = await app.call({ path: 'command', key: 'list' });
|
||||
console.log('list', res.body);
|
||||
}, 2000);
|
||||
@@ -3,6 +3,7 @@ import '../src/routes';
|
||||
import './ai-app/main';
|
||||
import './tailwind.css';
|
||||
import './workspace/entry';
|
||||
import './routes';
|
||||
|
||||
page.addPage('/', 'workspace');
|
||||
|
||||
|
||||
2
template/routes.ts
Normal file
2
template/routes.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import './user/route';
|
||||
import './command/routes';
|
||||
@@ -1,6 +1,45 @@
|
||||
import { app } from '../app';
|
||||
import { app, message } from '../app';
|
||||
|
||||
app.route({
|
||||
path: 'user',
|
||||
key: 'login',
|
||||
});
|
||||
app
|
||||
.route({
|
||||
path: 'user',
|
||||
key: 'login',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { username, password } = ctx.query;
|
||||
if (!username || !password) {
|
||||
message.error('用户名和密码不能为空');
|
||||
ctx.throw(400, '用户名和密码不能为空');
|
||||
}
|
||||
const res = await fetch('/api/router', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ path: 'user', key: 'login', username, password }),
|
||||
}).then((res) => res.json());
|
||||
if (res.code === 200) {
|
||||
localStorage.setItem('token', res.data.token);
|
||||
} else {
|
||||
message.error(res.message);
|
||||
ctx.throw(400, res.message);
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'user',
|
||||
key: 'logout',
|
||||
description: '退出登录',
|
||||
metadata: {
|
||||
command: 'logout',
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
localStorage.removeItem('token');
|
||||
fetch('/api/router?path=user&key=logout', {
|
||||
method: 'POST',
|
||||
});
|
||||
setTimeout(() => {
|
||||
window.location.href = '/user/login';
|
||||
}, 1000);
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
36
template/workspace/prompts/html示例.md
Normal file
36
template/workspace/prompts/html示例.md
Normal file
@@ -0,0 +1,36 @@
|
||||
把当前我的数据中,所有的title和description和path和key列出来,生成一个好看的卡片式的列表。只给我返回html的内容,其他的东西不返回给我。
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"command": "logout",
|
||||
"route": {
|
||||
"path": "user",
|
||||
"key": "logout",
|
||||
"description": "退出登录",
|
||||
"metadata": {
|
||||
"command": "logout"
|
||||
},
|
||||
"validator": {}
|
||||
}
|
||||
},
|
||||
{
|
||||
"command": "command-list",
|
||||
"route": {
|
||||
"path": "command",
|
||||
"key": "list",
|
||||
"description": "命令列表",
|
||||
"metadata": {
|
||||
"command": "command-list",
|
||||
"prompt": "把当前我的数据中,所有命令列表返回"
|
||||
},
|
||||
"validator": {
|
||||
"commands": {
|
||||
"type": "any",
|
||||
"required": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
6
template/workspace/prompts/命令列表.md
Normal file
6
template/workspace/prompts/命令列表.md
Normal file
@@ -0,0 +1,6 @@
|
||||
我有一个命令列表,我需要通过查询去获取相应的列表的内容,我提供你查询的方式。我需要你把我文本的内容转为查询的参数的格式。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
7
template/workspace/prompts/提取指令.md
Normal file
7
template/workspace/prompts/提取指令.md
Normal file
@@ -0,0 +1,7 @@
|
||||
我有一些命令匹配的文本,格式是: !command text-content 他是很多类同的命令结合一起的,其中text-content可能为空,其中命令和内容都可能是乱拼的,只要符合 !command ,你就要把内容返回给我。其中如果!单独存在,或者!之前面有内容,都不属于命令,都属于上一个命令的文本,你需要排出这些错误情况。你需要把命令和文本的内容返回给我一个json数据。返回的格式是[{command,content],你只需要把你对应的内容返回给我,不要返回其他内容。
|
||||
|
||||
我给你的命令文本是
|
||||
|
||||
!a 显示内容 !b 但是会计法 !c 飒短发 !fdsaf s !d d!!的身份 ! 是的! !ene
|
||||
|
||||
PROMPT_TEXT
|
||||
0
template/workspace/提取指令.md
Normal file
0
template/workspace/提取指令.md
Normal file
Reference in New Issue
Block a user