@nextclaw/nextclaw-narp-runtime-codex-sdk 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 NextClaw contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ import { CodexNarpRuntimeWrapper } from "../services/codex-narp-runtime-wrapper.service.js";
3
+ //#region src/controllers/codex-narp.controller.ts
4
+ new CodexNarpRuntimeWrapper().start();
5
+ //#endregion
6
+ export {};
@@ -0,0 +1,2 @@
1
+ import { CodexNarpRuntimeFactory, CodexNarpRuntimeWrapper } from "./services/codex-narp-runtime-wrapper.service.js";
2
+ export { type CodexNarpRuntimeFactory, CodexNarpRuntimeWrapper };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import { CodexNarpRuntimeWrapper } from "./services/codex-narp-runtime-wrapper.service.js";
2
+ export { CodexNarpRuntimeWrapper };
@@ -0,0 +1,19 @@
1
+ import { NarpStdioRuntimeWrapperContext } from "@nextclaw/nextclaw-narp-stdio-runtime-wrapper";
2
+ import { CodexSdkNcpAgentRuntimeConfig } from "@nextclaw/nextclaw-ncp-runtime-codex-sdk";
3
+ import { CodexOpenAiResponsesBridgeConfig, CodexOpenAiResponsesBridgeResult } from "@nextclaw/nextclaw-ncp-runtime-plugin-codex-sdk";
4
+ import { NcpAgentRuntime } from "@nextclaw/ncp";
5
+
6
+ //#region src/services/codex-narp-runtime-wrapper.service.d.ts
7
+ type CodexNarpRuntimeFactory = (config: CodexSdkNcpAgentRuntimeConfig) => NcpAgentRuntime;
8
+ type CodexResponsesBridgeFactory = (config: CodexOpenAiResponsesBridgeConfig) => Promise<CodexOpenAiResponsesBridgeResult>;
9
+ declare class CodexNarpRuntimeWrapper {
10
+ private readonly createRuntime;
11
+ private readonly ensureResponsesBridge;
12
+ constructor(createRuntime?: CodexNarpRuntimeFactory, ensureResponsesBridge?: CodexResponsesBridgeFactory);
13
+ start: () => void;
14
+ createCodexRuntime: (context: NarpStdioRuntimeWrapperContext) => NcpAgentRuntime;
15
+ buildRuntimeConfig: (context: NarpStdioRuntimeWrapperContext) => Promise<CodexSdkNcpAgentRuntimeConfig>;
16
+ private resolveRuntimeConfig;
17
+ }
18
+ //#endregion
19
+ export { CodexNarpRuntimeFactory, CodexNarpRuntimeWrapper };
@@ -0,0 +1,168 @@
1
+ import { NarpStdioRuntimeWrapper } from "@nextclaw/nextclaw-narp-stdio-runtime-wrapper";
2
+ import { CodexSdkNcpAgentRuntime } from "@nextclaw/nextclaw-ncp-runtime-codex-sdk";
3
+ import { buildCodexBridgeModelProviderId, ensureCodexOpenAiResponsesBridge } from "@nextclaw/nextclaw-ncp-runtime-plugin-codex-sdk";
4
+ //#region src/services/codex-narp-runtime-wrapper.service.ts
5
+ const NARP_API_MODE_HEADER = "x-nextclaw-narp-api-mode";
6
+ var DeferredCodexNarpRuntime = class {
7
+ runtimePromise = null;
8
+ constructor(createRuntime) {
9
+ this.createRuntime = createRuntime;
10
+ }
11
+ run = async function* (input, options) {
12
+ if (!this.runtimePromise) this.runtimePromise = this.createRuntime();
13
+ yield* (await this.runtimePromise).run(input, options);
14
+ };
15
+ };
16
+ var CodexNarpRuntimeWrapper = class {
17
+ constructor(createRuntime = (config) => new CodexSdkNcpAgentRuntime(config), ensureResponsesBridge = ensureCodexOpenAiResponsesBridge) {
18
+ this.createRuntime = createRuntime;
19
+ this.ensureResponsesBridge = ensureResponsesBridge;
20
+ }
21
+ start = () => {
22
+ new NarpStdioRuntimeWrapper({
23
+ agentName: "NextClaw Codex NARP",
24
+ createRuntime: (context) => this.createCodexRuntime(context)
25
+ }).start();
26
+ };
27
+ createCodexRuntime = (context) => {
28
+ return new DeferredCodexNarpRuntime(async () => this.createRuntime(await this.buildRuntimeConfig(context)));
29
+ };
30
+ buildRuntimeConfig = (context) => {
31
+ const { cwd, modelId, promptMeta, sessionId } = context;
32
+ const providerRoute = promptMeta.providerRoute;
33
+ const sessionMetadata = promptMeta.sessionMetadata ?? {};
34
+ const providerLocalModel = stripProviderPrefix(readString(providerRoute?.model)) ?? stripProviderPrefix(readString(modelId)) ?? readString(process.env.NEXTCLAW_MODEL);
35
+ const upstreamApiBase = readString(providerRoute?.apiBase) ?? readString(process.env.NEXTCLAW_API_BASE);
36
+ const externalModelProvider = resolveExternalModelProvider({
37
+ apiBase: upstreamApiBase,
38
+ modelId
39
+ });
40
+ return this.resolveRuntimeConfig({
41
+ apiKey: readString(providerRoute?.apiKey) ?? readString(process.env.NEXTCLAW_API_KEY) ?? "",
42
+ cwd,
43
+ externalModelProvider,
44
+ providerLocalModel,
45
+ sessionId,
46
+ sessionMetadata,
47
+ threadModel: readString(modelId) ?? composeModelRoute({
48
+ modelProvider: externalModelProvider,
49
+ providerLocalModel
50
+ }) ?? providerLocalModel,
51
+ upstreamApiBase,
52
+ upstreamExtraHeaders: providerRoute?.headers
53
+ });
54
+ };
55
+ resolveRuntimeConfig = async (params) => {
56
+ const { apiKey, cwd, externalModelProvider, providerLocalModel, sessionId, sessionMetadata, threadModel: requestedThreadModel, upstreamApiBase, upstreamExtraHeaders } = params;
57
+ const bridgeModelProvider = upstreamApiBase && shouldUseResponsesBridge({
58
+ apiBase: upstreamApiBase,
59
+ headers: upstreamExtraHeaders
60
+ }) ? buildCodexBridgeModelProviderId(externalModelProvider ?? "chat") : void 0;
61
+ const apiBase = (bridgeModelProvider ? await this.ensureResponsesBridge({
62
+ upstreamApiBase: upstreamApiBase ?? "",
63
+ upstreamApiKey: apiKey,
64
+ upstreamExtraHeaders: stripInternalRouteHeaders(upstreamExtraHeaders),
65
+ defaultModel: providerLocalModel,
66
+ upstreamReasoningSplit: isMiniMaxApiBase(upstreamApiBase ?? ""),
67
+ modelPrefixes: [externalModelProvider ?? "", bridgeModelProvider]
68
+ }) : null)?.baseUrl ?? upstreamApiBase;
69
+ const modelProvider = bridgeModelProvider ?? externalModelProvider;
70
+ const codexPathOverride = readString(process.env.NEXTCLAW_CODEX_PATH) ?? readString(process.env.CODEX_PATH);
71
+ const modelReasoningEffort = readReasoningEffort(sessionMetadata.preferred_thinking) ?? readReasoningEffort(sessionMetadata.thinking);
72
+ const threadModel = bridgeModelProvider ? composeModelRoute({
73
+ modelProvider: bridgeModelProvider,
74
+ providerLocalModel
75
+ }) : requestedThreadModel;
76
+ return {
77
+ sessionId,
78
+ apiKey,
79
+ apiBase,
80
+ model: providerLocalModel,
81
+ threadId: readString(sessionMetadata.codex_thread_id) ?? null,
82
+ ...codexPathOverride ? { codexPathOverride } : {},
83
+ sessionMetadata,
84
+ cliConfig: buildCodexCliConfig({
85
+ apiBase,
86
+ modelProvider
87
+ }),
88
+ threadOptions: {
89
+ ...threadModel ? { model: threadModel } : {},
90
+ workingDirectory: cwd ?? process.cwd(),
91
+ skipGitRepoCheck: true,
92
+ ...modelReasoningEffort ? { modelReasoningEffort } : {}
93
+ }
94
+ };
95
+ };
96
+ };
97
+ function readString(value) {
98
+ if (typeof value !== "string") return;
99
+ const trimmed = value.trim();
100
+ return trimmed.length > 0 ? trimmed : void 0;
101
+ }
102
+ function stripProviderPrefix(value) {
103
+ const normalized = readString(value);
104
+ if (!normalized) return;
105
+ const slashIndex = normalized.indexOf("/");
106
+ if (slashIndex <= 0) return normalized;
107
+ return readString(normalized.slice(slashIndex + 1));
108
+ }
109
+ function readReasoningEffort(value) {
110
+ if (typeof value !== "string") return;
111
+ const normalized = value.trim().toLowerCase();
112
+ if (normalized === "minimal" || normalized === "low" || normalized === "medium" || normalized === "high" || normalized === "xhigh") return normalized;
113
+ }
114
+ function readModelProvider(value) {
115
+ const normalized = readString(value);
116
+ if (!normalized) return;
117
+ const slashIndex = normalized.indexOf("/");
118
+ if (slashIndex <= 0) return;
119
+ return readString(normalized.slice(0, slashIndex));
120
+ }
121
+ function composeModelRoute(params) {
122
+ const { modelProvider, providerLocalModel } = params;
123
+ if (!modelProvider || !providerLocalModel) return;
124
+ return `${modelProvider}/${providerLocalModel}`;
125
+ }
126
+ function buildCodexCliConfig(params) {
127
+ const { apiBase, modelProvider } = params;
128
+ if (!modelProvider) return;
129
+ const config = {
130
+ model_provider: modelProvider,
131
+ preferred_auth_method: "apikey"
132
+ };
133
+ if (apiBase) config.model_providers = { [modelProvider]: {
134
+ name: modelProvider,
135
+ base_url: apiBase,
136
+ wire_api: "responses",
137
+ requires_openai_auth: true
138
+ } };
139
+ return config;
140
+ }
141
+ function readApiMode(headers) {
142
+ for (const [key, value] of Object.entries(headers ?? {})) if (key.toLowerCase() === NARP_API_MODE_HEADER) return readString(value)?.toLowerCase();
143
+ }
144
+ function shouldUseResponsesBridge(params) {
145
+ if (readApiMode(params.headers) === "chat_completions") return true;
146
+ return Boolean(params.apiBase && isMiniMaxApiBase(params.apiBase));
147
+ }
148
+ function stripInternalRouteHeaders(headers) {
149
+ const out = {};
150
+ for (const [key, value] of Object.entries(headers ?? {})) {
151
+ if (key.toLowerCase() === NARP_API_MODE_HEADER) continue;
152
+ out[key] = value;
153
+ }
154
+ return Object.keys(out).length > 0 ? out : void 0;
155
+ }
156
+ function resolveExternalModelProvider(params) {
157
+ return readModelProvider(params.modelId) ?? (params.apiBase && isMiniMaxApiBase(params.apiBase) ? "minimax" : void 0);
158
+ }
159
+ function isMiniMaxApiBase(apiBase) {
160
+ try {
161
+ const host = new URL(apiBase).hostname.toLowerCase();
162
+ return host === "api.minimaxi.com" || host.endsWith(".minimaxi.com");
163
+ } catch {
164
+ return apiBase.toLowerCase().includes("api.minimaxi.com");
165
+ }
166
+ }
167
+ //#endregion
168
+ export { CodexNarpRuntimeWrapper };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@nextclaw/nextclaw-narp-runtime-codex-sdk",
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "description": "NARP stdio launcher for the NextClaw Codex SDK runtime.",
6
+ "type": "module",
7
+ "bin": {
8
+ "nextclaw-codex-narp": "dist/controllers/codex-narp.controller.js"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "development": "./src/index.ts",
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md"
20
+ ],
21
+ "dependencies": {
22
+ "@nextclaw/nextclaw-ncp-runtime-plugin-codex-sdk": "0.1.58",
23
+ "@nextclaw/ncp": "0.5.7",
24
+ "@nextclaw/nextclaw-narp-stdio-runtime-wrapper": "0.1.1",
25
+ "@nextclaw/nextclaw-ncp-runtime-codex-sdk": "0.1.24"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^20.17.6",
29
+ "typescript": "^5.6.3",
30
+ "vitest": "^4.1.2"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown src/index.ts src/controllers/codex-narp.controller.ts --dts --clean --target es2022 --no-fixedExtension --unbundle",
34
+ "lint": "eslint .",
35
+ "tsc": "tsc -p tsconfig.json",
36
+ "test": "vitest run"
37
+ }
38
+ }