Compare commits
20 Commits
04bc9a2c16
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2a8c39145 | ||
|
|
82ad3e7854 | ||
| 1e3e14c590 | |||
| 267eecbf45 | |||
| a5282762ed | |||
|
|
77a09472aa | ||
|
|
4cfa110f2f | ||
|
|
ad9dade6a1 | ||
|
|
3a61da054e | ||
| fbc6144d82 | |||
| 72a9d58f4c | |||
|
|
a3c2250285 | ||
| 91e6f49342 | |||
|
|
35c5150a1f | ||
|
|
a7b245a670 | ||
|
|
df8270676a | ||
|
|
b0e4f5853e | ||
| 0efb041c71 | |||
| fa06207af8 | |||
| c7e0b56850 |
@@ -45,7 +45,7 @@ create table hudi_collect_build_b12.service_ai_flow_task
|
|||||||
status enum ('ERROR','FINISHED','RUNNING') not null comment '任务运行状态',
|
status enum ('ERROR','FINISHED','RUNNING') not null comment '任务运行状态',
|
||||||
template_description varchar(255) comment '任务对应的模板功能、内容说明',
|
template_description varchar(255) comment '任务对应的模板功能、内容说明',
|
||||||
template_flow_graph longtext not null comment '任务对应的模板前端流程图数据',
|
template_flow_graph longtext not null comment '任务对应的模板前端流程图数据',
|
||||||
template_input_schema longtext not null comment '任务对应的模板入参Schema',
|
template_input_schema longtext comment '任务对应的模板入参Schema',
|
||||||
template_name varchar(255) not null comment '任务对应的模板名称',
|
template_name varchar(255) not null comment '任务对应的模板名称',
|
||||||
primary key (id)
|
primary key (id)
|
||||||
) comment ='流程任务记录' charset = utf8mb4;
|
) comment ='流程任务记录' charset = utf8mb4;
|
||||||
@@ -57,7 +57,7 @@ create table hudi_collect_build_b12.service_ai_flow_task_template
|
|||||||
modified_time datetime(6) comment '记录更新时间',
|
modified_time datetime(6) comment '记录更新时间',
|
||||||
description varchar(255) comment '模板功能、内容说明',
|
description varchar(255) comment '模板功能、内容说明',
|
||||||
flow_graph longtext not null comment '前端流程图数据',
|
flow_graph longtext not null comment '前端流程图数据',
|
||||||
input_schema longtext not null comment '模板入参Schema',
|
input_schema longtext comment '模板入参Schema',
|
||||||
name varchar(255) not null comment '模板名称',
|
name varchar(255) not null comment '模板名称',
|
||||||
primary key (id)
|
primary key (id)
|
||||||
) comment ='流程任务模板' charset = utf8mb4;
|
) comment ='流程任务模板' charset = utf8mb4;
|
||||||
|
|||||||
@@ -86,6 +86,14 @@
|
|||||||
<groupId>org.noear</groupId>
|
<groupId>org.noear</groupId>
|
||||||
<artifactId>solon-ai-dialect-openai</artifactId>
|
<artifactId>solon-ai-dialect-openai</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.yomahub</groupId>
|
||||||
|
<artifactId>liteflow-script-graaljs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.yomahub</groupId>
|
||||||
|
<artifactId>liteflow-script-python</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate.orm</groupId>
|
<groupId>org.hibernate.orm</groupId>
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
|||||||
public class WebApplication implements ApplicationRunner {
|
public class WebApplication implements ApplicationRunner {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
|
||||||
|
|
||||||
SpringApplication.run(WebApplication.class, args);
|
SpringApplication.run(WebApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.controller.task;
|
package com.lanyuanxiaoyao.service.ai.web.controller.task;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
|
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
|
||||||
@@ -14,6 +16,7 @@ import java.util.Map;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@@ -39,12 +42,18 @@ public class TaskController extends SimpleControllerSupport<FlowTask, TaskContro
|
|||||||
@GetMapping("input_data/{id}")
|
@GetMapping("input_data/{id}")
|
||||||
public AmisResponse<?> getInputData(@PathVariable("id") Long id) throws JsonProcessingException {
|
public AmisResponse<?> getInputData(@PathVariable("id") Long id) throws JsonProcessingException {
|
||||||
var task = flowTaskService.detailOrThrow(id);
|
var task = flowTaskService.detailOrThrow(id);
|
||||||
|
if (ObjectUtil.isEmpty(task.getInput())) {
|
||||||
|
return AmisResponse.responseSuccess();
|
||||||
|
}
|
||||||
return AmisResponse.responseSuccess(mapper.readValue(task.getInput(), Map.class));
|
return AmisResponse.responseSuccess(mapper.readValue(task.getInput(), Map.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("input_schema/{id}")
|
@GetMapping("input_schema/{id}")
|
||||||
public AmisResponse<?> getInputSchema(@PathVariable("id") Long id) throws JsonProcessingException {
|
public AmisResponse<?> getInputSchema(@PathVariable("id") Long id) throws JsonProcessingException {
|
||||||
var task = flowTaskService.detailOrThrow(id);
|
var task = flowTaskService.detailOrThrow(id);
|
||||||
|
if (ObjectUtil.isEmpty(task.getTemplateInputSchema())) {
|
||||||
|
return AmisResponse.responseSuccess();
|
||||||
|
}
|
||||||
return AmisResponse.responseSuccess(mapper.readValue(task.getTemplateInputSchema(), Map.class));
|
return AmisResponse.responseSuccess(mapper.readValue(task.getTemplateInputSchema(), Map.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +72,7 @@ public class TaskController extends SimpleControllerSupport<FlowTask, TaskContro
|
|||||||
task.setTemplateDescription(template.getDescription());
|
task.setTemplateDescription(template.getDescription());
|
||||||
task.setTemplateInputSchema(template.getInputSchema());
|
task.setTemplateInputSchema(template.getInputSchema());
|
||||||
task.setTemplateFlowGraph(template.getFlowGraph());
|
task.setTemplateFlowGraph(template.getFlowGraph());
|
||||||
task.setInput(mapper.writeValueAsString(item.getInput()));
|
task.setInput(ObjectUtil.isEmpty(item.getInput()) ? null : mapper.writeValueAsString(item.getInput()));
|
||||||
return task;
|
return task;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -91,9 +100,13 @@ public class TaskController extends SimpleControllerSupport<FlowTask, TaskContro
|
|||||||
public static class ListItem extends SimpleItem {
|
public static class ListItem extends SimpleItem {
|
||||||
private String templateName;
|
private String templateName;
|
||||||
private FlowTask.Status status;
|
private FlowTask.Status status;
|
||||||
|
private Boolean hasInput;
|
||||||
|
|
||||||
@org.mapstruct.Mapper
|
@org.mapstruct.Mapper(imports = {
|
||||||
|
StrUtil.class
|
||||||
|
})
|
||||||
public static abstract class Mapper {
|
public static abstract class Mapper {
|
||||||
|
@Mapping(target = "hasInput", expression = "java(StrUtil.isNotBlank(task.getInput()))")
|
||||||
public abstract ListItem from(FlowTask task);
|
public abstract ListItem from(FlowTask task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,8 +117,11 @@ public class TaskController extends SimpleControllerSupport<FlowTask, TaskContro
|
|||||||
private String error;
|
private String error;
|
||||||
private String result;
|
private String result;
|
||||||
|
|
||||||
@org.mapstruct.Mapper
|
@org.mapstruct.Mapper(imports = {
|
||||||
|
StrUtil.class
|
||||||
|
})
|
||||||
public static abstract class Mapper extends ListItem.Mapper {
|
public static abstract class Mapper extends ListItem.Mapper {
|
||||||
|
@Mapping(target = "hasInput", expression = "java(StrUtil.isNotBlank(task.getInput()))")
|
||||||
public abstract DetailItem from(FlowTask task);
|
public abstract DetailItem from(FlowTask task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.controller.task;
|
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.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@@ -38,9 +39,18 @@ public class TaskTemplateController extends SimpleControllerSupport<FlowTaskTemp
|
|||||||
@GetMapping("input_schema/{id}")
|
@GetMapping("input_schema/{id}")
|
||||||
public AmisResponse<?> getInputSchema(@PathVariable("id") Long id) throws JsonProcessingException {
|
public AmisResponse<?> getInputSchema(@PathVariable("id") Long id) throws JsonProcessingException {
|
||||||
var template = flowTaskTemplateService.detailOrThrow(id);
|
var template = flowTaskTemplateService.detailOrThrow(id);
|
||||||
|
if (ObjectUtil.isEmpty(template.getInputSchema())) {
|
||||||
|
return AmisResponse.responseSuccess();
|
||||||
|
}
|
||||||
return AmisResponse.responseSuccess(mapper.readValue(template.getInputSchema(), Map.class));
|
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")
|
@PostMapping("update_flow_graph")
|
||||||
public AmisResponse<?> updateFlowGraph(@RequestBody UpdateGraphItem item) throws JsonProcessingException {
|
public AmisResponse<?> updateFlowGraph(@RequestBody UpdateGraphItem item) throws JsonProcessingException {
|
||||||
flowTaskTemplateService.updateFlowGraph(item.getId(), mapper.writeValueAsString(item.getGraph()));
|
flowTaskTemplateService.updateFlowGraph(item.getId(), mapper.writeValueAsString(item.getGraph()));
|
||||||
@@ -49,8 +59,7 @@ public class TaskTemplateController extends SimpleControllerSupport<FlowTaskTemp
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SaveItemMapper<FlowTaskTemplate, SaveItem> saveItemMapper() {
|
protected SaveItemMapper<FlowTaskTemplate, SaveItem> saveItemMapper() {
|
||||||
var map = Mappers.getMapper(SaveItem.Mapper.class);
|
return Mappers.getMapper(SaveItem.Mapper.class);
|
||||||
return item -> map.from(item, mapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -69,15 +78,9 @@ public class TaskTemplateController extends SimpleControllerSupport<FlowTaskTemp
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private Map<String, Object> inputSchema;
|
|
||||||
|
|
||||||
@org.mapstruct.Mapper
|
@org.mapstruct.Mapper
|
||||||
public static abstract class Mapper {
|
public interface Mapper extends SaveItemMapper<FlowTaskTemplate, SaveItem> {
|
||||||
public abstract FlowTaskTemplate from(SaveItem saveItem, @Context ObjectMapper mapper) throws Exception;
|
|
||||||
|
|
||||||
protected String mapInputSchema(Map<String, Object> inputSchema, @Context ObjectMapper mapper) throws JsonProcessingException {
|
|
||||||
return mapper.writeValueAsString(inputSchema);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +108,9 @@ public class TaskTemplateController extends SimpleControllerSupport<FlowTaskTemp
|
|||||||
public abstract DetailItem from(FlowTaskTemplate template, @Context ObjectMapper mapper) throws Exception;
|
public abstract DetailItem from(FlowTaskTemplate template, @Context ObjectMapper mapper) throws Exception;
|
||||||
|
|
||||||
public Map<String, Object> mapJson(String source, @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<>() {
|
return mapper.readValue(source, new TypeReference<>() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||||
@@ -44,8 +45,10 @@ public final class FlowGraphRunner {
|
|||||||
flowStore.init(flowGraph);
|
flowStore.init(flowGraph);
|
||||||
|
|
||||||
for (FlowNode node : flowGraph.nodes()) {
|
for (FlowNode node : flowGraph.nodes()) {
|
||||||
|
if (ObjectUtil.isNull(node.parentId())) {
|
||||||
executionQueue.offer(node);
|
executionQueue.offer(node);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while (!executionQueue.isEmpty()) {
|
while (!executionQueue.isEmpty()) {
|
||||||
var node = executionQueue.poll();
|
var node = executionQueue.poll();
|
||||||
if (ObjectUtil.isNull(node)) {
|
if (ObjectUtil.isNull(node)) {
|
||||||
@@ -77,8 +80,17 @@ public final class FlowGraphRunner {
|
|||||||
var runner = runnerClazz.getDeclaredConstructor().newInstance();
|
var runner = runnerClazz.getDeclaredConstructor().newInstance();
|
||||||
runner.setNodeId(node.id());
|
runner.setNodeId(node.id());
|
||||||
runner.setContext(context);
|
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();
|
runner.run();
|
||||||
|
|
||||||
|
// 处理选择节点的逻辑
|
||||||
if (runner instanceof FlowNodeOptionalRunner) {
|
if (runner instanceof FlowNodeOptionalRunner) {
|
||||||
var targetPoint = ((FlowNodeOptionalRunner) runner).getTargetPoint();
|
var targetPoint = ((FlowNodeOptionalRunner) runner).getTargetPoint();
|
||||||
for (FlowEdge edge : nodeOutputMap.get(node.id())) {
|
for (FlowEdge edge : nodeOutputMap.get(node.id())) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import cn.hutool.extra.template.TemplateEngine;
|
|||||||
import cn.hutool.extra.template.TemplateUtil;
|
import cn.hutool.extra.template.TemplateUtil;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.eclipse.collections.api.factory.Maps;
|
import org.eclipse.collections.api.factory.Maps;
|
||||||
import org.eclipse.collections.api.map.ImmutableMap;
|
import org.eclipse.collections.api.map.ImmutableMap;
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ import org.eclipse.collections.api.map.ImmutableMap;
|
|||||||
* @author lanyuanxiaoyao
|
* @author lanyuanxiaoyao
|
||||||
* @version 20250711
|
* @version 20250711
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class FlowHelper {
|
public class FlowHelper {
|
||||||
private static final TemplateEngine TEMPLATE_ENGINE = TemplateUtil.createEngine();
|
private static final TemplateEngine TEMPLATE_ENGINE = TemplateUtil.createEngine();
|
||||||
|
|
||||||
@@ -45,11 +47,6 @@ public class FlowHelper {
|
|||||||
throw new RuntimeException(StrUtil.format("Target node variable not found: {}.{}", targetNodeId, targetVariableName));
|
throw new RuntimeException(StrUtil.format("Target node variable not found: {}.{}", targetNodeId, targetVariableName));
|
||||||
}
|
}
|
||||||
return targetNodeData.get(targetVariableName);
|
return targetNodeData.get(targetVariableName);
|
||||||
} else if (context.getInput().containsKey(expression)) {
|
|
||||||
if (!context.getInput().containsKey(expression)) {
|
|
||||||
throw new RuntimeException(StrUtil.format("Target variable not found in input {}", expression));
|
|
||||||
}
|
|
||||||
return context.getInput().get(expression);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -2,12 +2,10 @@ package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.eclipse.collections.api.factory.Maps;
|
import org.eclipse.collections.api.factory.Maps;
|
||||||
import org.eclipse.collections.api.map.ImmutableMap;
|
|
||||||
import org.eclipse.collections.api.map.MutableMap;
|
import org.eclipse.collections.api.map.MutableMap;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class FlowContext {
|
public class FlowContext {
|
||||||
private ImmutableMap<String, Object> input = Maps.immutable.empty();
|
|
||||||
private MutableMap<String, MutableMap<String, Object>> data = Maps.mutable.<String, MutableMap<String, Object>>empty().asSynchronized();
|
private MutableMap<String, MutableMap<String, Object>> data = Maps.mutable.<String, MutableMap<String, Object>>empty().asSynchronized();
|
||||||
|
|
||||||
public MutableMap<String, Object> get(String key) {
|
public MutableMap<String, Object> get(String key) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,7 +13,9 @@ public record FlowEdge(
|
|||||||
String id,
|
String id,
|
||||||
String source,
|
String source,
|
||||||
String target,
|
String target,
|
||||||
|
@JsonProperty("sourceHandle")
|
||||||
String sourcePoint,
|
String sourcePoint,
|
||||||
|
@JsonProperty("targetHandle")
|
||||||
String targetPoint
|
String targetPoint
|
||||||
) {
|
) {
|
||||||
public enum Status {
|
public enum Status {
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import java.time.LocalDateTime;
|
|||||||
*/
|
*/
|
||||||
public record FlowNode(
|
public record FlowNode(
|
||||||
String id,
|
String id,
|
||||||
String type
|
String type,
|
||||||
|
String parentId
|
||||||
) {
|
) {
|
||||||
public enum Status {
|
public enum Status {
|
||||||
INITIAL, RUNNING, FINISHED, SKIPPED
|
INITIAL, RUNNING, FINISHED, SKIPPED
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.engine.node;
|
||||||
|
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
|
||||||
|
import java.util.Map;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250711
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class InputNode extends FlowNodeRunner {
|
||||||
|
public static final String KEY = "flow_inputs";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
var inputData = getContext().getData().get(KEY);
|
||||||
|
var inputs = this.<Map<String, Object>>getData("inputs");
|
||||||
|
for (String variable : inputs.keySet()) {
|
||||||
|
if (inputData.containsKey(variable)) {
|
||||||
|
setData(variable, inputData.get(variable));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,6 @@ public class LlmNode extends FlowNodeRunner {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
||||||
log.info("Variable map: {}", variableMap);
|
|
||||||
var sourcePrompt = (String) getData("systemPrompt");
|
var sourcePrompt = (String) getData("systemPrompt");
|
||||||
if (StrUtil.isNotBlank(sourcePrompt)) {
|
if (StrUtil.isNotBlank(sourcePrompt)) {
|
||||||
var prompt = FlowHelper.renderTemplateText(sourcePrompt, variableMap.toMap());
|
var prompt = FlowHelper.renderTemplateText(sourcePrompt, variableMap.toMap());
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,10 +10,11 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class OutputNode extends FlowNodeRunner {
|
public class OutputNode extends FlowNodeRunner {
|
||||||
|
private static final String KEY = "flow_outputs";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
String expression = getData("output");
|
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
||||||
var targetVariable = FlowHelper.generateVariable(expression, getContext());
|
getContext().getData().put(KEY, variableMap.toMap());
|
||||||
log.info("Target: {}", targetVariable);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -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<>() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<>() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ public class FlowTask extends SimpleEntity {
|
|||||||
@Comment("任务对应的模板功能、内容说明")
|
@Comment("任务对应的模板功能、内容说明")
|
||||||
private String templateDescription;
|
private String templateDescription;
|
||||||
@Comment("任务对应的模板入参Schema")
|
@Comment("任务对应的模板入参Schema")
|
||||||
@Column(nullable = false, columnDefinition = "longtext")
|
@Column(columnDefinition = "longtext")
|
||||||
private String templateInputSchema;
|
private String templateInputSchema;
|
||||||
@Comment("任务对应的模板前端流程图数据")
|
@Comment("任务对应的模板前端流程图数据")
|
||||||
@Column(nullable = false, columnDefinition = "longtext")
|
@Column(nullable = false, columnDefinition = "longtext")
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class FlowTaskTemplate extends SimpleEntity {
|
|||||||
@Comment("模板功能、内容说明")
|
@Comment("模板功能、内容说明")
|
||||||
private String description;
|
private String description;
|
||||||
@Comment("模板入参Schema")
|
@Comment("模板入参Schema")
|
||||||
@Column(nullable = false, columnDefinition = "longtext")
|
@Column(columnDefinition = "longtext")
|
||||||
private String inputSchema;
|
private String inputSchema;
|
||||||
@Comment("前端流程图数据")
|
@Comment("前端流程图数据")
|
||||||
@Column(nullable = false, columnDefinition = "longtext")
|
@Column(nullable = false, columnDefinition = "longtext")
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -7,9 +7,7 @@ import com.lanyuanxiaoyao.service.ai.web.entity.context.FeedbackContext;
|
|||||||
import com.lanyuanxiaoyao.service.ai.web.repository.FeedbackRepository;
|
import com.lanyuanxiaoyao.service.ai.web.repository.FeedbackRepository;
|
||||||
import com.yomahub.liteflow.core.FlowExecutor;
|
import com.yomahub.liteflow.core.FlowExecutor;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -24,7 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(initialDelay = 1, fixedDelay = 1, timeUnit = TimeUnit.MINUTES)
|
// @Scheduled(initialDelay = 1, fixedDelay = 1, timeUnit = TimeUnit.MINUTES)
|
||||||
public void analysis() {
|
public void analysis() {
|
||||||
List<Feedback> feedbacks = repository.findAll(
|
List<Feedback> feedbacks = repository.findAll(
|
||||||
builder -> builder
|
builder -> builder
|
||||||
|
|||||||
@@ -1,26 +1,28 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.service.task;
|
package com.lanyuanxiaoyao.service.ai.web.service.task;
|
||||||
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
|
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowExecutor;
|
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.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.FlowGraph;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
|
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.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.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.engine.store.InMemoryFlowStore;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
|
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 com.lanyuanxiaoyao.service.ai.web.repository.FlowTaskRepository;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import lombok.Data;
|
import java.util.Map;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.eclipse.collections.api.factory.Maps;
|
import org.eclipse.collections.api.factory.Maps;
|
||||||
import org.eclipse.collections.api.map.MutableMap;
|
|
||||||
import org.eclipse.collections.api.set.ImmutableSet;
|
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -42,21 +44,23 @@ public class FlowTaskService extends SimpleServiceSupport<FlowTask> {
|
|||||||
var store = new InMemoryFlowStore();
|
var store = new InMemoryFlowStore();
|
||||||
var executor = new FlowExecutor(
|
var executor = new FlowExecutor(
|
||||||
store,
|
store,
|
||||||
Maps.immutable.of(
|
Maps.immutable.ofAll(Map.of(
|
||||||
"output-node", OutputNode.class,
|
"loop-node", LoopNode.class,
|
||||||
"llm-node", LlmNode.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();
|
FlowContext context = new FlowContext();
|
||||||
context.setInput(mapper.readValue(flowTask.getInput(), new TypeReference<>() {}));
|
|
||||||
context.setData(graphVo.getData());
|
context.setData(graphVo.getData());
|
||||||
executor.execute(flowGraph, context);
|
|
||||||
|
if (ObjectUtil.isNotEmpty(flowTask.getInput())) {
|
||||||
|
context.getData().put(InputNode.KEY, mapper.readValue(flowTask.getInput(), new TypeReference<>() {
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
executor.execute(flowGraph, context);
|
||||||
public static final class FlowGraphVo {
|
|
||||||
private ImmutableSet<FlowNode> nodes;
|
|
||||||
private ImmutableSet<FlowEdge> edges;
|
|
||||||
private MutableMap<String, MutableMap<String, Object>> data;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,45 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.service.task;
|
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.base.service.SimpleServiceSupport;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
|
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 com.lanyuanxiaoyao.service.ai.web.repository.FlowTaskTemplateRepository;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class FlowTaskTemplateService extends SimpleServiceSupport<FlowTaskTemplate> {
|
public class FlowTaskTemplateService extends SimpleServiceSupport<FlowTaskTemplate> {
|
||||||
public FlowTaskTemplateService(FlowTaskTemplateRepository flowTaskTemplateRepository) {
|
private final ObjectMapper mapper;
|
||||||
|
|
||||||
|
public FlowTaskTemplateService(FlowTaskTemplateRepository flowTaskTemplateRepository, Jackson2ObjectMapperBuilder builder) {
|
||||||
super(flowTaskTemplateRepository);
|
super(flowTaskTemplateRepository);
|
||||||
|
this.mapper = builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateFlowGraph(Long id, String flowGraph) {
|
public void updateFlowGraph(Long id, String flowGraph) throws JsonProcessingException {
|
||||||
var template = detailOrThrow(id);
|
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")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template.setFlowGraph(flowGraph);
|
template.setFlowGraph(flowGraph);
|
||||||
save(template);
|
save(template);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,15 +35,15 @@ public class TestFlow {
|
|||||||
var graph = new FlowGraph(
|
var graph = new FlowGraph(
|
||||||
"graph-1",
|
"graph-1",
|
||||||
Sets.immutable.of(
|
Sets.immutable.of(
|
||||||
new FlowNode("node-1", "plain-node"),
|
new FlowNode("node-1", "plain-node", null),
|
||||||
new FlowNode("node-2", "plain-node"),
|
new FlowNode("node-2", "plain-node", null),
|
||||||
new FlowNode("node-4", "plain-node"),
|
new FlowNode("node-4", "plain-node", null),
|
||||||
new FlowNode("node-6", "plain-node"),
|
new FlowNode("node-6", "plain-node", null),
|
||||||
new FlowNode("node-7", "plain-node"),
|
new FlowNode("node-7", "plain-node", null),
|
||||||
new FlowNode("node-5", "plain-node"),
|
new FlowNode("node-5", "plain-node", null),
|
||||||
new FlowNode("node-8", "option-node"),
|
new FlowNode("node-8", "option-node", null),
|
||||||
new FlowNode("node-9", "plain-node"),
|
new FlowNode("node-9", "plain-node", null),
|
||||||
new FlowNode("node-3", "plain-node")
|
new FlowNode("node-3", "plain-node", null)
|
||||||
),
|
),
|
||||||
Sets.immutable.of(
|
Sets.immutable.of(
|
||||||
new FlowEdge("edge-1", "node-1", "node-2", null, null),
|
new FlowEdge("edge-1", "node-1", "node-2", null, null),
|
||||||
|
|||||||
@@ -20,8 +20,21 @@ export class CheckError extends Error {
|
|||||||
|
|
||||||
const getNodeById = (id: string, nodes: Node[]) => find(nodes, (n: Node) => isEqual(n.id, id))
|
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
|
// @ts-ignore
|
||||||
export const checkAddNode: (type: string, nodes: Node[], edges: Edge[]) => void = (type, nodes, edges) => {
|
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 sourceNodeNotFoundError = () => new CheckError(200, '连线起始节点未找到')
|
||||||
@@ -70,18 +83,17 @@ export const checkAddConnection: (connection: Connection, nodes: Node[], edges:
|
|||||||
export const atLeastOneNode = () => new CheckError(300, '至少包含一个节点')
|
export const atLeastOneNode = () => new CheckError(300, '至少包含一个节点')
|
||||||
export const hasUnfinishedNode = (nodeId: string) => new CheckError(301, `存在尚未配置完成的节点: ${nodeId}`)
|
export const hasUnfinishedNode = (nodeId: string) => new CheckError(301, `存在尚未配置完成的节点: ${nodeId}`)
|
||||||
export const nodeTypeNotFound = () => new CheckError(302, '节点类型不存在')
|
export const nodeTypeNotFound = () => new CheckError(302, '节点类型不存在')
|
||||||
export const nodeError = (nodeId: string, reason?: string) => new CheckError(303, reason ?? `节点配置存在错误:${nodeId}`)
|
export const saveNodeError = (nodeId: string, reason?: string) => new CheckError(303, reason ?? `节点配置存在错误:${nodeId}`)
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
export const checkSave: (inputSchema: Record<string, Record<string, any>>, nodes: Node[], edges: Edge[], data: any) => void = (inputSchema, nodes, edges, data) => {
|
export const checkSave: (nodes: Node[], edges: Edge[], data: any) => void = (nodes, edges, data) => {
|
||||||
if (isEmpty(nodes)) {
|
if (isEmpty(nodes)) {
|
||||||
throw atLeastOneNode()
|
throw atLeastOneNode()
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let node of nodes) {
|
for (let node of nodes) {
|
||||||
let nodeId = node.id
|
if (!has(data, node.id) || !data[node.id]?.finished) {
|
||||||
if (!has(data, nodeId) || !data[nodeId]?.finished) {
|
throw hasUnfinishedNode(node.id)
|
||||||
throw hasUnfinishedNode(nodeId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!has(node, 'type')) {
|
if (!has(node, 'type')) {
|
||||||
@@ -89,10 +101,10 @@ export const checkSave: (inputSchema: Record<string, Record<string, any>>, nodes
|
|||||||
}
|
}
|
||||||
let nodeType = node.type!
|
let nodeType = node.type!
|
||||||
let nodeDefine = NodeRegistryMap[nodeType]
|
let nodeDefine = NodeRegistryMap[nodeType]
|
||||||
for (let checker of nodeDefine.checkers) {
|
for (let checker of nodeDefine.checkers.save) {
|
||||||
let checkResult = checker(nodeId, inputSchema, nodes, edges, data)
|
let checkResult = checker(node.id, node.parentId, nodes, edges, data)
|
||||||
if (checkResult.error) {
|
if (checkResult.error) {
|
||||||
throw nodeError(nodeId, checkResult.message)
|
throw saveNodeError(node.id, checkResult.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import {PlusCircleFilled, RollbackOutlined, SaveFilled} from '@ant-design/icons'
|
import {RollbackOutlined, SaveFilled} from '@ant-design/icons'
|
||||||
import {Background, BackgroundVariant, Controls, MiniMap, Panel, ReactFlow} from '@xyflow/react'
|
import {Background, BackgroundVariant, Controls, MiniMap, Panel, ReactFlow} from '@xyflow/react'
|
||||||
import {Button, Dropdown, message, Popconfirm, Space} from 'antd'
|
import {Button, message, Popconfirm, Space} from 'antd'
|
||||||
import {arrToMap, isEqual, randomId, unique} from 'licia'
|
import {arrToMap} from 'licia'
|
||||||
import {useEffect} from 'react'
|
import {useEffect} from 'react'
|
||||||
import {useNavigate} from 'react-router'
|
import {useNavigate} from 'react-router'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
import '@xyflow/react/dist/style.css'
|
import '@xyflow/react/dist/style.css'
|
||||||
import {commonInfo} from '../../util/amis.tsx'
|
import {commonInfo} from '../../util/amis.tsx'
|
||||||
import {checkAddConnection, checkAddNode, checkSave} from './FlowChecker.tsx'
|
import AddNodeButton from './component/AddNodeButton.tsx'
|
||||||
|
import {checkAddConnection, checkSave} from './FlowChecker.tsx'
|
||||||
import {useNodeDrag} from './Helper.tsx'
|
import {useNodeDrag} from './Helper.tsx'
|
||||||
import {NodeRegistry, NodeRegistryMap} from './NodeRegistry.tsx'
|
import {NodeRegistryMap} from './NodeRegistry.tsx'
|
||||||
import {useContextStore} from './store/ContextStore.ts'
|
|
||||||
import {useDataStore} from './store/DataStore.ts'
|
import {useDataStore} from './store/DataStore.ts'
|
||||||
import {useFlowStore} from './store/FlowStore.ts'
|
import {useFlowStore} from './store/FlowStore.ts'
|
||||||
import {flowDotColor, type FlowEditorProps} from './types.ts'
|
import {flowDotColor, type FlowEditorProps} from './types.ts'
|
||||||
@@ -45,12 +45,10 @@ const FlowableDiv = styled.div`
|
|||||||
|
|
||||||
function FlowEditor(props: FlowEditorProps) {
|
function FlowEditor(props: FlowEditorProps) {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [messageApi, contextHolder] = message.useMessage()
|
|
||||||
|
|
||||||
const {data, setData, setDataById} = useDataStore()
|
const {data, setData} = useDataStore()
|
||||||
const {
|
const {
|
||||||
nodes,
|
nodes,
|
||||||
addNode,
|
|
||||||
setNodes,
|
setNodes,
|
||||||
onNodesChange,
|
onNodesChange,
|
||||||
edges,
|
edges,
|
||||||
@@ -59,8 +57,6 @@ function FlowEditor(props: FlowEditorProps) {
|
|||||||
onConnect,
|
onConnect,
|
||||||
} = useFlowStore()
|
} = useFlowStore()
|
||||||
|
|
||||||
const {inputSchema, setInputSchema} = useContextStore()
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// language=JSON
|
// 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 = 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":"你也是个聪明人"}}}')
|
||||||
@@ -73,7 +69,6 @@ function FlowEditor(props: FlowEditorProps) {
|
|||||||
setNodes(initialNodes)
|
setNodes(initialNodes)
|
||||||
setEdges(initialEdges)
|
setEdges(initialEdges)
|
||||||
|
|
||||||
setInputSchema(props.inputSchema)
|
|
||||||
}, [props.graphData])
|
}, [props.graphData])
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -84,7 +79,6 @@ function FlowEditor(props: FlowEditorProps) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<FlowableDiv className="h-full w-full">
|
<FlowableDiv className="h-full w-full">
|
||||||
{contextHolder}
|
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
className="rounded-xl"
|
className="rounded-xl"
|
||||||
nodes={nodes}
|
nodes={nodes}
|
||||||
@@ -100,7 +94,7 @@ function FlowEditor(props: FlowEditorProps) {
|
|||||||
onConnect(connection)
|
onConnect(connection)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
messageApi.error(e.toString())
|
message.error(e.toString())
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -108,56 +102,12 @@ function FlowEditor(props: FlowEditorProps) {
|
|||||||
onNodeDragStart={onNodeDragStart}
|
onNodeDragStart={onNodeDragStart}
|
||||||
onNodeDrag={onNodeDrag}
|
onNodeDrag={onNodeDrag}
|
||||||
onNodeDragStop={onNodeDragEnd}
|
onNodeDragStop={onNodeDragEnd}
|
||||||
|
onEdgesDelete={() => console.info('delete')}
|
||||||
|
fitView
|
||||||
>
|
>
|
||||||
<Panel position="top-right">
|
<Panel position="top-right">
|
||||||
<Space className="toolbar">
|
<Space className="toolbar">
|
||||||
<Dropdown
|
<AddNodeButton/>
|
||||||
menu={{
|
|
||||||
items: unique(NodeRegistry.map(i => i.group))
|
|
||||||
.map(group => ({
|
|
||||||
type: 'group',
|
|
||||||
label: group,
|
|
||||||
children: NodeRegistry.filter(i => isEqual(group, i.group))
|
|
||||||
.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, 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: 100, y: 100},
|
|
||||||
data: {},
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
// @ts-ignore
|
|
||||||
messageApi.error(e.toString())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Button type="default">
|
|
||||||
<PlusCircleFilled/>
|
|
||||||
新增节点
|
|
||||||
</Button>
|
|
||||||
</Dropdown>
|
|
||||||
<Popconfirm
|
<Popconfirm
|
||||||
title="返回上一页"
|
title="返回上一页"
|
||||||
description="未保存的流程图将会被丢弃,确认是否返回"
|
description="未保存的流程图将会被丢弃,确认是否返回"
|
||||||
@@ -173,11 +123,11 @@ function FlowEditor(props: FlowEditorProps) {
|
|||||||
if (commonInfo.debug) {
|
if (commonInfo.debug) {
|
||||||
console.info('Save', JSON.stringify({nodes, edges, data}))
|
console.info('Save', JSON.stringify({nodes, edges, data}))
|
||||||
}
|
}
|
||||||
checkSave(inputSchema, nodes, edges, data)
|
checkSave(nodes, edges, data)
|
||||||
props.onGraphDataChange({nodes, edges, data})
|
props.onGraphDataChange({nodes, edges, data})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
messageApi.error(e.toString())
|
message.error(e.toString())
|
||||||
}
|
}
|
||||||
}}>
|
}}>
|
||||||
<SaveFilled/>
|
<SaveFilled/>
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import {type Edge, getIncomers, type Node} from '@xyflow/react'
|
import {type Edge, getIncomers, type Node} from '@xyflow/react'
|
||||||
import type {Option} from 'amis/lib/Schema'
|
import type {Option} from 'amis/lib/Schema'
|
||||||
import {find, has, isEmpty, isEqual, max, min, unique} from 'licia'
|
import {contain, find, has, isEqual, max, min, unique} from 'licia'
|
||||||
import {type DependencyList, type MouseEvent as ReactMouseEvent, useCallback, useRef} from 'react'
|
import {type DependencyList, type MouseEvent as ReactMouseEvent, useCallback, useRef} from 'react'
|
||||||
import Queue from 'yocto-queue'
|
import Queue from 'yocto-queue'
|
||||||
import {useFlowStore} from './store/FlowStore.ts'
|
import {useFlowStore} from './store/FlowStore.ts'
|
||||||
import type {InputFormOptions, InputFormOptionsGroup} from './types.ts'
|
import {type OutputVariable, type OutputVariableType} from './types.ts'
|
||||||
|
|
||||||
export const getAllIncomerNodeById: (id: string, nodes: Node[], edges: Edge[]) => string[] = (id, nodes, edges) => {
|
export const getAllIncomerNodeById: (id: string, nodes: Node[], edges: Edge[]) => string[] = (id, nodes, edges) => {
|
||||||
let queue = new Queue<Node>()
|
let queue = new Queue<Node>()
|
||||||
@@ -20,35 +20,21 @@ export const getAllIncomerNodeById: (id: string, nodes: Node[], edges: Edge[]) =
|
|||||||
return unique(result, (a, b) => isEqual(a, b))
|
return unique(result, (a, b) => isEqual(a, b))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getAllIncomerNodeOutputVariables: (id: string, nodes: Node[], edges: Edge[], data: any) => {
|
export const getAllIncomerNodeOutputVariables: (id: string, nodes: Node[], edges: Edge[], data: any) => OutputVariable[] = (id, nodes, edges, data) => {
|
||||||
id: string,
|
let currentNode = find(nodes, n => isEqual(id, n.id))
|
||||||
variable: string
|
if (!currentNode) {
|
||||||
}[] = (id, nodes, edges, data) => {
|
return []
|
||||||
let incomerIds = getAllIncomerNodeById(id, nodes, edges)
|
|
||||||
let incomerVariables: { id: string, variable: string }[] = []
|
|
||||||
for (const incomerId of incomerIds) {
|
|
||||||
let nodeData = data[incomerId] ?? {}
|
|
||||||
if (has(nodeData, 'outputs')) {
|
|
||||||
let outputs = nodeData?.outputs ?? []
|
|
||||||
for (const output of Object.keys(outputs)) {
|
|
||||||
incomerVariables.push({
|
|
||||||
id: incomerId,
|
|
||||||
variable: output,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return incomerVariables
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const generateAllIncomerOutputVariablesFormOptions: (id: string, inputSchema: Record<string, Record<string, any>>, nodes: Node[], edges: Edge[], data: any) => Option[] = (id, inputSchema, nodes, edges, data) => {
|
|
||||||
let inputSchemaVariables: InputFormOptions[] = Object.keys(inputSchema).map(key => ({
|
|
||||||
label: `${key} (${inputSchema[key]?.label ?? ''})`,
|
|
||||||
value: key,
|
|
||||||
}))
|
|
||||||
|
|
||||||
let incomerIds = getAllIncomerNodeById(id, nodes, edges)
|
let incomerIds = getAllIncomerNodeById(id, nodes, edges)
|
||||||
let incomerVariables: InputFormOptionsGroup[] = []
|
if (currentNode.parentId) {
|
||||||
|
incomerIds = [
|
||||||
|
...incomerIds,
|
||||||
|
...getAllIncomerNodeById(currentNode.parentId, nodes, edges),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
let incomerVariables: OutputVariable[] = []
|
||||||
for (const incomerId of incomerIds) {
|
for (const incomerId of incomerIds) {
|
||||||
let nodeData = data[incomerId] ?? {}
|
let nodeData = data[incomerId] ?? {}
|
||||||
let group = incomerId
|
let group = incomerId
|
||||||
@@ -56,35 +42,129 @@ export const generateAllIncomerOutputVariablesFormOptions: (id: string, inputSch
|
|||||||
group = `${nodeData.node.name} ${incomerId}`
|
group = `${nodeData.node.name} ${incomerId}`
|
||||||
}
|
}
|
||||||
if (has(nodeData, 'outputs')) {
|
if (has(nodeData, 'outputs')) {
|
||||||
let outputs = nodeData?.outputs ?? []
|
let outputs = nodeData?.outputs ?? {}
|
||||||
|
for (const key of Object.keys(outputs)) {
|
||||||
incomerVariables.push({
|
incomerVariables.push({
|
||||||
group: group,
|
group: group,
|
||||||
variables: Object.keys(outputs).map(key => ({
|
name: key,
|
||||||
value: `${incomerId}.${key}`,
|
type: outputs[key].type,
|
||||||
label: key,
|
variable: `${incomerId}.${key}`,
|
||||||
})),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let inputVariables = [
|
|
||||||
...(isEmpty(inputSchemaVariables) ? [] : [
|
|
||||||
{
|
|
||||||
group: '流程入参',
|
|
||||||
variables: inputSchemaVariables,
|
|
||||||
},
|
|
||||||
]),
|
|
||||||
...incomerVariables,
|
|
||||||
]
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
...inputVariables.map(item => ({
|
...(currentNode.parentId ? [
|
||||||
label: item.group,
|
{
|
||||||
children: item.variables,
|
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) => {
|
export const useNodeDrag = (deps: DependencyList) => {
|
||||||
const currentPosition = useRef({x: 0, y: 0} as { x: number, y: number })
|
const currentPosition = useRef({x: 0, y: 0} as { x: number, y: number })
|
||||||
@@ -99,7 +179,7 @@ export const useNodeDrag = (deps: DependencyList) => {
|
|||||||
if (parentNode) {
|
if (parentNode) {
|
||||||
let newPosition = {
|
let newPosition = {
|
||||||
x: max(min(node.position.x, (parentNode.measured?.width ?? 0) - (node.measured?.width ?? 0) - 28), 28),
|
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), 90),
|
y: max(min(node.position.y, (parentNode.measured?.height ?? 0) - (node.measured?.height ?? 0) - 28), 130),
|
||||||
}
|
}
|
||||||
setNode({
|
setNode({
|
||||||
...node,
|
...node,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {has, isEmpty} from 'licia'
|
import {has, isEmpty, isEqual} from 'licia'
|
||||||
import {getAllIncomerNodeOutputVariables} from './Helper.tsx'
|
import {getAllIncomerNodeOutputVariables} from './Helper.tsx'
|
||||||
import CodeNode from './node/CodeNode.tsx'
|
import CodeNode from './node/CodeNode.tsx'
|
||||||
import KnowledgeNode from './node/KnowledgeNode.tsx'
|
import KnowledgeNode from './node/KnowledgeNode.tsx'
|
||||||
@@ -7,18 +7,16 @@ import LoopNode from './node/LoopNode.tsx'
|
|||||||
import OutputNode from './node/OutputNode.tsx'
|
import OutputNode from './node/OutputNode.tsx'
|
||||||
import SwitchNode from './node/SwitchNode.tsx'
|
import SwitchNode from './node/SwitchNode.tsx'
|
||||||
import TemplateNode from './node/TemplateNode.tsx'
|
import TemplateNode from './node/TemplateNode.tsx'
|
||||||
import type {NodeChecker, NodeDefine} from './types.ts'
|
import type {AddNodeChecker, NodeDefine, SaveNodeChecker} from './types.ts'
|
||||||
|
import InputNode from './node/InputNode.tsx'
|
||||||
|
|
||||||
const inputSingleVariableChecker: (field: string) => NodeChecker = field => {
|
const inputSingleVariableChecker: (field: string) => SaveNodeChecker = field => {
|
||||||
return (id, inputSchema, nodes, edges, data) => {
|
return (id, _parentId, nodes, edges, data) => {
|
||||||
let nodeData = data[id] ?? {}
|
let nodeData = data[id] ?? {}
|
||||||
if (has(nodeData, field)) {
|
if (has(nodeData, field)) {
|
||||||
let expression = nodeData?.[field] ?? ''
|
let expression = nodeData?.[field] ?? ''
|
||||||
if (!isEmpty(expression)) {
|
if (!isEmpty(expression)) {
|
||||||
let outputVariables = new Set([
|
let outputVariables = new Set(getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => i.variable))
|
||||||
...getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => `${i.id}.${i.variable}`),
|
|
||||||
...Object.keys(inputSchema),
|
|
||||||
])
|
|
||||||
if (!outputVariables.has(expression)) {
|
if (!outputVariables.has(expression)) {
|
||||||
return {
|
return {
|
||||||
error: true,
|
error: true,
|
||||||
@@ -31,15 +29,12 @@ const inputSingleVariableChecker: (field: string) => NodeChecker = field => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputMultiVariableChecker: NodeChecker = (id, inputSchema, nodes, edges, data) => {
|
const inputMultiVariableChecker: SaveNodeChecker = (id, _parentId, nodes, edges, data) => {
|
||||||
let nodeData = data[id] ?? {}
|
let nodeData = data[id] ?? {}
|
||||||
if (has(nodeData, 'inputs')) {
|
if (has(nodeData, 'inputs')) {
|
||||||
let inputs = nodeData?.inputs ?? {}
|
let inputs = nodeData?.inputs ?? {}
|
||||||
if (!isEmpty(inputs)) {
|
if (!isEmpty(inputs)) {
|
||||||
let outputVariables = new Set([
|
let outputVariables = new Set(getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => i.variable))
|
||||||
...getAllIncomerNodeOutputVariables(id, nodes, edges, data).map(i => `${i.id}.${i.variable}`),
|
|
||||||
...Object.keys(inputSchema),
|
|
||||||
])
|
|
||||||
for (const key of Object.keys(inputs)) {
|
for (const key of Object.keys(inputs)) {
|
||||||
let variable = inputs[key]?.variable ?? ''
|
let variable = inputs[key]?.variable ?? ''
|
||||||
if (!outputVariables.has(variable)) {
|
if (!outputVariables.has(variable)) {
|
||||||
@@ -54,6 +49,13 @@ const inputMultiVariableChecker: NodeChecker = (id, inputSchema, nodes, edges, d
|
|||||||
return {error: false}
|
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[] = [
|
export const NodeRegistry: NodeDefine[] = [
|
||||||
{
|
{
|
||||||
key: 'llm-node',
|
key: 'llm-node',
|
||||||
@@ -62,7 +64,10 @@ export const NodeRegistry: NodeDefine[] = [
|
|||||||
icon: <i className="fa fa-message"/>,
|
icon: <i className="fa fa-message"/>,
|
||||||
description: '使用大模型对话',
|
description: '使用大模型对话',
|
||||||
component: LlmNode,
|
component: LlmNode,
|
||||||
checkers: [inputMultiVariableChecker],
|
checkers: {
|
||||||
|
add: [],
|
||||||
|
save: [inputMultiVariableChecker]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'knowledge-node',
|
key: 'knowledge-node',
|
||||||
@@ -71,7 +76,10 @@ export const NodeRegistry: NodeDefine[] = [
|
|||||||
icon: <i className="fa fa-book-bookmark"/>,
|
icon: <i className="fa fa-book-bookmark"/>,
|
||||||
description: '',
|
description: '',
|
||||||
component: KnowledgeNode,
|
component: KnowledgeNode,
|
||||||
checkers: [inputMultiVariableChecker],
|
checkers: {
|
||||||
|
add: [],
|
||||||
|
save: [inputMultiVariableChecker]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'code-node',
|
key: 'code-node',
|
||||||
@@ -80,7 +88,10 @@ export const NodeRegistry: NodeDefine[] = [
|
|||||||
icon: <i className="fa fa-code"/>,
|
icon: <i className="fa fa-code"/>,
|
||||||
description: '执行自定义的处理代码',
|
description: '执行自定义的处理代码',
|
||||||
component: CodeNode,
|
component: CodeNode,
|
||||||
checkers: [inputMultiVariableChecker],
|
checkers: {
|
||||||
|
add: [],
|
||||||
|
save: [inputMultiVariableChecker]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'template-node',
|
key: 'template-node',
|
||||||
@@ -89,7 +100,10 @@ export const NodeRegistry: NodeDefine[] = [
|
|||||||
icon: <i className="fa fa-pen-nib"/>,
|
icon: <i className="fa fa-pen-nib"/>,
|
||||||
description: '使用模板聚合转换变量表示',
|
description: '使用模板聚合转换变量表示',
|
||||||
component: TemplateNode,
|
component: TemplateNode,
|
||||||
checkers: [inputMultiVariableChecker],
|
checkers: {
|
||||||
|
add: [],
|
||||||
|
save: [inputMultiVariableChecker]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'switch-node',
|
key: 'switch-node',
|
||||||
@@ -98,7 +112,10 @@ export const NodeRegistry: NodeDefine[] = [
|
|||||||
icon: <i className="fa fa-code-fork"/>,
|
icon: <i className="fa fa-code-fork"/>,
|
||||||
description: '根据不同的情况前往不同的分支',
|
description: '根据不同的情况前往不同的分支',
|
||||||
component: SwitchNode,
|
component: SwitchNode,
|
||||||
checkers: [],
|
checkers: {
|
||||||
|
add: [],
|
||||||
|
save: [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'loop-node',
|
key: 'loop-node',
|
||||||
@@ -107,16 +124,35 @@ export const NodeRegistry: NodeDefine[] = [
|
|||||||
icon: <i className="fa fa-repeat"/>,
|
icon: <i className="fa fa-repeat"/>,
|
||||||
description: '实现循环执行流程',
|
description: '实现循环执行流程',
|
||||||
component: LoopNode,
|
component: LoopNode,
|
||||||
checkers: [],
|
checkers: {
|
||||||
|
add: [],
|
||||||
|
save: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// 特殊节点特殊判断
|
||||||
|
{
|
||||||
|
key: 'input-node',
|
||||||
|
group: '数据节点',
|
||||||
|
name: '输入',
|
||||||
|
icon: <i className="fa fa-file"/>,
|
||||||
|
description: '定义流程输入变量',
|
||||||
|
component: InputNode,
|
||||||
|
checkers: {
|
||||||
|
add: [noMoreThanOneNodeType],
|
||||||
|
save: [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'output-node',
|
key: 'output-node',
|
||||||
group: '输出节点',
|
group: '数据节点',
|
||||||
name: '输出',
|
name: '输出',
|
||||||
icon: <i className="fa fa-file"/>,
|
icon: <i className="fa fa-file"/>,
|
||||||
description: '定义输出变量',
|
description: '定义流程输出变量',
|
||||||
component: OutputNode,
|
component: OutputNode,
|
||||||
checkers: [inputSingleVariableChecker('output')],
|
checkers: {
|
||||||
|
add: [noMoreThanOneNodeType],
|
||||||
|
save: [inputSingleVariableChecker('output')]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -8,10 +8,10 @@ import {amisRender, commonInfo, horizontalFormOptions} from '../../../util/amis.
|
|||||||
import {generateAllIncomerOutputVariablesFormOptions} from '../Helper.tsx'
|
import {generateAllIncomerOutputVariablesFormOptions} from '../Helper.tsx'
|
||||||
import {useDataStore} from '../store/DataStore.ts'
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
import {useFlowStore} from '../store/FlowStore.ts'
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
|
import {type FormSchema, OutputVariableTypeMap} from '../types.ts'
|
||||||
|
|
||||||
export function inputsFormColumns(
|
export function inputsFormColumns(
|
||||||
nodeId: string,
|
nodeId: string,
|
||||||
inputSchema: Record<string, Record<string, any>>,
|
|
||||||
nodes: Node[],
|
nodes: Node[],
|
||||||
edges: Edge[],
|
edges: Edge[],
|
||||||
data: any,
|
data: any,
|
||||||
@@ -37,7 +37,6 @@ export function inputsFormColumns(
|
|||||||
selectMode: 'group',
|
selectMode: 'group',
|
||||||
options: generateAllIncomerOutputVariablesFormOptions(
|
options: generateAllIncomerOutputVariablesFormOptions(
|
||||||
nodeId,
|
nodeId,
|
||||||
inputSchema,
|
|
||||||
nodes,
|
nodes,
|
||||||
edges,
|
edges,
|
||||||
data,
|
data,
|
||||||
@@ -70,24 +69,11 @@ export function outputsFormColumns(editable: boolean = false, required: boolean
|
|||||||
label: '参数',
|
label: '参数',
|
||||||
required: true,
|
required: true,
|
||||||
selectFirst: true,
|
selectFirst: true,
|
||||||
options: [
|
options: Object.keys(OutputVariableTypeMap).map(key => ({
|
||||||
{
|
// @ts-ignore
|
||||||
label: '文本',
|
label: OutputVariableTypeMap[key],
|
||||||
value: 'string',
|
value: key,
|
||||||
},
|
})),
|
||||||
{
|
|
||||||
label: '数字',
|
|
||||||
value: 'number',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: '文本数组',
|
|
||||||
value: 'array-string',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: '对象数组',
|
|
||||||
value: 'array-object',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -100,7 +86,7 @@ type AmisNodeProps = {
|
|||||||
nodeProps: NodeProps
|
nodeProps: NodeProps
|
||||||
extraNodeDescription?: JSX.Element
|
extraNodeDescription?: JSX.Element
|
||||||
handler: JSX.Element
|
handler: JSX.Element
|
||||||
columnSchema?: () => Schema[]
|
formSchema?: () => FormSchema,
|
||||||
resize?: { minWidth: number, minHeight: number }
|
resize?: { minWidth: number, minHeight: number }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +120,7 @@ const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
|
|||||||
nodeProps,
|
nodeProps,
|
||||||
extraNodeDescription,
|
extraNodeDescription,
|
||||||
handler,
|
handler,
|
||||||
columnSchema,
|
formSchema,
|
||||||
resize,
|
resize,
|
||||||
}) => {
|
}) => {
|
||||||
const {removeNode} = useFlowStore()
|
const {removeNode} = useFlowStore()
|
||||||
@@ -148,6 +134,7 @@ const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
|
|||||||
const [editDrawerOpen, setEditDrawerOpen] = useState(false)
|
const [editDrawerOpen, setEditDrawerOpen] = useState(false)
|
||||||
const [editDrawerForm, setEditDrawerForm] = useState<JSX.Element>(<></>)
|
const [editDrawerForm, setEditDrawerForm] = useState<JSX.Element>(<></>)
|
||||||
const onOpenEditDrawerClick = useCallback(() => {
|
const onOpenEditDrawerClick = useCallback(() => {
|
||||||
|
const schema = formSchema?.()
|
||||||
setEditDrawerForm(
|
setEditDrawerForm(
|
||||||
amisRender(
|
amisRender(
|
||||||
{
|
{
|
||||||
@@ -178,6 +165,7 @@ const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
...(schema?.events ?? {})
|
||||||
},
|
},
|
||||||
body: [
|
body: [
|
||||||
{
|
{
|
||||||
@@ -195,7 +183,7 @@ const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
|
|||||||
{
|
{
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
...(columnSchema?.() ?? []),
|
...(schema?.columns ?? []),
|
||||||
{
|
{
|
||||||
type: 'wrapper',
|
type: 'wrapper',
|
||||||
size: 'none',
|
size: 'none',
|
||||||
@@ -233,7 +221,7 @@ const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
setEditDrawerOpen(true)
|
setEditDrawerOpen(true)
|
||||||
}, [nodeData])
|
}, [id])
|
||||||
const onRemoveClick = useCallback(() => {
|
const onRemoveClick = useCallback(() => {
|
||||||
removeNode(id)
|
removeNode(id)
|
||||||
removeDataById(id)
|
removeDataById(id)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import {Tag} from 'antd'
|
import {Tag} from 'antd'
|
||||||
import React, {useCallback, useEffect} from 'react'
|
import React, {useCallback, useMemo} from 'react'
|
||||||
import {useContextStore} from '../store/ContextStore.ts'
|
|
||||||
import {useDataStore} from '../store/DataStore.ts'
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
import {useFlowStore} from '../store/FlowStore.ts'
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
import type {FormSchema} from '../types.ts'
|
||||||
|
|
||||||
const languageMap: Record<string, string> = {
|
const languageMap: Record<string, string> = {
|
||||||
'javascript': 'Javascript',
|
'javascript': 'Javascript',
|
||||||
@@ -14,26 +14,13 @@ const languageMap: Record<string, string> = {
|
|||||||
|
|
||||||
const CodeNode = (props: NodeProps) => {
|
const CodeNode = (props: NodeProps) => {
|
||||||
const {getNodes, getEdges} = useFlowStore()
|
const {getNodes, getEdges} = useFlowStore()
|
||||||
const {getData, mergeDataById, getDataById} = useDataStore()
|
const {getData, getDataById} = useDataStore()
|
||||||
const {getInputSchema} = useContextStore()
|
|
||||||
|
|
||||||
const nodeData = getDataById(props.id)
|
const nodeData = getDataById(props.id)
|
||||||
|
|
||||||
useEffect(() => {
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
mergeDataById(
|
columns: [
|
||||||
props.id,
|
...inputsFormColumns(props.id, getNodes(), getEdges(), getData()),
|
||||||
{
|
|
||||||
outputs: {
|
|
||||||
result: {
|
|
||||||
type: 'string',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}, [props.id])
|
|
||||||
|
|
||||||
const columnsSchema = useCallback(() => [
|
|
||||||
...inputsFormColumns(props.id, getInputSchema(), getNodes(), getEdges(), getData()),
|
|
||||||
{
|
{
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
@@ -58,21 +45,25 @@ const CodeNode = (props: NodeProps) => {
|
|||||||
{
|
{
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
...outputsFormColumns(true, true),
|
...outputsFormColumns(true, false),
|
||||||
], [props.id])
|
]
|
||||||
return (
|
}), [props.id])
|
||||||
<AmisNode
|
|
||||||
className={nodeClassName('code')}
|
const extraNodeDescription = useMemo(() => {
|
||||||
nodeProps={props}
|
return nodeData?.type
|
||||||
extraNodeDescription={
|
|
||||||
nodeData?.type
|
|
||||||
? <div className="mt-2 flex justify-between">
|
? <div className="mt-2 flex justify-between">
|
||||||
<span>代码类型</span>
|
<span>代码类型</span>
|
||||||
<Tag className="m-0" color="blue">{languageMap[nodeData.type]}</Tag>
|
<Tag className="m-0" color="blue">{languageMap[nodeData.type]}</Tag>
|
||||||
</div>
|
</div>
|
||||||
: <></>
|
: <></>
|
||||||
}
|
}, [nodeData])
|
||||||
columnSchema={columnsSchema}
|
|
||||||
|
return (
|
||||||
|
<AmisNode
|
||||||
|
className={nodeClassName('code')}
|
||||||
|
nodeProps={props}
|
||||||
|
extraNodeDescription={extraNodeDescription}
|
||||||
|
formSchema={formSchema}
|
||||||
handler={<NormalNodeHandler/>}
|
handler={<NormalNodeHandler/>}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
123
service-web/client/src/components/flow/node/InputNode.tsx
Normal file
123
service-web/client/src/components/flow/node/InputNode.tsx
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import type {NodeProps} from '@xyflow/react'
|
||||||
|
import React, {useCallback} from 'react'
|
||||||
|
import AmisNode, {nodeClassName, outputsFormColumns, StartNodeHandler} from './AmisNode.tsx'
|
||||||
|
import {horizontalFormOptions} from '../../../util/amis.tsx'
|
||||||
|
import {typeMap} from '../../../pages/ai/task/InputSchema.tsx'
|
||||||
|
import type {FormSchema, OutputVariableType} from '../types.ts'
|
||||||
|
import {isEmpty} from 'licia'
|
||||||
|
|
||||||
|
const originTypeMap: Record<string, OutputVariableType> = {
|
||||||
|
text: 'text',
|
||||||
|
textarea: 'text',
|
||||||
|
number: 'number',
|
||||||
|
files: 'array-text',
|
||||||
|
}
|
||||||
|
|
||||||
|
const InputNode = (props: NodeProps) => {
|
||||||
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
|
events: {
|
||||||
|
change: {
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
actionType: 'validate',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
actionType: 'custom',
|
||||||
|
// @ts-ignore
|
||||||
|
script: (context, doAction, event) => {
|
||||||
|
let data = event?.data
|
||||||
|
console.log(data)
|
||||||
|
if (data && isEmpty(data?.validateResult?.error ?? undefined)) {
|
||||||
|
let inputs = data.validateResult?.payload?.inputs ?? {}
|
||||||
|
if (inputs) {
|
||||||
|
let outputs: Record<string, { type: OutputVariableType }> = {}
|
||||||
|
for (let key of Object.keys(inputs)) {
|
||||||
|
outputs[key] = {
|
||||||
|
type: originTypeMap[inputs[key].type],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
doAction({
|
||||||
|
actionType: 'setValue',
|
||||||
|
args: {
|
||||||
|
value: {
|
||||||
|
outputs
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
type: 'input-kvs',
|
||||||
|
name: 'inputs',
|
||||||
|
label: '输入变量',
|
||||||
|
required: true,
|
||||||
|
addButtonText: '新增入参',
|
||||||
|
draggable: false,
|
||||||
|
keyItem: {
|
||||||
|
label: '参数名称',
|
||||||
|
...horizontalFormOptions(),
|
||||||
|
validations: {
|
||||||
|
isAlphanumeric: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
valueItems: [
|
||||||
|
{
|
||||||
|
...horizontalFormOptions(),
|
||||||
|
type: 'input-text',
|
||||||
|
name: 'label',
|
||||||
|
required: true,
|
||||||
|
label: '中文名称',
|
||||||
|
clearValueOnEmpty: true,
|
||||||
|
clearable: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...horizontalFormOptions(),
|
||||||
|
type: 'input-text',
|
||||||
|
name: 'description',
|
||||||
|
label: '参数描述',
|
||||||
|
clearValueOnEmpty: true,
|
||||||
|
clearable: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...horizontalFormOptions(),
|
||||||
|
type: 'select',
|
||||||
|
name: 'type',
|
||||||
|
label: '参数类型',
|
||||||
|
required: true,
|
||||||
|
selectFirst: true,
|
||||||
|
options: Object.keys(typeMap).map(key => ({label: typeMap[key], value: key})),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...horizontalFormOptions(),
|
||||||
|
type: 'switch',
|
||||||
|
name: 'required',
|
||||||
|
label: '是否必填',
|
||||||
|
required: true,
|
||||||
|
value: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'divider',
|
||||||
|
},
|
||||||
|
...outputsFormColumns(false, false),
|
||||||
|
]
|
||||||
|
}), [props.id])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AmisNode
|
||||||
|
className={nodeClassName('input')}
|
||||||
|
nodeProps={props}
|
||||||
|
formSchema={formSchema}
|
||||||
|
handler={<StartNodeHandler/>}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default React.memo(InputNode)
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import React, {useCallback, useEffect} from 'react'
|
import React, {useCallback, useEffect} from 'react'
|
||||||
import {commonInfo} from '../../../util/amis.tsx'
|
import {commonInfo} from '../../../util/amis.tsx'
|
||||||
import {useContextStore} from '../store/ContextStore.ts'
|
|
||||||
import {useDataStore} from '../store/DataStore.ts'
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
import {useFlowStore} from '../store/FlowStore.ts'
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
import type {FormSchema} from '../types.ts'
|
||||||
|
|
||||||
const KnowledgeNode = (props: NodeProps) => {
|
const KnowledgeNode = (props: NodeProps) => {
|
||||||
const {getNodes, getEdges} = useFlowStore()
|
const {getNodes, getEdges} = useFlowStore()
|
||||||
const {getData, mergeDataById} = useDataStore()
|
const {getData, mergeDataById} = useDataStore()
|
||||||
const {getInputSchema} = useContextStore()
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
mergeDataById(
|
mergeDataById(
|
||||||
@@ -24,8 +23,9 @@ const KnowledgeNode = (props: NodeProps) => {
|
|||||||
)
|
)
|
||||||
}, [props.id])
|
}, [props.id])
|
||||||
|
|
||||||
const columnsSchema = useCallback(() => [
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
...inputsFormColumns(props.id, getInputSchema(), getNodes(), getEdges(), getData()),
|
columns: [
|
||||||
|
...inputsFormColumns(props.id, getNodes(), getEdges(), getData()),
|
||||||
{
|
{
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
@@ -76,12 +76,13 @@ const KnowledgeNode = (props: NodeProps) => {
|
|||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
...outputsFormColumns(false, true),
|
...outputsFormColumns(false, true),
|
||||||
], [props.id])
|
]
|
||||||
|
}), [props.id])
|
||||||
return (
|
return (
|
||||||
<AmisNode
|
<AmisNode
|
||||||
className={nodeClassName('knowledge')}
|
className={nodeClassName('knowledge')}
|
||||||
nodeProps={props}
|
nodeProps={props}
|
||||||
columnSchema={columnsSchema}
|
formSchema={formSchema}
|
||||||
handler={<NormalNodeHandler/>}
|
handler={<NormalNodeHandler/>}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import {Tag} from 'antd'
|
import {Tag} from 'antd'
|
||||||
import React, {useCallback, useEffect} from 'react'
|
import React, {useCallback, useEffect, useMemo} from 'react'
|
||||||
import {useContextStore} from '../store/ContextStore.ts'
|
|
||||||
import {useDataStore} from '../store/DataStore.ts'
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
import {useFlowStore} from '../store/FlowStore.ts'
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
import type {FormSchema} from '../types.ts'
|
||||||
|
|
||||||
const modelMap: Record<string, string> = {
|
const modelMap: Record<string, string> = {
|
||||||
qwen3: 'Qwen3',
|
qwen3: 'Qwen3',
|
||||||
@@ -14,7 +14,6 @@ const modelMap: Record<string, string> = {
|
|||||||
const LlmNode = (props: NodeProps) => {
|
const LlmNode = (props: NodeProps) => {
|
||||||
const {getNodes, getEdges} = useFlowStore()
|
const {getNodes, getEdges} = useFlowStore()
|
||||||
const {getData, mergeDataById, getDataById} = useDataStore()
|
const {getData, mergeDataById, getDataById} = useDataStore()
|
||||||
const {getInputSchema} = useContextStore()
|
|
||||||
|
|
||||||
const nodeData = getDataById(props.id)
|
const nodeData = getDataById(props.id)
|
||||||
|
|
||||||
@@ -24,15 +23,16 @@ const LlmNode = (props: NodeProps) => {
|
|||||||
{
|
{
|
||||||
outputs: {
|
outputs: {
|
||||||
text: {
|
text: {
|
||||||
type: 'string',
|
type: 'text',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}, [props.id])
|
}, [props.id])
|
||||||
|
|
||||||
const columnsSchema = useCallback(() => [
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
...inputsFormColumns(props.id, getInputSchema(), getNodes(), getEdges(), getData()),
|
columns: [
|
||||||
|
...inputsFormColumns(props.id, getNodes(), getEdges(), getData()),
|
||||||
{
|
{
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
@@ -54,20 +54,24 @@ const LlmNode = (props: NodeProps) => {
|
|||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
...outputsFormColumns(false, true),
|
...outputsFormColumns(false, true),
|
||||||
], [props.id])
|
]
|
||||||
return (
|
}), [props.id])
|
||||||
<AmisNode
|
|
||||||
className={nodeClassName('llm')}
|
const extraNodeDescription = useMemo(() => {
|
||||||
nodeProps={props}
|
return nodeData?.model
|
||||||
extraNodeDescription={
|
|
||||||
nodeData?.model
|
|
||||||
? <div className="mt-2 flex justify-between">
|
? <div className="mt-2 flex justify-between">
|
||||||
<span>模型名称</span>
|
<span>模型名称</span>
|
||||||
<Tag className="m-0" color="blue">{modelMap[nodeData.model]}</Tag>
|
<Tag className="m-0" color="blue">{modelMap[nodeData.model]}</Tag>
|
||||||
</div>
|
</div>
|
||||||
: <></>
|
: <></>
|
||||||
}
|
}, [nodeData])
|
||||||
columnSchema={columnsSchema}
|
|
||||||
|
return (
|
||||||
|
<AmisNode
|
||||||
|
className={nodeClassName('llm')}
|
||||||
|
nodeProps={props}
|
||||||
|
extraNodeDescription={extraNodeDescription}
|
||||||
|
formSchema={formSchema}
|
||||||
handler={<NormalNodeHandler/>}
|
handler={<NormalNodeHandler/>}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,25 +1,119 @@
|
|||||||
import {Background, BackgroundVariant, type NodeProps} from '@xyflow/react'
|
import {Background, BackgroundVariant, type NodeProps} from '@xyflow/react'
|
||||||
import {classnames} from 'amis'
|
import {classnames} from 'amis'
|
||||||
import React from 'react'
|
import React, {useCallback, useEffect, useMemo} from 'react'
|
||||||
import {flowBackgroundColor, flowDotColor} from '../types.ts'
|
import AddNodeButton from '../component/AddNodeButton.tsx'
|
||||||
import AmisNode, {nodeClassName, NormalNodeHandler} from './AmisNode.tsx'
|
import {generateAllIncomerOutputVariablesFormOptions} from '../Helper.tsx'
|
||||||
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
|
import {flowBackgroundColor, flowDotColor, type FormSchema} from '../types.ts'
|
||||||
|
import AmisNode, {nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
|
||||||
const LoopNode = (props: NodeProps) => {
|
const LoopNode = (props: NodeProps) => {
|
||||||
|
const {getNodes, getEdges} = useFlowStore()
|
||||||
|
const {getData, mergeDataById} = useDataStore()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
mergeDataById(
|
||||||
|
props.id,
|
||||||
|
{
|
||||||
|
failFast: true,
|
||||||
|
parallel: false,
|
||||||
|
type: 'for',
|
||||||
|
count: 1,
|
||||||
|
outputs: {
|
||||||
|
output: {
|
||||||
|
type: 'array-object',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}, [props.id])
|
||||||
|
|
||||||
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
type: 'switch',
|
||||||
|
name: 'failFast',
|
||||||
|
label: '快速失败',
|
||||||
|
required: true,
|
||||||
|
description: '执行过程中一旦出现错误,及时中断循环任务的执行',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
disabled: true,
|
||||||
|
type: 'switch',
|
||||||
|
name: 'parallel',
|
||||||
|
label: '并行执行',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'select',
|
||||||
|
name: 'type',
|
||||||
|
label: '循环模式',
|
||||||
|
required: true,
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
label: '次数循环',
|
||||||
|
value: 'for',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: '次数循环 (引用变量)',
|
||||||
|
value: 'for-variable',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: '对象循环',
|
||||||
|
value: 'for-object',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
visibleOn: '${type === \'for\'}',
|
||||||
|
type: 'input-number',
|
||||||
|
name: 'count',
|
||||||
|
label: '循环次数',
|
||||||
|
required: true,
|
||||||
|
min: 1,
|
||||||
|
precision: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
visibleOn: '${type === \'for-variable\'}',
|
||||||
|
type: 'select',
|
||||||
|
name: 'countVariable',
|
||||||
|
label: '循环次数',
|
||||||
|
required: true,
|
||||||
|
selectMode: 'group',
|
||||||
|
options: generateAllIncomerOutputVariablesFormOptions(
|
||||||
|
props.id,
|
||||||
|
getNodes(),
|
||||||
|
getEdges(),
|
||||||
|
getData(),
|
||||||
|
['number'],
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
visibleOn: '${type === \'for-object\'}',
|
||||||
|
type: 'select',
|
||||||
|
name: 'countObject',
|
||||||
|
label: '循环对象',
|
||||||
|
required: true,
|
||||||
|
selectMode: 'group',
|
||||||
|
options: generateAllIncomerOutputVariablesFormOptions(
|
||||||
|
props.id,
|
||||||
|
getNodes(),
|
||||||
|
getEdges(),
|
||||||
|
getData(),
|
||||||
|
['array-text', 'array-object'],
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'divider',
|
||||||
|
},
|
||||||
|
...outputsFormColumns(false, true),
|
||||||
|
]
|
||||||
|
}), [props.id])
|
||||||
|
|
||||||
|
const extraNodeDescription = useMemo(() => {
|
||||||
return (
|
return (
|
||||||
<AmisNode
|
<div className="nodrag relative w-full h-full" style={{minHeight: '211px'}}>
|
||||||
className={classnames('w-full', 'h-full', nodeClassName('loop'))}
|
|
||||||
style={{
|
|
||||||
minWidth: '256px',
|
|
||||||
minHeight: '110px'
|
|
||||||
}}
|
|
||||||
nodeProps={props}
|
|
||||||
extraNodeDescription={
|
|
||||||
<div
|
|
||||||
className="nodrag relative h-full w-full"
|
|
||||||
style={{
|
|
||||||
minHeight: '8rem',
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Background
|
<Background
|
||||||
id={`loop-background-${props.id}`}
|
id={`loop-background-${props.id}`}
|
||||||
className="rounded-xl"
|
className="rounded-xl"
|
||||||
@@ -32,12 +126,25 @@ const LoopNode = (props: NodeProps) => {
|
|||||||
color={flowDotColor}
|
color={flowDotColor}
|
||||||
bgColor={flowBackgroundColor}
|
bgColor={flowBackgroundColor}
|
||||||
/>
|
/>
|
||||||
|
<AddNodeButton className="mt-2 ml-2" parent={props.id} onlyIcon/>
|
||||||
</div>
|
</div>
|
||||||
}
|
)
|
||||||
|
}, [props.id])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AmisNode
|
||||||
|
className={classnames('w-full', 'h-full', nodeClassName('loop'))}
|
||||||
|
style={{
|
||||||
|
minWidth: '350px',
|
||||||
|
minHeight: '290px',
|
||||||
|
}}
|
||||||
|
nodeProps={props}
|
||||||
|
extraNodeDescription={extraNodeDescription}
|
||||||
|
formSchema={formSchema}
|
||||||
handler={<NormalNodeHandler/>}
|
handler={<NormalNodeHandler/>}
|
||||||
resize={{
|
resize={{
|
||||||
minWidth: 256,
|
minWidth: 350,
|
||||||
minHeight: 208,
|
minHeight: 290,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,41 +1,23 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import React, {useCallback} from 'react'
|
import React, {useCallback} from 'react'
|
||||||
import {generateAllIncomerOutputVariablesFormOptions} from '../Helper.tsx'
|
|
||||||
import {useContextStore} from '../store/ContextStore.ts'
|
|
||||||
import {useDataStore} from '../store/DataStore.ts'
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
import {useFlowStore} from '../store/FlowStore.ts'
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
import AmisNode, {EndNodeHandler, nodeClassName} from './AmisNode.tsx'
|
import type {FormSchema} from '../types.ts'
|
||||||
|
import AmisNode, {EndNodeHandler, inputsFormColumns, nodeClassName} from './AmisNode.tsx'
|
||||||
|
|
||||||
const OutputNode = (props: NodeProps) => {
|
const OutputNode = (props: NodeProps) => {
|
||||||
const {getNodes, getEdges} = useFlowStore()
|
const {getNodes, getEdges} = useFlowStore()
|
||||||
const {getData} = useDataStore()
|
const {getData} = useDataStore()
|
||||||
const {getInputSchema} = useContextStore()
|
|
||||||
|
|
||||||
const columnsSchema = useCallback(
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
() => [
|
columns: inputsFormColumns(props.id, getNodes(), getEdges(), getData()),
|
||||||
{
|
}), [props.id])
|
||||||
type: 'select',
|
|
||||||
name: 'output',
|
|
||||||
label: '输出变量',
|
|
||||||
required: true,
|
|
||||||
selectMode: 'group',
|
|
||||||
options: generateAllIncomerOutputVariablesFormOptions(
|
|
||||||
props.id,
|
|
||||||
getInputSchema(),
|
|
||||||
getNodes(),
|
|
||||||
getEdges(),
|
|
||||||
getData(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
[props.id],
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AmisNode
|
<AmisNode
|
||||||
className={nodeClassName('output')}
|
className={nodeClassName('output')}
|
||||||
nodeProps={props}
|
nodeProps={props}
|
||||||
columnSchema={columnsSchema}
|
formSchema={formSchema}
|
||||||
handler={<EndNodeHandler/>}
|
handler={<EndNodeHandler/>}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,48 +1,95 @@
|
|||||||
import {Handle, type NodeProps, Position} from '@xyflow/react'
|
import {Handle, type NodeProps, Position} from '@xyflow/react'
|
||||||
|
import type {ConditionValue} from 'amis'
|
||||||
import {Tag} from 'antd'
|
import {Tag} from 'antd'
|
||||||
import React from 'react'
|
import {contain, isEqual} from 'licia'
|
||||||
|
import React, {useCallback, useMemo} from 'react'
|
||||||
|
import {generateAllIncomerOutputVariablesConditions} from '../Helper.tsx'
|
||||||
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
import AmisNode, {nodeClassName} from './AmisNode.tsx'
|
import AmisNode, {nodeClassName} from './AmisNode.tsx'
|
||||||
|
import type {FormSchema} from '../types.ts'
|
||||||
const cases = [
|
|
||||||
{
|
|
||||||
index: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
index: 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
index: 3,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
const SwitchNode = (props: NodeProps) => {
|
const SwitchNode = (props: NodeProps) => {
|
||||||
|
const {getNodes, getEdges, removeEdges} = useFlowStore()
|
||||||
|
const {getData, getDataById} = useDataStore()
|
||||||
|
|
||||||
|
const nodeData = getDataById(props.id)
|
||||||
|
// @ts-ignore
|
||||||
|
const conditions: ConditionValue[] = nodeData?.conditions?.map(c => c.condition) ?? []
|
||||||
|
|
||||||
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
type: 'combo',
|
||||||
|
name: 'conditions',
|
||||||
|
label: '分支',
|
||||||
|
multiple: true,
|
||||||
|
required: true,
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
type: 'condition-builder',
|
||||||
|
name: 'condition',
|
||||||
|
label: '条件',
|
||||||
|
required: true,
|
||||||
|
builderMode: 'simple',
|
||||||
|
showANDOR: true,
|
||||||
|
fields: generateAllIncomerOutputVariablesConditions(
|
||||||
|
props.id,
|
||||||
|
getNodes(),
|
||||||
|
getEdges(),
|
||||||
|
getData(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}), [props.id])
|
||||||
|
|
||||||
|
const extraNodeDescription = useMemo(() => {
|
||||||
|
return (
|
||||||
|
<div className="mt-2">
|
||||||
|
{conditions.map((item, index) => (
|
||||||
|
<div key={item.id} className="mt-1">
|
||||||
|
<Tag className="m-0" color="blue">分支 {index + 1}</Tag>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}, [nodeData])
|
||||||
|
|
||||||
|
const handler = useMemo(() => {
|
||||||
|
// @ts-ignore
|
||||||
|
const conditions: ConditionValue[] = nodeData?.conditions?.map(c => c.condition) ?? []
|
||||||
|
|
||||||
|
// 移除不该存在的边
|
||||||
|
const conditionIds = conditions.map(c => c.id)
|
||||||
|
const removeEdgeIds = getEdges()
|
||||||
|
.filter(edge => isEqual(edge.source, props.id) && !contain(conditionIds, edge.sourceHandle))
|
||||||
|
.map(edge => edge.id)
|
||||||
|
removeEdges(removeEdgeIds)
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Handle type="target" position={Position.Left}/>
|
||||||
|
{conditions.map((item, index) => (
|
||||||
|
<Handle
|
||||||
|
type="source"
|
||||||
|
position={Position.Right}
|
||||||
|
key={item.id}
|
||||||
|
id={item.id}
|
||||||
|
style={{top: 91 + (26 * index)}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}, [nodeData])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AmisNode
|
<AmisNode
|
||||||
className={nodeClassName('switch')}
|
className={nodeClassName('switch')}
|
||||||
nodeProps={props}
|
nodeProps={props}
|
||||||
extraNodeDescription={
|
extraNodeDescription={extraNodeDescription}
|
||||||
<div className="mt-2">
|
formSchema={formSchema}
|
||||||
{cases.map(item => (
|
handler={handler}
|
||||||
<div key={item.index} className="mt-1">
|
|
||||||
<Tag className="m-0" color="blue">分支 {item.index}</Tag>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
handler={
|
|
||||||
<>
|
|
||||||
<Handle type="target" position={Position.Left}/>
|
|
||||||
{cases.map((item, index) => (
|
|
||||||
<Handle
|
|
||||||
type="source"
|
|
||||||
position={Position.Right}
|
|
||||||
key={item.index}
|
|
||||||
id={`${item.index}`}
|
|
||||||
style={{top: 85 + (25 * index)}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import {Tag} from 'antd'
|
import {Tag} from 'antd'
|
||||||
import React, {useCallback, useEffect} from 'react'
|
import React, {useCallback, useEffect, useMemo} from 'react'
|
||||||
import {useContextStore} from '../store/ContextStore.ts'
|
|
||||||
import {useDataStore} from '../store/DataStore.ts'
|
import {useDataStore} from '../store/DataStore.ts'
|
||||||
import {useFlowStore} from '../store/FlowStore.ts'
|
import {useFlowStore} from '../store/FlowStore.ts'
|
||||||
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {inputsFormColumns, nodeClassName, NormalNodeHandler, outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
import type {FormSchema} from '../types.ts'
|
||||||
|
|
||||||
const typeMap: Record<string, string> = {
|
const typeMap: Record<string, string> = {
|
||||||
default: '默认',
|
default: '默认',
|
||||||
@@ -16,7 +16,6 @@ const typeMap: Record<string, string> = {
|
|||||||
const TemplateNode = (props: NodeProps) => {
|
const TemplateNode = (props: NodeProps) => {
|
||||||
const {getNodes, getEdges} = useFlowStore()
|
const {getNodes, getEdges} = useFlowStore()
|
||||||
const {getData, getDataById, mergeDataById} = useDataStore()
|
const {getData, getDataById, mergeDataById} = useDataStore()
|
||||||
const {getInputSchema} = useContextStore()
|
|
||||||
|
|
||||||
const nodeData = getDataById(props.id)
|
const nodeData = getDataById(props.id)
|
||||||
|
|
||||||
@@ -26,16 +25,16 @@ const TemplateNode = (props: NodeProps) => {
|
|||||||
{
|
{
|
||||||
outputs: {
|
outputs: {
|
||||||
text: {
|
text: {
|
||||||
type: 'string',
|
type: 'text',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}, [props.id])
|
}, [props.id])
|
||||||
|
|
||||||
const columnsSchema = useCallback(
|
const formSchema: () => FormSchema = useCallback(() => ({
|
||||||
() => [
|
columns: [
|
||||||
...inputsFormColumns(props.id, getInputSchema(), getNodes(), getEdges(), getData()),
|
...inputsFormColumns(props.id, getNodes(), getEdges(), getData()),
|
||||||
{
|
{
|
||||||
type: 'divider',
|
type: 'divider',
|
||||||
},
|
},
|
||||||
@@ -69,23 +68,24 @@ const TemplateNode = (props: NodeProps) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
...outputsFormColumns(false, true),
|
...outputsFormColumns(false, true),
|
||||||
],
|
]
|
||||||
[props.id],
|
}), [props.id])
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
const extraNodeDescription = useMemo(() => {
|
||||||
<AmisNode
|
return nodeData?.type
|
||||||
className={nodeClassName('template')}
|
|
||||||
nodeProps={props}
|
|
||||||
extraNodeDescription={
|
|
||||||
nodeData?.type
|
|
||||||
? <div className="mt-2 flex justify-between">
|
? <div className="mt-2 flex justify-between">
|
||||||
<span>模板类型</span>
|
<span>模板类型</span>
|
||||||
<Tag className="m-0" color="blue">{typeMap[nodeData.type]}</Tag>
|
<Tag className="m-0" color="blue">{typeMap[nodeData.type]}</Tag>
|
||||||
</div>
|
</div>
|
||||||
: <></>
|
: <></>
|
||||||
}
|
}, [nodeData])
|
||||||
columnSchema={columnsSchema}
|
|
||||||
|
return (
|
||||||
|
<AmisNode
|
||||||
|
className={nodeClassName('template')}
|
||||||
|
nodeProps={props}
|
||||||
|
extraNodeDescription={extraNodeDescription}
|
||||||
|
formSchema={formSchema}
|
||||||
handler={<NormalNodeHandler/>}
|
handler={<NormalNodeHandler/>}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
import {create} from 'zustand/react'
|
|
||||||
|
|
||||||
export const useContextStore = create<{
|
|
||||||
inputSchema: Record<string, Record<string, any>>,
|
|
||||||
getInputSchema: () => Record<string, Record<string, any>>,
|
|
||||||
setInputSchema: (inputSchema: Record<string, Record<string, any>>) => void,
|
|
||||||
}>((set, get) => ({
|
|
||||||
inputSchema: {},
|
|
||||||
getInputSchema: () => get().inputSchema,
|
|
||||||
setInputSchema: (inputSchema: Record<string, Record<string, any>>) => set({inputSchema}),
|
|
||||||
}))
|
|
||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
type OnEdgesChange,
|
type OnEdgesChange,
|
||||||
type OnNodesChange,
|
type OnNodesChange,
|
||||||
} from '@xyflow/react'
|
} from '@xyflow/react'
|
||||||
import {filter, find, isEqual} from 'licia'
|
import {contain, filter, find, isEqual} from 'licia'
|
||||||
import {create} from 'zustand/react'
|
import {create} from 'zustand/react'
|
||||||
|
|
||||||
export const useFlowStore = create<{
|
export const useFlowStore = create<{
|
||||||
@@ -24,6 +24,8 @@ export const useFlowStore = create<{
|
|||||||
edges: Edge[],
|
edges: Edge[],
|
||||||
getEdges: () => Edge[],
|
getEdges: () => Edge[],
|
||||||
onEdgesChange: OnEdgesChange,
|
onEdgesChange: OnEdgesChange,
|
||||||
|
removeEdge: (id: string) => void,
|
||||||
|
removeEdges: (ids: string[]) => void,
|
||||||
setEdges: (edges: Edge[]) => void,
|
setEdges: (edges: Edge[]) => void,
|
||||||
|
|
||||||
onConnect: OnConnect,
|
onConnect: OnConnect,
|
||||||
@@ -61,6 +63,16 @@ export const useFlowStore = create<{
|
|||||||
edges: applyEdgeChanges(changes, get().edges),
|
edges: applyEdgeChanges(changes, get().edges),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
removeEdge: id => {
|
||||||
|
set({
|
||||||
|
edges: filter(get().edges, edge => !isEqual(edge.id, id)),
|
||||||
|
})
|
||||||
|
},
|
||||||
|
removeEdges: ids => {
|
||||||
|
set({
|
||||||
|
edges: filter(get().edges, edge => !contain(ids, edge.id)),
|
||||||
|
})
|
||||||
|
},
|
||||||
setEdges: edges => set({edges}),
|
setEdges: edges => set({edges}),
|
||||||
|
|
||||||
onConnect: connection => {
|
onConnect: connection => {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import type {Edge, Node} from '@xyflow/react'
|
import type {Edge, Node} from '@xyflow/react'
|
||||||
import type {JSX} from 'react'
|
import type {JSX} from 'react'
|
||||||
|
import type {ListenerAction, Schema} from 'amis'
|
||||||
|
|
||||||
export const flowBackgroundColor = "#fafafa"
|
export const flowBackgroundColor = '#fafafa'
|
||||||
export const flowDotColor = "#dedede"
|
export const flowDotColor = '#dedede'
|
||||||
|
|
||||||
export type InputFormOptions = {
|
export type InputFormOptions = {
|
||||||
label: string
|
label: string
|
||||||
@@ -19,16 +20,27 @@ export type NodeError = {
|
|||||||
message?: string,
|
message?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NodeChecker = (id: string, inputSchema: Record<string, Record<string, any>>, nodes: Node[], edges: Edge[], data: any) => NodeError
|
export type AddNodeChecker = (type: string, parentId: string | undefined, nodes: Node[], edges: Edge[], data: any) => NodeError
|
||||||
|
export type SaveNodeChecker = (id: string, parentId: string | undefined, nodes: Node[], edges: Edge[], data: any) => NodeError
|
||||||
|
|
||||||
export type GraphData = { nodes: Node[], edges: Edge[], data: any }
|
export type GraphData = { nodes: Node[], edges: Edge[], data: any }
|
||||||
|
|
||||||
export type FlowEditorProps = {
|
export type FlowEditorProps = {
|
||||||
inputSchema: Record<string, Record<string, any>>,
|
|
||||||
graphData: GraphData,
|
graphData: GraphData,
|
||||||
onGraphDataChange: (graphData: GraphData) => void,
|
onGraphDataChange: (graphData: GraphData) => void,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type OutputVariableType = 'text' | 'boolean' | 'number' | 'object' | 'array-text' | 'array-object'
|
||||||
|
|
||||||
|
export const OutputVariableTypeMap: Record<OutputVariableType, string> = {
|
||||||
|
'text': '文本',
|
||||||
|
'boolean': '布尔值',
|
||||||
|
'number': '数字',
|
||||||
|
'object': '对象',
|
||||||
|
'array-text': '文本数组',
|
||||||
|
'array-object': '对象数组',
|
||||||
|
}
|
||||||
|
|
||||||
export type NodeDefine = {
|
export type NodeDefine = {
|
||||||
key: string,
|
key: string,
|
||||||
group: string,
|
group: string,
|
||||||
@@ -36,5 +48,20 @@ export type NodeDefine = {
|
|||||||
icon: JSX.Element,
|
icon: JSX.Element,
|
||||||
description: string,
|
description: string,
|
||||||
component: any,
|
component: any,
|
||||||
checkers: NodeChecker[],
|
checkers: {
|
||||||
|
add: AddNodeChecker[],
|
||||||
|
save: SaveNodeChecker[],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export type OutputVariable = {
|
||||||
|
group: string,
|
||||||
|
name: string | undefined,
|
||||||
|
type: OutputVariableType,
|
||||||
|
variable: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FormSchema = {
|
||||||
|
events?: Record<string, { actions: ListenerAction[] }>
|
||||||
|
columns: Schema[]
|
||||||
}
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -42,7 +42,6 @@ function Conversation() {
|
|||||||
const [input, setInput] = useState<string>('')
|
const [input, setInput] = useState<string>('')
|
||||||
|
|
||||||
useUnmount(() => {
|
useUnmount(() => {
|
||||||
console.log('Page Unmount')
|
|
||||||
abortController.current?.abort()
|
abortController.current?.abort()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ const FlowTask: React.FC = () => {
|
|||||||
width: 200,
|
width: 200,
|
||||||
buttons: [
|
buttons: [
|
||||||
{
|
{
|
||||||
|
visibleOn: 'hasInput',
|
||||||
type: 'action',
|
type: 'action',
|
||||||
label: '查看',
|
label: '查看',
|
||||||
level: 'link',
|
level: 'link',
|
||||||
@@ -107,17 +108,18 @@ const FlowTask: React.FC = () => {
|
|||||||
return {
|
return {
|
||||||
...payload,
|
...payload,
|
||||||
data: {
|
data: {
|
||||||
...generateInputForm(payload.data, undefined, false, true),
|
...generateInputForm(payload.data ?? {}, undefined, false, true),
|
||||||
id: 'db8a4d10-0c47-4e27-b1a4-d0f2e1c15992',
|
id: 'db8a4d10-0c47-4e27-b1a4-d0f2e1c15992',
|
||||||
initApi: {
|
initApi: {
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: `${commonInfo.baseAiUrl}/flow_task/input_data/\${id}`,
|
url: `${commonInfo.baseAiUrl}/flow_task/input_data/\${id}`,
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
adaptor: (payload, response, api, context) => {
|
adaptor: (payload, response, api, context) => {
|
||||||
|
console.log(payload)
|
||||||
return {
|
return {
|
||||||
...payload,
|
...payload,
|
||||||
data: {
|
data: {
|
||||||
inputData: payload.data,
|
inputData: payload.data ?? {},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ const FlowTaskAdd: React.FC = () => {
|
|||||||
actionType: 'custom',
|
actionType: 'custom',
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
script: (context, doAction, event) => {
|
script: (context, doAction, event) => {
|
||||||
console.log(event)
|
|
||||||
let selectedIds = (event?.data?.selectedItems ?? []).map((item: any) => item.id)
|
let selectedIds = (event?.data?.selectedItems ?? []).map((item: any) => item.id)
|
||||||
if (!isEmpty(selectedIds)) {
|
if (!isEmpty(selectedIds)) {
|
||||||
doAction({
|
doAction({
|
||||||
@@ -160,7 +159,7 @@ const FlowTaskAdd: React.FC = () => {
|
|||||||
input: '${inputData|default:undefined}',
|
input: '${inputData|default:undefined}',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
...generateInputForm(payload.data, undefined, false),
|
...generateInputForm(payload.data ?? {}, undefined, false),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import {isEmpty, isEqual} from 'licia'
|
import {isEqual} from 'licia'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {useNavigate, useParams} from 'react-router'
|
import {useNavigate, useParams} from 'react-router'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
import {amisRender, commonInfo, horizontalFormOptions} from '../../../../util/amis.tsx'
|
import {amisRender, commonInfo, horizontalFormOptions} from '../../../../util/amis.tsx'
|
||||||
import {generateInputForm, typeMap} from '../InputSchema.tsx'
|
|
||||||
|
|
||||||
const TemplateEditDiv = styled.div`
|
const TemplateEditDiv = styled.div`
|
||||||
.antd-EditorControl {
|
.antd-EditorControl {
|
||||||
@@ -39,31 +38,6 @@ const FlowTaskTemplateEdit: React.FC = () => {
|
|||||||
wrapWithPanel: false,
|
wrapWithPanel: false,
|
||||||
...horizontalFormOptions(),
|
...horizontalFormOptions(),
|
||||||
onEvent: {
|
onEvent: {
|
||||||
change: {
|
|
||||||
actions: [
|
|
||||||
{
|
|
||||||
actionType: 'validate',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
actionType: 'custom',
|
|
||||||
// @ts-ignore
|
|
||||||
script: (context, doAction, event) => {
|
|
||||||
let data = event?.data ?? {}
|
|
||||||
let inputSchema = data.inputSchema ?? []
|
|
||||||
if (!isEmpty(inputSchema) && isEmpty(data?.validateResult?.error ?? undefined)) {
|
|
||||||
doAction({
|
|
||||||
actionType: 'setValue',
|
|
||||||
args: {
|
|
||||||
value: {
|
|
||||||
inputPreview: generateInputForm(inputSchema, '入参表单预览'),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
submitSucc: {
|
submitSucc: {
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
@@ -73,8 +47,8 @@ const FlowTaskTemplateEdit: React.FC = () => {
|
|||||||
navigate(-1)
|
navigate(-1)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
],
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
body: [
|
body: [
|
||||||
{
|
{
|
||||||
@@ -99,71 +73,6 @@ const FlowTaskTemplateEdit: React.FC = () => {
|
|||||||
maxLength: 500,
|
maxLength: 500,
|
||||||
showCounter: true,
|
showCounter: true,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
type: 'group',
|
|
||||||
body: [
|
|
||||||
{
|
|
||||||
type: 'wrapper',
|
|
||||||
size: 'none',
|
|
||||||
body: [
|
|
||||||
{
|
|
||||||
type: 'input-kvs',
|
|
||||||
name: 'inputSchema',
|
|
||||||
label: '输入变量',
|
|
||||||
addButtonText: '新增入参',
|
|
||||||
draggable: false,
|
|
||||||
keyItem: {
|
|
||||||
label: '参数名称',
|
|
||||||
...horizontalFormOptions(),
|
|
||||||
validations: {
|
|
||||||
isAlphanumeric: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
valueItems: [
|
|
||||||
{
|
|
||||||
...horizontalFormOptions(),
|
|
||||||
type: 'input-text',
|
|
||||||
name: 'label',
|
|
||||||
required: true,
|
|
||||||
label: '中文名称',
|
|
||||||
clearValueOnEmpty: true,
|
|
||||||
clearable: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
...horizontalFormOptions(),
|
|
||||||
type: 'input-text',
|
|
||||||
name: 'description',
|
|
||||||
label: '参数描述',
|
|
||||||
clearValueOnEmpty: true,
|
|
||||||
clearable: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
...horizontalFormOptions(),
|
|
||||||
type: 'select',
|
|
||||||
name: 'type',
|
|
||||||
label: '参数类型',
|
|
||||||
required: true,
|
|
||||||
selectFirst: true,
|
|
||||||
options: Object.keys(typeMap).map(key => ({label: typeMap[key], value: key})),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
...horizontalFormOptions(),
|
|
||||||
type: 'switch',
|
|
||||||
name: 'required',
|
|
||||||
label: '是否必填',
|
|
||||||
required: true,
|
|
||||||
value: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'amis',
|
|
||||||
name: 'inputPreview',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
type: 'button-toolbar',
|
type: 'button-toolbar',
|
||||||
buttons: [
|
buttons: [
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ import axios from 'axios'
|
|||||||
import React, {useState} from 'react'
|
import React, {useState} from 'react'
|
||||||
import {useNavigate, useParams} from 'react-router'
|
import {useNavigate, useParams} from 'react-router'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
import FlowEditor, {type GraphData} from '../../../../components/flow/FlowEditor.tsx'
|
import FlowEditor from '../../../../components/flow/FlowEditor.tsx'
|
||||||
|
import type {GraphData} from '../../../../components/flow/types.ts'
|
||||||
import {commonInfo} from '../../../../util/amis.tsx'
|
import {commonInfo} from '../../../../util/amis.tsx'
|
||||||
|
|
||||||
const FlowTaskTemplateFlowEditDiv = styled.div`
|
const FlowTaskTemplateFlowEditDiv = styled.div`
|
||||||
@@ -12,35 +13,32 @@ const FlowTaskTemplateFlowEditDiv = styled.div`
|
|||||||
const FlowTaskTemplateFlowEdit: React.FC = () => {
|
const FlowTaskTemplateFlowEdit: React.FC = () => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const {template_id} = useParams()
|
const {template_id} = useParams()
|
||||||
const [inputSchema, setInputSchema] = useState<Record<string, Record<string, any>>>({})
|
|
||||||
const [graphData, setGraphData] = useState<GraphData>({nodes: [], edges: [], data: {}})
|
const [graphData, setGraphData] = useState<GraphData>({nodes: [], edges: [], data: {}})
|
||||||
|
|
||||||
useMount(async () => {
|
useMount(async () => {
|
||||||
let {data} = await axios.get(
|
let {data} = await axios.get(
|
||||||
`${commonInfo.baseAiUrl}/flow_task/template/detail/${template_id}`,
|
`${commonInfo.baseAiUrl}/flow_task/template/detail/${template_id}`,
|
||||||
{
|
{
|
||||||
headers: commonInfo.authorizationHeaders
|
headers: commonInfo.authorizationHeaders,
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
setInputSchema(data?.data?.inputSchema)
|
|
||||||
setGraphData(data?.data?.flowGraph)
|
setGraphData(data?.data?.flowGraph)
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FlowTaskTemplateFlowEditDiv className="h-full w-full">
|
<FlowTaskTemplateFlowEditDiv className="h-full w-full">
|
||||||
<FlowEditor
|
<FlowEditor
|
||||||
inputSchema={inputSchema}
|
|
||||||
graphData={graphData}
|
graphData={graphData}
|
||||||
onGraphDataChange={async data => {
|
onGraphDataChange={async data => {
|
||||||
await axios.post(
|
await axios.post(
|
||||||
`${commonInfo.baseAiUrl}/flow_task/template/update_flow_graph`,
|
`${commonInfo.baseAiUrl}/flow_task/template/update_flow_graph`,
|
||||||
{
|
{
|
||||||
id: template_id,
|
id: template_id,
|
||||||
graph: data
|
graph: data,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: commonInfo.authorizationHeaders
|
headers: commonInfo.authorizationHeaders,
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
navigate(-1)
|
navigate(-1)
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user