convex-vapi 0.0.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 +201 -0
- package/README.md +437 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/index.d.ts +115 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +172 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +34 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +125 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +4 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/lib.d.ts +101 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +177 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +53 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +30 -0
- package/dist/component/schema.js.map +1 -0
- package/package.json +105 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/index.ts +250 -0
- package/src/client/setup.test.ts +26 -0
- package/src/component/_generated/api.ts +50 -0
- package/src/component/_generated/component.ts +159 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/lib.test.ts +167 -0
- package/src/component/lib.ts +193 -0
- package/src/component/schema.ts +31 -0
- package/src/component/setup.test.ts +11 -0
- package/src/test.ts +18 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ActionBuilder,
|
|
13
|
+
HttpActionBuilder,
|
|
14
|
+
MutationBuilder,
|
|
15
|
+
QueryBuilder,
|
|
16
|
+
GenericActionCtx,
|
|
17
|
+
GenericMutationCtx,
|
|
18
|
+
GenericQueryCtx,
|
|
19
|
+
GenericDatabaseReader,
|
|
20
|
+
GenericDatabaseWriter,
|
|
21
|
+
} from "convex/server";
|
|
22
|
+
import {
|
|
23
|
+
actionGeneric,
|
|
24
|
+
httpActionGeneric,
|
|
25
|
+
queryGeneric,
|
|
26
|
+
mutationGeneric,
|
|
27
|
+
internalActionGeneric,
|
|
28
|
+
internalMutationGeneric,
|
|
29
|
+
internalQueryGeneric,
|
|
30
|
+
} from "convex/server";
|
|
31
|
+
import type { DataModel } from "./dataModel.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Typesafe environment variables.
|
|
35
|
+
*
|
|
36
|
+
* This includes platform-provided env vars and any variables declared in
|
|
37
|
+
* `convex.config.ts`.
|
|
38
|
+
*/
|
|
39
|
+
type Env = {
|
|
40
|
+
readonly CONVEX_CLOUD_URL: string;
|
|
41
|
+
readonly CONVEX_SITE_URL: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Define a query in this Convex app's public API.
|
|
46
|
+
*
|
|
47
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
48
|
+
*
|
|
49
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
50
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
51
|
+
*/
|
|
52
|
+
export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
56
|
+
*
|
|
57
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
58
|
+
*
|
|
59
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
60
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
61
|
+
*/
|
|
62
|
+
export const internalQuery: QueryBuilder<DataModel, "internal"> =
|
|
63
|
+
internalQueryGeneric;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Define a mutation in this Convex app's public API.
|
|
67
|
+
*
|
|
68
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
69
|
+
*
|
|
70
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
71
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
72
|
+
*/
|
|
73
|
+
export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
77
|
+
*
|
|
78
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
79
|
+
*
|
|
80
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
81
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
82
|
+
*/
|
|
83
|
+
export const internalMutation: MutationBuilder<DataModel, "internal"> =
|
|
84
|
+
internalMutationGeneric;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Define an action in this Convex app's public API.
|
|
88
|
+
*
|
|
89
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
90
|
+
* code and code with side-effects, like calling third-party services.
|
|
91
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
92
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
93
|
+
*
|
|
94
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
95
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
96
|
+
*/
|
|
97
|
+
export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
101
|
+
*
|
|
102
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
103
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
104
|
+
*/
|
|
105
|
+
export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
106
|
+
internalActionGeneric;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Define an HTTP action.
|
|
110
|
+
*
|
|
111
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
112
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
113
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
114
|
+
*
|
|
115
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
116
|
+
* and a Fetch API `Request` object as its second.
|
|
117
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
118
|
+
*/
|
|
119
|
+
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
120
|
+
export const env: Env = (globalThis as unknown as { process: { env: Env } })
|
|
121
|
+
.process.env;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A set of services for use within Convex query functions.
|
|
125
|
+
*
|
|
126
|
+
* The query context is passed as the first argument to any Convex query
|
|
127
|
+
* function run on the server.
|
|
128
|
+
*
|
|
129
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
130
|
+
*/
|
|
131
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A set of services for use within Convex mutation functions.
|
|
135
|
+
*
|
|
136
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
137
|
+
* function run on the server.
|
|
138
|
+
*
|
|
139
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
140
|
+
*/
|
|
141
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A set of services for use within Convex action functions.
|
|
145
|
+
*
|
|
146
|
+
* The action context is passed as the first argument to any Convex action
|
|
147
|
+
* function run on the server.
|
|
148
|
+
*/
|
|
149
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* An interface to read from the database within Convex query functions.
|
|
153
|
+
*
|
|
154
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
155
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
156
|
+
* building a query.
|
|
157
|
+
*/
|
|
158
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* An interface to read from and write to the database within Convex mutation
|
|
162
|
+
* functions.
|
|
163
|
+
*
|
|
164
|
+
* Convex guarantees that all writes within a single mutation are
|
|
165
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
166
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
167
|
+
* for the guarantees Convex provides your functions.
|
|
168
|
+
*/
|
|
169
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { initConvexTest } from "./setup.test.js";
|
|
3
|
+
import { api } from "./_generated/api.js";
|
|
4
|
+
|
|
5
|
+
describe("calls", () => {
|
|
6
|
+
test("recordCall inserts then merges a sparser status-update without blanking fields", async () => {
|
|
7
|
+
const t = initConvexTest();
|
|
8
|
+
|
|
9
|
+
await t.mutation(api.lib.recordCall, {
|
|
10
|
+
callId: "call_1",
|
|
11
|
+
assistantId: "assistant_1",
|
|
12
|
+
status: "ended",
|
|
13
|
+
transcript: "Hello, how can I help you today?",
|
|
14
|
+
summary: "Customer asked about pricing.",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
let call = await t.query(api.lib.getCall, { callId: "call_1" });
|
|
18
|
+
expect(call?.transcript).toBe("Hello, how can I help you today?");
|
|
19
|
+
|
|
20
|
+
// A later, sparser event (e.g. a duplicate status-update) must not erase
|
|
21
|
+
// the transcript/summary already recorded by the end-of-call-report.
|
|
22
|
+
await t.mutation(api.lib.recordCall, {
|
|
23
|
+
callId: "call_1",
|
|
24
|
+
status: "ended",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
call = await t.query(api.lib.getCall, { callId: "call_1" });
|
|
28
|
+
expect(call?.status).toBe("ended");
|
|
29
|
+
expect(call?.transcript).toBe("Hello, how can I help you today?");
|
|
30
|
+
expect(call?.summary).toBe("Customer asked about pricing.");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("recordCall never lets a late, out-of-order status-update regress a call away from ended", async () => {
|
|
34
|
+
const t = initConvexTest();
|
|
35
|
+
|
|
36
|
+
// The end-of-call-report arrives first and marks the call ended.
|
|
37
|
+
await t.mutation(api.lib.recordCall, {
|
|
38
|
+
callId: "call_2",
|
|
39
|
+
assistantId: "assistant_1",
|
|
40
|
+
status: "ended",
|
|
41
|
+
endedReason: "customer-ended-call",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Vapi gives no delivery ordering guarantee, so a status-update from
|
|
45
|
+
// earlier in the call can still arrive after the end-of-call-report.
|
|
46
|
+
// It must not regress the call's status back to an in-progress state.
|
|
47
|
+
await t.mutation(api.lib.recordCall, {
|
|
48
|
+
callId: "call_2",
|
|
49
|
+
assistantId: "assistant_1",
|
|
50
|
+
status: "in-progress",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const call = await t.query(api.lib.getCall, { callId: "call_2" });
|
|
54
|
+
expect(call?.status).toBe("ended");
|
|
55
|
+
expect(call?.endedReason).toBe("customer-ended-call");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("listCallsByAssistant scopes by assistantId", async () => {
|
|
59
|
+
const t = initConvexTest();
|
|
60
|
+
|
|
61
|
+
await t.mutation(api.lib.recordCall, {
|
|
62
|
+
callId: "call_a",
|
|
63
|
+
assistantId: "assistant_a",
|
|
64
|
+
status: "queued",
|
|
65
|
+
});
|
|
66
|
+
await t.mutation(api.lib.recordCall, {
|
|
67
|
+
callId: "call_b",
|
|
68
|
+
assistantId: "assistant_b",
|
|
69
|
+
status: "queued",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const results = await t.query(api.lib.listCallsByAssistant, {
|
|
73
|
+
assistantId: "assistant_a",
|
|
74
|
+
});
|
|
75
|
+
expect(results).toHaveLength(1);
|
|
76
|
+
expect(results[0].callId).toBe("call_a");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("dashboard queries", () => {
|
|
81
|
+
test("getStats counts calls by status and webhook events", async () => {
|
|
82
|
+
const t = initConvexTest();
|
|
83
|
+
|
|
84
|
+
await t.mutation(api.lib.recordCall, { callId: "call_a", status: "ended" });
|
|
85
|
+
await t.mutation(api.lib.recordCall, {
|
|
86
|
+
callId: "call_b",
|
|
87
|
+
status: "ringing",
|
|
88
|
+
});
|
|
89
|
+
await t.mutation(api.lib.recordCall, {
|
|
90
|
+
callId: "call_c",
|
|
91
|
+
status: "in-progress",
|
|
92
|
+
});
|
|
93
|
+
await t.mutation(api.lib.checkAndRecordEvent, {
|
|
94
|
+
eventId: "hash_a",
|
|
95
|
+
eventType: "status-update",
|
|
96
|
+
payload: "{}",
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const stats = await t.query(api.lib.getStats, {});
|
|
100
|
+
expect(stats).toEqual({
|
|
101
|
+
callCount: 3,
|
|
102
|
+
endedCount: 1,
|
|
103
|
+
liveCount: 2,
|
|
104
|
+
webhookEventCount: 1,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("listRecentCalls returns calls most-recently-updated first", async () => {
|
|
109
|
+
const t = initConvexTest();
|
|
110
|
+
|
|
111
|
+
await t.mutation(api.lib.recordCall, {
|
|
112
|
+
callId: "call_old",
|
|
113
|
+
status: "ended",
|
|
114
|
+
});
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
116
|
+
await t.mutation(api.lib.recordCall, {
|
|
117
|
+
callId: "call_new",
|
|
118
|
+
status: "ended",
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const recent = await t.query(api.lib.listRecentCalls, { limit: 10 });
|
|
122
|
+
expect(recent.map((c) => c.callId)).toEqual(["call_new", "call_old"]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("listRecentWebhookEvents returns events most-recent first", async () => {
|
|
126
|
+
const t = initConvexTest();
|
|
127
|
+
|
|
128
|
+
await t.mutation(api.lib.checkAndRecordEvent, {
|
|
129
|
+
eventId: "hash_old",
|
|
130
|
+
eventType: "status-update",
|
|
131
|
+
payload: "{}",
|
|
132
|
+
});
|
|
133
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
134
|
+
await t.mutation(api.lib.checkAndRecordEvent, {
|
|
135
|
+
eventId: "hash_new",
|
|
136
|
+
eventType: "end-of-call-report",
|
|
137
|
+
payload: "{}",
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const recent = await t.query(api.lib.listRecentWebhookEvents, {
|
|
141
|
+
limit: 10,
|
|
142
|
+
});
|
|
143
|
+
expect(recent.map((e) => e.eventId)).toEqual(["hash_new", "hash_old"]);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("webhook idempotency", () => {
|
|
148
|
+
test("checkAndRecordEvent flags duplicate payload hashes", async () => {
|
|
149
|
+
const t = initConvexTest();
|
|
150
|
+
|
|
151
|
+
const first = await t.mutation(api.lib.checkAndRecordEvent, {
|
|
152
|
+
eventId: "hash_1",
|
|
153
|
+
eventType: "status-update",
|
|
154
|
+
callId: "call_1",
|
|
155
|
+
payload: "{}",
|
|
156
|
+
});
|
|
157
|
+
expect(first.alreadyProcessed).toBe(false);
|
|
158
|
+
|
|
159
|
+
const second = await t.mutation(api.lib.checkAndRecordEvent, {
|
|
160
|
+
eventId: "hash_1",
|
|
161
|
+
eventType: "status-update",
|
|
162
|
+
callId: "call_1",
|
|
163
|
+
payload: "{}",
|
|
164
|
+
});
|
|
165
|
+
expect(second.alreadyProcessed).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation, query } from "./_generated/server.js";
|
|
3
|
+
|
|
4
|
+
const callValidator = v.object({
|
|
5
|
+
_id: v.id("calls"),
|
|
6
|
+
_creationTime: v.number(),
|
|
7
|
+
callId: v.string(),
|
|
8
|
+
assistantId: v.optional(v.string()),
|
|
9
|
+
phoneNumberId: v.optional(v.string()),
|
|
10
|
+
customerNumber: v.optional(v.string()),
|
|
11
|
+
status: v.string(),
|
|
12
|
+
endedReason: v.optional(v.string()),
|
|
13
|
+
transcript: v.optional(v.string()),
|
|
14
|
+
summary: v.optional(v.string()),
|
|
15
|
+
recordingUrl: v.optional(v.string()),
|
|
16
|
+
cost: v.optional(v.number()),
|
|
17
|
+
startedAt: v.optional(v.number()),
|
|
18
|
+
endedAt: v.optional(v.number()),
|
|
19
|
+
createdAt: v.number(),
|
|
20
|
+
updatedAt: v.number(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// ─── Queries ────────────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
export const getCall = query({
|
|
26
|
+
args: { callId: v.string() },
|
|
27
|
+
returns: v.union(v.null(), callValidator),
|
|
28
|
+
handler: async (ctx, args) => {
|
|
29
|
+
return await ctx.db
|
|
30
|
+
.query("calls")
|
|
31
|
+
.withIndex("by_callId", (q) => q.eq("callId", args.callId))
|
|
32
|
+
.first();
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export const listCallsByAssistant = query({
|
|
37
|
+
args: { assistantId: v.string(), limit: v.optional(v.number()) },
|
|
38
|
+
returns: v.array(callValidator),
|
|
39
|
+
handler: async (ctx, args) => {
|
|
40
|
+
return await ctx.db
|
|
41
|
+
.query("calls")
|
|
42
|
+
.withIndex("by_assistantId", (q) => q.eq("assistantId", args.assistantId))
|
|
43
|
+
.order("desc")
|
|
44
|
+
.take(args.limit ?? 50);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// ─── Mutations ──────────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
export const recordCall = mutation({
|
|
51
|
+
args: {
|
|
52
|
+
callId: v.string(),
|
|
53
|
+
assistantId: v.optional(v.string()),
|
|
54
|
+
phoneNumberId: v.optional(v.string()),
|
|
55
|
+
customerNumber: v.optional(v.string()),
|
|
56
|
+
status: v.string(),
|
|
57
|
+
endedReason: v.optional(v.string()),
|
|
58
|
+
transcript: v.optional(v.string()),
|
|
59
|
+
summary: v.optional(v.string()),
|
|
60
|
+
recordingUrl: v.optional(v.string()),
|
|
61
|
+
cost: v.optional(v.number()),
|
|
62
|
+
startedAt: v.optional(v.number()),
|
|
63
|
+
endedAt: v.optional(v.number()),
|
|
64
|
+
},
|
|
65
|
+
returns: v.id("calls"),
|
|
66
|
+
handler: async (ctx, args) => {
|
|
67
|
+
const now = Date.now();
|
|
68
|
+
const existing = await ctx.db
|
|
69
|
+
.query("calls")
|
|
70
|
+
.withIndex("by_callId", (q) => q.eq("callId", args.callId))
|
|
71
|
+
.first();
|
|
72
|
+
|
|
73
|
+
if (existing) {
|
|
74
|
+
// Merge rather than blanket-overwrite: a later, sparser event (e.g. a
|
|
75
|
+
// status-update with no transcript yet) must never erase fields an
|
|
76
|
+
// earlier, richer event (e.g. end-of-call-report) already recorded.
|
|
77
|
+
//
|
|
78
|
+
// status is a special case: Vapi's webhook deliveries carry no
|
|
79
|
+
// timestamp or sequence number, and delivery order is never
|
|
80
|
+
// guaranteed by any webhook provider, so a status-update that
|
|
81
|
+
// arrives late could otherwise regress a call backward (e.g. from
|
|
82
|
+
// "ended" back to "ringing"). "ended" is the one status Vapi never
|
|
83
|
+
// revises once sent, so once we've recorded it, later events are
|
|
84
|
+
// treated as stragglers and can't downgrade it back.
|
|
85
|
+
const status =
|
|
86
|
+
existing.status === "ended" ? existing.status : args.status;
|
|
87
|
+
const merged = {
|
|
88
|
+
callId: args.callId,
|
|
89
|
+
assistantId: args.assistantId ?? existing.assistantId,
|
|
90
|
+
phoneNumberId: args.phoneNumberId ?? existing.phoneNumberId,
|
|
91
|
+
customerNumber: args.customerNumber ?? existing.customerNumber,
|
|
92
|
+
status,
|
|
93
|
+
endedReason: args.endedReason ?? existing.endedReason,
|
|
94
|
+
transcript: args.transcript ?? existing.transcript,
|
|
95
|
+
summary: args.summary ?? existing.summary,
|
|
96
|
+
recordingUrl: args.recordingUrl ?? existing.recordingUrl,
|
|
97
|
+
cost: args.cost ?? existing.cost,
|
|
98
|
+
startedAt: args.startedAt ?? existing.startedAt,
|
|
99
|
+
endedAt: args.endedAt ?? existing.endedAt,
|
|
100
|
+
};
|
|
101
|
+
await ctx.db.patch(existing._id, { ...merged, updatedAt: now });
|
|
102
|
+
return existing._id;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return await ctx.db.insert("calls", {
|
|
106
|
+
...args,
|
|
107
|
+
createdAt: now,
|
|
108
|
+
updatedAt: now,
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
export const checkAndRecordEvent = mutation({
|
|
114
|
+
args: {
|
|
115
|
+
eventId: v.string(),
|
|
116
|
+
eventType: v.string(),
|
|
117
|
+
callId: v.optional(v.string()),
|
|
118
|
+
payload: v.string(),
|
|
119
|
+
},
|
|
120
|
+
returns: v.object({ alreadyProcessed: v.boolean() }),
|
|
121
|
+
handler: async (ctx, args) => {
|
|
122
|
+
const existing = await ctx.db
|
|
123
|
+
.query("webhookEvents")
|
|
124
|
+
.withIndex("by_eventId", (q) => q.eq("eventId", args.eventId))
|
|
125
|
+
.first();
|
|
126
|
+
if (existing) {
|
|
127
|
+
return { alreadyProcessed: true };
|
|
128
|
+
}
|
|
129
|
+
await ctx.db.insert("webhookEvents", { ...args, receivedAt: Date.now() });
|
|
130
|
+
return { alreadyProcessed: false };
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// ─── Dashboard queries ──────────────────────────────────────────────────────
|
|
135
|
+
// These do full, un-indexed scans across every call/event the component has
|
|
136
|
+
// ever recorded, on purpose — they power a demo's stats bar and activity
|
|
137
|
+
// history, not high-volume production use.
|
|
138
|
+
|
|
139
|
+
export const getStats = query({
|
|
140
|
+
args: {},
|
|
141
|
+
returns: v.object({
|
|
142
|
+
callCount: v.number(),
|
|
143
|
+
endedCount: v.number(),
|
|
144
|
+
liveCount: v.number(),
|
|
145
|
+
webhookEventCount: v.number(),
|
|
146
|
+
}),
|
|
147
|
+
handler: async (ctx) => {
|
|
148
|
+
const [calls, webhookEvents] = await Promise.all([
|
|
149
|
+
ctx.db.query("calls").collect(),
|
|
150
|
+
ctx.db.query("webhookEvents").collect(),
|
|
151
|
+
]);
|
|
152
|
+
const live = (status: string) =>
|
|
153
|
+
status === "queued" || status === "ringing" || status === "in-progress";
|
|
154
|
+
return {
|
|
155
|
+
callCount: calls.length,
|
|
156
|
+
endedCount: calls.filter((c) => c.status === "ended").length,
|
|
157
|
+
liveCount: calls.filter((c) => live(c.status)).length,
|
|
158
|
+
webhookEventCount: webhookEvents.length,
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
export const listRecentCalls = query({
|
|
164
|
+
args: { limit: v.optional(v.number()) },
|
|
165
|
+
returns: v.array(callValidator),
|
|
166
|
+
handler: async (ctx, args) => {
|
|
167
|
+
const calls = await ctx.db.query("calls").collect();
|
|
168
|
+
return calls
|
|
169
|
+
.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
170
|
+
.slice(0, args.limit ?? 20);
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
export const listRecentWebhookEvents = query({
|
|
175
|
+
args: { limit: v.optional(v.number()) },
|
|
176
|
+
returns: v.array(
|
|
177
|
+
v.object({
|
|
178
|
+
_id: v.id("webhookEvents"),
|
|
179
|
+
_creationTime: v.number(),
|
|
180
|
+
eventId: v.string(),
|
|
181
|
+
eventType: v.string(),
|
|
182
|
+
callId: v.optional(v.string()),
|
|
183
|
+
payload: v.string(),
|
|
184
|
+
receivedAt: v.number(),
|
|
185
|
+
}),
|
|
186
|
+
),
|
|
187
|
+
handler: async (ctx, args) => {
|
|
188
|
+
const events = await ctx.db.query("webhookEvents").collect();
|
|
189
|
+
return events
|
|
190
|
+
.sort((a, b) => b.receivedAt - a.receivedAt)
|
|
191
|
+
.slice(0, args.limit ?? 20);
|
|
192
|
+
},
|
|
193
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
export default defineSchema({
|
|
5
|
+
calls: defineTable({
|
|
6
|
+
callId: v.string(), // Vapi's call id
|
|
7
|
+
assistantId: v.optional(v.string()),
|
|
8
|
+
phoneNumberId: v.optional(v.string()),
|
|
9
|
+
customerNumber: v.optional(v.string()),
|
|
10
|
+
status: v.string(), // "queued" | "ringing" | "in-progress" | "forwarding" | "ended" | ...
|
|
11
|
+
endedReason: v.optional(v.string()),
|
|
12
|
+
transcript: v.optional(v.string()),
|
|
13
|
+
summary: v.optional(v.string()),
|
|
14
|
+
recordingUrl: v.optional(v.string()),
|
|
15
|
+
cost: v.optional(v.number()),
|
|
16
|
+
startedAt: v.optional(v.number()),
|
|
17
|
+
endedAt: v.optional(v.number()),
|
|
18
|
+
createdAt: v.number(),
|
|
19
|
+
updatedAt: v.number(),
|
|
20
|
+
})
|
|
21
|
+
.index("by_callId", ["callId"])
|
|
22
|
+
.index("by_assistantId", ["assistantId"]),
|
|
23
|
+
|
|
24
|
+
webhookEvents: defineTable({
|
|
25
|
+
eventId: v.string(), // sha256 hex of the raw body — Vapi sends no per-delivery id
|
|
26
|
+
eventType: v.string(), // message.type
|
|
27
|
+
callId: v.optional(v.string()),
|
|
28
|
+
payload: v.string(),
|
|
29
|
+
receivedAt: v.number(),
|
|
30
|
+
}).index("by_eventId", ["eventId"]),
|
|
31
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
import { test } from "vitest";
|
|
3
|
+
import schema from "./schema.js";
|
|
4
|
+
import { convexTest } from "convex-test";
|
|
5
|
+
export const modules = import.meta.glob("./**/*.*s");
|
|
6
|
+
|
|
7
|
+
export function initConvexTest() {
|
|
8
|
+
const t = convexTest(schema, modules);
|
|
9
|
+
return t;
|
|
10
|
+
}
|
|
11
|
+
test("setup", () => {});
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
import type { TestConvex } from "convex-test";
|
|
3
|
+
import type { GenericSchema, SchemaDefinition } from "convex/server";
|
|
4
|
+
import schema from "./component/schema.js";
|
|
5
|
+
const modules = import.meta.glob("./component/**/*.ts");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Register the component with the test convex instance.
|
|
9
|
+
* @param t - The test convex instance, e.g. from calling `convexTest`.
|
|
10
|
+
* @param name - The name of the component, as registered in convex.config.ts.
|
|
11
|
+
*/
|
|
12
|
+
export function register(
|
|
13
|
+
t: TestConvex<SchemaDefinition<GenericSchema, boolean>>,
|
|
14
|
+
name: string = "convexVapi",
|
|
15
|
+
) {
|
|
16
|
+
t.registerComponent(name, schema, modules);
|
|
17
|
+
}
|
|
18
|
+
export default { register, schema, modules };
|