-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_handler.go
More file actions
102 lines (94 loc) · 3.25 KB
/
remote_handler.go
File metadata and controls
102 lines (94 loc) · 3.25 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
//go:build !wasm
package devtui
import (
"github.com/tinywasm/context"
"github.com/tinywasm/mcp"
)
// newRemoteField constructs a *field populated from a StateEntry.
// Uses anyHandler closures directly — no intermediate interface types needed.
// The entry pointer is captured so optimistic value updates stay in sync.
// The tui reference is used to register shortcuts in the ShortcutRegistry.
func newRemoteField(entry StateEntry, client *mcp.Client, ts *tabSection, tui *DevTUI) *field {
e := entry // local copy captured by closures
var anyH *anyHandler
switch handlerType(e.HandlerType) {
case handlerTypeDisplay:
anyH = &anyHandler{
handlerType: handlerTypeDisplay,
handlerColor: e.HandlerColor,
nameFunc: func() string { return e.HandlerName },
valueFunc: func() string { return e.Value },
contentFunc: func() string { return e.Value },
editableFunc: func() bool { return false },
}
case handlerTypeEdit:
anyH = &anyHandler{
handlerType: handlerTypeEdit,
handlerColor: e.HandlerColor,
nameFunc: func() string { return e.HandlerName },
labelFunc: func() string { return e.Label },
valueFunc: func() string { return e.Value },
editableFunc: func() bool { return true },
changeFunc: func(v string) {
e.Value = v // optimistic update
postAction(client, e.Shortcut, v)
},
}
case handlerTypeExecution:
anyH = &anyHandler{
handlerType: handlerTypeExecution,
handlerColor: e.HandlerColor,
nameFunc: func() string { return e.HandlerName },
labelFunc: func() string { return e.Label },
valueFunc: func() string { return e.Label },
editableFunc: func() bool { return false },
executeFunc: func() { postAction(client, e.Shortcut, "") },
changeFunc: func(_ string) { postAction(client, e.Shortcut, "") },
}
case handlerTypeInteractive:
anyH = &anyHandler{
handlerType: handlerTypeInteractive,
handlerColor: e.HandlerColor,
nameFunc: func() string { return e.HandlerName },
labelFunc: func() string { return e.Label },
valueFunc: func() string { return e.Value },
editableFunc: func() bool { return true },
editModeFunc: func() bool { return false },
changeFunc: func(v string) {
postAction(client, e.Shortcut, v)
},
}
default:
return nil // HandlerTypeLoggable — no field, logs arrive via SSE
}
f := &field{handler: anyH, parentTab: ts, isRemote: true}
// Register shortcuts from StateEntry.Shortcuts in the TUI's registry
if tui != nil && tui.shortcutRegistry != nil && len(e.Shortcuts) > 0 {
fieldIndex := len(ts.FieldHandlers)
tabIndex := ts.Index
for _, m := range e.Shortcuts {
for key := range m {
entry := &ShortcutEntry{
Key: key,
Description: key, // Use key as description for remote shortcuts
TabIndex: tabIndex,
FieldIndex: fieldIndex,
HandlerName: e.HandlerName,
Value: key,
}
tui.shortcutRegistry.Register(key, entry)
}
}
}
return f
}
// postAction sends a tinywasm/action JSON-RPC call to the daemon (fire-and-forget).
func postAction(client *mcp.Client, shortcut, value string) {
if shortcut == "" || client == nil {
return
}
client.Dispatch(context.Background(), "tinywasm/action", &ActionArgs{
Key: shortcut,
Value: value,
})
}