Skip to content

Commit

Permalink
- refactor(job): from data
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisGray0626 committed Jun 28, 2024
1 parent 77466bf commit 4174d2a
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 99 deletions.
8 changes: 4 additions & 4 deletions src/components/BreakdownChart.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts" setup>
import G6 from "@antv/g6";
import { onMounted } from "vue";
import { useDataStore } from "@/store/data.ts";
import {onMounted} from "vue";
import {useJobStore} from "@/store/job.ts";
const props = defineProps<{
graphId: string;
}>();
const dataStore = useDataStore();
const dataStore = useJobStore();
const getGraphNum = function () {
return "mountNode-" + props.graphId;
Expand All @@ -34,7 +34,7 @@ onMounted(() => {
},
});
tree.data(dataStore.getTreeData);
tree.data(dataStore.breakdownData);
tree.render();
tree.fitView(50);
});
Expand Down
8 changes: 4 additions & 4 deletions src/components/WorkflowChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
@Date: 2024/6/7
-->
<script setup lang="ts">
import { onMounted } from "vue";
import {onMounted} from "vue";
import G6 from "@antv/g6";
import { useDataStore } from "@/store/data.ts";
import {useJobStore} from "@/store/job.ts";

const props = defineProps<{
graphId: string;
}>();

const dataStore = useDataStore();
const dataStore = useJobStore();

const getGraphNum = function () {
return "mountNode-" + props.graphId;
Expand All @@ -39,7 +39,7 @@ onMounted(() => {
},
});

graph.data(dataStore.getGraphData);
graph.data(dataStore.workflowData);
graph.render();

const group = graph.get("edgeGroup");
Expand Down
23 changes: 0 additions & 23 deletions src/store/data.ts

This file was deleted.

43 changes: 43 additions & 0 deletions src/store/job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {defineStore} from "pinia";
import {job2G6Tree, jobLeafNode2G6Graph} from "@/utils/graphUtil.ts";
import {computed} from "vue";
import {mockFetchJob} from "@/api/fetch.ts";
import {Task} from "@/type.ts";
// import axios from "@/api";

export const useJobStore = defineStore("job", () => {
let _job;

Check failure on line 9 in src/store/job.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Variable '_job' implicitly has type 'any' in some locations where its type cannot be determined.

async function fetchData(task: string) {
const res = ((await mockFetchJob(task)) as { data: any }).data;
// localData = (await axios.fetchJob(`{"task":"${task}"}`)).data;
console.debug("res", res);
_job = convertData(res);

function convertData(data: any) {
const children = data.subtasks;
const task = new Task(data.task, data.toolId, data.toolName);

if (children && children.length > 0) {
for (let i = 0; i < children.length; i++) {
task.addChild(convertData(children[i]));
}
}
return task;
}
}

const breakdownData = computed(() => job2G6Tree(_job));

Check failure on line 30 in src/store/job.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Variable '_job' implicitly has an 'any' type.

const workflowData = computed(() => jobLeafNode2G6Graph(_job));

Check failure on line 32 in src/store/job.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Variable '_job' implicitly has an 'any' type.

const job = computed(() => {
if (!_job) {

Check failure on line 35 in src/store/job.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Variable '_job' implicitly has an 'any' type.
return new Task();

Check failure on line 36 in src/store/job.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Expected 3 arguments, but got 0.
} else {
return _job;
}
})

return {fetchData, job, breakdownData, workflowData};
});
14 changes: 7 additions & 7 deletions src/store/session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import { generateId } from "@/utils/graphUtil";
import { useDataStore } from "@/store/data.ts";
import {defineStore} from "pinia";
import {reactive, ref} from "vue";
import {generateId} from "@/utils/graphUtil";
import {useJobStore} from "@/store/job.ts";

type Session = {
sessionId: string;
Expand All @@ -10,7 +10,7 @@ type Session = {
};

export const useSessionStore = defineStore("session", () => {
const dataStore = useDataStore();
const dataStore = useJobStore();

const session = reactive<Session>({
sessionId: generateId(),
Expand All @@ -20,8 +20,8 @@ export const useSessionStore = defineStore("session", () => {
const graphShow = ref(false);

async function chatted() {
if (!dataStore.localData.task) {
await dataStore.getData(session.question);
if (!dataStore.job.name) {
await dataStore.fetchData(session.question);
}
// session.question = "";
graphShow.value = true;
Expand Down
51 changes: 35 additions & 16 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
export type Task = {
task: string;
toolId: string;
toolName: string;
subtasks: Task[];
};
export class Task {
name: string;
tool: Tool;

Check failure on line 3 in src/type.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Property 'tool' has no initializer and is not definitely assigned in the constructor.
children: Task[];

constructor(task: string, toolId: string, toolName: string) {
this.name = task;
this.initTool(toolId, toolName);
this.children = [] as Task[];
}

initTool(toolId: string, toolName: string) {
this.tool = new Tool(toolId, toolName);
}

addChild(task: Task) {
this.children.push(task);
}
}

export class Tool {
id: string;
name: string;
description: string;

Check failure on line 24 in src/type.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Property 'description' has no initializer and is not definitely assigned in the constructor.
args: {

Check failure on line 25 in src/type.ts

View workflow job for this annotation

GitHub Actions / build (18.x, chrisgray0626/gis-agent-frontend, 0.1)

Property 'args' has no initializer and is not definitely assigned in the constructor.
name: string;
type: string;
description: string;
}[];

constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
}

// export type Tool = {
// id: string;
// name: string;
// description: string;
// args: {
// name: string;
// type: string;
// description: string;
// }[];
// };
export type Node = {
id: string;
name: string;
Expand Down
80 changes: 35 additions & 45 deletions src/utils/graphUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,70 @@
* @Author: Chris
* @Date: 2024/6/7
*/
import G6, { TreeGraphData } from "@antv/g6";
import { GraphData } from "@antv/g6-core/lib/types";
import { Task } from "@/type.ts";
import G6, {TreeGraphData} from "@antv/g6";
import {GraphData} from "@antv/g6-core/lib/types";
import {Task} from "@/type.ts";

// export type Tool = {
// id: string;
// name: string;
// description: string;
// args: {
// name: string;
// type: string;
// description: string;
// }[];
// };
// type TreeNode = {
// label: string;
// toolId: string;
// toolName: string;
// children: TreeNode[];
// };
export function job2G6Tree(job: Task) {

export function jobData2Tree(data: Task) {
const { subtasks } = data;
const graphData: TreeGraphData = {
id: generateId(),
label: data.task,
toolId: data.toolId,
toolName: data.toolName,
children: [],
};
if (subtasks && subtasks.length > 0) {
for (let i = 0; i < subtasks.length; i++) {
graphData.children!.push(jobData2Tree(subtasks[i]));
function buildTreeNode(task: Task) {
const {children} = task;
const treeNode: TreeGraphData = {
id: generateId(),
label: task.name,
toolId: task.tool.id,
toolName: task.tool.name,
children: [] as TreeGraphData[],
};
if (children && children.length > 0) {
for (let i = 0; i < children.length; i++) {
treeNode.children!.push(buildTreeNode(children[i]));
}
}
return treeNode;
}
return graphData;

return buildTreeNode(job);
}

export function tree2Graph(treeData: TreeGraphData) {
const finalTaskList: GraphData = {
export function jobLeafNode2G6Graph(job: Task) {
const leafNodes: GraphData = {
nodes: [],
edges: [],
};
function buildNode(nodes: TreeGraphData) {
const { id, label, toolId, toolName, children } = nodes;

function buildNode(task: Task) {
const {children} = task;
const node = {
id,
label,
toolId,
toolName,
id: generateId(),
label: task.name,
toolId: task.tool.id,
toolName: task.tool.name,
};
if (children && children.length > 0) {
children.forEach((child) => {
buildNode(child);
});
} else {
finalTaskList.nodes!.push(node);
leafNodes.nodes!.push(node);
}
}
function buildEdge(nodeData: GraphData) {
if (nodeData.nodes) {
for (let i = 1; i < nodeData.nodes.length; i++) {
finalTaskList.edges!.push({
leafNodes.edges!.push({
id: generateId(),
source: nodeData.nodes[i - 1].id,
target: nodeData.nodes[i].id,
});
}
}
}
buildNode(treeData);
buildEdge(finalTaskList);

return finalTaskList;
buildNode(job);
buildEdge(leafNodes);

return leafNodes;
}

/**
Expand Down

0 comments on commit 4174d2a

Please sign in to comment.