@llmrtc/llmrtc-core 1.0.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 +201 -0
- package/README.md +25 -0
- package/dist/hooks.d.ts +345 -0
- package/dist/hooks.js +52 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/logging-hooks.d.ts +120 -0
- package/dist/logging-hooks.js +280 -0
- package/dist/logging-hooks.js.map +1 -0
- package/dist/metrics.d.ts +243 -0
- package/dist/metrics.js +241 -0
- package/dist/metrics.js.map +1 -0
- package/dist/orchestrator.d.ts +73 -0
- package/dist/orchestrator.js +450 -0
- package/dist/orchestrator.js.map +1 -0
- package/dist/playbook-engine.d.ts +140 -0
- package/dist/playbook-engine.js +390 -0
- package/dist/playbook-engine.js.map +1 -0
- package/dist/playbook-orchestrator.d.ts +193 -0
- package/dist/playbook-orchestrator.js +687 -0
- package/dist/playbook-orchestrator.js.map +1 -0
- package/dist/playbook.d.ts +220 -0
- package/dist/playbook.js +92 -0
- package/dist/playbook.js.map +1 -0
- package/dist/protocol.d.ts +327 -0
- package/dist/protocol.js +149 -0
- package/dist/protocol.js.map +1 -0
- package/dist/tool-executor.d.ts +64 -0
- package/dist/tool-executor.js +240 -0
- package/dist/tool-executor.js.map +1 -0
- package/dist/tools.d.ts +183 -0
- package/dist/tools.js +157 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +213 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @llmrtc/llmrtc-core
|
|
2
|
+
|
|
3
|
+
Core types, interfaces, and orchestration logic for LLMRTC - real-time voice and vision AI with WebRTC + LLMs.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @llmrtc/llmrtc-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Provider-agnostic interfaces for LLM, STT, and TTS
|
|
14
|
+
- Conversation orchestration with streaming support
|
|
15
|
+
- Multi-stage playbook system with tool calling
|
|
16
|
+
- Hooks and metrics for observability
|
|
17
|
+
- Session resilience and state management
|
|
18
|
+
|
|
19
|
+
## Documentation
|
|
20
|
+
|
|
21
|
+
Full documentation: [https://www.llmrtc.org](https://www.llmrtc.org)
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
Apache-2.0
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook interfaces for observability and extensibility
|
|
3
|
+
*
|
|
4
|
+
* These hooks allow integrators to:
|
|
5
|
+
* - Capture timing/latency metrics for STT, LLM, and TTS operations
|
|
6
|
+
* - Plug in custom logging or metrics reporters
|
|
7
|
+
* - Add guardrails, validation, or routing logic without forking
|
|
8
|
+
*/
|
|
9
|
+
import type { STTResult, LLMRequest, LLMChunk, LLMResult, TTSChunk } from './types.js';
|
|
10
|
+
import type { ErrorCode } from './protocol.js';
|
|
11
|
+
import type { ToolCallRequest, ToolCallResult } from './tools.js';
|
|
12
|
+
import type { Stage, Transition } from './playbook.js';
|
|
13
|
+
/**
|
|
14
|
+
* Context for a single conversation turn
|
|
15
|
+
* Passed to all hooks within a turn
|
|
16
|
+
*/
|
|
17
|
+
export interface TurnContext {
|
|
18
|
+
/** Unique identifier for this turn */
|
|
19
|
+
turnId: string;
|
|
20
|
+
/** Optional session identifier */
|
|
21
|
+
sessionId?: string;
|
|
22
|
+
/** Timestamp when the turn started (Date.now()) */
|
|
23
|
+
startTime: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Timing information for completed operations
|
|
27
|
+
*/
|
|
28
|
+
export interface TimingInfo {
|
|
29
|
+
/** Timestamp when operation started (Date.now()) */
|
|
30
|
+
startTime: number;
|
|
31
|
+
/** Timestamp when operation ended (Date.now()) */
|
|
32
|
+
endTime: number;
|
|
33
|
+
/** Duration in milliseconds */
|
|
34
|
+
durationMs: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Context for error hooks
|
|
38
|
+
*/
|
|
39
|
+
export interface ErrorContext {
|
|
40
|
+
/** Structured error code */
|
|
41
|
+
code: ErrorCode;
|
|
42
|
+
/** Component that produced the error */
|
|
43
|
+
component: 'stt' | 'llm' | 'tts' | 'vad' | 'webrtc' | 'server' | 'tool' | 'playbook';
|
|
44
|
+
/** Session identifier if available */
|
|
45
|
+
sessionId?: string;
|
|
46
|
+
/** Turn identifier if available */
|
|
47
|
+
turnId?: string;
|
|
48
|
+
/** Timestamp when error occurred (Date.now()) */
|
|
49
|
+
timestamp: number;
|
|
50
|
+
/** Additional error details */
|
|
51
|
+
details?: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Hooks for the ConversationOrchestrator
|
|
55
|
+
*
|
|
56
|
+
* These hooks are called during the STT → LLM → TTS pipeline.
|
|
57
|
+
* All hooks are optional and can be sync or async.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const orchestrator = new ConversationOrchestrator({
|
|
62
|
+
* providers: { llm, stt, tts },
|
|
63
|
+
* hooks: {
|
|
64
|
+
* onTurnStart(ctx) {
|
|
65
|
+
* console.log(`Turn ${ctx.turnId} started`);
|
|
66
|
+
* },
|
|
67
|
+
* onSTTEnd(ctx, result, timing) {
|
|
68
|
+
* console.log(`STT: "${result.text}" in ${timing.durationMs}ms`);
|
|
69
|
+
* },
|
|
70
|
+
* onLLMEnd(ctx, result, timing) {
|
|
71
|
+
* // Guardrail: check for inappropriate content
|
|
72
|
+
* if (result.fullText.includes('forbidden')) {
|
|
73
|
+
* throw new Error('Content policy violation');
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
* }
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export interface OrchestratorHooks {
|
|
81
|
+
/**
|
|
82
|
+
* Called when a conversation turn starts
|
|
83
|
+
* @param ctx - Turn context with turnId and startTime
|
|
84
|
+
* @param audio - Input audio buffer to be transcribed
|
|
85
|
+
*/
|
|
86
|
+
onTurnStart?(ctx: TurnContext, audio: Buffer): void | Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Called when a conversation turn completes
|
|
89
|
+
* @param ctx - Turn context
|
|
90
|
+
* @param timing - Total turn duration
|
|
91
|
+
*/
|
|
92
|
+
onTurnEnd?(ctx: TurnContext, timing: TimingInfo): void | Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Called before STT transcription begins
|
|
95
|
+
* @param ctx - Turn context
|
|
96
|
+
* @param audio - Audio buffer being transcribed
|
|
97
|
+
*/
|
|
98
|
+
onSTTStart?(ctx: TurnContext, audio: Buffer): void | Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Called when STT transcription completes successfully
|
|
101
|
+
* @param ctx - Turn context
|
|
102
|
+
* @param result - Transcription result
|
|
103
|
+
* @param timing - STT operation duration
|
|
104
|
+
*/
|
|
105
|
+
onSTTEnd?(ctx: TurnContext, result: STTResult, timing: TimingInfo): void | Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Called when STT transcription fails
|
|
108
|
+
* @param ctx - Turn context
|
|
109
|
+
* @param error - Error that occurred
|
|
110
|
+
*/
|
|
111
|
+
onSTTError?(ctx: TurnContext, error: Error): void | Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Called before LLM inference begins
|
|
114
|
+
* @param ctx - Turn context
|
|
115
|
+
* @param request - LLM request with messages and config
|
|
116
|
+
*/
|
|
117
|
+
onLLMStart?(ctx: TurnContext, request: LLMRequest): void | Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Called for each LLM streaming chunk
|
|
120
|
+
* @param ctx - Turn context
|
|
121
|
+
* @param chunk - LLM response chunk
|
|
122
|
+
* @param chunkIndex - Zero-based index of this chunk
|
|
123
|
+
*/
|
|
124
|
+
onLLMChunk?(ctx: TurnContext, chunk: LLMChunk, chunkIndex: number): void | Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Called when LLM inference completes successfully
|
|
127
|
+
* Use this hook for content guardrails - throw an error to cancel the response
|
|
128
|
+
* @param ctx - Turn context
|
|
129
|
+
* @param result - Complete LLM response
|
|
130
|
+
* @param timing - LLM operation duration
|
|
131
|
+
*/
|
|
132
|
+
onLLMEnd?(ctx: TurnContext, result: LLMResult, timing: TimingInfo): void | Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Called when LLM inference fails
|
|
135
|
+
* @param ctx - Turn context
|
|
136
|
+
* @param error - Error that occurred
|
|
137
|
+
*/
|
|
138
|
+
onLLMError?(ctx: TurnContext, error: Error): void | Promise<void>;
|
|
139
|
+
/**
|
|
140
|
+
* Called before TTS synthesis begins for a sentence/text
|
|
141
|
+
* @param ctx - Turn context
|
|
142
|
+
* @param text - Text being synthesized
|
|
143
|
+
*/
|
|
144
|
+
onTTSStart?(ctx: TurnContext, text: string): void | Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Called for each TTS audio chunk during streaming
|
|
147
|
+
* @param ctx - Turn context
|
|
148
|
+
* @param chunk - TTS audio chunk
|
|
149
|
+
* @param chunkIndex - Zero-based index of this chunk
|
|
150
|
+
*/
|
|
151
|
+
onTTSChunk?(ctx: TurnContext, chunk: TTSChunk, chunkIndex: number): void | Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Called when all TTS synthesis completes for the turn
|
|
154
|
+
* @param ctx - Turn context
|
|
155
|
+
* @param timing - Total TTS operation duration
|
|
156
|
+
*/
|
|
157
|
+
onTTSEnd?(ctx: TurnContext, timing: TimingInfo): void | Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Called when TTS synthesis fails
|
|
160
|
+
* @param ctx - Turn context
|
|
161
|
+
* @param error - Error that occurred
|
|
162
|
+
*/
|
|
163
|
+
onTTSError?(ctx: TurnContext, error: Error): void | Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Called before a tool is executed
|
|
166
|
+
* @param ctx - Turn context
|
|
167
|
+
* @param request - Tool call request with name and arguments
|
|
168
|
+
*/
|
|
169
|
+
onToolStart?(ctx: TurnContext, request: ToolCallRequest): void | Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* Called when a tool execution completes
|
|
172
|
+
* Use this hook to log tool results or add guardrails
|
|
173
|
+
* @param ctx - Turn context
|
|
174
|
+
* @param result - Tool execution result
|
|
175
|
+
* @param timing - Tool execution duration
|
|
176
|
+
*/
|
|
177
|
+
onToolEnd?(ctx: TurnContext, result: ToolCallResult, timing: TimingInfo): void | Promise<void>;
|
|
178
|
+
/**
|
|
179
|
+
* Called when a tool execution fails
|
|
180
|
+
* @param ctx - Turn context
|
|
181
|
+
* @param request - The tool call that failed
|
|
182
|
+
* @param error - Error that occurred
|
|
183
|
+
*/
|
|
184
|
+
onToolError?(ctx: TurnContext, request: ToolCallRequest, error: Error): void | Promise<void>;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Hooks for the LLMRTCServer
|
|
188
|
+
*
|
|
189
|
+
* These hooks are called for server-level events like connections,
|
|
190
|
+
* disconnections, and speech detection.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const server = new LLMRTCServer({
|
|
195
|
+
* providers: { llm, stt, tts },
|
|
196
|
+
* hooks: {
|
|
197
|
+
* onConnection(sessionId, connectionId) {
|
|
198
|
+
* console.log(`Client ${connectionId} connected to session ${sessionId}`);
|
|
199
|
+
* },
|
|
200
|
+
* onDisconnect(sessionId, timing) {
|
|
201
|
+
* console.log(`Session ${sessionId} ended after ${timing.durationMs}ms`);
|
|
202
|
+
* },
|
|
203
|
+
* onError(error, context) {
|
|
204
|
+
* reportToSentry(error, context);
|
|
205
|
+
* }
|
|
206
|
+
* }
|
|
207
|
+
* });
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export interface ServerHooks {
|
|
211
|
+
/**
|
|
212
|
+
* Called when a new WebSocket connection is established
|
|
213
|
+
* @param sessionId - Session identifier
|
|
214
|
+
* @param connectionId - Connection identifier (for reconnection tracking)
|
|
215
|
+
*/
|
|
216
|
+
onConnection?(sessionId: string, connectionId: string): void | Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* Called when a WebSocket connection closes
|
|
219
|
+
* @param sessionId - Session identifier
|
|
220
|
+
* @param timing - Session duration from connection to disconnect
|
|
221
|
+
*/
|
|
222
|
+
onDisconnect?(sessionId: string, timing: TimingInfo): void | Promise<void>;
|
|
223
|
+
/**
|
|
224
|
+
* Called when VAD detects speech start
|
|
225
|
+
* @param sessionId - Session identifier
|
|
226
|
+
* @param timestamp - Timestamp when speech started (Date.now())
|
|
227
|
+
*/
|
|
228
|
+
onSpeechStart?(sessionId: string, timestamp: number): void | Promise<void>;
|
|
229
|
+
/**
|
|
230
|
+
* Called when VAD detects speech end
|
|
231
|
+
* @param sessionId - Session identifier
|
|
232
|
+
* @param timestamp - Timestamp when speech ended (Date.now())
|
|
233
|
+
* @param audioDurationMs - Duration of the speech audio in milliseconds
|
|
234
|
+
*/
|
|
235
|
+
onSpeechEnd?(sessionId: string, timestamp: number, audioDurationMs: number): void | Promise<void>;
|
|
236
|
+
/**
|
|
237
|
+
* Called when any error occurs
|
|
238
|
+
* Use this hook for centralized error reporting
|
|
239
|
+
* @param error - The error that occurred
|
|
240
|
+
* @param context - Error context with component, session, turn info
|
|
241
|
+
*/
|
|
242
|
+
onError?(error: Error, context: ErrorContext): void | Promise<void>;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Context for playbook hooks
|
|
246
|
+
*/
|
|
247
|
+
export interface PlaybookContext {
|
|
248
|
+
/** Playbook ID */
|
|
249
|
+
playbookId: string;
|
|
250
|
+
/** Current stage ID */
|
|
251
|
+
stageId: string;
|
|
252
|
+
/** Session ID if available */
|
|
253
|
+
sessionId?: string;
|
|
254
|
+
/** Number of turns in current stage */
|
|
255
|
+
turnCount: number;
|
|
256
|
+
/** Time spent in current stage (ms) */
|
|
257
|
+
timeInStage: number;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Hooks for playbook execution
|
|
261
|
+
*
|
|
262
|
+
* These hooks are called during playbook stage transitions
|
|
263
|
+
* and can be used for logging, analytics, or custom behavior.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* const orchestrator = new PlaybookOrchestrator({
|
|
268
|
+
* llm,
|
|
269
|
+
* playbook,
|
|
270
|
+
* hooks: {
|
|
271
|
+
* onStageEnter(ctx, stage) {
|
|
272
|
+
* console.log(`Entered stage: ${stage.name}`);
|
|
273
|
+
* analytics.track('stage_entered', { stage: stage.id });
|
|
274
|
+
* },
|
|
275
|
+
* onTransition(ctx, transition, from, to) {
|
|
276
|
+
* console.log(`Transitioned: ${from.id} -> ${to.id}`);
|
|
277
|
+
* }
|
|
278
|
+
* }
|
|
279
|
+
* });
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
export interface PlaybookHooks {
|
|
283
|
+
/**
|
|
284
|
+
* Called when entering a new stage
|
|
285
|
+
* @param ctx - Playbook context
|
|
286
|
+
* @param stage - The stage being entered
|
|
287
|
+
* @param previousStage - The previous stage (undefined if initial)
|
|
288
|
+
*/
|
|
289
|
+
onStageEnter?(ctx: PlaybookContext, stage: Stage, previousStage?: Stage): void | Promise<void>;
|
|
290
|
+
/**
|
|
291
|
+
* Called when exiting a stage
|
|
292
|
+
* @param ctx - Playbook context
|
|
293
|
+
* @param stage - The stage being exited
|
|
294
|
+
* @param nextStage - The stage being transitioned to
|
|
295
|
+
* @param timing - Time spent in the exited stage
|
|
296
|
+
*/
|
|
297
|
+
onStageExit?(ctx: PlaybookContext, stage: Stage, nextStage: Stage, timing: TimingInfo): void | Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Called when a stage transition occurs
|
|
300
|
+
* @param ctx - Playbook context
|
|
301
|
+
* @param transition - The transition that was triggered
|
|
302
|
+
* @param from - Source stage
|
|
303
|
+
* @param to - Target stage
|
|
304
|
+
*/
|
|
305
|
+
onTransition?(ctx: PlaybookContext, transition: Transition, from: Stage, to: Stage): void | Promise<void>;
|
|
306
|
+
/**
|
|
307
|
+
* Called when a playbook turn completes
|
|
308
|
+
* @param ctx - Playbook context
|
|
309
|
+
* @param response - The assistant's response
|
|
310
|
+
* @param toolCallCount - Number of tool calls made in this turn
|
|
311
|
+
*/
|
|
312
|
+
onPlaybookTurnEnd?(ctx: PlaybookContext, response: string, toolCallCount: number): void | Promise<void>;
|
|
313
|
+
/**
|
|
314
|
+
* Called when playbook execution completes
|
|
315
|
+
* @param ctx - Playbook context
|
|
316
|
+
* @param finalStage - The stage where execution ended
|
|
317
|
+
* @param totalTurns - Total turns across all stages
|
|
318
|
+
*/
|
|
319
|
+
onPlaybookComplete?(ctx: PlaybookContext, finalStage: Stage, totalTurns: number): void | Promise<void>;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Combined hooks interface for convenience
|
|
323
|
+
* Can be used when you want a single hooks object for server, orchestrator, and playbook
|
|
324
|
+
*/
|
|
325
|
+
export interface CombinedHooks extends OrchestratorHooks, ServerHooks, PlaybookHooks {
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Create timing info from start and end timestamps
|
|
329
|
+
*/
|
|
330
|
+
export declare function createTimingInfo(startTime: number, endTime: number): TimingInfo;
|
|
331
|
+
/**
|
|
332
|
+
* Create an error context object
|
|
333
|
+
*/
|
|
334
|
+
export declare function createErrorContext(code: ErrorCode, component: ErrorContext['component'], options?: {
|
|
335
|
+
sessionId?: string;
|
|
336
|
+
turnId?: string;
|
|
337
|
+
details?: Record<string, unknown>;
|
|
338
|
+
}): ErrorContext;
|
|
339
|
+
/**
|
|
340
|
+
* Safely call an async hook without blocking
|
|
341
|
+
* Errors are caught and logged but don't propagate
|
|
342
|
+
* @param hook - The hook function to call
|
|
343
|
+
* @param args - Arguments to pass to the hook
|
|
344
|
+
*/
|
|
345
|
+
export declare function callHookSafe<T extends unknown[]>(hook: ((...args: T) => void | Promise<void>) | undefined, ...args: T): Promise<void>;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook interfaces for observability and extensibility
|
|
3
|
+
*
|
|
4
|
+
* These hooks allow integrators to:
|
|
5
|
+
* - Capture timing/latency metrics for STT, LLM, and TTS operations
|
|
6
|
+
* - Plug in custom logging or metrics reporters
|
|
7
|
+
* - Add guardrails, validation, or routing logic without forking
|
|
8
|
+
*/
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// Utility Functions
|
|
11
|
+
// =============================================================================
|
|
12
|
+
/**
|
|
13
|
+
* Create timing info from start and end timestamps
|
|
14
|
+
*/
|
|
15
|
+
export function createTimingInfo(startTime, endTime) {
|
|
16
|
+
return {
|
|
17
|
+
startTime,
|
|
18
|
+
endTime,
|
|
19
|
+
durationMs: endTime - startTime
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create an error context object
|
|
24
|
+
*/
|
|
25
|
+
export function createErrorContext(code, component, options) {
|
|
26
|
+
return {
|
|
27
|
+
code,
|
|
28
|
+
component,
|
|
29
|
+
sessionId: options?.sessionId,
|
|
30
|
+
turnId: options?.turnId,
|
|
31
|
+
timestamp: Date.now(),
|
|
32
|
+
details: options?.details
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Safely call an async hook without blocking
|
|
37
|
+
* Errors are caught and logged but don't propagate
|
|
38
|
+
* @param hook - The hook function to call
|
|
39
|
+
* @param args - Arguments to pass to the hook
|
|
40
|
+
*/
|
|
41
|
+
export async function callHookSafe(hook, ...args) {
|
|
42
|
+
if (!hook)
|
|
43
|
+
return;
|
|
44
|
+
try {
|
|
45
|
+
await hook(...args);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
// Log but don't propagate hook errors
|
|
49
|
+
console.error('[hooks] Hook error:', error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA6YH,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAAiB,EAAE,OAAe;IACjE,OAAO;QACL,SAAS;QACT,OAAO;QACP,UAAU,EAAE,OAAO,GAAG,SAAS;KAChC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAe,EACf,SAAoC,EACpC,OAIC;IAED,OAAO;QACL,IAAI;QACJ,SAAS;QACT,SAAS,EAAE,OAAO,EAAE,SAAS;QAC7B,MAAM,EAAE,OAAO,EAAE,MAAM;QACvB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;QACrB,OAAO,EAAE,OAAO,EAAE,OAAO;KAC1B,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,IAAwD,EACxD,GAAG,IAAO;IAEV,IAAI,CAAC,IAAI;QAAE,OAAO;IAClB,IAAI,CAAC;QACH,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;IACtB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,sCAAsC;QACtC,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './tools.js';
|
|
3
|
+
export * from './tool-executor.js';
|
|
4
|
+
export * from './playbook.js';
|
|
5
|
+
export * from './playbook-engine.js';
|
|
6
|
+
export * from './playbook-orchestrator.js';
|
|
7
|
+
export * from './orchestrator.js';
|
|
8
|
+
export * from './protocol.js';
|
|
9
|
+
export * from './hooks.js';
|
|
10
|
+
export * from './metrics.js';
|
|
11
|
+
export * from './logging-hooks.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './tools.js';
|
|
3
|
+
export * from './tool-executor.js';
|
|
4
|
+
export * from './playbook.js';
|
|
5
|
+
export * from './playbook-engine.js';
|
|
6
|
+
export * from './playbook-orchestrator.js';
|
|
7
|
+
export * from './orchestrator.js';
|
|
8
|
+
export * from './protocol.js';
|
|
9
|
+
export * from './hooks.js';
|
|
10
|
+
export * from './metrics.js';
|
|
11
|
+
export * from './logging-hooks.js';
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC"}
|