diff --git a/FrontEnd/StyleSheets/Chatbook.nb b/FrontEnd/StyleSheets/Chatbook.nb index 08db8e93..a51c96fc 100644 --- a/FrontEnd/StyleSheets/Chatbook.nb +++ b/FrontEnd/StyleSheets/Chatbook.nb @@ -1161,7 +1161,7 @@ Notebook[ ], Cell[ StyleData["ChatStyleSheetInformation"], - TaggingRules -> <|"StyleSheetVersion" -> "1.3.4.3910935567"|> + TaggingRules -> <|"StyleSheetVersion" -> "1.3.6.3913177292"|> ], Cell[ StyleData["Text"], diff --git a/PacletInfo.wl b/PacletInfo.wl index fdf93604..6cdb64e1 100644 --- a/PacletInfo.wl +++ b/PacletInfo.wl @@ -1,7 +1,7 @@ PacletObject[ <| "Name" -> "Wolfram/Chatbook", "PublisherID" -> "Wolfram", - "Version" -> "1.3.5", + "Version" -> "1.3.6", "WolframVersion" -> "13.3+", "Description" -> "Wolfram Notebooks + LLMs", "License" -> "MIT", diff --git a/Source/Chatbook/Actions.wl b/Source/Chatbook/Actions.wl index 0eef2b55..625b84ee 100644 --- a/Source/Chatbook/Actions.wl +++ b/Source/Chatbook/Actions.wl @@ -392,7 +392,10 @@ EvaluateChatInput // beginDefinition; EvaluateChatInput[ ] := EvaluateChatInput @ rootEvaluationCell[ ]; EvaluateChatInput[ evalCell_CellObject? chatInputCellQ ] := - EvaluateChatInput[ evalCell, parentNotebook @ evalCell ]; + If[ chatExcludedQ @ evalCell, + Null, + EvaluateChatInput[ evalCell, parentNotebook @ evalCell ] + ]; EvaluateChatInput[ source: _CellObject | $Failed ] := With[ { evalCell = rootEvaluationCell @ source }, @@ -1034,7 +1037,7 @@ SendChat // beginDefinition; SendChat[ ] := SendChat @ rootEvaluationCell[ ]; -SendChat[ evalCell_CellObject, ___ ] /; MemberQ[ CurrentValue[ evalCell, CellStyle ], "ChatExcluded" ] := Null; +SendChat[ evalCell_CellObject? chatExcludedQ, ___ ] := Null; SendChat[ evalCell_CellObject ] := SendChat[ evalCell, parentNotebook @ evalCell ]; diff --git a/Source/Chatbook/ChatHistory.wl b/Source/Chatbook/ChatHistory.wl index 56225c0f..5f8d41c8 100644 --- a/Source/Chatbook/ChatHistory.wl +++ b/Source/Chatbook/ChatHistory.wl @@ -6,6 +6,7 @@ BeginPackage[ "Wolfram`Chatbook`ChatHistory`" ]; HoldComplete[ `accentIncludedCells; + `chatExcludedQ; `extraCellHeight; `filterChatCells; `getCellsInChatHistory; @@ -172,24 +173,27 @@ selectChatHistoryCells // endDefinition; (* ::**************************************************************************************************************:: *) (* ::Subsection::Closed:: *) -(*filterChatCells*) -filterChatCells // beginDefinition; +(*chatExcludedQ*) +chatExcludedQ // beginDefinition; -filterChatCells[ cellInfo: { ___Association } ] := Enclose[ - Module[ { styleExcluded, tagExcluded, cells }, +chatExcludedQ[ cell_CellObject ] := chatExcludedQ @ cellInformation @ cell; - styleExcluded = DeleteCases[ cellInfo, KeyValuePattern[ "Style" -> $$chatIgnoredStyle ] ]; +chatExcludedQ[ Cell[ __, $$chatIgnoredStyle, ___ ] ] := True; +chatExcludedQ[ Cell[ __, TaggingRules -> tags_, ___ ] ] := chatExcludedQ @ tags; +chatExcludedQ[ Cell[ ___ ] ] := False; - tagExcluded = DeleteCases[ - styleExcluded, - KeyValuePattern[ "ChatNotebookSettings" -> KeyValuePattern[ "ExcludeFromChat" -> True ] ] - ]; +chatExcludedQ[ KeyValuePattern[ "Style" -> $$chatIgnoredStyle ] ] := True; +chatExcludedQ[ KeyValuePattern[ "ChatNotebookSettings" -> settings_ ] ] := chatExcludedQ @ settings; +chatExcludedQ[ KeyValuePattern[ "ExcludeFromChat" -> exclude_ ] ] := TrueQ @ exclude; +chatExcludedQ[ KeyValuePattern[ { } ] ] := False; - tagExcluded - ], - throwInternalFailure[ filterChatCells @ cellInfo, ## ] & -]; +chatExcludedQ // endDefinition; +(* ::**************************************************************************************************************:: *) +(* ::Subsection::Closed:: *) +(*filterChatCells*) +filterChatCells // beginDefinition; +filterChatCells[ cellInfo: { ___Association } ] := Select[ cellInfo, Not @* chatExcludedQ ]; filterChatCells // endDefinition; (* ::**************************************************************************************************************:: *) diff --git a/Source/Chatbook/ChatMessages.wl b/Source/Chatbook/ChatMessages.wl index 886dccbe..8d0ef48a 100644 --- a/Source/Chatbook/ChatMessages.wl +++ b/Source/Chatbook/ChatMessages.wl @@ -216,8 +216,14 @@ makeChatMessages[ settings_, cells_ ] := { $multimodalMessages = TrueQ @ settings[ "Multimodal" ], $tokenBudget = settings[ "MaxContextTokens" ], - $tokenPressure = 0.0 + $tokenPressure = 0.0, + $initialCellStringBudget = Replace[ + settings[ "MaxCellStringLength" ], + Except[ $$size ] -> $defaultMaxCellStringLength + ], + $cellStringBudget }, + $cellStringBudget = $initialCellStringBudget; If[ settings[ "BasePrompt" ] =!= None, tokenCheckedMessage[ settings, $fullBasePrompt ] ]; (* FIXME: need to account for persona/tool prompting as well *) makeChatMessages0[ settings, cells ] @@ -241,10 +247,6 @@ makeChatMessages0[ settings0_, cells_List ] := Enclose[ toMessage0 = Confirm[ getCellMessageFunction @ settings, "CellMessageFunction" ]; $tokenBudgetLog = Internal`Bag[ ]; - $initialCellStringBudget = Replace[ - settings[ "MaxCellStringLength" ], - Except[ $$size ] -> $defaultMaxCellStringLength - ]; toMessage = Function @ With[ { msg = toMessage0[ #1, <| #2, "TokenBudget" -> $tokenBudget, "TokenPressure" -> $tokenPressure |> ] }, @@ -304,6 +306,8 @@ combineExcisedMessages // endDefinition; (*tokenCheckedMessage*) tokenCheckedMessage // beginDefinition; +tokenCheckedMessage[ as_Association, message_ ] /; $cellStringBudget === Infinity := message; + tokenCheckedMessage[ as_Association, message_ ] := Enclose[ Catch @ Module[ { count, budget, resized }, diff --git a/Source/Chatbook/Formatting.wl b/Source/Chatbook/Formatting.wl index 976171ad..73b47ebd 100644 --- a/Source/Chatbook/Formatting.wl +++ b/Source/Chatbook/Formatting.wl @@ -453,7 +453,7 @@ insertCodeBelow[ cell_Cell, evaluate_ ] := ]; insertCodeBelow[ string_String, evaluate_ ] := - insertCodeBelow[ Cell[ BoxData @ string, "Input" ], evaluate ]; + insertCodeBelow[ reparseCodeBoxes @ Cell[ BoxData @ string, "Input" ], evaluate ]; insertCodeBelow // endDefinition; @@ -491,7 +491,7 @@ getCodeBlockContent[ TemplateBox[ { boxes_ }, "ChatCodeBlockTemplate", ___ ] ] : getCodeBlockContent[ Cell[ BoxData[ boxes_, ___ ] ] ] := getCodeBlockContent @ boxes; getCodeBlockContent[ DynamicModuleBox[ _, boxes_, ___ ] ] := getCodeBlockContent @ boxes; getCodeBlockContent[ TagBox[ boxes_, _EventHandlerTag, ___ ] ] := getCodeBlockContent @ boxes; -getCodeBlockContent[ Cell[ boxes_, "ChatCode", "Input", ___ ] ] := Cell[ boxes, "Input" ]; +getCodeBlockContent[ Cell[ boxes_, "ChatCode", "Input", ___ ] ] := reparseCodeBoxes @ Cell[ boxes, "Input" ]; getCodeBlockContent[ Cell[ boxes_, "ExternalLanguage", ___, CellEvaluationLanguage -> lang_, ___ ] ] := Cell[ boxes, "ExternalLanguage", CellEvaluationLanguage -> lang ]; @@ -500,6 +500,19 @@ getCodeBlockContent[ cell: Cell[ _, _String, ___ ] ] := cell; getCodeBlockContent // endDefinition; +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*reparseCodeBoxes*) +reparseCodeBoxes // beginDefinition; + +reparseCodeBoxes[ Cell[ BoxData[ s_String ], a___ ] ] /; $cloudNotebooks := + Cell[ BoxData @ UsingFrontEnd @ stringToBoxes @ s, a ]; + +reparseCodeBoxes[ cell_Cell ] := + cell; + +reparseCodeBoxes // endDefinition; + (* ::**************************************************************************************************************:: *) (* ::Subsection::Closed:: *) (*Boxes*) diff --git a/Source/Chatbook/FrontEnd.wl b/Source/Chatbook/FrontEnd.wl index f70d3950..cf91e05c 100644 --- a/Source/Chatbook/FrontEnd.wl +++ b/Source/Chatbook/FrontEnd.wl @@ -592,7 +592,7 @@ compressRasterBoxes // endDefinition; (* Effectively equivalent to OpenerView, except strips out the unnecessary interpretation information, and compresses the hidden part if it's very large. *) -openerView[ args___ ] := openerView[ args ] = openerView0[ args ]; +openerView[ args___ ] := Verbatim[ openerView[ args ] ] = openerView0[ args ]; openerView0 // beginDefinition; openerView0[ { a_, b_ }, args___ ] /; ByteCount @ b > 50000 := openerView1[ { a, compressUntilViewed @ b }, args ]; diff --git a/Source/Chatbook/SendChat.wl b/Source/Chatbook/SendChat.wl index 5249e805..2bad5180 100644 --- a/Source/Chatbook/SendChat.wl +++ b/Source/Chatbook/SendChat.wl @@ -1120,6 +1120,9 @@ selectChatCells0[ cell_, cells: { __CellObject }, final_ ] := Enclose[ (* Filter out ignored cells *) filtered = ConfirmMatch[ filterChatCells @ selectedRange, { ___Association }, "FilteredCellInfo" ]; + (* If all cells are excluded, do nothing *) + If[ filtered === { }, throwTop @ Null ]; + (* Delete output cells that come after the evaluation cell *) rest = deleteExistingChatOutputs @ Drop[ cellData, cellPosition ]; @@ -1298,6 +1301,7 @@ $autoSettingKeyPriority := Enclose[ (* FIXME: need to hook into token pressure to gradually decrease limits *) chooseMaxCellStringLength // beginDefinition; chooseMaxCellStringLength[ as_Association ] := chooseMaxCellStringLength[ as, as[ "MaxContextTokens" ] ]; +chooseMaxCellStringLength[ as_, Infinity ] := Infinity; chooseMaxCellStringLength[ as_, tokens: $$size ] := Ceiling[ $defaultMaxCellStringLength * tokens / 2^13 ]; chooseMaxCellStringLength // endDefinition; @@ -2297,14 +2301,7 @@ attachChatOutputMenu[ cell_CellObject ] /; $cloudNotebooks := Null; attachChatOutputMenu[ cell_CellObject ] := ( $lastChatOutput = cell; - NotebookDelete @ Cells[ cell, AttachedCell -> True, CellStyle -> "ChatMenu" ]; - AttachCell[ - cell, - Cell[ BoxData @ TemplateBox[ { "ChatOutput", RGBColor[ "#ecf0f5" ] }, "ChatMenuButton" ], "ChatMenu" ], - { Right, Top }, - Offset[ { -7, -7 }, { Right, Top } ], - { Right, Top } - ] + Block[ { EvaluationCell = cell & }, CurrentValue[ cell, Initialization ] ] ); attachChatOutputMenu // endDefinition; diff --git a/Source/Chatbook/UI.wl b/Source/Chatbook/UI.wl index 5a67436a..f91fc248 100644 --- a/Source/Chatbook/UI.wl +++ b/Source/Chatbook/UI.wl @@ -745,7 +745,7 @@ makeChatActionMenuContent[ }, Module[{ personaValue = OptionValue["PersonaValue"], modelValue = OptionValue["ModelValue"], - roleValue = OptionValue["RoleValue"], + roleValue = Replace[OptionValue["RoleValue"], {source_, Inherited} :> {source, "User"}], toolValue = OptionValue["ToolCallFrequency"], tempValue = OptionValue["TemperatureValue"], advancedSettingsMenu,