-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·204 lines (176 loc) · 6.13 KB
/
Copy pathmain.go
File metadata and controls
executable file
·204 lines (176 loc) · 6.13 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
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
// Note: run `go doc -all` in this package to see all of the types and functions available.
// ./pdk.gen.go contains the domain types from the host where your plugin will run.
package main
import (
"fmt"
"github.com/extism/go-pdk"
)
// Called when the tool is invoked.
// If you support multiple tools, you must switch on the input.params.name to detect which tool is being called.
// The name will match one of the tool names returned from "describe".
// It takes CallToolRequest as input (The incoming tool request from the LLM)
// And returns CallToolResult (The servlet's response to the given tool call)
func Call(input CallToolRequest) (CallToolResult, error) {
apiKey, ok := pdk.GetConfig("api-key")
if !ok {
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some("No api-key configured"),
}},
}, nil
}
args := input.Params.Arguments.(map[string]interface{})
pdk.Log(pdk.LogDebug, fmt.Sprint("Args: ", args))
switch input.Params.Name {
case ListIssuesTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
return issueList(apiKey, owner, repo, args)
case GetIssueTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
issue, _ := args["issue"].(float64)
return issueGet(apiKey, owner, repo, int(issue))
case AddIssueCommentTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
issue, _ := args["issue"].(float64)
body, _ := args["body"].(string)
return issueAddComment(apiKey, owner, repo, int(issue), body)
case CreateIssueTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
data := issueFromArgs(args)
return issueCreate(apiKey, owner, repo, data)
case UpdateIssueTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
issue, _ := args["issue"].(float64)
data := issueFromArgs(args)
return issueUpdate(apiKey, owner, repo, int(issue), data)
case GetFileContentsTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
path, _ := args["path"].(string)
branch, _ := args["branch"].(string)
res := filesGetContents(apiKey, owner, repo, path, &branch)
return res, nil
case CreateOrUpdateFileTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
path, _ := args["path"].(string)
file := fileCreateFromArgs(args)
return filesCreateOrUpdate(apiKey, owner, repo, path, file)
case CreateBranchTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
from, _ := args["branch"].(string)
var maybeBranch *string
if branch, ok := args["from_branch"].(string); ok {
maybeBranch = &branch
}
return branchCreate(apiKey, owner, repo, from, maybeBranch), nil
case ListPullRequestsTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
return pullRequestList(apiKey, owner, repo, args)
case CreatePullRequestTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
pr := branchPullRequestSchemaFromArgs(args)
return branchCreatePullRequest(apiKey, owner, repo, pr), nil
case PushFilesTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
branch, _ := args["branch"].(string)
message, _ := args["message"].(string)
files := filePushFromArgs(args)
return filesPush(apiKey, owner, repo, branch, message, files), nil
case ListReposTool.Name:
owner, _ := args["owner"].(string)
return reposList(apiKey, owner, args)
case GetRepositoryCollaboratorsTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
return reposGetCollaborators(apiKey, owner, repo, args)
case GetRepositoryContributorsTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
return reposGetContributors(apiKey, owner, repo, args)
case GetRepositoryDetailsTool.Name:
owner, _ := args["owner"].(string)
repo, _ := args["repo"].(string)
return reposGetDetails(apiKey, owner, repo)
case CreateGistTool.Name:
description, _ := args["description"].(string)
files, _ := args["files"].(map[string]any)
return gistCreate(apiKey, description, files), nil
case GetGistTool.Name:
gistId, _ := args["gist_id"].(string)
return gistGet(apiKey, gistId), nil
case UpdateGistTool.Name:
gistId, _ := args["gist_id"].(string)
description, _ := args["description"].(string)
files, _ := args["files"].(map[string]any)
return gistUpdate(apiKey, gistId, description, files), nil
case DeleteGistTool.Name:
gistId, _ := args["gist_id"].(string)
return gistDelete(apiKey, gistId), nil
default:
return CallToolResult{
IsError: some(true),
Content: []Content{{
Type: ContentTypeText,
Text: some("Unknown tool " + input.Params.Name),
}},
}, nil
}
}
func Describe() (ListToolsResult, error) {
toolsets := [][]ToolDescription{
IssueTools,
FileTools,
BranchTools,
RepoTools,
GistTools,
}
tools := []ToolDescription{}
for _, toolset := range toolsets {
tools = append(tools, toolset...)
}
// Ensure each tool's InputSchema has a required field
for i := range tools {
// Check if InputSchema is a map[string]interface{}
if schema, ok := tools[i].InputSchema.(map[string]interface{}); ok {
// Check if required field is missing
if _, exists := schema["required"]; !exists {
// Add an empty required array if it doesn't exist
schema["required"] = []string{}
tools[i].InputSchema = schema
}
}
}
return ListToolsResult{
Tools: tools,
}, nil
}
func some[T any](t T) *T {
return &t
}
type SchemaProperty struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
AdditionalProperties *schema `json:"additionalProperties,omitempty"`
Items *schema `json:"items,omitempty"`
}
func prop(tpe, description string) SchemaProperty {
return SchemaProperty{Type: tpe, Description: description}
}
func arrprop(tpe, description, itemstpe string) SchemaProperty {
items := schema{"type": itemstpe}
return SchemaProperty{Type: tpe, Description: description, Items: &items}
}
type schema = map[string]interface{}
type props = map[string]SchemaProperty