@hebo-ai/gateway 0.9.0 → 0.9.2

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.
Files changed (212) hide show
  1. package/README.md +82 -4
  2. package/package.json +34 -34
  3. package/dist/config.d.ts +0 -2
  4. package/dist/config.js +0 -111
  5. package/dist/endpoints/chat-completions/converters.d.ts +0 -26
  6. package/dist/endpoints/chat-completions/converters.js +0 -524
  7. package/dist/endpoints/chat-completions/handler.d.ts +0 -2
  8. package/dist/endpoints/chat-completions/handler.js +0 -149
  9. package/dist/endpoints/chat-completions/index.d.ts +0 -4
  10. package/dist/endpoints/chat-completions/index.js +0 -4
  11. package/dist/endpoints/chat-completions/otel.d.ts +0 -5
  12. package/dist/endpoints/chat-completions/otel.js +0 -175
  13. package/dist/endpoints/chat-completions/schema.d.ts +0 -1170
  14. package/dist/endpoints/chat-completions/schema.js +0 -252
  15. package/dist/endpoints/conversations/converters.d.ts +0 -8
  16. package/dist/endpoints/conversations/converters.js +0 -29
  17. package/dist/endpoints/conversations/handler.d.ts +0 -2
  18. package/dist/endpoints/conversations/handler.js +0 -276
  19. package/dist/endpoints/conversations/index.d.ts +0 -3
  20. package/dist/endpoints/conversations/index.js +0 -3
  21. package/dist/endpoints/conversations/schema.d.ts +0 -1511
  22. package/dist/endpoints/conversations/schema.js +0 -74
  23. package/dist/endpoints/conversations/storage/dialects/greptime.d.ts +0 -10
  24. package/dist/endpoints/conversations/storage/dialects/greptime.js +0 -75
  25. package/dist/endpoints/conversations/storage/dialects/mysql.d.ts +0 -12
  26. package/dist/endpoints/conversations/storage/dialects/mysql.js +0 -118
  27. package/dist/endpoints/conversations/storage/dialects/postgres.d.ts +0 -16
  28. package/dist/endpoints/conversations/storage/dialects/postgres.js +0 -185
  29. package/dist/endpoints/conversations/storage/dialects/sqlite.d.ts +0 -11
  30. package/dist/endpoints/conversations/storage/dialects/sqlite.js +0 -176
  31. package/dist/endpoints/conversations/storage/dialects/types.d.ts +0 -42
  32. package/dist/endpoints/conversations/storage/dialects/types.js +0 -0
  33. package/dist/endpoints/conversations/storage/dialects/utils.d.ts +0 -25
  34. package/dist/endpoints/conversations/storage/dialects/utils.js +0 -80
  35. package/dist/endpoints/conversations/storage/memory.d.ts +0 -25
  36. package/dist/endpoints/conversations/storage/memory.js +0 -200
  37. package/dist/endpoints/conversations/storage/sql.d.ts +0 -33
  38. package/dist/endpoints/conversations/storage/sql.js +0 -278
  39. package/dist/endpoints/conversations/storage/types.d.ts +0 -39
  40. package/dist/endpoints/conversations/storage/types.js +0 -0
  41. package/dist/endpoints/embeddings/converters.d.ts +0 -10
  42. package/dist/endpoints/embeddings/converters.js +0 -31
  43. package/dist/endpoints/embeddings/handler.d.ts +0 -2
  44. package/dist/endpoints/embeddings/handler.js +0 -104
  45. package/dist/endpoints/embeddings/index.d.ts +0 -4
  46. package/dist/endpoints/embeddings/index.js +0 -4
  47. package/dist/endpoints/embeddings/otel.d.ts +0 -5
  48. package/dist/endpoints/embeddings/otel.js +0 -29
  49. package/dist/endpoints/embeddings/schema.d.ts +0 -44
  50. package/dist/endpoints/embeddings/schema.js +0 -29
  51. package/dist/endpoints/models/converters.d.ts +0 -6
  52. package/dist/endpoints/models/converters.js +0 -42
  53. package/dist/endpoints/models/handler.d.ts +0 -2
  54. package/dist/endpoints/models/handler.js +0 -29
  55. package/dist/endpoints/models/index.d.ts +0 -3
  56. package/dist/endpoints/models/index.js +0 -3
  57. package/dist/endpoints/models/schema.d.ts +0 -42
  58. package/dist/endpoints/models/schema.js +0 -31
  59. package/dist/endpoints/responses/converters.d.ts +0 -17
  60. package/dist/endpoints/responses/converters.js +0 -1034
  61. package/dist/endpoints/responses/handler.d.ts +0 -2
  62. package/dist/endpoints/responses/handler.js +0 -137
  63. package/dist/endpoints/responses/index.d.ts +0 -4
  64. package/dist/endpoints/responses/index.js +0 -4
  65. package/dist/endpoints/responses/otel.d.ts +0 -6
  66. package/dist/endpoints/responses/otel.js +0 -221
  67. package/dist/endpoints/responses/schema.d.ts +0 -2109
  68. package/dist/endpoints/responses/schema.js +0 -314
  69. package/dist/endpoints/shared/converters.d.ts +0 -55
  70. package/dist/endpoints/shared/converters.js +0 -179
  71. package/dist/endpoints/shared/schema.d.ts +0 -70
  72. package/dist/endpoints/shared/schema.js +0 -46
  73. package/dist/errors/ai-sdk.d.ts +0 -2
  74. package/dist/errors/ai-sdk.js +0 -52
  75. package/dist/errors/gateway.d.ts +0 -5
  76. package/dist/errors/gateway.js +0 -13
  77. package/dist/errors/openai.d.ts +0 -15
  78. package/dist/errors/openai.js +0 -40
  79. package/dist/errors/utils.d.ts +0 -22
  80. package/dist/errors/utils.js +0 -44
  81. package/dist/gateway.d.ts +0 -11
  82. package/dist/gateway.js +0 -44
  83. package/dist/index.d.ts +0 -11
  84. package/dist/index.js +0 -10
  85. package/dist/lifecycle.d.ts +0 -3
  86. package/dist/lifecycle.js +0 -113
  87. package/dist/logger/default.d.ts +0 -4
  88. package/dist/logger/default.js +0 -81
  89. package/dist/logger/index.d.ts +0 -11
  90. package/dist/logger/index.js +0 -25
  91. package/dist/middleware/common.d.ts +0 -12
  92. package/dist/middleware/common.js +0 -146
  93. package/dist/middleware/debug.d.ts +0 -3
  94. package/dist/middleware/debug.js +0 -27
  95. package/dist/middleware/matcher.d.ts +0 -28
  96. package/dist/middleware/matcher.js +0 -118
  97. package/dist/middleware/utils.d.ts +0 -2
  98. package/dist/middleware/utils.js +0 -24
  99. package/dist/models/amazon/index.d.ts +0 -2
  100. package/dist/models/amazon/index.js +0 -2
  101. package/dist/models/amazon/middleware.d.ts +0 -3
  102. package/dist/models/amazon/middleware.js +0 -68
  103. package/dist/models/amazon/presets.d.ts +0 -345
  104. package/dist/models/amazon/presets.js +0 -80
  105. package/dist/models/anthropic/index.d.ts +0 -2
  106. package/dist/models/anthropic/index.js +0 -2
  107. package/dist/models/anthropic/middleware.d.ts +0 -5
  108. package/dist/models/anthropic/middleware.js +0 -127
  109. package/dist/models/anthropic/presets.d.ts +0 -711
  110. package/dist/models/anthropic/presets.js +0 -135
  111. package/dist/models/catalog.d.ts +0 -4
  112. package/dist/models/catalog.js +0 -8
  113. package/dist/models/cohere/index.d.ts +0 -2
  114. package/dist/models/cohere/index.js +0 -2
  115. package/dist/models/cohere/middleware.d.ts +0 -3
  116. package/dist/models/cohere/middleware.js +0 -62
  117. package/dist/models/cohere/presets.d.ts +0 -411
  118. package/dist/models/cohere/presets.js +0 -134
  119. package/dist/models/google/index.d.ts +0 -2
  120. package/dist/models/google/index.js +0 -2
  121. package/dist/models/google/middleware.d.ts +0 -8
  122. package/dist/models/google/middleware.js +0 -111
  123. package/dist/models/google/presets.d.ts +0 -403
  124. package/dist/models/google/presets.js +0 -88
  125. package/dist/models/meta/index.d.ts +0 -1
  126. package/dist/models/meta/index.js +0 -1
  127. package/dist/models/meta/presets.d.ts +0 -483
  128. package/dist/models/meta/presets.js +0 -95
  129. package/dist/models/openai/index.d.ts +0 -2
  130. package/dist/models/openai/index.js +0 -2
  131. package/dist/models/openai/middleware.d.ts +0 -4
  132. package/dist/models/openai/middleware.js +0 -88
  133. package/dist/models/openai/presets.d.ts +0 -1319
  134. package/dist/models/openai/presets.js +0 -277
  135. package/dist/models/types.d.ts +0 -20
  136. package/dist/models/types.js +0 -92
  137. package/dist/models/voyage/index.d.ts +0 -2
  138. package/dist/models/voyage/index.js +0 -2
  139. package/dist/models/voyage/middleware.d.ts +0 -2
  140. package/dist/models/voyage/middleware.js +0 -19
  141. package/dist/models/voyage/presets.d.ts +0 -436
  142. package/dist/models/voyage/presets.js +0 -85
  143. package/dist/providers/anthropic/canonical.d.ts +0 -3
  144. package/dist/providers/anthropic/canonical.js +0 -9
  145. package/dist/providers/anthropic/index.d.ts +0 -1
  146. package/dist/providers/anthropic/index.js +0 -1
  147. package/dist/providers/bedrock/canonical.d.ts +0 -17
  148. package/dist/providers/bedrock/canonical.js +0 -61
  149. package/dist/providers/bedrock/index.d.ts +0 -2
  150. package/dist/providers/bedrock/index.js +0 -2
  151. package/dist/providers/bedrock/middleware.d.ts +0 -5
  152. package/dist/providers/bedrock/middleware.js +0 -137
  153. package/dist/providers/cohere/canonical.d.ts +0 -3
  154. package/dist/providers/cohere/canonical.js +0 -17
  155. package/dist/providers/cohere/index.d.ts +0 -1
  156. package/dist/providers/cohere/index.js +0 -1
  157. package/dist/providers/groq/canonical.d.ts +0 -3
  158. package/dist/providers/groq/canonical.js +0 -12
  159. package/dist/providers/groq/index.d.ts +0 -2
  160. package/dist/providers/groq/index.js +0 -2
  161. package/dist/providers/groq/middleware.d.ts +0 -2
  162. package/dist/providers/groq/middleware.js +0 -31
  163. package/dist/providers/openai/canonical.d.ts +0 -3
  164. package/dist/providers/openai/canonical.js +0 -8
  165. package/dist/providers/openai/index.d.ts +0 -1
  166. package/dist/providers/openai/index.js +0 -1
  167. package/dist/providers/registry.d.ts +0 -24
  168. package/dist/providers/registry.js +0 -103
  169. package/dist/providers/types.d.ts +0 -7
  170. package/dist/providers/types.js +0 -11
  171. package/dist/providers/vertex/canonical.d.ts +0 -3
  172. package/dist/providers/vertex/canonical.js +0 -8
  173. package/dist/providers/vertex/index.d.ts +0 -2
  174. package/dist/providers/vertex/index.js +0 -2
  175. package/dist/providers/vertex/middleware.d.ts +0 -2
  176. package/dist/providers/vertex/middleware.js +0 -47
  177. package/dist/providers/voyage/canonical.d.ts +0 -3
  178. package/dist/providers/voyage/canonical.js +0 -7
  179. package/dist/providers/voyage/index.d.ts +0 -1
  180. package/dist/providers/voyage/index.js +0 -1
  181. package/dist/telemetry/ai-sdk.d.ts +0 -2
  182. package/dist/telemetry/ai-sdk.js +0 -31
  183. package/dist/telemetry/baggage.d.ts +0 -1
  184. package/dist/telemetry/baggage.js +0 -24
  185. package/dist/telemetry/fetch.d.ts +0 -2
  186. package/dist/telemetry/fetch.js +0 -49
  187. package/dist/telemetry/gen-ai.d.ts +0 -6
  188. package/dist/telemetry/gen-ai.js +0 -78
  189. package/dist/telemetry/http.d.ts +0 -3
  190. package/dist/telemetry/http.js +0 -54
  191. package/dist/telemetry/index.d.ts +0 -1
  192. package/dist/telemetry/index.js +0 -1
  193. package/dist/telemetry/memory.d.ts +0 -2
  194. package/dist/telemetry/memory.js +0 -43
  195. package/dist/telemetry/span.d.ts +0 -13
  196. package/dist/telemetry/span.js +0 -60
  197. package/dist/types.d.ts +0 -216
  198. package/dist/types.js +0 -2
  199. package/dist/utils/env.d.ts +0 -2
  200. package/dist/utils/env.js +0 -7
  201. package/dist/utils/headers.d.ts +0 -4
  202. package/dist/utils/headers.js +0 -22
  203. package/dist/utils/preset.d.ts +0 -10
  204. package/dist/utils/preset.js +0 -41
  205. package/dist/utils/request.d.ts +0 -2
  206. package/dist/utils/request.js +0 -43
  207. package/dist/utils/response.d.ts +0 -6
  208. package/dist/utils/response.js +0 -55
  209. package/dist/utils/stream.d.ts +0 -9
  210. package/dist/utils/stream.js +0 -100
  211. package/dist/utils/url.d.ts +0 -4
  212. package/dist/utils/url.js +0 -21
@@ -1,42 +0,0 @@
1
- /**
2
- * Lightweight interface for Bun's SQL client.
3
- *
4
- * We define this locally instead of importing from "bun" to avoid leaking Bun-only
5
- * types in our public API. This prevents TypeScript compilation errors for users
6
- * who are not using Bun and don't have @types/bun installed.
7
- *
8
- * Because TypeScript uses structural typing, a real Bun SQL instance will still
9
- * match this interface perfectly.
10
- */
11
- export interface BunSql {
12
- unsafe<T = any>(query: string, params?: unknown[]): Promise<T>;
13
- transaction<T>(fn: (tx: BunSql) => Promise<T>): Promise<T>;
14
- }
15
- export interface QueryExecutor {
16
- all<T>(sql: string, params?: unknown[]): Promise<T[]>;
17
- get<T>(sql: string, params?: unknown[]): Promise<T | undefined>;
18
- run(sql: string, params?: unknown[]): Promise<{
19
- changes: number;
20
- }>;
21
- transaction<T>(fn: (executor: QueryExecutor) => Promise<T>): Promise<T>;
22
- }
23
- export interface DialectConfig {
24
- placeholder: (index: number) => string;
25
- quote: (name: string) => string;
26
- selectJson: (column: string) => string;
27
- jsonExtract: (column: string, key: string) => string;
28
- upsertSuffix?: (q: (n: string) => string, pk: string[], updateCols: string[]) => string;
29
- supportCreateIndexIfNotExists?: boolean;
30
- limitAsLiteral?: boolean;
31
- partitionClause?: (columns: string[]) => string;
32
- types: {
33
- varchar: string;
34
- json: string;
35
- timestamp: string;
36
- index: "BRIN" | "B-TREE" | "TIME";
37
- };
38
- }
39
- export interface SqlDialect {
40
- executor: QueryExecutor;
41
- config: DialectConfig;
42
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * Normalizes a list of parameters by applying a chain of atomic mappers to each value.
3
- */
4
- export declare function createParamsMapper(mappers: ((v: unknown) => unknown)[]): (params?: unknown[]) => (string | number | bigint | boolean | null)[];
5
- /**
6
- * Normalizes an object (row) by applying a chain of atomic mappers.
7
- * Mappers are expected to mutate the object for performance and to avoid spreads.
8
- */
9
- export declare function createRowMapper<T>(mappers: ((row: Record<string, unknown>) => Record<string, unknown>)[]): (row: Record<string, unknown>) => T;
10
- /**
11
- * Atomic mappers for input parameters.
12
- */
13
- export declare const dateToNumber: (v: unknown) => unknown;
14
- export declare const dateToBigInt: (v: unknown) => unknown;
15
- export declare const jsonStringify: (v: unknown, asBinary?: boolean) => unknown;
16
- /**
17
- * Escapes single quotes in a string for use in SQL literals.
18
- */
19
- export declare const escapeSqlString: (str: string) => string;
20
- /**
21
- * Atomic mappers for database rows.
22
- */
23
- export declare const parseJson: (key: string) => (row: Record<string, unknown>) => Record<string, unknown>;
24
- export declare const toMilliseconds: (key: string) => (row: Record<string, unknown>) => Record<string, unknown>;
25
- export declare const mergeData: (key: string) => (row: Record<string, unknown>) => Record<string, unknown>;
@@ -1,80 +0,0 @@
1
- /**
2
- * Generic utility to chain multiple functions together.
3
- */
4
- function pipe(fns) {
5
- return (v) => {
6
- let result = v;
7
- for (let i = 0; i < fns.length; i++) {
8
- const fn = fns[i];
9
- if (fn) {
10
- result = fn(result);
11
- }
12
- }
13
- return result;
14
- };
15
- }
16
- /**
17
- * Normalizes a list of parameters by applying a chain of atomic mappers to each value.
18
- */
19
- export function createParamsMapper(mappers) {
20
- const p = pipe(mappers);
21
- return (params) => params?.map((v) => p(v));
22
- }
23
- /**
24
- * Normalizes an object (row) by applying a chain of atomic mappers.
25
- * Mappers are expected to mutate the object for performance and to avoid spreads.
26
- */
27
- export function createRowMapper(mappers) {
28
- const p = pipe(mappers);
29
- return (row) => p(row);
30
- }
31
- /**
32
- * Atomic mappers for input parameters.
33
- */
34
- export const dateToNumber = (v) => (v instanceof Date ? v.getTime() : v);
35
- export const dateToBigInt = (v) => (v instanceof Date ? BigInt(v.getTime()) : v);
36
- export const jsonStringify = (v, asBinary = false) => v !== null && typeof v === "object" && !(v instanceof Date)
37
- ? asBinary
38
- ? new TextEncoder().encode(JSON.stringify(v))
39
- : JSON.stringify(v)
40
- : v;
41
- /**
42
- * Escapes single quotes in a string for use in SQL literals.
43
- */
44
- export const escapeSqlString = (str) => str.replaceAll("'", "''");
45
- /**
46
- * WORKAROUND: GreptimeDB can return Rust-style Unicode escapes (\u{xxxx})
47
- * inside JSON strings, which is invalid JSON and causes crashes in JSON.parse.
48
- * This normalization converts those escapes back into literal characters before parsing.
49
- */
50
- function normalizeJsonUnicodeEscapes(value) {
51
- return value.replaceAll(/\\u\{([0-9a-fA-F]+)\}/g, (_, hex) => String.fromCodePoint(+`0x${hex}`));
52
- }
53
- /**
54
- * Atomic mappers for database rows.
55
- */
56
- export const parseJson = (key) => (row) => {
57
- const val = row[key];
58
- if (typeof val === "string") {
59
- row[key] = val === "" || val === "{}" ? {} : JSON.parse(normalizeJsonUnicodeEscapes(val));
60
- }
61
- return row;
62
- };
63
- export const toMilliseconds = (key) => (row) => {
64
- const v = row[key];
65
- if (v instanceof Date) {
66
- row[key] = v.getTime();
67
- }
68
- else if (typeof v === "number" || typeof v === "bigint" || typeof v === "string") {
69
- row[key] = Number(v);
70
- }
71
- return row;
72
- };
73
- export const mergeData = (key) => (row) => {
74
- const data = row[key];
75
- if (data !== null && typeof data === "object") {
76
- Object.assign(row, data);
77
- delete row[key];
78
- }
79
- return row;
80
- };
@@ -1,25 +0,0 @@
1
- import type { ConversationStorage, ConversationEntity, ConversationItemEntity, ConversationMetadata, ConversationItemInput, ConversationQueryOptions } from "./types";
2
- export declare class InMemoryStorage implements ConversationStorage {
3
- private conversations;
4
- private items;
5
- constructor(options?: {
6
- maxSize?: number;
7
- });
8
- private estimateSize;
9
- private mapItem;
10
- createConversation(params: {
11
- metadata?: ConversationMetadata;
12
- items?: ConversationItemInput[];
13
- }): Promise<ConversationEntity>;
14
- getConversation(id: string): Promise<ConversationEntity | undefined>;
15
- listConversations(params: ConversationQueryOptions): Promise<ConversationEntity[]>;
16
- updateConversation(id: string, metadata: ConversationMetadata): Promise<ConversationEntity | undefined>;
17
- deleteConversation(id: string): Promise<{
18
- id: string;
19
- deleted: boolean;
20
- }>;
21
- addItems(conversationId: string, items: ConversationItemInput[]): Promise<ConversationItemEntity[] | undefined>;
22
- getItem(conversationId: string, itemId: string): Promise<ConversationItemEntity | undefined>;
23
- deleteItem(conversationId: string, itemId: string): Promise<ConversationEntity | undefined>;
24
- listItems(conversationId: string, params: ConversationQueryOptions): Promise<ConversationItemEntity[] | undefined>;
25
- }
@@ -1,200 +0,0 @@
1
- import { LRUCache } from "lru-cache";
2
- import { v7 as uuidv7 } from "uuid";
3
- export class InMemoryStorage {
4
- conversations = new Map();
5
- items;
6
- constructor(options) {
7
- // Default to 256MB
8
- const maxSize = options?.maxSize ?? 256 * 1024 * 1024;
9
- this.items = new LRUCache({
10
- maxSize,
11
- sizeCalculation: (items) => Math.max(1, this.estimateSize(items)),
12
- noDisposeOnSet: true,
13
- dispose: (_value, key) => {
14
- this.conversations.delete(key);
15
- },
16
- });
17
- }
18
- estimateSize(root) {
19
- let total = 0;
20
- const stack = [root];
21
- while (stack.length > 0) {
22
- const obj = stack.pop();
23
- if (obj === null || obj === undefined)
24
- continue;
25
- const t = typeof obj;
26
- if (t === "string") {
27
- total += obj.length * 2;
28
- continue;
29
- }
30
- if (t !== "object")
31
- continue;
32
- if (ArrayBuffer.isView(obj)) {
33
- total += obj.byteLength;
34
- continue;
35
- }
36
- if (Array.isArray(obj)) {
37
- const arr = obj;
38
- for (let i = 0, n = arr.length; i < n; i++)
39
- stack.push(arr[i]);
40
- continue;
41
- }
42
- if (obj instanceof Map) {
43
- for (const [k, v] of obj) {
44
- stack.push(k);
45
- stack.push(v);
46
- }
47
- continue;
48
- }
49
- const rec = obj;
50
- for (const k in rec)
51
- stack.push(rec[k]);
52
- }
53
- return total;
54
- }
55
- mapItem(conversationId, input) {
56
- const item = input;
57
- item.id ??= uuidv7();
58
- item.conversation_id = conversationId;
59
- item.created_at = Date.now();
60
- return item;
61
- }
62
- createConversation(params) {
63
- const id = uuidv7();
64
- const conversation = {
65
- id,
66
- created_at: Date.now(),
67
- metadata: params.metadata ?? null,
68
- };
69
- const itemMap = new Map();
70
- if (params.items) {
71
- for (const input of params.items) {
72
- const item = this.mapItem(id, input);
73
- itemMap.set(item.id, item);
74
- }
75
- }
76
- this.conversations.set(id, conversation);
77
- this.items.set(id, itemMap);
78
- return Promise.resolve(conversation);
79
- }
80
- getConversation(id) {
81
- // Updates the LRU position
82
- if (this.items.get(id) === undefined) {
83
- return Promise.resolve(undefined);
84
- }
85
- return Promise.resolve(this.conversations.get(id));
86
- }
87
- listConversations(params) {
88
- const { after, order, limit, metadata } = params;
89
- if (limit <= 0)
90
- return Promise.resolve([]);
91
- let results = Array.from(this.conversations.values());
92
- // Filter by metadata
93
- if (metadata) {
94
- results = results.filter((conv) => {
95
- if (!conv.metadata)
96
- return false;
97
- for (const [key, value] of Object.entries(metadata)) {
98
- if (conv.metadata[key] !== value)
99
- return false;
100
- }
101
- return true;
102
- });
103
- }
104
- // Sort by created_at (and ID as tiebreaker for cursor consistency)
105
- results.sort((a, b) => {
106
- if (a.created_at !== b.created_at) {
107
- return order === "asc" ? a.created_at - b.created_at : b.created_at - a.created_at;
108
- }
109
- return order === "asc" ? a.id.localeCompare(b.id) : b.id.localeCompare(a.id);
110
- });
111
- // Pagination: after
112
- if (after) {
113
- const index = results.findIndex((conv) => conv.id === after);
114
- if (index === -1)
115
- return Promise.resolve([]);
116
- results = results.slice(index + 1);
117
- }
118
- return Promise.resolve(results.slice(0, limit));
119
- }
120
- updateConversation(id, metadata) {
121
- // Updates the LRU position
122
- if (this.items.get(id) === undefined) {
123
- return Promise.resolve(undefined);
124
- }
125
- const conversation = this.conversations.get(id);
126
- if (conversation) {
127
- conversation.metadata = metadata ?? null;
128
- }
129
- return Promise.resolve(conversation);
130
- }
131
- deleteConversation(id) {
132
- // Triggers the dispose handler which cleans up this.conversations
133
- const deleted = this.items.delete(id);
134
- return Promise.resolve({ id, deleted });
135
- }
136
- addItems(conversationId, items) {
137
- const existing = this.items.get(conversationId);
138
- if (!existing) {
139
- return Promise.resolve(undefined);
140
- }
141
- const mappedItems = [];
142
- for (const input of items) {
143
- const item = this.mapItem(conversationId, input);
144
- existing.set(item.id, item);
145
- mappedItems.push(item);
146
- }
147
- // Recalculate the cache size
148
- this.items.set(conversationId, existing);
149
- return Promise.resolve(mappedItems);
150
- }
151
- getItem(conversationId, itemId) {
152
- return Promise.resolve(this.items.get(conversationId)?.get(itemId));
153
- }
154
- deleteItem(conversationId, itemId) {
155
- const existing = this.items.get(conversationId);
156
- if (!existing)
157
- return Promise.resolve(undefined);
158
- if (existing.delete(itemId)) {
159
- // Recalculate the cache size
160
- this.items.set(conversationId, existing);
161
- }
162
- return Promise.resolve(this.conversations.get(conversationId));
163
- }
164
- listItems(conversationId, params) {
165
- const { after, order, limit } = params;
166
- const existing = this.items.get(conversationId);
167
- if (!existing)
168
- return Promise.resolve(undefined);
169
- if (limit <= 0)
170
- return Promise.resolve([]);
171
- // If after is provided but doesn't exist, return empty list
172
- if (after && !existing.has(after))
173
- return Promise.resolve([]);
174
- const out = [];
175
- if (order === "asc") {
176
- let seen = after === null || after === undefined;
177
- for (const item of existing.values()) {
178
- if (!seen) {
179
- if (item.id === after)
180
- seen = true;
181
- continue;
182
- }
183
- out.push(item);
184
- if (out.length === limit)
185
- break;
186
- }
187
- return Promise.resolve(out);
188
- }
189
- // desc
190
- for (const item of existing.values()) {
191
- if (after !== null && after !== undefined && item.id === after)
192
- break;
193
- out.push(item);
194
- if (out.length > limit)
195
- out.shift(); // bounded buffer
196
- }
197
- out.reverse();
198
- return Promise.resolve(out);
199
- }
200
- }
@@ -1,33 +0,0 @@
1
- import type { ConversationStorage, ConversationEntity, ConversationItemEntity, ConversationMetadata, ConversationItemInput, ConversationQueryOptions } from "./types";
2
- import type { SqlDialect } from "./dialects/types";
3
- export declare class SqlStorage implements ConversationStorage {
4
- readonly dialect: SqlDialect;
5
- constructor(options: SqlDialect | {
6
- dialect: SqlDialect;
7
- });
8
- private get executor();
9
- private get config();
10
- migrate(): Promise<void>;
11
- createConversation(params: {
12
- metadata?: ConversationMetadata;
13
- items?: ConversationItemInput[];
14
- }): Promise<ConversationEntity>;
15
- getConversation(id: string): Promise<ConversationEntity | undefined>;
16
- private getConversationInternal;
17
- listConversations(params: ConversationQueryOptions): Promise<ConversationEntity[]>;
18
- updateConversation(id: string, metadata: ConversationMetadata): Promise<ConversationEntity | undefined>;
19
- deleteConversation(id: string): Promise<{
20
- id: string;
21
- deleted: boolean;
22
- }>;
23
- addItems(conversationId: string, items: ConversationItemInput[]): Promise<ConversationItemEntity[] | undefined>;
24
- private addItemsInternal;
25
- getItem(conversationId: string, itemId: string): Promise<ConversationItemEntity | undefined>;
26
- deleteItem(conversationId: string, itemId: string): Promise<ConversationEntity | undefined>;
27
- listItems(conversationId: string, params: ConversationQueryOptions): Promise<ConversationItemEntity[] | undefined>;
28
- }
29
- export * from "./dialects/greptime";
30
- export * from "./dialects/mysql";
31
- export * from "./dialects/postgres";
32
- export * from "./dialects/sqlite";
33
- export * from "./dialects/types";