|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import * as bedrockagentcore from 'aws-cdk-lib/aws-bedrockagentcore'; |
| 15 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 16 | +// Internal libs |
| 17 | +import { ManagedMemoryStrategy, ManagedStrategyProps } from './strategies/managed-strategy'; |
| 18 | +import { SelfManagedMemoryStrategy, SelfManagedStrategyProps } from './strategies/self-managed-strategy'; |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + * CONSTANTS |
| 22 | + *****************************************************************************/ |
| 23 | +/** |
| 24 | + * Minimum length for memory strategy name |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +export const MEMORY_NAME_MIN_LENGTH = 1; |
| 28 | +/** |
| 29 | + * Maximum length for memory strategy name |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +export const MEMORY_NAME_MAX_LENGTH = 48; |
| 33 | + |
| 34 | +/** |
| 35 | + * Long-term memory extraction strategy types. |
| 36 | + */ |
| 37 | +export enum MemoryStrategyType { |
| 38 | + /** |
| 39 | + * Summarization strategy - extracts concise summaries to preserve critical context and key insights |
| 40 | + */ |
| 41 | + SUMMARIZATION = 'SUMMARIZATION', |
| 42 | + /** |
| 43 | + * Semantic memory strategy - extracts general factual knowledge, concepts and meanings from raw conversations |
| 44 | + * using vector embeddings for similarity search. |
| 45 | + */ |
| 46 | + SEMANTIC = 'SEMANTIC', |
| 47 | + /** |
| 48 | + * User preferences strategy - extracts user behavior patterns from raw conversations. |
| 49 | + */ |
| 50 | + USER_PREFERENCE = 'USER_PREFERENCE', |
| 51 | + /** |
| 52 | + * Customize memory processing through custom foundation model and prompt templates. |
| 53 | + */ |
| 54 | + CUSTOM = 'CUSTOM', |
| 55 | +} |
| 56 | + |
| 57 | +/****************************************************************************** |
| 58 | + * Common |
| 59 | + *****************************************************************************/ |
| 60 | +/** |
| 61 | + * Configuration parameters common for any memory strategy |
| 62 | + */ |
| 63 | +export interface MemoryStrategyCommonProps { |
| 64 | + /** |
| 65 | + * The name for the strategy |
| 66 | + * @required - Yes |
| 67 | + */ |
| 68 | + readonly name: string; |
| 69 | + /** |
| 70 | + * The description of the strategy |
| 71 | + * @default No description |
| 72 | + * @required - No |
| 73 | + */ |
| 74 | + readonly description?: string; |
| 75 | +} |
| 76 | + |
| 77 | +/****************************************************************************** |
| 78 | + * Interface |
| 79 | + *****************************************************************************/ |
| 80 | +/** |
| 81 | + * Interface for Memory strategies |
| 82 | + */ |
| 83 | +export interface IMemoryStrategy { |
| 84 | + /** |
| 85 | + * The name of the memory strategy |
| 86 | + */ |
| 87 | + readonly name: string; |
| 88 | + /** |
| 89 | + * The description of the memory strategy |
| 90 | + */ |
| 91 | + readonly description?: string; |
| 92 | + /** |
| 93 | + * The type of memory strategy |
| 94 | + */ |
| 95 | + readonly strategyType: MemoryStrategyType; |
| 96 | + /** |
| 97 | + * Renders internal attributes to CloudFormation |
| 98 | + */ |
| 99 | + render(): bedrockagentcore.CfnMemory.MemoryStrategyProperty; |
| 100 | + /** |
| 101 | + * Grants the necessary permissions to the role |
| 102 | + * @param grantee - The grantee to grant permissions to |
| 103 | + * @returns The Grant object for chaining |
| 104 | + */ |
| 105 | + grant(grantee: iam.IGrantable): iam.Grant | undefined; |
| 106 | +} |
| 107 | + |
| 108 | +/****************************************************************************** |
| 109 | + * Factory |
| 110 | + *****************************************************************************/ |
| 111 | +/** |
| 112 | + * Factory class for creating memory strategies |
| 113 | + * If you need long-term memory for context recall across sessions, you can setup memory extraction strategies to extract the relevant memory from the raw events. |
| 114 | + * Use built-in strategies for quick setup, use built-in strategies with override to specify models and prompt templates. |
| 115 | + * |
| 116 | + * @see https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/memory-strategies.html |
| 117 | + */ |
| 118 | +export class MemoryStrategy { |
| 119 | + /** |
| 120 | + * Default strategies for organizing and extracting memory data, each optimized for specific use cases. |
| 121 | + * This strategy compresses conversations into concise overviews, preserving essential context and key insights for quick recall. |
| 122 | + * Extracted memory example: Users confused by cloud setup during onboarding. |
| 123 | + * @returns A ManagedMemoryStrategy. |
| 124 | + */ |
| 125 | + public static usingBuiltInSummarization(): ManagedMemoryStrategy { |
| 126 | + return new ManagedMemoryStrategy(MemoryStrategyType.SUMMARIZATION, { |
| 127 | + name: 'summary_builtin_cdkGen0001', |
| 128 | + description: 'Summarize interactions to preserve critical context and key insights', |
| 129 | + namespaces: ['/strategies/{memoryStrategyId}/actors/{actorId}/sessions/{sessionId}'], |
| 130 | + }); |
| 131 | + } |
| 132 | + /** |
| 133 | + * Default strategies for organizing and extracting memory data, each optimized for specific use cases. |
| 134 | + * Distills general facts, concepts, and underlying meanings from raw conversational data, presenting the information in a context-independent format. |
| 135 | + * Extracted memory example: In-context learning = task-solving via examples, no training needed. |
| 136 | + * @returns A ManagedMemoryStrategy. |
| 137 | + */ |
| 138 | + public static usingBuiltInSemantic(): ManagedMemoryStrategy { |
| 139 | + return new ManagedMemoryStrategy(MemoryStrategyType.SEMANTIC, { |
| 140 | + name: 'semantic_builtin_cdkGen0001', |
| 141 | + description: |
| 142 | + 'Extract general factual knowledge, concepts and meanings from raw conversations in a context-independent format.', |
| 143 | + namespaces: ['/strategies/{memoryStrategyId}/actors/{actorId}'], |
| 144 | + }); |
| 145 | + } |
| 146 | + /** |
| 147 | + * Default strategies for organizing and extracting memory data, each optimized for specific use cases. |
| 148 | + * Captures individual preferences, interaction patterns, and personalized settings to enhance future experiences. |
| 149 | + * Extracted memory example: User needs clear guidance on cloud storage account connection during onboarding. |
| 150 | + * @returns A ManagedMemoryStrategy. |
| 151 | + */ |
| 152 | + public static usingBuiltInUserPreference(): ManagedMemoryStrategy { |
| 153 | + return new ManagedMemoryStrategy(MemoryStrategyType.USER_PREFERENCE, { |
| 154 | + name: 'preference_builtin_cdkGen0001', |
| 155 | + description: 'Capture individual preferences, interaction patterns, and personalized settings to enhance future experiences.', |
| 156 | + namespaces: ['/strategies/{memoryStrategyId}/actors/{actorId}'], |
| 157 | + }); |
| 158 | + } |
| 159 | + /** |
| 160 | + * Creates a semantic memory strategy with custom configuration. |
| 161 | + * Distills general facts, concepts, and underlying meanings from raw conversational data, presenting the information in a context-independent format. |
| 162 | + * Extracted memory example: In-context learning = task-solving via examples, no training needed. |
| 163 | + * @param config - The configuration for the semantic memory strategy. |
| 164 | + * @returns A ManagedMemoryStrategy. |
| 165 | + */ |
| 166 | + public static usingSemantic(config: ManagedStrategyProps): ManagedMemoryStrategy { |
| 167 | + return new ManagedMemoryStrategy(MemoryStrategyType.SEMANTIC, config); |
| 168 | + } |
| 169 | + /** |
| 170 | + * Creates a user preference memory strategy with custom configuration. |
| 171 | + * Captures individual preferences, interaction patterns, and personalized settings to enhance future experiences. |
| 172 | + * Extracted memory example: User needs clear guidance on cloud storage account connection during onboarding. |
| 173 | + * @param config - The configuration for the user preference memory strategy. |
| 174 | + * @returns A ManagedMemoryStrategy. |
| 175 | + */ |
| 176 | + public static usingUserPreference(config: ManagedStrategyProps): ManagedMemoryStrategy { |
| 177 | + return new ManagedMemoryStrategy(MemoryStrategyType.USER_PREFERENCE, config); |
| 178 | + } |
| 179 | + /** |
| 180 | + * Creates a summarization memory strategy with custom configuration. |
| 181 | + * This strategy compresses conversations into concise overviews, preserving essential context and key insights for quick recall. |
| 182 | + * Extracted memory example: Users confused by cloud setup during onboarding. |
| 183 | + * @param config - The configuration for the summarization memory strategy. |
| 184 | + * @returns A ManagedMemoryStrategy. |
| 185 | + */ |
| 186 | + public static usingSummarization(config: ManagedStrategyProps): ManagedMemoryStrategy { |
| 187 | + return new ManagedMemoryStrategy(MemoryStrategyType.SUMMARIZATION, config); |
| 188 | + } |
| 189 | + /** |
| 190 | + * Creates a self-managed memory strategy. |
| 191 | + * A self-managed strategy gives you complete control over your memory extraction and consolidation pipelines. |
| 192 | + * @param config - The configuration for the self-managed memory strategy. |
| 193 | + * @returns A SelfManagedMemoryStrategy. |
| 194 | + */ |
| 195 | + public static usingSelfManaged(config: SelfManagedStrategyProps): SelfManagedMemoryStrategy { |
| 196 | + // Scope is passed for future use in permission granting |
| 197 | + return new SelfManagedMemoryStrategy(MemoryStrategyType.CUSTOM, config); |
| 198 | + } |
| 199 | +} |
0 commit comments