Skip to content

Commit 4a78525

Browse files
authored
Merge pull request #1364 from WolframResearch/bugfix/Sidebar7
Bugfix/sidebar7
2 parents 2e534f5 + 028da4d commit 4a78525

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

Source/Chatbook/ChatModes/ShowNotebookAssistance.wl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ Needs[ "Wolfram`Chatbook`ChatModes`Common`" ];
1111
(* ::Section::Closed:: *)
1212
(*Configuration*)
1313
$workspaceChatWidth := $workspaceChatWidth = Switch[ $OperatingSystem, "MacOSX", 450, _, 360 ];
14-
$sidebarChatWidth := Min[ AbsoluteCurrentValue[ EvaluationNotebook[ ], { WindowSize, 1 } ]*0.5, Switch[ $OperatingSystem, "MacOSX", 450, _, 360 ] ];
14+
$sidebarChatWidth[ nbo_NotebookObject ] := Switch[ $OperatingSystem,
15+
"MacOSX", Clip[ 450, { 255, AbsoluteCurrentValue[ nbo, { WindowSize, 1 } ]*0.5 } ],
16+
_, Clip[ 360, { 250, AbsoluteCurrentValue[ nbo, { WindowSize, 1 } ]*0.5 } ]
17+
];
1518

1619
$notebookAssistanceBaseSettings = <|
1720
"AllowSelectionContext" -> True,
@@ -522,18 +525,18 @@ sidebarCellObject// endDefinition;
522525
showNotebookAssistanceSidebar // beginDefinition;
523526

524527
showNotebookAssistanceSidebar[ nbo_NotebookObject, input_, evaluate_, settings0_Association ] := Enclose[
525-
Module[ { settings, sidebarCell, scrollingSidebarCell, movedLastChatToSourcesIndicatorQ },
528+
Module[ { sidebarCell },
526529

527530
sidebarCell = sidebarCellObject @ nbo; (* if the side bar has been opened once before IN ITS CONTAINING NOTEBOOK than this is a CellObject, else $Failed *)
528531

529532
If[ FailureQ @ sidebarCell,
530533
(* don't do anything else because this is the first time we've opened the sidebar in this notebook; Cell Initialization adds necessary TaggingRules *)
531-
FrontEndTokenExecute[ nbo, "SwitchSidebar", <| "PanelID" -> "NotebookAssistant", "PreferredSize" -> $sidebarChatWidth |> ];
534+
FrontEndTokenExecute[ nbo, "SwitchSidebar", <| "PanelID" -> "NotebookAssistant", "PreferredSize" -> $sidebarChatWidth @ nbo |> ];
532535
, (* ELSE the sidebar assistant is persistant so don't remove content cells *)
533536

534537
(* The sidebar assistant is persistant to a given notebook. Only remove content if "new chat" is selected. *)
535538

536-
FrontEndTokenExecute[ nbo, "SwitchSidebar", <| "PanelID" -> "NotebookAssistant", "PreferredSize" -> $sidebarChatWidth |> ];
539+
FrontEndTokenExecute[ nbo, "SwitchSidebar", <| "PanelID" -> "NotebookAssistant", "PreferredSize" -> $sidebarChatWidth @ nbo |> ];
537540

538541
(* will we ever need to do this with the sidebar chat...? *)
539542
If[ TrueQ @ evaluate,

Source/Chatbook/ChatModes/UI.wl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ sidebarHideButton[ Dynamic[ nbo_ ] ] := Button[
332332
],
333333
tr @ "SidebarToolbarButtonTooltipHideSidebar"
334334
],
335+
CurrentValue[ $FrontEndSession, "ShowNotebookAssistant" ] = False;
335336
FrontEndToken[nbo, "HideSidebar"],
336337
Appearance -> "Suppressed",
337338
Evaluator -> None
@@ -601,7 +602,7 @@ sidebarChatInputCell // beginDefinition;
601602
sidebarChatInputCell[ ] := sidebarChatInputCell[ "" ]
602603

603604
sidebarChatInputCell[ initialContent_ ] := Cell[
604-
BoxData @ ToBoxes @ DynamicModule[ { thisNB, thisCell, sidebarCell, chatEvalCell, fieldContent = initialContent, returnKeyDownQ, input },
605+
BoxData @ ToBoxes @ DynamicModule[ { thisNB, thisCell, sidebarCell, chatEvalCell, fieldContent = initialContent, returnKeyDownQ, input, kernelWasQuitQ = False, cachedChatInput = "", cachedSessionID = $SessionID },
605606
EventHandler[
606607
Pane[
607608
Grid[
@@ -620,6 +621,11 @@ sidebarChatInputCell[ initialContent_ ] := Cell[
620621
,
621622
If[ TrueQ @ returnKeyDownQ,
622623
returnKeyDownQ = False;
624+
Needs[ "Wolfram`Chatbook`" -> None ];
625+
If[ kernelWasQuitQ,
626+
If[ Not @ TrueQ @ $workspaceChatInitialized, initializeWorkspaceChat[ ] ];
627+
If[ fieldContent =!= cachedChatInput, fieldContent = cachedChatInput ];
628+
kernelWasQuitQ = False ];
623629
evaluateSidebarChat[ thisNB, sidebarCell, input, Dynamic @ chatEvalCell ]
624630
];
625631
(* spooky action at a distance: regenerating a side bar ChatOutput cell *)
@@ -691,7 +697,7 @@ sidebarChatInputCell[ initialContent_ ] := Cell[
691697
Method -> "Preemptive"
692698
],
693699
(* 15.0: the side bar is a Row of cells: docked cells, scrollable pane cell, footer cell (ChatInput) *)
694-
Initialization :> (thisNB = EvaluationNotebook[ ]; thisCell = EvaluationCell[ ]; sidebarCell = ParentCell @ thisCell)
700+
Initialization :> (thisNB = EvaluationNotebook[ ]; thisCell = EvaluationCell[ ]; sidebarCell = ParentCell @ thisCell; kernelWasQuitQ = cachedSessionID =!= $SessionID; cachedSessionID = $SessionID)
695701
],
696702
"ChatInputField",
697703
Background -> $inputFieldOuterBackground,
@@ -747,7 +753,7 @@ attachWorkspaceChatInput // endDefinition;
747753
(* :!CodeAnalysis::BeginBlock:: *)
748754
(* :!CodeAnalysis::Disable::DynamicImageSize:: *)
749755
attachedWorkspaceChatInputCell[ location_String ] := Cell[
750-
BoxData @ ToBoxes @ DynamicModule[ { thisNB },
756+
BoxData @ ToBoxes @ DynamicModule[ { thisNB, cachedSessionID = $SessionID, cachedChatInput = "", kernelWasQuitQ = False },
751757
EventHandler[
752758
Pane[
753759
Grid[
@@ -756,7 +762,7 @@ attachedWorkspaceChatInputCell[ location_String ] := Cell[
756762
RawBoxes @ TemplateBox[ { }, "ChatIconUser" ],
757763
Framed[
758764
InputField[
759-
Dynamic @ $WorkspaceChatInput,
765+
Dynamic[ $WorkspaceChatInput, Function[ cachedChatInput = $WorkspaceChatInput = # ] ], (* store a local copy of the text in case the kernel was quit *)
760766
Boxes,
761767
ContinuousAction -> True,
762768
$inputFieldOptions
@@ -773,6 +779,11 @@ attachedWorkspaceChatInputCell[ location_String ] := Cell[
773779
27
774780
],
775781
Needs[ "Wolfram`Chatbook`" -> None ];
782+
(* in case kernel was quit and the user hasn't typed something new into the search field, restore it to the cached value *)
783+
If[ kernelWasQuitQ,
784+
If[ Not @ TrueQ @ $workspaceChatInitialized, initializeWorkspaceChat[ ] ];
785+
If[ $WorkspaceChatInput =!= cachedChatInput, $WorkspaceChatInput = cachedChatInput ];
786+
kernelWasQuitQ = False ];
776787
Symbol[ "Wolfram`Chatbook`ChatbookAction" ][
777788
"EvaluateWorkspaceChat",
778789
thisNB,
@@ -821,6 +832,11 @@ attachedWorkspaceChatInputCell[ location_String ] := Cell[
821832
{
822833
"ReturnKeyDown" :> (
823834
Needs[ "Wolfram`Chatbook`" -> None ];
835+
(* in case kernel was quit and the user hasn't typed something new into the search field, restore it to the cached value *)
836+
If[ kernelWasQuitQ,
837+
If[ Not @ TrueQ @ $workspaceChatInitialized, initializeWorkspaceChat[ ] ];
838+
If[ $WorkspaceChatInput =!= cachedChatInput, $WorkspaceChatInput = cachedChatInput ];
839+
kernelWasQuitQ = False ];
824840
Symbol[ "Wolfram`Chatbook`ChatbookAction" ][
825841
"EvaluateWorkspaceChat",
826842
thisNB,
@@ -830,7 +846,7 @@ attachedWorkspaceChatInputCell[ location_String ] := Cell[
830846
},
831847
Method -> "Queued"
832848
],
833-
Initialization :> (thisNB = EvaluationNotebook[ ])
849+
Initialization :> (thisNB = EvaluationNotebook[ ]; kernelWasQuitQ = cachedSessionID =!= $SessionID; cachedSessionID = $SessionID)
834850
],
835851
"ChatInputField",
836852
Background -> $inputFieldOuterBackground,
@@ -865,7 +881,7 @@ $workspaceChatInitialized = False;
865881
(*initializeWorkspaceChat*)
866882
initializeWorkspaceChat // beginDefinition;
867883

868-
initializeWorkspaceChat[ ] := initializeWorkspaceChat[ ] = LogChatTiming[
884+
initializeWorkspaceChat[ ] := LogChatTiming[
869885
LogChatTiming[ getEmbeddings[ { "Hello" } ], "InitializeEmbeddings" ];
870886
LogChatTiming[ cachedTokenizer[ "gpt-4o-text" ], "InitializeTokenizer" ];
871887
LogChatTiming[ $llmKitService, "InitializeLLMKitService" ];
@@ -1532,6 +1548,8 @@ assistantMessageButtons[ includeFeedback_, sidebarCellQ_ ] :=
15321548
Tooltip[ assistantCopyAsActionMenu[ Dynamic[ cell ] ], tr[ "WorkspaceOutputRaftCopyAsTooltip" ] ],
15331549
Button[
15341550
Tooltip[ $regenerateLabel, tr[ "WorkspaceOutputRaftRegenerateTooltip" ] ],
1551+
Needs[ "Wolfram`Chatbook`" -> None ];
1552+
If[ Not @ TrueQ @ $workspaceChatInitialized, initializeWorkspaceChat[ ] ]; (* in case kernel was quit *)
15351553
ChatbookAction[ "RegenerateAssistantMessage", cell, sidebarCellQ ],
15361554
Appearance -> "Suppressed",
15371555
Method -> "Queued"

0 commit comments

Comments
 (0)