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,47 @@
|
|
|
1
|
+
import { type Peer, type Frame, type Connection } from '../../src/psionic';
|
|
2
|
+
import { psionicCodec } from '../../src/codecs/psionic';
|
|
3
|
+
const codec = psionicCodec();
|
|
4
|
+
|
|
5
|
+
export async function createPair(leftPeer: Peer, rightPeer: Peer) {
|
|
6
|
+
let leftMessages: Frame[] = [];
|
|
7
|
+
let rightMessages: Frame[] = [];
|
|
8
|
+
let right: Connection | undefined;
|
|
9
|
+
let left: Connection | undefined;
|
|
10
|
+
left = ({
|
|
11
|
+
close: () => {},
|
|
12
|
+
send: (message: Frame) => {
|
|
13
|
+
console.log('L>', codec.messageToWire(message));
|
|
14
|
+
leftMessages.push(message);
|
|
15
|
+
queueMicrotask(() => {
|
|
16
|
+
if (!rightPeer.connection) {
|
|
17
|
+
console.warn('Right peer is not connected yet, cannot deliver message');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
rightPeer.handleMessage(codec.messageFromWire(codec.messageToWire(message)));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
right = ({
|
|
25
|
+
close: () => {},
|
|
26
|
+
send: (message: Frame) => {
|
|
27
|
+
console.log('R>', codec.messageToWire(message));
|
|
28
|
+
rightMessages.push(message);
|
|
29
|
+
queueMicrotask(() => {
|
|
30
|
+
if (!leftPeer.connection) {
|
|
31
|
+
console.warn('Left peer is not connected yet, cannot deliver message');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
leftPeer.handleMessage(codec.messageFromWire(codec.messageToWire(message)));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
leftPeer.on('error', () => {});
|
|
39
|
+
rightPeer.on('error', () => {});
|
|
40
|
+
let pl = leftPeer.connect(async p => left, false);
|
|
41
|
+
let pr = rightPeer.connect(async p => right, false);
|
|
42
|
+
|
|
43
|
+
console.log('- Waiting for both peers to connect');
|
|
44
|
+
await Promise.all([pl, pr]);
|
|
45
|
+
console.log('- Both peers connected successfully');
|
|
46
|
+
return { left, right, leftMessages, rightMessages };
|
|
47
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Extending Psionic
|
|
2
|
+
|
|
3
|
+
You can extend Psionic with new transport adapters, communication codecs and policies. The frames themselves are fixed by the protocol, but the way they are encoded and transmitted can be customized by implementing new codecs and transports.
|
|
4
|
+
|
|
5
|
+
## Codecs
|
|
6
|
+
|
|
7
|
+
A codec defines how frames are encoded into bytes for transmission, and decoded back into frames on the receiving end. There are built-in codecs for space-separated JSON messages and CBOR. The objects have a couple of symbols to indicate special values that aren't easily represented in JSON, such as `AsyncFnKey` and `AsyncGenKey`.
|
|
8
|
+
|
|
9
|
+
The `codec` interface is two functions, `messageFromWire` and `messageToWire`, which convert between frames and byte arrays or strings. There is also a `type` parameter which can be `text` or `binary`. You can create a new codec by implementing these functions to encode frames in a different format, such as MessagePack, Protobuf, or a custom binary format.
|
|
10
|
+
|
|
11
|
+
## Transport Adapters
|
|
12
|
+
|
|
13
|
+
A transport adapter defines how encoded frames are transmitted between peers. There are built-in transports for Node.js Sockets, Web Sockets and Web Workers. You can create a new transport by implementing the `Adapter` interface.
|
|
14
|
+
|
|
15
|
+
Look at the `nodestreams` example for a simple adapter that uses Node.js child process stdio streams as the transport layer.
|
|
16
|
+
|
|
17
|
+
## Operation Tracking and Policies
|
|
18
|
+
|
|
19
|
+
The `Session` keeps track of all in-flight operations (promises, generators, incoming calls) for the peer connection. It provides a way to correlate responses to their original requests, enforce timeouts, and clean up resources on disconnect. You can create a custom operation tracker by implementing the `Session` interface, or use the provided `createSession` function which provides a reasonable default implementation with configurable timeouts.
|
package/docs/protocol.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# Psionic Protocol
|
|
2
|
+
|
|
3
|
+
The psionic protocol is kept intentionally minimal. These examples are shown using the psionic wire codec, designed to be easy to debug and interact with manually and in constrained environments, but the same protocol can be implemented over any transport layer and with any serialization format.
|
|
4
|
+
|
|
5
|
+
Each `frame` is a tuple of `[type, id, ...args]`
|
|
6
|
+
|
|
7
|
+
1. `type` is a single character string indicating the frame type
|
|
8
|
+
|
|
9
|
+
2. `id` is a string identifier that is written back in responses to the caller so they can correlate responses to their requests.
|
|
10
|
+
|
|
11
|
+
3. `args` is any additional data needed for the frame, which varies based on the frame type.
|
|
12
|
+
|
|
13
|
+
## Frame types
|
|
14
|
+
|
|
15
|
+
### *`D`* - Describe
|
|
16
|
+
|
|
17
|
+
**Example**
|
|
18
|
+
```
|
|
19
|
+
D "1" {"name":"Alice","greet":{"@@psion":"f"},"worker":{"@@psion":"g"}} true
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Send a new set of capabilities and state to the remote peer. The sender provides a description object, which is a structure that contains any combination of nested objects, arrays, primitive values, and functions. The receiver gets a live-updating view of this description as it changes over time, and can call functions within it as if they were local.
|
|
23
|
+
|
|
24
|
+
#### Frame arguments:
|
|
25
|
+
|
|
26
|
+
1. `description`: A nested object containing any combination of primitive values, objects, arrays, and functions. `{"@@psion":"f"}` indicates a callable function, and `{"@@psion":"g"}` for async generators.
|
|
27
|
+
|
|
28
|
+
2. `swap` (optional): A boolean flag indicating whether the description should replace the existing remote description (`true`), or be merged with it (`false` or omitted). If `swap` is `true`, the provided description completely replaces the existing remote description. If `swap` is `false` or omitted, the provided description is merged with the existing remote description, allowing for incremental updates.
|
|
29
|
+
|
|
30
|
+
### *`C`* - Call
|
|
31
|
+
|
|
32
|
+
**Example**
|
|
33
|
+
```
|
|
34
|
+
C "2" ["greet"] ["Hello, %s!"]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Invoke a function on the remote peer. The caller provides the path to the function within the remote description, and an array of arguments to call it with. The callee receives the call, executes the function, and sends back a response frame with the result.
|
|
38
|
+
|
|
39
|
+
If the function is a generator, the callee instead sends an iterator ID, which can be iterated over the generator with `.` (next), `<` (return), and `>` (throw) frames.
|
|
40
|
+
|
|
41
|
+
#### Frame arguments:
|
|
42
|
+
|
|
43
|
+
1. `name`: An array of strings representing the path to the function within the remote description. For example, `["user", "load"]` would call `peer.remote.user.load()`.
|
|
44
|
+
|
|
45
|
+
2. `args`: An array of arguments to call the function with.
|
|
46
|
+
|
|
47
|
+
### *`K`* - OK
|
|
48
|
+
|
|
49
|
+
**Example**
|
|
50
|
+
```
|
|
51
|
+
K "2" "Hello, Alice!"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Send a successful response back to the caller after any frame that expects a response (e.g. `D`, `C`, `.`, `>`) The `id` field matches the `id` of the original call frame, so the caller can correlate the response to their request.
|
|
55
|
+
|
|
56
|
+
For iterators (`.`, `<`, `>`), responses are an array where the first item is a `yielded` value, and the second is a `0` or `1` indicating if the generator is done. Continuing to call `next` on a completed generator will return an error.
|
|
57
|
+
|
|
58
|
+
#### Frame arguments:
|
|
59
|
+
|
|
60
|
+
1. `result`: The result of the call, which can be any value.
|
|
61
|
+
|
|
62
|
+
### *`E`* - Error
|
|
63
|
+
|
|
64
|
+
**Example**
|
|
65
|
+
```
|
|
66
|
+
E "2" 0 "Cannot greet user, server is shy."
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Send an error response back to the caller after any frame that expects a response (e.g. `D`, `C`, `.`, `>`) The `id` field matches the `id` of the original call frame, so the caller can correlate the response to their request.
|
|
70
|
+
|
|
71
|
+
#### Frame arguments:
|
|
72
|
+
|
|
73
|
+
1. `code`: A numeric error code.
|
|
74
|
+
|
|
75
|
+
2. `error` (optional): A string describing the error.
|
|
76
|
+
|
|
77
|
+
### *`.`* - Iterator Next
|
|
78
|
+
|
|
79
|
+
**Example**
|
|
80
|
+
```
|
|
81
|
+
. "3" "iterator-1" "Next value"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Advance a remote iterator to its next yield point. The `operationId` field matches the return of the original call frame that created the iterator. The callee responds with either an `OK` frame containing the yielded value, or an `Error` frame if the iterator is completed or throws.
|
|
85
|
+
|
|
86
|
+
#### Frame arguments:
|
|
87
|
+
|
|
88
|
+
1. `operationId`: The ID of the iterator to advance, as returned by the original call frame.
|
|
89
|
+
|
|
90
|
+
2. `value`: An optional value to send into the generator, for `yield` expressions.
|
|
91
|
+
|
|
92
|
+
### `<` - Iterator Return
|
|
93
|
+
|
|
94
|
+
**Example**
|
|
95
|
+
```
|
|
96
|
+
< "3" "iterator-1" "Final value"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Stop the iteration of a remote iterator, completing it. The `operationId` field matches the return of the original call frame that created the iterator. The callee responds with an `OK` frame confirming completion, or an `Error` frame if the iterator is already completed or throws.
|
|
100
|
+
|
|
101
|
+
#### Frame arguments:
|
|
102
|
+
|
|
103
|
+
1. `operationId`: The ID of the iterator to close, as returned by the original `Call` frame.
|
|
104
|
+
|
|
105
|
+
2. `value`: An optional return value to send into the generator.
|
|
106
|
+
|
|
107
|
+
### `>` - Iterator Throw
|
|
108
|
+
|
|
109
|
+
**Example**
|
|
110
|
+
```
|
|
111
|
+
> "3" "iterator-1" 0 "Something went wrong"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Used to throw an error into a remote iterator. The `operationId` field matches the return of the original call frame that created the iterator. The callee responds with either an `OK` frame containing the value returned by the generator, or an `Error` frame if the iterator is already completed or throws.
|
|
115
|
+
|
|
116
|
+
#### Frame arguments:
|
|
117
|
+
|
|
118
|
+
1. `operationId`: The ID of the iterator to advance, as returned by the original `Call` frame.
|
|
119
|
+
|
|
120
|
+
2. `code`: A numeric error code.
|
|
121
|
+
|
|
122
|
+
3. `error` (optional): A string describing the error.
|
|
123
|
+
|
|
124
|
+
# Errors
|
|
125
|
+
|
|
126
|
+
There are some standard errors defined by the protocol, but applications are free to define their own error codes and messages as well.
|
|
127
|
+
|
|
128
|
+
## *`0`* - Remote error
|
|
129
|
+
|
|
130
|
+
The generic remote error code indicates that an error occurred on the remote peer while processing a request. The error message may contain additional details about the error, but this is not guaranteed.
|
|
131
|
+
|
|
132
|
+
## *`1`* - No remote assigned
|
|
133
|
+
|
|
134
|
+
This error indicates that a call was attempted without an active remote peer connection. This can happen if the peer has not yet connected to a remote, or if the connection was lost.
|
|
135
|
+
|
|
136
|
+
## *`2`* - Disconnected
|
|
137
|
+
|
|
138
|
+
This error indicates that the connection to the remote peer was lost while a request was in-flight. The request may or may not have been processed by the remote peer.
|
|
139
|
+
|
|
140
|
+
## *`3`* - Protocol violation
|
|
141
|
+
|
|
142
|
+
This error indicates that a message was received that does not conform to the expected protocol format. This can happen if someone is sending data leading to corrupted frames or missing arguments.
|
|
143
|
+
|
|
144
|
+
## *`4`* - Function not found
|
|
145
|
+
|
|
146
|
+
This error indicates that a call was made to a function that does not exist on the remote peer's description. This can happen if the caller has an outdated view of the remote description, or if the caller is trying to call a function that was never described.
|
|
147
|
+
|
|
148
|
+
## *`5`* - Expired or invalid
|
|
149
|
+
|
|
150
|
+
This error indicates that a call was made with an ID that is no longer valid. This can happen if the caller is trying to respond to a call that has already been responded to, or if the caller is trying to interact with an iterator that has already completed.
|
|
151
|
+
|
|
152
|
+
## *`6`* - Rejected
|
|
153
|
+
|
|
154
|
+
This error indicates that a call was rejected by the remote peer. This can happen if the remote peer is overloaded, or if the remote peer has some application-level logic for rejecting certain calls.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createPeer } from "psionic";
|
|
2
|
+
|
|
3
|
+
// create a peer, with the description we want to send when we connect.
|
|
4
|
+
// could be anything, in this case, just the model we want the server to use.
|
|
5
|
+
const peer = createPeer({ description: { model: process.argv[3] || 'gpt-5.2' } });
|
|
6
|
+
|
|
7
|
+
// connect to the local socket
|
|
8
|
+
import { createConnection } from "net";
|
|
9
|
+
import { createNodeSocketAdapter } from "psionic/adapters/socket";
|
|
10
|
+
await peer.connect(createNodeSocketAdapter(createConnection({ port: 1234 })));
|
|
11
|
+
|
|
12
|
+
// pass the argument from the command line to the server's prompt function
|
|
13
|
+
for await (const chunk of peer.remote.prompt(process.argv[2])) {
|
|
14
|
+
// and stream the response back to the console as it comes in!
|
|
15
|
+
if (chunk.type === 'response.output_text.delta')
|
|
16
|
+
process.stdout.write(String(chunk.delta));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// close the connection so that the process exits
|
|
20
|
+
peer.close();
|
|
21
|
+
console.log('');
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cli-chat",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"author": "",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"openai": "^6.22.0",
|
|
15
|
+
"psionic": "file:../.."
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createPeer } from "psionic";
|
|
2
|
+
import OpenAI from "openai";
|
|
3
|
+
|
|
4
|
+
// listen for incoming socket connections and create a new peer for each one
|
|
5
|
+
import { createServer } from "net";
|
|
6
|
+
import { createNodeSocketAdapter } from "psionic/adapters/socket";
|
|
7
|
+
createServer(socket => handlePeer(createNodeSocketAdapter(socket))).listen(1234);
|
|
8
|
+
|
|
9
|
+
// global conversation_id for demo purposes.
|
|
10
|
+
let conversation_id = undefined;
|
|
11
|
+
|
|
12
|
+
async function handlePeer(adapter) {
|
|
13
|
+
const peer = createPeer();
|
|
14
|
+
await peer.connect(adapter);
|
|
15
|
+
|
|
16
|
+
const openAI = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
17
|
+
// send a list of functions and properties to the client.
|
|
18
|
+
// they can access this under peer.remote from their end, and we can update it at any time.
|
|
19
|
+
await peer.describe(desc => ({ ...desc, prompt }));
|
|
20
|
+
|
|
21
|
+
// this is a function that can be called by the client.
|
|
22
|
+
// it returns an async generator, which allows us to stream data back to the client in chunks.
|
|
23
|
+
async function* prompt(input) {
|
|
24
|
+
try {
|
|
25
|
+
const data = await openAI.responses.create({
|
|
26
|
+
// peer.remote is a live-updating view of the client's described state.
|
|
27
|
+
model: peer.remote?.model || 'gpt-5.2',
|
|
28
|
+
store: true,
|
|
29
|
+
stream: true,
|
|
30
|
+
previous_response_id: conversation_id,
|
|
31
|
+
tools: [{ type: "web_search" }],
|
|
32
|
+
instructions: `Send really terse responses.`,
|
|
33
|
+
input: String(input)
|
|
34
|
+
});
|
|
35
|
+
for await (const part of data) {
|
|
36
|
+
console.log(part.type, part.sequence_number);
|
|
37
|
+
if (part.type === 'response.completed')
|
|
38
|
+
conversation_id = part.response.id;
|
|
39
|
+
// yield one chunk to the client at a time
|
|
40
|
+
// this waits until they ask for the next one before continuing.
|
|
41
|
+
yield part;
|
|
42
|
+
}
|
|
43
|
+
} finally {
|
|
44
|
+
// this will run if the peer disconnects, times out, or if the function completes!
|
|
45
|
+
console.log('prompt ended!');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Cloudflare + Psionic Example
|
|
2
|
+
|
|
3
|
+
This example shows a simple Svelte chat application that runs in a Cloudflare Durable Object.
|
|
4
|
+
|
|
5
|
+
To run it, you'll need to fill in an OpenAI key in the `.env` file, like:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
OPENAI_API_KEY=sk-...
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Then you can run the development server with:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install
|
|
15
|
+
npm run dev
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
To publish it to your own Cloudflare account run:
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
npx wrangler secret put OPENAI_API_KEY
|
|
22
|
+
npm run deploy
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Structure
|
|
26
|
+
|
|
27
|
+
### Frontend
|
|
28
|
+
|
|
29
|
+
`src/api.ts` connects to the Cloudflare worker and sets up the Psionic peer.
|
|
30
|
+
|
|
31
|
+
`src/App.svelte` is the main Svelte component, which uses the peer to send messages and receive responses.
|
|
32
|
+
|
|
33
|
+
### Backend
|
|
34
|
+
|
|
35
|
+
`worker/cloudflare.ts` configures the Cloudflare Durable Object and sets up the Psionic peer, if the peer disconnects for 15 seconds the session is removed from the DO.
|
|
36
|
+
|
|
37
|
+
`worker/peer.ts` contains the logic for handling incoming messages, generating responses using the OpenAI API, and sending them back to the client.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import js from '@eslint/js'
|
|
2
|
+
import globals from 'globals'
|
|
3
|
+
import tseslint from 'typescript-eslint'
|
|
4
|
+
import { defineConfig, globalIgnores } from 'eslint/config'
|
|
5
|
+
|
|
6
|
+
export default defineConfig([
|
|
7
|
+
globalIgnores(['dist']),
|
|
8
|
+
{
|
|
9
|
+
files: ['**/*.{ts,tsx}'],
|
|
10
|
+
extends: [
|
|
11
|
+
js.configs.recommended,
|
|
12
|
+
tseslint.configs.recommended
|
|
13
|
+
],
|
|
14
|
+
languageOptions: {
|
|
15
|
+
ecmaVersion: 2020,
|
|
16
|
+
globals: globals.browser,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
])
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>Chat example</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div id="app"></div>
|
|
11
|
+
<script type="module" src="/src/main.ts"></script>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chat-example",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "tsc -b && vite build",
|
|
9
|
+
"lint": "eslint .",
|
|
10
|
+
"preview": "npm run build && vite preview",
|
|
11
|
+
"deploy": "npm run build && wrangler deploy",
|
|
12
|
+
"cf-typegen": "wrangler types"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@cloudflare/vite-plugin": "^1.25.2",
|
|
16
|
+
"@eslint/js": "^9.39.1",
|
|
17
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
18
|
+
"@types/node": "^24.10.13",
|
|
19
|
+
"eslint": "^9.39.1",
|
|
20
|
+
"globals": "^16.5.0",
|
|
21
|
+
"openai": "^6.24.0",
|
|
22
|
+
"psionic": "file:../..",
|
|
23
|
+
"svelte": "^5.53.3",
|
|
24
|
+
"typescript": "~5.9.3",
|
|
25
|
+
"typescript-eslint": "^8.48.0",
|
|
26
|
+
"vite": "^7.3.1",
|
|
27
|
+
"wrangler": "^4.67.0"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|