-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathirctool.go
307 lines (266 loc) · 7.86 KB
/
irctool.go
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"encoding/json"
"fmt"
"log"
ai "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
)
type IrcOpTool struct {
}
func RegisterIrcTools(registry *ToolRegistry) {
log.Println("registering irc tools")
registry.RegisterTool("irc_mode", &IrcOpTool{})
registry.RegisterTool("irc_kick", &IrcKickTool{})
registry.RegisterTool("irc_topic", &IrcTopicTool{})
registry.RegisterTool("irc_self_operator", &IrcOperTool{})
registry.RegisterTool("irc_action", &IrcActionTool{})
}
func (t *IrcOpTool) GetTool() (ai.Tool, error) {
return ai.Tool{
Type: ai.ToolTypeFunction,
Function: &ai.FunctionDefinition{
Name: "irc_mode",
Description: "grants or removes irc ops to a nick",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"nick": {
Type: jsonschema.String,
Description: "the irc nickname of the user to grant or revoke ops",
},
"op": {
Type: jsonschema.Boolean,
Description: "determines if it is a grant or revoke of ops",
},
},
Required: []string{"nick", "op"},
},
}}, nil
}
func (t *IrcOpTool) Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error) {
type kickRequest struct {
Nick string `json:"nick"`
Op bool `json:"op"`
}
var req kickRequest
err := json.Unmarshal([]byte(tool.Function.Arguments), &req)
if err != nil {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Content: "failed to unmarshal arguments" + err.Error(),
Name: tool.Function.Name}, err
}
if !ctx.IsAdmin() {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Name: tool.Function.Name,
Content: ctx.GetSource() + "doesn't have admin permission to perform this action."}, fmt.Errorf("unauthorized")
}
// set opcmd to the appropriate value
opcmd := "-o"
if req.Op {
opcmd = "+o"
}
ctx.Mode(ctx.GetConfig().Server.Channel, opcmd, req.Nick)
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
Content: "success",
Name: tool.Function.Name,
ToolCallID: tool.ID,
}, nil
}
type IrcKickTool struct {
}
func (t *IrcKickTool) GetTool() (ai.Tool, error) {
return ai.Tool{
Type: ai.ToolTypeFunction,
Function: &ai.FunctionDefinition{
Name: "irc_kick",
Description: "kicks a nick from the channel",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"nick": {
Type: jsonschema.String,
Description: "the irc nickname to kick",
},
"reason": {
Type: jsonschema.String,
Description: "the reason for the kick",
},
},
Required: []string{"nick", "reason"},
},
}}, nil
}
func (t *IrcKickTool) Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error) {
type kickRequest struct {
Nick string `json:"nick"`
Reason string `json:"reason"`
}
var req kickRequest
err := json.Unmarshal([]byte(tool.Function.Arguments), &req)
if err != nil {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Name: tool.Function.Name,
Content: "failed to unmarshal arguments" + err.Error(),
}, err
}
if !ctx.IsAdmin() {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
Name: tool.Function.Name,
ToolCallID: tool.ID,
Content: ctx.GetSource() + "doesn't have admin permission to perform this action.",
}, fmt.Errorf("unauthorized")
}
ctx.Kick(ctx.GetConfig().Server.Channel, req.Nick, req.Reason)
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
Content: "success",
ToolCallID: tool.ID,
Name: tool.Function.Name,
}, nil
}
type IrcTopicTool struct {
}
func (t *IrcTopicTool) GetTool() (ai.Tool, error) {
return ai.Tool{
Type: ai.ToolTypeFunction,
Function: &ai.FunctionDefinition{
Name: "irc_topic",
Description: "sets the channel topic",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"topic": {
Type: jsonschema.String,
Description: "the new channel topic",
},
},
Required: []string{"topic"},
},
}}, nil
}
func (t *IrcTopicTool) Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error) {
type topicRequest struct {
Topic string `json:"topic"`
}
var req topicRequest
err := json.Unmarshal([]byte(tool.Function.Arguments), &req)
if err != nil {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Name: tool.Function.Name,
Content: "failed to unmarshal arguments" + err.Error(),
}, err
}
if !ctx.IsAdmin() {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Name: tool.Function.Name,
Content: ctx.GetSource() + " has no admin permission to perform this action.",
}, fmt.Errorf("unauthorized")
}
ctx.Topic(ctx.GetConfig().Server.Channel, req.Topic)
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
Content: "success",
ToolCallID: tool.ID,
Name: tool.Function.Name,
}, nil
}
type IrcOperTool struct {
}
func (t *IrcOperTool) GetTool() (ai.Tool, error) {
return ai.Tool{
Type: ai.ToolTypeFunction,
Function: &ai.FunctionDefinition{
Name: "irc_self_operator",
Description: "grants the chatbot /oper operator status on the server (irc operator)",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"reason": {
Type: jsonschema.String,
Description: "the reason for the chatbot asking for operator status",
},
},
Required: []string{"reason"},
},
}}, nil
}
func (t *IrcOperTool) Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error) {
type operRequest struct {
Reason string `json:"reason"`
}
var req operRequest
err := json.Unmarshal([]byte(tool.Function.Arguments), &req)
if err != nil {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Name: tool.Function.Name,
Content: "failed to unmarshal arguments" + err.Error(),
}, err
}
ctx.Oper("", "")
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
Content: "success",
ToolCallID: tool.ID,
Name: tool.Function.Name,
}, nil
}
type IrcActionTool struct {
}
func (t *IrcActionTool) GetTool() (ai.Tool, error) {
return ai.Tool{
Type: ai.ToolTypeFunction,
Function: &ai.FunctionDefinition{
Name: "irc_action",
Description: "sends an irc action message to the channel",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"message": {
Type: jsonschema.String,
Description: "the action message to send",
},
},
Required: []string{"message"},
},
}}, nil
}
func (t *IrcActionTool) Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error) {
type actionRequest struct {
Message string `json:"message"`
}
var req actionRequest
err := json.Unmarshal([]byte(tool.Function.Arguments), &req)
if err != nil {
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
ToolCallID: tool.ID,
Name: tool.Function.Name,
Content: "failed to unmarshal arguments" + err.Error(),
}, err
}
ctx.Action(ctx.GetConfig().Server.Channel, req.Message)
return ai.ChatCompletionMessage{
Role: ai.ChatMessageRoleTool,
Content: "success. reply with simple confirmation.",
ToolCallID: tool.ID,
Name: tool.Function.Name,
}, nil
}
func checkBotOp(_ ChatContext) error {
return nil
}