-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplugin_marketplace.go
More file actions
281 lines (265 loc) · 8.1 KB
/
Copy pathplugin_marketplace.go
File metadata and controls
281 lines (265 loc) · 8.1 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
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
package main
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const (
openAICuratedMarketplaceName = "openai-curated"
openAIPluginsZipURL = "https://codeload.github.com/openai/plugins/zip/refs/heads/main"
openAIPluginsDownloadLimitBytes = 128 * 1024 * 1024
)
type pluginMarketplaceStatus struct {
CodexHome string `json:"codexHome"`
MarketplaceRoot *string `json:"marketplaceRoot,omitempty"`
ConfigRegistered bool `json:"configRegistered"`
NeedsRepair bool `json:"needsRepair"`
}
type pluginMarketplaceRepairPayload struct {
CodexHome string `json:"codexHome"`
MarketplaceRoot *string `json:"marketplaceRoot,omitempty"`
Initialized bool `json:"initialized"`
Configured bool `json:"configured"`
NeedsRepair bool `json:"needsRepair"`
}
func (s *server) pluginMarketplaceStatus() commandResult {
status := openAICuratedMarketplaceStatus(codexHomeDir())
message := "插件市场已可用。"
if status.NeedsRepair {
message = "插件市场需要初始化或注册。"
}
payload := map[string]any{
"codexHome": status.CodexHome,
"marketplaceRoot": nullableStringPtr(status.MarketplaceRoot),
"configRegistered": status.ConfigRegistered,
"needsRepair": status.NeedsRepair,
}
return ok(message, payload)
}
func (s *server) repairPluginMarketplace(ctx context.Context) commandResult {
home := codexHomeDir()
initialized := false
if localOpenAICuratedMarketplaceRoot(home) == "" {
if err := initializeOpenAICuratedMarketplaceFromGitHub(ctx, home); err != nil {
status := openAICuratedMarketplaceStatus(home)
return failed("插件市场修复失败:"+err.Error(), map[string]any{
"codexHome": status.CodexHome,
"marketplaceRoot": nullableStringPtr(status.MarketplaceRoot),
"initialized": initialized,
"configured": status.ConfigRegistered,
"needsRepair": status.NeedsRepair,
})
}
initialized = true
}
configured, err := ensureOpenAICuratedMarketplaceConfig(home)
if err != nil {
status := openAICuratedMarketplaceStatus(home)
return failed("插件市场修复失败:"+err.Error(), map[string]any{
"codexHome": status.CodexHome,
"marketplaceRoot": nullableStringPtr(status.MarketplaceRoot),
"initialized": initialized,
"configured": status.ConfigRegistered,
"needsRepair": status.NeedsRepair,
})
}
status := openAICuratedMarketplaceStatus(home)
message := "插件市场已可用,无需修复。"
if initialized {
message = "插件市场已从 openai/plugins 初始化并注册。"
} else if configured {
message = "已注册本地插件市场。"
}
return ok(message, map[string]any{
"codexHome": status.CodexHome,
"marketplaceRoot": nullableStringPtr(status.MarketplaceRoot),
"initialized": initialized,
"configured": configured,
"needsRepair": false,
})
}
func openAICuratedMarketplaceStatus(home string) pluginMarketplaceStatus {
root := localOpenAICuratedMarketplaceRoot(home)
var rootPtr *string
if root != "" {
rootPtr = &root
}
registered := root != "" && marketplaceConfigPointsToRoot(home, openAICuratedMarketplaceName, root)
return pluginMarketplaceStatus{
CodexHome: home,
MarketplaceRoot: rootPtr,
ConfigRegistered: registered,
NeedsRepair: root == "" || !registered,
}
}
func localOpenAICuratedMarketplaceRoot(home string) string {
root := filepath.Join(home, ".tmp", "plugins")
manifestPath := filepath.Join(root, ".agents", "plugins", "marketplace.json")
data, err := os.ReadFile(manifestPath)
if err != nil {
return ""
}
var manifest map[string]any
if json.Unmarshal(data, &manifest) != nil {
return ""
}
if stringFromAny(manifest["name"]) != openAICuratedMarketplaceName {
return ""
}
plugins, _ := manifest["plugins"].([]any)
if len(plugins) == 0 || !isDir(filepath.Join(root, "plugins")) {
return ""
}
return root
}
func marketplaceConfigPointsToRoot(home, name, root string) bool {
contents := readFile(filepath.Join(home, "config.toml"))
values := tableValues(contents, "marketplaces."+name)
return strings.TrimSpace(unquoteToml(values["source_type"])) == "local" && samePath(strings.TrimSpace(unquoteToml(values["source"])), root)
}
func ensureOpenAICuratedMarketplaceConfig(home string) (bool, error) {
root := localOpenAICuratedMarketplaceRoot(home)
if root == "" {
return false, nil
}
path := filepath.Join(home, "config.toml")
contents := readFile(path)
updated := repairCodexMarketplaceTable(contents, marketplaceSpec{Name: openAICuratedMarketplaceName, Source: root})
if updated == contents {
return false, nil
}
if _, err := writeCodexConfigWithBackup(path, updated, "plugin-marketplace"); err != nil {
return false, err
}
return true, nil
}
func initializeOpenAICuratedMarketplaceFromGitHub(ctx context.Context, home string) error {
bytes, err := downloadOpenAIPluginsZip(ctx)
if err != nil {
return err
}
return installOpenAIPluginsZip(home, bytes)
}
func downloadOpenAIPluginsZip(ctx context.Context) ([]byte, error) {
client := &http.Client{Timeout: 90 * time.Second}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, openAIPluginsZipURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("accept", "application/zip")
req.Header.Set("user-agent", appName+"/"+version)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("openai/plugins marketplace download returned HTTP %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, openAIPluginsDownloadLimitBytes+1))
if err != nil {
return nil, err
}
if len(body) > openAIPluginsDownloadLimitBytes {
return nil, fmt.Errorf("openai/plugins marketplace download is too large: %d bytes", len(body))
}
return body, nil
}
func installOpenAIPluginsZip(home string, data []byte) error {
destination := filepath.Join(home, ".tmp", "plugins")
stagingParent := filepath.Join(home, ".tmp")
if err := os.MkdirAll(stagingParent, 0o755); err != nil {
return err
}
staging := filepath.Join(stagingParent, fmt.Sprintf("plugins-download-%d", time.Now().UnixMilli()))
if err := os.MkdirAll(staging, 0o755); err != nil {
return err
}
cleanup := true
defer func() {
if cleanup {
_ = os.RemoveAll(staging)
}
}()
if err := extractOpenAIPluginsZip(data, staging); err != nil {
return err
}
if localOpenAICuratedMarketplaceRoot(staging) == "" {
return errors.New("downloaded openai/plugins marketplace is invalid")
}
if err := replaceDirectory(destination, staging); err != nil {
return err
}
cleanup = false
return nil
}
func extractOpenAIPluginsZip(data []byte, destination string) error {
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return err
}
for _, file := range reader.File {
relative, ok := zipEntryRelativePath(file.Name)
if !ok {
continue
}
target := filepath.Join(destination, relative)
if file.FileInfo().IsDir() {
if err := os.MkdirAll(target, file.Mode().Perm()); err != nil {
return err
}
continue
}
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
input, err := file.Open()
if err != nil {
return err
}
err = writeZipFile(target, input, file.Mode().Perm())
closeErr := input.Close()
if err != nil {
return err
}
if closeErr != nil {
return closeErr
}
}
return nil
}
func zipEntryRelativePath(name string) (string, bool) {
name = filepath.ToSlash(strings.TrimSpace(name))
parts := strings.Split(name, "/")
if len(parts) < 2 {
return "", false
}
relativeParts := parts[1:]
for _, part := range relativeParts {
if part == "" || part == "." || part == ".." {
return "", false
}
}
return filepath.Join(relativeParts...), true
}
func writeZipFile(path string, input io.Reader, perm os.FileMode) error {
output, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, perm)
if err != nil {
return err
}
_, copyErr := io.Copy(output, input)
closeErr := output.Close()
if copyErr != nil {
return copyErr
}
return closeErr
}