@restatedev/agents 0.0.0 → 0.1.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/LICENSE +21 -0
- package/README.md +688 -2
- package/dist/calls.d.ts +29 -0
- package/dist/calls.d.ts.map +1 -0
- package/dist/calls.js +91 -0
- package/dist/calls.js.map +1 -0
- package/dist/claude/conversation.d.ts +33 -0
- package/dist/claude/conversation.d.ts.map +1 -0
- package/dist/claude/conversation.js +95 -0
- package/dist/claude/conversation.js.map +1 -0
- package/dist/claude/index.d.ts +78 -0
- package/dist/claude/index.d.ts.map +1 -0
- package/dist/claude/index.js +269 -0
- package/dist/claude/index.js.map +1 -0
- package/dist/client.d.ts +83 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/common.d.ts +284 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +475 -0
- package/dist/common.js.map +1 -0
- package/dist/deepseek/index.d.ts +69 -0
- package/dist/deepseek/index.d.ts.map +1 -0
- package/dist/deepseek/index.js +381 -0
- package/dist/deepseek/index.js.map +1 -0
- package/dist/deepseek/persistence.d.ts +61 -0
- package/dist/deepseek/persistence.d.ts.map +1 -0
- package/dist/deepseek/persistence.js +224 -0
- package/dist/deepseek/persistence.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/langgraph/checkpointer.d.ts +29 -0
- package/dist/langgraph/checkpointer.d.ts.map +1 -0
- package/dist/langgraph/checkpointer.js +275 -0
- package/dist/langgraph/checkpointer.js.map +1 -0
- package/dist/langgraph/index.d.ts +67 -0
- package/dist/langgraph/index.d.ts.map +1 -0
- package/dist/langgraph/index.js +265 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/log.d.ts +29 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +145 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +181 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +232 -0
- package/dist/object.js.map +1 -0
- package/dist/opencode/db.d.ts +40 -0
- package/dist/opencode/db.d.ts.map +1 -0
- package/dist/opencode/db.js +161 -0
- package/dist/opencode/db.js.map +1 -0
- package/dist/opencode/index.d.ts +101 -0
- package/dist/opencode/index.d.ts.map +1 -0
- package/dist/opencode/index.js +464 -0
- package/dist/opencode/index.js.map +1 -0
- package/dist/output.d.ts +17 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +81 -0
- package/dist/output.js.map +1 -0
- package/dist/pi/index.d.ts +90 -0
- package/dist/pi/index.d.ts.map +1 -0
- package/dist/pi/index.js +313 -0
- package/dist/pi/index.js.map +1 -0
- package/dist/pi/storage.d.ts +5 -0
- package/dist/pi/storage.d.ts.map +1 -0
- package/dist/pi/storage.js +41 -0
- package/dist/pi/storage.js.map +1 -0
- package/dist/signals.d.ts +195 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +258 -0
- package/dist/signals.js.map +1 -0
- package/dist/tools.d.ts +158 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +211 -0
- package/dist/tools.js.map +1 -0
- package/dist/webhook.d.ts +73 -0
- package/dist/webhook.d.ts.map +1 -0
- package/dist/webhook.js +104 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +218 -5
- package/src/calls.ts +138 -0
- package/src/claude/README.md +84 -0
- package/src/claude/conversation.ts +114 -0
- package/src/claude/index.ts +364 -0
- package/src/client.ts +179 -0
- package/src/common.ts +593 -0
- package/src/deepseek/README.md +110 -0
- package/src/deepseek/index.ts +487 -0
- package/src/deepseek/persistence.ts +293 -0
- package/src/index.ts +60 -0
- package/src/langgraph/README.md +87 -0
- package/src/langgraph/checkpointer.ts +364 -0
- package/src/langgraph/index.ts +349 -0
- package/src/log.ts +193 -0
- package/src/object.ts +343 -0
- package/src/opencode/README.md +125 -0
- package/src/opencode/db.ts +223 -0
- package/src/opencode/index.ts +595 -0
- package/src/output.ts +95 -0
- package/src/pi/README.md +105 -0
- package/src/pi/index.ts +402 -0
- package/src/pi/storage.ts +65 -0
- package/src/signals.ts +426 -0
- package/src/tools.ts +343 -0
- package/src/webhook.ts +163 -0
package/dist/calls.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type * as restate from '@restatedev/sessions';
|
|
2
|
+
/** What the calls need of a handler's context. */
|
|
3
|
+
type CallsContext = Pick<restate.SessionContext, 'db' | 'request'>;
|
|
4
|
+
/**
|
|
5
|
+
* The call of tool `tool` with `input` that tool call `callId` makes: the
|
|
6
|
+
* one it made before (a run again), or the one an earlier attempt made with
|
|
7
|
+
* the same tool and input (made again under a new id), or else a new one.
|
|
8
|
+
* `run(id)` makes it with its own id, and stores that it answered.
|
|
9
|
+
*/
|
|
10
|
+
export declare function callOf(ctx: CallsContext, tool: string, input: unknown, callId: string): Promise<{
|
|
11
|
+
id: string;
|
|
12
|
+
/**
|
|
13
|
+
* It answered, with `failed` if a terminal error answered it: kept until
|
|
14
|
+
* the transcript has its result.
|
|
15
|
+
*/
|
|
16
|
+
answered(failed?: string): Promise<void>;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* Forget the calls that answered whose result the framework stored: those
|
|
20
|
+
* of which `stored(id, failed)` is true for the last id they were made with,
|
|
21
|
+
* `failed` the error that answered the call, if one did. Called
|
|
22
|
+
* at each publish, and before each tool call, with the transcript as the
|
|
23
|
+
* framework stored it: also in an attempt that made no call yet, which would
|
|
24
|
+
* otherwise take one an earlier attempt made, and the model has the result
|
|
25
|
+
* of.
|
|
26
|
+
*/
|
|
27
|
+
export declare function settle(ctx: CallsContext, stored: (id: string, failed?: string) => boolean): Promise<void>;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=calls.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calls.d.ts","sourceRoot":"","sources":["../src/calls.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,KAAK,OAAO,MAAM,sBAAsB,CAAC;AA+BrD,kDAAkD;AAClD,KAAK,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,GAAG,SAAS,CAAC,CAAC;AAmCnE;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM;;IAiBxF;;;OAGG;sBACqB,MAAM;GAMjC;AAED;;;;;;;;GAQG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,OAAO,iBAM/F"}
|
package/dist/calls.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Schema } from '@restatedev/sessions/internal';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
/** The table of the {@link Calls}: one row, of the invocation whose calls they are. */
|
|
4
|
+
const SCHEMA = new Schema('restate-agents.calls', [
|
|
5
|
+
`CREATE TABLE restate_agent_calls (
|
|
6
|
+
row INTEGER PRIMARY KEY CHECK (row = 0),
|
|
7
|
+
invocation TEXT NOT NULL,
|
|
8
|
+
calls TEXT NOT NULL
|
|
9
|
+
)`,
|
|
10
|
+
]);
|
|
11
|
+
/** Store `calls`, committed before the call they record is made. */
|
|
12
|
+
async function store(ctx, { invocation, calls }) {
|
|
13
|
+
await ctx.db.execute('INSERT OR REPLACE INTO restate_agent_calls (row, invocation, calls) VALUES (0, ?, ?)', [
|
|
14
|
+
invocation,
|
|
15
|
+
JSON.stringify(calls),
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
18
|
+
/** The calls of `invocation`, as stored: none if the row is another invocation's. */
|
|
19
|
+
async function load(ctx, invocation) {
|
|
20
|
+
await SCHEMA.ensure(ctx.db);
|
|
21
|
+
const row = await ctx.db.get('SELECT invocation, calls FROM restate_agent_calls');
|
|
22
|
+
return row?.invocation === invocation
|
|
23
|
+
? { invocation, calls: JSON.parse(row.calls) }
|
|
24
|
+
: { invocation, calls: [] };
|
|
25
|
+
}
|
|
26
|
+
/** Each attempt's id, and its calls, by its context. */
|
|
27
|
+
const attempts = new WeakMap();
|
|
28
|
+
/** The attempt of `ctx`: its id, and the calls of its invocation. */
|
|
29
|
+
function attemptOf(ctx) {
|
|
30
|
+
let attempt = attempts.get(ctx);
|
|
31
|
+
if (!attempt) {
|
|
32
|
+
const calls = load(ctx, ctx.request.id);
|
|
33
|
+
attempt = { id: randomUUID(), calls };
|
|
34
|
+
attempts.set(ctx, attempt);
|
|
35
|
+
}
|
|
36
|
+
return attempt;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The call of tool `tool` with `input` that tool call `callId` makes: the
|
|
40
|
+
* one it made before (a run again), or the one an earlier attempt made with
|
|
41
|
+
* the same tool and input (made again under a new id), or else a new one.
|
|
42
|
+
* `run(id)` makes it with its own id, and stores that it answered.
|
|
43
|
+
*/
|
|
44
|
+
export async function callOf(ctx, tool, input, callId) {
|
|
45
|
+
const attempt = attemptOf(ctx);
|
|
46
|
+
const state = await attempt.calls;
|
|
47
|
+
const json = JSON.stringify(input);
|
|
48
|
+
let call = state.calls.find((c) => c.ids.includes(callId)) ??
|
|
49
|
+
state.calls.find((c) => c.attempt !== attempt.id && c.tool === tool && c.input === json);
|
|
50
|
+
if (call) {
|
|
51
|
+
call.attempt = attempt.id;
|
|
52
|
+
if (!call.ids.includes(callId))
|
|
53
|
+
call.ids.push(callId);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
state.calls.push((call = { tool, input: json, ids: [callId], attempt: attempt.id }));
|
|
57
|
+
}
|
|
58
|
+
await store(ctx, state);
|
|
59
|
+
const made = call;
|
|
60
|
+
return {
|
|
61
|
+
id: made.ids[0],
|
|
62
|
+
/**
|
|
63
|
+
* It answered, with `failed` if a terminal error answered it: kept until
|
|
64
|
+
* the transcript has its result.
|
|
65
|
+
*/
|
|
66
|
+
async answered(failed) {
|
|
67
|
+
made.done = true;
|
|
68
|
+
if (failed !== undefined)
|
|
69
|
+
made.failed = failed;
|
|
70
|
+
await store(ctx, state);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Forget the calls that answered whose result the framework stored: those
|
|
76
|
+
* of which `stored(id, failed)` is true for the last id they were made with,
|
|
77
|
+
* `failed` the error that answered the call, if one did. Called
|
|
78
|
+
* at each publish, and before each tool call, with the transcript as the
|
|
79
|
+
* framework stored it: also in an attempt that made no call yet, which would
|
|
80
|
+
* otherwise take one an earlier attempt made, and the model has the result
|
|
81
|
+
* of.
|
|
82
|
+
*/
|
|
83
|
+
export async function settle(ctx, stored) {
|
|
84
|
+
const state = await attemptOf(ctx).calls;
|
|
85
|
+
const kept = state.calls.filter((c) => !c.done || !stored(c.ids.at(-1), c.failed));
|
|
86
|
+
if (kept.length === state.calls.length)
|
|
87
|
+
return;
|
|
88
|
+
state.calls = kept;
|
|
89
|
+
await store(ctx, state);
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=calls.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calls.js","sourceRoot":"","sources":["../src/calls.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAoBzC,uFAAuF;AACvF,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,sBAAsB,EAAE;IAChD;;;;IAIE;CACH,CAAC,CAAC;AAKH,oEAAoE;AACpE,KAAK,UAAU,KAAK,CAAC,GAAiB,EAAE,EAAE,UAAU,EAAE,KAAK,EAAS;IAClE,MAAM,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,sFAAsF,EAAE;QAC3G,UAAU;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AAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,GAAiB,EAAE,MAAgD;IAC9F,MAAM,KAAK,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC;IACzC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IACpF,IAAI,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,KAAK,CAAC,MAAM;QAAE,OAAO;IAC/C,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IACnB,MAAM,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import type { Database } from '@restatedev/sessions';
|
|
3
|
+
type MessageParam = Anthropic.Beta.Messages.BetaMessageParam;
|
|
4
|
+
/** The conversation in the object's database: the tool runner's messages, and each prompt's answer. */
|
|
5
|
+
export declare class Conversation {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(db: Database);
|
|
8
|
+
/** The conversation in `db` (`ctx.db`). */
|
|
9
|
+
static open(db: Database): Promise<Conversation>;
|
|
10
|
+
/** The answer to prompt `id`, or undefined. */
|
|
11
|
+
answer(id: string): Promise<string | undefined>;
|
|
12
|
+
/** The messages, in order. */
|
|
13
|
+
messages(): Promise<MessageParam[]>;
|
|
14
|
+
/** The messages stored after `seq`, in order, each with its own `seq`. */
|
|
15
|
+
since(seq: number): Promise<[seq: number, message: MessageParam][]>;
|
|
16
|
+
/** The prompt being answered: the last message's, unless it is answered. */
|
|
17
|
+
pending(): Promise<string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Close prompt `id`, which was cancelled: a result for each tool call of
|
|
20
|
+
* the last reply that has none, then `text` as the reply and the answer.
|
|
21
|
+
*/
|
|
22
|
+
cancel(id: string, text: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Close prompt `id` with `text` as its last reply and its answer, after
|
|
25
|
+
* `reply` if given (a reply that cannot go on: cut off, or declined): first
|
|
26
|
+
* a result of `text` for each tool call of the last reply that has none.
|
|
27
|
+
*/
|
|
28
|
+
close(id: string, text: string, reply?: MessageParam): Promise<void>;
|
|
29
|
+
/** Store `messages` of prompt `id`, and its `answer` if given, in one transaction. */
|
|
30
|
+
append(id: string, messages: MessageParam[], answer?: string): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=conversation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../../src/claude/conversation.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,SAAS,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGrD,KAAK,YAAY,GAAG,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC;AAO7D,uGAAuG;AACvG,qBAAa,YAAY;;IAKvB,YAAY,EAAE,EAAE,QAAQ,EAEvB;IAED,2CAA2C;IAC3C,OAAa,IAAI,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,YAAY,CAAC,CAGrD;IAED,+CAA+C;IACzC,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAGpD;IAED,8BAA8B;IACxB,QAAQ,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC,CAGxC;IAED,0EAA0E;IACpE,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,CAAC,EAAE,CAAC,CAKxE;IAED,4EAA4E;IACtE,OAAO,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAK3C;IAED;;;OAGG;IACG,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGpD;IAED;;;;OAIG;IACG,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAUzE;IAED,sFAAsF;IACtF,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG3E;CAcF"}
|
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
import { Schema } from '@restatedev/sessions/internal';
|
|
9
|
+
const SCHEMA = new Schema('restate-agents.claude', [
|
|
10
|
+
'CREATE TABLE restate_claude_messages (seq INTEGER PRIMARY KEY, prompt TEXT NOT NULL, message TEXT NOT NULL)',
|
|
11
|
+
'CREATE TABLE restate_claude_answers (prompt TEXT PRIMARY KEY, answer TEXT NOT NULL)',
|
|
12
|
+
]);
|
|
13
|
+
/** The conversation in the object's database: the tool runner's messages, and each prompt's answer. */
|
|
14
|
+
export class Conversation {
|
|
15
|
+
#db;
|
|
16
|
+
/** Cancelled: a run cut off then stores nothing more. */
|
|
17
|
+
#closed = false;
|
|
18
|
+
constructor(db) {
|
|
19
|
+
this.#db = db;
|
|
20
|
+
}
|
|
21
|
+
/** The conversation in `db` (`ctx.db`). */
|
|
22
|
+
static async open(db) {
|
|
23
|
+
await SCHEMA.ensure(db);
|
|
24
|
+
return new Conversation(db);
|
|
25
|
+
}
|
|
26
|
+
/** The answer to prompt `id`, or undefined. */
|
|
27
|
+
async answer(id) {
|
|
28
|
+
const [row] = await this.#db.values('SELECT answer FROM restate_claude_answers WHERE prompt = ?', [id]);
|
|
29
|
+
return row?.[0];
|
|
30
|
+
}
|
|
31
|
+
/** The messages, in order. */
|
|
32
|
+
async messages() {
|
|
33
|
+
const rows = await this.#db.values('SELECT message FROM restate_claude_messages ORDER BY seq');
|
|
34
|
+
return rows.map(([message]) => JSON.parse(String(message)));
|
|
35
|
+
}
|
|
36
|
+
/** The messages stored after `seq`, in order, each with its own `seq`. */
|
|
37
|
+
async since(seq) {
|
|
38
|
+
const rows = await this.#db.values('SELECT seq, message FROM restate_claude_messages WHERE seq > ? ORDER BY seq', [
|
|
39
|
+
seq,
|
|
40
|
+
]);
|
|
41
|
+
return rows.map(([at, message]) => [Number(at), JSON.parse(String(message))]);
|
|
42
|
+
}
|
|
43
|
+
/** The prompt being answered: the last message's, unless it is answered. */
|
|
44
|
+
async pending() {
|
|
45
|
+
const [row] = await this.#db.values('SELECT prompt FROM restate_claude_messages WHERE prompt NOT IN (SELECT prompt FROM restate_claude_answers) ORDER BY seq DESC LIMIT 1');
|
|
46
|
+
return row?.[0];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Close prompt `id`, which was cancelled: a result for each tool call of
|
|
50
|
+
* the last reply that has none, then `text` as the reply and the answer.
|
|
51
|
+
*/
|
|
52
|
+
async cancel(id, text) {
|
|
53
|
+
this.#closed = true;
|
|
54
|
+
await this.close(id, text);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Close prompt `id` with `text` as its last reply and its answer, after
|
|
58
|
+
* `reply` if given (a reply that cannot go on: cut off, or declined): first
|
|
59
|
+
* a result of `text` for each tool call of the last reply that has none.
|
|
60
|
+
*/
|
|
61
|
+
async close(id, text, reply) {
|
|
62
|
+
const calls = toolUses(reply ?? (await this.messages()).at(-1));
|
|
63
|
+
const results = calls.map((call) => ({
|
|
64
|
+
type: 'tool_result',
|
|
65
|
+
tool_use_id: call.id,
|
|
66
|
+
content: text,
|
|
67
|
+
is_error: true,
|
|
68
|
+
}));
|
|
69
|
+
const closing = results.length > 0 ? [{ role: 'user', content: results }] : [];
|
|
70
|
+
await this.#store(id, [...(reply ? [reply] : []), ...closing, { role: 'assistant', content: text }], text);
|
|
71
|
+
}
|
|
72
|
+
/** Store `messages` of prompt `id`, and its `answer` if given, in one transaction. */
|
|
73
|
+
append(id, messages, answer) {
|
|
74
|
+
if (this.#closed)
|
|
75
|
+
return Promise.reject(new Error('the conversation was cancelled'));
|
|
76
|
+
return this.#store(id, messages, answer);
|
|
77
|
+
}
|
|
78
|
+
#store(id, messages, answer) {
|
|
79
|
+
return this.#db.transaction(async (tx) => {
|
|
80
|
+
for (const message of messages) {
|
|
81
|
+
await tx.execute('INSERT INTO restate_claude_messages (prompt, message) VALUES (?, ?)', [
|
|
82
|
+
id,
|
|
83
|
+
JSON.stringify(message),
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
if (answer !== undefined)
|
|
87
|
+
await tx.execute('INSERT INTO restate_claude_answers (prompt, answer) VALUES (?, ?)', [id, answer]);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** The tool calls of `message`, if it is the assistant's. */
|
|
92
|
+
const toolUses = (message) => message?.role === 'assistant' && Array.isArray(message.content)
|
|
93
|
+
? message.content.filter((b) => b.type === 'tool_use')
|
|
94
|
+
: [];
|
|
95
|
+
//# sourceMappingURL=conversation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation.js","sourceRoot":"","sources":["../../src/claude/conversation.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,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,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC;IACtD,CAAC,CAAC,EAAE,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import * as restate from '@restatedev/sessions';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
import type { BetaRunnableTool, BetaToolRunContext } from '@anthropic-ai/sdk/lib/tools/BetaRunnableTool';
|
|
4
|
+
import { type Configure } from '../common.ts';
|
|
5
|
+
import { type Answer } from '../output.ts';
|
|
6
|
+
import type { z } from 'zod';
|
|
7
|
+
import { type Definition } from '../object.ts';
|
|
8
|
+
import { type Tool as AnyTool } from '../tools.ts';
|
|
9
|
+
/** The id of the tool call a tool runs for: the runner always passes it. */
|
|
10
|
+
export declare const callId: (context?: BetaToolRunContext) => string;
|
|
11
|
+
/** How much effort the model puts into a reply (Anthropic's `output_config.effort`). */
|
|
12
|
+
export type Effort = 'low' | 'medium' | 'high' | 'xhigh' | 'max';
|
|
13
|
+
/** An agent's options: `chat` and `prompt` take them. */
|
|
14
|
+
export interface Options<O extends z.ZodObject | undefined = z.ZodObject | undefined> {
|
|
15
|
+
/** The model, as `anthropic/model-id`: MODEL, or else anthropic/claude-sonnet-5-5. */
|
|
16
|
+
model?: string;
|
|
17
|
+
/** The system prompt. */
|
|
18
|
+
system?: string;
|
|
19
|
+
/** The agent's tools: `tool()`s, Restate's (`call`, `signal`...), or the SDK's (such as `betaZodTool`s). */
|
|
20
|
+
tools?: (AnyTool | BetaRunnableTool)[];
|
|
21
|
+
/** The client of the Anthropic API: by default one with ANTHROPIC_API_KEY. */
|
|
22
|
+
client?: Anthropic;
|
|
23
|
+
/**
|
|
24
|
+
* The most tokens of each of the model's replies (default 16000). A reply
|
|
25
|
+
* cut off there ends its prompt, which fails (a terminal error).
|
|
26
|
+
*/
|
|
27
|
+
maxTokens?: number;
|
|
28
|
+
/** How much effort the model puts into each reply: more is more thorough, and slower (default: the model's). */
|
|
29
|
+
effort?: Effort;
|
|
30
|
+
/**
|
|
31
|
+
* The answer as data: a zod object, whose JSON the agent replies with
|
|
32
|
+
* (checked), and `chat` and `prompt` answer with (see output.ts).
|
|
33
|
+
*/
|
|
34
|
+
output?: O;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
|
|
38
|
+
* of an invocation is a prompt of its own.
|
|
39
|
+
* With `output`, it answers with the value the agent replies with (see
|
|
40
|
+
* output.ts), not its text.
|
|
41
|
+
*/
|
|
42
|
+
export declare const prompt: <O extends z.ZodObject | undefined = undefined>(ctx: restate.SessionContext, text: string, options?: Options<O>) => Promise<Answer<O>>;
|
|
43
|
+
/**
|
|
44
|
+
* A chat handler: each message an invocation, the conversation in the
|
|
45
|
+
* object's database, with `options`, or the options a function of the
|
|
46
|
+
* invocation's context gives. It takes the message as plain text
|
|
47
|
+
* (`curl -d 'Hi'`) or as a JSON string (`curl --json '"Hi"'`).
|
|
48
|
+
*/
|
|
49
|
+
export declare const chat: <O extends z.ZodObject | undefined = undefined>(options?: Configure<Options<O>>) => {
|
|
50
|
+
input: restate.Serde<string>;
|
|
51
|
+
accept: string;
|
|
52
|
+
explicitCancellation: boolean;
|
|
53
|
+
handler: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* A handler of the conversation as clients read it: a page at a time, or
|
|
57
|
+
* what is new or changed since a cursor, from the log each change writes
|
|
58
|
+
* (log.ts). It answers while a message is answered.
|
|
59
|
+
*/
|
|
60
|
+
export declare const messages: () => (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
|
|
61
|
+
/** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
|
|
62
|
+
export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
|
|
63
|
+
signal: (ctx: restate.ViewContext, signal: import("../signals.ts").Signal) => Promise<import("../signals.ts").Resolved>;
|
|
64
|
+
pending: (ctx: restate.ViewContext) => Promise<import("../signals.ts").Waiting[]>;
|
|
65
|
+
status: (ctx: restate.ViewContext) => Promise<import("../object.ts").Status>;
|
|
66
|
+
stop: (ctx: restate.ViewContext) => Promise<import("../object.ts").Stopped>;
|
|
67
|
+
steer: (ctx: restate.ViewContext, text: string) => Promise<import("../signals.ts").Steered>;
|
|
68
|
+
notifications: (ctx: restate.ViewContext) => Promise<import("../signals.ts").Notifications>;
|
|
69
|
+
watch: (ctx: restate.ViewContext, watch: import("../signals.ts").Watch) => Promise<import("../signals.ts").Notifications>;
|
|
70
|
+
} & {
|
|
71
|
+
clear: (ctx: restate.ViewContext) => Promise<import("../object.ts").Stopped>;
|
|
72
|
+
sync: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit }?: import("../object.ts").SyncRequest) => Promise<import("../object.ts").Synced>;
|
|
73
|
+
} & {
|
|
74
|
+
chat: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
|
|
75
|
+
} & {
|
|
76
|
+
messages: (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
|
|
77
|
+
}>;
|
|
78
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/claude/index.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAEhD,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAC;AACzG,OAAO,EAML,KAAK,SAAS,EAef,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,KAAK,MAAM,EAAc,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,EAAE,CAA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AMF;;;;GAIG;AACH,eAAO,MAAM,QAAQ,gIAA0B,CAAC;AA4BhD,6HAA6H;AAC7H,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,SAAS,GAAG,SAAS,yDAMlF,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;;;;;;;;;;;;;;;EAQzB,CAAC"}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Claude agents in Restate virtual objects, on the tool runner of the
|
|
2
|
+
// Anthropic SDK (`client.beta.messages.toolRunner`): the conversation is in
|
|
3
|
+
// the object's database (conversation.ts).
|
|
4
|
+
//
|
|
5
|
+
// const assistant = claude.agent({ name: 'assistant', system: 'Answer briefly.', tools: [weather] });
|
|
6
|
+
//
|
|
7
|
+
// or a handler of an object of your own: `handlers: { chat: claude.chat() }`,
|
|
8
|
+
// with options for each invocation: `claude.chat(async (ctx) => ({ system }))`,
|
|
9
|
+
// or a prompt of your own handler: `claude.prompt(ctx, text, options)`
|
|
10
|
+
//
|
|
11
|
+
// The model is `model`, or else MODEL, or else anthropic/claude-sonnet-5-5,
|
|
12
|
+
// with ANTHROPIC_API_KEY.
|
|
13
|
+
//
|
|
14
|
+
// The handler is not replayed: a retried attempt (a crash, a lost connection)
|
|
15
|
+
// runs again from the start, and starts the runner with the stored messages:
|
|
16
|
+
// - Each reply of the model is stored before its tools run, and their results
|
|
17
|
+
// before the next request. So a crash during the tools resumes them with the
|
|
18
|
+
// same tool call ids (`callId(context)`), without asking the model again,
|
|
19
|
+
// and a tool passes the id on to stay idempotent.
|
|
20
|
+
// - A tool's error fails the attempt, which Restate retries (by default the
|
|
21
|
+
// runner hands it to the model as the tool's result).
|
|
22
|
+
// - A message is answered once per invocation: its answer is stored with the
|
|
23
|
+
// model's last reply, and a retried attempt returns it.
|
|
24
|
+
// - A reply cut off at `maxTokens`, or declined, ends the prompt: its tool
|
|
25
|
+
// calls do not run, and the handler fails with a terminal error.
|
|
26
|
+
import * as restate from '@restatedev/sessions';
|
|
27
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
28
|
+
import { aborted, AttemptEnded, CANCELLED, chatHandler, CLAUDE_MODEL, configured, fromTool, messagesHandler, modelIdOf, modelOf, nextPromptId, refusal, stopSignalOf, storedAnswer, toolCallEnded, toolCallStarted, transcribing, Transcript, UNANSWERED, } from '../common.js';
|
|
29
|
+
import { withOutput } from '../output.js';
|
|
30
|
+
import { sessionObject } from '../object.js';
|
|
31
|
+
import { isTool, jsonSchemaOf } from '../tools.js';
|
|
32
|
+
import { Conversation } from './conversation.js';
|
|
33
|
+
/** The id of the tool call a tool runs for: the runner always passes it. */
|
|
34
|
+
export const callId = (context) => {
|
|
35
|
+
if (!context)
|
|
36
|
+
throw new Error('a tool call without its context');
|
|
37
|
+
return context.toolUse.id;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* `tools`, whose errors fail the attempt: the runner would hand a thrown error
|
|
41
|
+
* to the model as the tool's result, so each one is kept, for `check()` to
|
|
42
|
+
* throw once the tools ran.
|
|
43
|
+
*/
|
|
44
|
+
function failing(tools) {
|
|
45
|
+
let failure;
|
|
46
|
+
return {
|
|
47
|
+
tools: tools.map((tool) => ({
|
|
48
|
+
...tool,
|
|
49
|
+
run: async (args, context) => {
|
|
50
|
+
try {
|
|
51
|
+
return await tool.run(args, context);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
fromTool(e);
|
|
55
|
+
failure ??= e;
|
|
56
|
+
throw e;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
})),
|
|
60
|
+
check: () => {
|
|
61
|
+
if (failure !== undefined)
|
|
62
|
+
throw failure;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const textOf = (content) => content
|
|
67
|
+
.filter((c) => c.type === 'text')
|
|
68
|
+
.map((c) => c.text)
|
|
69
|
+
.join('');
|
|
70
|
+
/**
|
|
71
|
+
* Run prompt `id` of `conversation` until it is answered, from its stored
|
|
72
|
+
* messages; each round of tool calls published as it starts and ends (for
|
|
73
|
+
* the clients that follow the conversation).
|
|
74
|
+
*/
|
|
75
|
+
async function run(ctx, { client, model, maxTokens, effort, system, tools: all }, conversation, id, signal) {
|
|
76
|
+
const { tools, check } = failing(all);
|
|
77
|
+
const runner = client.beta.messages.toolRunner({
|
|
78
|
+
model,
|
|
79
|
+
max_tokens: maxTokens,
|
|
80
|
+
...(effort === undefined ? {} : { output_config: { effort } }),
|
|
81
|
+
...(system === undefined ? {} : { system }),
|
|
82
|
+
tools,
|
|
83
|
+
messages: await conversation.messages(),
|
|
84
|
+
}, { signal });
|
|
85
|
+
// A reply whose tools did not finish (a crash): run them, with the same ids,
|
|
86
|
+
// once the calls whose results are stored are settled, as for any reply:
|
|
87
|
+
// else one of its calls could take the id of an identical call answered before.
|
|
88
|
+
if (runner.params.messages.at(-1)?.role === 'assistant') {
|
|
89
|
+
await toolCallStarted(ctx);
|
|
90
|
+
const results = await runner.generateToolResponse();
|
|
91
|
+
check();
|
|
92
|
+
if (results) {
|
|
93
|
+
await conversation.append(id, [results]);
|
|
94
|
+
runner.pushMessages(results);
|
|
95
|
+
}
|
|
96
|
+
await toolCallEnded(ctx);
|
|
97
|
+
}
|
|
98
|
+
for await (const message of runner) {
|
|
99
|
+
const reply = { role: 'assistant', content: message.content };
|
|
100
|
+
// A reply that cannot go on ends the prompt, whose tool calls do not run.
|
|
101
|
+
const stopped = stoppedShort(message, maxTokens);
|
|
102
|
+
if (stopped) {
|
|
103
|
+
await conversation.close(id, stopped.text, reply);
|
|
104
|
+
throw new restate.TerminalError(stopped.error);
|
|
105
|
+
}
|
|
106
|
+
if (!message.content.some((c) => c.type === 'tool_use')) {
|
|
107
|
+
await conversation.append(id, [reply], textOf(message.content));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// The reply first, then its tools.
|
|
111
|
+
await conversation.append(id, [reply]);
|
|
112
|
+
await toolCallStarted(ctx);
|
|
113
|
+
const results = await runner.generateToolResponse();
|
|
114
|
+
check();
|
|
115
|
+
if (results)
|
|
116
|
+
await conversation.append(id, [results]);
|
|
117
|
+
await toolCallEnded(ctx);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Why the model's `message` stopped short of a reply to go on from: cut off
|
|
122
|
+
* at `maxTokens` (its text, or a tool call's input, unfinished), or declined.
|
|
123
|
+
*/
|
|
124
|
+
function stoppedShort(message, maxTokens) {
|
|
125
|
+
if (message.stop_reason === 'max_tokens') {
|
|
126
|
+
return {
|
|
127
|
+
text: `Cut off: the reply reached ${maxTokens} tokens before it ended.`,
|
|
128
|
+
error: `the model's reply reached maxTokens (${maxTokens}) before it ended: raise the agent's maxTokens`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (message.stop_reason === 'refusal') {
|
|
132
|
+
const category = message.stop_details?.category;
|
|
133
|
+
return {
|
|
134
|
+
text: 'Declined: the model did not answer.',
|
|
135
|
+
error: `the model declined to answer${category ? ` (${category})` : ''}`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
/** `t` as a tool of the Anthropic SDK's tool runner: `t.run` checks the model's arguments itself. */
|
|
141
|
+
const native = (t, ctx) => ({
|
|
142
|
+
type: 'custom',
|
|
143
|
+
name: t.name,
|
|
144
|
+
description: t.description,
|
|
145
|
+
input_schema: jsonSchemaOf(t),
|
|
146
|
+
parse: (args) => args,
|
|
147
|
+
run: (args, context) => t.run(args, { ctx, callId: callId(context) }),
|
|
148
|
+
});
|
|
149
|
+
/** The client of the agents without one of their own, made when first used. */
|
|
150
|
+
let shared;
|
|
151
|
+
/** {@link prompt}, answering with the agent's text. */
|
|
152
|
+
const promptText = (ctx, text, options) => transcribing(ctx, () => transcribe(ctx.db), () => answerOnce(ctx, text, options));
|
|
153
|
+
const answerOnce = async (ctx, text, { model: named, system, tools = [], client, maxTokens = 16_000, effort }) => {
|
|
154
|
+
const agent = {
|
|
155
|
+
client: client ?? (shared ??= new Anthropic()),
|
|
156
|
+
model: modelIdOf(modelOf(named, CLAUDE_MODEL), 'anthropic'),
|
|
157
|
+
maxTokens,
|
|
158
|
+
effort,
|
|
159
|
+
system,
|
|
160
|
+
tools: tools.map((t) => (isTool(t) ? native(t, ctx) : t)),
|
|
161
|
+
};
|
|
162
|
+
const id = nextPromptId(ctx);
|
|
163
|
+
const conversation = await Conversation.open(ctx.db);
|
|
164
|
+
for (;;) {
|
|
165
|
+
const answer = await conversation.answer(id);
|
|
166
|
+
if (answer !== undefined)
|
|
167
|
+
return storedAnswer(answer);
|
|
168
|
+
// Finish an interrupted prompt first (this one's, after a crash), then
|
|
169
|
+
// start this one.
|
|
170
|
+
let pending = await conversation.pending();
|
|
171
|
+
if (pending === undefined) {
|
|
172
|
+
await conversation.append(id, [{ role: 'user', content: text }]);
|
|
173
|
+
pending = id;
|
|
174
|
+
}
|
|
175
|
+
const turn = stopSignalOf(ctx);
|
|
176
|
+
try {
|
|
177
|
+
const running = run(ctx, agent, conversation, pending, turn);
|
|
178
|
+
running.catch(() => { });
|
|
179
|
+
await (turn ? Promise.race([running, aborted(turn)]) : running);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
// The attempt ended: it stops, and the next attempt goes on with the prompt.
|
|
183
|
+
if (turn?.reason instanceof AttemptEnded)
|
|
184
|
+
throw turn.reason;
|
|
185
|
+
// Cancelled: the prompt ends, and the next one starts afresh.
|
|
186
|
+
if (turn?.aborted || error instanceof restate.CancelledError) {
|
|
187
|
+
await conversation.cancel(pending, CANCELLED);
|
|
188
|
+
throw turn?.aborted ? turn.reason : error;
|
|
189
|
+
}
|
|
190
|
+
// An earlier prompt that ended (cut off, or declined): this one goes on.
|
|
191
|
+
if (pending !== id &&
|
|
192
|
+
error instanceof restate.TerminalError &&
|
|
193
|
+
(await conversation.answer(pending)) !== undefined) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// Refused for good (a wrong API key): the prompt ends unanswered, and
|
|
197
|
+
// fails as terminal. An earlier one's, this one goes on.
|
|
198
|
+
const refused = refusal(error);
|
|
199
|
+
if (!refused)
|
|
200
|
+
throw error;
|
|
201
|
+
await conversation.close(pending, UNANSWERED);
|
|
202
|
+
if (pending === id)
|
|
203
|
+
throw refused;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
|
|
209
|
+
* of an invocation is a prompt of its own.
|
|
210
|
+
* With `output`, it answers with the value the agent replies with (see
|
|
211
|
+
* output.ts), not its text.
|
|
212
|
+
*/
|
|
213
|
+
export const prompt = (ctx, text, options = {}) => withOutput(text, options, (t, o) => promptText(ctx, t, o));
|
|
214
|
+
/**
|
|
215
|
+
* A chat handler: each message an invocation, the conversation in the
|
|
216
|
+
* object's database, with `options`, or the options a function of the
|
|
217
|
+
* invocation's context gives. It takes the message as plain text
|
|
218
|
+
* (`curl -d 'Hi'`) or as a JSON string (`curl --json '"Hi"'`).
|
|
219
|
+
*/
|
|
220
|
+
export const chat = (options = {}) => {
|
|
221
|
+
if (typeof options === 'object')
|
|
222
|
+
modelIdOf(modelOf(options.model, CLAUDE_MODEL), 'anthropic');
|
|
223
|
+
return chatHandler(async (ctx, message) => prompt(ctx, message, await configured(options, ctx)));
|
|
224
|
+
};
|
|
225
|
+
/** The text of stored content, without its tool calls and results. */
|
|
226
|
+
const storedText = (content) => typeof content === 'string' ? content : (content ?? []).flatMap((b) => (b.type === 'text' ? [b.text] : [])).join('');
|
|
227
|
+
/**
|
|
228
|
+
* A handler of the conversation as clients read it: a page at a time, or
|
|
229
|
+
* what is new or changed since a cursor, from the log each change writes
|
|
230
|
+
* (log.ts). It answers while a message is answered.
|
|
231
|
+
*/
|
|
232
|
+
export const messages = () => messagesHandler();
|
|
233
|
+
/**
|
|
234
|
+
* The transcript of each session's conversation (`ctx.db`, one per
|
|
235
|
+
* attempt), as far as it was read: the conversation only grows, so each
|
|
236
|
+
* read adds the messages stored since.
|
|
237
|
+
*/
|
|
238
|
+
const transcripts = new WeakMap();
|
|
239
|
+
/** The conversation in `db`, as `messages` gives it. */
|
|
240
|
+
const transcribe = async (db) => {
|
|
241
|
+
const read = transcripts.get(db) ?? { seq: 0, transcript: new Transcript() };
|
|
242
|
+
transcripts.set(db, read);
|
|
243
|
+
const { transcript } = read;
|
|
244
|
+
for (const [seq, m] of await (await Conversation.open(db)).since(read.seq)) {
|
|
245
|
+
read.seq = seq;
|
|
246
|
+
if (m.role === 'system')
|
|
247
|
+
continue;
|
|
248
|
+
transcript.say(m.role, storedText(m.content));
|
|
249
|
+
if (typeof m.content === 'string')
|
|
250
|
+
continue;
|
|
251
|
+
for (const block of m.content) {
|
|
252
|
+
if (block.type === 'tool_use')
|
|
253
|
+
transcript.call(block.id, block.name, block.input);
|
|
254
|
+
if (block.type === 'tool_result')
|
|
255
|
+
transcript.result(block.tool_use_id, storedText(block.content), block.is_error === true);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return transcript;
|
|
259
|
+
};
|
|
260
|
+
/** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
|
|
261
|
+
export const agent = ({ name, description, schema, background, ...options }) => sessionObject({
|
|
262
|
+
name,
|
|
263
|
+
description,
|
|
264
|
+
schema,
|
|
265
|
+
background,
|
|
266
|
+
handlers: { chat: chat(options) },
|
|
267
|
+
views: { messages: messages() },
|
|
268
|
+
});
|
|
269
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/claude/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,4EAA4E;AAC5E,2CAA2C;AAC3C,EAAE;AACF,wGAAwG;AACxG,EAAE;AACF,8EAA8E;AAC9E,gF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