@mastra/clickhouse 1.0.0-beta.10 → 1.0.0-beta.11

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/index.js CHANGED
@@ -1066,26 +1066,68 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1066
1066
  );
1067
1067
  }
1068
1068
  }
1069
- async listThreadsByResourceId(args) {
1070
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
1069
+ async listThreads(args) {
1070
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
1071
+ try {
1072
+ this.validatePaginationInput(page, perPageInput ?? 100);
1073
+ } catch (error) {
1074
+ throw new MastraError(
1075
+ {
1076
+ id: createStorageErrorId("CLICKHOUSE", "LIST_THREADS", "INVALID_PAGE"),
1077
+ domain: ErrorDomain.STORAGE,
1078
+ category: ErrorCategory.USER,
1079
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
1080
+ },
1081
+ error instanceof Error ? error : new Error("Invalid pagination parameters")
1082
+ );
1083
+ }
1071
1084
  const perPage = normalizePerPage(perPageInput, 100);
1072
- if (page < 0) {
1085
+ try {
1086
+ this.validateMetadataKeys(filter?.metadata);
1087
+ } catch (error) {
1073
1088
  throw new MastraError(
1074
1089
  {
1075
- id: createStorageErrorId("CLICKHOUSE", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
1090
+ id: createStorageErrorId("CLICKHOUSE", "LIST_THREADS", "INVALID_METADATA_KEY"),
1076
1091
  domain: ErrorDomain.STORAGE,
1077
1092
  category: ErrorCategory.USER,
1078
- details: { page }
1093
+ details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
1079
1094
  },
1080
- new Error("page must be >= 0")
1095
+ error instanceof Error ? error : new Error("Invalid metadata key")
1081
1096
  );
1082
1097
  }
1083
1098
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1084
1099
  const { field, direction } = this.parseOrderBy(orderBy);
1085
1100
  try {
1101
+ const whereClauses = [];
1102
+ const queryParams = {};
1103
+ if (filter?.resourceId) {
1104
+ whereClauses.push("resourceId = {resourceId:String}");
1105
+ queryParams.resourceId = filter.resourceId;
1106
+ }
1107
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
1108
+ let metadataIndex = 0;
1109
+ for (const [key, value] of Object.entries(filter.metadata)) {
1110
+ const paramName = `metadata${metadataIndex}`;
1111
+ whereClauses.push(`JSONExtractRaw(metadata, '${key}') = {${paramName}:String}`);
1112
+ queryParams[paramName] = JSON.stringify(value);
1113
+ metadataIndex++;
1114
+ }
1115
+ }
1086
1116
  const countResult = await this.client.query({
1087
- query: `SELECT count(DISTINCT id) as total FROM ${TABLE_THREADS} WHERE resourceId = {resourceId:String}`,
1088
- query_params: { resourceId },
1117
+ query: `
1118
+ WITH ranked_threads AS (
1119
+ SELECT
1120
+ id,
1121
+ resourceId,
1122
+ metadata,
1123
+ ROW_NUMBER() OVER (PARTITION BY id ORDER BY updatedAt DESC) as row_num
1124
+ FROM ${TABLE_THREADS}
1125
+ )
1126
+ SELECT count(*) as total
1127
+ FROM ranked_threads
1128
+ WHERE row_num = 1 ${whereClauses.length > 0 ? `AND ${whereClauses.join(" AND ")}` : ""}
1129
+ `,
1130
+ query_params: queryParams,
1089
1131
  clickhouse_settings: {
1090
1132
  date_time_input_format: "best_effort",
1091
1133
  date_time_output_format: "iso",
@@ -1116,7 +1158,6 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1116
1158
  toDateTime64(updatedAt, 3) as updatedAt,
1117
1159
  ROW_NUMBER() OVER (PARTITION BY id ORDER BY updatedAt DESC) as row_num
1118
1160
  FROM ${TABLE_THREADS}
1119
- WHERE resourceId = {resourceId:String}
1120
1161
  )
1121
1162
  SELECT
1122
1163
  id,
@@ -1126,12 +1167,12 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1126
1167
  createdAt,
1127
1168
  updatedAt
1128
1169
  FROM ranked_threads
1129
- WHERE row_num = 1
1170
+ WHERE row_num = 1 ${whereClauses.length > 0 ? `AND ${whereClauses.join(" AND ")}` : ""}
1130
1171
  ORDER BY "${field}" ${direction === "DESC" ? "DESC" : "ASC"}
1131
1172
  LIMIT {perPage:Int64} OFFSET {offset:Int64}
1132
1173
  `,
1133
1174
  query_params: {
1134
- resourceId,
1175
+ ...queryParams,
1135
1176
  perPage,
1136
1177
  offset
1137
1178
  },
@@ -1157,10 +1198,14 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1157
1198
  } catch (error) {
1158
1199
  throw new MastraError(
1159
1200
  {
1160
- id: createStorageErrorId("CLICKHOUSE", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
1201
+ id: createStorageErrorId("CLICKHOUSE", "LIST_THREADS", "FAILED"),
1161
1202
  domain: ErrorDomain.STORAGE,
1162
1203
  category: ErrorCategory.THIRD_PARTY,
1163
- details: { resourceId, page }
1204
+ details: {
1205
+ ...filter?.resourceId && { resourceId: filter.resourceId },
1206
+ hasMetadataFilter: !!filter?.metadata,
1207
+ page
1208
+ }
1164
1209
  },
1165
1210
  error
1166
1211
  );