-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathhelpers.go
More file actions
145 lines (132 loc) · 4.11 KB
/
Copy pathhelpers.go
File metadata and controls
145 lines (132 loc) · 4.11 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
// Copyright (c) 2023-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package conversation
import (
"encoding/json"
"strings"
"github.com/mattermost/mattermost-plugin-agents/v2/llm"
"github.com/mattermost/mattermost-plugin-agents/v2/mmapi"
"github.com/mattermost/mattermost-plugin-agents/v2/toolrunner"
"github.com/mattermost/mattermost/server/public/model"
)
// textBlocks creates content blocks from a plain text message.
func textBlocks(message string) []ContentBlock {
if message == "" {
return nil
}
return []ContentBlock{{Type: BlockTypeText, Text: message}}
}
// userBlocksWithAttachments emits a text block followed by image/file blocks
// for each fileID. A failed GetFileInfo is logged and the bad ID is skipped
// so one deleted or unreadable attachment does not poison the whole turn.
func userBlocksWithAttachments(message string, fileIDs []string, mmClient mmapi.Client) []ContentBlock {
blocks := textBlocks(message)
if mmClient == nil {
return blocks
}
for _, fileID := range fileIDs {
fileInfo, err := mmClient.GetFileInfo(fileID)
if err != nil {
mmClient.LogError("failed to get file info for user attachment", "error", err, "file_id", fileID)
continue
}
if strings.HasPrefix(fileInfo.MimeType, "image/") {
blocks = append(blocks, ContentBlock{
Type: BlockTypeImage,
FileID: fileID,
Filename: fileInfo.Name,
MimeType: fileInfo.MimeType,
})
} else {
blocks = append(blocks, ContentBlock{
Type: BlockTypeFile,
FileID: fileID,
Filename: fileInfo.Name,
MimeType: fileInfo.MimeType,
})
}
}
return blocks
}
// marshalBlocks serializes content blocks to JSON for store.Turn.Content.
func marshalBlocks(blocks []ContentBlock) (json.RawMessage, error) {
if blocks == nil {
blocks = []ContentBlock{}
}
return json.Marshal(blocks)
}
// unmarshalBlocks deserializes JSON content from store.Turn.Content.
func unmarshalBlocks(raw json.RawMessage) ([]ContentBlock, error) {
if len(raw) == 0 {
return nil, nil
}
var blocks []ContentBlock
if err := json.Unmarshal(raw, &blocks); err != nil {
return nil, err
}
return blocks, nil
}
// toolUseBlocks builds assistant-side content blocks from ToolRunner output.
// Tool calls must carry their resolved status (AutoApproved / Error) — the
// toolrunner stores resolved tool calls on ToolTurn.AssistantToolCalls after
// execution, so this helper just forwards tc.Status verbatim.
func toolUseBlocks(
message string,
reasoning llm.ReasoningData,
toolCalls []llm.ToolCall,
shared bool,
) []ContentBlock {
var blocks []ContentBlock
if reasoning.Text != "" {
blocks = append(blocks, ContentBlock{
Type: BlockTypeThinking,
Text: reasoning.Text,
Signature: reasoning.Signature,
})
}
if message != "" {
blocks = append(blocks, ContentBlock{
Type: BlockTypeText,
Text: message,
})
}
for _, tc := range toolCalls {
blocks = append(blocks, ContentBlock{
Type: BlockTypeToolUse,
ID: tc.ID,
Name: tc.Name,
ServerOrigin: tc.ServerOrigin,
Input: tc.Arguments,
MCPBareName: tc.MCPBareName,
Status: StatusToString(tc.Status),
Shared: BoolPtr(shared),
UserInteraction: tc.UserInteraction,
Title: tc.Title,
Description: tc.Description,
})
}
return blocks
}
// toolResultBlocks builds tool_result-side content blocks from ToolRunner output.
// Auto-executed tool rounds are terminal: there is no share/keep-private step,
// so stamp DecidedAt at creation time to reflect that no further approval UI
// is needed. DMs inherit the same treatment (shared=true, decided).
func toolResultBlocks(results []toolrunner.ToolResult, shared bool) []ContentBlock {
now := model.GetMillis()
blocks := make([]ContentBlock, len(results))
for i, tr := range results {
status := StatusSuccess
if tr.IsError {
status = StatusError
}
blocks[i] = ContentBlock{
Type: BlockTypeToolResult,
ToolUseID: tr.ToolCallID,
Content: tr.Result,
Status: status,
Shared: BoolPtr(shared),
DecidedAt: Int64Ptr(now),
}
}
return blocks
}