@talkpilot/core-db 1.0.2 → 1.0.4
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/dist/talkpilot/agents/agents.getters.d.ts +7 -7
- package/dist/talkpilot/agents/agents.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.d.ts +2 -2
- package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.js +18 -14
- package/dist/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.js.map +1 -1
- package/dist/talkpilot/groups/groups.getters.d.ts +1 -1
- package/dist/talkpilot/groups/groups.getters.d.ts.map +1 -1
- package/jest.config.js +0 -3
- package/package.json +1 -1
- package/src/talkpilot/clientAudioBuffers/__tests__/clientAudioBuffer.getters.spec.ts +53 -5
- package/src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts +17 -19
- package/tsconfig.json +1 -5
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Agent } from '../index';
|
|
2
|
-
export declare const getAgentsCollection: () => import("mongodb
|
|
3
|
-
export declare const findAgents: (filter?: any) => Promise<import("mongodb
|
|
4
|
-
export declare const findAgentById: (agentId: string) => Promise<import("mongodb
|
|
5
|
-
export declare const findAgentsByClientId: (clientId: string) => Promise<import("mongodb
|
|
6
|
-
export declare const createAgent: (agent: Agent) => Promise<import("mongodb
|
|
7
|
-
export declare const updateAgent: (agentId: string, data: Partial<Agent>) => Promise<import("mongodb
|
|
8
|
-
export declare const removeAgent: (agentId: string) => Promise<import("mongodb
|
|
2
|
+
export declare const getAgentsCollection: () => import("mongodb").Collection<Agent>;
|
|
3
|
+
export declare const findAgents: (filter?: any) => Promise<import("mongodb").WithId<Agent>[]>;
|
|
4
|
+
export declare const findAgentById: (agentId: string) => Promise<import("mongodb").WithId<Agent> | null>;
|
|
5
|
+
export declare const findAgentsByClientId: (clientId: string) => Promise<import("mongodb").WithId<Agent>[]>;
|
|
6
|
+
export declare const createAgent: (agent: Agent) => Promise<import("mongodb").WithId<Agent> | null>;
|
|
7
|
+
export declare const updateAgent: (agentId: string, data: Partial<Agent>) => Promise<import("mongodb").WithId<Agent> | null>;
|
|
8
|
+
export declare const removeAgent: (agentId: string) => Promise<import("mongodb").DeleteResult>;
|
|
9
9
|
//# sourceMappingURL=agents.getters.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agents.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/agents/agents.getters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,EAAY,MAAM,UAAU,CAAC;AAElD,eAAO,MAAM,mBAAmB,
|
|
1
|
+
{"version":3,"file":"agents.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/agents/agents.getters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,EAAY,MAAM,UAAU,CAAC;AAElD,eAAO,MAAM,mBAAmB,2CAE/B,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,SAAQ,GAAQ,+CAEhD,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,SAAS,MAAM,oDAE5C,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,UAAU,MAAM,+CAEpD,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,OAAO,KAAK,oDAG7C,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,SAAS,MAAM,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,oDAMhE,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,SAAS,MAAM,4CAE1C,CAAC"}
|
|
@@ -3,7 +3,7 @@ import { Collection, InsertOneResult } from 'mongodb';
|
|
|
3
3
|
declare const getClientAudioBuffersCollection: () => Collection<ClientAudioBufferType>;
|
|
4
4
|
declare const insertClientAudioBuffer: (audioBufferInput: CreateAudioBufferInput) => Promise<InsertOneResult<ClientAudioBufferType>>;
|
|
5
5
|
declare const findClientAudioBuffersByCallSidAndRange: (callSid: string, startMs?: number, endMs?: number) => Promise<ClientAudioBufferDoc[]>;
|
|
6
|
-
declare const getAudioChunksByCallSid: (callSid: string) => Promise<Buffer[]>;
|
|
7
|
-
declare const getMergedAudioByCallSid: (callSid: string) => Promise<Buffer>;
|
|
6
|
+
declare const getAudioChunksByCallSid: (callSid: string, limit?: number) => Promise<Buffer[]>;
|
|
7
|
+
declare const getMergedAudioByCallSid: (callSid: string, limit: number) => Promise<Buffer>;
|
|
8
8
|
export { insertClientAudioBuffer, getClientAudioBuffersCollection, findClientAudioBuffersByCallSidAndRange, getAudioChunksByCallSid, getMergedAudioByCallSid, };
|
|
9
9
|
//# sourceMappingURL=clientAudioBuffer.getters.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clientAudioBuffer.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEtD,QAAA,MAAM,+BAA+B,QAAO,UAAU,CAAC,qBAAqB,CAE3E,CAAC;AAEF,QAAA,MAAM,uBAAuB,GAC3B,kBAAkB,sBAAsB,KACvC,OAAO,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAsBhD,CAAC;AAEF,QAAA,MAAM,uCAAuC,GAC3C,SAAS,MAAM,EACf,UAAU,MAAM,EAChB,QAAQ,MAAM,KACb,OAAO,CAAC,oBAAoB,EAAE,CAehC,CAAC;AAEF,QAAA,MAAM,uBAAuB,GAAU,SAAS,MAAM,KAAG,OAAO,CAAC,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"clientAudioBuffer.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEtD,QAAA,MAAM,+BAA+B,QAAO,UAAU,CAAC,qBAAqB,CAE3E,CAAC;AAEF,QAAA,MAAM,uBAAuB,GAC3B,kBAAkB,sBAAsB,KACvC,OAAO,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAsBhD,CAAC;AAEF,QAAA,MAAM,uCAAuC,GAC3C,SAAS,MAAM,EACf,UAAU,MAAM,EAChB,QAAQ,MAAM,KACb,OAAO,CAAC,oBAAoB,EAAE,CAehC,CAAC;AAEF,QAAA,MAAM,uBAAuB,GAAU,SAAS,MAAM,EAAE,QAAQ,MAAM,KAAG,OAAO,CAAC,MAAM,EAAE,CAqBxF,CAAC;AAEF,QAAA,MAAM,uBAAuB,GAAU,SAAS,MAAM,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,MAAM,CAQrF,CAAC;AAEF,OAAO,EACL,uBAAuB,EACvB,+BAA+B,EAC/B,uCAAuC,EACvC,uBAAuB,EACvB,uBAAuB,GACxB,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getMergedAudioByCallSid = exports.getAudioChunksByCallSid = exports.findClientAudioBuffersByCallSidAndRange = exports.getClientAudioBuffersCollection = exports.insertClientAudioBuffer = void 0;
|
|
4
|
-
const
|
|
4
|
+
const index_1 = require("../index");
|
|
5
5
|
const getClientAudioBuffersCollection = () => {
|
|
6
|
-
return (0,
|
|
6
|
+
return (0, index_1.getDb)().collection('clientAudioBuffers');
|
|
7
7
|
};
|
|
8
8
|
exports.getClientAudioBuffersCollection = getClientAudioBuffersCollection;
|
|
9
9
|
const insertClientAudioBuffer = async (audioBufferInput) => {
|
|
@@ -46,27 +46,31 @@ const findClientAudioBuffersByCallSidAndRange = async (callSid, startMs, endMs)
|
|
|
46
46
|
.toArray();
|
|
47
47
|
};
|
|
48
48
|
exports.findClientAudioBuffersByCallSidAndRange = findClientAudioBuffersByCallSidAndRange;
|
|
49
|
-
const getAudioChunksByCallSid = async (callSid) => {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
const getAudioChunksByCallSid = async (callSid, limit) => {
|
|
50
|
+
const query = getClientAudioBuffersCollection().find({ callSid });
|
|
51
|
+
if (limit) {
|
|
52
|
+
query.sort({ bucketStartMs: -1 }).limit(limit);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
query.sort({ bucketStartMs: 1 });
|
|
56
|
+
}
|
|
57
|
+
const docs = await query.toArray();
|
|
58
|
+
if (!docs.length) {
|
|
53
59
|
return [];
|
|
54
60
|
}
|
|
55
|
-
|
|
56
|
-
return
|
|
61
|
+
const orderedDocs = limit ? docs.reverse() : docs;
|
|
62
|
+
return orderedDocs.map((chunk) => {
|
|
57
63
|
const data = chunk.data;
|
|
58
|
-
if (Buffer.isBuffer(data))
|
|
64
|
+
if (Buffer.isBuffer(data))
|
|
59
65
|
return data;
|
|
60
|
-
|
|
61
|
-
if (data?.buffer) {
|
|
66
|
+
if (data?.buffer)
|
|
62
67
|
return Buffer.from(data.buffer);
|
|
63
|
-
}
|
|
64
68
|
return Buffer.from(data);
|
|
65
69
|
});
|
|
66
70
|
};
|
|
67
71
|
exports.getAudioChunksByCallSid = getAudioChunksByCallSid;
|
|
68
|
-
const getMergedAudioByCallSid = async (callSid) => {
|
|
69
|
-
const buffers = await getAudioChunksByCallSid(callSid);
|
|
72
|
+
const getMergedAudioByCallSid = async (callSid, limit) => {
|
|
73
|
+
const buffers = await getAudioChunksByCallSid(callSid, limit);
|
|
70
74
|
if (!buffers.length) {
|
|
71
75
|
return Buffer.alloc(0);
|
|
72
76
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clientAudioBuffer.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"clientAudioBuffer.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts"],"names":[],"mappings":"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"}
|
|
@@ -2,7 +2,7 @@ import { Group } from '../index';
|
|
|
2
2
|
import { Collection, Filter } from 'mongodb';
|
|
3
3
|
export declare const getGroupsCollection: () => Collection<Group>;
|
|
4
4
|
export declare const findGroups: (filter: Filter<Group>) => Promise<Group[]>;
|
|
5
|
-
export declare const createGroup: (group: Group) => Promise<import("
|
|
5
|
+
export declare const createGroup: (group: Group) => Promise<import("bson").ObjectId>;
|
|
6
6
|
export declare const updateGroup: (filter: Filter<Group>, data: Partial<Group>) => Promise<import("mongodb").WithId<Group> | null>;
|
|
7
7
|
export declare const removeGroup: (groupId: string) => Promise<import("mongodb").DeleteResult>;
|
|
8
8
|
//# sourceMappingURL=groups.getters.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"groups.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/groups/groups.getters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,mBAAmB,QAAO,UAAU,CAAC,KAAK,CAEtD,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,QAAQ,MAAM,CAAC,KAAK,CAAC,KAAG,OAAO,CAAC,KAAK,EAAE,CAEvE,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"groups.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/groups/groups.getters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,mBAAmB,QAAO,UAAU,CAAC,KAAK,CAEtD,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,QAAQ,MAAM,CAAC,KAAK,CAAC,KAAG,OAAO,CAAC,KAAK,EAAE,CAEvE,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,OAAO,KAAK,qCAG7C,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,QAAQ,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,oDAM5E,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,SAAS,MAAM,4CAEhD,CAAC"}
|
package/jest.config.js
CHANGED
package/package.json
CHANGED
|
@@ -3,8 +3,9 @@ import {
|
|
|
3
3
|
insertClientAudioBuffer,
|
|
4
4
|
findClientAudioBuffersByCallSidAndRange,
|
|
5
5
|
getMergedAudioByCallSid,
|
|
6
|
+
getAudioChunksByCallSid,
|
|
6
7
|
} from '../clientAudioBuffer.getters';
|
|
7
|
-
import { ObjectId } from '
|
|
8
|
+
import { ObjectId } from '../../index';
|
|
8
9
|
import { createAudioBufferInput } from '../../../test-utils/factories';
|
|
9
10
|
|
|
10
11
|
describe('clientAudioBuffers getters', () => {
|
|
@@ -59,20 +60,67 @@ describe('clientAudioBuffers getters', () => {
|
|
|
59
60
|
const thirdBuffer = Buffer.from([4, 5]);
|
|
60
61
|
|
|
61
62
|
await insertClientAudioBuffer(
|
|
62
|
-
|
|
63
|
+
createAudioBufferInput({ callSid, bucketStartMs: 1000, data: secondBuffer })
|
|
63
64
|
);
|
|
64
65
|
await insertClientAudioBuffer(
|
|
65
|
-
|
|
66
|
+
createAudioBufferInput({ callSid, bucketStartMs: 500, data: firstBuffer })
|
|
66
67
|
);
|
|
67
68
|
await insertClientAudioBuffer(
|
|
68
|
-
|
|
69
|
+
createAudioBufferInput({ callSid, bucketStartMs: 3000, data: thirdBuffer })
|
|
69
70
|
);
|
|
70
71
|
|
|
71
|
-
const merged = await getMergedAudioByCallSid(callSid);
|
|
72
|
+
const merged = await getMergedAudioByCallSid(callSid, 10);
|
|
72
73
|
const expected = Buffer.concat([firstBuffer, secondBuffer, thirdBuffer]);
|
|
73
74
|
|
|
74
75
|
expect(merged).toBeInstanceOf(Buffer);
|
|
75
76
|
expect(merged.length).toBe(expected.length);
|
|
76
77
|
expect(merged.equals(expected)).toBe(true);
|
|
77
78
|
});
|
|
79
|
+
|
|
80
|
+
it('should return all audio chunks when less than or equal to limit buffers exist', async () => {
|
|
81
|
+
const callSid = 'CA1234567890abcdef1234567890abclt';
|
|
82
|
+
|
|
83
|
+
const limit = 30;
|
|
84
|
+
const buffers: Buffer[] = [];
|
|
85
|
+
for (let i = 0; i < limit; i++) {
|
|
86
|
+
const buf = Buffer.from([i]);
|
|
87
|
+
buffers.push(buf);
|
|
88
|
+
await insertClientAudioBuffer(
|
|
89
|
+
createAudioBufferInput({
|
|
90
|
+
callSid,
|
|
91
|
+
bucketStartMs: 1000 + i * 1000,
|
|
92
|
+
data: buf,
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const chunks = await getAudioChunksByCallSid(callSid, limit);
|
|
98
|
+
|
|
99
|
+
expect(chunks).toHaveLength(buffers.length);
|
|
100
|
+
expect(chunks.every((chunk) => chunk instanceof Buffer)).toBe(true);
|
|
101
|
+
expect(Buffer.concat(chunks).equals(Buffer.concat(buffers))).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should return only the last N audio chunks when more than limit buffers exist', async () => {
|
|
105
|
+
const callSid = 'CA1234567890abcdef1234567890abclm';
|
|
106
|
+
|
|
107
|
+
const limit = 30;
|
|
108
|
+
const buffers: Buffer[] = [];
|
|
109
|
+
for (let i = 0; i < 35; i++) {
|
|
110
|
+
const buf = Buffer.from([i]);
|
|
111
|
+
buffers.push(buf);
|
|
112
|
+
await insertClientAudioBuffer(
|
|
113
|
+
createAudioBufferInput({
|
|
114
|
+
callSid,
|
|
115
|
+
bucketStartMs: 1000 + i * 1000,
|
|
116
|
+
data: buf,
|
|
117
|
+
})
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const chunks = await getAudioChunksByCallSid(callSid, limit);
|
|
122
|
+
const expectedBuffers = buffers.slice(-limit);
|
|
123
|
+
expect(chunks).toHaveLength(expectedBuffers.length);
|
|
124
|
+
expect(Buffer.concat(chunks).equals(Buffer.concat(expectedBuffers))).toBe(true);
|
|
125
|
+
});
|
|
78
126
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getDb } from '
|
|
1
|
+
import { getDb } from '../index';
|
|
2
2
|
import {
|
|
3
3
|
ClientAudioBufferDoc,
|
|
4
4
|
ClientAudioBufferType,
|
|
@@ -57,33 +57,31 @@ const findClientAudioBuffersByCallSidAndRange = async (
|
|
|
57
57
|
.toArray();
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
const getAudioChunksByCallSid = async (callSid: string): Promise<Buffer[]> => {
|
|
61
|
-
const
|
|
62
|
-
|
|
60
|
+
const getAudioChunksByCallSid = async (callSid: string, limit?: number): Promise<Buffer[]> => {
|
|
61
|
+
const query = getClientAudioBuffersCollection().find({ callSid });
|
|
62
|
+
if (limit) {
|
|
63
|
+
query.sort({ bucketStartMs: -1 }).limit(limit);
|
|
64
|
+
} else {
|
|
65
|
+
query.sort({ bucketStartMs: 1 });
|
|
66
|
+
}
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
const docs = await query.toArray();
|
|
69
|
+
|
|
70
|
+
if (!docs.length) {
|
|
65
71
|
return [];
|
|
66
72
|
}
|
|
73
|
+
const orderedDocs = limit ? docs.reverse() : docs;
|
|
67
74
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return chunks.map((chunk: any) => {
|
|
75
|
+
return orderedDocs.map((chunk) => {
|
|
71
76
|
const data: any = chunk.data;
|
|
72
|
-
|
|
73
|
-
if (Buffer.
|
|
74
|
-
return data;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (data?.buffer) {
|
|
78
|
-
return Buffer.from(data.buffer);
|
|
79
|
-
}
|
|
80
|
-
|
|
77
|
+
if (Buffer.isBuffer(data)) return data;
|
|
78
|
+
if (data?.buffer) return Buffer.from(data.buffer);
|
|
81
79
|
return Buffer.from(data);
|
|
82
80
|
});
|
|
83
81
|
};
|
|
84
82
|
|
|
85
|
-
const getMergedAudioByCallSid = async (callSid: string): Promise<Buffer> => {
|
|
86
|
-
const buffers = await getAudioChunksByCallSid(callSid);
|
|
83
|
+
const getMergedAudioByCallSid = async (callSid: string, limit: number): Promise<Buffer> => {
|
|
84
|
+
const buffers = await getAudioChunksByCallSid(callSid, limit);
|
|
87
85
|
|
|
88
86
|
if (!buffers.length) {
|
|
89
87
|
return Buffer.alloc(0);
|
package/tsconfig.json
CHANGED
|
@@ -16,11 +16,7 @@
|
|
|
16
16
|
"resolveJsonModule": true,
|
|
17
17
|
"experimentalDecorators": true,
|
|
18
18
|
"emitDecoratorMetadata": true,
|
|
19
|
-
"baseUrl": "."
|
|
20
|
-
"paths": {
|
|
21
|
-
"@core/db": ["src/index.ts"],
|
|
22
|
-
"*": ["node_modules/*"]
|
|
23
|
-
}
|
|
19
|
+
"baseUrl": "."
|
|
24
20
|
},
|
|
25
21
|
"include": ["src/**/*"],
|
|
26
22
|
"exclude": ["node_modules", "dist", "test", "**/*.test.ts", "**/__tests__/*", "**/*.spec.ts"]
|