@nextclaw/nextclaw-ncp-runtime-adapter-hermes-http 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 +21 -0
- package/README.md +53 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +48 -0
- package/dist/hermes-http-adapter-config.utils.d.ts +16 -0
- package/dist/hermes-http-adapter-config.utils.js +59 -0
- package/dist/hermes-http-adapter-message.utils.d.ts +83 -0
- package/dist/hermes-http-adapter-message.utils.js +458 -0
- package/dist/hermes-http-adapter-session-store.service.js +105 -0
- package/dist/hermes-http-adapter.service.d.ts +14 -0
- package/dist/hermes-http-adapter.service.js +454 -0
- package/dist/hermes-http-adapter.types.d.ts +26 -0
- package/dist/hermes-openai-stream-parser.utils.d.ts +6 -0
- package/dist/hermes-openai-stream-parser.utils.js +54 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/package.json +37 -0
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# NextClaw Hermes HTTP Adapter
|
|
2
|
+
|
|
3
|
+
This package exposes a standalone NCP-over-HTTP adapter for Hermes API Server.
|
|
4
|
+
|
|
5
|
+
It is intentionally not a Hermes-specific NextClaw runtime. NextClaw keeps using the generic `http-runtime` kind, while this server translates that runtime contract into Hermes' OpenAI-compatible `/v1/chat/completions` streaming API.
|
|
6
|
+
|
|
7
|
+
## Start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm -C packages/nextclaw-ncp-runtime-adapter-hermes-http build
|
|
11
|
+
nextclaw-hermes-http-adapter \
|
|
12
|
+
--port 8765 \
|
|
13
|
+
--hermes-base-url http://127.0.0.1:8642 \
|
|
14
|
+
--api-key change-me-local-dev
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Environment variables are also supported:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
NEXTCLAW_HERMES_ADAPTER_PORT=8765
|
|
21
|
+
HERMES_API_BASE_URL=http://127.0.0.1:8642
|
|
22
|
+
HERMES_API_KEY=change-me-local-dev
|
|
23
|
+
HERMES_MODEL=hermes-agent
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## NextClaw Runtime Entry
|
|
27
|
+
|
|
28
|
+
Point a `type: "narp-http"` runtime entry at this adapter:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"label": "Hermes",
|
|
33
|
+
"type": "narp-http",
|
|
34
|
+
"config": {
|
|
35
|
+
"baseUrl": "http://127.0.0.1:8765",
|
|
36
|
+
"basePath": "/ncp/agent",
|
|
37
|
+
"healthcheckUrl": "http://127.0.0.1:8765/health",
|
|
38
|
+
"recommendedModel": "hermes-agent",
|
|
39
|
+
"supportedModels": ["hermes-agent"]
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Contract
|
|
45
|
+
|
|
46
|
+
The adapter exposes:
|
|
47
|
+
|
|
48
|
+
- `GET /health`
|
|
49
|
+
- `POST /ncp/agent/send`
|
|
50
|
+
- `GET /ncp/agent/stream?sessionId=...`
|
|
51
|
+
- `POST /ncp/agent/abort`
|
|
52
|
+
|
|
53
|
+
Hermes conversation continuity is preserved through `X-Hermes-Session-Id`, stored per NextClaw `sessionId`.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { HermesHttpAdapterServer } from "./hermes-http-adapter.service.js";
|
|
3
|
+
//#region src/cli.ts
|
|
4
|
+
var HermesHttpAdapterCli = class {
|
|
5
|
+
run = async () => {
|
|
6
|
+
const server = new HermesHttpAdapterServer(this.readArgs());
|
|
7
|
+
await server.start();
|
|
8
|
+
process.stdout.write(`[hermes-http-adapter] listening on http://${server.config.host}:${server.config.port}${server.config.basePath}\n`);
|
|
9
|
+
const shutdown = async () => {
|
|
10
|
+
await server.stop();
|
|
11
|
+
process.exit(0);
|
|
12
|
+
};
|
|
13
|
+
process.once("SIGINT", () => {
|
|
14
|
+
shutdown();
|
|
15
|
+
});
|
|
16
|
+
process.once("SIGTERM", () => {
|
|
17
|
+
shutdown();
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
readArgs = () => {
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
const config = {};
|
|
23
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
24
|
+
const current = args[index];
|
|
25
|
+
if (!current?.startsWith("--")) continue;
|
|
26
|
+
const key = current.slice(2);
|
|
27
|
+
const nextValue = args[index + 1];
|
|
28
|
+
if (!nextValue || nextValue.startsWith("--")) {
|
|
29
|
+
config[key] = true;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
config[key] = nextValue;
|
|
33
|
+
index += 1;
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
host: config.host,
|
|
37
|
+
port: config.port,
|
|
38
|
+
basePath: config["base-path"],
|
|
39
|
+
hermesBaseUrl: config["hermes-base-url"],
|
|
40
|
+
hermesApiKey: config["api-key"],
|
|
41
|
+
model: config.model,
|
|
42
|
+
systemPrompt: config["system-prompt"]
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
new HermesHttpAdapterCli().run();
|
|
47
|
+
//#endregion
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { HermesHttpAdapterConfig, HermesHttpAdapterFetchLike, HermesHttpAdapterResolvedConfig } from "./hermes-http-adapter.types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hermes-http-adapter-config.utils.d.ts
|
|
4
|
+
type HermesHttpAdapterConfigInput = Partial<Omit<HermesHttpAdapterConfig, "fetchImpl">> & {
|
|
5
|
+
fetchImpl?: HermesHttpAdapterFetchLike;
|
|
6
|
+
};
|
|
7
|
+
declare class HermesHttpAdapterConfigResolver {
|
|
8
|
+
private readonly source;
|
|
9
|
+
constructor(source?: HermesHttpAdapterConfigInput);
|
|
10
|
+
resolve: () => HermesHttpAdapterResolvedConfig;
|
|
11
|
+
}
|
|
12
|
+
declare function normalizeBasePath(value: string): string;
|
|
13
|
+
declare function resolveHermesApiUrl(baseUrl: string, path: string): string;
|
|
14
|
+
declare function resolveHermesHealthcheckUrl(baseUrl: string): string;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { HermesHttpAdapterConfigInput, HermesHttpAdapterConfigResolver, normalizeBasePath, resolveHermesApiUrl, resolveHermesHealthcheckUrl };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/hermes-http-adapter-config.utils.ts
|
|
2
|
+
const DEFAULT_HOST = "127.0.0.1";
|
|
3
|
+
const DEFAULT_PORT = 8765;
|
|
4
|
+
const DEFAULT_BASE_PATH = "/ncp/agent";
|
|
5
|
+
const DEFAULT_HERMES_BASE_URL = "http://127.0.0.1:8642";
|
|
6
|
+
const DEFAULT_MODEL = "hermes-agent";
|
|
7
|
+
const DEFAULT_STREAM_WAIT_TIMEOUT_MS = 15e3;
|
|
8
|
+
const DEFAULT_HEALTHCHECK_TIMEOUT_MS = 3e3;
|
|
9
|
+
var HermesHttpAdapterConfigResolver = class {
|
|
10
|
+
constructor(source = {}) {
|
|
11
|
+
this.source = source;
|
|
12
|
+
}
|
|
13
|
+
resolve = () => {
|
|
14
|
+
const basePath = normalizeBasePath(this.source.basePath ?? process.env.NEXTCLAW_HERMES_ADAPTER_BASE_PATH ?? DEFAULT_BASE_PATH);
|
|
15
|
+
const hermesBaseUrl = readString(this.source.hermesBaseUrl ?? process.env.HERMES_API_BASE_URL) ?? DEFAULT_HERMES_BASE_URL;
|
|
16
|
+
return {
|
|
17
|
+
host: readString(this.source.host ?? process.env.NEXTCLAW_HERMES_ADAPTER_HOST) ?? DEFAULT_HOST,
|
|
18
|
+
port: readPositiveInteger(this.source.port) ?? readPositiveInteger(process.env.NEXTCLAW_HERMES_ADAPTER_PORT) ?? DEFAULT_PORT,
|
|
19
|
+
basePath,
|
|
20
|
+
hermesBaseUrl,
|
|
21
|
+
hermesApiKey: readString(this.source.hermesApiKey ?? process.env.HERMES_API_KEY),
|
|
22
|
+
model: readString(this.source.model ?? process.env.HERMES_MODEL) ?? DEFAULT_MODEL,
|
|
23
|
+
systemPrompt: readString(this.source.systemPrompt ?? process.env.HERMES_SYSTEM_PROMPT),
|
|
24
|
+
streamWaitTimeoutMs: readPositiveInteger(this.source.streamWaitTimeoutMs) ?? readPositiveInteger(process.env.NEXTCLAW_HERMES_ADAPTER_STREAM_WAIT_TIMEOUT_MS) ?? DEFAULT_STREAM_WAIT_TIMEOUT_MS,
|
|
25
|
+
healthcheckTimeoutMs: readPositiveInteger(this.source.healthcheckTimeoutMs) ?? readPositiveInteger(process.env.NEXTCLAW_HERMES_ADAPTER_HEALTHCHECK_TIMEOUT_MS) ?? DEFAULT_HEALTHCHECK_TIMEOUT_MS,
|
|
26
|
+
...this.source.fetchImpl ? { fetchImpl: this.source.fetchImpl } : {},
|
|
27
|
+
chatCompletionsUrl: resolveHermesApiUrl(hermesBaseUrl, "chat/completions"),
|
|
28
|
+
healthcheckUrl: resolveHermesHealthcheckUrl(hermesBaseUrl)
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
function normalizeBasePath(value) {
|
|
33
|
+
const trimmed = value.trim();
|
|
34
|
+
if (!trimmed) return DEFAULT_BASE_PATH;
|
|
35
|
+
const withLeadingSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
36
|
+
return withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
|
|
37
|
+
}
|
|
38
|
+
function resolveHermesApiUrl(baseUrl, path) {
|
|
39
|
+
const normalizedBase = baseUrl.trim().replace(/\/+$/, "");
|
|
40
|
+
const normalizedPath = path.replace(/^\/+/, "");
|
|
41
|
+
return `${normalizedBase}${/\/v1$/u.test(normalizedBase) ? "" : "/v1"}/${normalizedPath}`;
|
|
42
|
+
}
|
|
43
|
+
function resolveHermesHealthcheckUrl(baseUrl) {
|
|
44
|
+
const normalizedBase = baseUrl.trim().replace(/\/+$/, "");
|
|
45
|
+
return `${normalizedBase.endsWith("/v1") ? normalizedBase.slice(0, -3) : normalizedBase}/health`;
|
|
46
|
+
}
|
|
47
|
+
function readString(value) {
|
|
48
|
+
if (typeof value !== "string") return;
|
|
49
|
+
const trimmed = value.trim();
|
|
50
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
51
|
+
}
|
|
52
|
+
function readPositiveInteger(value) {
|
|
53
|
+
const parsed = typeof value === "number" ? value : typeof value === "string" ? Number.parseInt(value, 10) : NaN;
|
|
54
|
+
if (!Number.isFinite(parsed)) return;
|
|
55
|
+
const normalized = Math.trunc(parsed);
|
|
56
|
+
return normalized > 0 ? normalized : void 0;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { HermesHttpAdapterConfigResolver, normalizeBasePath, resolveHermesApiUrl, resolveHermesHealthcheckUrl };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { HermesOpenAIMessage } from "./hermes-http-adapter.types.js";
|
|
2
|
+
import { NcpEndpointEvent, NcpError, NcpMessage, NcpMessagePart, NcpRequestEnvelope } from "@nextclaw/ncp";
|
|
3
|
+
|
|
4
|
+
//#region src/hermes-http-adapter-message.utils.d.ts
|
|
5
|
+
type HermesProviderRoute = {
|
|
6
|
+
model: string;
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
apiBase?: string;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
apiMode?: "chat_completions" | "codex_responses" | "anthropic_messages";
|
|
11
|
+
};
|
|
12
|
+
declare function readHermesProviderRoute(envelope: NcpRequestEnvelope): HermesProviderRoute | undefined;
|
|
13
|
+
declare function buildHermesMessages(params: {
|
|
14
|
+
envelope: NcpRequestEnvelope;
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
}): HermesOpenAIMessage[];
|
|
17
|
+
declare function createAssistantMessage(params: {
|
|
18
|
+
sessionId: string;
|
|
19
|
+
messageId: string;
|
|
20
|
+
text: string;
|
|
21
|
+
timestamp: string;
|
|
22
|
+
metadata?: Record<string, unknown>;
|
|
23
|
+
}): NcpMessage;
|
|
24
|
+
declare function createAssistantMessageFromParts(params: {
|
|
25
|
+
sessionId: string;
|
|
26
|
+
messageId: string;
|
|
27
|
+
parts: NcpMessagePart[];
|
|
28
|
+
timestamp: string;
|
|
29
|
+
metadata?: Record<string, unknown>;
|
|
30
|
+
}): NcpMessage;
|
|
31
|
+
declare class HermesAssistantEventCollector {
|
|
32
|
+
private readonly parts;
|
|
33
|
+
private readonly toolPartIndexByCallId;
|
|
34
|
+
applyEvent: (event: NcpEndpointEvent) => void;
|
|
35
|
+
hasParts: () => boolean;
|
|
36
|
+
buildParts: () => NcpMessagePart[];
|
|
37
|
+
private appendTextPart;
|
|
38
|
+
private appendReasoningPart;
|
|
39
|
+
private applyToolCallArgsDelta;
|
|
40
|
+
private upsertToolInvocationPart;
|
|
41
|
+
private readToolArgs;
|
|
42
|
+
private readToolName;
|
|
43
|
+
private findToolPart;
|
|
44
|
+
}
|
|
45
|
+
declare class HermesInlineToolTraceTranslator {
|
|
46
|
+
private pendingTextBuffer;
|
|
47
|
+
private sawStructuredToolCall;
|
|
48
|
+
private toolCallCount;
|
|
49
|
+
translate: (this: HermesInlineToolTraceTranslator, events: AsyncIterable<NcpEndpointEvent>) => AsyncGenerator<NcpEndpointEvent>;
|
|
50
|
+
private flushPendingText;
|
|
51
|
+
private shouldFlushPendingText;
|
|
52
|
+
private isStructuredToolCallEvent;
|
|
53
|
+
private readEventSessionId;
|
|
54
|
+
private readEventMessageId;
|
|
55
|
+
}
|
|
56
|
+
declare class HermesReasoningDeltaTranslator {
|
|
57
|
+
private readonly normalizer;
|
|
58
|
+
translate: (this: HermesReasoningDeltaTranslator, events: AsyncIterable<NcpEndpointEvent>) => AsyncGenerator<NcpEndpointEvent>;
|
|
59
|
+
private emitNormalizedTextDelta;
|
|
60
|
+
private flushPendingText;
|
|
61
|
+
private flushSegments;
|
|
62
|
+
private shouldFlushPendingText;
|
|
63
|
+
private readEventSessionId;
|
|
64
|
+
private readEventMessageId;
|
|
65
|
+
}
|
|
66
|
+
declare function resolveHermesModel(params: {
|
|
67
|
+
envelope: NcpRequestEnvelope;
|
|
68
|
+
fallbackModel: string;
|
|
69
|
+
}): string;
|
|
70
|
+
declare function normalizeHermesRequestedModel(model: string): string;
|
|
71
|
+
declare function inferHermesProvider(params: {
|
|
72
|
+
model: string;
|
|
73
|
+
apiBase?: string | null;
|
|
74
|
+
apiMode?: HermesProviderRoute["apiMode"];
|
|
75
|
+
}): string | undefined;
|
|
76
|
+
declare function toNcpError(error: unknown, code: NcpError["code"]): NcpError;
|
|
77
|
+
declare function toNcpSseFrame(event: NcpEndpointEvent): string;
|
|
78
|
+
declare function toErrorSseFrame(error: {
|
|
79
|
+
code: string;
|
|
80
|
+
message: string;
|
|
81
|
+
}): string;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { HermesAssistantEventCollector, HermesInlineToolTraceTranslator, HermesProviderRoute, HermesReasoningDeltaTranslator, buildHermesMessages, createAssistantMessage, createAssistantMessageFromParts, inferHermesProvider, normalizeHermesRequestedModel, readHermesProviderRoute, resolveHermesModel, toErrorSseFrame, toNcpError, toNcpSseFrame };
|