MCPcopy
hub / github.com/ax-llm/ax / ScriptedCodeSession

Class ScriptedCodeSession

packages/java/dev/axllm/ax/Conformance.java:310–387  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

308 }
309
310 static final class ScriptedCodeSession implements AxCodeSession {
311 final ScriptedCodeRuntime runtime;
312 Map<String, Object> globals;
313 Map<String, Object> createOptions;
314 boolean closed;
315
316 ScriptedCodeSession(ScriptedCodeRuntime runtime, Map<String, Object> globals, Map<String, Object> options) {
317 this.runtime = runtime;
318 this.globals = new LinkedHashMap<>(globals == null ? Map.of() : globals);
319 this.createOptions = new LinkedHashMap<>(options == null ? Map.of() : options);
320 }
321
322 public Object execute(String code, Map<String, Object> options) {
323 if (closed) return new LinkedHashMap<>(Map.of("is_error", true, "error_category", "session_closed", "error", "session closed"));
324 if (runtime.script.isEmpty()) throw new RuntimeException("scripted runtime exhausted");
325 Map<String, Object> step = Core.asMap(runtime.script.remove(0));
326 Object expected = step.get("expected_code");
327 if (expected != null && !String.valueOf(expected).equals(code)) throw new RuntimeException("expected code " + expected + ", got " + code);
328 if (step.containsKey("expected_options_subset")) assertSubset(options == null ? Map.of() : options, step.get("expected_options_subset"), "runtime execute options");
329 runtime.executed.add(code);
330 runtime.executeOptions.add(new LinkedHashMap<>(options == null ? Map.of() : options));
331 globals.putAll(Core.asMap(step.get("bindings_patch")));
332 if (Boolean.TRUE.equals(step.get("close_before_result"))) closed = true;
333 return step.getOrDefault("result", new LinkedHashMap<>(Map.of("kind", "result", "result", new LinkedHashMap<>(globals))));
334 }
335
336 public Object inspectGlobals(Map<String, Object> options) {
337 if (Boolean.FALSE.equals(runtime.capabilities.get("inspect"))) {
338 return "[runtime state inspection unavailable: runtime session does not implement inspectGlobals()]";
339 }
340 return new LinkedHashMap<>(globals);
341 }
342
343 public Object snapshotGlobals(Map<String, Object> options) {
344 if (Boolean.FALSE.equals(runtime.capabilities.get("snapshot"))) {
345 throw new RuntimeException("AxCodeSession.snapshotGlobals() is required to export AxAgent state");
346 }
347 List<Object> entries = new ArrayList<>();
348 for (Map.Entry<String, Object> entry : globals.entrySet()) {
349 entries.add(new LinkedHashMap<>(Map.of(
350 "name", entry.getKey(),
351 "type", entry.getValue() == null ? "null" : entry.getValue().getClass().getSimpleName(),
352 "preview", String.valueOf(entry.getValue())
353 )));
354 }
355 Map<String, Object> out = new LinkedHashMap<>();
356 out.put("version", 1);
357 out.put("entries", entries);
358 out.put("bindings", new LinkedHashMap<>(globals));
359 out.put("globals", new LinkedHashMap<>(globals));
360 out.put("closed", closed);
361 return out;
362 }
363
364 public Object patchGlobals(Object snapshot, Map<String, Object> options) {
365 if (Boolean.FALSE.equals(runtime.capabilities.get("patch"))) {
366 throw new RuntimeException("AxCodeSession.patchGlobals() is required to restore AxAgent state");
367 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…