@restatedev/agents 0.3.0 → 0.5.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 +107 -53
- package/dist/calls.d.ts +29 -5
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +77 -10
- package/dist/calls.js.map +1 -1
- package/dist/claude/index.d.ts +36 -7
- package/dist/claude/index.d.ts.map +1 -1
- package/dist/claude/index.js +232 -151
- package/dist/claude/index.js.map +1 -1
- package/dist/client.d.ts +12 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/common.d.ts +151 -70
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +246 -204
- package/dist/common.js.map +1 -1
- package/dist/conversation.d.ts +478 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +1256 -0
- package/dist/conversation.js.map +1 -0
- package/dist/deepseek/index.d.ts +38 -5
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +322 -106
- package/dist/deepseek/index.js.map +1 -1
- package/dist/deepseek/persistence.d.ts +9 -3
- package/dist/deepseek/persistence.d.ts.map +1 -1
- package/dist/deepseek/persistence.js +12 -6
- package/dist/deepseek/persistence.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/langgraph/checkpointer.d.ts +16 -2
- package/dist/langgraph/checkpointer.d.ts.map +1 -1
- package/dist/langgraph/checkpointer.js +133 -26
- package/dist/langgraph/checkpointer.js.map +1 -1
- package/dist/langgraph/context.d.ts +10 -0
- package/dist/langgraph/context.d.ts.map +1 -0
- package/dist/langgraph/context.js +21 -0
- package/dist/langgraph/context.js.map +1 -0
- package/dist/langgraph/errors.d.ts +16 -0
- package/dist/langgraph/errors.d.ts.map +1 -0
- package/dist/langgraph/errors.js +30 -0
- package/dist/langgraph/errors.js.map +1 -0
- package/dist/langgraph/graph.d.ts +25 -0
- package/dist/langgraph/graph.d.ts.map +1 -0
- package/dist/langgraph/graph.js +346 -0
- package/dist/langgraph/graph.js.map +1 -0
- package/dist/langgraph/index.d.ts +65 -12
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/langgraph/index.js +227 -171
- package/dist/langgraph/index.js.map +1 -1
- package/dist/langgraph/nodes.d.ts +70 -0
- package/dist/langgraph/nodes.d.ts.map +1 -0
- package/dist/langgraph/nodes.js +145 -0
- package/dist/langgraph/nodes.js.map +1 -0
- package/dist/messages.d.ts +18 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +182 -0
- package/dist/messages.js.map +1 -0
- package/dist/object.d.ts +65 -23
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +123 -35
- package/dist/object.js.map +1 -1
- package/dist/opencode/db.d.ts +51 -10
- package/dist/opencode/db.d.ts.map +1 -1
- package/dist/opencode/db.js +122 -34
- package/dist/opencode/db.js.map +1 -1
- package/dist/opencode/index.d.ts +105 -21
- package/dist/opencode/index.d.ts.map +1 -1
- package/dist/opencode/index.js +827 -279
- package/dist/opencode/index.js.map +1 -1
- package/dist/output.d.ts +15 -7
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +44 -26
- package/dist/output.js.map +1 -1
- package/dist/pi/index.d.ts +39 -11
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +281 -77
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/storage.d.ts +29 -4
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +18 -6
- package/dist/pi/storage.js.map +1 -1
- package/dist/signals.d.ts +74 -4
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +144 -15
- package/dist/signals.js.map +1 -1
- package/dist/steering.d.ts +31 -0
- package/dist/steering.d.ts.map +1 -0
- package/dist/steering.js +45 -0
- package/dist/steering.js.map +1 -0
- package/dist/tools.d.ts +28 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +34 -5
- package/dist/tools.js.map +1 -1
- package/package.json +42 -13
- package/src/calls.ts +107 -15
- package/src/claude/README.md +41 -20
- package/src/claude/index.ts +266 -178
- package/src/client.ts +14 -3
- package/src/common.ts +317 -232
- package/src/conversation.ts +1547 -0
- package/src/deepseek/README.md +78 -11
- package/src/deepseek/index.ts +384 -137
- package/src/deepseek/persistence.ts +11 -8
- package/src/index.ts +13 -4
- package/src/langgraph/README.md +115 -25
- package/src/langgraph/checkpointer.ts +168 -28
- package/src/langgraph/context.ts +27 -0
- package/src/langgraph/errors.ts +26 -0
- package/src/langgraph/graph.ts +396 -0
- package/src/langgraph/index.ts +326 -181
- package/src/langgraph/nodes.ts +243 -0
- package/src/messages.ts +244 -0
- package/src/object.ts +169 -38
- package/src/opencode/README.md +172 -66
- package/src/opencode/db.ts +204 -40
- package/src/opencode/index.ts +1010 -325
- package/src/output.ts +52 -31
- package/src/pi/README.md +18 -3
- package/src/pi/index.ts +346 -78
- package/src/pi/storage.ts +20 -7
- package/src/signals.ts +225 -19
- package/src/steering.ts +67 -0
- package/src/tools.ts +61 -8
- package/dist/claude/conversation.d.ts +0 -33
- package/dist/claude/conversation.d.ts.map +0 -1
- package/dist/claude/conversation.js +0 -95
- package/dist/claude/conversation.js.map +0 -1
- package/dist/log.d.ts +0 -29
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -146
- package/dist/log.js.map +0 -1
- package/src/claude/conversation.ts +0 -114
- package/src/log.ts +0 -194
package/dist/log.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { Schema, ScopeKey } from '@restatedev/sessions/internal';
|
|
2
|
-
const TABLE = 'restate_agent_log';
|
|
3
|
-
const SCHEMA = new Schema('restate-agents.log', [
|
|
4
|
-
`CREATE TABLE ${TABLE} (
|
|
5
|
-
id INTEGER PRIMARY KEY,
|
|
6
|
-
seq INTEGER NOT NULL UNIQUE,
|
|
7
|
-
position INTEGER UNIQUE,
|
|
8
|
-
message TEXT NOT NULL
|
|
9
|
-
)`,
|
|
10
|
-
// Where the log starts, which `clear` sets: no row before.
|
|
11
|
-
`CREATE TABLE ${TABLE}_start (
|
|
12
|
-
row INTEGER PRIMARY KEY CHECK (row = 0),
|
|
13
|
-
seq INTEGER NOT NULL,
|
|
14
|
-
id INTEGER NOT NULL
|
|
15
|
-
)`,
|
|
16
|
-
]);
|
|
17
|
-
/** Where the log of `db` starts, if `clear` set it. */
|
|
18
|
-
const startOf = async (db) => db.get(`SELECT seq, id FROM ${TABLE}_start`);
|
|
19
|
-
/**
|
|
20
|
-
* Start the log at `start`, in the transaction `tx` of `clear` that emptied
|
|
21
|
-
* the database: the next conversation's ids and cursors go on from there.
|
|
22
|
-
*/
|
|
23
|
-
export async function restart(tx, start) {
|
|
24
|
-
await tx.execute(`INSERT OR REPLACE INTO ${TABLE}_start (row, seq, id) VALUES (0, ?, ?)`, [start.seq, start.id]);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* The log's head, as the attempt's last `sync` stored it: the next compares
|
|
28
|
-
* the transcript with it, rather than read the whole log again at each step.
|
|
29
|
-
*/
|
|
30
|
-
const HEAD = new ScopeKey('the log');
|
|
31
|
-
/** The log's head in the attempt of `ctx`: as it stored it last, or else as the log has it. */
|
|
32
|
-
async function headOf(ctx) {
|
|
33
|
-
const cached = HEAD.get(ctx);
|
|
34
|
-
if (cached)
|
|
35
|
-
return cached;
|
|
36
|
-
const { db } = ctx;
|
|
37
|
-
const rows = (await db.values(`SELECT id, message FROM ${TABLE} WHERE position IS NOT NULL ORDER BY position`)).map(([id, json]) => ({ id: Number(id), message: JSON.parse(String(json)), json: String(json) }));
|
|
38
|
-
const { last, next } = (await db.get(`SELECT MAX(seq) AS last, MAX(id) + 1 AS next FROM ${TABLE}`));
|
|
39
|
-
if (last !== null)
|
|
40
|
-
return { rows, seq: last, next: next };
|
|
41
|
-
const from = (await startOf(db)) ?? { seq: 0, id: 0 };
|
|
42
|
-
return { rows, seq: from.seq, next: from.id };
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Store the transcript's `messages`, in order, in the log: those that
|
|
46
|
-
* changed since the last time, in one transaction.
|
|
47
|
-
*/
|
|
48
|
-
export async function sync(ctx, messages) {
|
|
49
|
-
const { db } = ctx;
|
|
50
|
-
await SCHEMA.ensure(db);
|
|
51
|
-
const head = await headOf(ctx);
|
|
52
|
-
// A message the transcript did not change is the one stored (see Transcript in common.ts):
|
|
53
|
-
// only the others are compared, as JSON.
|
|
54
|
-
const jsons = [];
|
|
55
|
-
const jsonOf = (position) => (jsons[position] ??= JSON.stringify(messages[position]));
|
|
56
|
-
const unchanged = (position) => {
|
|
57
|
-
const row = head.rows[position];
|
|
58
|
-
return row.message === messages[position] || row.json === jsonOf(position);
|
|
59
|
-
};
|
|
60
|
-
const both = Math.min(head.rows.length, messages.length);
|
|
61
|
-
let same = 0;
|
|
62
|
-
while (same < both && unchanged(same))
|
|
63
|
-
same += 1;
|
|
64
|
-
let grown = same;
|
|
65
|
-
while (grown < both && grownFrom(messages[grown], head.rows[grown].message))
|
|
66
|
-
grown += 1;
|
|
67
|
-
if (same === head.rows.length && same === messages.length)
|
|
68
|
-
return;
|
|
69
|
-
let { seq, next: id } = head;
|
|
70
|
-
const rows = head.rows.slice(0, same);
|
|
71
|
-
try {
|
|
72
|
-
await db.transaction(async (tx) => {
|
|
73
|
-
for (let position = same; position < grown; position += 1) {
|
|
74
|
-
const row = { id: head.rows[position].id, message: messages[position], json: jsonOf(position) };
|
|
75
|
-
await tx.execute(`UPDATE ${TABLE} SET seq = ?, message = ? WHERE id = ?`, [(seq += 1), row.json, row.id]);
|
|
76
|
-
rows.push(row);
|
|
77
|
-
}
|
|
78
|
-
// The rest of the transcript as it was is replaced: its messages stay in the log, out of it.
|
|
79
|
-
await tx.execute(`UPDATE ${TABLE} SET position = NULL WHERE position >= ?`, [grown]);
|
|
80
|
-
for (let position = grown; position < messages.length; position += 1) {
|
|
81
|
-
const row = { id: id++, message: messages[position], json: jsonOf(position) };
|
|
82
|
-
await tx.execute(`INSERT INTO ${TABLE} VALUES (?, ?, ?, ?)`, [row.id, (seq += 1), position, row.json]);
|
|
83
|
-
rows.push(row);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
// Rolled back: the log is as it was, which the next sync reads again.
|
|
89
|
-
HEAD.delete(ctx);
|
|
90
|
-
throw error;
|
|
91
|
-
}
|
|
92
|
-
HEAD.set(ctx, { rows, seq, next: id });
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* A page of the log. With `cursor`: the messages new or changed since it,
|
|
96
|
-
* by when they changed, and the cursor to read on from. Without: the latest
|
|
97
|
-
* `limit` messages (or those before `before`, an id), oldest first, and the
|
|
98
|
-
* cursor to follow the conversation from.
|
|
99
|
-
*/
|
|
100
|
-
export async function page(db, { limit = 50, before, cursor }) {
|
|
101
|
-
await SCHEMA.ensure(db);
|
|
102
|
-
const start = await startOf(db);
|
|
103
|
-
const first = start ? { first: start.id } : {};
|
|
104
|
-
if (cursor !== undefined) {
|
|
105
|
-
const rows = await db.values(`SELECT id, message, seq FROM ${TABLE} WHERE seq > ? ORDER BY seq LIMIT ?`, [
|
|
106
|
-
cursor,
|
|
107
|
-
limit + 1,
|
|
108
|
-
]);
|
|
109
|
-
const shown = rows.slice(0, limit);
|
|
110
|
-
return {
|
|
111
|
-
messages: shown.map(message),
|
|
112
|
-
more: rows.length > limit,
|
|
113
|
-
cursor: shown.length > 0 ? Number(shown.at(-1)[2]) : cursor,
|
|
114
|
-
...first,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
const { now } = (await db.get(`SELECT COALESCE(MAX(seq), ?) AS now FROM ${TABLE}`, [
|
|
118
|
-
start?.seq ?? 0,
|
|
119
|
-
]));
|
|
120
|
-
const rows = await db.values(`SELECT id, message FROM ${TABLE} WHERE id < ? ORDER BY id DESC LIMIT ?`, [
|
|
121
|
-
before ?? Number.MAX_SAFE_INTEGER,
|
|
122
|
-
limit + 1,
|
|
123
|
-
]);
|
|
124
|
-
return {
|
|
125
|
-
messages: rows.slice(0, limit).toReversed().map(message),
|
|
126
|
-
more: rows.length > limit,
|
|
127
|
-
cursor: now,
|
|
128
|
-
...first,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
/** A row of the log, as `messages` gives it. */
|
|
132
|
-
const message = ([id, m]) => ({ id: Number(id), ...JSON.parse(String(m)) });
|
|
133
|
-
/** Whether `now` is `was` grown in place: the same role and parts, with more (a tool call's output, say). */
|
|
134
|
-
function grownFrom(now, was) {
|
|
135
|
-
if (was.role !== now.role || now.parts.length < was.parts.length)
|
|
136
|
-
return false;
|
|
137
|
-
return was.parts.every((part, i) => {
|
|
138
|
-
const other = now.parts[i];
|
|
139
|
-
if (part.type !== other.type)
|
|
140
|
-
return false;
|
|
141
|
-
if (part.type === 'text')
|
|
142
|
-
return other.type === 'text' && other.text.startsWith(part.text);
|
|
143
|
-
return other.type === 'tool' && other.id === part.id;
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
//# sourceMappingURL=log.js.map
|
package/dist/log.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"log.js","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AAGjE,MAAM,KAAK,GAAG,mBAAmB,CAAC;AAalC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,oBAAoB,EAAE;IAC9C,gBAAgB,KAAK;;;;;IAKnB;IACF,2DAA2D;IAC3D,gBAAgB,KAAK;;;;IAInB;CACH,CAAC,CAAC;AAEH,uDAAuD;AACvD,MAAM,OAAO,GAAG,KAAK,EAAE,EAAkB,EAA8B,EAAE,CACvE,EAAE,CAAC,GAAG,CAAQ,uBAAuB,KAAK,QAAQ,CAAC,CAAC;AAEtD;;;GAGG;AACH,MAAM,CAAC,KAAK,U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QAAE,OAAO,KAAK,CAAC;IAC/E,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE;QACjC,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC;QAC5B,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI;YAAE,OAAO,KAAK,CAAC;QAC3C,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM;YAAE,OAAO,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3F,OAAO,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
// A conversation with Claude in a Restate virtual object's database: its
|
|
2
|
-
// messages, in order, each with the id of the prompt it belongs to, and the
|
|
3
|
-
// answer to each prompt.
|
|
4
|
-
//
|
|
5
|
-
// It is the only state of the tool runner (@anthropic-ai/sdk's agent loop),
|
|
6
|
-
// which keeps the conversation in its params: an attempt starts a runner with
|
|
7
|
-
// the stored messages, and stores each message the runner adds.
|
|
8
|
-
|
|
9
|
-
import type Anthropic from '@anthropic-ai/sdk';
|
|
10
|
-
import type { Database } from '@restatedev/sessions';
|
|
11
|
-
import { Schema } from '@restatedev/sessions/internal';
|
|
12
|
-
|
|
13
|
-
type MessageParam = Anthropic.Beta.Messages.BetaMessageParam;
|
|
14
|
-
|
|
15
|
-
const SCHEMA = new Schema('restate-agents.claude', [
|
|
16
|
-
'CREATE TABLE restate_claude_messages (seq INTEGER PRIMARY KEY, prompt TEXT NOT NULL, message TEXT NOT NULL)',
|
|
17
|
-
'CREATE TABLE restate_claude_answers (prompt TEXT PRIMARY KEY, answer TEXT NOT NULL)',
|
|
18
|
-
]);
|
|
19
|
-
|
|
20
|
-
/** The conversation in the object's database: the tool runner's messages, and each prompt's answer. */
|
|
21
|
-
export class Conversation {
|
|
22
|
-
#db: Database;
|
|
23
|
-
/** Cancelled: a run cut off then stores nothing more. */
|
|
24
|
-
#closed = false;
|
|
25
|
-
|
|
26
|
-
constructor(db: Database) {
|
|
27
|
-
this.#db = db;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** The conversation in `db` (`ctx.db`). */
|
|
31
|
-
static async open(db: Database): Promise<Conversation> {
|
|
32
|
-
await SCHEMA.ensure(db);
|
|
33
|
-
return new Conversation(db);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** The answer to prompt `id`, or undefined. */
|
|
37
|
-
async answer(id: string): Promise<string | undefined> {
|
|
38
|
-
const [row] = await this.#db.values('SELECT answer FROM restate_claude_answers WHERE prompt = ?', [id]);
|
|
39
|
-
return row?.[0] as string | undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** The messages, in order. */
|
|
43
|
-
async messages(): Promise<MessageParam[]> {
|
|
44
|
-
const rows = await this.#db.values('SELECT message FROM restate_claude_messages ORDER BY seq');
|
|
45
|
-
return rows.map(([message]) => JSON.parse(String(message)));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** The messages stored after `seq`, in order, each with its own `seq`. */
|
|
49
|
-
async since(seq: number): Promise<[seq: number, message: MessageParam][]> {
|
|
50
|
-
const rows = await this.#db.values('SELECT seq, message FROM restate_claude_messages WHERE seq > ? ORDER BY seq', [
|
|
51
|
-
seq,
|
|
52
|
-
]);
|
|
53
|
-
return rows.map(([at, message]) => [Number(at), JSON.parse(String(message)) as MessageParam]);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** The prompt being answered: the last message's, unless it is answered. */
|
|
57
|
-
async pending(): Promise<string | undefined> {
|
|
58
|
-
const [row] = await this.#db.values(
|
|
59
|
-
'SELECT prompt FROM restate_claude_messages WHERE prompt NOT IN (SELECT prompt FROM restate_claude_answers) ORDER BY seq DESC LIMIT 1',
|
|
60
|
-
);
|
|
61
|
-
return row?.[0] as string | undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Close prompt `id`, which was cancelled: a result for each tool call of
|
|
66
|
-
* the last reply that has none, then `text` as the reply and the answer.
|
|
67
|
-
*/
|
|
68
|
-
async cancel(id: string, text: string): Promise<void> {
|
|
69
|
-
this.#closed = true;
|
|
70
|
-
await this.close(id, text);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Close prompt `id` with `text` as its last reply and its answer, after
|
|
75
|
-
* `reply` if given (a reply that cannot go on: cut off, or declined): first
|
|
76
|
-
* a result of `text` for each tool call of the last reply that has none.
|
|
77
|
-
*/
|
|
78
|
-
async close(id: string, text: string, reply?: MessageParam): Promise<void> {
|
|
79
|
-
const calls = toolUses(reply ?? (await this.messages()).at(-1));
|
|
80
|
-
const results = calls.map((call) => ({
|
|
81
|
-
type: 'tool_result' as const,
|
|
82
|
-
tool_use_id: call.id,
|
|
83
|
-
content: text,
|
|
84
|
-
is_error: true,
|
|
85
|
-
}));
|
|
86
|
-
const closing: MessageParam[] = results.length > 0 ? [{ role: 'user', content: results }] : [];
|
|
87
|
-
await this.#store(id, [...(reply ? [reply] : []), ...closing, { role: 'assistant', content: text }], text);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Store `messages` of prompt `id`, and its `answer` if given, in one transaction. */
|
|
91
|
-
append(id: string, messages: MessageParam[], answer?: string): Promise<void> {
|
|
92
|
-
if (this.#closed) return Promise.reject(new Error('the conversation was cancelled'));
|
|
93
|
-
return this.#store(id, messages, answer);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
#store(id: string, messages: MessageParam[], answer?: string): Promise<void> {
|
|
97
|
-
return this.#db.transaction(async (tx) => {
|
|
98
|
-
for (const message of messages) {
|
|
99
|
-
await tx.execute('INSERT INTO restate_claude_messages (prompt, message) VALUES (?, ?)', [
|
|
100
|
-
id,
|
|
101
|
-
JSON.stringify(message),
|
|
102
|
-
]);
|
|
103
|
-
}
|
|
104
|
-
if (answer !== undefined)
|
|
105
|
-
await tx.execute('INSERT INTO restate_claude_answers (prompt, answer) VALUES (?, ?)', [id, answer]);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The tool calls of `message`, if it is the assistant's. */
|
|
111
|
-
const toolUses = (message: MessageParam | undefined) =>
|
|
112
|
-
message?.role === 'assistant' && Array.isArray(message.content)
|
|
113
|
-
? message.content.filter((b) => b.type === 'tool_use')
|
|
114
|
-
: [];
|
package/src/log.ts
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
// An agent's conversation as clients read it: a log in the object's
|
|
2
|
-
// database, kept in step with the framework's transcript each time the agent
|
|
3
|
-
// publishes a change (`transcribing` in common.ts), and read by `messages`
|
|
4
|
-
// and `sync`.
|
|
5
|
-
//
|
|
6
|
-
// Each message has an `id` that never changes, and a `seq`: the sequence
|
|
7
|
-
// number of its last change, which is the cursor. A message that grows in
|
|
8
|
-
// place (a tool call's output comes) keeps its id and takes a new `seq`; a
|
|
9
|
-
// transcript replaced (compacted, reset) leaves its messages in the log, and
|
|
10
|
-
// the messages after them are new ones. So a client reads from a cursor, and
|
|
11
|
-
// gets each message that is new or changed since, once.
|
|
12
|
-
//
|
|
13
|
-
// `clear` forgets the log with the rest of the conversation, but not where it
|
|
14
|
-
// got to: the next conversation's ids and cursors go on from there (its
|
|
15
|
-
// start, a row of `restate_agent_log_start`), so a client following it reads
|
|
16
|
-
// on, and drops the messages before `first`.
|
|
17
|
-
import type { DatabaseReader, DatabaseWriter, SessionContext } from '@restatedev/sessions';
|
|
18
|
-
import { Schema, ScopeKey } from '@restatedev/sessions/internal';
|
|
19
|
-
import type { ChatMessage, Messages, MessagesRequest } from './common.ts';
|
|
20
|
-
|
|
21
|
-
const TABLE = 'restate_agent_log';
|
|
22
|
-
|
|
23
|
-
/** Where the log starts: past the ids and cursors of the conversations `clear` forgot. */
|
|
24
|
-
export interface Start {
|
|
25
|
-
/** The cursor it starts at. */
|
|
26
|
-
seq: number;
|
|
27
|
-
/** Its first message's id. */
|
|
28
|
-
id: number;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** A message as the transcript gives it: its role and parts, without an id. */
|
|
32
|
-
export type Said = Omit<ChatMessage, 'id'>;
|
|
33
|
-
|
|
34
|
-
const SCHEMA = new Schema('restate-agents.log', [
|
|
35
|
-
`CREATE TABLE ${TABLE} (
|
|
36
|
-
id INTEGER PRIMARY KEY,
|
|
37
|
-
seq INTEGER NOT NULL UNIQUE,
|
|
38
|
-
position INTEGER UNIQUE,
|
|
39
|
-
message TEXT NOT NULL
|
|
40
|
-
)`,
|
|
41
|
-
// Where the log starts, which `clear` sets: no row before.
|
|
42
|
-
`CREATE TABLE ${TABLE}_start (
|
|
43
|
-
row INTEGER PRIMARY KEY CHECK (row = 0),
|
|
44
|
-
seq INTEGER NOT NULL,
|
|
45
|
-
id INTEGER NOT NULL
|
|
46
|
-
)`,
|
|
47
|
-
]);
|
|
48
|
-
|
|
49
|
-
/** Where the log of `db` starts, if `clear` set it. */
|
|
50
|
-
const startOf = async (db: DatabaseReader): Promise<Start | undefined> =>
|
|
51
|
-
db.get<Start>(`SELECT seq, id FROM ${TABLE}_start`);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Start the log at `start`, in the transaction `tx` of `clear` that emptied
|
|
55
|
-
* the database: the next conversation's ids and cursors go on from there.
|
|
56
|
-
*/
|
|
57
|
-
export async function restart(tx: DatabaseWriter, start: Start) {
|
|
58
|
-
await tx.execute(`INSERT OR REPLACE INTO ${TABLE}_start (row, seq, id) VALUES (0, ?, ?)`, [start.seq, start.id]);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** A message of the log's head, the transcript as last stored: its id, and the message, also as JSON. */
|
|
62
|
-
interface Row {
|
|
63
|
-
id: number;
|
|
64
|
-
message: Said;
|
|
65
|
-
json: string;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** The log's head as a session stored it last, with the last cursor and the next id. */
|
|
69
|
-
interface Head {
|
|
70
|
-
rows: Row[];
|
|
71
|
-
seq: number;
|
|
72
|
-
next: number;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* The log's head, as the attempt's last `sync` stored it: the next compares
|
|
77
|
-
* the transcript with it, rather than read the whole log again at each step.
|
|
78
|
-
*/
|
|
79
|
-
const HEAD = new ScopeKey<Head>('the log');
|
|
80
|
-
|
|
81
|
-
/** The log's head in the attempt of `ctx`: as it stored it last, or else as the log has it. */
|
|
82
|
-
async function headOf(ctx: SessionContext): Promise<Head> {
|
|
83
|
-
const cached = HEAD.get(ctx);
|
|
84
|
-
if (cached) return cached;
|
|
85
|
-
const { db } = ctx;
|
|
86
|
-
const rows = (await db.values(`SELECT id, message FROM ${TABLE} WHERE position IS NOT NULL ORDER BY position`)).map(
|
|
87
|
-
([id, json]) => ({ id: Number(id), message: JSON.parse(String(json)) as Said, json: String(json) }),
|
|
88
|
-
);
|
|
89
|
-
const { last, next } = (await db.get<{ last: number | null; next: number | null }>(
|
|
90
|
-
`SELECT MAX(seq) AS last, MAX(id) + 1 AS next FROM ${TABLE}`,
|
|
91
|
-
))!;
|
|
92
|
-
if (last !== null) return { rows, seq: last, next: next! };
|
|
93
|
-
const from = (await startOf(db)) ?? { seq: 0, id: 0 };
|
|
94
|
-
return { rows, seq: from.seq, next: from.id };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Store the transcript's `messages`, in order, in the log: those that
|
|
99
|
-
* changed since the last time, in one transaction.
|
|
100
|
-
*/
|
|
101
|
-
export async function sync(ctx: SessionContext, messages: Said[]) {
|
|
102
|
-
const { db } = ctx;
|
|
103
|
-
await SCHEMA.ensure(db);
|
|
104
|
-
const head = await headOf(ctx);
|
|
105
|
-
// A message the transcript did not change is the one stored (see Transcript in common.ts):
|
|
106
|
-
// only the others are compared, as JSON.
|
|
107
|
-
const jsons: string[] = [];
|
|
108
|
-
const jsonOf = (position: number) => (jsons[position] ??= JSON.stringify(messages[position]));
|
|
109
|
-
const unchanged = (position: number) => {
|
|
110
|
-
const row = head.rows[position]!;
|
|
111
|
-
return row.message === messages[position] || row.json === jsonOf(position);
|
|
112
|
-
};
|
|
113
|
-
const both = Math.min(head.rows.length, messages.length);
|
|
114
|
-
let same = 0;
|
|
115
|
-
while (same < both && unchanged(same)) same += 1;
|
|
116
|
-
let grown = same;
|
|
117
|
-
while (grown < both && grownFrom(messages[grown]!, head.rows[grown]!.message)) grown += 1;
|
|
118
|
-
if (same === head.rows.length && same === messages.length) return;
|
|
119
|
-
let { seq, next: id } = head;
|
|
120
|
-
const rows = head.rows.slice(0, same);
|
|
121
|
-
try {
|
|
122
|
-
await db.transaction(async (tx) => {
|
|
123
|
-
for (let position = same; position < grown; position += 1) {
|
|
124
|
-
const row = { id: head.rows[position]!.id, message: messages[position]!, json: jsonOf(position) };
|
|
125
|
-
await tx.execute(`UPDATE ${TABLE} SET seq = ?, message = ? WHERE id = ?`, [(seq += 1), row.json, row.id]);
|
|
126
|
-
rows.push(row);
|
|
127
|
-
}
|
|
128
|
-
// The rest of the transcript as it was is replaced: its messages stay in the log, out of it.
|
|
129
|
-
await tx.execute(`UPDATE ${TABLE} SET position = NULL WHERE position >= ?`, [grown]);
|
|
130
|
-
for (let position = grown; position < messages.length; position += 1) {
|
|
131
|
-
const row = { id: id++, message: messages[position]!, json: jsonOf(position) };
|
|
132
|
-
await tx.execute(`INSERT INTO ${TABLE} VALUES (?, ?, ?, ?)`, [row.id, (seq += 1), position, row.json]);
|
|
133
|
-
rows.push(row);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
} catch (error) {
|
|
137
|
-
// Rolled back: the log is as it was, which the next sync reads again.
|
|
138
|
-
HEAD.delete(ctx);
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
HEAD.set(ctx, { rows, seq, next: id });
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* A page of the log. With `cursor`: the messages new or changed since it,
|
|
146
|
-
* by when they changed, and the cursor to read on from. Without: the latest
|
|
147
|
-
* `limit` messages (or those before `before`, an id), oldest first, and the
|
|
148
|
-
* cursor to follow the conversation from.
|
|
149
|
-
*/
|
|
150
|
-
export async function page(db: DatabaseReader, { limit = 50, before, cursor }: MessagesRequest): Promise<Messages> {
|
|
151
|
-
await SCHEMA.ensure(db);
|
|
152
|
-
const start = await startOf(db);
|
|
153
|
-
const first = start ? { first: start.id } : {};
|
|
154
|
-
if (cursor !== undefined) {
|
|
155
|
-
const rows = await db.values(`SELECT id, message, seq FROM ${TABLE} WHERE seq > ? ORDER BY seq LIMIT ?`, [
|
|
156
|
-
cursor,
|
|
157
|
-
limit + 1,
|
|
158
|
-
]);
|
|
159
|
-
const shown = rows.slice(0, limit);
|
|
160
|
-
return {
|
|
161
|
-
messages: shown.map(message),
|
|
162
|
-
more: rows.length > limit,
|
|
163
|
-
cursor: shown.length > 0 ? Number(shown.at(-1)![2]) : cursor,
|
|
164
|
-
...first,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
const { now } = (await db.get<{ now: number }>(`SELECT COALESCE(MAX(seq), ?) AS now FROM ${TABLE}`, [
|
|
168
|
-
start?.seq ?? 0,
|
|
169
|
-
]))!;
|
|
170
|
-
const rows = await db.values(`SELECT id, message FROM ${TABLE} WHERE id < ? ORDER BY id DESC LIMIT ?`, [
|
|
171
|
-
before ?? Number.MAX_SAFE_INTEGER,
|
|
172
|
-
limit + 1,
|
|
173
|
-
]);
|
|
174
|
-
return {
|
|
175
|
-
messages: rows.slice(0, limit).toReversed().map(message),
|
|
176
|
-
more: rows.length > limit,
|
|
177
|
-
cursor: now,
|
|
178
|
-
...first,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** A row of the log, as `messages` gives it. */
|
|
183
|
-
const message = ([id, m]: unknown[]): ChatMessage => ({ id: Number(id), ...(JSON.parse(String(m)) as Said) });
|
|
184
|
-
|
|
185
|
-
/** Whether `now` is `was` grown in place: the same role and parts, with more (a tool call's output, say). */
|
|
186
|
-
function grownFrom(now: Said, was: Said) {
|
|
187
|
-
if (was.role !== now.role || now.parts.length < was.parts.length) return false;
|
|
188
|
-
return was.parts.every((part, i) => {
|
|
189
|
-
const other = now.parts[i]!;
|
|
190
|
-
if (part.type !== other.type) return false;
|
|
191
|
-
if (part.type === 'text') return other.type === 'text' && other.text.startsWith(part.text);
|
|
192
|
-
return other.type === 'tool' && other.id === part.id;
|
|
193
|
-
});
|
|
194
|
-
}
|