@frontera-sdk/agents 0.0.0 → 1.52.27
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 +78 -2
- package/package.json +67 -4
- package/src/agents-client.ts +56 -0
- package/src/errors.ts +15 -0
- package/src/hooks.ts +130 -0
- package/src/provider.tsx +19 -0
- package/src/types.ts +132 -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
CHANGED
|
@@ -1,4 +1,80 @@
|
|
|
1
1
|
# @frontera-sdk/agents
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
React hooks for reading Agents from inside a Frontera app: the conversation
|
|
4
|
+
counts, outcomes, handoffs, errors, reply times and cost that Agents › Monitor
|
|
5
|
+
shows, and the customer sessions and their text that Agents › Sessions shows.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @frontera-sdk/agents @frontera-sdk/core react @tanstack/react-query
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { agentsProvider } from '@frontera-sdk/agents/provider'
|
|
13
|
+
import { useAgentMonitor, useAgents } from '@frontera-sdk/agents/hooks'
|
|
14
|
+
import { isAppReadOff } from '@frontera-sdk/agents/errors'
|
|
15
|
+
import { createFronteraApp } from '@frontera-sdk/core/create-frontera-app'
|
|
16
|
+
|
|
17
|
+
createFronteraApp(<App />, { providers: [agentsProvider] })
|
|
18
|
+
|
|
19
|
+
function App() {
|
|
20
|
+
const agents = useAgents()
|
|
21
|
+
const agent = agents.data?.[0]
|
|
22
|
+
const monitor = useAgentMonitor(agent?.id, { from: '2026-10-01', to: '2026-10-08' })
|
|
23
|
+
|
|
24
|
+
if (isAppReadOff(agents.error)) return <p>Ask a workspace admin to turn on Agent metrics in Apps.</p>
|
|
25
|
+
return <p>{monitor.data ? `${monitor.data.sessions.total} conversations` : 'Loading…'}</p>
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## What an App can read
|
|
30
|
+
|
|
31
|
+
| Export | Returns |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `useAgents()` | The Agents of the App's workspace, as `{ id, name }`, while either read is on |
|
|
34
|
+
| `useAgentMonitor(agentId, period, options)` | That Agent's Monitor metrics for the period |
|
|
35
|
+
| `useAgentSessions(agentId, query, options)` | One page of that Agent's customer sessions, newest first, and `nextCursor` |
|
|
36
|
+
| `useAgentSession(agentId, sessionId, options)` | One session and its text lines |
|
|
37
|
+
| `isAppReadOff(error)` | `true` when the workspace has the read turned off |
|
|
38
|
+
|
|
39
|
+
Each hook stays disabled while an id it needs is `undefined`, so it composes
|
|
40
|
+
with `useAgents` without a guard. Every hook accepts React Query options.
|
|
41
|
+
|
|
42
|
+
The period covers sessions that started from `from` up to, and not including,
|
|
43
|
+
`to`. Each value is a `Date`, a day such as `2026-10-01`, or an ISO date-time.
|
|
44
|
+
Without a period the metrics cover the last 7 days. A period is at most a year.
|
|
45
|
+
Pass `channelId` to count one channel only.
|
|
46
|
+
|
|
47
|
+
## Conversations
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
const page = useAgentSessions(agent?.id, { status: 'ended', from: '2026-10-01', limit: 20, cursor })
|
|
51
|
+
const open = useAgentSession(agent?.id, openId)
|
|
52
|
+
|
|
53
|
+
page.data?.items.map((s) => `${s.startedAt} ${s.channel?.name ?? s.surface} ${s.endCustomer.label}`)
|
|
54
|
+
open.data?.transcript.map((line) => `${line.role}: ${line.text}`)
|
|
55
|
+
// Next page: setCursor(page.data?.nextCursor ?? undefined)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The list holds customer sessions only. Preview, Try and evaluation sessions
|
|
59
|
+
never appear, and neither do a member's own conversations: Frontera Chat, a
|
|
60
|
+
Chat App they opened, or an Agent run for them. Messages that reach the Agent
|
|
61
|
+
through a channel, such as Slack, Teams or WhatsApp, are listed whoever wrote
|
|
62
|
+
them, a colleague's included. A customer is shown as `Guest`
|
|
63
|
+
or `Verified customer`, never by any part of their phone number or account.
|
|
64
|
+
`previousSessionId` links one customer's sessions. A session's `transcript`
|
|
65
|
+
holds the text lines the customer, the Agent and staff wrote, oldest first, up
|
|
66
|
+
to the newest 500 messages; it never holds tool steps or collected values. Text
|
|
67
|
+
is shown as it was written. A customer's lines can contain anything they typed,
|
|
68
|
+
and the Agent's replies can repeat details the Agent looked up. A session of another
|
|
69
|
+
Agent answers `404`. With conversations on, `useAgentMonitor` also returns
|
|
70
|
+
`attention`, the sessions that need attention.
|
|
71
|
+
|
|
72
|
+
## Who sees what
|
|
73
|
+
|
|
74
|
+
The workspace decides. Metrics are off until a workspace admin who can read
|
|
75
|
+
metrics turns on **Agent metrics in Apps** in Workspace settings; sessions and
|
|
76
|
+
their text are off until a workspace admin who can read conversations turns on
|
|
77
|
+
**Agent conversations in Apps**. Then everyone who can open the App, and anyone with
|
|
78
|
+
a workspace API key, reads the same data, whatever their own role. Each time an
|
|
79
|
+
App opens one session's text, the audit log records it. An Agent of another
|
|
80
|
+
workspace answers `404`, like one that does not exist.
|
package/package.json
CHANGED
|
@@ -1,8 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontera-sdk/agents",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.52.27",
|
|
4
|
+
"description": "React hooks for reading Agent metrics and conversations from inside a Frontera app.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"frontera",
|
|
7
|
+
"agents",
|
|
8
|
+
"react",
|
|
9
|
+
"hooks"
|
|
10
|
+
],
|
|
5
11
|
"license": "Apache-2.0",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
12
|
+
"type": "module",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"!src/__tests__",
|
|
16
|
+
"!src/**/*.test.ts",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
"./types": {
|
|
25
|
+
"types": "./src/types.ts",
|
|
26
|
+
"import": "./src/types.ts"
|
|
27
|
+
},
|
|
28
|
+
"./errors": {
|
|
29
|
+
"types": "./src/errors.ts",
|
|
30
|
+
"import": "./src/errors.ts"
|
|
31
|
+
},
|
|
32
|
+
"./agents-client": {
|
|
33
|
+
"types": "./src/agents-client.ts",
|
|
34
|
+
"import": "./src/agents-client.ts"
|
|
35
|
+
},
|
|
36
|
+
"./hooks": {
|
|
37
|
+
"types": "./src/hooks.ts",
|
|
38
|
+
"import": "./src/hooks.ts"
|
|
39
|
+
},
|
|
40
|
+
"./provider": {
|
|
41
|
+
"types": "./src/provider.tsx",
|
|
42
|
+
"import": "./src/provider.tsx"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "bun test",
|
|
47
|
+
"typecheck": "bunx tsc --noEmit"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@frontera-sdk/core": "1.52.27"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@tanstack/react-query": "^5.90.21",
|
|
54
|
+
"react": "^19.0.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"@tanstack/react-query": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"react": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@tanstack/react-query": "^5.90.21",
|
|
66
|
+
"@types/bun": "^1.3.14",
|
|
67
|
+
"@types/react": "^19",
|
|
68
|
+
"react": "19.2.5",
|
|
69
|
+
"typescript": "^5.9.3"
|
|
70
|
+
}
|
|
8
71
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { FronteraClient } from '@frontera-sdk/core/client'
|
|
2
|
+
|
|
3
|
+
import type { AgentConversation, AgentMonitor, AgentSessionPage, AgentSessionQuery, AppAgent, MonitorPeriod } from './types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Typed client for the App read routes.
|
|
7
|
+
*
|
|
8
|
+
* Every method reads the App's own workspace; the App token on the
|
|
9
|
+
* `FronteraClient` names the App. While the workspace has the read turned off
|
|
10
|
+
* (Agent metrics in Apps for `monitor`, Agent conversations in Apps for
|
|
11
|
+
* `sessions` and `session`, both for `agents`), the method fails with
|
|
12
|
+
* `isAppReadOff(error)` true.
|
|
13
|
+
* An Agent outside the workspace answers 404, like one that does not exist, so
|
|
14
|
+
* start from `agents()` rather than a hard-coded id.
|
|
15
|
+
*/
|
|
16
|
+
export class AgentsClient {
|
|
17
|
+
constructor(private readonly client: FronteraClient) {}
|
|
18
|
+
|
|
19
|
+
async agents(): Promise<AppAgent[]> {
|
|
20
|
+
return (await this.client.request<{ agents: AppAgent[] }>('/v1/app-reads/agents')).agents
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
monitor(agentId: string, period: MonitorPeriod = {}): Promise<AgentMonitor> {
|
|
24
|
+
return this.client.request<AgentMonitor>(`/v1/app-reads/agents/${encodeURIComponent(agentId)}/monitor`, {
|
|
25
|
+
query: {
|
|
26
|
+
from: wire(period.from),
|
|
27
|
+
to: wire(period.to),
|
|
28
|
+
channelId: period.channelId,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
sessions(agentId: string, query: AgentSessionQuery = {}): Promise<AgentSessionPage> {
|
|
34
|
+
return this.client.request<AgentSessionPage>(`/v1/app-reads/agents/${encodeURIComponent(agentId)}/sessions`, {
|
|
35
|
+
query: {
|
|
36
|
+
from: wire(query.from),
|
|
37
|
+
to: wire(query.to),
|
|
38
|
+
status: query.status,
|
|
39
|
+
channelId: query.channelId,
|
|
40
|
+
limit: query.limit,
|
|
41
|
+
cursor: query.cursor,
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
session(agentId: string, sessionId: string): Promise<AgentConversation> {
|
|
47
|
+
return this.client.request<AgentConversation>(
|
|
48
|
+
`/v1/app-reads/agents/${encodeURIComponent(agentId)}/sessions/${encodeURIComponent(sessionId)}`,
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A `Date` as ISO; a string as given, so a day stays a day. */
|
|
54
|
+
export function wire(value: Date | string | undefined): string | undefined {
|
|
55
|
+
return value instanceof Date ? value.toISOString() : value
|
|
56
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FronteraError } from '@frontera-sdk/core/errors'
|
|
2
|
+
|
|
3
|
+
/** A refusal because the App's workspace has this read turned off. */
|
|
4
|
+
export type AppReadOffError = FronteraError & { code: 'APP_READ_OFF' }
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Narrow an error from any hook or client method here. Retrying cannot change
|
|
8
|
+
* the answer: a workspace admin turns on Agent metrics in Apps (for metrics)
|
|
9
|
+
* or Agent conversations in Apps (for sessions and their text) in Workspace
|
|
10
|
+
* settings. Either one opens the Agents list.
|
|
11
|
+
*/
|
|
12
|
+
export function isAppReadOff(error: unknown): error is AppReadOffError {
|
|
13
|
+
// By code rather than `instanceof`: an App can bundle a second copy of the core package.
|
|
14
|
+
return error instanceof Error && (error as { code?: unknown }).code === 'APP_READ_OFF'
|
|
15
|
+
}
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react'
|
|
2
|
+
import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query'
|
|
3
|
+
|
|
4
|
+
import { wire, type AgentsClient } from './agents-client'
|
|
5
|
+
import { isAppReadOff } from './errors'
|
|
6
|
+
import type { AgentConversation, AgentMonitor, AgentSessionPage, AgentSessionQuery, AppAgent, MonitorPeriod } from './types'
|
|
7
|
+
|
|
8
|
+
/** Query keys. Periods are sent on the wire form first, so a `Date` and its ISO string share a cache entry. */
|
|
9
|
+
export const agentKeys = {
|
|
10
|
+
all: ['frontera-agents'] as const,
|
|
11
|
+
agents: () => ['frontera-agents', 'agents'] as const,
|
|
12
|
+
monitor: (agentId: string, period: MonitorPeriod = {}) =>
|
|
13
|
+
['frontera-agents', 'monitor', agentId, wire(period.from) ?? null, wire(period.to) ?? null, period.channelId ?? null] as const,
|
|
14
|
+
sessions: (agentId: string, query: AgentSessionQuery = {}) =>
|
|
15
|
+
[
|
|
16
|
+
'frontera-agents', 'sessions', agentId, wire(query.from) ?? null, wire(query.to) ?? null, query.status ?? null,
|
|
17
|
+
query.channelId ?? null, query.limit ?? null, query.cursor ?? null,
|
|
18
|
+
] as const,
|
|
19
|
+
session: (agentId: string, sessionId: string) => ['frontera-agents', 'session', agentId, sessionId] as const,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const FronteraAgentsContext = createContext<AgentsClient | null>(null)
|
|
23
|
+
|
|
24
|
+
export function useAgentsClient(): AgentsClient {
|
|
25
|
+
const client = useContext(FronteraAgentsContext)
|
|
26
|
+
if (!client) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
'No AgentsClient in context. Add agentsProvider to the providers of createFronteraApp or FronteraAppProvider.',
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
return client
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type ReadOptions<TData> = Omit<UseQueryOptions<TData, Error>, 'queryKey' | 'queryFn'>
|
|
35
|
+
|
|
36
|
+
/** A switched-off read is not retried: asking again cannot change the answer. */
|
|
37
|
+
const retryUnlessOff = (failures: number, error: Error) => !isAppReadOff(error) && failures < 3
|
|
38
|
+
|
|
39
|
+
/** The Agents of the App's workspace. */
|
|
40
|
+
export function useAgents(options: ReadOptions<AppAgent[]> = {}): UseQueryResult<AppAgent[], Error> {
|
|
41
|
+
const client = useAgentsClient()
|
|
42
|
+
return useQuery({
|
|
43
|
+
queryKey: agentKeys.agents(),
|
|
44
|
+
queryFn: () => client.agents(),
|
|
45
|
+
retry: retryUnlessOff,
|
|
46
|
+
...options,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The query `useAgentMonitor` runs. Disabled while `agentId` is missing, so a
|
|
52
|
+
* caller composes it with `useAgents` without a guard; the caller's own
|
|
53
|
+
* `enabled` applies once the id is known.
|
|
54
|
+
*/
|
|
55
|
+
export function agentMonitorQuery(
|
|
56
|
+
client: AgentsClient,
|
|
57
|
+
agentId: string | null | undefined,
|
|
58
|
+
period: MonitorPeriod = {},
|
|
59
|
+
options: ReadOptions<AgentMonitor> = {},
|
|
60
|
+
): UseQueryOptions<AgentMonitor, Error> {
|
|
61
|
+
return {
|
|
62
|
+
queryKey: agentKeys.monitor(agentId ?? '', period),
|
|
63
|
+
queryFn: () => client.monitor(agentId!, period),
|
|
64
|
+
retry: retryUnlessOff,
|
|
65
|
+
...options,
|
|
66
|
+
enabled: agentId ? (options.enabled ?? true) : false,
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** An Agent's Monitor metrics for a period. */
|
|
71
|
+
export function useAgentMonitor(
|
|
72
|
+
agentId: string | null | undefined,
|
|
73
|
+
period: MonitorPeriod = {},
|
|
74
|
+
options: ReadOptions<AgentMonitor> = {},
|
|
75
|
+
): UseQueryResult<AgentMonitor, Error> {
|
|
76
|
+
return useQuery(agentMonitorQuery(useAgentsClient(), agentId, period, options))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The query `useAgentSessions` runs: one page of an Agent's customer
|
|
81
|
+
* sessions. Disabled while `agentId` is missing.
|
|
82
|
+
*/
|
|
83
|
+
export function agentSessionsQuery(
|
|
84
|
+
client: AgentsClient,
|
|
85
|
+
agentId: string | null | undefined,
|
|
86
|
+
query: AgentSessionQuery = {},
|
|
87
|
+
options: ReadOptions<AgentSessionPage> = {},
|
|
88
|
+
): UseQueryOptions<AgentSessionPage, Error> {
|
|
89
|
+
return {
|
|
90
|
+
queryKey: agentKeys.sessions(agentId ?? '', query),
|
|
91
|
+
queryFn: () => client.sessions(agentId!, query),
|
|
92
|
+
retry: retryUnlessOff,
|
|
93
|
+
...options,
|
|
94
|
+
enabled: agentId ? (options.enabled ?? true) : false,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** One page of an Agent's customer sessions, newest first. Pass `nextCursor` as `cursor` for the next page. */
|
|
99
|
+
export function useAgentSessions(
|
|
100
|
+
agentId: string | null | undefined,
|
|
101
|
+
query: AgentSessionQuery = {},
|
|
102
|
+
options: ReadOptions<AgentSessionPage> = {},
|
|
103
|
+
): UseQueryResult<AgentSessionPage, Error> {
|
|
104
|
+
return useQuery(agentSessionsQuery(useAgentsClient(), agentId, query, options))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The query `useAgentSession` runs. Disabled until both ids are known. */
|
|
108
|
+
export function agentSessionQuery(
|
|
109
|
+
client: AgentsClient,
|
|
110
|
+
agentId: string | null | undefined,
|
|
111
|
+
sessionId: string | null | undefined,
|
|
112
|
+
options: ReadOptions<AgentConversation> = {},
|
|
113
|
+
): UseQueryOptions<AgentConversation, Error> {
|
|
114
|
+
return {
|
|
115
|
+
queryKey: agentKeys.session(agentId ?? '', sessionId ?? ''),
|
|
116
|
+
queryFn: () => client.session(agentId!, sessionId!),
|
|
117
|
+
retry: retryUnlessOff,
|
|
118
|
+
...options,
|
|
119
|
+
enabled: agentId && sessionId ? (options.enabled ?? true) : false,
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** One session and its text lines, as the customer and the Agent wrote them. */
|
|
124
|
+
export function useAgentSession(
|
|
125
|
+
agentId: string | null | undefined,
|
|
126
|
+
sessionId: string | null | undefined,
|
|
127
|
+
options: ReadOptions<AgentConversation> = {},
|
|
128
|
+
): UseQueryResult<AgentConversation, Error> {
|
|
129
|
+
return useQuery(agentSessionQuery(useAgentsClient(), agentId, sessionId, options))
|
|
130
|
+
}
|
package/src/provider.tsx
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import type { FronteraClient } from '@frontera-sdk/core/client'
|
|
3
|
+
|
|
4
|
+
import { AgentsClient } from './agents-client'
|
|
5
|
+
import { FronteraAgentsContext } from './hooks'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Plug Agent reads into `createFronteraApp` or `FronteraAppProvider`:
|
|
9
|
+
*
|
|
10
|
+
* ```tsx
|
|
11
|
+
* createFronteraApp(<App />, { providers: [blueprintProvider, agentsProvider] })
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The client is rebuilt whenever the host rotates the credential, because the
|
|
15
|
+
* `FronteraClient` it wraps is replaced rather than mutated.
|
|
16
|
+
*/
|
|
17
|
+
export function agentsProvider(value: { client: FronteraClient }, children: ReactNode): ReactNode {
|
|
18
|
+
return <FronteraAgentsContext.Provider value={new AgentsClient(value.client)}>{children}</FronteraAgentsContext.Provider>
|
|
19
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an App reads from Agents, as the App sees it. Dates arrive as ISO
|
|
3
|
+
* strings (JSON), so they are typed as strings.
|
|
4
|
+
*
|
|
5
|
+
* Metrics are open only while the App's workspace turns on Agent metrics in
|
|
6
|
+
* Apps; sessions and their text only while it turns on Agent conversations in
|
|
7
|
+
* Apps.
|
|
8
|
+
* Every person who opens the App reads the same data.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** An Agent of the App's workspace. */
|
|
12
|
+
export interface AppAgent {
|
|
13
|
+
id: string
|
|
14
|
+
name: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Which sessions the metrics cover: those that started in `[from, to)`,
|
|
19
|
+
* optionally on one channel. A `Date`, a day (`2026-10-01`) or an ISO
|
|
20
|
+
* date-time. Defaults to the last 7 days; at most a year.
|
|
21
|
+
*/
|
|
22
|
+
export interface MonitorPeriod {
|
|
23
|
+
from?: Date | string
|
|
24
|
+
to?: Date | string
|
|
25
|
+
channelId?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An Agent's Monitor metrics for a period, as Agents › Monitor shows them.
|
|
30
|
+
* Customer sessions only: Preview, Try and evaluation sessions never count.
|
|
31
|
+
*/
|
|
32
|
+
export interface AgentMonitor {
|
|
33
|
+
period: { from: string; to: string }
|
|
34
|
+
sessions: { total: number; open: number; ended: number; continued: number; endedByInactivity: number }
|
|
35
|
+
outcomes: {
|
|
36
|
+
recorded: { resolved: number; handedOff: number; unresolved: number; resolutionRate: number | null; concluded: number }
|
|
37
|
+
inferred: { analysed: number; resolved: number; unresolved: number; abandoned: number; unknown: number }
|
|
38
|
+
unknown: number
|
|
39
|
+
}
|
|
40
|
+
handoffs: { sessions: number; rate: number | null }
|
|
41
|
+
focus: { decisions: number; rejected: number; uncertain: number; unavailable: number; blockedRate: number | null }
|
|
42
|
+
errors: { sessions: number; events: number; actionFailures: number; reconciliations: number; repliesBlocked: number }
|
|
43
|
+
actions: { completed: number }
|
|
44
|
+
latency: { replies: number; p50Ms: number | null; p95Ms: number | null }
|
|
45
|
+
messages: { fromCustomers: number }
|
|
46
|
+
cost: { usd: number; tokens: number; perSession: number | null }
|
|
47
|
+
/** One entry per day with sessions, `day` as `YYYY-MM-DD` in UTC. */
|
|
48
|
+
daily: Array<{ day: string; sessions: number; resolved: number; handedOff: number }>
|
|
49
|
+
byChannel: Array<{ channelId: string | null; surface: string; name: string | null; type: string | null; sessions: number; resolved: number; handedOff: number }>
|
|
50
|
+
byProcedure: Array<{ procedureId: string | null; sessions: number; resolved: number; handedOff: number; unresolved: number }>
|
|
51
|
+
/** What each metric counts and where it comes from. */
|
|
52
|
+
definitions: Record<string, { denominator: string; source: string }>
|
|
53
|
+
/**
|
|
54
|
+
* Up to 20 sessions that need attention, newest first. Present only while
|
|
55
|
+
* the workspace also has Agent conversations in Apps turned on.
|
|
56
|
+
*/
|
|
57
|
+
attention?: Array<{
|
|
58
|
+
sessionId: string
|
|
59
|
+
status: AgentSessionStatus
|
|
60
|
+
reason: 'reconciliation' | 'error' | 'analysis_failed'
|
|
61
|
+
startedAt: string
|
|
62
|
+
lastActivityAt: string
|
|
63
|
+
}>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type AgentSessionStatus = 'active' | 'waiting_customer' | 'action_pending' | 'ended'
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Which sessions to list: those that started in `[from, to)`, newest first.
|
|
70
|
+
* `status` `attention` keeps the sessions Monitor counts as needing attention.
|
|
71
|
+
* Pass the previous page's `nextCursor` as `cursor` for the next page.
|
|
72
|
+
*/
|
|
73
|
+
export interface AgentSessionQuery {
|
|
74
|
+
from?: Date | string
|
|
75
|
+
to?: Date | string
|
|
76
|
+
status?: 'open' | 'ended' | 'attention'
|
|
77
|
+
channelId?: string
|
|
78
|
+
/** 1 to 100; 25 by default. */
|
|
79
|
+
limit?: number
|
|
80
|
+
cursor?: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* One customer session. Preview, Try and evaluation sessions never appear, and
|
|
85
|
+
* neither do a member's own conversations: Frontera Chat, a Chat App they
|
|
86
|
+
* opened, or an Agent run for them.
|
|
87
|
+
*/
|
|
88
|
+
export interface AgentSession {
|
|
89
|
+
id: string
|
|
90
|
+
surface: 'channel' | 'chat-app' | 'api'
|
|
91
|
+
channel: { id: string; name: string; type: string } | null
|
|
92
|
+
status: AgentSessionStatus
|
|
93
|
+
harness: string
|
|
94
|
+
phase: 'step-by-step' | null
|
|
95
|
+
procedure: { id: string; name: string } | null
|
|
96
|
+
outcome: 'resolved' | 'handed_off' | 'abandoned' | 'unresolved' | 'unknown' | 'transferred' | null
|
|
97
|
+
outcomeSource: 'recorded' | 'inferred' | null
|
|
98
|
+
endReason: 'inactivity' | 'explicit' | 'call_completed' | 'superseded' | 'closed_by_staff' | 'transferred' | null
|
|
99
|
+
startedAt: string
|
|
100
|
+
lastActivityAt: string
|
|
101
|
+
lastCustomerActivityAt: string
|
|
102
|
+
expiresAt: string | null
|
|
103
|
+
endedAt: string | null
|
|
104
|
+
/** Never the customer's phone number or any part of it. */
|
|
105
|
+
endCustomer: { label: 'Guest' | 'Verified customer'; verified: boolean }
|
|
106
|
+
configVersion: { id: string; number: number | null } | null
|
|
107
|
+
/** The same customer's session before this one, when their conversation continued. */
|
|
108
|
+
previousSessionId: string | null
|
|
109
|
+
analysisStatus: string | null
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface AgentSessionPage {
|
|
113
|
+
items: AgentSession[]
|
|
114
|
+
/** The `cursor` for the next page; `null` on the last page. */
|
|
115
|
+
nextCursor: string | null
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** One line of a conversation: text as the customer, the Agent or staff wrote it. */
|
|
119
|
+
export interface TranscriptLine {
|
|
120
|
+
id: string
|
|
121
|
+
role: 'customer' | 'agent'
|
|
122
|
+
/** A person replied while they had control. */
|
|
123
|
+
staff?: boolean
|
|
124
|
+
text: string
|
|
125
|
+
at: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** One session and its text lines, oldest first, at most the newest 500 messages. */
|
|
129
|
+
export interface AgentConversation {
|
|
130
|
+
session: AgentSession
|
|
131
|
+
transcript: TranscriptLine[]
|
|
132
|
+
}
|