Skip to content

Commit

Permalink
Pull request #123: Use custom command to save the Notebook Assistant …
Browse files Browse the repository at this point in the history
…as a chat notebook

Merge in PAC/chatbook from bugfix/SaveAssistantAsChatbook to main

* commit '86c9e0bb5ab9dce2f39d019be50259ff85ab91e8':
  Added comment about possible crash in 14.1
  Use NotebookRead instead of NotebookGet
  Rebuilt stylesheets
  Bugfix: Bad replacement
  Added exported symbol `SaveAsChatNotebook` to be called from stylesheet code
  Some refactoring and error handling
  Use custom command to save the Notebook Assistant as a chat notebook
  • Loading branch information
Kevin Daily authored and rhennigan committed Dec 16, 2024
2 parents b20b976 + 86c9e0b commit f622c8b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Developer/Resources/WorkspaceStyles.wl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Cell[
DefaultNewCellStyle -> "AutoMoveToChatInputField",
DockedCells -> $workspaceChatDockedCells,
Magnification -> 0.85,
NotebookEventActions -> {
ParentList,
{ "MenuCommand", "SaveRename" } :> (
Needs[ "Wolfram`Chatbook`" -> None ];
Symbol[ "Wolfram`Chatbook`SaveAsChatNotebook" ][ EvaluationNotebook[ ] ]
)
},
Saveable -> False,
Selectable -> False,
ShowCellBracket -> False,
Expand Down
2 changes: 1 addition & 1 deletion FrontEnd/Assets/Extensions/CoreExtensions.nb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Notebook[
Cell["Chatbook Core.nb Extensions", "Title"],
Cell[
StyleData["ChatStyleSheetInformation"],
TaggingRules -> <|"StyleSheetVersion" -> "2.0.1.3943076969"|>
TaggingRules -> <|"StyleSheetVersion" -> "2.0.4.3943330431"|>
],
Cell[
StyleData["NotebookAssistant`Text"],
Expand Down
2 changes: 1 addition & 1 deletion FrontEnd/StyleSheets/Chatbook.nb
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ Notebook[
],
Cell[
StyleData["ChatStyleSheetInformation"],
TaggingRules -> <|"StyleSheetVersion" -> "2.0.1.3943076969"|>
TaggingRules -> <|"StyleSheetVersion" -> "2.0.4.3943330431"|>
],
Cell[
StyleData["NotebookAssistant`Text"],
Expand Down
12 changes: 9 additions & 3 deletions FrontEnd/StyleSheets/Wolfram/WorkspaceChat.nb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Notebook[
Saveable -> False,
WindowToolbars -> { },
CellInsertionPointCell -> None,
NotebookEventActions -> {
ParentList,
{"MenuCommand", "SaveRename"} :>
(Needs["Wolfram`Chatbook`" -> None];
Symbol["Wolfram`Chatbook`SaveAsChatNotebook"][
EvaluationNotebook[]
])
},
Selectable -> False,
WindowSize -> {350, Automatic},
WindowMargins -> {{0, Automatic}, {Automatic, 0}},
Expand Down Expand Up @@ -75,9 +83,7 @@ Notebook[
Cell[StyleData["CellExpression"], Selectable -> True],
Cell[
StyleData["WorkspaceChatStyleSheetInformation"],
TaggingRules -> <|
"WorkspaceChatStyleSheetVersion" -> "1.5.2.3.3941177587"
|>
TaggingRules -> <|"WorkspaceChatStyleSheetVersion" -> "2.0.4.3943330431"|>
],
Cell[
StyleData["AttachedCell"],
Expand Down
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.2",
"Version" -> "2.0.4",
"WolframVersion" -> "14.1+",
"Description" -> "Wolfram Notebooks + LLMs",
"License" -> "MIT",
Expand Down
45 changes: 45 additions & 0 deletions Source/Chatbook/ChatModes/UI.wl
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,51 @@ $inlineToWorkspaceConversionRules := $inlineToWorkspaceConversionRules = Dispatc
]
};

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*SaveAsChatNotebook*)
SaveAsChatNotebook // beginDefinition;
(* FIXME: This seems to often lead to a crash in 14.1, so we might need a version check to make this 14.2+ *)
SaveAsChatNotebook[ nbo_NotebookObject ] := catchMine @ saveAsChatNB @ nbo;
SaveAsChatNotebook // endExportedDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*saveAsChatNB*)
saveAsChatNB // beginDefinition;

saveAsChatNB[ targetObj_NotebookObject ] := Enclose[
Catch @ Module[ { cellObjects, title, filepath, cells, nbExpr },
cellObjects = Cells @ targetObj;
If[ ! MatchQ[ cellObjects, { __CellObject } ], Throw @ Null ];
title = Replace[
CurrentValue[ targetObj, { TaggingRules, "ConversationTitle" } ],
"" | Except[ _String ] -> None
];
filepath = SystemDialogInput[
"FileSave",
If[ title === None,
"UntitledChat-" <> DateString[ "ISODate" ] <> ".nb",
StringReplace[ title, " " -> "_" ] <> ".nb"
]
];
Which[
filepath === $Canceled,
Null,
StringQ @ filepath && StringEndsQ[ filepath, ".nb" ],
cells = NotebookRead @ cellObjects;
If[ ! MatchQ[ cells, { __Cell } ], Throw @ Null ];
nbExpr = ConfirmMatch[ cellsToChatNB @ cells, _Notebook, "Converted" ];
ConfirmBy[ Export[ filepath, nbExpr, "NB" ], FileExistsQ, "Exported" ],
True,
Null
]
],
throwInternalFailure
];

saveAsChatNB // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*popOutChatNB*)
Expand Down
2 changes: 2 additions & 0 deletions Source/Chatbook/Main.wl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ BeginPackage[ "Wolfram`Chatbook`" ];
`RelatedWolframAlphaQueries;
`RemoveChatFromSearchIndex;
`SandboxLinguisticAssistantData;
`SaveAsChatNotebook;
`SaveChat;
`SearchChats;
`SetModel;
Expand Down Expand Up @@ -243,6 +244,7 @@ $ChatbookProtectedNames = "Wolfram`Chatbook`" <> # & /@ {
"RelatedWolframAlphaQueries",
"RemoveChatFromSearchIndex",
"SandboxLinguisticAssistantData",
"SaveAsChatNotebook",
"SaveChat",
"SearchChats",
"SetModel",
Expand Down

0 comments on commit f622c8b

Please sign in to comment.