Skip to content

Commit af13d14

Browse files
committed
new components
1 parent 5c1ebd6 commit af13d14

File tree

10 files changed

+480
-3
lines changed

10 files changed

+480
-3
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: 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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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,
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+
this._setLastTs(Date.parse(sessions[0][orderBy]));
57+
return sessions.reverse();
58+
},
59+
emitEvent(item) {
60+
const meta = this.generateMeta(item);
61+
this.$emit(item, meta);
62+
},
63+
getNewResults() {
64+
throw new Error("getNewResults is not implemented");
65+
},
66+
generateMeta() {
67+
throw new Error("generateMeta is not implemented");
68+
},
69+
},
70+
hooks: {
71+
async deploy() {
72+
await this.processEvent(25);
73+
},
74+
},
75+
async run() {
76+
await this.processEvent();
77+
},
78+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "zep-new-message",
6+
name: "New Message in Session",
7+
description: "Emit new event when a message is added to a session. [See the documentation](https://help.getzep.com/api-reference/memory/get-session-messages)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
sessionId: {
14+
propDefinition: [
15+
common.props.zep,
16+
"sessionId",
17+
],
18+
},
19+
},
20+
methods: {
21+
...common.methods,
22+
async getNewResults(lastTs, max) {
23+
const results = this.paginateMessages();
24+
25+
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);
30+
}
31+
}
32+
33+
if (messages.length) {
34+
this._setLastTs(Date.parse(messages[messages.length - 1].created_at));
35+
}
36+
37+
if (max) {
38+
messages = messages.slice(-1 * max);
39+
}
40+
41+
return messages;
42+
},
43+
async *paginateMessages() {
44+
const params = {
45+
limit: 1000,
46+
cursor: 1,
47+
};
48+
let total;
49+
50+
do {
51+
const { messages } = await this.zep.listMessages({
52+
sessionId: this.sessionId,
53+
params,
54+
});
55+
for (const message of messages) {
56+
yield message;
57+
}
58+
total = messages?.length;
59+
params.cursor++;
60+
} while (total);
61+
},
62+
generateMeta(message) {
63+
return {
64+
id: message.uuid,
65+
summary: `New Message: ${message.content}`,
66+
ts: Date.parse(message.created_at),
67+
};
68+
},
69+
},
70+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "zep-new-session",
6+
name: "New Session Created",
7+
description: "Emit new event when a new session is created in Zep. [See the documentation](https://help.getzep.com/api-reference/memory/list-sessions)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getNewResults(lastTs, max) {
14+
return this.getSessions({
15+
lastTs,
16+
max,
17+
orderBy: "created_at",
18+
});
19+
},
20+
generateMeta(session) {
21+
return {
22+
id: session.session_id,
23+
summary: `New Session: ${session.session_id}`,
24+
ts: Date.parse(session.created_at),
25+
};
26+
},
27+
},
28+
};

0 commit comments

Comments
 (0)