diff --git a/PacletInfo.wl b/PacletInfo.wl index 2fa06525..0bc24492 100644 --- a/PacletInfo.wl +++ b/PacletInfo.wl @@ -1,7 +1,7 @@ PacletObject[ <| "Name" -> "Wolfram/Chatbook", "PublisherID" -> "Wolfram", - "Version" -> "2.0.4", + "Version" -> "2.0.5", "WolframVersion" -> "14.1+", "Description" -> "Wolfram Notebooks + LLMs", "License" -> "MIT", diff --git a/Source/Chatbook/ChatModes/UI.wl b/Source/Chatbook/ChatModes/UI.wl index 86752675..9689e365 100644 --- a/Source/Chatbook/ChatModes/UI.wl +++ b/Source/Chatbook/ChatModes/UI.wl @@ -1380,7 +1380,7 @@ SaveAsChatNotebook // endExportedDefinition; saveAsChatNB // beginDefinition; saveAsChatNB[ targetObj_NotebookObject ] := Enclose[ - Catch @ Module[ { cellObjects, title, filepath, cells, nbExpr }, + Catch @ Module[ { cellObjects, title, filepath, cells, settings, nbExpr }, cellObjects = Cells @ targetObj; If[ ! MatchQ[ cellObjects, { __CellObject } ], Throw @ Null ]; title = Replace[ @@ -1400,7 +1400,8 @@ saveAsChatNB[ targetObj_NotebookObject ] := Enclose[ StringQ @ filepath && StringEndsQ[ filepath, ".nb" ], cells = NotebookRead @ cellObjects; If[ ! MatchQ[ cells, { __Cell } ], Throw @ Null ]; - nbExpr = ConfirmMatch[ cellsToChatNB @ cells, _Notebook, "Converted" ]; + settings = CurrentChatSettings @ targetObj; + nbExpr = ConfirmMatch[ cellsToChatNB[ cells, settings ], _Notebook, "Converted" ]; ConfirmBy[ Export[ filepath, nbExpr, "NB" ], FileExistsQ, "Exported" ], True, Null @@ -1415,24 +1416,25 @@ saveAsChatNB // endDefinition; (* ::Subsection::Closed:: *) (*popOutChatNB*) popOutChatNB // beginDefinition; -popOutChatNB[ nbo_NotebookObject ] := popOutChatNB @ NotebookGet @ nbo; -popOutChatNB[ Notebook[ cells_, ___ ] ] := popOutChatNB @ cells; -popOutChatNB[ Dynamic[ messageList_ ] ] := popOutChatNB @ messageList; -popOutChatNB[ cells: { ___Cell } ] := NotebookPut @ cellsToChatNB @ cells; -popOutChatNB[ chat_Association ] := popOutChatNB0 @ chat; +popOutChatNB[ nbo_NotebookObject ] := popOutChatNB[ NotebookGet @ nbo, CurrentChatSettings @ nbo ]; +popOutChatNB[ Notebook[ cells_, ___ ], settings_ ] := popOutChatNB[ cells, settings ]; +popOutChatNB[ cells: { ___Cell }, settings_ ] := NotebookPut @ cellsToChatNB[ cells, settings ]; +popOutChatNB[ chat_Association, settings_Association ] := popOutChatNB0[ chat, settings ]; popOutChatNB // endDefinition; popOutChatNB0 // beginDefinition; -popOutChatNB0[ id_ ] := Enclose[ - Module[ { loaded, uuid, title, messages, cells }, +popOutChatNB0[ id_, settings_Association ] := Enclose[ + Module[ { loaded, uuid, title, messages, dingbat, cells, options }, loaded = ConfirmBy[ LoadChat @ id, AssociationQ, "Loaded" ]; uuid = ConfirmBy[ loaded[ "ConversationUUID" ], StringQ, "UUID" ]; title = ConfirmBy[ loaded[ "ConversationTitle" ], StringQ, "Title" ]; messages = ConfirmBy[ loaded[ "Messages" ], ListQ, "Messages" ]; - cells = ConfirmMatch[ ChatMessageToCell @ messages, { __Cell }, "Cells" ]; - ConfirmMatch[ CreateChatNotebook @ cells, _NotebookObject, "Notebook" ] + dingbat = Cell[ BoxData @ makeOutputDingbat @ settings, Background -> None ]; + cells = ConfirmMatch[ updateCellDingbats[ ChatMessageToCell @ messages, dingbat ], { __Cell }, "Cells" ]; + options = Sequence @@ Normal[ settings, Association ]; + ConfirmMatch[ CreateChatNotebook[ cells, options ], _NotebookObject, "Notebook" ] ], throwInternalFailure ]; @@ -1447,9 +1449,43 @@ cellsToChatNB // beginDefinition; cellsToChatNB[ cells: { ___Cell } ] := Notebook[ cells /. $fromWorkspaceChatConversionRules, StyleDefinitions -> "Chatbook.nb" ]; +cellsToChatNB[ cells: { ___Cell }, settings_Association ] := + Module[ { dingbat, notebook }, + + dingbat = Cell[ BoxData @ makeOutputDingbat @ settings, Background -> None ]; + notebook = updateCellDingbats[ cellsToChatNB @ cells, dingbat ]; + + Append[ + notebook, + TaggingRules -> <| "ChatNotebookSettings" -> KeyTake[ settings, $popOutSettings ] |> + ] + ]; + cellsToChatNB // endDefinition; +$popOutSettings = { + "LLMEvaluator", + "MaxContextTokens", + "MaxToolResponses", + "Model" +}; + +(* TODO: we should really have something better for this *) +$evaluatedChatInputDingbat = Cell[ + BoxData @ DynamicBox @ ToBoxes[ + If[ TrueQ @ CloudSystem`$CloudNotebooks, + RawBoxes @ TemplateBox[ { }, "ChatIconUser" ], + RawBoxes @ TemplateBox[ { }, "ChatInputCellDingbat" ] + ], + StandardForm + ], + Background -> None, + CellFrame -> 0, + CellMargins -> 0 +]; + + $fromWorkspaceChatConversionRules := $fromWorkspaceChatConversionRules = Dispatch @ { Cell[ BoxData @ TemplateBox[ { Cell[ TextData[ text_ ], ___ ] }, "UserMessageBox", ___ ], "ChatInput", ___ ] :> Cell[ Flatten @ TextData @ text, "ChatInput" ] @@ -1461,6 +1497,21 @@ $fromWorkspaceChatConversionRules := $fromWorkspaceChatConversionRules = Dispatc Cell[ Flatten @ TextData @ text, "ChatOutput" ] }; +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*updateCellDingbats*) +updateCellDingbats // beginDefinition; + +updateCellDingbats[ cells_, outputDingbat_ ] := ReplaceAll[ + cells, + { + Cell[ a__, "ChatInput" , b___ ] :> Cell[ a, "ChatInput" , b, CellDingbat -> $evaluatedChatInputDingbat ], + Cell[ a__, "ChatOutput", b___ ] :> Cell[ a, "ChatOutput", b, CellDingbat -> outputDingbat ] + } +]; + +updateCellDingbats // endDefinition; + (* ::**************************************************************************************************************:: *) (* ::Section::Closed:: *) (*Overlay Menus*) @@ -1977,7 +2028,7 @@ makeHistoryMenuItem[ Dynamic[ rows_ ], nbo_NotebookObject, chat_Association ] := { { Button[ $popOutButtonLabel, - popOutChatNB @ chat, + popOutChatNB[ chat, CurrentChatSettings @ nbo ], Appearance -> "Suppressed" ], Button[