psionic 0.3.0 → 0.3.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/README.md +321 -0
- package/__tests__/async-functions.ts +43 -0
- package/__tests__/async-generator-functions.ts +137 -0
- package/__tests__/convert-from-wire-format.ts +65 -0
- package/__tests__/convert-to-wire-format.ts +66 -0
- package/__tests__/describe.ts +26 -0
- package/__tests__/lib/adapter.ts +47 -0
- package/docs/extending.md +19 -0
- package/docs/protocol.md +154 -0
- package/examples/cli-chat/client.mjs +21 -0
- package/examples/cli-chat/package.json +17 -0
- package/examples/cli-chat/server.mjs +48 -0
- package/examples/cloudflare/README.md +37 -0
- package/examples/cloudflare/eslint.config.js +19 -0
- package/examples/cloudflare/index.html +13 -0
- package/examples/cloudflare/package.json +29 -0
- package/examples/cloudflare/public/vite.svg +1 -0
- package/examples/cloudflare/src/App.svelte +450 -0
- package/examples/cloudflare/src/api.ts +33 -0
- package/examples/cloudflare/src/main.ts +4 -0
- package/examples/cloudflare/tsconfig.app.json +27 -0
- package/examples/cloudflare/tsconfig.json +14 -0
- package/examples/cloudflare/tsconfig.node.json +26 -0
- package/examples/cloudflare/tsconfig.worker.json +8 -0
- package/examples/cloudflare/vite.config.ts +8 -0
- package/examples/cloudflare/worker/cloudflare.ts +115 -0
- package/examples/cloudflare/worker/index.ts +17 -0
- package/examples/cloudflare/worker/peer.ts +47 -0
- package/examples/cloudflare/worker-configuration.d.ts +10922 -0
- package/examples/cloudflare/wrangler.jsonc +58 -0
- package/examples/nodestreams/adapter.mjs +36 -0
- package/examples/nodestreams/child.mjs +20 -0
- package/examples/nodestreams/package.json +14 -0
- package/examples/nodestreams/parent.mjs +20 -0
- package/examples/reconnect/client.mjs +22 -0
- package/examples/reconnect/package.json +15 -0
- package/examples/reconnect/server.mjs +30 -0
- package/examples/tokens/client.mjs +26 -0
- package/examples/tokens/package.json +20 -0
- package/examples/tokens/server.mjs +46 -0
- package/make-package.js +23 -0
- package/package.json +24 -42
- package/src/adapters/reconnect.ts +54 -0
- package/src/adapters/resend.ts +34 -0
- package/src/adapters/socket.ts +183 -0
- package/src/adapters/websocket.ts +157 -0
- package/src/adapters/webworker.ts +48 -0
- package/src/codecs/cbor.ts +239 -0
- package/src/codecs/psionic.ts +75 -0
- package/src/psionic.ts +876 -0
- package/src/session.ts +164 -0
- package/src/utils/react-hooks.ts +48 -0
- package/tests +8 -0
- package/tsdown.config.ts +19 -0
- package/adapters/reconnect.d.mts +0 -10
- package/adapters/reconnect.mjs +0 -42
- package/adapters/reconnect.mjs.map +0 -1
- package/adapters/resend.d.mts +0 -13
- package/adapters/resend.mjs +0 -29
- package/adapters/resend.mjs.map +0 -1
- package/adapters/socket.d.mts +0 -14
- package/adapters/socket.mjs +0 -160
- package/adapters/socket.mjs.map +0 -1
- package/adapters/websocket.d.mts +0 -20
- package/adapters/websocket.mjs +0 -124
- package/adapters/websocket.mjs.map +0 -1
- package/adapters/webworker.d.mts +0 -8
- package/adapters/webworker.mjs +0 -42
- package/adapters/webworker.mjs.map +0 -1
- package/codecs/cbor.d.mts +0 -12
- package/codecs/cbor.mjs +0 -198
- package/codecs/cbor.mjs.map +0 -1
- package/codecs/psionic.d.mts +0 -12
- package/codecs/psionic.mjs +0 -57
- package/codecs/psionic.mjs.map +0 -1
- package/psionic.d.mts +0 -203
- package/psionic.mjs +0 -671
- package/psionic.mjs.map +0 -1
- package/session.d.mts +0 -63
- package/session.mjs +0 -92
- package/session.mjs.map +0 -1
- package/utils/react-hooks.d.mts +0 -24
- package/utils/react-hooks.mjs +0 -37
- package/utils/react-hooks.mjs.map +0 -1
|
@@ -0,0 +1,17 @@
|
|
|
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>;
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
}
|