Overview
Concrete tool for embedding the jcode agent engine through a TypeScript SDK, covering client initialization, session management, turn execution, permission handling, and structured output.
Description
The JcodeClient class is the main entry point of the @1jehuang/jcode-sdk TypeScript SDK. It provides two static factory methods: launch() spawns a private daemon instance on an isolated socket, and connect() attaches to the user's existing daemon. Both complete a hello/hello_ok handshake to negotiate the API version. The client supports session creation with working directory and tool configuration, turn execution via NDJSON event streaming, permission handling (interactive or auto-approve), structured output with JSON Schema validation and correction retries, and clean shutdown.
Usage
- Embed jcode as engine: Use
launch() to start a private daemon and get an isolated client.
- Automate user's jcode: Use
connect() to attach to the user's running daemon.
- Stream events: Use
run() or events() to consume text_delta, tool_done, token_usage, and turn_complete events.
- Structured output: Use
runStructured() for JSON Schema-validated responses.
- Clean shutdown: Call
close() to disconnect and optionally stop a launched daemon.
Code Reference
Source Location
- Repository: jcode
- File: sdk/typescript/src/client.ts
Signature
class JcodeClient {
static async launch(options?: LaunchOptions & ConnectOptions): Promise<JcodeClient>
static async connect(options?: ConnectOptions): Promise<JcodeClient>
async createSession(options?: string | CreateSessionOptions): Promise<SessionInfo>
async run(sessionId: string, content: string, options?: RunOptions): Promise<TurnResult>
async sendMessage(sessionId: string, content: string, images?: ImageAttachment[]): Promise<void>
async events(sessionId?: string): AsyncIterableIterator<ApiEvent>
async respondToPermission(sessionId: string, requestId: string, decision: PermissionDecision): Promise<void>
async runStructured<T>(sessionId: string, content: string, options: RunStructuredOptions<T>): Promise<T>
close(): Promise<void>
}
interface TurnResult {
stopReason?: TurnStopReason;
text: string;
finalText: string;
messages: AssistantTextMessage[];
reasoning: string;
toolCalls: Array<{ callId: string; name: string; output: string; error?: string }>;
usage?: { input: number; output: number; cacheReadInput?: number };
}
Import
import { JcodeClient } from "@1jehuang/jcode-sdk";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| options.socketPath |
string |
No |
API socket path (defaults to shared daemon socket)
|
| options.clientName |
string |
No |
Client identity (default: "jcode-sdk-ts")
|
| options.transport |
Transport |
No |
Custom transport implementation
|
| options.requestTimeoutMs |
number |
No /// Request timeout (default: 30,000ms)
|
| options.inheritLogins |
boolean |
No /// Inherit user's provider logins (default: true, launch only)
|
| content |
string |
Yes |
User prompt text
|
| options.schema |
StructuredOutputSchema |
Yes (runStructured) |
JSON Schema for structured output
|
Outputs
| Name |
Type |
Description
|
| JcodeClient |
JcodeClient |
Client instance with completed handshake
|
| SessionInfo |
SessionInfo |
Session ID, provider, model, capabilities
|
| TurnResult |
TurnResult |
Final text, messages, reasoning, tool calls, usage
|
| events |
AsyncIterableIterator<ApiEvent> |
Stream of text_delta, tool_done, token_usage, turn_complete
|
Usage Examples
Launch and Run a Turn
import { JcodeClient } from "@1jehuang/jcode-sdk";
// 1. Launch a private instance
const client = await JcodeClient.launch({ inheritLogins: true });
// 2. Create a session
const session = await client.createSession({ workingDir: "/project" });
// 3. Run a turn with event streaming
const result = await client.run(session.session_id, "Refactor this function", {
onEvent: (event) => {
if (event.ev === "text_delta") process.stdout.write(event.text);
},
});
console.log(result.finalText);
// 4. Clean shutdown
await client.close();
Structured Output
const result = await client.runStructured(session.session_id, "Classify this issue", {
schema: {
type: "object",
properties: {
category: { type: "string", enum: ["bug", "feature", "question"] },
priority: { type: "string", enum: ["low", "medium", "high"] },
},
required: ["category", "priority"],
},
maxRetries: 2,
});
console.log(result.data); // { category: "bug", priority: "high" }
Connect to Existing Daemon
const client = await JcodeClient.connect({ clientName: "my-editor-plugin" });
const session = await client.createSession({ workingDir: process.cwd() });
// ... use the user's live sessions
Related Pages
Implements Principle
Requires Environment
Uses Heuristics