@restatedev/agents 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +688 -2
- package/dist/calls.d.ts +29 -0
- package/dist/calls.d.ts.map +1 -0
- package/dist/calls.js +91 -0
- package/dist/calls.js.map +1 -0
- package/dist/claude/conversation.d.ts +33 -0
- package/dist/claude/conversation.d.ts.map +1 -0
- package/dist/claude/conversation.js +95 -0
- package/dist/claude/conversation.js.map +1 -0
- package/dist/claude/index.d.ts +78 -0
- package/dist/claude/index.d.ts.map +1 -0
- package/dist/claude/index.js +269 -0
- package/dist/claude/index.js.map +1 -0
- package/dist/client.d.ts +83 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/common.d.ts +284 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +475 -0
- package/dist/common.js.map +1 -0
- package/dist/deepseek/index.d.ts +69 -0
- package/dist/deepseek/index.d.ts.map +1 -0
- package/dist/deepseek/index.js +381 -0
- package/dist/deepseek/index.js.map +1 -0
- package/dist/deepseek/persistence.d.ts +61 -0
- package/dist/deepseek/persistence.d.ts.map +1 -0
- package/dist/deepseek/persistence.js +224 -0
- package/dist/deepseek/persistence.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/langgraph/checkpointer.d.ts +29 -0
- package/dist/langgraph/checkpointer.d.ts.map +1 -0
- package/dist/langgraph/checkpointer.js +275 -0
- package/dist/langgraph/checkpointer.js.map +1 -0
- package/dist/langgraph/index.d.ts +67 -0
- package/dist/langgraph/index.d.ts.map +1 -0
- package/dist/langgraph/index.js +265 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/log.d.ts +29 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +145 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +181 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +232 -0
- package/dist/object.js.map +1 -0
- package/dist/opencode/db.d.ts +40 -0
- package/dist/opencode/db.d.ts.map +1 -0
- package/dist/opencode/db.js +161 -0
- package/dist/opencode/db.js.map +1 -0
- package/dist/opencode/index.d.ts +101 -0
- package/dist/opencode/index.d.ts.map +1 -0
- package/dist/opencode/index.js +464 -0
- package/dist/opencode/index.js.map +1 -0
- package/dist/output.d.ts +17 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +81 -0
- package/dist/output.js.map +1 -0
- package/dist/pi/index.d.ts +90 -0
- package/dist/pi/index.d.ts.map +1 -0
- package/dist/pi/index.js +313 -0
- package/dist/pi/index.js.map +1 -0
- package/dist/pi/storage.d.ts +5 -0
- package/dist/pi/storage.d.ts.map +1 -0
- package/dist/pi/storage.js +41 -0
- package/dist/pi/storage.js.map +1 -0
- package/dist/signals.d.ts +195 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +258 -0
- package/dist/signals.js.map +1 -0
- package/dist/tools.d.ts +158 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +211 -0
- package/dist/tools.js.map +1 -0
- package/dist/webhook.d.ts +73 -0
- package/dist/webhook.d.ts.map +1 -0
- package/dist/webhook.js +104 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +218 -5
- package/src/calls.ts +138 -0
- package/src/claude/README.md +84 -0
- package/src/claude/conversation.ts +114 -0
- package/src/claude/index.ts +364 -0
- package/src/client.ts +179 -0
- package/src/common.ts +593 -0
- package/src/deepseek/README.md +110 -0
- package/src/deepseek/index.ts +487 -0
- package/src/deepseek/persistence.ts +293 -0
- package/src/index.ts +60 -0
- package/src/langgraph/README.md +87 -0
- package/src/langgraph/checkpointer.ts +364 -0
- package/src/langgraph/index.ts +349 -0
- package/src/log.ts +193 -0
- package/src/object.ts +343 -0
- package/src/opencode/README.md +125 -0
- package/src/opencode/db.ts +223 -0
- package/src/opencode/index.ts +595 -0
- package/src/output.ts +95 -0
- package/src/pi/README.md +105 -0
- package/src/pi/index.ts +402 -0
- package/src/pi/storage.ts +65 -0
- package/src/signals.ts +426 -0
- package/src/tools.ts +343 -0
- package/src/webhook.ts +163 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as restate from '@restatedev/sessions';
|
|
2
|
+
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
3
|
+
import { type StructuredToolInterface } from '@langchain/core/tools';
|
|
4
|
+
import { type Configure } from '../common.ts';
|
|
5
|
+
import { type Answer } from '../output.ts';
|
|
6
|
+
import type { z } from 'zod';
|
|
7
|
+
import { type Definition } from '../object.ts';
|
|
8
|
+
import { type Tool as AnyTool } from '../tools.ts';
|
|
9
|
+
import { SqlCheckpointSaver } from './checkpointer.ts';
|
|
10
|
+
export { SqlCheckpointSaver };
|
|
11
|
+
/** An agent's options: `chat` and `prompt` take them. */
|
|
12
|
+
export interface Options<O extends z.ZodObject | undefined = z.ZodObject | undefined> {
|
|
13
|
+
/** The model: `openai/model-id` or `anthropic/model-id` (MODEL, or else openai/gpt-6-sol), or a chat model that calls tools. */
|
|
14
|
+
model?: string | BaseChatModel;
|
|
15
|
+
/** The system prompt: who the agent is, and how it answers. */
|
|
16
|
+
system?: string;
|
|
17
|
+
/** The agent's tools: `tool()`s, Restate's (`call`, `signal`...), or LangChain's. */
|
|
18
|
+
tools?: (AnyTool | StructuredToolInterface)[];
|
|
19
|
+
/**
|
|
20
|
+
* The answer as data: a zod object, whose JSON the agent replies with
|
|
21
|
+
* (checked), and `chat` and `prompt` answer with (see output.ts).
|
|
22
|
+
*/
|
|
23
|
+
output?: O;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
|
|
27
|
+
* of an invocation is a prompt of its own. The thread is the object's key.
|
|
28
|
+
* With `output`, it answers with the value the agent replies with (see
|
|
29
|
+
* output.ts), not its text.
|
|
30
|
+
*/
|
|
31
|
+
export declare const prompt: <O extends z.ZodObject | undefined = undefined>(ctx: restate.SessionContext, text: string, options?: Options<O>) => Promise<Answer<O>>;
|
|
32
|
+
/**
|
|
33
|
+
* A chat handler: each message an invocation, the thread (the object's key)
|
|
34
|
+
* in the object's database, with `options`, or the options a function of the
|
|
35
|
+
* invocation's context gives. It takes the message as plain text
|
|
36
|
+
* (`curl -d 'Hi'`) or as a JSON string (`curl --json '"Hi"'`).
|
|
37
|
+
*/
|
|
38
|
+
export declare const chat: <O extends z.ZodObject | undefined = undefined>(options?: Configure<Options<O>>) => {
|
|
39
|
+
input: restate.Serde<string>;
|
|
40
|
+
accept: string;
|
|
41
|
+
explicitCancellation: boolean;
|
|
42
|
+
handler: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* A handler of the conversation as clients read it: a page at a time, or
|
|
46
|
+
* what is new or changed since a cursor, from the log each change writes
|
|
47
|
+
* (log.ts). It answers while a message is answered.
|
|
48
|
+
*/
|
|
49
|
+
export declare const messages: () => (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
|
|
50
|
+
/** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
|
|
51
|
+
export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
|
|
52
|
+
signal: (ctx: restate.ViewContext, signal: import("../signals.ts").Signal) => Promise<import("../signals.ts").Resolved>;
|
|
53
|
+
pending: (ctx: restate.ViewContext) => Promise<import("../signals.ts").Waiting[]>;
|
|
54
|
+
status: (ctx: restate.ViewContext) => Promise<import("../object.ts").Status>;
|
|
55
|
+
stop: (ctx: restate.ViewContext) => Promise<import("../object.ts").Stopped>;
|
|
56
|
+
steer: (ctx: restate.ViewContext, text: string) => Promise<import("../signals.ts").Steered>;
|
|
57
|
+
notifications: (ctx: restate.ViewContext) => Promise<import("../signals.ts").Notifications>;
|
|
58
|
+
watch: (ctx: restate.ViewContext, watch: import("../signals.ts").Watch) => Promise<import("../signals.ts").Notifications>;
|
|
59
|
+
} & {
|
|
60
|
+
clear: (ctx: restate.ViewContext) => Promise<import("../object.ts").Stopped>;
|
|
61
|
+
sync: (ctx: restate.ViewContext, { cursor, revision, timeoutSeconds, limit }?: import("../object.ts").SyncRequest) => Promise<import("../object.ts").Synced>;
|
|
62
|
+
} & {
|
|
63
|
+
chat: (ctx: restate.SessionContext, text: string) => Promise<Answer<O>>;
|
|
64
|
+
} & {
|
|
65
|
+
messages: (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
|
|
66
|
+
}>;
|
|
67
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/langgraph/index.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAKhD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAEjF,OAAO,EAAE,KAAK,uBAAuB,EAAmC,MAAM,uBAAuB,CAAC;AAGtG,OAAO,EAIL,KAAK,SAAS,EAgBf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,KAAK,MAAM,EAAc,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,EAAE,KAAK,UAAU,EAAiB,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAwB,KAAK,IAAI,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC;AAEzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,kBAAkB,EAAE,CAAC;AAwH9B,yDAAyD;AACzD,MAAM,WAAW,OAAO,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,SAAS,GAAG,CAAC,CAAC,SAAS,GAAG,SAAS;IAClF,gIAAgI;IAChI,KAAK,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IAC/B,+DAA+D;IAC/D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qFAAqF;IACrF,KAAK,CAAC,EAAE,CAAC,OAAO,GAAG,uBAAuB,CAAC,EAAE,CAAC;IAC9C;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC;CACZ;AA0FD;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,SAAS,GAAG,SAAS,OAC7D,OAAO,CAAC,cAAc,QACrB,MAAM,YACH,OAAO,CAAC,CAAC,CAAC,KAClB,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAqF,CAAC;AAE1G;;;;;GAKG;AACH,eAAO,MAAM,IAAI,GAAI,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,SAAS,GAAG,SAAS,YAAW,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;;;;;CAGjG,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,QAAQ,gIAA0B,CAAC;AAiBhD,6HAA6H;AAC7H,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,CAAC,CAAC,SAAS,GAAG,SAAS,GAAG,SAAS,yDAMlF,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;;;;;;;;;;;;;;;EAQzB,CAAC"}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// LangGraph agents in Restate virtual objects: a tool-calling graph whose
|
|
2
|
+
// checkpoints are in the object's database (checkpointer.ts).
|
|
3
|
+
//
|
|
4
|
+
// const assistant = langgraph.agent({ name: 'assistant', tools: [search] });
|
|
5
|
+
//
|
|
6
|
+
// or a handler of an object of your own: `handlers: { chat: langgraph.chat() }`,
|
|
7
|
+
// with options for each invocation: `langgraph.chat(async (ctx) => ({ system }))`,
|
|
8
|
+
// or a prompt of your own handler: `langgraph.prompt(ctx, text, options)`
|
|
9
|
+
//
|
|
10
|
+
// The graph is the usual tool-calling loop: a model node, a tools node, and
|
|
11
|
+
// back to the model until it answers. The model is `model` (a chat model,
|
|
12
|
+
// `openai/model-id` or `anthropic/model-id`), or else MODEL, or else
|
|
13
|
+
// openai/gpt-6-sol, with OPENAI_API_KEY.
|
|
14
|
+
//
|
|
15
|
+
// The handler is not replayed: a retried attempt (a crash, a lost connection)
|
|
16
|
+
// runs again from the start, and LangGraph carries on from the last step it
|
|
17
|
+
// stored:
|
|
18
|
+
// - Each step is stored before the next starts (`durability: 'sync'`), in
|
|
19
|
+
// order with everything else the handler does. So the tool calls a model
|
|
20
|
+
// made are stored before any tool runs, and a crash during the tools
|
|
21
|
+
// resumes them with the same tool call ids, which a tool passes on to stay
|
|
22
|
+
// idempotent.
|
|
23
|
+
// - A tool's error fails the attempt, which Restate retries (the ToolNode
|
|
24
|
+
// would hand it to the model as the tool's result); a call of a tool the
|
|
25
|
+
// agent lacks, or with arguments that do not fit, is the model's to fix.
|
|
26
|
+
// - A message is answered once per invocation: the graph records each answer
|
|
27
|
+
// in its state, and a retried attempt returns it.
|
|
28
|
+
import * as restate from '@restatedev/sessions';
|
|
29
|
+
import { AIMessage, HumanMessage, SystemMessage, ToolMessage } from '@langchain/core/messages';
|
|
30
|
+
import { tool, ToolInputParsingException } from '@langchain/core/tools';
|
|
31
|
+
import { Annotation, END, isGraphInterrupt, MessagesAnnotation, START, StateGraph } from '@langchain/langgraph';
|
|
32
|
+
import { ToolNode, toolsCondition } from '@langchain/langgraph/prebuilt';
|
|
33
|
+
import { AttemptEnded, CANCELLED, chatHandler, configured, fromTool, messagesHandler, ModelError, modelOf, nextPromptId, providerOf, refusal, stopSignalOf, storedAnswer, toolCallEnded, toolCallStarted, transcribing, Transcript, UNANSWERED, } from '../common.js';
|
|
34
|
+
import { withOutput } from '../output.js';
|
|
35
|
+
import { sessionObject } from '../object.js';
|
|
36
|
+
import { isTool, jsonSchemaOf } from '../tools.js';
|
|
37
|
+
import { SqlCheckpointSaver } from './checkpointer.js';
|
|
38
|
+
export { SqlCheckpointSaver };
|
|
39
|
+
const State = Annotation.Root({
|
|
40
|
+
...MessagesAnnotation.spec,
|
|
41
|
+
/** The id of the prompt being answered. */
|
|
42
|
+
prompt: Annotation(),
|
|
43
|
+
/** The answer to each prompt, by id. */
|
|
44
|
+
answers: Annotation({ reducer: (a, b) => ({ ...a, ...b }), default: () => ({}) }),
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* The tools node: each tool call of the model's last message run by a
|
|
48
|
+
* ToolNode, which answers the model with what is wrong when a call names a
|
|
49
|
+
* tool the agent does not have, or arguments that do not fit (the model's to
|
|
50
|
+
* fix: failing the attempt, every retry would meet the same call again). Any
|
|
51
|
+
* other error of a tool fails the attempt, at once (the ToolNode would hand
|
|
52
|
+
* it to the model too). The round is published as it starts and ends, for
|
|
53
|
+
* the clients that follow the conversation.
|
|
54
|
+
*/
|
|
55
|
+
function toolsNode(ctx, tools) {
|
|
56
|
+
return async (state, config) => {
|
|
57
|
+
await toolCallStarted(ctx);
|
|
58
|
+
// The first error fails the attempt at once, not once the other calls end.
|
|
59
|
+
const { promise: failure, reject: failed } = Promise.withResolvers();
|
|
60
|
+
failure.catch(() => { });
|
|
61
|
+
const node = new ToolNode(tools.map((t) => failing(t, failed)), { handleToolErrors: true });
|
|
62
|
+
const ran = await Promise.race([node.invoke(state, config), failure]);
|
|
63
|
+
await toolCallEnded(ctx);
|
|
64
|
+
return ran;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** `t`, its errors given to `failed`, but for its input's (arguments that do not fit) and LangGraph's interrupts. */
|
|
68
|
+
const failing = (t, failed) => Object.create(t, {
|
|
69
|
+
invoke: {
|
|
70
|
+
value: async (...args) => {
|
|
71
|
+
try {
|
|
72
|
+
return await t.invoke(...args);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (!(error instanceof ToolInputParsingException) && !isGraphInterrupt(error)) {
|
|
76
|
+
fromTool(error);
|
|
77
|
+
failed(error);
|
|
78
|
+
}
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
/** `provider/model-id` as its provider and model id, for a provider the usual graph takes. */
|
|
85
|
+
const providerOfModel = (named) => {
|
|
86
|
+
const [provider, id] = providerOf(named);
|
|
87
|
+
if ((provider !== 'openai' && provider !== 'anthropic') || !id) {
|
|
88
|
+
const from = process.env.MODEL === named ? ' (from MODEL)' : '';
|
|
89
|
+
throw new ModelError(`the model "${named}"${from} is neither openai/<model-id> nor anthropic/<model-id>`);
|
|
90
|
+
}
|
|
91
|
+
return [provider, id];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* The chat model `provider/model-id` names: OpenAI's or Anthropic's, through
|
|
95
|
+
* LangChain's integration of the provider, loaded once, when a model of it is
|
|
96
|
+
* first used: only that one need be installed.
|
|
97
|
+
*/
|
|
98
|
+
const chatModel = async (named) => {
|
|
99
|
+
const [provider, id] = providerOfModel(named);
|
|
100
|
+
if (provider === 'openai') {
|
|
101
|
+
openai ??= integration('@langchain/openai', () => import('@langchain/openai'));
|
|
102
|
+
return new (await openai).ChatOpenAI({ model: id });
|
|
103
|
+
}
|
|
104
|
+
anthropic ??= integration('@langchain/anthropic', () => import('@langchain/anthropic'));
|
|
105
|
+
return new (await anthropic).ChatAnthropic({ model: id });
|
|
106
|
+
};
|
|
107
|
+
let openai;
|
|
108
|
+
let anthropic;
|
|
109
|
+
/** The integration `name`, which its user installs beside @restatedev/agents. */
|
|
110
|
+
async function integration(name, load) {
|
|
111
|
+
try {
|
|
112
|
+
return await load();
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error.code !== 'ERR_MODULE_NOT_FOUND')
|
|
116
|
+
throw error;
|
|
117
|
+
throw new ModelError(`${name.replace('@langchain/', '')}/<model-id> models need ${name}: install it beside @restatedev/agents`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** The agent's graph: `model`, calling `tools`. */
|
|
121
|
+
function graph(ctx, model, tools, system) {
|
|
122
|
+
if (!model.bindTools)
|
|
123
|
+
throw new Error(`${model.getName()} does not call tools`);
|
|
124
|
+
const withTools = model.bindTools(tools);
|
|
125
|
+
return new StateGraph(State)
|
|
126
|
+
.addNode('model', async ({ messages }) => ({
|
|
127
|
+
messages: [await withTools.invoke(system ? [new SystemMessage(system), ...messages] : messages)],
|
|
128
|
+
}))
|
|
129
|
+
.addNode('tools', toolsNode(ctx, tools))
|
|
130
|
+
.addNode('answer', ({ prompt, messages }) => ({ answers: { [prompt]: messages.at(-1).text } }))
|
|
131
|
+
.addEdge(START, 'model')
|
|
132
|
+
.addConditionalEdges('model', toolsCondition, { tools: 'tools', [END]: 'answer' })
|
|
133
|
+
.addEdge('tools', 'model')
|
|
134
|
+
.addEdge('answer', END);
|
|
135
|
+
}
|
|
136
|
+
/** `t` as a LangChain tool: `t.run` checks the model's arguments itself. */
|
|
137
|
+
const native = (t, ctx) => tool(async (args, { toolCall }) => {
|
|
138
|
+
if (!toolCall?.id)
|
|
139
|
+
throw new Error('a tool call without an id');
|
|
140
|
+
return t.run(args, { ctx, callId: toolCall.id });
|
|
141
|
+
},
|
|
142
|
+
// What does not fit, for the model to fix: not only that something does not.
|
|
143
|
+
{ name: t.name, description: t.description, schema: jsonSchemaOf(t), verboseParsingErrors: true });
|
|
144
|
+
/** {@link prompt}, answering with the agent's text. */
|
|
145
|
+
const promptText = (ctx, text, options) => transcribing(ctx, () => transcribe(ctx.db, ctx.key), () => answerOnce(ctx, text, options));
|
|
146
|
+
const answerOnce = async (ctx, text, { model, system, tools = [] }) => {
|
|
147
|
+
const llm = typeof model === 'object' ? model : await chatModel(modelOf(model));
|
|
148
|
+
// The latest checkpoint is all an agent resumes from: earlier ones would
|
|
149
|
+
// store the conversation again at each step.
|
|
150
|
+
const checkpointer = new SqlCheckpointSaver(ctx.db, { history: 'latest' });
|
|
151
|
+
const app = graph(ctx, llm, tools.map((t) => (isTool(t) ? native(t, ctx) : t)), system).compile({ checkpointer });
|
|
152
|
+
const config = { configurable: { thread_id: ctx.key }, durability: 'sync' };
|
|
153
|
+
const id = nextPromptId(ctx);
|
|
154
|
+
for (;;) {
|
|
155
|
+
const state = await app.getState(config);
|
|
156
|
+
const answer = state.values.answers?.[id];
|
|
157
|
+
if (answer !== undefined)
|
|
158
|
+
return storedAnswer(answer);
|
|
159
|
+
// Finish an interrupted run first (this prompt's, after a crash), then
|
|
160
|
+
// start this prompt's. A run is unfinished while its prompt has no
|
|
161
|
+
// answer: `next` does not say, as it leaves out the steps whose writes
|
|
162
|
+
// are stored.
|
|
163
|
+
const { prompt: last, answers = {} } = state.values;
|
|
164
|
+
const interrupted = last !== undefined && answers[last] === undefined;
|
|
165
|
+
const turn = stopSignalOf(ctx);
|
|
166
|
+
try {
|
|
167
|
+
await app.invoke(interrupted ? null : { messages: [new HumanMessage(text)], prompt: id }, {
|
|
168
|
+
...config,
|
|
169
|
+
signal: turn,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
// The attempt ended: it stops, and the next attempt goes on with the run.
|
|
174
|
+
if (turn?.reason instanceof AttemptEnded)
|
|
175
|
+
throw turn.reason;
|
|
176
|
+
// Cancelled: the run ends, and the next prompt starts afresh.
|
|
177
|
+
if (turn?.aborted || error instanceof restate.CancelledError) {
|
|
178
|
+
await close(app, config, CANCELLED);
|
|
179
|
+
throw turn?.aborted ? turn.reason : error;
|
|
180
|
+
}
|
|
181
|
+
// Refused for good (a wrong API key): the run ends unanswered, and
|
|
182
|
+
// fails as terminal. An earlier prompt's, this one goes on.
|
|
183
|
+
const refused = refusal(error);
|
|
184
|
+
if (!refused)
|
|
185
|
+
throw error;
|
|
186
|
+
await close(app, config, UNANSWERED);
|
|
187
|
+
if (!interrupted || last === id)
|
|
188
|
+
throw refused;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* End the run of `app` that was cancelled or refused, as its `answer` node: a
|
|
194
|
+
* result of `text` for each tool call of the last reply that has none, then
|
|
195
|
+
* `text` as the reply and the prompt's answer. A run that stored no input
|
|
196
|
+
* yet (cancelled at once) has nothing to end: the prompt the state has is
|
|
197
|
+
* an earlier one, which has its answer.
|
|
198
|
+
*/
|
|
199
|
+
async function close(app, config, text) {
|
|
200
|
+
const { values } = await app.getState(config);
|
|
201
|
+
if (values.prompt === undefined || values.answers?.[values.prompt] !== undefined)
|
|
202
|
+
return;
|
|
203
|
+
const messages = (values.messages ?? []);
|
|
204
|
+
const answered = new Set(messages.filter((m) => ToolMessage.isInstance(m)).map((m) => m.tool_call_id));
|
|
205
|
+
const last = messages.findLast((m) => AIMessage.isInstance(m));
|
|
206
|
+
const calls = (last && AIMessage.isInstance(last) ? (last.tool_calls ?? []) : []).filter((call) => call.id !== undefined && !answered.has(call.id));
|
|
207
|
+
await app.updateState(config, {
|
|
208
|
+
messages: [
|
|
209
|
+
...calls.map((call) => new ToolMessage({ tool_call_id: call.id, content: text, status: 'error' })),
|
|
210
|
+
new AIMessage(text),
|
|
211
|
+
],
|
|
212
|
+
answers: { [values.prompt]: text },
|
|
213
|
+
}, 'answer');
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
|
|
217
|
+
* of an invocation is a prompt of its own. The thread is the object's key.
|
|
218
|
+
* With `output`, it answers with the value the agent replies with (see
|
|
219
|
+
* output.ts), not its text.
|
|
220
|
+
*/
|
|
221
|
+
export const prompt = (ctx, text, options = {}) => withOutput(text, options, (t, o) => promptText(ctx, t, o));
|
|
222
|
+
/**
|
|
223
|
+
* A chat handler: each message an invocation, the thread (the object's key)
|
|
224
|
+
* in the object's database, with `options`, or the options a function of the
|
|
225
|
+
* invocation's context gives. It takes the message as plain text
|
|
226
|
+
* (`curl -d 'Hi'`) or as a JSON string (`curl --json '"Hi"'`).
|
|
227
|
+
*/
|
|
228
|
+
export const chat = (options = {}) => {
|
|
229
|
+
if (typeof options === 'object' && typeof options.model !== 'object')
|
|
230
|
+
providerOfModel(modelOf(options.model));
|
|
231
|
+
return chatHandler(async (ctx, message) => prompt(ctx, message, await configured(options, ctx)));
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* A handler of the conversation as clients read it: a page at a time, or
|
|
235
|
+
* what is new or changed since a cursor, from the log each change writes
|
|
236
|
+
* (log.ts). It answers while a message is answered.
|
|
237
|
+
*/
|
|
238
|
+
export const messages = () => messagesHandler();
|
|
239
|
+
/** The thread `key` in `db`, from its last checkpoint, as `messages` gives it. */
|
|
240
|
+
const transcribe = async (db, key) => {
|
|
241
|
+
const transcript = new Transcript();
|
|
242
|
+
const saved = await new SqlCheckpointSaver(db).getTuple({ configurable: { thread_id: key } });
|
|
243
|
+
for (const m of (saved?.checkpoint.channel_values.messages ?? [])) {
|
|
244
|
+
if (HumanMessage.isInstance(m))
|
|
245
|
+
transcript.say('user', m.text);
|
|
246
|
+
if (AIMessage.isInstance(m)) {
|
|
247
|
+
transcript.say('assistant', m.text);
|
|
248
|
+
for (const call of m.tool_calls ?? [])
|
|
249
|
+
transcript.call(call.id ?? '', call.name, call.args);
|
|
250
|
+
}
|
|
251
|
+
if (ToolMessage.isInstance(m))
|
|
252
|
+
transcript.result(m.tool_call_id, m.text, m.status === 'error');
|
|
253
|
+
}
|
|
254
|
+
return transcript;
|
|
255
|
+
};
|
|
256
|
+
/** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
|
|
257
|
+
export const agent = ({ name, description, schema, background, ...options }) => sessionObject({
|
|
258
|
+
name,
|
|
259
|
+
description,
|
|
260
|
+
schema,
|
|
261
|
+
background,
|
|
262
|
+
handlers: { chat: chat(options) },
|
|
263
|
+
views: { messages: messages() },
|
|
264
|
+
});
|
|
265
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/langgraph/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,8DAA8D;AAC9D,EAAE;AACF,+EAA+E;AAC/E,EAAE;AACF,iFAAiF;AACjF,mFAAmF;AACnF,0EAA0E;AAC1E,EAAE;AACF,4EAA4E;AAC5E,0EAA0E;AAC1E,qEAAqE;AACrE,yCAAyC;AACzC,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,UAAU;AACV,0EAA0E;AAC1E,2EAA2E;AAC3E,uEAAuE;AACvE,6EAA6E;AAC7E,gBAAgB;AAChB,0EAA0E;AAC1E,2EAA2E;AAC3E,2EAA2E;AAC3E,6EAA6E;AAC7E,oDAAoD;AACpD,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAMhD,OAAO,EAAE,SAAS,EAAoB,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACjH,OAAO,EAAgC,IAAI,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AACtG,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAChH,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,EACL,YAAY,EACZ,SAAS,EACT,WAAW,EAEX,UAAU,EACV,QAAQ,EACR,eAAe,EACf,UAAU,EACV,OAAO,EACP,YAAY,EACZ,UAAU,EACV,OAAO,EACP,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,eAAe,EACf,YAAY,EACZ,UAAU,EACV,UAAU,GACX,MAAM,cAAc,CAAC;AACtB,OAAO,EAAe,UAAU,EAAE,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C,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,KAAK,OAAO,CAAC,CAAC;IACjG,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AAEF,6HAA6H;AAC7H,MAAM,CAAC,MAAM,KAAK,GAAG,CAAkE,EACrF,IAAI,EACJ,WAAW,EACX,MAAM,EACN,UAAU,EACV,GAAG,OAAO,EACiB,EAAE,EAAE,CAC/B,aAAa,CAAC;IACZ,IAAI;IACJ,WAAW;IACX,MAAM;IACN,UAAU;IACV,QAAQ,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE;IACjC,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE;CAChC,CAAC,CAAC"}
|
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Database, DatabaseReader, DatabaseWriter } from '@restatedev/sessions';
|
|
2
|
+
import type { ChatMessage, Messages, MessagesRequest } from './common.ts';
|
|
3
|
+
/** Where the log starts: past the ids and cursors of the conversations `clear` forgot. */
|
|
4
|
+
export interface Start {
|
|
5
|
+
/** The cursor it starts at. */
|
|
6
|
+
seq: number;
|
|
7
|
+
/** Its first message's id. */
|
|
8
|
+
id: number;
|
|
9
|
+
}
|
|
10
|
+
/** A message as the transcript gives it: its role and parts, without an id. */
|
|
11
|
+
export type Said = Omit<ChatMessage, 'id'>;
|
|
12
|
+
/**
|
|
13
|
+
* Start the log at `start`, in the transaction `tx` of `clear` that emptied
|
|
14
|
+
* the database: the next conversation's ids and cursors go on from there.
|
|
15
|
+
*/
|
|
16
|
+
export declare function restart(tx: DatabaseWriter, start: Start): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Store the transcript's `messages`, in order, in the log: those that
|
|
19
|
+
* changed since the last time, in one transaction.
|
|
20
|
+
*/
|
|
21
|
+
export declare function sync(db: Database, messages: Said[]): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* A page of the log. With `cursor`: the messages new or changed since it,
|
|
24
|
+
* by when they changed, and the cursor to read on from. Without: the latest
|
|
25
|
+
* `limit` messages (or those before `before`, an id), oldest first, and the
|
|
26
|
+
* cursor to follow the conversation from.
|
|
27
|
+
*/
|
|
28
|
+
export declare function page(db: DatabaseReader, { limit, before, cursor }: MessagesRequest): Promise<Messages>;
|
|
29
|
+
//# sourceMappingURL=log.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAErF,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAI1E,0FAA0F;AAC1F,MAAM,WAAW,KAAK;IACpB,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,8BAA8B;IAC9B,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,+EAA+E;AAC/E,MAAM,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;AAqB3C;;;GAGG;AACH,wBAAsB,OAAO,CAAC,EAAE,EAAE,cAAc,EAAE,KAAK,EAAE,KAAK,iBAE7D;AAsCD;;;GAGG;AACH,wBAAsB,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,iBAwCxD;AAED;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,EAAE,EAAE,cAAc,EAAE,EAAE,KAAU,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8BjH"}
|
package/dist/log.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Schema } from '@restatedev/sessions/internal';
|
|
2
|
+
const TABLE = 'restate_agent_log';
|
|
3
|
+
const SCHEMA = new Schema('restate-agents.log', [
|
|
4
|
+
`CREATE TABLE ${TABLE} (
|
|
5
|
+
id INTEGER PRIMARY KEY,
|
|
6
|
+
seq INTEGER NOT NULL UNIQUE,
|
|
7
|
+
position INTEGER UNIQUE,
|
|
8
|
+
message TEXT NOT NULL
|
|
9
|
+
)`,
|
|
10
|
+
// Where the log starts, which `clear` sets: no row before.
|
|
11
|
+
`CREATE TABLE ${TABLE}_start (
|
|
12
|
+
row INTEGER PRIMARY KEY CHECK (row = 0),
|
|
13
|
+
seq INTEGER NOT NULL,
|
|
14
|
+
id INTEGER NOT NULL
|
|
15
|
+
)`,
|
|
16
|
+
]);
|
|
17
|
+
/** Where the log of `db` starts, if `clear` set it. */
|
|
18
|
+
const startOf = async (db) => db.get(`SELECT seq, id FROM ${TABLE}_start`);
|
|
19
|
+
/**
|
|
20
|
+
* Start the log at `start`, in the transaction `tx` of `clear` that emptied
|
|
21
|
+
* the database: the next conversation's ids and cursors go on from there.
|
|
22
|
+
*/
|
|
23
|
+
export async function restart(tx, start) {
|
|
24
|
+
await tx.execute(`INSERT OR REPLACE INTO ${TABLE}_start (row, seq, id) VALUES (0, ?, ?)`, [start.seq, start.id]);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The head of each session (`ctx.db`, one per attempt), as its last `sync`
|
|
28
|
+
* stored it: the next compares the transcript with it, rather than read the
|
|
29
|
+
* whole log again at each step.
|
|
30
|
+
*/
|
|
31
|
+
const heads = new WeakMap();
|
|
32
|
+
/** The log's head in `db`: as this session stored it last, or else as the log has it. */
|
|
33
|
+
async function headOf(db) {
|
|
34
|
+
const cached = heads.get(db);
|
|
35
|
+
if (cached)
|
|
36
|
+
return cached;
|
|
37
|
+
const rows = (await db.values(`SELECT id, message FROM ${TABLE} WHERE position IS NOT NULL ORDER BY position`)).map(([id, json]) => ({ id: Number(id), message: JSON.parse(String(json)), json: String(json) }));
|
|
38
|
+
const { last, next } = (await db.get(`SELECT MAX(seq) AS last, MAX(id) + 1 AS next FROM ${TABLE}`));
|
|
39
|
+
if (last !== null)
|
|
40
|
+
return { rows, seq: last, next: next };
|
|
41
|
+
const from = (await startOf(db)) ?? { seq: 0, id: 0 };
|
|
42
|
+
return { rows, seq: from.seq, next: from.id };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Store the transcript's `messages`, in order, in the log: those that
|
|
46
|
+
* changed since the last time, in one transaction.
|
|
47
|
+
*/
|
|
48
|
+
export async function sync(db, messages) {
|
|
49
|
+
await SCHEMA.ensure(db);
|
|
50
|
+
const head = await headOf(db);
|
|
51
|
+
// A message the transcript did not change is the one stored (see Transcript in common.ts):
|
|
52
|
+
// only the others are compared, as JSON.
|
|
53
|
+
const jsons = [];
|
|
54
|
+
const jsonOf = (position) => (jsons[position] ??= JSON.stringify(messages[position]));
|
|
55
|
+
const unchanged = (position) => {
|
|
56
|
+
const row = head.rows[position];
|
|
57
|
+
return row.message === messages[position] || row.json === jsonOf(position);
|
|
58
|
+
};
|
|
59
|
+
const both = Math.min(head.rows.length, messages.length);
|
|
60
|
+
let same = 0;
|
|
61
|
+
while (same < both && unchanged(same))
|
|
62
|
+
same += 1;
|
|
63
|
+
let grown = same;
|
|
64
|
+
while (grown < both && grownFrom(messages[grown], head.rows[grown].message))
|
|
65
|
+
grown += 1;
|
|
66
|
+
if (same === head.rows.length && same === messages.length)
|
|
67
|
+
return;
|
|
68
|
+
let { seq, next: id } = head;
|
|
69
|
+
const rows = head.rows.slice(0, same);
|
|
70
|
+
try {
|
|
71
|
+
await db.transaction(async (tx) => {
|
|
72
|
+
for (let position = same; position < grown; position += 1) {
|
|
73
|
+
const row = { id: head.rows[position].id, message: messages[position], json: jsonOf(position) };
|
|
74
|
+
await tx.execute(`UPDATE ${TABLE} SET seq = ?, message = ? WHERE id = ?`, [(seq += 1), row.json, row.id]);
|
|
75
|
+
rows.push(row);
|
|
76
|
+
}
|
|
77
|
+
// The rest of the transcript as it was is replaced: its messages stay in the log, out of it.
|
|
78
|
+
await tx.execute(`UPDATE ${TABLE} SET position = NULL WHERE position >= ?`, [grown]);
|
|
79
|
+
for (let position = grown; position < messages.length; position += 1) {
|
|
80
|
+
const row = { id: id++, message: messages[position], json: jsonOf(position) };
|
|
81
|
+
await tx.execute(`INSERT INTO ${TABLE} VALUES (?, ?, ?, ?)`, [row.id, (seq += 1), position, row.json]);
|
|
82
|
+
rows.push(row);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
// Rolled back: the log is as it was, which the next sync reads again.
|
|
88
|
+
heads.delete(db);
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
heads.set(db, { rows, seq, next: id });
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A page of the log. With `cursor`: the messages new or changed since it,
|
|
95
|
+
* by when they changed, and the cursor to read on from. Without: the latest
|
|
96
|
+
* `limit` messages (or those before `before`, an id), oldest first, and the
|
|
97
|
+
* cursor to follow the conversation from.
|
|
98
|
+
*/
|
|
99
|
+
export async function page(db, { limit = 50, before, cursor }) {
|
|
100
|
+
await SCHEMA.ensure(db);
|
|
101
|
+
const start = await startOf(db);
|
|
102
|
+
const first = start ? { first: start.id } : {};
|
|
103
|
+
if (cursor !== undefined) {
|
|
104
|
+
const rows = await db.values(`SELECT id, message, seq FROM ${TABLE} WHERE seq > ? ORDER BY seq LIMIT ?`, [
|
|
105
|
+
cursor,
|
|
106
|
+
limit + 1,
|
|
107
|
+
]);
|
|
108
|
+
const shown = rows.slice(0, limit);
|
|
109
|
+
return {
|
|
110
|
+
messages: shown.map(message),
|
|
111
|
+
more: rows.length > limit,
|
|
112
|
+
cursor: shown.length > 0 ? Number(shown.at(-1)[2]) : cursor,
|
|
113
|
+
...first,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const { now } = (await db.get(`SELECT COALESCE(MAX(seq), ?) AS now FROM ${TABLE}`, [
|
|
117
|
+
start?.seq ?? 0,
|
|
118
|
+
]));
|
|
119
|
+
const rows = await db.values(`SELECT id, message FROM ${TABLE} WHERE id < ? ORDER BY id DESC LIMIT ?`, [
|
|
120
|
+
before ?? Number.MAX_SAFE_INTEGER,
|
|
121
|
+
limit + 1,
|
|
122
|
+
]);
|
|
123
|
+
return {
|
|
124
|
+
messages: rows.slice(0, limit).toReversed().map(message),
|
|
125
|
+
more: rows.length > limit,
|
|
126
|
+
cursor: now,
|
|
127
|
+
...first,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** A row of the log, as `messages` gives it. */
|
|
131
|
+
const message = ([id, m]) => ({ id: Number(id), ...JSON.parse(String(m)) });
|
|
132
|
+
/** Whether `now` is `was` grown in place: the same role and parts, with more (a tool call's output, say). */
|
|
133
|
+
function grownFrom(now, was) {
|
|
134
|
+
if (was.role !== now.role || now.parts.length < was.parts.length)
|
|
135
|
+
return false;
|
|
136
|
+
return was.parts.every((part, i) => {
|
|
137
|
+
const other = now.parts[i];
|
|
138
|
+
if (part.type !== other.type)
|
|
139
|
+
return false;
|
|
140
|
+
if (part.type === 'text')
|
|
141
|
+
return other.type === 'text' && other.text.startsWith(part.text);
|
|
142
|
+
return other.type === 'tool' && other.id === part.id;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=log.js.map
|
package/dist/log.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.js","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAGvD,MAAM,KAAK,GAAG,mBAAmB,CAAC;AAalC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,oBAAoB,EAAE;IAC9C,gBAAgB,KAAK;;;;;IAKnB;IACF,2DAA2D;IAC3D,gBAAgB,KAAK;;;;IAInB;CACH,CAAC,CAAC;AAEH,uDAAuD;AACvD,MAAM,OAAO,GAAG,KAAK,EAAE,EAAkB,EAA8B,EAAE,CACvE,EAA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