Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #32.
This commit is contained in:
+127
-35
@@ -61,7 +61,7 @@ type SnippetCandidate struct {
|
||||
|
||||
func NewSnippetExtractor() *SnippetExtractor {
|
||||
return &SnippetExtractor{
|
||||
Version: "snippet-extract/v1",
|
||||
Version: "snippet-extract/v2",
|
||||
IncludeBash: os.Getenv("KNOX_SNIPPET_BASH") != "0",
|
||||
}
|
||||
}
|
||||
@@ -82,12 +82,27 @@ type ocPart struct {
|
||||
|
||||
type toolInput struct {
|
||||
FilePath string `json:"filePath"`
|
||||
Path string `json:"path"` // v2 write/edit input
|
||||
Content string `json:"content"`
|
||||
NewString string `json:"newString"`
|
||||
Command string `json:"command"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// file is the target path of a write/edit. opencode v2 sends "path", v1 sent
|
||||
// "filePath"; both are accepted so either transcript shape works.
|
||||
func (in toolInput) file() string {
|
||||
if in.FilePath != "" {
|
||||
return in.FilePath
|
||||
}
|
||||
return in.Path
|
||||
}
|
||||
|
||||
// isShellTool: opencode v1 named the shell tool "bash", v2 also emits "shell".
|
||||
func isShellTool(name string) bool {
|
||||
return name == "bash" || name == "shell"
|
||||
}
|
||||
|
||||
type toolMeta struct {
|
||||
Exit *int `json:"exit"`
|
||||
Output string `json:"output"`
|
||||
@@ -129,6 +144,101 @@ func (e *SnippetExtractor) ExtractAll() ([]SnippetCandidate, error) {
|
||||
dirOf[s.ID] = s.Directory
|
||||
}
|
||||
|
||||
parts, err := loadToolParts(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out []SnippetCandidate
|
||||
for _, s := range sessions {
|
||||
if len(parts[s.ID]) == 0 {
|
||||
continue
|
||||
}
|
||||
out = append(out, e.processSession(s.ID, dirOf[s.ID], parts[s.ID])...)
|
||||
}
|
||||
|
||||
return dedupPrimaries(out), nil
|
||||
}
|
||||
|
||||
// loadToolParts returns session_id -> ordered tool/patch parts.
|
||||
//
|
||||
// opencode 2.0.24 stores tool calls inside session_message.data as a content
|
||||
// array and stopped writing the v1 part/message tables. v2 is read first and
|
||||
// wins per session; v1 is only consulted for sessions v2 does not cover, so a
|
||||
// migrated session is never counted twice.
|
||||
func loadToolParts(db *sql.DB) (map[string][]ocPart, error) {
|
||||
out := make(map[string][]ocPart)
|
||||
|
||||
has, err := hasTables(db, "session_message")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if has {
|
||||
if err := appendToolPartsV2(db, out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
has, err = hasTables(db, "part", "message")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if has {
|
||||
if err := appendToolPartsV1(db, out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func hasTables(db *sql.DB, names ...string) (bool, error) {
|
||||
in, args := inClause(names)
|
||||
var n int
|
||||
if err := db.QueryRow(
|
||||
`SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name IN `+in, args...).Scan(&n); err != nil {
|
||||
return false, fmt.Errorf("check tables: %w", err)
|
||||
}
|
||||
return n == len(names), nil
|
||||
}
|
||||
|
||||
// appendToolPartsV2 reads $.content[] of assistant messages. Tool calls carry
|
||||
// their name at $.name (v1 used $.tool) and their result at $.state.content.
|
||||
func appendToolPartsV2(db *sql.DB, out map[string][]ocPart) error {
|
||||
rows, err := db.Query(
|
||||
`SELECT m.session_id,
|
||||
m.id,
|
||||
COALESCE(json_extract(m.data,'$.model.id'),''),
|
||||
COALESCE(m.time_created,0),
|
||||
COALESCE(json_extract(j.value,'$.name'),''),
|
||||
COALESCE(json_extract(j.value,'$.state.status'),''),
|
||||
COALESCE(json_extract(j.value,'$.state.input'),''),
|
||||
COALESCE(json_extract(j.value,'$.state.metadata'),''),
|
||||
COALESCE(json_extract(j.value,'$.state.error'),'')
|
||||
FROM session_message m, json_each(m.data,'$.content') j
|
||||
WHERE m.type='assistant'
|
||||
AND json_extract(j.value,'$.type')='tool'
|
||||
ORDER BY m.session_id, m.seq, CAST(j.key AS INTEGER)`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("query session_message: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var p ocPart
|
||||
if err := rows.Scan(&p.Session, &p.Message, &p.Model, &p.Time, &p.Tool,
|
||||
&p.Status, &p.Input, &p.Metadata, &p.Err); err != nil {
|
||||
return err
|
||||
}
|
||||
p.Kind = "tool"
|
||||
out[p.Session] = append(out[p.Session], p)
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// appendToolPartsV1 reads the pre-migration part/message tables, skipping any
|
||||
// session v2 already covered.
|
||||
func appendToolPartsV1(db *sql.DB, out map[string][]ocPart) error {
|
||||
rows, err := db.Query(
|
||||
`SELECT p.session_id,
|
||||
COALESCE(p.message_id,''),
|
||||
@@ -145,40 +255,22 @@ func (e *SnippetExtractor) ExtractAll() ([]SnippetCandidate, error) {
|
||||
WHERE json_extract(p.data,'$.type') IN ('tool','patch')
|
||||
ORDER BY p.session_id, p.time_created`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query parts: %w", err)
|
||||
return fmt.Errorf("query parts: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var (
|
||||
out []SnippetCandidate
|
||||
curSession string
|
||||
events []ocPart
|
||||
)
|
||||
flush := func() {
|
||||
if curSession == "" || len(events) == 0 {
|
||||
return
|
||||
}
|
||||
out = append(out, e.processSession(curSession, dirOf[curSession], events)...)
|
||||
events = events[:0]
|
||||
}
|
||||
for rows.Next() {
|
||||
var p ocPart
|
||||
if err := rows.Scan(&p.Session, &p.Message, &p.Model, &p.Time, &p.Kind,
|
||||
&p.Tool, &p.Status, &p.Input, &p.Metadata, &p.Err, &p.PatchHash); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
if p.Session != curSession {
|
||||
flush()
|
||||
curSession = p.Session
|
||||
if _, covered := out[p.Session]; covered {
|
||||
continue
|
||||
}
|
||||
events = append(events, p)
|
||||
out[p.Session] = append(out[p.Session], p)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flush()
|
||||
|
||||
return dedupPrimaries(out), nil
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func (e *SnippetExtractor) processSession(sessionID, directory string, events []ocPart) []SnippetCandidate {
|
||||
@@ -209,10 +301,10 @@ func (e *SnippetExtractor) processSession(sessionID, directory string, events []
|
||||
if ev.Kind != "tool" || ev.Status != "completed" {
|
||||
continue
|
||||
}
|
||||
if ev.Tool != "write" && ev.Tool != "edit" && ev.Tool != "bash" {
|
||||
if ev.Tool != "write" && ev.Tool != "edit" && !isShellTool(ev.Tool) {
|
||||
continue
|
||||
}
|
||||
if ev.Tool == "bash" && !e.IncludeBash {
|
||||
if isShellTool(ev.Tool) && !e.IncludeBash {
|
||||
continue
|
||||
}
|
||||
if len(cands) >= snippetCodeCap {
|
||||
@@ -229,10 +321,10 @@ func (e *SnippetExtractor) processSession(sessionID, directory string, events []
|
||||
var code, filePath, language, desc string
|
||||
switch ev.Tool {
|
||||
case "write":
|
||||
code, filePath = in.Content, in.FilePath
|
||||
code, filePath = in.Content, in.file()
|
||||
case "edit":
|
||||
code, filePath = in.NewString, in.FilePath
|
||||
case "bash":
|
||||
code, filePath = in.NewString, in.file()
|
||||
case "bash", "shell":
|
||||
if meta.Exit == nil || *meta.Exit != 0 {
|
||||
continue
|
||||
}
|
||||
@@ -279,12 +371,12 @@ func (e *SnippetExtractor) processSession(sessionID, directory string, events []
|
||||
// command is a *technique*, not junk, even when it's a one-liner tied to
|
||||
// a specific host. Only code files get the import/stub junk verdict.
|
||||
kind, junk := "code", classifyJunk(code, language)
|
||||
if ev.Tool == "bash" {
|
||||
if isShellTool(ev.Tool) {
|
||||
kind, junk = "technique", trivialProbe(in.Command)
|
||||
}
|
||||
|
||||
title := snippetTitle(ev.Tool, filePath)
|
||||
if ev.Tool == "bash" {
|
||||
if isShellTool(ev.Tool) {
|
||||
switch {
|
||||
case desc != "":
|
||||
title = truncate(desc, 60)
|
||||
@@ -374,10 +466,10 @@ func dedupPrimaries(in []SnippetCandidate) []SnippetCandidate {
|
||||
|
||||
func snippetTitle(tool, filePath string) string {
|
||||
base := filepath.Base(filePath)
|
||||
switch tool {
|
||||
case "bash":
|
||||
switch {
|
||||
case isShellTool(tool):
|
||||
return "command"
|
||||
case "write":
|
||||
case tool == "write":
|
||||
return "new file: " + base
|
||||
default:
|
||||
return "edit " + base
|
||||
|
||||
Reference in New Issue
Block a user