@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
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { signals, signalsOf } from '../signals.js';
|
|
2
|
+
import { handlerContext } from './context.js';
|
|
3
|
+
/** Whether a target is keyed: a virtual object (key) or a workflow (id). */
|
|
4
|
+
const keyed = (target) => 'object' in target || 'workflow' in target;
|
|
5
|
+
/** LangGraph's keys, in a node's config, of its run's checkpointer, durability, and task id. */
|
|
6
|
+
const CHECKPOINTER = '__pregel_checkpointer';
|
|
7
|
+
const DURABILITY = '__pregel_durability';
|
|
8
|
+
const TASK_ID = '__pregel_task_id';
|
|
9
|
+
/** The run of a node of `kind`, from LangGraph's config. */
|
|
10
|
+
function stepOf(config, kind) {
|
|
11
|
+
const ctx = handlerContext(config.context);
|
|
12
|
+
if (!ctx) {
|
|
13
|
+
throw new TypeError(`nodes.${kind} runs in a graph that langgraph.agent({ graph }) runs, or that is invoked with the handler's ` +
|
|
14
|
+
'context: invoke(input, { context: ctx })');
|
|
15
|
+
}
|
|
16
|
+
const configurable = (config.configurable ?? {});
|
|
17
|
+
if (!configurable[CHECKPOINTER]) {
|
|
18
|
+
throw new TypeError(`nodes.${kind} runs in a graph with a checkpointer, so that a retried attempt resumes it: ` +
|
|
19
|
+
'compile({ checkpointer: new SqlCheckpointSaver(ctx.db) })');
|
|
20
|
+
}
|
|
21
|
+
// Only 'sync' stores a step's checkpoint before its tasks run. Otherwise a crash before the save makes the resumed
|
|
22
|
+
// run create the checkpoint again, with a new id, and so new task ids: the step would be made twice.
|
|
23
|
+
const durability = config.durability ?? configurable[DURABILITY];
|
|
24
|
+
if (durability !== 'sync') {
|
|
25
|
+
throw new TypeError(`nodes.${kind} runs in a graph that stores each step's checkpoint before the step runs, so that a retried ` +
|
|
26
|
+
"attempt gets the same task ids: invoke(input, { durability: 'sync' })");
|
|
27
|
+
}
|
|
28
|
+
// The task's namespace holds its id, which LangGraph derives from the checkpoint the step started from: a node
|
|
29
|
+
// run again from that checkpoint gets the same one. It holds the subgraph's path too.
|
|
30
|
+
const namespace = String(configurable.checkpoint_ns || configurable[TASK_ID]);
|
|
31
|
+
const node = namespace.split('|').at(-1).split(':')[0];
|
|
32
|
+
return { ctx, node, idempotencyKey: `langgraph:${ctx.key}:${namespace}` };
|
|
33
|
+
}
|
|
34
|
+
/** The state's update that writes `value` to `output`, if given. */
|
|
35
|
+
const update = (output, value) => (output === undefined ? {} : { [output]: value });
|
|
36
|
+
/** As JSON, which Restate and the companion store: the state may hold class instances. */
|
|
37
|
+
const plain = (value) => (value === undefined ? undefined : JSON.parse(JSON.stringify(value)));
|
|
38
|
+
/**
|
|
39
|
+
* A node that waits for the signal `name` to the agent, and writes its value
|
|
40
|
+
* to `output`: a person's approval, say, or another system's result. The
|
|
41
|
+
* graph waits without holding a process, for as long as it takes. The
|
|
42
|
+
* agent's `pending` view lists the wait, with `input`'s value, and its
|
|
43
|
+
* `signal` view resolves it:
|
|
44
|
+
*
|
|
45
|
+
* graph.addNode('approve', nodes.waitForSignal('approval', { input: (s) => s.refund, output: 'approved' }))
|
|
46
|
+
*/
|
|
47
|
+
export const waitForSignal =
|
|
48
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- any graph's state
|
|
49
|
+
(name, { output, input } = {}) => async (state, config) => {
|
|
50
|
+
const step = stepOf(config, 'waitForSignal');
|
|
51
|
+
// The wait's id starts with the invocation's (the same in each attempt), as the waits of tools do, so that a
|
|
52
|
+
// turn that ends without it (killed, say) has it closed: the companion closes the waits whose ids start with
|
|
53
|
+
// the turn's.
|
|
54
|
+
const id = `${step.ctx.request.id}:${step.idempotencyKey}`;
|
|
55
|
+
const shown = input === undefined ? {} : { input: plain(input(state)) };
|
|
56
|
+
const value = await step.ctx
|
|
57
|
+
.rpc(signals, signalsOf(step.ctx), { idempotencyKey: id })
|
|
58
|
+
.wait({ id, name, ...shown });
|
|
59
|
+
return update(output, value);
|
|
60
|
+
};
|
|
61
|
+
/** Throw a `TypeError` if `key` is given for a target that is not keyed, or the other way around. */
|
|
62
|
+
function checkKey(target, key) {
|
|
63
|
+
if (keyed(target) !== (key !== undefined)) {
|
|
64
|
+
throw new TypeError(`${target.name} ${keyed(target) ? 'is' : 'is not'} keyed: ${keyed(target) ? 'pass' : 'drop'} key`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A node that calls `handler` of `target` (a service, virtual object or
|
|
69
|
+
* workflow), and writes its result to `output`:
|
|
70
|
+
*
|
|
71
|
+
* graph.addNode('status', nodes.call(orders, 'status', { input: (s) => s.order, key: (s) => s.customer, output: 'status' }))
|
|
72
|
+
*/
|
|
73
|
+
export const call = (target, handler, options = {}) => {
|
|
74
|
+
checkKey(target, options.key);
|
|
75
|
+
return async (state, config) => {
|
|
76
|
+
const { ctx, idempotencyKey } = stepOf(config, 'call');
|
|
77
|
+
const result = await ctx.rpc({
|
|
78
|
+
service: target.name,
|
|
79
|
+
handler,
|
|
80
|
+
key: options.key?.(state),
|
|
81
|
+
input: plain(options.input?.(state)),
|
|
82
|
+
idempotencyKey,
|
|
83
|
+
});
|
|
84
|
+
return update(options.output, result);
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* A node that sends to `handler` of `target`, without waiting for it to run,
|
|
89
|
+
* after `delay` if given (such as `{ days: 1 }`). It writes nothing:
|
|
90
|
+
*
|
|
91
|
+
* graph.addNode('remind', nodes.send(checkins, 'remind', { input: (s) => s.checkin, delay: { days: 1 } }))
|
|
92
|
+
*/
|
|
93
|
+
export const send = (target, handler, options = {}) => {
|
|
94
|
+
checkKey(target, options.key);
|
|
95
|
+
return async (state, config) => {
|
|
96
|
+
const { ctx, idempotencyKey } = stepOf(config, 'send');
|
|
97
|
+
ctx.send({
|
|
98
|
+
service: target.name,
|
|
99
|
+
handler,
|
|
100
|
+
key: options.key?.(state),
|
|
101
|
+
input: plain(options.input?.(state)),
|
|
102
|
+
delay: options.delay,
|
|
103
|
+
idempotencyKey,
|
|
104
|
+
});
|
|
105
|
+
return {};
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* A node that runs the workflow `target` (its `run` handler) to the end, and
|
|
110
|
+
* writes its result to `output`. The workflow's id is `key`'s, or one of the
|
|
111
|
+
* node's own, made of the agent's key and the node's task:
|
|
112
|
+
*
|
|
113
|
+
* graph.addNode('ship', nodes.workflow(shipping, { input: (s) => s.order, output: 'receipt' }))
|
|
114
|
+
*/
|
|
115
|
+
export const workflow = (target, { output, input, key } = {}) => async (state, config) => {
|
|
116
|
+
const { ctx, idempotencyKey } = stepOf(config, 'workflow');
|
|
117
|
+
const result = await ctx.rpc({
|
|
118
|
+
service: target.name,
|
|
119
|
+
handler: 'run',
|
|
120
|
+
key: key?.(state) ?? idempotencyKey,
|
|
121
|
+
input: plain(input?.(state)),
|
|
122
|
+
idempotencyKey,
|
|
123
|
+
});
|
|
124
|
+
return update(output, result);
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* A node that sends a message to another agent (or any virtual object with
|
|
128
|
+
* a handler that takes text), and writes its answer to `output`. The message
|
|
129
|
+
* goes to the agent's key `<this key>.<node name>` unless `key` gives one, so
|
|
130
|
+
* each run of the graph continues the same conversation with it:
|
|
131
|
+
*
|
|
132
|
+
* graph.addNode('research', nodes.subagent(researcher, { message: (s) => s.question, output: 'findings' }))
|
|
133
|
+
*/
|
|
134
|
+
export const subagent = (agent, { message, output, key, handler = 'chat', }) => async (state, config) => {
|
|
135
|
+
const { ctx, node, idempotencyKey } = stepOf(config, 'subagent');
|
|
136
|
+
const answer = await ctx.rpc({
|
|
137
|
+
service: agent.name,
|
|
138
|
+
handler,
|
|
139
|
+
key: key?.(state) ?? `${ctx.key}.${node}`,
|
|
140
|
+
input: message(state),
|
|
141
|
+
idempotencyKey,
|
|
142
|
+
});
|
|
143
|
+
return update(output, answer);
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=nodes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../../src/langgraph/nodes.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAY9C,4EAA4E;AAC5E,MAAM,KAAK,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,QAAQ,IAAI,MAAM,IAAI,UAAU,IAAI,MAAM,CAAC;AAS7E,gGAAgG;AAChG,MAAM,YAAY,GAAG,uBAAuB,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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,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACjE,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"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;QAC5B,MAAM,EAAE,IAAI,EAAE,KAAK,GAAG,EAAE,EAAE,OAAO,GAAG,EAAE,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACvF,IAAI,IAAI,EAAE,CAAC;YACT,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,IAAI,CAAC;YAC1B,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;YACvG,IAAI,IAAI,CAAC,IAAI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J,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
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import * as restate from '@restatedev/sessions';
|
|
2
|
-
import { type Messages, type MessagesRequest, type
|
|
3
|
-
import { type
|
|
2
|
+
import { 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 './signals.ts';
|
|
4
5
|
/** What an object is doing: the message it answers, if any, and what its tools wait for. */
|
|
5
6
|
export interface Status {
|
|
6
7
|
/** The message being answered (by a `chat` handler), or null. */
|
|
7
|
-
answering:
|
|
8
|
+
answering: Answering | null;
|
|
8
9
|
/** The waits of its tools, oldest first (see `pending`). */
|
|
9
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[];
|
|
10
13
|
}
|
|
11
14
|
/** What `stop` did: the invocation of the message it stopped, or null if none was answered. */
|
|
12
15
|
export interface Stopped {
|
|
@@ -18,7 +21,7 @@ export interface Stopped {
|
|
|
18
21
|
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
19
22
|
* it answers, and `notifications` and `watch`, the revision of its
|
|
20
23
|
* conversation, and a wait for a newer one (an agent's
|
|
21
|
-
* has `clear` and `sync` too: {@link
|
|
24
|
+
* has `clear` and `sync` too: {@link FOLLOWED}). Each asks the object's
|
|
22
25
|
* `signals` companion, which knows what the object does: they run while the
|
|
23
26
|
* object's handler works.
|
|
24
27
|
*/
|
|
@@ -43,21 +46,13 @@ declare const BUILT_IN: {
|
|
|
43
46
|
handler: (ctx: restate.ViewContext, watch: Watch) => Promise<Notifications>;
|
|
44
47
|
};
|
|
45
48
|
};
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
* those of every object: `clear`, which forgets the conversation; and
|
|
49
|
-
* `sync`, the conversation since `cursor` (by default, all of it), what the
|
|
50
|
-
* agent is doing, and the revision, in one request; with `revision`, once
|
|
51
|
-
* there is a newer one, or at the timeout. A client follows the
|
|
52
|
-
* conversation by sending each answer's `revision` and `cursor` back, at
|
|
53
|
-
* once while it has `more`.
|
|
54
|
-
*/
|
|
55
|
-
declare const followed: (messages: (ctx: restate.ViewContext, request: MessagesRequest) => Promise<Messages>) => {
|
|
49
|
+
/** The views of every agent: {@link followed}'s. */
|
|
50
|
+
declare const FOLLOWED: {
|
|
56
51
|
/**
|
|
57
52
|
* Forget the conversation: stop the message being answered (as `stop`),
|
|
58
53
|
* then empty the key's database, and what its `signals` hold (waits and
|
|
59
|
-
* kept signals). The
|
|
60
|
-
*
|
|
54
|
+
* kept signals). The conversation's ids and cursors go on (see
|
|
55
|
+
* conversation.ts), so a follower reads on, and drops the messages before `first`.
|
|
61
56
|
* A message sent before it is answered first, then forgotten too; one sent
|
|
62
57
|
* after starts anew.
|
|
63
58
|
*/
|
|
@@ -67,24 +62,56 @@ declare const followed: (messages: (ctx: restate.ViewContext, request: MessagesR
|
|
|
67
62
|
};
|
|
68
63
|
handler: (ctx: restate.ViewContext) => Promise<Stopped>;
|
|
69
64
|
};
|
|
65
|
+
/**
|
|
66
|
+
* Send a message to the agent while it answers another: the turn being
|
|
67
|
+
* answered takes it at its next step (once the model's tool calls have
|
|
68
|
+
* their results), if its framework takes steered messages and it reaches
|
|
69
|
+
* one; otherwise the message is answered as a turn of its own, after the
|
|
70
|
+
* messages before it, in the order it was sent. With no message being
|
|
71
|
+
* answered, it starts a turn. Either way it is answered once: send it with
|
|
72
|
+
* an idempotency key, so that a retried request is the same message. It
|
|
73
|
+
* takes the message as plain text or as a JSON string, as `chat` does.
|
|
74
|
+
*/
|
|
75
|
+
steer: {
|
|
76
|
+
input: restate.Serde<string>;
|
|
77
|
+
accept: string;
|
|
78
|
+
handler: (ctx: restate.ViewContext, text: string) => Promise<Steered>;
|
|
79
|
+
};
|
|
70
80
|
sync: {
|
|
71
81
|
inactivityTimeout: {
|
|
72
82
|
minutes: number;
|
|
73
83
|
};
|
|
74
|
-
handler: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit }?: SyncRequest) => Promise<Synced>;
|
|
84
|
+
handler: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit, view }?: SyncRequest) => Promise<Synced>;
|
|
75
85
|
};
|
|
76
86
|
};
|
|
77
87
|
/**
|
|
78
88
|
* The agents' handlers that only their own views call (Restate's ingress
|
|
79
89
|
* refuses them), and their types leave out: `erase`, `clear`'s work, once
|
|
80
90
|
* the handlers before it are done. It empties the key's database and its
|
|
81
|
-
* signals', and the
|
|
91
|
+
* signals', and the conversation's ids and cursors go on where they got to
|
|
92
|
+
* (conversation.ts).
|
|
82
93
|
*/
|
|
83
|
-
declare const
|
|
94
|
+
declare const internal: (items: Items<unknown> | undefined) => {
|
|
84
95
|
erase: {
|
|
85
96
|
ingressPrivate: boolean;
|
|
86
97
|
handler: (ctx: restate.SessionContext) => Promise<void>;
|
|
87
98
|
};
|
|
99
|
+
/**
|
|
100
|
+
* Close the latest turn, unanswered, if invocation `invocationId` ran it
|
|
101
|
+
* and is gone: the companion sends this once it finds the turn's
|
|
102
|
+
* invocation ended without answering (killed, say), so that the
|
|
103
|
+
* conversation shows the turn closed before the next message. Whether it
|
|
104
|
+
* closed it.
|
|
105
|
+
*/
|
|
106
|
+
settle: {
|
|
107
|
+
ingressPrivate: boolean;
|
|
108
|
+
handler: (ctx: restate.SessionContext, invocationId: string) => Promise<boolean>;
|
|
109
|
+
};
|
|
110
|
+
/** Summarize the earliest turns, past the agent's budget. Whether it did. */
|
|
111
|
+
compact: {
|
|
112
|
+
ingressPrivate: boolean;
|
|
113
|
+
handler: (ctx: restate.SessionContext) => Promise<boolean>;
|
|
114
|
+
};
|
|
88
115
|
};
|
|
89
116
|
/**
|
|
90
117
|
* What `sync` reads: what is new or changed since `cursor` (default 0, all
|
|
@@ -92,6 +119,8 @@ declare const INTERNAL: {
|
|
|
92
119
|
* `revision`, if given, or at `timeoutSeconds` (default 50, at most 60).
|
|
93
120
|
*/
|
|
94
121
|
export interface SyncRequest {
|
|
122
|
+
/** The messages' view: `full` (the default), or `summary` (see `MessagesRequest`). */
|
|
123
|
+
view?: MessagesRequest['view'];
|
|
95
124
|
cursor?: number;
|
|
96
125
|
revision?: number;
|
|
97
126
|
timeoutSeconds?: number;
|
|
@@ -114,6 +143,20 @@ export interface Definition<P extends string> {
|
|
|
114
143
|
* sees each turn once it ended. `messages` has every turn as before.
|
|
115
144
|
*/
|
|
116
145
|
background?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* For an agent on the conversation of turns: how many of the latest
|
|
148
|
+
* answered turns its model sees whole, with their tool calls and results
|
|
149
|
+
* (default 1), or `'all'`.
|
|
150
|
+
*/
|
|
151
|
+
window?: Window;
|
|
152
|
+
/** For an agent on the conversation of turns: how many tokens of the turns before the window its model sees (default 32 000). */
|
|
153
|
+
budget?: number;
|
|
154
|
+
/**
|
|
155
|
+
* For an agent on the conversation of turns: how it summarizes the turns
|
|
156
|
+
* before the window once they outgrow `budget`, in an invocation of its
|
|
157
|
+
* own (`compact`), after the answer. Without, they are only left out.
|
|
158
|
+
*/
|
|
159
|
+
summarize?: Summarize;
|
|
117
160
|
}
|
|
118
161
|
/** Each entry's function, as clients see it; none for entries left out. */
|
|
119
162
|
type Functions<H> = string extends keyof H ? unknown : {
|
|
@@ -123,12 +166,12 @@ type Functions<H> = string extends keyof H ? unknown : {
|
|
|
123
166
|
};
|
|
124
167
|
/** The names of the handlers and views every object (or agent) has, which its own cannot take. */
|
|
125
168
|
type Reserved = {
|
|
126
|
-
[name in keyof typeof BUILT_IN | keyof
|
|
169
|
+
[name in keyof typeof BUILT_IN | keyof typeof FOLLOWED | keyof ReturnType<typeof internal> | 'steered']?: 'a handler every object has: name yours otherwise';
|
|
127
170
|
};
|
|
128
171
|
/** What an object with `handlers` and `views` has: those of every object, an agent's `clear` and `sync`, and its own. */
|
|
129
172
|
type With<H, V> = Functions<typeof BUILT_IN> & (V extends {
|
|
130
173
|
messages: unknown;
|
|
131
|
-
} ? Functions<
|
|
174
|
+
} ? Functions<typeof FOLLOWED> : unknown) & Functions<H> & Functions<V>;
|
|
132
175
|
/**
|
|
133
176
|
* A Restate virtual object with a SQL database per key, `ctx.db`: a
|
|
134
177
|
* `restate.sessionObject` of @restatedev/sessions, set up for agents.
|
|
@@ -153,8 +196,7 @@ type With<H, V> = Functions<typeof BUILT_IN> & (V extends {
|
|
|
153
196
|
* `schema`, optional: tables of your own, beside the agent's (which are its
|
|
154
197
|
* own business), made in each key's database as a session object makes
|
|
155
198
|
* them. Any name is yours but those starting with `restate_`, the agent's
|
|
156
|
-
* (`restate_pi_tasks`, `
|
|
157
|
-
* OpenCode's tables are in a database of their own.
|
|
199
|
+
* (`restate_pi_tasks`, `restate_agent_turns`, `restate_agent_items`...).
|
|
158
200
|
*
|
|
159
201
|
* `options` and `metadata` add to (or replace) the ones set here, such as
|
|
160
202
|
* `options: { retryPolicy: ... }`.
|
package/dist/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAEhD,OAAO,
|
|
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,SAAS,EAUd,KAAK,QAAQ,EACb,KA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