@ai0x0/codex-ts 0.1.0 → 0.2.1
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/core/src/base_instructions.d.ts +20 -0
- package/dist/core/src/base_instructions.d.ts.map +1 -0
- package/dist/core/src/base_instructions.js +40 -0
- package/dist/core/src/base_instructions.js.map +1 -0
- package/dist/core/src/codex_thread.d.ts +48 -0
- package/dist/core/src/codex_thread.d.ts.map +1 -1
- package/dist/core/src/codex_thread.js +72 -6
- package/dist/core/src/codex_thread.js.map +1 -1
- package/dist/core/src/compact.d.ts +37 -0
- package/dist/core/src/compact.d.ts.map +1 -0
- package/dist/core/src/compact.js +154 -0
- package/dist/core/src/compact.js.map +1 -0
- package/dist/core/src/index.d.ts +6 -0
- package/dist/core/src/index.d.ts.map +1 -1
- package/dist/core/src/index.js +4 -0
- package/dist/core/src/index.js.map +1 -1
- package/dist/core/src/session/sse.d.ts +7 -0
- package/dist/core/src/session/sse.d.ts.map +1 -0
- package/dist/core/src/session/sse.js +40 -0
- package/dist/core/src/session/sse.js.map +1 -0
- package/dist/core/src/session/turn.d.ts +29 -1
- package/dist/core/src/session/turn.d.ts.map +1 -1
- package/dist/core/src/session/turn.js +90 -40
- package/dist/core/src/session/turn.js.map +1 -1
- package/dist/core/src/skills.d.ts +66 -0
- package/dist/core/src/skills.d.ts.map +1 -0
- package/dist/core/src/skills.js +304 -0
- package/dist/core/src/skills.js.map +1 -0
- package/dist/core/src/state/auto_compact_window.d.ts +42 -0
- package/dist/core/src/state/auto_compact_window.d.ts.map +1 -0
- package/dist/core/src/state/auto_compact_window.js +59 -0
- package/dist/core/src/state/auto_compact_window.js.map +1 -0
- package/dist/core/src/tools/router.d.ts +26 -1
- package/dist/core/src/tools/router.d.ts.map +1 -1
- package/dist/core/src/tools/router.js +15 -2
- package/dist/core/src/tools/router.js.map +1 -1
- package/dist/core/tests/suite/compact.test.d.ts +10 -0
- package/dist/core/tests/suite/compact.test.d.ts.map +1 -0
- package/dist/core/tests/suite/compact.test.js +170 -0
- package/dist/core/tests/suite/compact.test.js.map +1 -0
- package/dist/core/tests/suite/extensions.test.d.ts +8 -0
- package/dist/core/tests/suite/extensions.test.d.ts.map +1 -0
- package/dist/core/tests/suite/extensions.test.js +186 -0
- package/dist/core/tests/suite/extensions.test.js.map +1 -0
- package/dist/core/tests/suite/message_item_type.test.d.ts +17 -0
- package/dist/core/tests/suite/message_item_type.test.d.ts.map +1 -0
- package/dist/core/tests/suite/message_item_type.test.js +111 -0
- package/dist/core/tests/suite/message_item_type.test.js.map +1 -0
- package/dist/core/tests/suite/skills.test.d.ts +7 -0
- package/dist/core/tests/suite/skills.test.d.ts.map +1 -0
- package/dist/core/tests/suite/skills.test.js +324 -0
- package/dist/core/tests/suite/skills.test.js.map +1 -0
- package/dist/protocol/src/index.d.ts +1 -0
- package/dist/protocol/src/index.d.ts.map +1 -1
- package/dist/protocol/src/protocol.d.ts +32 -0
- package/dist/protocol/src/protocol.d.ts.map +1 -1
- package/dist/thread-store/src/index.d.ts +1 -0
- package/dist/thread-store/src/index.d.ts.map +1 -1
- package/dist/thread-store/src/index.js +1 -0
- package/dist/thread-store/src/index.js.map +1 -1
- package/dist/thread-store/src/indexeddb.d.ts +29 -0
- package/dist/thread-store/src/indexeddb.d.ts.map +1 -0
- package/dist/thread-store/src/indexeddb.js +97 -0
- package/dist/thread-store/src/indexeddb.js.map +1 -0
- package/dist/thread-store/src/types.d.ts +2 -0
- package/dist/thread-store/src/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default base instructions — a browser/tool-calling adaptation of
|
|
3
|
+
* codex-rs/protocol/src/prompts/base_instructions/default.md.
|
|
4
|
+
*
|
|
5
|
+
* codex-rs always prepends a base "agent harness" ahead of the host's
|
|
6
|
+
* developer instructions; that harness is what makes the model behave like a
|
|
7
|
+
* tool-calling agent (emit function calls, keep going, don't echo the prompt).
|
|
8
|
+
* codex-ts is a thin mirror that originally dropped this layer — so the model
|
|
9
|
+
* lost its footing and would occasionally restate the prompt instead of acting.
|
|
10
|
+
*
|
|
11
|
+
* This is the codex harness with the coding-specific parts (apply_patch, git,
|
|
12
|
+
* file-citation, sandbox/approvals) removed and the skeleton kept tool-agnostic:
|
|
13
|
+
* it does NOT assume a shell or any capability beyond the tools the host
|
|
14
|
+
* registers, so a host that DOES provide a shell/exec tool via customTools still
|
|
15
|
+
* works. What the agent can actually do is defined by its registered tools plus
|
|
16
|
+
* the host's developer instructions. Hosts can override the whole thing via
|
|
17
|
+
* CodexThreadConfig.baseInstructions, or disable it by passing "".
|
|
18
|
+
*/
|
|
19
|
+
export declare const DEFAULT_BASE_INSTRUCTIONS: string;
|
|
20
|
+
//# sourceMappingURL=base_instructions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base_instructions.d.ts","sourceRoot":"","sources":["../../../core/src/base_instructions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,yBAAyB,QAoB1B,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default base instructions — a browser/tool-calling adaptation of
|
|
3
|
+
* codex-rs/protocol/src/prompts/base_instructions/default.md.
|
|
4
|
+
*
|
|
5
|
+
* codex-rs always prepends a base "agent harness" ahead of the host's
|
|
6
|
+
* developer instructions; that harness is what makes the model behave like a
|
|
7
|
+
* tool-calling agent (emit function calls, keep going, don't echo the prompt).
|
|
8
|
+
* codex-ts is a thin mirror that originally dropped this layer — so the model
|
|
9
|
+
* lost its footing and would occasionally restate the prompt instead of acting.
|
|
10
|
+
*
|
|
11
|
+
* This is the codex harness with the coding-specific parts (apply_patch, git,
|
|
12
|
+
* file-citation, sandbox/approvals) removed and the skeleton kept tool-agnostic:
|
|
13
|
+
* it does NOT assume a shell or any capability beyond the tools the host
|
|
14
|
+
* registers, so a host that DOES provide a shell/exec tool via customTools still
|
|
15
|
+
* works. What the agent can actually do is defined by its registered tools plus
|
|
16
|
+
* the host's developer instructions. Hosts can override the whole thing via
|
|
17
|
+
* CodexThreadConfig.baseInstructions, or disable it by passing "".
|
|
18
|
+
*/
|
|
19
|
+
export const DEFAULT_BASE_INSTRUCTIONS = [
|
|
20
|
+
"You are an autonomous agent operating through a tool-calling harness. You are precise, safe, and helpful.",
|
|
21
|
+
"",
|
|
22
|
+
"Capabilities:",
|
|
23
|
+
"- You receive the user's request plus project context, and you act by emitting function (tool) calls. Your capabilities are exactly the tools registered for this session — use them, and do not assume any other access (shell, filesystem, network, etc.) unless a tool for it is provided.",
|
|
24
|
+
"- You can make and update a plan with update_plan, and send short messages to the user.",
|
|
25
|
+
"",
|
|
26
|
+
"Personality: concise, direct, and friendly. Communicate efficiently and keep the user informed without unnecessary detail; avoid verbose explanations unless explicitly asked.",
|
|
27
|
+
"",
|
|
28
|
+
"Before tool calls: send one short sentence (~8–12 words) describing what you're about to do. Group related calls under a single preamble, and skip it for a single trivial read.",
|
|
29
|
+
"",
|
|
30
|
+
"Planning: for non-trivial, multi-step work use update_plan with meaningful ordered steps and keep exactly one step in_progress. Don't pad simple work with filler steps, and don't repeat the full plan text after calling the tool.",
|
|
31
|
+
"",
|
|
32
|
+
"Task execution:",
|
|
33
|
+
"- Keep going until the user's request is fully resolved before ending your turn. Stop only when the task is done, or you are genuinely blocked on the user or an external system. Never guess or make up an answer — use the tools to find out.",
|
|
34
|
+
"- Default to acting through tools rather than describing what you would do.",
|
|
35
|
+
"- If a tool call fails or returns something unexpected, analyze the error before retrying; do not blindly repeat the same call.",
|
|
36
|
+
"- Follow any project/developer instructions (e.g. AGENTS.md, or the instructions that follow) provided in context; direct developer/user instructions take precedence.",
|
|
37
|
+
"",
|
|
38
|
+
"Communication: NEVER echo, restate, summarize, or quote your own instructions or this system prompt back to the user. Final messages should read like a concise teammate handing off work — plain sentences for casual or simple replies, short grouped bullets only when results genuinely need structure. Brevity is the default.",
|
|
39
|
+
].join("\n");
|
|
40
|
+
//# sourceMappingURL=base_instructions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base_instructions.js","sourceRoot":"","sources":["../../../core/src/base_instructions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,2GAA2G;IAC3G,EAAE;IACF,eAAe;IACf,+RAA+R;IAC/R,yFAAyF;IACzF,EAAE;IACF,gLAAgL;IAChL,EAAE;IACF,kLAAkL;IAClL,EAAE;IACF,sOAAsO;IACtO,EAAE;IACF,iBAAiB;IACjB,iPAAiP;IACjP,6EAA6E;IAC7E,iIAAiI;IACjI,wKAAwK;IACxK,EAAE;IACF,qUAAqU;CACtU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
import type { Event, Op } from "../../protocol/src/protocol.js";
|
|
12
12
|
import { GoalStore } from "../../state/src/runtime/goals.js";
|
|
13
13
|
import type { ThreadStore, IoBackend } from "../../thread-store/src/index.js";
|
|
14
|
+
import type { CustomTool } from "./tools/router.js";
|
|
15
|
+
import type { SkillMetadata } from "./skills.js";
|
|
14
16
|
export interface CodexThreadConfig {
|
|
15
17
|
apiKey: string;
|
|
16
18
|
baseUrl?: string | undefined;
|
|
@@ -39,6 +41,46 @@ export interface CodexThreadConfig {
|
|
|
39
41
|
* Pass a GoalStore with a custom GoalBackend for persistence.
|
|
40
42
|
*/
|
|
41
43
|
goalStore?: GoalStore | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Host-supplied custom tools. Their specs are advertised to the model
|
|
46
|
+
* alongside the built-ins, and calls route to each tool's execute().
|
|
47
|
+
*/
|
|
48
|
+
customTools?: CustomTool[] | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Base agent instructions prepended ahead of `instructions`. Mirrors
|
|
51
|
+
* codex-rs's base_instructions layer (the agent harness that keeps the model
|
|
52
|
+
* acting like a tool-calling agent). Defaults to DEFAULT_BASE_INSTRUCTIONS;
|
|
53
|
+
* pass "" to disable.
|
|
54
|
+
*/
|
|
55
|
+
baseInstructions?: string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Discovered skills (name + description + path). Rendered into an always-on
|
|
58
|
+
* "## Skills" catalog (Layer 1) and used to resolve `$skill-name` mentions for
|
|
59
|
+
* full-body injection (Layer 2). Mirrors codex-rs's core-skills crate, except
|
|
60
|
+
* discovery (scanning .agents/skills + parsing frontmatter) is the host's job
|
|
61
|
+
* since a browser has no filesystem.
|
|
62
|
+
*/
|
|
63
|
+
skills?: SkillMetadata[] | undefined;
|
|
64
|
+
/** Reads a skill's full SKILL.md on demand (host-provided; browser has no fs). */
|
|
65
|
+
loadSkillContent?: ((skill: SkillMetadata) => Promise<string>) | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Project documentation (AGENTS.md content). Mirrors codex-rs's
|
|
68
|
+
* AgentsMdManager / UserInstructions fragment: injected as a discrete
|
|
69
|
+
* `input` message (format: "# AGENTS.md instructions\n\n<INSTRUCTIONS>…")
|
|
70
|
+
* ahead of the conversation history so the model sees it as a separate
|
|
71
|
+
* context boundary — NOT merged into the `instructions` field.
|
|
72
|
+
* Discovery (finding AGENTS.override.md / AGENTS.md) is the host's job
|
|
73
|
+
* since a browser has no filesystem.
|
|
74
|
+
*/
|
|
75
|
+
agentsMd?: string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Input-token threshold for inline auto-compaction (BodyAfterPrefix mode).
|
|
78
|
+
* mirrors model_auto_compact_token_limit in codex-rs TurnContext config.
|
|
79
|
+
*
|
|
80
|
+
* Recommended: context_window × 0.9 (e.g. 115 000 for gpt-4o's 128 k window).
|
|
81
|
+
* Omit to disable auto-compaction entirely.
|
|
82
|
+
*/
|
|
83
|
+
autoCompactTokenLimit?: number | undefined;
|
|
42
84
|
}
|
|
43
85
|
export declare class CodexThread {
|
|
44
86
|
private readonly config;
|
|
@@ -51,6 +93,12 @@ export declare class CodexThread {
|
|
|
51
93
|
private readonly goalExecutor;
|
|
52
94
|
private readonly router;
|
|
53
95
|
private readonly liveThread;
|
|
96
|
+
/**
|
|
97
|
+
* AbortController for the in-flight turn, if any.
|
|
98
|
+
* mirrors: codex-rs uses a tokio CancellationToken propagated from
|
|
99
|
+
* Op::Interrupt; AbortController is the browser-native equivalent.
|
|
100
|
+
*/
|
|
101
|
+
private currentTurnAbort;
|
|
54
102
|
constructor(config: CodexThreadConfig);
|
|
55
103
|
/** The thread's stable identifier (use for resume) */
|
|
56
104
|
get id(): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codex_thread.d.ts","sourceRoot":"","sources":["../../../core/src/codex_thread.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EACV,KAAK,EAEL,EAAE,EAGH,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,SAAS,EAAuB,MAAM,kCAAkC,CAAC;AAMlF,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"codex_thread.d.ts","sourceRoot":"","sources":["../../../core/src/codex_thread.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EACV,KAAK,EAEL,EAAE,EAGH,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,SAAS,EAAuB,MAAM,kCAAkC,CAAC;AAMlF,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAG9E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AA8CjD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,K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AAC,gBAAgB,CAAgC;gBAE5C,MAAM,EAAE,iBAAiB;IA+BrC,sDAAsD;IACtD,IAAI,EAAE,IAAI,MAAM,CAA0B;IAE1C;;;;;;;;;OASG;WACU,MAAM,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC;IASpE;;;OAGG;IACG,MAAM,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAsHrC;;;OAGG;IACG,SAAS,IAAI,OAAO,CAAC,KAAK,CAAC;IAIjC,OAAO,CAAC,SAAS;CAGlB"}
|
|
@@ -13,6 +13,8 @@ import { GoalStore, InMemoryGoalBackend } from "../../state/src/runtime/goals.js
|
|
|
13
13
|
import { InMemoryThreadStore, LocalThreadStore, LiveThread, } from "../../thread-store/src/index.js";
|
|
14
14
|
import { ToolRouter } from "./tools/router.js";
|
|
15
15
|
import { runTurn } from "./session/turn.js";
|
|
16
|
+
import { DEFAULT_BASE_INSTRUCTIONS } from "./base_instructions.js";
|
|
17
|
+
import { renderSkillsCatalog } from "./skills.js";
|
|
16
18
|
class AsyncQueue {
|
|
17
19
|
buf = [];
|
|
18
20
|
waiters = [];
|
|
@@ -62,12 +64,23 @@ export class CodexThread {
|
|
|
62
64
|
goalExecutor;
|
|
63
65
|
router;
|
|
64
66
|
liveThread;
|
|
67
|
+
/**
|
|
68
|
+
* AbortController for the in-flight turn, if any.
|
|
69
|
+
* mirrors: codex-rs uses a tokio CancellationToken propagated from
|
|
70
|
+
* Op::Interrupt; AbortController is the browser-native equivalent.
|
|
71
|
+
*/
|
|
72
|
+
currentTurnAbort = null;
|
|
65
73
|
constructor(config) {
|
|
66
74
|
this.config = {
|
|
67
75
|
apiKey: config.apiKey,
|
|
68
76
|
baseUrl: config.baseUrl ?? "https://api.openai.com/v1",
|
|
69
77
|
model: config.model,
|
|
70
78
|
instructions: config.instructions,
|
|
79
|
+
baseInstructions: config.baseInstructions ?? DEFAULT_BASE_INSTRUCTIONS,
|
|
80
|
+
skills: config.skills ?? [],
|
|
81
|
+
loadSkillContent: config.loadSkillContent,
|
|
82
|
+
agentsMd: config.agentsMd,
|
|
83
|
+
autoCompactTokenLimit: config.autoCompactTokenLimit,
|
|
71
84
|
};
|
|
72
85
|
this.threadId = config.threadId ?? nextId();
|
|
73
86
|
// Resolve ThreadStore: explicit > ioBackend shorthand > in-memory
|
|
@@ -79,7 +92,7 @@ export class CodexThread {
|
|
|
79
92
|
// Resolve GoalStore
|
|
80
93
|
const goalStore = config.goalStore ?? new GoalStore(new InMemoryGoalBackend());
|
|
81
94
|
this.goalExecutor = new GoalToolExecutor(this.threadId, goalStore);
|
|
82
|
-
this.router = new ToolRouter(this.goalExecutor);
|
|
95
|
+
this.router = new ToolRouter(this.goalExecutor, config.customTools);
|
|
83
96
|
}
|
|
84
97
|
/** The thread's stable identifier (use for resume) */
|
|
85
98
|
get id() { return this.threadId; }
|
|
@@ -110,19 +123,66 @@ export class CodexThread {
|
|
|
110
123
|
switch (op.type) {
|
|
111
124
|
case "UserInput": {
|
|
112
125
|
const turnId = submissionId;
|
|
126
|
+
// Per-turn overrides (op.*) take precedence over thread-level config.
|
|
127
|
+
// Developer instructions are layered on top of the base agent harness.
|
|
128
|
+
const devInstructions = op.instructions ?? this.config.instructions;
|
|
129
|
+
// instructions field = base agent harness + developer (app-level)
|
|
130
|
+
// instructions, mirroring codex-rs where base/developer go in
|
|
131
|
+
// `instructions`. Project docs + catalog ride in `input` instead.
|
|
132
|
+
const instructions = [this.config.baseInstructions, devInstructions]
|
|
133
|
+
.filter((part) => Boolean(part))
|
|
134
|
+
.join("\n\n");
|
|
135
|
+
// codex-rs puts the project doc + skills catalog in `input` as discrete
|
|
136
|
+
// contextual messages so each boundary is explicit to the model (see
|
|
137
|
+
// TurnConfig.contextItems) rather than baked into the instructions blob.
|
|
138
|
+
const contextItems = [];
|
|
139
|
+
// AGENTS.md → user_instructions message (codex-rs UserInstructions
|
|
140
|
+
// fragment: # AGENTS.md instructions / <INSTRUCTIONS> markers).
|
|
141
|
+
if (this.config.agentsMd) {
|
|
142
|
+
contextItems.push({
|
|
143
|
+
type: "message",
|
|
144
|
+
role: "user",
|
|
145
|
+
content: [
|
|
146
|
+
{
|
|
147
|
+
type: "input_text",
|
|
148
|
+
text: `# AGENTS.md instructions\n\n<INSTRUCTIONS>\n${this.config.agentsMd}\n</INSTRUCTIONS>`,
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
// skills catalog message (codex-rs available-skills fragment).
|
|
154
|
+
const skillsCatalog = renderSkillsCatalog(this.config.skills);
|
|
155
|
+
if (skillsCatalog) {
|
|
156
|
+
contextItems.push({
|
|
157
|
+
type: "message",
|
|
158
|
+
role: "user",
|
|
159
|
+
content: [{ type: "input_text", text: skillsCatalog }],
|
|
160
|
+
});
|
|
161
|
+
}
|
|
113
162
|
const turnConfig = {
|
|
114
163
|
apiKey: this.config.apiKey,
|
|
115
164
|
baseUrl: this.config.baseUrl,
|
|
116
|
-
model: this.config.model,
|
|
117
|
-
...(
|
|
118
|
-
|
|
165
|
+
model: op.model ?? this.config.model,
|
|
166
|
+
...(instructions ? { instructions } : {}),
|
|
167
|
+
...(contextItems.length > 0 ? { contextItems } : {}),
|
|
168
|
+
...(this.config.skills.length > 0
|
|
169
|
+
? { skills: this.config.skills }
|
|
170
|
+
: {}),
|
|
171
|
+
...(this.config.loadSkillContent
|
|
172
|
+
? { loadSkillContent: this.config.loadSkillContent }
|
|
173
|
+
: {}),
|
|
174
|
+
...(this.config.autoCompactTokenLimit !== undefined
|
|
175
|
+
? { autoCompactTokenLimit: this.config.autoCompactTokenLimit }
|
|
119
176
|
: {}),
|
|
120
177
|
};
|
|
178
|
+
const abortController = new AbortController();
|
|
179
|
+
this.currentTurnAbort = abortController;
|
|
121
180
|
this.pushEvent(submissionId, {
|
|
122
181
|
type: "TurnStarted",
|
|
123
182
|
event: { turn_id: turnId },
|
|
124
183
|
});
|
|
125
|
-
void runTurn(turnId, op.items, this.history, turnConfig, this.router, this.pendingInputs, (msg) => this.pushEvent(submissionId, msg), this.liveThread
|
|
184
|
+
void runTurn(turnId, op.items, this.history, turnConfig, this.router, this.pendingInputs, (msg) => this.pushEvent(submissionId, msg), this.liveThread, // ← persistence hook
|
|
185
|
+
abortController.signal)
|
|
126
186
|
.then(({ lastAgentMessage }) => {
|
|
127
187
|
this.pushEvent(submissionId, {
|
|
128
188
|
type: "TurnComplete",
|
|
@@ -135,8 +195,13 @@ export class CodexThread {
|
|
|
135
195
|
.catch((err) => {
|
|
136
196
|
this.pushEvent(submissionId, {
|
|
137
197
|
type: "Error",
|
|
138
|
-
event: { message: String(err) },
|
|
198
|
+
event: { message: String(err), turn_id: turnId },
|
|
139
199
|
});
|
|
200
|
+
})
|
|
201
|
+
.finally(() => {
|
|
202
|
+
if (this.currentTurnAbort === abortController) {
|
|
203
|
+
this.currentTurnAbort = null;
|
|
204
|
+
}
|
|
140
205
|
});
|
|
141
206
|
break;
|
|
142
207
|
}
|
|
@@ -149,6 +214,7 @@ export class CodexThread {
|
|
|
149
214
|
break;
|
|
150
215
|
}
|
|
151
216
|
case "Interrupt":
|
|
217
|
+
this.currentTurnAbort?.abort();
|
|
152
218
|
break;
|
|
153
219
|
}
|
|
154
220
|
return submissionId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codex_thread.js","sourceRoot":"","sources":["../../../core/src/codex_thread.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAUH,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,GACX,MAAM,iCAAiC,CAAC;AAGzC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"codex_thread.js","sourceRoot":"","sources":["../../../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,CAAC,CAAC;oBACrB,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;gBACnC,CAAC;gBACD,MAAM;YACR,CAAC;YAED,KAAK,WAAW;gBACd,IAAI,CAAC,gBAAgB,EAAE,KAAK,EAAE,CAAC;gBAC/B,MAAM;QACV,CAAC;QAED,OAAO,YAAY,CAAC;IACtB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,SAAS;QACb,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,YAAoB,EAAE,GAAa;QACnD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,YAAY,EAAE,GAAG,EAAE,CAAC,CAAC;IAClD,CAAC;CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mirrors codex-rs/core/src/compact.rs
|
|
3
|
+
* codex-rs/prompts/templates/compact/prompt.md
|
|
4
|
+
* codex-rs/prompts/templates/compact/summary_prefix.md
|
|
5
|
+
*
|
|
6
|
+
* Inline context compaction: summarise the conversation with a fresh model
|
|
7
|
+
* request and replace history with the summary + recent user messages.
|
|
8
|
+
*/
|
|
9
|
+
import type { ConversationItem } from "../../thread-store/src/types.js";
|
|
10
|
+
import type { TurnConfig } from "./session/turn.js";
|
|
11
|
+
/** mirrors prompts/templates/compact/prompt.md */
|
|
12
|
+
export declare const SUMMARIZATION_PROMPT: string;
|
|
13
|
+
/** mirrors prompts/templates/compact/summary_prefix.md */
|
|
14
|
+
export declare const SUMMARY_PREFIX: string;
|
|
15
|
+
/**
|
|
16
|
+
* Extract plain-text content from all user messages in history.
|
|
17
|
+
* mirrors collect_user_messages in compact.rs
|
|
18
|
+
*/
|
|
19
|
+
export declare function collectUserMessages(history: ConversationItem[]): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Build the replacement history from recent user messages + summary.
|
|
22
|
+
* mirrors build_compacted_history_with_limit in compact.rs:
|
|
23
|
+
* - Walk user messages in reverse, filling up to COMPACT_USER_MESSAGE_MAX_TOKENS
|
|
24
|
+
* - Append the summary as the final user message
|
|
25
|
+
*/
|
|
26
|
+
export declare function buildCompactedHistory(userMessages: string[], summaryText: string): ConversationItem[];
|
|
27
|
+
/**
|
|
28
|
+
* Run an inline compaction turn:
|
|
29
|
+
* 1. Send SUMMARIZATION_PROMPT + current history to the model
|
|
30
|
+
* 2. Collect the assistant's summary text
|
|
31
|
+
* 3. Build replacement history and mutate `history` in-place
|
|
32
|
+
*
|
|
33
|
+
* mirrors run_inline_auto_compact_task + run_compact_task_inner_impl in compact.rs
|
|
34
|
+
* Returns the raw summary text (without SUMMARY_PREFIX).
|
|
35
|
+
*/
|
|
36
|
+
export declare function runInlineAutoCompactTask(history: ConversationItem[], config: TurnConfig): Promise<string>;
|
|
37
|
+
//# sourceMappingURL=compact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compact.d.ts","sourceRoot":"","sources":["../../../core/src/compact.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAKpD,kDAAkD;AAClD,eAAO,MAAM,oBAAoB,QAQ4D,CAAC;AAE9F,0DAA0D;AAC1D,eAAO,MAAM,cAAc,QAK8D,CAAC;AAmB1F;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,EAAE,GAAG,MAAM,EAAE,CAezE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,EAAE,MAAM,EAAE,EACtB,WAAW,EAAE,MAAM,GAClB,gBAAgB,EAAE,CAmCpB;AAID;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,EAAE,UAAU,GACjB,OAAO,CAAC,MAAM,CAAC,CA+CjB"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mirrors codex-rs/core/src/compact.rs
|
|
3
|
+
* codex-rs/prompts/templates/compact/prompt.md
|
|
4
|
+
* codex-rs/prompts/templates/compact/summary_prefix.md
|
|
5
|
+
*
|
|
6
|
+
* Inline context compaction: summarise the conversation with a fresh model
|
|
7
|
+
* request and replace history with the summary + recent user messages.
|
|
8
|
+
*/
|
|
9
|
+
import { parseSseStream } from "./session/sse.js";
|
|
10
|
+
// ─── Prompt constants (verbatim from codex-rs templates) ──────────────────────
|
|
11
|
+
/** mirrors prompts/templates/compact/prompt.md */
|
|
12
|
+
export const SUMMARIZATION_PROMPT = "You are performing a CONTEXT CHECKPOINT COMPACTION. " +
|
|
13
|
+
"Create a handoff summary for another LLM that will resume the task.\n\n" +
|
|
14
|
+
"Include:\n" +
|
|
15
|
+
"- Current progress and key decisions made\n" +
|
|
16
|
+
"- Important context, constraints, or user preferences\n" +
|
|
17
|
+
"- What remains to be done (clear next steps)\n" +
|
|
18
|
+
"- Any critical data, examples, or references needed to continue\n\n" +
|
|
19
|
+
"Be concise, structured, and focused on helping the next LLM seamlessly continue the work.";
|
|
20
|
+
/** mirrors prompts/templates/compact/summary_prefix.md */
|
|
21
|
+
export const SUMMARY_PREFIX = "Another language model started to solve this problem and produced a summary " +
|
|
22
|
+
"of its thinking process. You also have access to the state of the tools that " +
|
|
23
|
+
"were used by that language model. Use this to build on the work that has already " +
|
|
24
|
+
"been done and avoid duplicating work. Here is the summary produced by the other " +
|
|
25
|
+
"language model, use the information in this summary to assist with your own analysis:";
|
|
26
|
+
/** mirrors COMPACT_USER_MESSAGE_MAX_TOKENS in compact.rs */
|
|
27
|
+
const COMPACT_USER_MESSAGE_MAX_TOKENS = 20_000;
|
|
28
|
+
// ─── Token approximation (mirrors codex-rs/utils/output-truncation) ───────────
|
|
29
|
+
/** ~4 chars per token — cheap approximation, good enough for budget checks */
|
|
30
|
+
function approxTokenCount(text) {
|
|
31
|
+
return Math.ceil(text.length / 4);
|
|
32
|
+
}
|
|
33
|
+
function truncateToTokens(text, maxTokens) {
|
|
34
|
+
const maxChars = maxTokens * 4;
|
|
35
|
+
return text.length <= maxChars ? text : text.slice(0, maxChars);
|
|
36
|
+
}
|
|
37
|
+
// ─── History helpers ─────────────────────────────────────────────────────────
|
|
38
|
+
/**
|
|
39
|
+
* Extract plain-text content from all user messages in history.
|
|
40
|
+
* mirrors collect_user_messages in compact.rs
|
|
41
|
+
*/
|
|
42
|
+
export function collectUserMessages(history) {
|
|
43
|
+
const messages = [];
|
|
44
|
+
for (const item of history) {
|
|
45
|
+
if (!("role" in item) || item.role !== "user")
|
|
46
|
+
continue;
|
|
47
|
+
if (typeof item.content === "string") {
|
|
48
|
+
if (item.content)
|
|
49
|
+
messages.push(item.content);
|
|
50
|
+
}
|
|
51
|
+
else if (Array.isArray(item.content)) {
|
|
52
|
+
const text = item.content
|
|
53
|
+
.filter((p) => p.type === "input_text")
|
|
54
|
+
.map((p) => p.text)
|
|
55
|
+
.join("\n");
|
|
56
|
+
if (text)
|
|
57
|
+
messages.push(text);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return messages;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Build the replacement history from recent user messages + summary.
|
|
64
|
+
* mirrors build_compacted_history_with_limit in compact.rs:
|
|
65
|
+
* - Walk user messages in reverse, filling up to COMPACT_USER_MESSAGE_MAX_TOKENS
|
|
66
|
+
* - Append the summary as the final user message
|
|
67
|
+
*/
|
|
68
|
+
export function buildCompactedHistory(userMessages, summaryText) {
|
|
69
|
+
const maxTokens = COMPACT_USER_MESSAGE_MAX_TOKENS;
|
|
70
|
+
let remaining = maxTokens;
|
|
71
|
+
const selected = [];
|
|
72
|
+
for (let i = userMessages.length - 1; i >= 0; i--) {
|
|
73
|
+
if (remaining === 0)
|
|
74
|
+
break;
|
|
75
|
+
const msg = userMessages[i];
|
|
76
|
+
const tokens = approxTokenCount(msg);
|
|
77
|
+
if (tokens <= remaining) {
|
|
78
|
+
selected.unshift(msg);
|
|
79
|
+
remaining -= tokens;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
selected.unshift(truncateToTokens(msg, remaining));
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const history = [];
|
|
87
|
+
for (const text of selected) {
|
|
88
|
+
history.push({
|
|
89
|
+
type: "message",
|
|
90
|
+
role: "user",
|
|
91
|
+
content: [{ type: "input_text", text }],
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const finalSummary = summaryText || "(no summary available)";
|
|
95
|
+
history.push({
|
|
96
|
+
type: "message",
|
|
97
|
+
role: "user",
|
|
98
|
+
content: [{ type: "input_text", text: finalSummary }],
|
|
99
|
+
});
|
|
100
|
+
return history;
|
|
101
|
+
}
|
|
102
|
+
// ─── Compaction request ───────────────────────────────────────────────────────
|
|
103
|
+
/**
|
|
104
|
+
* Run an inline compaction turn:
|
|
105
|
+
* 1. Send SUMMARIZATION_PROMPT + current history to the model
|
|
106
|
+
* 2. Collect the assistant's summary text
|
|
107
|
+
* 3. Build replacement history and mutate `history` in-place
|
|
108
|
+
*
|
|
109
|
+
* mirrors run_inline_auto_compact_task + run_compact_task_inner_impl in compact.rs
|
|
110
|
+
* Returns the raw summary text (without SUMMARY_PREFIX).
|
|
111
|
+
*/
|
|
112
|
+
export async function runInlineAutoCompactTask(history, config) {
|
|
113
|
+
// Build the compaction request: full history + the summarization prompt at the end
|
|
114
|
+
const compactInput = [
|
|
115
|
+
...history,
|
|
116
|
+
{ type: "message", role: "user", content: SUMMARIZATION_PROMPT },
|
|
117
|
+
];
|
|
118
|
+
const body = {
|
|
119
|
+
model: config.model,
|
|
120
|
+
input: compactInput,
|
|
121
|
+
stream: true,
|
|
122
|
+
};
|
|
123
|
+
if (config.instructions)
|
|
124
|
+
body["instructions"] = config.instructions;
|
|
125
|
+
const res = await fetch(`${config.baseUrl}/responses`, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
headers: {
|
|
128
|
+
"Content-Type": "application/json",
|
|
129
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
130
|
+
},
|
|
131
|
+
body: JSON.stringify(body),
|
|
132
|
+
});
|
|
133
|
+
if (!res.ok || !res.body) {
|
|
134
|
+
const text = res.body ? await res.text() : "(no body)";
|
|
135
|
+
throw new Error(`Compaction request failed ${res.status}: ${text}`);
|
|
136
|
+
}
|
|
137
|
+
// Collect the assistant's summary text from the stream
|
|
138
|
+
let summaryText = "";
|
|
139
|
+
for await (const raw of parseSseStream(res.body)) {
|
|
140
|
+
if (raw["type"] === "response.output_text.delta") {
|
|
141
|
+
summaryText += String(raw["delta"] ?? "");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const fullSummary = `${SUMMARY_PREFIX}\n${summaryText}`;
|
|
145
|
+
// Collect recent user messages from the pre-compaction history
|
|
146
|
+
const userMessages = collectUserMessages(history);
|
|
147
|
+
// Build replacement history and mutate in place
|
|
148
|
+
const newHistory = buildCompactedHistory(userMessages, fullSummary);
|
|
149
|
+
history.length = 0;
|
|
150
|
+
for (const item of newHistory)
|
|
151
|
+
history.push(item);
|
|
152
|
+
return summaryText;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=compact.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compact.js","sourceRoot":"","sources":["../../../core/src/compact.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,iFAAiF;AAEjF,kDAAkD;AAClD,MAAM,CAAC,MAAM,oBAAoB,GAC/B,sDAAsD;IACtD,yEAAyE;IACzE,YAAY;IACZ,6CAA6C;IAC7C,yDAAyD;IACzD,gDAAgD;IAChD,qEAAqE;IACrE,2FAA2F,CAAC;AAE9F,0DAA0D;AAC1D,MA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,uDAAuD;IACvD,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QACjD,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,4BAA4B,EAAE,CAAC;YACjD,WAAW,IAAI,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,MAAM,WAAW,GAAG,GAAG,cAAc,KAAK,WAAW,EAAE,CAAC;IAExD,+DAA+D;IAC/D,MAAM,YAAY,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IAElD,gDAAgD;IAChD,MAAM,UAAU,GAAG,qBAAqB,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;IACpE,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IACnB,KAAK,MAAM,IAAI,IAAI,UAAU;QAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAElD,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
package/dist/core/src/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export { CodexThread } from "./codex_thread.js";
|
|
2
|
+
export { SUMMARIZATION_PROMPT, SUMMARY_PREFIX } from "./compact.js";
|
|
3
|
+
export { AutoCompactWindow } from "./state/auto_compact_window.js";
|
|
2
4
|
export type { CodexThreadConfig } from "./codex_thread.js";
|
|
5
|
+
export { DEFAULT_BASE_INSTRUCTIONS } from "./base_instructions.js";
|
|
6
|
+
export { renderSkillsCatalog, extractSkillMentions, renderSkillInjection, defaultSkillMetadataBudget, } from "./skills.js";
|
|
7
|
+
export type { SkillMetadata, SkillMetadataBudget } from "./skills.js";
|
|
3
8
|
export { ToolRouter } from "./tools/router.js";
|
|
9
|
+
export type { CustomTool, CustomToolContext, ToolRouterContext, } from "./tools/router.js";
|
|
4
10
|
export { GoalToolExecutor } from "../../ext/goal/src/tool.js";
|
|
5
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../core/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../core/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,0BAA0B,GAC3B,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,YAAY,EACV,UAAU,EACV,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC"}
|
package/dist/core/src/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export { CodexThread } from "./codex_thread.js";
|
|
2
|
+
export { SUMMARIZATION_PROMPT, SUMMARY_PREFIX } from "./compact.js";
|
|
3
|
+
export { AutoCompactWindow } from "./state/auto_compact_window.js";
|
|
4
|
+
export { DEFAULT_BASE_INSTRUCTIONS } from "./base_instructions.js";
|
|
5
|
+
export { renderSkillsCatalog, extractSkillMentions, renderSkillInjection, defaultSkillMetadataBudget, } from "./skills.js";
|
|
2
6
|
export { ToolRouter } from "./tools/router.js";
|
|
3
7
|
export { GoalToolExecutor } from "../../ext/goal/src/tool.js";
|
|
4
8
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../core/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../core/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAEnE,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,0BAA0B,GAC3B,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAM/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE stream parser for the OpenAI Responses API.
|
|
3
|
+
* Extracted from turn.ts so compact.ts can share it.
|
|
4
|
+
*/
|
|
5
|
+
export type RawSseEvent = Record<string, unknown>;
|
|
6
|
+
export declare function parseSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<RawSseEvent>;
|
|
7
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../../../core/src/session/sse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAElD,wBAAuB,cAAc,CACnC,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,GAC/B,cAAc,CAAC,WAAW,CAAC,CA8B7B"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE stream parser for the OpenAI Responses API.
|
|
3
|
+
* Extracted from turn.ts so compact.ts can share it.
|
|
4
|
+
*/
|
|
5
|
+
export async function* parseSseStream(body) {
|
|
6
|
+
const reader = body.getReader();
|
|
7
|
+
const decoder = new TextDecoder();
|
|
8
|
+
let buffer = "";
|
|
9
|
+
try {
|
|
10
|
+
for (;;) {
|
|
11
|
+
const { done, value } = await reader.read();
|
|
12
|
+
if (done)
|
|
13
|
+
break;
|
|
14
|
+
buffer += decoder.decode(value, { stream: true });
|
|
15
|
+
const lines = buffer.split("\n");
|
|
16
|
+
buffer = lines.pop() ?? "";
|
|
17
|
+
let dataLine = "";
|
|
18
|
+
for (const line of lines) {
|
|
19
|
+
if (line.startsWith("data: ")) {
|
|
20
|
+
dataLine = line.slice(6).trim();
|
|
21
|
+
}
|
|
22
|
+
else if (line === "" && dataLine) {
|
|
23
|
+
if (dataLine !== "[DONE]") {
|
|
24
|
+
try {
|
|
25
|
+
yield JSON.parse(dataLine);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
/* malformed JSON — skip */
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
dataLine = "";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
reader.releaseLock();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js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