@@ -5,7 +5,9 @@ package aiusechat
55
66import (
77 "context"
8+ "encoding/json"
89 "fmt"
10+ "net/http"
911 "strings"
1012 "time"
1113
@@ -177,6 +179,19 @@ func generateToolsForBlock(block *waveobj.Block) []uctypes.ToolDefinition {
177179 switch viewType {
178180 case "web" :
179181 tools = append (tools , GetWebNavigateToolDefinition (block ))
182+ case "tsunami" :
183+ // Check if tsunami widget is running
184+ status := blockcontroller .GetBlockControllerRuntimeStatus (block .OID )
185+ if status != nil && status .ShellProcStatus == blockcontroller .Status_Running && status .TsunamiPort > 0 {
186+ // Check if schemas are available
187+ blockORef := waveobj .MakeORef (waveobj .OType_Block , block .OID )
188+ rtInfo := wstore .GetRTInfo (blockORef )
189+ if rtInfo != nil && rtInfo .TsunamiSchemas != nil {
190+ if tool := GetTsunamiGetDataToolDefinition (block , rtInfo , status ); tool != nil {
191+ tools = append (tools , * tool )
192+ }
193+ }
194+ }
180195 }
181196
182197 return tools
@@ -230,6 +245,62 @@ func GetWebNavigateToolDefinition(block *waveobj.Block) uctypes.ToolDefinition {
230245 }
231246}
232247
248+ func GetTsunamiGetDataToolDefinition (block * waveobj.Block , rtInfo * waveobj.ObjRTInfo , status * blockcontroller.BlockControllerRuntimeStatus ) * uctypes.ToolDefinition {
249+ blockIdPrefix := block .OID [:8 ]
250+ toolName := fmt .Sprintf ("tsunami_getdata_%s" , blockIdPrefix )
251+
252+ var inputSchema map [string ]any
253+ if rtInfo != nil && rtInfo .TsunamiSchemas != nil {
254+ if schemasMap , ok := rtInfo .TsunamiSchemas .(map [string ]any ); ok {
255+ if dataSchema , exists := schemasMap ["data" ]; exists {
256+ inputSchema = dataSchema .(map [string ]any )
257+ }
258+ }
259+ }
260+
261+ // Return nil if no data schema found
262+ if inputSchema == nil {
263+ return nil
264+ }
265+
266+ return & uctypes.ToolDefinition {
267+ Name : toolName ,
268+ InputSchema : inputSchema ,
269+ ToolAnyCallback : func (input any ) (any , error ) {
270+ if status .TsunamiPort == 0 {
271+ return nil , fmt .Errorf ("tsunami port not available" )
272+ }
273+
274+ url := fmt .Sprintf ("http://localhost:%d/api/data" , status .TsunamiPort )
275+
276+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
277+ defer cancel ()
278+
279+ req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
280+ if err != nil {
281+ return nil , fmt .Errorf ("failed to create request: %w" , err )
282+ }
283+
284+ resp , err := http .DefaultClient .Do (req )
285+ if err != nil {
286+ return nil , fmt .Errorf ("failed to make request to tsunami: %w" , err )
287+ }
288+ defer resp .Body .Close ()
289+
290+ if resp .StatusCode != http .StatusOK {
291+ return nil , fmt .Errorf ("tsunami returned status %d" , resp .StatusCode )
292+ }
293+
294+ var result any
295+ if err := json .NewDecoder (resp .Body ).Decode (& result ); err != nil {
296+ return nil , fmt .Errorf ("failed to decode tsunami response: %w" , err )
297+ }
298+
299+ return result , nil
300+ },
301+ }
302+ }
303+
233304func GetAdderToolDefinition () uctypes.ToolDefinition {
234305 return uctypes.ToolDefinition {
235306 Name : "adder" ,
0 commit comments