From 66248422c371f7b5613feb98e09b783acd4e77a8 Mon Sep 17 00:00:00 2001 From: vrtnis <123119434+vrtnis@users.noreply.github.com> Date: Tue, 5 Aug 2025 15:07:38 -0700 Subject: [PATCH 1/4] build: restore ESM/CJS configs, shims, and integration test setup --- eslint.config.mjs | 1 + integration-tests/esm-import.test.ts | 20 ++++++ integration-tests/esm-import/.npmrc | 2 + integration-tests/esm-import/package.json | 7 ++ integration-tests/lambda-local.test.ts | 20 ++++++ integration-tests/lambda-local/.npmrc | 2 + integration-tests/lambda-local/event.json | 1 + integration-tests/lambda-local/index.cjs | 10 +++ integration-tests/lambda-local/package.json | 7 ++ package.json | 4 +- packages/agents-core/package.json | 64 +++++-------------- packages/agents-core/src/shims/shims-node.ts | 12 ++-- .../agents-core/src/shims/shims-workerd.ts | 12 ++-- packages/agents-core/tsconfig.cjs.json | 8 +++ packages/agents-extensions/package.json | 2 +- packages/agents-openai/package.json | 15 ++--- packages/agents-openai/tsconfig.cjs.json | 8 +++ packages/agents-realtime/package.json | 58 +++++------------ packages/agents-realtime/tsconfig.cjs.json | 8 +++ packages/agents/package.json | 36 ++++++----- packages/agents/src/shims/shims-node.ts | 1 + packages/agents/tsconfig.cjs.json | 8 +++ tsc-multi.cjs.json | 11 ++++ 23 files changed, 192 insertions(+), 125 deletions(-) create mode 100644 integration-tests/esm-import.test.ts create mode 100644 integration-tests/esm-import/.npmrc create mode 100644 integration-tests/esm-import/package.json create mode 100644 integration-tests/lambda-local.test.ts create mode 100644 integration-tests/lambda-local/.npmrc create mode 100644 integration-tests/lambda-local/event.json create mode 100644 integration-tests/lambda-local/index.cjs create mode 100644 integration-tests/lambda-local/package.json create mode 100644 packages/agents-core/tsconfig.cjs.json create mode 100644 packages/agents-openai/tsconfig.cjs.json create mode 100644 packages/agents-realtime/tsconfig.cjs.json create mode 100644 packages/agents/src/shims/shims-node.ts create mode 100644 packages/agents/tsconfig.cjs.json create mode 100644 tsc-multi.cjs.json diff --git a/eslint.config.mjs b/eslint.config.mjs index 151a3817..c482defa 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -18,6 +18,7 @@ import { globalIgnores } from 'eslint/config'; export default tseslint.config( globalIgnores([ '**/dist/**', + '**/dist-cjs/**', '**/node_modules/**', '**/docs/.astro/**', 'examples/realtime-next/**', diff --git a/integration-tests/esm-import.test.ts b/integration-tests/esm-import.test.ts new file mode 100644 index 00000000..3ec0e29d --- /dev/null +++ b/integration-tests/esm-import.test.ts @@ -0,0 +1,20 @@ +import { describe, test, beforeAll, expect } from 'vitest'; +import { execa as execaBase } from 'execa'; + +const execa = execaBase({ cwd: './integration-tests/esm-import' }); + +describe('ESM import', () => { + beforeAll(async () => { + // Remove existing modules to ensure a clean install. + console.log('[esm-import] Removing node_modules'); + await execa`rm -rf node_modules`; + console.log('[esm-import] Installing dependencies'); + await execa`npm install`; + }, 60000); + + test('should import @openai/agents under Node 22', async () => { + const { exitCode } = + await execa`npx --yes node@22 -e "import('@openai/agents');"`; + expect(exitCode).toBe(0); + }); +}); diff --git a/integration-tests/esm-import/.npmrc b/integration-tests/esm-import/.npmrc new file mode 100644 index 00000000..c596bb38 --- /dev/null +++ b/integration-tests/esm-import/.npmrc @@ -0,0 +1,2 @@ +@openai:registry=http://localhost:4873 +package-lock=false diff --git a/integration-tests/esm-import/package.json b/integration-tests/esm-import/package.json new file mode 100644 index 00000000..3a8997ed --- /dev/null +++ b/integration-tests/esm-import/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "type": "module", + "dependencies": { + "@openai/agents": "latest" + } +} diff --git a/integration-tests/lambda-local.test.ts b/integration-tests/lambda-local.test.ts new file mode 100644 index 00000000..172902d5 --- /dev/null +++ b/integration-tests/lambda-local.test.ts @@ -0,0 +1,20 @@ +import { describe, test, expect, beforeAll } from 'vitest'; +import { execa as execaBase } from 'execa'; + +const execa = execaBase({ cwd: './integration-tests/lambda-local' }); + +describe('lambda-local', () => { + beforeAll(async () => { + // Remove existing modules to ensure a clean install. + console.log('[lambda-local] Removing node_modules'); + await execa`rm -rf node_modules`; + console.log('[lambda-local] Installing dependencies'); + await execa`npm install`; + }, 60000); + + test('should return a 200 response', async () => { + const { stdout } = + await execa`npx --yes lambda-local -l index.cjs -h handler -e event.json`; + expect(stdout).toContain('"statusCode": 200'); + }); +}); diff --git a/integration-tests/lambda-local/.npmrc b/integration-tests/lambda-local/.npmrc new file mode 100644 index 00000000..c596bb38 --- /dev/null +++ b/integration-tests/lambda-local/.npmrc @@ -0,0 +1,2 @@ +@openai:registry=http://localhost:4873 +package-lock=false diff --git a/integration-tests/lambda-local/event.json b/integration-tests/lambda-local/event.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/integration-tests/lambda-local/event.json @@ -0,0 +1 @@ +{} diff --git a/integration-tests/lambda-local/index.cjs b/integration-tests/lambda-local/index.cjs new file mode 100644 index 00000000..0f8d3e39 --- /dev/null +++ b/integration-tests/lambda-local/index.cjs @@ -0,0 +1,10 @@ +const { Agent } = require('@openai/agents'); + +exports.handler = async () => { + // Instantiate the agent to ensure the import succeeds. + new Agent({ name: 'Test Agent', instructions: 'Say hello.' }); + return { + statusCode: 200, + body: 'ok', + }; +}; diff --git a/integration-tests/lambda-local/package.json b/integration-tests/lambda-local/package.json new file mode 100644 index 00000000..4ddb10fa --- /dev/null +++ b/integration-tests/lambda-local/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "@openai/agents": "latest", + "lambda-local": "^2.2.0" + } +} diff --git a/package.json b/package.json index dc4dbcd7..7f7cefc4 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "scripts": { "clean": "tsc-multi --clean", "prebuild": "pnpm -F @openai/* -r prebuild", - "build": "tsc-multi", + "build:esm": "tsc-multi", + "build:cjs": "tsc-multi --config tsc-multi.cjs.json", + "build": "npm run build:esm && npm run build:cjs", "postbuild": "pnpm -r -F @openai/* bundle", "packages:dev": "tsc-multi --watch", "docs:dev": "pnpm -F docs dev", diff --git a/packages/agents-core/package.json b/packages/agents-core/package.json index 6672371e..416ce1a4 100644 --- a/packages/agents-core/package.json +++ b/packages/agents-core/package.json @@ -5,76 +5,45 @@ "version": "0.0.16", "description": "The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.", "author": "OpenAI ", - "main": "dist/index.js", + "main": "./dist-cjs/index.cjs", + "module": "./dist/index.js", "types": "dist/index.d.ts", "scripts": { "prebuild": "tsx ../../scripts/embedMeta.ts", "build": "tsc", + "build:cjs": "tsc -p tsconfig.cjs.json", "build-check": "tsc --noEmit -p ./tsconfig.test.json" }, "exports": { ".": { - "require": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" - }, "types": "./dist/index.d.ts", - "default": "./dist/index.mjs" + "import": "./dist/index.js", + "require": "./dist-cjs/index.cjs" }, "./model": { - "require": { - "types": "./dist/model.d.ts", - "default": "./dist/model.js" - }, "types": "./dist/model.d.ts", - "default": "./dist/model.mjs" + "import": "./dist/model.js", + "require": "./dist-cjs/model.cjs" }, "./utils": { - "require": { - "types": "./dist/utils/index.d.ts", - "default": "./dist/utils/index.js" - }, "types": "./dist/utils/index.d.ts", - "default": "./dist/utils/index.mjs" + "import": "./dist/utils/index.js", + "require": "./dist-cjs/utils/index.cjs" }, "./extensions": { - "require": { - "types": "./dist/extensions/index.d.ts", - "default": "./dist/extensions/index.js" - }, "types": "./dist/extensions/index.d.ts", - "default": "./dist/extensions/index.mjs" + "import": "./dist/extensions/index.js", + "require": "./dist-cjs/extensions/index.cjs" }, "./types": { - "require": { - "types": "./dist/types/index.d.ts", - "default": "./dist/types/index.js" - }, "types": "./dist/types/index.d.ts", - "default": "./dist/types/index.mjs" + "import": "./dist/types/index.js", + "require": "./dist-cjs/types/index.cjs" }, "./_shims": { - "workerd": { - "require": "./dist/shims/shims-workerd.js", - "types": "./dist/shims/shims-workerd.d.ts", - "default": "./dist/shims/shims-workerd.mjs" - }, - "browser": { - "require": "./dist/shims/shims-browser.js", - "types": "./dist/shims/shims-browser.d.ts", - "default": "./dist/shims/shims-browser.mjs" - }, - "node": { - "require": "./dist/shims/shims-node.js", - "types": "./dist/shims/shims-node.d.ts", - "default": "./dist/shims/shims-node.mjs" - }, - "require": { - "types": "./dist/shims/shims-node.d.ts", - "default": "./dist/shims/shims-node.js" - }, "types": "./dist/shims/shims-node.d.ts", - "default": "./dist/shims/shims-node.mjs" + "import": "./dist/shims/shims-node.mjs", + "require": "./dist-cjs/shims/shims-node.cjs" } }, "keywords": [ @@ -123,6 +92,7 @@ "zod": "^3.25.40" }, "files": [ - "dist" + "dist", + "dist-cjs/**" ] } diff --git a/packages/agents-core/src/shims/shims-node.ts b/packages/agents-core/src/shims/shims-node.ts index 1bb0086d..d56a0866 100644 --- a/packages/agents-core/src/shims/shims-node.ts +++ b/packages/agents-core/src/shims/shims-node.ts @@ -14,11 +14,13 @@ declare global { // circular dependency resolution issues caused by other exports in '@openai/agents-core/_shims' export function loadEnv(): Record { if (typeof process === 'undefined' || typeof process.env === 'undefined') { - if ( - typeof import.meta === 'object' && - typeof import.meta.env === 'object' - ) { - return import.meta.env as unknown as Record; + try { + const m = (0, eval)('import.meta'); + if (typeof m === 'object' && typeof m.env === 'object') { + return m.env as unknown as Record; + } + } catch { + /* CJS. */ } return {}; } diff --git a/packages/agents-core/src/shims/shims-workerd.ts b/packages/agents-core/src/shims/shims-workerd.ts index 110cdb20..0ec60288 100644 --- a/packages/agents-core/src/shims/shims-workerd.ts +++ b/packages/agents-core/src/shims/shims-workerd.ts @@ -15,11 +15,13 @@ declare global { // circular dependency resolution issues caused by other exports in '@openai/agents-core/_shims' export function loadEnv(): Record { if (typeof process === 'undefined' || typeof process.env === 'undefined') { - if ( - typeof import.meta === 'object' && - typeof import.meta.env === 'object' - ) { - return import.meta.env as unknown as Record; + try { + const m = (0, eval)('import.meta'); + if (typeof m === 'object' && typeof m.env === 'object') { + return m.env as unknown as Record; + } + } catch { + /* CJS. */ } return {}; } diff --git a/packages/agents-core/tsconfig.cjs.json b/packages/agents-core/tsconfig.cjs.json new file mode 100644 index 00000000..83251973 --- /dev/null +++ b/packages/agents-core/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist-cjs" + }, + "exclude": ["dist/**", "dist-cjs/**", "test/**"] +} diff --git a/packages/agents-extensions/package.json b/packages/agents-extensions/package.json index fbfdc61d..76b744e9 100644 --- a/packages/agents-extensions/package.json +++ b/packages/agents-extensions/package.json @@ -19,11 +19,11 @@ }, "exports": { ".": { + "types": "./dist/index.d.ts", "require": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }, - "types": "./dist/index.d.ts", "default": "./dist/index.mjs" } }, diff --git a/packages/agents-openai/package.json b/packages/agents-openai/package.json index 5a1750e3..9eaffae3 100644 --- a/packages/agents-openai/package.json +++ b/packages/agents-openai/package.json @@ -5,16 +5,13 @@ "version": "0.0.16", "description": "The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.", "author": "OpenAI ", - "main": "dist/index.js", + "main": "./dist-cjs/index.cjs", + "module": "./dist/index.js", "types": "dist/index.d.ts", "exports": { ".": { - "require": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" - }, - "types": "./dist/index.d.ts", - "default": "./dist/index.mjs" + "import": "./dist/index.js", + "require": "./dist-cjs/index.cjs" } }, "dependencies": { @@ -25,6 +22,7 @@ "scripts": { "prebuild": "tsx ../../scripts/embedMeta.ts", "build": "tsc", + "build:cjs": "tsc -p tsconfig.cjs.json", "build-check": "tsc --noEmit -p ./tsconfig.test.json" }, "keywords": [ @@ -43,6 +41,7 @@ "zod": "^3.25.40" }, "files": [ - "dist" + "dist", + "dist-cjs/**" ] } diff --git a/packages/agents-openai/tsconfig.cjs.json b/packages/agents-openai/tsconfig.cjs.json new file mode 100644 index 00000000..83251973 --- /dev/null +++ b/packages/agents-openai/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist-cjs" + }, + "exclude": ["dist/**", "dist-cjs/**", "test/**"] +} diff --git a/packages/agents-realtime/package.json b/packages/agents-realtime/package.json index 41452781..3e06c25c 100644 --- a/packages/agents-realtime/package.json +++ b/packages/agents-realtime/package.json @@ -5,57 +5,25 @@ "version": "0.0.16", "description": "The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows. This package contains the logic for building realtime voice agents on the server or in the browser.", "author": "OpenAI ", - "main": "dist/index.js", + "main": "./dist-cjs/index.cjs", + "module": "./dist/index.js", "types": "dist/index.d.ts", - "browser": "./dist/bundle/openai-realtime-agents.umd.cjs", "exports": { ".": { - "browser": { - "require": "./dist/index.js", - "types": "./dist/index.d.ts", - "default": "./dist/index.mjs" - }, - "require": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" - }, "types": "./dist/index.d.ts", - "default": "./dist/index.mjs" + "import": "./dist/index.js", + "require": "./dist-cjs/index.cjs" }, "./_shims": { - "workerd": { - "require": "./dist/shims/shims-workerd.js", - "types": "./dist/shims/shims-workerd.d.ts", - "default": "./dist/shims/shims-workerd.mjs" - }, - "browser": { - "require": "./dist/shims/shims-browser.js", - "types": "./dist/shims/shims-browser.d.ts", - "default": "./dist/shims/shims-browser.mjs" - }, - "node": { - "require": "./dist/shims/shims-node.js", - "types": "./dist/shims/shims-node.d.ts", - "default": "./dist/shims/shims-node.mjs" - }, - "require": { - "types": "./dist/shims/shims-node.d.ts", - "default": "./dist/shims/shims-node.js" - }, "types": "./dist/shims/shims-node.d.ts", - "default": "./dist/shims/shims-node.mjs" - } - }, - "typesVersions": { - "*": { - "_shims": [ - "dist/shims/shims-node.d.ts" - ] + "import": "./dist/shims/shims-node.mjs", + "require": "./dist-cjs/shims/shims-node.cjs" } }, "scripts": { "prebuild": "tsx ../../scripts/embedMeta.ts", "build": "tsc", + "build:cjs": "tsc -p tsconfig.cjs.json", "build-check": "tsc --noEmit -p ./tsconfig.test.json", "bundle": "vite build" }, @@ -81,6 +49,14 @@ "zod": "^3.25.40" }, "files": [ - "dist" - ] + "dist", + "dist-cjs/**" + ], + "typesVersions": { + "*": { + "_shims": [ + "dist/shims/shims-node.d.ts" + ] + } + } } diff --git a/packages/agents-realtime/tsconfig.cjs.json b/packages/agents-realtime/tsconfig.cjs.json new file mode 100644 index 00000000..83251973 --- /dev/null +++ b/packages/agents-realtime/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist-cjs" + }, + "exclude": ["dist/**", "dist-cjs/**", "test/**"] +} diff --git a/packages/agents/package.json b/packages/agents/package.json index b40c3437..b1291769 100644 --- a/packages/agents/package.json +++ b/packages/agents/package.json @@ -5,37 +5,35 @@ "version": "0.0.16", "description": "The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.", "author": "OpenAI ", - "main": "dist/index.js", + "main": "./dist-cjs/index.cjs", + "module": "./dist/index.js", "types": "dist/index.d.ts", "exports": { ".": { - "require": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" - }, "types": "./dist/index.d.ts", - "default": "./dist/index.mjs" + "import": "./dist/index.js", + "require": "./dist-cjs/index.cjs" }, "./realtime": { - "require": { - "types": "./dist/realtime/index.d.ts", - "default": "./dist/realtime/index.js" - }, "types": "./dist/realtime/index.d.ts", - "default": "./dist/realtime/index.mjs" + "import": "./dist/realtime/index.js", + "require": "./dist-cjs/realtime/index.cjs" }, "./utils": { - "require": { - "types": "./dist/utils/index.d.ts", - "default": "./dist/utils/index.js" - }, "types": "./dist/utils/index.d.ts", - "default": "./dist/utils/index.mjs" + "import": "./dist/utils/index.js", + "require": "./dist-cjs/utils/index.cjs" + }, + "./_shims": { + "types": "./dist/shims/shims-node.d.ts", + "import": "./dist/shims/shims-node.mjs", + "require": "./dist-cjs/shims/shims-node.cjs" } }, "scripts": { "prebuild": "tsx ../../scripts/embedMeta.ts", "build": "tsc", + "build:cjs": "tsc -p tsconfig.cjs.json", "build-check": "tsc --noEmit -p ./tsconfig.test.json" }, "dependencies": { @@ -60,7 +58,8 @@ "zod": "^3.25.40" }, "files": [ - "dist" + "dist", + "dist-cjs/**" ], "typesVersions": { "*": { @@ -69,6 +68,9 @@ ], "utils": [ "dist/utils/index.d.ts" + ], + "_shims": [ + "dist/shims/shims-node.d.ts" ] } } diff --git a/packages/agents/src/shims/shims-node.ts b/packages/agents/src/shims/shims-node.ts new file mode 100644 index 00000000..ba8a276d --- /dev/null +++ b/packages/agents/src/shims/shims-node.ts @@ -0,0 +1 @@ +export * from '@openai/agents-core/_shims'; diff --git a/packages/agents/tsconfig.cjs.json b/packages/agents/tsconfig.cjs.json new file mode 100644 index 00000000..83251973 --- /dev/null +++ b/packages/agents/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist-cjs" + }, + "exclude": ["dist/**", "dist-cjs/**", "test/**"] +} diff --git a/tsc-multi.cjs.json b/tsc-multi.cjs.json new file mode 100644 index 00000000..5cdfc5f6 --- /dev/null +++ b/tsc-multi.cjs.json @@ -0,0 +1,11 @@ +{ + "targets": [ + { "extname": ".cjs", "module": "commonjs", "moduleResolution": "node" } + ], + "projects": [ + "packages/agents-core/tsconfig.cjs.json", + "packages/agents/tsconfig.cjs.json", + "packages/agents-openai/tsconfig.cjs.json", + "packages/agents-realtime/tsconfig.cjs.json" + ] +} From 8438c24a6c657a8b46ae43c2d6d312ef356c9e54 Mon Sep 17 00:00:00 2001 From: vrtnis <123119434+vrtnis@users.noreply.github.com> Date: Tue, 5 Aug 2025 15:37:27 -0700 Subject: [PATCH 2/4] fix: prevent Vite from bundling node-only MCP SDK deps --- packages/agents-realtime/vite.config.js | 39 +++++++++++++++++++------ 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/agents-realtime/vite.config.js b/packages/agents-realtime/vite.config.js index a01aac0c..e7f8db9d 100644 --- a/packages/agents-realtime/vite.config.js +++ b/packages/agents-realtime/vite.config.js @@ -6,26 +6,47 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); export default defineConfig({ build: { + // Run this as a Node lib, not a browser bundle + target: 'node16', + platform: 'node', + lib: { entry: resolve(__dirname, 'dist/index.mjs'), name: 'OpenAIAgentsRealtime', - // the proper extensions will be added fileName: 'openai-realtime-agents', + // only emit ESM (remove 'umd' which is the default 3rd format) + formats: ['es'], }, + sourcemap: 'inline', + rollupOptions: { - // make sure to externalize deps that shouldn't be bundled - // into your library - external: [], + // Externalize all Node built-ins & optional SDK + external: (id) => { + if (id.startsWith('node:')) return true; + const builtins = [ + 'fs', + 'path', + 'events', + 'stream', + 'stream/web', + 'async_hooks', + 'timers', + 'crypto', + 'child_process', + ]; + if (builtins.includes(id)) return true; + if (id === '@modelcontextprotocol/sdk' || id === 'cross-spawn') + return true; + return false; + }, + output: { dir: 'dist/bundle', banner: '/** OpenAI Agents Realtime **/', minifyInternalExports: false, - // Provide global variables to use in the UMD build - // for externalized deps - globals: { - // vue: 'Vue', - }, + // no need for UMD globals now since we’re not building UMD + globals: {}, }, }, }, From 0772072abdc396fabb8e761d698b4dcd4544b362 Mon Sep 17 00:00:00 2001 From: vrtnis <123119434+vrtnis@users.noreply.github.com> Date: Wed, 13 Aug 2025 12:09:25 -0700 Subject: [PATCH 3/4] chore(eslint): ignore generated test .cjs files and sourcemaps --- .changeset/odd-islands-melt.md | 9 ++ eslint.config.mjs | 220 ++++++++++++++++++++++++++++----- 2 files changed, 199 insertions(+), 30 deletions(-) create mode 100644 .changeset/odd-islands-melt.md diff --git a/.changeset/odd-islands-melt.md b/.changeset/odd-islands-melt.md new file mode 100644 index 00000000..8638538a --- /dev/null +++ b/.changeset/odd-islands-melt.md @@ -0,0 +1,9 @@ +--- +'@openai/agents-extensions': patch +'@openai/agents-realtime': patch +'@openai/agents-openai': patch +'@openai/agents-core': patch +'@openai/agents': patch +--- + +Dual ESM + CJS builds with import.meta shim guard and Lambda smoke-tests, plus patch bumps for all packages. diff --git a/eslint.config.mjs b/eslint.config.mjs index c482defa..3925919d 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,21 +1,10 @@ -// export default { -// parser: '@typescript-eslint/parser', -// plugins: ['@typescript-eslint', 'unused-imports', 'prettier'], -// rules: { -// 'no-unused-vars': 'off', -// 'prettier/prettier': 'error', -// 'unused-imports/no-unused-imports': 'error', -// }, -// root: true, -// }; - import eslint from '@eslint/js'; -// import someOtherConfig from 'eslint-config-other-configuration-that-enables-formatting-rules'; import prettierConfig from 'eslint-config-prettier'; import tseslint from 'typescript-eslint'; import { globalIgnores } from 'eslint/config'; export default tseslint.config( + // Ignore build outputs and large example folders globalIgnores([ '**/dist/**', '**/dist-cjs/**', @@ -24,30 +13,201 @@ export default tseslint.config( 'examples/realtime-next/**', 'examples/realtime-demo/**', 'examples/nextjs/**', - 'integration-tests//**', + 'examples/tools/**', + 'integration-tests/**', + // Docs code snippets: not intended to pass lint as runtime code + 'examples/docs/**', + 'packages/**/test/**/*.cjs', + 'packages/**/test/**/*.cjs.map', ]), + + // Base + TS + Prettier eslint.configs.recommended, tseslint.configs.recommended, prettierConfig, - [ - { - rules: { - '@typescript-eslint/no-explicit-any': 'off', - '@typescript-eslint/no-unused-vars': [ - 'error', - { - argsIgnorePattern: '^_', - varsIgnorePattern: '^_', - caughtErrorsIgnorePattern: '^_', - }, - ], + + // Repo-wide TS rules + { + rules: { + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-unused-vars': [ + 'error', + { + argsIgnorePattern: '^_', + varsIgnorePattern: '^_', + caughtErrorsIgnorePattern: '^_', + }, + ], + }, + }, + + // Allow CommonJS in .cjs files (configs, tests, scripts, CJS entrypoints) + { + files: ['**/*.cjs'], + languageOptions: { + sourceType: 'script', // treat as CommonJS + ecmaVersion: 'latest', + globals: { + // Node/CJS + require: 'readonly', + module: 'readonly', + exports: 'readonly', + __dirname: 'readonly', + __filename: 'readonly', + process: 'readonly', + console: 'readonly', + Buffer: 'readonly', + setTimeout: 'readonly', + clearTimeout: 'readonly', + setImmediate: 'readonly', + structuredClone: 'readonly', + URL: 'readonly', + AbortController: 'readonly', + // Some runtimes/polyfills supply these even in CJS contexts + Response: 'readonly', }, }, - { - files: ['examples/docs/**'], - rules: { - '@typescript-eslint/no-unused-vars': 'off', + rules: { + '@typescript-eslint/no-require-imports': 'off', + // Helpful if these plugins are present: + 'import/no-commonjs': 'off', + 'n/no-missing-require': 'off', + 'n/no-unsupported-features/es-syntax': 'off', + 'unicorn/prefer-module': 'off', + // Some CJS files intentionally use expression statements + '@typescript-eslint/no-unused-expressions': 'off', + }, + }, + + // Tests override: Node + web-like globals used/mocked in CJS tests + { + files: ['**/test/**/*.cjs'], + languageOptions: { + sourceType: 'script', + ecmaVersion: 'latest', + globals: { + // Node-ish + global: 'readonly', + process: 'readonly', + require: 'readonly', + module: 'readonly', + exports: 'readonly', + console: 'readonly', + setTimeout: 'readonly', + clearTimeout: 'readonly', + setImmediate: 'readonly', + Buffer: 'readonly', + URL: 'readonly', + AbortController: 'readonly', + + // Web APIs commonly mocked/polyfilled in tests + Event: 'readonly', + EventTarget: 'readonly', + MessageEvent: 'readonly', + TextEncoder: 'readonly', + FormData: 'readonly', + fetch: 'readonly', + navigator: 'readonly', + document: 'readonly', + window: 'readonly', + atob: 'readonly', + btoa: 'readonly', + Response: 'readonly', + RTCPeerConnection: 'readonly', + + // Vitest-style test globals + describe: 'readonly', + it: 'readonly', + test: 'readonly', + expect: 'readonly', + beforeAll: 'readonly', + afterAll: 'readonly', + beforeEach: 'readonly', + afterEach: 'readonly', + vi: 'readonly', }, }, - ], + rules: { + '@typescript-eslint/no-require-imports': 'off', + }, + }, + + // Source CJS that target browser-like environments (e.g., realtime WebRTC) + { + files: ['**/src/**/*.cjs'], + languageOptions: { + sourceType: 'script', + ecmaVersion: 'latest', + globals: { + // Node + require: 'readonly', + module: 'readonly', + exports: 'readonly', + process: 'readonly', + console: 'readonly', + setTimeout: 'readonly', + clearTimeout: 'readonly', + setImmediate: 'readonly', + Buffer: 'readonly', + structuredClone: 'readonly', + URL: 'readonly', + AbortController: 'readonly', + // Web + Event: 'readonly', + EventTarget: 'readonly', + MessageEvent: 'readonly', + TextEncoder: 'readonly', + FormData: 'readonly', + fetch: 'readonly', + navigator: 'readonly', + document: 'readonly', + window: 'readonly', + atob: 'readonly', + btoa: 'readonly', + RTCPeerConnection: 'readonly', + CustomEvent: 'readonly', + crypto: 'readonly', + Response: 'readonly', + }, + }, + }, + + // Shims often intentionally shadow/process globals; relax specific rules there + { + files: [ + '**/src/shims/**/*.cjs', + '**/src/shims/**/*.js', + '**/src/shims/**/*.ts', + 'docs/src/scripts/**/*.cjs', // docs script: allow redefining __filename/__dirname + ], + rules: { + 'no-redeclare': 'off', + }, + }, + + // Lambda test folder uses CommonJS + { + files: ['lambda-test/**'], + languageOptions: { + sourceType: 'script', + globals: { + require: 'readonly', + module: 'readonly', + exports: 'readonly', + process: 'readonly', + }, + }, + rules: { + '@typescript-eslint/no-require-imports': 'off', + }, + }, + + // Declaration files: don't flag private class members as unused in .d.ts + { + files: ['**/*.d.ts'], + rules: { + 'no-unused-private-class-members': 'off', + '@typescript-eslint/no-unused-vars': 'off', + }, + }, ); From 80cdb88be7d11a66b63943f6f59277d68d848948 Mon Sep 17 00:00:00 2001 From: vrtnis <123119434+vrtnis@users.noreply.github.com> Date: Fri, 15 Aug 2025 11:58:32 -0700 Subject: [PATCH 4/4] chore: clean up TwilioRealtimeTransport lint warnings and update package.json --- package.json | 2 +- .../agents-extensions/src/TwilioRealtimeTransport.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 7f7cefc4..f6e565f2 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "prebuild": "pnpm -F @openai/* -r prebuild", "build:esm": "tsc-multi", "build:cjs": "tsc-multi --config tsc-multi.cjs.json", - "build": "npm run build:esm && npm run build:cjs", + "build": "tsc-multi && tsc-multi --config tsc-multi.cjs.json", "postbuild": "pnpm -r -F @openai/* bundle", "packages:dev": "tsc-multi --watch", "docs:dev": "pnpm -F docs dev", diff --git a/packages/agents-extensions/src/TwilioRealtimeTransport.ts b/packages/agents-extensions/src/TwilioRealtimeTransport.ts index 0a7007a8..d987f8dc 100644 --- a/packages/agents-extensions/src/TwilioRealtimeTransport.ts +++ b/packages/agents-extensions/src/TwilioRealtimeTransport.ts @@ -98,7 +98,7 @@ export class TwilioRealtimeTransportLayer extends OpenAIRealtimeWebSocket { } else { this.#logger.debug('Twilio message:', data); } - this.emit('*', { + (this as any).emit?.('*', { type: 'twilio_message', message: data, }); @@ -140,7 +140,7 @@ export class TwilioRealtimeTransportLayer extends OpenAIRealtimeWebSocket { 'Message:', message, ); - this.emit('error', { + (this as any).emit?.('error', { type: 'error', error, }); @@ -155,14 +155,14 @@ export class TwilioRealtimeTransportLayer extends OpenAIRealtimeWebSocket { this.#twilioWebSocket.addEventListener( 'error', (error: ErrorEvent | NodeErrorEvent) => { - this.emit('error', { + (this as any).emit?.('error', { type: 'error', error, }); this.close(); }, ); - this.on('audio_done', () => { + (this as any).on?.('audio_done', () => { this.#twilioWebSocket.send( JSON.stringify({ event: 'mark', @@ -178,7 +178,7 @@ export class TwilioRealtimeTransportLayer extends OpenAIRealtimeWebSocket { updateSessionConfig(config: Partial): void { const newConfig = this._setInputAndOutputAudioFormat(config); - super.updateSessionConfig(newConfig); + (this as any).updateSessionConfig?.(newConfig); } _interrupt(_elapsedTime: number, cancelOngoingResponse: boolean = true) { @@ -221,6 +221,6 @@ export class TwilioRealtimeTransportLayer extends OpenAIRealtimeWebSocket { }, }), ); - this.emit('audio', audioEvent); + (this as any).emit?.('audio', audioEvent); } }