|
| 1 | +// Copyright 2026 The OpenAgent Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package object |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "mime" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/ThinkInAIXYZ/go-mcp/protocol" |
| 26 | + "github.com/the-open-agent/openagent/tool" |
| 27 | + "github.com/the-open-agent/openagent/util" |
| 28 | +) |
| 29 | + |
| 30 | +type generatedResourceArchiveBuiltinTool struct { |
| 31 | + inner tool.BuiltinTool |
| 32 | +} |
| 33 | + |
| 34 | +var archiveGeneratedResourceFile = archiveGeneratedResourceFileToStorage |
| 35 | + |
| 36 | +func wrapGeneratedResourceBuiltin(builtin tool.BuiltinTool) tool.BuiltinTool { |
| 37 | + if builtin == nil { |
| 38 | + return nil |
| 39 | + } |
| 40 | + if !isGeneratedResourceTool(builtin.GetName()) { |
| 41 | + return builtin |
| 42 | + } |
| 43 | + return &generatedResourceArchiveBuiltinTool{inner: builtin} |
| 44 | +} |
| 45 | + |
| 46 | +func isGeneratedResourceTool(toolName string) bool { |
| 47 | + switch toolName { |
| 48 | + case "pptx_write", "word_write", "excel_write": |
| 49 | + return true |
| 50 | + default: |
| 51 | + return false |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (t *generatedResourceArchiveBuiltinTool) GetName() string { |
| 56 | + return t.inner.GetName() |
| 57 | +} |
| 58 | + |
| 59 | +func (t *generatedResourceArchiveBuiltinTool) GetDescription() string { |
| 60 | + return t.inner.GetDescription() |
| 61 | +} |
| 62 | + |
| 63 | +func (t *generatedResourceArchiveBuiltinTool) GetInputSchema() interface{} { |
| 64 | + return t.inner.GetInputSchema() |
| 65 | +} |
| 66 | + |
| 67 | +func (t *generatedResourceArchiveBuiltinTool) Execute(ctx context.Context, arguments map[string]interface{}) (*protocol.CallToolResult, error) { |
| 68 | + result, innerErr := t.inner.Execute(ctx, arguments) |
| 69 | + if innerErr != nil || result == nil || result.IsError { |
| 70 | + return result, innerErr |
| 71 | + } |
| 72 | + |
| 73 | + path, ok := generatedResourceTargetPath(t.GetName(), arguments) |
| 74 | + if !ok { |
| 75 | + return result, innerErr |
| 76 | + } |
| 77 | + |
| 78 | + resource, err := archiveGeneratedResourceFile(path) |
| 79 | + if err != nil { |
| 80 | + appendGeneratedResourceArchiveText(result, fmt.Sprintf("Resource archive warning: file was created but could not be saved to Resources: %s", err.Error())) |
| 81 | + return result, innerErr |
| 82 | + } |
| 83 | + if resource == nil { |
| 84 | + appendGeneratedResourceArchiveText(result, "Resource archive warning: file was created but no Resource record was returned") |
| 85 | + return result, innerErr |
| 86 | + } |
| 87 | + |
| 88 | + appendGeneratedResourceArchiveText(result, fmt.Sprintf("Saved to Resources: %s", resource.Url)) |
| 89 | + return result, innerErr |
| 90 | +} |
| 91 | + |
| 92 | +func generatedResourceTargetPath(toolName string, arguments map[string]interface{}) (string, bool) { |
| 93 | + switch toolName { |
| 94 | + case "pptx_write", "word_write", "excel_write": |
| 95 | + path := resourceArchiveStringArg(arguments, "path") |
| 96 | + if path == "" { |
| 97 | + return "", false |
| 98 | + } |
| 99 | + return filepath.Clean(tool.ResolveOutputPath(path)), true |
| 100 | + default: |
| 101 | + return "", false |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func resourceArchiveStringArg(arguments map[string]interface{}, key string) string { |
| 106 | + value, _ := arguments[key].(string) |
| 107 | + return strings.TrimSpace(value) |
| 108 | +} |
| 109 | + |
| 110 | +func archiveGeneratedResourceFileToStorage(path string) (*Resource, error) { |
| 111 | + info, err := os.Stat(path) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + if info.IsDir() { |
| 116 | + return nil, fmt.Errorf("path is a directory: %s", path) |
| 117 | + } |
| 118 | + |
| 119 | + fileBytes, err := os.ReadFile(path) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + fileName := filepath.Base(path) |
| 125 | + if fileName == "." || fileName == string(os.PathSeparator) || fileName == "" { |
| 126 | + fileName = "generated_file" |
| 127 | + } |
| 128 | + ext := strings.ToLower(filepath.Ext(fileName)) |
| 129 | + fileType := getGeneratedResourceFileType(ext) |
| 130 | + storageName := fmt.Sprintf( |
| 131 | + "openagent/resources/generated/%s_%s_%s", |
| 132 | + resourceArchiveSafePathSegment(util.GetCurrentTime()), |
| 133 | + util.GetRandomName(), |
| 134 | + resourceArchiveSafePathSegment(fileName), |
| 135 | + ) |
| 136 | + |
| 137 | + fileUrl, err := UploadFileToStorageSafe(storageName, fileBytes, "", "") |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + resource := NewResourceFromUpload("admin", "", "generated", fileName, fileType, ext, fileUrl, storageName, len(fileBytes), "", "") |
| 143 | + if _, err = AddResource(resource); err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + return resource, nil |
| 147 | +} |
| 148 | + |
| 149 | +func getGeneratedResourceFileType(ext string) string { |
| 150 | + mimeType := mime.TypeByExtension(ext) |
| 151 | + if mimeType == "" { |
| 152 | + return "unknown" |
| 153 | + } |
| 154 | + parts := strings.SplitN(mimeType, "/", 2) |
| 155 | + if len(parts) == 0 || parts[0] == "" { |
| 156 | + return "unknown" |
| 157 | + } |
| 158 | + return parts[0] |
| 159 | +} |
| 160 | + |
| 161 | +func resourceArchiveSafePathSegment(value string) string { |
| 162 | + value = strings.TrimSpace(value) |
| 163 | + if value == "" { |
| 164 | + return "unknown" |
| 165 | + } |
| 166 | + return strings.Map(func(r rune) rune { |
| 167 | + switch r { |
| 168 | + case '/', '\\', 0: |
| 169 | + return '_' |
| 170 | + default: |
| 171 | + return r |
| 172 | + } |
| 173 | + }, value) |
| 174 | +} |
| 175 | + |
| 176 | +func appendGeneratedResourceArchiveText(result *protocol.CallToolResult, text string) { |
| 177 | + if result == nil || text == "" { |
| 178 | + return |
| 179 | + } |
| 180 | + result.Content = append(result.Content, &protocol.TextContent{Type: "text", Text: text}) |
| 181 | +} |
0 commit comments