-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathagentActions.ts
More file actions
278 lines (239 loc) · 8.5 KB
/
agentActions.ts
File metadata and controls
278 lines (239 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Composable for handling AI agent action suggestions in the UI
*/
import { ref } from "vue";
import { useRouter } from "vue-router/composables";
import { parse } from "yaml";
import { type DynamicUnprivilegedToolCreatePayload, GalaxyApi } from "@/api";
import { useConfig } from "@/composables/config";
import { useToast } from "@/composables/toast";
import { useUnprivilegedToolStore } from "@/stores/unprivilegedToolStore";
/* eslint-disable no-unused-vars */
// Action types from backend - values are used in switch/case and icon maps
export enum ActionType {
TOOL_RUN = "tool_run",
DOCUMENTATION = "documentation",
CONTACT_SUPPORT = "contact_support",
VIEW_EXTERNAL = "view_external",
SAVE_TOOL = "save_tool",
REFINE_QUERY = "refine_query",
WORKFLOW_IMPORT = "workflow_import",
}
/* eslint-enable no-unused-vars */
export interface ActionSuggestion {
action_type: ActionType;
description: string;
parameters: Record<string, any>;
confidence: "low" | "medium" | "high";
priority: number;
}
export interface AgentResponse {
content: string;
agent_type: string;
confidence: "low" | "medium" | "high";
suggestions: ActionSuggestion[];
metadata: Record<string, any>;
reasoning?: string;
}
export function useAgentActions() {
const router = useRouter();
const toast = useToast();
const { config } = useConfig();
const unprivilegedToolStore = useUnprivilegedToolStore();
const processingAction = ref(false);
/**
* Handle an action suggestion from an agent response
*/
async function handleAction(action: ActionSuggestion, agentResponse: AgentResponse) {
processingAction.value = true;
try {
switch (action.action_type) {
case ActionType.TOOL_RUN:
await handleToolRun(action);
break;
case ActionType.SAVE_TOOL:
await handleSaveTool(agentResponse);
break;
case ActionType.CONTACT_SUPPORT:
handleContactSupport();
break;
case ActionType.REFINE_QUERY:
toast.info("Please refine your query with more details");
break;
case ActionType.VIEW_EXTERNAL:
handleViewExternal(action);
break;
case ActionType.DOCUMENTATION:
handleDocumentation(action);
break;
case ActionType.WORKFLOW_IMPORT:
await handleWorkflowImport(action);
break;
default:
// Unknown actions default to contact support
console.warn(`Unknown action type: ${action.action_type}, redirecting to support`);
handleContactSupport();
}
} catch (error) {
console.error("Error handling action:", error);
const errorMessage = error instanceof Error ? error.message : String(error);
toast.error(`Failed to perform action: ${errorMessage}`);
} finally {
processingAction.value = false;
}
}
/**
* Handle TOOL_RUN action - navigate to tool with parameters
*/
async function handleToolRun(action: ActionSuggestion) {
const toolId = action.parameters.tool_id;
const params = action.parameters.tool_params || {};
if (!toolId) {
toast.error("No tool ID provided for tool run action");
return;
}
// Navigate to tool panel with the tool ID
router.push({
path: "/",
query: {
tool_id: toolId,
...params,
},
});
toast.success(`Opening tool: ${toolId}`);
}
/**
* Handle SAVE_TOOL action - save custom tool as unprivileged user tool
*/
async function handleSaveTool(agentResponse: AgentResponse) {
const toolYaml = agentResponse.metadata?.tool_yaml;
if (!toolYaml) {
toast.error("No tool YAML provided for save action");
return;
}
try {
const representation = parse(toolYaml);
const payload: DynamicUnprivilegedToolCreatePayload = {
active: true,
hidden: false,
representation,
src: "representation",
};
const { data, error } = await GalaxyApi().POST("/api/unprivileged_tools", { body: payload });
if (error) {
toast.error(`Failed to save tool: ${String(error)}`);
return;
}
toast.success(`Tool "${data.representation.name}" saved successfully!`);
unprivilegedToolStore.load(true);
router.push(`/tools/editor/${data.uuid}`);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : String(err);
toast.error(`Error saving tool: ${errorMessage}`);
}
}
/**
* Handle CONTACT_SUPPORT action - use configured support URL or default
*/
function handleContactSupport() {
const supportUrl = config.value.support_url || "https://galaxyproject.org/support/";
window.open(supportUrl, "_blank");
toast.info("Opening Galaxy support page");
}
/**
* Handle VIEW_EXTERNAL action - open external URL in new tab
*/
function handleViewExternal(action: ActionSuggestion) {
const url = action.parameters.url;
if (!url) {
toast.error("No URL provided for external view action");
return;
}
// Open URL in new tab
window.open(url, "_blank");
toast.success("Opening in new tab");
}
/**
* Handle WORKFLOW_IMPORT action - import an IWC workflow by trsID
*/
async function handleWorkflowImport(action: ActionSuggestion) {
const trsId = action.parameters.trs_id;
const name = action.parameters.name || "IWC workflow";
if (!trsId) {
toast.error("No trs_id provided for workflow import action");
return;
}
const { data, error } = await GalaxyApi().POST("/api/workflows/from_iwc", {
body: { trs_id: trsId },
});
if (error) {
toast.error(`Failed to import ${name}: ${String(error)}`);
return;
}
if (data.missing_tools && data.missing_tools.length > 0) {
toast.warning(
`${data.name} imported, but ${data.missing_tools.length} tool(s) are not installed on this server.`,
);
} else {
toast.success(`Imported ${data.name} from IWC`);
}
router.push(`/workflows/edit?id=${data.id}`);
}
/**
* Handle DOCUMENTATION action - open tool documentation
*/
function handleDocumentation(action: ActionSuggestion) {
const toolId = action.parameters.tool_id;
if (toolId && toolId !== "unknown") {
// Navigate to tool help page
router.push({
path: "/",
query: {
tool_id: toolId,
show_help: "true",
},
});
toast.info(`Opening documentation for ${toolId}`);
} else {
// Open general Galaxy documentation
window.open("https://training.galaxyproject.org/", "_blank");
toast.info("Opening Galaxy Training Network");
}
}
/**
* Get action icon based on type
*/
function getActionIcon(actionType: ActionType): string {
const icons: Record<ActionType, string> = {
[ActionType.TOOL_RUN]: "🔧",
[ActionType.SAVE_TOOL]: "💾",
[ActionType.DOCUMENTATION]: "📖",
[ActionType.CONTACT_SUPPORT]: "🆘",
[ActionType.REFINE_QUERY]: "✏️",
[ActionType.VIEW_EXTERNAL]: "🔗",
[ActionType.WORKFLOW_IMPORT]: "📥",
};
return icons[actionType] || "❓";
}
/**
* Get action button variant based on priority
*/
function getActionVariant(priority: number): string {
switch (priority) {
case 1:
return "primary";
case 2:
return "secondary";
case 3:
return "info";
default:
return "light";
}
}
return {
processingAction,
handleAction,
getActionIcon,
getActionVariant,
};
}