mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-02 18:39:05 +01:00
panic handlers in wshrpc. also stop waveai from grabbing focus all the time
This commit is contained in:
parent
c5707de2fd
commit
a23b5ca0e0
@ -52,6 +52,7 @@ export class WaveAiModel implements ViewModel {
|
|||||||
addMessageAtom: jotai.WritableAtom<unknown, [message: ChatMessageType], void>;
|
addMessageAtom: jotai.WritableAtom<unknown, [message: ChatMessageType], void>;
|
||||||
updateLastMessageAtom: jotai.WritableAtom<unknown, [text: string, isUpdating: boolean], void>;
|
updateLastMessageAtom: jotai.WritableAtom<unknown, [text: string, isUpdating: boolean], void>;
|
||||||
simulateAssistantResponseAtom: jotai.WritableAtom<unknown, [userMessage: ChatMessageType], Promise<void>>;
|
simulateAssistantResponseAtom: jotai.WritableAtom<unknown, [userMessage: ChatMessageType], Promise<void>>;
|
||||||
|
textAreaRef: React.RefObject<HTMLTextAreaElement>;
|
||||||
|
|
||||||
constructor(blockId: string) {
|
constructor(blockId: string) {
|
||||||
this.blockId = blockId;
|
this.blockId = blockId;
|
||||||
@ -127,6 +128,14 @@ export class WaveAiModel implements ViewModel {
|
|||||||
return history;
|
return history;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
giveFocus(): boolean {
|
||||||
|
if (this?.textAreaRef?.current) {
|
||||||
|
this.textAreaRef.current?.focus();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
useWaveAi() {
|
useWaveAi() {
|
||||||
const [messages] = jotai.useAtom(this.messagesAtom);
|
const [messages] = jotai.useAtom(this.messagesAtom);
|
||||||
const [, addMessage] = jotai.useAtom(this.addMessageAtom);
|
const [, addMessage] = jotai.useAtom(this.addMessageAtom);
|
||||||
@ -341,18 +350,17 @@ interface ChatInputProps {
|
|||||||
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
||||||
onKeyDown: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void;
|
onKeyDown: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void;
|
||||||
onMouseDown: (e: React.MouseEvent<HTMLTextAreaElement>) => void;
|
onMouseDown: (e: React.MouseEvent<HTMLTextAreaElement>) => void;
|
||||||
|
model: WaveAiModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ChatInput = forwardRef<HTMLTextAreaElement, ChatInputProps>(
|
const ChatInput = forwardRef<HTMLTextAreaElement, ChatInputProps>(
|
||||||
({ value, onChange, onKeyDown, onMouseDown, termFontSize }, ref) => {
|
({ value, onChange, onKeyDown, onMouseDown, termFontSize, model }, ref) => {
|
||||||
const textAreaRef = useRef<HTMLTextAreaElement>(null);
|
const textAreaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
|
||||||
useImperativeHandle(ref, () => textAreaRef.current as HTMLTextAreaElement);
|
useImperativeHandle(ref, () => textAreaRef.current as HTMLTextAreaElement);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (textAreaRef.current) {
|
model.textAreaRef = textAreaRef;
|
||||||
textAreaRef.current.focus();
|
|
||||||
}
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const adjustTextAreaHeight = () => {
|
const adjustTextAreaHeight = () => {
|
||||||
@ -586,6 +594,7 @@ const WaveAi = ({ model }: { model: WaveAiModel }) => {
|
|||||||
<ChatInput
|
<ChatInput
|
||||||
ref={inputRef}
|
ref={inputRef}
|
||||||
value={value}
|
value={value}
|
||||||
|
model={model}
|
||||||
onChange={handleTextAreaChange}
|
onChange={handleTextAreaChange}
|
||||||
onKeyDown={handleTextAreaKeyDown}
|
onKeyDown={handleTextAreaKeyDown}
|
||||||
onMouseDown={handleTextAreaMouseDown}
|
onMouseDown={handleTextAreaMouseDown}
|
||||||
|
@ -363,6 +363,12 @@ func (handler *RpcRequestHandler) Context() context.Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (handler *RpcRequestHandler) SendCancel() {
|
func (handler *RpcRequestHandler) SendCancel() {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
// this is likely a write to closed channel
|
||||||
|
log.Printf("panic in SendCancel: %v\n", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
msg := &RpcMessage{
|
msg := &RpcMessage{
|
||||||
Cancel: true,
|
Cancel: true,
|
||||||
ReqId: handler.reqId,
|
ReqId: handler.reqId,
|
||||||
@ -444,6 +450,13 @@ func (handler *RpcResponseHandler) SendMessage(msg string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (handler *RpcResponseHandler) SendResponse(data any, done bool) error {
|
func (handler *RpcResponseHandler) SendResponse(data any, done bool) error {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
// this is likely a write to closed channel
|
||||||
|
log.Printf("panic in SendResponse: %v\n", r)
|
||||||
|
handler.close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
if handler.reqId == "" {
|
if handler.reqId == "" {
|
||||||
return nil // no response expected
|
return nil // no response expected
|
||||||
}
|
}
|
||||||
@ -467,6 +480,13 @@ func (handler *RpcResponseHandler) SendResponse(data any, done bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (handler *RpcResponseHandler) SendResponseError(err error) {
|
func (handler *RpcResponseHandler) SendResponseError(err error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
// this is likely a write to closed channel
|
||||||
|
log.Printf("panic in SendResponseError: %v\n", r)
|
||||||
|
handler.close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
if handler.reqId == "" || handler.done.Load() {
|
if handler.reqId == "" || handler.done.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -506,7 +526,13 @@ func (handler *RpcResponseHandler) IsDone() bool {
|
|||||||
return handler.done.Load()
|
return handler.done.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WshRpc) SendComplexRequest(command string, data any, expectsResponse bool, timeoutMs int) (*RpcRequestHandler, error) {
|
func (w *WshRpc) SendComplexRequest(command string, data any, expectsResponse bool, timeoutMs int) (rtnHandler *RpcRequestHandler, rtnErr error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
log.Printf("panic in SendComplexRequest: %v\n", r)
|
||||||
|
rtnErr = fmt.Errorf("panic: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
if command == "" {
|
if command == "" {
|
||||||
return nil, fmt.Errorf("command cannot be empty")
|
return nil, fmt.Errorf("command cannot be empty")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user