Skip to content

Commit 8cc9c50

Browse files
Zawwarsami16claude
andcommitted
phase 1.2: JS/TS client library — wire-compatible mirror of Python
js/ workspace shipping @zawwarsami/zhub on npm. browser + Node clients can publish() and connect() with the same primitives as Python. tsc checks clean locally; CI gets a parallel js-test job. components: - js/package.json: ESM-only, single dependency on `ws` for Node, types via tsc emit. exports field with .d.ts. - js/tsconfig.json: strict, ES2022 target, declaration+types. - js/src/manifest.ts: Manifest + Capability types, chatOnlyManifest() helper, byte-equivalent JSON shape to Python. - js/src/protocol.ts: Envelope + helpers (registerPublisher, registerConnection, chatRequest, chatResponse, chatChunk, invokeRequest, invokeResult, errorEnvelope). request_id matches Python's uuid4().hex (32 lowercase hex chars). - js/src/errors.ts: ZhubError hierarchy mirroring Python. - js/src/client.ts: ZhubPublication + ZhubConnection classes with publish() and connect() factories. * Auto-reconnect with backoff (mirrors Phase 1.5 Python). * Picks global WebSocket in browsers, falls back to `ws` in Node. * Streaming chat (sync + async iterators yield chat-chunks). * Bidirectional invoke (publisher.invoke(cid, cap, args)). * AuthError on register_failed = terminal, no busy-loop. - js/src/index.ts: re-exports. - js/test/protocol.test.ts: 9 cases — all envelope shapes round-trip + match Python wire format exactly. - js/test/manifest.test.ts: 3 cases — chatOnlyManifest defaults, rate_limit thread-through, public flag. - js/README.md: install + publish + connect examples + browser caveat. - .github/workflows/ci.yml: js-test job runs npm install + tsc check + vitest. parallel with python matrix + kotlin job. local testing caveat (proot only): rollup native binding dlopen blocked in Termux, so vitest can't run locally. tsc-noEmit is clean. CI runs the full vitest suite on real Linux. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ed87285 commit 8cc9c50

911 files changed

Lines changed: 592170 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,29 @@ jobs:
5858
working-directory: kotlin
5959
run: |
6060
gradle test build --no-daemon --stacktrace
61+
62+
js-test:
63+
name: JS/TS module test
64+
runs-on: ubuntu-latest
65+
66+
steps:
67+
- uses: actions/checkout@v4
68+
69+
- name: Set up Node 20
70+
uses: actions/setup-node@v4
71+
with:
72+
node-version: '20'
73+
cache: 'npm'
74+
cache-dependency-path: js/package-lock.json
75+
76+
- name: Install
77+
working-directory: js
78+
run: npm install --no-audit --no-fund
79+
80+
- name: Type check
81+
working-directory: js
82+
run: npx tsc -p tsconfig.json --noEmit
83+
84+
- name: Test
85+
working-directory: js
86+
run: npm test

js/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# @zawwarsami/zhub — JS/TS client
2+
3+
Browser + Node client for [zhub](https://github.com/Zawwarsami16/zhub). Wire-compatible with the Python core. Same `publish()` and `connect()` primitives.
4+
5+
## Install
6+
7+
```bash
8+
npm install @zawwarsami/zhub
9+
```
10+
11+
## Publish (Node)
12+
13+
```typescript
14+
import { publish } from '@zawwarsami/zhub';
15+
16+
const pub = publish({
17+
name: 'my-ai',
18+
description: 'A custom AI agent',
19+
hubUrl: 'wss://hub.example.com',
20+
chatHandler: (messages) => {
21+
const last = messages.find((m) => m.role === 'user')?.content ?? '';
22+
return `You said: ${last}`;
23+
},
24+
});
25+
26+
while (!pub.apiKey) await new Promise((r) => setTimeout(r, 50));
27+
console.log('URL:', `https://hub.example.com${pub.baseUrl}`);
28+
console.log('KEY:', pub.apiKey);
29+
```
30+
31+
## Connect — chat from a browser/Node client
32+
33+
```typescript
34+
import { connect } from '@zawwarsami/zhub';
35+
36+
const conn = connect({
37+
aiName: 'my-ai',
38+
apiKey: 'zk_a8f2c9...',
39+
hubUrl: 'wss://hub.example.com',
40+
capabilities: {
41+
send_whatsapp: [
42+
{
43+
type: 'object',
44+
required: ['to', 'message'],
45+
properties: { to: { type: 'string' }, message: { type: 'string' } },
46+
},
47+
async (args) => ({ delivered: true, to: args.to }),
48+
],
49+
},
50+
});
51+
52+
await new Promise((r) => setTimeout(r, 500));
53+
const reply = await conn.chat([{ role: 'user', content: 'hi' }]);
54+
console.log('AI replied:', reply.text);
55+
```
56+
57+
## Browser usage
58+
59+
The library uses the global `WebSocket` when present (browsers) and falls back to `ws` in Node. Connect-mode is the primary browser use case. Publish-mode also works in browsers.
60+
61+
## API parity with Python
62+
63+
Field names on the wire match Python's `zhub.protocol`. Both sides interoperate against the same hub.

js/node_modules/.bin/acorn

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/esbuild

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/nanoid

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/node-which

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/rollup

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/tsc

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/tsserver

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/node_modules/.bin/vite

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)