Эх сурвалжийг харах

fix: symmetry_ll bracket bug + MCP execute_plugin console sync

- symmetry_ll: all variables now wrapped in () to avoid
  operator precedence issues with negative values
- execute_plugin: now publishes events via AppSession for
  real-time WebView console display

Co-Authored-By: Claude <noreply@anthropic.com>
nonvioce 3 сар өмнө
parent
commit
c2ee3c1322

+ 1 - 1
app/src/main/assets/plugins/symmetry_ll.json

@@ -12,5 +12,5 @@
     {"name": "B", "label": "对称轴Ax+By+C=0的B", "defaultValue": "0"},
     {"name": "C", "label": "对称轴Ax+By+C=0的C", "defaultValue": "0"}
   ],
-  "jsCode": "function calculate(inputs, Symja) {\n  var P=inputs.P, Q=inputs.Q, R=inputs.R, A=inputs.A, B=inputs.B, C=inputs.C;\n  var denom = Symja.eval(A + '^2 + ' + B + '^2');\n  var k = Symja.eval('2*(' + P + '*' + A + '+' + Q + '*' + B + ')/' + denom);\n  var newA = Symja.eval(P + '-' + A + '*(' + k + ')');\n  var newB = Symja.eval(Q + '-' + B + '*(' + k + ')');\n  var newC = Symja.eval(R + '-' + C + '*(' + k + ')');\n  Symja.print('原直线: ' + P + 'x+' + Q + 'y+' + R + '=0');\n  Symja.print('对称轴: ' + A + 'x+' + B + 'y+' + C + '=0');\n  Symja.print('对称直线: ' + newA + 'x + ' + newB + 'y + ' + newC + ' = 0');\n  return 'Null';\n}"
+  "jsCode": "function calculate(inputs, Symja) {\n  var P=inputs.P, Q=inputs.Q, R=inputs.R, A=inputs.A, B=inputs.B, C=inputs.C;\n  var denom = Symja.eval('(' + A + ')^2 + (' + B + ')^2');\n  var k = Symja.eval('2*((' + P + ')*(' + A + ')+(' + Q + ')*(' + B + '))/(' + denom + ')');\n  var newA = Symja.eval('(' + P + ')-(' + A + ')*(' + k + ')');\n  var newB = Symja.eval('(' + Q + ')-(' + B + ')*(' + k + ')');\n  var newC = Symja.eval('(' + R + ')-(' + C + ')*(' + k + ')');\n  Symja.print('原直线: ' + P + 'x+' + Q + 'y+' + R + '=0');\n  Symja.print('对称轴: ' + A + 'x+' + B + 'y+' + C + '=0');\n  Symja.print('对称直线: ' + newA + 'x + ' + newB + 'y + ' + newC + ' = 0');\n  return 'Null';\n}"
 }

+ 17 - 2
app/src/main/java/com/nonvioce/ijs/McpServer.java

@@ -389,7 +389,7 @@ public class McpServer extends NanoHTTPD {
             case "execute_plugin":
                 textResult = callExecutePlugin(
                     arguments.getString("plugin_id"),
-                    arguments.optJSONObject("arguments"));
+                    extractArgsObject(arguments, "arguments"));
                 break;
             case "plot":
                 textResult = callPlot(
@@ -545,6 +545,19 @@ public class McpServer extends NanoHTTPD {
         return pluginManager.deletePlugin(pluginId);
     }
 
+    /** 从工具参数中提取嵌套的 arguments,兼容 JSONObject 和 JSON 字符串 */
+    private JSONObject extractArgsObject(JSONObject params, String key) throws JSONException {
+        Object val = params.opt(key);
+        if (val == null || JSONObject.NULL.equals(val)) return null;
+        if (val instanceof JSONObject) return (JSONObject) val;
+        if (val instanceof String) {
+            String s = ((String) val).trim();
+            if (s.isEmpty()) return null;
+            return new JSONObject(s);
+        }
+        return null;
+    }
+
     private String callPlot(String function, String variable, String xMin, String xMax) {
         String result = symjaEngine.plot(function, variable, xMin, xMax);
         String expr = "Plot[" + function + ", {" + variable + ", " + xMin + ", " + xMax + "}]";
@@ -599,7 +612,9 @@ public class McpServer extends NanoHTTPD {
                 }
             }
 
-            return executePluginJS(jsCode, inputValues);
+            String result = executePluginJS(jsCode, inputValues);
+            AppSession.publishMcpEvent("plugin:" + pluginId, result);
+            return result;
         } catch (JSONException e) {
             return "Error: Malformed plugin data: " + e.getMessage();
         }