@@ -88,6 +88,12 @@ $nlpData = <| |>;
8888
8989$$simpleTemplateBoxName = "Entity" |"EntityClass" |"EntityProperty" |"DateObject" |"Quantity" ;
9090
91+ (* ::**************************************************************************************************************:: *)
92+ (* ::Section::Closed:: *)
93+ (*Messages*)
94+ Chatbook ::InvalidEvaluatorCode = "Invalid evaluator code: `1`." ;
95+ Chatbook ::InvalidEvaluatorProperty = "Invalid evaluator property: `1`." ;
96+
9197(* ::**************************************************************************************************************:: *)
9298(* ::Section::Closed:: *)
9399(*WolframLanguageToolEvaluate*)
@@ -103,8 +109,8 @@ WolframLanguageToolEvaluate // Options = {
103109 TimeConstraint -> 60
104110};
105111
106- WolframLanguageToolEvaluate [ code_ ? validCodeQ , opts : OptionsPattern [ ] ] :=
107- catchMine @ WolframLanguageToolEvaluate [ code , "String" , opts ];
112+ WolframLanguageToolEvaluate [ code_ , opts : OptionsPattern [ ] ] :=
113+ catchMine @ WolframLanguageToolEvaluate [ Unevaluated @ code , "String" , opts ];
108114
109115WolframLanguageToolEvaluate [ code_ ? validCodeQ , property_ ? validPropertyQ , opts : OptionsPattern [ ] ] :=
110116 catchMine @ wolframLanguageToolEvaluate [
@@ -113,14 +119,21 @@ WolframLanguageToolEvaluate[ code_? validCodeQ, property_? validPropertyQ, opts:
113119 optionsAssociation [ WolframLanguageToolEvaluate , opts ]
114120 ];
115121
122+ WolframLanguageToolEvaluate [ code_ /; ! validCodeQ @ code , property_ , opts : OptionsPattern [ ] ] :=
123+ catchMine @ throwFailure [ "InvalidEvaluatorCode" , HoldForm @ code ];
124+
125+ WolframLanguageToolEvaluate [ code_ , property_ /; ! validPropertyQ @ property , opts : OptionsPattern [ ] ] :=
126+ catchMine @ throwFailure [ "InvalidEvaluatorProperty" , HoldForm @ property ];
127+
116128WolframLanguageToolEvaluate // endExportedDefinition ;
117129
118130(* ::**************************************************************************************************************:: *)
119131(* ::Subsection::Closed:: *)
120132(*validCodeQ*)
121133validCodeQ // beginDefinition ;
122- validCodeQ [ KeyValuePattern [ "code" -> code_ ] ] := validCodeQ @ code ;
123- validCodeQ [ code_ String ] := StringQ @ code ;
134+ validCodeQ // Attributes = { HoldAllComplete };
135+ validCodeQ [ code_ String ] := StringQ @ code && ! StringMatchQ [ code , WhitespaceCharacter ... ];
136+ validCodeQ [ { __ } ] := True ;
124137validCodeQ [ HoldComplete [ __ ] ] := True ;
125138validCodeQ [ ___ ] := False ;
126139validCodeQ // endDefinition ;
@@ -132,7 +145,7 @@ $$propertyName = "Packets"|"Result"|"SessionMX"|"String";
132145
133146validPropertyQ // beginDefinition ;
134147validPropertyQ [ $$propertyName ] := True ;
135- validPropertyQ [ All ] := True ;
148+ validPropertyQ [ All | Automatic ] := True ;
136149validPropertyQ [ { $$propertyName ... } ] := True ;
137150validPropertyQ [ ___ ] := False ;
138151validPropertyQ // endDefinition ;
@@ -141,6 +154,7 @@ validPropertyQ // endDefinition;
141154(* ::Subsection::Closed:: *)
142155(*wolframLanguageToolEvaluate*)
143156wolframLanguageToolEvaluate // beginDefinition ;
157+ wolframLanguageToolEvaluate // Attributes = { HoldFirst };
144158
145159wolframLanguageToolEvaluate [ code_ , property_ , opts_ Association ] := Enclose [
146160 Block [
@@ -154,7 +168,7 @@ wolframLanguageToolEvaluate[ code_, property_, opts_Association ] := Enclose[
154168 $sandboxEvaluationTimeout = getOption [ "TimeConstraint" , opts ],
155169 $Line = getOption [ "Line" , opts ]
156170 },
157- getProperty [ sandboxEvaluate @ code , property ]
171+ getProperty [ sandboxEvaluate @ preprocessCodeForTool @ code , property ]
158172 ],
159173 throwInternalFailure
160174];
@@ -205,10 +219,46 @@ getOption // endDefinition;
205219(*getProperty*)
206220getProperty // beginDefinition ;
207221getProperty [ as_ Association , property_ String ] := Lookup [ as , property ];
208- getProperty [ as_ Association , All ] := as ;
222+ getProperty [ as_ Association , All | Automatic ] := as ;
209223getProperty [ as_ Association , properties_ List ] := AssociationMap [ getProperty [ as , # ] & , properties ];
210224getProperty // endDefinition ;
211225
226+ (* ::**************************************************************************************************************:: *)
227+ (* ::Subsubsection::Closed:: *)
228+ (*preprocessCodeForTool*)
229+ preprocessCodeForTool // beginDefinition ;
230+ preprocessCodeForTool // Attributes = { HoldAllComplete };
231+
232+ preprocessCodeForTool [ code : _ String | _ HoldComplete ] :=
233+ code ;
234+
235+ preprocessCodeForTool [ code_ List ] := Enclose [
236+ StringJoin @ ConfirmMatch [ makeInlineExpressionString /@ Unevaluated @ code , { __ String }, "Inlined" ],
237+ throwInternalFailure
238+ ];
239+
240+ preprocessCodeForTool // endDefinition ;
241+
242+ (* ::**************************************************************************************************************:: *)
243+ (* ::Subsubsection::Closed:: *)
244+ (*makeInlineExpressionString*)
245+ makeInlineExpressionString // beginDefinition ;
246+ makeInlineExpressionString // Attributes = { HoldAllComplete };
247+
248+ makeInlineExpressionString [ fragment_ String ] :=
249+ fragment ;
250+
251+ makeInlineExpressionString [ expr_ ] := Enclose [
252+ Module [ { link , key },
253+ link = ConfirmBy [ MakeExpressionURI @ Unevaluated @ expr , StringQ , "Link" ];
254+ key = ConfirmBy [ expressionURIKey @ link , StringQ , "Key" ];
255+ "InlinedExpression[\" attachment://" <> key <> "\" ]"
256+ ],
257+ throwInternalFailure
258+ ];
259+
260+ makeInlineExpressionString // endDefinition ;
261+
212262(* ::**************************************************************************************************************:: *)
213263(* ::Section::Closed:: *)
214264(*SandboxLinguisticAssistantData*)
@@ -941,7 +991,7 @@ toSandboxExpression[ s_String ] := $lastSandboxExpression =
941991 ];
942992
943993toSandboxExpression [ s_ , expr_ HoldComplete ] :=
944- expandSandboxMacros @ expr ;
994+ expr ;
945995
946996toSandboxExpression [ s_ String , $Failed ] /; StringContainsQ [ s , "'" ] :=
947997 Module [ { new , held },
@@ -992,7 +1042,7 @@ toSandboxExpression // endDefinition;
9921042toHeldExpression // beginDefinition ;
9931043
9941044toHeldExpression [ s_ String ] :=
995- Replace [
1045+ expandSandboxMacros @ Replace [
9961046 DeleteCases [ Quiet @ ToExpression [ s , InputForm , HoldComplete ], Null ],
9971047 {
9981048 HoldComplete [ xs__ , CompoundExpression [ x_ , Null ] ] :> Replace [
@@ -1069,6 +1119,8 @@ expandSandboxMacros[ expr_HoldComplete ] := Enclose[
10691119 throwInternalFailure
10701120];
10711121
1122+ expandSandboxMacros [ $Failed ] := $Failed ;
1123+
10721124expandSandboxMacros // endDefinition ;
10731125
10741126(* ::**************************************************************************************************************:: *)
0 commit comments