@northflare/runner 0.0.16 → 0.0.17

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.
Files changed (79) hide show
  1. package/lib/codex-sdk/dist/src/codex.d.ts +27 -0
  2. package/lib/codex-sdk/dist/src/codex.d.ts.map +1 -0
  3. package/lib/codex-sdk/dist/src/codex.js +33 -0
  4. package/lib/codex-sdk/dist/src/codex.js.map +1 -0
  5. package/lib/codex-sdk/dist/src/codexOptions.d.ts +11 -0
  6. package/lib/codex-sdk/dist/src/codexOptions.d.ts.map +1 -0
  7. package/lib/codex-sdk/dist/src/codexOptions.js +2 -0
  8. package/lib/codex-sdk/dist/src/codexOptions.js.map +1 -0
  9. package/lib/codex-sdk/dist/src/events.d.ts +60 -0
  10. package/lib/codex-sdk/dist/src/events.d.ts.map +1 -0
  11. package/lib/codex-sdk/dist/src/events.js +3 -0
  12. package/lib/codex-sdk/dist/src/events.js.map +1 -0
  13. package/lib/codex-sdk/dist/src/exec.d.ts +27 -0
  14. package/lib/codex-sdk/dist/src/exec.d.ts.map +1 -0
  15. package/lib/codex-sdk/dist/src/exec.js +264 -0
  16. package/lib/codex-sdk/dist/src/exec.js.map +1 -0
  17. package/lib/codex-sdk/dist/src/index.d.ts +9 -0
  18. package/lib/codex-sdk/dist/src/index.d.ts.map +1 -0
  19. package/lib/codex-sdk/dist/src/index.js +3 -0
  20. package/lib/codex-sdk/dist/src/index.js.map +1 -0
  21. package/lib/codex-sdk/dist/src/items.d.ts +103 -0
  22. package/lib/codex-sdk/dist/src/items.d.ts.map +1 -0
  23. package/lib/codex-sdk/dist/src/items.js +3 -0
  24. package/lib/codex-sdk/dist/src/items.js.map +1 -0
  25. package/lib/codex-sdk/dist/src/outputSchemaFile.d.ts +6 -0
  26. package/lib/codex-sdk/dist/src/outputSchemaFile.d.ts.map +1 -0
  27. package/lib/codex-sdk/dist/src/outputSchemaFile.js +33 -0
  28. package/lib/codex-sdk/dist/src/outputSchemaFile.js.map +1 -0
  29. package/lib/codex-sdk/dist/src/thread.d.ts +41 -0
  30. package/lib/codex-sdk/dist/src/thread.d.ts.map +1 -0
  31. package/lib/codex-sdk/dist/src/thread.js +109 -0
  32. package/lib/codex-sdk/dist/src/thread.js.map +1 -0
  33. package/lib/codex-sdk/dist/src/threadOptions.d.ts +16 -0
  34. package/lib/codex-sdk/dist/src/threadOptions.d.ts.map +1 -0
  35. package/lib/codex-sdk/dist/src/threadOptions.js +2 -0
  36. package/lib/codex-sdk/dist/src/threadOptions.js.map +1 -0
  37. package/lib/codex-sdk/dist/src/turnOptions.d.ts +7 -0
  38. package/lib/codex-sdk/dist/src/turnOptions.d.ts.map +1 -0
  39. package/lib/codex-sdk/dist/src/turnOptions.js +2 -0
  40. package/lib/codex-sdk/dist/src/turnOptions.js.map +1 -0
  41. package/lib/codex-sdk/dist/tests/abort.test.d.ts +2 -0
  42. package/lib/codex-sdk/dist/tests/abort.test.d.ts.map +1 -0
  43. package/lib/codex-sdk/dist/tests/abort.test.js +130 -0
  44. package/lib/codex-sdk/dist/tests/abort.test.js.map +1 -0
  45. package/lib/codex-sdk/dist/tests/codexExecSpy.d.ts +6 -0
  46. package/lib/codex-sdk/dist/tests/codexExecSpy.d.ts.map +1 -0
  47. package/lib/codex-sdk/dist/tests/codexExecSpy.js +28 -0
  48. package/lib/codex-sdk/dist/tests/codexExecSpy.js.map +1 -0
  49. package/lib/codex-sdk/dist/tests/responsesProxy.d.ts +56 -0
  50. package/lib/codex-sdk/dist/tests/responsesProxy.d.ts.map +1 -0
  51. package/lib/codex-sdk/dist/tests/responsesProxy.js +155 -0
  52. package/lib/codex-sdk/dist/tests/responsesProxy.js.map +1 -0
  53. package/lib/codex-sdk/dist/tests/run.test.d.ts +2 -0
  54. package/lib/codex-sdk/dist/tests/run.test.d.ts.map +1 -0
  55. package/lib/codex-sdk/dist/tests/run.test.js +524 -0
  56. package/lib/codex-sdk/dist/tests/run.test.js.map +1 -0
  57. package/lib/codex-sdk/dist/tests/runStreamed.test.d.ts +2 -0
  58. package/lib/codex-sdk/dist/tests/runStreamed.test.d.ts.map +1 -0
  59. package/lib/codex-sdk/dist/tests/runStreamed.test.js +153 -0
  60. package/lib/codex-sdk/dist/tests/runStreamed.test.js.map +1 -0
  61. package/lib/codex-sdk/samples/basic_streaming.ts +0 -0
  62. package/lib/codex-sdk/samples/structured_output.ts +0 -0
  63. package/lib/codex-sdk/samples/structured_output_zod.ts +0 -0
  64. package/lib/codex-sdk/src/codex.ts +38 -0
  65. package/lib/codex-sdk/src/codexOptions.ts +10 -0
  66. package/lib/codex-sdk/src/events.ts +80 -0
  67. package/lib/codex-sdk/src/exec.ts +336 -0
  68. package/lib/codex-sdk/src/index.ts +39 -0
  69. package/lib/codex-sdk/src/items.ts +127 -0
  70. package/lib/codex-sdk/src/outputSchemaFile.ts +40 -0
  71. package/lib/codex-sdk/src/thread.ts +155 -0
  72. package/lib/codex-sdk/src/threadOptions.ts +18 -0
  73. package/lib/codex-sdk/src/turnOptions.ts +6 -0
  74. package/lib/codex-sdk/tests/abort.test.ts +165 -0
  75. package/lib/codex-sdk/tests/codexExecSpy.ts +37 -0
  76. package/lib/codex-sdk/tests/responsesProxy.ts +225 -0
  77. package/lib/codex-sdk/tests/run.test.ts +687 -0
  78. package/lib/codex-sdk/tests/runStreamed.test.ts +211 -0
  79. package/package.json +15 -16
@@ -0,0 +1,225 @@
1
+ import http from "node:http";
2
+
3
+ const DEFAULT_RESPONSE_ID = "resp_mock";
4
+ const DEFAULT_MESSAGE_ID = "msg_mock";
5
+
6
+ type ResponseCompletedUsage = {
7
+ input_tokens: number;
8
+ input_tokens_details: { cached_tokens: number } | null;
9
+ output_tokens: number;
10
+ output_tokens_details: { reasoning_tokens: number } | null;
11
+ total_tokens: number;
12
+ };
13
+
14
+ const DEFAULT_COMPLETED_USAGE: ResponseCompletedUsage = {
15
+ input_tokens: 42,
16
+ input_tokens_details: { cached_tokens: 12 },
17
+ output_tokens: 5,
18
+ output_tokens_details: null,
19
+ total_tokens: 47,
20
+ };
21
+
22
+ export type SseEvent = {
23
+ type: string;
24
+ [key: string]: unknown;
25
+ };
26
+
27
+ export type SseResponseBody = {
28
+ kind: "sse";
29
+ events: SseEvent[];
30
+ };
31
+
32
+ export type ResponsesProxyOptions = {
33
+ responseBodies: Generator<SseResponseBody> | SseResponseBody[];
34
+ statusCode?: number;
35
+ };
36
+
37
+ export type ResponsesProxy = {
38
+ url: string;
39
+ close: () => Promise<void>;
40
+ requests: RecordedRequest[];
41
+ };
42
+
43
+ export type ResponsesApiRequest = {
44
+ model?: string;
45
+ input: Array<{
46
+ role: string;
47
+ content?: Array<{ type: string; text: string }>;
48
+ }>;
49
+ text?: {
50
+ format?: Record<string, unknown>;
51
+ };
52
+ };
53
+
54
+ export type RecordedRequest = {
55
+ body: string;
56
+ json: ResponsesApiRequest;
57
+ headers: http.IncomingHttpHeaders;
58
+ };
59
+
60
+ function formatSseEvent(event: SseEvent): string {
61
+ return `event: ${event.type}\n` + `data: ${JSON.stringify(event)}\n\n`;
62
+ }
63
+
64
+ export async function startResponsesTestProxy(
65
+ options: ResponsesProxyOptions,
66
+ ): Promise<ResponsesProxy> {
67
+ function* createGenerator(array: SseResponseBody[]): Generator<SseResponseBody> {
68
+ for (const elem of array) {
69
+ yield elem;
70
+ }
71
+ throw new Error("not enough responses provided");
72
+ }
73
+
74
+ const responseBodies: Generator<SseResponseBody> = Array.isArray(options.responseBodies)
75
+ ? createGenerator(options.responseBodies)
76
+ : options.responseBodies;
77
+
78
+ const requests: RecordedRequest[] = [];
79
+
80
+ function readRequestBody(req: http.IncomingMessage): Promise<string> {
81
+ return new Promise<string>((resolve, reject) => {
82
+ const chunks: Buffer[] = [];
83
+ req.on("data", (chunk) => {
84
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
85
+ });
86
+ req.on("end", () => {
87
+ resolve(Buffer.concat(chunks).toString("utf8"));
88
+ });
89
+ req.on("error", reject);
90
+ });
91
+ }
92
+
93
+ const server = http.createServer((req, res) => {
94
+ async function handle(): Promise<void> {
95
+ if (req.method === "POST" && req.url === "/responses") {
96
+ const body = await readRequestBody(req);
97
+ const json = JSON.parse(body);
98
+ requests.push({ body, json, headers: { ...req.headers } });
99
+
100
+ const status = options.statusCode ?? 200;
101
+ res.statusCode = status;
102
+ res.setHeader("content-type", "text/event-stream");
103
+
104
+ const responseBody = responseBodies.next().value;
105
+ for (const event of responseBody.events) {
106
+ res.write(formatSseEvent(event));
107
+ }
108
+ res.end();
109
+ return;
110
+ }
111
+
112
+ res.statusCode = 404;
113
+ res.end();
114
+ }
115
+
116
+ handle().catch(() => {
117
+ res.statusCode = 500;
118
+ res.end();
119
+ });
120
+ });
121
+
122
+ const url = await new Promise<string>((resolve, reject) => {
123
+ server.listen(0, "127.0.0.1", () => {
124
+ const address = server.address();
125
+ if (!address || typeof address === "string") {
126
+ reject(new Error("Unable to determine proxy address"));
127
+ return;
128
+ }
129
+ server.off("error", reject);
130
+ const info = address;
131
+ resolve(`http://${info.address}:${info.port}`);
132
+ });
133
+ server.once("error", reject);
134
+ });
135
+
136
+ async function close(): Promise<void> {
137
+ await new Promise<void>((resolve, reject) => {
138
+ server.close((err) => {
139
+ if (err) {
140
+ reject(err);
141
+ return;
142
+ }
143
+ resolve();
144
+ });
145
+ });
146
+ }
147
+ return { url, close, requests };
148
+ }
149
+
150
+ export function sse(...events: SseEvent[]): SseResponseBody {
151
+ return {
152
+ kind: "sse",
153
+ events,
154
+ };
155
+ }
156
+
157
+ export function responseStarted(responseId: string = DEFAULT_RESPONSE_ID): SseEvent {
158
+ return {
159
+ type: "response.created",
160
+ response: {
161
+ id: responseId,
162
+ },
163
+ };
164
+ }
165
+
166
+ export function assistantMessage(text: string, itemId: string = DEFAULT_MESSAGE_ID): SseEvent {
167
+ return {
168
+ type: "response.output_item.done",
169
+ item: {
170
+ type: "message",
171
+ role: "assistant",
172
+ id: itemId,
173
+ content: [
174
+ {
175
+ type: "output_text",
176
+ text,
177
+ },
178
+ ],
179
+ },
180
+ };
181
+ }
182
+
183
+ export function shell_call(): SseEvent {
184
+ const command = ["bash", "-lc", "echo 'Hello, world!'"];
185
+ return {
186
+ type: "response.output_item.done",
187
+ item: {
188
+ type: "function_call",
189
+ call_id: `call_id${Math.random().toString(36).slice(2)}`,
190
+ name: "shell",
191
+ arguments: JSON.stringify({
192
+ command,
193
+ timeout_ms: 100,
194
+ }),
195
+ },
196
+ };
197
+ }
198
+
199
+ export function responseFailed(errorMessage: string): SseEvent {
200
+ return {
201
+ type: "error",
202
+ error: { code: "rate_limit_exceeded", message: errorMessage },
203
+ };
204
+ }
205
+
206
+ export function responseCompleted(
207
+ responseId: string = DEFAULT_RESPONSE_ID,
208
+ usage: ResponseCompletedUsage = DEFAULT_COMPLETED_USAGE,
209
+ ): SseEvent {
210
+ const inputDetails = usage.input_tokens_details ? { ...usage.input_tokens_details } : null;
211
+ const outputDetails = usage.output_tokens_details ? { ...usage.output_tokens_details } : null;
212
+ return {
213
+ type: "response.completed",
214
+ response: {
215
+ id: responseId,
216
+ usage: {
217
+ input_tokens: usage.input_tokens,
218
+ input_tokens_details: inputDetails,
219
+ output_tokens: usage.output_tokens,
220
+ output_tokens_details: outputDetails,
221
+ total_tokens: usage.total_tokens,
222
+ },
223
+ },
224
+ };
225
+ }