@@ -6,27 +6,99 @@ package aiusechat
66import (
77 "context"
88 "fmt"
9+ "strings"
910
1011 "github.com/google/uuid"
1112 "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
1213 "github.com/wavetermdev/waveterm/pkg/waveobj"
1314 "github.com/wavetermdev/waveterm/pkg/wstore"
1415)
1516
16- func MakeToolsForTab (ctx context.Context , tabid string , widgetAccess bool ) ([]uctypes.ToolDefinition , error ) {
17+ func MakeBlockShortDesc (block * waveobj.Block ) string {
18+ if block .Meta == nil {
19+ return ""
20+ }
21+
22+ viewType , ok := block .Meta ["view" ].(string )
23+ if ! ok {
24+ return ""
25+ }
26+
27+ switch viewType {
28+ case "term" :
29+ connection , hasConnection := block .Meta ["connection" ].(string )
30+ cwd , hasCwd := block .Meta ["cmd:cwd" ].(string )
31+ hasCurCwd , _ := block .Meta ["cmd:hascurcwd" ].(bool )
32+
33+ var desc string
34+ if hasConnection && connection != "" {
35+ desc = fmt .Sprintf ("CLI terminal on %q" , connection )
36+ } else {
37+ desc = "local CLI terminal"
38+ }
39+
40+ if hasCurCwd && hasCwd && cwd != "" {
41+ desc += fmt .Sprintf (" in directory %q" , cwd )
42+ }
43+
44+ return desc
45+ case "preview" :
46+ file , hasFile := block .Meta ["file" ].(string )
47+ connection , hasConnection := block .Meta ["connection" ].(string )
48+
49+ if hasConnection && connection != "" {
50+ if hasFile && file != "" {
51+ return fmt .Sprintf ("preview widget viewing %q on %q" , file , connection )
52+ }
53+ return fmt .Sprintf ("preview widget viewing files on %q" , connection )
54+ }
55+ if hasFile && file != "" {
56+ return fmt .Sprintf ("preview widget viewing %q" , file )
57+ }
58+ return "file and directory preview widget"
59+ case "web" :
60+ if url , hasUrl := block .Meta ["url" ].(string ); hasUrl && url != "" {
61+ return fmt .Sprintf ("web browser widget pointing at %q" , url )
62+ }
63+ return "web browser widget"
64+ case "waveai" :
65+ return "AI chat widget"
66+ case "cpuplot" :
67+ if connection , hasConnection := block .Meta ["connection" ].(string ); hasConnection && connection != "" {
68+ return fmt .Sprintf ("cpu graph for %q" , connection )
69+ }
70+ return "cpu graph"
71+ case "tips" :
72+ return "Wave quick tips widget"
73+ case "help" :
74+ return "Wave documentation widget"
75+ case "launcher" :
76+ return "placeholder widget used to launch other widgets"
77+ case "tsunami" :
78+ return "custom 'tsunami' framework widget"
79+ default :
80+ return fmt .Sprintf ("unknown widget with type %q" , viewType )
81+ }
82+ }
83+
84+ func AddToolsForTab (ctx context.Context , tabid string , widgetAccess bool , chatOpts * uctypes.WaveChatOpts ) error {
1785 if tabid == "" {
18- return nil , nil
86+ return nil
1987 }
20-
88+ if ! widgetAccess {
89+ chatOpts .SystemPrompt = append (chatOpts .SystemPrompt , "The user has chosen not to share widget context with you." )
90+ return nil
91+ }
92+
2193 if _ , err := uuid .Parse (tabid ); err != nil {
22- return nil , fmt .Errorf ("tabid must be a valid UUID" )
94+ return fmt .Errorf ("tabid must be a valid UUID" )
2395 }
24-
96+
2597 tabObj , err := wstore .DBMustGet [* waveobj.Tab ](ctx , tabid )
2698 if err != nil {
27- return nil , fmt .Errorf ("error getting tab: %v" , err )
99+ return fmt .Errorf ("error getting tab: %v" , err )
28100 }
29-
101+
30102 var blocks []* waveobj.Block
31103 for _ , blockId := range tabObj .BlockIds {
32104 block , err := wstore .DBGet [* waveobj.Block ](ctx , blockId )
@@ -35,8 +107,44 @@ func MakeToolsForTab(ctx context.Context, tabid string, widgetAccess bool) ([]uc
35107 }
36108 blocks = append (blocks , block )
37109 }
38-
39- return nil , nil
110+
111+ systemPrompt := generateTabSystemPrompt (blocks )
112+ chatOpts .SystemPrompt = append (chatOpts .SystemPrompt , systemPrompt )
113+
114+ return nil
115+ }
116+
117+ func generateTabSystemPrompt (blocks []* waveobj.Block ) string {
118+ if len (blocks ) == 0 {
119+ return "This tab is empty with no widgets currently open."
120+ }
121+
122+ var widgetDescriptions []string
123+ for _ , block := range blocks {
124+ desc := MakeBlockShortDesc (block )
125+ if desc == "" {
126+ continue
127+ }
128+ blockIdPrefix := block .OID [:8 ]
129+ fullDesc := fmt .Sprintf ("(%s) %s" , blockIdPrefix , desc )
130+ widgetDescriptions = append (widgetDescriptions , fullDesc )
131+ }
132+
133+ totalWidgets := len (widgetDescriptions )
134+ var prompt strings.Builder
135+ if totalWidgets == 1 {
136+ prompt .WriteString ("In this tab there is 1 widget open (the widgetid appears in parentheses before the description):\n " )
137+ } else {
138+ prompt .WriteString (fmt .Sprintf ("In this tab there are %d widgets open (the widgetid appears in parentheses before the description):\n " , totalWidgets ))
139+ }
140+
141+ for _ , desc := range widgetDescriptions {
142+ prompt .WriteString ("* " )
143+ prompt .WriteString (desc )
144+ prompt .WriteString ("\n " )
145+ }
146+
147+ return prompt .String ()
40148}
41149
42150func GetAdderToolDefinition () uctypes.ToolDefinition {
0 commit comments