Skip to content

Commit 347a227

Browse files
committed
v4.0.0-beta.7
1 parent 96e5b7f commit 347a227

File tree

20 files changed

+353
-203
lines changed

20 files changed

+353
-203
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import OpenAI from 'openai';
5050
const openai = new OpenAI();
5151

5252
async function main() {
53-
const completion = await openai.chat.completions.create({
53+
const stream = await openai.chat.completions.create({
5454
model: 'gpt-4',
5555
messages: [{ role: 'user', content: 'Say this is a test' }],
5656
stream: true,

api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Types:
1717

1818
- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
1919
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
20+
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>
2021

2122
Methods:
2223

ecosystem-tests/cli.ts

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ function parseArgs() {
125125
default: false,
126126
description: 'Push projects to live servers',
127127
},
128+
jobs: {
129+
type: 'number',
130+
default: 1,
131+
description: 'number of parallel jobs to run',
132+
},
133+
parallel: {
134+
type: 'boolean',
135+
default: false,
136+
description: 'run all projects in parallel (jobs = # projects)',
137+
},
128138
})
129139
.help().argv;
130140
}
@@ -157,26 +167,97 @@ async function main() {
157167

158168
const failed: typeof projectNames = [];
159169

160-
for (const project of projectsToRun) {
161-
const fn = projects[project];
162-
163-
await withChdir(path.join(rootDir, 'ecosystem-tests', project), async () => {
164-
console.error('\n');
165-
console.error(banner(project));
166-
console.error('\n');
167-
168-
try {
169-
await fn();
170-
console.error(`✅ - Successfully ran ${project}`);
171-
} catch (err) {
172-
if (err && (err as any).shortMessage) {
173-
console.error((err as any).shortMessage);
174-
} else {
175-
console.error(err);
176-
}
177-
failed.push(project);
170+
let { jobs } = args;
171+
if (args.parallel) jobs = projectsToRun.length;
172+
if (jobs > 1) {
173+
const queue = [...projectsToRun];
174+
const runningProjects = new Set();
175+
176+
const cursorLeft = '\x1B[G';
177+
const eraseLine = '\x1B[2K';
178+
179+
let progressDisplayed = false;
180+
function clearProgress() {
181+
if (progressDisplayed) {
182+
process.stderr.write(cursorLeft + eraseLine);
183+
progressDisplayed = false;
178184
}
179-
});
185+
}
186+
const spinner = ['|', '/', '-', '\\'];
187+
188+
function showProgress() {
189+
clearProgress();
190+
progressDisplayed = true;
191+
const spin = spinner[Math.floor(Date.now() / 500) % spinner.length];
192+
process.stderr.write(
193+
`${spin} Running ${[...runningProjects].join(', ')}`.substring(0, process.stdout.columns - 3) + '...',
194+
);
195+
}
196+
197+
const progressInterval = setInterval(showProgress, process.stdout.isTTY ? 500 : 5000);
198+
showProgress();
199+
200+
await Promise.all(
201+
[...Array(jobs).keys()].map(async () => {
202+
while (queue.length) {
203+
const project = queue.shift();
204+
if (!project) break;
205+
let stdout, stderr;
206+
try {
207+
runningProjects.add(project);
208+
const result = await execa(
209+
'yarn',
210+
[
211+
'tsn',
212+
__filename,
213+
project,
214+
'--skip-pack',
215+
...(args.live ? ['--live'] : []),
216+
...(args.verbose ? ['--verbose'] : []),
217+
...(args.deploy ? ['--deploy'] : []),
218+
...(args.fromNpm ? ['--from-npm'] : []),
219+
],
220+
{ stdio: 'pipe', encoding: 'utf8', maxBuffer: 100 * 1024 * 1024 },
221+
);
222+
({ stdout, stderr } = result);
223+
} catch (error) {
224+
({ stdout, stderr } = error as any);
225+
failed.push(project);
226+
} finally {
227+
runningProjects.delete(project);
228+
}
229+
230+
if (stdout) process.stdout.write(stdout);
231+
if (stderr) process.stderr.write(stderr);
232+
}
233+
}),
234+
);
235+
236+
clearInterval(progressInterval);
237+
clearProgress();
238+
} else {
239+
for (const project of projectsToRun) {
240+
const fn = projects[project];
241+
242+
await withChdir(path.join(rootDir, 'ecosystem-tests', project), async () => {
243+
console.error('\n');
244+
console.error(banner(project));
245+
console.error('\n');
246+
247+
try {
248+
await fn();
249+
console.error(`✅ - Successfully ran ${project}`);
250+
} catch (err) {
251+
if (err && (err as any).shortMessage) {
252+
console.error('❌', (err as any).shortMessage);
253+
} else {
254+
console.error('❌', err);
255+
}
256+
failed.push(project);
257+
}
258+
console.error('\n');
259+
});
260+
}
180261
}
181262

182263
if (failed.length) {
Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import OpenAI from 'openai';
2-
import { uploadWebApiTestCases } from './uploadWebApiTestCases.js';
3-
import { distance } from 'fastest-levenshtein'
1+
import { distance } from 'fastest-levenshtein';
42

53
/**
64
* Welcome to Cloudflare Workers! This is your first worker.
@@ -35,54 +33,68 @@ type Test = { description: string; handler: () => Promise<void> };
3533

3634
export default {
3735
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
38-
const client = new OpenAI({ apiKey: env.OPENAI_API_KEY });
36+
try {
37+
console.error('importing openai');
38+
const { default: OpenAI } = await import('openai');
39+
console.error('importing test cases');
40+
const { uploadWebApiTestCases } = await import('./uploadWebApiTestCases.js');
41+
console.error('creating client');
42+
const client = new OpenAI({ apiKey: env.OPENAI_API_KEY });
43+
console.error('created client');
3944

40-
const tests: Test[] = [];
41-
function it(description: string, handler: () => Promise<void>) {
42-
tests.push({ description, handler });
43-
}
44-
function expectEqual(a: any, b: any) {
45-
if (!Object.is(a, b)) {
46-
throw new Error(`expected values to be equal: ${JSON.stringify({ a, b })}`);
45+
const tests: Test[] = [];
46+
function it(description: string, handler: () => Promise<void>) {
47+
tests.push({ description, handler });
4748
}
48-
}
49-
function expectSimilar(received: string, expected: string, maxDistance: number) {
50-
const receivedDistance = distance(received, expected);
51-
if (receivedDistance < maxDistance) {
52-
return;
49+
function expectEqual(a: any, b: any) {
50+
if (!Object.is(a, b)) {
51+
throw new Error(`expected values to be equal: ${JSON.stringify({ a, b })}`);
52+
}
5353
}
54+
function expectSimilar(received: string, expected: string, maxDistance: number) {
55+
const receivedDistance = distance(received, expected);
56+
if (receivedDistance < maxDistance) {
57+
return;
58+
}
5459

55-
const message = [
56-
`Received: ${JSON.stringify(received)}`,
57-
`Expected: ${JSON.stringify(expected)}`,
58-
`Max distance: ${maxDistance}`,
59-
`Received distance: ${receivedDistance}`,
60-
].join('\n');
60+
const message = [
61+
`Received: ${JSON.stringify(received)}`,
62+
`Expected: ${JSON.stringify(expected)}`,
63+
`Max distance: ${maxDistance}`,
64+
`Received distance: ${receivedDistance}`,
65+
].join('\n');
6166

62-
throw new Error(message);
63-
}
67+
throw new Error(message);
68+
}
6469

65-
uploadWebApiTestCases({
66-
client: client as any,
67-
it,
68-
expectEqual,
69-
expectSimilar,
70-
});
70+
uploadWebApiTestCases({
71+
client: client as any,
72+
it,
73+
expectEqual,
74+
expectSimilar,
75+
});
7176

72-
let allPassed = true;
73-
const results = [];
77+
let allPassed = true;
78+
const results = [];
7479

75-
for (const { description, handler } of tests) {
76-
let result;
77-
try {
78-
result = await handler();
79-
} catch (error) {
80-
allPassed = false;
81-
result = error instanceof Error ? error.stack : String(error);
80+
for (const { description, handler } of tests) {
81+
console.error('running', description);
82+
let result;
83+
try {
84+
result = await handler();
85+
console.error('passed ', description);
86+
} catch (error) {
87+
console.error('failed ', description, error);
88+
allPassed = false;
89+
result = error instanceof Error ? error.stack : String(error);
90+
}
91+
results.push(`${description}\n\n${String(result)}`);
8292
}
83-
results.push(`${description}\n\n${String(result)}`);
84-
}
8593

86-
return new Response(allPassed ? 'Passed!' : results.join('\n\n'));
94+
return new Response(allPassed ? 'Passed!' : results.join('\n\n'));
95+
} catch (error) {
96+
console.error(error instanceof Error ? error.stack : String(error));
97+
return new Response(error instanceof Error ? error.stack : String(error), { status: 500 });
98+
}
8799
},
88100
};
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import fetch from 'node-fetch';
22

3-
it('works', async () => {
4-
expect(await (await fetch('http://localhost:8787')).text()).toEqual('Passed!');
5-
}, 30000);
3+
it(
4+
'works',
5+
async () => {
6+
expect(await (await fetch('http://localhost:8787')).text()).toEqual('Passed!');
7+
},
8+
3 * 60000
9+
);

ecosystem-tests/deno/main_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals, AssertionError } from 'https://deno.land/[email protected]/testing/asserts.ts';
22
import OpenAI, { toFile } from 'npm:[email protected]';
3-
import { distance } from 'https://deno.land/x/fastest_levenshtein/mod.ts'
3+
import { distance } from 'https://deno.land/x/fastest_levenshtein/mod.ts';
44

55
const url = 'https://audio-samples.github.io/samples/mp3/blizzard_biased/sample-1.mp3';
66
const filename = 'sample-1.mp3';

ecosystem-tests/node-ts-cjs-dom/jest.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ module.exports = {
55
testMatch: ['<rootDir>/tests/*.ts'],
66
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
77
verbose: false,
8-
testTimeout: 15000,
8+
testTimeout: 60000,
99
};

ecosystem-tests/node-ts-cjs/jest.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ module.exports = {
55
testMatch: ['<rootDir>/tests/*.ts'],
66
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
77
verbose: false,
8-
testTimeout: 15000,
8+
testTimeout: 60000,
99
};

ecosystem-tests/node-ts-esm-dom/jest.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ module.exports = {
1919
testMatch: ['<rootDir>/tests/*.ts'],
2020
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
2121
verbose: false,
22-
testTimeout: 15000,
22+
testTimeout: 60000,
2323
};

ecosystem-tests/node-ts-esm/jest.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ module.exports = {
1919
testMatch: ['<rootDir>/tests/*.ts'],
2020
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
2121
verbose: false,
22-
testTimeout: 15000,
22+
testTimeout: 60000,
2323
};

0 commit comments

Comments
 (0)