@thoughtminers/agent-runtime-postgres 0.1.0 → 0.2.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.
- package/README.md +27 -10
- package/dist/index.d.ts +93 -69
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +103 -56
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/prisma/schema.prisma +12 -4
package/README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
# @thoughtminers/agent-runtime-postgres
|
|
2
2
|
|
|
3
|
-
Postgres
|
|
3
|
+
Postgres stores for `@thoughtminers/agent-runtime-core`, layered over **your**
|
|
4
4
|
Prisma client — the adapter never owns a `PrismaClient`, a connection pool, or
|
|
5
5
|
a migration pipeline.
|
|
6
6
|
|
|
7
7
|
## Setup
|
|
8
8
|
|
|
9
|
-
1. Copy the
|
|
10
|
-
your application's schema (`simple_agent_messages` +
|
|
9
|
+
1. Copy the models from [`prisma/schema.prisma`](prisma/schema.prisma) into
|
|
10
|
+
your application's schema: both (`simple_agent_messages` +
|
|
11
|
+
`simple_agent_messages_llm`) for `createPostgresStore`, or only
|
|
12
|
+
`SimpleAgentTrace` for `createPostgresTraceStore` (your app keeps its own
|
|
13
|
+
record of the conversation).
|
|
11
14
|
2. Run your usual migration (`prisma migrate dev` / your deploy flow).
|
|
12
15
|
3. Hand the adapter your generated client:
|
|
13
16
|
|
|
@@ -16,18 +19,32 @@ import { PrismaClient } from '@prisma/client';
|
|
|
16
19
|
import { createPostgresStore } from '@thoughtminers/agent-runtime-postgres';
|
|
17
20
|
|
|
18
21
|
const prisma = new PrismaClient();
|
|
19
|
-
const store = createPostgresStore(prisma);
|
|
22
|
+
const store = createPostgresStore(prisma); // transcript + trace (TranscriptStore)
|
|
23
|
+
// or: createPostgresTraceStore(prisma.simpleAgentTrace) — trace only (TraceStore)
|
|
20
24
|
```
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
client generated from the reference models satisfies
|
|
26
|
+
The delegate types (`MessageDelegate`, `TraceDelegate`) are structural, so a
|
|
27
|
+
client generated from the reference models satisfies them whatever you name
|
|
28
|
+
the models — no cast needed.
|
|
24
29
|
|
|
25
30
|
## Guarantees
|
|
26
31
|
|
|
27
|
-
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
- The trace's user row carries the inbound `external_message_id`, unique per
|
|
33
|
+
conversation: a duplicate delivery gets the first run's row back (P2002
|
|
34
|
+
catch) and the agent stops, so concurrent deliveries of one message run once,
|
|
35
|
+
in both stores. `appendMessage` is idempotent on the same key.
|
|
36
|
+
- `seen()` answers from the trace, in both stores. A failed run's rows are
|
|
37
|
+
flagged `failed` and lose their key, so the message's retry runs again.
|
|
38
|
+
- `loadHistory({ limit })` returns the newest rows, oldest first.
|
|
39
|
+
- **Upgrading from 0.1 (required before deploying 0.2):** add to the trace table
|
|
40
|
+
the nullable `external_message_id` column, the `failed` boolean (default
|
|
41
|
+
false) and the **unique** index on `(conversation_id, external_message_id)`.
|
|
42
|
+
Without the unique index everything still compiles, but concurrent duplicate
|
|
43
|
+
deliveries silently run twice.
|
|
44
|
+
- Your models may have any name, but their **field names** must match the
|
|
45
|
+
reference models.
|
|
46
|
+
- The trace table is append-only, apart from the `interrupted` and `failed`
|
|
47
|
+
flags and a failed run's cleared key.
|
|
31
48
|
|
|
32
49
|
## Integration test
|
|
33
50
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TraceStore, TranscriptStore } from '@thoughtminers/agent-runtime-core';
|
|
2
2
|
/**
|
|
3
|
-
* Postgres
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* migration, then:
|
|
3
|
+
* Postgres stores over the CONSUMER'S Prisma client — this adapter never owns a
|
|
4
|
+
* PrismaClient (no second connection pool, your migrations). Copy the models
|
|
5
|
+
* from ../prisma/schema.prisma into your schema, run your migration, then:
|
|
7
6
|
*
|
|
8
|
-
* const store = createPostgresStore(prisma);
|
|
7
|
+
* const store = createPostgresStore(prisma); // transcript + trace
|
|
8
|
+
* const store = createPostgresTraceStore(prisma.simpleAgentTrace); // trace only
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* The delegate types are structural: any generated client for the reference
|
|
11
|
+
* models satisfies them, whatever you name the models.
|
|
12
12
|
*/
|
|
13
13
|
export interface MessageRow {
|
|
14
14
|
id: string;
|
|
@@ -29,70 +29,94 @@ export interface TraceRow {
|
|
|
29
29
|
model: string | null;
|
|
30
30
|
usage: unknown;
|
|
31
31
|
latencyMs: number | null;
|
|
32
|
+
externalMessageId: string | null;
|
|
32
33
|
interrupted: boolean;
|
|
34
|
+
failed: boolean;
|
|
33
35
|
createdAt: Date;
|
|
34
36
|
}
|
|
37
|
+
/** What Prisma accepts for a Json column: anything JSON-serialisable. */
|
|
38
|
+
export type JsonInput = string | number | boolean | {
|
|
39
|
+
readonly [key: string]: JsonInput | null;
|
|
40
|
+
} | readonly (JsonInput | null)[];
|
|
41
|
+
export interface MessageDelegate {
|
|
42
|
+
findFirst(args: {
|
|
43
|
+
where: {
|
|
44
|
+
conversationId: string;
|
|
45
|
+
externalMessageId: string;
|
|
46
|
+
};
|
|
47
|
+
}): Promise<MessageRow | null>;
|
|
48
|
+
findMany(args: {
|
|
49
|
+
where: {
|
|
50
|
+
conversationId: string;
|
|
51
|
+
};
|
|
52
|
+
orderBy: {
|
|
53
|
+
createdAt: 'asc' | 'desc';
|
|
54
|
+
};
|
|
55
|
+
take?: number;
|
|
56
|
+
}): Promise<MessageRow[]>;
|
|
57
|
+
create(args: {
|
|
58
|
+
data: {
|
|
59
|
+
id: string;
|
|
60
|
+
conversationId: string;
|
|
61
|
+
role: string;
|
|
62
|
+
content: string;
|
|
63
|
+
externalMessageId: string | null;
|
|
64
|
+
interrupted: boolean;
|
|
65
|
+
};
|
|
66
|
+
}): Promise<MessageRow>;
|
|
67
|
+
}
|
|
68
|
+
export interface TraceDelegate {
|
|
69
|
+
findFirst(args: {
|
|
70
|
+
where: {
|
|
71
|
+
conversationId: string;
|
|
72
|
+
externalMessageId: string;
|
|
73
|
+
};
|
|
74
|
+
}): Promise<TraceRow | null>;
|
|
75
|
+
findMany(args: {
|
|
76
|
+
where: {
|
|
77
|
+
conversationId: string;
|
|
78
|
+
};
|
|
79
|
+
orderBy: {
|
|
80
|
+
createdAt: 'asc' | 'desc';
|
|
81
|
+
};
|
|
82
|
+
take?: number;
|
|
83
|
+
}): Promise<TraceRow[]>;
|
|
84
|
+
create(args: {
|
|
85
|
+
data: {
|
|
86
|
+
id: string;
|
|
87
|
+
conversationId: string;
|
|
88
|
+
runId: string;
|
|
89
|
+
role: string;
|
|
90
|
+
content: string | null;
|
|
91
|
+
toolCalls?: JsonInput;
|
|
92
|
+
model: string | null;
|
|
93
|
+
usage?: JsonInput;
|
|
94
|
+
latencyMs: number | null;
|
|
95
|
+
externalMessageId: string | null;
|
|
96
|
+
interrupted: boolean;
|
|
97
|
+
};
|
|
98
|
+
}): Promise<TraceRow>;
|
|
99
|
+
updateMany(args: {
|
|
100
|
+
where: {
|
|
101
|
+
runId: string;
|
|
102
|
+
};
|
|
103
|
+
data: {
|
|
104
|
+
interrupted: true;
|
|
105
|
+
} | {
|
|
106
|
+
failed: true;
|
|
107
|
+
externalMessageId: null;
|
|
108
|
+
};
|
|
109
|
+
}): Promise<unknown>;
|
|
110
|
+
}
|
|
35
111
|
export interface SimpleAgentDb {
|
|
36
|
-
simpleAgentMessage:
|
|
37
|
-
|
|
38
|
-
where: {
|
|
39
|
-
conversationId: string;
|
|
40
|
-
externalMessageId: string;
|
|
41
|
-
};
|
|
42
|
-
}): Promise<MessageRow | null>;
|
|
43
|
-
findMany(args: {
|
|
44
|
-
where: {
|
|
45
|
-
conversationId: string;
|
|
46
|
-
};
|
|
47
|
-
orderBy: {
|
|
48
|
-
createdAt: 'asc';
|
|
49
|
-
};
|
|
50
|
-
take?: number;
|
|
51
|
-
}): Promise<MessageRow[]>;
|
|
52
|
-
create(args: {
|
|
53
|
-
data: {
|
|
54
|
-
id: string;
|
|
55
|
-
conversationId: string;
|
|
56
|
-
role: string;
|
|
57
|
-
content: string;
|
|
58
|
-
externalMessageId: string | null;
|
|
59
|
-
interrupted: boolean;
|
|
60
|
-
};
|
|
61
|
-
}): Promise<MessageRow>;
|
|
62
|
-
};
|
|
63
|
-
simpleAgentTrace: {
|
|
64
|
-
findMany(args: {
|
|
65
|
-
where: {
|
|
66
|
-
conversationId: string;
|
|
67
|
-
};
|
|
68
|
-
orderBy: {
|
|
69
|
-
createdAt: 'asc';
|
|
70
|
-
};
|
|
71
|
-
take?: number;
|
|
72
|
-
}): Promise<TraceRow[]>;
|
|
73
|
-
create(args: {
|
|
74
|
-
data: {
|
|
75
|
-
id: string;
|
|
76
|
-
conversationId: string;
|
|
77
|
-
runId: string;
|
|
78
|
-
role: string;
|
|
79
|
-
content: string | null;
|
|
80
|
-
toolCalls?: unknown;
|
|
81
|
-
model: string | null;
|
|
82
|
-
usage?: unknown;
|
|
83
|
-
latencyMs: number | null;
|
|
84
|
-
interrupted: boolean;
|
|
85
|
-
};
|
|
86
|
-
}): Promise<TraceRow>;
|
|
87
|
-
updateMany(args: {
|
|
88
|
-
where: {
|
|
89
|
-
runId: string;
|
|
90
|
-
};
|
|
91
|
-
data: {
|
|
92
|
-
interrupted: boolean;
|
|
93
|
-
};
|
|
94
|
-
}): Promise<unknown>;
|
|
95
|
-
};
|
|
112
|
+
simpleAgentMessage: MessageDelegate;
|
|
113
|
+
simpleAgentTrace: TraceDelegate;
|
|
96
114
|
}
|
|
97
|
-
|
|
115
|
+
/** Transcript + trace: `simple_agent_messages` and `simple_agent_messages_llm`. */
|
|
116
|
+
export declare const createPostgresStore: (db: SimpleAgentDb) => TranscriptStore;
|
|
117
|
+
/**
|
|
118
|
+
* Trace only (`simple_agent_messages_llm`), for an app that keeps its own record of the
|
|
119
|
+
* conversation: no transcript is written.
|
|
120
|
+
*/
|
|
121
|
+
export declare const createPostgresTraceStore: (trace: TraceDelegate) => TraceStore;
|
|
98
122
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAMV,UAAU,EACV,eAAe,EAEhB,MAAM,mCAAmC,CAAC;AAE3C;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,WAAW,EAAE,OAAO,CAAC;IACrB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,EAAE,OAAO,CAAC;IACf,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,WAAW,EAAE,OAAO,CAAC;IACrB,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,yEAAyE;AACzE,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,OAAO,GACP;IAAE,QAAQ,EAAE,GAAG,EAAE,M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E,MAAM,CAAC;KACf,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IACxB,MAAM,CAAC,IAAI,EAAE;QACX,IAAI,EAAE;YACJ,EAAE,EAAE,MAAM,CAAC;YACX,cAAc,EAAE,MAAM,CAAC;YACvB,KAAK,EAAE,MAAM,CAAC;YACd,IAAI,EAAE,MAAM,CAAC;YACb,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;YACvB,SAAS,CAAC,EAAE,SAAS,CAAC;YACtB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;YACrB,KAAK,CAAC,EAAE,SAAS,CAAC;YAClB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;YACzB,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;YACjC,WAAW,EAAE,OAAO,CAAC;SACtB,CAAC;KACH,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IACtB,UAAU,CAAC,IAAI,EAAE;QACf,KAAK,EAAE;YAAE,KAAK,EAAE,MAAM,CAAA;SAAE,CAAC;QACzB,IAAI,EAAE;YAAE,WAAW,EAAE,IAAI,CAAA;SAAE,GAAG;YAAE,MAAM,EAAE,IAAI,CAAC;YAAC,iBAAiB,EAAE,IAAI,CAAA;SAAE,CAAC;KACzE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,kBAAkB,EAAE,eAAe,CAAC;IACpC,gBAAgB,EAAE,aAAa,CAAC;CACjC;AAsGD,mFAAmF;AACnF,eAAO,MAAM,mBAAmB,GAAI,IAAI,aAAa,KAAG,eAkDvD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,GAAI,OAAO,aAAa,KAAG,UAY/D,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -19,80 +19,127 @@ const toTraceRecord = (row) => ({
|
|
|
19
19
|
model: row.model,
|
|
20
20
|
usage: (row.usage ?? null),
|
|
21
21
|
latencyMs: row.latencyMs,
|
|
22
|
+
externalMessageId: row.externalMessageId,
|
|
22
23
|
interrupted: row.interrupted,
|
|
24
|
+
failed: row.failed,
|
|
23
25
|
createdAt: row.createdAt,
|
|
24
26
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
// The newest `take` rows, oldest first (what the in-memory stores return).
|
|
28
|
+
const newest = async (findMany, conversationId, take) => take === undefined
|
|
29
|
+
? findMany({ where: { conversationId }, orderBy: { createdAt: 'asc' } })
|
|
30
|
+
: (await findMany({ where: { conversationId }, orderBy: { createdAt: 'desc' }, take })).reverse();
|
|
31
|
+
// The trace half, shared by both stores.
|
|
32
|
+
const traceMethods = (trace) => ({
|
|
33
|
+
appendTrace: async (row) => {
|
|
34
|
+
const externalMessageId = row.externalMessageId ?? null;
|
|
29
35
|
try {
|
|
30
|
-
const
|
|
36
|
+
const created = await trace.create({
|
|
31
37
|
data: {
|
|
32
38
|
id: randomUUID(),
|
|
33
|
-
conversationId:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
conversationId: row.conversationId,
|
|
40
|
+
runId: row.runId,
|
|
41
|
+
role: row.role,
|
|
42
|
+
content: row.content,
|
|
43
|
+
// Omit null JSON columns so they default to SQL NULL.
|
|
44
|
+
...(row.toolCalls !== null ? { toolCalls: row.toolCalls } : {}),
|
|
45
|
+
model: row.model,
|
|
46
|
+
...(row.usage !== null ? { usage: row.usage } : {}),
|
|
47
|
+
latencyMs: row.latencyMs,
|
|
48
|
+
externalMessageId,
|
|
49
|
+
interrupted: row.interrupted ?? false,
|
|
38
50
|
},
|
|
39
51
|
});
|
|
40
|
-
return
|
|
52
|
+
return toTraceRecord(created);
|
|
41
53
|
}
|
|
42
54
|
catch (err) {
|
|
43
|
-
// Idempotency: the (conversationId, externalMessageId) unique index
|
|
44
|
-
//
|
|
45
|
-
if (isUniqueViolation(err) &&
|
|
46
|
-
const existing = await
|
|
47
|
-
where: {
|
|
48
|
-
conversationId: msg.conversationId,
|
|
49
|
-
externalMessageId: msg.externalMessageId,
|
|
50
|
-
},
|
|
55
|
+
// Idempotency: the (conversationId, externalMessageId) unique index fired — a concurrent
|
|
56
|
+
// or repeated delivery of the same message. Return the first run's row.
|
|
57
|
+
if (isUniqueViolation(err) && externalMessageId !== null) {
|
|
58
|
+
const existing = await trace.findFirst({
|
|
59
|
+
where: { conversationId: row.conversationId, externalMessageId },
|
|
51
60
|
});
|
|
52
61
|
if (existing)
|
|
53
|
-
return
|
|
62
|
+
return toTraceRecord(existing);
|
|
54
63
|
}
|
|
55
64
|
throw err;
|
|
56
65
|
}
|
|
57
66
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
runId: row.runId,
|
|
64
|
-
role: row.role,
|
|
65
|
-
content: row.content,
|
|
66
|
-
// Omit null JSON columns so they default to SQL NULL.
|
|
67
|
-
...(row.toolCalls !== null ? { toolCalls: row.toolCalls } : {}),
|
|
68
|
-
model: row.model,
|
|
69
|
-
...(row.usage !== null ? { usage: row.usage } : {}),
|
|
70
|
-
latencyMs: row.latencyMs,
|
|
71
|
-
interrupted: row.interrupted ?? false,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
return toTraceRecord(created);
|
|
75
|
-
},
|
|
76
|
-
loadHistory: async (conversationId, opts) => {
|
|
77
|
-
const [messages, trace] = await Promise.all([
|
|
78
|
-
db.simpleAgentMessage.findMany({
|
|
79
|
-
where: { conversationId },
|
|
80
|
-
orderBy: { createdAt: 'asc' },
|
|
81
|
-
...(opts?.limit !== undefined ? { take: opts.limit } : {}),
|
|
82
|
-
}),
|
|
83
|
-
db.simpleAgentTrace.findMany({
|
|
84
|
-
where: { conversationId },
|
|
85
|
-
orderBy: { createdAt: 'asc' },
|
|
86
|
-
...(opts?.limit !== undefined ? { take: opts.limit * 4 } : {}),
|
|
87
|
-
}),
|
|
88
|
-
]);
|
|
89
|
-
return {
|
|
90
|
-
messages: messages.map(toMessageRecord),
|
|
91
|
-
trace: trace.map(toTraceRecord),
|
|
92
|
-
};
|
|
67
|
+
seen: async (conversationId, externalMessageId) => (await trace.findFirst({ where: { conversationId, externalMessageId } })) !== null,
|
|
68
|
+
loadTrace: async (conversationId, limit) => {
|
|
69
|
+
const take = limit !== undefined ? limit * 4 : undefined;
|
|
70
|
+
const rows = await newest((args) => trace.findMany(args), conversationId, take);
|
|
71
|
+
return rows.map(toTraceRecord);
|
|
93
72
|
},
|
|
94
73
|
markRunInterrupted: async (runId) => {
|
|
95
|
-
await
|
|
74
|
+
await trace.updateMany({ where: { runId }, data: { interrupted: true } });
|
|
75
|
+
},
|
|
76
|
+
markRunFailed: async (runId) => {
|
|
77
|
+
await trace.updateMany({ where: { runId }, data: { failed: true, externalMessageId: null } });
|
|
96
78
|
},
|
|
97
79
|
});
|
|
80
|
+
/** Transcript + trace: `simple_agent_messages` and `simple_agent_messages_llm`. */
|
|
81
|
+
export const createPostgresStore = (db) => {
|
|
82
|
+
const { seen, appendTrace, loadTrace, markRunInterrupted, markRunFailed } = traceMethods(db.simpleAgentTrace);
|
|
83
|
+
const messages = db.simpleAgentMessage;
|
|
84
|
+
return {
|
|
85
|
+
seen,
|
|
86
|
+
appendMessage: async (msg) => {
|
|
87
|
+
try {
|
|
88
|
+
const row = await messages.create({
|
|
89
|
+
data: {
|
|
90
|
+
id: randomUUID(),
|
|
91
|
+
conversationId: msg.conversationId,
|
|
92
|
+
role: msg.role,
|
|
93
|
+
content: msg.content,
|
|
94
|
+
externalMessageId: msg.externalMessageId,
|
|
95
|
+
interrupted: msg.interrupted ?? false,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
return toMessageRecord(row);
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
// Idempotency: the (conversationId, externalMessageId) unique index
|
|
102
|
+
// fired — a concurrent or repeated delivery. Return the existing row.
|
|
103
|
+
if (isUniqueViolation(err) && msg.externalMessageId !== null) {
|
|
104
|
+
const existing = await messages.findFirst({
|
|
105
|
+
where: {
|
|
106
|
+
conversationId: msg.conversationId,
|
|
107
|
+
externalMessageId: msg.externalMessageId,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
if (existing)
|
|
111
|
+
return toMessageRecord(existing);
|
|
112
|
+
}
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
appendTrace,
|
|
117
|
+
loadHistory: async (conversationId, opts) => {
|
|
118
|
+
const [rows, trace] = await Promise.all([
|
|
119
|
+
newest((args) => messages.findMany(args), conversationId, opts?.limit),
|
|
120
|
+
loadTrace(conversationId, opts?.limit),
|
|
121
|
+
]);
|
|
122
|
+
return { messages: rows.map(toMessageRecord), trace };
|
|
123
|
+
},
|
|
124
|
+
markRunInterrupted,
|
|
125
|
+
markRunFailed,
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Trace only (`simple_agent_messages_llm`), for an app that keeps its own record of the
|
|
130
|
+
* conversation: no transcript is written.
|
|
131
|
+
*/
|
|
132
|
+
export const createPostgresTraceStore = (trace) => {
|
|
133
|
+
const { seen, appendTrace, loadTrace, markRunInterrupted, markRunFailed } = traceMethods(trace);
|
|
134
|
+
return {
|
|
135
|
+
seen,
|
|
136
|
+
appendTrace,
|
|
137
|
+
loadHistory: async (conversationId, opts) => ({
|
|
138
|
+
messages: [],
|
|
139
|
+
trace: await loadTrace(conversationId, opts?.limit),
|
|
140
|
+
}),
|
|
141
|
+
markRunInterrupted,
|
|
142
|
+
markRunFailed,
|
|
143
|
+
};
|
|
144
|
+
};
|
|
98
145
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":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AC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thoughtminers/agent-runtime-postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Postgres/Prisma ConversationStore adapter for @thoughtminers/agent-runtime-core (structural interface over your Prisma client).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@thoughtminers/agent-runtime-core": "0.
|
|
31
|
+
"@thoughtminers/agent-runtime-core": "0.2.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@prisma/client": ">=6"
|
package/prisma/schema.prisma
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// Reference schema for @
|
|
1
|
+
// Reference schema for @thoughtminers/agent-runtime-postgres.
|
|
2
2
|
//
|
|
3
3
|
// COPY the two models below into your application's Prisma schema (or drop
|
|
4
|
-
// this file into your prisma/schema folder).
|
|
4
|
+
// this file into your prisma/schema folder). For createPostgresTraceStore, copy
|
|
5
|
+
// only SimpleAgentTrace. The adapter takes YOUR generated
|
|
5
6
|
// client — it never owns a PrismaClient or a migration pipeline.
|
|
6
7
|
//
|
|
7
8
|
// The datasource/generator blocks exist so this file also works standalone
|
|
@@ -44,10 +45,17 @@ model SimpleAgentTrace {
|
|
|
44
45
|
model String? @db.VarChar(128)
|
|
45
46
|
usage Json? // { inputTokens, outputTokens, cache* }
|
|
46
47
|
latencyMs Int? @map("latency_ms")
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
// The inbound message's transport id, on the user row of the run it started: the
|
|
49
|
+
// idempotency key. Set to null when the run fails, so a redelivery runs again.
|
|
50
|
+
externalMessageId String? @map("external_message_id")
|
|
51
|
+
interrupted Boolean @default(false)
|
|
52
|
+
failed Boolean @default(false)
|
|
53
|
+
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
|
|
49
54
|
|
|
50
55
|
@@index([conversationId, createdAt])
|
|
56
|
+
// One run per inbound message: a duplicate delivery converges on the first run's row.
|
|
57
|
+
// NULLs are distinct in Postgres, so assistant rows are unaffected.
|
|
58
|
+
@@unique([conversationId, externalMessageId])
|
|
51
59
|
@@index([runId])
|
|
52
60
|
@@map("simple_agent_messages_llm")
|
|
53
61
|
}
|