Skip to content

Commit

Permalink
Merge pull request #983 from WolframResearch/feature/inherit-some-NA-…
Browse files Browse the repository at this point in the history
…settings-in-popout-chat

Inherit some NA settings in popout chat
  • Loading branch information
rhennigan authored Dec 17, 2024
2 parents f622c8b + df93187 commit 67eef22
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion PacletInfo.wl
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
75 changes: 63 additions & 12 deletions Source/Chatbook/ChatModes/UI.wl
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand All @@ -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
Expand All @@ -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
];
Expand All @@ -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" ]
Expand All @@ -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*)
Expand Down Expand Up @@ -1977,7 +2028,7 @@ makeHistoryMenuItem[ Dynamic[ rows_ ], nbo_NotebookObject, chat_Association ] :=
{ {
Button[
$popOutButtonLabel,
popOutChatNB @ chat,
popOutChatNB[ chat, CurrentChatSettings @ nbo ],
Appearance -> "Suppressed"
],
Button[
Expand Down

0 comments on commit 67eef22

Please sign in to comment.