mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-06 19:18:22 +01:00
8acda3525b
* Break update code out of sstore * add license disclaimers * missed one * add another * fix regression in openai updates, remove unnecessary functions * another copyright * update casts * fix issue with variadic updates * remove logs * remove log * remove unnecessary log * save work * moved a bunch of stuff to scbus * make modelupdate an object * fix new screen not updating active screen * add comment * make updates into packet types * different cast * update comments, remove unused methods * add one more comment * add an IsEmpty() on model updates to prevent sending empty updates to client
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
// Copyright 2024, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Provides a mechanism for the backend to request user input from the frontend.
|
|
package userinput
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbus"
|
|
)
|
|
|
|
// An RpcPacket for requesting user input from the client
|
|
type UserInputRequestType struct {
|
|
RequestId string `json:"requestid"`
|
|
QueryText string `json:"querytext"`
|
|
ResponseType string `json:"responsetype"`
|
|
Title string `json:"title"`
|
|
Markdown bool `json:"markdown"`
|
|
TimeoutMs int `json:"timeoutms"`
|
|
}
|
|
|
|
func (*UserInputRequestType) GetType() string {
|
|
return "userinputrequest"
|
|
}
|
|
|
|
func (req *UserInputRequestType) SetReqId(reqId string) {
|
|
req.RequestId = reqId
|
|
}
|
|
|
|
func (req *UserInputRequestType) SetTimeoutMs(timeoutMs int) {
|
|
req.TimeoutMs = timeoutMs
|
|
}
|
|
|
|
const UserInputResponsePacketStr = "userinputresp"
|
|
|
|
// An RpcResponse for user input requests
|
|
type UserInputResponsePacketType struct {
|
|
Type string `json:"type"`
|
|
RequestId string `json:"requestid"`
|
|
Text string `json:"text,omitempty"`
|
|
Confirm bool `json:"confirm,omitempty"`
|
|
ErrorMsg string `json:"errormsg,omitempty"`
|
|
}
|
|
|
|
func (*UserInputResponsePacketType) GetType() string {
|
|
return UserInputResponsePacketStr
|
|
}
|
|
|
|
func (pk *UserInputResponsePacketType) GetError() string {
|
|
return pk.ErrorMsg
|
|
}
|
|
|
|
func (pk *UserInputResponsePacketType) SetError(err string) {
|
|
pk.ErrorMsg = err
|
|
}
|
|
|
|
// Send a user input request to the frontend and wait for a response
|
|
func GetUserInput(ctx context.Context, bus *scbus.RpcBus, userInputRequest *UserInputRequestType) (*UserInputResponsePacketType, error) {
|
|
resp, err := scbus.MainRpcBus.DoRpc(ctx, userInputRequest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ret, ok := resp.(*UserInputResponsePacketType); !ok {
|
|
return nil, fmt.Errorf("unexpected response type: %v", reflect.TypeOf(resp))
|
|
} else {
|
|
return ret, nil
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
// Register the user input request packet type
|
|
packet.RegisterPacketType(UserInputResponsePacketStr, reflect.TypeOf(UserInputResponsePacketType{}))
|
|
}
|