@restatedev/agents 0.0.0-stage → 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 +689 -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 +224 -3
- 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
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { SessionContext } from '@restatedev/sessions';
|
|
2
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
3
|
+
import type { Turn } from './common.ts';
|
|
4
|
+
import type { Status } from './object.ts';
|
|
5
|
+
/** An agent's context, as far as its signals need it: any handler's or view's of its object. */
|
|
6
|
+
type AgentContext = {
|
|
7
|
+
readonly key: string;
|
|
8
|
+
readonly request: {
|
|
9
|
+
readonly target: {
|
|
10
|
+
readonly service: string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
/** The agent of `ctx`: its object's name, and key. */
|
|
15
|
+
export declare const agentOf: (ctx: AgentContext) => Agent;
|
|
16
|
+
/** The key of the signals of the agent of `ctx`. */
|
|
17
|
+
export declare const signalsOf: (ctx: AgentContext) => string;
|
|
18
|
+
/** Where an object of @restatedev/agents (`sessionObject()`, every `agent()`) points to its companion. */
|
|
19
|
+
declare const COMPANION: unique symbol;
|
|
20
|
+
/**
|
|
21
|
+
* An agent's companion, for one of its keys: its `signals` object, which
|
|
22
|
+
* knows what the agent is doing (the message it answers, what its tools wait
|
|
23
|
+
* for), and how to reach it.
|
|
24
|
+
*/
|
|
25
|
+
export interface Companion {
|
|
26
|
+
/** The `signals` object, as a client takes it. */
|
|
27
|
+
object: typeof signals;
|
|
28
|
+
/** The key of the agent's key there: `<agent>:<key>`. */
|
|
29
|
+
key: string;
|
|
30
|
+
/** The agent, and its key. */
|
|
31
|
+
agent: Agent;
|
|
32
|
+
}
|
|
33
|
+
/** `definition`, pointing to its companion: what `sessionObject()` returns. */
|
|
34
|
+
export declare const withCompanion: <D extends {
|
|
35
|
+
name: string;
|
|
36
|
+
}>(definition: D) => D;
|
|
37
|
+
/**
|
|
38
|
+
* The companion of `agent`'s key `key`: for anyone holding an agent (an
|
|
39
|
+
* `sessionObject()` or `agent()` of @restatedev/agents) to reach what it is doing.
|
|
40
|
+
*/
|
|
41
|
+
export declare function companionOf(agent: {
|
|
42
|
+
name: string;
|
|
43
|
+
}, key: string): Companion;
|
|
44
|
+
/** Whether `agent` is an object of @restatedev/agents, with a companion. */
|
|
45
|
+
export declare const hasCompanion: (agent: object) => agent is object & Record<typeof COMPANION, unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* Deliver `text` to `agent`'s key `key`, from any handler: to the message it
|
|
48
|
+
* answers, if it answers one, which takes it at its next step (as `steer`);
|
|
49
|
+
* else as a message of its own (as `chat`). A turn that ends, or is stopped,
|
|
50
|
+
* before it takes it answers it after. It is sent, not awaited: `opts` may
|
|
51
|
+
* give it an idempotency key, so that it is delivered once.
|
|
52
|
+
*
|
|
53
|
+
* deliver(ctx, triage, 'ENG-1', 'The issue was closed', { idempotencyKey: delivery });
|
|
54
|
+
*/
|
|
55
|
+
export declare function deliver(ctx: Pick<restate.Context, 'objectSendClient'> | Pick<SessionContext, 'send'>, agent: {
|
|
56
|
+
name: string;
|
|
57
|
+
}, key: string, text: string, opts?: {
|
|
58
|
+
idempotencyKey?: string;
|
|
59
|
+
}): void;
|
|
60
|
+
/** A wait for a signal, as `pending` lists it. */
|
|
61
|
+
export interface Waiting {
|
|
62
|
+
/** The wait's id, which `signal` takes to resolve this wait and no other. */
|
|
63
|
+
id: string;
|
|
64
|
+
/** The signal it waits for, such as `approval`. */
|
|
65
|
+
name: string;
|
|
66
|
+
/** What it waits for, as the model said it (such as the refund to approve), if the tool takes an input. */
|
|
67
|
+
input?: unknown;
|
|
68
|
+
/** When it started, as an ISO date. */
|
|
69
|
+
since: string;
|
|
70
|
+
}
|
|
71
|
+
/** Which wait a signal resolves, and with what. */
|
|
72
|
+
export interface Signal {
|
|
73
|
+
/** The wait of this id (from `pending`). */
|
|
74
|
+
id?: string;
|
|
75
|
+
/** Else the oldest wait for the signal of this name. */
|
|
76
|
+
name?: string;
|
|
77
|
+
/** Its value: what the wait answers the model with. */
|
|
78
|
+
value?: unknown;
|
|
79
|
+
/** With a `name` and no one waiting for it, keep the signal for the next wait for it (by default it is dropped). */
|
|
80
|
+
keep?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/** What `signal` did: the id of the wait it resolved, or null if none waited (and it was dropped, or kept). */
|
|
83
|
+
export interface Resolved {
|
|
84
|
+
resolved: string | null;
|
|
85
|
+
}
|
|
86
|
+
/** A message steered to the message being answered (its turn): its id, `<turn>:steer:<n>`, and its text. */
|
|
87
|
+
export interface Steer {
|
|
88
|
+
id: string;
|
|
89
|
+
text: string;
|
|
90
|
+
/** A delivery (`deliver`): a cancelled turn answers it as a message of its own, rather than drop it. */
|
|
91
|
+
keep?: boolean;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* What `steer` did: the invocation that answers it, and whether it steers
|
|
95
|
+
* the message being answered (or, with none answered, was sent as a message
|
|
96
|
+
* of its own).
|
|
97
|
+
*/
|
|
98
|
+
export interface Steered {
|
|
99
|
+
invocationId: string;
|
|
100
|
+
steered: boolean;
|
|
101
|
+
}
|
|
102
|
+
/** The revision of an agent's conversation: each change of what a client reads increases it. */
|
|
103
|
+
export interface Notifications {
|
|
104
|
+
revision: number;
|
|
105
|
+
}
|
|
106
|
+
/** What `watch` waits for: a revision newer than `revision`, for at most `timeoutSeconds` (default 50, at most 60). */
|
|
107
|
+
export interface Watch {
|
|
108
|
+
revision: number;
|
|
109
|
+
timeoutSeconds?: number;
|
|
110
|
+
}
|
|
111
|
+
/** An agent: its object's name, and key. */
|
|
112
|
+
interface Agent {
|
|
113
|
+
service: string;
|
|
114
|
+
key: string;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The waits, in the order they started, each with its awakeable; the signals
|
|
118
|
+
* kept for the next wait; the messages steered to the turn being answered,
|
|
119
|
+
* and the turns closed last; and the conversation's revision, with the
|
|
120
|
+
* `watch`es waiting for a newer one.
|
|
121
|
+
*/
|
|
122
|
+
interface State {
|
|
123
|
+
/** The message the agent answers, from its turn's start to its close. */
|
|
124
|
+
turn: Turn;
|
|
125
|
+
waiting: (Waiting & {
|
|
126
|
+
awakeable: string;
|
|
127
|
+
})[];
|
|
128
|
+
kept: Record<string, unknown[]>;
|
|
129
|
+
steering: {
|
|
130
|
+
turn: string;
|
|
131
|
+
messages: Steer[];
|
|
132
|
+
};
|
|
133
|
+
closed: string[];
|
|
134
|
+
revision: number;
|
|
135
|
+
watching: {
|
|
136
|
+
revision: number;
|
|
137
|
+
awakeable: string;
|
|
138
|
+
}[];
|
|
139
|
+
}
|
|
140
|
+
/** A turn's close: see `close`. */
|
|
141
|
+
interface Close {
|
|
142
|
+
agent: Agent;
|
|
143
|
+
turn: string;
|
|
144
|
+
taken: string[];
|
|
145
|
+
cancelled: boolean;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The signals of every agent, keyed `<agent>:<key>` ({@link signalsOf}).
|
|
149
|
+
* `serve` serves it beside the agents; the handlers marked `@internal` are
|
|
150
|
+
* called by the agents' own handlers, and Restate's ingress refuses them.
|
|
151
|
+
*/
|
|
152
|
+
export declare const signals: restate.VirtualObjectDefinition<"signals", {
|
|
153
|
+
wait: (ctx: restate.ObjectSharedContext<State>, input: {
|
|
154
|
+
id: string;
|
|
155
|
+
name: string;
|
|
156
|
+
input?: unknown;
|
|
157
|
+
}) => Promise<unknown>;
|
|
158
|
+
resolve: (ctx: restate.ObjectContext<State>, { id, name, value, keep }: Signal) => Promise<{
|
|
159
|
+
resolved: null;
|
|
160
|
+
} | {
|
|
161
|
+
resolved: string;
|
|
162
|
+
}>;
|
|
163
|
+
pending: (ctx: restate.ObjectSharedContext<State>) => Promise<Waiting[]>;
|
|
164
|
+
listen: (ctx: restate.ObjectContext<State>, input: Waiting & {
|
|
165
|
+
awakeable: string;
|
|
166
|
+
}) => Promise<void>;
|
|
167
|
+
steer: (ctx: restate.ObjectContext<State>, input: {
|
|
168
|
+
agent: Agent;
|
|
169
|
+
text: string;
|
|
170
|
+
}) => Promise<Steered>;
|
|
171
|
+
start: (ctx: restate.ObjectContext<State>, input: {
|
|
172
|
+
agent: Agent;
|
|
173
|
+
turn: Turn;
|
|
174
|
+
}) => Promise<void>;
|
|
175
|
+
status: (ctx: restate.ObjectSharedContext<State>) => Promise<Status>;
|
|
176
|
+
watch: (ctx: restate.ObjectSharedContext<State>, input: Watch) => Promise<Notifications>;
|
|
177
|
+
deliver: (ctx: restate.ObjectContext<State>, input: {
|
|
178
|
+
agent: Agent;
|
|
179
|
+
text: string;
|
|
180
|
+
}) => Promise<Steered>;
|
|
181
|
+
steering: (ctx: restate.ObjectSharedContext<State>, input: string) => Promise<Steer[]>;
|
|
182
|
+
close: (ctx: restate.ObjectContext<State>, input: Close) => Promise<void>;
|
|
183
|
+
reset: (ctx: restate.ObjectContext<State>) => Promise<void>;
|
|
184
|
+
forget: (ctx: restate.ObjectContext<State>, input: string) => Promise<void>;
|
|
185
|
+
publish: (ctx: restate.ObjectContext<State>) => Promise<Notifications>;
|
|
186
|
+
progress: (ctx: restate.ObjectContext<State>, input: string) => Promise<Steer[]>;
|
|
187
|
+
notifications: (ctx: restate.ObjectSharedContext<State>) => Promise<Notifications>;
|
|
188
|
+
subscribe: (ctx: restate.ObjectContext<State>, input: {
|
|
189
|
+
revision: number;
|
|
190
|
+
awakeable: string;
|
|
191
|
+
}) => Promise<void>;
|
|
192
|
+
unsubscribe: (ctx: restate.ObjectContext<State>, input: string) => Promise<void>;
|
|
193
|
+
}>;
|
|
194
|
+
export {};
|
|
195
|
+
//# sourceMappingURL=signals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signals.d.ts","sourceRoot":"","sources":["../src/signals.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AA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AM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;CACb;AAKD;;;;;GAKG;AACH,UAAU,KAAK;IACb,yEAAyE;IACzE,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,CAAC,OAAO,GAAG;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;IAC7C,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IAChC,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,KAAK,EAAE,CAAA;KAAE,CAAC;IAC9C,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CACrD;AAgCD,mCAAmC;AACnC,UAAU,KAAK;IACb,KAAK,EAAE,KAAK,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB;AAoBD;;;;GAIG;AACH,eAAO,MAAM,OAAO;;YAK8C,MAAM;cAAQ,MAAM;gBAAU,OAAO;;mBAe9E,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,6BAA4C,MAAM;;;;;;;mBAwBX,MAAM;;;eAiBV,KAAK;cAAQ,MAAM;;;eAUnB,KAAK;cAAQ,IAAI;;;;;eAwCf,KAAK;cAAQ,MAAM;;;;;;;;;;kBA8DxB,MAAM;mBAAa,MAAM;;;EAkB5G,CAAC"}
|
package/dist/signals.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
2
|
+
/** The agent of `ctx`: its object's name, and key. */
|
|
3
|
+
export const agentOf = (ctx) => ({ service: ctx.request.target.service, key: ctx.key });
|
|
4
|
+
/** The key of the signals of agent `service`'s key `key`: `<agent>:<key>`. */
|
|
5
|
+
const keyOf = (service, key) => `${service}:${key}`;
|
|
6
|
+
/** The key of the signals of the agent of `ctx`. */
|
|
7
|
+
export const signalsOf = (ctx) => keyOf(ctx.request.target.service, ctx.key);
|
|
8
|
+
/** Where an object of @restatedev/agents (`sessionObject()`, every `agent()`) points to its companion. */
|
|
9
|
+
const COMPANION = Symbol('restate-agents.companion');
|
|
10
|
+
/** `definition`, pointing to its companion: what `sessionObject()` returns. */
|
|
11
|
+
export const withCompanion = (definition) => Object.defineProperty(definition, COMPANION, {
|
|
12
|
+
value: (key) => ({
|
|
13
|
+
object: signals,
|
|
14
|
+
key: keyOf(definition.name, key),
|
|
15
|
+
agent: { service: definition.name, key },
|
|
16
|
+
}),
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* The companion of `agent`'s key `key`: for anyone holding an agent (an
|
|
20
|
+
* `sessionObject()` or `agent()` of @restatedev/agents) to reach what it is doing.
|
|
21
|
+
*/
|
|
22
|
+
export function companionOf(agent, key) {
|
|
23
|
+
const companion = agent[COMPANION];
|
|
24
|
+
if (!companion)
|
|
25
|
+
throw new Error(`${agent.name} is not an agent of @restatedev/agents (a sessionObject() or an agent())`);
|
|
26
|
+
return companion(key);
|
|
27
|
+
}
|
|
28
|
+
/** Whether `agent` is an object of @restatedev/agents, with a companion. */
|
|
29
|
+
export const hasCompanion = (agent) => COMPANION in agent;
|
|
30
|
+
/**
|
|
31
|
+
* Deliver `text` to `agent`'s key `key`, from any handler: to the message it
|
|
32
|
+
* answers, if it answers one, which takes it at its next step (as `steer`);
|
|
33
|
+
* else as a message of its own (as `chat`). A turn that ends, or is stopped,
|
|
34
|
+
* before it takes it answers it after. It is sent, not awaited: `opts` may
|
|
35
|
+
* give it an idempotency key, so that it is delivered once.
|
|
36
|
+
*
|
|
37
|
+
* deliver(ctx, triage, 'ENG-1', 'The issue was closed', { idempotencyKey: delivery });
|
|
38
|
+
*/
|
|
39
|
+
export function deliver(ctx, agent, key, text, opts = {}) {
|
|
40
|
+
const { object, key: companion, agent: target } = companionOf(agent, key);
|
|
41
|
+
const delivery = { agent: target, text };
|
|
42
|
+
// A session object's handler, or one of the SDK's.
|
|
43
|
+
if ('send' in ctx)
|
|
44
|
+
ctx.send(object, companion, opts).deliver(delivery);
|
|
45
|
+
else
|
|
46
|
+
ctx.objectSendClient(object, companion).deliver(delivery, restate.rpc.sendOpts(opts));
|
|
47
|
+
}
|
|
48
|
+
/** The turns closed last, newest last: a message steered to one of them is a message of its own. */
|
|
49
|
+
const CLOSED = 16;
|
|
50
|
+
/** Increase the revision, and answer each `watch` waiting for it: the new revision. */
|
|
51
|
+
async function publish(ctx) {
|
|
52
|
+
const revision = ((await ctx.get('revision')) ?? 0) + 1;
|
|
53
|
+
ctx.set('revision', revision);
|
|
54
|
+
for (const { awakeable } of (await ctx.get('watching')) ?? [])
|
|
55
|
+
ctx.resolveAwakeable(awakeable, { revision });
|
|
56
|
+
ctx.clear('watching');
|
|
57
|
+
return { revision };
|
|
58
|
+
}
|
|
59
|
+
/** Send `text` to `agent`'s `chat`, as a message of its own: its invocation's id. */
|
|
60
|
+
const send = (ctx, { service, key }, text) => ctx.genericSend({ service, method: 'chat', key, parameter: text, inputSerde: restate.serde.json }).invocationId;
|
|
61
|
+
/**
|
|
62
|
+
* Steer `turn` (the message `agent` answers, if any) with `text`: queued for
|
|
63
|
+
* it to take at its next step, or, if it is over, sent as a message of its own.
|
|
64
|
+
*/
|
|
65
|
+
async function steered(ctx, agent, text, keep = false) {
|
|
66
|
+
const turn = (await ctx.get('turn'))?.invocationId ?? null;
|
|
67
|
+
if (turn === null || ((await ctx.get('closed')) ?? []).includes(turn)) {
|
|
68
|
+
return { invocationId: await send(ctx, agent, text), steered: false };
|
|
69
|
+
}
|
|
70
|
+
const steering = await ctx.get('steering');
|
|
71
|
+
const messages = steering?.turn === turn ? steering.messages : [];
|
|
72
|
+
const id = `${turn}:steer:${messages.length + 1}`;
|
|
73
|
+
ctx.set('steering', { turn, messages: [...messages, { id, text, ...(keep ? { keep } : {}) }] });
|
|
74
|
+
await publish(ctx);
|
|
75
|
+
return { invocationId: turn, steered: true };
|
|
76
|
+
}
|
|
77
|
+
/** Close `turn`, once: see `close`. */
|
|
78
|
+
async function closed(ctx, { agent, turn, taken, cancelled }) {
|
|
79
|
+
if ((await ctx.get('turn'))?.invocationId === turn)
|
|
80
|
+
ctx.clear('turn');
|
|
81
|
+
const done = (await ctx.get('closed')) ?? [];
|
|
82
|
+
if (done.includes(turn))
|
|
83
|
+
return;
|
|
84
|
+
ctx.set('closed', [...done, turn].slice(-CLOSED));
|
|
85
|
+
const steering = await ctx.get('steering');
|
|
86
|
+
if (steering?.turn !== turn)
|
|
87
|
+
return;
|
|
88
|
+
ctx.clear('steering');
|
|
89
|
+
for (const { id, text, keep } of steering.messages) {
|
|
90
|
+
if (!taken.includes(id) && (keep || !cancelled))
|
|
91
|
+
await send(ctx, agent, text);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** A handler that the agents' own handlers call, which clients cannot (through Restate's ingress). */
|
|
95
|
+
const internal = (fn) => restate.handlers.object.exclusive({ ingressPrivate: true }, fn);
|
|
96
|
+
/**
|
|
97
|
+
* The signals of every agent, keyed `<agent>:<key>` ({@link signalsOf}).
|
|
98
|
+
* `serve` serves it beside the agents; the handlers marked `@internal` are
|
|
99
|
+
* called by the agents' own handlers, and Restate's ingress refuses them.
|
|
100
|
+
*/
|
|
101
|
+
export const signals = restate.object({
|
|
102
|
+
name: 'signals',
|
|
103
|
+
handlers: {
|
|
104
|
+
/** Wait for the signal `name`, as wait `id` (for `input`), and return its value. */
|
|
105
|
+
wait: restate.handlers.object.shared(async (ctx, wait) => {
|
|
106
|
+
const { id, promise } = ctx.awakeable();
|
|
107
|
+
const since = new Date(await ctx.date.now()).toISOString();
|
|
108
|
+
ctx.objectSendClient(signals, ctx.key).listen({ ...wait, since, awakeable: id });
|
|
109
|
+
try {
|
|
110
|
+
return await promise;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
// The agent's invocation was cancelled: no one waits any more.
|
|
114
|
+
if (error instanceof restate.CancelledError)
|
|
115
|
+
ctx.objectSendClient(signals, ctx.key).forget(wait.id);
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}),
|
|
119
|
+
/** Resolve the wait `id`, or else the oldest wait for `name`, with `value`. */
|
|
120
|
+
resolve: async (ctx, { id, name, value = null, keep = false }) => {
|
|
121
|
+
if (id === undefined && name === undefined)
|
|
122
|
+
throw new restate.TerminalError('a signal needs an id or a name');
|
|
123
|
+
const waiting = (await ctx.get('waiting')) ?? [];
|
|
124
|
+
const at = waiting.findIndex((w) => (id === undefined ? w.name === name : w.id === id));
|
|
125
|
+
if (at < 0) {
|
|
126
|
+
if (keep && name !== undefined && id === undefined) {
|
|
127
|
+
const kept = (await ctx.get('kept')) ?? {};
|
|
128
|
+
ctx.set('kept', { ...kept, [name]: [...(kept[name] ?? []), value] });
|
|
129
|
+
}
|
|
130
|
+
return { resolved: null };
|
|
131
|
+
}
|
|
132
|
+
const [wait] = waiting.splice(at, 1);
|
|
133
|
+
ctx.resolveAwakeable(wait.awakeable, value);
|
|
134
|
+
ctx.set('waiting', waiting);
|
|
135
|
+
await publish(ctx);
|
|
136
|
+
return { resolved: wait.id };
|
|
137
|
+
},
|
|
138
|
+
/** The waits, oldest first. */
|
|
139
|
+
pending: restate.handlers.object.shared(async (ctx) => ((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since }))),
|
|
140
|
+
/** @internal A wait: resolved now with a signal kept for it, or else when one comes. */
|
|
141
|
+
listen: internal(async (ctx, wait) => {
|
|
142
|
+
const kept = (await ctx.get('kept')) ?? {};
|
|
143
|
+
const [value, ...rest] = kept[wait.name] ?? [];
|
|
144
|
+
if (kept[wait.name]?.length) {
|
|
145
|
+
ctx.resolveAwakeable(wait.awakeable, value);
|
|
146
|
+
ctx.set('kept', { ...kept, [wait.name]: rest });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
ctx.set('waiting', [...((await ctx.get('waiting')) ?? []), wait]);
|
|
150
|
+
await publish(ctx);
|
|
151
|
+
}),
|
|
152
|
+
/**
|
|
153
|
+
* @internal Steer the message `agent` answers, if any, with `text`:
|
|
154
|
+
* queued for it to take at its next step, or, if none is answered, sent
|
|
155
|
+
* as a message of its own.
|
|
156
|
+
*/
|
|
157
|
+
steer: internal(async (ctx, { agent, text }) => steered(ctx, agent, text)),
|
|
158
|
+
/**
|
|
159
|
+
* @internal The agent's `chat` started answering `turn`: the same
|
|
160
|
+
* invocation again (a retry) keeps when it started. A turn before it that
|
|
161
|
+
* never closed (its invocation was killed) closes now, as a cancelled one:
|
|
162
|
+
* the deliveries steered to it are answered.
|
|
163
|
+
*/
|
|
164
|
+
start: internal(async (ctx, { agent, turn }) => {
|
|
165
|
+
const before = (await ctx.get('turn'))?.invocationId;
|
|
166
|
+
if (before === turn.invocationId)
|
|
167
|
+
return;
|
|
168
|
+
if (before !== undefined)
|
|
169
|
+
await closed(ctx, { agent, turn: before, taken: [], cancelled: true });
|
|
170
|
+
ctx.set('turn', turn);
|
|
171
|
+
}),
|
|
172
|
+
/** What the agent does: the message it answers, if any, and the waits of its tools. */
|
|
173
|
+
status: restate.handlers.object.shared(async (ctx) => ({
|
|
174
|
+
answering: (await ctx.get('turn')) ?? null,
|
|
175
|
+
waiting: ((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since })),
|
|
176
|
+
})),
|
|
177
|
+
/**
|
|
178
|
+
* The revision, once there is one newer than `revision`, or at the
|
|
179
|
+
* timeout (50 s by default, at most 60 s), whichever comes first. It
|
|
180
|
+
* waits in Restate, holding no process; a caller that retries it with the
|
|
181
|
+
* same idempotency key joins the same wait.
|
|
182
|
+
*/
|
|
183
|
+
watch: restate.handlers.object.shared(async (ctx, { revision, timeoutSeconds = 50 }) => {
|
|
184
|
+
const { id, promise } = ctx.awakeable();
|
|
185
|
+
ctx.objectSendClient(signals, ctx.key).subscribe({ revision, awakeable: id });
|
|
186
|
+
const timeout = ctx.sleep({ seconds: Math.min(Math.max(timeoutSeconds, 0), 60) }).map(() => null);
|
|
187
|
+
const changed = await restate.RestatePromise.race([promise, timeout]);
|
|
188
|
+
if (changed)
|
|
189
|
+
return changed;
|
|
190
|
+
ctx.objectSendClient(signals, ctx.key).unsubscribe(id);
|
|
191
|
+
return { revision: (await ctx.get('revision')) ?? 0 };
|
|
192
|
+
}),
|
|
193
|
+
/**
|
|
194
|
+
* @internal Deliver `text` to `agent` (a webhook's delivery, say): steered
|
|
195
|
+
* to the message it answers, if any, which takes it at its next step;
|
|
196
|
+
* else, or once that turn is over, as a message of its own. Unlike a
|
|
197
|
+
* steer, a cancelled turn does not drop it: it is answered after.
|
|
198
|
+
*/
|
|
199
|
+
deliver: internal(async (ctx, { agent, text }) => steered(ctx, agent, text, true)),
|
|
200
|
+
/** @internal The messages steered to `turn`, in order. */
|
|
201
|
+
steering: restate.handlers.object.shared({ ingressPrivate: true }, async (ctx, turn) => {
|
|
202
|
+
const steering = await ctx.get('steering');
|
|
203
|
+
return steering?.turn === turn ? steering.messages : [];
|
|
204
|
+
}),
|
|
205
|
+
/**
|
|
206
|
+
* @internal `turn` is over: each message steered to it that it did not
|
|
207
|
+
* take (not in `taken`) is sent as a message of its own, unless the turn
|
|
208
|
+
* was cancelled (but for a delivery); later ones are, too.
|
|
209
|
+
*/
|
|
210
|
+
close: internal(closed),
|
|
211
|
+
/**
|
|
212
|
+
* @internal The agent forgot its conversation (`clear`): forget its
|
|
213
|
+
* waits, kept signals and steered messages, and publish the change.
|
|
214
|
+
*/
|
|
215
|
+
reset: restate.handlers.object.exclusive({ ingressPrivate: true }, async (ctx) => {
|
|
216
|
+
ctx.clear('waiting');
|
|
217
|
+
ctx.clear('kept');
|
|
218
|
+
ctx.clear('steering');
|
|
219
|
+
await publish(ctx);
|
|
220
|
+
}),
|
|
221
|
+
/** @internal Forget wait `id`, whose agent no longer waits. */
|
|
222
|
+
forget: internal(async (ctx, id) => {
|
|
223
|
+
const waiting = (await ctx.get('waiting')) ?? [];
|
|
224
|
+
ctx.set('waiting', waiting.filter((w) => w.id !== id));
|
|
225
|
+
await publish(ctx);
|
|
226
|
+
}),
|
|
227
|
+
/** @internal The conversation changed: increase its revision, and answer the `watch`es. */
|
|
228
|
+
publish: restate.handlers.object.exclusive({ ingressPrivate: true }, publish),
|
|
229
|
+
/**
|
|
230
|
+
* @internal The turn `turn` made progress (a tool call ended): publish
|
|
231
|
+
* it, and answer the messages steered to the turn.
|
|
232
|
+
*/
|
|
233
|
+
progress: internal(async (ctx, turn) => {
|
|
234
|
+
await publish(ctx);
|
|
235
|
+
const steering = await ctx.get('steering');
|
|
236
|
+
return steering?.turn === turn ? steering.messages : [];
|
|
237
|
+
}),
|
|
238
|
+
/** The conversation's revision. */
|
|
239
|
+
notifications: restate.handlers.object.shared(async (ctx) => ({
|
|
240
|
+
revision: (await ctx.get('revision')) ?? 0,
|
|
241
|
+
})),
|
|
242
|
+
/** @internal Answer `awakeable` once there is a revision newer than `revision`: at once, if there is. */
|
|
243
|
+
subscribe: internal(async (ctx, watch) => {
|
|
244
|
+
const revision = (await ctx.get('revision')) ?? 0;
|
|
245
|
+
if (revision > watch.revision) {
|
|
246
|
+
ctx.resolveAwakeable(watch.awakeable, { revision });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
ctx.set('watching', [...((await ctx.get('watching')) ?? []), watch]);
|
|
250
|
+
}),
|
|
251
|
+
/** @internal Forget the `watch` of `awakeable`, which no longer waits. */
|
|
252
|
+
unsubscribe: internal(async (ctx, awakeable) => {
|
|
253
|
+
const watching = (await ctx.get('watching')) ?? [];
|
|
254
|
+
ctx.set('watching', watching.filter((w) => w.awakeable !== awakeable));
|
|
255
|
+
}),
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
//# sourceMappingURL=signals.js.map
|
|
@@ -0,0 +1 @@
|
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C;QACvE,CAAC,CAAC;QAEF,0EAA0E;QAC1E,WAAW,EAAE,QAAQ,CAAC,KAAK,EAAE,GAAiC,EAAE,SAAiB,EAAE,EAAE;YACnF,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,EAAE,CAAC;YACnD,GAAG,CAAC,GAAG,CACL,UAAU,EACV,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC,CAClD,CAAC;QACJ,CAAC,CAAC;KACH;CACF,CAAC,CAAC"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as restate from '@restatedev/sessions';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { type Configure } from './common.ts';
|
|
4
|
+
declare const TOOL: unique symbol;
|
|
5
|
+
/** A tool call: the agent's context, and the id of the model's call. */
|
|
6
|
+
export interface ToolCall {
|
|
7
|
+
/** The agent's context: Restate calls, `ctx.key`, `ctx.db`... */
|
|
8
|
+
ctx: restate.SessionContext;
|
|
9
|
+
/** The id of the model's tool call: the same when a crash runs the call again. */
|
|
10
|
+
callId: string;
|
|
11
|
+
}
|
|
12
|
+
/** What a tool's `run` gets besides its input. */
|
|
13
|
+
export interface ToolContext extends ToolCall {
|
|
14
|
+
/**
|
|
15
|
+
* The tool call's idempotency key, `<invocation id>:<call id>`: for a call
|
|
16
|
+
* the tool makes once, however often it runs (a Restate call's
|
|
17
|
+
* `idempotencyKey`, or a payment API's).
|
|
18
|
+
*/
|
|
19
|
+
idempotencyKey: string;
|
|
20
|
+
}
|
|
21
|
+
/** A tool of any framework: see {@link tool}. */
|
|
22
|
+
export interface Tool {
|
|
23
|
+
readonly [TOOL]: true;
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly description: string;
|
|
26
|
+
/** The input: a zod object. A framework offers it to the model as its JSON schema ({@link jsonSchemaOf}). */
|
|
27
|
+
readonly input: z.ZodObject;
|
|
28
|
+
/**
|
|
29
|
+
* Its result, as text for the model, of the model's arguments as they came:
|
|
30
|
+
* `run` checks them against `input`, and answers arguments that do not fit
|
|
31
|
+
* with what is wrong, for the model to call it again.
|
|
32
|
+
*/
|
|
33
|
+
run(args: unknown, call: ToolCall): Promise<string>;
|
|
34
|
+
}
|
|
35
|
+
/** The JSON schema of `t`'s input, which a framework offers the model. */
|
|
36
|
+
export declare const jsonSchemaOf: (t: Tool) => Record<string, unknown>;
|
|
37
|
+
/** Whether `value` is a {@link Tool}, rather than a framework's own. */
|
|
38
|
+
export declare const isTool: (value: unknown) => value is Tool;
|
|
39
|
+
/**
|
|
40
|
+
* A tool, for any framework's agent:
|
|
41
|
+
*
|
|
42
|
+
* const weather = tool({
|
|
43
|
+
* name: 'get_weather',
|
|
44
|
+
* description: 'The current weather in a city.',
|
|
45
|
+
* input: z.object({ city: z.string() }),
|
|
46
|
+
* run: async ({ city }) => `Sunny in ${city}`,
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* `run` returns the result (or a promise of it), text or anything JSON; it
|
|
50
|
+
* gets the agent's `ctx`, the tool call's `callId`, and an `idempotencyKey`
|
|
51
|
+
* made of both, for what the tool must do once. A `restate.TerminalError` it throws (or
|
|
52
|
+
* a Restate call it makes fails with) is its answer, `Error: <message>`, for
|
|
53
|
+
* the model to act on; and so are arguments that do not fit `input`, which
|
|
54
|
+
* `run` never sees.
|
|
55
|
+
*/
|
|
56
|
+
export declare const tool: <S extends z.ZodObject>(options: {
|
|
57
|
+
name: string;
|
|
58
|
+
description: string;
|
|
59
|
+
input: S;
|
|
60
|
+
run: (input: z.infer<S>, context: ToolContext) => unknown;
|
|
61
|
+
}) => Tool;
|
|
62
|
+
/** A service, virtual object or workflow, as its definition (`restate.service(...)` and so on), of handlers `M`. */
|
|
63
|
+
type Target<M = unknown> = restate.ServiceDefinition<string, M> | restate.VirtualObjectDefinition<string, M> | restate.WorkflowDefinition<string, M>;
|
|
64
|
+
interface TargetOptions<S extends z.ZodObject | undefined> {
|
|
65
|
+
/** The tool's name, for the model; by default the handler's, such as `orders_status`. */
|
|
66
|
+
name?: string;
|
|
67
|
+
/** What it does, for the model. */
|
|
68
|
+
description: string;
|
|
69
|
+
/** The handler's input: a zod object, which the model fills in. Without one, the handler takes none. */
|
|
70
|
+
input?: S;
|
|
71
|
+
/**
|
|
72
|
+
* For a virtual object, its key (or the workflow's id): fixed, or for an
|
|
73
|
+
* invocation. Without one, the model chooses it.
|
|
74
|
+
*/
|
|
75
|
+
key?: Configure<string>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A tool that calls `handler` of `target` and answers with its result:
|
|
79
|
+
*
|
|
80
|
+
* call(orders, 'status', {
|
|
81
|
+
* description: 'The status of an order.',
|
|
82
|
+
* input: z.object({ order: z.string() }),
|
|
83
|
+
* })
|
|
84
|
+
*
|
|
85
|
+
* Of a virtual object, the model chooses the key, unless `key` gives it.
|
|
86
|
+
*/
|
|
87
|
+
export declare const call: <M, S extends z.ZodObject | undefined = undefined>(target: Target<M>, handler: keyof M & string, options: TargetOptions<S>) => Tool;
|
|
88
|
+
/**
|
|
89
|
+
* A tool that sends to `handler` of `target`, without waiting for it: a
|
|
90
|
+
* one-way call, made once, after `delay` if given (such as `{ minutes: 5 }`).
|
|
91
|
+
*/
|
|
92
|
+
export declare const send: <M, S extends z.ZodObject | undefined = undefined>(target: Target<M>, handler: keyof M & string, options: TargetOptions<S> & {
|
|
93
|
+
delay?: restate.Duration;
|
|
94
|
+
}) => Tool;
|
|
95
|
+
/**
|
|
96
|
+
* A tool that runs the workflow `target` (its `run` handler) to the end, and
|
|
97
|
+
* answers with its result. Each tool call is a workflow of its own, with an
|
|
98
|
+
* id made of the agent's key and the tool call's id, unless `key` gives one.
|
|
99
|
+
*/
|
|
100
|
+
export declare const workflow: <S extends z.ZodObject | undefined = undefined>(target: restate.WorkflowDefinition<string, {
|
|
101
|
+
run: unknown;
|
|
102
|
+
}>, options: TargetOptions<S>) => Tool;
|
|
103
|
+
/** An object with a `chat` handler; another object needs subagent's `handler`. */
|
|
104
|
+
type WithChat<M> = M extends {
|
|
105
|
+
chat: unknown;
|
|
106
|
+
} ? unknown : {
|
|
107
|
+
'an agent with a chat handler, or a handler option': never;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* A tool, named after `agent` (an agent, or any virtual object) unless `name` names it, that hands
|
|
111
|
+
* a task to it: to the object keyed `<this key>.<task name>`, through its
|
|
112
|
+
* `handler` (`chat`), and answers with its answer. Tasks of different names
|
|
113
|
+
* run at once; the same name continues the same task. The model reads what
|
|
114
|
+
* the agent is from its `description` (or `description` here).
|
|
115
|
+
*
|
|
116
|
+
* const coder = pi.agent({ name: 'coder', description: 'Builds and tests code in Python' });
|
|
117
|
+
* const lead = pi.agent({ name: 'lead', tools: [subagent(coder)] });
|
|
118
|
+
*/
|
|
119
|
+
export declare function subagent<M>(agent: restate.VirtualObjectDefinition<string, M> & WithChat<M>, options?: {
|
|
120
|
+
description?: string;
|
|
121
|
+
name?: string;
|
|
122
|
+
}): Tool;
|
|
123
|
+
/** A sub-agent through another of its handlers. */
|
|
124
|
+
export declare function subagent<M>(agent: restate.VirtualObjectDefinition<string, M>, options: {
|
|
125
|
+
description?: string;
|
|
126
|
+
name?: string;
|
|
127
|
+
handler: keyof M & string;
|
|
128
|
+
}): Tool;
|
|
129
|
+
/**
|
|
130
|
+
* A tool that waits for the signal `name` to the agent, and answers with its
|
|
131
|
+
* value: a person's approval, say, or another system's result. The agent
|
|
132
|
+
* waits without holding a process. With an `input`, the model says what it
|
|
133
|
+
* waits for (such as the refund to approve), which `pending` shows.
|
|
134
|
+
*
|
|
135
|
+
* signal('approval', {
|
|
136
|
+
* description: 'Wait for a manager to approve a refund.',
|
|
137
|
+
* input: z.object({ order: z.string(), amount: z.number() }),
|
|
138
|
+
* })
|
|
139
|
+
*
|
|
140
|
+
* Each wait has an id: `pending` (each `sessionObject()` has it) lists them, and
|
|
141
|
+
* the agent's `signal` handler resolves one, by its id, or the oldest wait
|
|
142
|
+
* for a name:
|
|
143
|
+
*
|
|
144
|
+
* curl localhost:8080/support/alice/pending
|
|
145
|
+
* curl localhost:8080/support/alice/signal --json '{"id": "inv_1f…:toolu_01", "value": "approved"}'
|
|
146
|
+
* curl localhost:8080/support/alice/signal --json '{"name": "approval", "value": "approved"}'
|
|
147
|
+
*
|
|
148
|
+
* It answers with the id of the wait it resolved, or `null` if none waited:
|
|
149
|
+
* the signal is dropped then, unless it has `"keep": true`, which keeps it
|
|
150
|
+
* for the next wait for its name.
|
|
151
|
+
*/
|
|
152
|
+
export declare const signal: (name: string, options: {
|
|
153
|
+
description: string;
|
|
154
|
+
input?: z.ZodObject;
|
|
155
|
+
name?: string;
|
|
156
|
+
}) => Tool;
|
|
157
|
+
export {};
|
|
158
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAgB,KAAK,SAAS,EAAyB,MAAM,aAAa,CAAC;AAIlF,QAAA,MAAM,IAAI,eAAoC,CAAC;AAE/C,wEAAwE;AACxE,MAAM,WAAW,QAAQ;IACvB,iEAAiE;IACjE,GAAG,EAAE,OAAO,CAAC,cAAc,CAAC;IAC5B,kFAAkF;IAClF,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,kDAAkD;AAClD,MAAM,WAAW,WAAY,SAAQ,QAAQ;IAC3C;;;;OAIG;IACH,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,iDAAiD;AACjD,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC;IACtB,QAAQ,CAAC,IAAI,EA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