Principle:1jehuang Jcode SDK Session Lifecycle
| Knowledge Sources | |
|---|---|
| Domains | SDK, API_Design |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
An API design pattern for embedding an agent engine through a programmatic SDK, covering client initialization (launch or connect), session creation, turn execution with event streaming, permission handling, and clean shutdown.
Description
The SDK session lifecycle is a structured API for embedding an agent engine in external applications. It provides two initialization modes: launch() starts a private daemon instance (isolated socket, no shared state), and connect() attaches to the user's existing daemon. Sessions are created with a working directory and optional system prompt. Turns are executed by sending a prompt and consuming a stream of typed events (text deltas, tool calls, tool results, turn completion). Permission requests can be auto-approved or handled interactively.
The problem solved is programmatic access to an agent engine: without an SDK, embedding an agent requires shelling out to the CLI and parsing terminal output. The SDK provides a typed, event-driven interface that handles the NDJSON framing protocol, request-reply correlation, and lifecycle management.
The design includes structured output (JSON Schema-validated responses), tool configuration per session, and clean shutdown (closing the transport and optionally stopping a launched daemon).
Usage
Use this principle when building an application that needs to embed an agent engine programmatically. The trigger is: "the application needs to create agent sessions, send prompts, and consume structured events — not through a terminal." It is appropriate for: IDE plugins, chat bots, CI automation, and headless pipelines.
Theoretical Basis
The lifecycle has six phases:
- Initialization:
JcodeClient.launch(options)spawns a private daemon and connects to it, orJcodeClient.connect(options)connects to an existing daemon. Both complete a handshake (hello→hello_ok) to negotiate the API version. - Session creation:
createSession(options)sends acreate_sessionrequest and receives aSessionInfowith session ID, provider, and model. - Turn execution:
run(sessionId, prompt, options)sends the prompt viasendMessage()and consumes the event stream viaevents(). Events includetext_delta,tool_done,token_usage, andturn_complete. - Permission handling: When a
permission_requestevent arrives, the client callsrespondToPermission(sessionId, requestId, "allow"|"deny"). Auto-approval is available viaoptions.autoApprove. - Structured output:
runStructured(sessionId, prompt, {schema})wrapsrun()with JSON Schema validation and correction retries. - Shutdown:
close()closes the transport socket and, if the client launched a private instance, terminates the daemon and bridge processes.
Pseudo-code logic:
# Abstract algorithm (NOT real implementation)
async def sdk_lifecycle():
# 1. Initialize
client = await JcodeClient.launch({inheritLogins: True})
# 2. Create session
session = await client.createSession({workingDir: "/project"})
# 3. Run a turn
async for event in client.events(session.id):
if event.type == "text_delta":
print(event.text)
elif event.type == "turn_complete":
break
await client.sendMessage(session.id, "refactor this")
# 4. Structured output
result = await client.runStructured(
session.id, "classify this", {schema: classification_schema}
)
# 5. Shutdown
await client.close()
Related Pages
Implemented By
- Implementation:1jehuang_Jcode_JcodeClient
- Implementation:1jehuang_Jcode_Platform_Binary_Resolution
- Implementation:1jehuang_Jcode_NdjsonDecoder_EncodeFrame
- Implementation:1jehuang_Jcode_Sdk_Socket_Path_Resolution