Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected for them.
| 2 | M2 — MCP server | **Complete** — 25 Sept, released as v0.2 |
| 3 | M3 — fil IDs, library folder, PDFs, text, citation context, FTS5 | **Complete** — 30 Oct, released as v0.3 ([`ARCHITECTURE_PHASE3.md`](ARCHITECTURE_PHASE3.md)) |
| 4 | M4 — retrieval (was "retrieval and answers"; D35) | **Complete** — 18 Nov, checked across five fields and at 39,000 passages (`VALIDATION.md`). 15 Nov: a retrieval test set first (`eval/retrieval`, 20 working and 20 locked questions). Spike 14 measured meaning search inside fil (pure Go: 10 of 20, 44 min to embed) and through Ollama (15 of 20, a 3.2 GB install); neither is built. fil writes no answers either: the user's assistant does, over MCP (D35). Instead search leaves out filler words and ranks papers by their best passages fused with title and abstract (schema v15): 13 → 15 of 20 in the first three on the working set (MRR 0.60 → 0.73), 15 of 20 on the locked set (MRR 0.60 → 0.68). The AI assistants screen shows its call log again (#35) |
| 5 | M5 — the desktop app (was "web interface"; D24) | **In progress** — the shell is Wails v2, in the same process (spike 13, D25); the desktop app comes first and the browser version later (D26). 2 Nov: `desktop/` opens a real window with the Library, Search, Citation map, Inbox and AI assistants screens, and the Reader with notes. The Papers list reaches the whole library, with sorting and filters by year, author and venue. The Citation map shows the whole library on one screen as well as the map around a paper. 3 Nov: Favourites, collections inside collections, and coloured labels, with several papers picked at once, a right-click menu and dragging (schema v11, D28). Bringing in a Zotero library through its local API, read-only, from the Inbox or `fil zotero` (schema v12, D29), or from a Zotero RDF export (`fil zotero --from`). Your own documents can be matched to OpenAlex afterwards, by an identifier or by choosing from a title search (`fil match`). The window has no frame on Windows: the page draws a title bar with File, Edit, View, Library, Go, Paper and Help menus, keyboard shortcuts, and a library switcher, and libraries can be made, opened, switched, moved and closed while the app runs (D30). 4 Nov: a paper's PDF can be downloaded from its page, and the papers one citation step from it, what it cites and what cites it, brought in without growing the rest of the library (D31). The paper shows its abstract. The citation map sizes papers by citations here or worldwide and filters them by citations, year, PDF and your own papers, fading or hiding the rest; a paper with no legal copy takes a PDF you have (D32). 6 Nov: Search takes the Papers list's filters, applied before the best passages are chosen, and the list's filters can be saved as views in the sidebar that update themselves (schema v13, D33). 7 Nov: citation groups, the library's topics found from its citations alone by Louvain clustering, suggested in the sidebar, on the Papers list and on the citation map, and made into collections only when asked; `fil groups` and the `citation_groups` tool (schema v14, D34). 19 Nov: search moved into the title bar and the Papers list shows one row per paper; papers can be deleted, their folders to the system's bin, remembered so that expanding does not bring them back (schema v16, D36) |
| 5 | M5 — the desktop app (was "web interface"; D24) | **In progress** — the shell is Wails v2, in the same process (spike 13, D25); the desktop app comes first and the browser version later (D26). 2 Nov: `desktop/` opens a real window with the Library, Search, Citation map, Inbox and AI assistants screens, and the Reader with notes. The Papers list reaches the whole library, with sorting and filters by year, author and venue. The Citation map shows the whole library on one screen as well as the map around a paper. 3 Nov: Favourites, collections inside collections, and coloured labels, with several papers picked at once, a right-click menu and dragging (schema v11, D28). Bringing in a Zotero library through its local API, read-only, from the Inbox or `fil zotero` (schema v12, D29), or from a Zotero RDF export (`fil zotero --from`). Your own documents can be matched to OpenAlex afterwards, by an identifier or by choosing from a title search (`fil match`). The window has no frame on Windows: the page draws a title bar with File, Edit, View, Library, Go, Paper and Help menus, keyboard shortcuts, and a library switcher, and libraries can be made, opened, switched, moved and closed while the app runs (D30). 4 Nov: a paper's PDF can be downloaded from its page, and the papers one citation step from it, what it cites and what cites it, brought in without growing the rest of the library (D31). The paper shows its abstract. The citation map sizes papers by citations here or worldwide and filters them by citations, year, PDF and your own papers, fading or hiding the rest; a paper with no legal copy takes a PDF you have (D32). 6 Nov: Search takes the Papers list's filters, applied before the best passages are chosen, and the list's filters can be saved as views in the sidebar that update themselves (schema v13, D33). 7 Nov: citation groups, the library's topics found from its citations alone by Louvain clustering, suggested in the sidebar, on the Papers list and on the citation map, and made into collections only when asked; `fil groups` and the `citation_groups` tool (schema v14, D34). 19 Nov: search moved into the title bar and the Papers list shows one row per paper; papers can be deleted, their folders to the system's bin, remembered so that expanding does not bring them back (schema v16, D36). 24 Nov: Reader tabs. Several papers stay open at once, one tab each, after a Library tab that holds the side menu and every other screen. Each tab keeps its page, zoom, side panel and find words; a citation on the page opens beside the paper it is cited in; Ctrl+Tab, Ctrl+W and Ctrl+Shift+T in the Go menu. The tabs are kept in the library's meta table, with no schema change, so a library opened again shows its own papers where they were left; only the four tabs seen last keep their PDF open |
| 6 | M6 — claim genealogy | Not started |

---
Expand Down
144 changes: 144 additions & 0 deletions internal/library/tabs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package library

import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"unicode/utf8"

"github.com/codevector-2003/filiation/internal/errs"
"github.com/codevector-2003/filiation/internal/identity"
"github.com/codevector-2003/filiation/internal/store"
)

// The desktop app keeps several papers open in the Reader at once, one tab
// each, beside the Library tab that holds every other screen. The tabs are
// kept in the library, so a library opened again shows its own papers where
// they were left, and another library shows its own.

// metaReaderTabs is the meta key the tabs are kept under, as JSON.
const metaReaderTabs = "reader_tabs"

// MaxReaderTabs is how many paper tabs are kept.
const MaxReaderTabs = 50

// ReaderTab is one paper open in the Reader, and where its reader was in it.
type ReaderTab struct {
ID string `json:"id"` // the paper's fil ID
Page int `json:"page"` // from 1
Zoom float64 `json:"zoom,omitempty"` // the page's scale; 0 leaves the Reader's own
Side string `json:"side,omitempty"` // notes, cited or details
Find string `json:"find,omitempty"` // the words in Find in this paper
}

// ReaderTabs is every paper tab, in the order shown, and which tab was in
// front: a paper's fil ID, or "" for the Library tab. Screen is the screen the
// Library tab was showing.
type ReaderTabs struct {
Tabs []ReaderTab `json:"tabs"`
Active string `json:"active"`
Screen string `json:"screen,omitempty"`
}

var readerSides = map[string]bool{"notes": true, "cited": true, "details": true}

// ReaderTabs returns the tabs kept for this library, leaving out any whose
// paper has gone or no longer has a PDF to show. Tabs that cannot be read
// back are forgotten rather than refused: they are a convenience, and the
// app must open without them.
func (l *Library) ReaderTabs(ctx context.Context) (ReaderTabs, error) {
raw, err := l.db.Meta(ctx, metaReaderTabs)
if err != nil {
return ReaderTabs{Tabs: []ReaderTab{}}, err
}
var kept ReaderTabs
if raw == "" || json.Unmarshal([]byte(raw), &kept) != nil {
return ReaderTabs{Tabs: []ReaderTab{}}, nil
}
kept = tidyTabs(kept)
out := ReaderTabs{Tabs: []ReaderTab{}, Screen: kept.Screen}
for _, t := range kept.Tabs {
ok, err := l.HasPDF(ctx, t.ID)
if err != nil {
return ReaderTabs{Tabs: []ReaderTab{}}, err
}
if ok {
out.Tabs = append(out.Tabs, t)
if t.ID == kept.Active {
out.Active = t.ID
}
}
}
return out, nil
}

// SaveReaderTabs keeps the tabs for the next time this library is opened.
func (l *Library) SaveReaderTabs(ctx context.Context, tabs ReaderTabs) error {
if len(tabs.Tabs) > MaxReaderTabs {
return fmt.Errorf("at most %d papers can be kept open: %w", MaxReaderTabs, errs.ErrInvalidInput)
}
for _, t := range tabs.Tabs {
if _, err := identity.NormaliseFilID(t.ID); err != nil {
return fmt.Errorf("%q is not a fil ID: %w", t.ID, errs.ErrInvalidInput)
}
}
raw, err := json.Marshal(tidyTabs(tabs))
if err != nil {
return err
}
return l.db.Tx(ctx, func(tx *store.Tx) error { return tx.SetMeta(ctx, metaReaderTabs, string(raw)) })
}

// tidyTabs puts the tabs in a shape the Reader can always open: one tab per
// paper, a page it can turn to, a side panel it has.
func tidyTabs(in ReaderTabs) ReaderTabs {
out := ReaderTabs{Tabs: []ReaderTab{}, Screen: shortText(in.Screen, 40)}
seen := map[string]bool{}
for _, t := range in.Tabs {
id, err := identity.NormaliseFilID(t.ID)
if err != nil || seen[id] || len(out.Tabs) == MaxReaderTabs {
continue
}
seen[id] = true
t.ID = id
t.Page = max(t.Page, 1)
if t.Zoom < 0.25 || t.Zoom > 5 {
t.Zoom = 0
}
if !readerSides[t.Side] {
t.Side = ""
}
t.Find = shortText(t.Find, 200)
out.Tabs = append(out.Tabs, t)
}
if active, err := identity.NormaliseFilID(in.Active); err == nil && seen[active] {
out.Active = active
}
return out
}

// HasPDF reports whether the library holds a PDF of the paper that is still
// on disk, as far as it knows.
func (l *Library) HasPDF(ctx context.Context, filID string) (bool, error) {
files, err := l.db.FilesOf(ctx, filID)
if err != nil {
return false, err
}
for _, f := range files {
if !f.Missing && strings.EqualFold(filepath.Ext(f.Path), ".pdf") {
return true, nil
}
}
return false, nil
}

// shortText trims s to at most n characters, never cutting one in half.
func shortText(s string, n int) string {
s = strings.TrimSpace(s)
if utf8.RuneCountInString(s) <= n {
return s
}
return strings.TrimSpace(string([]rune(s)[:n]))
}
92 changes: 92 additions & 0 deletions internal/library/tabs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package library

import (
"errors"
"testing"

"github.com/codevector-2003/filiation/internal/errs"
"github.com/codevector-2003/filiation/internal/store"
)

func TestReaderTabs(t *testing.T) {
t.Parallel()
l, ids, _ := viewsTest(t)
ctx := t.Context()

none, err := l.ReaderTabs(ctx)
if err != nil || len(none.Tabs) != 0 || none.Active != "" || none.Tabs == nil {
t.Fatalf("before any were kept = %+v, %v; want no tabs, as an empty list", none, err)
}

// The seed has no PDF, so a tab on it has nothing to show.
seed, err := l.Add(ctx, "10.7717/peerj.4375", AddOptions{})
if err != nil {
t.Fatal(err)
}
err = l.SaveReaderTabs(ctx, ReaderTabs{
Tabs: []ReaderTab{
{ID: ids[0], Page: 4, Zoom: 1.3, Side: "cited", Find: " hedgerow "},
{ID: ids[1], Page: 0, Zoom: 99, Side: "elsewhere"},
{ID: seed.Work.FilID, Page: 2},
{ID: ids[0], Page: 9}, // the same paper twice keeps the first
},
Active: ids[1],
Screen: "search",
})
if err != nil {
t.Fatal(err)
}
got, err := l.ReaderTabs(ctx)
if err != nil {
t.Fatal(err)
}
want := []ReaderTab{
{ID: ids[0], Page: 4, Zoom: 1.3, Side: "cited", Find: "hedgerow"},
{ID: ids[1], Page: 1},
}
if len(got.Tabs) != len(want) || got.Tabs[0] != want[0] || got.Tabs[1] != want[1] {
t.Fatalf("tabs = %+v, want %+v", got.Tabs, want)
}
if got.Active != ids[1] || got.Screen != "search" {
t.Errorf("active %q on screen %q, want %q on search", got.Active, got.Screen, ids[1])
}

// A paper deleted since is left out, and the Library tab comes to the front.
if _, err := l.DeletePapers(ctx, []string{ids[1]}); err != nil {
t.Fatal(err)
}
got, err = l.ReaderTabs(ctx)
if err != nil || len(got.Tabs) != 1 || got.Tabs[0].ID != ids[0] || got.Active != "" {
t.Fatalf("after deleting the paper in front = %+v, %v", got, err)
}
}

func TestSaveReaderTabsRefuses(t *testing.T) {
t.Parallel()
l, ids, _ := viewsTest(t)
ctx := t.Context()

if err := l.SaveReaderTabs(ctx, ReaderTabs{Tabs: []ReaderTab{{ID: "../papers", Page: 1}}}); !errors.Is(err, errs.ErrInvalidInput) {
t.Errorf("a tab that names no paper: %v, want ErrInvalidInput", err)
}
many := make([]ReaderTab, MaxReaderTabs+1)
for i := range many {
many[i] = ReaderTab{ID: ids[0], Page: 1}
}
if err := l.SaveReaderTabs(ctx, ReaderTabs{Tabs: many}); !errors.Is(err, errs.ErrInvalidInput) {
t.Errorf("%d tabs: %v, want ErrInvalidInput", len(many), err)
}
}

func TestReaderTabsForgetWhatCannotBeRead(t *testing.T) {
t.Parallel()
l, _, _ := viewsTest(t)
ctx := t.Context()
if err := l.db.Tx(ctx, func(tx *store.Tx) error { return tx.SetMeta(ctx, metaReaderTabs, "{not json") }); err != nil {
t.Fatal(err)
}
got, err := l.ReaderTabs(ctx)
if err != nil || len(got.Tabs) != 0 {
t.Fatalf("tabs kept as broken JSON = %+v, %v; want none and no error", got, err)
}
}
40 changes: 40 additions & 0 deletions internal/store/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package store

import (
"context"
"database/sql"
"errors"
"fmt"
)

// The meta table keeps the library's bookkeeping, one value per key: the
// schema version, and small things the screens remember, such as which papers
// were open in the Reader. A value is the caller's to read; the store only
// keeps it.

// Meta reads the value kept under key, "" when there is none.
func (db *DB) Meta(ctx context.Context, key string) (string, error) {
var v sql.NullString
err := db.read.QueryRowContext(ctx, `SELECT value FROM meta WHERE key = ?;`, key).Scan(&v)
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("store: read %s: %w", key, err)
}
return v.String, nil
}

// SetMeta keeps value under key, replacing what was there. The schema version
// is the migrations' alone to change.
func (tx *Tx) SetMeta(ctx context.Context, key, value string) error {
if key == metaSchemaVersion {
return fmt.Errorf("store: %s is set by migration only", key)
}
if _, err := tx.tx.ExecContext(ctx,
`INSERT INTO meta(key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value;`,
key, value); err != nil {
return fmt.Errorf("store: write %s: %w", key, err)
}
return nil
}
28 changes: 28 additions & 0 deletions internal/store/meta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package store

import "testing"

func TestMeta(t *testing.T) {
db := migrated(t)
ctx := t.Context()

got, err := db.Meta(ctx, "reader_tabs")
if err != nil || got != "" {
t.Fatalf("Meta before any write = %q, %v; want \"\", nil", got, err)
}
for _, want := range []string{`{"a":1}`, `{"a":2}`} {
if err := db.Tx(ctx, func(tx *Tx) error { return tx.SetMeta(ctx, "reader_tabs", want) }); err != nil {
t.Fatal(err)
}
if got, err := db.Meta(ctx, "reader_tabs"); err != nil || got != want {
t.Errorf("Meta = %q, %v; want %q", got, err, want)
}
}

if err := db.Tx(ctx, func(tx *Tx) error { return tx.SetMeta(ctx, metaSchemaVersion, "1") }); err == nil {
t.Error("SetMeta changed the schema version")
}
if v, _ := db.Meta(ctx, metaSchemaVersion); v != "16" {
t.Errorf("schema version = %q after a refused write, want 16", v)
}
}
28 changes: 26 additions & 2 deletions internal/ui/assets/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,6 @@ dialog form { display: flex; flex-direction: column; gap: 10px; }
/* The Reader */
#screen-reader { flex-direction: column; }
.reader-bar { flex: none; min-height: 52px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.reader-back { height: 34px; padding: 0 12px; border: 0; border-radius: 6px; background: transparent; }
.reader-back:hover { background: var(--quote); }
.reader-title { max-width: 260px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--muted); }
.sep { width: 1px; height: 24px; background: var(--line); }
.tools { display: flex; gap: 2px; padding: 3px; border-radius: 8px; background: var(--side); }
Expand Down Expand Up @@ -879,3 +877,29 @@ body[data-platform="darwin"] .tb-menus { display: none; }
.mf-group .key { flex: none; }
.mf-group-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mf-note { font-size: 12px; line-height: 1.45; }

/* Reader tabs: the Library tab, then one tab for each paper open. */
.tabstrip { flex: none; height: 34px; display: flex; align-items: flex-end; gap: 2px; padding: 0 8px; background: #E1DFD9; border-bottom: 1px solid var(--line); overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tabstrip::-webkit-scrollbar { display: none; }
.tab { position: relative; flex: none; height: 29px; display: flex; align-items: center; border: 1px solid transparent; border-bottom: 0; border-radius: 7px 7px 0 0; background: transparent; color: var(--ink-2); }
.tab:hover:not(.on) { background: #D8D6CF; }
.tab.on { height: 30px; margin-bottom: -1px; border-color: var(--line); background: var(--panel); color: var(--ink); font-weight: 600; }
.tab.lib { gap: 8px; padding: 0 14px 0 11px; }
.tab.lib.on { background: var(--side); }
.tab.lib svg, .tab-go svg { width: 14px; height: 14px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.5; }
.tab.paper { flex: 0 1 200px; min-width: 96px; }
.tab.paper.dragging { opacity: 0.6; }
.tab-go { flex: 1; min-width: 0; height: 100%; display: flex; align-items: center; gap: 7px; padding: 0 0 0 11px; border: 0; background: transparent; color: inherit; font-weight: inherit; text-align: left; }
.tab-go svg { color: var(--muted); }
.tab.on .tab-go svg { color: var(--accent); }
.tab-t { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.tab-pg { flex: none; font-size: 11px; font-weight: 400; color: var(--muted); }
.tab-x { flex: none; width: 20px; height: 20px; margin: 0 5px 0 3px; padding: 0; border: 0; border-radius: 4px; background: transparent; color: var(--muted); display: grid; place-items: center; }
.tab-x svg { width: 8px; height: 8px; stroke: currentColor; stroke-width: 1.6; }
.tab-x:hover { background: var(--line-2); color: var(--ink); }
.tab.paper:not(.on):not(:hover):not(:focus-within) .tab-x { visibility: hidden; }
.tab-all { position: sticky; right: 0; margin-left: auto; align-self: center; flex: none; height: 26px; display: flex; align-items: center; gap: 6px; padding: 0 9px; border: 0; border-radius: 6px; background: #E1DFD9; color: var(--muted); font-size: 12px; }
.tab-all:hover { background: #D8D6CF; color: var(--ink); }
.tab-all svg { width: 10px; height: 10px; fill: none; stroke: currentColor; stroke-width: 1.4; }
.chip.open-tab { background: var(--selected); color: var(--accent); }
.pop-item.on { font-weight: 600; }
Loading
Loading