@openai-oauth/core 2.0.0-beta.0

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/state.js ADDED
@@ -0,0 +1,154 @@
1
+ const MAX_ITEM_CACHE_SIZE = 2_000;
2
+ const MAX_RESPONSE_CACHE_SIZE = 256;
3
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
+ const cloneValue = (value) => structuredClone(value);
5
+ const trimOldestEntries = (map, maxEntries) => {
6
+ while (map.size > maxEntries) {
7
+ const oldestKey = map.keys().next().value;
8
+ if (oldestKey == null) {
9
+ break;
10
+ }
11
+ map.delete(oldestKey);
12
+ }
13
+ };
14
+ export class CodexResponsesState {
15
+ items = new Map();
16
+ responses = new Map();
17
+ pendingCaptures = new Set();
18
+ onChange;
19
+ constructor(options = {}) {
20
+ this.onChange = options.onChange;
21
+ for (const entry of options.snapshot?.items ?? []) {
22
+ if (typeof entry.id !== "string") {
23
+ continue;
24
+ }
25
+ this.items.set(entry.id, cloneValue(entry.item));
26
+ }
27
+ for (const entry of options.snapshot?.responses ?? []) {
28
+ if (typeof entry.id !== "string") {
29
+ continue;
30
+ }
31
+ this.responses.set(entry.id, {
32
+ input: entry.input.map((item) => cloneValue(item)),
33
+ output: entry.output.map((item) => cloneValue(item)),
34
+ });
35
+ }
36
+ trimOldestEntries(this.items, MAX_ITEM_CACHE_SIZE);
37
+ trimOldestEntries(this.responses, MAX_RESPONSE_CACHE_SIZE);
38
+ }
39
+ async waitForPendingCaptures() {
40
+ if (this.pendingCaptures.size === 0) {
41
+ return;
42
+ }
43
+ await Promise.allSettled([...this.pendingCaptures]);
44
+ }
45
+ trackPendingCapture(promise) {
46
+ this.pendingCaptures.add(promise);
47
+ void promise.finally(() => {
48
+ this.pendingCaptures.delete(promise);
49
+ });
50
+ }
51
+ requiresCachedState(body) {
52
+ if (typeof body.previous_response_id === "string") {
53
+ return true;
54
+ }
55
+ if (!Array.isArray(body.input)) {
56
+ return false;
57
+ }
58
+ return body.input.some((item) => isRecord(item) &&
59
+ item.type === "item_reference" &&
60
+ typeof item.id === "string");
61
+ }
62
+ expandRequestBody(body) {
63
+ const nextBody = { ...body };
64
+ const previousResponseId = typeof body.previous_response_id === "string"
65
+ ? body.previous_response_id
66
+ : undefined;
67
+ const previousHistory = previousResponseId == null
68
+ ? undefined
69
+ : this.responses.get(previousResponseId);
70
+ const directInput = Array.isArray(body.input)
71
+ ? this.expandInput(body.input)
72
+ : body.input;
73
+ if (previousHistory != null) {
74
+ nextBody.input = [
75
+ ...cloneValue(previousHistory.input),
76
+ ...cloneValue(previousHistory.output),
77
+ ...(Array.isArray(directInput) ? directInput : []),
78
+ ];
79
+ delete nextBody.previous_response_id;
80
+ return nextBody;
81
+ }
82
+ if (Array.isArray(directInput)) {
83
+ nextBody.input = directInput;
84
+ }
85
+ return nextBody;
86
+ }
87
+ rememberResponse(response, requestBody) {
88
+ if (!isRecord(response)) {
89
+ return;
90
+ }
91
+ let changed = false;
92
+ const responseId = typeof response.id === "string" ? response.id : undefined;
93
+ const output = Array.isArray(response.output)
94
+ ? response.output.filter(isRecord).map((item) => cloneValue(item))
95
+ : [];
96
+ for (const item of output) {
97
+ if (typeof item.id !== "string") {
98
+ continue;
99
+ }
100
+ this.items.delete(item.id);
101
+ this.items.set(item.id, item);
102
+ changed = true;
103
+ }
104
+ trimOldestEntries(this.items, MAX_ITEM_CACHE_SIZE);
105
+ if (responseId == null || requestBody == null) {
106
+ if (changed) {
107
+ this.emitChange();
108
+ }
109
+ return;
110
+ }
111
+ const input = Array.isArray(requestBody.input)
112
+ ? requestBody.input.map((item) => cloneValue(item))
113
+ : [];
114
+ this.responses.delete(responseId);
115
+ this.responses.set(responseId, {
116
+ input,
117
+ output,
118
+ });
119
+ changed = true;
120
+ trimOldestEntries(this.responses, MAX_RESPONSE_CACHE_SIZE);
121
+ if (changed) {
122
+ this.emitChange();
123
+ }
124
+ }
125
+ snapshot() {
126
+ return {
127
+ items: [...this.items.entries()].map(([id, item]) => ({
128
+ id,
129
+ item: cloneValue(item),
130
+ })),
131
+ responses: [...this.responses.entries()].map(([id, response]) => ({
132
+ id,
133
+ input: response.input.map((item) => cloneValue(item)),
134
+ output: response.output.map((item) => cloneValue(item)),
135
+ })),
136
+ };
137
+ }
138
+ expandInput(input) {
139
+ return input.map((item) => {
140
+ if (isRecord(item) &&
141
+ item.type === "item_reference" &&
142
+ typeof item.id === "string") {
143
+ const cachedItem = this.items.get(item.id);
144
+ if (cachedItem != null) {
145
+ return cloneValue(cachedItem);
146
+ }
147
+ }
148
+ return cloneValue(item);
149
+ });
150
+ }
151
+ emitChange() {
152
+ this.onChange?.(this.snapshot());
153
+ }
154
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@openai-oauth/core",
3
+ "version": "2.0.0-beta.0",
4
+ "description": "Core auth and in-memory OpenAI-compatible transport for openai-oauth.",
5
+ "keywords": [
6
+ "openai",
7
+ "oauth",
8
+ "chatgpt",
9
+ "codex",
10
+ "transport"
11
+ ],
12
+ "type": "module",
13
+ "sideEffects": false,
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "default": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.build.json",
30
+ "prepublishOnly": "bun run build",
31
+ "typecheck": "tsc --noEmit",
32
+ "test": "vitest run"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "@ai-sdk/provider-utils": "4.0.19"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "20.17.24",
42
+ "typescript": "latest",
43
+ "vitest": "latest"
44
+ },
45
+ "author": "EvanZhouDev",
46
+ "license": "AGPL-3.0-only",
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/EvanZhouDev/openai-oauth.git",
50
+ "directory": "packages/core"
51
+ },
52
+ "bugs": {
53
+ "url": "https://github.com/EvanZhouDev/openai-oauth/issues"
54
+ },
55
+ "homepage": "https://github.com/EvanZhouDev/openai-oauth#readme"
56
+ }