-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTaskMessageInput.tsx
More file actions
118 lines (105 loc) · 2.95 KB
/
TaskMessageInput.tsx
File metadata and controls
118 lines (105 loc) · 2.95 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
import {
getTaskLabel,
getTaskPermissions,
isTaskWorking,
type Task,
} from "@repo/shared";
import { logger } from "@repo/webview-shared/logger";
import { useMutation } from "@tanstack/react-query";
import { useState } from "react";
import { useTasksApi } from "../hooks/useTasksApi";
import { PromptInput, type PromptInputProps } from "./PromptInput";
type ActionProps = Pick<
PromptInputProps,
| "onSubmit"
| "disabled"
| "loading"
| "actionIcon"
| "actionLabel"
| "actionEnabled"
>;
function getPlaceholder(task: Task): string {
switch (task.status) {
case "paused":
return "Resume the task to send messages";
case "initializing":
case "pending":
return "Waiting for the agent to start...";
case "error":
case "unknown":
return "Task is in an error state and cannot receive messages";
case "active":
break;
}
switch (task.current_state?.state) {
case "working":
return "Agent is working — you can pause or wait for it to finish...";
case "complete":
return "Task completed — send a follow-up to continue...";
case "failed":
return "Task failed — send a message to retry...";
default:
return "Send a message to the agent...";
}
}
interface TaskMessageInputProps {
task: Task;
}
export function TaskMessageInput({ task }: TaskMessageInputProps) {
const api = useTasksApi();
const [message, setMessage] = useState("");
const { mutate: pauseTask, isPending: isPausing } = useMutation({
mutationFn: () =>
api.pauseTask({ taskId: task.id, taskName: getTaskLabel(task) }),
onError: (err) => logger.error("Failed to pause task", err),
});
const { mutate: resumeTask, isPending: isResuming } = useMutation({
mutationFn: () =>
api.resumeTask({ taskId: task.id, taskName: getTaskLabel(task) }),
onError: (err) => logger.error("Failed to resume task", err),
});
const { mutate: sendMessage, isPending: isSending } = useMutation({
mutationFn: (msg: string) =>
api.sendTaskMessage({ taskId: task.id, message: msg }),
onSuccess: () => setMessage(""),
onError: (err) => logger.error("Failed to send message", err),
});
const { canPause, canResume, canSendMessage } = getTaskPermissions(task);
let actionProps: ActionProps;
if (isTaskWorking(task) && canPause) {
actionProps = {
onSubmit: pauseTask,
loading: isPausing,
actionIcon: "debug-pause",
actionLabel: "Pause task",
disabled: false,
actionEnabled: true,
};
} else if (canResume) {
actionProps = {
onSubmit: resumeTask,
loading: isResuming,
actionIcon: "debug-start",
actionLabel: "Resume task",
disabled: true,
actionEnabled: true,
};
} else {
actionProps = {
onSubmit: () => sendMessage(message),
loading: isSending,
actionIcon: "send",
actionLabel: "Send message",
disabled: !canSendMessage,
actionEnabled: canSendMessage && message.trim().length > 0,
};
}
return (
<PromptInput
placeholder={getPlaceholder(task)}
value={message}
onChange={setMessage}
{...actionProps}
/>
);
}