Skip to content

Commit

Permalink
v2 remove all absolute imports from openai-types and package.json scr…
Browse files Browse the repository at this point in the history
…ipts cleanup (#37)
  • Loading branch information
transitive-bullshit authored Nov 7, 2023
1 parent 1adb936 commit bae240a
Show file tree
Hide file tree
Showing 44 changed files with 469 additions and 232 deletions.
8 changes: 0 additions & 8 deletions .eslintrc

This file was deleted.

36 changes: 13 additions & 23 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master
on: [push, pull_request]

jobs:
lint:
name: Lint
test:
name: Test Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
node-version:
- 21
- 20
- 18
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn lint
types:
name: Types
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn typecheck
- run: yarn test
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enable-pre-post-scripts=true
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
openai-types
dist
27 changes: 12 additions & 15 deletions extract-types.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

/**
* This is used to extract the type declarations from the openai Node.js
* package. Doing this allows it to be a devDependency instead of a dependency,
* which greatly reduces the size of the final bundle.
*/
function extractTypes(srcDir, destDir, root = false) {
function extractTypes(srcDir, destDir) {
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
Expand All @@ -20,20 +20,17 @@ function extractTypes(srcDir, destDir, root = false) {
if (entry.isDirectory()) {
extractTypes(srcPath, destPath);
} else if (entry.isFile() && entry.name.endsWith('.d.ts')) {
console.log(destPath);
const depth = Math.max(0, destPath.split('/').length - 2);
const relativePath = depth > 0 ? '../'.repeat(depth) : './';

if (root && entry.name === 'index.d.ts') {
// OpenAI has one line with an absolute package import, so switch it to
// be a relative import.
const content = fs
.readFileSync(srcPath, 'utf8')
.replaceAll("'openai/resources/index'", "'./resources/index.js'");
fs.writeFileSync(destPath, content);
} else {
fs.copyFileSync(srcPath, destPath);
}
// OpenAI has some absolute package imports, so switch them to use relative imports.
const content = fs
.readFileSync(srcPath, 'utf8')
.replaceAll(/'openai\/([^'.]*)'/g, `'${relativePath}$1.js'`);
fs.writeFileSync(destPath, content);
console.log(destPath);
}
}
}

extractTypes('node_modules/openai', 'openai-types', true);
extractTypes('node_modules/openai', 'openai-types');
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Riley Tomasek
Copyright (c) 2023 Riley Tomasek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion openai-types/_shims/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/
import { manual } from "./manual-types.js";
import * as auto from 'openai/_shims/auto/types';
import * as auto from '../_shims/auto/types.js';
import { type RequestOptions } from "../core.js";

type SelectType<Manual, Auto> = unknown extends Manual ? Auto : Manual;
Expand Down
4 changes: 2 additions & 2 deletions openai-types/_shims/manual-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/**
* Types will get added to this namespace when you import one of the following:
*
* import 'openai/shims/node'
* import 'openai/shims/web'
* import '../shims/node.js'
* import '../shims/web.js'
*
* Importing more than one will cause type and runtime errors.
*/
Expand Down
8 changes: 4 additions & 4 deletions openai-types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export declare class APIPromise<T> extends Promise<T> {
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` if you can,
* or add one of these imports before your first `import … from 'openai'`:
* - `import 'openai/shims/node'` (if you're running on Node)
* - `import 'openai/shims/web'` (otherwise)
* - `import './shims/node.js'` (if you're running on Node)
* - `import './shims/web.js'` (otherwise)
*/
asResponse(): Promise<Response>;
/**
Expand All @@ -46,8 +46,8 @@ export declare class APIPromise<T> extends Promise<T> {
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` if you can,
* or add one of these imports before your first `import … from 'openai'`:
* - `import 'openai/shims/node'` (if you're running on Node)
* - `import 'openai/shims/web'` (otherwise)
* - `import './shims/node.js'` (if you're running on Node)
* - `import './shims/web.js'` (otherwise)
*/
withResponse(): Promise<{
data: T;
Expand Down
8 changes: 4 additions & 4 deletions openai-types/lib/AbstractChatCompletionRunner.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import * as Core from 'openai/core';
import { type CompletionUsage } from 'openai/resources/completions';
import { type Completions, type ChatCompletion, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionCreateParams } from 'openai/resources/chat/completions';
import { APIUserAbortError, OpenAIError } from 'openai/error';
import * as Core from '../core.js';
import { type CompletionUsage } from '../resources/completions.js';
import { type Completions, type ChatCompletion, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionCreateParams } from '../resources/chat/completions.js';
import { APIUserAbortError, OpenAIError } from '../error.js';
import { type BaseFunctionsArgs } from "./RunnableFunction.js";
import { ChatCompletionFunctionRunnerParams, ChatCompletionToolRunnerParams } from "./ChatCompletionRunner.js";
import { ChatCompletionStreamingFunctionRunnerParams, ChatCompletionStreamingToolRunnerParams } from "./ChatCompletionStreamingRunner.js";
Expand Down
2 changes: 1 addition & 1 deletion openai-types/lib/ChatCompletionRunner.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Completions, type ChatCompletionMessageParam, type ChatCompletionCreateParamsNonStreaming } from 'openai/resources/chat/completions';
import { type Completions, type ChatCompletionMessageParam, type ChatCompletionCreateParamsNonStreaming } from '../resources/chat/completions.js';
import { type RunnableFunctions, type BaseFunctionsArgs, RunnableTools } from "./RunnableFunction.js";
import { AbstractChatCompletionRunner, AbstractChatCompletionRunnerEvents, RunnerOptions } from "./AbstractChatCompletionRunner.js";
export interface ChatCompletionRunnerEvents extends AbstractChatCompletionRunnerEvents {
Expand Down
6 changes: 3 additions & 3 deletions openai-types/lib/ChatCompletionStream.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { Completions, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionCreateParamsBase } from 'openai/resources/chat/completions';
import * as Core from '../core.js';
import { Completions, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionCreateParamsBase } from '../resources/chat/completions.js';
import { AbstractChatCompletionRunner, type AbstractChatCompletionRunnerEvents } from "./AbstractChatCompletionRunner.js";
import { type ReadableStream } from 'openai/_shims/index';
import { type ReadableStream } from '../_shims/index.js';
export interface ChatCompletionStreamEvents extends AbstractChatCompletionRunnerEvents {
content: (contentDelta: string, contentSnapshot: string) => void;
chunk: (chunk: ChatCompletionChunk, snapshot: ChatCompletionSnapshot) => void;
Expand Down
4 changes: 2 additions & 2 deletions openai-types/lib/ChatCompletionStreamingRunner.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Completions, type ChatCompletionChunk, type ChatCompletionCreateParamsStreaming } from 'openai/resources/chat/completions';
import { Completions, type ChatCompletionChunk, type ChatCompletionCreateParamsStreaming } from '../resources/chat/completions.js';
import { RunnerOptions, type AbstractChatCompletionRunnerEvents } from "./AbstractChatCompletionRunner.js";
import { type ReadableStream } from 'openai/_shims/index';
import { type ReadableStream } from '../_shims/index.js';
import { RunnableTools, type BaseFunctionsArgs, type RunnableFunctions } from "./RunnableFunction.js";
import { ChatCompletionSnapshot, ChatCompletionStream } from "./ChatCompletionStream.js";
export interface ChatCompletionStreamEvents extends AbstractChatCompletionRunnerEvents {
Expand Down
2 changes: 1 addition & 1 deletion openai-types/lib/chatCompletionUtils.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ChatCompletionAssistantMessageParam, type ChatCompletionFunctionMessageParam, type ChatCompletionMessageParam, type ChatCompletionToolMessageParam } from 'openai/resources';
import { type ChatCompletionAssistantMessageParam, type ChatCompletionFunctionMessageParam, type ChatCompletionMessageParam, type ChatCompletionToolMessageParam } from '../resources.js';
export declare const isAssistantMessage: (message: ChatCompletionMessageParam | null | undefined) => message is ChatCompletionAssistantMessageParam;
export declare const isFunctionMessage: (message: ChatCompletionMessageParam | null | undefined) => message is ChatCompletionFunctionMessageParam;
export declare const isToolMessage: (message: ChatCompletionMessageParam | null | undefined) => message is ChatCompletionToolMessageParam;
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/audio/audio.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIResource } from 'openai/resource';
import * as SpeechAPI from 'openai/resources/audio/speech';
import * as TranscriptionsAPI from 'openai/resources/audio/transcriptions';
import * as TranslationsAPI from 'openai/resources/audio/translations';
import { APIResource } from '../../resource.js';
import * as SpeechAPI from '../../resources/audio/speech.js';
import * as TranscriptionsAPI from '../../resources/audio/transcriptions.js';
import * as TranslationsAPI from '../../resources/audio/translations.js';
export declare class Audio extends APIResource {
transcriptions: TranscriptionsAPI.Transcriptions;
translations: TranslationsAPI.Translations;
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/audio/speech.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import { type Response } from 'openai/_shims/index';
import * as SpeechAPI from 'openai/resources/audio/speech';
import * as Core from '../../core.js';
import { APIResource } from '../../resource.js';
import { type Response } from '../../_shims/index.js';
import * as SpeechAPI from '../../resources/audio/speech.js';
export declare class Speech extends APIResource {
/**
* Generates audio from the input text.
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/audio/transcriptions.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as TranscriptionsAPI from 'openai/resources/audio/transcriptions';
import { type Uploadable } from 'openai/core';
import * as Core from '../../core.js';
import { APIResource } from '../../resource.js';
import * as TranscriptionsAPI from '../../resources/audio/transcriptions.js';
import { type Uploadable } from '../../core.js';
export declare class Transcriptions extends APIResource {
/**
* Transcribes audio into the input language.
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/audio/translations.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as TranslationsAPI from 'openai/resources/audio/translations';
import { type Uploadable } from 'openai/core';
import * as Core from '../../core.js';
import { APIResource } from '../../resource.js';
import * as TranslationsAPI from '../../resources/audio/translations.js';
import { type Uploadable } from '../../core.js';
export declare class Translations extends APIResource {
/**
* Translates audio into English.
Expand Down
10 changes: 5 additions & 5 deletions openai-types/resources/beta/assistants/assistants.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as AssistantsAPI from 'openai/resources/beta/assistants/assistants';
import * as FilesAPI from 'openai/resources/beta/assistants/files';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
import * as Core from '../../../core.js';
import { APIResource } from '../../../resource.js';
import * as AssistantsAPI from '../../../resources/beta/assistants/assistants.js';
import * as FilesAPI from '../../../resources/beta/assistants/files.js';
import { CursorPage, type CursorPageParams } from '../../../pagination.js';
export declare class Assistants extends APIResource {
files: FilesAPI.Files;
/**
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/beta/assistants/files.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as FilesAPI from 'openai/resources/beta/assistants/files';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
import * as Core from '../../../core.js';
import { APIResource } from '../../../resource.js';
import * as FilesAPI from '../../../resources/beta/assistants/files.js';
import { CursorPage, type CursorPageParams } from '../../../pagination.js';
export declare class Files extends APIResource {
/**
* Create an assistant file by attaching a
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/beta/beta.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIResource } from 'openai/resource';
import * as AssistantsAPI from 'openai/resources/beta/assistants/assistants';
import * as ChatAPI from 'openai/resources/beta/chat/chat';
import * as ThreadsAPI from 'openai/resources/beta/threads/threads';
import { APIResource } from '../../resource.js';
import * as AssistantsAPI from '../../resources/beta/assistants/assistants.js';
import * as ChatAPI from '../../resources/beta/chat/chat.js';
import * as ThreadsAPI from '../../resources/beta/threads/threads.js';
export declare class Beta extends APIResource {
chat: ChatAPI.Chat;
assistants: AssistantsAPI.Assistants;
Expand Down
4 changes: 2 additions & 2 deletions openai-types/resources/beta/chat/chat.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIResource } from 'openai/resource';
import * as CompletionsAPI from 'openai/resources/beta/chat/completions';
import { APIResource } from '../../../resource.js';
import * as CompletionsAPI from '../../../resources/beta/chat/completions.js';
export declare class Chat extends APIResource {
completions: CompletionsAPI.Completions;
}
Expand Down
28 changes: 14 additions & 14 deletions openai-types/resources/beta/chat/completions.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import { ChatCompletionRunner, ChatCompletionFunctionRunnerParams } from 'openai/lib/ChatCompletionRunner';
export { ChatCompletionRunner, ChatCompletionFunctionRunnerParams } from 'openai/lib/ChatCompletionRunner';
import { ChatCompletionStreamingRunner, ChatCompletionStreamingFunctionRunnerParams } from 'openai/lib/ChatCompletionStreamingRunner';
export { ChatCompletionStreamingRunner, ChatCompletionStreamingFunctionRunnerParams, } from 'openai/lib/ChatCompletionStreamingRunner';
import { BaseFunctionsArgs } from 'openai/lib/RunnableFunction';
export { RunnableFunction, RunnableFunctions, RunnableFunctionWithParse, RunnableFunctionWithoutParse, ParsingFunction, } from 'openai/lib/RunnableFunction';
import { ChatCompletionToolRunnerParams } from 'openai/lib/ChatCompletionRunner';
export { ChatCompletionToolRunnerParams } from 'openai/lib/ChatCompletionRunner';
import { ChatCompletionStreamingToolRunnerParams } from 'openai/lib/ChatCompletionStreamingRunner';
export { ChatCompletionStreamingToolRunnerParams } from 'openai/lib/ChatCompletionStreamingRunner';
import { ChatCompletionStream, type ChatCompletionStreamParams } from 'openai/lib/ChatCompletionStream';
export { ChatCompletionStream, type ChatCompletionStreamParams } from 'openai/lib/ChatCompletionStream';
import * as Core from '../../../core.js';
import { APIResource } from '../../../resource.js';
import { ChatCompletionRunner, ChatCompletionFunctionRunnerParams } from '../../../lib/ChatCompletionRunner.js';
export { ChatCompletionRunner, ChatCompletionFunctionRunnerParams } from '../../../lib/ChatCompletionRunner.js';
import { ChatCompletionStreamingRunner, ChatCompletionStreamingFunctionRunnerParams } from '../../../lib/ChatCompletionStreamingRunner.js';
export { ChatCompletionStreamingRunner, ChatCompletionStreamingFunctionRunnerParams, } from '../../../lib/ChatCompletionStreamingRunner.js';
import { BaseFunctionsArgs } from '../../../lib/RunnableFunction.js';
export { RunnableFunction, RunnableFunctions, RunnableFunctionWithParse, RunnableFunctionWithoutParse, ParsingFunction, } from '../../../lib/RunnableFunction.js';
import { ChatCompletionToolRunnerParams } from '../../../lib/ChatCompletionRunner.js';
export { ChatCompletionToolRunnerParams } from '../../../lib/ChatCompletionRunner.js';
import { ChatCompletionStreamingToolRunnerParams } from '../../../lib/ChatCompletionStreamingRunner.js';
export { ChatCompletionStreamingToolRunnerParams } from '../../../lib/ChatCompletionStreamingRunner.js';
import { ChatCompletionStream, type ChatCompletionStreamParams } from '../../../lib/ChatCompletionStream.js';
export { ChatCompletionStream, type ChatCompletionStreamParams } from '../../../lib/ChatCompletionStream.js';
export declare class Completions extends APIResource {
/**
* A convenience helper for using function calls with the /chat/completions
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/beta/threads/messages/files.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as FilesAPI from 'openai/resources/beta/threads/messages/files';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
import * as Core from '../../../../core.js';
import { APIResource } from '../../../../resource.js';
import * as FilesAPI from '../../../../resources/beta/threads/messages/files.js';
import { CursorPage, type CursorPageParams } from '../../../../pagination.js';
export declare class Files extends APIResource {
/**
* Retrieves a message file.
Expand Down
10 changes: 5 additions & 5 deletions openai-types/resources/beta/threads/messages/messages.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as MessagesAPI from 'openai/resources/beta/threads/messages/messages';
import * as FilesAPI from 'openai/resources/beta/threads/messages/files';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
import * as Core from '../../../../core.js';
import { APIResource } from '../../../../resource.js';
import * as MessagesAPI from '../../../../resources/beta/threads/messages/messages.js';
import * as FilesAPI from '../../../../resources/beta/threads/messages/files.js';
import { CursorPage, type CursorPageParams } from '../../../../pagination.js';
export declare class Messages extends APIResource {
files: FilesAPI.Files;
/**
Expand Down
10 changes: 5 additions & 5 deletions openai-types/resources/beta/threads/runs/runs.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as RunsAPI from 'openai/resources/beta/threads/runs/runs';
import * as StepsAPI from 'openai/resources/beta/threads/runs/steps';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
import * as Core from '../../../../core.js';
import { APIResource } from '../../../../resource.js';
import * as RunsAPI from '../../../../resources/beta/threads/runs/runs.js';
import * as StepsAPI from '../../../../resources/beta/threads/runs/steps.js';
import { CursorPage, type CursorPageParams } from '../../../../pagination.js';
export declare class Runs extends APIResource {
steps: StepsAPI.Steps;
/**
Expand Down
8 changes: 4 additions & 4 deletions openai-types/resources/beta/threads/runs/steps.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import * as StepsAPI from 'openai/resources/beta/threads/runs/steps';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
import * as Core from '../../../../core.js';
import { APIResource } from '../../../../resource.js';
import * as StepsAPI from '../../../../resources/beta/threads/runs/steps.js';
import { CursorPage, type CursorPageParams } from '../../../../pagination.js';
export declare class Steps extends APIResource {
/**
* Retrieves a run step.
Expand Down
Loading

0 comments on commit bae240a

Please sign in to comment.