Create a context

ctx = machine.code_interpreter.create_code_context(
    cwd="/workspace",
    language="python",
)
const ctx = await machine.code.createContext({
  cwd: "/workspace",
  language: "python",
});
nullspace machine code context create mch_123 --cwd /workspace --language python
curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{"cwd": "/workspace", "language": "python"}'

Run in a context

machine.code_interpreter.run_code("value = 10", context=ctx)
result = machine.code_interpreter.run_code("value * 2", context=ctx)
print(result.results[0].text)
await machine.code.run("value = 10", { context: ctx });
const result = await machine.code.run("value * 2", { context: ctx });
console.log(result.results[0].text);
nullspace machine code run mch_123 "value = 10" --context <context-id>
nullspace machine code run mch_123 "value * 2" --context <context-id>
# returns a Server-Sent Events stream of JSON events
curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{"code": "value = 10", "context_id": "<context-id>"}'
curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{"code": "value * 2", "context_id": "<context-id>"}'

List, restart, and remove

contexts = machine.code_interpreter.list_code_contexts()
for item in contexts:
    print(item.id, item.language, item.cwd)

machine.code_interpreter.restart_code_context(ctx)
machine.code_interpreter.remove_code_context(ctx)
const contexts = await machine.code.listContexts();
for (const item of contexts) {
  console.log(item.id, item.language, item.cwd);
}

await machine.code.restartContext(ctx);
await machine.code.removeContext(ctx);
nullspace machine code context list mch_123
nullspace machine code context restart mch_123 <context-id>
nullspace machine code context remove mch_123 <context-id>
curl "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}"
curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts/<context-id>/restart" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}"
curl -X DELETE "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts/<context-id>" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}"

Interrupt long-running code

machine.code_interpreter.interrupt_code_context(ctx)
await machine.code.interruptContext(ctx);
nullspace machine code context interrupt mch_123 <context-id>
curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts/<context-id>/interrupt" \
  -H "Authorization: Bearer ${NULLSPACE_API_KEY}"