-
-
Notifications
You must be signed in to change notification settings - Fork 972
Expand file tree
/
Copy pathtools_tsunami.go
More file actions
203 lines (170 loc) · 6.36 KB
/
tools_tsunami.go
File metadata and controls
203 lines (170 loc) · 6.36 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
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package aiusechat
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
"github.com/wavetermdev/waveterm/pkg/wstore"
)
func getTsunamiShortDesc(rtInfo *waveobj.ObjRTInfo) string {
if rtInfo == nil || rtInfo.TsunamiAppMeta == nil {
return ""
}
var appMeta wshrpc.AppMeta
if err := utilfn.ReUnmarshal(&appMeta, rtInfo.TsunamiAppMeta); err == nil && appMeta.ShortDesc != "" {
return appMeta.ShortDesc
}
return ""
}
func handleTsunamiBlockDesc(block *waveobj.Block) string {
status := blockcontroller.GetBlockControllerRuntimeStatus(block.OID)
if status == nil || status.ShellProcStatus != blockcontroller.Status_Running {
return "tsunami framework widget that is currently not running"
}
blockORef := waveobj.MakeORef(waveobj.OType_Block, block.OID)
rtInfo := wstore.GetRTInfo(blockORef)
if shortDesc := getTsunamiShortDesc(rtInfo); shortDesc != "" {
return fmt.Sprintf("tsunami widget - %s", shortDesc)
}
return "tsunami widget - unknown description"
}
func makeTsunamiGetCallback(status *blockcontroller.BlockControllerRuntimeStatus, apiPath string) func(any, *uctypes.UIMessageDataToolUse) (any, error) {
return func(input any, toolUseData *uctypes.UIMessageDataToolUse) (any, error) {
if status.TsunamiPort == 0 {
return nil, fmt.Errorf("tsunami port not available")
}
url := fmt.Sprintf("http://localhost:%d%s", status.TsunamiPort, apiPath)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request to tsunami: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("tsunami returned status %d", resp.StatusCode)
}
var result any
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode tsunami response: %w", err)
}
return result, nil
}
}
func makeTsunamiPostCallback(status *blockcontroller.BlockControllerRuntimeStatus, apiPath string) func(any, *uctypes.UIMessageDataToolUse) (any, error) {
return func(input any, toolUseData *uctypes.UIMessageDataToolUse) (any, error) {
if status.TsunamiPort == 0 {
return nil, fmt.Errorf("tsunami port not available")
}
url := fmt.Sprintf("http://localhost:%d%s", status.TsunamiPort, apiPath)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var reqBody []byte
var err error
if input != nil {
reqBody, err = json.Marshal(input)
if err != nil {
return nil, fmt.Errorf("failed to marshal input: %w", err)
}
}
req, err := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(string(reqBody)))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request to tsunami: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("tsunami returned status %d", resp.StatusCode)
}
return true, nil
}
}
func GetTsunamiGetDataToolDefinition(block *waveobj.Block, rtInfo *waveobj.ObjRTInfo, status *blockcontroller.BlockControllerRuntimeStatus) *uctypes.ToolDefinition {
blockIdPrefix := block.OID[:8]
toolName := fmt.Sprintf("tsunami_getdata_%s", blockIdPrefix)
desc := "tsunami widget"
if shortDesc := getTsunamiShortDesc(rtInfo); shortDesc != "" {
desc = shortDesc
}
return &uctypes.ToolDefinition{
Name: toolName,
ToolLogName: "tsunami:getdata",
Strict: true,
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{},
"additionalProperties": false,
},
ToolCallDesc: func(input any, output any, toolUseData *uctypes.UIMessageDataToolUse) string {
return fmt.Sprintf("getting data from %s (%s)", desc, blockIdPrefix)
},
ToolAnyCallback: makeTsunamiGetCallback(status, "/api/data"),
}
}
func GetTsunamiGetConfigToolDefinition(block *waveobj.Block, rtInfo *waveobj.ObjRTInfo, status *blockcontroller.BlockControllerRuntimeStatus) *uctypes.ToolDefinition {
blockIdPrefix := block.OID[:8]
toolName := fmt.Sprintf("tsunami_getconfig_%s", blockIdPrefix)
desc := "tsunami widget"
if shortDesc := getTsunamiShortDesc(rtInfo); shortDesc != "" {
desc = shortDesc
}
return &uctypes.ToolDefinition{
Name: toolName,
ToolLogName: "tsunami:getconfig",
Strict: true,
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{},
"additionalProperties": false,
},
ToolCallDesc: func(input any, output any, toolUseData *uctypes.UIMessageDataToolUse) string {
return fmt.Sprintf("getting config from %s (%s)", desc, blockIdPrefix)
},
ToolAnyCallback: makeTsunamiGetCallback(status, "/api/config"),
}
}
func GetTsunamiSetConfigToolDefinition(block *waveobj.Block, rtInfo *waveobj.ObjRTInfo, status *blockcontroller.BlockControllerRuntimeStatus) *uctypes.ToolDefinition {
blockIdPrefix := block.OID[:8]
toolName := fmt.Sprintf("tsunami_setconfig_%s", blockIdPrefix)
var inputSchema map[string]any
if rtInfo != nil && rtInfo.TsunamiSchemas != nil {
if schemasMap, ok := rtInfo.TsunamiSchemas.(map[string]any); ok {
if configSchema, exists := schemasMap["config"]; exists {
inputSchema = configSchema.(map[string]any)
}
}
}
if inputSchema == nil {
return nil
}
desc := "tsunami widget"
if shortDesc := getTsunamiShortDesc(rtInfo); shortDesc != "" {
desc = shortDesc
}
return &uctypes.ToolDefinition{
Name: toolName,
ToolLogName: "tsunami:setconfig",
InputSchema: inputSchema,
ToolCallDesc: func(input any, output any, toolUseData *uctypes.UIMessageDataToolUse) string {
return fmt.Sprintf("updating config for %s (%s)", desc, blockIdPrefix)
},
ToolAnyCallback: makeTsunamiPostCallback(status, "/api/config"),
}
}