@wlv-zedd/dsh-chatgpt-web 1.0.0 → 1.0.2
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/LICENSE +21 -21
- package/README.md +140 -140
- package/cordis.patch.yml +4 -4
- package/lib/cli.js +13269 -11860
- package/lib/plugin.js +3 -50
- package/package.json +89 -88
- package/src/adapters/base.ts +16 -16
- package/src/adapters/chatgpt-web/adapter-error.ts +59 -59
- package/src/adapters/chatgpt-web/browser-helper-main.ts +513 -513
- package/src/adapters/chatgpt-web/browser-helper-prompt-selection.ts +27 -27
- package/src/adapters/chatgpt-web/browser-worker.ts +4944 -4944
- package/src/adapters/chatgpt-web/codex-rollout-environment.ts +628 -628
- package/src/adapters/chatgpt-web/compaction-handoff.ts +533 -533
- package/src/adapters/chatgpt-web/compaction-transaction.ts +142 -142
- package/src/adapters/chatgpt-web/concurrency.ts +6 -6
- package/src/adapters/chatgpt-web/conversation-key.ts +58 -58
- package/src/adapters/chatgpt-web/environment.ts +669 -669
- package/src/adapters/chatgpt-web/index.ts +1544 -1544
- package/src/adapters/chatgpt-web/input-tokens.ts +74 -74
- package/src/adapters/chatgpt-web/launcher-helper-client.ts +695 -695
- package/src/adapters/chatgpt-web/markdown.ts +418 -418
- package/src/adapters/chatgpt-web/mcp-main.ts +25 -25
- package/src/adapters/chatgpt-web/mcp-server.ts +933 -933
- package/src/adapters/chatgpt-web/model.ts +70 -70
- package/src/adapters/chatgpt-web/native-compaction-control.ts +74 -74
- package/src/adapters/chatgpt-web/output-validation.ts +62 -62
- package/src/adapters/chatgpt-web/process-line-writer.ts +46 -46
- package/src/adapters/chatgpt-web/prompt.ts +702 -702
- package/src/adapters/chatgpt-web/retry-policy.ts +73 -73
- package/src/adapters/chatgpt-web/rolling-checkpoint.ts +384 -384
- package/src/adapters/chatgpt-web/thread-environment.ts +238 -238
- package/src/adapters/chatgpt-web/tool-stream-parser.ts +1 -1
- package/src/adapters/chatgpt-web/turn-broker.ts +1481 -1481
- package/src/adapters/chatgpt-web/turn-execution.ts +816 -816
- package/src/adapters/chatgpt-web/turn-progress.ts +292 -292
- package/src/adapters/chatgpt-web/usage.ts +121 -121
- package/src/adapters/image.ts +9 -9
- package/src/bridge.ts +1083 -1083
- package/src/browser-login.ts +521 -521
- package/src/chatgpt-session.ts +240 -240
- package/src/chatgpt-web-models.ts +400 -400
- package/src/cli.ts +568 -568
- package/src/codex-integration-document.ts +824 -824
- package/src/codex-integration-journal.ts +212 -212
- package/src/codex-integration-route.ts +515 -515
- package/src/codex-integration-shared.ts +332 -332
- package/src/codex-integration.ts +529 -529
- package/src/codex-interrupt-hook.ts +158 -158
- package/src/config.ts +616 -616
- package/src/dev-chat/cli.ts +432 -432
- package/src/dev-chat/constants.ts +3 -3
- package/src/dev-chat/driver.ts +655 -655
- package/src/dev-chat/profile.ts +223 -223
- package/src/dev-chat/session.ts +287 -287
- package/src/dev-chat/transport.ts +54 -54
- package/src/doctor.ts +237 -237
- package/src/event-queue.ts +45 -45
- package/src/http-body.ts +30 -30
- package/src/launcher-browser-host.ts +695 -695
- package/src/lib/errors.ts +281 -281
- package/src/lib/token-estimate.ts +42 -42
- package/src/login-helper.cjs +1 -1
- package/src/model-catalog.ts +197 -197
- package/src/native-passthrough.ts +261 -261
- package/src/plugin.ts +191 -191
- package/src/process.ts +45 -45
- package/src/responses/compaction.ts +199 -199
- package/src/responses/parser.ts +633 -633
- package/src/responses/reasoning-envelope.ts +49 -49
- package/src/responses/schema.ts +172 -172
- package/src/responses/state.ts +230 -230
- package/src/server.ts +1111 -1111
- package/src/service.ts +315 -315
- package/src/setup.ts +671 -671
- package/src/stall-timeout.ts +23 -23
- package/src/tunnel-service.ts +160 -160
- package/src/tunnel.ts +417 -417
- package/src/turndown-plugin-gfm.d.ts +5 -5
- package/src/types.ts +307 -307
- package/src/usage/totals.ts +12 -12
- package/src/version.ts +1 -1
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
|
|
3
|
-
export interface CompactionTransactionHandle {
|
|
4
|
-
token: string;
|
|
5
|
-
handoffId: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface TransactionWaiter {
|
|
9
|
-
resolve: (summary: string) => void;
|
|
10
|
-
reject: (error: Error) => void;
|
|
11
|
-
signal?: AbortSignal;
|
|
12
|
-
onAbort?: () => void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface CompactionTransaction extends CompactionTransactionHandle {
|
|
16
|
-
traceId: string;
|
|
17
|
-
summary?: string;
|
|
18
|
-
waiter?: TransactionWaiter;
|
|
19
|
-
timer?: ReturnType<typeof setTimeout>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function opaqueId(prefix: "control" | "handoff"): string {
|
|
23
|
-
return `${prefix}_${randomBytes(16).toString("hex")}`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** One-shot capability store for the summary only; it never owns a Codex tool environment. */
|
|
27
|
-
export class CompactionTransactionStore {
|
|
28
|
-
private readonly transactions = new Map<string, CompactionTransaction>();
|
|
29
|
-
|
|
30
|
-
begin(traceId: string, ttlMs: number): CompactionTransactionHandle {
|
|
31
|
-
if (!traceId.trim()) throw new Error("compaction transaction trace id is required");
|
|
32
|
-
if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
|
|
33
|
-
throw new Error("compaction transaction TTL must be a positive finite number");
|
|
34
|
-
}
|
|
35
|
-
const transaction: CompactionTransaction = {
|
|
36
|
-
token: opaqueId("control"),
|
|
37
|
-
handoffId: opaqueId("handoff"),
|
|
38
|
-
traceId,
|
|
39
|
-
};
|
|
40
|
-
transaction.timer = setTimeout(() => {
|
|
41
|
-
this.finishError(transaction, new Error("compaction transaction timed out"));
|
|
42
|
-
}, ttlMs);
|
|
43
|
-
transaction.timer.unref?.();
|
|
44
|
-
this.transactions.set(transaction.token, transaction);
|
|
45
|
-
return { token: transaction.token, handoffId: transaction.handoffId };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
submit(token: string, handoffId: string, summary: string): void {
|
|
49
|
-
const transaction = this.transactions.get(token);
|
|
50
|
-
if (!transaction) throw new Error("compaction control token is invalid, expired, or consumed");
|
|
51
|
-
if (transaction.summary !== undefined) throw new Error("compaction handoff was already submitted");
|
|
52
|
-
if (handoffId !== transaction.handoffId) {
|
|
53
|
-
throw new Error("compaction handoff id does not match the pending transaction");
|
|
54
|
-
}
|
|
55
|
-
const normalized = summary.trim();
|
|
56
|
-
if (!normalized) throw new Error("compaction handoff summary is empty");
|
|
57
|
-
transaction.summary = normalized;
|
|
58
|
-
console.info(`[chatgpt-web] broker trace=${transaction.traceId} accepted structured compaction handoff`);
|
|
59
|
-
if (transaction.timer) clearTimeout(transaction.timer);
|
|
60
|
-
transaction.timer = undefined;
|
|
61
|
-
if (transaction.waiter) this.consume(transaction);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
wait(token: string, signal?: AbortSignal): Promise<string> {
|
|
65
|
-
const transaction = this.transactions.get(token);
|
|
66
|
-
if (!transaction) return Promise.reject(new Error("compaction control token is invalid, expired, or consumed"));
|
|
67
|
-
if (transaction.waiter) return Promise.reject(new Error("compaction transaction already has a waiter"));
|
|
68
|
-
if (transaction.summary !== undefined) return Promise.resolve(this.consume(transaction));
|
|
69
|
-
if (signal?.aborted) {
|
|
70
|
-
const error = new DOMException("compaction transaction aborted", "AbortError");
|
|
71
|
-
this.finishError(transaction, error);
|
|
72
|
-
return Promise.reject(error);
|
|
73
|
-
}
|
|
74
|
-
return new Promise<string>((resolve, reject) => {
|
|
75
|
-
const waiter: TransactionWaiter = { resolve, reject, ...(signal ? { signal } : {}) };
|
|
76
|
-
if (signal) {
|
|
77
|
-
waiter.onAbort = () => this.finishError(
|
|
78
|
-
transaction,
|
|
79
|
-
new DOMException("compaction transaction aborted", "AbortError"),
|
|
80
|
-
);
|
|
81
|
-
signal.addEventListener("abort", waiter.onAbort, { once: true });
|
|
82
|
-
}
|
|
83
|
-
transaction.waiter = waiter;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
abort(token: string): void {
|
|
88
|
-
const transaction = this.transactions.get(token);
|
|
89
|
-
if (!transaction) return;
|
|
90
|
-
if (transaction.summary !== undefined) {
|
|
91
|
-
this.transactions.delete(token);
|
|
92
|
-
if (transaction.timer) clearTimeout(transaction.timer);
|
|
93
|
-
transaction.timer = undefined;
|
|
94
|
-
this.detachWaiter(transaction);
|
|
95
|
-
transaction.waiter = undefined;
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
this.finishError(transaction, new Error("compaction transaction aborted"));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
abortTrace(traceId: string): void {
|
|
102
|
-
for (const transaction of [...this.transactions.values()]) {
|
|
103
|
-
if (transaction.traceId === traceId && transaction.summary === undefined) {
|
|
104
|
-
this.finishError(transaction, new Error("compaction transaction was revoked"));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
close(): void {
|
|
110
|
-
for (const transaction of [...this.transactions.values()]) {
|
|
111
|
-
this.finishError(transaction, new Error("compaction transaction broker closed"));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private consume(transaction: CompactionTransaction): string {
|
|
116
|
-
if (transaction.summary === undefined) throw new Error("compaction transaction is not ready");
|
|
117
|
-
const summary = transaction.summary;
|
|
118
|
-
const waiter = transaction.waiter;
|
|
119
|
-
this.transactions.delete(transaction.token);
|
|
120
|
-
this.detachWaiter(transaction);
|
|
121
|
-
transaction.waiter = undefined;
|
|
122
|
-
waiter?.resolve(summary);
|
|
123
|
-
return summary;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private finishError(transaction: CompactionTransaction, error: Error): void {
|
|
127
|
-
if (!this.transactions.delete(transaction.token)) return;
|
|
128
|
-
if (transaction.timer) clearTimeout(transaction.timer);
|
|
129
|
-
transaction.timer = undefined;
|
|
130
|
-
const waiter = transaction.waiter;
|
|
131
|
-
this.detachWaiter(transaction);
|
|
132
|
-
transaction.waiter = undefined;
|
|
133
|
-
waiter?.reject(error);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private detachWaiter(transaction: CompactionTransaction): void {
|
|
137
|
-
const waiter = transaction.waiter;
|
|
138
|
-
if (waiter?.signal && waiter.onAbort) {
|
|
139
|
-
waiter.signal.removeEventListener("abort", waiter.onAbort);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
export interface CompactionTransactionHandle {
|
|
4
|
+
token: string;
|
|
5
|
+
handoffId: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface TransactionWaiter {
|
|
9
|
+
resolve: (summary: string) => void;
|
|
10
|
+
reject: (error: Error) => void;
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
onAbort?: () => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface CompactionTransaction extends CompactionTransactionHandle {
|
|
16
|
+
traceId: string;
|
|
17
|
+
summary?: string;
|
|
18
|
+
waiter?: TransactionWaiter;
|
|
19
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function opaqueId(prefix: "control" | "handoff"): string {
|
|
23
|
+
return `${prefix}_${randomBytes(16).toString("hex")}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One-shot capability store for the summary only; it never owns a Codex tool environment. */
|
|
27
|
+
export class CompactionTransactionStore {
|
|
28
|
+
private readonly transactions = new Map<string, CompactionTransaction>();
|
|
29
|
+
|
|
30
|
+
begin(traceId: string, ttlMs: number): CompactionTransactionHandle {
|
|
31
|
+
if (!traceId.trim()) throw new Error("compaction transaction trace id is required");
|
|
32
|
+
if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
|
|
33
|
+
throw new Error("compaction transaction TTL must be a positive finite number");
|
|
34
|
+
}
|
|
35
|
+
const transaction: CompactionTransaction = {
|
|
36
|
+
token: opaqueId("control"),
|
|
37
|
+
handoffId: opaqueId("handoff"),
|
|
38
|
+
traceId,
|
|
39
|
+
};
|
|
40
|
+
transaction.timer = setTimeout(() => {
|
|
41
|
+
this.finishError(transaction, new Error("compaction transaction timed out"));
|
|
42
|
+
}, ttlMs);
|
|
43
|
+
transaction.timer.unref?.();
|
|
44
|
+
this.transactions.set(transaction.token, transaction);
|
|
45
|
+
return { token: transaction.token, handoffId: transaction.handoffId };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
submit(token: string, handoffId: string, summary: string): void {
|
|
49
|
+
const transaction = this.transactions.get(token);
|
|
50
|
+
if (!transaction) throw new Error("compaction control token is invalid, expired, or consumed");
|
|
51
|
+
if (transaction.summary !== undefined) throw new Error("compaction handoff was already submitted");
|
|
52
|
+
if (handoffId !== transaction.handoffId) {
|
|
53
|
+
throw new Error("compaction handoff id does not match the pending transaction");
|
|
54
|
+
}
|
|
55
|
+
const normalized = summary.trim();
|
|
56
|
+
if (!normalized) throw new Error("compaction handoff summary is empty");
|
|
57
|
+
transaction.summary = normalized;
|
|
58
|
+
console.info(`[chatgpt-web] broker trace=${transaction.traceId} accepted structured compaction handoff`);
|
|
59
|
+
if (transaction.timer) clearTimeout(transaction.timer);
|
|
60
|
+
transaction.timer = undefined;
|
|
61
|
+
if (transaction.waiter) this.consume(transaction);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
wait(token: string, signal?: AbortSignal): Promise<string> {
|
|
65
|
+
const transaction = this.transactions.get(token);
|
|
66
|
+
if (!transaction) return Promise.reject(new Error("compaction control token is invalid, expired, or consumed"));
|
|
67
|
+
if (transaction.waiter) return Promise.reject(new Error("compaction transaction already has a waiter"));
|
|
68
|
+
if (transaction.summary !== undefined) return Promise.resolve(this.consume(transaction));
|
|
69
|
+
if (signal?.aborted) {
|
|
70
|
+
const error = new DOMException("compaction transaction aborted", "AbortError");
|
|
71
|
+
this.finishError(transaction, error);
|
|
72
|
+
return Promise.reject(error);
|
|
73
|
+
}
|
|
74
|
+
return new Promise<string>((resolve, reject) => {
|
|
75
|
+
const waiter: TransactionWaiter = { resolve, reject, ...(signal ? { signal } : {}) };
|
|
76
|
+
if (signal) {
|
|
77
|
+
waiter.onAbort = () => this.finishError(
|
|
78
|
+
transaction,
|
|
79
|
+
new DOMException("compaction transaction aborted", "AbortError"),
|
|
80
|
+
);
|
|
81
|
+
signal.addEventListener("abort", waiter.onAbort, { once: true });
|
|
82
|
+
}
|
|
83
|
+
transaction.waiter = waiter;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
abort(token: string): void {
|
|
88
|
+
const transaction = this.transactions.get(token);
|
|
89
|
+
if (!transaction) return;
|
|
90
|
+
if (transaction.summary !== undefined) {
|
|
91
|
+
this.transactions.delete(token);
|
|
92
|
+
if (transaction.timer) clearTimeout(transaction.timer);
|
|
93
|
+
transaction.timer = undefined;
|
|
94
|
+
this.detachWaiter(transaction);
|
|
95
|
+
transaction.waiter = undefined;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
this.finishError(transaction, new Error("compaction transaction aborted"));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
abortTrace(traceId: string): void {
|
|
102
|
+
for (const transaction of [...this.transactions.values()]) {
|
|
103
|
+
if (transaction.traceId === traceId && transaction.summary === undefined) {
|
|
104
|
+
this.finishError(transaction, new Error("compaction transaction was revoked"));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
close(): void {
|
|
110
|
+
for (const transaction of [...this.transactions.values()]) {
|
|
111
|
+
this.finishError(transaction, new Error("compaction transaction broker closed"));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private consume(transaction: CompactionTransaction): string {
|
|
116
|
+
if (transaction.summary === undefined) throw new Error("compaction transaction is not ready");
|
|
117
|
+
const summary = transaction.summary;
|
|
118
|
+
const waiter = transaction.waiter;
|
|
119
|
+
this.transactions.delete(transaction.token);
|
|
120
|
+
this.detachWaiter(transaction);
|
|
121
|
+
transaction.waiter = undefined;
|
|
122
|
+
waiter?.resolve(summary);
|
|
123
|
+
return summary;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private finishError(transaction: CompactionTransaction, error: Error): void {
|
|
127
|
+
if (!this.transactions.delete(transaction.token)) return;
|
|
128
|
+
if (transaction.timer) clearTimeout(transaction.timer);
|
|
129
|
+
transaction.timer = undefined;
|
|
130
|
+
const waiter = transaction.waiter;
|
|
131
|
+
this.detachWaiter(transaction);
|
|
132
|
+
transaction.waiter = undefined;
|
|
133
|
+
waiter?.reject(error);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private detachWaiter(transaction: CompactionTransaction): void {
|
|
137
|
+
const waiter = transaction.waiter;
|
|
138
|
+
if (waiter?.signal && waiter.onAbort) {
|
|
139
|
+
waiter.signal.removeEventListener("abort", waiter.onAbort);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ChatGPT Web concurrency is deliberately bounded. Every active Codex turn owns a real
|
|
3
|
-
* browser document in the signed-in account, so unbounded fan-out would create account-level
|
|
4
|
-
* traffic that is indistinguishable from spam.
|
|
5
|
-
*/
|
|
6
|
-
export const MAX_CHATGPT_BROWSER_TABS = 5;
|
|
1
|
+
/**
|
|
2
|
+
* ChatGPT Web concurrency is deliberately bounded. Every active Codex turn owns a real
|
|
3
|
+
* browser document in the signed-in account, so unbounded fan-out would create account-level
|
|
4
|
+
* traffic that is indistinguishable from spam.
|
|
5
|
+
*/
|
|
6
|
+
export const MAX_CHATGPT_BROWSER_TABS = 5;
|
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { SUMMARY_PREFIX } from "../../responses/compaction";
|
|
3
|
-
import type { CodexParsedRequest } from "../../types";
|
|
4
|
-
import { extractChatGptTurnIdentity } from "./environment";
|
|
5
|
-
|
|
6
|
-
function messageText(item: Record<string, unknown>): string | undefined {
|
|
7
|
-
const content = item.content;
|
|
8
|
-
if (typeof content === "string") return content;
|
|
9
|
-
if (!Array.isArray(content)) return undefined;
|
|
10
|
-
return content.flatMap(block => {
|
|
11
|
-
if (!block || typeof block !== "object" || Array.isArray(block)) return [];
|
|
12
|
-
const text = (block as { text?: unknown }).text;
|
|
13
|
-
return typeof text === "string" ? [text] : [];
|
|
14
|
-
}).join("\n");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Native compaction remains part of the exact identity of a replayed Codex turn. */
|
|
18
|
-
function compactionEpoch(input: unknown[] | undefined): unknown {
|
|
19
|
-
return input?.findLast(item => {
|
|
20
|
-
if (!item || typeof item !== "object" || Array.isArray(item)) return false;
|
|
21
|
-
const record = item as Record<string, unknown>;
|
|
22
|
-
return record.type === "compaction"
|
|
23
|
-
|| record.type === "compaction_summary"
|
|
24
|
-
|| record.type === "context_compaction"
|
|
25
|
-
|| (record.role === "user" && messageText(record)?.startsWith(`${SUMMARY_PREFIX}\n`));
|
|
26
|
-
}) ?? null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function chatGptConversationKey(
|
|
30
|
-
parsed: CodexParsedRequest,
|
|
31
|
-
namespace: string,
|
|
32
|
-
): string | undefined {
|
|
33
|
-
const identity = extractChatGptTurnIdentity(parsed);
|
|
34
|
-
if (!identity.threadId) return undefined;
|
|
35
|
-
const raw = parsed._rawBody as { input?: unknown[] } | undefined;
|
|
36
|
-
return createHash("sha256").update(JSON.stringify({
|
|
37
|
-
namespace,
|
|
38
|
-
threadId: identity.threadId,
|
|
39
|
-
modelId: parsed.modelId,
|
|
40
|
-
reasoning: parsed.options.reasoning,
|
|
41
|
-
compaction: compactionEpoch(raw?.input),
|
|
42
|
-
})).digest("hex");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Full history remains canonical; a retained epoch receives only the suffix after its last assistant reply. */
|
|
46
|
-
export function retainedConversationResumeRequest(
|
|
47
|
-
parsed: CodexParsedRequest,
|
|
48
|
-
): CodexParsedRequest | undefined {
|
|
49
|
-
const lastAssistant = parsed.context.messages.findLastIndex(message => message.role === "assistant");
|
|
50
|
-
if (lastAssistant < 0 || lastAssistant === parsed.context.messages.length - 1) return undefined;
|
|
51
|
-
return {
|
|
52
|
-
...parsed,
|
|
53
|
-
context: {
|
|
54
|
-
...parsed.context,
|
|
55
|
-
messages: parsed.context.messages.slice(lastAssistant + 1),
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
}
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { SUMMARY_PREFIX } from "../../responses/compaction";
|
|
3
|
+
import type { CodexParsedRequest } from "../../types";
|
|
4
|
+
import { extractChatGptTurnIdentity } from "./environment";
|
|
5
|
+
|
|
6
|
+
function messageText(item: Record<string, unknown>): string | undefined {
|
|
7
|
+
const content = item.content;
|
|
8
|
+
if (typeof content === "string") return content;
|
|
9
|
+
if (!Array.isArray(content)) return undefined;
|
|
10
|
+
return content.flatMap(block => {
|
|
11
|
+
if (!block || typeof block !== "object" || Array.isArray(block)) return [];
|
|
12
|
+
const text = (block as { text?: unknown }).text;
|
|
13
|
+
return typeof text === "string" ? [text] : [];
|
|
14
|
+
}).join("\n");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Native compaction remains part of the exact identity of a replayed Codex turn. */
|
|
18
|
+
function compactionEpoch(input: unknown[] | undefined): unknown {
|
|
19
|
+
return input?.findLast(item => {
|
|
20
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) return false;
|
|
21
|
+
const record = item as Record<string, unknown>;
|
|
22
|
+
return record.type === "compaction"
|
|
23
|
+
|| record.type === "compaction_summary"
|
|
24
|
+
|| record.type === "context_compaction"
|
|
25
|
+
|| (record.role === "user" && messageText(record)?.startsWith(`${SUMMARY_PREFIX}\n`));
|
|
26
|
+
}) ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function chatGptConversationKey(
|
|
30
|
+
parsed: CodexParsedRequest,
|
|
31
|
+
namespace: string,
|
|
32
|
+
): string | undefined {
|
|
33
|
+
const identity = extractChatGptTurnIdentity(parsed);
|
|
34
|
+
if (!identity.threadId) return undefined;
|
|
35
|
+
const raw = parsed._rawBody as { input?: unknown[] } | undefined;
|
|
36
|
+
return createHash("sha256").update(JSON.stringify({
|
|
37
|
+
namespace,
|
|
38
|
+
threadId: identity.threadId,
|
|
39
|
+
modelId: parsed.modelId,
|
|
40
|
+
reasoning: parsed.options.reasoning,
|
|
41
|
+
compaction: compactionEpoch(raw?.input),
|
|
42
|
+
})).digest("hex");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Full history remains canonical; a retained epoch receives only the suffix after its last assistant reply. */
|
|
46
|
+
export function retainedConversationResumeRequest(
|
|
47
|
+
parsed: CodexParsedRequest,
|
|
48
|
+
): CodexParsedRequest | undefined {
|
|
49
|
+
const lastAssistant = parsed.context.messages.findLastIndex(message => message.role === "assistant");
|
|
50
|
+
if (lastAssistant < 0 || lastAssistant === parsed.context.messages.length - 1) return undefined;
|
|
51
|
+
return {
|
|
52
|
+
...parsed,
|
|
53
|
+
context: {
|
|
54
|
+
...parsed.context,
|
|
55
|
+
messages: parsed.context.messages.slice(lastAssistant + 1),
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|