Skip to content

Commit f622c8b

Browse files
KMDailyrhennigan
authored andcommitted
Pull request #123: Use custom command to save the Notebook Assistant 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
2 parents b20b976 + 86c9e0b commit f622c8b

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

Developer/Resources/WorkspaceStyles.wl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Cell[
1313
DefaultNewCellStyle -> "AutoMoveToChatInputField",
1414
DockedCells -> $workspaceChatDockedCells,
1515
Magnification -> 0.85,
16+
NotebookEventActions -> {
17+
ParentList,
18+
{ "MenuCommand", "SaveRename" } :> (
19+
Needs[ "Wolfram`Chatbook`" -> None ];
20+
Symbol[ "Wolfram`Chatbook`SaveAsChatNotebook" ][ EvaluationNotebook[ ] ]
21+
)
22+
},
1623
Saveable -> False,
1724
Selectable -> False,
1825
ShowCellBracket -> False,

FrontEnd/Assets/Extensions/CoreExtensions.nb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Notebook[
1111
Cell["Chatbook Core.nb Extensions", "Title"],
1212
Cell[
1313
StyleData["ChatStyleSheetInformation"],
14-
TaggingRules -> <|"StyleSheetVersion" -> "2.0.1.3943076969"|>
14+
TaggingRules -> <|"StyleSheetVersion" -> "2.0.4.3943330431"|>
1515
],
1616
Cell[
1717
StyleData["NotebookAssistant`Text"],

FrontEnd/StyleSheets/Chatbook.nb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ Notebook[
824824
],
825825
Cell[
826826
StyleData["ChatStyleSheetInformation"],
827-
TaggingRules -> <|"StyleSheetVersion" -> "2.0.1.3943076969"|>
827+
TaggingRules -> <|"StyleSheetVersion" -> "2.0.4.3943330431"|>
828828
],
829829
Cell[
830830
StyleData["NotebookAssistant`Text"],

FrontEnd/StyleSheets/Wolfram/WorkspaceChat.nb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ Notebook[
1515
Saveable -> False,
1616
WindowToolbars -> { },
1717
CellInsertionPointCell -> None,
18+
NotebookEventActions -> {
19+
ParentList,
20+
{"MenuCommand", "SaveRename"} :>
21+
(Needs["Wolfram`Chatbook`" -> None];
22+
Symbol["Wolfram`Chatbook`SaveAsChatNotebook"][
23+
EvaluationNotebook[]
24+
])
25+
},
1826
Selectable -> False,
1927
WindowSize -> {350, Automatic},
2028
WindowMargins -> {{0, Automatic}, {Automatic, 0}},
@@ -75,9 +83,7 @@ Notebook[
7583
Cell[StyleData["CellExpression"], Selectable -> True],
7684
Cell[
7785
StyleData["WorkspaceChatStyleSheetInformation"],
78-
TaggingRules -> <|
79-
"WorkspaceChatStyleSheetVersion" -> "1.5.2.3.3941177587"
80-
|>
86+
TaggingRules -> <|"WorkspaceChatStyleSheetVersion" -> "2.0.4.3943330431"|>
8187
],
8288
Cell[
8389
StyleData["AttachedCell"],

PacletInfo.wl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PacletObject[ <|
22
"Name" -> "Wolfram/Chatbook",
33
"PublisherID" -> "Wolfram",
4-
"Version" -> "2.0.2",
4+
"Version" -> "2.0.4",
55
"WolframVersion" -> "14.1+",
66
"Description" -> "Wolfram Notebooks + LLMs",
77
"License" -> "MIT",

Source/Chatbook/ChatModes/UI.wl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,51 @@ $inlineToWorkspaceConversionRules := $inlineToWorkspaceConversionRules = Dispatc
13661366
]
13671367
};
13681368

1369+
(* ::**************************************************************************************************************:: *)
1370+
(* ::Subsection::Closed:: *)
1371+
(*SaveAsChatNotebook*)
1372+
SaveAsChatNotebook // beginDefinition;
1373+
(* FIXME: This seems to often lead to a crash in 14.1, so we might need a version check to make this 14.2+ *)
1374+
SaveAsChatNotebook[ nbo_NotebookObject ] := catchMine @ saveAsChatNB @ nbo;
1375+
SaveAsChatNotebook // endExportedDefinition;
1376+
1377+
(* ::**************************************************************************************************************:: *)
1378+
(* ::Subsubsection::Closed:: *)
1379+
(*saveAsChatNB*)
1380+
saveAsChatNB // beginDefinition;
1381+
1382+
saveAsChatNB[ targetObj_NotebookObject ] := Enclose[
1383+
Catch @ Module[ { cellObjects, title, filepath, cells, nbExpr },
1384+
cellObjects = Cells @ targetObj;
1385+
If[ ! MatchQ[ cellObjects, { __CellObject } ], Throw @ Null ];
1386+
title = Replace[
1387+
CurrentValue[ targetObj, { TaggingRules, "ConversationTitle" } ],
1388+
"" | Except[ _String ] -> None
1389+
];
1390+
filepath = SystemDialogInput[
1391+
"FileSave",
1392+
If[ title === None,
1393+
"UntitledChat-" <> DateString[ "ISODate" ] <> ".nb",
1394+
StringReplace[ title, " " -> "_" ] <> ".nb"
1395+
]
1396+
];
1397+
Which[
1398+
filepath === $Canceled,
1399+
Null,
1400+
StringQ @ filepath && StringEndsQ[ filepath, ".nb" ],
1401+
cells = NotebookRead @ cellObjects;
1402+
If[ ! MatchQ[ cells, { __Cell } ], Throw @ Null ];
1403+
nbExpr = ConfirmMatch[ cellsToChatNB @ cells, _Notebook, "Converted" ];
1404+
ConfirmBy[ Export[ filepath, nbExpr, "NB" ], FileExistsQ, "Exported" ],
1405+
True,
1406+
Null
1407+
]
1408+
],
1409+
throwInternalFailure
1410+
];
1411+
1412+
saveAsChatNB // endDefinition;
1413+
13691414
(* ::**************************************************************************************************************:: *)
13701415
(* ::Subsection::Closed:: *)
13711416
(*popOutChatNB*)

Source/Chatbook/Main.wl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ BeginPackage[ "Wolfram`Chatbook`" ];
7777
`RelatedWolframAlphaQueries;
7878
`RemoveChatFromSearchIndex;
7979
`SandboxLinguisticAssistantData;
80+
`SaveAsChatNotebook;
8081
`SaveChat;
8182
`SearchChats;
8283
`SetModel;
@@ -243,6 +244,7 @@ $ChatbookProtectedNames = "Wolfram`Chatbook`" <> # & /@ {
243244
"RelatedWolframAlphaQueries",
244245
"RemoveChatFromSearchIndex",
245246
"SandboxLinguisticAssistantData",
247+
"SaveAsChatNotebook",
246248
"SaveChat",
247249
"SearchChats",
248250
"SetModel",

0 commit comments

Comments
 (0)