-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.ts
More file actions
283 lines (263 loc) · 9.63 KB
/
Copy pathsummarizer.ts
File metadata and controls
283 lines (263 loc) · 9.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* Session summarization — Phase 3 deterministic stand-in.
*
* `extractSessionSummary` converts a `SessionResult` (+ the session's tool-call
* history) into a persisted `SessionSummary`, and `buildRollingSummary` turns a
* list of session summaries into a compact markdown recap so a session can
* carry context forward WITHOUT keeping the infinite raw history (the
* AgentMemory equivalent).
*
* This is a DETERMINISTIC stand-in: the summary text is the session's final
* answer (or failure reason), files/commands are derived from the tool-call
* history, and facts are extracted with keyword heuristics. An LLM-based
* extractor (the spec's Phase 3.1 "summarize session" endpoint) can be plugged
* in later via the `SummarizeWithLlm` hook — NO LLM is required for the
* default path or for tests.
*/
import type { ChatMessage, LlmClient, SessionResult } from "../engine/types.js"
import type { MemoryFact, SessionSummary } from "./types.js"
/** Maximum number of facts extracted from one session result. */
export const MAX_FACTS_PER_SESSION = 8
/** Maximum length of an extracted fact's content. */
export const MAX_FACT_CHARS = 500
/** Default number of sessions kept in a rolling summary. */
export const DEFAULT_ROLLING_MAX_ENTRIES = 10
/**
* Optional LLM-based summarization hook (Phase 3.1 placeholder).
*
* `(llmClient, transcript) => Promise<summaryText>`. Not wired into the
* deterministic path; documented as the swap-in point for the future
* AIRunner/UwUChat summarize-session endpoint.
*/
export type SummarizeWithLlm = (llmClient: LlmClient, transcript: string) => Promise<string>
/**
* Default summarizer: returns the transcript unchanged (deterministic, no
* LLM). Callers that want the LLM hook may implement/replace this.
*/
export class NoopSummarizer {
summarize(transcript: string): string {
return transcript
}
}
export interface ExtractSummaryOptions {
taskText: string
mode?: string
project: string
/**
* Full session message history (system/user/assistant/tool) — used to
* derive `filesTouched` + `commandsRun` from the executed tool calls.
* When omitted, those lists are empty (callers that have the history
* should always pass it).
*/
messages?: ChatMessage[]
}
/**
* Build a `SessionSummary` from a session result + its tool history.
*
* Deterministic, no LLM required:
* - summary = attempt_completion result text, or the final assistant text,
* or the failure error message;
* - outcome = success | failure from `result.status`;
* - filesTouched = paths from write_to_file / read_file (and cwd of
* execute_command) calls in the message history;
* - commandsRun = command strings from execute_command calls;
* - facts = heuristic extraction from the result text (see
* `extractFacts`), capped at MAX_FACTS_PER_SESSION.
*/
export function extractSessionSummary(result: SessionResult, options: ExtractSummaryOptions): SessionSummary {
const outcome: "success" | "failure" = result.status === "success" ? "success" : "failure"
const summaryText =
(result.result ?? "").trim() || (result.error ?? "").trim() || "Task completed without a final message"
const { filesTouched, commandsRun } = deriveToolActivity(options.messages ?? [])
return {
id: `summary_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`,
project: options.project,
task: options.taskText.trim() || "(untitled task)",
mode: options.mode,
outcome,
summary: summaryText.slice(0, 4000),
facts: extractFacts(summaryText, options),
filesTouched,
commandsRun,
createdAt: new Date().toISOString(),
}
}
/**
* Deterministic fact extraction from a session result text.
*
* Heuristic: split into lines, keep lines containing fact-ish keywords, map
* them to a kind by keyword, cap content at MAX_FACT_CHARS, dedupe by content,
* cap at MAX_FACTS_PER_SESSION. This is the Phase 3 stand-in for the LLM-based
* extractor the spec's summarize endpoint will run.
*/
export function extractFacts(text: string, options: ExtractSummaryOptions): MemoryFact[] {
const source = `session:${options.taskText.trim().slice(0, 80) || "extracted"}`
const facts: MemoryFact[] = []
const seen = new Set<string>()
for (const rawLine of text.split("\n")) {
const line = rawLine.replace(/^[-*\d.\s)\]]+\s*/, "").trim()
if (line.length < 12) {
continue
}
const kind = classifyLine(line)
if (!kind) {
continue
}
const content = line.slice(0, MAX_FACT_CHARS)
if (seen.has(content)) {
continue
}
seen.add(content)
facts.push({
id: "", // assigned/project-scoped by the store on addFact
project: options.project,
kind,
content,
tags: keywordTags(line),
source,
createdAt: new Date().toISOString(),
})
if (facts.length >= MAX_FACTS_PER_SESSION) {
break
}
}
return facts
}
/**
* Keyword → kind mapping (first match wins; priority: failure > decision >
* convention > knowledge). "Things that didn't work" map to `failure`.
*/
const FACT_KEYWORD_RULES: Array<{ kind: MemoryFact["kind"]; regex: RegExp }> = [
{
kind: "failure",
regex: /never|bug|break|broke|failed|doesn'?t work|didn'?t work|does not work|did not work|\bfix(ed|ing|es)?\b/i,
},
{
kind: "decision",
regex: /decision|decided|chose|migration|todo|xxx|we (will|should|now) (use|keep|adopt)/i,
},
{ kind: "convention", regex: /rule|convention|always/i },
{ kind: "knowledge", regex: /note|remember/i },
]
export function classifyLine(line: string): MemoryFact["kind"] | null {
for (const rule of FACT_KEYWORD_RULES) {
if (rule.regex.test(line)) {
return rule.kind
}
}
return null
}
/** Tags derived from the matched keywords in a line (deduped, lowercased). */
function keywordTags(line: string): string[] {
const words = line.toLowerCase().match(/[a-z]{3,}/g) ?? []
const interesting = words.filter((w) =>
["never", "always", "rule", "convention", "bug", "fix", "migration", "decision", "note", "todo", "xxx"].includes(w),
)
return [...new Set(interesting)]
}
/**
* Derive filesTouched + commandsRun from the executed tool-call history.
*
* The PRIMARY source is assistant messages' `tool_calls` (the args the model
* emitted) — the loop stores the executor's RESULT text in `tool` messages,
* not the args, so they can't be parsed from there. A `tool`-message fallback
* (content that is itself JSON args) is kept for robustness.
*/
function deriveToolActivity(messages: ChatMessage[]): { filesTouched: string[]; commandsRun: string[] } {
const filesTouched: string[] = []
const commandsRun: string[] = []
const seenFiles = new Set<string>()
const seenCommands = new Set<string>()
const recordFile = (p: string): void => {
const key = p.trim()
if (key !== "" && !seenFiles.has(key)) {
seenFiles.add(key)
filesTouched.push(key)
}
}
const recordCommand = (c: string): void => {
const key = c.trim()
if (key !== "" && !seenCommands.has(key)) {
seenCommands.add(key)
commandsRun.push(key)
}
}
for (const message of messages) {
if (message.role === "assistant" && message.tool_calls) {
for (const call of message.tool_calls) {
const name = call.function?.name
let args: Record<string, unknown> = {}
try {
args = JSON.parse(call.function?.arguments ?? "") as Record<string, unknown>
} catch {
// Malformed args — best effort: skip this call.
}
recordToolActivity(name, args, recordFile, recordCommand)
}
} else if (message.role === "tool") {
let args: Record<string, unknown> = {}
try {
args = JSON.parse(message.content ?? "") as Record<string, unknown>
} catch {
continue // tool results carry no args (real loop shape)
}
recordToolActivity(message.name, args, recordFile, recordCommand)
}
}
return { filesTouched, commandsRun }
}
function recordToolActivity(
name: string | undefined,
args: Record<string, unknown>,
recordFile: (p: string) => void,
recordCommand: (c: string) => void,
): void {
if (name === "write_to_file" || name === "read_file") {
if (typeof args["path"] === "string") {
recordFile(args["path"])
}
} else if (name === "execute_command") {
if (typeof args["command"] === "string") {
recordCommand(args["command"])
}
if (typeof args["cwd"] === "string") {
recordFile(args["cwd"])
}
}
}
/**
* Compact markdown recap of the last `maxEntries` sessions (most recent
* first) — the AgentMemory equivalent: a session can carry this forward as
* context instead of the full raw history.
*
* Deterministic ordering: sessions are sorted by `createdAt` desc, then the
* most recent `maxEntries` are rendered.
*/
export function buildRollingSummary(sessions: SessionSummary[], maxEntries = DEFAULT_ROLLING_MAX_ENTRIES): string {
const sorted = [...sessions].sort((a, b) => (a.createdAt < b.createdAt ? 1 : a.createdAt > b.createdAt ? -1 : 0))
const recent = sorted.slice(0, maxEntries)
if (recent.length === 0) {
return "No prior sessions recorded for this project."
}
const lines: string[] = []
lines.push(`### Rolling session recap (last ${recent.length} session${recent.length === 1 ? "" : "s"})`)
for (const session of recent) {
lines.push("")
lines.push(`**${session.createdAt}** — outcome: ${session.outcome}${session.mode ? ` (mode: ${session.mode})` : ""}`)
lines.push(`Task: ${session.task}`)
lines.push(`Summary: ${session.summary}`)
if (session.filesTouched.length > 0) {
lines.push(`Files touched: ${session.filesTouched.join(", ")}`)
}
if (session.commandsRun.length > 0) {
lines.push(`Commands run: ${session.commandsRun.join("; ")}`)
}
if (session.facts.length > 0) {
lines.push("Key facts:")
for (const fact of session.facts) {
lines.push(`- [${fact.kind}] ${fact.content}`)
}
}
}
return lines.join("\n")
}