Skip to content

Commit 31102af

Browse files
authored
Add initial set of doc comments to the SDK (#4513)
Also perform minor code cleanup.
1 parent 5d78c1e commit 31102af

File tree

8 files changed

+201
-33
lines changed

8 files changed

+201
-33
lines changed

codex-rs/exec/src/exec_events.rs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,45 @@ use serde::Deserialize;
22
use serde::Serialize;
33
use ts_rs::TS;
44

5-
/// Top-level events emitted on the Codex Exec thread stream.
5+
/// Top-level JSONL events emitted by codex exec
66
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
77
#[serde(tag = "type")]
88
pub enum ThreadEvent {
9+
/// Emitted when a new thread is started as the first event.
910
#[serde(rename = "thread.started")]
1011
ThreadStarted(ThreadStartedEvent),
12+
/// Emitted when a turn is started by sending a new prompt to the model.
13+
/// A turn encompasses all events that happen while agent is processing the prompt.
1114
#[serde(rename = "turn.started")]
1215
TurnStarted(TurnStartedEvent),
16+
/// Emitted when a turn is completed. Typically right after the assistant's response.
1317
#[serde(rename = "turn.completed")]
1418
TurnCompleted(TurnCompletedEvent),
19+
/// Indicates that a turn failed with an error.
1520
#[serde(rename = "turn.failed")]
1621
TurnFailed(TurnFailedEvent),
22+
/// Emitted when a new item is added to the thread. Typically the item will be in an "in progress" state.
1723
#[serde(rename = "item.started")]
1824
ItemStarted(ItemStartedEvent),
25+
/// Emitted when an item is updated.
1926
#[serde(rename = "item.updated")]
2027
ItemUpdated(ItemUpdatedEvent),
28+
/// Signals that an item has reached a terminal state—either success or failure.
2129
#[serde(rename = "item.completed")]
2230
ItemCompleted(ItemCompletedEvent),
31+
/// Represents an unrecoverable error emitted directly by the event stream.
2332
#[serde(rename = "error")]
2433
Error(ThreadErrorEvent),
2534
}
2635

2736
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
2837
pub struct ThreadStartedEvent {
38+
/// The identified of the new thread. Can be used to resume the thread later.
2939
pub thread_id: String,
3040
}
3141

3242
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS, Default)]
43+
3344
pub struct TurnStartedEvent {}
3445

3546
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
@@ -42,11 +53,14 @@ pub struct TurnFailedEvent {
4253
pub error: ThreadErrorEvent,
4354
}
4455

45-
/// Minimal usage summary for a turn.
56+
/// Describes the usage of tokens during a turn.
4657
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS, Default)]
4758
pub struct Usage {
59+
/// The number of input tokens used during the turn.
4860
pub input_tokens: u64,
61+
/// The number of cached input tokens used during the turn.
4962
pub cached_input_tokens: u64,
63+
/// The number of output tokens used during the turn.
5064
pub output_tokens: u64,
5165
}
5266

@@ -83,34 +97,44 @@ pub struct ThreadItem {
8397
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
8498
#[serde(tag = "item_type", rename_all = "snake_case")]
8599
pub enum ThreadItemDetails {
100+
/// Response from the agent.
101+
/// Either a natural-language response or a JSON string when structured output is requested.
86102
AssistantMessage(AssistantMessageItem),
103+
/// Agent's reasoning summary.
87104
Reasoning(ReasoningItem),
105+
/// Tracks a command executed by the agent. The item starts when the command is
106+
/// spawned, and completes when the process exits with an exit code.
88107
CommandExecution(CommandExecutionItem),
108+
/// Represents a set of file changes by the agent. The item is emitted only as a
109+
/// completed event once the patch succeeds or fails.
89110
FileChange(FileChangeItem),
111+
/// Represents a call to an MCP tool. The item starts when the invocation is
112+
/// dispatched and completes when the MCP server reports success or failure.
90113
McpToolCall(McpToolCallItem),
114+
/// Captures a web search request. It starts when the search is kicked off
115+
/// and completes when results are returned to the agent.
91116
WebSearch(WebSearchItem),
117+
/// Tracks the agent's running to-do list. It starts when the plan is first
118+
/// issued, updates as steps change state, and completes when the turn ends.
92119
TodoList(TodoListItem),
120+
/// Describes a non-fatal error surfaced as an item.
93121
Error(ErrorItem),
94122
}
95123

96-
/// Session metadata.
97-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
98-
pub struct SessionItem {
99-
pub session_id: String,
100-
}
101-
102-
/// Assistant message payload.
124+
/// Response from the agent.
125+
/// Either a natural-language response or a JSON string when structured output is requested.
103126
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
104127
pub struct AssistantMessageItem {
105128
pub text: String,
106129
}
107130

108-
/// Model reasoning summary payload.
131+
/// Agent's reasoning summary.
109132
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
110133
pub struct ReasoningItem {
111134
pub text: String,
112135
}
113136

137+
/// The status of a command execution.
114138
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default, TS)]
115139
#[serde(rename_all = "snake_case")]
116140
pub enum CommandExecutionStatus {
@@ -120,7 +144,7 @@ pub enum CommandExecutionStatus {
120144
Failed,
121145
}
122146

123-
/// Local shell command execution payload.
147+
/// A command executed by the agent.
124148
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
125149
pub struct CommandExecutionItem {
126150
pub command: String,
@@ -130,28 +154,29 @@ pub struct CommandExecutionItem {
130154
pub status: CommandExecutionStatus,
131155
}
132156

133-
/// Single file change summary for a patch.
157+
/// A set of file changes by the agent.
134158
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
135159
pub struct FileUpdateChange {
136160
pub path: String,
137161
pub kind: PatchChangeKind,
138162
}
139163

164+
/// The status of a file change.
140165
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
141166
#[serde(rename_all = "snake_case")]
142167
pub enum PatchApplyStatus {
143168
Completed,
144169
Failed,
145170
}
146171

147-
/// Patch application payload.
172+
/// A set of file changes by the agent.
148173
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
149174
pub struct FileChangeItem {
150175
pub changes: Vec<FileUpdateChange>,
151176
pub status: PatchApplyStatus,
152177
}
153178

154-
/// Known change kinds for a patch.
179+
/// Indicates the type of the file change.
155180
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
156181
#[serde(rename_all = "snake_case")]
157182
pub enum PatchChangeKind {
@@ -160,6 +185,7 @@ pub enum PatchChangeKind {
160185
Update,
161186
}
162187

188+
/// The status of an MCP tool call.
163189
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default, TS)]
164190
#[serde(rename_all = "snake_case")]
165191
pub enum McpToolCallStatus {
@@ -169,23 +195,27 @@ pub enum McpToolCallStatus {
169195
Failed,
170196
}
171197

198+
/// A call to an MCP tool.
172199
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
173200
pub struct McpToolCallItem {
174201
pub server: String,
175202
pub tool: String,
176203
pub status: McpToolCallStatus,
177204
}
178205

206+
/// A web search request.
179207
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
180208
pub struct WebSearchItem {
181209
pub query: String,
182210
}
183211

212+
/// An error notification.
184213
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
185214
pub struct ErrorItem {
186215
pub message: String,
187216
}
188217

218+
/// An item in agent's to-do list.
189219
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
190220
pub struct TodoItem {
191221
pub text: String,

sdk/typescript/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
11
# Codex SDK
2+
3+
Bring the power of the best coding agent to your application.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @openai/codex-sdk
9+
```
10+
11+
## Usage
12+
13+
Call `startThread()` and `run()` to start a thead with Codex.
14+
15+
```typescript
16+
import { Codex } from "@openai/codex-sdk";
17+
18+
const codex = new Codex();
19+
const thread = codex.startThread();
20+
const result = await thread.run("Diagnose the test failure and propose a fix");
21+
22+
console.log(result);
23+
```
24+
25+
You can call `run()` again to continue the same thread.
26+
27+
```typescript
28+
const result = await thread.run("Implement the fix");
29+
30+
console.log(result);
31+
```
32+
33+
### Streaming
34+
35+
The `await run()` method completes when a thread turn is complete and agent is prepared the final response.
36+
37+
You can thread items while they are being produced by calling `await runStreamed()`.
38+
39+
```typescript
40+
const result = thread.runStreamed("Diagnose the test failure and propose a fix");
41+
```
42+
43+
### Resuming a thread
44+
45+
If you don't have the original `Thread` instance to continue the thread, you can resume a thread by calling `resumeThread()` and providing the thread.
46+
47+
```typescript
48+
const threadId = "...";
49+
const thread = codex.resumeThread(threadId);
50+
const result = await thread.run("Implement the fix");
51+
52+
console.log(result);
53+
```

sdk/typescript/src/codex.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { CodexOptions } from "./codexOptions";
22
import { CodexExec } from "./exec";
33
import { Thread } from "./thread";
44

5+
/**
6+
* Codex is the main class for interacting with the Codex agent.
7+
*
8+
* Use the `startThread()` method to start a new thread or `resumeThread()` to resume a previously started thread.
9+
*/
510
export class Codex {
611
private exec: CodexExec;
712
private options: CodexOptions;
@@ -11,10 +16,21 @@ export class Codex {
1116
this.options = options;
1217
}
1318

19+
/**
20+
* Starts a new conversation with an agent.
21+
* @returns A new thread instance.
22+
*/
1423
startThread(): Thread {
1524
return new Thread(this.exec, this.options);
1625
}
1726

27+
/**
28+
* Resumes a conversation with an agent based on the thread id.
29+
* Threads are persisted in ~/.codex/sessions.
30+
*
31+
* @param id The id of the thread to resume.
32+
* @returns A new thread instance.
33+
*/
1834
resumeThread(id: string): Thread {
1935
return new Thread(this.exec, this.options, id);
2036
}

sdk/typescript/src/events.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,73 @@
22

33
import type { ThreadItem } from "./items";
44

5+
/** Emitted when a new thread is started as the first event. */
56
export type ThreadStartedEvent = {
67
type: "thread.started";
8+
/** The identifier of the new thread. Can be used to resume the thread later. */
79
thread_id: string;
810
};
911

12+
/**
13+
* Emitted when a turn is started by sending a new prompt to the model.
14+
* A turn encompasses all events that happen while the agent is processing the prompt.
15+
*/
1016
export type TurnStartedEvent = {
1117
type: "turn.started";
1218
};
1319

20+
/** Describes the usage of tokens during a turn. */
1421
export type Usage = {
22+
/** The number of input tokens used during the turn. */
1523
input_tokens: number;
24+
/** The number of cached input tokens used during the turn. */
1625
cached_input_tokens: number;
26+
/** The number of output tokens used during the turn. */
1727
output_tokens: number;
1828
};
1929

30+
/** Emitted when a turn is completed. Typically right after the assistant's response. */
2031
export type TurnCompletedEvent = {
2132
type: "turn.completed";
2233
usage: Usage;
2334
};
2435

36+
/** Indicates that a turn failed with an error. */
2537
export type TurnFailedEvent = {
2638
type: "turn.failed";
2739
error: ThreadError;
2840
};
2941

42+
/** Emitted when a new item is added to the thread. Typically the item is initially "in progress". */
3043
export type ItemStartedEvent = {
3144
type: "item.started";
3245
item: ThreadItem;
3346
};
3447

48+
/** Emitted when an item is updated. */
3549
export type ItemUpdatedEvent = {
3650
type: "item.updated";
3751
item: ThreadItem;
3852
};
3953

54+
/** Signals that an item has reached a terminal state—either success or failure. */
4055
export type ItemCompletedEvent = {
4156
type: "item.completed";
4257
item: ThreadItem;
4358
};
4459

60+
/** Fatal error emitted by the stream. */
4561
export type ThreadError = {
4662
message: string;
4763
};
4864

65+
/** Represents an unrecoverable error emitted directly by the event stream. */
4966
export type ThreadErrorEvent = {
5067
type: "error";
5168
message: string;
5269
};
5370

71+
/** Top-level JSONL events emitted by codex exec. */
5472
export type ThreadEvent =
5573
| ThreadStartedEvent
5674
| TurnStartedEvent

sdk/typescript/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export type {
2222
ErrorItem,
2323
} from "./items";
2424

25-
export { Thread, RunResult, RunStreamedResult, Input } from "./thread";
25+
export { Thread } from "./thread";
26+
export type { RunResult, RunStreamedResult, Input } from "./thread";
2627

2728
export { Codex } from "./codex";
2829

0 commit comments

Comments
 (0)