@memberjunction/aiengine 3.4.0 → 4.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/README.md +123 -666
- package/dist/AIEngine.d.ts +243 -13
- package/dist/AIEngine.d.ts.map +1 -1
- package/dist/AIEngine.js +406 -154
- package/dist/AIEngine.js.map +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +8 -24
- package/dist/index.js.map +1 -1
- package/dist/services/ActionEmbeddingService.d.ts +72 -1
- package/dist/services/ActionEmbeddingService.d.ts.map +1 -1
- package/dist/services/ActionEmbeddingService.js +88 -12
- package/dist/services/ActionEmbeddingService.js.map +1 -1
- package/dist/services/AgentEmbeddingService.d.ts +70 -1
- package/dist/services/AgentEmbeddingService.d.ts.map +1 -1
- package/dist/services/AgentEmbeddingService.js +87 -13
- package/dist/services/AgentEmbeddingService.js.map +1 -1
- package/dist/services/ConversationAttachmentService.d.ts +126 -0
- package/dist/services/ConversationAttachmentService.d.ts.map +1 -1
- package/dist/services/ConversationAttachmentService.js +180 -25
- package/dist/services/ConversationAttachmentService.js.map +1 -1
- package/dist/types/ActionMatchResult.d.ts +48 -0
- package/dist/types/ActionMatchResult.d.ts.map +1 -1
- package/dist/types/ActionMatchResult.js +1 -2
- package/dist/types/AgentMatchResult.d.ts +52 -0
- package/dist/types/AgentMatchResult.d.ts.map +1 -1
- package/dist/types/AgentMatchResult.js +1 -2
- package/dist/types/ExampleMatchResult.d.ts +8 -0
- package/dist/types/ExampleMatchResult.d.ts.map +1 -1
- package/dist/types/ExampleMatchResult.js +1 -2
- package/dist/types/NoteMatchResult.d.ts +8 -0
- package/dist/types/NoteMatchResult.d.ts.map +1 -1
- package/dist/types/NoteMatchResult.js +1 -2
- package/package.json +16 -15
|
@@ -1,16 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { LogError } from '@memberjunction/core';
|
|
2
|
+
/**
|
|
3
|
+
* Utility service for agent embedding operations.
|
|
4
|
+
* Uses agent data already loaded by AIEngine - no separate initialization needed.
|
|
5
|
+
* All methods are static - no instance state required.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Generate embeddings during AIEngine initialization
|
|
10
|
+
* const entries = await AgentEmbeddingService.GenerateAgentEmbeddings(
|
|
11
|
+
* agents,
|
|
12
|
+
* (text) => aiEngine.EmbedTextLocal(text)
|
|
13
|
+
* );
|
|
14
|
+
*
|
|
15
|
+
* // Find similar agents
|
|
16
|
+
* const matches = await AgentEmbeddingService.FindSimilarAgents(
|
|
17
|
+
* vectorService,
|
|
18
|
+
* "Handle customer support",
|
|
19
|
+
* (text) => aiEngine.EmbedTextLocal(text),
|
|
20
|
+
* 5,
|
|
21
|
+
* 0.7
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class AgentEmbeddingService {
|
|
26
|
+
/**
|
|
27
|
+
* Generate embeddings for agents using provided embedding function.
|
|
28
|
+
* Called by AIEngine during AdditionalLoading.
|
|
29
|
+
*
|
|
30
|
+
* @param agents - Array of agents (already loaded by AIEngine)
|
|
31
|
+
* @param embedFunction - Function to generate embeddings (from AIEngine)
|
|
32
|
+
* @returns Vector entries ready for loading into vector service
|
|
33
|
+
*/
|
|
6
34
|
static async GenerateAgentEmbeddings(agents, embedFunction) {
|
|
7
35
|
const entries = [];
|
|
8
36
|
for (const agent of agents) {
|
|
9
37
|
try {
|
|
38
|
+
// Create embedding text from agent name and description
|
|
10
39
|
const embeddingText = this.createEmbeddingText(agent);
|
|
40
|
+
// Generate embedding using provided function
|
|
11
41
|
const embeddingResult = await embedFunction(embeddingText);
|
|
12
42
|
if (!embeddingResult || !embeddingResult.result || embeddingResult.result.vector.length === 0) {
|
|
13
|
-
|
|
43
|
+
LogError(`Failed to generate embedding for agent ${agent.Name}`);
|
|
14
44
|
continue;
|
|
15
45
|
}
|
|
16
46
|
entries.push({
|
|
@@ -20,7 +50,7 @@ class AgentEmbeddingService {
|
|
|
20
50
|
id: agent.ID,
|
|
21
51
|
name: agent.Name,
|
|
22
52
|
description: agent.Description || '',
|
|
23
|
-
systemPrompt: '',
|
|
53
|
+
systemPrompt: '', // SystemPrompt not available on extended class
|
|
24
54
|
typeName: agent.Type || '',
|
|
25
55
|
status: agent.Status || 'Active',
|
|
26
56
|
invocationMode: agent.InvocationMode || 'Any',
|
|
@@ -30,11 +60,24 @@ class AgentEmbeddingService {
|
|
|
30
60
|
});
|
|
31
61
|
}
|
|
32
62
|
catch (error) {
|
|
33
|
-
|
|
63
|
+
LogError(`Error generating embedding for agent ${agent.Name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
64
|
+
// Continue with other agents
|
|
34
65
|
}
|
|
35
66
|
}
|
|
36
67
|
return entries;
|
|
37
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Find agents similar to a given task description.
|
|
71
|
+
*
|
|
72
|
+
* @param vectorService - Vector service containing agent embeddings
|
|
73
|
+
* @param taskDescription - The task description to match against agent capabilities
|
|
74
|
+
* @param embedFunction - Function to generate embeddings (from AIEngine)
|
|
75
|
+
* @param topK - Maximum number of results to return (default: 5)
|
|
76
|
+
* @param minSimilarity - Minimum similarity score 0-1 (default: 0.5)
|
|
77
|
+
* @returns Array of matching agents sorted by similarity score (highest first)
|
|
78
|
+
*
|
|
79
|
+
* @throws Error if embedding generation fails
|
|
80
|
+
*/
|
|
38
81
|
static async FindSimilarAgents(vectorService, taskDescription, embedFunction, topK = 5, minSimilarity = 0.5) {
|
|
39
82
|
if (!taskDescription || taskDescription.trim().length === 0) {
|
|
40
83
|
throw new Error('taskDescription cannot be empty');
|
|
@@ -46,11 +89,15 @@ class AgentEmbeddingService {
|
|
|
46
89
|
throw new Error('minSimilarity must be between 0 and 1');
|
|
47
90
|
}
|
|
48
91
|
try {
|
|
92
|
+
// Generate embedding for the task description
|
|
49
93
|
const queryEmbedding = await embedFunction(taskDescription);
|
|
50
94
|
if (!queryEmbedding || !queryEmbedding.result || queryEmbedding.result.vector.length === 0) {
|
|
51
95
|
throw new Error('Failed to generate embedding for task description');
|
|
52
96
|
}
|
|
53
|
-
|
|
97
|
+
// Find nearest neighbors using cosine similarity
|
|
98
|
+
const results = vectorService.FindNearest(queryEmbedding.result.vector, topK, minSimilarity, 'cosine' // Best for text embeddings
|
|
99
|
+
);
|
|
100
|
+
// Map to AgentMatchResult format
|
|
54
101
|
return results.map(r => ({
|
|
55
102
|
agentId: r.key,
|
|
56
103
|
agentName: r.metadata?.name || 'Unknown',
|
|
@@ -65,10 +112,21 @@ class AgentEmbeddingService {
|
|
|
65
112
|
}));
|
|
66
113
|
}
|
|
67
114
|
catch (error) {
|
|
68
|
-
|
|
115
|
+
LogError(`Error finding similar agents: ${error instanceof Error ? error.message : String(error)}`);
|
|
69
116
|
throw error;
|
|
70
117
|
}
|
|
71
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Find agents similar to a specific agent.
|
|
121
|
+
*
|
|
122
|
+
* @param vectorService - Vector service containing agent embeddings
|
|
123
|
+
* @param agentId - The ID of the agent to find similar agents to
|
|
124
|
+
* @param topK - Maximum number of results to return (default: 5)
|
|
125
|
+
* @param minSimilarity - Minimum similarity score 0-1 (default: 0.5)
|
|
126
|
+
* @returns Array of matching agents (excludes the source agent)
|
|
127
|
+
*
|
|
128
|
+
* @throws Error if agent not found in embeddings
|
|
129
|
+
*/
|
|
72
130
|
static FindRelatedAgents(vectorService, agentId, topK = 5, minSimilarity = 0.5) {
|
|
73
131
|
if (!vectorService.Has(agentId)) {
|
|
74
132
|
throw new Error(`Agent ${agentId} not found in embeddings`);
|
|
@@ -88,10 +146,20 @@ class AgentEmbeddingService {
|
|
|
88
146
|
}));
|
|
89
147
|
}
|
|
90
148
|
catch (error) {
|
|
91
|
-
|
|
149
|
+
LogError(`Error finding related agents: ${error instanceof Error ? error.message : String(error)}`);
|
|
92
150
|
throw error;
|
|
93
151
|
}
|
|
94
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Get the similarity score between two specific agents.
|
|
155
|
+
*
|
|
156
|
+
* @param vectorService - Vector service containing agent embeddings
|
|
157
|
+
* @param agentId1 - First agent ID
|
|
158
|
+
* @param agentId2 - Second agent ID
|
|
159
|
+
* @returns Similarity score (0-1)
|
|
160
|
+
*
|
|
161
|
+
* @throws Error if either agent is not found
|
|
162
|
+
*/
|
|
95
163
|
static GetSimilarity(vectorService, agentId1, agentId2) {
|
|
96
164
|
if (!vectorService.Has(agentId1)) {
|
|
97
165
|
throw new Error(`Agent ${agentId1} not found in embeddings`);
|
|
@@ -101,14 +169,20 @@ class AgentEmbeddingService {
|
|
|
101
169
|
}
|
|
102
170
|
return vectorService.Similarity(agentId1, agentId2);
|
|
103
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Create the embedding text from agent properties.
|
|
174
|
+
* Combines name and description with proper weighting.
|
|
175
|
+
* @private
|
|
176
|
+
*/
|
|
104
177
|
static createEmbeddingText(agent) {
|
|
178
|
+
// Weight the agent name more heavily by repeating it
|
|
179
|
+
// This ensures that name matches have higher similarity
|
|
105
180
|
const parts = [
|
|
106
181
|
agent.Name,
|
|
107
|
-
agent.Name,
|
|
182
|
+
agent.Name, // Repeat for emphasis
|
|
108
183
|
agent.Description || ''
|
|
109
184
|
];
|
|
110
185
|
return parts.filter(p => p.trim().length > 0).join('. ');
|
|
111
186
|
}
|
|
112
187
|
}
|
|
113
|
-
exports.AgentEmbeddingService = AgentEmbeddingService;
|
|
114
188
|
//# sourceMappingURL=AgentEmbeddingService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgentEmbeddingService.js","sourceRoot":"","sources":["../../src/services/AgentEmbeddingService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AgentEmbeddingService.js","sourceRoot":"","sources":["../../src/services/AgentEmbeddingService.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAA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C;IACxD,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,mBAAmB,CAAC,KAA4B;QAC3D,qDAAqD;QACrD,wDAAwD;QACxD,MAAM,KAAK,GAAG;YACV,KAAK,CAAC,IAAI;YACV,KAAK,CAAC,IAAI,EAAG,sBAAsB;YACnC,KAAK,CAAC,WAAW,IAAI,EAAE;SAC1B,CAAC;QAEF,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7D,CAAC;CACJ"}
|
|
@@ -1,47 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Service for managing conversation attachments.
|
|
3
|
+
*
|
|
4
|
+
* This service provides centralized attachment management including:
|
|
5
|
+
* - Validation against size/count limits with cascade logic
|
|
6
|
+
* - Inline vs MJStorage storage decisions
|
|
7
|
+
* - Thumbnail generation for images
|
|
8
|
+
* - CRUD operations for attachments
|
|
9
|
+
*
|
|
10
|
+
* @module @memberjunction/aiengine
|
|
11
|
+
* @author MemberJunction.com
|
|
12
|
+
* @since 2.130.0
|
|
13
|
+
*/
|
|
1
14
|
import { UserInfo } from '@memberjunction/core';
|
|
2
15
|
import { AIAgentEntity, AIModelEntity, ConversationDetailAttachmentEntity } from '@memberjunction/core-entities';
|
|
3
16
|
import { AttachmentContent } from '@memberjunction/ai-core-plus';
|
|
17
|
+
/**
|
|
18
|
+
* Input for adding a new attachment
|
|
19
|
+
*/
|
|
4
20
|
export interface AddAttachmentInput {
|
|
21
|
+
/** Base64 data URL or raw base64 string */
|
|
5
22
|
data: string;
|
|
23
|
+
/** MIME type (e.g., 'image/png') */
|
|
6
24
|
mimeType: string;
|
|
25
|
+
/** Original filename (optional) */
|
|
7
26
|
fileName?: string;
|
|
27
|
+
/** Width in pixels (for images/video) */
|
|
8
28
|
width?: number;
|
|
29
|
+
/** Height in pixels (for images/video) */
|
|
9
30
|
height?: number;
|
|
31
|
+
/** Duration in seconds (for audio/video) */
|
|
10
32
|
durationSeconds?: number;
|
|
11
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Result of adding an attachment
|
|
36
|
+
*/
|
|
12
37
|
export interface AddAttachmentResult {
|
|
38
|
+
/** Whether the operation succeeded */
|
|
13
39
|
success: boolean;
|
|
40
|
+
/** The created attachment entity (if successful) */
|
|
14
41
|
attachment?: ConversationDetailAttachmentEntity;
|
|
42
|
+
/** Error message (if failed) */
|
|
15
43
|
error?: string;
|
|
16
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Attachment data with content for AI consumption
|
|
47
|
+
*/
|
|
17
48
|
export interface AttachmentWithData {
|
|
18
49
|
attachment: ConversationDetailAttachmentEntity;
|
|
50
|
+
/** The full content as data URL or pre-auth download URL */
|
|
19
51
|
contentUrl: string;
|
|
20
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Service for managing conversation attachments.
|
|
55
|
+
* Handles validation, storage, thumbnails, and CRUD operations.
|
|
56
|
+
*/
|
|
21
57
|
export declare class ConversationAttachmentService {
|
|
22
58
|
private md;
|
|
23
59
|
private modalityCache;
|
|
24
60
|
constructor();
|
|
61
|
+
/**
|
|
62
|
+
* Load and cache modalities for efficient lookup
|
|
63
|
+
*/
|
|
25
64
|
private loadModalitiesIfNeeded;
|
|
65
|
+
/**
|
|
66
|
+
* Get modality by name (e.g., 'Image', 'Audio', 'Video', 'File')
|
|
67
|
+
*/
|
|
26
68
|
private getModalityByName;
|
|
69
|
+
/**
|
|
70
|
+
* Convert AttachmentType to modality name
|
|
71
|
+
*/
|
|
27
72
|
private attachmentTypeToModalityName;
|
|
73
|
+
/**
|
|
74
|
+
* Add a new attachment to a conversation detail.
|
|
75
|
+
* Handles validation, storage decision, thumbnail generation, and record creation.
|
|
76
|
+
*
|
|
77
|
+
* @param conversationDetailId - The ID of the conversation detail
|
|
78
|
+
* @param input - The attachment data and metadata
|
|
79
|
+
* @param agent - The AI agent (for limit overrides)
|
|
80
|
+
* @param model - The AI model (for capability limits)
|
|
81
|
+
* @param contextUser - The current user context
|
|
82
|
+
* @param existingCounts - Current attachment counts for validation
|
|
83
|
+
* @returns The result with created attachment or error
|
|
84
|
+
*/
|
|
28
85
|
addAttachment(conversationDetailId: string, input: AddAttachmentInput, agent: AIAgentEntity | null, model: AIModelEntity | null, contextUser: UserInfo, existingCounts?: {
|
|
29
86
|
images: number;
|
|
30
87
|
videos: number;
|
|
31
88
|
audios: number;
|
|
32
89
|
documents: number;
|
|
33
90
|
}): Promise<AddAttachmentResult>;
|
|
91
|
+
/**
|
|
92
|
+
* Get attachment data with content URL.
|
|
93
|
+
* For inline attachments, returns the base64 data URL.
|
|
94
|
+
* For MJStorage attachments, returns a pre-authenticated download URL.
|
|
95
|
+
*
|
|
96
|
+
* @param attachment - The attachment entity
|
|
97
|
+
* @param contextUser - The current user context
|
|
98
|
+
* @returns The attachment with content URL
|
|
99
|
+
*/
|
|
34
100
|
getAttachmentData(attachment: ConversationDetailAttachmentEntity, contextUser: UserInfo): Promise<AttachmentWithData | null>;
|
|
101
|
+
/**
|
|
102
|
+
* Get a pre-authenticated download URL for an MJStorage file.
|
|
103
|
+
*
|
|
104
|
+
* @param fileId - The File entity ID
|
|
105
|
+
* @param contextUser - The current user context
|
|
106
|
+
* @returns The download URL or null if unavailable
|
|
107
|
+
*/
|
|
35
108
|
getDownloadUrl(fileId: string, contextUser: UserInfo): Promise<string | null>;
|
|
109
|
+
/**
|
|
110
|
+
* Get all attachments for a conversation detail.
|
|
111
|
+
*
|
|
112
|
+
* @param conversationDetailId - The conversation detail ID
|
|
113
|
+
* @param contextUser - The current user context
|
|
114
|
+
* @returns Array of attachment entities
|
|
115
|
+
*/
|
|
36
116
|
getAttachments(conversationDetailId: string, contextUser: UserInfo): Promise<ConversationDetailAttachmentEntity[]>;
|
|
117
|
+
/**
|
|
118
|
+
* Get attachments for multiple conversation details (batch load).
|
|
119
|
+
*
|
|
120
|
+
* @param conversationDetailIds - Array of conversation detail IDs
|
|
121
|
+
* @param contextUser - The current user context
|
|
122
|
+
* @returns Map of conversation detail ID to attachments
|
|
123
|
+
*/
|
|
37
124
|
getAttachmentsBatch(conversationDetailIds: string[], contextUser: UserInfo): Promise<Map<string, ConversationDetailAttachmentEntity[]>>;
|
|
125
|
+
/**
|
|
126
|
+
* Delete an attachment and its underlying storage.
|
|
127
|
+
*
|
|
128
|
+
* @param attachmentId - The attachment ID
|
|
129
|
+
* @param contextUser - The current user context
|
|
130
|
+
* @returns Whether deletion succeeded
|
|
131
|
+
*/
|
|
38
132
|
deleteAttachment(attachmentId: string, contextUser: UserInfo): Promise<boolean>;
|
|
133
|
+
/**
|
|
134
|
+
* Create an AttachmentContent reference for embedding in messages.
|
|
135
|
+
* Maps from entity to the content block type used in AI messages.
|
|
136
|
+
*
|
|
137
|
+
* @param attachment - The attachment entity
|
|
138
|
+
* @returns The attachment content reference
|
|
139
|
+
*/
|
|
39
140
|
createAttachmentReference(attachment: ConversationDetailAttachmentEntity): AttachmentContent;
|
|
141
|
+
/**
|
|
142
|
+
* Calculate the decoded size of base64 data
|
|
143
|
+
*/
|
|
40
144
|
private calculateBase64Size;
|
|
145
|
+
/**
|
|
146
|
+
* Upload data to MJStorage
|
|
147
|
+
*/
|
|
41
148
|
private uploadToStorage;
|
|
149
|
+
/**
|
|
150
|
+
* Delete a file from MJStorage
|
|
151
|
+
*/
|
|
42
152
|
private deleteStorageFile;
|
|
153
|
+
/**
|
|
154
|
+
* Generate a thumbnail for an image.
|
|
155
|
+
* Returns base64-encoded thumbnail data.
|
|
156
|
+
*
|
|
157
|
+
* Note: This is a placeholder implementation.
|
|
158
|
+
* In production, you'd use a library like sharp or Jimp for server-side
|
|
159
|
+
* image processing, or Canvas API for client-side.
|
|
160
|
+
*/
|
|
43
161
|
private generateImageThumbnail;
|
|
162
|
+
/**
|
|
163
|
+
* Create an attachment record in the database using strongly-typed entity.
|
|
164
|
+
*/
|
|
44
165
|
private createAttachmentRecord;
|
|
45
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Get the singleton instance of the ConversationAttachmentService.
|
|
169
|
+
*
|
|
170
|
+
* @returns The attachment service instance
|
|
171
|
+
*/
|
|
46
172
|
export declare function getAttachmentService(): ConversationAttachmentService;
|
|
47
173
|
//# sourceMappingURL=ConversationAttachmentService.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConversationAttachmentService.d.ts","sourceRoot":"","sources":["../../src/services/ConversationAttachmentService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ConversationAttachmentService.d.ts","sourceRoot":"","sources":["../../src/services/ConversationAttachmentService.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAqB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAGH,aAAa,EACb,aAAa,EACb,kCAAkC,EAErC,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EAIH,iBAAiB,EAGpB,MAAM,8BAA8B,CAAC;AAGtC;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,2CAA2C;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,mCAAmC;IACnC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yCAAyC;IACzC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,0CAA0C;IAC1C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC,sCAAsC;IACtC,OAAO,EAAE,OAAO,CAAC;IACjB,oDAAoD;IACpD,UAAU,CAAC,EAAE,kCAAkC,CAAC;IAChD,gCAAgC;IAChC,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB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