From d14ceecd20e7cc356d240e62b44833955857244b Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Thu, 14 Dec 2023 10:01:15 -0500 Subject: [PATCH] Move cloud toolbar content to separate file and use new preferences controls --- Source/Chatbook/CloudToolbar.wl | 107 +++++++++++++++++++++++ Source/Chatbook/Dialogs.wl | 1 + Source/Chatbook/Main.wl | 1 + Source/Chatbook/UI.wl | 145 +------------------------------- 4 files changed, 111 insertions(+), 143 deletions(-) create mode 100644 Source/Chatbook/CloudToolbar.wl diff --git a/Source/Chatbook/CloudToolbar.wl b/Source/Chatbook/CloudToolbar.wl new file mode 100644 index 00000000..13c71667 --- /dev/null +++ b/Source/Chatbook/CloudToolbar.wl @@ -0,0 +1,107 @@ +(* ::Section::Closed:: *) +(*Package Header*) +BeginPackage[ "Wolfram`Chatbook`CloudToolbar`" ]; + +HoldComplete[ + `makeChatCloudDockedCellContents; +]; + +Begin[ "`Private`" ]; + +Needs[ "Wolfram`Chatbook`" ]; +Needs[ "Wolfram`Chatbook`Common`" ]; +Needs[ "Wolfram`Chatbook`Dialogs`" ]; +Needs[ "Wolfram`Chatbook`Dynamics`" ]; +Needs[ "Wolfram`Chatbook`PreferencesContent`" ]; + +(* ::**************************************************************************************************************:: *) +(* ::Section::Closed:: *) +(*Configuration*) +$notebookTypeLabelOptions = Sequence[ + FontColor -> RGBColor[ "#333333" ], + FontFamily -> "Source Sans Pro", + FontSize -> 16, + FontWeight -> "DemiBold" +]; + +(* ::**************************************************************************************************************:: *) +(* ::Section::Closed:: *) +(*Docked Cell Contents*) + +(* ::**************************************************************************************************************:: *) +(* ::Subsection::Closed:: *) +(*makeChatCloudDockedCellContents*) +makeChatCloudDockedCellContents // beginDefinition; + +makeChatCloudDockedCellContents[ ] := + Block[ { $preferencesScope := EvaluationNotebook[ ] }, + Grid[ + { + { + Item[ $cloudChatBanner, Alignment -> Left ], + Item[ "", ItemSize -> Fit ], + makePersonaSelector[ ], + makeModelSelector[ ] + } + }, + Dividers -> { { False, False, False, True }, False }, + Spacings -> { 2, 0 }, + BaseStyle -> { "Text", FontSize -> 14, FontColor -> GrayLevel[ 0.4 ] }, + FrameStyle -> Directive[ Thickness[ 2 ], GrayLevel[ 0.9 ] ] + ] + ]; + +makeChatCloudDockedCellContents // endDefinition; + +(* ::**************************************************************************************************************:: *) +(* ::Section::Closed:: *) +(*Notebook Type Label*) + +(* ::**************************************************************************************************************:: *) +(* ::Subsection::Closed:: *) +(*$cloudChatBanner*) +$cloudChatBanner := $cloudChatBanner = cvExpand @ PaneSelector[ + { True -> $chatDrivenNotebookLabel, False -> $chatEnabledNotebookLabel }, + Dynamic @ TrueQ @ cv[ EvaluationNotebook[ ], "ChatDrivenNotebook" ], + ImageSize -> Automatic +]; + +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*$chatDrivenNotebookLabel*) +$chatDrivenNotebookLabel := Grid[ + { + { + "", + chatbookIcon[ "ChatDrivenNotebookIcon", False ], + Style[ "Chat-Driven Notebook", $notebookTypeLabelOptions ] + } + }, + Alignment -> { Automatic, Center }, + Spacings -> 0.5 +]; + +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*$chatEnabledNotebookLabel*) +$chatEnabledNotebookLabel := Grid[ + { + { + "", + chatbookIcon[ "ChatEnabledNotebookIcon", False ], + Style[ "Chat-Enabled Notebook", $notebookTypeLabelOptions ] + } + }, + Alignment -> { Automatic, Center }, + Spacings -> 0.5 +]; + +(* ::**************************************************************************************************************:: *) +(* ::Section::Closed:: *) +(*Package Footer*) +If[ Wolfram`ChatbookInternal`$BuildingMX, + $cloudChatBanner; +]; + +End[ ]; +EndPackage[ ]; diff --git a/Source/Chatbook/Dialogs.wl b/Source/Chatbook/Dialogs.wl index 724f7aef..3e8048e4 100644 --- a/Source/Chatbook/Dialogs.wl +++ b/Source/Chatbook/Dialogs.wl @@ -256,6 +256,7 @@ redDialogButtonLabel // endDefinition; cvExpand // beginDefinition; cvExpand // Attributes = { HoldFirst }; cvExpand[ expr_ ] := expr /. $cvRules; +cvExpand /: SetDelayed[ lhs_, cvExpand[ rhs_ ] ] := Unevaluated @ SetDelayed[ lhs, rhs ] /. $cvRules; cvExpand // endDefinition; (* ::**************************************************************************************************************:: *) diff --git a/Source/Chatbook/Main.wl b/Source/Chatbook/Main.wl index 12d526b3..5f1baeca 100644 --- a/Source/Chatbook/Main.wl +++ b/Source/Chatbook/Main.wl @@ -98,6 +98,7 @@ Block[ { $ContextPath }, Get[ "Wolfram`Chatbook`ToolManager`" ]; Get[ "Wolfram`Chatbook`PersonaManager`" ]; Get[ "Wolfram`Chatbook`ChatHistory`" ]; + Get[ "Wolfram`Chatbook`CloudToolbar`" ]; ]; (* ::**************************************************************************************************************:: *) diff --git a/Source/Chatbook/UI.wl b/Source/Chatbook/UI.wl index bc6a4eb1..b6a94692 100644 --- a/Source/Chatbook/UI.wl +++ b/Source/Chatbook/UI.wl @@ -42,6 +42,7 @@ Begin["`Private`"] Needs[ "Wolfram`Chatbook`" ]; Needs[ "Wolfram`Chatbook`Actions`" ]; +Needs[ "Wolfram`Chatbook`CloudToolbar`" ]; Needs[ "Wolfram`Chatbook`Common`" ]; Needs[ "Wolfram`Chatbook`Dynamics`" ]; Needs[ "Wolfram`Chatbook`Errors`" ]; @@ -69,108 +70,7 @@ $chatMenuWidth = 220; (* ::**************************************************************************************************************:: *) (* ::Subsection::Closed:: *) (*MakeChatCloudDockedCellContents*) -MakeChatCloudDockedCellContents[] := Grid[ - {{ - Item[$cloudChatBanner, Alignment -> Left], - Item["", ItemSize -> Fit], - Row[{"Persona", Spacer[5], trackedDynamic[$cloudPersonaChooser, "Personas"]}], - Row[{"Model", Spacer[5], trackedDynamic[$cloudModelChooser, "Models"]}] - }}, - Dividers -> {{False, False, False, True}, False}, - Spacings -> {2, 0}, - BaseStyle -> {"Text", FontSize -> 14, FontColor -> GrayLevel[0.4]}, - FrameStyle -> Directive[Thickness[2], GrayLevel[0.9]] -] - -(* ::**************************************************************************************************************:: *) -(* ::Subsubsection::Closed:: *) -(*$cloudPersonaChooser*) -$cloudPersonaChooser := PopupMenu[ - Dynamic[ - Replace[ - CurrentValue[EvaluationNotebook[], {TaggingRules, "ChatNotebookSettings", "LLMEvaluator"}], - Inherited :> Lookup[$defaultChatSettings, "LLMEvaluator", "CodeAssistant"] - ], - Function[CurrentValue[EvaluationNotebook[], {TaggingRules, "ChatNotebookSettings", "LLMEvaluator"}] = #] - ], - KeyValueMap[ - Function[{key, as}, key -> Grid[{{resizeMenuIcon[getPersonaMenuIcon[as]], personaDisplayName[key, as]}}]], - GetCachedPersonaData[] - ], - ImageSize -> {Automatic, 30}, - Alignment -> {Left, Baseline}, - BaseStyle -> {FontSize -> 12} -] - -(* ::**************************************************************************************************************:: *) -(* ::Subsubsection::Closed:: *) -(*$cloudModelChooser*) -$cloudModelChooser := PopupMenu[ - Dynamic[ - Replace[ - CurrentValue[EvaluationNotebook[], {TaggingRules, "ChatNotebookSettings", "Model"}], - Inherited :> Lookup[$defaultChatSettings, "Model", "gpt-3.5-turbo"] - ], - Function[CurrentValue[EvaluationNotebook[], {TaggingRules, "ChatNotebookSettings", "Model"}] = #] - ], - KeyValueMap[ - {modelName, settings} |-> ( - modelName -> Grid[{{getModelMenuIcon[settings], modelDisplayName[modelName]}}] - ), - (* FIXME: use the new system *) - getModelsMenuItems[] - ], - ImageSize -> {Automatic, 30}, - Alignment -> {Left, Baseline}, - BaseStyle -> {FontSize -> 12} -] - -(* ::**************************************************************************************************************:: *) -(* ::Subsubsection::Closed:: *) -(*$cloudChatBanner*) -$cloudChatBanner := PaneSelector[ - { - True -> Grid[ - { - { - "", - chatbookIcon[ "ChatDrivenNotebookIcon", False ], - Style[ - "Chat-Driven Notebook", - FontColor -> RGBColor[ "#333333" ], - FontFamily -> "Source Sans Pro", - FontSize -> 16, - FontWeight -> "DemiBold" - ] - } - }, - Alignment -> { Automatic, Center }, - Spacings -> 0.5 - ], - False -> Grid[ - { - { - "", - chatbookIcon[ "ChatEnabledNotebookIcon", False ], - Style[ - "Chat-Enabled Notebook", - FontColor -> RGBColor[ "#333333" ], - FontFamily -> "Source Sans Pro", - FontSize -> 16, - FontWeight -> "DemiBold" - ] - } - }, - Alignment -> { Automatic, Center }, - Spacings -> 0.5 - ] - }, - Dynamic @ TrueQ @ CurrentValue[ - EvaluationNotebook[ ], - { TaggingRules, "ChatNotebookSettings", "ChatDrivenNotebook" } - ], - ImageSize -> Automatic -] +MakeChatCloudDockedCellContents[] := makeChatCloudDockedCellContents[ ]; (* ::**************************************************************************************************************:: *) (* ::Section::Closed:: *) @@ -399,47 +299,6 @@ labeledCheckbox[value_, label_, enabled_ : Automatic] := (*====================================*) -makeToolCallFrequencySlider[ obj_ ] := Pane[ - Grid[ - { - { - labeledCheckbox[ - Dynamic[ - currentChatSettings[ obj, "ToolCallFrequency" ] === Automatic, - Function[ - If[ TrueQ[ # ], - CurrentValue[ obj, { TaggingRules, "ChatNotebookSettings", "ToolCallFrequency" } ] = Inherited, - CurrentValue[ obj, { TaggingRules, "ChatNotebookSettings", "ToolCallFrequency" } ] = 0.5 - ] - ] - ], - Style[ "Choose automatically", "ChatMenuLabel" ] - ] - }, - { - Pane[ - Slider[ - Dynamic[ - Replace[ currentChatSettings[ obj, "ToolCallFrequency" ], Automatic -> 0.5 ], - (CurrentValue[ obj, { TaggingRules, "ChatNotebookSettings", "ToolCallFrequency" } ] = #) & - ], - { 0, 1, 0.01 }, - (* Enabled -> Dynamic[ currentChatSettings[ obj, "ToolCallFrequency" ] =!= Automatic ], *) - ImageSize -> { 150, Automatic }, - ImageMargins -> { { 5, 0 }, { 5, 5 } } - ], - ImageSize -> { 180, Automatic }, - BaseStyle -> { FontSize -> 12 } - ], - SpanFromLeft - } - }, - Alignment -> Left, - Spacings -> { Automatic, 0 } - ], - ImageMargins -> { { 5, 0 }, { 5, 5 } } -]; - makeToolCallFrequencySlider[ obj_ ] := Module[ { checkbox, slider }, checkbox = labeledCheckbox[