-
Notifications
You must be signed in to change notification settings - Fork 139
/
base.ts
55 lines (49 loc) · 1.53 KB
/
base.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
import {
ToolEmitter,
StringToolOutput,
Tool,
ToolInput,
ToolInputValidationError,
} from "bee-agent-framework/tools/base";
import { z } from "zod";
import { randomInteger } from "remeda";
import { Emitter } from "bee-agent-framework/emitter/emitter";
export class RiddleTool extends Tool<StringToolOutput> {
name = "Riddle";
description = "It generates a random puzzle to test your knowledge.";
public readonly emitter: ToolEmitter<ToolInput<this>, StringToolOutput> = Emitter.root.child({
namespace: ["tool", "riddle"],
creator: this,
});
inputSchema() {
return z.object({
index: z
.number()
.int()
.min(0)
.max(RiddleTool.data.length - 1)
.optional(),
});
}
public static data = [
"What has hands but can’t clap?",
"What has a face and two hands but no arms or legs?",
"What gets wetter the more it dries?",
"What has to be broken before you can use it?",
"What has a head, a tail, but no body?",
"The more you take, the more you leave behind. What am I?",
"What goes up but never comes down?",
];
static {
// Makes the class serializable
this.register();
}
protected async _run(input: ToolInput<this>): Promise<StringToolOutput> {
const index = input.index ?? randomInteger(0, RiddleTool.data.length - 1);
const riddle = RiddleTool.data[index];
if (!riddle) {
throw new ToolInputValidationError(`Riddle with such index (${index}) does not exist!`);
}
return new StringToolOutput(riddle);
}
}