@ykurilov/agent-chat-storage-postgres 0.0.0-stage → 0.56.0

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 (46) hide show
  1. package/CHANGELOG.md +1220 -0
  2. package/LICENSE +21 -0
  3. package/README.md +37 -2
  4. package/dist/index.js +801 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/types/drizzle-chat-storage.d.ts +1604 -0
  7. package/dist/types/index.d.ts +2 -0
  8. package/dist/types/migrate.d.ts +6 -0
  9. package/dist/types/postgres-storage.d.ts +18 -0
  10. package/dist/types/schema.d.ts +1678 -0
  11. package/drizzle/0000_init.sql +32 -0
  12. package/drizzle/0001_chat_reasoning.sql +1 -0
  13. package/drizzle/0002_compactions.sql +16 -0
  14. package/drizzle/0003_branch_selections.sql +10 -0
  15. package/drizzle/0004_chat_title.sql +22 -0
  16. package/drizzle/0005_usage_records.sql +19 -0
  17. package/drizzle/0006_user_model_choices.sql +7 -0
  18. package/drizzle/0007_voice_dialog.sql +7 -0
  19. package/drizzle/0008_memory_notes.sql +11 -0
  20. package/drizzle/0009_memory_optimization.sql +15 -0
  21. package/drizzle/0010_usage_estimated.sql +1 -0
  22. package/drizzle/0011_message_alive_at.sql +1 -0
  23. package/drizzle/0012_spending_limit_states.sql +6 -0
  24. package/drizzle/0013_chat_origin.sql +1 -0
  25. package/drizzle/0014_chat_subject_after_turn.sql +5 -0
  26. package/drizzle/0015_chat_closed.sql +1 -0
  27. package/drizzle/0016_chat_turn_leases.sql +5 -0
  28. package/drizzle/meta/0000_snapshot.json +259 -0
  29. package/drizzle/meta/0001_snapshot.json +265 -0
  30. package/drizzle/meta/0002_snapshot.json +392 -0
  31. package/drizzle/meta/0003_snapshot.json +483 -0
  32. package/drizzle/meta/0004_snapshot.json +489 -0
  33. package/drizzle/meta/0005_snapshot.json +629 -0
  34. package/drizzle/meta/0006_snapshot.json +672 -0
  35. package/drizzle/meta/0007_snapshot.json +709 -0
  36. package/drizzle/meta/0008_snapshot.json +787 -0
  37. package/drizzle/meta/0009_snapshot.json +908 -0
  38. package/drizzle/meta/0010_snapshot.json +915 -0
  39. package/drizzle/meta/0011_snapshot.json +921 -0
  40. package/drizzle/meta/0012_snapshot.json +958 -0
  41. package/drizzle/meta/0013_snapshot.json +964 -0
  42. package/drizzle/meta/0014_snapshot.json +1019 -0
  43. package/drizzle/meta/0015_snapshot.json +1025 -0
  44. package/drizzle/meta/0016_snapshot.json +1056 -0
  45. package/drizzle/meta/_journal.json +125 -0
  46. package/package.json +46 -3
@@ -0,0 +1,32 @@
1
+ CREATE SCHEMA IF NOT EXISTS "agent_chat";
2
+ --> statement-breakpoint
3
+ CREATE TABLE "agent_chat"."chats" (
4
+ "id" text PRIMARY KEY NOT NULL,
5
+ "owner_id" text NOT NULL,
6
+ "model_id" text NOT NULL,
7
+ "provider_id" text NOT NULL,
8
+ "created_at" text NOT NULL,
9
+ "updated_at" text NOT NULL
10
+ );
11
+ --> statement-breakpoint
12
+ CREATE TABLE "agent_chat"."messages" (
13
+ "seq" bigint GENERATED ALWAYS AS IDENTITY (sequence name "agent_chat"."messages_seq_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
14
+ "id" text PRIMARY KEY NOT NULL,
15
+ "chat_id" text NOT NULL,
16
+ "parent_id" text,
17
+ "role" text NOT NULL,
18
+ "parts" json NOT NULL,
19
+ "format_version" integer NOT NULL,
20
+ "status" text NOT NULL,
21
+ "model_id" text,
22
+ "provider_id" text,
23
+ "usage" json,
24
+ "reasoning_durations_ms" json NOT NULL,
25
+ "error" json,
26
+ "created_at" text NOT NULL
27
+ );
28
+ --> statement-breakpoint
29
+ ALTER TABLE "agent_chat"."messages" ADD CONSTRAINT "messages_chat_id_chats_id_fk" FOREIGN KEY ("chat_id") REFERENCES "agent_chat"."chats"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
30
+ CREATE INDEX "chats_owner_updated_idx" ON "agent_chat"."chats" USING btree ("owner_id","updated_at");--> statement-breakpoint
31
+ CREATE INDEX "messages_chat_created_idx" ON "agent_chat"."messages" USING btree ("chat_id","created_at","seq");--> statement-breakpoint
32
+ CREATE INDEX "messages_streaming_idx" ON "agent_chat"."messages" USING btree ("status") WHERE "agent_chat"."messages"."status" = 'streaming';
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent_chat"."chats" ADD COLUMN "reasoning" text;
@@ -0,0 +1,16 @@
1
+ CREATE TABLE "agent_chat"."compactions" (
2
+ "seq" bigint GENERATED ALWAYS AS IDENTITY (sequence name "agent_chat"."compactions_seq_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
3
+ "id" text PRIMARY KEY NOT NULL,
4
+ "chat_id" text NOT NULL,
5
+ "from_message_id" text NOT NULL,
6
+ "through_message_id" text NOT NULL,
7
+ "summary" text NOT NULL,
8
+ "summary_tokens" integer NOT NULL,
9
+ "model_id" text NOT NULL,
10
+ "provider_id" text NOT NULL,
11
+ "usage" json,
12
+ "created_at" text NOT NULL
13
+ );
14
+ --> statement-breakpoint
15
+ ALTER TABLE "agent_chat"."compactions" ADD CONSTRAINT "compactions_chat_id_chats_id_fk" FOREIGN KEY ("chat_id") REFERENCES "agent_chat"."chats"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
16
+ CREATE INDEX "compactions_chat_seq_idx" ON "agent_chat"."compactions" USING btree ("chat_id","seq");
@@ -0,0 +1,10 @@
1
+ CREATE TABLE "agent_chat"."branch_selections" (
2
+ "seq" bigint GENERATED ALWAYS AS IDENTITY (sequence name "agent_chat"."branch_selections_seq_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
3
+ "id" text PRIMARY KEY NOT NULL,
4
+ "chat_id" text NOT NULL,
5
+ "message_id" text NOT NULL,
6
+ "selected_at" text NOT NULL
7
+ );
8
+ --> statement-breakpoint
9
+ ALTER TABLE "agent_chat"."branch_selections" ADD CONSTRAINT "branch_selections_chat_id_chats_id_fk" FOREIGN KEY ("chat_id") REFERENCES "agent_chat"."chats"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
10
+ CREATE INDEX "branch_selections_chat_seq_idx" ON "agent_chat"."branch_selections" USING btree ("chat_id","seq");
@@ -0,0 +1,22 @@
1
+ ALTER TABLE "agent_chat"."chats" ADD COLUMN "title" text;--> statement-breakpoint
2
+ -- Названия у прежних чатов: из первого текста первого сообщения собеседника, как это делает ядро
3
+ -- (пробелы схлопнуты, не длиннее 60 знаков и «…»).
4
+ UPDATE "agent_chat"."chats" AS c SET "title" = (
5
+ SELECT CASE
6
+ WHEN char_length(t) = 0 THEN NULL
7
+ WHEN char_length(t) > 60 THEN rtrim(left(t, 60)) || '…'
8
+ ELSE t
9
+ END
10
+ FROM (
11
+ SELECT btrim(regexp_replace(x, '\s+', ' ', 'g')) AS t
12
+ FROM (
13
+ SELECT (
14
+ SELECT p->>'text' FROM json_array_elements(m.parts) AS p
15
+ WHERE p->>'type' = 'text' LIMIT 1
16
+ ) AS x
17
+ FROM "agent_chat"."messages" AS m
18
+ WHERE m.chat_id = c.id AND m.role = 'user'
19
+ ORDER BY m.created_at, m.seq LIMIT 1
20
+ ) AS first_text
21
+ ) AS cleaned
22
+ );
@@ -0,0 +1,19 @@
1
+ CREATE TABLE "agent_chat"."usage_records" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "owner_id" text NOT NULL,
4
+ "chat_id" text NOT NULL,
5
+ "message_id" text,
6
+ "source" text NOT NULL,
7
+ "model_id" text NOT NULL,
8
+ "provider_id" text NOT NULL,
9
+ "input_tokens" bigint NOT NULL,
10
+ "cached_input_tokens" bigint NOT NULL,
11
+ "cache_write_tokens" bigint NOT NULL,
12
+ "output_tokens" bigint NOT NULL,
13
+ "reasoning_tokens" bigint NOT NULL,
14
+ "cost_usd" double precision NOT NULL,
15
+ "created_at" text NOT NULL
16
+ );
17
+ --> statement-breakpoint
18
+ CREATE INDEX "usage_records_owner_created_idx" ON "agent_chat"."usage_records" USING btree ("owner_id","created_at");--> statement-breakpoint
19
+ CREATE INDEX "usage_records_chat_created_idx" ON "agent_chat"."usage_records" USING btree ("chat_id","created_at");
@@ -0,0 +1,7 @@
1
+ CREATE TABLE "agent_chat"."user_model_choices" (
2
+ "owner_id" text PRIMARY KEY NOT NULL,
3
+ "model_id" text NOT NULL,
4
+ "provider_id" text NOT NULL,
5
+ "reasoning" text,
6
+ "updated_at" text NOT NULL
7
+ );
@@ -0,0 +1,7 @@
1
+ CREATE TABLE "agent_chat"."voice_choices" (
2
+ "owner_id" text PRIMARY KEY NOT NULL,
3
+ "voice_id" text NOT NULL,
4
+ "updated_at" text NOT NULL
5
+ );
6
+ --> statement-breakpoint
7
+ ALTER TABLE "agent_chat"."messages" ADD COLUMN "voice_cut" json;
@@ -0,0 +1,11 @@
1
+ CREATE TABLE "agent_chat"."memory_notes" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "owner_id" text NOT NULL,
4
+ "categories" jsonb NOT NULL,
5
+ "content" text NOT NULL,
6
+ "version" integer DEFAULT 1 NOT NULL,
7
+ "created_at" timestamp with time zone NOT NULL,
8
+ "updated_at" timestamp with time zone NOT NULL
9
+ );
10
+ --> statement-breakpoint
11
+ CREATE INDEX "memory_notes_owner_updated_idx" ON "agent_chat"."memory_notes" USING btree ("owner_id","updated_at");
@@ -0,0 +1,15 @@
1
+ CREATE TABLE "agent_chat"."memory_optimization_runs" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "owner_id" text NOT NULL,
4
+ "started_at" timestamp with time zone NOT NULL,
5
+ "finished_at" timestamp with time zone,
6
+ "status" text NOT NULL,
7
+ "groups" jsonb DEFAULT '[]'::jsonb NOT NULL,
8
+ "error" text,
9
+ "usage_record_id" text
10
+ );
11
+ --> statement-breakpoint
12
+ ALTER TABLE "agent_chat"."memory_notes" ADD COLUMN "merged_into" text;--> statement-breakpoint
13
+ CREATE INDEX "memory_optimization_runs_owner_started_idx" ON "agent_chat"."memory_optimization_runs" USING btree ("owner_id","started_at");--> statement-breakpoint
14
+ CREATE UNIQUE INDEX "memory_optimization_runs_one_running_idx" ON "agent_chat"."memory_optimization_runs" USING btree ("owner_id") WHERE "agent_chat"."memory_optimization_runs"."status" = 'running';--> statement-breakpoint
15
+ CREATE INDEX "memory_notes_merged_into_idx" ON "agent_chat"."memory_notes" USING btree ("merged_into");
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent_chat"."usage_records" ADD COLUMN "estimated" boolean DEFAULT false NOT NULL;
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent_chat"."messages" ADD COLUMN "alive_at" text;
@@ -0,0 +1,6 @@
1
+ CREATE TABLE "agent_chat"."spending_limit_states" (
2
+ "key" text PRIMARY KEY NOT NULL,
3
+ "value" text NOT NULL,
4
+ "version" integer NOT NULL,
5
+ "updated_at" text NOT NULL
6
+ );
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent_chat"."chats" ADD COLUMN "origin" text;
@@ -0,0 +1,5 @@
1
+ ALTER TABLE "agent_chat"."chats" ADD COLUMN "subject_key" text;--> statement-breakpoint
2
+ ALTER TABLE "agent_chat"."chats" ADD COLUMN "subject_data" json;--> statement-breakpoint
3
+ ALTER TABLE "agent_chat"."messages" ADD COLUMN "after_turn" text;--> statement-breakpoint
4
+ CREATE UNIQUE INDEX "chats_owner_subject_idx" ON "agent_chat"."chats" USING btree ("owner_id","subject_key");--> statement-breakpoint
5
+ CREATE INDEX "messages_after_turn_pending_idx" ON "agent_chat"."messages" USING btree ("after_turn") WHERE "agent_chat"."messages"."after_turn" = 'pending';
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent_chat"."chats" ADD COLUMN "closed" json;
@@ -0,0 +1,5 @@
1
+ CREATE TABLE "agent_chat"."chat_turn_leases" (
2
+ "chat_id" text PRIMARY KEY NOT NULL,
3
+ "holder" text NOT NULL,
4
+ "until" timestamp with time zone NOT NULL
5
+ );
@@ -0,0 +1,259 @@
1
+ {
2
+ "id": "6599f9f0-aaf7-46ab-afa5-2b2397ee8b8d",
3
+ "prevId": "00000000-0000-0000-0000-000000000000",
4
+ "version": "7",
5
+ "dialect": "postgresql",
6
+ "tables": {
7
+ "agent_chat.chats": {
8
+ "name": "chats",
9
+ "schema": "agent_chat",
10
+ "columns": {
11
+ "id": {
12
+ "name": "id",
13
+ "type": "text",
14
+ "primaryKey": true,
15
+ "notNull": true
16
+ },
17
+ "owner_id": {
18
+ "name": "owner_id",
19
+ "type": "text",
20
+ "primaryKey": false,
21
+ "notNull": true
22
+ },
23
+ "model_id": {
24
+ "name": "model_id",
25
+ "type": "text",
26
+ "primaryKey": false,
27
+ "notNull": true
28
+ },
29
+ "provider_id": {
30
+ "name": "provider_id",
31
+ "type": "text",
32
+ "primaryKey": false,
33
+ "notNull": true
34
+ },
35
+ "created_at": {
36
+ "name": "created_at",
37
+ "type": "text",
38
+ "primaryKey": false,
39
+ "notNull": true
40
+ },
41
+ "updated_at": {
42
+ "name": "updated_at",
43
+ "type": "text",
44
+ "primaryKey": false,
45
+ "notNull": true
46
+ }
47
+ },
48
+ "indexes": {
49
+ "chats_owner_updated_idx": {
50
+ "name": "chats_owner_updated_idx",
51
+ "columns": [
52
+ {
53
+ "expression": "owner_id",
54
+ "isExpression": false,
55
+ "asc": true,
56
+ "nulls": "last"
57
+ },
58
+ {
59
+ "expression": "updated_at",
60
+ "isExpression": false,
61
+ "asc": true,
62
+ "nulls": "last"
63
+ }
64
+ ],
65
+ "isUnique": false,
66
+ "concurrently": false,
67
+ "method": "btree",
68
+ "with": {}
69
+ }
70
+ },
71
+ "foreignKeys": {},
72
+ "compositePrimaryKeys": {},
73
+ "uniqueConstraints": {},
74
+ "policies": {},
75
+ "checkConstraints": {},
76
+ "isRLSEnabled": false
77
+ },
78
+ "agent_chat.messages": {
79
+ "name": "messages",
80
+ "schema": "agent_chat",
81
+ "columns": {
82
+ "seq": {
83
+ "name": "seq",
84
+ "type": "bigint",
85
+ "primaryKey": false,
86
+ "notNull": true,
87
+ "identity": {
88
+ "type": "always",
89
+ "name": "messages_seq_seq",
90
+ "schema": "agent_chat",
91
+ "increment": "1",
92
+ "startWith": "1",
93
+ "minValue": "1",
94
+ "maxValue": "9223372036854775807",
95
+ "cache": "1",
96
+ "cycle": false
97
+ }
98
+ },
99
+ "id": {
100
+ "name": "id",
101
+ "type": "text",
102
+ "primaryKey": true,
103
+ "notNull": true
104
+ },
105
+ "chat_id": {
106
+ "name": "chat_id",
107
+ "type": "text",
108
+ "primaryKey": false,
109
+ "notNull": true
110
+ },
111
+ "parent_id": {
112
+ "name": "parent_id",
113
+ "type": "text",
114
+ "primaryKey": false,
115
+ "notNull": false
116
+ },
117
+ "role": {
118
+ "name": "role",
119
+ "type": "text",
120
+ "primaryKey": false,
121
+ "notNull": true
122
+ },
123
+ "parts": {
124
+ "name": "parts",
125
+ "type": "json",
126
+ "primaryKey": false,
127
+ "notNull": true
128
+ },
129
+ "format_version": {
130
+ "name": "format_version",
131
+ "type": "integer",
132
+ "primaryKey": false,
133
+ "notNull": true
134
+ },
135
+ "status": {
136
+ "name": "status",
137
+ "type": "text",
138
+ "primaryKey": false,
139
+ "notNull": true
140
+ },
141
+ "model_id": {
142
+ "name": "model_id",
143
+ "type": "text",
144
+ "primaryKey": false,
145
+ "notNull": false
146
+ },
147
+ "provider_id": {
148
+ "name": "provider_id",
149
+ "type": "text",
150
+ "primaryKey": false,
151
+ "notNull": false
152
+ },
153
+ "usage": {
154
+ "name": "usage",
155
+ "type": "json",
156
+ "primaryKey": false,
157
+ "notNull": false
158
+ },
159
+ "reasoning_durations_ms": {
160
+ "name": "reasoning_durations_ms",
161
+ "type": "json",
162
+ "primaryKey": false,
163
+ "notNull": true
164
+ },
165
+ "error": {
166
+ "name": "error",
167
+ "type": "json",
168
+ "primaryKey": false,
169
+ "notNull": false
170
+ },
171
+ "created_at": {
172
+ "name": "created_at",
173
+ "type": "text",
174
+ "primaryKey": false,
175
+ "notNull": true
176
+ }
177
+ },
178
+ "indexes": {
179
+ "messages_chat_created_idx": {
180
+ "name": "messages_chat_created_idx",
181
+ "columns": [
182
+ {
183
+ "expression": "chat_id",
184
+ "isExpression": false,
185
+ "asc": true,
186
+ "nulls": "last"
187
+ },
188
+ {
189
+ "expression": "created_at",
190
+ "isExpression": false,
191
+ "asc": true,
192
+ "nulls": "last"
193
+ },
194
+ {
195
+ "expression": "seq",
196
+ "isExpression": false,
197
+ "asc": true,
198
+ "nulls": "last"
199
+ }
200
+ ],
201
+ "isUnique": false,
202
+ "concurrently": false,
203
+ "method": "btree",
204
+ "with": {}
205
+ },
206
+ "messages_streaming_idx": {
207
+ "name": "messages_streaming_idx",
208
+ "columns": [
209
+ {
210
+ "expression": "status",
211
+ "isExpression": false,
212
+ "asc": true,
213
+ "nulls": "last"
214
+ }
215
+ ],
216
+ "isUnique": false,
217
+ "where": "\"agent_chat\".\"messages\".\"status\" = 'streaming'",
218
+ "concurrently": false,
219
+ "method": "btree",
220
+ "with": {}
221
+ }
222
+ },
223
+ "foreignKeys": {
224
+ "messages_chat_id_chats_id_fk": {
225
+ "name": "messages_chat_id_chats_id_fk",
226
+ "tableFrom": "messages",
227
+ "tableTo": "chats",
228
+ "schemaTo": "agent_chat",
229
+ "columnsFrom": [
230
+ "chat_id"
231
+ ],
232
+ "columnsTo": [
233
+ "id"
234
+ ],
235
+ "onDelete": "cascade",
236
+ "onUpdate": "no action"
237
+ }
238
+ },
239
+ "compositePrimaryKeys": {},
240
+ "uniqueConstraints": {},
241
+ "policies": {},
242
+ "checkConstraints": {},
243
+ "isRLSEnabled": false
244
+ }
245
+ },
246
+ "enums": {},
247
+ "schemas": {
248
+ "agent_chat": "agent_chat"
249
+ },
250
+ "sequences": {},
251
+ "roles": {},
252
+ "policies": {},
253
+ "views": {},
254
+ "_meta": {
255
+ "columns": {},
256
+ "schemas": {},
257
+ "tables": {}
258
+ }
259
+ }