@labacacia/nps-sdk 1.0.0-alpha.1 → 1.0.0-alpha.2
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/.npmrc.publish +1 -0
- package/CHANGELOG.cn.md +39 -0
- package/CHANGELOG.md +39 -0
- package/CONTRIBUTING.cn.md +35 -0
- package/CONTRIBUTING.md +2 -0
- package/README.cn.md +155 -0
- package/README.md +5 -3
- package/dist/core/frames.d.ts +1 -0
- package/dist/core/frames.d.ts.map +1 -1
- package/dist/core/frames.js +1 -0
- package/dist/core/frames.js.map +1 -1
- package/dist/core/index.d.ts +6 -4
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +17 -5
- package/dist/core/index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/ncp/frames.d.ts +18 -0
- package/dist/ncp/frames.d.ts.map +1 -1
- package/dist/ncp/frames.js +45 -0
- package/dist/ncp/frames.js.map +1 -1
- package/dist/ncp/registry.d.ts.map +1 -1
- package/dist/ncp/registry.js +2 -1
- package/dist/ncp/registry.js.map +1 -1
- package/doc/nps-sdk.core.cn.md +321 -0
- package/doc/nps-sdk.core.md +326 -0
- package/doc/nps-sdk.ncp.cn.md +270 -0
- package/doc/nps-sdk.ncp.md +276 -0
- package/doc/nps-sdk.ndp.cn.md +267 -0
- package/doc/nps-sdk.ndp.md +273 -0
- package/doc/nps-sdk.nip.cn.md +235 -0
- package/doc/nps-sdk.nip.md +242 -0
- package/doc/nps-sdk.nop.cn.md +329 -0
- package/doc/nps-sdk.nop.md +332 -0
- package/doc/nps-sdk.nwp.cn.md +217 -0
- package/doc/nps-sdk.nwp.md +224 -0
- package/doc/overview.cn.md +149 -0
- package/doc/overview.md +153 -0
- package/package.json +21 -4
- package/src/core/frames.ts +1 -0
- package/src/core/index.ts +37 -5
- package/src/index.ts +1 -1
- package/src/ncp/frames.ts +52 -0
- package/src/ncp/registry.ts +2 -1
package/doc/overview.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
English | [中文版](./overview.cn.md)
|
|
2
|
+
|
|
3
|
+
# `@labacacia/nps-sdk` — API Reference Overview
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@labacacia/nps-sdk)
|
|
6
|
+
|
|
7
|
+
The NPS TypeScript SDK is a dual-format (ESM + CJS) port of the .NET reference
|
|
8
|
+
implementation. This document is the entry point for the per-module API
|
|
9
|
+
references — each protocol has its own file below.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Package layout
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
@labacacia/nps-sdk
|
|
17
|
+
├── / # root: VERSION, createDefaultRegistry, createFullRegistry
|
|
18
|
+
├── /core # Wire primitives: FrameHeader, codecs, AnchorCache, errors
|
|
19
|
+
├── /ncp # NCP frames + handshake + stream manager
|
|
20
|
+
├── /nwp # NWP frames + async NwpClient
|
|
21
|
+
├── /nip # NIP frames + NipIdentity (Ed25519)
|
|
22
|
+
├── /ndp # NDP frames + InMemoryNdpRegistry + validator
|
|
23
|
+
└── /nop # NOP frames + TaskDag model + NopClient
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Reference documents
|
|
27
|
+
|
|
28
|
+
| Subpath | Module | Reference |
|
|
29
|
+
|---------|--------|-----------|
|
|
30
|
+
| — | Root helpers & registry factories | this file |
|
|
31
|
+
| `@labacacia/nps-sdk/core` | Frame header, codec, anchor cache, exceptions | [`nps-sdk.core.md`](./nps-sdk.core.md) |
|
|
32
|
+
| `@labacacia/nps-sdk/ncp` | NCP frame set (`AnchorFrame`, `DiffFrame`, `StreamFrame`, `CapsFrame`, `ErrorFrame`, `HelloFrame`) | [`nps-sdk.ncp.md`](./nps-sdk.ncp.md) |
|
|
33
|
+
| `@labacacia/nps-sdk/nwp` | `QueryFrame`, `ActionFrame`, `NwpClient` | [`nps-sdk.nwp.md`](./nps-sdk.nwp.md) |
|
|
34
|
+
| `@labacacia/nps-sdk/nip` | `IdentFrame`, `TrustFrame`, `RevokeFrame`, `NipIdentity` | [`nps-sdk.nip.md`](./nps-sdk.nip.md) |
|
|
35
|
+
| `@labacacia/nps-sdk/ndp` | `AnnounceFrame`, `ResolveFrame`, `GraphFrame`, registry, validator | [`nps-sdk.ndp.md`](./nps-sdk.ndp.md) |
|
|
36
|
+
| `@labacacia/nps-sdk/nop` | Task DAG, `TaskFrame`, `DelegateFrame`, `SyncFrame`, `AlignStreamFrame`, `NopClient` | [`nps-sdk.nop.md`](./nps-sdk.nop.md) |
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install @labacacia/nps-sdk
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Requires **Node.js 18+** (for Web Crypto) or a modern browser with native
|
|
47
|
+
`crypto.subtle`.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Root module
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { VERSION, createDefaultRegistry, createFullRegistry } from "@labacacia/nps-sdk";
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- `VERSION` — SDK version constant.
|
|
58
|
+
- `createDefaultRegistry()` — new `FrameRegistry` with NCP frames only
|
|
59
|
+
(`ANCHOR`, `DIFF`, `STREAM`, `CAPS`, `ERROR`).
|
|
60
|
+
- `createFullRegistry()` — new `FrameRegistry` with all five protocols
|
|
61
|
+
(NCP + NWP + NIP + NDP + NOP) pre-registered. Use this when decoding
|
|
62
|
+
arbitrary frames.
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
const registry = createFullRegistry();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Minimal end-to-end example
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { NwpClient, QueryFrame, ActionFrame } from "@labacacia/nps-sdk/nwp";
|
|
74
|
+
|
|
75
|
+
const client = new NwpClient("http://node.example.com:17433");
|
|
76
|
+
|
|
77
|
+
// Paginated query
|
|
78
|
+
const caps = await client.query(
|
|
79
|
+
new QueryFrame("sha256:<anchor-id>", { active: true }, 50),
|
|
80
|
+
);
|
|
81
|
+
console.log(caps.count, caps.data);
|
|
82
|
+
|
|
83
|
+
// Streaming query
|
|
84
|
+
for await (const chunk of client.stream(new QueryFrame("sha256:<anchor-id>"))) {
|
|
85
|
+
console.log(chunk.seq, chunk.data);
|
|
86
|
+
if (chunk.isLast) break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Action invocation
|
|
90
|
+
const result = await client.invoke(
|
|
91
|
+
new ActionFrame("summarise", { maxTokens: 500 }),
|
|
92
|
+
);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Encoding tiers
|
|
98
|
+
|
|
99
|
+
Every frame has a `preferredTier`. MsgPack is the production default; JSON
|
|
100
|
+
tier stays available for diagnostics.
|
|
101
|
+
|
|
102
|
+
| Tier | `EncodingTier` value | Description |
|
|
103
|
+
|------|---------------------|-------------|
|
|
104
|
+
| Tier-1 JSON | `0x00` | UTF-8 JSON. Development & compatibility. |
|
|
105
|
+
| Tier-2 MsgPack | `0x01` | MessagePack binary. **Production default** — ~60% smaller. |
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { EncodingTier } from "@labacacia/nps-sdk/core";
|
|
109
|
+
import { NpsFrameCodec } from "@labacacia/nps-sdk/core";
|
|
110
|
+
|
|
111
|
+
const codec = new NpsFrameCodec(createFullRegistry());
|
|
112
|
+
const wire = codec.encode(frame, { overrideTier: EncodingTier.JSON });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Async conventions
|
|
118
|
+
|
|
119
|
+
- All network-facing clients (`NwpClient`, `NopClient`) return `Promise<T>`.
|
|
120
|
+
- Streaming uses `AsyncGenerator<StreamFrame>` — consume with
|
|
121
|
+
`for await (const chunk of …) { … }`.
|
|
122
|
+
- Clients do **not** require explicit dispose — the underlying `fetch`
|
|
123
|
+
owns connection lifecycle.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Error hierarchy
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
Error
|
|
131
|
+
└── NpsError from "@labacacia/nps-sdk/core"
|
|
132
|
+
├── NpsFrameError — header parse / structural error
|
|
133
|
+
├── NpsCodecError — encode/decode failure
|
|
134
|
+
├── NpsAnchorNotFoundError — anchor not in cache
|
|
135
|
+
├── NpsAnchorPoisonError — anchor_id / schema mismatch
|
|
136
|
+
└── NpsStreamError — stream seq gap / unknown stream id
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`NcpError` (from `@labacacia/nps-sdk/core`) is a separate protocol-level
|
|
140
|
+
error carrying a machine-readable `code` (e.g. `NCP-ANCHOR-NOT-FOUND`,
|
|
141
|
+
`NCP-STREAM-SEQ-GAP`).
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Reference specs
|
|
146
|
+
|
|
147
|
+
| Module | Spec |
|
|
148
|
+
|--------|------|
|
|
149
|
+
| `core` + `ncp` | [NPS-1 NCP v0.4](https://github.com/labacacia/NPS-Release/blob/main/spec/NPS-1-NCP.md) |
|
|
150
|
+
| `nwp` | [NPS-2 NWP v0.4](https://github.com/labacacia/NPS-Release/blob/main/spec/NPS-2-NWP.md) |
|
|
151
|
+
| `nip` | [NPS-3 NIP v0.2](https://github.com/labacacia/NPS-Release/blob/main/spec/NPS-3-NIP.md) |
|
|
152
|
+
| `ndp` | [NPS-4 NDP v0.2](https://github.com/labacacia/NPS-Release/blob/main/spec/NPS-4-NDP.md) |
|
|
153
|
+
| `nop` | [NPS-5 NOP v0.3](https://github.com/labacacia/NPS-Release/blob/main/spec/NPS-5-NOP.md) |
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@labacacia/nps-sdk",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
4
|
-
"description": "TypeScript SDK for the Neural Protocol Suite (NPS)",
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
|
+
"description": "TypeScript SDK for the Neural Protocol Suite (NPS) — NCP, NWP, NIP, NDP, NOP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -17,6 +17,22 @@
|
|
|
17
17
|
"./ncp": {
|
|
18
18
|
"import": "./dist/ncp/index.js",
|
|
19
19
|
"types": "./dist/ncp/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./nwp": {
|
|
22
|
+
"import": "./dist/nwp/index.js",
|
|
23
|
+
"types": "./dist/nwp/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./nip": {
|
|
26
|
+
"import": "./dist/nip/index.js",
|
|
27
|
+
"types": "./dist/nip/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./ndp": {
|
|
30
|
+
"import": "./dist/ndp/index.js",
|
|
31
|
+
"types": "./dist/ndp/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./nop": {
|
|
34
|
+
"import": "./dist/nop/index.js",
|
|
35
|
+
"types": "./dist/nop/index.d.ts"
|
|
20
36
|
}
|
|
21
37
|
},
|
|
22
38
|
"scripts": {
|
|
@@ -27,8 +43,7 @@
|
|
|
27
43
|
},
|
|
28
44
|
"repository": {
|
|
29
45
|
"type": "git",
|
|
30
|
-
"url": "https://github.com/labacacia/
|
|
31
|
-
"directory": "impl/typescript"
|
|
46
|
+
"url": "https://github.com/labacacia/NPS-sdk-ts.git"
|
|
32
47
|
},
|
|
33
48
|
"license": "Apache-2.0",
|
|
34
49
|
"engines": {
|
|
@@ -36,6 +51,8 @@
|
|
|
36
51
|
},
|
|
37
52
|
"dependencies": {
|
|
38
53
|
"@msgpack/msgpack": "^3.0.0",
|
|
54
|
+
"@noble/ed25519": "^2.1.0",
|
|
55
|
+
"@noble/hashes": "^1.4.0",
|
|
39
56
|
"canonicalize": "^2.0.0",
|
|
40
57
|
"fast-json-patch": "^3.1.1"
|
|
41
58
|
},
|
package/src/core/frames.ts
CHANGED
package/src/core/index.ts
CHANGED
|
@@ -1,10 +1,42 @@
|
|
|
1
|
+
// Copyright 2026 INNO LOTUS PTY LTD
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
//
|
|
4
|
+
// @labacacia/nps-sdk/core — public re-exports for NCP wire primitives.
|
|
5
|
+
//
|
|
6
|
+
// This module exposes the shipped OOP codec API used by NWP / NOP / NIP / NDP
|
|
7
|
+
// clients. The parallel functional API under ./codecs/ and ./frame-header.js
|
|
8
|
+
// remains importable by direct path but is not re-exported here to avoid
|
|
9
|
+
// symbol collisions (both files define FrameType / EncodingTier / FrameHeader).
|
|
10
|
+
|
|
11
|
+
// ── Frame primitives (OOP) ────────────────────────────────────────────────────
|
|
12
|
+
export {
|
|
13
|
+
FrameType,
|
|
14
|
+
EncodingTier,
|
|
15
|
+
FrameFlags,
|
|
16
|
+
FrameHeader,
|
|
17
|
+
DEFAULT_HEADER_SIZE,
|
|
18
|
+
EXTENDED_HEADER_SIZE,
|
|
19
|
+
DEFAULT_MAX_PAYLOAD,
|
|
20
|
+
EXTENDED_MAX_PAYLOAD,
|
|
21
|
+
} from "./frames.js";
|
|
22
|
+
|
|
23
|
+
// ── Codec (OOP) ───────────────────────────────────────────────────────────────
|
|
24
|
+
export {
|
|
25
|
+
Tier1JsonCodec,
|
|
26
|
+
Tier2MsgPackCodec,
|
|
27
|
+
NpsFrameCodec,
|
|
28
|
+
} from "./codec.js";
|
|
29
|
+
export type { NpsFrame } from "./codec.js";
|
|
30
|
+
|
|
31
|
+
// ── Registry (OOP) ────────────────────────────────────────────────────────────
|
|
32
|
+
export { FrameRegistry } from "./registry.js";
|
|
33
|
+
export type { FrameClass } from "./registry.js";
|
|
34
|
+
|
|
35
|
+
// ── Anchor cache (OOP) ────────────────────────────────────────────────────────
|
|
36
|
+
export { AnchorFrameCache } from "./cache.js";
|
|
37
|
+
|
|
38
|
+
// ── Exceptions / Status / Canonical JSON ──────────────────────────────────────
|
|
6
39
|
export * from "./exceptions.js";
|
|
7
40
|
export * from "./status-codes.js";
|
|
8
41
|
export { jcsStringify, sortKeysStringify } from "./canonical-json.js";
|
|
9
42
|
export type { CryptoProvider } from "./crypto-provider.js";
|
|
10
|
-
export * from "./codecs/index.js";
|
package/src/index.ts
CHANGED
package/src/ncp/frames.ts
CHANGED
|
@@ -197,3 +197,55 @@ export class ErrorFrame implements NpsFrame {
|
|
|
197
197
|
);
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
|
+
|
|
201
|
+
// ── HelloFrame ────────────────────────────────────────────────────────────────
|
|
202
|
+
// NPS-1 §4.6 — Native-mode handshake (0x06). Always Tier-1 JSON: encoding
|
|
203
|
+
// is not yet negotiated when this frame is sent.
|
|
204
|
+
|
|
205
|
+
export class HelloFrame implements NpsFrame {
|
|
206
|
+
readonly frameType = FrameType.HELLO;
|
|
207
|
+
readonly preferredTier = EncodingTier.JSON;
|
|
208
|
+
|
|
209
|
+
static readonly DEFAULT_MAX_FRAME_PAYLOAD = 0xFFFF;
|
|
210
|
+
static readonly DEFAULT_MAX_CONCURRENT_STREAMS = 32;
|
|
211
|
+
|
|
212
|
+
constructor(
|
|
213
|
+
public readonly npsVersion: string,
|
|
214
|
+
public readonly supportedEncodings: readonly string[],
|
|
215
|
+
public readonly supportedProtocols: readonly string[],
|
|
216
|
+
public minVersion?: string,
|
|
217
|
+
public agentId?: string,
|
|
218
|
+
public maxFramePayload: number = HelloFrame.DEFAULT_MAX_FRAME_PAYLOAD,
|
|
219
|
+
public extSupport: boolean = false,
|
|
220
|
+
public maxConcurrentStreams: number = HelloFrame.DEFAULT_MAX_CONCURRENT_STREAMS,
|
|
221
|
+
public e2eEncAlgorithms?: readonly string[],
|
|
222
|
+
) {}
|
|
223
|
+
|
|
224
|
+
toDict(): Record<string, unknown> {
|
|
225
|
+
return {
|
|
226
|
+
nps_version: this.npsVersion,
|
|
227
|
+
supported_encodings: [...this.supportedEncodings],
|
|
228
|
+
supported_protocols: [...this.supportedProtocols],
|
|
229
|
+
min_version: this.minVersion ?? null,
|
|
230
|
+
agent_id: this.agentId ?? null,
|
|
231
|
+
max_frame_payload: this.maxFramePayload,
|
|
232
|
+
ext_support: this.extSupport,
|
|
233
|
+
max_concurrent_streams: this.maxConcurrentStreams,
|
|
234
|
+
e2e_enc_algorithms: this.e2eEncAlgorithms ? [...this.e2eEncAlgorithms] : null,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static fromDict(data: Record<string, unknown>): HelloFrame {
|
|
239
|
+
return new HelloFrame(
|
|
240
|
+
data["nps_version"] as string,
|
|
241
|
+
(data["supported_encodings"] as string[]) ?? [],
|
|
242
|
+
(data["supported_protocols"] as string[]) ?? [],
|
|
243
|
+
(data["min_version"] as string | null) ?? undefined,
|
|
244
|
+
(data["agent_id"] as string | null) ?? undefined,
|
|
245
|
+
(data["max_frame_payload"] as number | null) ?? HelloFrame.DEFAULT_MAX_FRAME_PAYLOAD,
|
|
246
|
+
(data["ext_support"] as boolean | null) ?? false,
|
|
247
|
+
(data["max_concurrent_streams"] as number | null) ?? HelloFrame.DEFAULT_MAX_CONCURRENT_STREAMS,
|
|
248
|
+
(data["e2e_enc_algorithms"] as string[] | null) ?? undefined,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
package/src/ncp/registry.ts
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
import { FrameRegistry } from "../core/registry.js";
|
|
5
5
|
import { FrameType } from "../core/frames.js";
|
|
6
|
-
import { AnchorFrame, CapsFrame, DiffFrame, ErrorFrame, StreamFrame } from "./frames.js";
|
|
6
|
+
import { AnchorFrame, CapsFrame, DiffFrame, ErrorFrame, HelloFrame, StreamFrame } from "./frames.js";
|
|
7
7
|
|
|
8
8
|
export function registerNcpFrames(registry: FrameRegistry): void {
|
|
9
9
|
registry.register(FrameType.ANCHOR, AnchorFrame);
|
|
10
10
|
registry.register(FrameType.DIFF, DiffFrame);
|
|
11
11
|
registry.register(FrameType.STREAM, StreamFrame);
|
|
12
12
|
registry.register(FrameType.CAPS, CapsFrame);
|
|
13
|
+
registry.register(FrameType.HELLO, HelloFrame);
|
|
13
14
|
registry.register(FrameType.ERROR, ErrorFrame);
|
|
14
15
|
}
|