@leoustc/service-llm 0.2.5 → 0.2.6
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/THIRD_PARTY_NOTICES.md +11 -0
- package/VERSION +1 -1
- package/dist/auth.js +2 -2
- package/dist/backend.js +1 -1
- package/dist/codex-oauth.js +137 -0
- package/dist/codex.js +184 -0
- package/package.json +4 -5
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
Portions of the Codex OAuth and Responses transport implementation are adapted from `@earendil-works/pi-ai`, part of the [pi project](https://github.com/earendil-works/pi), originally authored by Mario Zechner and contributors.
|
|
4
|
+
|
|
5
|
+
Copyright (c) Mario Zechner and contributors
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.6
|
package/dist/auth.js
CHANGED
|
@@ -6,8 +6,8 @@ import {
|
|
|
6
6
|
loginOpenAICodex,
|
|
7
7
|
loginOpenAICodexDeviceCode,
|
|
8
8
|
refreshOpenAICodexToken,
|
|
9
|
-
} from "
|
|
10
|
-
import { getModels } from "
|
|
9
|
+
} from "./codex-oauth.js";
|
|
10
|
+
import { getModels } from "./codex.js";
|
|
11
11
|
|
|
12
12
|
export const authPath = join(homedir(), ".service-llm", "auth.json");
|
|
13
13
|
export const modelsPath = join(homedir(), ".service-llm", "models.json");
|
package/dist/backend.js
CHANGED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Codex OAuth flow adapted from @earendil-works/pi-ai (MIT).
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
|
|
5
|
+
const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann";
|
|
6
|
+
const AUTH_BASE_URL = "https://auth.openai.com";
|
|
7
|
+
const TOKEN_URL = `${AUTH_BASE_URL}/oauth/token`;
|
|
8
|
+
const REDIRECT_URI = "http://localhost:1455/auth/callback";
|
|
9
|
+
const DEVICE_REDIRECT_URI = `${AUTH_BASE_URL}/deviceauth/callback`;
|
|
10
|
+
const DEVICE_VERIFICATION_URI = `${AUTH_BASE_URL}/codex/device`;
|
|
11
|
+
const DEVICE_TIMEOUT_SECONDS = 15 * 60;
|
|
12
|
+
|
|
13
|
+
function accountIdFromToken(token) {
|
|
14
|
+
try {
|
|
15
|
+
const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString("utf8"));
|
|
16
|
+
return payload?.["https://api.openai.com/auth"]?.chatgpt_account_id;
|
|
17
|
+
} catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function tokenResponse(response, operation) {
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
const detail = await response.text().catch(() => "");
|
|
25
|
+
throw new Error(`OpenAI Codex token ${operation} failed (${response.status}): ${detail || response.statusText}`);
|
|
26
|
+
}
|
|
27
|
+
const body = await response.json();
|
|
28
|
+
if (!body?.access_token || !body.refresh_token || typeof body.expires_in !== "number") {
|
|
29
|
+
throw new Error(`OpenAI Codex token ${operation} response is incomplete`);
|
|
30
|
+
}
|
|
31
|
+
const accountId = accountIdFromToken(body.access_token);
|
|
32
|
+
if (!accountId) throw new Error("Failed to extract the ChatGPT account ID from the access token");
|
|
33
|
+
return { access: body.access_token, refresh: body.refresh_token, expires: Date.now() + body.expires_in * 1000, accountId };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function exchangeCode(code, verifier, redirectUri, signal) {
|
|
37
|
+
const response = await fetch(TOKEN_URL, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
40
|
+
body: new URLSearchParams({ grant_type: "authorization_code", client_id: CLIENT_ID, code, code_verifier: verifier, redirect_uri: redirectUri }),
|
|
41
|
+
signal,
|
|
42
|
+
});
|
|
43
|
+
return tokenResponse(response, "exchange");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function refreshOpenAICodexToken(refreshToken) {
|
|
47
|
+
const response = await fetch(TOKEN_URL, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
50
|
+
body: new URLSearchParams({ grant_type: "refresh_token", refresh_token: refreshToken, client_id: CLIENT_ID }),
|
|
51
|
+
});
|
|
52
|
+
return tokenResponse(response, "refresh");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function sleep(ms, signal) {
|
|
56
|
+
await new Promise((resolve, reject) => {
|
|
57
|
+
if (signal?.aborted) return reject(new Error("Login cancelled"));
|
|
58
|
+
const timer = setTimeout(resolve, ms);
|
|
59
|
+
signal?.addEventListener("abort", () => { clearTimeout(timer); reject(new Error("Login cancelled")); }, { once: true });
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function loginOpenAICodexDeviceCode({ onDeviceCode, signal } = {}) {
|
|
64
|
+
const start = await fetch(`${AUTH_BASE_URL}/api/accounts/deviceauth/usercode`, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers: { "content-type": "application/json" },
|
|
67
|
+
body: JSON.stringify({ client_id: CLIENT_ID }),
|
|
68
|
+
signal,
|
|
69
|
+
});
|
|
70
|
+
if (!start.ok) throw new Error(`OpenAI Codex device code request failed (${start.status})`);
|
|
71
|
+
const device = await start.json();
|
|
72
|
+
const intervalSeconds = Number(device.interval);
|
|
73
|
+
if (!device.device_auth_id || !device.user_code || !Number.isFinite(intervalSeconds)) throw new Error("Invalid OpenAI Codex device code response");
|
|
74
|
+
onDeviceCode?.({ userCode: device.user_code, verificationUri: DEVICE_VERIFICATION_URI, intervalSeconds, expiresInSeconds: DEVICE_TIMEOUT_SECONDS });
|
|
75
|
+
|
|
76
|
+
const deadline = Date.now() + DEVICE_TIMEOUT_SECONDS * 1000;
|
|
77
|
+
let intervalMs = Math.max(1000, intervalSeconds * 1000);
|
|
78
|
+
while (Date.now() < deadline) {
|
|
79
|
+
const response = await fetch(`${AUTH_BASE_URL}/api/accounts/deviceauth/token`, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: { "content-type": "application/json" },
|
|
82
|
+
body: JSON.stringify({ device_auth_id: device.device_auth_id, user_code: device.user_code }),
|
|
83
|
+
signal,
|
|
84
|
+
});
|
|
85
|
+
if (response.ok) {
|
|
86
|
+
const result = await response.json();
|
|
87
|
+
if (!result.authorization_code || !result.code_verifier) throw new Error("Invalid OpenAI Codex device token response");
|
|
88
|
+
return exchangeCode(result.authorization_code, result.code_verifier, DEVICE_REDIRECT_URI, signal);
|
|
89
|
+
}
|
|
90
|
+
const detail = await response.text().catch(() => "");
|
|
91
|
+
if (response.status !== 403 && response.status !== 404 && !detail.includes("authorization_pending") && !detail.includes("slow_down")) {
|
|
92
|
+
throw new Error(`OpenAI Codex device authentication failed (${response.status}): ${detail}`);
|
|
93
|
+
}
|
|
94
|
+
if (detail.includes("slow_down")) intervalMs += 5000;
|
|
95
|
+
await sleep(Math.min(intervalMs, deadline - Date.now()), signal);
|
|
96
|
+
}
|
|
97
|
+
throw new Error("Device flow timed out");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function pkce() {
|
|
101
|
+
const verifier = randomBytes(32).toString("base64url");
|
|
102
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
|
|
103
|
+
return { verifier, challenge: Buffer.from(digest).toString("base64url") };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function callbackServer(expectedState) {
|
|
107
|
+
let settle;
|
|
108
|
+
const result = new Promise((resolve) => { settle = resolve; });
|
|
109
|
+
const server = createServer((request, response) => {
|
|
110
|
+
const url = new URL(request.url ?? "", "http://localhost");
|
|
111
|
+
const valid = url.pathname === "/auth/callback" && url.searchParams.get("state") === expectedState && url.searchParams.has("code");
|
|
112
|
+
response.writeHead(valid ? 200 : 400, { "content-type": "text/html; charset=utf-8" });
|
|
113
|
+
response.end(`<h1>${valid ? "Authentication successful" : "Authentication failed"}</h1><p>${valid ? "You may close this window." : "Invalid OAuth callback."}</p>`);
|
|
114
|
+
if (valid) settle(url.searchParams.get("code"));
|
|
115
|
+
});
|
|
116
|
+
return new Promise((resolve) => {
|
|
117
|
+
server.once("error", () => resolve({ server, result: Promise.resolve(undefined) }));
|
|
118
|
+
server.listen(1455, process.env.PI_OAUTH_CALLBACK_HOST || "127.0.0.1", () => resolve({ server, result }));
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function loginOpenAICodex({ onAuth, onPrompt, originator = "pi" } = {}) {
|
|
123
|
+
const { verifier, challenge } = await pkce();
|
|
124
|
+
const state = randomBytes(16).toString("hex");
|
|
125
|
+
const url = new URL(`${AUTH_BASE_URL}/oauth/authorize`);
|
|
126
|
+
for (const [key, value] of Object.entries({ response_type: "code", client_id: CLIENT_ID, redirect_uri: REDIRECT_URI, scope: "openid profile email offline_access", code_challenge: challenge, code_challenge_method: "S256", state, id_token_add_organizations: "true", codex_cli_simplified_flow: "true", originator })) url.searchParams.set(key, value);
|
|
127
|
+
const callback = await callbackServer(state);
|
|
128
|
+
onAuth?.({ url: url.toString(), instructions: "Complete login in your browser." });
|
|
129
|
+
try {
|
|
130
|
+
let code = await callback.result;
|
|
131
|
+
if (!code && onPrompt) code = await onPrompt({ message: "Paste the authorization code:" });
|
|
132
|
+
if (!code) throw new Error("Missing authorization code");
|
|
133
|
+
return await exchangeCode(code, verifier, REDIRECT_URI);
|
|
134
|
+
} finally {
|
|
135
|
+
callback.server.close();
|
|
136
|
+
}
|
|
137
|
+
}
|
package/dist/codex.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// Codex Responses transport adapted from @earendil-works/pi-ai (MIT).
|
|
2
|
+
import { platform, release, arch } from "node:os";
|
|
3
|
+
|
|
4
|
+
const BASE_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
5
|
+
const MODEL_DATA = [
|
|
6
|
+
["gpt-5.3-codex-spark", "GPT-5.3 Codex Spark", 1.75, 14],
|
|
7
|
+
["gpt-5.4", "GPT-5.4", 2.5, 15],
|
|
8
|
+
["gpt-5.4-mini", "GPT-5.4 mini", 0.75, 4.5],
|
|
9
|
+
["gpt-5.5", "GPT-5.5", 5, 30],
|
|
10
|
+
["gpt-5.6-luna", "GPT-5.6 Luna", 1, 6],
|
|
11
|
+
["gpt-5.6-sol", "GPT-5.6 Sol", 5, 30],
|
|
12
|
+
["gpt-5.6-terra", "GPT-5.6 Terra", 2.5, 15],
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const models = MODEL_DATA.map(([id, name, input, output]) => ({
|
|
16
|
+
id, name, provider: "openai-codex", api: "openai-codex-responses", baseUrl: BASE_URL, cost: { input, output }, input: ["text", "image"],
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
export function getModels(provider) {
|
|
20
|
+
return provider && provider !== "openai-codex" ? [] : models;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getModel(provider, id) {
|
|
24
|
+
return provider === "openai-codex" ? models.find((model) => model.id === id) : undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function accountId(token) {
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString("utf8"))?.["https://api.openai.com/auth"]?.chatgpt_account_id;
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function inputItems(context) {
|
|
36
|
+
const result = [];
|
|
37
|
+
for (const message of context.messages ?? []) {
|
|
38
|
+
if (message.role === "user") {
|
|
39
|
+
result.push({ role: "user", content: [{ type: "input_text", text: String(message.content ?? "") }] });
|
|
40
|
+
} else if (message.role === "assistant") {
|
|
41
|
+
for (const part of message.content ?? []) {
|
|
42
|
+
if (part.type === "text") result.push({ role: "assistant", content: [{ type: "output_text", text: part.text, annotations: [] }] });
|
|
43
|
+
if (part.type === "toolCall") {
|
|
44
|
+
const [callId, itemId] = part.id.split("|");
|
|
45
|
+
result.push({ type: "function_call", ...(itemId ? { id: itemId } : {}), call_id: callId, name: part.name, arguments: JSON.stringify(part.arguments ?? {}) });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} else if (message.role === "toolResult") {
|
|
49
|
+
const [callId] = message.toolCallId.split("|");
|
|
50
|
+
const text = (message.content ?? []).filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
51
|
+
result.push({ type: "function_call_output", call_id: callId, output: text || "(no tool output)" });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function requestBody(model, context, options) {
|
|
58
|
+
const body = {
|
|
59
|
+
model: model.id,
|
|
60
|
+
store: false,
|
|
61
|
+
stream: true,
|
|
62
|
+
instructions: context.systemPrompt || "You are a helpful assistant.",
|
|
63
|
+
input: inputItems(context),
|
|
64
|
+
text: { verbosity: "low" },
|
|
65
|
+
include: ["reasoning.encrypted_content"],
|
|
66
|
+
tool_choice: "auto",
|
|
67
|
+
parallel_tool_calls: true,
|
|
68
|
+
};
|
|
69
|
+
if (context.tools?.length) body.tools = context.tools.map((tool) => ({ type: "function", name: tool.name, description: tool.description, parameters: tool.parameters, strict: null }));
|
|
70
|
+
if (options.reasoningEffort) body.reasoning = { effort: options.reasoningEffort === "minimal" ? "low" : options.reasoningEffort, summary: "auto" };
|
|
71
|
+
return body;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function* sse(response, signal) {
|
|
75
|
+
const reader = response.body.getReader();
|
|
76
|
+
const decoder = new TextDecoder();
|
|
77
|
+
let buffer = "";
|
|
78
|
+
while (true) {
|
|
79
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
80
|
+
const { done, value } = await reader.read();
|
|
81
|
+
buffer += decoder.decode(value ?? new Uint8Array(), { stream: !done }).replaceAll("\r\n", "\n");
|
|
82
|
+
let boundary;
|
|
83
|
+
while ((boundary = buffer.indexOf("\n\n")) >= 0) {
|
|
84
|
+
const block = buffer.slice(0, boundary);
|
|
85
|
+
buffer = buffer.slice(boundary + 2);
|
|
86
|
+
const data = block.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trim()).join("\n");
|
|
87
|
+
if (data && data !== "[DONE]") yield JSON.parse(data);
|
|
88
|
+
}
|
|
89
|
+
if (done) break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function usageFrom(response) {
|
|
94
|
+
const usage = response?.usage ?? {};
|
|
95
|
+
const cached = usage.input_tokens_details?.cached_tokens ?? 0;
|
|
96
|
+
const cacheWrite = usage.input_tokens_details?.cache_write_tokens ?? 0;
|
|
97
|
+
return { input: Math.max(0, (usage.input_tokens ?? 0) - cached - cacheWrite), output: usage.output_tokens ?? 0, cacheRead: cached, cacheWrite, totalTokens: usage.total_tokens ?? 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function stream(model, context, options = {}) {
|
|
101
|
+
return (async function* () {
|
|
102
|
+
const output = { role: "assistant", content: [], api: model.api, provider: model.provider, model: model.id, usage: usageFrom(), stopReason: "stop", timestamp: Date.now() };
|
|
103
|
+
try {
|
|
104
|
+
const id = accountId(options.apiKey);
|
|
105
|
+
if (!id) throw new Error("Failed to extract the ChatGPT account ID from the access token");
|
|
106
|
+
let body = requestBody(model, context, options);
|
|
107
|
+
body = (await options.onPayload?.(body, model)) ?? body;
|
|
108
|
+
const response = await fetch(model.baseUrl, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: {
|
|
111
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
112
|
+
"chatgpt-account-id": id,
|
|
113
|
+
originator: "pi",
|
|
114
|
+
"user-agent": `service-llm (${platform()} ${release()}; ${arch()})`,
|
|
115
|
+
"openai-beta": "responses=experimental",
|
|
116
|
+
accept: "text/event-stream",
|
|
117
|
+
"content-type": "application/json",
|
|
118
|
+
},
|
|
119
|
+
body: JSON.stringify(body),
|
|
120
|
+
signal: options.signal,
|
|
121
|
+
});
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
const detail = await response.text();
|
|
124
|
+
let message = detail || response.statusText;
|
|
125
|
+
try { message = JSON.parse(detail)?.error?.message || message; } catch { /* text error */ }
|
|
126
|
+
throw new Error(`Codex error: ${message}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const slots = new Map();
|
|
130
|
+
let terminal;
|
|
131
|
+
for await (const event of sse(response, options.signal)) {
|
|
132
|
+
if (event.type === "error") throw new Error(`Codex error: ${event.error?.message || event.message || JSON.stringify(event)}`);
|
|
133
|
+
if (event.type === "response.failed") throw new Error(event.response?.error?.message || "Codex response failed");
|
|
134
|
+
if (event.type === "response.output_item.added") {
|
|
135
|
+
if (event.item.type === "message") {
|
|
136
|
+
const part = { type: "text", text: "" };
|
|
137
|
+
output.content.push(part);
|
|
138
|
+
slots.set(event.output_index, part);
|
|
139
|
+
} else if (event.item.type === "function_call") {
|
|
140
|
+
const part = { type: "toolCall", id: `${event.item.call_id}|${event.item.id}`, name: event.item.name, arguments: {}, partialJson: event.item.arguments || "" };
|
|
141
|
+
output.content.push(part);
|
|
142
|
+
slots.set(event.output_index, part);
|
|
143
|
+
}
|
|
144
|
+
} else if (event.type === "response.output_text.delta") {
|
|
145
|
+
const part = slots.get(event.output_index);
|
|
146
|
+
if (part?.type === "text") { part.text += event.delta; yield { type: "text_delta", delta: event.delta, partial: output }; }
|
|
147
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
148
|
+
const part = slots.get(event.output_index);
|
|
149
|
+
if (part?.type === "toolCall") part.partialJson += event.delta;
|
|
150
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
151
|
+
const part = slots.get(event.output_index);
|
|
152
|
+
if (part?.type === "toolCall") part.partialJson = event.arguments;
|
|
153
|
+
} else if (event.type === "response.output_item.done") {
|
|
154
|
+
let part = slots.get(event.output_index);
|
|
155
|
+
if (!part && event.item.type === "function_call") {
|
|
156
|
+
part = { type: "toolCall", id: `${event.item.call_id}|${event.item.id}`, name: event.item.name, arguments: {} };
|
|
157
|
+
output.content.push(part);
|
|
158
|
+
}
|
|
159
|
+
if (part?.type === "text") {
|
|
160
|
+
const finalText = event.item.content?.map((item) => item.text ?? item.refusal ?? "").join("") ?? part.text;
|
|
161
|
+
if (!part.text && finalText) yield { type: "text_delta", delta: finalText, partial: output };
|
|
162
|
+
part.text = finalText;
|
|
163
|
+
} else if (part?.type === "toolCall") {
|
|
164
|
+
part.name = event.item.name ?? part.name;
|
|
165
|
+
part.arguments = JSON.parse(event.item.arguments || part.partialJson || "{}");
|
|
166
|
+
delete part.partialJson;
|
|
167
|
+
yield { type: "toolcall_end", toolCall: part, partial: output };
|
|
168
|
+
}
|
|
169
|
+
slots.delete(event.output_index);
|
|
170
|
+
} else if (["response.completed", "response.done", "response.incomplete"].includes(event.type)) {
|
|
171
|
+
terminal = event.response;
|
|
172
|
+
output.usage = usageFrom(terminal);
|
|
173
|
+
output.stopReason = terminal?.status === "incomplete" ? "length" : output.content.some((part) => part.type === "toolCall") ? "toolUse" : "stop";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!terminal) throw new Error("Codex stream ended before a terminal response");
|
|
177
|
+
yield { type: "done", reason: output.stopReason, message: output };
|
|
178
|
+
} catch (error) {
|
|
179
|
+
output.stopReason = options.signal?.aborted ? "aborted" : "error";
|
|
180
|
+
output.errorMessage = error.message ?? String(error);
|
|
181
|
+
yield { type: "error", reason: output.stopReason, error: output };
|
|
182
|
+
}
|
|
183
|
+
})();
|
|
184
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leoustc/service-llm",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Standalone CLI service exposing a Codex subscription through OpenAI-compatible and MCP endpoints.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -12,16 +12,15 @@
|
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"dist",
|
|
15
|
-
"VERSION"
|
|
15
|
+
"VERSION",
|
|
16
|
+
"THIRD_PARTY_NOTICES.md"
|
|
16
17
|
],
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "node ./scripts/build.mjs",
|
|
19
20
|
"clean": "node ./scripts/clean.mjs",
|
|
20
21
|
"prepublishOnly": "npm run build"
|
|
21
22
|
},
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"@earendil-works/pi-ai": "0.80.6"
|
|
24
|
-
},
|
|
23
|
+
"dependencies": {},
|
|
25
24
|
"engines": {
|
|
26
25
|
"node": ">=22.19.0"
|
|
27
26
|
},
|