Skip to content

Commit 5e9be67

Browse files
authored
New Components - zep (#15616)
* zep init * remove components (documents endpoints deprecated) * new components * pnpm-lock.yaml * updates * make userId required
1 parent 5300065 commit 5e9be67

File tree

12 files changed

+586
-6
lines changed

12 files changed

+586
-6
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import zep from "../../zep.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "zep-add-memory",
6+
name: "Add Memory to Session",
7+
description: "Adds memory to an existing session in Zep. [See the documentation](https://help.getzep.com/api-reference/memory/add)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
zep,
12+
sessionId: {
13+
propDefinition: [
14+
zep,
15+
"sessionId",
16+
],
17+
},
18+
messages: {
19+
type: "string[]",
20+
label: "Messages",
21+
description: "An array of message objects, where each message contains a role (`norole`, `system`, `assistant`, `user`, `function`, or `tool`) and content. Example: `[{ \"content\": \"content\", \"role_type\": \"norole\" }]` [See the documentation](https://help.getzep.com/api-reference/memory/add) for more information",
22+
},
23+
factInstruction: {
24+
type: "string",
25+
label: "Fact Instruction",
26+
description: "Additional instruction for generating the facts",
27+
optional: true,
28+
},
29+
returnContext: {
30+
type: "boolean",
31+
label: "Return Context",
32+
description: "Optionally return memory context relevant to the most recent messages",
33+
optional: true,
34+
},
35+
summaryInstruction: {
36+
type: "string",
37+
label: "Summary Instructions",
38+
description: "Additional instruction for generating the summary",
39+
optional: true,
40+
},
41+
},
42+
async run({ $ }) {
43+
const response = await this.zep.addMemoryToSession({
44+
sessionId: this.sessionId,
45+
data: {
46+
messages: utils.parseArray(this.messages),
47+
factInstruction: this.factInstruction,
48+
returnContext: this.returnContext,
49+
summaryInstruction: this.summaryInstruction,
50+
},
51+
});
52+
$.export("$summary", `Added memory to session ${this.sessionId}`);
53+
return response;
54+
},
55+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
userId: {
13+
type: "string",
14+
label: "User ID",
15+
description: "The unique identifier of the new user",
16+
},
17+
email: {
18+
type: "string",
19+
label: "Email",
20+
description: "Email address of the user",
21+
optional: true,
22+
},
23+
firstName: {
24+
type: "string",
25+
label: "First Name",
26+
description: "First name of the new user",
27+
optional: true,
28+
},
29+
lastName: {
30+
type: "string",
31+
label: "Last Name",
32+
description: "Last name of the new user",
33+
optional: true,
34+
},
35+
factRatingInstructions: {
36+
propDefinition: [
37+
zep,
38+
"factRatingInstructions",
39+
],
40+
},
41+
metadata: {
42+
propDefinition: [
43+
zep,
44+
"metadata",
45+
],
46+
optional: true,
47+
},
48+
},
49+
async run({ $ }) {
50+
const response = await this.zep.createUser({
51+
$,
52+
data: {
53+
email: this.email,
54+
first_name: this.firstName,
55+
last_name: this.lastName,
56+
fact_rating_instructions: utils.parseObject(this.factRatingInstructions),
57+
metadata: utils.parseObject(this.metadata),
58+
user_id: this.userId,
59+
},
60+
});
61+
$.export("$summary", `Successfully added user with ID: ${response.id}`);
62+
return response;
63+
},
64+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import zep from "../../zep.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "zep-create-session",
6+
name: "Create Session",
7+
description: "Creates a new session in Zep. [See the documentation](https://help.getzep.com/api-reference/memory/add-session)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
zep,
12+
sessionId: {
13+
type: "string",
14+
label: "Session ID",
15+
description: "The unique identifier of the session",
16+
},
17+
userId: {
18+
propDefinition: [
19+
zep,
20+
"userId",
21+
],
22+
},
23+
factRatingInstructions: {
24+
propDefinition: [
25+
zep,
26+
"factRatingInstructions",
27+
],
28+
},
29+
metadata: {
30+
propDefinition: [
31+
zep,
32+
"metadata",
33+
],
34+
optional: true,
35+
},
36+
},
37+
async run({ $ }) {
38+
const response = await this.zep.createSession({
39+
$,
40+
data: {
41+
session_id: this.sessionId,
42+
user_id: this.userId,
43+
fact_rating_instructions: utils.parseObject(this.factRatingInstructions),
44+
metadata: utils.parseObject(this.metadata),
45+
},
46+
});
47+
$.export("$summary", `Created session with ID ${this.sessionId}`);
48+
return response;
49+
},
50+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import zep from "../../zep.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "zep-update-session",
6+
name: "Update Session",
7+
description: "Updates an existing session in Zep. [See the documentation](https://help.getzep.com/api-reference/memory/update-session)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
zep,
12+
sessionId: {
13+
propDefinition: [
14+
zep,
15+
"sessionId",
16+
],
17+
},
18+
metadata: {
19+
propDefinition: [
20+
zep,
21+
"metadata",
22+
],
23+
description: "An object of key/value pairs representing the metadata to add to the session",
24+
},
25+
factRatingInstructions: {
26+
propDefinition: [
27+
zep,
28+
"factRatingInstructions",
29+
],
30+
},
31+
},
32+
async run({ $ }) {
33+
const response = await this.zep.updateSession({
34+
$,
35+
sessionId: this.sessionId,
36+
data: {
37+
fact_rating_instructions: utils.parseObject(this.factRatingInstructions),
38+
metadata: utils.parseObject(this.metadata),
39+
},
40+
});
41+
$.export("$summary", `Successfully updated ssession with ID ${this.sessionId}`);
42+
return response;
43+
},
44+
};

components/zep/common/utils.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function parseObject(obj) {
2+
if (!obj) {
3+
return undefined;
4+
}
5+
6+
return typeof obj === "string"
7+
? JSON.parse(obj)
8+
: obj;
9+
}
10+
11+
function parseArray(arr) {
12+
if (!arr) {
13+
return undefined;
14+
}
15+
16+
if (typeof arr === "string") {
17+
return JSON.parse(arr);
18+
}
19+
20+
if (Array.isArray(arr)) {
21+
return arr.map((item) => parseObject(item));
22+
}
23+
24+
return arr;
25+
}
26+
27+
export default {
28+
parseObject,
29+
parseArray,
30+
};

components/zep/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/zep",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Zep Components",
55
"main": "zep.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
1518
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import zep from "../../zep.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
props: {
6+
zep,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getLastTs() {
17+
return this.db.get("lastTs") || 0;
18+
},
19+
_setLastTs(lastTs) {
20+
this.db.set("lastTs", lastTs);
21+
},
22+
async processEvent(max) {
23+
const lastTs = this._getLastTs();
24+
const results = await this.getNewResults(lastTs, max);
25+
results.forEach((item) => this.emitEvent(item));
26+
},
27+
async getSessions({
28+
lastTs, orderBy, max, updateLastTs = true,
29+
}) {
30+
const params = {
31+
page_size: max || 1000,
32+
order_by: orderBy,
33+
asc: false,
34+
};
35+
36+
const { sessions: results } = await this.zep.listSessions({
37+
params,
38+
});
39+
40+
const sessions = [];
41+
for (const session of results) {
42+
const ts = Date.parse(session[orderBy]);
43+
if (ts >= lastTs) {
44+
sessions.push(session);
45+
} else {
46+
break;
47+
}
48+
if (max && sessions.length >= max) {
49+
break;
50+
}
51+
}
52+
53+
if (!sessions.length) {
54+
return [];
55+
}
56+
if (updateLastTs) {
57+
this._setLastTs(Date.parse(sessions[0][orderBy]));
58+
}
59+
return sessions.reverse();
60+
},
61+
emitEvent(item) {
62+
const meta = this.generateMeta(item);
63+
this.$emit(item, meta);
64+
},
65+
getNewResults() {
66+
throw new Error("getNewResults is not implemented");
67+
},
68+
generateMeta() {
69+
throw new Error("generateMeta is not implemented");
70+
},
71+
},
72+
hooks: {
73+
async deploy() {
74+
await this.processEvent(25);
75+
},
76+
},
77+
async run() {
78+
await this.processEvent();
79+
},
80+
};

0 commit comments

Comments
 (0)