-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.py
More file actions
62 lines (42 loc) · 1.81 KB
/
Copy pathsummarizer.py
File metadata and controls
62 lines (42 loc) · 1.81 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
"""The summarizer interface (feat-012 summaries) — injectable like PatternJudge.
``BedrockClaudeSummarizer`` is the live adapter; ``ScriptedSummarizer`` keeps the
bottom-up enricher (ordering, embedding, budget, idempotency) deterministic in
CI with no model.
"""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass, field
from typing import Protocol, runtime_checkable
from pydantic import BaseModel
@dataclass
class FileContext:
path: str
symbols: list[tuple[str, str]] = field(default_factory=list) # (name, signature)
imports: list[str] = field(default_factory=list)
class Summary(BaseModel):
text: str
model: str = ""
@runtime_checkable
class Summarizer(Protocol):
async def summarize_file(self, ctx: FileContext, max_words: int) -> Summary: ...
async def summarize_repo(
self, repo: str, file_summaries: list[tuple[str, str]], max_words: int
) -> Summary: ...
@property
def cost_usd(self) -> float: ...
FileFn = Callable[[FileContext], str]
class ScriptedSummarizer:
"""Deterministic summarizer for tests. The default derives a stable string
from the context; pass ``fn`` to script file summaries."""
def __init__(self, fn: FileFn | None = None) -> None:
self._fn = fn or (lambda ctx: f"summary of {ctx.path} ({len(ctx.symbols)} symbols)")
self._cost = 0.0
async def summarize_file(self, ctx: FileContext, max_words: int) -> Summary:
return Summary(text=self._fn(ctx), model="scripted")
async def summarize_repo(
self, repo: str, file_summaries: list[tuple[str, str]], max_words: int
) -> Summary:
return Summary(text=f"repo {repo}: {len(file_summaries)} files", model="scripted")
@property
def cost_usd(self) -> float:
return self._cost