-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
149 lines (129 loc) · 4.69 KB
/
index.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
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
import { ModelMessage } from "../models/types";
import { Plugin } from "../plugins/index";
import { PluginInvocation, PluginOutput } from "../plugins/types";
import { MessageRoles } from "../types";
import { AgentCallbacks } from "./types";
export abstract class Agent {
abstract basePrompt: () => string;
abstract detectPluginUse: (response: string) => false | PluginInvocation;
abstract handlePluginOutput: (input: PluginInvocation, output: PluginOutput) => void;
abstract run(prompt: string, role?: MessageRoles): void;
plugins: Plugin[] = [];
handlers: AgentCallbacks[] = [];
messages: ModelMessage[] = [];
verbose: boolean = false;
pluginDetectRegex: RegExp | null = null;
currentStreamingMessage: ModelMessage | null = null;
apiSpecModel: (invoke: PluginInvocation) => Promise<PluginInvocation> = async (invoke) => invoke;
constructor(plugins: Plugin[]) {
this.plugins = plugins;
}
async init() {
this.messages = [...(await this.metaprompt())];
}
metaprompt: () => Promise<ModelMessage[]> = async () => [
{
role: "system",
content: this.basePrompt(),
},
];
filterPluginInvocation = (input: string): string => {
if (!this.pluginDetectRegex) {
return input;
}
return input.replace(this.pluginDetectRegex, "");
};
addHandler = (callbacks: AgentCallbacks) => this.handlers.push(callbacks);
onError = (err: any) => {
console.log("Error: " + JSON.stringify(err));
this.handlers.forEach((h) => (h.onError ? h.onError(err.message) : null));
};
onPluginStart = (input: PluginInvocation) =>
this.handlers.forEach((h) => (h.onPluginStart ? h.onPluginStart(input) : null));
onPluginFinish = (input: PluginInvocation) =>
this.handlers.forEach((h) => (h.onPluginFinish ? h.onPluginFinish(input) : null));
onPluginError = (input: PluginInvocation, err: any) =>
this.handlers.forEach((h) => (h.onPluginError ? h.onPluginError(input, err) : null));
onPluginMessage = (input: PluginInvocation, output: PluginOutput) => {
this.handlePluginOutput(input, output);
this.handlers.forEach((h) => (h.onPluginMessage ? h.onPluginMessage(input, output) : null));
};
onStart = () => this.handlers.forEach((h) => (h.onStart ? h.onStart() : null));
onFinish = () => {
if (this.currentStreamingMessage) {
this.onMessage
? this.onMessage({
role: this.currentStreamingMessage.role,
content: this.currentStreamingMessage.content,
})
: null;
}
this.handlers.forEach((h) => (h.onFinish ? h.onFinish() : null));
};
onToken = (delta: ModelMessage) => {
if (undefined === delta.content && undefined === delta.role) {
return;
}
this.handlers.forEach((h) => (h.onToken ? h.onToken(delta) : null));
if (!this.currentStreamingMessage) {
this.currentStreamingMessage = {
content: "",
};
}
if (delta.role) {
this.currentStreamingMessage.role = delta.role;
} else if (delta.content && delta.content != undefined) {
this.currentStreamingMessage.content += delta.content;
}
};
onMessage = (msg: ModelMessage): void => {
if (undefined === msg.content) {
return;
}
this.messages.push(msg);
if (this.verbose) {
console.log(`Message: ${msg.content}`);
}
if (this.currentStreamingMessage) {
this.currentStreamingMessage = null;
} else {
this.handlers.forEach((h) =>
h.onMessage
? h.onMessage({
role: msg.role,
content: this.filterPluginInvocation(msg.content?.trim() || ""),
})
: null
);
}
const pluginInvocation = this.detectPluginUse(msg.content);
if (pluginInvocation) {
const plugin = this.plugins.find(
(p) => p.manifest.name_for_model.toUpperCase() === pluginInvocation.name.toUpperCase()
);
if (plugin) {
this.onPluginStart(pluginInvocation);
this.apiSpecModel({
name: plugin.manifest.name_for_model,
action: pluginInvocation.action,
input: pluginInvocation.input,
})
.then((expanded: PluginInvocation) => {
plugin.run(expanded.action, pluginInvocation.input).then((result) => {
if (result.error) {
this.onPluginError(pluginInvocation, result.error);
} else {
this.onPluginMessage(pluginInvocation, result);
}
this.onPluginFinish(pluginInvocation);
});
})
.catch(() => {
this.onPluginError(pluginInvocation, "Failed to expand input with API spec model");
});
} else {
this.onError(`No plugin found for ${pluginInvocation.name}`);
}
}
};
}