Skip to content

Commit 889236e

Browse files
committed
updates
1 parent b7b1cf2 commit 889236e

File tree

4 files changed

+110
-24
lines changed

4 files changed

+110
-24
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import zep from "../../zep.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "zep-add-user",
6+
name: "Add User",
7+
description: "Adds a user in Zep. [See the documentation](https://help.getzep.com/api-reference/user/add)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
zep,
12+
email: {
13+
type: "string",
14+
label: "Email",
15+
description: "Email address of the user",
16+
optional: true,
17+
},
18+
firstName: {
19+
type: "string",
20+
label: "First Name",
21+
description: "First name of the new user",
22+
optional: true,
23+
},
24+
lastName: {
25+
type: "string",
26+
label: "Last Name",
27+
description: "Last name of the new user",
28+
optional: true,
29+
},
30+
factRatingInstructions: {
31+
propDefinition: [
32+
zep,
33+
"factRatingInstructions",
34+
],
35+
},
36+
metadata: {
37+
propDefinition: [
38+
zep,
39+
"metadata",
40+
],
41+
optional: true,
42+
},
43+
userId: {
44+
type: "string",
45+
label: "User ID",
46+
description: "The unique identifier of the new user",
47+
optional: true,
48+
},
49+
},
50+
async run({ $ }) {
51+
const response = await this.zep.createUser({
52+
$,
53+
data: {
54+
email: this.email,
55+
first_name: this.firstName,
56+
last_name: this.lastName,
57+
fact_rating_instructions: utils.parseObject(this.factRatingInstructions),
58+
metadata: utils.parseObject(this.metadata),
59+
user_id: this.userId,
60+
},
61+
});
62+
$.export("$summary", `Successfully added user with ID: ${response.id}`);
63+
return response;
64+
},
65+
};

components/zep/sources/common/base.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
results.forEach((item) => this.emitEvent(item));
2626
},
2727
async getSessions({
28-
lastTs, orderBy, max,
28+
lastTs, orderBy, max, updateLastTs = true,
2929
}) {
3030
const params = {
3131
page_size: max || 1000,
@@ -53,7 +53,9 @@ export default {
5353
if (!sessions.length) {
5454
return [];
5555
}
56-
this._setLastTs(Date.parse(sessions[0][orderBy]));
56+
if (updateLastTs) {
57+
this._setLastTs(Date.parse(sessions[0][orderBy]));
58+
}
5759
return sessions.reverse();
5860
},
5961
emitEvent(item) {

components/zep/sources/new-message/new-message.mjs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,49 @@ export default {
88
version: "0.0.1",
99
type: "source",
1010
dedupe: "unique",
11-
props: {
12-
...common.props,
13-
sessionId: {
14-
propDefinition: [
15-
common.props.zep,
16-
"sessionId",
17-
],
18-
},
19-
},
2011
methods: {
2112
...common.methods,
2213
async getNewResults(lastTs, max) {
23-
const results = this.paginateMessages();
24-
14+
const sessionIds = await this.getRecentlyUpdatedSessionIds(lastTs);
2515
let messages = [];
26-
for await (const message of results) {
27-
const ts = Date.parse(message.created_at);
28-
if (ts >= lastTs) {
29-
messages.push(message);
16+
let maxTs = lastTs;
17+
18+
for (const sessionId of sessionIds) {
19+
const results = this.paginateMessages(sessionId);
20+
21+
for await (const message of results) {
22+
const ts = Date.parse(message.created_at);
23+
if (ts >= lastTs) {
24+
messages.push({
25+
...message,
26+
session_id: sessionId,
27+
});
28+
maxTs = Math.max(maxTs, ts);
29+
}
3030
}
3131
}
3232

33-
if (messages.length) {
34-
this._setLastTs(Date.parse(messages[messages.length - 1].created_at));
35-
}
33+
this._setLastTs(maxTs);
34+
35+
// sort by created_at
36+
messages = messages.sort((a, b) => Date.parse(a.created_at) - Date.parse(b.created_at));
3637

3738
if (max) {
3839
messages = messages.slice(-1 * max);
3940
}
4041

4142
return messages;
4243
},
43-
async *paginateMessages() {
44+
async getRecentlyUpdatedSessionIds(lastTs) {
45+
const sessions = await this.getSessions({
46+
lastTs,
47+
orderBy: "updated_at",
48+
updateLastTs: false,
49+
max: 100,
50+
});
51+
return sessions?.map(({ session_id: id }) => id) || [];
52+
},
53+
async *paginateMessages(sessionId) {
4454
const params = {
4555
limit: 1000,
4656
cursor: 1,
@@ -49,7 +59,7 @@ export default {
4959

5060
do {
5161
const { messages } = await this.zep.listMessages({
52-
sessionId: this.sessionId,
62+
sessionId,
5363
params,
5464
});
5565
for (const message of messages) {

components/zep/zep.app.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ export default {
3030
},
3131
});
3232
return users?.map(({
33-
user_id: value, first_name: firstName, last_name: lastName,
33+
user_id: value, first_name: firstName, last_name: lastName, email,
3434
}) => ({
3535
value,
36-
label: (`${firstName} ${lastName}`).trim(),
36+
label: firstName || lastName
37+
? (`${firstName} ${lastName}`).trim()
38+
: email || value,
3739
})) || [];
3840
},
3941
},
@@ -93,6 +95,13 @@ export default {
9395
...opts,
9496
});
9597
},
98+
createUser(opts = {}) {
99+
return this._makeRequest({
100+
method: "POST",
101+
path: "/users",
102+
...opts,
103+
});
104+
},
96105
addMemoryToSession({
97106
sessionId, ...opts
98107
}) {

0 commit comments

Comments
 (0)