panic handlers in wshrpc. also stop waveai from grabbing focus all the time

This commit is contained in:
sawka 2024-08-01 00:03:19 -07:00
parent c5707de2fd
commit a23b5ca0e0
2 changed files with 40 additions and 5 deletions

View File

@ -52,6 +52,7 @@ export class WaveAiModel implements ViewModel {
addMessageAtom: jotai.WritableAtom<unknown, [message: ChatMessageType], void>;
updateLastMessageAtom: jotai.WritableAtom<unknown, [text: string, isUpdating: boolean], void>;
simulateAssistantResponseAtom: jotai.WritableAtom<unknown, [userMessage: ChatMessageType], Promise<void>>;
textAreaRef: React.RefObject<HTMLTextAreaElement>;
constructor(blockId: string) {
this.blockId = blockId;
@ -127,6 +128,14 @@ export class WaveAiModel implements ViewModel {
return history;
}
giveFocus(): boolean {
if (this?.textAreaRef?.current) {
this.textAreaRef.current?.focus();
return true;
}
return false;
}
useWaveAi() {
const [messages] = jotai.useAtom(this.messagesAtom);
const [, addMessage] = jotai.useAtom(this.addMessageAtom);
@ -341,18 +350,17 @@ interface ChatInputProps {
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
onKeyDown: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void;
onMouseDown: (e: React.MouseEvent<HTMLTextAreaElement>) => void;
model: WaveAiModel;
}
const ChatInput = forwardRef<HTMLTextAreaElement, ChatInputProps>(
({ value, onChange, onKeyDown, onMouseDown, termFontSize }, ref) => {
({ value, onChange, onKeyDown, onMouseDown, termFontSize, model }, ref) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null);
useImperativeHandle(ref, () => textAreaRef.current as HTMLTextAreaElement);
useEffect(() => {
if (textAreaRef.current) {
textAreaRef.current.focus();
}
model.textAreaRef = textAreaRef;
}, []);
const adjustTextAreaHeight = () => {
@ -586,6 +594,7 @@ const WaveAi = ({ model }: { model: WaveAiModel }) => {
<ChatInput
ref={inputRef}
value={value}
model={model}
onChange={handleTextAreaChange}
onKeyDown={handleTextAreaKeyDown}
onMouseDown={handleTextAreaMouseDown}

View File

@ -363,6 +363,12 @@ func (handler *RpcRequestHandler) Context() context.Context {
}
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{
Cancel: true,
ReqId: handler.reqId,
@ -444,6 +450,13 @@ func (handler *RpcResponseHandler) SendMessage(msg string) {
}
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 == "" {
return nil // no response expected
}
@ -467,6 +480,13 @@ func (handler *RpcResponseHandler) SendResponse(data any, done bool) 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() {
return
}
@ -506,7 +526,13 @@ func (handler *RpcResponseHandler) IsDone() bool {
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 == "" {
return nil, fmt.Errorf("command cannot be empty")
}