codex-sdk

MoonBit SDK for embedding the Codex agent in workflows and applications

openai
codex
sdk
moonbit
moon add totto2727/codex-sdk@0.4.0
Download zip
Author
Version
0.4.0
License
MIT
Last updated
2 days ago
Downloads
88
README

#Codex SDK for MoonBit

Embed the Codex agent in MoonBit workflows and applications.

This package is a direct MoonBit port of the official @openai/codex-sdk. It wraps the codex CLI and exchanges JSONL events over stdin and stdout.

The immutable upstream reference for this port is commit f201c30c52a35f819262865a53df94b6f4ea7a50. Every ported source process and test carries a comment linking to its corresponding file or line at that commit.

#Workspace usage

import {
"totto2727/codex-sdk" @codex_sdk,
}

The native codex executable must be available on PATH, or supplied with codex_path_override.

#Quickstart

async fn main {
let codex = @codex_sdk.Codex::Codex()
let thread = codex.start_thread()
let turn = thread.run(
@codex_sdk.Input::Prompt("Diagnose the test failure and propose a fix"),
)
println(turn.final_response)
}

Call run repeatedly on the same Thread value to continue that conversation.

#Streaming responses

MoonBit uses an asynchronous callback in place of TypeScript's AsyncGenerator. The callback receives the same structured event variants and remains inside the task that owns the Codex subprocess.

thread.run_streamed(
@codex_sdk.Input::Prompt("Diagnose the test failure"),
async event => {
match event {
ItemCompleted(completed) => println("\{completed.item}")
TurnCompleted(completed) => println("\{completed.usage}")
_ => ()
}
},
)

Cancelling the MoonBit task that runs run or run_streamed cancels the Codex subprocess, which is the native equivalent of passing an AbortSignal.

#Structured output

Pass a JSON object as the per-turn output schema. The SDK writes it to a temporary file and forwards the path through --output-schema.

let schema = Json::object({
"type": "object",
"properties": {
"summary": Json::object({ "type": "string" }),
},
"required": ["summary"],
"additionalProperties": false,
})
let turn = thread.run(
Prompt("Summarize repository status"),
turn_options=@codex_sdk.TurnOptions::TurnOptions(output_schema=schema),
)

#TypeScript parity

The public event, item, option, thread, and turn models follow the official TypeScript SDK. MoonBit paths use moonbitlang/x/path.Path, task cancellation replaces AbortSignal, and streaming uses an async callback because the pinned MoonBit async runtime does not expose an async-generator type. Node's optional-package binary lookup is replaced by PATH lookup because a MoonBit package has no Node module-resolution context.

The source layout follows the upstream files using MoonBit snake-case filenames:

Upstream TypeScriptMoonBit
codex.tscodex.mbt
codexOptions.tscodex_options.mbt
events.tsevents.mbt
exec.tsexec.mbt
index.tsindex.mbt
items.tsitems.mbt
outputSchemaFile.tsoutput_schema_file.mbt
thread.tsthread.mbt
threadOptions.tsthread_options.mbt
turnOptions.tsturn_options.mbt

MoonBit-only files without a direct upstream module use descriptive names and document the corresponding upstream process and language or test-runtime requirement.

#Tests

Run the native package suite from the repository root:

moon test --target native mbt/package/codex-sdk/src

The 37 upstream abort, exec, run, and runStreamed cases are ported one-for-one against a native fake Codex executable that records arguments, environment variables, stdin, schemas, JSONL events, process exits, and cancellation. Another 26 cases cover the explicit MoonBit item and event decoders, including every discriminated union branch, malformed payloads, unknown variants, and invalid JSONL. The Node-only optional-package layout cases are represented by documented MoonBit-runtime substitutions for an explicit executable override, PATH executable fallback, exact caller-provided PATH, and preservation of the Windows Path key.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io