@restatedev/agents 0.11.2 → 0.12.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 +189 -808
- package/dist/pi/factory.d.ts +73 -0
- package/dist/pi/factory.d.ts.map +1 -0
- package/dist/pi/factory.js +145 -0
- package/dist/pi/factory.js.map +1 -0
- package/dist/pi/index.d.ts +5 -129
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +31 -571
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/keepalive.d.ts +3 -16
- package/dist/pi/keepalive.d.ts.map +1 -1
- package/dist/pi/keepalive.js +38 -41
- package/dist/pi/keepalive.js.map +1 -1
- package/dist/pi/left.d.ts +16 -0
- package/dist/pi/left.d.ts.map +1 -0
- package/dist/pi/left.js +23 -0
- package/dist/pi/left.js.map +1 -0
- package/dist/pi/storage.d.ts +19 -76
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +50 -368
- package/dist/pi/storage.js.map +1 -1
- package/dist/pi/tools.d.ts +12 -0
- package/dist/pi/tools.d.ts.map +1 -0
- package/dist/pi/tools.js +70 -0
- package/dist/pi/tools.js.map +1 -0
- package/package.json +11 -254
- package/src/pi/factory.ts +231 -0
- package/src/pi/index.ts +31 -759
- package/src/pi/keepalive.ts +46 -56
- package/src/pi/left.ts +52 -0
- package/src/pi/storage.ts +75 -409
- package/src/pi/tools.ts +78 -0
- package/dist/agentlib/agent.d.ts +0 -32
- package/dist/agentlib/agent.d.ts.map +0 -1
- package/dist/agentlib/agent.js +0 -174
- package/dist/agentlib/agent.js.map +0 -1
- package/dist/agentlib/index.d.ts +0 -5
- package/dist/agentlib/index.d.ts.map +0 -1
- package/dist/agentlib/index.js +0 -15
- package/dist/agentlib/index.js.map +0 -1
- package/dist/agentlib/messages.d.ts +0 -39
- package/dist/agentlib/messages.d.ts.map +0 -1
- package/dist/agentlib/messages.js +0 -177
- package/dist/agentlib/messages.js.map +0 -1
- package/dist/agentlib/model.d.ts +0 -31
- package/dist/agentlib/model.d.ts.map +0 -1
- package/dist/agentlib/model.js +0 -140
- package/dist/agentlib/model.js.map +0 -1
- package/dist/agentlib/options.d.ts +0 -58
- package/dist/agentlib/options.d.ts.map +0 -1
- package/dist/agentlib/options.js +0 -2
- package/dist/agentlib/options.js.map +0 -1
- package/dist/agentlib/runtime.d.ts +0 -37
- package/dist/agentlib/runtime.d.ts.map +0 -1
- package/dist/agentlib/runtime.js +0 -216
- package/dist/agentlib/runtime.js.map +0 -1
- package/dist/agentlib/state.d.ts +0 -114
- package/dist/agentlib/state.d.ts.map +0 -1
- package/dist/agentlib/state.js +0 -52
- package/dist/agentlib/state.js.map +0 -1
- package/dist/agentlib/steering.d.ts +0 -23
- package/dist/agentlib/steering.d.ts.map +0 -1
- package/dist/agentlib/steering.js +0 -48
- package/dist/agentlib/steering.js.map +0 -1
- package/dist/agentlib/tools.d.ts +0 -33
- package/dist/agentlib/tools.d.ts.map +0 -1
- package/dist/agentlib/tools.js +0 -245
- package/dist/agentlib/tools.js.map +0 -1
- package/dist/agentlib/turn.d.ts +0 -82
- package/dist/agentlib/turn.d.ts.map +0 -1
- package/dist/agentlib/turn.js +0 -253
- package/dist/agentlib/turn.js.map +0 -1
- package/dist/calls.d.ts +0 -90
- package/dist/calls.d.ts.map +0 -1
- package/dist/calls.js +0 -241
- package/dist/calls.js.map +0 -1
- package/dist/claude/index.d.ts +0 -106
- package/dist/claude/index.d.ts.map +0 -1
- package/dist/claude/index.js +0 -386
- package/dist/claude/index.js.map +0 -1
- package/dist/client.d.ts +0 -90
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -88
- package/dist/client.js.map +0 -1
- package/dist/common.d.ts +0 -410
- package/dist/common.d.ts.map +0 -1
- package/dist/common.js +0 -563
- package/dist/common.js.map +0 -1
- package/dist/companion.d.ts +0 -286
- package/dist/companion.d.ts.map +0 -1
- package/dist/companion.js +0 -463
- package/dist/companion.js.map +0 -1
- package/dist/conversation.d.ts +0 -576
- package/dist/conversation.d.ts.map +0 -1
- package/dist/conversation.js +0 -1476
- package/dist/conversation.js.map +0 -1
- package/dist/deepseek/barriers.d.ts +0 -11
- package/dist/deepseek/barriers.d.ts.map +0 -1
- package/dist/deepseek/barriers.js +0 -36
- package/dist/deepseek/barriers.js.map +0 -1
- package/dist/deepseek/index.d.ts +0 -97
- package/dist/deepseek/index.d.ts.map +0 -1
- package/dist/deepseek/index.js +0 -603
- package/dist/deepseek/index.js.map +0 -1
- package/dist/deepseek/persistence.d.ts +0 -83
- package/dist/deepseek/persistence.d.ts.map +0 -1
- package/dist/deepseek/persistence.js +0 -262
- package/dist/deepseek/persistence.js.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -17
- package/dist/index.js.map +0 -1
- package/dist/langgraph/checkpointer.d.ts +0 -52
- package/dist/langgraph/checkpointer.d.ts.map +0 -1
- package/dist/langgraph/checkpointer.js +0 -477
- package/dist/langgraph/checkpointer.js.map +0 -1
- package/dist/langgraph/context.d.ts +0 -10
- package/dist/langgraph/context.d.ts.map +0 -1
- package/dist/langgraph/context.js +0 -21
- package/dist/langgraph/context.js.map +0 -1
- package/dist/langgraph/errors.d.ts +0 -16
- package/dist/langgraph/errors.d.ts.map +0 -1
- package/dist/langgraph/errors.js +0 -30
- package/dist/langgraph/errors.js.map +0 -1
- package/dist/langgraph/graph.d.ts +0 -25
- package/dist/langgraph/graph.d.ts.map +0 -1
- package/dist/langgraph/graph.js +0 -378
- package/dist/langgraph/graph.js.map +0 -1
- package/dist/langgraph/index.d.ts +0 -119
- package/dist/langgraph/index.d.ts.map +0 -1
- package/dist/langgraph/index.js +0 -325
- package/dist/langgraph/index.js.map +0 -1
- package/dist/langgraph/keepalive.d.ts +0 -16
- package/dist/langgraph/keepalive.d.ts.map +0 -1
- package/dist/langgraph/keepalive.js +0 -51
- package/dist/langgraph/keepalive.js.map +0 -1
- package/dist/langgraph/nodes.d.ts +0 -71
- package/dist/langgraph/nodes.d.ts.map +0 -1
- package/dist/langgraph/nodes.js +0 -148
- package/dist/langgraph/nodes.js.map +0 -1
- package/dist/messages.d.ts +0 -18
- package/dist/messages.d.ts.map +0 -1
- package/dist/messages.js +0 -182
- package/dist/messages.js.map +0 -1
- package/dist/object.d.ts +0 -213
- package/dist/object.d.ts.map +0 -1
- package/dist/object.js +0 -309
- package/dist/object.js.map +0 -1
- package/dist/opencode/db.d.ts +0 -81
- package/dist/opencode/db.d.ts.map +0 -1
- package/dist/opencode/db.js +0 -249
- package/dist/opencode/db.js.map +0 -1
- package/dist/opencode/index.d.ts +0 -184
- package/dist/opencode/index.d.ts.map +0 -1
- package/dist/opencode/index.js +0 -1079
- package/dist/opencode/index.js.map +0 -1
- package/dist/output.d.ts +0 -25
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -99
- package/dist/output.js.map +0 -1
- package/dist/pi/commits.d.ts +0 -6
- package/dist/pi/commits.d.ts.map +0 -1
- package/dist/pi/commits.js +0 -3
- package/dist/pi/commits.js.map +0 -1
- package/dist/steering.d.ts +0 -32
- package/dist/steering.d.ts.map +0 -1
- package/dist/steering.js +0 -45
- package/dist/steering.js.map +0 -1
- package/dist/tools.d.ts +0 -267
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -305
- package/dist/tools.js.map +0 -1
- package/dist/webhook.d.ts +0 -71
- package/dist/webhook.d.ts.map +0 -1
- package/dist/webhook.js +0 -102
- package/dist/webhook.js.map +0 -1
- package/src/agentlib/README.md +0 -229
- package/src/agentlib/agent.ts +0 -216
- package/src/agentlib/index.ts +0 -16
- package/src/agentlib/messages.ts +0 -207
- package/src/agentlib/model.ts +0 -156
- package/src/agentlib/options.ts +0 -62
- package/src/agentlib/runtime.ts +0 -252
- package/src/agentlib/state.ts +0 -93
- package/src/agentlib/steering.ts +0 -53
- package/src/agentlib/tools.ts +0 -355
- package/src/agentlib/turn.ts +0 -302
- package/src/calls.ts +0 -333
- package/src/claude/README.md +0 -107
- package/src/claude/index.ts +0 -487
- package/src/client.ts +0 -187
- package/src/common.ts +0 -766
- package/src/companion.ts +0 -675
- package/src/conversation.ts +0 -1799
- package/src/deepseek/README.md +0 -202
- package/src/deepseek/barriers.ts +0 -63
- package/src/deepseek/index.ts +0 -740
- package/src/deepseek/persistence.ts +0 -337
- package/src/index.ts +0 -41
- package/src/langgraph/README.md +0 -199
- package/src/langgraph/checkpointer.ts +0 -614
- package/src/langgraph/context.ts +0 -27
- package/src/langgraph/errors.ts +0 -26
- package/src/langgraph/graph.ts +0 -422
- package/src/langgraph/index.ts +0 -500
- package/src/langgraph/keepalive.ts +0 -58
- package/src/langgraph/nodes.ts +0 -247
- package/src/messages.ts +0 -244
- package/src/object.ts +0 -454
- package/src/opencode/README.md +0 -249
- package/src/opencode/db.ts +0 -387
- package/src/opencode/index.ts +0 -1339
- package/src/output.ts +0 -116
- package/src/pi/README.md +0 -174
- package/src/pi/commits.ts +0 -7
- package/src/steering.ts +0 -68
- package/src/tools.ts +0 -515
- package/src/webhook.ts +0 -161
package/dist/langgraph/nodes.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import { flushHeld } from '../calls.js';
|
|
2
|
-
import { companionHere } from '../companion.js';
|
|
3
|
-
import { runWorkflow } from '../tools.js';
|
|
4
|
-
import { handlerContext } from './context.js';
|
|
5
|
-
/** Whether a target is keyed: a virtual object (key) or a workflow (id). */
|
|
6
|
-
const keyed = (target) => 'object' in target || 'workflow' in target;
|
|
7
|
-
/** LangGraph's keys, in a node's config, of its run's checkpointer, durability, and task id. */
|
|
8
|
-
const CHECKPOINTER = '__pregel_checkpointer';
|
|
9
|
-
const DURABILITY = '__pregel_durability';
|
|
10
|
-
const TASK_ID = '__pregel_task_id';
|
|
11
|
-
/**
|
|
12
|
-
* The run of a node of `kind`, from LangGraph's config. The rows the turn
|
|
13
|
-
* holds back are written first (`flushHeld`): the node's call, send or wait
|
|
14
|
-
* outlives the attempt.
|
|
15
|
-
*/
|
|
16
|
-
async function stepOf(config, kind) {
|
|
17
|
-
const ctx = handlerContext(config.context);
|
|
18
|
-
if (!ctx) {
|
|
19
|
-
throw new TypeError(`nodes.${kind} runs in a graph that langgraph.agent({ graph }) runs, or that is invoked with the handler's ` +
|
|
20
|
-
'context: invoke(input, { context: ctx })');
|
|
21
|
-
}
|
|
22
|
-
const configurable = (config.configurable ?? {});
|
|
23
|
-
if (!configurable[CHECKPOINTER]) {
|
|
24
|
-
throw new TypeError(`nodes.${kind} runs in a graph with a checkpointer, so that a retried attempt resumes it: ` +
|
|
25
|
-
'compile({ checkpointer: new SqlCheckpointSaver(ctx.db) })');
|
|
26
|
-
}
|
|
27
|
-
// Only 'sync' stores a step's checkpoint before its tasks run. Otherwise a crash before the save makes the resumed
|
|
28
|
-
// run create the checkpoint again, with a new id, and so new task ids: the step would be made twice.
|
|
29
|
-
const durability = config.durability ?? configurable[DURABILITY];
|
|
30
|
-
if (durability !== 'sync') {
|
|
31
|
-
throw new TypeError(`nodes.${kind} runs in a graph that stores each step's checkpoint before the step runs, so that a retried ` +
|
|
32
|
-
"attempt gets the same task ids: invoke(input, { durability: 'sync' })");
|
|
33
|
-
}
|
|
34
|
-
// The task's namespace holds its id, which LangGraph derives from the checkpoint the step started from: a node
|
|
35
|
-
// run again from that checkpoint gets the same one. It holds the subgraph's path too.
|
|
36
|
-
const namespace = String(configurable.checkpoint_ns || configurable[TASK_ID]);
|
|
37
|
-
const node = namespace.split('|').at(-1).split(':')[0];
|
|
38
|
-
await flushHeld(ctx);
|
|
39
|
-
return { ctx, node, idempotencyKey: `langgraph:${ctx.key}:${namespace}` };
|
|
40
|
-
}
|
|
41
|
-
/** The state's update that writes `value` to `output`, if given. */
|
|
42
|
-
const update = (output, value) => (output === undefined ? {} : { [output]: value });
|
|
43
|
-
/** As JSON, which Restate and the companion store: the state may hold class instances. */
|
|
44
|
-
const plain = (value) => (value === undefined ? undefined : JSON.parse(JSON.stringify(value)));
|
|
45
|
-
/**
|
|
46
|
-
* A node that waits for the signal `name` to the agent, and writes its value
|
|
47
|
-
* to `output`: a person's approval, say, or another system's result. The
|
|
48
|
-
* graph waits without holding a process, for as long as it takes. The
|
|
49
|
-
* agent's `pending` view lists the wait, with `input`'s value, and its
|
|
50
|
-
* `signal` view resolves it:
|
|
51
|
-
*
|
|
52
|
-
* graph.addNode('approve', nodes.waitForSignal('approval', { input: (s) => s.refund, output: 'approved' }))
|
|
53
|
-
*/
|
|
54
|
-
export const waitForSignal =
|
|
55
|
-
// oxlint-disable-next-line typescript/no-explicit-any -- any graph's state
|
|
56
|
-
(name, { output, input } = {}) => async (state, config) => {
|
|
57
|
-
const step = await stepOf(config, 'waitForSignal');
|
|
58
|
-
// The wait's id starts with the invocation's (the same in each attempt), as the waits of tools do, so that a
|
|
59
|
-
// turn that ends without it (killed, say) has it closed: the companion closes the waits whose ids start with
|
|
60
|
-
// the turn's.
|
|
61
|
-
const id = `${step.ctx.request.id}:${step.idempotencyKey}`;
|
|
62
|
-
const shown = input === undefined ? {} : { input: plain(input(state)) };
|
|
63
|
-
const value = await step.ctx
|
|
64
|
-
.rpc(companionHere(step.ctx, 'a wait for a signal'), step.ctx.key, { idempotencyKey: id })
|
|
65
|
-
.wait({ id, name, ...shown });
|
|
66
|
-
return update(output, value);
|
|
67
|
-
};
|
|
68
|
-
/** Throw a `TypeError` if `key` is given for a target that is not keyed, or the other way around. */
|
|
69
|
-
function checkKey(target, key) {
|
|
70
|
-
if (keyed(target) !== (key !== undefined)) {
|
|
71
|
-
throw new TypeError(`${target.name} ${keyed(target) ? 'is' : 'is not'} keyed: ${keyed(target) ? 'pass' : 'drop'} key`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* A node that calls `handler` of `target` (a service, virtual object or
|
|
76
|
-
* workflow), and writes its result to `output`:
|
|
77
|
-
*
|
|
78
|
-
* graph.addNode('status', nodes.call(orders, 'status', { input: (s) => s.order, key: (s) => s.customer, output: 'status' }))
|
|
79
|
-
*/
|
|
80
|
-
export const call = (target, handler, options = {}) => {
|
|
81
|
-
checkKey(target, options.key);
|
|
82
|
-
return async (state, config) => {
|
|
83
|
-
const { ctx, idempotencyKey } = await stepOf(config, 'call');
|
|
84
|
-
const parameters = { service: target.name, key: options.key?.(state), input: plain(options.input?.(state)) };
|
|
85
|
-
const result = 'workflow' in target && handler === 'run'
|
|
86
|
-
? await runWorkflow(ctx, { ...parameters, key: parameters.key })
|
|
87
|
-
: await ctx.rpc({ ...parameters, handler, idempotencyKey });
|
|
88
|
-
return update(options.output, result);
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
/**
|
|
92
|
-
* A node that sends to `handler` of `target`, without waiting for it to run,
|
|
93
|
-
* after `delay` if given (such as `{ days: 1 }`). It writes nothing:
|
|
94
|
-
*
|
|
95
|
-
* graph.addNode('remind', nodes.send(checkins, 'remind', { input: (s) => s.checkin, delay: { days: 1 } }))
|
|
96
|
-
*/
|
|
97
|
-
export const send = (target, handler, options = {}) => {
|
|
98
|
-
checkKey(target, options.key);
|
|
99
|
-
return async (state, config) => {
|
|
100
|
-
const { ctx, idempotencyKey } = await stepOf(config, 'send');
|
|
101
|
-
ctx.send({
|
|
102
|
-
service: target.name,
|
|
103
|
-
handler,
|
|
104
|
-
key: options.key?.(state),
|
|
105
|
-
input: plain(options.input?.(state)),
|
|
106
|
-
delay: options.delay,
|
|
107
|
-
idempotencyKey,
|
|
108
|
-
});
|
|
109
|
-
return {};
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
/**
|
|
113
|
-
* A node that runs the workflow `target` (its `run` handler) to the end, and
|
|
114
|
-
* writes its result to `output`. The workflow's id is `key`'s, or one of the
|
|
115
|
-
* node's own, made of the agent's key and the node's task. Run again, the
|
|
116
|
-
* node waits for the run it started (see `runWorkflow` in tools.ts):
|
|
117
|
-
*
|
|
118
|
-
* graph.addNode('ship', nodes.workflow(shipping, { input: (s) => s.order, output: 'receipt' }))
|
|
119
|
-
*/
|
|
120
|
-
export const workflow = (target, { output, input, key } = {}) => async (state, config) => {
|
|
121
|
-
const { ctx, idempotencyKey } = await stepOf(config, 'workflow');
|
|
122
|
-
const result = await runWorkflow(ctx, {
|
|
123
|
-
service: target.name,
|
|
124
|
-
key: key?.(state) ?? idempotencyKey,
|
|
125
|
-
input: plain(input?.(state)),
|
|
126
|
-
});
|
|
127
|
-
return update(output, result);
|
|
128
|
-
};
|
|
129
|
-
/**
|
|
130
|
-
* A node that sends a message to another agent (or any virtual object with
|
|
131
|
-
* a handler that takes text), and writes its answer to `output`. The message
|
|
132
|
-
* goes to the agent's key `<this key>.<node name>` unless `key` gives one, so
|
|
133
|
-
* each run of the graph continues the same conversation with it:
|
|
134
|
-
*
|
|
135
|
-
* graph.addNode('research', nodes.subagent(researcher, { message: (s) => s.question, output: 'findings' }))
|
|
136
|
-
*/
|
|
137
|
-
export const subagent = (agent, { message, output, key, handler = 'chat', }) => async (state, config) => {
|
|
138
|
-
const { ctx, node, idempotencyKey } = await stepOf(config, 'subagent');
|
|
139
|
-
const answer = await ctx.rpc({
|
|
140
|
-
service: agent.name,
|
|
141
|
-
handler,
|
|
142
|
-
key: key?.(state) ?? `${ctx.key}.${node}`,
|
|
143
|
-
input: message(state),
|
|
144
|
-
idempotencyKey,
|
|
145
|
-
});
|
|
146
|
-
return update(output, answer);
|
|
147
|
-
};
|
|
148
|
-
//# sourceMappingURL=nodes.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../../src/langgraph/nodes.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAY9C,4EAA4E;AAC5E,MAAM,KAAK,GAAG,C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Ac,EAAE,GAAG,MAAM,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACjE,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE;QACpC,OAAO,EAAE,MAAM,CAAC,IAAI;QACpB,GAAG,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,cAAc;QACnC,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;KAC7B,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAChC,CAAC,CAAC;AAEJ;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GACnB,CACE,KAAiD,EACjD,EACE,OAAO,EACP,MAAM,EACN,GAAG,EACH,OAAO,GAAG,MAAM,GAQjB,EACK,EAAE,CACV,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;IACtB,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,MAAM,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACvE,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,CAAC;QAC3B,OAAO,EAAE,KAAK,CAAC,IAAI;QACnB,OAAO;QACP,GAAG,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,IAAI,IAAI,EAAE;QACzC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC;QACrB,cAAc;KACf,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAChC,CAAC,CAAC"}
|
package/dist/messages.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { DatabaseReader, ViewContext } from '@restatedev/sessions';
|
|
2
|
-
import { type Messages, type MessagesRequest } from './common.ts';
|
|
3
|
-
import { type Items } from './conversation.ts';
|
|
4
|
-
/**
|
|
5
|
-
* A page of the conversation in `db`, whose items are `items`'. With a
|
|
6
|
-
* `cursor`: the messages that changed since it, in the order they changed,
|
|
7
|
-
* and the cursor to read on from. Without: the latest `limit` messages (or
|
|
8
|
-
* the latest before id `before`), oldest first, and the cursor to follow the
|
|
9
|
-
* conversation from. `view: 'summary'` gives each turn's message and answer.
|
|
10
|
-
*/
|
|
11
|
-
export declare function page<I>(db: DatabaseReader, items: Items<I>, request?: MessagesRequest): Promise<Messages>;
|
|
12
|
-
/**
|
|
13
|
-
* The `messages` view of an agent whose framework keeps the conversation of
|
|
14
|
-
* turns, with its `items`: `sessionObject` finds them on it, and gives the
|
|
15
|
-
* agent `settle` and `compact`, and `clear` that keeps the ids going.
|
|
16
|
-
*/
|
|
17
|
-
export declare const conversationView: <I>(items: Items<I>) => (ctx: ViewContext, request?: MessagesRequest) => Promise<Messages>;
|
|
18
|
-
//# sourceMappingURL=messages.d.ts.map
|
package/dist/messages.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EAA2B,KAAK,QAAQ,EAAE,KAAK,eAAe,EAAa,MAAM,aAAa,CAAC;AACtG,OAAO,EAAU,KAAK,KAAK,EAA4D,MAAM,mBAAmB,CAAC;AAsDjH;;;;;;GAMG;AACH,wBAAsB,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,cAAc,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAUnH;AA6ID;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC,WAEnC,WAAW,YAAY,eAAe,KAAG,OAAO,CAAC,QAAQ,CAEtE,CAAC"}
|
package/dist/messages.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { ITEMS } from './common.js';
|
|
2
|
-
import { failed, lastOf, loadItem, SCHEMA, startOf, STEERED, STEPS, TURNS } from './conversation.js';
|
|
3
|
-
/** The conversation's `rows` (`n`, `seq`, `item`, in order) as messages, without the empty ones. */
|
|
4
|
-
function render(items, rows) {
|
|
5
|
-
const messages = [];
|
|
6
|
-
/** Each tool call's message, by the call's id. */
|
|
7
|
-
const at = new Map();
|
|
8
|
-
for (const [n, seq, stored] of rows) {
|
|
9
|
-
const position = Number(n);
|
|
10
|
-
const changed = Number(seq);
|
|
11
|
-
const { said, calls = [], results = [] } = items.read(loadItem(items, String(stored)));
|
|
12
|
-
if (said) {
|
|
13
|
-
const [role, text] = said;
|
|
14
|
-
messages.push({ id: position, role: role === 'user' ? 'user' : 'assistant', parts: [], seq: changed });
|
|
15
|
-
if (text.trim())
|
|
16
|
-
messages.at(-1).parts.push({ type: 'text', text });
|
|
17
|
-
}
|
|
18
|
-
for (const call of calls) {
|
|
19
|
-
if (messages.at(-1)?.role !== 'assistant') {
|
|
20
|
-
messages.push({ id: position, role: 'assistant', parts: [], seq: changed });
|
|
21
|
-
}
|
|
22
|
-
const message = messages.at(-1);
|
|
23
|
-
message.parts.push({ type: 'tool', id: call.id, name: call.name, input: call.input });
|
|
24
|
-
message.seq = changed;
|
|
25
|
-
at.set(call.id, message);
|
|
26
|
-
}
|
|
27
|
-
for (const result of results) {
|
|
28
|
-
const answered = at.get(result.id);
|
|
29
|
-
if (!answered)
|
|
30
|
-
continue;
|
|
31
|
-
const error = failed(result);
|
|
32
|
-
answered.parts = answered.parts.map((part) => part.type === 'tool' && part.id === result.id
|
|
33
|
-
? { ...part, output: result.output, ...(error ? { error: true } : {}) }
|
|
34
|
-
: part);
|
|
35
|
-
answered.seq = changed;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return messages.filter((message) => message.parts.length > 0);
|
|
39
|
-
}
|
|
40
|
-
const chat = ({ id, role, parts }) => ({ id, role, parts });
|
|
41
|
-
/** The latest `limit` of `messages`: none for a limit of 0 (`slice(-0)` would be all of them). */
|
|
42
|
-
const latestOf = (messages, limit) => (limit > 0 ? messages.slice(-limit) : []);
|
|
43
|
-
/**
|
|
44
|
-
* A page of the conversation in `db`, whose items are `items`'. With a
|
|
45
|
-
* `cursor`: the messages that changed since it, in the order they changed,
|
|
46
|
-
* and the cursor to read on from. Without: the latest `limit` messages (or
|
|
47
|
-
* the latest before id `before`), oldest first, and the cursor to follow the
|
|
48
|
-
* conversation from. `view: 'summary'` gives each turn's message and answer.
|
|
49
|
-
*/
|
|
50
|
-
export async function page(db, items, request = {}) {
|
|
51
|
-
const { limit = 50, before, cursor, view } = request;
|
|
52
|
-
await SCHEMA.ensure(db);
|
|
53
|
-
const start = await startOf(db);
|
|
54
|
-
const summary = view === 'summary';
|
|
55
|
-
const result = cursor === undefined
|
|
56
|
-
? await (summary ? summaryLatest : latest)(db, items, before, limit)
|
|
57
|
-
: await (summary ? summarySince : since)(db, items, cursor, limit);
|
|
58
|
-
return start.n ? { ...result, first: start.n } : result;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* The rows (`n`, `seq`, `item`) of the transcript and the trajectories from
|
|
62
|
-
* position `first` on, before position `until` if given, in order.
|
|
63
|
-
*/
|
|
64
|
-
async function rowsFrom(db, first, until = Number.MAX_SAFE_INTEGER) {
|
|
65
|
-
const range = 'WHERE n >= ? AND n < ? ORDER BY n';
|
|
66
|
-
const turns = await db.values(`SELECT n, seq, item FROM ${TURNS} ${range}`, [first, until]);
|
|
67
|
-
const steps = await db.values(`SELECT n, seq, item FROM ${STEPS} ${range}`, [first, until]);
|
|
68
|
-
return [...turns, ...steps].toSorted((a, b) => Number(a[0]) - Number(b[0]));
|
|
69
|
-
}
|
|
70
|
-
/** Whether the conversation has a turn before a position: a page that starts there has more before it. */
|
|
71
|
-
const EARLIER = `SELECT EXISTS (SELECT 1 FROM ${TURNS} WHERE n < ?)`;
|
|
72
|
-
/** The first value of the first row of `sql`. */
|
|
73
|
-
const one = async (db, sql, params) => (await db.values(sql, params))[0]?.[0];
|
|
74
|
-
/** The cursor of the conversation as it is now. */
|
|
75
|
-
async function now(db) {
|
|
76
|
-
const start = await startOf(db);
|
|
77
|
-
const [, seq] = await lastOf(db);
|
|
78
|
-
return Math.max(start.seq, seq);
|
|
79
|
-
}
|
|
80
|
-
/** The messages that changed since `cursor`, rendered from the turn of the earliest change on. */
|
|
81
|
-
async function since(db, items, cursor, limit) {
|
|
82
|
-
const prompt = await one(db, `SELECT MIN(n) FROM ${TURNS} WHERE seq > ?`, [cursor]);
|
|
83
|
-
const step = await one(db, `SELECT MIN(turn) FROM ${STEPS} WHERE seq > ?`, [cursor]);
|
|
84
|
-
const changed = [prompt, step].filter((n) => n !== null && n !== undefined).map(Number);
|
|
85
|
-
if (changed.length === 0)
|
|
86
|
-
return { messages: [], more: false, cursor };
|
|
87
|
-
const messages = render(items, await rowsFrom(db, Math.min(...changed)))
|
|
88
|
-
.filter((m) => m.seq > cursor)
|
|
89
|
-
.toSorted((a, b) => a.seq - b.seq);
|
|
90
|
-
const shown = messages.slice(0, limit);
|
|
91
|
-
return { messages: shown.map(chat), more: messages.length > limit, cursor: shown.at(-1)?.seq ?? cursor };
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* The latest `limit` messages before id `before`, rendered from enough turns
|
|
95
|
-
* back to hold them: each turn starts with a message, so the rows from the
|
|
96
|
-
* `limit + 1`-th latest turn before `before` on hold more than `limit`. And
|
|
97
|
-
* to no later than the turn `before` is in: a turn's rows add only to its
|
|
98
|
-
* own messages, so the turns from `before` on change none before it.
|
|
99
|
-
*/
|
|
100
|
-
async function latest(db, items, before, limit) {
|
|
101
|
-
const end = before ?? Number.MAX_SAFE_INTEGER;
|
|
102
|
-
const first = await one(db, `SELECT COALESCE((SELECT n FROM ${TURNS} WHERE n < ? ORDER BY n DESC LIMIT 1 OFFSET ?), (SELECT MIN(n) FROM ${TURNS}))`, [end, limit]);
|
|
103
|
-
const cursor = await now(db);
|
|
104
|
-
if (first === null || first === undefined)
|
|
105
|
-
return { messages: [], more: false, cursor };
|
|
106
|
-
const until = before === undefined ? undefined : await one(db, `SELECT MIN(n) FROM ${TURNS} WHERE n >= ?`, [end]);
|
|
107
|
-
const rows = await rowsFrom(db, Number(first), until === null || until === undefined ? undefined : Number(until));
|
|
108
|
-
const messages = render(items, rows).filter((m) => m.id < end);
|
|
109
|
-
const earlier = await one(db, EARLIER, [first]);
|
|
110
|
-
return {
|
|
111
|
-
messages: latestOf(messages, limit).map(chat),
|
|
112
|
-
more: messages.length > limit || Boolean(Number(earlier)),
|
|
113
|
-
cursor,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
/** The columns of a turn the summary view reads. */
|
|
117
|
-
const TURN = `SELECT n, seq, item, answer, answered_at, answered_seq FROM ${TURNS}`;
|
|
118
|
-
/** The messages steered to turns that took them (`n`, `seq`, `item`): messages of the user, which the summary shows too. */
|
|
119
|
-
const STEERS = `SELECT st.n, s.seq, s.item FROM ${STEERED} st JOIN ${STEPS} s ON s.n = st.n`;
|
|
120
|
-
/**
|
|
121
|
-
* Turns' rows ({@link TURN}'s columns) as their user's messages and answers,
|
|
122
|
-
* with the messages steered to them (`steered`, {@link STEERS}'s columns),
|
|
123
|
-
* in order: the answer's message has the id it has in the full view (the
|
|
124
|
-
* turn's last message of the assistant), and the `seq` it was stored with;
|
|
125
|
-
* so does a steered message.
|
|
126
|
-
*/
|
|
127
|
-
function summarized(items, rows, steered = []) {
|
|
128
|
-
const messages = [];
|
|
129
|
-
const user = (n, seq, stored) => {
|
|
130
|
-
const text = items.read(loadItem(items, String(stored))).said?.[1] ?? '';
|
|
131
|
-
return { id: Number(n), role: 'user', parts: text.trim() ? [{ type: 'text', text }] : [], seq: Number(seq) };
|
|
132
|
-
};
|
|
133
|
-
for (const [n, seq, stored, answer, answeredAt, answeredSeq] of rows) {
|
|
134
|
-
messages.push(user(n, seq, stored));
|
|
135
|
-
if (answer !== null && answer !== undefined && answeredAt !== null && answeredAt !== undefined) {
|
|
136
|
-
const answered = typeof answer === 'string' ? answer : JSON.stringify(answer);
|
|
137
|
-
messages.push({
|
|
138
|
-
id: Number(answeredAt),
|
|
139
|
-
role: 'assistant',
|
|
140
|
-
parts: answered.trim() ? [{ type: 'text', text: answered }] : [],
|
|
141
|
-
seq: Number(answeredSeq),
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
for (const [n, seq, stored] of steered)
|
|
146
|
-
messages.push(user(n, seq, stored));
|
|
147
|
-
return messages.filter((message) => message.parts.length > 0).toSorted((a, b) => a.id - b.id);
|
|
148
|
-
}
|
|
149
|
-
/** The user's messages and answers stored since `cursor`, in the order they were. */
|
|
150
|
-
async function summarySince(db, items, cursor, limit) {
|
|
151
|
-
const asked = await db.values(`${TURN} WHERE seq > ?`, [cursor]);
|
|
152
|
-
const answered = await db.values(`${TURN} WHERE answered_seq > ?`, [cursor]);
|
|
153
|
-
const steered = await db.values(`${STEERS} WHERE s.seq > ?`, [cursor]);
|
|
154
|
-
const rows = new Map([...asked, ...answered].map((row) => [Number(row[0]), row]));
|
|
155
|
-
const messages = summarized(items, [...rows.values()], steered)
|
|
156
|
-
.filter((m) => m.seq > cursor)
|
|
157
|
-
.toSorted((a, b) => a.seq - b.seq);
|
|
158
|
-
const shown = messages.slice(0, limit);
|
|
159
|
-
return { messages: shown.map(chat), more: messages.length > limit, cursor: shown.at(-1)?.seq ?? cursor };
|
|
160
|
-
}
|
|
161
|
-
/** The latest `limit` user's messages and answers before id `before`: from as many turns, at most. */
|
|
162
|
-
async function summaryLatest(db, items, before, limit) {
|
|
163
|
-
const end = before ?? Number.MAX_SAFE_INTEGER;
|
|
164
|
-
const rows = await db.values(`${TURN} WHERE n < ? ORDER BY n DESC LIMIT ?`, [end, limit]);
|
|
165
|
-
const cursor = await now(db);
|
|
166
|
-
const first = rows.length > 0 ? Number(rows.at(-1)[0]) : end;
|
|
167
|
-
const steered = await db.values(`${STEERS} WHERE st.n >= ? AND st.n < ? ORDER BY st.n`, [first, end]);
|
|
168
|
-
const messages = summarized(items, rows.toReversed(), steered).filter((m) => m.id < end);
|
|
169
|
-
const earlier = await one(db, EARLIER, [first]);
|
|
170
|
-
return {
|
|
171
|
-
messages: latestOf(messages, limit).map(chat),
|
|
172
|
-
more: messages.length > limit || Boolean(Number(earlier)),
|
|
173
|
-
cursor,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* The `messages` view of an agent whose framework keeps the conversation of
|
|
178
|
-
* turns, with its `items`: `sessionObject` finds them on it, and gives the
|
|
179
|
-
* agent `settle` and `compact`, and `clear` that keeps the ids going.
|
|
180
|
-
*/
|
|
181
|
-
export const conversationView = (items) => ITEMS.on(async (ctx, request) => page(ctx.db, items, request ?? {}), items);
|
|
182
|
-
//# sourceMappingURL=messages.js.map
|
package/dist/messages.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAoB,KAAK,EAAkD,MAAM,aAAa,CAAC;AACtG,OAAO,EAAE,MAAM,EAAc,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAWjH,oGAAoG;AACpG,SAAS,MAAM,CAAI,KAAe,EAAE,IAAiB;IACnD,MAAM,QAAQ,GAAe,EAAE,CAAC;IAChC,kDAAkD;IAClD,MAAM,EAAE,GAAG,IAAI,GAAG,EAAoB,CAAC;IACvC,KAAK,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QAC3B,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC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AC;IACtG,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC;IACzF,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAChD,OAAO;QACL,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC;QAC7C,IAAI,EAAE,QAAQ,CAAC,MAAM,GAAG,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACzD,MAAM;KACP,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAI,KAAe,EAAE,EAAE,CACrD,KAAK,CAAC,EAAE,CACN,KAAK,EAAE,GAAgB,EAAE,OAAyB,EAAqB,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,IAAI,EAAE,CAAC,EAC5G,KAAK,CACN,CAAC"}
|
package/dist/object.d.ts
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import * as restate from '@restatedev/sessions';
|
|
2
|
-
import { type AgentContext, type AgentViewContext, type Answering, type Messages, type MessagesRequest, type Summarize, type Window } from './common.ts';
|
|
3
|
-
import { type Items } from './conversation.ts';
|
|
4
|
-
import { type Notifications, type Resolved, type Signal, type Steered, type Steering, type Waiting, type Watch } from './companion.ts';
|
|
5
|
-
/** What an object is doing: the message it answers, if any, and what its tools wait for. */
|
|
6
|
-
export interface Status {
|
|
7
|
-
/** The message being answered (by a `chat` handler), or null. */
|
|
8
|
-
answering: Answering | null;
|
|
9
|
-
/** The waits of its tools, oldest first (see `pending`). */
|
|
10
|
-
waiting: Waiting[];
|
|
11
|
-
/** The messages steered to the message being answered that it has not taken yet, oldest first; absent if none. */
|
|
12
|
-
steering?: Steering[];
|
|
13
|
-
}
|
|
14
|
-
/** What `stop` did: the invocation of the message it stopped, or null if none was answered. */
|
|
15
|
-
export interface Stopped {
|
|
16
|
-
stopped: string | null;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* The views every object here has, beside its own: `signal`, which resolves
|
|
20
|
-
* a wait of the object's tools (see `signal` in tools.ts), `pending`, the
|
|
21
|
-
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
22
|
-
* it answers, and `notifications` and `watch`, the revision of its
|
|
23
|
-
* conversation, and a wait for a newer one (an agent's
|
|
24
|
-
* has `clear` and `sync` too: {@link FOLLOWED}). Each asks the object's
|
|
25
|
-
* companion (its own `<name>.prebuilt.restate`, at the same key), which
|
|
26
|
-
* knows what the object does: they run while the object's handler works.
|
|
27
|
-
* They are the only way a client reaches the companion, whose handlers
|
|
28
|
-
* Restate's ingress refuses.
|
|
29
|
-
*/
|
|
30
|
-
declare const BUILT_IN: {
|
|
31
|
-
signal: (ctx: AgentViewContext, signal: Signal) => Promise<Resolved>;
|
|
32
|
-
pending: (ctx: AgentViewContext) => Promise<Waiting[]>;
|
|
33
|
-
status: (ctx: AgentViewContext) => Promise<Status>;
|
|
34
|
-
/** Cancel the invocation of the message being answered (`status`'s), as Restate's admin API cancels one. */
|
|
35
|
-
stop: (ctx: AgentViewContext) => Promise<Stopped>;
|
|
36
|
-
/** The revision of the conversation: each change of what `messages` and `status` give increases it. */
|
|
37
|
-
notifications: (ctx: AgentViewContext) => Promise<Notifications>;
|
|
38
|
-
/**
|
|
39
|
-
* The revision, once there is one newer than `revision`, or at the timeout (50 s by
|
|
40
|
-
* default, at most 60 s), whichever comes first: a client then reads what
|
|
41
|
-
* changed. It waits in Restate, holding no process; a client that retries
|
|
42
|
-
* it with the same idempotency key joins the same wait.
|
|
43
|
-
*/
|
|
44
|
-
watch: {
|
|
45
|
-
inactivityTimeout: {
|
|
46
|
-
minutes: number;
|
|
47
|
-
};
|
|
48
|
-
handler: (ctx: AgentViewContext, watch: Watch) => Promise<Notifications>;
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
/** The views of every agent: {@link followed}'s. */
|
|
52
|
-
declare const FOLLOWED: {
|
|
53
|
-
/**
|
|
54
|
-
* Forget the conversation: stop the message being answered (as `stop`),
|
|
55
|
-
* then empty the key's database, and what its companion holds (waits and
|
|
56
|
-
* kept signals). The conversation's ids and cursors go on (see
|
|
57
|
-
* conversation.ts), so a follower reads on, and drops the messages before `first`.
|
|
58
|
-
* A message sent before it is answered first, then forgotten too; one sent
|
|
59
|
-
* after starts anew.
|
|
60
|
-
*/
|
|
61
|
-
clear: {
|
|
62
|
-
inactivityTimeout: {
|
|
63
|
-
minutes: number;
|
|
64
|
-
};
|
|
65
|
-
handler: (ctx: AgentViewContext) => Promise<Stopped>;
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* Send a message to the agent while it answers another: the turn being
|
|
69
|
-
* answered takes it at its next step (once the model's tool calls have
|
|
70
|
-
* their results), if its framework takes steered messages and it reaches
|
|
71
|
-
* one; otherwise the message is answered as a turn of its own, after the
|
|
72
|
-
* messages before it, in the order it was sent. With no message being
|
|
73
|
-
* answered, it starts a turn. Either way it is answered once: send it with
|
|
74
|
-
* an idempotency key, so that a retried request is the same message. It
|
|
75
|
-
* takes the message as plain text or as a JSON string, as `chat` does.
|
|
76
|
-
*/
|
|
77
|
-
steer: {
|
|
78
|
-
input: restate.Serde<string>;
|
|
79
|
-
accept: string;
|
|
80
|
-
handler: (ctx: AgentViewContext, text: string) => Promise<Steered>;
|
|
81
|
-
};
|
|
82
|
-
sync: {
|
|
83
|
-
inactivityTimeout: {
|
|
84
|
-
minutes: number;
|
|
85
|
-
};
|
|
86
|
-
handler: (ctx: AgentViewContext, { cursor, revision, timeoutSeconds, limit, view }?: SyncRequest) => Promise<Synced>;
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* The agents' handlers that only their own views call (Restate's ingress
|
|
91
|
-
* refuses them), and their types leave out: `erase`, `clear`'s work, once
|
|
92
|
-
* the handlers before it are done. It empties the key's database and what its
|
|
93
|
-
* companion holds, and the conversation's ids and cursors go on where they got to
|
|
94
|
-
* (conversation.ts).
|
|
95
|
-
*/
|
|
96
|
-
declare const internal: (items: Items<unknown> | undefined) => {
|
|
97
|
-
erase: {
|
|
98
|
-
ingressPrivate: boolean;
|
|
99
|
-
handler: (ctx: AgentContext) => Promise<void>;
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Close the latest turn, unanswered, if invocation `invocationId` ran it
|
|
103
|
-
* and is gone: the companion sends this once it finds the turn's
|
|
104
|
-
* invocation ended without answering (killed, say), so that the
|
|
105
|
-
* conversation shows the turn closed before the next message. Whether it
|
|
106
|
-
* closed it.
|
|
107
|
-
*/
|
|
108
|
-
settle: {
|
|
109
|
-
ingressPrivate: boolean;
|
|
110
|
-
handler: (ctx: AgentContext, invocationId: string) => Promise<boolean>;
|
|
111
|
-
};
|
|
112
|
-
/** Summarize the earliest turns, past the agent's budget. Whether it did. */
|
|
113
|
-
compact: {
|
|
114
|
-
ingressPrivate: boolean;
|
|
115
|
-
handler: (ctx: AgentContext) => Promise<boolean>;
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* What `sync` reads: what is new or changed since `cursor` (default 0, all
|
|
120
|
-
* of it), at most `limit` messages (default 100); once the revision is past
|
|
121
|
-
* `revision`, if given, or at `timeoutSeconds` (default 50, at most 60).
|
|
122
|
-
*/
|
|
123
|
-
export interface SyncRequest {
|
|
124
|
-
/** The messages' view: `full` (the default), or `summary` (see `MessagesRequest`). */
|
|
125
|
-
view?: MessagesRequest['view'];
|
|
126
|
-
cursor?: number;
|
|
127
|
-
revision?: number;
|
|
128
|
-
timeoutSeconds?: number;
|
|
129
|
-
limit?: number;
|
|
130
|
-
}
|
|
131
|
-
/** What `sync` answers: the messages since the cursor, as `messages` gives them, the status and the revision. */
|
|
132
|
-
export interface Synced extends Messages {
|
|
133
|
-
revision: number;
|
|
134
|
-
status: Status;
|
|
135
|
-
}
|
|
136
|
-
/** An object's (or agent's) name, its description, and the tables of your own in its database, if any (see `sessionObject`). */
|
|
137
|
-
export interface Definition<P extends string> {
|
|
138
|
-
name: P;
|
|
139
|
-
/** What it is: for the UI, and for the model of a parent that hands it tasks (`subagent`). */
|
|
140
|
-
description?: string;
|
|
141
|
-
schema?: readonly restate.Step[];
|
|
142
|
-
/**
|
|
143
|
-
* Whether it works unwatched, such as a subagent: its turn publishes to its
|
|
144
|
-
* companion only at its end, not at each step, so a client that follows it
|
|
145
|
-
* sees each turn once it ended. `messages` has every turn as before.
|
|
146
|
-
*/
|
|
147
|
-
background?: boolean;
|
|
148
|
-
/**
|
|
149
|
-
* For an agent on the conversation of turns: how many of the latest
|
|
150
|
-
* answered turns its model sees whole, with their tool calls and results
|
|
151
|
-
* (default 1), or `'all'`.
|
|
152
|
-
*/
|
|
153
|
-
window?: Window;
|
|
154
|
-
/** For an agent on the conversation of turns: how many tokens of the turns before the window its model sees (default 32 000). */
|
|
155
|
-
budget?: number;
|
|
156
|
-
/**
|
|
157
|
-
* For an agent on the conversation of turns: how it summarizes the turns
|
|
158
|
-
* before the window once they outgrow `budget`, in an invocation of its
|
|
159
|
-
* own (`compact`), after the answer. Without, they are only left out.
|
|
160
|
-
*/
|
|
161
|
-
summarize?: Summarize;
|
|
162
|
-
}
|
|
163
|
-
/** Each entry's function, as clients see it; none for entries left out. */
|
|
164
|
-
type Functions<H> = string extends keyof H ? unknown : {
|
|
165
|
-
[K in keyof H]: H[K] extends {
|
|
166
|
-
handler: infer F;
|
|
167
|
-
} ? F : H[K];
|
|
168
|
-
};
|
|
169
|
-
/** The names of the handlers and views every object (or agent) has, which its own cannot take. */
|
|
170
|
-
type Reserved = {
|
|
171
|
-
[name in keyof typeof BUILT_IN | keyof typeof FOLLOWED | keyof ReturnType<typeof internal> | 'steered']?: 'a handler every object has: name yours otherwise';
|
|
172
|
-
};
|
|
173
|
-
/** What an object with `handlers` and `views` has: those of every object, an agent's `clear` and `sync`, and its own. */
|
|
174
|
-
type With<H, V> = Functions<typeof BUILT_IN> & (V extends {
|
|
175
|
-
messages: unknown;
|
|
176
|
-
} ? Functions<typeof FOLLOWED> : unknown) & Functions<H> & Functions<V>;
|
|
177
|
-
/**
|
|
178
|
-
* A Restate virtual object with a SQL database per key, `ctx.db`: a
|
|
179
|
-
* `restate.sessionObject` of @restatedev/sessions, set up for agents.
|
|
180
|
-
*
|
|
181
|
-
* const notes = sessionObject({
|
|
182
|
-
* name: 'notes',
|
|
183
|
-
* handlers: {
|
|
184
|
-
* add: (ctx, note: string) => ctx.db.execute('INSERT INTO notes VALUES (?)', [note]),
|
|
185
|
-
* },
|
|
186
|
-
* });
|
|
187
|
-
*
|
|
188
|
-
* Besides its own handlers and views, it has the views `status`, what it is
|
|
189
|
-
* doing (the message it answers, and what its tools wait for), `stop`,
|
|
190
|
-
* which stops that message, `pending`, the waits
|
|
191
|
-
* of its tools (`signal()`), `signal({ id, value })`, which resolves one,
|
|
192
|
-
* and `notifications` and `watch({ revision })`, to follow its
|
|
193
|
-
* conversation. An agent, an object with a `messages` view (such as
|
|
194
|
-
* `claude.messages()`), has `clear` too, which forgets its conversation
|
|
195
|
-
* (its database too), and `sync`, which `follow()` of @restatedev/agents/client
|
|
196
|
-
* reads.
|
|
197
|
-
*
|
|
198
|
-
* `schema`, optional: tables of your own, beside the agent's (which are its
|
|
199
|
-
* own business), made in each key's database as a session object makes
|
|
200
|
-
* them. Any name is yours but those starting with `restate_`, the agent's
|
|
201
|
-
* (`restate_pi_tasks`, `restate_agent_turns`, `restate_agent_items`...).
|
|
202
|
-
*
|
|
203
|
-
* `options` and `metadata` add to (or replace) the ones set here, such as
|
|
204
|
-
* `options: { retryPolicy: ... }`.
|
|
205
|
-
*/
|
|
206
|
-
export declare const sessionObject: <P extends string, H extends Record<string, restate.Entry<AgentContext>>, V extends Record<string, restate.Entry<AgentViewContext>>>(definition: Definition<P> & {
|
|
207
|
-
handlers?: H & Reserved;
|
|
208
|
-
views?: V & Reserved;
|
|
209
|
-
metadata?: Record<string, string>;
|
|
210
|
-
options?: restate.ObjectOptions;
|
|
211
|
-
}) => restate.VirtualObjectDefinition<P, With<H, V>>;
|
|
212
|
-
export {};
|
|
213
|
-
//# sourceMappingURL=object.d.ts.map
|
package/dist/object.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAEhD,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EAUd,KAAK,QAAQ,EACb,KAAK,eAAe,EAGpB,KAAK,SAAS,EAEd,KAAK,MAAM,EACZ,MAAM,aAAa,CAAC;AACrB,OAAO,EAAW,KAAK,KAAK,EAAmD,MAAM,mBAAmB,CAAC;AACzG,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,MAAM,EAEX,KAAK,OAAO,EACZ,KAAK,QAAQ,EAEb,KAAK,OAAO,EACZ,KAAK,KAAK,EACX,MAAM,gBAAgB,CAAC;AAYxB,4FAA4F;AAC5F,MAAM,WAAW,MAAM;IACrB,iEAAiE;IACjE,SAAS,EAAE,SAAS,GAAG,IAAI,CAAC;IAC5B,4DAA4D;IAC5D,OAAO,EAAE,OAAO,EAAE,CAAC;IACnB,kHAAkH;IAClH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;CACvB;AAED,+FAA+F;AAC/F,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAgCD;;;;;;;;;;;GAWG;AACH,QAAA,MAAM,QAAQ;kBACQ,gBAAgB,UAAU,MAAM,KAAG,OAAO,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