@frontera-sdk/chat 1.50.40
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 +215 -0
- package/package.json +110 -0
- package/src/auth.ts +27 -0
- package/src/callback-page.ts +133 -0
- package/src/chat-client.ts +472 -0
- package/src/connect-popup.ts +352 -0
- package/src/doctor.ts +176 -0
- package/src/hooks.ts +202 -0
- package/src/provider.tsx +25 -0
- package/src/stream.ts +61 -0
- package/src/token-session.ts +182 -0
- package/src/transport.ts +200 -0
- package/src/types.ts +370 -0
- package/src/ui-stream.ts +49 -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 Sebati
|
|
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,215 @@
|
|
|
1
|
+
# @frontera-sdk/chat
|
|
2
|
+
|
|
3
|
+
Typed client and React hooks for chatting with a Frontera agent from your own application. This package is the chat **API** — you bring the UI.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun add @frontera-sdk/chat @frontera-sdk/core ai
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Credentials
|
|
10
|
+
|
|
11
|
+
Two kinds, both carried by `FronteraClient`:
|
|
12
|
+
|
|
13
|
+
- **Agent API key (`sak_…`)** — minted in Console → Agents → API keys. Bound to one agent and its workspace. **A server-side secret: never ship it to a browser.**
|
|
14
|
+
- **End-user chat token (`cht_…`)** — obtained by your backend via `exchangeChatToken` (requires a deployment with the `/v1/chat-tokens` endpoint). Short-lived, scoped to one agent and one of *your* users, safe to use from your frontend. Sebati enforces per-user session isolation for these.
|
|
15
|
+
|
|
16
|
+
## Server-side quickstart
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { FronteraClient } from '@frontera-sdk/core/client'
|
|
20
|
+
import { ChatClient } from '@frontera-sdk/chat/chat-client'
|
|
21
|
+
import { readChatStream } from '@frontera-sdk/chat/ui-stream'
|
|
22
|
+
|
|
23
|
+
const client = new FronteraClient({
|
|
24
|
+
apiBaseUrl: 'https://api.your-deployment.example',
|
|
25
|
+
credential: { kind: 'apiKey', key: process.env.SEBATI_AGENT_KEY! },
|
|
26
|
+
})
|
|
27
|
+
const chat = new ChatClient(client)
|
|
28
|
+
|
|
29
|
+
const session = await chat.createSession()
|
|
30
|
+
const run = await chat.startChat({
|
|
31
|
+
conversationId: session.id,
|
|
32
|
+
messages: [{ id: crypto.randomUUID(), role: 'user', parts: [{ type: 'text', text: 'Halo!' }] }],
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
for await (const message of readChatStream(run.response)) {
|
|
36
|
+
// message.parts grows as the agent streams
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const history = await chat.getMessages(run.conversationId)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## React quickstart
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import { useChat } from '@ai-sdk/react'
|
|
46
|
+
import { createChatTransport } from '@frontera-sdk/chat/transport'
|
|
47
|
+
|
|
48
|
+
const transport = createChatTransport(client, {
|
|
49
|
+
onStart: ({ runId }) => setRunId(runId), // for cancel/steer/reconnect
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
function Chat() {
|
|
53
|
+
const { messages, sendMessage, status } = useChat({ transport })
|
|
54
|
+
// render messages, call sendMessage({ text }) from your composer
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Persisted state (session lists, history) has TanStack Query hooks in `@frontera-sdk/chat/hooks` behind `FronteraChatContext.Provider`.
|
|
59
|
+
|
|
60
|
+
## Deployment patterns
|
|
61
|
+
|
|
62
|
+
1. **Proxy (works everywhere today).** Your frontend talks to your backend; your backend holds the `sak_` key and forwards `/v1/*` to Sebati (`apiBaseUrl` can simply point at your proxy). All of your users share one principal, so **you** must enforce which user may touch which conversation — wire `authorizeConversation` to your own user↔conversation store:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const chat = new ChatClient(client, {
|
|
66
|
+
authorizeConversation: async (conversationId) =>
|
|
67
|
+
(await myDb.conversationOwner(conversationId)) === currentUser.id,
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Under API-key auth the server accepts any conversation id belonging to the key's agent — this callback is the only barrier between your users. Run ids are capabilities: hand them only to the user whose send produced them.
|
|
72
|
+
|
|
73
|
+
2. **Token exchange (deployments with `/v1/chat-tokens`).** Your backend exchanges the key for a per-user token; your frontend talks to Sebati directly and Sebati enforces isolation:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { exchangeChatToken, withChatToken } from '@frontera-sdk/chat/auth'
|
|
77
|
+
const token = await exchangeChatToken(client, { externalUserId: user.id, name: user.name })
|
|
78
|
+
// send `token` to the frontend; there:
|
|
79
|
+
const userClient = withChatToken(client, token)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Staying signed in
|
|
83
|
+
|
|
84
|
+
Mint once per user session; the SDK keeps it alive against the platform, so your backend is
|
|
85
|
+
not called again on a timer:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { createTokenSession } from '@frontera-sdk/chat/token-session'
|
|
89
|
+
|
|
90
|
+
const session = createTokenSession({
|
|
91
|
+
apiBaseUrl: 'https://api.your-deployment.example',
|
|
92
|
+
getToken: () => fetch('/api/chat-token').then((r) => r.json()),
|
|
93
|
+
})
|
|
94
|
+
const chat = new ChatClient(session.getClient())
|
|
95
|
+
|
|
96
|
+
// and so a token that expired while the tab slept costs a retry, not a failure:
|
|
97
|
+
const transport = createChatTransport(session.getClient, { onUnauthorized: session.refresh })
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Renews at 80% of each token's life against `POST /v1/chat-tokens/renew`, falls back to
|
|
101
|
+
`getToken` when the session hits its 12-hour ceiling, and shares one in-flight refresh
|
|
102
|
+
between concurrent callers. Revocation does not depend on any of this: disabling an external
|
|
103
|
+
user takes effect on their next request.
|
|
104
|
+
|
|
105
|
+
## Approvals
|
|
106
|
+
|
|
107
|
+
An agent in `ask` approval mode parks a tool call and **waits**: the stream
|
|
108
|
+
emits `{ type: 'tool-approval-request', approvalId, toolCallId }` and then goes
|
|
109
|
+
quiet until someone answers. Ignore it and the conversation looks stalled.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const pending = await chat.listPendingApprovals(conversationId) // also on mount
|
|
113
|
+
await chat.respondToApproval(pending[0].id, 'approve') // or 'deny'
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
React: `usePendingApprovals(conversationId)` / `useRespondToApproval(conversationId)`.
|
|
117
|
+
|
|
118
|
+
Requires an end-user `cht_` token — a decision belongs to a person, and an
|
|
119
|
+
`sak_` key stands for all of your users at once (403). For unattended
|
|
120
|
+
operation, set the agent's approval mode to `auto` instead.
|
|
121
|
+
|
|
122
|
+
## Connecting your user's own accounts
|
|
123
|
+
|
|
124
|
+
A tool needing the end user's own Google/Microsoft/Slack account parks with
|
|
125
|
+
`pauseReason: 'needs_connect'` and an `installId`:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const conn = await chat.startAppConnection({
|
|
129
|
+
installId: parked.installId,
|
|
130
|
+
redirectUri: 'https://app.yourproduct.example/frontera/oauth/callback',
|
|
131
|
+
})
|
|
132
|
+
window.open(conn.authUrl, 'connect', 'width=520,height=640')
|
|
133
|
+
// your callback → your page:
|
|
134
|
+
await chat.completeAppConnection({ code, state })
|
|
135
|
+
await chat.resumeToolCall(parked.id)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Or let `runAppConnection` do all of it — popup, origin check, exchange, resume, the "they
|
|
139
|
+
closed the window" case, and the mobile fallback where popups are blocked and the flow
|
|
140
|
+
becomes a full-page redirect:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { runAppConnection, completeRedirectedConnection } from '@frontera-sdk/chat/connect-popup'
|
|
144
|
+
|
|
145
|
+
const result = await runAppConnection(chat, {
|
|
146
|
+
pendingToolCallId: parked.id,
|
|
147
|
+
installId: parked.installId,
|
|
148
|
+
redirectUri: 'https://app.yourproduct.example/frontera/oauth/callback',
|
|
149
|
+
})
|
|
150
|
+
// 'connected' | 'cancelled' | 'redirecting' (redirecting = mobile; finishes on return)
|
|
151
|
+
|
|
152
|
+
// once, when your chat mounts:
|
|
153
|
+
useEffect(() => { void completeRedirectedConnection(chat) }, [chat])
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Your callback page, in whichever form fits: `<FronteraOAuthCallback />` from
|
|
157
|
+
`@frontera-sdk/chat-ui/oauth-callback`, `connectCallbackResponse()` from
|
|
158
|
+
`@frontera-sdk/chat/callback-page` for any JS server, or the same HTML served statically by a
|
|
159
|
+
backend in another language.
|
|
160
|
+
|
|
161
|
+
The callback must be on **your** domain — a provider only authorises redirect
|
|
162
|
+
URIs under a domain the OAuth project's owner has verified, and you cannot
|
|
163
|
+
verify ours. So this needs your own OAuth client, which is also the honest
|
|
164
|
+
answer to whose name appears on the consent screen.
|
|
165
|
+
|
|
166
|
+
Setup, once: register the redirect URI with your OAuth client and on the
|
|
167
|
+
workspace embed allowlist, add your client under Settings → Secrets, turn on
|
|
168
|
+
*allow external users* for the install, and mint tokens with
|
|
169
|
+
`capabilities: ['plugin-connect']`. The last two are the two halves of the
|
|
170
|
+
authorization — we decide whether an install is reachable by external users at
|
|
171
|
+
all, you decide which of your users may do it, against your own roles. We never
|
|
172
|
+
model or store those roles; your decision expires with the token.
|
|
173
|
+
|
|
174
|
+
## Development doctor
|
|
175
|
+
|
|
176
|
+
The SDK watches its own stream in development. When a run parks and nothing
|
|
177
|
+
settles it within 10 seconds, it says so in your console, naming the call you
|
|
178
|
+
are missing:
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
[frontera-chat] A tool call is parked waiting for a person to approve it, and nothing has answered.
|
|
182
|
+
The run is alive and BLOCKED — it will not continue until it is settled or expires.
|
|
183
|
+
|
|
184
|
+
const pending = await chat.listPendingApprovals(conversationId)
|
|
185
|
+
await chat.respondToApproval(pending[0].id, 'approve')
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
It covers the three silent failures: an unhandled approval, an unhandled
|
|
189
|
+
`needs_connect`, and a `401` with no refresh wired. Off in production, where it
|
|
190
|
+
would cost a stream tee for a warning nobody reads.
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { chatDoctor } from '@frontera-sdk/chat/doctor'
|
|
194
|
+
chatDoctor.configure({ enabled: false }) // or: graceMs, onWarn
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Errors
|
|
198
|
+
|
|
199
|
+
Failures — including ones that end a `useChat` send — surface as
|
|
200
|
+
`FronteraError` with the service's `code` and `status`, so `UNAUTHORIZED`
|
|
201
|
+
(refresh the token), `QUOTA_EXCEEDED`, `RATE_LIMITED` and `FORBIDDEN` are
|
|
202
|
+
distinguishable rather than one opaque string. Don't switch exhaustively: the
|
|
203
|
+
service adds codes.
|
|
204
|
+
|
|
205
|
+
## Scope and limitations
|
|
206
|
+
|
|
207
|
+
- One `sak_` key = one agent. Multiple agents = one client per key. Per-user agent permissions are your mint/routing decision.
|
|
208
|
+
- The credential's agent always wins; `agentName` in requests is ignored.
|
|
209
|
+
- No file upload endpoint under key auth: `fileAttachments[].url` must be a URL you host.
|
|
210
|
+
- Artifacts/docs the agent creates surface as ids in message parts; their bytes are not fetchable under key auth.
|
|
211
|
+
- `updateSession` / `deleteSession` / `forkSession` / `exportSession` need a credential that names one person: they work under a `cht_` end-user token, and answer 403 under an `sak_` key.
|
|
212
|
+
- Sharing a conversation is platform-only under both credentials — a share link needs no credential at all, so publishing is your product's decision to implement.
|
|
213
|
+
- App tools bound to an END USER's own account (`accountMode: end_user`) need the connect flow above, which requires your own OAuth client and a callback on your domain. Workspace- and agent-owned accounts need none of that.
|
|
214
|
+
- `POST /v1/chat-tokens` is rate limited per credential (120/min by default, `429 RATE_LIMITED`). Exchange once per end-user session — the token is valid for its whole TTL.
|
|
215
|
+
- The stream carries `: ping` SSE keepalive comments every 15s; AI SDK parsers ignore them — don't hand-parse the stream.
|
package/package.json
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@frontera-sdk/chat",
|
|
3
|
+
"version": "1.50.40",
|
|
4
|
+
"description": "Typed client and React hooks for chatting with a Frontera agent from your own application.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"frontera",
|
|
7
|
+
"chat",
|
|
8
|
+
"agent",
|
|
9
|
+
"react",
|
|
10
|
+
"hooks"
|
|
11
|
+
],
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"!src/__tests__",
|
|
17
|
+
"!src/**/*.test.ts",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
"./types": {
|
|
26
|
+
"types": "./src/types.ts",
|
|
27
|
+
"import": "./src/types.ts"
|
|
28
|
+
},
|
|
29
|
+
"./chat-client": {
|
|
30
|
+
"types": "./src/chat-client.ts",
|
|
31
|
+
"import": "./src/chat-client.ts"
|
|
32
|
+
},
|
|
33
|
+
"./auth": {
|
|
34
|
+
"types": "./src/auth.ts",
|
|
35
|
+
"import": "./src/auth.ts"
|
|
36
|
+
},
|
|
37
|
+
"./stream": {
|
|
38
|
+
"types": "./src/stream.ts",
|
|
39
|
+
"import": "./src/stream.ts"
|
|
40
|
+
},
|
|
41
|
+
"./ui-stream": {
|
|
42
|
+
"types": "./src/ui-stream.ts",
|
|
43
|
+
"import": "./src/ui-stream.ts"
|
|
44
|
+
},
|
|
45
|
+
"./doctor": {
|
|
46
|
+
"types": "./src/doctor.ts",
|
|
47
|
+
"import": "./src/doctor.ts"
|
|
48
|
+
},
|
|
49
|
+
"./token-session": {
|
|
50
|
+
"types": "./src/token-session.ts",
|
|
51
|
+
"import": "./src/token-session.ts"
|
|
52
|
+
},
|
|
53
|
+
"./callback-page": {
|
|
54
|
+
"types": "./src/callback-page.ts",
|
|
55
|
+
"import": "./src/callback-page.ts"
|
|
56
|
+
},
|
|
57
|
+
"./connect-popup": {
|
|
58
|
+
"types": "./src/connect-popup.ts",
|
|
59
|
+
"import": "./src/connect-popup.ts"
|
|
60
|
+
},
|
|
61
|
+
"./transport": {
|
|
62
|
+
"types": "./src/transport.ts",
|
|
63
|
+
"import": "./src/transport.ts"
|
|
64
|
+
},
|
|
65
|
+
"./hooks": {
|
|
66
|
+
"types": "./src/hooks.ts",
|
|
67
|
+
"import": "./src/hooks.ts"
|
|
68
|
+
},
|
|
69
|
+
"./provider": {
|
|
70
|
+
"types": "./src/provider.tsx",
|
|
71
|
+
"import": "./src/provider.tsx"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"test": "bun test",
|
|
76
|
+
"typecheck": "bunx tsc --noEmit",
|
|
77
|
+
"smoke": "bun run scripts/smoke.ts"
|
|
78
|
+
},
|
|
79
|
+
"dependencies": {
|
|
80
|
+
"@frontera-sdk/core": "1.50.40"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"@ai-sdk/react": "^3.0.0",
|
|
84
|
+
"@tanstack/react-query": "^5.90.21",
|
|
85
|
+
"ai": "^6.0.116",
|
|
86
|
+
"react": "^19.0.0"
|
|
87
|
+
},
|
|
88
|
+
"peerDependenciesMeta": {
|
|
89
|
+
"@ai-sdk/react": {
|
|
90
|
+
"optional": true
|
|
91
|
+
},
|
|
92
|
+
"@tanstack/react-query": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"ai": {
|
|
96
|
+
"optional": true
|
|
97
|
+
},
|
|
98
|
+
"react": {
|
|
99
|
+
"optional": true
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"devDependencies": {
|
|
103
|
+
"@tanstack/react-query": "^5.90.21",
|
|
104
|
+
"@types/bun": "^1.3.14",
|
|
105
|
+
"@types/react": "^19",
|
|
106
|
+
"ai": "^6.0.116",
|
|
107
|
+
"react": "19.2.5",
|
|
108
|
+
"typescript": "^5.9.3"
|
|
109
|
+
}
|
|
110
|
+
}
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { FronteraClient } from '@frontera-sdk/core/client'
|
|
2
|
+
|
|
3
|
+
import type { ChatToken, ExchangeTokenRequest } from './types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Exchange an agent API key (`sak_`) plus your user's identity for a
|
|
7
|
+
* short-lived scoped chat token (`cht_`). Server-side only: the client passed
|
|
8
|
+
* here must carry the `sak_` credential, which must never reach a browser.
|
|
9
|
+
*
|
|
10
|
+
* The token is safe to hand to your frontend — it is scoped to one workspace,
|
|
11
|
+
* one agent and one external user, and Sebati enforces session isolation per
|
|
12
|
+
* external user.
|
|
13
|
+
*
|
|
14
|
+
* Requires a deployment with the `/v1/chat-tokens` endpoint; older deployments
|
|
15
|
+
* answer 404, surfaced as a `FronteraError` with code `NOT_FOUND`.
|
|
16
|
+
*/
|
|
17
|
+
export function exchangeChatToken(
|
|
18
|
+
client: FronteraClient,
|
|
19
|
+
request: ExchangeTokenRequest,
|
|
20
|
+
): Promise<ChatToken> {
|
|
21
|
+
return client.request<ChatToken>('/v1/chat-tokens', { method: 'POST', body: request })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A client that authenticates with the exchanged end-user token. */
|
|
25
|
+
export function withChatToken(client: FronteraClient, token: ChatToken): FronteraClient {
|
|
26
|
+
return client.withCredential({ kind: 'token', token: token.token })
|
|
27
|
+
}
|