-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
agentTypes.ts
100 lines (85 loc) 路 2.11 KB
/
agentTypes.ts
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
import { z } from "zod";
/* Message & Task Type */
export const [
MESSAGE_TYPE_GOAL,
MESSAGE_TYPE_THINKING,
MESSAGE_TYPE_TASK,
MESSAGE_TYPE_ACTION,
MESSAGE_TYPE_SYSTEM,
] = [
"goal" as const,
"thinking" as const,
"task" as const,
"action" as const,
"system" as const,
];
export const [
TASK_STATUS_STARTED,
TASK_STATUS_EXECUTING,
TASK_STATUS_COMPLETED,
TASK_STATUS_FINAL,
] = [
"started" as const,
"executing" as const,
"completed" as const,
"final" as const,
];
const TaskStatusSchema = z.union([
z.literal(TASK_STATUS_STARTED),
z.literal(TASK_STATUS_EXECUTING),
z.literal(TASK_STATUS_COMPLETED),
z.literal(TASK_STATUS_FINAL),
z.literal(""),
]);
export type TaskStatus = z.infer<typeof TaskStatusSchema>;
export const messageSchemaBase = z.object({
value: z.string(),
info: z.string().optional().nullable(),
});
export const taskSchema = z
.object({
taskId: z.string().optional(),
type: z.literal(MESSAGE_TYPE_TASK),
status: TaskStatusSchema,
})
.merge(messageSchemaBase);
export const nonTaskScehma = z
.object({
type: z.union([
z.literal(MESSAGE_TYPE_GOAL),
z.literal(MESSAGE_TYPE_THINKING),
z.literal(MESSAGE_TYPE_ACTION),
z.literal(MESSAGE_TYPE_SYSTEM),
]),
})
.merge(messageSchemaBase);
export const messageSchema = z.union([taskSchema, nonTaskScehma]);
export type Task = z.infer<typeof taskSchema>;
export type Message = z.infer<typeof messageSchema>;
/* Agent Type */
export const [AGENT_STATUS_RUNNING, AGENT_STATUS_STOPPED] = [
"running" as const,
"stopped" as const,
];
export type AgentStatus =
| typeof AGENT_STATUS_RUNNING
| typeof AGENT_STATUS_STOPPED;
/* Type Predicates */
export const isTask = (value: unknown): value is Task => {
try {
taskSchema.parse(value);
return true;
} catch (err) {
return false;
}
};
/* Helper Functions */
export const getTaskStatus = (value: unknown): string | undefined => {
if (!isTask(value)) {
return;
}
return value.status;
};
export const isAction = (value: unknown): boolean => {
return isTask(value) && value.status === TASK_STATUS_COMPLETED;
};