~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0

Grain's page pages its galleries

Merged chadtmiller.com asked to merge into main from chadtmiller.com/hud:grain/paged-galleries
crates/hud-core/src/ui/document.rs+1−1
@@ -1223,7 +1223,7 @@ mod tests {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
}
crates/hud-core/src/ui/fields.rs+2−2
@@ -357,7 +357,7 @@ mod tests {
inline: None,
lexicon: Some(&lex),
handles: Some(&handles),
paged: &[],
paged: Vec::new(),
me: None,
people: None,
};
@@ -453,7 +453,7 @@ mod tests {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
};
crates/hud-core/src/ui/layout.rs+1−1
@@ -94,7 +94,7 @@ mod tests {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
}
crates/hud-core/src/ui/media.rs+8−8
@@ -17,7 +17,7 @@
//! subtitleOpen?, duration, art }]`; `src` the audio, null where nothing
//! may play ("not playable"), `duration` in seconds.
//!
//! `page` as a `List`'s: items bound to `items` or `records`, which a page
//! `page` as a `List`'s (`paged`): items that are a page's, which a page
//! more grows.
use super::*;
@@ -31,10 +31,10 @@ fn items<'a>(p: &'a Value, scope: &'a Value) -> R<&'a Vec<Value>> {
}
}
/// `{c, p: {page?}, items}`: `page` when its items are a page's, as `list` says.
fn listing(c: &str, p: &Value, drawn: Vec<Value>) -> Value {
/// `{c, p: {page?}, items}`: `page` when its items are a page's (`paged`).
fn listing(c: &str, p: &Value, scope: &Value, ctx: &Ctx, drawn: Vec<Value>) -> Value {
let mut o = Map::new();
if matches!(p["items"]["$"].as_str(), Some("items" | "records")) {
if paged(p, scope, ctx) {
o.insert("page".into(), true.into());
}
let mut out = make(c, o, vec![]);
@@ -91,7 +91,7 @@ pub(super) fn gallery(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
"ratio": ratio,
}));
}
let mut out = listing("Gallery", p, drawn);
let mut out = listing("Gallery", p, scope, ctx, drawn);
if p["style"].as_str() == Some("stack") {
out["p"]["style"] = "stack".into();
}
@@ -140,7 +140,7 @@ pub(super) fn covers(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
}
drawn.push(o);
}
let mut out = listing("Covers", p, drawn);
let mut out = listing("Covers", p, scope, ctx, drawn);
if p["style"].as_str() == Some("tiles") {
out["p"]["style"] = "tiles".into();
}
@@ -183,7 +183,7 @@ pub(super) fn player(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
}
drawn.push(o);
}
Ok(Some(listing("Player", p, drawn)))
Ok(Some(listing("Player", p, scope, ctx, drawn)))
}
#[cfg(test)]
@@ -203,7 +203,7 @@ mod tests {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
}
crates/hud-core/src/ui/mod.rs+26−13
@@ -178,10 +178,10 @@ pub struct Ctx<'a> {
pub lexicon: Option<&'a Value>,
/// The verified handle of each DID the data names, for `Fields`' accounts.
pub handles: Option<&'a Value>,
/// A records view's query joins: a List of the answer of one that reads
/// a page's records (`{"$root": "joins.<as>…"}`) is the page's, as one
/// of the records is.
pub paged: &'a [crate::view::QueryJoin],
/// The query joins, by `as`, whose answer is the page's, as the records
/// are: a records view's that read a page's records, an app page's
/// that pages it. A listing of one's answer is the page list (`paged`).
pub paged: Vec<&'a str>,
/// The signed-in person's DID, when the call was made signed in: their
/// own messages are `mine`, and only then may they send.
pub me: Option<&'a str>,
@@ -358,6 +358,25 @@ fn is_root_binding(v: &Value) -> bool {
.is_some_and(|o| o.len() == 1 && o.contains_key("$root"))
}
/// Whether a listing's items are the page's, which a page more grows: bound
/// to `items` or `records`, or to the answer of a query join that is the
/// page's (`ctx.paged`), by `{"$root": "joins.<as>…"}` or, in the root
/// scope itself, `{"$": "joins.<as>…"}`.
fn paged(p: &Value, scope: &Value, ctx: &Ctx) -> bool {
let bound = p["items"]["$"].as_str();
if matches!(bound, Some("items" | "records")) {
return true;
}
let joined = match p["items"]["$root"].as_str() {
Some(path) => Some(path),
None => bound.filter(|_| std::ptr::eq(scope, ctx.root)),
};
joined
.and_then(|path| path.strip_prefix("joins."))
.map(|rest| rest.split('.').next().unwrap_or(rest))
.is_some_and(|name| ctx.paged.contains(&name))
}
fn paths(b: &Value) -> R<Vec<Path>> {
let list: Vec<&Value> = match &b["$"] {
Value::Array(a) => a.iter().collect(),
@@ -1068,13 +1087,7 @@ fn list(n: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
let items = whose(p, items, ctx.me)?;
let items = kept(p, items)?;
let bound = p["items"]["$"].as_str();
// Of a join that reads the page's records: `joins.<as>…`.
let joined = p["items"]["$root"]
.as_str()
.and_then(|path| path.strip_prefix("joins."))
.map(|rest| rest.split('.').next().unwrap_or(rest));
let page = matches!(bound, Some("items" | "records"))
|| joined.is_some_and(|name| ctx.paged.iter().any(|j| j.name == name && j.per_page()));
let page = paged(p, scope, ctx);
let fallback = json!({ "$": "uri" });
let mut drawn = Vec::new();
for item in &items {
@@ -1507,7 +1520,7 @@ fn messages(n: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
.len()
> 2;
o.insert("group".into(), many.into());
if matches!(p["items"]["$"].as_str(), Some("items" | "records")) {
if paged(p, scope, ctx) {
o.insert("page".into(), true.into());
}
if p["send"].is_object() && ctx.me.is_some() {
@@ -1538,7 +1551,7 @@ mod tests {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
}
crates/hud-core/src/ui/thread.rs+1−1
@@ -136,7 +136,7 @@ mod tests {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
}
crates/hud-core/src/tools.rs+102−9
@@ -177,7 +177,9 @@ pub fn definitions(asked: Option<&str>, view_uri: &str) -> Vec<Value> {
schema(
json!({ "domain": { "type": "string",
"description": "The app's domain, as bsky.app or knotless.dev." },
"actor": actor }),
"actor": actor,
"cursor": { "type": "string",
"description": "The cursor a previous call returned, for the next page." } }),
&["domain"],
),
true,
@@ -623,7 +625,7 @@ fn profile_head(structured: &Value) -> Vec<Value> {
inline: None,
lexicon: None,
handles: None,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
};
@@ -822,10 +824,21 @@ fn page_scope(
}
/// An app page view's layout resolved over its main scope (`page_scope`),
/// as `_meta.ui`, titled with the app's name.
/// as `_meta.ui`, titled with the app's name; `next` the page more, where
/// the join that pages it (`pages`) answered a cursor.
fn page_ui(v: &View, scope: &Value, me: Option<&str>) -> Value {
let layout = &v.doc["page"]["layout"];
let mut ui = resolved_ui(v, true, layout, scope, None, None, me, (None, None));
let next = v
.page
.as_ref()
.and_then(|p| p.pages.as_deref())
.and_then(|name| scope["joins"][name]["cursor"].as_str())
.filter(|c| !c.is_empty())
.map(|cursor| {
json!({ "name": "describe_app", "args": {
"domain": scope["domain"], "actor": scope["actor"]["did"], "cursor": cursor } })
});
let mut ui = resolved_ui(v, true, layout, scope, None, next, me, (None, None));
ui["title"] = scope["name"].clone();
ui
}
@@ -1284,13 +1297,14 @@ impl Hud {
/// (`actor`) or else the signed-in one; else the generic page
/// (`app_ui`): where to go in it, made the person's where signed in;
/// with `actor`, that account's part in it, its profile's sections for
/// the app asked as `profile_sections` asks them.
/// the app asked as `profile_sections` asks them. With `cursor`, an app
/// page's page more: the join that pages it alone, asked from there.
async fn describe_app_tool(
&self,
arguments: Option<&Value>,
account: Option<&Account>,
) -> Result<Output, String> {
let args = Args::of(arguments, &["domain", "actor"])?;
let args = Args::of(arguments, &["domain", "actor", "cursor"])?;
let given = args.required("domain")?;
let domain = crate::icons::checked_domain(given, true)
.ok_or_else(|| format!("Not an app's domain: {}", given.trim()))?;
@@ -1301,6 +1315,14 @@ impl Hud {
let mut structured =
json!({ "domain": domain, "name": name, "icon": apps::icon(origin, &domain) });
let actor = args.text("actor")?.filter(|a| !a.trim().is_empty());
let more = match args.text("cursor")?.filter(|c| !c.is_empty()) {
None => None,
Some(c) => Some(
view::for_domain(&domain)
.and_then(|v| v.page.as_ref()?.more(c))
.ok_or_else(|| format!("{name}'s page has no page more"))?,
),
};
if let Some(v) = view::for_domain(&domain) {
let whose = match actor {
Some(a) => {
@@ -1311,7 +1333,10 @@ impl Hud {
};
if let Some((did, handle)) = whose {
let mut scope = page_scope(structured, (&did, handle.as_deref()), me, &entries);
let queries = v.page.as_ref().map_or(&[][..], |p| &p.queries[..]);
let queries = match &more {
Some(m) => std::slice::from_ref(m),
None => v.page.as_ref().map_or(&[][..], |p| &p.queries[..]),
};
let joined = self
.query_joins(
Some(queries.iter()),
@@ -1321,6 +1346,12 @@ impl Hud {
)
.await
.unwrap_or_default();
if let Some(m) = more
.as_ref()
.filter(|m| joined.get(&m.name).is_none_or(Value::is_null))
{
return Err(format!("{name}'s page more did not answer ({})", m.method));
}
let text = app_text(&domain, &name, &entries, None);
let text = summary::with_said(text, queries, &joined);
scope["joins"] = Value::Object(joined);
@@ -1332,6 +1363,11 @@ impl Hud {
});
}
}
if more.is_some() {
return Err(format!(
"{name}'s page more is of an account: name one, or sign in"
));
}
let here = match actor {
None => None,
Some(actor) => {
@@ -2146,7 +2182,7 @@ fn generic_records_ui(
inline: None,
lexicon,
handles,
paged: &[],
paged: Vec::new(),
me: None,
people: None,
};
@@ -2216,7 +2252,16 @@ fn view_ctx<'a>(v: &'a View, trusted: bool, root: &'a Value) -> ui::Ctx<'a> {
},
lexicon: None,
handles: None,
paged: v.list.as_ref().map_or(&[], |m| m.queries.as_slice()),
paged: match (&v.list, &v.page) {
(Some(m), _) => m
.queries
.iter()
.filter(|j| j.per_page())
.map(|j| j.name.as_str())
.collect(),
(None, Some(p)) => p.pages.as_deref().into_iter().collect(),
(None, None) => Vec::new(),
},
me: None,
// A bundled view's app, where it has a page: an account it names
// opens there.
@@ -3119,6 +3164,54 @@ mod tests {
assert!(view::for_domain("bsky.chat").is_none());
}
/// Grain's page pages its galleries (`pages`): their Covers is the page
/// list, its `next` `describe_app` from the cursor; a page more, its
/// join alone answered, draws those galleries as the page list, and
/// ends where they have no cursor.
#[test]
fn grain_pages_its_galleries() {
fn page_lists(n: &Value) -> Vec<&Value> {
let mut found: Vec<&Value> =
(n["p"]["page"] == true).then_some(n).into_iter().collect();
for k in n["k"].as_array().into_iter().flatten() {
found.extend(page_lists(k));
}
found
}
let did = "did:plc:dan";
let v = view::for_domain("grain.social").unwrap();
let gallery = |n: usize| {
json!({ "uri": format!("at://{did}/social.grain.gallery/{n}"), "title": format!("Gallery {n}"),
"items": [], "creator": { "did": did, "handle": "dan.example" } })
};
let app = json!({ "domain": "grain.social", "name": "Grain",
"icon": apps::icon("https://hud.example", "grain.social") });
let mut scope = page_scope(app, (did, Some("dan.example")), None, &[]);
scope["joins"] = json!({
"profile": { "did": did, "handle": "dan.example", "galleryCount": 20 },
"galleries": { "items": (0..18).map(gallery).collect::<Vec<_>>(), "cursor": "c18" },
});
let page = page_ui(v, &scope, None);
assert_eq!(ui::check(&page), Ok(()), "{page:#}");
assert_eq!(
page["next"],
json!({ "name": "describe_app",
"args": { "domain": "grain.social", "actor": did, "cursor": "c18" } })
);
let lists = page_lists(&page["root"]);
assert_eq!(lists.len(), 1, "{page:#}");
assert_eq!(lists[0]["c"], "Covers");
assert_eq!(lists[0]["items"].as_array().map(Vec::len), Some(18));
// The page more: the galleries alone, the last of them.
scope["joins"] =
json!({ "galleries": { "items": (18..20).map(gallery).collect::<Vec<_>>() } });
let more = page_ui(v, &scope, None);
assert!(more.get("next").is_none(), "{more:#}");
let lists = page_lists(&more["root"]);
assert_eq!(lists.len(), 1, "{more:#}");
assert_eq!(lists[0]["items"][1]["title"], "Gallery 19");
}
/// An account a bundled view names opens as its page in the view's app,
/// where that app has one (so its mentions too, by the tree's `people`);
/// in an app with none, or an inline view, as its profile across apps.
crates/hud-core/src/view.rs+49−8
@@ -289,13 +289,27 @@ pub struct QueryMode {
}
/// An app page view's one mode: the queries it joins on the page's main
/// scope (`PAGE_SCOPE`), its layout, in `doc`, and what a #tag in its
/// scope (`PAGE_SCOPE`), its layout, in `doc`, what a #tag in its
/// app's text opens (`{ query, params, service? }`, its params read from
/// `{ tag }`), where the app has a search for one.
/// `{ tag }`), where the app has a search for one, and the join that pages
/// the page (`pages`, its `as`): its answer's `cursor` the page more.
#[derive(Clone, Debug, PartialEq)]
pub struct PageMode {
pub queries: Vec<QueryJoin>,
pub tag: Option<Value>,
pub pages: Option<String>,
}
impl PageMode {
/// The join that pages the page, asking for the page at `cursor`.
pub fn more(&self, cursor: &str) -> Option<QueryJoin> {
let name = self.pages.as_deref()?;
let mut j = self.queries.iter().find(|j| j.name == name)?.clone();
j.params.retain(|(k, _)| k != "cursor");
j.params
.push(("cursor".into(), Param::Literal(cursor.into())));
Some(j)
}
}
/// A view, checked: of a collection's records (`list`, `record`), of a
@@ -869,9 +883,10 @@ fn parse_view(p: &mut Problems, doc: &Value, trusted: bool) -> Option<View> {
})
}
/// An app page view's `domain` and `page`: `{ join?, layout }`, its joins
/// query joins alone, their params reading the page's main scope
/// (`PAGE_SCOPE`). A bundled view's alone: a page is the app's own.
/// An app page view's `domain` and `page`: `{ join?, layout, tag?, pages? }`,
/// its joins query joins alone, their params reading the page's main scope
/// (`PAGE_SCOPE`), `pages` one of them by its `as`. A bundled view's alone:
/// a page is the app's own.
fn parse_app_page(
p: &mut Problems,
o: &Map<String, Value>,
@@ -899,12 +914,12 @@ fn parse_app_page(
let Some(v) = o.get("page") else {
p.add(
"view.page",
"an app page needs its page: { join?, layout, tag? }",
"an app page needs its page: { join?, layout, tag?, pages? }",
);
return None;
};
let at = "page";
let po = p.object(v, at, &["join", "layout", "tag"])?;
let po = p.object(v, at, &["join", "layout", "tag", "pages"])?;
let queries = match po.get("join") {
Some(j) => joins(p, j, "page.join", 1, Some((PAGE_SCOPE, false))).1,
None => Vec::new(),
@@ -914,7 +929,22 @@ fn parse_app_page(
if let Some(t) = &tag {
open_query(p, t, "page.tag");
}
Some((domain?, PageMode { queries, tag }))
let pages = match po.get("pages") {
None => None,
Some(Value::String(s)) if queries.iter().any(|j| &j.name == s) => Some(s.clone()),
Some(_) => {
p.add("page.pages", "the as of one of the page's joins");
None
}
};
Some((
domain?,
PageMode {
queries,
tag,
pages,
},
))
}
/// The most characters an app's name takes.
@@ -5183,6 +5213,16 @@ mod tests {
);
seen.push(format!("{} {}", b.id, j.name));
}
// A page that pages asks its join for the page at a cursor.
if let Some(more) = b.view.page.as_ref().and_then(|p| p.more("c")) {
assert_eq!(
query_join_problems(&more, &docs),
Vec::<String>::new(),
"{}",
b.id
);
seen.push(format!("{} {} pages", b.id, more.name));
}
}
assert_eq!(
seen,
@@ -5196,6 +5236,7 @@ mod tests {
"bsky-profile feeds",
"grain-app profile",
"grain-app galleries",
"grain-app galleries pages",
"grain-gallery-view comments",
"knotless-app issues",
"knotless-app pulls",
docs/app-pages.md+6−2
@@ -11,7 +11,7 @@ A bundled view can draw an app's page instead. That's an app page view.
## App page views
An app page view names the app's `domain` (`"knotless.dev"`) in place of a collection or method. It also gives `app`, `description` and the `service` its joins call. It has one mode, `page`, shaped as `{ "join"?: [query joins], "layout" }`.
An app page view names the app's `domain` (`"knotless.dev"`) in place of a collection or method. It also gives `app`, `description` and the `service` its joins call. It has one mode, `page`, shaped as `{ "join"?: [query joins], "layout", "pages"? }`.
### The page's scope
@@ -26,6 +26,10 @@ The main scope is `{ domain, name, icon, actor, me, mine, entries, joins }`. It
Its [query joins](views.md) read `domain`, `actor`, `me` and the rest of that scope, as any mode's do. HUD skips a join whose binding doesn't resolve.
### Pages
`pages` names one of the page's joins by its `as`. A `List`, `Covers`, `Gallery` or `Player` of that join's answer is the page's page list. Where the answer has a `cursor`, the tree's `next` is `describe_app` with that `cursor`. That call runs the join alone, from the cursor, and the clients add its items to the list.
### When it's drawn
`describe_app` draws that view's layout where the app has one and there's an account to draw it for. With neither an actor nor anyone signed in, it draws the generic page.
@@ -48,7 +52,7 @@ More on knotless's views is in [knotless](knotless.md).
### Grain
Grain's page (`grain-app`) is its profile. It shows the account's avatar large beside its galleries, followers and following, its name, handle and bio, and on one's own page where to go (For you, Following, Recent, Galleries, Notifications). It shows its 18 latest galleries as 3:4 tiles, each opening the gallery. The data comes from Grain's AppView (`getProfile` and the `actor` feed of `getFeed`).
Grain's page (`grain-app`) is its profile. It shows the account's avatar large beside its galleries, followers and following, its name, handle and bio, and on one's own page where to go (For you, Following, Recent, Galleries, Notifications). It shows its galleries as 3:4 tiles, newest first, each opening the gallery, 18 a page (`pages`). The data comes from Grain's AppView (`getProfile` and the `actor` feed of `getFeed`).
### Bluesky
docs/tools.md+1−1
@@ -14,7 +14,7 @@ HUD serves its reads, writes and workspace as MCP tools. Seven of them read and
| Tool | Input | View |
| --- | --- | --- |
| `describe_repo` | `actor` (handle, DID, or `me`) | yes |
| `describe_app` | `domain` (as `bsky.app`), `actor` (optional: handle, DID, or `me`) | yes |
| `describe_app` | `domain` (as `bsky.app`), `actor` (optional: handle, DID, or `me`), `cursor` (an app page's page more) | yes |
| `describe_collection` | `actor`, `collection` | no |
| `describe_method` | `method` (a query's NSID) | no |
| `list_records` | `actor`, `collection`, `limit` (25, ≤100 · a view's `pageSize`), `cursor`, `view` | yes |
schemas/place.hud.view.json+2−1
views/grain-app.json+1−0