MCPcopy Create free account
hub / github.com/QuantumNous/new-api / UploadTaskPlugin

Function UploadTaskPlugin

controller/task_plugin.go:35–86  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

33}
34
35func UploadTaskPlugin(c *gin.Context) {
36 var request taskPluginUploadRequest
37 if err := c.ShouldBindJSON(&request); err != nil {
38 common.ApiErrorMsg(c, err.Error())
39 return
40 }
41 if len(request.Source) > maxTaskPluginSourceBytes {
42 common.ApiErrorMsg(c, "plugin source exceeds 1 MiB")
43 return
44 }
45 if expected := strings.TrimSpace(request.SourceSha256); expected != "" {
46 actual := fmt.Sprintf("%x", sha256.Sum256([]byte(request.Source)))
47 if !strings.EqualFold(actual, expected) {
48 common.ApiErrorMsg(c, "plugin source sha256 mismatch")
49 return
50 }
51 }
52 temporary := jsplugin.NewRegistry()
53 loaded, err := temporary.Register(request.Source, jsplugin.Options{})
54 if err != nil {
55 common.ApiErrorMsg(c, err.Error())
56 return
57 }
58 if err = jsplugin.ValidateV1Meta(loaded.Meta); err != nil {
59 common.ApiErrorMsg(c, err.Error())
60 return
61 }
62 enabled := true
63 if request.Enabled != nil {
64 enabled = *request.Enabled
65 }
66 if enabled && !request.Force {
67 if err = jsplugin.PreflightRoutingConflict(jsplugin.DefaultRegistry.Generation(), loaded); err != nil {
68 common.ApiErrorMsg(c, err.Error())
69 return
70 }
71 }
72 plugin := model.TaskPlugin{
73 Key: loaded.Meta.Key, APIVersion: loaded.Meta.APIVersion, Version: loaded.Meta.Version,
74 Source: request.Source, SourceHash: fmt.Sprintf("%x", sha256.Sum256([]byte(request.Source))),
75 Enabled: enabled, Remark: request.Remark,
76 }
77 if err = model.SaveTaskPlugin(&plugin); err != nil {
78 common.ApiError(c, err)
79 return
80 }
81 if err = syncTaskPluginsOnceContext(c.Request.Context()); err != nil {
82 common.ApiError(c, err)
83 return
84 }
85 common.ApiSuccess(c, taskPluginDetail{Plugin: &plugin, Meta: loaded.Meta, Source: plugin.Source, Layer: "override"})
86}
87
88func GetTaskPluginVersions(c *gin.Context) {
89 plugins, err := model.ListTaskPluginVersions(c.Param("key"))

Calls 11

RegisterMethod · 0.95
ApiErrorMsgFunction · 0.92
NewRegistryFunction · 0.92
ValidateV1MetaFunction · 0.92
PreflightRoutingConflictFunction · 0.92
SaveTaskPluginFunction · 0.92
ApiErrorFunction · 0.92
ApiSuccessFunction · 0.92
GenerationMethod · 0.80
ErrorMethod · 0.45