psionic 0.3.2 → 0.3.3
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/adapters/reconnect.d.mts +10 -0
- package/adapters/reconnect.mjs +2 -0
- package/adapters/reconnect.mjs.map +1 -0
- package/adapters/resend.d.mts +13 -0
- package/adapters/resend.mjs +2 -0
- package/adapters/resend.mjs.map +1 -0
- package/adapters/socket.d.mts +14 -0
- package/adapters/socket.mjs +3 -0
- package/adapters/socket.mjs.map +1 -0
- package/adapters/websocket.d.mts +21 -0
- package/adapters/websocket.mjs +2 -0
- package/adapters/websocket.mjs.map +1 -0
- package/adapters/webworker.d.mts +8 -0
- package/adapters/webworker.mjs +2 -0
- package/adapters/webworker.mjs.map +1 -0
- package/codecs/cbor.d.mts +12 -0
- package/codecs/cbor.mjs +2 -0
- package/codecs/cbor.mjs.map +1 -0
- package/codecs/psionic.d.mts +12 -0
- package/codecs/psionic.mjs +2 -0
- package/codecs/psionic.mjs.map +1 -0
- package/package.json +42 -24
- package/psionic.d.mts +203 -0
- package/psionic.mjs +2 -0
- package/psionic.mjs.map +1 -0
- package/session.d.mts +63 -0
- package/session.mjs +2 -0
- package/session.mjs.map +1 -0
- package/utils/react-hooks.d.mts +24 -0
- package/utils/react-hooks.mjs +2 -0
- package/utils/react-hooks.mjs.map +1 -0
- package/README.md +0 -321
- package/__tests__/async-functions.ts +0 -43
- package/__tests__/async-generator-functions.ts +0 -137
- package/__tests__/convert-from-wire-format.ts +0 -65
- package/__tests__/convert-to-wire-format.ts +0 -66
- package/__tests__/describe.ts +0 -26
- package/__tests__/lib/adapter.ts +0 -47
- package/docs/extending.md +0 -19
- package/docs/protocol.md +0 -154
- package/examples/cli-chat/client.mjs +0 -21
- package/examples/cli-chat/package.json +0 -17
- package/examples/cli-chat/server.mjs +0 -48
- package/examples/cloudflare/README.md +0 -37
- package/examples/cloudflare/eslint.config.js +0 -19
- package/examples/cloudflare/index.html +0 -13
- package/examples/cloudflare/package.json +0 -29
- package/examples/cloudflare/public/vite.svg +0 -1
- package/examples/cloudflare/src/App.svelte +0 -450
- package/examples/cloudflare/src/api.ts +0 -33
- package/examples/cloudflare/src/main.ts +0 -4
- package/examples/cloudflare/tsconfig.app.json +0 -27
- package/examples/cloudflare/tsconfig.json +0 -14
- package/examples/cloudflare/tsconfig.node.json +0 -26
- package/examples/cloudflare/tsconfig.worker.json +0 -8
- package/examples/cloudflare/vite.config.ts +0 -8
- package/examples/cloudflare/worker/cloudflare.ts +0 -115
- package/examples/cloudflare/worker/index.ts +0 -17
- package/examples/cloudflare/worker/peer.ts +0 -47
- package/examples/cloudflare/worker-configuration.d.ts +0 -10922
- package/examples/cloudflare/wrangler.jsonc +0 -58
- package/examples/nodestreams/adapter.mjs +0 -36
- package/examples/nodestreams/child.mjs +0 -20
- package/examples/nodestreams/package.json +0 -14
- package/examples/nodestreams/parent.mjs +0 -20
- package/examples/reconnect/client.mjs +0 -22
- package/examples/reconnect/package.json +0 -15
- package/examples/reconnect/server.mjs +0 -30
- package/examples/tokens/client.mjs +0 -26
- package/examples/tokens/package.json +0 -20
- package/examples/tokens/server.mjs +0 -46
- package/make-package.js +0 -23
- package/src/adapters/reconnect.ts +0 -54
- package/src/adapters/resend.ts +0 -34
- package/src/adapters/socket.ts +0 -183
- package/src/adapters/websocket.ts +0 -157
- package/src/adapters/webworker.ts +0 -48
- package/src/codecs/cbor.ts +0 -239
- package/src/codecs/psionic.ts +0 -75
- package/src/psionic.ts +0 -876
- package/src/session.ts +0 -164
- package/src/utils/react-hooks.ts +0 -48
- package/tests +0 -8
- package/tsdown.config.ts +0 -19
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { DurableObject } from "cloudflare:workers";
|
|
2
|
-
import { createPeer } from "psionic";
|
|
3
|
-
import { createWebSocketAdapter } from "psionic/adapters/websocket";
|
|
4
|
-
import type { ClientAPI } from "../src/api";
|
|
5
|
-
import type { CFWorkerAPI, PeerType } from "./peer";
|
|
6
|
-
import { handlePeer } from "./peer";
|
|
7
|
-
import { createSession } from "psionic/session";
|
|
8
|
-
|
|
9
|
-
type SessionState = {
|
|
10
|
-
peer: PeerType,
|
|
11
|
-
disconnectTimer?: number;
|
|
12
|
-
ws?: WebSocket;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export class Psionic extends DurableObject {
|
|
16
|
-
private sessions = new Map<string, SessionState>();
|
|
17
|
-
|
|
18
|
-
constructor(ctx: DurableObjectState, env: Env) {
|
|
19
|
-
super(ctx, env);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async fetch(request: Request): Promise<Response> {
|
|
23
|
-
const url = new URL(request.url);
|
|
24
|
-
|
|
25
|
-
if (url.pathname !== "/websocket") {
|
|
26
|
-
return new Response("Not found", { status: 404 });
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const upgradeHeader = request.headers.get("Upgrade");
|
|
30
|
-
if (!upgradeHeader || upgradeHeader.toLowerCase() !== "websocket") {
|
|
31
|
-
return new Response("Expected Upgrade: websocket", { status: 426 });
|
|
32
|
-
}
|
|
33
|
-
if (request.method !== "GET") {
|
|
34
|
-
return new Response("Expected GET", { status: 400 });
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Session identity: ensures reconnect attaches to same peer state.
|
|
38
|
-
const sid = url.searchParams.get("sid") || crypto.randomUUID();
|
|
39
|
-
|
|
40
|
-
const pair = new WebSocketPair();
|
|
41
|
-
const client = pair[0];
|
|
42
|
-
const server = pair[1];
|
|
43
|
-
|
|
44
|
-
//this.ctx.acceptWebSocket(server);
|
|
45
|
-
server.accept();
|
|
46
|
-
|
|
47
|
-
// Get/create session state
|
|
48
|
-
let sess = this.sessions.get(sid);
|
|
49
|
-
if (!sess) {
|
|
50
|
-
sess = {
|
|
51
|
-
peer: createPeer<ClientAPI, CFWorkerAPI>({ session: createSession() }),
|
|
52
|
-
};
|
|
53
|
-
this.sessions.set(sid, sess);
|
|
54
|
-
console.log('starting session', sid);
|
|
55
|
-
} else {
|
|
56
|
-
console.log('reusing session', sid);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const peer = sess.peer;
|
|
60
|
-
|
|
61
|
-
// If there was a pending soft-disconnect cleanup, cancel it.
|
|
62
|
-
if (sess.disconnectTimer) {
|
|
63
|
-
clearTimeout(sess.disconnectTimer);
|
|
64
|
-
sess.disconnectTimer = undefined;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// If there was an existing ws, you can decide policy:
|
|
68
|
-
// - close old one and replace (common)
|
|
69
|
-
// - reject new connection
|
|
70
|
-
if (sess.ws) {
|
|
71
|
-
try {
|
|
72
|
-
sess.ws.close(1000, "Replaced by new connection");
|
|
73
|
-
} catch { }
|
|
74
|
-
}
|
|
75
|
-
sess.ws = server;
|
|
76
|
-
|
|
77
|
-
const adapter = createWebSocketAdapter(server as any);
|
|
78
|
-
|
|
79
|
-
server.addEventListener("close", () => {
|
|
80
|
-
console.log(new Date(), 'peer closed ', sid);
|
|
81
|
-
|
|
82
|
-
// Soft-disconnect: keep peer/inflight/generators alive briefly to survive hiccups.
|
|
83
|
-
// If client reconnects within the window, everything continues.
|
|
84
|
-
const GRACE_MS = 15_000;
|
|
85
|
-
|
|
86
|
-
// Avoid scheduling multiple timers for the same session
|
|
87
|
-
if (!sess) return;
|
|
88
|
-
if (sess.disconnectTimer) {
|
|
89
|
-
clearTimeout(sess.disconnectTimer);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
sess.disconnectTimer = setTimeout(() => {
|
|
93
|
-
// Hard cleanup after grace window
|
|
94
|
-
try {
|
|
95
|
-
peer.close();
|
|
96
|
-
} catch { }
|
|
97
|
-
console.log(new Date(), 'peer cleanup ', sid);
|
|
98
|
-
this.sessions.delete(sid);
|
|
99
|
-
}, GRACE_MS) as unknown as number;
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
// Attach peer to this remote (does describe/ready sequencing)
|
|
103
|
-
peer.connect(adapter).then(() => {
|
|
104
|
-
return handlePeer(sid, peer);
|
|
105
|
-
}).catch((err: any) => {
|
|
106
|
-
console.error(new Date(), "Failed to attach peer:", err);
|
|
107
|
-
try {
|
|
108
|
-
server.close(3000, "Peer attach failed");
|
|
109
|
-
} catch { }
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
// set SID cookie for client to use in future connections (optional, depends on your SID strategy)
|
|
113
|
-
return new Response(null, { status: 101, webSocket: client });
|
|
114
|
-
}
|
|
115
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { env } from "cloudflare:workers";
|
|
2
|
-
export { Psionic } from "./cloudflare";
|
|
3
|
-
|
|
4
|
-
export default {
|
|
5
|
-
fetch(request) {
|
|
6
|
-
const url = new URL(request.url);
|
|
7
|
-
|
|
8
|
-
if (url.pathname === "/websocket") {
|
|
9
|
-
const sid = url.searchParams.get("sid") ?? "global";
|
|
10
|
-
const id = env.PSIONIC.idFromName(sid);
|
|
11
|
-
const stub = env.PSIONIC.get(id);
|
|
12
|
-
return stub.fetch(request);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return new Response(null, { status: 404 });
|
|
16
|
-
},
|
|
17
|
-
} satisfies ExportedHandler<Env>;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { Peer } from "psionic";
|
|
2
|
-
import type { ClientAPI } from "../src/api";
|
|
3
|
-
import OpenAI from "openai";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export type CFWorkerAPI = {
|
|
7
|
-
/** The session ID of the current peer */
|
|
8
|
-
sid?: string;
|
|
9
|
-
/** The ID of the current conversation */
|
|
10
|
-
conversation_id?: string | null;
|
|
11
|
-
/** A function to send a prompt to the AI model */
|
|
12
|
-
prompt: (input: string) => AsyncGenerator<OpenAI.Responses.ResponseStreamEvent>;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type PeerType = Peer<ClientAPI, CFWorkerAPI>;
|
|
16
|
-
|
|
17
|
-
export async function handlePeer(sid: string, peer: PeerType) {
|
|
18
|
-
const openAI = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
19
|
-
|
|
20
|
-
// send a list of functions and properties to the client.
|
|
21
|
-
// they can access this under peer.remote from their end, and we can update it at any time.
|
|
22
|
-
await peer.describe(desc => ({ ...desc, sid, prompt }));
|
|
23
|
-
|
|
24
|
-
// this is a function that can be called by the client.
|
|
25
|
-
// it returns an async generator, which allows us to stream data back to the client in chunks.
|
|
26
|
-
async function* prompt(input: string) {
|
|
27
|
-
const conversation_id = peer.local?.conversation_id
|
|
28
|
-
const data = await openAI.responses.create({
|
|
29
|
-
// peer.remote is a live-updating view of the client's described state.
|
|
30
|
-
model: 'gpt-5.2',
|
|
31
|
-
store: true,
|
|
32
|
-
stream: true,
|
|
33
|
-
previous_response_id: conversation_id,
|
|
34
|
-
tools: [{ type: "web_search" }],
|
|
35
|
-
input: String(input || 'Hello, world!'),
|
|
36
|
-
});
|
|
37
|
-
for await (const part of data) {
|
|
38
|
-
console.log(part.type, part.sequence_number);
|
|
39
|
-
if (part.type === 'response.completed') {
|
|
40
|
-
await peer.describe(desc => ({ ...desc, conversation_id: part.response.id }));
|
|
41
|
-
}
|
|
42
|
-
// yield one chunk to the client at a time
|
|
43
|
-
// this waits until they ask for the next one before continuing.
|
|
44
|
-
yield part;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|