104 Commits

Author SHA1 Message Date
267eecbf45 refactor(ai-web): 使用输入节点代替inputSchema
将输入表单放在流程图中一起定义,方便统一参数的处理,不需要单独为输入表单的变量进行合并和操作
2025-07-20 19:27:09 +08:00
a5282762ed refractor(web): 增加输入节点 2025-07-20 17:17:22 +08:00
v-zhangjc9
77a09472aa feat(ai-web): 完成代码节点的执行 2025-07-18 15:48:35 +08:00
v-zhangjc9
4cfa110f2f feat(web): 增加数组的判断 2025-07-18 11:07:48 +08:00
v-zhangjc9
ad9dade6a1 feat(web): 完成子流程节点和代码节点的运行 2025-07-17 18:38:19 +08:00
v-zhangjc9
3a61da054e feat(web): 增加循环节点参数 2025-07-17 18:37:43 +08:00
fbc6144d82 refractor(web): 优化选择节点的选择器 2025-07-16 23:08:25 +08:00
72a9d58f4c fix(web): 修复节点入参不更新 2025-07-16 22:54:48 +08:00
v-zhangjc9
a3c2250285 feat(web): 完成分支节点动态改变handle 2025-07-16 19:54:48 +08:00
91e6f49342 feat(web): 增加判断节点对不同类型的适配和处理 2025-07-15 23:19:33 +08:00
v-zhangjc9
35c5150a1f feat(web): 入参增加类型 2025-07-15 17:32:23 +08:00
v-zhangjc9
a7b245a670 refractor(web): 修复入参解析 2025-07-15 16:27:00 +08:00
v-zhangjc9
df8270676a feat(web): 增加循环节点的基本参数 2025-07-15 11:11:36 +08:00
v-zhangjc9
b0e4f5853e feat(web): 增加循环节点的输出变量 2025-07-15 09:09:23 +08:00
0efb041c71 fix(web): 不太优雅地修复循环节点初始化高度的问题 2025-07-14 23:34:58 +08:00
fa06207af8 feat(web): 实现循环节点中新增节点 2025-07-14 23:12:43 +08:00
c7e0b56850 feat(web): 增加循环节点子节点的循环入参 2025-07-14 22:41:27 +08:00
v-zhangjc9
04bc9a2c16 feat(web): 完成循环节点的基本配置 2025-07-14 19:10:58 +08:00
v-zhangjc9
c77395fec4 feat(web): 升级部份依赖 2025-07-14 12:16:13 +08:00
21f02660e9 refractor(web): 优化节点和流程图显示效果 2025-07-13 22:42:37 +08:00
f6e9c9bc70 fix(web): 修复模板节点没有输出 2025-07-13 22:24:40 +08:00
v-zhangjc9
e798332828 feat(ai-web): 增加模板渲染的处理 2025-07-12 23:24:22 +08:00
v-zhangjc9
b0d41e0d88 feat(web): 增加模板节点 2025-07-12 22:56:15 +08:00
v-zhangjc9
02b2d44ccc feat(ai-web): 修复出现遗留边的时候导致出现的null错误 2025-07-12 21:46:00 +08:00
v-zhangjc9
47de3cc376 feat(web): 输出节点增加输出类型 2025-07-12 21:16:06 +08:00
v-zhangjc9
5b9920449d feat(web): 增加节点图标 2025-07-12 20:46:12 +08:00
v-zhangjc9
60f6b79167 feat(web): 增加节点分组 2025-07-12 19:09:47 +08:00
v-zhangjc9
528e66c497 feat(ai-web): 实现入参解析 2025-07-11 16:50:11 +08:00
v-zhangjc9
707f538213 feat(web): 增加文本段输入 2025-07-11 14:43:48 +08:00
v-zhangjc9
eae0d8dacd fix(web): 修复变量校验没有包含入参 2025-07-11 14:43:26 +08:00
v-zhangjc9
bf37c163fb feat(ai-web): 完成流程任务的前后端拉通 2025-07-11 14:26:30 +08:00
v-zhangjc9
ac2b6b1611 feat(web): 增加表单数据校验 2025-07-11 11:12:06 +08:00
v-zhangjc9
863638deaa feat(web): 优化节点展现 2025-07-11 09:46:12 +08:00
v-zhangjc9
fad190567b feat(web): 节点描述和名称直接放在节点数据中 2025-07-10 16:34:47 +08:00
v-zhangjc9
333da7ef88 style(web): 移除未使用的引入 2025-07-10 14:35:29 +08:00
v-zhangjc9
d0ca36e9d7 style(web): 优化命名 2025-07-10 14:33:36 +08:00
v-zhangjc9
f70b3b2a32 fix(web): 移除节点未移除节点数据 2025-07-10 14:28:32 +08:00
v-zhangjc9
f707a0d2b5 refactor(web): 优化节点展现 2025-07-10 12:44:37 +08:00
v-zhangjc9
5e763637da refactor(web): 优化流程节点的定义和实现 2025-07-10 12:08:13 +08:00
v-zhangjc9
898e20d5d7 fix(web): 修复节点option key错误 2025-07-08 10:29:55 +08:00
605cfb7182 feat(web): 尝试增加校验 2025-07-07 23:31:04 +08:00
v-zhangjc9
3afdff0a05 feat(web): 完成前序节点输出变量注入 2025-07-07 19:50:22 +08:00
v-zhangjc9
f523fc7638 feat(web): 优化节点编辑性能 2025-07-07 18:51:35 +08:00
03d0d9d85b feat(web): 尝试优化流程图性能 2025-07-06 22:39:26 +08:00
187c565da4 feat(ai-web): 将任务模板信息固化到任务对象中
保证每个任务对象都有对应的模板信息可以追溯,不会随着模板被修改而失效
2025-07-05 18:45:24 +08:00
f3dfff5075 fix(web): 修复删除api的路径 2025-07-05 17:41:27 +08:00
69420094ec feat(web): 实现任务显示文件列表 2025-07-05 17:39:44 +08:00
v-zhangjc9
051b3dbad2 feat(ai-web): 完成执行任务的创建(提交漏的文件) 2025-07-05 14:48:53 +08:00
v-zhangjc9
64ef5514b4 feat(ai-web): 完成执行任务的创建 2025-07-04 19:31:24 +08:00
v-zhangjc9
4f1dc84405 feat(web): 调整侧边栏实现 2025-07-04 10:54:45 +08:00
d979b3941d feat(ai-web): 完成自研流程图的保存 2025-07-03 23:40:41 +08:00
abdbb5ed03 feat(web): 增加任务和流程图CRUD 2025-07-03 22:21:31 +08:00
v-zhangjc9
8ebaf5de8e feat(web): 优化流程任务编辑 2025-07-03 19:18:17 +08:00
v-zhangjc9
5f7eeb3596 feat(ai-web): 完成任务模板的CRUD 2025-07-03 18:01:36 +08:00
v-zhangjc9
064443f740 feat(ai-web): 移除liteflow入参,该用自己实现的流程引擎 2025-07-03 16:10:02 +08:00
v-zhangjc9
6809750cfa fix(ai-web): 不能设置私有网络访问,会拒绝所有的跨域访问 2025-07-03 10:37:29 +08:00
v-zhangjc9
49cb62b287 fix(bin): 修复pom文件名错误 2025-07-03 10:36:57 +08:00
v-zhangjc9
5cbda28594 fix(gateway): 网关导致重复跨域标记,关闭网关的跨域设置 2025-07-03 10:36:33 +08:00
v-zhangjc9
11c5481287 fix(bin): 修复项目编译时间不正确导致无法识别项目修改状态 2025-07-03 10:01:47 +08:00
v-zhangjc9
67e0cada00 feat(bin): 优化打包脚本,遇到没有修改的模块就跳过发布流程 2025-07-02 17:29:28 +08:00
v-zhangjc9
cdf51cc85f fix(configuration): 优化安全配置 2025-07-02 16:25:06 +08:00
v-zhangjc9
9277d1690c feat(web): 增加常用app list 2025-07-02 15:48:22 +08:00
v-zhangjc9
24ac681cb3 feat(web): 增加装饰 2025-07-02 13:06:10 +08:00
v-zhangjc9
c46da52942 fix(web): 修复访问地址改到了本地测试 2025-07-02 11:28:44 +08:00
v-zhangjc9
7209b52e3d feat(ai-web): 增加JPA建表语句自动生成 2025-07-02 10:54:32 +08:00
v-zhangjc9
959d6fb5c7 feat(ai-web): 完成选择节点 2025-07-01 18:16:05 +08:00
v-zhangjc9
c5c62ab713 feat(ai-web): 完成流程引擎雏形 2025-07-01 15:33:07 +08:00
e59e89a5ad feat(ai-web): 尝试设计流程引擎 2025-06-30 23:31:01 +08:00
b7626180c1 refactor(ai-web): 统一bean工具类 2025-06-30 22:54:40 +08:00
v-zhangjc9
68e54d5110 feat(ai-web): 尝试自建流程引擎 2025-06-30 19:15:19 +08:00
v-zhangjc9
5f133fbfc3 refactor(web): 无聊的改动 2025-06-30 17:15:35 +08:00
v-zhangjc9
d28fbbbba8 fix(web): 优化冗余边检测导致分支节点不正常 2025-06-30 11:14:45 +08:00
v-zhangjc9
67f41c08a0 feat(web): 优化冗余边检测 2025-06-30 10:41:40 +08:00
111ca49815 feat(web): 引入dify的流程检查算法 2025-06-30 00:12:51 +08:00
779fd0eb18 feat(web): 增加分支节点,增加流程检查的测试 2025-06-29 19:02:19 +08:00
8884495a89 fix(ai-web): 恢复配置文件 2025-06-29 14:27:42 +08:00
d08a6babbe feat(ai-web): 增加任务和任务模板 2025-06-28 21:08:31 +08:00
9a3375bd03 fix(ai-web): 修复跨域错误 2025-06-28 17:23:19 +08:00
v-zhangjc9
2c808a5bc9 feat(web): 增加节点的参数展示 2025-06-27 11:31:02 +08:00
v-zhangjc9
6e667c45e1 feat(web): 节点增加入参变量 2025-06-27 10:04:48 +08:00
v-zhangjc9
635c6537ed feat(web): 实现EL表达式的解析 2025-06-27 10:04:00 +08:00
v-zhangjc9
d6b70b1750 feat(web): 尝试增加并行节点解决解析问题 2025-06-26 20:48:49 +08:00
v-zhangjc9
c92a374591 feat(web): 优化UI展示 2025-06-26 20:48:20 +08:00
v-zhangjc9
a2aba82b6e feat(web): 调整节点入参形式 2025-06-26 14:38:09 +08:00
873c1a1d20 feat(web): 增加EL表达式转换 2025-06-26 00:31:04 +08:00
v-zhangjc9
f6bd7e52e1 feat(ai-web): 尝试解析流程图 2025-06-25 17:55:05 +08:00
v-zhangjc9
6f7f7cea67 feat(web): 增加代码节点 2025-06-25 17:54:43 +08:00
v-zhangjc9
33df256863 feat(web): 增加知识库节点 2025-06-25 17:25:50 +08:00
v-zhangjc9
3a51d1e33f feat(web): 升级依赖 2025-06-25 12:36:43 +08:00
v-zhangjc9
d3c7457889 fix(web): 调整样式 2025-06-25 10:43:26 +08:00
2d2eaafcd4 feat(web): 优化样式 2025-06-25 00:04:27 +08:00
v-zhangjc9
566dfef208 feat(web): 增加流程图连线限制 2025-06-24 14:07:07 +08:00
1cba0f4422 feat(web): 完成流程图外部加载 2025-06-24 10:44:18 +08:00
ab56385c8a feat(web): 增加节点输出编辑 2025-06-24 10:15:08 +08:00
b58c34443f feat(web): 增加跨站导航列表 2025-06-24 09:29:48 +08:00
53638a8a6d feat(web): 优化流程界面显示 2025-06-23 23:55:31 +08:00
dc55605c99 feat(web): 正式提取流程设计能力到AI目录下,做代码拆分 2025-06-23 22:51:40 +08:00
7345774258 feat(web): 增加编辑侧边栏关闭时保存节点数据 2025-06-23 22:27:23 +08:00
fcf5f8ad18 feat(web): 优化编辑界面 2025-06-23 22:27:23 +08:00
v-zhangjc9
b53ee57dc3 feat(web): 替换amis渲染,amis渲染太慢,导致卡顿 2025-06-23 22:27:23 +08:00
v-zhangjc9
b916acb1c3 feat(web): 增加流程定义基本能力 2025-06-23 22:27:23 +08:00
v-zhangjc9
c9616eb63a fix(web): 提交遗漏文件 2025-06-23 16:55:36 +08:00
v-zhangjc9
5b3c27ea48 feat(ai-web): 完成JPA存储适配 2025-06-23 16:53:34 +08:00
e48d7e8649 feat(ai-web): 尝试使用jpa作为通用数据库后端 2025-06-23 00:26:31 +08:00
131 changed files with 6960 additions and 1511 deletions

1
.gitignore vendored
View File

@@ -110,3 +110,4 @@ Network Trash Folder
Temporary Items
.apdisk
**/temp/
.build

View File

@@ -1,5 +1,5 @@
import {$, fetch, fs, glob, os, path, spinner, syncProcessCwd, usePowerShell} from 'zx'
import {isEqual, trim, fileSize} from "licia";
import {fileSize, isEqual, trim} from "licia";
import md5file from 'md5-file'
syncProcessCwd(true)
@@ -41,20 +41,70 @@ const millisecondToString = (timestamp) => {
return parts.join('')
}
const dotBuildPath = () => `.build`
const modifiedDataPath = () => `${dotBuildPath()}/modified_time.json`
const readModifiedTimeData = async () => {
if (!fs.existsSync(dotBuildPath())) {
fs.mkdirSync(dotBuildPath(), {recursive: true})
}
if (!(await fs.exists(modifiedDataPath()))) {
fs.writeFileSync(modifiedDataPath(), '{}')
}
return JSON.parse(await fs.readFile(modifiedDataPath(), 'utf-8'))
}
const updateModifiedTimeData = (data) => {
fs.writeFileSync(modifiedDataPath(), JSON.stringify(data, null, 2))
}
const isModified = async (target) => {
if (!target || !(await fs.exists(target))) {
throw new Error("Target 不存在")
}
let stat = fs.statSync(target)
let currentModifiedTime = stat.mtimeMs
let lastModifiedTime = (await readModifiedTimeData())[target]
return !(lastModifiedTime && isEqual(currentModifiedTime, lastModifiedTime));
}
const updateModifiedTime = async (target) => {
if (!target || !(await fs.exists(target))) {
throw new Error("Target 不存在")
}
let stat = fs.statSync(target)
let currentModifiedTime = stat.mtimeMs
let modifiedTimeData = await readModifiedTimeData()
modifiedTimeData[target] = currentModifiedTime
updateModifiedTimeData(modifiedTimeData)
}
export const run_deploy = async (project) => {
if (!(await isModified(project))) {
console.log(`✅ Skip deploy ${project}`)
return
}
let output = await spinner(
`Deploying project ${project}`,
() => $`mvn -pl ${project} clean deploy -D skipTests -s ${maven_setting}`
)
console.log(`✅ Finished deploy ${project} (${millisecondToString(output['duration'])})`)
await updateModifiedTime(project)
}
export const run_deploy_root = async () => {
if (!(await isModified('pom.xml'))) {
console.log(`✅ Skip deploy root`)
return
}
let output = await spinner(
`Deploying root`,
() => $`mvn clean deploy -N -D skipTests -s ${maven_setting}`
)
console.log(`✅ Finished deploy root (${millisecondToString(output['duration'])})`)
await updateModifiedTime('pom.xml')
}
export const run_deploy_batch = async (projects) => {

3
bin/test.js Normal file
View File

@@ -0,0 +1,3 @@
import {isModified} from './library.js'
console.log(await isModified('/Users/lanyuanxiaoyao/Project/IdeaProjects/hudi-service/pom.xml'))

View File

@@ -0,0 +1,86 @@
create table hudi_collect_build_b12.service_ai_feedback
(
id bigint not null comment '记录唯一标记',
created_time datetime(6) comment '记录创建时间',
modified_time datetime(6) comment '记录更新时间',
analysis longtext comment 'AI的分析结果',
conclusion longtext comment 'AI的解决方案',
source longtext not null comment '原始报障说明',
status enum ('ANALYSIS_PROCESSING','ANALYSIS_SUCCESS','FINISHED') not null comment '报障处理状态',
primary key (id)
) comment ='报障信息记录' charset = utf8mb4;
create table hudi_collect_build_b12.service_ai_feedback_pictures
(
feedback_id bigint not null,
pictures_id bigint not null,
primary key (feedback_id, pictures_id)
) comment ='报障相关截图' charset = utf8mb4;
alter table hudi_collect_build_b12.service_ai_feedback_pictures
add constraint UK3npjcyjyqfbdlf2v5tj64j2g3 unique (pictures_id);
create table hudi_collect_build_b12.service_ai_file
(
id bigint not null comment '记录唯一标记',
created_time datetime(6) comment '记录创建时间',
modified_time datetime(6) comment '记录更新时间',
filename varchar(255) comment '文件名称',
md5 varchar(255) comment '文件的md5编码用于校验文件的完整性',
path varchar(255) comment '文件在主机上存储的实际路径',
size bigint comment '文件大小单位是byte',
type varchar(255) comment '文件类型,通常记录的是文件的后缀名',
primary key (id)
) comment ='记录上传的文件存储信息' charset = utf8mb4;
create table hudi_collect_build_b12.service_ai_flow_task
(
id bigint not null comment '记录唯一标记',
created_time datetime(6) comment '记录创建时间',
modified_time datetime(6) comment '记录更新时间',
comment text comment '任务注释,用于额外说明',
error longtext comment '任务运行产生的报错',
input longtext comment '任务输入',
result longtext comment '任务运行结果',
status enum ('ERROR','FINISHED','RUNNING') not null comment '任务运行状态',
template_description varchar(255) comment '任务对应的模板功能、内容说明',
template_flow_graph longtext not null comment '任务对应的模板前端流程图数据',
template_input_schema longtext not null comment '任务对应的模板入参Schema',
template_name varchar(255) not null comment '任务对应的模板名称',
primary key (id)
) comment ='流程任务记录' charset = utf8mb4;
create table hudi_collect_build_b12.service_ai_flow_task_template
(
id bigint not null comment '记录唯一标记',
created_time datetime(6) comment '记录创建时间',
modified_time datetime(6) comment '记录更新时间',
description varchar(255) comment '模板功能、内容说明',
flow_graph longtext not null comment '前端流程图数据',
input_schema longtext not null comment '模板入参Schema',
name varchar(255) not null comment '模板名称',
primary key (id)
) comment ='流程任务模板' charset = utf8mb4;
create table hudi_collect_build_b12.service_ai_group
(
id bigint not null comment '记录唯一标记',
created_time datetime(6) comment '记录创建时间',
modified_time datetime(6) comment '记录更新时间',
name varchar(255) not null comment '分组名称',
status enum ('FINISHED','RUNNING') not null comment '分组处理状态',
knowledge_id bigint not null,
primary key (id)
) comment ='知识库下包含的分组' charset = utf8mb4;
create table hudi_collect_build_b12.service_ai_knowledge
(
id bigint not null comment '记录唯一标记',
created_time datetime(6) comment '记录创建时间',
modified_time datetime(6) comment '记录更新时间',
description longtext not null comment '知识库说明',
name varchar(255) not null comment '知识库名称',
strategy enum ('Cosine','Euclid') not null comment '知识库策略',
vector_source_id bigint not null comment '知识库对应的向量库名',
primary key (id)
) comment ='知识库' charset = utf8mb4;

View File

@@ -1,11 +0,0 @@
CREATE TABLE `service_ai_feedback`
(
`id` bigint NOT NULL,
`source` longtext NOT NULL,
`conclusion` longtext,
`analysis` longtext,
`pictures` longtext,
`status` varchar(50) NOT NULL DEFAULT 'ANALYSIS_PROCESSING',
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) DEFAULT CHARSET = utf8mb4;

View File

@@ -1,11 +0,0 @@
CREATE TABLE `service_ai_file`
(
`id` bigint NOT NULL,
`filename` varchar(500) DEFAULT NULL,
`size` bigint DEFAULT NULL,
`md5` varchar(100) DEFAULT NULL,
`path` varchar(500) DEFAULT NULL,
`type` varchar(50) DEFAULT NULL,
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) DEFAULT CHARSET = utf8mb4;

View File

@@ -1,9 +0,0 @@
CREATE TABLE `service_ai_group`
(
`id` bigint NOT NULL,
`knowledge_id` bigint NOT NULL,
`name` varchar(100) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT 'RUNNING',
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) DEFAULT CHARSET = utf8mb4;

View File

@@ -1,11 +0,0 @@
CREATE TABLE `service_ai_knowledge`
(
`id` bigint NOT NULL,
`vector_source_id` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
`description` longtext NOT NULL,
`strategy` varchar(10) NOT NULL,
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) DEFAULT CHARSET = utf8mb4;

View File

@@ -30,6 +30,7 @@
<curator.version>5.1.0</curator.version>
<hutool.version>5.8.27</hutool.version>
<mapstruct.version>1.6.3</mapstruct.version>
<liteflow.version>2.13.2</liteflow.version>
</properties>
<dependencies>
@@ -110,6 +111,11 @@
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.blinkfox</groupId>
<artifactId>fenix-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 日志相关 -->
<dependency>
@@ -148,7 +154,27 @@
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.13.2</version>
<version>${liteflow.version}</version>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-el-builder</artifactId>
<version>${liteflow.version}</version>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-graaljs</artifactId>
<version>${liteflow.version}</version>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-python</artifactId>
<version>${liteflow.version}</version>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-lua</artifactId>
<version>${liteflow.version}</version>
</dependency>
<dependency>
<groupId>org.noear</groupId>

View File

@@ -1,74 +0,0 @@
package com.lanyuanxiaoyao.service.ai.core.configuration;
import java.time.Instant;
/**
* 使用雪花算法作为ID生成器
*
* @author lanyuanxiaoyao
* @date 2024-11-14
*/
public class SnowflakeId {
/**
* 起始的时间戳
*/
private final static long START_TIMESTAMP = 1;
/**
* 序列号占用的位数
*/
private final static long SEQUENCE_BIT = 11;
/**
* 序列号最大值
*/
private final static long MAX_SEQUENCE_BIT = ~(-1 << SEQUENCE_BIT);
/**
* 时间戳值向左位移
*/
private final static long TIMESTAMP_OFFSET = SEQUENCE_BIT;
/**
* 序列号
*/
private static long sequence = 0;
/**
* 上一次时间戳
*/
private static long lastTimestamp = -1;
public static synchronized long next() {
long currentTimestamp = nowTimestamp();
if (currentTimestamp < lastTimestamp) {
throw new RuntimeException("Clock have moved backwards.");
}
if (currentTimestamp == lastTimestamp) {
// 相同毫秒内, 序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE_BIT;
// 同一毫秒的序列数已经达到最大
if (sequence == 0) {
currentTimestamp = nextTimestamp();
}
} else {
// 不同毫秒内, 序列号置为0
sequence = 0;
}
lastTimestamp = currentTimestamp;
return (currentTimestamp - START_TIMESTAMP) << TIMESTAMP_OFFSET | sequence;
}
private static long nextTimestamp() {
long milli = nowTimestamp();
while (milli <= lastTimestamp) {
milli = nowTimestamp();
}
return milli;
}
private static long nowTimestamp() {
return Instant.now().toEpochMilli();
}
}

View File

@@ -1,7 +1,5 @@
package com.lanyuanxiaoyao.service.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
@@ -13,6 +11,9 @@ import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* @author lanyuanxiaoyao
@@ -21,17 +22,30 @@ import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.addAllowedOriginPattern("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.setMaxAge(7200L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return new CorsFilter(source);
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.authorizeHttpRequests(
registry -> registry
.requestMatchers(HttpMethod.OPTIONS, "/**")
.permitAll()
.anyRequest()
.authenticated()
registry -> registry
.requestMatchers(HttpMethod.OPTIONS, "/**")
.permitAll()
.anyRequest()
.authenticated()
)
.httpBasic(Customizer.withDefaults())
.cors(AbstractHttpConfigurer::disable)
.cors(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.build();

View File

@@ -20,8 +20,16 @@
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
@@ -40,7 +48,15 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>com.blinkfox</groupId>
<artifactId>fenix-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
@@ -50,6 +66,10 @@
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-el-builder</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-tika-document-reader</artifactId>
@@ -66,6 +86,21 @@
<groupId>org.noear</groupId>
<artifactId>solon-ai-dialect-openai</artifactId>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-graaljs</artifactId>
</dependency>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-python</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-ant</artifactId>
<version>6.6.8.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -89,6 +124,11 @@
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.6.8.Final</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amapstruct.defaultComponentModel=spring</arg>

View File

@@ -1,15 +1,14 @@
package com.lanyuanxiaoyao.service.ai.web;
import com.blinkfox.fenix.EnableFenix;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -23,27 +22,17 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@EnableEncryptableProperties
@EnableRetry
@EnableScheduling
public class WebApplication implements ApplicationRunner, ApplicationContextAware {
private static ApplicationContext context;
@EnableFenix
@EnableJpaAuditing
public class WebApplication implements ApplicationRunner {
public static void main(String[] args) {
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
SpringApplication.run(WebApplication.class, args);
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return context.getBean(name, clazz);
}
@Override
public void run(ApplicationArguments args) {
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
WebApplication.context = context;
}
}

View File

@@ -0,0 +1,13 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
/**
* @author lanyuanxiaoyao
* @date 2024-11-28
*/
public interface DetailController<DETAIL_ITEM> {
String DETAIL = "/detail/{id}";
AmisResponse<DETAIL_ITEM> detail(Long id) throws Exception;
}

View File

@@ -0,0 +1,16 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisCrudResponse;
import com.lanyuanxiaoyao.service.ai.web.base.controller.query.Query;
/**
* @author lanyuanxiaoyao
* @date 2024-11-28
*/
public interface ListController {
String LIST = "/list";
AmisCrudResponse list() throws Exception;
AmisCrudResponse list(Query query) throws Exception;
}

View File

@@ -0,0 +1,13 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
/**
* @author lanyuanxiaoyao
* @date 2024-11-28
*/
public interface RemoveController {
String REMOVE = "/remove/{id}";
AmisResponse<Object> remove(Long id) throws Exception;
}

View File

@@ -0,0 +1,13 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
/**
* @author lanyuanxiaoyao
* @date 2024-11-28
*/
public interface SaveController<SAVE_ITEM> {
String SAVE = "/save";
AmisResponse<Long> save(SAVE_ITEM item) throws Exception;
}

View File

@@ -0,0 +1,8 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller;
/**
* @author lanyuanxiaoyao
* @date 2024-11-28
*/
public interface SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> extends SaveController<SAVE_ITEM>, ListController, DetailController<DETAIL_ITEM>, RemoveController {
}

View File

@@ -0,0 +1,105 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller;
import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisCrudResponse;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.base.controller.query.Query;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* @author lanyuanxiaoyao
* @date 2024-11-26
*/
@Slf4j
public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> implements SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
protected final SimpleServiceSupport<ENTITY> service;
public SimpleControllerSupport(SimpleServiceSupport<ENTITY> service) {
this.service = service;
}
@PostMapping(SAVE)
@Override
public AmisResponse<Long> save(@RequestBody SAVE_ITEM item) throws Exception {
SaveItemMapper<ENTITY, SAVE_ITEM> mapper = saveItemMapper();
return AmisResponse.responseSuccess(service.save(mapper.from(item)));
}
@GetMapping(LIST)
@Override
public AmisCrudResponse list() throws Exception {
ListItemMapper<ENTITY, LIST_ITEM> mapper = listItemMapper();
return AmisCrudResponse.responseCrudData(
service.list()
.collect(entity -> {
try {
return mapper.from(entity);
} catch (Exception e) {
throw new RuntimeException(e);
}
})
);
}
@PostMapping(LIST)
@Override
public AmisCrudResponse list(@RequestBody Query query) throws Exception {
if (ObjectUtil.isNull(query)) {
return AmisCrudResponse.responseCrudData(List.of(), 0);
}
ListItemMapper<ENTITY, LIST_ITEM> mapper = listItemMapper();
Page<ENTITY> result = service.list(query);
return AmisCrudResponse.responseCrudData(
result.get()
.map(entity -> {
try {
return mapper.from(entity);
} catch (Exception e) {
throw new RuntimeException(e);
}
})
.toList(),
result.getTotalElements()
);
}
@GetMapping(DETAIL)
@Override
public AmisResponse<DETAIL_ITEM> detail(@PathVariable("id") Long id) throws Exception {
DetailItemMapper<ENTITY, DETAIL_ITEM> mapper = detailItemMapper();
return AmisResponse.responseSuccess(mapper.from(service.detailOrThrow(id)));
}
@GetMapping(REMOVE)
@Override
public AmisResponse<Object> remove(@PathVariable("id") Long id) throws Exception {
service.remove(id);
return AmisResponse.responseSuccess();
}
protected abstract SaveItemMapper<ENTITY, SAVE_ITEM> saveItemMapper();
protected abstract ListItemMapper<ENTITY, LIST_ITEM> listItemMapper();
protected abstract DetailItemMapper<ENTITY, DETAIL_ITEM> detailItemMapper();
public interface SaveItemMapper<ENTITY, SAVE_ITEM> {
ENTITY from(SAVE_ITEM item) throws Exception;
}
public interface ListItemMapper<ENTITY, LIST_ITEM> {
LIST_ITEM from(ENTITY entity) throws Exception;
}
public interface DetailItemMapper<ENTITY, DETAIL_ITEM> {
DETAIL_ITEM from(ENTITY entity) throws Exception;
}
}

View File

@@ -0,0 +1,61 @@
package com.lanyuanxiaoyao.service.ai.web.base.controller.query;
import lombok.Data;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
/**
* 统一查询
*
* @author lanyuanxiaoyao
* @date 2024-12-03
*/
@Data
public class Query {
private Queryable query;
private ImmutableList<Sortable> sort;
private Pageable page;
@Data
public static class Queryable {
private ImmutableList<String> nullEqual;
private ImmutableList<String> notNullEqual;
private ImmutableList<String> empty;
private ImmutableList<String> notEmpty;
private ImmutableMap<String, ?> equal;
private ImmutableMap<String, ?> notEqual;
private ImmutableMap<String, String> like;
private ImmutableMap<String, String> notLike;
private ImmutableMap<String, ?> great;
private ImmutableMap<String, ?> less;
private ImmutableMap<String, ?> greatEqual;
private ImmutableMap<String, ?> lessEqual;
private ImmutableMap<String, ImmutableList<?>> in;
private ImmutableMap<String, ImmutableList<?>> notIn;
private ImmutableMap<String, Between> between;
private ImmutableMap<String, Between> notBetween;
@Data
public static class Between {
private String start;
private String end;
}
}
@Data
public static class Sortable {
private String column;
private Direction direction;
public enum Direction {
ASC,
DESC,
}
}
@Data
public static class Pageable {
private Integer index;
private Integer size;
}
}

View File

@@ -0,0 +1,31 @@
package com.lanyuanxiaoyao.service.ai.web.base.entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* 实体类公共字段
*
* @author lanyuanxiaoyao
* @date 2024-11-20
*/
@Getter
@Setter
@ToString
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class IdOnlyEntity {
@Comment("记录唯一标记")
@Id
@GeneratedValue(generator = "snowflake")
@GenericGenerator(name = "snowflake", strategy = "com.lanyuanxiaoyao.service.ai.web.configuration.SnowflakeIdGenerator")
private Long id;
}

View File

@@ -0,0 +1,32 @@
package com.lanyuanxiaoyao.service.ai.web.base.entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* 实体类公共字段
*
* @author lanyuanxiaoyao
* @date 2024-11-20
*/
@Getter
@Setter
@ToString(callSuper = true)
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class SimpleEntity extends IdOnlyEntity {
@Comment("记录创建时间")
@CreatedDate
private LocalDateTime createdTime;
@Comment("记录更新时间")
@LastModifiedDate
private LocalDateTime modifiedTime;
}

View File

@@ -0,0 +1,17 @@
package com.lanyuanxiaoyao.service.ai.web.base.entity;
import java.time.LocalDateTime;
import lombok.Data;
/**
* 创建对象使用的接收类
*
* @author lanyuanxiaoyao
* @date 2024-11-26
*/
@Data
public class SimpleItem {
private Long id;
private LocalDateTime createdTime;
private LocalDateTime modifiedTime;
}

View File

@@ -0,0 +1,16 @@
package com.lanyuanxiaoyao.service.ai.web.base.repository;
import com.blinkfox.fenix.jpa.FenixJpaRepository;
import com.blinkfox.fenix.specification.FenixJpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.QueryByExampleExecutor;
/**
* 整合一下
*
* @author lanyuanxiaoyao
* @date 2024-11-21
*/
@NoRepositoryBean
public interface SimpleRepository<E, ID> extends FenixJpaRepository<E, ID>, FenixJpaSpecificationExecutor<E>, QueryByExampleExecutor<E> {
}

View File

@@ -0,0 +1,34 @@
package com.lanyuanxiaoyao.service.ai.web.base.service;
import com.lanyuanxiaoyao.service.ai.web.base.controller.query.Query;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import java.util.Optional;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.set.ImmutableSet;
import org.springframework.data.domain.Page;
/**
* @author lanyuanxiaoyao
* @date 2024-11-28
*/
public interface SimpleService<ENTITY extends SimpleEntity> {
Long save(ENTITY entity) throws Exception;
Long count() throws Exception;
ImmutableList<ENTITY> list() throws Exception;
ImmutableList<ENTITY> list(ImmutableSet<Long> ids) throws Exception;
Page<ENTITY> list(Query query) throws Exception;
Optional<ENTITY> detailOptional(Long id) throws Exception;
ENTITY detail(Long id) throws Exception;
ENTITY detailOrThrow(Long id) throws Exception;
ENTITY detailOrNull(Long id) throws Exception;
void remove(Long id) throws Exception;
}

View File

@@ -0,0 +1,251 @@
package com.lanyuanxiaoyao.service.ai.web.base.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.base.controller.query.Query;
import com.lanyuanxiaoyao.service.ai.web.base.entity.IdOnlyEntity_;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity_;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.transaction.Transactional;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.set.ImmutableSet;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
/**
* @author lanyuanxiaoyao
* @date 2024-11-21
*/
@Slf4j
public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implements SimpleService<ENTITY> {
private static final Integer DEFAULT_PAGE_INDEX = 1;
private static final Integer DEFAULT_PAGE_SIZE = 10;
protected final SimpleRepository<ENTITY, Long> repository;
public SimpleServiceSupport(SimpleRepository<ENTITY, Long> repository) {
this.repository = repository;
}
@Transactional(rollbackOn = Throwable.class)
@Override
public Long save(ENTITY entity) {
if (ObjectUtil.isNotNull(entity.getId())) {
Long id = entity.getId();
ENTITY targetEntity = repository.findById(entity.getId()).orElseThrow(() -> new IdNotFoundException(id));
BeanUtil.copyProperties(
entity,
targetEntity,
CopyOptions.create()
.setIgnoreProperties(
IdOnlyEntity_.ID,
SimpleEntity_.CREATED_TIME,
SimpleEntity_.MODIFIED_TIME
)
);
entity = targetEntity;
}
entity = repository.save(entity);
return entity.getId();
}
@Override
public Long count() {
return repository.count(
(root, query, builder) ->
builder.and(
listPredicate(root, query, builder)
.reject(ObjectUtil::isNull)
.toArray(new Predicate[]{})
)
);
}
@Override
public ImmutableList<ENTITY> list() {
return Lists.immutable.ofAll(repository.findAll(
(root, query, builder) ->
builder.and(
listPredicate(root, query, builder)
.reject(ObjectUtil::isNull)
.toArray(new Predicate[]{})
)
));
}
@Override
public ImmutableList<ENTITY> list(ImmutableSet<Long> ids) {
return Lists.immutable.ofAll(repository.findAll(
(root, query, builder) -> {
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicate(root, query, builder));
predicates.add(builder.in(root.get("id")).value(ids));
return builder.and(predicates.reject(ObjectUtil::isNull).toArray(new Predicate[predicates.size()]));
}
));
}
private <Y> Path<Y> column(Root<ENTITY> root, String column) {
String[] columns = StrUtil.splitToArray(column, "/");
Path<Y> path = root.get(columns[0]);
for (int i = 1; i < columns.length; i++) {
path = path.get(columns[i]);
}
return path;
}
@SuppressWarnings({"unchecked", "rawtypes"})
private <Y> Object value(Path<Y> column, Object value) {
Class<?> javaType = column.getJavaType();
if (EnumUtil.isEnum(javaType)) {
return EnumUtil.fromString((Class<Enum>) javaType, (String) value);
}
return value;
}
@SuppressWarnings("unchecked")
protected ImmutableList<Predicate> queryPredicates(Query.Queryable queryable, Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
MutableList<Predicate> predicates = Lists.mutable.empty();
if (ObjectUtil.isEmpty(queryable)) {
return predicates.toImmutable();
}
if (ObjectUtil.isNotEmpty(queryable.getNullEqual())) {
queryable.getNullEqual().forEach(column -> predicates.add(builder.isNull(column(root, column))));
}
if (ObjectUtil.isNotEmpty(queryable.getNotNullEqual())) {
queryable.getNotNullEqual().forEach(column -> predicates.add(builder.isNull(column(root, column))));
}
if (ObjectUtil.isNotEmpty(queryable.getEmpty())) {
queryable.getEmpty().forEach(column -> predicates.add(builder.isEmpty(column(root, column))));
}
if (ObjectUtil.isNotEmpty(queryable.getNotEmpty())) {
queryable.getNotEmpty().forEach(column -> predicates.add(builder.isNotEmpty(column(root, column))));
}
if (ObjectUtil.isNotEmpty(queryable.getEqual())) {
queryable.getEqual().forEachKeyValue((column, value) -> {
Path<Object> path = column(root, column);
predicates.add(builder.equal(path, value(path, value)));
});
}
if (ObjectUtil.isNotEmpty(queryable.getNotEqual())) {
queryable.getEqual().forEachKeyValue((column, value) -> {
Path<Object> path = column(root, column);
predicates.add(builder.notEqual(path, value(path, value)));
});
}
if (ObjectUtil.isNotEmpty(queryable.getLike())) {
queryable.getLike().forEachKeyValue((column, value) -> predicates.add(builder.like(column(root, column), value)));
}
if (ObjectUtil.isNotEmpty(queryable.getNotLike())) {
queryable.getNotLike().forEachKeyValue((column, value) -> predicates.add(builder.notLike(column(root, column), value)));
}
if (ObjectUtil.isNotEmpty(queryable.getGreat())) {
queryable.getGreat().forEachKeyValue((column, value) -> predicates.add(builder.greaterThan(column(root, column), (Comparable<Object>) value)));
}
if (ObjectUtil.isNotEmpty(queryable.getLess())) {
queryable.getLess().forEachKeyValue((column, value) -> predicates.add(builder.lessThan(column(root, column), (Comparable<Object>) value)));
}
if (ObjectUtil.isNotEmpty(queryable.getGreatEqual())) {
queryable.getGreatEqual().forEachKeyValue((column, value) -> predicates.add(builder.greaterThanOrEqualTo(column(root, column), (Comparable<Object>) value)));
}
if (ObjectUtil.isNotEmpty(queryable.getLessEqual())) {
queryable.getLessEqual().forEachKeyValue((column, value) -> predicates.add(builder.lessThanOrEqualTo(column(root, column), (Comparable<Object>) value)));
}
if (ObjectUtil.isNotEmpty(queryable.getIn())) {
queryable.getIn().forEachKeyValue((column, value) -> predicates.add(builder.in(column(root, column)).value(value)));
}
if (ObjectUtil.isNotEmpty(queryable.getNotIn())) {
queryable.getNotIn().forEachKeyValue((column, value) -> predicates.add(builder.in(column(root, column)).value(value).not()));
}
if (ObjectUtil.isNotEmpty(queryable.getBetween())) {
queryable.getBetween().forEachKeyValue((column, value) -> predicates.add(builder.between(column(root, column), value.getStart(), value.getEnd())));
}
if (ObjectUtil.isNotEmpty(queryable.getNotBetween())) {
queryable.getNotBetween().forEachKeyValue((column, value) -> predicates.add(builder.between(column(root, column), value.getStart(), value.getEnd())));
}
return predicates.toImmutable();
}
protected ImmutableList<Predicate> listPredicate(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return Lists.immutable.empty();
}
@Override
public Page<ENTITY> list(Query listQuery) {
PageRequest pageRequest = PageRequest.of(DEFAULT_PAGE_INDEX - 1, DEFAULT_PAGE_SIZE, Sort.by(SimpleEntity_.CREATED_TIME).descending());
if (ObjectUtil.isNotNull(listQuery.getPage())) {
pageRequest = PageRequest.of(
ObjectUtil.defaultIfNull(listQuery.getPage().getIndex(), DEFAULT_PAGE_INDEX) - 1,
ObjectUtil.defaultIfNull(listQuery.getPage().getSize(), DEFAULT_PAGE_SIZE),
Sort.by(SimpleEntity_.CREATED_TIME).descending()
);
}
return repository.findAll(
(root, query, builder) -> {
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicate(root, query, builder));
predicates.addAllIterable(queryPredicates(listQuery.getQuery(), root, query, builder));
return builder.and(predicates.reject(ObjectUtil::isNull).toArray(new Predicate[predicates.size()]));
},
pageRequest
);
}
@Override
public Optional<ENTITY> detailOptional(Long id) {
if (ObjectUtil.isNull(id)) {
return Optional.empty();
}
return repository.findOne(
(root, query, builder) -> {
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicate(root, query, builder));
predicates.add(builder.equal(root.get(IdOnlyEntity_.id), id));
return builder.and(predicates.reject(ObjectUtil::isNull).toArray(new Predicate[predicates.size()]));
}
);
}
@Override
public ENTITY detail(Long id) {
return detailOrNull(id);
}
@Override
public ENTITY detailOrThrow(Long id) {
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
}
@Override
public ENTITY detailOrNull(Long id) {
return detailOptional(id).orElse(null);
}
@Transactional(rollbackOn = Throwable.class)
@Override
public void remove(Long id) {
if (ObjectUtil.isNotNull(id)) {
repository.deleteById(id);
}
}
public static final class IdNotFoundException extends RuntimeException {
public IdNotFoundException() {
super("资源不存在");
}
public IdNotFoundException(Long id) {
super(StrUtil.format("ID为{}的资源不存在", id));
}
}
}

View File

@@ -0,0 +1,92 @@
package com.lanyuanxiaoyao.service.ai.web.configuration;
import java.io.Serializable;
import java.time.Instant;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
/**
* 使用雪花算法作为ID生成器
*
* @author lanyuanxiaoyao
* @date 2024-11-14
*/
@Slf4j
public class SnowflakeIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) {
try {
return Snowflake.next();
} catch (Exception e) {
log.error("Generate snowflake id failed", e);
throw new RuntimeException(e);
}
}
public static class Snowflake {
/**
* 起始的时间戳
*/
private final static long START_TIMESTAMP = 1;
/**
* 序列号占用的位数
*/
private final static long SEQUENCE_BIT = 11;
/**
* 序列号最大值
*/
private final static long MAX_SEQUENCE_BIT = ~(-1 << SEQUENCE_BIT);
/**
* 时间戳值向左位移
*/
private final static long TIMESTAMP_OFFSET = SEQUENCE_BIT;
/**
* 序列号
*/
private static long sequence = 0;
/**
* 上一次时间戳
*/
private static long lastTimestamp = -1;
public static synchronized long next() {
long currentTimestamp = nowTimestamp();
if (currentTimestamp < lastTimestamp) {
throw new RuntimeException("Clock have moved backwards.");
}
if (currentTimestamp == lastTimestamp) {
// 相同毫秒内, 序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE_BIT;
// 同一毫秒的序列数已经达到最大
if (sequence == 0) {
currentTimestamp = nextTimestamp();
}
} else {
// 不同毫秒内, 序列号置为0
sequence = 0;
}
lastTimestamp = currentTimestamp;
return (currentTimestamp - START_TIMESTAMP) << TIMESTAMP_OFFSET | sequence;
}
private static long nextTimestamp() {
long milli = nowTimestamp();
while (milli <= lastTimestamp) {
milli = nowTimestamp();
}
return milli;
}
private static long nowTimestamp() {
return Instant.now().toEpochMilli();
}
}
}

View File

@@ -0,0 +1,24 @@
package com.lanyuanxiaoyao.service.ai.web.configuration;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringBeanGetter implements ApplicationContextAware {
private static ApplicationContext context;
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return context.getBean(name, clazz);
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringBeanGetter.context = context;
}
}

View File

@@ -8,7 +8,7 @@ import cn.hutool.crypto.SecureUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.configuration.FileStoreProperties;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.DataFileVO;
import com.lanyuanxiaoyao.service.ai.web.entity.DataFile;
import com.lanyuanxiaoyao.service.ai.web.service.DataFileService;
import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
@@ -21,7 +21,10 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.set.ImmutableSet;
import org.mapstruct.factory.Mappers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@@ -56,6 +59,26 @@ public class DataFileController {
this.sliceFolderPath = StrUtil.format("{}/slice", uploadFolderPath);
}
@PostMapping("/detail")
public AmisResponse<?> detail(@RequestBody DetailRequest request) {
var mapper = Mappers.getMapper(DetailResponse.Mapper.class);
return AmisResponse.responseCrudData(dataFileService.downloadFile(request.ids).collect(mapper::from));
}
@GetMapping("/detail")
public AmisResponse<?> detail(@RequestParam("ids") String ids) {
if (StrUtil.isBlank(ids)) {
return AmisResponse.responseCrudData(Sets.immutable.empty());
}
var mapper = Mappers.getMapper(DetailResponse.Mapper.class);
return AmisResponse.responseCrudData(
dataFileService.downloadFile(
Sets.immutable.ofAll(StrUtil.split(ids, ","))
.collect(Long::parseLong)
).collect(mapper::from)
);
}
@PostMapping("")
public AmisResponse<FinishResponse> upload(@RequestParam("file") MultipartFile file) throws IOException {
String filename = file.getOriginalFilename();
@@ -81,7 +104,7 @@ public class DataFileController {
@GetMapping("/download/{id}")
public void download(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
DataFileVO dataFile = dataFileService.downloadFile(id);
DataFile dataFile = dataFileService.downloadFile(id);
File targetFile = new File(dataFile.getPath());
response.setHeader("Content-Type", dataFile.getType());
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
@@ -170,6 +193,24 @@ public class DataFileController {
}
}
@Data
public static final class DetailRequest {
private ImmutableSet<Long> ids;
}
@Data
public static final class DetailResponse {
private Long id;
private String filename;
private Long size;
private String md5;
@org.mapstruct.Mapper
public interface Mapper {
DetailResponse from(DataFile file);
}
}
@Data
public static final class StartRequest {
private String name;

View File

@@ -1,52 +1,40 @@
package com.lanyuanxiaoyao.service.ai.web.controller.feedback;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisCrudResponse;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.configuration.FileStoreProperties;
import com.lanyuanxiaoyao.service.ai.web.base.controller.SimpleControllerSupport;
import com.lanyuanxiaoyao.service.ai.web.base.entity.IdOnlyEntity;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleItem;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import com.lanyuanxiaoyao.service.ai.web.service.DataFileService;
import com.lanyuanxiaoyao.service.ai.web.service.feedback.FeedbackService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.set.ImmutableSet;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("feedback")
public class FeedbackController {
private final FileStoreProperties fileStoreProperties;
public class FeedbackController extends SimpleControllerSupport<Feedback, FeedbackController.SaveItem, FeedbackController.ListItem, FeedbackController.ListItem> {
private final FeedbackService feedbackService;
private final DataFileService dataFileService;
public FeedbackController(FileStoreProperties fileStoreProperties, FeedbackService feedbackService) {
this.fileStoreProperties = fileStoreProperties;
public FeedbackController(FeedbackService feedbackService, DataFileService dataFileService) {
super(feedbackService);
this.feedbackService = feedbackService;
this.dataFileService = dataFileService;
}
@PostMapping("add")
public void add(@RequestBody CreateItem item) {
feedbackService.add(item.source, ObjectUtil.defaultIfNull(item.pictures, Lists.immutable.empty()));
}
@GetMapping("list")
public AmisCrudResponse list() {
return AmisResponse.responseCrudData(feedbackService.list().collect(feedback -> new ListItem(fileStoreProperties, feedback)));
}
@GetMapping("delete")
public void delete(@RequestParam("id") Long id) {
feedbackService.remove(id);
}
@GetMapping("reanalysis")
public void reanalysis(@RequestParam("id") Long id) {
@GetMapping("reanalysis/{id}")
public void reanalysis(@PathVariable("id") Long id) {
feedbackService.reanalysis(id);
}
@@ -55,10 +43,31 @@ public class FeedbackController {
feedbackService.updateConclusion(item.getId(), item.getConclusion());
}
@Override
protected SaveItemMapper<Feedback, SaveItem> saveItemMapper() {
return item -> {
Feedback feedback = new Feedback();
feedback.setSource(item.getSource());
feedback.setPictures(dataFileService.downloadFile(item.getPictures()).toSet());
return feedback;
};
}
@Override
protected ListItemMapper<Feedback, ListItem> listItemMapper() {
return Mappers.getMapper(ListItem.Mapper.class);
}
@Override
protected DetailItemMapper<Feedback, ListItem> detailItemMapper() {
ListItemMapper<Feedback, ListItem> mapper = listItemMapper();
return mapper::from;
}
@Data
public static final class CreateItem {
public static final class SaveItem {
private String source;
private ImmutableList<Long> pictures;
private ImmutableSet<Long> pictures;
}
@Data
@@ -68,22 +77,24 @@ public class FeedbackController {
}
@Data
public static final class ListItem {
private Long id;
@EqualsAndHashCode(callSuper = true)
public static class ListItem extends SimpleItem {
private String source;
private ImmutableList<String> pictures;
private Feedback.Status status;
private ImmutableSet<Long> pictures;
private String analysis;
private String conclusion;
private Feedback.Status status;
public ListItem(FileStoreProperties fileStoreProperties, Feedback feedback) {
this.id = feedback.getId();
this.source = feedback.getSource();
this.pictures = feedback.getPictureIds()
.collect(id -> StrUtil.format("{}/upload/download/{}", fileStoreProperties.getDownloadPrefix(), id));
this.status = feedback.getStatus();
this.analysis = feedback.getAnalysis();
this.conclusion = feedback.getConclusion();
@org.mapstruct.Mapper(
imports = {
IdOnlyEntity.class,
Sets.class
}
)
public interface Mapper extends ListItemMapper<Feedback, ListItem> {
@Mapping(target = "pictures", expression = "java(Sets.immutable.ofAll(feedback.getPictures()).collect(IdOnlyEntity::getId))")
@Override
ListItem from(Feedback feedback);
}
}
}

View File

@@ -1,12 +1,15 @@
package com.lanyuanxiaoyao.service.ai.web.controller.knowledge;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.base.controller.SimpleControllerSupport;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleItem;
import com.lanyuanxiaoyao.service.ai.web.entity.Group;
import com.lanyuanxiaoyao.service.ai.web.service.knowledge.GroupService;
import java.util.concurrent.ExecutionException;
import com.lanyuanxiaoyao.service.ai.web.service.knowledge.KnowledgeBaseService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.mapstruct.factory.Mappers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
@@ -16,21 +19,49 @@ import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("knowledge/group")
public class GroupController {
private final GroupService groupService;
public class GroupController extends SimpleControllerSupport<Group, GroupController.SaveItem, GroupController.ListItem, GroupController.ListItem> {
private final KnowledgeBaseService knowledgeBaseService;
public GroupController(GroupService groupService) {
this.groupService = groupService;
public GroupController(GroupService groupService, KnowledgeBaseService knowledgeBaseService) {
super(groupService);
this.knowledgeBaseService = knowledgeBaseService;
}
@GetMapping("list")
public AmisResponse<?> list(@RequestParam("knowledge_id") Long knowledgeId) {
return AmisResponse.responseCrudData(groupService.list(knowledgeId));
@Override
protected SaveItemMapper<Group, SaveItem> saveItemMapper() {
return item -> {
Group group = new Group();
group.setName(item.getName());
group.setKnowledge(knowledgeBaseService.detailOrThrow(item.getKnowledgeId()));
return group;
};
}
@GetMapping("delete")
public AmisResponse<?> delete(@RequestParam("id") Long id) throws ExecutionException, InterruptedException {
groupService.remove(id);
return AmisResponse.responseSuccess();
@Override
protected ListItemMapper<Group, ListItem> listItemMapper() {
return Mappers.getMapper(ListItem.Mapper.class);
}
@Override
protected DetailItemMapper<Group, ListItem> detailItemMapper() {
ListItemMapper<Group, ListItem> mapper = listItemMapper();
return mapper::from;
}
@Data
public static final class SaveItem {
private String name;
private Long knowledgeId;
}
@Data
@EqualsAndHashCode(callSuper = true)
public static final class ListItem extends SimpleItem {
private String name;
private Group.Status status;
@org.mapstruct.Mapper
public interface Mapper extends ListItemMapper<Group, ListItem> {
}
}
}

View File

@@ -3,15 +3,24 @@ package com.lanyuanxiaoyao.service.ai.web.controller.knowledge;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisMapResponse;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.base.controller.SimpleControllerSupport;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleItem;
import com.lanyuanxiaoyao.service.ai.web.entity.Knowledge;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.SegmentVO;
import com.lanyuanxiaoyao.service.ai.web.service.EmbeddingService;
import com.lanyuanxiaoyao.service.ai.web.service.knowledge.KnowledgeBaseService;
import io.qdrant.client.grpc.Collections;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -25,58 +34,52 @@ import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("knowledge")
public class KnowledgeBaseController {
public class KnowledgeBaseController extends SimpleControllerSupport<Knowledge, KnowledgeBaseController.SaveItem, KnowledgeBaseController.ListItem, KnowledgeBaseController.ListItem> {
private final KnowledgeBaseService knowledgeBaseService;
private final EmbeddingService embeddingService;
public KnowledgeBaseController(KnowledgeBaseService knowledgeBaseService, EmbeddingService embeddingService) {
super(knowledgeBaseService);
this.knowledgeBaseService = knowledgeBaseService;
this.embeddingService = embeddingService;
}
@PostMapping("add")
public void add(
@RequestParam("name") String name,
@RequestParam("description") String description,
@RequestParam("strategy") String strategy
) throws ExecutionException, InterruptedException {
knowledgeBaseService.add(name, description, strategy);
@Override
protected SaveItemMapper<Knowledge, SaveItem> saveItemMapper() {
return Mappers.getMapper(SaveItem.Mapper.class);
}
@Override
protected ListItemMapper<Knowledge, ListItem> listItemMapper() {
ListItem.Mapper mapper = Mappers.getMapper(ListItem.Mapper.class);
return knowledge -> mapper.from(knowledge, knowledgeBaseService.collectionInfo(knowledge.getVectorSourceId()));
}
@Override
protected DetailItemMapper<Knowledge, ListItem> detailItemMapper() {
ListItem.Mapper mapper = Mappers.getMapper(ListItem.Mapper.class);
return knowledge -> mapper.from(knowledge, knowledgeBaseService.collectionInfo(knowledge.getVectorSourceId()));
}
@GetMapping("{id}/name")
public AmisMapResponse name(@PathVariable("id") Long id) {
return AmisResponse.responseMapData()
.setData("name", knowledgeBaseService.getName(id));
}
@PostMapping("update_description")
public void updateDescription(
@RequestParam("id") Long id,
@RequestParam("description") String description
) {
) throws Exception {
knowledgeBaseService.updateDescription(id, description);
}
@GetMapping("name")
public AmisMapResponse name(@RequestParam("id") Long id) {
return AmisResponse.responseMapData()
.setData("name", knowledgeBaseService.getName(id));
}
@GetMapping("list")
public AmisResponse<?> list() {
return AmisResponse.responseCrudData(knowledgeBaseService.list());
}
@GetMapping("delete")
public void delete(@RequestParam("id") Long id) throws ExecutionException, InterruptedException {
knowledgeBaseService.remove(id);
}
@PostMapping("preview_text")
public AmisResponse<?> previewText(
@RequestParam(value = "mode", defaultValue = "NORMAL") String mode,
@RequestParam(value = "type", defaultValue = "text") String type,
@RequestParam(value = "content", required = false) String content,
@RequestParam(value = "files", required = false) String files
) {
if (StrUtil.equals("text", type)) {
public AmisResponse<?> previewText(@RequestBody SubmitItem item) {
if (StrUtil.equals("text", item.getType())) {
return AmisResponse.responseCrudData(
embeddingService.preview(mode, content)
embeddingService.preview(item.getMode(), item.getContent())
.collect(doc -> {
SegmentVO vo = new SegmentVO();
vo.setId(doc.getId());
@@ -84,9 +87,9 @@ public class KnowledgeBaseController {
return vo;
})
);
} else if (StrUtil.equals("file", type)) {
} else if (StrUtil.equals("file", item.getType())) {
return AmisResponse.responseCrudData(
embeddingService.preview(mode, Lists.immutable.of(files.split(",")))
embeddingService.preview(item.getMode(), item.getFiles())
.collect(doc -> {
SegmentVO vo = new SegmentVO();
vo.setId(doc.getId());
@@ -95,44 +98,87 @@ public class KnowledgeBaseController {
})
);
} else {
throw new IllegalArgumentException("Unsupported type: " + type);
throw new IllegalArgumentException("Unsupported type: " + item.getType());
}
}
@PostMapping("submit_text")
public void submitText(
@RequestParam("id") Long id,
@RequestParam(value = "mode", defaultValue = "NORMAL") String mode,
@RequestParam(value = "type", defaultValue = "text") String type,
@RequestParam(value = "content", required = false) String content,
@RequestParam(value = "files", required = false) String files
) {
if (StrUtil.equals("text", type)) {
embeddingService.submit(id, mode, content);
} else if (StrUtil.equals("file", type)) {
embeddingService.submit(id, mode, Lists.immutable.of(files.split(",")));
public void submitText(@RequestBody SubmitItem item) {
if (StrUtil.equals("text", item.getMode())) {
embeddingService.submit(item.getId(), item.getMode(), item.getContent());
} else if (StrUtil.equals("file", item.getType())) {
embeddingService.submit(item.getId(), item.getMode(), item.getFiles());
} else {
throw new IllegalArgumentException("Unsupported type: " + type);
throw new IllegalArgumentException("Unsupported type: " + item.getType());
}
}
@PostMapping("submit_text_directly")
public void submitDirectly(
@RequestParam("id") Long id,
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "split_key", defaultValue = "\n\n") String splitKey,
@RequestBody String content
) {
embeddingService.submitDirectly(id, name, Lists.immutable.of(content.split(splitKey)));
public void submitDirectly(@RequestBody SubmitDirectlyItem item) {
embeddingService.submitDirectly(item.getId(), item.getName(), Lists.immutable.ofAll(StrUtil.split(item.getContent(), item.getSplitKey())));
}
@PostMapping("query")
public ImmutableList<String> query(
@RequestParam("id") Long id,
@RequestParam(value = "limit", defaultValue = "5") Integer limit,
@RequestParam(value = "threshold", defaultValue = "0.6") Double threshold,
@RequestBody String text
) throws ExecutionException, InterruptedException, IOException {
return knowledgeBaseService.query(id, text, limit, threshold);
public ImmutableList<String> query(@RequestBody QueryItem item) throws ExecutionException, InterruptedException, IOException {
return knowledgeBaseService.query(item.getId(), item.getText(), item.getLimit(), item.getThreshold());
}
@Data
public static final class SaveItem {
private String name;
private String strategy;
private String description;
@org.mapstruct.Mapper
public interface Mapper extends SaveItemMapper<Knowledge, SaveItem> {
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public static final class ListItem extends SimpleItem {
private Long vectorSourceId;
private String name;
private String description;
private String strategy;
private Long size;
private Long points;
private Long segments;
private String status;
@org.mapstruct.Mapper
public interface Mapper extends ListItemMapper<Knowledge, ListItem> {
@Mapping(source = "info.config.params.vectorsConfig.params.distance", target = "strategy")
@Mapping(source = "info.config.params.vectorsConfig.params.size", target = "size")
@Mapping(source = "info.pointsCount", target = "points")
@Mapping(source = "info.segmentsCount", target = "segments")
@Mapping(source = "info.status", target = "status")
ListItem from(Knowledge knowledge, Collections.CollectionInfo info);
}
}
@Data
public static final class SubmitItem {
private Long id;
private String mode;
private String type;
private String content;
private ImmutableList<Long> files;
}
@Data
public static final class SubmitDirectlyItem {
private Long id;
private String name;
private String splitKey = "\n\n";
private String content;
}
@Data
public static final class QueryItem {
private Long id;
private Integer limit = 5;
private Double threshold = 0.6;
private String text;
}
}

View File

@@ -0,0 +1,112 @@
package com.lanyuanxiaoyao.service.ai.web.controller.task;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.base.controller.SimpleControllerSupport;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleItem;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskService;
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskTemplateService;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.factory.Mappers;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("flow_task")
public class TaskController extends SimpleControllerSupport<FlowTask, TaskController.SaveItem, TaskController.ListItem, TaskController.DetailItem> {
private final FlowTaskService flowTaskService;
private final FlowTaskTemplateService flowTaskTemplateService;
private final ObjectMapper mapper;
public TaskController(FlowTaskService flowTaskService, FlowTaskTemplateService flowTaskTemplateService, Jackson2ObjectMapperBuilder builder) {
super(flowTaskService);
this.flowTaskService = flowTaskService;
this.flowTaskTemplateService = flowTaskTemplateService;
this.mapper = builder.build();
}
@GetMapping("input_data/{id}")
public AmisResponse<?> getInputData(@PathVariable("id") Long id) throws JsonProcessingException {
var task = flowTaskService.detailOrThrow(id);
return AmisResponse.responseSuccess(mapper.readValue(task.getInput(), Map.class));
}
@GetMapping("input_schema/{id}")
public AmisResponse<?> getInputSchema(@PathVariable("id") Long id) throws JsonProcessingException {
var task = flowTaskService.detailOrThrow(id);
return AmisResponse.responseSuccess(mapper.readValue(task.getTemplateInputSchema(), Map.class));
}
@GetMapping("execute/{id}")
public AmisResponse<?> execute(@PathVariable("id") Long id) throws JsonProcessingException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
flowTaskService.execute(id);
return AmisResponse.responseSuccess();
}
@Override
protected SaveItemMapper<FlowTask, SaveItem> saveItemMapper() {
return item -> {
FlowTask task = new FlowTask();
FlowTaskTemplate template = flowTaskTemplateService.detailOrThrow(item.getTemplateId());
task.setTemplateName(template.getName());
task.setTemplateDescription(template.getDescription());
task.setTemplateInputSchema(template.getInputSchema());
task.setTemplateFlowGraph(template.getFlowGraph());
task.setInput(mapper.writeValueAsString(item.getInput()));
return task;
};
}
@Override
protected ListItemMapper<FlowTask, ListItem> listItemMapper() {
ListItem.Mapper map = Mappers.getMapper(ListItem.Mapper.class);
return map::from;
}
@Override
protected DetailItemMapper<FlowTask, DetailItem> detailItemMapper() {
DetailItem.Mapper map = Mappers.getMapper(DetailItem.Mapper.class);
return map::from;
}
@Data
public static final class SaveItem {
private Long templateId;
private Object input;
}
@Data
@EqualsAndHashCode(callSuper = true)
public static class ListItem extends SimpleItem {
private String templateName;
private FlowTask.Status status;
@org.mapstruct.Mapper
public static abstract class Mapper {
public abstract ListItem from(FlowTask task);
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public static class DetailItem extends ListItem {
private String error;
private String result;
@org.mapstruct.Mapper
public static abstract class Mapper extends ListItem.Mapper {
public abstract DetailItem from(FlowTask task);
}
}
}

View File

@@ -0,0 +1,122 @@
package com.lanyuanxiaoyao.service.ai.web.controller.task;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.web.base.controller.SimpleControllerSupport;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleItem;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskTemplateService;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Context;
import org.mapstruct.factory.Mappers;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("flow_task/template")
public class TaskTemplateController extends SimpleControllerSupport<FlowTaskTemplate, TaskTemplateController.SaveItem, TaskTemplateController.ListItem, TaskTemplateController.DetailItem> {
private final FlowTaskTemplateService flowTaskTemplateService;
private final ObjectMapper mapper;
public TaskTemplateController(FlowTaskTemplateService flowTaskTemplateService, Jackson2ObjectMapperBuilder builder) {
super(flowTaskTemplateService);
this.flowTaskTemplateService = flowTaskTemplateService;
this.mapper = builder.build();
}
@GetMapping("input_schema/{id}")
public AmisResponse<?> getInputSchema(@PathVariable("id") Long id) throws JsonProcessingException {
var template = flowTaskTemplateService.detailOrThrow(id);
return AmisResponse.responseSuccess(mapper.readValue(template.getInputSchema(), Map.class));
}
@GetMapping("flow_graph/{id}")
public AmisResponse<?> getFlowGraph(@PathVariable("id") Long id) throws JsonProcessingException {
var template = flowTaskTemplateService.detailOrThrow(id);
return AmisResponse.responseSuccess(mapper.readValue(template.getFlowGraph(), Map.class));
}
@PostMapping("update_flow_graph")
public AmisResponse<?> updateFlowGraph(@RequestBody UpdateGraphItem item) throws JsonProcessingException {
flowTaskTemplateService.updateFlowGraph(item.getId(), mapper.writeValueAsString(item.getGraph()));
return AmisResponse.responseSuccess();
}
@Override
protected SaveItemMapper<FlowTaskTemplate, SaveItem> saveItemMapper() {
return Mappers.getMapper(SaveItem.Mapper.class);
}
@Override
protected ListItemMapper<FlowTaskTemplate, ListItem> listItemMapper() {
return Mappers.getMapper(ListItem.Mapper.class);
}
@Override
protected DetailItemMapper<FlowTaskTemplate, DetailItem> detailItemMapper() {
var map = Mappers.getMapper(DetailItem.Mapper.class);
return template -> map.from(template, mapper);
}
@Data
public static final class SaveItem {
private Long id;
private String name;
private String description;
@org.mapstruct.Mapper
public interface Mapper extends SaveItemMapper<FlowTaskTemplate, SaveItem> {
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public static class ListItem extends SimpleItem {
private String name;
private String description;
@org.mapstruct.Mapper
public interface Mapper extends ListItemMapper<FlowTaskTemplate, ListItem> {
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public static class DetailItem extends SimpleItem {
private String name;
private String description;
private Map<String, Object> inputSchema;
private Map<String, Object> flowGraph;
@org.mapstruct.Mapper
public static abstract class Mapper {
public abstract DetailItem from(FlowTaskTemplate template, @Context ObjectMapper mapper) throws Exception;
public Map<String, Object> mapJson(String source, @Context ObjectMapper mapper) throws Exception {
if (ObjectUtil.isNull(source)) {
return null;
}
return mapper.readValue(source, new TypeReference<>() {
});
}
}
}
@Data
public static class UpdateGraphItem {
private Long id;
private Map<String, Object> graph;
}
}

View File

@@ -0,0 +1,32 @@
package com.lanyuanxiaoyao.service.ai.web.engine;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import com.lanyuanxiaoyao.service.ai.web.engine.store.FlowStore;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.collections.api.map.ImmutableMap;
/**
* 流程执行器
*
* @author lanyuanxiaoyao
* @version 20250630
*/
public class FlowExecutor {
private final FlowStore flowStore;
private final ImmutableMap<String, Class<? extends FlowNodeRunner>> runnerMap;
public FlowExecutor(FlowStore flowStore, ImmutableMap<String, Class<? extends FlowNodeRunner>> runnerMap) {
this.flowStore = flowStore;
this.runnerMap = runnerMap;
}
public void execute(FlowGraph graph) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
execute(graph, new FlowContext());
}
public void execute(FlowGraph graph, FlowContext context) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
var runner = new FlowGraphRunner(graph, context, flowStore, runnerMap);
runner.run();
}
}

View File

@@ -0,0 +1,124 @@
package com.lanyuanxiaoyao.service.ai.web.engine;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
import com.lanyuanxiaoyao.service.ai.web.engine.store.FlowStore;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.Queue;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.multimap.set.ImmutableSetMultimap;
/**
* Graph执行器
*
* @author lanyuanxiaoyao
* @version 20250701
*/
public final class FlowGraphRunner {
private final FlowGraph flowGraph;
private final FlowContext flowContext;
private final FlowStore flowStore;
private final ImmutableMap<String, Class<? extends FlowNodeRunner>> nodeRunnerClass;
private final Queue<FlowNode> executionQueue = new LinkedList<>();
private final ImmutableSetMultimap<String, FlowEdge> nodeInputMap;
private final ImmutableSetMultimap<String, FlowEdge> nodeOutputMap;
private final ImmutableMap<String, FlowNode> nodeMap;
public FlowGraphRunner(FlowGraph flowGraph, FlowContext flowContext, FlowStore flowStore, ImmutableMap<String, Class<? extends FlowNodeRunner>> nodeRunnerClass) {
this.flowGraph = flowGraph;
this.flowContext = flowContext;
this.flowStore = flowStore;
this.nodeRunnerClass = nodeRunnerClass;
nodeInputMap = flowGraph.edges().groupBy(FlowEdge::target);
nodeOutputMap = flowGraph.edges().groupBy(FlowEdge::source);
nodeMap = flowGraph.nodes().toImmutableMap(FlowNode::id, node -> node);
}
public void run() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
flowStore.init(flowGraph);
for (FlowNode node : flowGraph.nodes()) {
if (ObjectUtil.isNull(node.parentId())) {
executionQueue.offer(node);
}
}
while (!executionQueue.isEmpty()) {
var node = executionQueue.poll();
if (ObjectUtil.isNull(node)) {
continue;
}
process(node, flowContext);
}
}
private void process(FlowNode node, FlowContext context) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
if (
(
// 没有入节点,即开始节点
!nodeInputMap.containsKey(node.id())
// 或者所有入的边状态都已经完成
|| nodeInputMap.get(node.id()).allSatisfy(edge -> flowStore.checkEdgeStatus(flowGraph.id(), edge.id(), FlowEdge.Status.EXECUTE, FlowEdge.Status.SKIP))
)
// 当前节点还未执行
&& flowStore.checkNodeStatus(flowGraph.id(), node.id(), FlowNode.Status.INITIAL)
) {
// 是开始节点或入的边有至少一条是「执行」
if (
!nodeInputMap.containsKey(node.id())
|| nodeInputMap.get(node.id()).anySatisfy(edge -> flowStore.checkEdgeStatus(flowGraph.id(), edge.id(), FlowEdge.Status.EXECUTE))
) {
flowStore.updateNodeToRunning(flowGraph.id(), node.id());
var runnerClazz = nodeRunnerClass.get(node.type());
var runner = runnerClazz.getDeclaredConstructor().newInstance();
runner.setNodeId(node.id());
runner.setContext(context);
// 处理子流程节点的逻辑
if (runner instanceof FlowNodeSubflowRunner subflowRunner) {
var subflowNodes = flowGraph.nodes().select(n -> StrUtil.equals(n.parentId(), node.id()));
var subGraph = new FlowGraph(IdUtil.fastUUID(), subflowNodes, flowGraph.edges());
subflowRunner.setSubGraph(subGraph);
}
runner.run();
// 处理选择节点的逻辑
if (runner instanceof FlowNodeOptionalRunner) {
var targetPoint = ((FlowNodeOptionalRunner) runner).getTargetPoint();
for (FlowEdge edge : nodeOutputMap.get(node.id())) {
if (StrUtil.equals(targetPoint, edge.sourcePoint())) {
flowStore.updateEdgeToExecute(flowGraph.id(), edge.id());
} else {
flowStore.updateEdgeToSkip(flowGraph.id(), edge.id());
}
executionQueue.offer(nodeMap.get(edge.target()));
}
} else {
for (FlowEdge edge : nodeOutputMap.get(node.id())) {
flowStore.updateEdgeToExecute(flowGraph.id(), edge.id());
executionQueue.offer(nodeMap.get(edge.target()));
}
}
flowStore.updateNodeToFinished(flowGraph.id(), node.id());
}
// 所有入的边都是跳过,当前节点就跳过
else {
flowStore.updateNodeToSkipped(flowGraph.id(), node.id());
for (FlowEdge edge : nodeOutputMap.get(node.id())) {
flowStore.updateEdgeToSkip(flowGraph.id(), edge.id());
executionQueue.offer(nodeMap.get(edge.target()));
}
}
}
}
}

View File

@@ -0,0 +1,58 @@
package com.lanyuanxiaoyao.service.ai.web.engine;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
/**
* @author lanyuanxiaoyao
* @version 20250711
*/
@Slf4j
public class FlowHelper {
private static final TemplateEngine TEMPLATE_ENGINE = TemplateUtil.createEngine();
public static ImmutableMap<String, Object> generateInputVariablesMap(String nodeId, FlowContext context) {
var variableMap = Maps.mutable.<String, Object>empty();
var currentNodeData = context.get(nodeId);
if (currentNodeData.containsKey("inputs")) {
var inputsMap = (Map<String, Map<String, String>>) currentNodeData.get("inputs");
for (String variableName : inputsMap.keySet()) {
var expression = inputsMap.get(variableName).get("variable");
var targetVariable = generateVariable(expression, context);
if (ObjectUtil.isNotNull(targetVariable)) {
variableMap.put(variableName, targetVariable);
}
}
}
return variableMap.toImmutable();
}
public static Object generateVariable(String expression, FlowContext context) {
if (StrUtil.contains(expression, ".")) {
var splits = StrUtil.splitTrim(expression, ".", 2);
var targetNodeId = splits.get(0);
var targetVariableName = splits.get(1);
if (!context.getData().containsKey(targetNodeId)) {
throw new RuntimeException(StrUtil.format("Target node id not found: {}", targetNodeId));
}
var targetNodeData = context.getData().get(targetNodeId);
if (!targetNodeData.containsKey(targetVariableName)) {
throw new RuntimeException(StrUtil.format("Target node variable not found: {}.{}", targetNodeId, targetVariableName));
}
return targetNodeData.get(targetVariableName);
}
return null;
}
public static String renderTemplateText(String templateText, Map<?, ?> data) {
var template = TEMPLATE_ENGINE.getTemplate(templateText);
return template.render(data);
}
}

View File

@@ -0,0 +1,15 @@
package com.lanyuanxiaoyao.service.ai.web.engine;
import lombok.Getter;
public abstract class FlowNodeOptionalRunner extends FlowNodeRunner {
@Getter
private String targetPoint;
public abstract String runOptional();
@Override
public void run() {
this.targetPoint = runOptional();
}
}

View File

@@ -0,0 +1,36 @@
package com.lanyuanxiaoyao.service.ai.web.engine;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
import lombok.Getter;
import org.eclipse.collections.api.map.MutableMap;
public abstract class FlowNodeRunner {
@Getter
private String nodeId;
@Getter
private FlowContext context;
public abstract void run();
void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
void setContext(FlowContext context) {
this.context = context;
}
protected MutableMap<String, Object> getData() {
return context.get(nodeId);
}
protected <T> T getData(String key) {
var data = context.get(nodeId);
return (T) data.get(key);
}
protected <T> void setData(String key, T value) {
var data = context.get(nodeId);
data.put(key, value);
}
}

View File

@@ -0,0 +1,17 @@
package com.lanyuanxiaoyao.service.ai.web.engine;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import lombok.Getter;
import lombok.Setter;
/**
* 包含子流程的流程
*
* @author lanyuanxiaoyao
* @version 20250717
*/
public abstract class FlowNodeSubflowRunner extends FlowNodeRunner {
@Getter
@Setter
private FlowGraph subGraph;
}

View File

@@ -0,0 +1,17 @@
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
import lombok.Data;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.MutableMap;
@Data
public class FlowContext {
private MutableMap<String, MutableMap<String, Object>> data = Maps.mutable.<String, MutableMap<String, Object>>empty().asSynchronized();
public MutableMap<String, Object> get(String key) {
if (!data.containsKey(key)) {
data.put(key, Maps.mutable.<String, Object>empty().asSynchronized());
}
return data.get(key);
}
}

View File

@@ -0,0 +1,39 @@
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
/**
* 流程图中的边
*
* @author lanyuanxiaoyao
* @version 20250630
*/
public record FlowEdge(
String id,
String source,
String target,
@JsonProperty("sourceHandle")
String sourcePoint,
@JsonProperty("targetHandle")
String targetPoint
) {
public enum Status {
INITIAL, EXECUTE, SKIP
}
public record State(
String id,
Status status,
LocalDateTime startingTime,
LocalDateTime finishedTime
) {
public State(String edgeId) {
this(edgeId, Status.INITIAL, LocalDateTime.now(), null);
}
public State(String edgeId, Status status) {
this(edgeId, status, LocalDateTime.now(), null);
}
}
}

View File

@@ -0,0 +1,16 @@
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
import org.eclipse.collections.api.set.ImmutableSet;
/**
* 流程图
*
* @author lanyuanxiaoyao
* @version 20250630
*/
public record FlowGraph(
String id,
ImmutableSet<FlowNode> nodes,
ImmutableSet<FlowEdge> edges
) {
}

View File

@@ -0,0 +1,34 @@
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
import java.time.LocalDateTime;
/**
* 流程图中的节点
*
* @author lanyuanxiaoyao
* @version 20250630
*/
public record FlowNode(
String id,
String type,
String parentId
) {
public enum Status {
INITIAL, RUNNING, FINISHED, SKIPPED
}
public record State(
String id,
Status status,
LocalDateTime startingTime,
LocalDateTime finishedTime
) {
public State(String nodeId) {
this(nodeId, Status.INITIAL, LocalDateTime.now(), null);
}
public State(String nodeId, Status status) {
this(nodeId, status, LocalDateTime.now(), null);
}
}
}

View File

@@ -0,0 +1,37 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
import com.lanyuanxiaoyao.service.ai.web.engine.node.code.CodeExecutor;
import com.lanyuanxiaoyao.service.ai.web.engine.node.code.JavaScriptCodeExecutor;
import com.lanyuanxiaoyao.service.ai.web.engine.node.code.PythonCodeExecutor;
import lombok.extern.slf4j.Slf4j;
/**
* @author lanyuanxiaoyao
* @version 20250717
*/
@Slf4j
public class CodeNode extends FlowNodeRunner {
@Override
public void run() {
var mapper = SpringBeanGetter.getBean(ObjectMapper.class);
var inputVariablesMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
var type = this.<String>getData("type");
var script = this.<String>getData("content");
CodeExecutor executor = switch (type) {
case "javascript" -> new JavaScriptCodeExecutor(mapper);
case "python" -> new PythonCodeExecutor(mapper);
default -> null;
};
if (ObjectUtil.isNull(executor)) {
throw new RuntimeException(StrUtil.format("Unsupported type: {}", type));
}
var result = executor.execute(script, inputVariablesMap);
result.forEachKeyValue(this::setData);
}
}

View File

@@ -0,0 +1,16 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
import lombok.extern.slf4j.Slf4j;
/**
* @author lanyuanxiaoyao
* @version 20250711
*/
@Slf4j
public class InputNode extends FlowNodeRunner {
@Override
public void run() {
// 入参相关的内容在启动流程的时候已经注入了
}
}

View File

@@ -0,0 +1,32 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
/**
* @author lanyuanxiaoyao
* @version 20250711
*/
@Slf4j
public class LlmNode extends FlowNodeRunner {
@Override
public void run() {
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
log.info("Variable map: {}", variableMap);
var sourcePrompt = (String) getData("systemPrompt");
if (StrUtil.isNotBlank(sourcePrompt)) {
var prompt = FlowHelper.renderTemplateText(sourcePrompt, variableMap.toMap());
var builder = SpringBeanGetter.getBean("chat", ChatClient.Builder.class);
var client = builder.build();
var content = client.prompt()
.user(prompt)
.call()
.content();
setData("text", content);
}
}
}

View File

@@ -0,0 +1,18 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeSubflowRunner;
import lombok.extern.slf4j.Slf4j;
/**
* 循环节点
*
* @author lanyuanxiaoyao
* @version 20250717
*/
@Slf4j
public class LoopNode extends FlowNodeSubflowRunner {
@Override
public void run() {
log.info("{}", getSubGraph());
}
}

View File

@@ -0,0 +1,19 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
import lombok.extern.slf4j.Slf4j;
/**
* @author lanyuanxiaoyao
* @version 20250711
*/
@Slf4j
public class OutputNode extends FlowNodeRunner {
@Override
public void run() {
String expression = getData("output");
var targetVariable = FlowHelper.generateVariable(expression, getContext());
log.info("Target: {}", targetVariable);
}
}

View File

@@ -0,0 +1,87 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeOptionalRunner;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
/**
* @author lanyuanxiaoyao
* @version 20250717
*/
@SuppressWarnings("unchecked")
@Slf4j
public class SwitchNode extends FlowNodeOptionalRunner {
@Override
public String runOptional() {
var conditions = this.<List<Map<String, Object>>>getData("conditions");
for (Map<String, Object> item : conditions) {
var condition = (Map<String, Object>) item.getOrDefault("condition", Map.of());
var id = (String) condition.getOrDefault("id", "");
var conjunction = (String) condition.getOrDefault("conjunction", "and");
var conditionChildren = ((List<Map<String, Object>>) condition.getOrDefault("children", List.<Map<String, Object>>of()));
if (
StrUtil.equals(conjunction, "and")
&& conditionChildren.stream().allMatch(this::check)
) {
return id;
} else if (
StrUtil.equals(conjunction, "or")
&& conditionChildren.stream().anyMatch(this::check)
) {
return id;
}
}
return "";
}
private Boolean check(Map<String, Object> condition) {
var leftVariable = (String) BeanUtil.getProperty(condition, "left.field");
var left = FlowHelper.generateVariable(leftVariable, getContext());
var operator = (String) condition.get("op");
var right = condition.get("right");
if (left instanceof CharSequence || left instanceof Boolean) {
String source = StrUtil.toStringOrNull(left);
String target = StrUtil.toStringOrNull(right);
return switch (operator) {
case "equal" -> StrUtil.equals(source, target);
case "not_equal" -> !StrUtil.equals(source, target);
case "is_empty" -> StrUtil.isBlank(source);
case "is_not_empty" -> StrUtil.isNotBlank(source);
case "like" -> StrUtil.contains(source, target);
case "not_like" -> !StrUtil.contains(source, target);
case "starts_with" -> StrUtil.startWith(source, target);
case "ends_with" -> StrUtil.endWith(source, target);
default -> false;
};
} else if (left instanceof Number source) {
var sourceNumber = new BigDecimal(StrUtil.toString(source));
var targetNumber = new BigDecimal(StrUtil.toString(right));
return switch (operator) {
case "equal" -> NumberUtil.equals(sourceNumber, targetNumber);
case "not_equal" -> !NumberUtil.equals(sourceNumber, targetNumber);
case "greater" -> NumberUtil.isGreater(sourceNumber, targetNumber);
case "greater_equal" -> NumberUtil.isGreaterOrEqual(sourceNumber, targetNumber);
case "less" -> NumberUtil.isLess(sourceNumber, targetNumber);
case "less_equal" -> NumberUtil.isLessOrEqual(sourceNumber, targetNumber);
default -> false;
};
} else if (left instanceof Collection<?> source) {
return switch (operator) {
case "is_empty" -> CollectionUtil.isEmpty(source);
case "is_not_empty" -> CollectionUtil.isNotEmpty(source);
case "contain" -> CollectionUtil.safeContains(source, right);
case "not_contain" -> !CollectionUtil.safeContains(source, right);
default -> false;
};
}
return false;
}
}

View File

@@ -0,0 +1,11 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node.code;
import org.eclipse.collections.api.map.ImmutableMap;
/**
* @author lanyuanxiaoyao
* @version 20250717
*/
public interface CodeExecutor {
ImmutableMap<String, Object> execute(String script, ImmutableMap<String, Object> inputVariablesMap);
}

View File

@@ -0,0 +1,57 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node.code;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Source;
/**
* @author lanyuanxiaoyao
* @version 20250717
*/
@Slf4j
public class JavaScriptCodeExecutor implements CodeExecutor {
private final ObjectMapper mapper;
public JavaScriptCodeExecutor(ObjectMapper mapper) {
this.mapper = mapper;
}
@SneakyThrows
@Override
public ImmutableMap<String, Object> execute(String script, ImmutableMap<String, Object> inputVariablesMap) {
if (StrUtil.isBlank(script)) {
return Maps.immutable.empty();
}
try (var engin = Engine.create()) {
try (
var context = Context.newBuilder()
.allowAllAccess(true)
.engine(engin)
.build()
) {
var bindings = context.getBindings("js");
bindings.putMember("context", inputVariablesMap);
var result = context.eval(
Source.create(
"js",
"""
function process() {
%s
}
var result = process();
JSON.stringify(result? result: {})
""".formatted(script)
)
);
return mapper.readValue(result.asString(), new TypeReference<>() {});
}
}
}
}

View File

@@ -0,0 +1,50 @@
package com.lanyuanxiaoyao.service.ai.web.engine.node.code;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.map.ImmutableMap;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;
/**
* @author lanyuanxiaoyao
* @version 20250718
*/
@Slf4j
public class PythonCodeExecutor implements CodeExecutor {
private final ObjectMapper mapper;
public PythonCodeExecutor(ObjectMapper mapper) {
this.mapper = mapper;
}
@SneakyThrows
@Override
public ImmutableMap<String, Object> execute(String script, ImmutableMap<String, Object> inputVariablesMap) {
try (var systemState = new PySystemState()) {
systemState.setdefaultencoding("UTF-8");
var interpreter = new PythonInterpreter(null, systemState);
script = Arrays.stream(script.split("\n"))
.map(line -> " " + line)
.collect(Collectors.joining("\n"));
interpreter.set("context", inputVariablesMap);
var pythonScript = interpreter.compile(
"""
import json
def process():
%s
result = json.dumps(process())
""".formatted(script)
);
interpreter.exec(pythonScript);
var result = interpreter.get("result");
return mapper.readValue((String) result.__tojava__(String.class), new TypeReference<>() {});
}
}
}

View File

@@ -0,0 +1,31 @@
package com.lanyuanxiaoyao.service.ai.web.engine.store;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
/**
* 存储状态
*
* @author lanyuanxiaoyao
* @version 20250701
*/
public interface FlowStore {
void init(FlowGraph flowGraph);
void updateNodeToRunning(String graphId, String nodeId);
void updateNodeToSkipped(String graphId, String nodeId);
void updateNodeToFinished(String graphId, String nodeId);
void updateEdgeToExecute(String graphId, String edgeId);
void updateEdgeToSkip(String graphId, String edgeId);
boolean checkNodeStatus(String graphId, String nodeId, FlowNode.Status... statuses);
boolean checkEdgeStatus(String graphId, String edgeId, FlowEdge.Status... statuses);
void print();
}

View File

@@ -0,0 +1,107 @@
package com.lanyuanxiaoyao.service.ai.web.engine.store;
import cn.hutool.core.util.ArrayUtil;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.MutableMap;
/**
* 基于内存的流程状态存储
*
* @author lanyuanxiaoyao
* @version 20250701
*/
@Slf4j
public class InMemoryFlowStore implements FlowStore {
private static final MutableMap<String, FlowNode.State> flowNodeStateMap = Maps.mutable.<String, FlowNode.State>empty().asSynchronized();
private static final MutableMap<String, FlowEdge.State> flowEdgeStateMap = Maps.mutable.<String, FlowEdge.State>empty().asSynchronized();
private String multiKey(String... key) {
return String.join("-", key);
}
@Override
public void init(FlowGraph flowGraph) {
for (FlowNode node : flowGraph.nodes()) {
flowNodeStateMap.put(multiKey(flowGraph.id(), node.id()), new FlowNode.State(node.id()));
}
for (FlowEdge edge : flowGraph.edges()) {
flowEdgeStateMap.put(multiKey(flowGraph.id(), edge.id()), new FlowEdge.State(edge.id()));
}
}
@Override
public void updateNodeToRunning(String graphId, String nodeId) {
flowNodeStateMap.updateValue(
multiKey(graphId, nodeId),
() -> new FlowNode.State(nodeId, FlowNode.Status.RUNNING),
old -> new FlowNode.State(nodeId, FlowNode.Status.RUNNING, old.startingTime(), old.finishedTime())
);
}
@Override
public void updateNodeToSkipped(String graphId, String nodeId) {
flowNodeStateMap.updateValue(
multiKey(graphId, nodeId),
() -> new FlowNode.State(nodeId, FlowNode.Status.SKIPPED),
old -> new FlowNode.State(nodeId, FlowNode.Status.SKIPPED, old.startingTime(), old.finishedTime())
);
}
@Override
public void updateNodeToFinished(String graphId, String nodeId) {
flowNodeStateMap.updateValue(
multiKey(graphId, nodeId),
() -> new FlowNode.State(nodeId, FlowNode.Status.FINISHED),
old -> new FlowNode.State(nodeId, FlowNode.Status.FINISHED, old.startingTime(), old.finishedTime())
);
}
@Override
public void updateEdgeToExecute(String graphId, String edgeId) {
flowEdgeStateMap.updateValue(
multiKey(graphId, edgeId),
() -> new FlowEdge.State(edgeId, FlowEdge.Status.EXECUTE),
old -> new FlowEdge.State(edgeId, FlowEdge.Status.EXECUTE, old.startingTime(), old.finishedTime())
);
}
@Override
public void updateEdgeToSkip(String graphId, String edgeId) {
flowEdgeStateMap.updateValue(
multiKey(graphId, edgeId),
() -> new FlowEdge.State(edgeId, FlowEdge.Status.SKIP),
old -> new FlowEdge.State(edgeId, FlowEdge.Status.SKIP, old.startingTime(), old.finishedTime())
);
}
@Override
public boolean checkNodeStatus(String graphId, String nodeId, FlowNode.Status... statuses) {
String key = multiKey(graphId, nodeId);
if (flowNodeStateMap.containsKey(key)) {
return ArrayUtil.contains(statuses, flowNodeStateMap.get(key).status());
}
return false;
}
@Override
public boolean checkEdgeStatus(String graphId, String edgeId, FlowEdge.Status... statuses) {
String key = multiKey(graphId, edgeId);
if (flowEdgeStateMap.containsKey(key)) {
return ArrayUtil.contains(statuses, flowEdgeStateMap.get(key).status());
}
return false;
}
@Override
public void print() {
log.info("====== Flow Store ======");
log.info("====== Flow Node ======");
flowNodeStateMap.forEachKeyValue((key, value) -> log.info("{}: {}", key, value.status()));
log.info("====== Flow Edge ======");
flowEdgeStateMap.forEachKeyValue((key, value) -> log.info("{}: {}", key, value.status()));
}
}

View File

@@ -0,0 +1,43 @@
package com.lanyuanxiaoyao.service.ai.web.entity;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.common.Constants;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
/**
* 上传文件
*
* @author lanyuanxiaoyao
* @date 2024-11-21
*/
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@Table(catalog = Constants.DATABASE_NAME, name = "service_ai_file")
@NoArgsConstructor
@Comment("记录上传的文件存储信息")
public class DataFile extends SimpleEntity {
@Comment("文件名称")
private String filename;
@Comment("文件大小单位是byte")
private Long size;
@Comment("文件的md5编码用于校验文件的完整性")
private String md5;
@Comment("文件在主机上存储的实际路径")
private String path;
@Comment("文件类型,通常记录的是文件的后缀名")
private String type;
public DataFile(String filename) {
this.filename = filename;
}
}

View File

@@ -1,18 +1,58 @@
package com.lanyuanxiaoyao.service.ai.web.entity;
import lombok.Data;
import org.eclipse.collections.api.list.ImmutableList;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.common.Constants;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinTable;
import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Data
public class Feedback {
private Long id;
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(catalog = Constants.DATABASE_NAME, name = "service_ai_feedback")
@NamedEntityGraph(name = "feedback.detail", attributeNodes = {
@NamedAttributeNode("pictures")
})
@Comment("报障信息记录")
public class Feedback extends SimpleEntity {
@Comment("原始报障说明")
@Column(nullable = false, columnDefinition = "longtext")
private String source;
private ImmutableList<Long> pictureIds;
@Comment("报障相关截图")
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(catalog = Constants.DATABASE_NAME, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ToString.Exclude
private Set<DataFile> pictures;
@Comment("AI的分析结果")
@Column(columnDefinition = "longtext")
private String analysis;
@Comment("AI的解决方案")
@Column(columnDefinition = "longtext")
private String conclusion;
private Status status;
private Long createdTime;
private Long modifiedTime;
@Comment("报障处理状态")
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Status status = Status.ANALYSIS_PROCESSING;
public enum Status {
ANALYSIS_PROCESSING,

View File

@@ -0,0 +1,63 @@
package com.lanyuanxiaoyao.service.ai.web.entity;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.common.Constants;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(catalog = Constants.DATABASE_NAME, name = "service_ai_flow_task")
@Comment("流程任务记录")
public class FlowTask extends SimpleEntity {
// 每个任务对应的模板都是唯一,避免模板修改之后任务的状态、运行等状态都无法展示
// 不管允许不允许任务重跑,这些都要保存下来
@Comment("任务对应的模板名称")
@Column(nullable = false)
private String templateName;
@Comment("任务对应的模板功能、内容说明")
private String templateDescription;
@Comment("任务对应的模板入参Schema")
@Column(nullable = false, columnDefinition = "longtext")
private String templateInputSchema;
@Comment("任务对应的模板前端流程图数据")
@Column(nullable = false, columnDefinition = "longtext")
private String templateFlowGraph = "{}";
@Comment("任务注释,用于额外说明")
@Column(columnDefinition = "text")
private String comment;
@Comment("任务输入")
@Column(columnDefinition = "longtext")
private String input;
@Comment("任务运行状态")
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Status status = Status.RUNNING;
@Comment("任务运行产生的报错")
@Column(columnDefinition = "longtext")
private String error;
@Comment("任务运行结果")
@Column(columnDefinition = "longtext")
private String result;
public enum Status {
RUNNING,
ERROR,
FINISHED,
}
}

View File

@@ -0,0 +1,36 @@
package com.lanyuanxiaoyao.service.ai.web.entity;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.common.Constants;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(catalog = Constants.DATABASE_NAME, name = "service_ai_flow_task_template")
@Comment("流程任务模板")
public class FlowTaskTemplate extends SimpleEntity {
@Comment("模板名称")
@Column(nullable = false)
private String name;
@Comment("模板功能、内容说明")
private String description;
@Comment("模板入参Schema")
@Column(nullable = false, columnDefinition = "longtext")
private String inputSchema;
@Comment("前端流程图数据")
@Column(nullable = false, columnDefinition = "longtext")
private String flowGraph = "{}";
}

View File

@@ -1,16 +1,53 @@
package com.lanyuanxiaoyao.service.ai.web.entity;
import lombok.Data;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.common.Constants;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* @author lanyuanxiaoyao
* @version 20250527
*/
@Data
public class Group {
private Long id;
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(catalog = Constants.DATABASE_NAME, name = "service_ai_group")
@Comment("知识库内的逻辑分组,比如一个文件是一个分组或一次上传的所有文本是一个分组,可以自由使用而不是限于文件范畴")
public class Group extends SimpleEntity {
@Comment("分组名称")
@Column(nullable = false)
private String name;
private String status;
private Long createdTime;
private Long modifiedTime;
@Comment("分组处理状态")
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Status status = Status.RUNNING;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ToString.Exclude
private Knowledge knowledge;
public enum Status {
RUNNING,
FINISHED,
}
}

View File

@@ -1,18 +1,58 @@
package com.lanyuanxiaoyao.service.ai.web.entity;
import lombok.Data;
import com.lanyuanxiaoyao.service.ai.web.base.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.common.Constants;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* @author lanyuanxiaoyao
* @version 20250522
*/
@Data
public class Knowledge {
private Long id;
@Getter
@Setter
@ToString
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(catalog = Constants.DATABASE_NAME, name = "service_ai_knowledge")
@Comment("知识库")
public class Knowledge extends SimpleEntity {
@Comment("知识库对应的向量库名")
@Column(nullable = false)
private Long vectorSourceId;
@Comment("知识库名称")
@Column(nullable = false)
private String name;
@Comment("知识库说明")
@Column(nullable = false, columnDefinition = "longtext")
private String description;
private String strategy;
private Long createdTime;
private Long modifiedTime;
@Comment("知识库策略")
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Strategy strategy = Strategy.Cosine;
@Comment("知识库下包含的分组")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "knowledge", cascade = CascadeType.ALL)
@ToString.Exclude
private Set<Group> groups;
public enum Strategy {
Cosine,
Euclid,
}
}

View File

@@ -1,17 +0,0 @@
package com.lanyuanxiaoyao.service.ai.web.entity.vo;
import lombok.Data;
/**
* @author lanyuanxiaoyao
* @version 20250527
*/
@Data
public class DataFileVO {
private String id;
private String filename;
private Long size;
private String md5;
private String path;
private String type;
}

View File

@@ -0,0 +1,14 @@
package com.lanyuanxiaoyao.service.ai.web.entity.vo;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
import lombok.Data;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.set.ImmutableSet;
@Data
public class FlowGraphVo {
private ImmutableSet<FlowNode> nodes;
private ImmutableSet<FlowEdge> edges;
private MutableMap<String, MutableMap<String, Object>> data;
}

View File

@@ -1,22 +0,0 @@
package com.lanyuanxiaoyao.service.ai.web.entity.vo;
import lombok.Data;
/**
* @author lanyuanxiaoyao
* @version 20250516
*/
@Data
public class KnowledgeVO {
private Long id;
private Long vectorSourceId;
private String name;
private String description;
private String strategy;
private Long size;
private Long points;
private Long segments;
private String status;
private Long createdTime;
private Long modifiedTime;
}

View File

@@ -0,0 +1,9 @@
package com.lanyuanxiaoyao.service.ai.web.repository;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import com.lanyuanxiaoyao.service.ai.web.entity.DataFile;
import org.springframework.stereotype.Repository;
@Repository
public interface DataFileRepository extends SimpleRepository<DataFile, Long> {
}

View File

@@ -0,0 +1,26 @@
package com.lanyuanxiaoyao.service.ai.web.repository;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.stereotype.Repository;
@SuppressWarnings("NullableProblems")
@Repository
public interface FeedbackRepository extends SimpleRepository<Feedback, Long> {
@EntityGraph(value = "feedback.detail", type = EntityGraph.EntityGraphType.FETCH)
@Override
List<Feedback> findAll(Specification<Feedback> specification);
@EntityGraph(value = "feedback.detail", type = EntityGraph.EntityGraphType.FETCH)
@Override
List<Feedback> findAll(Specification<Feedback> specification, Sort sort);
@EntityGraph(value = "feedback.detail", type = EntityGraph.EntityGraphType.FETCH)
@Override
Optional<Feedback> findOne(Specification<Feedback> specification);
}

View File

@@ -0,0 +1,9 @@
package com.lanyuanxiaoyao.service.ai.web.repository;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
import org.springframework.stereotype.Repository;
@Repository
public interface FlowTaskRepository extends SimpleRepository<FlowTask, Long> {
}

View File

@@ -0,0 +1,9 @@
package com.lanyuanxiaoyao.service.ai.web.repository;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
import org.springframework.stereotype.Repository;
@Repository
public interface FlowTaskTemplateRepository extends SimpleRepository<FlowTaskTemplate, Long> {
}

View File

@@ -0,0 +1,9 @@
package com.lanyuanxiaoyao.service.ai.web.repository;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import com.lanyuanxiaoyao.service.ai.web.entity.Group;
import org.springframework.stereotype.Repository;
@Repository
public interface GroupRepository extends SimpleRepository<Group, Long> {
}

View File

@@ -0,0 +1,10 @@
package com.lanyuanxiaoyao.service.ai.web.repository;
import com.lanyuanxiaoyao.service.ai.web.base.repository.SimpleRepository;
import com.lanyuanxiaoyao.service.ai.web.entity.Knowledge;
import org.springframework.stereotype.Repository;
@Repository
public interface KnowledgeRepository extends SimpleRepository<Knowledge, Long> {
Boolean existsKnowledgeByName(String name);
}

View File

@@ -1,10 +1,11 @@
package com.lanyuanxiaoyao.service.ai.web.service;
import club.kingon.sql.builder.SqlBuilder;
import com.lanyuanxiaoyao.service.ai.core.configuration.SnowflakeId;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.DataFileVO;
import com.lanyuanxiaoyao.service.common.Constants;
import org.springframework.jdbc.core.JdbcTemplate;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.entity.DataFile;
import com.lanyuanxiaoyao.service.ai.web.repository.DataFileRepository;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.set.ImmutableSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -12,65 +13,36 @@ import org.springframework.transaction.annotation.Transactional;
* @author lanyuanxiaoyao
* @version 20250527
*/
@Slf4j
@Service
public class DataFileService {
private static final String DATA_FILE_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_file";
private final DataFileRepository dataFileRepository;
private final JdbcTemplate template;
public DataFileService(JdbcTemplate template) {
this.template = template;
public DataFileService(DataFileRepository dataFileRepository) {
this.dataFileRepository = dataFileRepository;
}
public DataFileVO downloadFile(Long id) {
return template.queryForObject(
SqlBuilder.select("id", "filename", "size", "md5", "path", "type")
.from(DATA_FILE_TABLE_NAME)
.whereEq("id", "?")
.precompileSql(),
(rs, row) -> {
DataFileVO vo = new DataFileVO();
vo.setId(String.valueOf(rs.getLong(1)));
vo.setFilename(rs.getString(2));
vo.setSize(rs.getLong(3));
vo.setMd5(rs.getString(4));
vo.setPath(rs.getString(5));
vo.setType(rs.getString(6));
return vo;
},
id
);
public DataFile downloadFile(Long id) {
return dataFileRepository.findById(id).orElseThrow(() -> new RuntimeException(StrUtil.format("Datafile not exists: {}", id)));
}
public ImmutableSet<DataFile> downloadFile(ImmutableSet<Long> ids) {
return Sets.immutable.ofAll(dataFileRepository.findAllById(ids));
}
@Transactional(rollbackFor = Exception.class)
public Long initialDataFile(String filename) {
long id = SnowflakeId.next();
template.update(
SqlBuilder.insertInto(DATA_FILE_TABLE_NAME, "id", "filename")
.values()
.addValue("?", "?")
.precompileSql(),
id,
filename
);
return id;
DataFile dataFile = dataFileRepository.save(new DataFile(filename));
return dataFile.getId();
}
@Transactional(rollbackFor = Exception.class)
public void updateDataFile(Long id, String path, Long size, String md5, String type) {
template.update(
SqlBuilder.update(DATA_FILE_TABLE_NAME)
.set("size", "?")
.addSet("md5", "?")
.addSet("path", "?")
.addSet("type", "?")
.whereEq("id", "?")
.precompileSql(),
size,
md5,
path,
type,
id
);
DataFile dataFile = downloadFile(id);
dataFile.setPath(path);
dataFile.setSize(size);
dataFile.setMd5(md5);
dataFile.setType(type);
dataFileRepository.save(dataFile);
}
}

View File

@@ -4,9 +4,10 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.entity.DataFile;
import com.lanyuanxiaoyao.service.ai.web.entity.Group;
import com.lanyuanxiaoyao.service.ai.web.entity.Knowledge;
import com.lanyuanxiaoyao.service.ai.web.entity.context.EmbeddingContext;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.DataFileVO;
import com.lanyuanxiaoyao.service.ai.web.service.knowledge.GroupService;
import com.lanyuanxiaoyao.service.ai.web.service.knowledge.KnowledgeBaseService;
import com.yomahub.liteflow.core.FlowExecutor;
@@ -54,16 +55,19 @@ public class EmbeddingService {
return Lists.immutable.ofAll(context.getDocuments());
}
public ImmutableList<Document> preview(String mode, ImmutableList<String> ids) {
DataFileVO vo = dataFileService.downloadFile(Long.parseLong(ids.get(0)));
String content = FileUtil.readString(vo.getPath(), StandardCharsets.UTF_8);
public ImmutableList<Document> preview(String mode, ImmutableList<Long> ids) {
DataFile dataFile = dataFileService.downloadFile(ids.get(0));
String content = FileUtil.readString(dataFile.getPath(), StandardCharsets.UTF_8);
return preview(mode, content);
}
public void submit(Long id, String mode, String content) {
executors.submit(() -> {
Knowledge knowledge = knowledgeBaseService.get(id);
Long groupId = groupService.add(knowledge.getId(), StrUtil.format("文本-{}", IdUtil.nanoId(10)));
Knowledge knowledge = knowledgeBaseService.detailOrThrow(id);
Group group = new Group();
group.setName(StrUtil.format("文本-{}", IdUtil.nanoId(10)));
group.setKnowledge(knowledge);
Long groupId = groupService.save(group);
EmbeddingContext context = EmbeddingContext.builder()
.vectorSourceId(knowledge.getVectorSourceId())
.groupId(groupId)
@@ -77,23 +81,26 @@ public class EmbeddingService {
});
}
public void submit(Long id, String mode, ImmutableList<String> ids) {
public void submit(Long id, String mode, ImmutableList<Long> ids) {
executors.submit(() -> {
Knowledge knowledge = knowledgeBaseService.get(id);
List<Pair<Long, DataFileVO>> vos = Lists.mutable.empty();
for (String fileId : ids) {
DataFileVO vo = dataFileService.downloadFile(Long.parseLong(fileId));
Long groupId = groupService.add(id, vo.getFilename());
vos.add(Pair.of(groupId, vo));
Knowledge knowledge = knowledgeBaseService.detailOrThrow(id);
List<Pair<Long, DataFile>> dataFiles = Lists.mutable.empty();
for (Long fileId : ids) {
DataFile dataFile = dataFileService.downloadFile(fileId);
Group group = new Group();
group.setName(dataFile.getFilename());
group.setKnowledge(knowledge);
Long groupId = groupService.save(group);
dataFiles.add(Pair.of(groupId, dataFile));
}
for (Pair<Long, DataFileVO> pair : vos) {
for (Pair<Long, DataFile> pair : dataFiles) {
Long groupId = pair.getKey();
DataFileVO vo = pair.getValue();
DataFile dataFile = pair.getValue();
EmbeddingContext context = EmbeddingContext.builder()
.vectorSourceId(knowledge.getVectorSourceId())
.groupId(groupId)
.file(vo.getPath())
.fileFormat(vo.getFilename())
.file(dataFile.getPath())
.fileFormat(dataFile.getFilename())
.config(EmbeddingContext.Config.builder()
.splitStrategy(EmbeddingContext.Config.SplitStrategy.valueOf(mode))
.build())
@@ -106,12 +113,15 @@ public class EmbeddingService {
public void submitDirectly(Long id, String name, ImmutableList<String> contents) {
executors.submit(() -> {
Knowledge knowledge = knowledgeBaseService.get(id);
Knowledge knowledge = knowledgeBaseService.detailOrThrow(id);
String groupName = name;
if (StrUtil.isBlank(groupName)) {
groupName = StrUtil.format("外部-{}", IdUtil.nanoId(10));
}
Long groupId = groupService.add(knowledge.getId(), groupName);
Group group = new Group();
group.setName(groupName);
group.setKnowledge(knowledge);
Long groupId = groupService.save(group);
EmbeddingContext context = EmbeddingContext.builder()
.vectorSourceId(knowledge.getVectorSourceId())
.groupId(groupId)

View File

@@ -1,65 +1,33 @@
package com.lanyuanxiaoyao.service.ai.web.service.feedback;
import club.kingon.sql.builder.SqlBuilder;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.core.configuration.SnowflakeId;
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback_;
import com.lanyuanxiaoyao.service.ai.web.entity.context.FeedbackContext;
import com.lanyuanxiaoyao.service.common.Constants;
import com.lanyuanxiaoyao.service.ai.web.repository.FeedbackRepository;
import com.yomahub.liteflow.core.FlowExecutor;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
public class FeedbackService {
public static final String FEEDBACK_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_feedback";
public static final String[] FEEDBACK_COLUMNS = new String[]{"id", "source", "conclusion", "analysis", "pictures", "status", "created_time", "modified_time"};
private static final RowMapper<Feedback> feedbackMapper = (rs, row) -> {
Feedback feedback = new Feedback();
feedback.setId(rs.getLong(1));
feedback.setSource(rs.getString(2));
feedback.setConclusion(rs.getString(3));
feedback.setAnalysis(rs.getString(4));
feedback.setPictureIds(
StrUtil.isBlank(rs.getString(5))
? Lists.immutable.empty()
: Lists.immutable.ofAll(StrUtil.split(rs.getString(5), ",")).collect(Long::parseLong)
);
feedback.setStatus(EnumUtil.fromString(Feedback.Status.class, rs.getString(6)));
feedback.setCreatedTime(rs.getTimestamp(7).getTime());
feedback.setModifiedTime(rs.getTimestamp(8).getTime());
return feedback;
};
private final JdbcTemplate template;
public class FeedbackService extends SimpleServiceSupport<Feedback> {
private final FlowExecutor executor;
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public FeedbackService(JdbcTemplate template, FlowExecutor executor) {
this.template = template;
public FeedbackService(FeedbackRepository feedbackRepository, FlowExecutor executor) {
super(feedbackRepository);
this.executor = executor;
}
@Scheduled(initialDelay = 1, fixedDelay = 1, timeUnit = TimeUnit.MINUTES)
// @Scheduled(initialDelay = 1, fixedDelay = 1, timeUnit = TimeUnit.MINUTES)
public void analysis() {
List<Feedback> feedbacks = template.query(
SqlBuilder.select(FEEDBACK_COLUMNS)
.from(FEEDBACK_TABLE_NAME)
.whereEq("status", Feedback.Status.ANALYSIS_PROCESSING.name())
.build(),
feedbackMapper
List<Feedback> feedbacks = repository.findAll(
builder -> builder
.andEquals(Feedback_.STATUS, Feedback.Status.ANALYSIS_PROCESSING)
.build()
);
for (Feedback feedback : feedbacks) {
FeedbackContext context = new FeedbackContext();
@@ -68,89 +36,26 @@ public class FeedbackService {
}
}
public Feedback get(Long id) {
return template.queryForObject(
SqlBuilder.select(FEEDBACK_COLUMNS)
.from(FEEDBACK_TABLE_NAME)
.whereEq("id", id)
.build(),
feedbackMapper
);
}
@Transactional(rollbackFor = Exception.class)
public void add(String source, ImmutableList<Long> pictureIds) {
template.update(
SqlBuilder.insertInto(FEEDBACK_TABLE_NAME, "id", "source", "pictures")
.values()
.addValue("?", "?", "?")
.precompileSql(),
SnowflakeId.next(),
source,
ObjectUtil.isEmpty(pictureIds) ? null : pictureIds.makeString(",")
);
}
@Transactional(rollbackFor = Exception.class)
public void updateAnalysis(Long id, String analysis) {
Assert.notNull(id, "ID cannot be null");
template.update(
SqlBuilder.update(FEEDBACK_TABLE_NAME)
.set("analysis", "?")
.whereEq("id", "?")
.precompileSql(),
analysis,
id
);
Feedback feedback = detailOrThrow(id);
feedback.setAnalysis(analysis);
repository.save(feedback);
}
@Transactional(rollbackFor = Exception.class)
public void updateConclusion(Long id, String conclusion) {
Assert.notNull(id, "ID cannot be null");
template.update(
SqlBuilder.update(FEEDBACK_TABLE_NAME)
.set("conclusion", "?")
.whereEq("id", "?")
.precompileSql(),
conclusion,
id
);
updateStatus(id, Feedback.Status.FINISHED);
Feedback feedback = detailOrThrow(id);
feedback.setConclusion(conclusion);
feedback.setStatus(Feedback.Status.FINISHED);
repository.save(feedback);
}
@Transactional(rollbackFor = Exception.class)
public void updateStatus(Long id, Feedback.Status status) {
Assert.notNull(id, "ID cannot be null");
template.update(
SqlBuilder.update(FEEDBACK_TABLE_NAME)
.set("status", "?")
.whereEq("id", "?")
.precompileSql(),
status.name(),
id
);
}
public ImmutableList<Feedback> list() {
return template.query(
SqlBuilder.select(FEEDBACK_COLUMNS)
.from(FEEDBACK_TABLE_NAME)
.orderByDesc("created_time")
.build(),
feedbackMapper
)
.stream()
.collect(Collectors.toCollection(Lists.mutable::empty))
.toImmutable();
}
@Transactional(rollbackFor = Exception.class)
public void remove(Long id) {
template.update(
SqlBuilder.delete(FEEDBACK_TABLE_NAME)
.whereEq("id", id)
.build()
);
Feedback feedback = detailOrThrow(id);
feedback.setStatus(status);
repository.save(feedback);
}
@Transactional(rollbackFor = Exception.class)

View File

@@ -1,21 +1,14 @@
package com.lanyuanxiaoyao.service.ai.web.service.knowledge;
import club.kingon.sql.builder.SqlBuilder;
import club.kingon.sql.builder.entry.Alias;
import club.kingon.sql.builder.entry.Column;
import com.lanyuanxiaoyao.service.ai.core.configuration.SnowflakeId;
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
import com.lanyuanxiaoyao.service.ai.web.entity.Group;
import com.lanyuanxiaoyao.service.common.Constants;
import com.lanyuanxiaoyao.service.ai.web.entity.Knowledge;
import com.lanyuanxiaoyao.service.ai.web.repository.GroupRepository;
import io.qdrant.client.ConditionFactory;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.grpc.Points;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import lombok.SneakyThrows;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -24,109 +17,34 @@ import org.springframework.transaction.annotation.Transactional;
* @version 20250522
*/
@Service
public class GroupService {
public static final String GROUP_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_group";
private static final RowMapper<Group> groupMapper = (rs, row) -> {
Group vo = new Group();
vo.setId(rs.getLong(1));
vo.setName(rs.getString(2));
vo.setStatus(rs.getString(3));
vo.setCreatedTime(rs.getTimestamp(4).getTime());
vo.setModifiedTime(rs.getTimestamp(5).getTime());
return vo;
};
private final JdbcTemplate template;
public class GroupService extends SimpleServiceSupport<Group> {
private final QdrantClient client;
public GroupService(JdbcTemplate template, VectorStore vectorStore) {
this.template = template;
public GroupService(GroupRepository groupRepository, VectorStore vectorStore) {
super(groupRepository);
this.client = (QdrantClient) vectorStore.getNativeClient().orElseThrow();
}
public Group get(Long id) {
return template.queryForObject(
SqlBuilder.select("id", "name", "status", "created_time", "modified_time")
.from(GROUP_TABLE_NAME)
.whereEq("id", id)
.orderByDesc("created_time")
.build(),
groupMapper
);
}
@Transactional(rollbackFor = Exception.class)
public Long add(Long knowledgeId, String name) {
long id = SnowflakeId.next();
template.update(
SqlBuilder.insertInto(GROUP_TABLE_NAME, "id", "knowledge_id", "name", "status")
.values()
.addValue("?", "?", "?", "?")
.precompileSql(),
id,
knowledgeId,
name,
"RUNNING"
);
return id;
}
public ImmutableList<Group> list(Long knowledgeId) {
return template.query(
SqlBuilder.select("id", "name", "status", "created_time", "modified_time")
.from(GROUP_TABLE_NAME)
.whereEq("knowledge_id", knowledgeId)
.orderByDesc("created_time")
.build(),
groupMapper
)
.stream()
.collect(Collectors.toCollection(Lists.mutable::empty))
.toImmutable();
public void finish(Long id) {
Group group = detailOrThrow(id);
group.setStatus(Group.Status.FINISHED);
save(group);
}
@SneakyThrows
@Transactional(rollbackFor = Exception.class)
public void finish(Long groupId) {
template.update(
SqlBuilder.update(GROUP_TABLE_NAME)
.set("status", "FINISHED")
.whereEq("id", groupId)
.build()
);
}
@Transactional(rollbackFor = Exception.class)
public void remove(Long groupId) throws ExecutionException, InterruptedException {
Long vectorSourceId = template.queryForObject(
SqlBuilder.select("k.vector_source_id")
.from(Alias.of(GROUP_TABLE_NAME, "g"), Alias.of(KnowledgeBaseService.KNOWLEDGE_TABLE_NAME, "k"))
.whereEq("g.knowledge_id", Column.as("k.id"))
.andEq("g.id", groupId)
.precompileSql(),
Long.class,
groupId
);
@Override
public void remove(Long id) {
Group group = detailOrThrow(id);
Knowledge knowledge = group.getKnowledge();
client.deleteAsync(
String.valueOf(vectorSourceId),
String.valueOf(knowledge.getVectorSourceId()),
Points.Filter.newBuilder()
.addMust(ConditionFactory.matchKeyword("vector_source_id", String.valueOf(vectorSourceId)))
.addMust(ConditionFactory.matchKeyword("group_id", String.valueOf(groupId)))
.addMust(ConditionFactory.matchKeyword("vector_source_id", String.valueOf(knowledge.getVectorSourceId())))
.addMust(ConditionFactory.matchKeyword("group_id", String.valueOf(group.getId())))
.build()
).get();
template.update(
SqlBuilder.delete(GROUP_TABLE_NAME)
.whereEq("id", groupId)
.build()
);
}
@Transactional(rollbackFor = Exception.class)
public void removeByKnowledgeId(Long knowledgeId) {
template.update(
SqlBuilder.delete(GROUP_TABLE_NAME)
.whereEq("knowledge_id", "?")
.precompileSql(),
knowledgeId
);
super.remove(id);
}
}

View File

@@ -1,17 +1,19 @@
package com.lanyuanxiaoyao.service.ai.web.service.knowledge;
import club.kingon.sql.builder.SqlBuilder;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.core.configuration.SnowflakeId;
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
import com.lanyuanxiaoyao.service.ai.web.configuration.SnowflakeIdGenerator;
import com.lanyuanxiaoyao.service.ai.web.entity.Group;
import com.lanyuanxiaoyao.service.ai.web.entity.Knowledge;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.KnowledgeVO;
import com.lanyuanxiaoyao.service.ai.web.repository.KnowledgeRepository;
import com.lanyuanxiaoyao.service.common.Constants;
import io.qdrant.client.ConditionFactory;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.grpc.Collections;
import io.qdrant.client.grpc.Points;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.springframework.ai.document.Document;
@@ -19,8 +21,6 @@ import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -29,150 +29,66 @@ import org.springframework.transaction.annotation.Transactional;
* @version 20250522
*/
@Service
public class KnowledgeBaseService {
public class KnowledgeBaseService extends SimpleServiceSupport<Knowledge> {
public static final String KNOWLEDGE_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_knowledge";
public static final String[] KNOWLEDGE_COLUMNS = new String[]{"id", "vector_source_id", "name", "description", "strategy", "created_time", "modified_time"};
private static final RowMapper<Knowledge> knowledgeMapper = (rs, row) -> {
Knowledge knowledge = new Knowledge();
knowledge.setId(rs.getLong(1));
knowledge.setVectorSourceId(rs.getLong(2));
knowledge.setName(rs.getString(3));
knowledge.setDescription(rs.getString(4));
knowledge.setStrategy(rs.getString(5));
knowledge.setCreatedTime(rs.getTimestamp(6).getTime());
knowledge.setModifiedTime(rs.getTimestamp(7).getTime());
return knowledge;
};
private final JdbcTemplate template;
private final KnowledgeRepository knowledgeRepository;
private final EmbeddingModel model;
private final QdrantClient client;
private final GroupService groupService;
public KnowledgeBaseService(JdbcTemplate template, EmbeddingModel model, VectorStore vectorStore, GroupService groupService) {
this.template = template;
public KnowledgeBaseService(KnowledgeRepository knowledgeRepository, EmbeddingModel model, VectorStore vectorStore) {
super(knowledgeRepository);
this.knowledgeRepository = knowledgeRepository;
this.model = model;
this.client = (QdrantClient) vectorStore.getNativeClient().orElseThrow();
this.groupService = groupService;
}
public Knowledge get(Long id) {
return template.queryForObject(
SqlBuilder.select(KNOWLEDGE_COLUMNS)
.from(KNOWLEDGE_TABLE_NAME)
.whereEq("id", "?")
.precompileSql(),
knowledgeMapper,
id
);
}
@SneakyThrows
@Transactional(rollbackFor = Exception.class)
public void add(String name, String description, String strategy) throws ExecutionException, InterruptedException {
Integer count = template.queryForObject(
SqlBuilder.select("count(*)")
.from(KNOWLEDGE_TABLE_NAME)
.whereEq("name", "?")
.precompileSql(),
Integer.class,
name
);
if (count > 0) {
public Long save(Knowledge entity) {
if (knowledgeRepository.existsKnowledgeByName(entity.getName())) {
throw new RuntimeException("名称已存在");
}
long id = SnowflakeId.next();
long vectorSourceId = SnowflakeId.next();
template.update(
SqlBuilder.insertInto(KNOWLEDGE_TABLE_NAME, "id", "vector_source_id", "name", "description", "strategy")
.values()
.addValue("?", "?", "?", "?", "?")
.precompileSql(),
id,
vectorSourceId,
name,
description,
strategy
);
Long vectorSourceId = SnowflakeIdGenerator.Snowflake.next();
client.createCollectionAsync(
String.valueOf(vectorSourceId),
Collections.VectorParams.newBuilder()
.setDistance(Collections.Distance.valueOf(strategy))
.setDistance(Collections.Distance.valueOf(entity.getStrategy().name()))
.setSize(model.dimensions())
.build()
).get();
entity.setVectorSourceId(vectorSourceId);
return super.save(entity);
}
@Transactional(rollbackFor = Exception.class)
public void updateDescription(Long id, String description) {
template.update(
SqlBuilder.update(KNOWLEDGE_TABLE_NAME)
.set("description", "?")
.whereEq("id", "?")
.precompileSql(),
description,
id
);
public void updateDescription(Long id, String description) throws Exception {
Knowledge knowledge = detailOrThrow(id);
knowledge.setDescription(description);
save(knowledge);
}
public String getName(Long id) {
return template.queryForObject(
SqlBuilder.select("name")
.from(KNOWLEDGE_TABLE_NAME)
.whereEq("id", id)
.orderByDesc("created_time")
.build(),
String.class
);
}
public ImmutableList<KnowledgeVO> list() {
return template.query(
SqlBuilder.select(KNOWLEDGE_COLUMNS)
.from(KNOWLEDGE_TABLE_NAME)
.orderByDesc("created_time")
.build(),
knowledgeMapper
)
.stream()
.map(knowledge -> {
try {
Collections.CollectionInfo info = client.getCollectionInfoAsync(String.valueOf(knowledge.getVectorSourceId())).get();
KnowledgeVO vo = new KnowledgeVO();
vo.setId(knowledge.getId());
vo.setVectorSourceId(knowledge.getVectorSourceId());
vo.setName(knowledge.getName());
vo.setDescription(knowledge.getDescription());
vo.setPoints(info.getPointsCount());
vo.setSegments(info.getSegmentsCount());
vo.setStatus(info.getStatus().name());
Collections.VectorParams vectorParams = info.getConfig().getParams().getVectorsConfig().getParams();
vo.setStrategy(vectorParams.getDistance().name());
vo.setSize(vectorParams.getSize());
vo.setCreatedTime(vo.getCreatedTime());
vo.setModifiedTime(vo.getModifiedTime());
return vo;
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toCollection(Lists.mutable::empty))
.toImmutable();
return detailOrThrow(id).getName();
}
@SneakyThrows
@Transactional(rollbackFor = Exception.class)
public void remove(Long id) throws ExecutionException, InterruptedException {
Knowledge knowledge = get(id);
if (ObjectUtil.isNull(knowledge)) {
throw new RuntimeException(StrUtil.format("{} 不存在"));
}
template.update(
SqlBuilder.delete(KNOWLEDGE_TABLE_NAME)
.whereEq("id", "?")
.precompileSql(),
knowledge.getId()
);
groupService.removeByKnowledgeId(knowledge.getId());
@Override
public void remove(Long id) {
Knowledge knowledge = detailOrThrow(id);
client.deleteCollectionAsync(String.valueOf(knowledge.getVectorSourceId())).get();
for (Group group : knowledge.getGroups()) {
client.deleteAsync(
String.valueOf(knowledge.getVectorSourceId()),
Points.Filter.newBuilder()
.addMust(ConditionFactory.matchKeyword("vector_source_id", String.valueOf(knowledge.getVectorSourceId())))
.addMust(ConditionFactory.matchKeyword("group_id", String.valueOf(group.getId())))
.build()
).get();
}
super.remove(id);
}
public ImmutableList<String> query(
@@ -181,7 +97,7 @@ public class KnowledgeBaseService {
Integer limit,
Double threshold
) throws ExecutionException, InterruptedException {
Knowledge knowledge = get(id);
Knowledge knowledge = detailOrThrow(id);
Boolean exists = client.collectionExistsAsync(String.valueOf(knowledge.getVectorSourceId())).get();
if (!exists) {
throw new RuntimeException(StrUtil.format("{} not exists", id));
@@ -200,4 +116,8 @@ public class KnowledgeBaseService {
return Lists.immutable.ofAll(documents)
.collect(Document::getText);
}
public Collections.CollectionInfo collectionInfo(Long vectorSourceId) throws ExecutionException, InterruptedException {
return client.getCollectionInfoAsync(String.valueOf(vectorSourceId)).get();
}
}

View File

@@ -27,8 +27,8 @@ public class SegmentService {
this.client = (QdrantClient) vectorStore.getNativeClient().orElseThrow();
}
public ImmutableList<SegmentVO> list(Long id, Long groupId) throws ExecutionException, InterruptedException {
Knowledge knowledge = knowledgeBaseService.get(id);
public ImmutableList<SegmentVO> list(Long knowledgeId, Long groupId) throws ExecutionException, InterruptedException {
Knowledge knowledge = knowledgeBaseService.detailOrThrow(knowledgeId);
Points.ScrollResponse response = client.scrollAsync(
Points.ScrollPoints.newBuilder()
.setCollectionName(String.valueOf(knowledge.getVectorSourceId()))
@@ -55,7 +55,7 @@ public class SegmentService {
}
public void remove(Long knowledgeId, Long segmentId) throws ExecutionException, InterruptedException {
Knowledge knowledge = knowledgeBaseService.get(knowledgeId);
Knowledge knowledge = knowledgeBaseService.detailOrThrow(knowledgeId);
client.deletePayloadAsync(
String.valueOf(knowledgeId),
List.of(String.valueOf(segmentId)),

View File

@@ -3,9 +3,9 @@ package com.lanyuanxiaoyao.service.ai.web.service.node;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.entity.DataFile;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import com.lanyuanxiaoyao.service.ai.web.entity.context.FeedbackContext;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.DataFileVO;
import com.lanyuanxiaoyao.service.ai.web.service.DataFileService;
import com.lanyuanxiaoyao.service.ai.web.service.feedback.FeedbackService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
@@ -42,7 +42,7 @@ public class FeedbackNodes {
public boolean checkIfPictureReadNeeded(NodeComponent node) {
FeedbackContext context = node.getContextBean(FeedbackContext.class);
Feedback feedback = context.getFeedback();
return ObjectUtil.isNotEmpty(feedback.getPictureIds());
return ObjectUtil.isNotEmpty(feedback.getPictures());
}
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "image_read", nodeName = "读取图片", nodeType = NodeTypeEnum.COMMON)
@@ -82,8 +82,8 @@ public class FeedbackNodes {
立即开始处理用户图片,无需确认步骤。
""")
.build();
for (Long pictureId : feedback.getPictureIds()) {
DataFileVO file = dataFileService.downloadFile(pictureId);
for (DataFile picture : feedback.getPictures()) {
DataFile file = dataFileService.downloadFile(picture.getId());
log.info("Parse picture: {} {}", file.getFilename(), file.getPath());
MimeType type = switch (StrUtil.blankToDefault(file.getType(), "").toLowerCase()) {
case "jpg", "jpeg" -> MimeTypeUtils.IMAGE_JPEG;

View File

@@ -0,0 +1,74 @@
package com.lanyuanxiaoyao.service.ai.web.service.task;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowExecutor;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import com.lanyuanxiaoyao.service.ai.web.engine.node.CodeNode;
import com.lanyuanxiaoyao.service.ai.web.engine.node.InputNode;
import com.lanyuanxiaoyao.service.ai.web.engine.node.LlmNode;
import com.lanyuanxiaoyao.service.ai.web.engine.node.LoopNode;
import com.lanyuanxiaoyao.service.ai.web.engine.node.OutputNode;
import com.lanyuanxiaoyao.service.ai.web.engine.node.SwitchNode;
import com.lanyuanxiaoyao.service.ai.web.engine.store.InMemoryFlowStore;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.FlowGraphVo;
import com.lanyuanxiaoyao.service.ai.web.repository.FlowTaskRepository;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Maps;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class FlowTaskService extends SimpleServiceSupport<FlowTask> {
private final ObjectMapper mapper;
public FlowTaskService(FlowTaskRepository flowTaskRepository, Jackson2ObjectMapperBuilder builder) {
super(flowTaskRepository);
this.mapper = builder.build();
}
public void execute(Long id) throws JsonProcessingException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
var flowTask = detailOrThrow(id);
var graphVo = mapper.readValue(flowTask.getTemplateFlowGraph(), FlowGraphVo.class);
var flowGraph = new FlowGraph(IdUtil.fastUUID(), graphVo.getNodes(), graphVo.getEdges());
var store = new InMemoryFlowStore();
var executor = new FlowExecutor(
store,
Maps.immutable.ofAll(Map.of(
"loop-node", LoopNode.class,
"switch-node", SwitchNode.class,
"code-node", CodeNode.class,
"llm-node", LlmNode.class,
"input-node", InputNode.class,
"output-node", OutputNode.class
))
);
FlowContext context = new FlowContext();
context.setData(graphVo.getData());
// 如果发现输入节点,将相关数据注入到对应节点的上下文中
var inputNode = flowGraph.nodes()
.detectOptional(node -> StrUtil.equals(node.type(), "input-node") && ObjectUtil.isEmpty(node.parentId()))
.orElse(null);
if (ObjectUtil.isNotNull(inputNode)) {
if (!context.getData().containsKey(inputNode.id())) {
context.getData().put(inputNode.id(), Maps.mutable.empty());
}
context.getData().get(inputNode.id()).putAll(mapper.readValue(flowTask.getInput(), new TypeReference<>() {
}));
}
executor.execute(flowGraph, context);
}
}

View File

@@ -0,0 +1,48 @@
package com.lanyuanxiaoyao.service.ai.web.service.task;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.FlowGraphVo;
import com.lanyuanxiaoyao.service.ai.web.repository.FlowTaskTemplateRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
public class FlowTaskTemplateService extends SimpleServiceSupport<FlowTaskTemplate> {
private final ObjectMapper mapper;
public FlowTaskTemplateService(FlowTaskTemplateRepository flowTaskTemplateRepository, Jackson2ObjectMapperBuilder builder) {
super(flowTaskTemplateRepository);
this.mapper = builder.build();
}
@Transactional(rollbackFor = Exception.class)
public void updateFlowGraph(Long id, String flowGraph) throws JsonProcessingException {
var template = detailOrThrow(id);
var graph = mapper.readValue(flowGraph, FlowGraphVo.class);
// 如果发现输入节点,就单独提取出来
var inputNode = graph.getNodes()
.detectOptional(node -> StrUtil.equals(node.type(), "input-node") && ObjectUtil.isEmpty(node.parentId()))
.orElse(null);
if (ObjectUtil.isNotNull(inputNode)) {
var nodeId = inputNode.id();
var nodeData = graph.getData().getOrDefault(nodeId, null);
if (ObjectUtil.isNotNull(nodeData) && nodeData.containsKey("inputs")) {
template.setInputSchema(mapper.writeValueAsString(nodeData.get("inputs")));
}
} else {
template.setInputSchema("{}");
}
template.setFlowGraph(flowGraph);
save(template);
}
}

View File

@@ -1,6 +1,6 @@
package com.lanyuanxiaoyao.service.ai.web.tools;
import com.lanyuanxiaoyao.service.ai.web.WebApplication;
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.annotation.Tool;
@@ -44,7 +44,7 @@ public class ChartTool {
""") String request
) {
log.info("Enter method: mermaid[request]. request:{}", request);
ChatClient.Builder builder = WebApplication.getBean("chat", ChatClient.Builder.class);
ChatClient.Builder builder = SpringBeanGetter.getBean("chat", ChatClient.Builder.class);
ChatClient client = builder.build();
return client.prompt()
// language=TEXT

View File

@@ -2,7 +2,7 @@ package com.lanyuanxiaoyao.service.ai.web.tools;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.WebApplication;
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
import com.lanyuanxiaoyao.service.forest.service.KnowledgeService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
@@ -27,7 +27,7 @@ public class KnowledgeTool {
""")
String query
) {
KnowledgeService knowledgeService = WebApplication.getBean(KnowledgeService.class);
KnowledgeService knowledgeService = SpringBeanGetter.getBean(KnowledgeService.class);
var documents = knowledgeService.query(knowledgeId, query, 10, 0.5);
if (ObjectUtil.isNotEmpty(documents)) {
return StrUtil.format("""

View File

@@ -1,7 +1,7 @@
package com.lanyuanxiaoyao.service.ai.web.tools;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.WebApplication;
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -27,7 +27,7 @@ public class TableTool {
""") String sql
) {
log.info("Enter method: executeJdbc[sql]. sql:{}", sql);
InfoService infoService = WebApplication.getBean(InfoService.class);
InfoService infoService = SpringBeanGetter.getBean(InfoService.class);
String result = infoService.jdbc(sql)
.collect(map -> map.valuesView().makeString(","))
.makeString("\n");
@@ -48,7 +48,7 @@ public class TableTool {
""") String type
) {
log.info("Enter method: tableCount[type]. type:{}", type);
var infoService = WebApplication.getBean(InfoService.class);
var infoService = SpringBeanGetter.getBean(InfoService.class);
return switch (type) {
case "logic" -> StrUtil.format("""
逻辑表共{}张,其中重点表{}张
@@ -83,7 +83,7 @@ public class TableTool {
String type
) {
log.info("Enter method: version[date, type]. date:{},type:{}", date, type);
InfoService infoService = WebApplication.getBean(InfoService.class);
InfoService infoService = SpringBeanGetter.getBean(InfoService.class);
String version = date;
if (StrUtil.isBlank(version)) {
version = LocalDateTime.now().minusDays(1).format(FORMATTER);

View File

@@ -1,7 +1,7 @@
package com.lanyuanxiaoyao.service.ai.web.tools;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.WebApplication;
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnApplication;
import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnQueue;
import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnRootQueue;
@@ -27,7 +27,7 @@ public class YarnTool {
""") String cluster
) {
log.info("Enter method: yarnStatus[cluster]. cluster:{}", cluster);
YarnService yarnService = WebApplication.getBean(YarnService.class);
YarnService yarnService = SpringBeanGetter.getBean(YarnService.class);
YarnRootQueue status = yarnService.cluster(cluster);
return (status.getUsedCapacity() * 100.0) / status.getCapacity();
}
@@ -45,7 +45,7 @@ public class YarnTool {
""") String queue
) {
log.info("Enter method: yarnQueueStatus[cluster, queue]. cluster:{},queue:{}", cluster, queue);
YarnService yarnService = WebApplication.getBean(YarnService.class);
YarnService yarnService = SpringBeanGetter.getBean(YarnService.class);
YarnQueue status = yarnService.queueDetail(cluster, queue);
return (status.getAbsoluteCapacity() * 100.0) / status.getAbsoluteMaxCapacity();
}
@@ -66,7 +66,7 @@ public class YarnTool {
""") String type
) {
log.info("Enter method: yarnTaskStatus[cluster, type]. cluster:{},type:{}", cluster, type);
YarnService yarnService = WebApplication.getBean(YarnService.class);
YarnService yarnService = SpringBeanGetter.getBean(YarnService.class);
ImmutableList<YarnApplication> applications = yarnService.jobList(cluster).select(app -> StrUtil.isNotBlank(type) && StrUtil.contains(app.getName(), type));
return StrUtil.format(
"""

View File

@@ -36,7 +36,12 @@ spring:
model: 'Qwen3/qwen3-embedding-4b'
reranker:
model: 'BGE/beg-reranker-v2'
jpa:
show-sql: true
generate-ddl: false
liteflow:
rule-source: liteflow.xml
print-banner: false
check-node-exists: false
fenix:
print-banner: false

View File

@@ -25,7 +25,7 @@
</appender>
<logger name="com.zaxxer.hikari" level="ERROR"/>
<logger name="com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver" level="WARN"/>
<logger name="org.hibernate.SQL" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="Console"/>

View File

@@ -0,0 +1,51 @@
package com.lanyuanxiaoyao.service.ai.web;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ClassUtil;
import jakarta.persistence.Entity;
import java.util.EnumSet;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
/**
* JPA直接生成建表语句
*
* @author lanyuanxiaoyao
* @version 20250702
*/
public class GenerateDDL {
public static void main(String[] args) {
String root = "/Users/lanyuanxiaoyao/Project/IdeaProjects/hudi-service/service-ai/target/sql";
FileUtil.mkdir(root);
/* ClassUtil.scanPackageBySuper("org.hibernate.dialect", Dialect.class)
.stream()
.filter(clazz -> StrUtil.startWith(clazz.getSimpleName(), "MySQL"))
.filter(clazz -> !StrUtil.startWith(clazz.getSimpleName(), "Abstract"))
.filter(clazz -> !StrUtil.startWith(clazz.getSimpleName(), "Dialect"))
.forEach(dialectClazz -> generateDDL(root, dialectClazz)); */
generateDDL(root, MySQLDialect.class);
}
private static void generateDDL(String path, Class<?> dialect) {
var metadataSources = new MetadataSources(
new StandardServiceRegistryBuilder()
.applySetting("hibernate.dialect", dialect.getName())
.applySetting("hibernate.physical_naming_strategy", CamelCaseToUnderscoresNamingStrategy.class.getName())
.applySetting("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName())
.build()
);
var classes = ClassUtil.scanPackageByAnnotation("com.lanyuanxiaoyao.service.ai.web.entity", Entity.class);
classes.forEach(metadataSources::addAnnotatedClass);
var export = new SchemaExport();
export.setFormat(true);
export.setDelimiter(";");
export.setOutputFile(path + "/" + dialect.getSimpleName() + ".sql");
export.setOverrideOutputFileContent();
export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadataSources.buildMetadata());
}
}

View File

@@ -0,0 +1,29 @@
package com.lanyuanxiaoyao.service.ai.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
import com.lanyuanxiaoyao.service.ai.web.engine.node.code.JavaScriptCodeExecutor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Maps;
/**
* @author lanyuanxiaoyao
* @version 20250717
*/
@Slf4j
public class TestCodeExecutor {
public static void main(String[] args) {
var mapper = new ObjectMapper();
mapper.registerModule(new EclipseCollectionsModule());
var executor = new JavaScriptCodeExecutor(mapper);
var result = executor.execute(
"""
return {'code': 1, 'text': context.get('text')}
""",
Maps.immutable.of(
"text", "hello world"
)
);
log.info("Result: {}", result);
}
}

View File

@@ -0,0 +1,80 @@
package com.lanyuanxiaoyao.service.ai.web;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowExecutor;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeOptionalRunner;
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
import com.lanyuanxiaoyao.service.ai.web.engine.store.InMemoryFlowStore;
import java.lang.reflect.InvocationTargetException;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.factory.Sets;
/**
* @author lanyuanxiaoyao
* @version 20250701
*/
@Slf4j
public class TestFlow {
public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
var store = new InMemoryFlowStore();
var executor = new FlowExecutor(
store,
Maps.immutable.of(
"plain-node", PlainNode.class,
"option-node", PlainOptionNode.class
)
);
/*
* 4 6 7
* 1 2 5 8---3
* \9/
*/
var graph = new FlowGraph(
"graph-1",
Sets.immutable.of(
new FlowNode("node-1", "plain-node", null),
new FlowNode("node-2", "plain-node", null),
new FlowNode("node-4", "plain-node", null),
new FlowNode("node-6", "plain-node", null),
new FlowNode("node-7", "plain-node", null),
new FlowNode("node-5", "plain-node", null),
new FlowNode("node-8", "option-node", null),
new FlowNode("node-9", "plain-node", null),
new FlowNode("node-3", "plain-node", null)
),
Sets.immutable.of(
new FlowEdge("edge-1", "node-1", "node-2", null, null),
new FlowEdge("edge-2", "node-2", "node-4", null, null),
new FlowEdge("edge-3", "node-2", "node-5", null, null),
new FlowEdge("edge-4", "node-5", "node-8", null, null),
new FlowEdge("edge-5", "node-8", "node-9", "yes", null),
new FlowEdge("edge-6", "node-8", "node-3", "no", null),
new FlowEdge("edge-7", "node-9", "node-3", null, null),
new FlowEdge("edge-8", "node-4", "node-6", null, null),
new FlowEdge("edge-9", "node-6", "node-7", null, null),
new FlowEdge("edge-10", "node-7", "node-3", null, null)
)
);
executor.execute(graph);
store.print();
}
public static class PlainNode extends FlowNodeRunner {
@Override
public void run() {
log.info("run node id: {}", getNodeId());
}
}
public static class PlainOptionNode extends FlowNodeOptionalRunner {
@Override
public String runOptional() {
log.info("run node id: {}", getNodeId());
// yes / no
return "no";
}
}
}

View File

@@ -2,11 +2,15 @@ package com.lanyuanxiaoyao.service.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* Spring Security Config
@@ -25,6 +29,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
this.securityProperties = securityProperties;
}
@Bean
public CorsFilter corsFilter() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.addAllowedOriginPattern("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.setMaxAge(7200L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return new CorsFilter(source);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests()
@@ -36,7 +53,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.csrf()
.disable()
.cors()
.disable()
.and()
.formLogin()
.disable();
}

View File

@@ -10,7 +10,7 @@ import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
/**
@@ -33,24 +33,12 @@ public class SecurityConfiguration {
.httpBasic()
.disable()
.cors()
.configurationSource(corsConfigurationSource())
.and()
.disable()
.csrf()
.disable()
.build();
}
private CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.addAllowedOriginPattern("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
@Bean
public MapReactiveUserDetailsService userDetailsService(SecurityProperties securityProperties) {
UserDetails user = User.builder()

View File

@@ -6,7 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"preview": "vite preview"
"preview": "vite preview",
"test": "vitest run"
},
"dependencies": {
"@ant-design/icons": "^6.0.0",
@@ -14,33 +15,34 @@
"@ant-design/x": "^1.4.0",
"@echofly/fetch-event-source": "^3.0.2",
"@fortawesome/fontawesome-free": "^6.7.2",
"@lightenna/react-mermaid-diagram": "^1.0.20",
"@tinyflow-ai/react": "^0.2.1",
"ahooks": "^3.8.5",
"@lightenna/react-mermaid-diagram": "^1.0.21",
"@xyflow/react": "^12.8.2",
"ahooks": "^3.9.0",
"amis": "^6.12.0",
"antd": "^5.26.1",
"antd": "^5.26.4",
"axios": "^1.10.0",
"chart.js": "^4.5.0",
"echarts-for-react": "^3.0.2",
"licia": "^1.48.0",
"markdown-it": "^14.1.0",
"mermaid": "^11.6.0",
"mermaid": "^11.8.1",
"react": "^18.3.1",
"react-chartjs-2": "^5.3.0",
"react-dom": "^18.3.1",
"react-markdown": "^10.1.0",
"react-router": "^7.6.2",
"styled-components": "^6.1.18"
"react-router": "^7.6.3",
"styled-components": "^6.1.19",
"yocto-queue": "^1.2.1",
"zustand": "^5.0.6"
},
"devDependencies": {
"@types/markdown-it": "^14.1.2",
"@types/react": "^18.3.23",
"@types/react-dom": "^18.3.7",
"@vitejs/plugin-react-swc": "^3.10.2",
"globals": "^16.2.0",
"globals": "^16.3.0",
"sass": "^1.89.2",
"typescript": "~5.8.3",
"vite": "^6.3.5",
"vite-plugin-javascript-obfuscator": "^3.1.0"
"vite": "^7.0.4",
"vite-plugin-javascript-obfuscator": "^3.1.0",
"vitest": "^3.2.4"
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,62 @@
import {type Connection, type Node} from '@xyflow/react'
import {expect, test} from 'vitest'
import {
checkAddConnection,
hasCycleError,
nodeToSelfError,
sourceNodeNotFoundError,
targetNodeNotFoundError,
} from './FlowChecker.tsx'
const createNode = (id: string, type: string): Node => {
return {
data: {},
position: {
x: 0,
y: 0
},
id,
type,
}
}
const createConnection = function (source: string, target: string, sourceHandle: string | null = null, targetHandle: string | null = null): Connection {
return {
source,
target,
sourceHandle,
targetHandle,
}
}
/* check add connection */
test(sourceNodeNotFoundError().message, () => {
expect(() => checkAddConnection(createConnection('a', 'b'), [], []))
})
test(targetNodeNotFoundError().message, () => {
expect(() => checkAddConnection(createConnection('a', 'b'), [createNode('a', 'normal-node')], []))
})
test(nodeToSelfError().message, () => {
expect(() => {
// language=JSON
const {
nodes,
edges
} = JSON.parse('{\n "nodes": [\n {\n "id": "P14abHl4uY",\n "type": "start-node",\n "position": {\n "x": 100,\n "y": 100\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 82\n }\n },\n {\n "id": "3YDRebKqCX",\n "type": "end-node",\n "position": {\n "x": 773.3027344262372,\n "y": 101.42648884412338\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 74\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "YXJ91nHVaz",\n "type": "llm-node",\n "position": {\n "x": 430.94541183662506,\n "y": 101.42648884412338\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 74\n },\n "selected": true,\n "dragging": false\n }\n ],\n "edges": [\n {\n "source": "P14abHl4uY",\n "target": "YXJ91nHVaz",\n "id": "xy-edge__P14abHl4uY-YXJ91nHVaz"\n },\n {\n "source": "YXJ91nHVaz",\n "target": "3YDRebKqCX",\n "id": "xy-edge__YXJ91nHVaz-3YDRebKqCX"\n }\n ],\n "data": {}\n}')
checkAddConnection(createConnection('YXJ91nHVaz', 'YXJ91nHVaz'), nodes, edges)
}).toThrowError(nodeToSelfError())
})
test(hasCycleError().message, () => {
expect(() => {
// language=JSON
const {
nodes,
edges,
} = JSON.parse('{\n "nodes": [\n {\n "id": "-DKfXm7r3f",\n "type": "start-node",\n "position": {\n "x": -75.45812782717618,\n "y": 14.410669352596976\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 82\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "2uL3Hw2CAW",\n "type": "end-node",\n "position": {\n "x": 734.7875356349059,\n "y": -1.2807079327602473\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 74\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "yp-yYfKUzC",\n "type": "llm-node",\n "position": {\n "x": 338.2236369686051,\n "y": -92.5759939566568\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 74\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "N4HQPN-NYZ",\n "type": "llm-node",\n "position": {\n "x": 332.51768159211156,\n "y": 114.26488844123382\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 74\n },\n "selected": true,\n "dragging": false\n }\n ],\n "edges": [\n {\n "source": "-DKfXm7r3f",\n "target": "yp-yYfKUzC",\n "id": "xy-edge__-DKfXm7r3f-yp-yYfKUzC"\n },\n {\n "source": "yp-yYfKUzC",\n "target": "2uL3Hw2CAW",\n "id": "xy-edge__yp-yYfKUzC-2uL3Hw2CAW"\n },\n {\n "source": "-DKfXm7r3f",\n "target": "N4HQPN-NYZ",\n "id": "xy-edge__-DKfXm7r3f-N4HQPN-NYZ"\n },\n {\n "source": "N4HQPN-NYZ",\n "target": "yp-yYfKUzC",\n "id": "xy-edge__N4HQPN-NYZ-yp-yYfKUzC"\n }\n ],\n "data": {}\n}')
// language=JSON
checkAddConnection(JSON.parse('{\n "source": "yp-yYfKUzC",\n "sourceHandle": null,\n "target": "N4HQPN-NYZ",\n "targetHandle": null\n}'), nodes, edges)
}).toThrowError(hasCycleError())
})

View File

@@ -0,0 +1,111 @@
import {type Connection, type Edge, getOutgoers, type Node} from '@xyflow/react'
import {find, has, isEmpty, isEqual, lpad, toStr} from 'licia'
import {NodeRegistryMap} from './NodeRegistry.tsx'
export class CheckError extends Error {
readonly id: string
constructor(
id: number,
message: string,
) {
super(message)
this.id = `E${lpad(toStr(id), 6, '0')}`
}
public toString(): string {
return `${this.id}: ${this.message}`
}
}
const getNodeById = (id: string, nodes: Node[]) => find(nodes, (n: Node) => isEqual(n.id, id))
export const typeNotFound = (type: string) => new CheckError(100, `类型 ${type} 不存在`)
export const addNodeError = (message?: string) => new CheckError(101, message ?? '无法添加节点')
// @ts-ignore
export const checkAddNode: (type: string, parentId: string | undefined, nodes: Node[], edges: Edge[]) => void = (type, parentId, nodes, edges) => {
let nodeDefine = NodeRegistryMap[type]
if (!nodeDefine) {
throw typeNotFound(type)
}
for (const checker of nodeDefine.checkers.add) {
let checkResult = checker(type, parentId, nodes, edges, undefined)
if (checkResult.error) {
throw addNodeError(checkResult.message)
}
}
}
export const sourceNodeNotFoundError = () => new CheckError(200, '连线起始节点未找到')
export const targetNodeNotFoundError = () => new CheckError(201, '连线目标节点未找到')
export const nodeToSelfError = () => new CheckError(203, '节点不能直连自身')
export const hasCycleError = () => new CheckError(204, '禁止流程循环')
export const differentParent = () => new CheckError(205, '子流程禁止连接外部节点')
const hasCycle = (sourceNode: Node, targetNode: Node, nodes: Node[], edges: Edge[], visited = new Set<string>()) => {
if (visited.has(targetNode.id)) return false
visited.add(targetNode.id)
for (const outgoer of getOutgoers(targetNode, nodes, edges)) {
if (isEqual(outgoer.id, sourceNode.id)) return true
if (hasCycle(sourceNode, outgoer, nodes, edges, visited)) return true
}
}
export const checkAddConnection: (connection: Connection, nodes: Node[], edges: Edge[]) => void = (connection, nodes, edges) => {
let sourceNode = getNodeById(connection.source, nodes)
if (!sourceNode) {
throw sourceNodeNotFoundError()
}
let targetNode = getNodeById(connection.target, nodes)
if (!targetNode) {
throw targetNodeNotFoundError()
}
if (!isEqual(sourceNode.parentId, targetNode.parentId)) {
throw differentParent()
}
// 禁止流程出现环,必须是有向无环图
if (isEqual(sourceNode.id, targetNode.id)) {
throw nodeToSelfError()
} else if (hasCycle(sourceNode, targetNode, nodes, edges)) {
throw hasCycleError()
}
// let newEdges = [...clone(edges), {...connection, id: uuid()}]
// let {hasAbnormalEdges} = getParallelInfo(nodes, newEdges)
// if (hasAbnormalEdges) {
// throw hasRedundantEdgeError()
// }
}
export const atLeastOneNode = () => new CheckError(300, '至少包含一个节点')
export const hasUnfinishedNode = (nodeId: string) => new CheckError(301, `存在尚未配置完成的节点: ${nodeId}`)
export const nodeTypeNotFound = () => new CheckError(302, '节点类型不存在')
export const saveNodeError = (nodeId: string, reason?: string) => new CheckError(303, reason ?? `节点配置存在错误:${nodeId}`)
// @ts-ignore
export const checkSave: (nodes: Node[], edges: Edge[], data: any) => void = (nodes, edges, data) => {
if (isEmpty(nodes)) {
throw atLeastOneNode()
}
for (let node of nodes) {
if (!has(data, node.id) || !data[node.id]?.finished) {
throw hasUnfinishedNode(node.id)
}
if (!has(node, 'type')) {
throw nodeTypeNotFound()
}
let nodeType = node.type!
let nodeDefine = NodeRegistryMap[nodeType]
for (let checker of nodeDefine.checkers.save) {
let checkResult = checker(node.id, node.parentId, nodes, edges, data)
if (checkResult.error) {
throw saveNodeError(node.id, checkResult.message)
}
}
}
}

View File

@@ -0,0 +1,151 @@
import {RollbackOutlined, SaveFilled} from '@ant-design/icons'
import {Background, BackgroundVariant, Controls, MiniMap, Panel, ReactFlow} from '@xyflow/react'
import {Button, message, Popconfirm, Space} from 'antd'
import {arrToMap} from 'licia'
import {useEffect} from 'react'
import {useNavigate} from 'react-router'
import styled from 'styled-components'
import '@xyflow/react/dist/style.css'
import {commonInfo} from '../../util/amis.tsx'
import AddNodeButton from './component/AddNodeButton.tsx'
import {checkAddConnection, checkSave} from './FlowChecker.tsx'
import {useNodeDrag} from './Helper.tsx'
import {NodeRegistryMap} from './NodeRegistry.tsx'
import {useDataStore} from './store/DataStore.ts'
import {useFlowStore} from './store/FlowStore.ts'
import {flowDotColor, type FlowEditorProps} from './types.ts'
const FlowableDiv = styled.div`
.react-flow__node.selectable {
&:focus {
box-shadow: 0 0 20px 1px #e8e8e8;
border-radius: 8px;
}
}
.react-flow__handle.connectionindicator {
width: 10px;
height: 10px;
background-color: #ffffff;
border: 1px solid #000000;
&:hover {
background-color: #e8e8e8;
border: 1px solid #c6c6c6;
}
}
.node-card {
cursor: default;
.card-container {
}
}
`
function FlowEditor(props: FlowEditorProps) {
const navigate = useNavigate()
const {data, setData} = useDataStore()
const {
nodes,
setNodes,
onNodesChange,
edges,
setEdges,
onEdgesChange,
onConnect,
} = useFlowStore()
useEffect(() => {
// language=JSON
// let initialData = JSON.parse('{"nodes":[{"id":"TCxPixrdkI","type":"start-node","position":{"x":-256,"y":109.5},"data":{},"measured":{"width":256,"height":83},"selected":false,"dragging":false},{"id":"tGs78_ietp","type":"llm-node","position":{"x":108,"y":-2.5},"data":{},"measured":{"width":256,"height":105},"selected":false,"dragging":false},{"id":"OeZdaU7LpY","type":"llm-node","position":{"x":111,"y":196},"data":{},"measured":{"width":256,"height":105},"selected":false,"dragging":false},{"id":"LjfoCYZo-E","type":"knowledge-node","position":{"x":497.62196259607214,"y":-10.792497317791003},"data":{},"measured":{"width":256,"height":75},"selected":false,"dragging":false},{"id":"sQM_22GYB5","type":"end-node","position":{"x":874.3164534765615,"y":151.70316541496913},"data":{},"measured":{"width":256,"height":75},"selected":false,"dragging":false},{"id":"KpMH_xc3ZZ","type":"llm-node","position":{"x":529.6286840434341,"y":150.4721376669937},"data":{},"measured":{"width":256,"height":75},"selected":false,"dragging":false},{"id":"pOrR6EMVbe","type":"switch-node","position":{"x":110.33793030183864,"y":373.9551529987239},"data":{},"measured":{"width":256,"height":157},"selected":false,"dragging":false}],"edges":[{"source":"TCxPixrdkI","sourceHandle":"source","target":"tGs78_ietp","targetHandle":"target","id":"xy-edge__TCxPixrdkIsource-tGs78_ietptarget"},{"source":"TCxPixrdkI","sourceHandle":"source","target":"OeZdaU7LpY","targetHandle":"target","id":"xy-edge__TCxPixrdkIsource-OeZdaU7LpYtarget"},{"source":"tGs78_ietp","sourceHandle":"source","target":"LjfoCYZo-E","targetHandle":"target","id":"xy-edge__tGs78_ietpsource-LjfoCYZo-Etarget"},{"source":"LjfoCYZo-E","sourceHandle":"source","target":"KpMH_xc3ZZ","targetHandle":"target","id":"xy-edge__LjfoCYZo-Esource-KpMH_xc3ZZtarget"},{"source":"OeZdaU7LpY","sourceHandle":"source","target":"KpMH_xc3ZZ","targetHandle":"target","id":"xy-edge__OeZdaU7LpYsource-KpMH_xc3ZZtarget"},{"source":"KpMH_xc3ZZ","sourceHandle":"source","target":"sQM_22GYB5","targetHandle":"target","id":"xy-edge__KpMH_xc3ZZsource-sQM_22GYB5target"},{"source":"TCxPixrdkI","sourceHandle":"source","target":"pOrR6EMVbe","id":"xy-edge__TCxPixrdkIsource-pOrR6EMVbe"},{"source":"pOrR6EMVbe","sourceHandle":"3","target":"sQM_22GYB5","targetHandle":"target","id":"xy-edge__pOrR6EMVbe3-sQM_22GYB5target"},{"source":"pOrR6EMVbe","sourceHandle":"1","target":"KpMH_xc3ZZ","targetHandle":"target","id":"xy-edge__pOrR6EMVbe1-KpMH_xc3ZZtarget"}],"data":{"tGs78_ietp":{"model":"qwen3","outputs":{"text":{"type":"string"}},"systemPrompt":"你是个聪明人"},"OeZdaU7LpY":{"model":"qwen3","outputs":{"text":{"type":"string"}},"systemPrompt":"你也是个聪明人"}}}')
// let initialData: any = {}
let initialNodes = props.graphData?.nodes ?? []
let initialEdges = props.graphData?.edges ?? []
let initialNodeData = props.graphData?.data ?? {}
setData(initialNodeData)
setNodes(initialNodes)
setEdges(initialEdges)
}, [props.graphData])
const {
onNodeDragStart,
onNodeDrag,
onNodeDragEnd,
} = useNodeDrag([props.graphData])
return (
<FlowableDiv className="h-full w-full">
<ReactFlow
className="rounded-xl"
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={(connection) => {
try {
if (commonInfo.debug) {
console.info('Connection', JSON.stringify(connection), JSON.stringify({nodes, edges, data}))
}
checkAddConnection(connection, nodes, edges)
onConnect(connection)
} catch (e) {
// @ts-ignore
messageApi.error(e.toString())
}
}}
// @ts-ignore
nodeTypes={arrToMap(Object.keys(NodeRegistryMap), key => NodeRegistryMap[key]!.component)}
onNodeDragStart={onNodeDragStart}
onNodeDrag={onNodeDrag}
onNodeDragStop={onNodeDragEnd}
onEdgesDelete={() => console.info('delete')}
fitView
>
<Panel position="top-right">
<Space className="toolbar">
<AddNodeButton/>
<Popconfirm
title="返回上一页"
description="未保存的流程图将会被丢弃,确认是否返回"
onConfirm={() => navigate(-1)}
>
<Button type="default">
<RollbackOutlined/>
</Button>
</Popconfirm>
<Button type="primary" onClick={() => {
try {
if (commonInfo.debug) {
console.info('Save', JSON.stringify({nodes, edges, data}))
}
checkSave(nodes, edges, data)
props.onGraphDataChange({nodes, edges, data})
} catch (e) {
// @ts-ignore
message.error(e.toString())
}
}}>
<SaveFilled/>
</Button>
</Space>
</Panel>
<Controls/>
<MiniMap/>
<Background
variant={BackgroundVariant.Cross}
gap={20}
size={3}
color={flowDotColor}
/>
</ReactFlow>
</FlowableDiv>
)
}
export default FlowEditor

View File

@@ -0,0 +1,206 @@
import {type Edge, getIncomers, type Node} from '@xyflow/react'
import type {Option} from 'amis/lib/Schema'
import {contain, find, has, isEqual, max, min, unique} from 'licia'
import {type DependencyList, type MouseEvent as ReactMouseEvent, useCallback, useRef} from 'react'
import Queue from 'yocto-queue'
import {useFlowStore} from './store/FlowStore.ts'
import {type OutputVariable, type OutputVariableType} from './types.ts'
export const getAllIncomerNodeById: (id: string, nodes: Node[], edges: Edge[]) => string[] = (id, nodes, edges) => {
let queue = new Queue<Node>()
queue.enqueue(find(nodes, node => isEqual(node.id, id))!)
let result: string[] = []
while (queue.size !== 0) {
let currentNode = queue.dequeue()!
for (const incomer of getIncomers(currentNode, nodes, edges)) {
result.push(incomer.id)
queue.enqueue(incomer)
}
}
return unique(result, (a, b) => isEqual(a, b))
}
export const getAllIncomerNodeOutputVariables: (id: string, nodes: Node[], edges: Edge[], data: any) => OutputVariable[] = (id, nodes, edges, data) => {
let currentNode = find(nodes, n => isEqual(id, n.id))
if (!currentNode) {
return []
}
let incomerIds = getAllIncomerNodeById(id, nodes, edges)
if (currentNode.parentId) {
incomerIds = [
...incomerIds,
...getAllIncomerNodeById(currentNode.parentId, nodes, edges),
]
}
let incomerVariables: OutputVariable[] = []
for (const incomerId of incomerIds) {
let nodeData = data[incomerId] ?? {}
let group = incomerId
if (has(nodeData, 'node') && has(nodeData.node, 'name')) {
group = `${nodeData.node.name} ${incomerId}`
}
if (has(nodeData, 'outputs')) {
let outputs = nodeData?.outputs ?? {}
for (const key of Object.keys(outputs)) {
incomerVariables.push({
group: group,
name: key,
type: outputs[key].type,
variable: `${incomerId}.${key}`,
})
}
}
}
return [
...(currentNode.parentId ? [
{
group: '循环入参',
name: 'loopIndex (当前迭代索引)',
type: 'number',
variable: 'loopIndex',
} as OutputVariable,
{
group: '循环入参',
name: 'loopItem (当前迭代对象)',
type: 'object',
variable: 'loopItem',
} as OutputVariable,
] : []),
...incomerVariables,
]
}
export const generateAllIncomerOutputVariablesFormOptions: (id: string, nodes: Node[], edges: Edge[], data: any, targetTypes?: OutputVariableType[]) => Option[] = (id, nodes, edges, data, targetTypes) => {
let optionMap: Record<string, Option[]> = {}
for (const item of getAllIncomerNodeOutputVariables(id, nodes, edges, data)) {
if (targetTypes && !contain(targetTypes, item.type)) {
continue
}
if (!optionMap[item.group]) {
optionMap[item.group] = []
}
optionMap[item.group].push({
label: item.name,
value: item.variable,
})
}
return Object.keys(optionMap)
.map(key => ({
label: key,
children: optionMap[key],
}))
}
type ConditionOperator = string | { label: string, value: string }
const textOperators: ConditionOperator[] = ['equal', 'not_equal', 'is_empty', 'is_not_empty', 'like', 'not_like', 'starts_with', 'ends_with']
const textDefaultOperator: string = 'equal'
const booleanOperators: ConditionOperator[] = ['equal', 'not_equal']
const booleanDefaultOperator: string = 'equal'
const numberOperators: ConditionOperator[] = [
'equal',
'not_equal',
{label: '大于', value: 'greater'},
{label: '大于或等于', value: 'greater_equal'},
{label: '小于', value: 'less'},
{label: '小于或等于', value: 'less_equal'},
]
const numberDefaultOperator: string = 'equal'
const arrayOperators: ConditionOperator[] = ['is_empty', 'is_not_empty']
const arrayDefaultOperator: string = 'is_empty'
export const generateAllIncomerOutputVariablesConditions: (id: string, nodes: Node[], edges: Edge[], data: any) => Option[] = (id, nodes, edges, data) => {
let optionMap: Record<string, Option[]> = {}
for (const item of getAllIncomerNodeOutputVariables(id, nodes, edges, data)) {
if (!optionMap[item.group]) {
optionMap[item.group] = []
}
optionMap[item.group].push({
label: item.name,
type: 'custom',
name: item.variable,
...(item.type === 'text' ? {
value: {
type: 'input-text',
required: true,
clearable: true,
},
defaultOp: textDefaultOperator,
operators: textOperators,
} : {}),
...(item.type === 'boolean' ? {
value: {
type: 'select',
required: true,
selectFirst: true,
options: [
{label: '真', value: true},
{label: '假', value: false},
],
},
defaultOp: booleanDefaultOperator,
operators: booleanOperators,
} : {}),
...(item.type === 'number' ? {
value: {
type: 'input-number',
required: true,
clearable: true,
},
defaultOp: numberDefaultOperator,
operators: numberOperators,
} : {}),
...((item.type === 'array-text' || item.type === 'array-object') ? {
defaultOp: arrayDefaultOperator,
operators: arrayOperators,
} : {}),
})
}
return Object.keys(optionMap)
.map(key => ({
label: key,
children: optionMap[key],
}))
}
// 处理循环节点的边界问题
export const useNodeDrag = (deps: DependencyList) => {
const currentPosition = useRef({x: 0, y: 0} as { x: number, y: number })
const {setNode, getNodeById} = useFlowStore()
const onNodeDragStart = useCallback(() => {
}, deps)
const onNodeDrag = useCallback((event: ReactMouseEvent, node: Node) => {
event.stopPropagation()
if (node.parentId) {
let parentNode = getNodeById(node.parentId)
if (parentNode) {
let newPosition = {
x: max(min(node.position.x, (parentNode.measured?.width ?? 0) - (node.measured?.width ?? 0) - 28), 28),
y: max(min(node.position.y, (parentNode.measured?.height ?? 0) - (node.measured?.height ?? 0) - 28), 130),
}
setNode({
...node,
position: newPosition,
})
currentPosition.current = newPosition
}
}
}, deps)
const onNodeDragEnd = useCallback((_event: ReactMouseEvent, node: Node) => {
if (node.parentId) {
setNode({
...node,
position: currentPosition.current,
})
}
}, deps)
return {
onNodeDragStart,
onNodeDrag,
onNodeDragEnd,
}
}

View File

@@ -0,0 +1,159 @@
import {has, isEmpty, isEqual} from 'licia'
import {getAllIncomerNodeOutputVariables} from './Helper.tsx'
import CodeNode from './node/CodeNode.tsx'
import KnowledgeNode from './node/KnowledgeNode.tsx'
import LlmNode from './node/LlmNode.tsx'
import LoopNode from './node/LoopNode.tsx'
import OutputNode from './node/OutputNode.tsx'
import SwitchNode from './node/SwitchNode.tsx'
import TemplateNode from './node/TemplateNode.tsx'
import type {AddNodeChecker, NodeDefine, SaveNodeChecker} from './types.ts'
import InputNode from './node/InputNode.tsx'
const inputSingleVariableChecker: (field: string) => SaveNodeChecker = field => {
return (id, _parentId, nodes, edges, data) => {
let nodeData = data[id] ?? {}
if (has(nodeData, field)) {
let expression = nodeData?.[field] ?? ''
if (!isEmpty(expression)) {
let outputVariables = new Set(getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => i.variable))
if (!outputVariables.has(expression)) {
return {
error: true,
message: `节点 ${id} 存在错误:变量 ${expression} 不存在`,
}
}
}
}
return {error: false}
}
}
const inputMultiVariableChecker: SaveNodeChecker = (id, _parentId, nodes, edges, data) => {
let nodeData = data[id] ?? {}
if (has(nodeData, 'inputs')) {
let inputs = nodeData?.inputs ?? {}
if (!isEmpty(inputs)) {
let outputVariables = new Set(getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => i.variable))
for (const key of Object.keys(inputs)) {
let variable = inputs[key]?.variable ?? ''
if (!outputVariables.has(variable)) {
return {
error: true,
message: `节点 ${id} 存在错误:变量 ${variable} 不存在`,
}
}
}
}
}
return {error: false}
}
const noMoreThanOneNodeType: AddNodeChecker = (type, parentId, nodes) => {
return {
error: nodes.filter(n => isEqual(n.parentId, parentId) && isEqual(n.type, type)).length > 0,
message: `同一个流程(子流程)中类型为 ${type} 的节点至多有一个`
}
}
export const NodeRegistry: NodeDefine[] = [
{
key: 'llm-node',
group: '普通节点',
name: '大模型',
icon: <i className="fa fa-message"/>,
description: '使用大模型对话',
component: LlmNode,
checkers: {
add: [],
save: [inputMultiVariableChecker]
},
},
{
key: 'knowledge-node',
group: '普通节点',
name: '知识库',
icon: <i className="fa fa-book-bookmark"/>,
description: '',
component: KnowledgeNode,
checkers: {
add: [],
save: [inputMultiVariableChecker]
},
},
{
key: 'code-node',
group: '普通节点',
name: '代码执行',
icon: <i className="fa fa-code"/>,
description: '执行自定义的处理代码',
component: CodeNode,
checkers: {
add: [],
save: [inputMultiVariableChecker]
},
},
{
key: 'template-node',
group: '普通节点',
name: '模板替换',
icon: <i className="fa fa-pen-nib"/>,
description: '使用模板聚合转换变量表示',
component: TemplateNode,
checkers: {
add: [],
save: [inputMultiVariableChecker]
},
},
{
key: 'switch-node',
group: '逻辑节点',
name: '分支',
icon: <i className="fa fa-code-fork"/>,
description: '根据不同的情况前往不同的分支',
component: SwitchNode,
checkers: {
add: [],
save: [],
},
},
{
key: 'loop-node',
group: '逻辑节点',
name: '循环',
icon: <i className="fa fa-repeat"/>,
description: '实现循环执行流程',
component: LoopNode,
checkers: {
add: [],
save: [],
},
},
// 特殊节点特殊判断
{
key: 'input-node',
group: '数据节点',
name: '输入',
icon: <i className="fa fa-file"/>,
description: '定义流程输入变量',
component: InputNode,
checkers: {
add: [noMoreThanOneNodeType],
save: [],
},
},
{
key: 'output-node',
group: '数据节点',
name: '输出',
icon: <i className="fa fa-file"/>,
description: '定义流程输出变量',
component: OutputNode,
checkers: {
add: [noMoreThanOneNodeType],
save: [inputSingleVariableChecker('output')]
},
},
]
export const NodeRegistryMap: Record<string, NodeDefine> = NodeRegistry.reduce((a, v) => ({...a, [v.key]: v}), {})

View File

@@ -0,0 +1,78 @@
import {PlusCircleFilled} from '@ant-design/icons'
import {Button, Dropdown, message} from 'antd'
import type {ButtonProps} from 'antd/lib'
import {isEqual, randomId, unique} from 'licia'
import {commonInfo} from '../../../util/amis.tsx'
import {checkAddNode} from '../FlowChecker.tsx'
import {NodeRegistry, NodeRegistryMap} from '../NodeRegistry.tsx'
import {useDataStore} from '../store/DataStore.ts'
import {useFlowStore} from '../store/FlowStore.ts'
export type AddNodeButtonProps = ButtonProps & {
parent?: string
onlyIcon?: boolean
}
const AddNodeButton = (props: AddNodeButtonProps) => {
const {data, setDataById} = useDataStore()
const {nodes, addNode, edges} = useFlowStore()
return (
<Dropdown
menu={{
items: unique(NodeRegistry.map(i => i.group))
.map(group => ({
type: 'group',
label: group,
children: NodeRegistry
.filter(i => isEqual(group, i.group))
// 循环节点里不能再嵌套循环节点
.filter(i => !props.parent || (props.parent && !isEqual(i.key, 'loop-node')))
.map(i => ({key: i.key, label: i.name, icon: i.icon})),
})),
onClick: ({key}) => {
try {
if (commonInfo.debug) {
console.info('Add', key, JSON.stringify({nodes, edges, data}))
}
checkAddNode(key, props.parent, nodes, edges)
let nodeId = randomId(10, 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM')
let define = NodeRegistryMap[key]
setDataById(
nodeId,
{
node: {
name: define.name,
description: define.description,
},
},
)
addNode({
id: nodeId,
type: key,
position: {x: 50, y: 130},
data: {},
// 如果是循环节点就将节点加入到循环节点中
...(props.parent ? {
parentId: props.parent,
extent: 'parent',
} : {}),
})
} catch (e) {
// @ts-ignore
message.error(e.toString())
}
},
}}
>
<Button {...props}>
<PlusCircleFilled/>
{props.onlyIcon ? undefined : '新增节点'}
</Button>
</Dropdown>
)
}
export default AddNodeButton

View File

@@ -0,0 +1,298 @@
import {CopyFilled, DeleteFilled, EditFilled} from '@ant-design/icons'
import {type Edge, Handle, type Node, type NodeProps, NodeResizeControl, NodeToolbar, Position} from '@xyflow/react'
import {type ClassName, classnames, type Schema} from 'amis'
import {Button, Drawer, Space, Tooltip} from 'antd'
import {type CSSProperties, type JSX, useCallback, useState} from 'react'
import styled from 'styled-components'
import {amisRender, commonInfo, horizontalFormOptions} from '../../../util/amis.tsx'
import {generateAllIncomerOutputVariablesFormOptions} from '../Helper.tsx'
import {useDataStore} from '../store/DataStore.ts'
import {useFlowStore} from '../store/FlowStore.ts'
import {type FormSchema, OutputVariableTypeMap} from '../types.ts'
export function inputsFormColumns(
nodeId: string,
nodes: Node[],
edges: Edge[],
data: any,
): Schema[] {
return [
{
type: 'input-kvs',
name: 'inputs',
label: '输入变量',
addButtonText: '新增输入',
draggable: false,
keyItem: {
...horizontalFormOptions(),
label: '参数名称',
},
valueItems: [
{
...horizontalFormOptions(),
type: 'select',
name: 'variable',
label: '变量',
required: true,
selectMode: 'group',
options: generateAllIncomerOutputVariablesFormOptions(
nodeId,
nodes,
edges,
data,
),
},
],
},
]
}
export function outputsFormColumns(editable: boolean = false, required: boolean = false): Schema[] {
return [
{
disabled: !editable,
type: 'input-kvs',
name: 'outputs',
label: '输出变量',
addButtonText: '新增输出',
draggable: false,
keyItem: {
...horizontalFormOptions(),
label: '参数名称',
},
required: required,
valueItems: [
{
...horizontalFormOptions(),
type: 'select',
name: 'type',
label: '参数',
required: true,
selectFirst: true,
options: Object.keys(OutputVariableTypeMap).map(key => ({
// @ts-ignore
label: OutputVariableTypeMap[key],
value: key,
})),
},
],
},
]
}
type AmisNodeProps = {
className: ClassName,
style?: CSSProperties,
nodeProps: NodeProps
extraNodeDescription?: JSX.Element
handler: JSX.Element
formSchema?: () => FormSchema,
resize?: { minWidth: number, minHeight: number }
}
const AmisNodeContainerDiv = styled.div`
`
export const StartNodeHandler = () => {
return <Handle type="source" position={Position.Right} id="source"/>
}
export const EndNodeHandler = () => {
return <Handle type="target" position={Position.Left} id="target"/>
}
export const NormalNodeHandler = () => {
return (
<>
<StartNodeHandler/>
<EndNodeHandler/>
</>
)
}
export const nodeClassName = (name: string) => {
return `flow-node flow-node-${name}`
}
const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
className,
style,
nodeProps,
extraNodeDescription,
handler,
formSchema,
resize,
}) => {
const {removeNode} = useFlowStore()
const {getDataById, setDataById, removeDataById} = useDataStore()
const {id} = nodeProps
// @ts-ignore
const nodeData = getDataById(id)
const nodeName = nodeData?.node?.name ?? ''
const nodeDescription = nodeData?.node?.description ?? ''
const [editDrawerOpen, setEditDrawerOpen] = useState(false)
const [editDrawerForm, setEditDrawerForm] = useState<JSX.Element>(<></>)
const onOpenEditDrawerClick = useCallback(() => {
const schema = formSchema?.()
setEditDrawerForm(
amisRender(
{
type: 'wrapper',
size: 'none',
body: [
{
debug: commonInfo.debug,
type: 'form',
...horizontalFormOptions(),
wrapWithPanel: false,
onEvent: {
submitSucc: {
actions: [
{
actionType: 'custom',
// @ts-ignore
script: (context, action, event) => {
setDataById(
id,
{
...context.props.data,
finished: true,
},
)
setEditDrawerOpen(false)
},
},
],
},
...(schema?.events ?? {})
},
body: [
{
type: 'input-text',
name: 'node.name',
label: '节点名称',
placeholder: nodeName,
},
{
type: 'textarea',
name: 'node.description',
label: '节点描述',
placeholder: nodeDescription,
},
{
type: 'divider',
},
...(schema?.columns ?? []),
{
type: 'wrapper',
size: 'none',
className: 'space-x-2 text-right',
body: [
{
type: 'action',
label: '取消',
onEvent: {
click: {
actions: [
{
actionType: 'custom',
// @ts-ignore
script: (context, action, event) => {
setEditDrawerOpen(false)
},
},
],
},
},
},
{
type: 'submit',
label: '保存',
level: 'primary',
},
],
},
],
},
],
},
getDataById(id),
),
)
setEditDrawerOpen(true)
}, [id])
const onRemoveClick = useCallback(() => {
removeNode(id)
removeDataById(id)
}, [])
return (
<AmisNodeContainerDiv className={classnames(className, 'w-64')} style={style}>
<Drawer
title="节点编辑"
open={editDrawerOpen}
closeIcon={false}
maskClosable={false}
destroyOnHidden
size="large"
>
{editDrawerForm}
</Drawer>
<NodeToolbar>
<Space>
<Tooltip title="复制节点">
<Button
className="text-secondary"
disabled
type="text"
size="small"
icon={<CopyFilled/>}
/>
</Tooltip>
<Tooltip title="编辑节点">
<Button
className="text-secondary"
type="text"
size="small"
icon={<EditFilled/>}
onClick={() => onOpenEditDrawerClick()}
/>
</Tooltip>
<Tooltip title="删除节点">
<Button
className="text-secondary"
type="text"
size="small"
icon={<DeleteFilled/>}
onClick={() => onRemoveClick()}
/>
</Tooltip>
</Space>
</NodeToolbar>
<div className="node-card h-full flex flex-col bg-white rounded-md border border-gray-100 border-solid">
<div
className="node-card-header items-center flex justify-between p-2 border-t-0 border-l-0 border-r-0 border-b border-gray-100 border-solid">
<span className="font-bold">{nodeName}</span>
<span className="text-gray-300 text-sm">{id}</span>
</div>
<div className="node-card-description flex flex-col flex-1 p-2 text-secondary text-sm">
<div className="node-card-description-node">
{nodeDescription}
</div>
<div className="node-card-description-extra flex-1 mt-1">
{extraNodeDescription}
</div>
</div>
</div>
{resize ? <>
<NodeResizeControl
minWidth={resize.minWidth}
minHeight={resize.minHeight}
/>
</> : undefined}
{handler}
</AmisNodeContainerDiv>
)
}
export default AmisNode

Some files were not shown because too many files have changed in this diff Show More