-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathprompt.ts
More file actions
58 lines (50 loc) · 1.91 KB
/
prompt.ts
File metadata and controls
58 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { MemoryStore, isExpired } from "./store"
import { MEMORY_DEFAULT_INJECTION_BUDGET, type MemoryBlock } from "./types"
import { Telemetry } from "@/altimate/telemetry"
export namespace MemoryPrompt {
export function formatBlock(block: MemoryBlock): string {
const tagsStr = block.tags.length > 0 ? ` [${block.tags.join(", ")}]` : ""
const expiresStr = block.expires ? ` (expires: ${block.expires})` : ""
let result = `### ${block.id} (${block.scope})${tagsStr}${expiresStr}\n${block.content}`
if (block.citations && block.citations.length > 0) {
const citationLines = block.citations.map((c) => {
const lineStr = c.line ? `:${c.line}` : ""
const noteStr = c.note ? ` — ${c.note}` : ""
return `- \`${c.file}${lineStr}\`${noteStr}`
})
result += "\n\n**Sources:**\n" + citationLines.join("\n")
}
return result
}
export async function inject(budget: number = MEMORY_DEFAULT_INJECTION_BUDGET): Promise<string> {
const blocks = await MemoryStore.listAll()
if (blocks.length === 0) return ""
const header = "## Altimate Memory\n\nThe following memory blocks were saved from previous sessions:\n"
let result = header
let used = header.length
let injectedCount = 0
const scopesSeen = new Set<string>()
for (const block of blocks) {
if (isExpired(block)) continue
const formatted = formatBlock(block)
const needed = formatted.length + 2
if (used + needed > budget) break
result += "\n" + formatted + "\n"
used += needed
injectedCount++
scopesSeen.add(block.scope)
}
if (injectedCount > 0) {
Telemetry.track({
type: "memory_injection",
timestamp: Date.now(),
session_id: Telemetry.getContext().sessionId,
block_count: injectedCount,
total_chars: used,
budget,
scopes_used: [...scopesSeen],
})
}
return result
}
}