|
| 1 | +import CodexKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +@MainActor |
| 5 | +extension AgentDemoViewModel { |
| 6 | + func runAutomaticPolicyMemoryDemo() async { |
| 7 | + guard session != nil else { |
| 8 | + lastError = "Sign in before running policy-based memory capture." |
| 9 | + return |
| 10 | + } |
| 11 | + guard !isRunningMemoryDemo else { |
| 12 | + return |
| 13 | + } |
| 14 | + |
| 15 | + isRunningMemoryDemo = true |
| 16 | + lastError = nil |
| 17 | + defer { |
| 18 | + isRunningMemoryDemo = false |
| 19 | + } |
| 20 | + |
| 21 | + do { |
| 22 | + let thread = try await runtime.createThread( |
| 23 | + title: "Memory Demo: Automatic Policy", |
| 24 | + memoryContext: AgentMemoryContext( |
| 25 | + namespace: DemoMemoryExamples.namespace, |
| 26 | + scopes: [DemoMemoryExamples.healthCoachScope], |
| 27 | + kinds: ["preference"] |
| 28 | + ) |
| 29 | + ) |
| 30 | + _ = try await runtime.sendMessage( |
| 31 | + UserMessageRequest(text: DemoMemoryExamples.automaticPolicyPrompt), |
| 32 | + in: thread.id |
| 33 | + ) |
| 34 | + |
| 35 | + let store = try SQLiteMemoryStore(url: AgentDemoRuntimeFactory.defaultMemoryURL()) |
| 36 | + let result = try await store.query( |
| 37 | + MemoryQuery( |
| 38 | + namespace: DemoMemoryExamples.namespace, |
| 39 | + scopes: [DemoMemoryExamples.healthCoachScope], |
| 40 | + text: "direct blunt steps", |
| 41 | + limit: 4, |
| 42 | + maxCharacters: 800 |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + automaticPolicyMemoryResult = AutomaticPolicyMemoryDemoResult( |
| 47 | + threadID: thread.id, |
| 48 | + threadTitle: thread.title ?? "Memory Demo: Automatic Policy", |
| 49 | + prompt: DemoMemoryExamples.automaticPolicyPrompt, |
| 50 | + records: result.matches.map(\.record) |
| 51 | + ) |
| 52 | + threads = await runtime.threads() |
| 53 | + } catch { |
| 54 | + lastError = error.localizedDescription |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + func runAutomaticMemoryDemo() async { |
| 59 | + guard session != nil else { |
| 60 | + lastError = "Sign in before running automatic memory capture." |
| 61 | + return |
| 62 | + } |
| 63 | + guard !isRunningMemoryDemo else { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + isRunningMemoryDemo = true |
| 68 | + lastError = nil |
| 69 | + defer { |
| 70 | + isRunningMemoryDemo = false |
| 71 | + } |
| 72 | + |
| 73 | + do { |
| 74 | + let thread = try await runtime.createThread( |
| 75 | + title: "Memory Demo: Automatic Capture", |
| 76 | + memoryContext: DemoMemoryExamples.previewContext |
| 77 | + ) |
| 78 | + let capture = try await runtime.captureMemories( |
| 79 | + from: .text(DemoMemoryExamples.automaticCaptureTranscript), |
| 80 | + for: thread.id, |
| 81 | + options: .init( |
| 82 | + defaults: DemoMemoryExamples.guidedDefaults, |
| 83 | + maxMemories: 3 |
| 84 | + ) |
| 85 | + ) |
| 86 | + automaticMemoryResult = AutomaticMemoryDemoResult( |
| 87 | + threadID: thread.id, |
| 88 | + threadTitle: thread.title ?? "Memory Demo: Automatic Capture", |
| 89 | + capture: capture |
| 90 | + ) |
| 91 | + threads = await runtime.threads() |
| 92 | + } catch { |
| 93 | + lastError = error.localizedDescription |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + func runGuidedMemoryDemo() async { |
| 98 | + guard !isRunningMemoryDemo else { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + isRunningMemoryDemo = true |
| 103 | + lastError = nil |
| 104 | + defer { |
| 105 | + isRunningMemoryDemo = false |
| 106 | + } |
| 107 | + |
| 108 | + do { |
| 109 | + let writer = try await runtime.memoryWriter(defaults: DemoMemoryExamples.guidedDefaults) |
| 110 | + let record = try await writer.upsert(DemoMemoryExamples.guidedDraft) |
| 111 | + let diagnostics = try await writer.diagnostics() |
| 112 | + |
| 113 | + guidedMemoryResult = GuidedMemoryDemoResult( |
| 114 | + record: record, |
| 115 | + diagnostics: diagnostics |
| 116 | + ) |
| 117 | + } catch { |
| 118 | + lastError = error.localizedDescription |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + func runRawMemoryDemo() async { |
| 123 | + guard !isRunningMemoryDemo else { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + isRunningMemoryDemo = true |
| 128 | + lastError = nil |
| 129 | + defer { |
| 130 | + isRunningMemoryDemo = false |
| 131 | + } |
| 132 | + |
| 133 | + do { |
| 134 | + let store = try SQLiteMemoryStore(url: AgentDemoRuntimeFactory.defaultMemoryURL()) |
| 135 | + try await store.upsert( |
| 136 | + DemoMemoryExamples.rawRecord, |
| 137 | + dedupeKey: DemoMemoryExamples.rawRecord.dedupeKey ?? DemoMemoryExamples.rawRecord.id |
| 138 | + ) |
| 139 | + let diagnostics = try await store.diagnostics(namespace: DemoMemoryExamples.namespace) |
| 140 | + |
| 141 | + rawMemoryResult = RawMemoryDemoResult( |
| 142 | + record: DemoMemoryExamples.rawRecord, |
| 143 | + diagnostics: diagnostics |
| 144 | + ) |
| 145 | + } catch { |
| 146 | + lastError = error.localizedDescription |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + func runMemoryPreviewDemo() async { |
| 151 | + guard !isRunningMemoryDemo else { |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + isRunningMemoryDemo = true |
| 156 | + lastError = nil |
| 157 | + defer { |
| 158 | + isRunningMemoryDemo = false |
| 159 | + } |
| 160 | + |
| 161 | + do { |
| 162 | + let result: MemoryQueryResult |
| 163 | + var previewThreadID: String? |
| 164 | + var previewThreadTitle: String? |
| 165 | + |
| 166 | + if session != nil { |
| 167 | + let thread = try await runtime.createThread( |
| 168 | + title: "Memory Demo: Prompt Injection", |
| 169 | + memoryContext: DemoMemoryExamples.previewContext |
| 170 | + ) |
| 171 | + previewThreadID = thread.id |
| 172 | + previewThreadTitle = thread.title |
| 173 | + result = try await runtime.memoryQueryPreview( |
| 174 | + for: thread.id, |
| 175 | + request: UserMessageRequest(text: DemoMemoryExamples.previewRequestText) |
| 176 | + ) ?? MemoryQueryResult(matches: [], truncated: false) |
| 177 | + threads = await runtime.threads() |
| 178 | + } else { |
| 179 | + let store = try SQLiteMemoryStore(url: AgentDemoRuntimeFactory.defaultMemoryURL()) |
| 180 | + result = try await store.query( |
| 181 | + MemoryQuery( |
| 182 | + namespace: DemoMemoryExamples.namespace, |
| 183 | + scopes: DemoMemoryExamples.previewContext.scopes, |
| 184 | + text: DemoMemoryExamples.previewRequestText, |
| 185 | + limit: DemoMemoryExamples.previewBudget.maxItems, |
| 186 | + maxCharacters: DemoMemoryExamples.previewBudget.maxCharacters |
| 187 | + ) |
| 188 | + ) |
| 189 | + } |
| 190 | + |
| 191 | + memoryPreviewResult = MemoryPreviewDemoResult( |
| 192 | + threadID: previewThreadID, |
| 193 | + threadTitle: previewThreadTitle, |
| 194 | + requestText: DemoMemoryExamples.previewRequestText, |
| 195 | + result: result, |
| 196 | + renderedPrompt: DefaultMemoryPromptRenderer().render( |
| 197 | + result: result, |
| 198 | + budget: DemoMemoryExamples.previewBudget |
| 199 | + ) |
| 200 | + ) |
| 201 | + } catch { |
| 202 | + lastError = error.localizedDescription |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments