@framers/agentos-ext-voice-plivo 0.1.0 → 0.1.1
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 +98 -0
- package/dist/PlivoVoiceProvider.d.ts +86 -0
- package/dist/PlivoVoiceProvider.d.ts.map +1 -0
- package/dist/PlivoVoiceProvider.js +306 -0
- package/dist/PlivoVoiceProvider.js.map +1 -0
- package/dist/PlivoVoiceService.d.ts +118 -0
- package/dist/PlivoVoiceService.d.ts.map +1 -0
- package/dist/PlivoVoiceService.js +228 -0
- package/dist/PlivoVoiceService.js.map +1 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +151 -0
- package/dist/index.js.map +1 -0
- package/dist/tools/voiceCall.d.ts +113 -0
- package/dist/tools/voiceCall.d.ts.map +1 -0
- package/dist/tools/voiceCall.js +205 -0
- package/dist/tools/voiceCall.js.map +1 -0
- package/manifest.json +1 -1
- package/package.json +37 -14
package/LICENSE
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
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, and distribution as defined by Sections 1 through 9 of this document.
|
|
10
|
+
|
|
11
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
12
|
+
|
|
13
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
14
|
+
|
|
15
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
16
|
+
|
|
17
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
18
|
+
|
|
19
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
20
|
+
|
|
21
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
22
|
+
|
|
23
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
24
|
+
|
|
25
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
26
|
+
|
|
27
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
28
|
+
|
|
29
|
+
2. Grant of Copyright License.
|
|
30
|
+
|
|
31
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
+
|
|
33
|
+
3. Grant of Patent License.
|
|
34
|
+
|
|
35
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
36
|
+
|
|
37
|
+
4. Redistribution.
|
|
38
|
+
|
|
39
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
40
|
+
|
|
41
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
42
|
+
|
|
43
|
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
44
|
+
|
|
45
|
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
46
|
+
|
|
47
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
48
|
+
|
|
49
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
50
|
+
|
|
51
|
+
5. Submission of Contributions.
|
|
52
|
+
|
|
53
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
54
|
+
|
|
55
|
+
6. Trademarks.
|
|
56
|
+
|
|
57
|
+
This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
58
|
+
|
|
59
|
+
7. Disclaimer of Warranty.
|
|
60
|
+
|
|
61
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
62
|
+
|
|
63
|
+
8. Limitation of Liability.
|
|
64
|
+
|
|
65
|
+
In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
66
|
+
|
|
67
|
+
9. Accepting Warranty or Additional Liability.
|
|
68
|
+
|
|
69
|
+
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
70
|
+
|
|
71
|
+
END OF TERMS AND CONDITIONS
|
|
72
|
+
|
|
73
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
74
|
+
|
|
75
|
+
To apply the Apache License to your work, attach the following
|
|
76
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
77
|
+
replaced with your own identifying information. (Don't include
|
|
78
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
79
|
+
comment syntax for the file format. We also recommend that a
|
|
80
|
+
file or class name and description of purpose be included on the
|
|
81
|
+
same "printed page" as the copyright notice for easier
|
|
82
|
+
identification within third-party archives.
|
|
83
|
+
|
|
84
|
+
Copyright (c) 2025 Framers
|
|
85
|
+
|
|
86
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
87
|
+
you may not use this file except in compliance with the License.
|
|
88
|
+
You may obtain a copy of the License at
|
|
89
|
+
|
|
90
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
91
|
+
|
|
92
|
+
Unless required by applicable law or agreed to in writing, software
|
|
93
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
94
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
95
|
+
See the License for the specific language governing permissions and
|
|
96
|
+
limitations under the License.
|
|
97
|
+
|
|
98
|
+
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview IVoiceCallProvider implementation for Plivo.
|
|
3
|
+
*
|
|
4
|
+
* Normalizes Plivo's webhook payloads (form-encoded, like Twilio) into the
|
|
5
|
+
* {@link NormalizedCallEvent} discriminated union consumed by {@link CallManager}.
|
|
6
|
+
*
|
|
7
|
+
* Plivo call statuses and hangup causes are mapped to the common event types:
|
|
8
|
+
* - CallStatus 'ringing' -> 'call-ringing'
|
|
9
|
+
* - CallStatus 'in-progress' / 'answered' -> 'call-answered'
|
|
10
|
+
* - CallStatus 'completed' -> 'call-completed'
|
|
11
|
+
* - CallStatus 'failed' -> 'call-failed'
|
|
12
|
+
* - CallStatus 'busy' -> 'call-busy'
|
|
13
|
+
* - CallStatus 'no-answer' -> 'call-no-answer'
|
|
14
|
+
* - CallStatus 'cancel' -> 'call-hangup-user'
|
|
15
|
+
* - HangupCause maps to appropriate terminal state
|
|
16
|
+
*
|
|
17
|
+
* @module @framers/agentos-ext-voice-plivo/PlivoVoiceProvider
|
|
18
|
+
*/
|
|
19
|
+
import type { IVoiceCallProvider, InitiateCallInput, InitiateCallResult, HangupCallInput, PlayTtsInput } from '@framers/agentos';
|
|
20
|
+
import type { VoiceProviderName, WebhookContext, WebhookParseResult, WebhookVerificationResult } from '@framers/agentos';
|
|
21
|
+
import { PlivoVoiceService } from './PlivoVoiceService';
|
|
22
|
+
export declare class PlivoVoiceProvider implements IVoiceCallProvider {
|
|
23
|
+
private readonly service;
|
|
24
|
+
readonly name: VoiceProviderName;
|
|
25
|
+
constructor(service: PlivoVoiceService);
|
|
26
|
+
/**
|
|
27
|
+
* Verify the authenticity of a Plivo webhook using V3 signature (HMAC-SHA256).
|
|
28
|
+
*
|
|
29
|
+
* Plivo sends:
|
|
30
|
+
* - `X-Plivo-Signature-V3`: Base64-encoded HMAC-SHA256 signature
|
|
31
|
+
* - `X-Plivo-Signature-V3-Nonce`: Random nonce used in signature computation
|
|
32
|
+
*
|
|
33
|
+
* The signature is computed as: HMAC-SHA256(authToken, requestUrl + nonce).
|
|
34
|
+
*/
|
|
35
|
+
verifyWebhook(ctx: WebhookContext): WebhookVerificationResult;
|
|
36
|
+
/**
|
|
37
|
+
* Parse a Plivo webhook payload into normalized call events.
|
|
38
|
+
*
|
|
39
|
+
* Plivo sends form-encoded POST bodies with fields like CallUUID, CallStatus,
|
|
40
|
+
* HangupCause, From, To, Direction, Duration, Speech, Digits, etc.
|
|
41
|
+
*/
|
|
42
|
+
parseWebhookEvent(ctx: WebhookContext): WebhookParseResult;
|
|
43
|
+
/**
|
|
44
|
+
* Initiate an outbound call via Plivo.
|
|
45
|
+
*
|
|
46
|
+
* For 'notify' mode: generates Plivo XML with <Speak> + <Hangup/> and hosts
|
|
47
|
+
* it at the answer URL.
|
|
48
|
+
*
|
|
49
|
+
* For 'conversation' mode: generates Plivo XML with <Speak> + <GetInput> +
|
|
50
|
+
* <Redirect> loop and hosts it at the answer URL.
|
|
51
|
+
*
|
|
52
|
+
* In both cases, Plivo fetches the answer URL when the callee picks up.
|
|
53
|
+
* The answer URL should be a publicly accessible endpoint that returns
|
|
54
|
+
* the pre-generated XML — this is handled by the webhook router in the
|
|
55
|
+
* hosting application.
|
|
56
|
+
*/
|
|
57
|
+
initiateCall(input: InitiateCallInput): Promise<InitiateCallResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Hang up an active call by its Plivo call UUID.
|
|
60
|
+
*/
|
|
61
|
+
hangupCall(input: HangupCallInput): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Play TTS speech into an active call via Plivo's Speak API.
|
|
64
|
+
*/
|
|
65
|
+
playTts(input: PlayTtsInput): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Extract a header value from the headers map (case-insensitive).
|
|
68
|
+
*/
|
|
69
|
+
private getHeader;
|
|
70
|
+
/**
|
|
71
|
+
* Parse the webhook body from form-encoded or JSON format.
|
|
72
|
+
* Plivo sends form-encoded data by default (like Twilio).
|
|
73
|
+
*/
|
|
74
|
+
private parseBody;
|
|
75
|
+
/**
|
|
76
|
+
* Get the auth token from the service config.
|
|
77
|
+
* Used for signature verification.
|
|
78
|
+
*/
|
|
79
|
+
private getAuthToken;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Factory to create a PlivoVoiceProvider with the auth token accessible
|
|
83
|
+
* for webhook verification.
|
|
84
|
+
*/
|
|
85
|
+
export declare function createPlivoVoiceProvider(service: PlivoVoiceService, authToken: string): PlivoVoiceProvider;
|
|
86
|
+
//# sourceMappingURL=PlivoVoiceProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlivoVoiceProvider.d.ts","sour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overview IVoiceCallProvider implementation for Plivo.
|
|
5
|
+
*
|
|
6
|
+
* Normalizes Plivo's webhook payloads (form-encoded, like Twilio) into the
|
|
7
|
+
* {@link NormalizedCallEvent} discriminated union consumed by {@link CallManager}.
|
|
8
|
+
*
|
|
9
|
+
* Plivo call statuses and hangup causes are mapped to the common event types:
|
|
10
|
+
* - CallStatus 'ringing' -> 'call-ringing'
|
|
11
|
+
* - CallStatus 'in-progress' / 'answered' -> 'call-answered'
|
|
12
|
+
* - CallStatus 'completed' -> 'call-completed'
|
|
13
|
+
* - CallStatus 'failed' -> 'call-failed'
|
|
14
|
+
* - CallStatus 'busy' -> 'call-busy'
|
|
15
|
+
* - CallStatus 'no-answer' -> 'call-no-answer'
|
|
16
|
+
* - CallStatus 'cancel' -> 'call-hangup-user'
|
|
17
|
+
* - HangupCause maps to appropriate terminal state
|
|
18
|
+
*
|
|
19
|
+
* @module @framers/agentos-ext-voice-plivo/PlivoVoiceProvider
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.PlivoVoiceProvider = void 0;
|
|
23
|
+
exports.createPlivoVoiceProvider = createPlivoVoiceProvider;
|
|
24
|
+
const node_crypto_1 = require("node:crypto");
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Status Mapping
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/**
|
|
29
|
+
* Map Plivo CallStatus values to NormalizedCallEvent kinds.
|
|
30
|
+
*/
|
|
31
|
+
function mapCallStatus(status, hangupCause) {
|
|
32
|
+
switch (status.toLowerCase()) {
|
|
33
|
+
case 'ringing':
|
|
34
|
+
case 'early':
|
|
35
|
+
return 'call-ringing';
|
|
36
|
+
case 'in-progress':
|
|
37
|
+
case 'answered':
|
|
38
|
+
return 'call-answered';
|
|
39
|
+
case 'completed':
|
|
40
|
+
case 'hangup':
|
|
41
|
+
// Inspect HangupCause for more specific mapping
|
|
42
|
+
return mapHangupCause(hangupCause) ?? 'call-completed';
|
|
43
|
+
case 'failed':
|
|
44
|
+
return 'call-failed';
|
|
45
|
+
case 'busy':
|
|
46
|
+
return 'call-busy';
|
|
47
|
+
case 'no-answer':
|
|
48
|
+
case 'timeout':
|
|
49
|
+
return 'call-no-answer';
|
|
50
|
+
case 'cancel':
|
|
51
|
+
case 'canceled':
|
|
52
|
+
return 'call-hangup-user';
|
|
53
|
+
default:
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Map Plivo HangupCause values to more specific event kinds.
|
|
59
|
+
* Plivo uses SIP-style hangup cause strings.
|
|
60
|
+
*
|
|
61
|
+
* @see https://www.plivo.com/docs/voice/api/call/#hangup-causes
|
|
62
|
+
*/
|
|
63
|
+
function mapHangupCause(cause) {
|
|
64
|
+
if (!cause)
|
|
65
|
+
return null;
|
|
66
|
+
switch (cause.toUpperCase()) {
|
|
67
|
+
case 'NORMAL_CLEARING':
|
|
68
|
+
case 'NORMAL_CALL_CLEARING':
|
|
69
|
+
return 'call-completed';
|
|
70
|
+
case 'ORIGINATOR_CANCEL':
|
|
71
|
+
case 'NORMAL_UNSPECIFIED':
|
|
72
|
+
return 'call-hangup-user';
|
|
73
|
+
case 'USER_BUSY':
|
|
74
|
+
return 'call-busy';
|
|
75
|
+
case 'NO_ANSWER':
|
|
76
|
+
case 'NO_USER_RESPONSE':
|
|
77
|
+
case 'ALLOTTED_TIMEOUT':
|
|
78
|
+
return 'call-no-answer';
|
|
79
|
+
case 'CALL_REJECTED':
|
|
80
|
+
case 'UNALLOCATED_NUMBER':
|
|
81
|
+
case 'NUMBER_CHANGED':
|
|
82
|
+
case 'INVALID_NUMBER_FORMAT':
|
|
83
|
+
return 'call-failed';
|
|
84
|
+
case 'DESTINATION_OUT_OF_ORDER':
|
|
85
|
+
case 'NETWORK_OUT_OF_ORDER':
|
|
86
|
+
case 'SERVICE_UNAVAILABLE':
|
|
87
|
+
case 'RECOVERY_ON_TIMER_EXPIRE':
|
|
88
|
+
return 'call-error';
|
|
89
|
+
default:
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// ============================================================================
|
|
94
|
+
// Provider Implementation
|
|
95
|
+
// ============================================================================
|
|
96
|
+
class PlivoVoiceProvider {
|
|
97
|
+
constructor(service) {
|
|
98
|
+
this.service = service;
|
|
99
|
+
this.name = 'plivo';
|
|
100
|
+
}
|
|
101
|
+
// ── Webhook Processing ──
|
|
102
|
+
/**
|
|
103
|
+
* Verify the authenticity of a Plivo webhook using V3 signature (HMAC-SHA256).
|
|
104
|
+
*
|
|
105
|
+
* Plivo sends:
|
|
106
|
+
* - `X-Plivo-Signature-V3`: Base64-encoded HMAC-SHA256 signature
|
|
107
|
+
* - `X-Plivo-Signature-V3-Nonce`: Random nonce used in signature computation
|
|
108
|
+
*
|
|
109
|
+
* The signature is computed as: HMAC-SHA256(authToken, requestUrl + nonce).
|
|
110
|
+
*/
|
|
111
|
+
verifyWebhook(ctx) {
|
|
112
|
+
const signature = this.getHeader(ctx.headers, 'x-plivo-signature-v3');
|
|
113
|
+
const nonce = this.getHeader(ctx.headers, 'x-plivo-signature-v3-nonce');
|
|
114
|
+
if (!signature || !nonce) {
|
|
115
|
+
// Plivo also supports V2 (legacy) signatures — for V3-only enforcement,
|
|
116
|
+
// missing headers indicate an unsigned or tampered request.
|
|
117
|
+
// In development, allow unsigned webhooks to simplify testing.
|
|
118
|
+
if (process.env.NODE_ENV === 'development' || process.env.PLIVO_SKIP_SIGNATURE === 'true') {
|
|
119
|
+
return { valid: true };
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
valid: false,
|
|
123
|
+
error: 'Missing X-Plivo-Signature-V3 or X-Plivo-Signature-V3-Nonce header',
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const valid = this.service.verifySignature(ctx.url, nonce, signature, this.getAuthToken());
|
|
127
|
+
return valid
|
|
128
|
+
? { valid: true }
|
|
129
|
+
: { valid: false, error: 'Plivo HMAC-SHA256 signature mismatch' };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Parse a Plivo webhook payload into normalized call events.
|
|
133
|
+
*
|
|
134
|
+
* Plivo sends form-encoded POST bodies with fields like CallUUID, CallStatus,
|
|
135
|
+
* HangupCause, From, To, Direction, Duration, Speech, Digits, etc.
|
|
136
|
+
*/
|
|
137
|
+
parseWebhookEvent(ctx) {
|
|
138
|
+
const body = this.parseBody(ctx);
|
|
139
|
+
const events = [];
|
|
140
|
+
const callUuid = body.CallUUID;
|
|
141
|
+
if (!callUuid) {
|
|
142
|
+
return { events, rawData: body };
|
|
143
|
+
}
|
|
144
|
+
const timestamp = Date.now();
|
|
145
|
+
// Handle speech recognition results from GetInput
|
|
146
|
+
if (body.Speech) {
|
|
147
|
+
events.push({
|
|
148
|
+
kind: 'transcript',
|
|
149
|
+
eventId: `plivo-speech-${callUuid}-${timestamp}`,
|
|
150
|
+
providerCallId: callUuid,
|
|
151
|
+
timestamp,
|
|
152
|
+
text: body.Speech,
|
|
153
|
+
isFinal: true,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
// Handle call status events
|
|
157
|
+
if (body.CallStatus) {
|
|
158
|
+
const kind = mapCallStatus(body.CallStatus, body.HangupCause);
|
|
159
|
+
if (kind) {
|
|
160
|
+
const eventId = `plivo-${kind}-${callUuid}-${(0, node_crypto_1.randomUUID)().slice(0, 8)}`;
|
|
161
|
+
const baseEvent = { eventId, providerCallId: callUuid, timestamp };
|
|
162
|
+
switch (kind) {
|
|
163
|
+
case 'call-completed': {
|
|
164
|
+
const duration = body.Duration ? parseInt(body.Duration, 10) : undefined;
|
|
165
|
+
events.push({ ...baseEvent, kind: 'call-completed', duration });
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case 'call-failed':
|
|
169
|
+
events.push({
|
|
170
|
+
...baseEvent,
|
|
171
|
+
kind: 'call-failed',
|
|
172
|
+
reason: body.HangupCause ?? 'Unknown failure',
|
|
173
|
+
});
|
|
174
|
+
break;
|
|
175
|
+
case 'call-error':
|
|
176
|
+
events.push({
|
|
177
|
+
...baseEvent,
|
|
178
|
+
kind: 'call-error',
|
|
179
|
+
error: body.HangupCause ?? 'Unknown error',
|
|
180
|
+
});
|
|
181
|
+
break;
|
|
182
|
+
default:
|
|
183
|
+
// Simple event kinds with no extra fields
|
|
184
|
+
events.push({ ...baseEvent, kind });
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return { events, rawData: body };
|
|
190
|
+
}
|
|
191
|
+
// ── Call Control ──
|
|
192
|
+
/**
|
|
193
|
+
* Initiate an outbound call via Plivo.
|
|
194
|
+
*
|
|
195
|
+
* For 'notify' mode: generates Plivo XML with <Speak> + <Hangup/> and hosts
|
|
196
|
+
* it at the answer URL.
|
|
197
|
+
*
|
|
198
|
+
* For 'conversation' mode: generates Plivo XML with <Speak> + <GetInput> +
|
|
199
|
+
* <Redirect> loop and hosts it at the answer URL.
|
|
200
|
+
*
|
|
201
|
+
* In both cases, Plivo fetches the answer URL when the callee picks up.
|
|
202
|
+
* The answer URL should be a publicly accessible endpoint that returns
|
|
203
|
+
* the pre-generated XML — this is handled by the webhook router in the
|
|
204
|
+
* hosting application.
|
|
205
|
+
*/
|
|
206
|
+
async initiateCall(input) {
|
|
207
|
+
try {
|
|
208
|
+
// Generate the appropriate XML response for the answer URL
|
|
209
|
+
let _answerXml;
|
|
210
|
+
if (input.mode === 'notify') {
|
|
211
|
+
_answerXml = this.service.generateNotifyXml(input.message ?? 'Hello, this is an automated message.', input.notifyVoice);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
_answerXml = this.service.generateConversationXml(input.webhookUrl, input.message, input.notifyVoice);
|
|
215
|
+
}
|
|
216
|
+
// Store the XML so the webhook endpoint can serve it.
|
|
217
|
+
// The answer URL is the webhook URL; when Plivo GETs/POSTs it,
|
|
218
|
+
// the application should return this XML.
|
|
219
|
+
// The callId is passed as a query parameter for routing.
|
|
220
|
+
const answerUrl = `${input.webhookUrl}?callId=${encodeURIComponent(input.callId)}&mode=${input.mode}`;
|
|
221
|
+
const response = await this.service.createCall(input.fromNumber, input.toNumber, answerUrl, 'POST');
|
|
222
|
+
return {
|
|
223
|
+
providerCallId: response.requestUuid,
|
|
224
|
+
success: true,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
return {
|
|
229
|
+
providerCallId: '',
|
|
230
|
+
success: false,
|
|
231
|
+
error: err instanceof Error ? err.message : String(err),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Hang up an active call by its Plivo call UUID.
|
|
237
|
+
*/
|
|
238
|
+
async hangupCall(input) {
|
|
239
|
+
await this.service.hangupCall(input.providerCallId);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Play TTS speech into an active call via Plivo's Speak API.
|
|
243
|
+
*/
|
|
244
|
+
async playTts(input) {
|
|
245
|
+
await this.service.speakText(input.providerCallId, input.text, input.voice);
|
|
246
|
+
}
|
|
247
|
+
// ── Internal Helpers ──
|
|
248
|
+
/**
|
|
249
|
+
* Extract a header value from the headers map (case-insensitive).
|
|
250
|
+
*/
|
|
251
|
+
getHeader(headers, name) {
|
|
252
|
+
// Headers may be stored with varying casing
|
|
253
|
+
const key = Object.keys(headers).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
254
|
+
if (!key)
|
|
255
|
+
return undefined;
|
|
256
|
+
const value = headers[key];
|
|
257
|
+
return Array.isArray(value) ? value[0] : value;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Parse the webhook body from form-encoded or JSON format.
|
|
261
|
+
* Plivo sends form-encoded data by default (like Twilio).
|
|
262
|
+
*/
|
|
263
|
+
parseBody(ctx) {
|
|
264
|
+
// Prefer pre-parsed body if available
|
|
265
|
+
if (ctx.parsedBody) {
|
|
266
|
+
return ctx.parsedBody;
|
|
267
|
+
}
|
|
268
|
+
const bodyStr = typeof ctx.body === 'string' ? ctx.body : ctx.body.toString('utf-8');
|
|
269
|
+
// Try JSON first
|
|
270
|
+
try {
|
|
271
|
+
return JSON.parse(bodyStr);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// Fall through to form-encoded parsing
|
|
275
|
+
}
|
|
276
|
+
// Parse application/x-www-form-urlencoded
|
|
277
|
+
const params = new URLSearchParams(bodyStr);
|
|
278
|
+
const result = {};
|
|
279
|
+
for (const [key, value] of params.entries()) {
|
|
280
|
+
result[key] = value;
|
|
281
|
+
}
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Get the auth token from the service config.
|
|
286
|
+
* Used for signature verification.
|
|
287
|
+
*/
|
|
288
|
+
getAuthToken() {
|
|
289
|
+
// Access the service's config through a known property.
|
|
290
|
+
// The service exposes verifySignature which needs the token,
|
|
291
|
+
// but we also need it here for the provider-level verify.
|
|
292
|
+
// We store it during construction via the factory.
|
|
293
|
+
return this._authToken ?? '';
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
exports.PlivoVoiceProvider = PlivoVoiceProvider;
|
|
297
|
+
/**
|
|
298
|
+
* Factory to create a PlivoVoiceProvider with the auth token accessible
|
|
299
|
+
* for webhook verification.
|
|
300
|
+
*/
|
|
301
|
+
function createPlivoVoiceProvider(service, authToken) {
|
|
302
|
+
const provider = new PlivoVoiceProvider(service);
|
|
303
|
+
provider._authToken = authToken;
|
|
304
|
+
return provider;
|
|
305
|
+
}
|
|
306
|
+
//# sourceMappingURL=PlivoVoiceProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlivoVoiceProvider.js","sourceRoot":"","sources":["../src/PlivoVoiceProvider.ts"],"names":[],"mappings":";AAAA,cAAc;AACd;;;;;;;;;;;;;;;;;GAiBG;;;AA+XH,4DAOC;AApYD,6CAAyC;AAyCzC,+EAA+E;AAC/E,i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IAEjB,MAAM,QAAQ,GAAG,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAChD,QAAgB,CAAC,UAAU,GAAG,SAAS,CAAC;IACzC,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Wrapper around the Plivo Voice API SDK.
|
|
3
|
+
*
|
|
4
|
+
* Handles Plivo client lifecycle, call control operations (create, hangup,
|
|
5
|
+
* speak), Plivo XML generation for notify/conversation modes, and webhook
|
|
6
|
+
* signature verification.
|
|
7
|
+
*
|
|
8
|
+
* @module @framers/agentos-ext-voice-plivo/PlivoVoiceService
|
|
9
|
+
*/
|
|
10
|
+
export interface PlivoServiceConfig {
|
|
11
|
+
/** Plivo Auth ID. */
|
|
12
|
+
authId: string;
|
|
13
|
+
/** Plivo Auth Token. */
|
|
14
|
+
authToken: string;
|
|
15
|
+
/** Default outbound caller ID (E.164). */
|
|
16
|
+
fromNumber: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class PlivoVoiceService {
|
|
19
|
+
private client;
|
|
20
|
+
private readonly config;
|
|
21
|
+
constructor(config: PlivoServiceConfig);
|
|
22
|
+
/**
|
|
23
|
+
* Initialize the Plivo SDK client via dynamic import.
|
|
24
|
+
* Plivo's Node SDK is CommonJS; dynamic import avoids bundling issues.
|
|
25
|
+
*/
|
|
26
|
+
initialize(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Gracefully shut down the service and release resources.
|
|
29
|
+
*/
|
|
30
|
+
shutdown(): Promise<void>;
|
|
31
|
+
/** Whether the service has been initialized. */
|
|
32
|
+
get isInitialized(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Create an outbound call via the Plivo Calls API.
|
|
35
|
+
*
|
|
36
|
+
* @param from - E.164 caller ID.
|
|
37
|
+
* @param to - E.164 destination number.
|
|
38
|
+
* @param answerUrl - URL Plivo fetches when the call is answered.
|
|
39
|
+
* @param answerMethod - HTTP method for the answer URL (GET or POST).
|
|
40
|
+
* @returns The Plivo API response containing `requestUuid` (call UUID).
|
|
41
|
+
*/
|
|
42
|
+
createCall(from: string, to: string, answerUrl: string, answerMethod?: 'GET' | 'POST'): Promise<{
|
|
43
|
+
requestUuid: string;
|
|
44
|
+
apiId: string;
|
|
45
|
+
message: string;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Hang up an active call by its UUID.
|
|
49
|
+
*
|
|
50
|
+
* @param callUuid - The Plivo call UUID to terminate.
|
|
51
|
+
*/
|
|
52
|
+
hangupCall(callUuid: string): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Play TTS speech into an active call via the Plivo Speak API.
|
|
55
|
+
*
|
|
56
|
+
* @param callUuid - The Plivo call UUID.
|
|
57
|
+
* @param text - Text to speak.
|
|
58
|
+
* @param voice - Plivo voice name (default: Polly.Joanna).
|
|
59
|
+
*/
|
|
60
|
+
speakText(callUuid: string, text: string, voice?: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Generate Plivo XML for "notify" mode: speak a message then hang up.
|
|
63
|
+
*
|
|
64
|
+
* @param message - The message text to speak.
|
|
65
|
+
* @param voice - Plivo TTS voice (default: Polly.Joanna).
|
|
66
|
+
* @returns Plivo XML string.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```xml
|
|
70
|
+
* <?xml version="1.0" encoding="UTF-8"?>
|
|
71
|
+
* <Response>
|
|
72
|
+
* <Speak voice="Polly.Joanna">Hello, your appointment is confirmed.</Speak>
|
|
73
|
+
* <Hangup/>
|
|
74
|
+
* </Response>
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
generateNotifyXml(message: string, voice?: string): string;
|
|
78
|
+
/**
|
|
79
|
+
* Generate Plivo XML for "conversation" mode: speak a greeting,
|
|
80
|
+
* gather DTMF/speech input, then redirect back to the webhook URL
|
|
81
|
+
* for a multi-turn loop.
|
|
82
|
+
*
|
|
83
|
+
* @param webhookUrl - URL to redirect to after gathering input.
|
|
84
|
+
* @param greeting - Optional greeting to speak before gathering.
|
|
85
|
+
* @param voice - Plivo TTS voice (default: Polly.Joanna).
|
|
86
|
+
* @returns Plivo XML string.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```xml
|
|
90
|
+
* <?xml version="1.0" encoding="UTF-8"?>
|
|
91
|
+
* <Response>
|
|
92
|
+
* <Speak voice="Polly.Joanna">Hello, how can I help you?</Speak>
|
|
93
|
+
* <GetInput action="https://example.com/voice/webhook/plivo"
|
|
94
|
+
* method="POST" inputType="speech" executionTimeout="30">
|
|
95
|
+
* <Speak voice="Polly.Joanna">I am listening.</Speak>
|
|
96
|
+
* </GetInput>
|
|
97
|
+
* <Redirect method="POST">https://example.com/voice/webhook/plivo</Redirect>
|
|
98
|
+
* </Response>
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
generateConversationXml(webhookUrl: string, greeting?: string, voice?: string): string;
|
|
102
|
+
/**
|
|
103
|
+
* Verify a Plivo webhook signature (V3 — HMAC-SHA256).
|
|
104
|
+
*
|
|
105
|
+
* Plivo V3 signatures are computed as:
|
|
106
|
+
* HMAC-SHA256(authToken, requestUrl + nonce)
|
|
107
|
+
* and sent via the `X-Plivo-Signature-V3` header.
|
|
108
|
+
*
|
|
109
|
+
* @param url - The full request URL (including query string).
|
|
110
|
+
* @param nonce - The nonce from `X-Plivo-Signature-V3-Nonce` header.
|
|
111
|
+
* @param signature - The signature from `X-Plivo-Signature-V3` header.
|
|
112
|
+
* @param authToken - The Plivo auth token used as the HMAC key.
|
|
113
|
+
* @returns Whether the signature is valid.
|
|
114
|
+
*/
|
|
115
|
+
verifySignature(url: string, nonce: string, signature: string, authToken: string): boolean;
|
|
116
|
+
private ensureInitialized;
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=PlivoVoiceService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlivoVoiceService.d.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