@clawvoice/voice-assistant 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.
Files changed (50) hide show
  1. package/.env.example +125 -0
  2. package/CHANGELOG.md +112 -0
  3. package/LICENSE +21 -0
  4. package/README.md +215 -0
  5. package/dist/cli.d.ts +10 -0
  6. package/dist/cli.js +272 -0
  7. package/dist/config.d.ts +42 -0
  8. package/dist/config.js +182 -0
  9. package/dist/diagnostics/health.d.ts +14 -0
  10. package/dist/diagnostics/health.js +182 -0
  11. package/dist/hooks.d.ts +16 -0
  12. package/dist/hooks.js +113 -0
  13. package/dist/inbound/classifier.d.ts +5 -0
  14. package/dist/inbound/classifier.js +72 -0
  15. package/dist/inbound/types.d.ts +30 -0
  16. package/dist/inbound/types.js +2 -0
  17. package/dist/index.d.ts +5 -0
  18. package/dist/index.js +52 -0
  19. package/dist/routes.d.ts +6 -0
  20. package/dist/routes.js +89 -0
  21. package/dist/services/memory-extraction.d.ts +42 -0
  22. package/dist/services/memory-extraction.js +117 -0
  23. package/dist/services/post-call.d.ts +56 -0
  24. package/dist/services/post-call.js +112 -0
  25. package/dist/services/relay.d.ts +9 -0
  26. package/dist/services/relay.js +19 -0
  27. package/dist/services/voice-call.d.ts +61 -0
  28. package/dist/services/voice-call.js +189 -0
  29. package/dist/telephony/telnyx.d.ts +12 -0
  30. package/dist/telephony/telnyx.js +60 -0
  31. package/dist/telephony/twilio.d.ts +12 -0
  32. package/dist/telephony/twilio.js +63 -0
  33. package/dist/telephony/types.d.ts +15 -0
  34. package/dist/telephony/types.js +2 -0
  35. package/dist/telephony/util.d.ts +2 -0
  36. package/dist/telephony/util.js +25 -0
  37. package/dist/tools.d.ts +5 -0
  38. package/dist/tools.js +167 -0
  39. package/dist/voice/bridge.d.ts +47 -0
  40. package/dist/voice/bridge.js +411 -0
  41. package/dist/voice/types.d.ts +168 -0
  42. package/dist/voice/types.js +42 -0
  43. package/dist/webhooks/verify.d.ts +30 -0
  44. package/dist/webhooks/verify.js +95 -0
  45. package/docs/FEATURES.md +36 -0
  46. package/docs/OPENCLAW_PLUGIN_GUIDE.md +1202 -0
  47. package/docs/SETUP.md +303 -0
  48. package/openclaw.plugin.json +137 -0
  49. package/package.json +37 -0
  50. package/skills/voice-assistant/SKILL.md +15 -0
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TwilioTelephonyAdapter = void 0;
4
+ const util_1 = require("./util");
5
+ class TwilioTelephonyAdapter {
6
+ constructor(config, fetchFn) {
7
+ this.config = config;
8
+ this.fetchFn = fetchFn ?? globalThis.fetch;
9
+ }
10
+ providerName() {
11
+ return "twilio";
12
+ }
13
+ async startCall(input) {
14
+ const normalizedTo = (0, util_1.normalizeE164)(input.to);
15
+ if (!this.config.twilioAccountSid ||
16
+ !this.config.twilioAuthToken ||
17
+ !this.config.twilioPhoneNumber) {
18
+ throw new Error("Twilio credentials missing: twilioAccountSid, twilioAuthToken, and twilioPhoneNumber are required");
19
+ }
20
+ const url = `https://api.twilio.com/2010-04-01/Accounts/${this.config.twilioAccountSid}/Calls.json`;
21
+ const from = input.from ?? this.config.twilioPhoneNumber;
22
+ const twiml = `<Response><Say>${input.greeting ?? "Hello"}</Say></Response>`;
23
+ const body = new URLSearchParams({
24
+ To: normalizedTo,
25
+ From: from ?? "",
26
+ Twiml: twiml,
27
+ });
28
+ const auth = Buffer.from(`${this.config.twilioAccountSid}:${this.config.twilioAuthToken}`).toString("base64");
29
+ const response = await this.fetchFn(url, {
30
+ method: "POST",
31
+ headers: {
32
+ Authorization: `Basic ${auth}`,
33
+ "Content-Type": "application/x-www-form-urlencoded",
34
+ },
35
+ body: body.toString(),
36
+ });
37
+ if (!response.ok) {
38
+ const errorText = await response.text().catch(() => "Unknown error");
39
+ throw new Error(`Twilio API error (${response.status}): ${errorText}`);
40
+ }
41
+ const data = (await response.json());
42
+ return {
43
+ providerCallId: data.sid,
44
+ normalizedTo,
45
+ };
46
+ }
47
+ async hangup(providerCallId) {
48
+ if (!this.config.twilioAccountSid || !this.config.twilioAuthToken) {
49
+ return;
50
+ }
51
+ const url = `https://api.twilio.com/2010-04-01/Accounts/${this.config.twilioAccountSid}/Calls/${providerCallId}.json`;
52
+ const auth = Buffer.from(`${this.config.twilioAccountSid}:${this.config.twilioAuthToken}`).toString("base64");
53
+ await this.fetchFn(url, {
54
+ method: "POST",
55
+ headers: {
56
+ Authorization: `Basic ${auth}`,
57
+ "Content-Type": "application/x-www-form-urlencoded",
58
+ },
59
+ body: new URLSearchParams({ Status: "completed" }).toString(),
60
+ }).catch(() => undefined);
61
+ }
62
+ }
63
+ exports.TwilioTelephonyAdapter = TwilioTelephonyAdapter;
@@ -0,0 +1,15 @@
1
+ export interface StartCallInput {
2
+ to: string;
3
+ from?: string;
4
+ greeting?: string;
5
+ purpose?: string;
6
+ }
7
+ export interface StartCallResult {
8
+ providerCallId: string;
9
+ normalizedTo: string;
10
+ }
11
+ export interface TelephonyProviderAdapter {
12
+ providerName(): string;
13
+ startCall(input: StartCallInput): Promise<StartCallResult>;
14
+ hangup(providerCallId: string): Promise<void>;
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export declare function normalizeE164(phoneNumber: string): string;
2
+ export declare function simulatedCallId(prefix: string): string;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeE164 = normalizeE164;
4
+ exports.simulatedCallId = simulatedCallId;
5
+ function normalizeE164(phoneNumber) {
6
+ if (phoneNumber.startsWith("+")) {
7
+ const digits = phoneNumber.replace(/\D/g, "");
8
+ if (digits.length < 10) {
9
+ throw new Error("Invalid international phone number. Must contain at least 10 digits.");
10
+ }
11
+ return `+${digits}`;
12
+ }
13
+ const digits = phoneNumber.replace(/\D/g, "");
14
+ if (digits.length !== 10) {
15
+ throw new Error("Invalid US phone number. Provide exactly 10 digits or valid E.164 number.");
16
+ }
17
+ return `+1${digits}`;
18
+ }
19
+ function simulatedCallId(prefix) {
20
+ const now = Date.now();
21
+ const random = Math.floor(Math.random() * 1000000)
22
+ .toString()
23
+ .padStart(6, "0");
24
+ return `${prefix}-${now}-${random}`;
25
+ }
@@ -0,0 +1,5 @@
1
+ import { PluginAPI } from "@openclaw/plugin-sdk";
2
+ import { ClawVoiceConfig } from "./config";
3
+ import { VoiceCallService } from "./services/voice-call";
4
+ import { MemoryExtractionService } from "./services/memory-extraction";
5
+ export declare function registerTools(api: PluginAPI, config: ClawVoiceConfig, callService: VoiceCallService, memoryService?: MemoryExtractionService): void;
package/dist/tools.js ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerTools = registerTools;
4
+ function readString(value) {
5
+ return typeof value === "string" && value.trim().length > 0
6
+ ? value.trim()
7
+ : undefined;
8
+ }
9
+ function registerTools(api, config, callService, memoryService) {
10
+ api.tools.register({
11
+ name: "voice_assistant.call",
12
+ description: "Initiate an outbound voice call",
13
+ parameters: {
14
+ type: "object",
15
+ properties: {
16
+ phoneNumber: {
17
+ type: "string",
18
+ description: "Phone number in E.164 format",
19
+ },
20
+ purpose: {
21
+ type: "string",
22
+ description: "Brief description of call purpose",
23
+ },
24
+ greeting: {
25
+ type: "string",
26
+ description: "Custom greeting spoken at call start (overrides default)",
27
+ },
28
+ },
29
+ required: ["phoneNumber"],
30
+ },
31
+ handler: async (input) => {
32
+ const phoneNumber = readString(input.phoneNumber);
33
+ if (!phoneNumber) {
34
+ throw new Error("phoneNumber is required and must be a non-empty string.");
35
+ }
36
+ const purpose = readString(input.purpose);
37
+ const greeting = readString(input.greeting);
38
+ const result = await callService.startCall({
39
+ phoneNumber,
40
+ purpose,
41
+ greeting,
42
+ });
43
+ return {
44
+ content: `${result.message} Greeting: \"${result.openingGreeting}\"`,
45
+ data: {
46
+ callId: result.callId,
47
+ to: result.to,
48
+ provider: config.telephonyProvider,
49
+ purpose: purpose ?? null,
50
+ },
51
+ };
52
+ },
53
+ });
54
+ api.tools.register({
55
+ name: "voice_assistant.hangup",
56
+ description: "End an active voice call",
57
+ parameters: {
58
+ type: "object",
59
+ properties: {
60
+ callId: {
61
+ type: "string",
62
+ description: "Call ID to hang up. Omit for most recent.",
63
+ },
64
+ },
65
+ },
66
+ handler: async (input) => {
67
+ const result = await callService.hangup(readString(input.callId));
68
+ return {
69
+ content: result.message,
70
+ data: {
71
+ callId: result.callId,
72
+ },
73
+ };
74
+ },
75
+ });
76
+ api.tools.register({
77
+ name: "voice_assistant.status",
78
+ description: "Get active call status or post-call summary with retry context",
79
+ parameters: {
80
+ type: "object",
81
+ properties: {
82
+ callId: {
83
+ type: "string",
84
+ description: "Specific call ID to get summary for (optional)",
85
+ },
86
+ },
87
+ },
88
+ handler: async (input) => {
89
+ const summaryCallId = readString(input.callId);
90
+ if (summaryCallId) {
91
+ const summary = callService.getCallSummary(summaryCallId);
92
+ if (summary) {
93
+ const failureText = summary.failures.length > 0
94
+ ? ` Failures: ${summary.failures.map((f) => f.description).join("; ")}.`
95
+ : "";
96
+ const retryText = summary.retryContext
97
+ ? ` Retry suggestion: ${summary.retryContext.suggestedApproach}`
98
+ : "";
99
+ return {
100
+ content: `Call ${summaryCallId}: ${summary.outcome}.${failureText}${retryText}`,
101
+ data: { summary },
102
+ };
103
+ }
104
+ }
105
+ const activeCalls = callService.getActiveCalls();
106
+ return {
107
+ content: activeCalls.length > 0
108
+ ? `There are ${activeCalls.length} active call(s).`
109
+ : "No active calls.",
110
+ data: {
111
+ activeCalls,
112
+ },
113
+ };
114
+ },
115
+ });
116
+ api.tools.register({
117
+ name: "voice_assistant.promote_memory",
118
+ description: "Review and promote a voice memory to main MEMORY.md. Requires operator confirmation.",
119
+ parameters: {
120
+ type: "object",
121
+ properties: {
122
+ memoryId: {
123
+ type: "string",
124
+ description: "ID of the voice memory entry to promote",
125
+ },
126
+ confirm: {
127
+ type: "boolean",
128
+ description: "Set to true to confirm promotion. First call without confirm to preview.",
129
+ },
130
+ },
131
+ required: ["memoryId"],
132
+ },
133
+ handler: async (input) => {
134
+ const memoryId = readString(input.memoryId);
135
+ if (!memoryId) {
136
+ throw new Error("memoryId is required.");
137
+ }
138
+ if (!memoryService) {
139
+ return { content: "Memory extraction service not available." };
140
+ }
141
+ const candidate = memoryService.getCandidate(memoryId);
142
+ if (!candidate) {
143
+ return { content: `Memory candidate ${memoryId} not found.` };
144
+ }
145
+ const confirmed = input.confirm === true || input.confirm === "true";
146
+ if (!confirmed) {
147
+ return {
148
+ content: `Preview: "${candidate.content}" (${candidate.category}, confidence: ${candidate.confidence}). Call again with confirm: true to promote.`,
149
+ data: {
150
+ memoryId,
151
+ category: candidate.category,
152
+ content: candidate.content,
153
+ requiresConfirmation: true,
154
+ },
155
+ };
156
+ }
157
+ const result = await memoryService.approveAndPromote(memoryId);
158
+ if (result.promoted) {
159
+ return {
160
+ content: `Memory ${memoryId} promoted to main memory (${candidate.category}).`,
161
+ data: { memoryId, category: candidate.category },
162
+ };
163
+ }
164
+ return { content: `Promotion failed: ${result.reason}` };
165
+ },
166
+ });
167
+ }
@@ -0,0 +1,47 @@
1
+ import { AudioCodec, BridgeEvent, BridgeSessionConfig, CallFailure, CallSummary, CodecNegotiationResult, DisconnectionReason, DisconnectionRecord, FunctionCallRequest, FunctionCallResponse, TranscriptEntry, TurnState, VoiceAgentMessageResult } from "./types";
2
+ import { ClawVoiceConfig } from "../config";
3
+ export interface VoiceWebSocket {
4
+ send(data: string | Buffer): void;
5
+ close(): void;
6
+ readyState: number;
7
+ }
8
+ export type DisconnectionHandler = (record: DisconnectionRecord) => void;
9
+ export declare class VoiceBridgeService {
10
+ private readonly config;
11
+ private readonly bridges;
12
+ private disconnectionHandler;
13
+ constructor(config: ClawVoiceConfig);
14
+ onDisconnection(handler: DisconnectionHandler): void;
15
+ negotiateAndValidate(telephonyCodec?: AudioCodec, voiceProviderCodec?: AudioCodec, sampleRate?: number): CodecNegotiationResult;
16
+ createSession(sessionConfig: BridgeSessionConfig): BridgeEvent;
17
+ buildSettingsMessage(sessionConfig: BridgeSessionConfig): Record<string, unknown>;
18
+ setVoiceSocket(callId: string, socket: VoiceWebSocket): void;
19
+ getVoiceSocket(callId: string): VoiceWebSocket | null;
20
+ startKeepAlive(callId: string, intervalMs: number): void;
21
+ recordActivity(callId: string): void;
22
+ startHeartbeatMonitor(callId: string, timeoutMs?: number): void;
23
+ stopHeartbeatMonitor(callId: string): void;
24
+ reportDisconnection(callId: string, reason: DisconnectionReason, detail: string): DisconnectionRecord | null;
25
+ getDisconnectionRecord(callId: string): DisconnectionRecord | null;
26
+ endGreetingGrace(callId: string): void;
27
+ isGreetingGraceActive(callId: string): boolean;
28
+ bufferTelephonyAudio(callId: string, chunk: Buffer): Buffer | null;
29
+ flushAudioBuffer(callId: string): Buffer | null;
30
+ addTranscriptEntry(callId: string, entry: TranscriptEntry): void;
31
+ getTranscript(callId: string): TranscriptEntry[];
32
+ destroySession(callId: string): BridgeEvent;
33
+ handleVoiceAgentMessage(callId: string, message: Record<string, unknown>): VoiceAgentMessageResult;
34
+ handleBargeIn(callId: string): VoiceAgentMessageResult;
35
+ completeFunctionCall(callId: string, response: FunctionCallResponse): boolean;
36
+ getTurnState(callId: string): TurnState;
37
+ setTurnState(callId: string, state: TurnState): void;
38
+ getPendingFunctionCalls(callId: string): FunctionCallRequest[];
39
+ recordFailure(callId: string, failure: CallFailure): void;
40
+ getFailures(callId: string): CallFailure[];
41
+ generateCallSummary(callId: string): CallSummary | null;
42
+ private determineOutcome;
43
+ private buildRetryContext;
44
+ getActiveBridgeCount(): number;
45
+ hasActiveBridge(callId: string): boolean;
46
+ stopAll(): Promise<void>;
47
+ }