@taico/adk-session-store 0.4.1 → 0.5.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @taico/adk-session-store
2
2
 
3
- [![Socket Badge](https://badge.socket.dev/npm/package/@taico/adk-session-store/0.4.1)](https://badge.socket.dev/npm/package/@taico/adk-session-store/0.4.1)
3
+ [![Socket Badge](https://badge.socket.dev/npm/package/@taico/adk-session-store/0.5.1)](https://badge.socket.dev/npm/package/@taico/adk-session-store/0.5.1)
4
4
 
5
5
  PostgreSQL and SQLite session storage for Google ADK. Taico production uses
6
6
  `PostgresSessionService`; the SQLite implementation remains available to package
@@ -0,0 +1,8 @@
1
+ import { ListSessionsRequest, ListSessionsResponse, Session } from '@google/adk';
2
+ type Pagination = Pick<ListSessionsRequest, 'limit' | 'offset' | 'page' | 'order'>;
3
+ /**
4
+ * Applies the ordering and pagination contract of ADK's `listSessions`
5
+ * (matching `InMemorySessionService`) to an already-fetched list of sessions.
6
+ */
7
+ export declare function paginateSessions(sessions: Session[], { limit, offset, page, order }: Pagination): ListSessionsResponse;
8
+ export {};
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Applies the ordering and pagination contract of ADK's `listSessions`
3
+ * (matching `InMemorySessionService`) to an already-fetched list of sessions.
4
+ */
5
+ export function paginateSessions(sessions, { limit, offset, page, order }) {
6
+ const all = [...sessions];
7
+ if (order === 'asc') {
8
+ all.sort((a, b) => a.lastUpdateTime - b.lastUpdateTime || a.id.localeCompare(b.id));
9
+ }
10
+ else if (order === 'desc') {
11
+ all.sort((a, b) => b.lastUpdateTime - a.lastUpdateTime || a.id.localeCompare(b.id));
12
+ }
13
+ const totalItems = all.length;
14
+ if (limit === undefined) {
15
+ return {
16
+ sessions: offset ? all.slice(offset) : all,
17
+ page: 1,
18
+ limit: totalItems,
19
+ totalItems,
20
+ totalPages: totalItems === 0 ? 0 : 1,
21
+ };
22
+ }
23
+ const effectiveOffset = page !== undefined ? (page - 1) * limit : offset ?? 0;
24
+ const effectivePage = page !== undefined ? page : limit === 0 ? 1 : Math.floor(effectiveOffset / limit) + 1;
25
+ return {
26
+ sessions: all.slice(effectiveOffset, effectiveOffset + limit),
27
+ page: effectivePage,
28
+ limit,
29
+ totalItems,
30
+ totalPages: limit === 0 ? 0 : Math.ceil(totalItems / limit),
31
+ };
32
+ }
@@ -9,7 +9,7 @@ export declare class PostgresSessionService extends BaseSessionService {
9
9
  constructor(options: PostgresSessionServiceOptions);
10
10
  createSession({ appName, userId, state, sessionId, }: CreateSessionRequest): Promise<Session>;
11
11
  getSession({ appName, userId, sessionId, config, }: GetSessionRequest): Promise<Session | undefined>;
12
- listSessions({ appName, userId, }: ListSessionsRequest): Promise<ListSessionsResponse>;
12
+ listSessions({ appName, userId, ...pagination }: ListSessionsRequest): Promise<ListSessionsResponse>;
13
13
  deleteSession({ appName, userId, sessionId, }: DeleteSessionRequest): Promise<void>;
14
14
  appendEvent({ session, event }: AppendEventRequest): Promise<Event>;
15
15
  close(): Promise<void>;
@@ -1,6 +1,7 @@
1
1
  import { BaseSessionService, State, createSession, } from "@google/adk";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { Pool } from "pg";
4
+ import { paginateSessions } from "./paginate-sessions.js";
4
5
  export class PostgresSessionService extends BaseSessionService {
5
6
  pool;
6
7
  ready;
@@ -62,20 +63,18 @@ export class PostgresSessionService extends BaseSessionService {
62
63
  lastUpdateTime: Number(sessionRow.last_update_time),
63
64
  }));
64
65
  }
65
- async listSessions({ appName, userId, }) {
66
+ async listSessions({ appName, userId, ...pagination }) {
66
67
  await this.ready;
67
68
  const rows = await this.many(`SELECT app_name, user_id, session_id, state_json, last_update_time
68
- FROM adk_sessions WHERE app_name = $1 AND user_id = $2`, [appName, userId]);
69
- return {
70
- sessions: rows.map((row) => createSession({
71
- id: row.session_id,
72
- appName: row.app_name,
73
- userId: row.user_id,
74
- state: {},
75
- events: [],
76
- lastUpdateTime: Number(row.last_update_time),
77
- })),
78
- };
69
+ FROM adk_sessions WHERE app_name = $1${userId === undefined ? "" : " AND user_id = $2"}`, userId === undefined ? [appName] : [appName, userId]);
70
+ return paginateSessions(rows.map((row) => createSession({
71
+ id: row.session_id,
72
+ appName: row.app_name,
73
+ userId: row.user_id,
74
+ state: {},
75
+ events: [],
76
+ lastUpdateTime: Number(row.last_update_time),
77
+ })), pagination);
79
78
  }
80
79
  async deleteSession({ appName, userId, sessionId, }) {
81
80
  await this.ready;
@@ -8,7 +8,7 @@ export declare class SqliteSessionService extends BaseSessionService {
8
8
  constructor(options?: SqliteSessionServiceOptions);
9
9
  createSession({ appName, userId, state, sessionId }: CreateSessionRequest): Promise<Session>;
10
10
  getSession({ appName, userId, sessionId, config }: GetSessionRequest): Promise<Session | undefined>;
11
- listSessions({ appName, userId }: ListSessionsRequest): Promise<ListSessionsResponse>;
11
+ listSessions({ appName, userId, ...pagination }: ListSessionsRequest): Promise<ListSessionsResponse>;
12
12
  deleteSession({ appName, userId, sessionId }: DeleteSessionRequest): Promise<void>;
13
13
  appendEvent({ session, event }: AppendEventRequest): Promise<Event>;
14
14
  close(): Promise<void>;
@@ -1,5 +1,6 @@
1
1
  import { BaseSessionService, State, createSession, } from '@google/adk';
2
2
  import * as sqlite3 from 'sqlite3';
3
+ import { paginateSessions } from './paginate-sessions.js';
3
4
  import { randomUUID } from 'node:crypto';
4
5
  const sqlite3Module = sqlite3.default ?? sqlite3;
5
6
  export class SqliteSessionService extends BaseSessionService {
@@ -56,19 +57,19 @@ export class SqliteSessionService extends BaseSessionService {
56
57
  });
57
58
  return this.mergeState(session);
58
59
  }
59
- async listSessions({ appName, userId }) {
60
+ async listSessions({ appName, userId, ...pagination }) {
60
61
  await this.ready;
61
- const rows = await this.all('SELECT app_name, user_id, session_id, last_update_time FROM sessions WHERE app_name = ? AND user_id = ?', [appName, userId]);
62
- return {
63
- sessions: rows.map((row) => createSession({
64
- id: row.session_id,
65
- appName: row.app_name,
66
- userId: row.user_id,
67
- state: {},
68
- events: [],
69
- lastUpdateTime: row.last_update_time,
70
- })),
71
- };
62
+ const rows = await this.all(userId === undefined
63
+ ? 'SELECT app_name, user_id, session_id, last_update_time FROM sessions WHERE app_name = ?'
64
+ : 'SELECT app_name, user_id, session_id, last_update_time FROM sessions WHERE app_name = ? AND user_id = ?', userId === undefined ? [appName] : [appName, userId]);
65
+ return paginateSessions(rows.map((row) => createSession({
66
+ id: row.session_id,
67
+ appName: row.app_name,
68
+ userId: row.user_id,
69
+ state: {},
70
+ events: [],
71
+ lastUpdateTime: row.last_update_time,
72
+ })), pagination);
72
73
  }
73
74
  async deleteSession({ appName, userId, sessionId }) {
74
75
  await this.ready;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taico/adk-session-store",
3
- "version": "0.4.1",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "license": "PolyForm-Small-Business-1.0.0",
@@ -27,9 +27,10 @@
27
27
  "release": "npm run build && npm publish --access public"
28
28
  },
29
29
  "dependencies": {
30
- "@google/adk": "^0.2.5",
30
+ "@google/adk": "^2.2.1",
31
31
  "pg": "^8.16.3",
32
- "sqlite3": "^5.1.7"
32
+ "sqlite3": "^6.0.1",
33
+ "uuid": "^11.1.1"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@types/pg": "^8.15.5",