@n8n/agents 0.4.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/dist/build.tsbuildinfo +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/agent-runtime.d.ts +2 -1
- package/dist/runtime/agent-runtime.js +21 -76
- package/dist/runtime/agent-runtime.js.map +1 -1
- package/dist/runtime/message-list.d.ts +1 -0
- package/dist/runtime/message-list.js +2 -2
- package/dist/runtime/message-list.js.map +1 -1
- package/dist/runtime/model-factory.js +17 -4
- package/dist/runtime/model-factory.js.map +1 -1
- package/dist/runtime/title-generation.d.ts +4 -0
- package/dist/runtime/title-generation.js +48 -18
- package/dist/runtime/title-generation.js.map +1 -1
- package/dist/runtime/working-memory.d.ts +10 -17
- package/dist/runtime/working-memory.js +41 -110
- package/dist/runtime/working-memory.js.map +1 -1
- package/dist/sdk/memory.d.ts +2 -0
- package/dist/sdk/memory.js +10 -0
- package/dist/sdk/memory.js.map +1 -1
- package/dist/types/sdk/agent.d.ts +1 -0
- package/dist/types/sdk/memory.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateTitleFromMessage = generateTitleFromMessage;
|
|
3
4
|
exports.generateThreadTitle = generateThreadTitle;
|
|
4
5
|
const ai_1 = require("ai");
|
|
5
6
|
const logger_1 = require("./logger");
|
|
@@ -7,12 +8,56 @@ const model_factory_1 = require("./model-factory");
|
|
|
7
8
|
const logger = (0, logger_1.createFilteredLogger)();
|
|
8
9
|
const DEFAULT_TITLE_INSTRUCTIONS = [
|
|
9
10
|
'- you will generate a short title based on the first message a user begins a conversation with',
|
|
10
|
-
|
|
11
|
+
'- the title should describe what the user asked for, not what an assistant might reply',
|
|
11
12
|
'- 1 to 5 words, no more than 80 characters',
|
|
12
13
|
'- use sentence case (e.g. "Conversation title" instead of "Conversation Title")',
|
|
13
14
|
'- do not use quotes, colons, or markdown formatting',
|
|
14
15
|
'- the entire text you return will be used directly as the title, so respond with the title only',
|
|
15
16
|
].join('\n');
|
|
17
|
+
const TRIVIAL_MESSAGE_MAX_CHARS = 15;
|
|
18
|
+
const TRIVIAL_MESSAGE_MAX_WORDS = 3;
|
|
19
|
+
const MAX_TITLE_LENGTH = 80;
|
|
20
|
+
function isTrivialMessage(message) {
|
|
21
|
+
const normalized = message.trim();
|
|
22
|
+
if (normalized.length <= TRIVIAL_MESSAGE_MAX_CHARS)
|
|
23
|
+
return true;
|
|
24
|
+
const wordCount = normalized.split(/\s+/).filter(Boolean).length;
|
|
25
|
+
return wordCount <= TRIVIAL_MESSAGE_MAX_WORDS;
|
|
26
|
+
}
|
|
27
|
+
function sanitizeTitle(raw) {
|
|
28
|
+
let title = raw.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
|
29
|
+
title = title
|
|
30
|
+
.replace(/^#{1,6}\s+/, '')
|
|
31
|
+
.replace(/\*+/g, '')
|
|
32
|
+
.trim();
|
|
33
|
+
title = title.replace(/^["']|["']$/g, '').trim();
|
|
34
|
+
if (title.length > MAX_TITLE_LENGTH) {
|
|
35
|
+
const truncated = title.slice(0, MAX_TITLE_LENGTH);
|
|
36
|
+
const lastSpace = truncated.lastIndexOf(' ');
|
|
37
|
+
title = (lastSpace > 20 ? truncated.slice(0, lastSpace) : truncated) + '\u2026';
|
|
38
|
+
}
|
|
39
|
+
return title;
|
|
40
|
+
}
|
|
41
|
+
async function generateTitleFromMessage(model, userMessage, opts) {
|
|
42
|
+
const trimmed = userMessage.trim();
|
|
43
|
+
if (!trimmed)
|
|
44
|
+
return null;
|
|
45
|
+
if (isTrivialMessage(trimmed)) {
|
|
46
|
+
return sanitizeTitle(trimmed) || null;
|
|
47
|
+
}
|
|
48
|
+
const result = await (0, ai_1.generateText)({
|
|
49
|
+
model,
|
|
50
|
+
messages: [
|
|
51
|
+
{ role: 'system', content: opts?.instructions ?? DEFAULT_TITLE_INSTRUCTIONS },
|
|
52
|
+
{ role: 'user', content: trimmed },
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
const raw = result.text?.trim();
|
|
56
|
+
if (!raw)
|
|
57
|
+
return null;
|
|
58
|
+
const title = sanitizeTitle(raw);
|
|
59
|
+
return title || null;
|
|
60
|
+
}
|
|
16
61
|
async function generateThreadTitle(opts) {
|
|
17
62
|
try {
|
|
18
63
|
const thread = await opts.memory.getThread(opts.threadId);
|
|
@@ -29,24 +74,9 @@ async function generateThreadTitle(opts) {
|
|
|
29
74
|
return;
|
|
30
75
|
const titleModelId = opts.titleConfig.model ?? opts.agentModel;
|
|
31
76
|
const titleModel = (0, model_factory_1.createModel)(titleModelId);
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
model: titleModel,
|
|
35
|
-
messages: [
|
|
36
|
-
{ role: 'system', content: instructions },
|
|
37
|
-
{ role: 'user', content: userText },
|
|
38
|
-
],
|
|
77
|
+
const title = await generateTitleFromMessage(titleModel, userText, {
|
|
78
|
+
instructions: opts.titleConfig.instructions,
|
|
39
79
|
});
|
|
40
|
-
let title = result.text?.trim();
|
|
41
|
-
if (!title)
|
|
42
|
-
return;
|
|
43
|
-
title = title.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
|
44
|
-
if (!title)
|
|
45
|
-
return;
|
|
46
|
-
title = title
|
|
47
|
-
.replace(/^#{1,6}\s+/, '')
|
|
48
|
-
.replace(/\*+/g, '')
|
|
49
|
-
.trim();
|
|
50
80
|
if (!title)
|
|
51
81
|
return;
|
|
52
82
|
await opts.memory.saveThread({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"title-generation.js","sourceRoot":"","sources":["../../src/runtime/title-generation.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"title-generation.js","sourceRoot":"","sources":["../../src/runtime/title-generation.ts"],"names":[],"mapp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{ z } from 'zod';
|
|
2
|
+
import type { BuiltTool } from '../types';
|
|
3
3
|
type ZodObjectSchema = z.ZodObject<z.ZodRawShape>;
|
|
4
|
-
export declare
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
export declare function buildWorkingMemoryInstruction(template: string, structured: boolean): string;
|
|
4
|
+
export declare const UPDATE_WORKING_MEMORY_TOOL_NAME = "updateWorkingMemory";
|
|
5
|
+
export declare const WORKING_MEMORY_DEFAULT_INSTRUCTION: string;
|
|
6
|
+
export declare function buildWorkingMemoryInstruction(template: string, structured: boolean, instruction?: string): string;
|
|
9
7
|
export declare function templateFromSchema(schema: ZodObjectSchema): string;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
private state;
|
|
15
|
-
private buffer;
|
|
16
|
-
private pendingText;
|
|
17
|
-
constructor(writer: WritableStreamDefaultWriter<StreamChunk>, persist: PersistFn);
|
|
18
|
-
write(chunk: StreamChunk): Promise<void>;
|
|
19
|
-
flush(): Promise<void>;
|
|
8
|
+
export interface WorkingMemoryToolConfig {
|
|
9
|
+
structured: boolean;
|
|
10
|
+
schema?: ZodObjectSchema;
|
|
11
|
+
persist: (content: string) => Promise<void>;
|
|
20
12
|
}
|
|
13
|
+
export declare function buildWorkingMemoryTool(config: WorkingMemoryToolConfig): BuiltTool;
|
|
21
14
|
export {};
|
|
@@ -1,42 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.parseWorkingMemory = parseWorkingMemory;
|
|
3
|
+
exports.WORKING_MEMORY_DEFAULT_INSTRUCTION = exports.UPDATE_WORKING_MEMORY_TOOL_NAME = void 0;
|
|
5
4
|
exports.buildWorkingMemoryInstruction = buildWorkingMemoryInstruction;
|
|
6
5
|
exports.templateFromSchema = templateFromSchema;
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return { cleanText: text, workingMemory: null };
|
|
18
|
-
const contentStart = openIdx + OPEN_TAG.length;
|
|
19
|
-
const rawContent = text.slice(contentStart, closeIdx);
|
|
20
|
-
const workingMemory = rawContent.replace(/^\n/, '').replace(/\n$/, '');
|
|
21
|
-
const before = text.slice(0, openIdx).replace(/\n$/, '');
|
|
22
|
-
const after = text.slice(closeIdx + CLOSE_TAG.length).replace(/^\n/, '');
|
|
23
|
-
const cleanText = (before + (after ? '\n' + after : '')).trim();
|
|
24
|
-
return { cleanText, workingMemory };
|
|
25
|
-
}
|
|
26
|
-
function buildWorkingMemoryInstruction(template, structured) {
|
|
6
|
+
exports.buildWorkingMemoryTool = buildWorkingMemoryTool;
|
|
7
|
+
const zod_1 = require("zod");
|
|
8
|
+
exports.UPDATE_WORKING_MEMORY_TOOL_NAME = 'updateWorkingMemory';
|
|
9
|
+
exports.WORKING_MEMORY_DEFAULT_INSTRUCTION = [
|
|
10
|
+
'You have persistent working memory that survives across conversations.',
|
|
11
|
+
'Your current working memory state is shown below.',
|
|
12
|
+
`When you learn new information about the user or conversation that should be remembered, call the \`${exports.UPDATE_WORKING_MEMORY_TOOL_NAME}\` tool.`,
|
|
13
|
+
'Only call it when something has actually changed — do NOT call it if nothing new was learned.',
|
|
14
|
+
].join('\n');
|
|
15
|
+
function buildWorkingMemoryInstruction(template, structured, instruction) {
|
|
27
16
|
const format = structured
|
|
28
|
-
? '
|
|
17
|
+
? 'The memory argument must be valid JSON matching the schema'
|
|
29
18
|
: 'Update the template with any new information learned';
|
|
19
|
+
const body = instruction ?? exports.WORKING_MEMORY_DEFAULT_INSTRUCTION;
|
|
30
20
|
return [
|
|
31
21
|
'',
|
|
32
22
|
'## Working Memory',
|
|
33
23
|
'',
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
'IMPORTANT: Always respond to the user first with your normal reply.',
|
|
37
|
-
`Then, at the very end of your response, emit your updated working memory inside ${OPEN_TAG}...${CLOSE_TAG} tags on a new line.`,
|
|
38
|
-
`${format}. If nothing changed, emit the current state unchanged.`,
|
|
39
|
-
'The working memory block must be the last thing in your response, after your reply to the user.',
|
|
24
|
+
body,
|
|
25
|
+
`${format}.`,
|
|
40
26
|
'',
|
|
41
27
|
'Current template:',
|
|
42
28
|
'```',
|
|
@@ -52,87 +38,32 @@ function templateFromSchema(schema) {
|
|
|
52
38
|
}
|
|
53
39
|
return JSON.stringify(obj, null, 2);
|
|
54
40
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
this.pendingText += chunk.delta;
|
|
69
|
-
while (this.pendingText.length > 0) {
|
|
70
|
-
if (this.state === 'normal') {
|
|
71
|
-
const openIdx = this.pendingText.indexOf(OPEN_TAG);
|
|
72
|
-
if (openIdx === -1) {
|
|
73
|
-
const lastLt = this.pendingText.lastIndexOf('<');
|
|
74
|
-
if (lastLt !== -1 &&
|
|
75
|
-
this.pendingText.length - lastLt < OPEN_TAG.length &&
|
|
76
|
-
OPEN_TAG.startsWith(this.pendingText.slice(lastLt))) {
|
|
77
|
-
if (lastLt > 0) {
|
|
78
|
-
await this.writer.write({
|
|
79
|
-
type: 'text-delta',
|
|
80
|
-
delta: this.pendingText.slice(0, lastLt),
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
this.pendingText = this.pendingText.slice(lastLt);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
await this.writer.write({ type: 'text-delta', delta: this.pendingText });
|
|
87
|
-
this.pendingText = '';
|
|
88
|
-
}
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
if (openIdx > 0) {
|
|
92
|
-
await this.writer.write({
|
|
93
|
-
type: 'text-delta',
|
|
94
|
-
delta: this.pendingText.slice(0, openIdx),
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
this.state = 'inside';
|
|
98
|
-
this.pendingText = this.pendingText.slice(openIdx + OPEN_TAG.length);
|
|
99
|
-
this.buffer = '';
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
const closeIdx = this.pendingText.indexOf(CLOSE_TAG);
|
|
103
|
-
if (closeIdx === -1) {
|
|
104
|
-
const lastLt = this.pendingText.lastIndexOf('<');
|
|
105
|
-
if (lastLt !== -1 &&
|
|
106
|
-
this.pendingText.length - lastLt < CLOSE_TAG.length &&
|
|
107
|
-
CLOSE_TAG.startsWith(this.pendingText.slice(lastLt))) {
|
|
108
|
-
this.buffer += this.pendingText.slice(0, lastLt);
|
|
109
|
-
this.pendingText = this.pendingText.slice(lastLt);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
this.buffer += this.pendingText;
|
|
113
|
-
this.pendingText = '';
|
|
114
|
-
}
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
this.buffer += this.pendingText.slice(0, closeIdx);
|
|
118
|
-
this.pendingText = this.pendingText.slice(closeIdx + CLOSE_TAG.length);
|
|
119
|
-
this.state = 'normal';
|
|
120
|
-
const content = this.buffer.replace(/^\n/, '').replace(/\n$/, '');
|
|
121
|
-
this.persist(content).catch((error) => {
|
|
122
|
-
logger.warn('Failed to persist working memory', { error });
|
|
123
|
-
});
|
|
124
|
-
this.buffer = '';
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
async flush() {
|
|
129
|
-
if (this.state === 'normal' && this.pendingText.length > 0) {
|
|
130
|
-
await this.writer.write({ type: 'text-delta', delta: this.pendingText });
|
|
131
|
-
}
|
|
132
|
-
this.pendingText = '';
|
|
133
|
-
this.buffer = '';
|
|
134
|
-
this.state = 'normal';
|
|
41
|
+
function buildWorkingMemoryTool(config) {
|
|
42
|
+
if (config.structured && config.schema) {
|
|
43
|
+
const schema = config.schema;
|
|
44
|
+
return {
|
|
45
|
+
name: exports.UPDATE_WORKING_MEMORY_TOOL_NAME,
|
|
46
|
+
description: 'Update your persistent working memory with new information about the user or conversation. Only call this when something has actually changed.',
|
|
47
|
+
inputSchema: schema,
|
|
48
|
+
handler: async (input) => {
|
|
49
|
+
const content = JSON.stringify(input, null, 2);
|
|
50
|
+
await config.persist(content);
|
|
51
|
+
return { success: true, message: 'Working memory updated.' };
|
|
52
|
+
},
|
|
53
|
+
};
|
|
135
54
|
}
|
|
55
|
+
const freeformSchema = zod_1.z.object({
|
|
56
|
+
memory: zod_1.z.string().describe('The updated working memory content.'),
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
name: exports.UPDATE_WORKING_MEMORY_TOOL_NAME,
|
|
60
|
+
description: 'Update your persistent working memory with new information about the user or conversation. Only call this when something has actually changed.',
|
|
61
|
+
inputSchema: freeformSchema,
|
|
62
|
+
handler: async (input) => {
|
|
63
|
+
const { memory } = input;
|
|
64
|
+
await config.persist(memory);
|
|
65
|
+
return { success: true, message: 'Working memory updated.' };
|
|
66
|
+
},
|
|
67
|
+
};
|
|
136
68
|
}
|
|
137
|
-
exports.WorkingMemoryStreamFilter = WorkingMemoryStreamFilter;
|
|
138
69
|
//# sourceMappingURL=working-memory.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"working-memory.js","sourceRoot":"","sources":["../../src/runtime/working-memory.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"working-memory.js","sourceRoot":"","sources":["../../src/runtime/working-memory.ts"],"names":[],"mappings":";;;AA4BA,sEAuBC;AAKD,gDAOC;AAkBD,wDA+BC;AAhHD,6BAAwB;AAMX,QAAA,+BAA+B,GAAG,qBAAqB,CAAC;AAMxD,QAAA,kCAAkC,GAAG;IACjD,wEAAwE;IACxE,mDAAmD;IACnD,uGAAuG,uCAA+B,UAAU;IAChJ,+FAA+F;CAC/F,CAAC,IAAI,CAAC,I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C,OAAO,CAAC,CAAC;gBAC9B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC;YAC9D,CAAC;SACD,CAAC;IACH,CAAC;IAED,MAAM,cAAc,GAAG,OAAC,CAAC,MAAM,CAAC;QAC/B,MAAM,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,qCAAqC,CAAC;KAClE,CAAC,CAAC;IAEH,OAAO;QACN,IAAI,EAAE,uCAA+B;QACrC,WAAW,EACV,gJAAgJ;QACjJ,WAAW,EAAE,cAAc;QAC3B,OAAO,EAAE,KAAK,EAAE,KAAc,EAAE,EAAE;YACjC,MAAM,EAAE,MAAM,EAAE,GAAG,KAAuC,CAAC;YAC3D,MAAM,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAC7B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC;QAC9D,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/sdk/memory.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare class Memory {
|
|
|
7
7
|
private workingMemorySchema?;
|
|
8
8
|
private workingMemoryTemplate?;
|
|
9
9
|
private workingMemoryScope;
|
|
10
|
+
private workingMemoryInstruction?;
|
|
10
11
|
private memoryBackend?;
|
|
11
12
|
private titleGenerationConfig?;
|
|
12
13
|
get lastMessageCount(): number;
|
|
@@ -16,6 +17,7 @@ export declare class Memory {
|
|
|
16
17
|
structured(schema: ZodObjectSchema): this;
|
|
17
18
|
freeform(template: string): this;
|
|
18
19
|
scope(s: 'resource' | 'thread'): this;
|
|
20
|
+
instruction(text: string): this;
|
|
19
21
|
titleGeneration(config: boolean | TitleGenerationConfig): this;
|
|
20
22
|
build(): MemoryConfig;
|
|
21
23
|
}
|
package/dist/sdk/memory.js
CHANGED
|
@@ -41,6 +41,10 @@ class Memory {
|
|
|
41
41
|
this.workingMemoryScope = s;
|
|
42
42
|
return this;
|
|
43
43
|
}
|
|
44
|
+
instruction(text) {
|
|
45
|
+
this.workingMemoryInstruction = text;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
44
48
|
titleGeneration(config) {
|
|
45
49
|
if (config === true) {
|
|
46
50
|
this.titleGenerationConfig = {};
|
|
@@ -79,6 +83,9 @@ class Memory {
|
|
|
79
83
|
structured: true,
|
|
80
84
|
schema: this.workingMemorySchema,
|
|
81
85
|
scope: this.workingMemoryScope,
|
|
86
|
+
...(this.workingMemoryInstruction !== undefined && {
|
|
87
|
+
instruction: this.workingMemoryInstruction,
|
|
88
|
+
}),
|
|
82
89
|
};
|
|
83
90
|
}
|
|
84
91
|
else if (this.workingMemoryTemplate !== undefined) {
|
|
@@ -86,6 +93,9 @@ class Memory {
|
|
|
86
93
|
template: this.workingMemoryTemplate,
|
|
87
94
|
structured: false,
|
|
88
95
|
scope: this.workingMemoryScope,
|
|
96
|
+
...(this.workingMemoryInstruction !== undefined && {
|
|
97
|
+
instruction: this.workingMemoryInstruction,
|
|
98
|
+
}),
|
|
89
99
|
};
|
|
90
100
|
}
|
|
91
101
|
return {
|
package/dist/sdk/memory.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/sdk/memory.ts"],"names":[],"mappings":";;;AAEA,0DAAyD;AACzD,8DAA+D;AAU/D,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAejC,MAAa,MAAM;IAAnB;QACS,sBAAiB,GAAW,qBAAqB,CAAC;QAQlD,uBAAkB,GAA0B,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/sdk/memory.ts"],"names":[],"mappings":";;;AAEA,0DAAyD;AACzD,8DAA+D;AAU/D,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAejC,MAAa,MAAM;IAAnB;QACS,sBAAiB,GAAW,qBAAqB,CAAC;QAQlD,uBAAkB,GAA0B,UAAU,CAAC;IAiLhE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