@thynker-labs/transport-web 0.0.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/dist/index.d.ts +31 -0
- package/dist/index.js +72 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Identity, Transport, Channel, Envelope, OutboundMessage } from '@thynker-labs/core';
|
|
2
|
+
|
|
3
|
+
interface WebChatRequest {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
message: string;
|
|
6
|
+
identity?: Partial<Identity>;
|
|
7
|
+
}
|
|
8
|
+
interface WebTransportOptions {
|
|
9
|
+
/** Max ms to wait for an inline reply (default 120_000). */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* HTTP-driven web chat transport — channel "web".
|
|
14
|
+
* Inbound via {@link chat}; outbound via kernel calling {@link send}.
|
|
15
|
+
*/
|
|
16
|
+
declare class WebTransport implements Transport {
|
|
17
|
+
channels: Channel[];
|
|
18
|
+
private handler?;
|
|
19
|
+
private replyWaiters;
|
|
20
|
+
private options;
|
|
21
|
+
constructor(options?: WebTransportOptions);
|
|
22
|
+
listen(onEnvelope: (e: Envelope) => Promise<void>): Promise<void>;
|
|
23
|
+
/** Process an inbound chat message and wait for the kernel reply. */
|
|
24
|
+
chat(request: WebChatRequest): Promise<string>;
|
|
25
|
+
send(msg: OutboundMessage): Promise<void>;
|
|
26
|
+
/** Cancel a pending inline reply (e.g. client disconnect). */
|
|
27
|
+
cancel(sessionId: string): void;
|
|
28
|
+
}
|
|
29
|
+
declare function createWebTransport(options?: WebTransportOptions): WebTransport;
|
|
30
|
+
|
|
31
|
+
export { type WebChatRequest, WebTransport, type WebTransportOptions, createWebTransport };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var WebTransport = class {
|
|
3
|
+
channels = ["web"];
|
|
4
|
+
handler;
|
|
5
|
+
replyWaiters = /* @__PURE__ */ new Map();
|
|
6
|
+
options;
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
async listen(onEnvelope) {
|
|
11
|
+
this.handler = onEnvelope;
|
|
12
|
+
}
|
|
13
|
+
/** Process an inbound chat message and wait for the kernel reply. */
|
|
14
|
+
async chat(request) {
|
|
15
|
+
if (!this.handler) {
|
|
16
|
+
throw new Error("WebTransport not listening \u2014 start the kernel first");
|
|
17
|
+
}
|
|
18
|
+
const sessionId = request.sessionId;
|
|
19
|
+
const timeoutMs = this.options.timeoutMs ?? 12e4;
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const timer = setTimeout(() => {
|
|
22
|
+
this.replyWaiters.delete(sessionId);
|
|
23
|
+
reject(new Error(`Web chat timed out after ${timeoutMs}ms`));
|
|
24
|
+
}, timeoutMs);
|
|
25
|
+
this.replyWaiters.set(sessionId, { resolve, reject, timer });
|
|
26
|
+
const envelope = {
|
|
27
|
+
identity: {
|
|
28
|
+
id: request.identity?.id ?? sessionId,
|
|
29
|
+
plane: request.identity?.plane ?? "user",
|
|
30
|
+
roles: request.identity?.roles ?? ["user"],
|
|
31
|
+
scopes: request.identity?.scopes ?? ["*"]
|
|
32
|
+
},
|
|
33
|
+
channel: "web",
|
|
34
|
+
replyTo: sessionId,
|
|
35
|
+
parts: [{ kind: "text", text: request.message }]
|
|
36
|
+
};
|
|
37
|
+
const handler = this.handler;
|
|
38
|
+
void handler(envelope).catch((err) => {
|
|
39
|
+
const waiter = this.replyWaiters.get(sessionId);
|
|
40
|
+
if (waiter) {
|
|
41
|
+
clearTimeout(waiter.timer);
|
|
42
|
+
this.replyWaiters.delete(sessionId);
|
|
43
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async send(msg) {
|
|
49
|
+
const waiter = this.replyWaiters.get(msg.to);
|
|
50
|
+
if (waiter) {
|
|
51
|
+
clearTimeout(waiter.timer);
|
|
52
|
+
this.replyWaiters.delete(msg.to);
|
|
53
|
+
waiter.resolve(msg.body);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Cancel a pending inline reply (e.g. client disconnect). */
|
|
57
|
+
cancel(sessionId) {
|
|
58
|
+
const waiter = this.replyWaiters.get(sessionId);
|
|
59
|
+
if (waiter) {
|
|
60
|
+
clearTimeout(waiter.timer);
|
|
61
|
+
this.replyWaiters.delete(sessionId);
|
|
62
|
+
waiter.reject(new Error("Chat cancelled"));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
function createWebTransport(options) {
|
|
67
|
+
return new WebTransport(options);
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
WebTransport,
|
|
71
|
+
createWebTransport
|
|
72
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thynker-labs/transport-web",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Web chat Transport adapter for HTTP/API clients",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@thynker-labs/core": "0.0.1"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
}
|
|
31
|
+
}
|