@northflare/runner 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/claude-sdk-manager.d.ts +1 -1
- package/dist/components/claude-sdk-manager.d.ts.map +1 -1
- package/dist/components/claude-sdk-manager.js +25 -12
- package/dist/components/claude-sdk-manager.js.map +1 -1
- package/dist/components/codex-sdk-manager.d.ts +58 -0
- package/dist/components/codex-sdk-manager.d.ts.map +1 -0
- package/dist/components/codex-sdk-manager.js +907 -0
- package/dist/components/codex-sdk-manager.js.map +1 -0
- package/dist/components/message-handler-sse.d.ts +3 -0
- package/dist/components/message-handler-sse.d.ts.map +1 -1
- package/dist/components/message-handler-sse.js +51 -7
- package/dist/components/message-handler-sse.js.map +1 -1
- package/dist/runner-sse.d.ts +4 -0
- package/dist/runner-sse.d.ts.map +1 -1
- package/dist/runner-sse.js +145 -15
- package/dist/runner-sse.js.map +1 -1
- package/dist/types/claude.d.ts +11 -1
- package/dist/types/claude.d.ts.map +1 -1
- package/dist/types/runner-interface.d.ts +2 -0
- package/dist/types/runner-interface.d.ts.map +1 -1
- package/dist/utils/model.d.ts +6 -0
- package/dist/utils/model.d.ts.map +1 -0
- package/dist/utils/model.js +23 -0
- package/dist/utils/model.js.map +1 -0
- package/dist/utils/status-line.d.ts +2 -2
- package/dist/utils/status-line.js +5 -5
- package/dist/utils/status-line.js.map +1 -1
- package/lib/codex-sdk/.prettierignore +3 -0
- package/lib/codex-sdk/.prettierrc +5 -0
- package/lib/codex-sdk/README.md +133 -0
- package/lib/codex-sdk/dist/index.d.ts +260 -0
- package/lib/codex-sdk/dist/index.js +426 -0
- package/lib/codex-sdk/eslint.config.js +21 -0
- package/lib/codex-sdk/jest.config.cjs +31 -0
- package/lib/codex-sdk/package.json +65 -0
- package/lib/codex-sdk/samples/basic_streaming.ts +90 -0
- package/lib/codex-sdk/samples/helpers.ts +8 -0
- package/lib/codex-sdk/samples/structured_output.ts +22 -0
- package/lib/codex-sdk/samples/structured_output_zod.ts +19 -0
- package/lib/codex-sdk/src/codex.ts +38 -0
- package/lib/codex-sdk/src/codexOptions.ts +10 -0
- package/lib/codex-sdk/src/events.ts +80 -0
- package/lib/codex-sdk/src/exec.ts +336 -0
- package/lib/codex-sdk/src/index.ts +39 -0
- package/lib/codex-sdk/src/items.ts +127 -0
- package/lib/codex-sdk/src/outputSchemaFile.ts +40 -0
- package/lib/codex-sdk/src/thread.ts +155 -0
- package/lib/codex-sdk/src/threadOptions.ts +18 -0
- package/lib/codex-sdk/src/turnOptions.ts +6 -0
- package/lib/codex-sdk/tests/abort.test.ts +165 -0
- package/lib/codex-sdk/tests/codexExecSpy.ts +37 -0
- package/lib/codex-sdk/tests/responsesProxy.ts +225 -0
- package/lib/codex-sdk/tests/run.test.ts +687 -0
- package/lib/codex-sdk/tests/runStreamed.test.ts +211 -0
- package/lib/codex-sdk/tsconfig.json +24 -0
- package/lib/codex-sdk/tsup.config.ts +12 -0
- package/package.json +3 -1
- package/rejections.log +2 -0
- package/src/components/claude-sdk-manager.ts +33 -13
- package/src/components/codex-sdk-manager.ts +1248 -0
- package/src/components/message-handler-sse.ts +79 -8
- package/src/runner-sse.ts +174 -15
- package/src/types/claude.ts +12 -1
- package/src/types/runner-interface.ts +3 -1
- package/src/utils/model.ts +29 -0
- package/src/utils/status-line.ts +6 -6
- package/src/utils/codex-sdk.js +0 -448
- package/src/utils/sdk-demo.js +0 -34
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** @type {import('jest').Config} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: "ts-jest/presets/default-esm",
|
|
4
|
+
testEnvironment: "node",
|
|
5
|
+
extensionsToTreatAsEsm: [".ts"],
|
|
6
|
+
moduleNameMapper: {
|
|
7
|
+
"^(\\.{1,2}/.*)\\.js$": "$1",
|
|
8
|
+
},
|
|
9
|
+
testMatch: ["**/tests/**/*.test.ts"],
|
|
10
|
+
transform: {
|
|
11
|
+
"^.+\\.tsx?$": [
|
|
12
|
+
"ts-jest",
|
|
13
|
+
{
|
|
14
|
+
useESM: true,
|
|
15
|
+
tsconfig: "tsconfig.json",
|
|
16
|
+
diagnostics: {
|
|
17
|
+
ignoreCodes: [1343],
|
|
18
|
+
},
|
|
19
|
+
astTransformers: {
|
|
20
|
+
before: [
|
|
21
|
+
{
|
|
22
|
+
path: "ts-jest-mock-import-meta",
|
|
23
|
+
// Workaround for meta.url not working in jest
|
|
24
|
+
options: { metaObjectReplacement: { url: "file://" + __dirname + "/dist/index.js" } },
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@northflare/codex-sdk",
|
|
3
|
+
"version": "0.0.0-dev",
|
|
4
|
+
"description": "TypeScript SDK for Codex APIs.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/openai/codex.git",
|
|
8
|
+
"directory": "sdk/typescript"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"openai",
|
|
12
|
+
"codex",
|
|
13
|
+
"sdk",
|
|
14
|
+
"typescript",
|
|
15
|
+
"api"
|
|
16
|
+
],
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"scripts": {
|
|
35
|
+
"clean": "rm -rf dist",
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"build:watch": "tsup --watch",
|
|
38
|
+
"lint": "pnpm eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
39
|
+
"lint:fix": "pnpm eslint --fix \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
40
|
+
"test": "jest",
|
|
41
|
+
"test:watch": "jest --watch",
|
|
42
|
+
"coverage": "jest --coverage",
|
|
43
|
+
"format": "prettier --check .",
|
|
44
|
+
"format:fix": "prettier --write ."
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@modelcontextprotocol/sdk": "^1.20.2",
|
|
48
|
+
"@types/jest": "^29.5.14",
|
|
49
|
+
"@types/node": "^20.19.18",
|
|
50
|
+
"eslint": "^9.36.0",
|
|
51
|
+
"eslint-config-prettier": "^9.1.2",
|
|
52
|
+
"eslint-plugin-jest": "^29.0.1",
|
|
53
|
+
"eslint-plugin-node-import": "^1.0.5",
|
|
54
|
+
"jest": "^29.7.0",
|
|
55
|
+
"prettier": "^3.6.2",
|
|
56
|
+
"ts-jest": "^29.3.4",
|
|
57
|
+
"ts-jest-mock-import-meta": "^1.3.1",
|
|
58
|
+
"ts-node": "^10.9.2",
|
|
59
|
+
"tsup": "^8.5.0",
|
|
60
|
+
"typescript": "^5.9.2",
|
|
61
|
+
"typescript-eslint": "^8.45.0",
|
|
62
|
+
"zod": "^3.24.2",
|
|
63
|
+
"zod-to-json-schema": "^3.24.6"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env -S NODE_NO_WARNINGS=1 pnpm ts-node-esm --files
|
|
2
|
+
|
|
3
|
+
import { createInterface } from "node:readline/promises";
|
|
4
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
5
|
+
|
|
6
|
+
import { Codex } from "@openai/codex-sdk";
|
|
7
|
+
import type { ThreadEvent, ThreadItem } from "@openai/codex-sdk";
|
|
8
|
+
import { codexPathOverride } from "./helpers.ts";
|
|
9
|
+
|
|
10
|
+
const codex = new Codex({ codexPathOverride: codexPathOverride() });
|
|
11
|
+
const thread = codex.startThread();
|
|
12
|
+
const rl = createInterface({ input, output });
|
|
13
|
+
|
|
14
|
+
const handleItemCompleted = (item: ThreadItem): void => {
|
|
15
|
+
switch (item.type) {
|
|
16
|
+
case "agent_message":
|
|
17
|
+
console.log(`Assistant: ${item.text}`);
|
|
18
|
+
break;
|
|
19
|
+
case "reasoning":
|
|
20
|
+
console.log(`Reasoning: ${item.text}`);
|
|
21
|
+
break;
|
|
22
|
+
case "command_execution": {
|
|
23
|
+
const exitText = item.exit_code !== undefined ? ` Exit code ${item.exit_code}.` : "";
|
|
24
|
+
console.log(`Command ${item.command} ${item.status}.${exitText}`);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case "file_change": {
|
|
28
|
+
for (const change of item.changes) {
|
|
29
|
+
console.log(`File ${change.kind} ${change.path}`);
|
|
30
|
+
}
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const handleItemUpdated = (item: ThreadItem): void => {
|
|
37
|
+
switch (item.type) {
|
|
38
|
+
case "todo_list": {
|
|
39
|
+
console.log(`Todo:`);
|
|
40
|
+
for (const todo of item.items) {
|
|
41
|
+
console.log(`\t ${todo.completed ? "x" : " "} ${todo.text}`);
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const handleEvent = (event: ThreadEvent): void => {
|
|
49
|
+
switch (event.type) {
|
|
50
|
+
case "item.completed":
|
|
51
|
+
handleItemCompleted(event.item);
|
|
52
|
+
break;
|
|
53
|
+
case "item.updated":
|
|
54
|
+
case "item.started":
|
|
55
|
+
handleItemUpdated(event.item);
|
|
56
|
+
break;
|
|
57
|
+
case "turn.completed":
|
|
58
|
+
console.log(
|
|
59
|
+
`Used ${event.usage.input_tokens} input tokens, ${event.usage.cached_input_tokens} cached input tokens, ${event.usage.output_tokens} output tokens.`,
|
|
60
|
+
);
|
|
61
|
+
break;
|
|
62
|
+
case "turn.failed":
|
|
63
|
+
console.error(`Turn failed: ${event.error.message}`);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const main = async (): Promise<void> => {
|
|
69
|
+
try {
|
|
70
|
+
while (true) {
|
|
71
|
+
const inputText = await rl.question(">");
|
|
72
|
+
const trimmed = inputText.trim();
|
|
73
|
+
if (trimmed.length === 0) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const { events } = await thread.runStreamed(inputText);
|
|
77
|
+
for await (const event of events) {
|
|
78
|
+
handleEvent(event);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} finally {
|
|
82
|
+
rl.close();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
main().catch((err) => {
|
|
87
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
88
|
+
console.error(`Unexpected error: ${message}`);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env -S NODE_NO_WARNINGS=1 pnpm ts-node-esm --files
|
|
2
|
+
|
|
3
|
+
import { Codex } from "@openai/codex-sdk";
|
|
4
|
+
|
|
5
|
+
import { codexPathOverride } from "./helpers.ts";
|
|
6
|
+
|
|
7
|
+
const codex = new Codex({ codexPathOverride: codexPathOverride() });
|
|
8
|
+
|
|
9
|
+
const thread = codex.startThread();
|
|
10
|
+
|
|
11
|
+
const schema = {
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: {
|
|
14
|
+
summary: { type: "string" },
|
|
15
|
+
status: { type: "string", enum: ["ok", "action_required"] },
|
|
16
|
+
},
|
|
17
|
+
required: ["summary", "status"],
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
const turn = await thread.run("Summarize repository status", { outputSchema: schema });
|
|
22
|
+
console.log(turn.finalResponse);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env -S NODE_NO_WARNINGS=1 pnpm ts-node-esm --files
|
|
2
|
+
|
|
3
|
+
import { Codex } from "@openai/codex-sdk";
|
|
4
|
+
import { codexPathOverride } from "./helpers.ts";
|
|
5
|
+
import z from "zod";
|
|
6
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
|
7
|
+
|
|
8
|
+
const codex = new Codex({ codexPathOverride: codexPathOverride() });
|
|
9
|
+
const thread = codex.startThread();
|
|
10
|
+
|
|
11
|
+
const schema = z.object({
|
|
12
|
+
summary: z.string(),
|
|
13
|
+
status: z.enum(["ok", "action_required"]),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const turn = await thread.run("Summarize repository status", {
|
|
17
|
+
outputSchema: zodToJsonSchema(schema, { target: "openAi" }),
|
|
18
|
+
});
|
|
19
|
+
console.log(turn.finalResponse);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CodexOptions } from "./codexOptions";
|
|
2
|
+
import { CodexExec } from "./exec";
|
|
3
|
+
import { Thread } from "./thread";
|
|
4
|
+
import { ThreadOptions } from "./threadOptions";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Codex is the main class for interacting with the Codex agent.
|
|
8
|
+
*
|
|
9
|
+
* Use the `startThread()` method to start a new thread or `resumeThread()` to resume a previously started thread.
|
|
10
|
+
*/
|
|
11
|
+
export class Codex {
|
|
12
|
+
private exec: CodexExec;
|
|
13
|
+
private options: CodexOptions;
|
|
14
|
+
|
|
15
|
+
constructor(options: CodexOptions = {}) {
|
|
16
|
+
this.exec = new CodexExec(options.codexPathOverride, options.env);
|
|
17
|
+
this.options = options;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Starts a new conversation with an agent.
|
|
22
|
+
* @returns A new thread instance.
|
|
23
|
+
*/
|
|
24
|
+
startThread(options: ThreadOptions = {}): Thread {
|
|
25
|
+
return new Thread(this.exec, this.options, options);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Resumes a conversation with an agent based on the thread id.
|
|
30
|
+
* Threads are persisted in ~/.codex/sessions.
|
|
31
|
+
*
|
|
32
|
+
* @param id The id of the thread to resume.
|
|
33
|
+
* @returns A new thread instance.
|
|
34
|
+
*/
|
|
35
|
+
resumeThread(id: string, options: ThreadOptions = {}): Thread {
|
|
36
|
+
return new Thread(this.exec, this.options, options, id);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type CodexOptions = {
|
|
2
|
+
codexPathOverride?: string;
|
|
3
|
+
baseUrl?: string;
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
/**
|
|
6
|
+
* Environment variables passed to the Codex CLI process. When provided, the SDK
|
|
7
|
+
* will not inherit variables from `process.env`.
|
|
8
|
+
*/
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// based on event types from codex-rs/exec/src/exec_events.rs
|
|
2
|
+
|
|
3
|
+
import type { ThreadItem } from "./items";
|
|
4
|
+
|
|
5
|
+
/** Emitted when a new thread is started as the first event. */
|
|
6
|
+
export type ThreadStartedEvent = {
|
|
7
|
+
type: "thread.started";
|
|
8
|
+
/** The identifier of the new thread. Can be used to resume the thread later. */
|
|
9
|
+
thread_id: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
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
|
+
*/
|
|
16
|
+
export type TurnStartedEvent = {
|
|
17
|
+
type: "turn.started";
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Describes the usage of tokens during a turn. */
|
|
21
|
+
export type Usage = {
|
|
22
|
+
/** The number of input tokens used during the turn. */
|
|
23
|
+
input_tokens: number;
|
|
24
|
+
/** The number of cached input tokens used during the turn. */
|
|
25
|
+
cached_input_tokens: number;
|
|
26
|
+
/** The number of output tokens used during the turn. */
|
|
27
|
+
output_tokens: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Emitted when a turn is completed. Typically right after the assistant's response. */
|
|
31
|
+
export type TurnCompletedEvent = {
|
|
32
|
+
type: "turn.completed";
|
|
33
|
+
usage: Usage;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Indicates that a turn failed with an error. */
|
|
37
|
+
export type TurnFailedEvent = {
|
|
38
|
+
type: "turn.failed";
|
|
39
|
+
error: ThreadError;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Emitted when a new item is added to the thread. Typically the item is initially "in progress". */
|
|
43
|
+
export type ItemStartedEvent = {
|
|
44
|
+
type: "item.started";
|
|
45
|
+
item: ThreadItem;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Emitted when an item is updated. */
|
|
49
|
+
export type ItemUpdatedEvent = {
|
|
50
|
+
type: "item.updated";
|
|
51
|
+
item: ThreadItem;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Signals that an item has reached a terminal state—either success or failure. */
|
|
55
|
+
export type ItemCompletedEvent = {
|
|
56
|
+
type: "item.completed";
|
|
57
|
+
item: ThreadItem;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Fatal error emitted by the stream. */
|
|
61
|
+
export type ThreadError = {
|
|
62
|
+
message: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** Represents an unrecoverable error emitted directly by the event stream. */
|
|
66
|
+
export type ThreadErrorEvent = {
|
|
67
|
+
type: "error";
|
|
68
|
+
message: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Top-level JSONL events emitted by codex exec. */
|
|
72
|
+
export type ThreadEvent =
|
|
73
|
+
| ThreadStartedEvent
|
|
74
|
+
| TurnStartedEvent
|
|
75
|
+
| TurnCompletedEvent
|
|
76
|
+
| TurnFailedEvent
|
|
77
|
+
| ItemStartedEvent
|
|
78
|
+
| ItemUpdatedEvent
|
|
79
|
+
| ItemCompletedEvent
|
|
80
|
+
| ThreadErrorEvent;
|