@pinecall/room 0.1.0
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/LICENSE +202 -0
- package/README.md +283 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/log/fold.d.ts +23 -0
- package/dist/log/fold.d.ts.map +1 -0
- package/dist/log/fold.js +40 -0
- package/dist/log/fold.js.map +1 -0
- package/dist/log/follow.d.ts +24 -0
- package/dist/log/follow.d.ts.map +1 -0
- package/dist/log/follow.js +96 -0
- package/dist/log/follow.js.map +1 -0
- package/dist/log/sse.d.ts +13 -0
- package/dist/log/sse.d.ts.map +1 -0
- package/dist/log/sse.js +50 -0
- package/dist/log/sse.js.map +1 -0
- package/dist/react/index.d.ts +13 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +65 -0
- package/dist/react/index.js.map +1 -0
- package/dist/room.d.ts +54 -0
- package/dist/room.d.ts.map +1 -0
- package/dist/room.js +283 -0
- package/dist/room.js.map +1 -0
- package/dist/rows.d.ts +37 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +89 -0
- package/dist/rows.js.map +1 -0
- package/dist/seat/join.d.ts +35 -0
- package/dist/seat/join.d.ts.map +1 -0
- package/dist/seat/join.js +64 -0
- package/dist/seat/join.js.map +1 -0
- package/dist/seat/livekit.d.ts +51 -0
- package/dist/seat/livekit.d.ts.map +1 -0
- package/dist/seat/livekit.js +8 -0
- package/dist/seat/livekit.js.map +1 -0
- package/dist/seat/sink.d.ts +11 -0
- package/dist/seat/sink.d.ts.map +1 -0
- package/dist/seat/sink.js +23 -0
- package/dist/seat/sink.js.map +1 -0
- package/dist/server/index.d.ts +48 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +73 -0
- package/dist/server/index.js.map +1 -0
- package/dist/store.d.ts +20 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +55 -0
- package/dist/store.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Pinecall
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# @pinecall/room
|
|
2
|
+
|
|
3
|
+
The caller's side of a Pinecall call, in a browser. A visitor talks to your agent, writes to it, or
|
|
4
|
+
asks it to ring their phone; the page draws what is happening from the call's own log. No
|
|
5
|
+
framework, no key in the page.
|
|
6
|
+
|
|
7
|
+
## One store, three imports
|
|
8
|
+
|
|
9
|
+
Everything is one store: a value you read, a function you subscribe with, and a handful of verbs
|
|
10
|
+
that change it. The package has three entry points, and each one is for a different place.
|
|
11
|
+
|
|
12
|
+
| import | where it runs | what it gives you |
|
|
13
|
+
|---|---|---|
|
|
14
|
+
| `@pinecall/room` | the page | `room()`, the store of one conversation, and `rowsOf`, `knownBy`, `brief` to draw it |
|
|
15
|
+
| `@pinecall/room/react` | the page, in React | `useRoom()` and `useStore()` |
|
|
16
|
+
| `@pinecall/room/server` | your server | `mint()`, `dial()` and `GatewayRefused`: the only code that touches the key |
|
|
17
|
+
|
|
18
|
+
A conversation has two parts, and they are not the same thing. **The seat** is a LiveKit room: it
|
|
19
|
+
carries the audio and the typed lines. **The log** is the call's record on the gateway, relayed by
|
|
20
|
+
your server: every turn, every tool the agent called and what came back, the agent's own state, and
|
|
21
|
+
at the end the cost and the score. The store joins the two. What the page draws is the log folded
|
|
22
|
+
by `@pinecall/protocol`, the same fold the Pinecall console draws from.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm add @pinecall/room livekit-client
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`livekit-client` is only loaded when a seat is taken, so a page that only offers "call me" never
|
|
31
|
+
downloads it. React is optional: `@pinecall/room/react` needs React 18 or 19, the rest needs
|
|
32
|
+
nothing. The server entry needs Node 24 or anything else with `fetch`.
|
|
33
|
+
|
|
34
|
+
## From a page
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { room, rowsOf } from "@pinecall/room";
|
|
38
|
+
|
|
39
|
+
const call = room({
|
|
40
|
+
tokens: async (scope) => {
|
|
41
|
+
const answer = await fetch("/api/token", {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: { "content-type": "application/json" },
|
|
44
|
+
body: JSON.stringify({ scope }),
|
|
45
|
+
});
|
|
46
|
+
const body = await answer.json();
|
|
47
|
+
if (!answer.ok) throw new Error(body.detail ?? `the server answered ${answer.status}`);
|
|
48
|
+
return body;
|
|
49
|
+
},
|
|
50
|
+
log: (call) => `/api/log?call=${encodeURIComponent(call)}`,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
call.subscribe((state) => {
|
|
54
|
+
status.textContent = state.phase === "failed" ? state.error : state.phase;
|
|
55
|
+
lines.replaceChildren(
|
|
56
|
+
...rowsOf(state.log).map((row) => {
|
|
57
|
+
const line = document.createElement("p");
|
|
58
|
+
line.textContent = row.kind === "turn" ? `${row.role}: ${row.text}` : `${row.name}(${row.args})`;
|
|
59
|
+
return line;
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
talkButton.onclick = () => call.start("talk");
|
|
65
|
+
chatButton.onclick = () => call.start("chat");
|
|
66
|
+
form.onsubmit = (event) => {
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
void call.send(input.value);
|
|
69
|
+
input.value = "";
|
|
70
|
+
};
|
|
71
|
+
hangUp.onclick = () => call.leave();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`start` is safe to call from a button that gets clicked twice: while a conversation is opening or
|
|
75
|
+
live, it does nothing. Once one has ended or failed, it opens a new one. When the page goes away,
|
|
76
|
+
`call.close()` takes everything down at once.
|
|
77
|
+
|
|
78
|
+
## With React
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
import { useRoom } from "@pinecall/room/react";
|
|
82
|
+
import { rowsOf } from "@pinecall/room";
|
|
83
|
+
|
|
84
|
+
// `tokens` is the function from the page above.
|
|
85
|
+
export function Visitor() {
|
|
86
|
+
const call = useRoom({ tokens, log: (id) => `/api/log?call=${encodeURIComponent(id)}` });
|
|
87
|
+
|
|
88
|
+
if (call.phase === "idle") return <button onClick={() => call.start("talk")}>Talk</button>;
|
|
89
|
+
if (call.phase === "failed") return <p>{call.error}</p>;
|
|
90
|
+
return (
|
|
91
|
+
<>
|
|
92
|
+
{call.wantsSound && <button onClick={call.playSound}>Turn the sound on</button>}
|
|
93
|
+
{rowsOf(call.log).map((row) => (
|
|
94
|
+
<p key={row.key}>{row.kind === "turn" ? row.text : row.name}</p>
|
|
95
|
+
))}
|
|
96
|
+
{call.phase === "live" && <button onClick={() => call.leave()}>Hang up</button>}
|
|
97
|
+
</>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`useRoom` makes one room per mounted component and closes it when the component unmounts. The
|
|
103
|
+
options are read when they are used, not when the room is made, so a `tokens` written inline — a
|
|
104
|
+
new function every render — is fine: the one from the last render is the one that runs. It hands
|
|
105
|
+
back the state with the five verbs beside it.
|
|
106
|
+
|
|
107
|
+
`useStore(store)` is the plain version: any store of this package, read with React's
|
|
108
|
+
`useSyncExternalStore`. `useRoom` is `useStore` over a room it owns.
|
|
109
|
+
|
|
110
|
+
## Your server: the three routes
|
|
111
|
+
|
|
112
|
+
The page never holds a key. Every question it asks goes to your own server, which asks the
|
|
113
|
+
gateway with the org's key and hands back only what that one visitor needs. The key needs two
|
|
114
|
+
scopes: `talk` (a ticket, a dial) and `calls` (reading a call's log). It never reaches the page.
|
|
115
|
+
|
|
116
|
+
| route | the page sends | your server does | it answers |
|
|
117
|
+
|---|---|---|---|
|
|
118
|
+
| `POST /api/token` | `{scope}`, `talk` or `chat` | `mint(key, {url, agent, scope})` | `{server_url, participant_token, call}` |
|
|
119
|
+
| `GET /api/log?call=` | the call id, and `last-event-id` on a resume | streams `GET /v1/calls/{call}/events` with the key | the gateway's stream, its status passed through: `204` when the call is sealed |
|
|
120
|
+
| `POST /api/call-me` | `{to}`, a number in E.164 | `dial(key, {url, agent, to})` | `{call}` |
|
|
121
|
+
|
|
122
|
+
The names of the routes are yours; the page only knows the functions you give `room()`. With
|
|
123
|
+
Hono:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { Hono } from "hono";
|
|
127
|
+
import { dial, GatewayRefused, mint } from "@pinecall/room/server";
|
|
128
|
+
|
|
129
|
+
const KEY = process.env.PINECALL_KEY!;
|
|
130
|
+
const url = "https://box.pinecall.io";
|
|
131
|
+
const agent = "clinica-norte";
|
|
132
|
+
const app = new Hono();
|
|
133
|
+
|
|
134
|
+
// A refusal keeps the gateway's own sentence and status: it names the fix.
|
|
135
|
+
const refused = (error: unknown) => {
|
|
136
|
+
if (error instanceof GatewayRefused) return Response.json({ detail: error.message }, { status: error.status });
|
|
137
|
+
throw error;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
app.post("/api/token", async (c) => {
|
|
141
|
+
const { scope } = await c.req.json();
|
|
142
|
+
return mint(KEY, { url, agent, scope }).then((minted) => c.json(minted), refused);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
app.post("/api/call-me", async (c) => {
|
|
146
|
+
const { to } = await c.req.json();
|
|
147
|
+
return dial(KEY, { url, agent, to }).then(({ call }) => c.json({ call }), refused);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
app.get("/api/log", async (c) => {
|
|
151
|
+
const call = c.req.query("call") ?? "";
|
|
152
|
+
if (!mine(c, call)) return c.body(null, 404);
|
|
153
|
+
const resume = c.req.header("last-event-id");
|
|
154
|
+
const answer = await fetch(`${url}/v1/calls/${encodeURIComponent(call)}/events`, {
|
|
155
|
+
headers: { authorization: `Bearer ${KEY}`, accept: "text/event-stream", ...(resume ? { "last-event-id": resume } : {}) },
|
|
156
|
+
signal: c.req.raw.signal,
|
|
157
|
+
});
|
|
158
|
+
return new Response(answer.body, { status: answer.status, headers: { "content-type": "text/event-stream" } });
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The relay is four lines that matter: the call, the resume cursor, the key on the header, and the
|
|
163
|
+
gateway's status and body passed through as they came. It forwards `last-event-id`, so a page
|
|
164
|
+
whose stream dropped picks up where it was; and it passes a `204` through, so a sealed call is
|
|
165
|
+
known to be over rather than retried.
|
|
166
|
+
|
|
167
|
+
`mine(c, call)` is yours to write, and it is not optional: a relay that streams any call id it is
|
|
168
|
+
handed is a window onto every call of the org. Keep the call ids a visitor's session minted or
|
|
169
|
+
dialled, and relay only those.
|
|
170
|
+
|
|
171
|
+
## Have the agent call me
|
|
172
|
+
|
|
173
|
+
Give `room()` a `callMe`, and the page can ask for a phone call instead of a seat:
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
const call = room({
|
|
177
|
+
tokens,
|
|
178
|
+
log: (id) => `/api/log?call=${encodeURIComponent(id)}`,
|
|
179
|
+
callMe: async (to) => {
|
|
180
|
+
const answer = await fetch("/api/call-me", {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: { "content-type": "application/json" },
|
|
183
|
+
body: JSON.stringify({ to }),
|
|
184
|
+
});
|
|
185
|
+
const body = await answer.json();
|
|
186
|
+
if (!answer.ok) throw new Error(body.detail ?? `the server answered ${answer.status}`);
|
|
187
|
+
return body;
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
await call.callMe("+34600000001");
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
There is no seat on a phone call: the conversation happens on the phone, and the page only
|
|
195
|
+
watches. Everything it says comes from the log. The phase is `ringing` while the call is being
|
|
196
|
+
placed, `live` once the log says it was answered, and `ended` when the log says it is over — or
|
|
197
|
+
straight from `ringing` when nobody picked up. `speaking` comes from the log too.
|
|
198
|
+
|
|
199
|
+
The gateway refuses a dial to a number that has never called or written to the org, and counts
|
|
200
|
+
dials per minute and per day. Those refusals reach the page as a failed call with the gateway's
|
|
201
|
+
sentence.
|
|
202
|
+
|
|
203
|
+
## What `state` says
|
|
204
|
+
|
|
205
|
+
Every field is the truth at the moment it was published. A new state is a new object; one you were
|
|
206
|
+
handed is never changed afterwards, so it can be compared by reference and kept.
|
|
207
|
+
|
|
208
|
+
| field | what it is |
|
|
209
|
+
|---|---|
|
|
210
|
+
| `phase` | `idle` · `opening` (minting, joining) · `ringing` (a phone call not yet answered) · `live` · `ended` · `failed` |
|
|
211
|
+
| `mode` | `talk`, `chat`, `phone`, or `null` before anything started |
|
|
212
|
+
| `call` | the call id, once the tokens or the dial answered it; `""` before |
|
|
213
|
+
| `error` | the sentence to show when `phase` is `failed`; `""` otherwise |
|
|
214
|
+
| `log` | the call as `@pinecall/protocol` folds it: `turns`, `tools`, `app_state`, `status`, `live` (the words being said right now), `cost` |
|
|
215
|
+
| `entries` | every entry of the log read so far, in order |
|
|
216
|
+
| `connection` | the log's stream: `connecting` · `live` · `reconnecting` · `ended` (also before anything opened) |
|
|
217
|
+
| `wantsSound` | `true` when the browser has not let the page play sound yet: show a button that calls `playSound()` |
|
|
218
|
+
| `speaking` | `{agent, user, level}`: who is making a sound right now, and how loud the agent is, 0 to 1 |
|
|
219
|
+
|
|
220
|
+
The phase and everything around it — `error`, `connection`, `wantsSound`, `speaking` — are
|
|
221
|
+
published the moment they change. The log is painted at most ten times a second: a burst of
|
|
222
|
+
entries inside one turn is one repaint, not forty.
|
|
223
|
+
|
|
224
|
+
The log is read for a while after the seat closes. The agent's last words, the call's cost and its
|
|
225
|
+
score are entries that arrive after the visitor has gone, so `cost` is `null` until `call.summary`,
|
|
226
|
+
which comes after `call.ended`. The stream is followed until `call.score`, the last thing a log ever
|
|
227
|
+
says, or for `linger` milliseconds after the call ended (60 000 by default: the worker notices a caller has gone some twenty seconds after the fact, and the score comes after that), whichever is first.
|
|
228
|
+
|
|
229
|
+
A stream that drops is opened again from the last entry read, after half a second, doubling to
|
|
230
|
+
eight seconds while the relay does not answer.
|
|
231
|
+
|
|
232
|
+
## Drawing the conversation
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { brief, knownBy, rowsOf } from "@pinecall/room";
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
`rowsOf(state.log)` is the conversation as one column, in the order things happened: every turn,
|
|
239
|
+
and every tool run above the reply it was called for. A turn row has `role`, `text`, and for the
|
|
240
|
+
agent `ms` (how long the reply took, end to end), `ttft` (the model's time to its first token —
|
|
241
|
+
what a written call has, since nobody spoke) and `interrupted`. A tool row has `name`, `args` and `output`
|
|
242
|
+
as one short line each, `status` (`running`, `done`, `failed`) and `error` — and `raw`, the
|
|
243
|
+
`arguments` and `output` as they are, for a page that draws more than one line.
|
|
244
|
+
|
|
245
|
+
`knownBy(state.log)` is the agent's own declared fields, in the order its class writes them, as
|
|
246
|
+
`[name, value]` pairs with the empty ones left out. `brief(value, max)` is a value as one line of
|
|
247
|
+
JSON, cut at `max` characters. A field that is an object is opened one level — `person.name`, `person.email` — so a page shows a name rather than a JSON blob.
|
|
248
|
+
|
|
249
|
+
## Sound and the browser's permission
|
|
250
|
+
|
|
251
|
+
In `talk`, the microphone is turned on once the room is joined, and the agent's voice plays through
|
|
252
|
+
a hidden audio element at the end of the page. `chat` joins the same room — the typed lines ride it
|
|
253
|
+
on LiveKit's `lk.chat` topic — and never asks for the microphone and never plays a sound.
|
|
254
|
+
|
|
255
|
+
A browser plays sound only after the visitor has done something on the page. When it has not let
|
|
256
|
+
the page yet, `wantsSound` is `true`: show a button, and call `playSound()` from its click. It is
|
|
257
|
+
asked for once, not on every event the browser fires about it, and after the click it is `false`
|
|
258
|
+
until the browser changes its mind.
|
|
259
|
+
|
|
260
|
+
The microphone is the browser's to give. When the visitor says no, or the device is missing, the
|
|
261
|
+
call fails with a sentence meant for them (below), and the page can offer to write instead.
|
|
262
|
+
|
|
263
|
+
## When something is refused
|
|
264
|
+
|
|
265
|
+
Nothing fails silently. Every refusal is a phase and a sentence, or a rejected promise.
|
|
266
|
+
|
|
267
|
+
| what happened | the state |
|
|
268
|
+
|---|---|
|
|
269
|
+
| `tokens` threw (your server, or the gateway behind it, said no) | `failed`, `error` is its message; nothing was opened |
|
|
270
|
+
| the microphone was refused in `talk` | `failed`, `error` is "I could not get to your microphone. Check the browser's permission, or write instead." |
|
|
271
|
+
| the room could not be joined in `chat` | `failed`, `error` is LiveKit's message |
|
|
272
|
+
| `livekit-client` could not be loaded | `failed`, `error` is the loader's message |
|
|
273
|
+
| `callMe` threw | `failed`, `error` is its message |
|
|
274
|
+
| `callMe()` on a room given no `callMe` | `failed`, `error` says so |
|
|
275
|
+
| the log relay answered something other than 2xx or 204 | on the phone: `failed`, "the log answered 401". In a room: `connection` is `ended` and the phase is left to the seat, which is the call |
|
|
276
|
+
| the room closed from the other side | `ended`; the log is read on until the score |
|
|
277
|
+
| an entry of the log this version cannot read | skipped; `onSkipped(why)` is called, and the rest of the call still draws |
|
|
278
|
+
| `send()` outside a live seat | the promise rejects with "not in a call"; an empty line is nothing |
|
|
279
|
+
|
|
280
|
+
On the server, `mint` and `dial` throw `GatewayRefused` for a refusal: `status` is the gateway's,
|
|
281
|
+
and `detail` its sentence when it sent one (`null` when the body was not the gateway's JSON). The
|
|
282
|
+
sentence names the fix — a quota, a scope the key does not hold, a number the org cannot call —
|
|
283
|
+
so pass it on rather than rewording it.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** @pinecall/room: the caller's side of a Pinecall call in a browser — one store, no framework. */
|
|
2
|
+
export { room } from "./room.js";
|
|
3
|
+
export type { Minted, Mode, Phase, RoomOptions, RoomState, RoomStore } from "./room.js";
|
|
4
|
+
export type { Store } from "./store.js";
|
|
5
|
+
export type { Connection } from "./log/follow.js";
|
|
6
|
+
export type { Speaking } from "./seat/join.js";
|
|
7
|
+
export type { LivekitModule } from "./seat/livekit.js";
|
|
8
|
+
export { brief, knownBy, rowsOf, type Row } from "./rows.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mGAAmG;AAEnG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACxF,YAAY,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,GAAG,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mGAAmG;AAEnG,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAMjC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAY,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The log folded as it arrives: the protocol's own reducer, one entry at a time, every entry kept. */
|
|
2
|
+
import { type Entry, type State } from "@pinecall/protocol";
|
|
3
|
+
import type { SseMessage } from "./sse.js";
|
|
4
|
+
/** What the page draws from: the call as the protocol folds it, and the entries it was folded from. */
|
|
5
|
+
export interface Folded {
|
|
6
|
+
log: State;
|
|
7
|
+
entries: Entry[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* One call's log, folding. The protocol's `apply` mutates the state it is given, so what this
|
|
11
|
+
* holds is a working copy: `snapshot()` is what leaves it, and nothing that left is ever changed.
|
|
12
|
+
*/
|
|
13
|
+
export declare class LogFold {
|
|
14
|
+
#private;
|
|
15
|
+
constructor(onSkipped: (why: string) => void);
|
|
16
|
+
/** The working state, for a question asked right now. Read it; never keep it or hand it out. */
|
|
17
|
+
get now(): Readonly<State>;
|
|
18
|
+
/** Fold one message in. An entry this copy of the wire cannot read is one entry, not a broken call. */
|
|
19
|
+
take(message: SseMessage): Entry | null;
|
|
20
|
+
/** A copy to publish: the page may keep it, compare it and hand it to React. */
|
|
21
|
+
snapshot(): Folded;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=fold.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold.d.ts","sourceRoot":"","sources":["../../src/log/fold.ts"],"names":[],"mappings":"AAAA,uGAAuG;AAEvG,OAAO,EAAoC,KAAK,KAAK,EAAE,KAAK,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE9F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,uGAAuG;AACvG,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,KAAK,CAAC;IACX,OAAO,EAAE,KAAK,EAAE,CAAC;CAClB;AAED;;;GAGG;AACH,qBAAa,OAAO;;IAKlB,YAAY,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EAE3C;IAED,gGAAgG;IAChG,IAAI,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,CAEzB;IAED,uGAAuG;IACvG,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,KAAK,GAAG,IAAI,CAatC;IAED,gFAAgF;IAChF,QAAQ,IAAI,MAAM,CAEjB;CACF"}
|
package/dist/log/fold.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** The log folded as it arrives: the protocol's own reducer, one entry at a time, every entry kept. */
|
|
2
|
+
import { apply, decodeEntry, initialState } from "@pinecall/protocol";
|
|
3
|
+
/**
|
|
4
|
+
* One call's log, folding. The protocol's `apply` mutates the state it is given, so what this
|
|
5
|
+
* holds is a working copy: `snapshot()` is what leaves it, and nothing that left is ever changed.
|
|
6
|
+
*/
|
|
7
|
+
export class LogFold {
|
|
8
|
+
#state = initialState();
|
|
9
|
+
#entries = [];
|
|
10
|
+
#onSkipped;
|
|
11
|
+
constructor(onSkipped) {
|
|
12
|
+
this.#onSkipped = onSkipped;
|
|
13
|
+
}
|
|
14
|
+
/** The working state, for a question asked right now. Read it; never keep it or hand it out. */
|
|
15
|
+
get now() {
|
|
16
|
+
return this.#state;
|
|
17
|
+
}
|
|
18
|
+
/** Fold one message in. An entry this copy of the wire cannot read is one entry, not a broken call. */
|
|
19
|
+
take(message) {
|
|
20
|
+
if (message.data === "")
|
|
21
|
+
return null;
|
|
22
|
+
let entry;
|
|
23
|
+
try {
|
|
24
|
+
entry = decodeEntry(JSON.parse(message.data));
|
|
25
|
+
}
|
|
26
|
+
catch (why) {
|
|
27
|
+
const said = why instanceof Error ? why.message : String(why);
|
|
28
|
+
this.#onSkipped(`${message.event ?? "a message"} at ${message.id ?? "no id"}: ${said.split("\n")[0] ?? said}`);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
this.#state = apply(this.#state, entry);
|
|
32
|
+
this.#entries.push(entry);
|
|
33
|
+
return entry;
|
|
34
|
+
}
|
|
35
|
+
/** A copy to publish: the page may keep it, compare it and hand it to React. */
|
|
36
|
+
snapshot() {
|
|
37
|
+
return { log: structuredClone(this.#state), entries: [...this.#entries] };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=fold.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold.js","sourceRoot":"","sources":["../../src/log/fold.ts"],"names":[],"mappings":"AAAA,uGAAuG;AAEvG,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,YAAY,EAA0B,MAAM,oBAAoB,CAAC;AAU9F;;;GAGG;AACH,MAAM,OAAO,OAAO;IAClB,MAAM,GAAU,YAAY,EAAE,CAAC;IAC/B,QAAQ,GAAY,EAAE,CAAC;IACd,UAAU,CAAwB;IAE3C,YAAY,SAAgC;QAC1C,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED,gGAAgG;IAChG,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,uGAAuG;IACvG,IAAI,CAAC,OAAmB;QACtB,IAAI,OAAO,CAAC,IAAI,KAAK,EAAE;YAAE,OAAO,IAAI,CAAC;QACrC,IAAI,KAAY,CAAC;QACjB,IAAI,CAAC;YACH,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QAChD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC9D,IAAI,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,KAAK,IAAI,WAAW,OAAO,OAAO,CAAC,EAAE,IAAI,OAAO,KAAK,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;YAC/G,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,gFAAgF;IAChF,QAAQ;QACN,OAAO,EAAE,GAAG,EAAE,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;IAC5E,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Follow a call's log over fetch: from where it was, through a dropped stream, to the score. */
|
|
2
|
+
import { type SseMessage } from "./sse.js";
|
|
3
|
+
/** What the page can honestly say about its stream. Nothing here is a guess. */
|
|
4
|
+
export type Connection = "connecting" | "live" | "reconnecting" | "ended";
|
|
5
|
+
/** Why a follow stopped by itself: the score arrived, the call was sealed, or the relay said no. */
|
|
6
|
+
export type Ending = {
|
|
7
|
+
kind: "scored";
|
|
8
|
+
} | {
|
|
9
|
+
kind: "sealed";
|
|
10
|
+
} | {
|
|
11
|
+
kind: "refused";
|
|
12
|
+
status: number;
|
|
13
|
+
};
|
|
14
|
+
/** What the room wants of its stream: every message in order, and how the connection is going. */
|
|
15
|
+
export interface LogReader {
|
|
16
|
+
/** Every message, in seq order, the moment it is framed. The cursor has already moved past it. */
|
|
17
|
+
onMessage: (message: SseMessage) => void;
|
|
18
|
+
onConnection: (connection: Exclude<Connection, "ended">) => void;
|
|
19
|
+
/** Called once, and nothing is said after it. Never called when the room stopped the follow. */
|
|
20
|
+
onEnded: (ending: Ending) => void;
|
|
21
|
+
}
|
|
22
|
+
/** Open the log at `url`. Returns the call that stops it; stopping twice is stopping once. */
|
|
23
|
+
export declare function followLog(url: string, reader: LogReader, fetcher: typeof fetch): () => void;
|
|
24
|
+
//# sourceMappingURL=follow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"follow.d.ts","sourceRoot":"","sources":["../../src/log/follow.ts"],"names":[],"mappings":"AAAA,iGAAiG;AAIjG,OAAO,EAAa,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtD,gFAAgF;AAChF,MAAM,MAAM,UAAU,GAAG,YAAY,GAAG,MAAM,GAAG,cAAc,GAAG,OAAO,CAAC;AAE1E,oGAAoG;AACpG,MAAM,MAAM,MAAM,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnG,kGAAkG;AAClG,MAAM,WAAW,SAAS;IACxB,kGAAkG;IAClG,SAAS,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,IAAI,CAAC;IACzC,YAAY,EAAE,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IACjE,gGAAgG;IAChG,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC;AAUD,8FAA8F;AAC9F,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,KAAK,GAAG,MAAM,IAAI,CAsE3F"}
|