Skip to content

Commit

Permalink
Merge pull request #164 from WolframResearch/161-chat-output-menu-exp…
Browse files Browse the repository at this point in the history
…loded-cells-items-are-not-implemented

Implement Explode Cells Menu Items
  • Loading branch information
rhennigan authored Jun 12, 2023
2 parents b1fb118 + 44b8c4d commit 141d116
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 1 deletion.
53 changes: 52 additions & 1 deletion Source/Chatbook/Actions.wl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Needs[ "Wolfram`Chatbook`PersonaInstaller`" ];
Needs[ "Wolfram`Chatbook`Personas`" ];
Needs[ "Wolfram`Chatbook`Serialization`" ];
Needs[ "Wolfram`Chatbook`Formatting`" ];
Needs[ "Wolfram`Chatbook`Explode`" ];
Needs[ "Wolfram`Chatbook`FrontEnd`" ];
Needs[ "Wolfram`Chatbook`InlineReferences`" ];
Needs[ "Wolfram`Chatbook`Prompting`" ];
Expand All @@ -45,9 +46,12 @@ ChatbookAction[ "AIAutoAssist" , args___ ] := catchMine @ AIAutoAssist @
ChatbookAction[ "Ask" , args___ ] := catchMine @ AskChat @ args;
ChatbookAction[ "AttachCodeButtons" , args___ ] := catchMine @ AttachCodeButtons @ args;
ChatbookAction[ "CopyChatObject" , args___ ] := catchMine @ CopyChatObject @ args;
ChatbookAction[ "CopyExplodedCells" , args___ ] := catchMine @ CopyExplodedCells @ args;
ChatbookAction[ "DisableAssistance" , args___ ] := catchMine @ DisableAssistance @ args;
ChatbookAction[ "EvaluateChatInput" , args___ ] := catchMine @ EvaluateChatInput @ args;
ChatbookAction[ "ExclusionToggle" , args___ ] := catchMine @ ExclusionToggle @ args;
ChatbookAction[ "ExplodeDuplicate" , args___ ] := catchMine @ ExplodeDuplicate @ args;
ChatbookAction[ "ExplodeInPlace" , args___ ] := catchMine @ ExplodeInPlace @ args;
ChatbookAction[ "InsertInlineReference", args___ ] := catchMine @ InsertInlineReference @ args;
ChatbookAction[ "OpenChatBlockSettings", args___ ] := catchMine @ OpenChatBlockSettings @ args;
ChatbookAction[ "OpenChatMenu" , args___ ] := catchMine @ OpenChatMenu @ args;
Expand All @@ -62,6 +66,53 @@ ChatbookAction[ "WidgetSend" , args___ ] := catchMine @ WidgetSend @ a
ChatbookAction[ name_String , args___ ] := catchMine @ throwFailure[ "NotImplemented", name, args ];
ChatbookAction[ args___ ] := catchMine @ throwInternalFailure @ ChatbookAction @ args;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*CopyExplodedCells*)
CopyExplodedCells // beginDefinition;

CopyExplodedCells[ cellObject_CellObject ] := Enclose[
Module[ { exploded },
exploded = ConfirmMatch[ explodeCell @ cellObject, { ___Cell }, "ExplodeCell" ];
CopyToClipboard @ exploded
],
throwInternalFailure[ CopyExplodedCells @ cellObject, ## ] &
];

CopyExplodedCells // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*ExplodeDuplicate*)
ExplodeDuplicate // beginDefinition;

ExplodeDuplicate[ cellObject_CellObject ] := Enclose[
Module[ { exploded, nbo },
exploded = ConfirmMatch[ explodeCell @ cellObject, { __Cell }, "ExplodeCell" ];
SelectionMove[ cellObject, After, Cell, AutoScroll -> False ];
nbo = ConfirmMatch[ parentNotebook @ cellObject, _NotebookObject, "ParentNotebook" ];
ConfirmMatch[ NotebookWrite[ nbo, exploded ], Null, "NotebookWrite" ]
],
throwInternalFailure[ ExplodeDuplicate @ cellObject, ## ] &
];

ExplodeDuplicate // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*ExplodeInPlace*)
ExplodeInPlace // beginDefinition;

ExplodeInPlace[ cellObject_CellObject ] := Enclose[
Module[ { exploded },
exploded = ConfirmMatch[ explodeCell @ cellObject, { __Cell }, "ExplodeCell" ];
ConfirmMatch[ NotebookWrite[ cellObject, exploded ], Null, "NotebookWrite" ]
],
throwInternalFailure[ ExplodeInPlace @ cellObject, ## ] &
];

ExplodeInPlace // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*ToggleFormatting*)
Expand Down Expand Up @@ -90,7 +141,7 @@ toggleFormatting // beginDefinition;
(* Convert a plain string to formatted TextData: *)
toggleFormatting[ cellObject_CellObject, nbo_NotebookObject, string_String ] := Enclose[
Module[ { textDataList },
textDataList = ConfirmMatch[ reformatTextData @ string, { (_String|_Cell|_StyleBox)... }, "ReformatTextData" ];
textDataList = ConfirmMatch[ reformatTextData @ string, $$textDataList, "ReformatTextData" ];
Confirm[ SelectionMove[ cellObject, All, CellContents, AutoScroll -> False ], "SelectionMove" ];
Confirm[ NotebookWrite[ nbo, TextData @ textDataList, None ], "NotebookWrite" ]
],
Expand Down
4 changes: 4 additions & 0 deletions Source/Chatbook/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ BeginPackage[ "Wolfram`Chatbook`Common`" ];
`$$chatOutputStyle;
`$$excludeHistoryStyle;

`$$textDataList;

`$catchTopTag;
`beginDefinition;
`catchTop;
Expand Down Expand Up @@ -60,6 +62,8 @@ $$chatInputStyle = Alternatives @@ $chatInputStyles | { ___, Alternati
$$chatOutputStyle = Alternatives @@ $chatOutputStyles | { ___, Alternatives @@ $chatOutputStyles , ___ };
$$excludeHistoryStyle = Alternatives @@ $excludeHistoryStyles | { ___, Alternatives @@ $excludeHistoryStyles, ___ };

$$textDataList = { (_String|_Cell|_StyleBox|_ButtonBox)... };

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Messages*)
Expand Down
148 changes: 148 additions & 0 deletions Source/Chatbook/Explode.wl
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
(* ::Section::Closed:: *)
(*Package Header*)
BeginPackage[ "Wolfram`Chatbook`Explode`" ];

`explodeCell;

Begin[ "`Private`" ];

Needs[ "Wolfram`Chatbook`" ];
Needs[ "Wolfram`Chatbook`Common`" ];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Explode Cell*)

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*explodeCell*)
explodeCell // beginDefinition;

explodeCell[ cellObject_CellObject ] := explodeCell @ NotebookRead @ cellObject;
explodeCell[ Cell[ content_, ___ ] ] := explodeCell @ content;
explodeCell[ string_String ] := Cell[ #, "Text" ] & /@ StringSplit[ string, Longest[ "\n".. ] ];
explodeCell[ (BoxData|TextData)[ textData_ ] ] := explodeCell @ textData;

explodeCell[ textData_List ] := Enclose[
Module[ { processed },
processed = ConfirmMatch[ ReplaceRepeated[ textData, $preprocessingRules ], $$textDataList, "Preprocessing" ];
Global`processed = processed;
ConfirmMatch[ regroupCells @ processed, $$textDataList, "RegroupCells" ]
],
throwInternalFailure[ explodeCell @ textData, ## ] &
];

explodeCell // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*$preprocessingRules*)
$preprocessingRules := $preprocessingRules = Dispatch @ {
(* Convert TextRefLink to plain hyperlink: *)
Cell @ BoxData @ TemplateBox[ { label_, uri_ }, "TextRefLink" ] :>
Cell @ BoxData @ ButtonBox[
StyleBox[ label, "Text" ],
BaseStyle -> "Link",
ButtonData -> uri,
ButtonNote -> uri
],

(* Convert interactive code blocks to input cells: *)
DynamicModuleBox[
_,
TagBox[ cell_Cell, ___ ],
___,
TaggingRules -> Association @ OrderlessPatternSequence[
"CellToStringType" -> "InlineInteractiveCodeCell",
___
],
___
] :> cell,

(* Convert "ChatCodeInlineTemplate" to "InlineCode" cells: *)
Cell[ BoxData @ TemplateBox[ { boxes_ }, "ChatCodeInlineTemplate" ], "ChatCode"|"ChatCodeActive", ___ ] :>
Cell[ BoxData @ boxes, "InlineCode" ],

(* Remove "ChatCode" styling from inputs: *)
Cell[ boxes_, "ChatCode", "Input", ___ ] :> Cell[ boxes, "Input" ],

(* Remove "ChatCodeBlock" styling: *)
Cell[ BoxData[ cell_Cell ], "ChatCodeBlock", ___ ] :> cell,

(* Remove "ChatCodeBlockTemplate" template boxes: *)
TemplateBox[ { cell_Cell }, "ChatCodeBlockTemplate" ] :> cell,

(* Remove nested cells: *)
Cell @ BoxData[ cell_Cell ] :> cell,

(* Remove extra style overrides from external language cells: *)
Cell[ content_, "ExternalLanguage", OrderlessPatternSequence[ System`CellEvaluationLanguage -> lang_, __ ] ] :>
Cell[ content, "ExternalLanguage", System`CellEvaluationLanguage -> lang ],

(* Inline tool calls: *)
Cell[ _, "InlineToolCall", TaggingRules -> tags: KeyValuePattern[ { } ], ___ ] :>
Cell[
BoxData @ ToBoxes @ Iconize[
tags, (* FIXME: iconize an actual tool response object here *)
"Used " <> Lookup[ tags, "DisplayName", Lookup[ tags, "Name", "LLMTool" ] ]
],
"Input"
]
};

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*regroupCells*)
regroupCells // beginDefinition;

regroupCells[ textData_List ] :=
regroupCells[ { }, { }, textData ];

regroupCells[ { grouped___ }, { grouping___ }, { cell: Cell[ _BoxData ], rest___ } ] :=
regroupCells[ { grouped }, { grouping, cell }, { rest } ];

regroupCells[ { grouped___ }, { grouping___ }, { box: _StyleBox|_ButtonBox|Cell[ _, "InlineCode", ___ ], rest___ } ] :=
regroupCells[ { grouped }, { grouping, box }, { rest } ];

regroupCells[ { grouped___ }, { grouping___ }, { cell: Cell[ _, "Input"|"ExternalLanguage", ___ ], rest___ } ] :=
regroupCells[ { grouped, Cell[ TextData @ { grouping }, "Text" ], cell }, { }, { rest } ];

regroupCells[ { grouped___ }, { grouping___ }, { string_String, rest___ } ] /; StringFreeQ[ string, "\n" ] :=
regroupCells[ { grouped }, { grouping, string }, { rest } ];

regroupCells[ { grouped___ }, { grouping___ }, { string_String, rest___ } ] :=
Replace[
StringSplit[ StringTrim[ string, Longest[ "\n".. ] ], Longest[ "\n".. ] ],
{
{ a_String, b___String, c_String } :>
regroupCells[
Flatten @ { grouped, Cell[ TextData @ { grouping, a }, "Text" ], Cell[ #, "Text" ] & /@ { b } },
{ c },
{ rest }
]
,
{ a_String } :>
regroupCells[ { grouped }, { grouping, a }, { rest } ]
,
{ } :>
regroupCells[ { grouped }, { grouping }, { rest } ]
,
___ :>
throwInternalFailure @ regroupCells[ { grouped }, { grouping }, { string, rest } ]
}
];

regroupCells[ { grouped___ }, { grouping___ }, { } ] :=
DeleteCases[ { grouped, Cell[ TextData @ { grouping }, "Text" ] }, Cell[ TextData @ { }, ___ ] ];

regroupCells // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Package Footer*)
If[ Wolfram`Chatbook`Internal`$BuildingMX,
$preprocessingRules;
];

End[ ];
EndPackage[ ];
1 change: 1 addition & 0 deletions Source/Chatbook/Main.wl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Block[ { $ContextPath },
Get[ "Wolfram`Chatbook`Tools`" ];
Get[ "Wolfram`Chatbook`Formatting`" ];
Get[ "Wolfram`Chatbook`Prompting`" ];
Get[ "Wolfram`Chatbook`Explode`" ];
Get[ "Wolfram`Chatbook`Actions`" ];
Get[ "Wolfram`Chatbook`Menus`" ];
Get[ "Wolfram`Chatbook`Personas`" ];
Expand Down

0 comments on commit 141d116

Please sign in to comment.