replicas-engine 0.1.667 → 0.1.671

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.
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/utils/presigned-upload.ts
4
+ import { createReadStream } from "fs";
5
+ import { request as httpRequest } from "http";
6
+ import { request as httpsRequest } from "https";
7
+ async function putPresignedFile(urlValue, filePath, size, contentType) {
8
+ await new Promise((resolve, reject) => {
9
+ const url = new URL(urlValue);
10
+ const request = (url.protocol === "https:" ? httpsRequest : httpRequest)(url, {
11
+ method: "PUT",
12
+ headers: {
13
+ "content-length": String(size),
14
+ "content-type": contentType
15
+ }
16
+ }, (response) => {
17
+ response.setEncoding("utf8");
18
+ let body = "";
19
+ response.on("data", (chunk) => {
20
+ body += chunk;
21
+ });
22
+ response.on("end", () => {
23
+ const status = response.statusCode ?? 0;
24
+ if (status >= 200 && status < 300) resolve();
25
+ else reject(new Error(`upload failed: ${status} ${body}`));
26
+ });
27
+ response.on("error", reject);
28
+ });
29
+ request.on("error", reject);
30
+ const file = size > 0 ? createReadStream(filePath, { start: 0, end: size - 1 }) : createReadStream(filePath);
31
+ file.on("error", (error) => request.destroy(error));
32
+ file.pipe(request);
33
+ });
34
+ }
35
+
36
+ // src/utils/codex-agent-env.ts
37
+ function buildCodexAgentEnv(source = process.env) {
38
+ const env = Object.fromEntries(
39
+ Object.entries(source).filter((entry) => typeof entry[1] === "string")
40
+ );
41
+ if (env.REPLICAS_CODEX_AUTH_METHOD === "oauth" || env.REPLICAS_CODEX_AUTH_METHOD === "foundry") {
42
+ delete env.OPENAI_API_KEY;
43
+ }
44
+ delete env.GH_TOKEN;
45
+ delete env.GITHUB_TOKEN;
46
+ delete env.GH_CONFIG_DIR;
47
+ return env;
48
+ }
49
+
50
+ // src/managers/codex-asp/notification-dispatch.ts
51
+ var TURN_STARTED_METHOD = "turn/started";
52
+ var TURN_COMPLETED_METHOD = "turn/completed";
53
+ var TURN_PLAN_UPDATED_METHOD = "turn/plan/updated";
54
+ var THREAD_GOAL_UPDATED_METHOD = "thread/goal/updated";
55
+ var THREAD_GOAL_CLEARED_METHOD = "thread/goal/cleared";
56
+ var ITEM_STARTED_METHOD = "item/started";
57
+ var ITEM_COMPLETED_METHOD = "item/completed";
58
+ var AGENT_MESSAGE_DELTA_METHOD = "item/agentMessage/delta";
59
+ var REASONING_SUMMARY_TEXT_DELTA_METHOD = "item/reasoning/summaryTextDelta";
60
+ var REASONING_TEXT_DELTA_METHOD = "item/reasoning/textDelta";
61
+ var REASONING_SUMMARY_PART_ADDED_METHOD = "item/reasoning/summaryPartAdded";
62
+ var COMMAND_EXECUTION_OUTPUT_DELTA_METHOD = "item/commandExecution/outputDelta";
63
+ var FILE_CHANGE_OUTPUT_DELTA_METHOD = "item/fileChange/outputDelta";
64
+ var ACCOUNT_RATE_LIMITS_UPDATED_METHOD = "account/rateLimits/updated";
65
+ var THREAD_TOKEN_USAGE_UPDATED_METHOD = "thread/tokenUsage/updated";
66
+ var THREAD_COMPACTED_METHOD = "thread/compacted";
67
+ function dispatchAspNotification(notification, handlers) {
68
+ const handler = handlers[notification.method];
69
+ if (!handler) return;
70
+ handler(notification);
71
+ }
72
+ function recoverCompletedTurn(turn, completedItems, agentMessageDeltas) {
73
+ const items = turn.items.length > 0 ? [...turn.items] : [];
74
+ const itemIds = new Set(items.map((item) => item.id));
75
+ for (const item of completedItems) {
76
+ if (itemIds.has(item.id)) continue;
77
+ items.push(item);
78
+ itemIds.add(item.id);
79
+ }
80
+ for (const [itemId, text] of agentMessageDeltas) {
81
+ if (itemIds.has(itemId)) continue;
82
+ items.push({ type: "agentMessage", id: itemId, text, phase: null, memoryCitation: null });
83
+ }
84
+ return items.length > 0 ? { ...turn, items, itemsView: "full" } : turn;
85
+ }
86
+
87
+ export {
88
+ putPresignedFile,
89
+ buildCodexAgentEnv,
90
+ TURN_STARTED_METHOD,
91
+ TURN_COMPLETED_METHOD,
92
+ TURN_PLAN_UPDATED_METHOD,
93
+ THREAD_GOAL_UPDATED_METHOD,
94
+ THREAD_GOAL_CLEARED_METHOD,
95
+ ITEM_STARTED_METHOD,
96
+ ITEM_COMPLETED_METHOD,
97
+ AGENT_MESSAGE_DELTA_METHOD,
98
+ REASONING_SUMMARY_TEXT_DELTA_METHOD,
99
+ REASONING_TEXT_DELTA_METHOD,
100
+ REASONING_SUMMARY_PART_ADDED_METHOD,
101
+ COMMAND_EXECUTION_OUTPUT_DELTA_METHOD,
102
+ FILE_CHANGE_OUTPUT_DELTA_METHOD,
103
+ ACCOUNT_RATE_LIMITS_UPDATED_METHOD,
104
+ THREAD_TOKEN_USAGE_UPDATED_METHOD,
105
+ THREAD_COMPACTED_METHOD,
106
+ dispatchAspNotification,
107
+ recoverCompletedTurn
108
+ };
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ CodexAspAuthMethodChangedError,
4
+ CodexTokenManager,
5
+ codexTokenManager
6
+ } from "./chunk-6WY7NPCL.js";
7
+ import "./chunk-4FTBKGMO.js";
8
+ import "./chunk-TMERKNQV.js";
9
+ export {
10
+ CodexAspAuthMethodChangedError,
11
+ CodexTokenManager,
12
+ codexTokenManager
13
+ };
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ ENGINE_ENV,
4
+ IS_WARMING_MODE,
5
+ loadEngineEnv,
6
+ setAgentCredentialSnapshot
7
+ } from "./chunk-4FTBKGMO.js";
8
+ import "./chunk-TMERKNQV.js";
9
+ export {
10
+ ENGINE_ENV,
11
+ IS_WARMING_MODE,
12
+ loadEngineEnv,
13
+ setAgentCredentialSnapshot
14
+ };
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
- AGENT,
4
- AppServerProcess,
5
3
  buildCodexAgentEnv,
6
- getMemoryOutputSafetyViolation,
7
- headlessAgentRequestSchema,
8
4
  putPresignedFile,
9
5
  recoverCompletedTurn
10
- } from "./chunk-P2Q47GLV.js";
6
+ } from "./chunk-XQW4B35Y.js";
7
+ import {
8
+ AGENT,
9
+ getMemoryOutputSafetyViolation,
10
+ headlessAgentRequestSchema
11
+ } from "./chunk-TMERKNQV.js";
11
12
 
12
13
  // src/headless-agent.ts
13
14
  import { createHash } from "crypto";
@@ -171,38 +172,52 @@ function runCodexTurn(client, params, timeoutMs) {
171
172
  });
172
173
  }
173
174
  async function runCodex(request) {
174
- const appServer = new AppServerProcess({
175
- cwd: request.workingDirectory,
176
- env: buildCodexAgentEnv(),
177
- configOverrides: ["shell_environment_policy.inherit=none", "tools.web_search=false"]
178
- });
179
- try {
180
- const { client } = await appServer.start();
181
- client.on("serverRequest", (serverRequest) => {
182
- client.reject(serverRequest.id, -32601, "Headless agents do not accept server requests");
183
- });
184
- const thread = await client.request("thread/start", {
185
- model: request.model,
175
+ const [{ AppServerProcess }, { codexTokenManager }, { ENGINE_ENV }] = await Promise.all([
176
+ import("./app-server-process-S2SD6PQE.js"),
177
+ import("./codex-token-manager-RX4N2W22.js"),
178
+ import("./engine-env-A2FZA66L.js")
179
+ ]);
180
+ for (let attempt = 0; attempt < 2; attempt++) {
181
+ const authMethod = ENGINE_ENV.REPLICAS_CODEX_AUTH_METHOD;
182
+ const oauthOptions = await codexTokenManager.prepareAspOauthOptions();
183
+ const appServer = new AppServerProcess({
186
184
  cwd: request.workingDirectory,
187
- approvalPolicy: "never",
188
- approvalsReviewer: "user",
189
- sandbox: request.mode === "filesystem" ? "workspace-write" : "read-only",
190
- ephemeral: true
185
+ env: buildCodexAgentEnv(),
186
+ configOverrides: ["shell_environment_policy.inherit=none", "tools.web_search=false"],
187
+ ...oauthOptions
191
188
  });
192
- const { turn, message } = await runCodexTurn(client, {
193
- threadId: thread.thread.id,
194
- input: [{ type: "text", text: request.prompt, text_elements: [] }],
195
- ...request.mode === "structured" ? { outputSchema: request.outputSchema } : {},
196
- approvalPolicy: "never",
197
- approvalsReviewer: "user"
198
- }, request.timeoutSeconds * 1e3);
199
- if (turn.status === "failed") throw new Error(turn.error?.message ?? "Codex ASP turn failed");
200
- if (!message) throw new Error("Codex ASP returned no final message");
201
- if (request.mode === "filesystem") return message;
202
- return JSON.parse(message);
203
- } finally {
204
- await appServer.stop();
189
+ try {
190
+ const { client } = await appServer.start();
191
+ client.on("serverRequest", (serverRequest) => {
192
+ if (serverRequest.method === "account/chatgptAuthTokens/refresh") return;
193
+ client.reject(serverRequest.id, -32601, "Headless agents do not accept server requests");
194
+ });
195
+ const thread = await client.request("thread/start", {
196
+ model: request.model,
197
+ cwd: request.workingDirectory,
198
+ approvalPolicy: "never",
199
+ approvalsReviewer: "user",
200
+ sandbox: request.mode === "filesystem" ? "workspace-write" : "read-only",
201
+ ephemeral: true
202
+ });
203
+ const { turn, message } = await runCodexTurn(client, {
204
+ threadId: thread.thread.id,
205
+ input: [{ type: "text", text: request.prompt, text_elements: [] }],
206
+ ...request.mode === "structured" ? { outputSchema: request.outputSchema } : {},
207
+ approvalPolicy: "never",
208
+ approvalsReviewer: "user"
209
+ }, request.timeoutSeconds * 1e3);
210
+ if (turn.status === "failed") throw new Error(turn.error?.message ?? "Codex ASP turn failed");
211
+ if (!message) throw new Error("Codex ASP returned no final message");
212
+ if (request.mode === "filesystem") return message;
213
+ return JSON.parse(message);
214
+ } catch (error) {
215
+ if (attempt > 0 || ENGINE_ENV.REPLICAS_CODEX_AUTH_METHOD === authMethod) throw error;
216
+ } finally {
217
+ await appServer.stop();
218
+ }
205
219
  }
220
+ throw new Error("Codex ASP could not restart with fallback credentials");
206
221
  }
207
222
  async function main() {
208
223
  const [requestPath, outputPath] = process.argv.slice(2);