diff --git a/apps/albedo/src/albedo/history/conversation.gleam b/apps/albedo/src/albedo/history/conversation.gleam index c40b8a06e..69e947179 100644 --- a/apps/albedo/src/albedo/history/conversation.gleam +++ b/apps/albedo/src/albedo/history/conversation.gleam @@ -31,6 +31,7 @@ import albedo/usage/usage import albedo_bytes/scalars import albedo_model as types import albedo_sdk/context_block +import albedo_sdk/conversation_text import albedo_sdk/history import albedo_sdk/image_fit import albedo_sdk/images as image_store @@ -1874,6 +1875,74 @@ fn range_entries( ) } +/// Read the visible text attributable to a human submission or assistant +/// reply. Page bounds include rows with no eligible text. +pub fn conversation_page( + db: sqlight.Connection, + snapshot: transcript.Snapshot, + after: Int, + limit: Int, +) -> Result(conversation_text.Page, String) { + let limit = int.clamp(limit, 1, history_page_rows) + use sizes <- result.try(range_sizes_sql.after( + db:, + session: snapshot.session, + upper: snapshot.upper, + boundary: after, + page_size: limit + 1, + )) + let admitted = + history_prefix(list.take(sizes, limit), fn(row) { row.bytes }, 0, 0) + use rows <- result.try(range_entries_sql.after( + db:, + session: snapshot.session, + upper: snapshot.upper, + boundary: after, + page_size: admitted, + )) + let through = case list.last(rows), admitted < list.length(sizes) { + Ok(last), True -> last.seq + _, _ -> snapshot.upper + } + use messages <- result.map(list.try_map(rows, conversation_message)) + conversation_text.Page(through, option.values(messages)) +} + +fn conversation_message( + row: range_entries_sql.RangeEntryRow, +) -> Result(Option(conversation_text.Message), String) { + case row.display { + Some(saved) -> { + let d = operations.decode_display(saved) + let text = operations.human_text(d.operation_id, d.source, d.title_text) + Ok( + option.map(text, conversation_text.Message( + row.seq, + row.timestamp, + conversation_text.Human, + _, + )), + ) + } + None -> { + // Text projection never consumes image bytes. + use input <- result.map( + codec.unpack(row.payload, fn(_) { Error(Nil) }) + |> result.replace_error("invalid saved transcript item"), + ) + option.map( + events.visible_assistant_text(input), + conversation_text.Message( + row.seq, + row.timestamp, + conversation_text.Assistant, + _, + ), + ) + } + } +} + /// Read a bounded chronological page through a captured append boundary. /// Later commits cannot leak into a reset assembled from that snapshot. pub fn read_range( diff --git a/apps/albedo/src/albedo/storage/core_store.gleam b/apps/albedo/src/albedo/storage/core_store.gleam index aa978795d..0283520c9 100644 --- a/apps/albedo/src/albedo/storage/core_store.gleam +++ b/apps/albedo/src/albedo/storage/core_store.gleam @@ -2,6 +2,7 @@ //// with (`sdk/core_tables`). Each area's operations live with that area's //// schema and migrations; this module only gathers them. +import albedo/history/conversation import albedo/session/family import albedo/storage/images import albedo/storage/mail @@ -16,7 +17,7 @@ pub fn tables() -> core_tables.Tables { core_tables.Tables( sessions: session_rows.operations(), family: family.operations(), - transcript: transcript_rows.operations(), + transcript: transcript_rows.operations(conversation.conversation_page), mail: mail.operations(), images: images.operations(), requests: requests.operations(), diff --git a/apps/albedo/src/albedo/storage/transcript_rows.gleam b/apps/albedo/src/albedo/storage/transcript_rows.gleam index 15cce7ced..dda54caba 100644 --- a/apps/albedo/src/albedo/storage/transcript_rows.gleam +++ b/apps/albedo/src/albedo/storage/transcript_rows.gleam @@ -7,6 +7,7 @@ import albedo/generated/sql/storage/transcript_rows/window_sql import albedo/generated/sql/storage/transcript_rows_sql +import albedo_sdk/conversation_text import albedo_sdk/core_tables.{type Hits, type Window, Hits} import albedo_sdk/transcript.{ type Snapshot, type SourceStats, Snapshot, SourceStats, @@ -21,7 +22,10 @@ import gleam/string import sqlight /// The operations `core_store.tables` gathers. -pub fn operations() -> core_tables.Transcript { +pub fn operations( + conversation_page: fn(sqlight.Connection, Snapshot, Int, Int) -> + Result(conversation_text.Page, String), +) -> core_tables.Transcript { core_tables.Transcript( snapshot:, source_stats:, @@ -31,6 +35,7 @@ pub fn operations() -> core_tables.Transcript { span:, image_fits:, sources:, + conversation_page:, user_payload:, uploads:, newest:, diff --git a/apps/albedo/test/albedo/composition/extension_capability_test.gleam b/apps/albedo/test/albedo/composition/extension_capability_test.gleam index d520b8ea9..20253c82c 100644 --- a/apps/albedo/test/albedo/composition/extension_capability_test.gleam +++ b/apps/albedo/test/albedo/composition/extension_capability_test.gleam @@ -986,6 +986,9 @@ fn function_calls(world: World) -> List(Call) { reads("albedo_sdk/history.rows_before", fn(h, s) { outcome(history.rows_before(h, forged(s), 1_000_000)) }), + reads("albedo_sdk/history.conversation_page", fn(h, s) { + outcome(history.conversation_page(h, forged(s), 0, 10)) + }), reads("albedo_sdk/history.fold_sources", fn(h, s) { outcome( history.fold_sources(h, forged(s), 0, 1_000_000, Nil, fn(_, _) { diff --git a/apps/albedo/test/albedo/history/conversation_test.gleam b/apps/albedo/test/albedo/history/conversation_test.gleam index 5b40cf51e..fa8eba151 100644 --- a/apps/albedo/test/albedo/history/conversation_test.gleam +++ b/apps/albedo/test/albedo/history/conversation_test.gleam @@ -5,6 +5,7 @@ import albedo/generated/sql/storage/migrations/conversation_columns_sql import albedo/history/conversation import albedo/runtime import albedo/session/family +import albedo/session/inbox import albedo/session/session_submission import albedo/session/turn import albedo/storage/core_store @@ -13,6 +14,7 @@ import albedo/storage/session_rows import albedo/support/scratch import albedo/support/store_fixture import albedo_model as types +import albedo_sdk/conversation_text import albedo_sdk/history import albedo_sdk/session_info import albedo_sdk/storage @@ -232,7 +234,12 @@ pub fn transcript_timestamps_migrate_without_invention_and_roundtrip_test() -> N scratch.remove_database(path) } -pub fn human_prompt_excludes_generated_submissions_test() -> Nil { +/// A schedule and an actual prompt both enter the transcript in the user +/// role. Only the identified admission belongs to the human peer. +pub fn conversation_text_follows_admissions_across_empty_pages_test() -> Nil { + let assert Ok(ledger) = store_fixture.start(":memory:", core_store.tables()) + let assert Ok(_) = conversation.initialise(ledger) + let assert Ok(_) = conversation.create(ledger, session("s")) let human = turn.Submission( "/skill (daemon annotation)", @@ -269,6 +276,65 @@ pub fn human_prompt_excludes_generated_submissions_test() -> Nil { ) let submissions = [schedule, human, command] session_submission.human_prompt(submissions) |> should.equal(Some("/skill")) + list.each(submissions, fn(item) { + let assert Ok(_) = inbox.post(ledger, "s", item.client_id, item) + Nil + }) + let assert Ok(at) = + conversation.commit_operations( + ledger, + "s", + session_submission.inputs(submissions), + session_info.Model, + None, + [], + session_submission.commits(submissions, 0), + [], + session_submission.kinds(submissions, 0), + "run", + "/tmp", + [], + ) + let assert Ok(image) = + types.image("image/png", "iVBORw0KGgoAAAANSUhEUgAAAAIAAAAD", 2, 3, 24) + let assert Ok(reply_at) = + conversation.commit( + ledger, + "s", + [types.ToolOutput("tool", "secret", [image]), types.Assistant("reply")], + session_info.Idle, + ) + let db = storage.host(ledger) + let assert Ok(snapshot) = history.snapshot(db, "s") + let assert Ok(_) = + conversation.commit( + ledger, + "s", + [types.Assistant("later")], + session_info.Idle, + ) + history.conversation_page(db, snapshot, 0, 1) + |> should.equal(Ok(conversation_text.Page(1, []))) + history.conversation_page(db, snapshot, 1, 100) + |> should.equal( + Ok( + conversation_text.Page(5, [ + conversation_text.Message( + 2, + Some(at), + conversation_text.Human, + "/skill", + ), + conversation_text.Message( + 5, + Some(reply_at), + conversation_text.Assistant, + "reply", + ), + ]), + ), + ) + store.close(ledger) } /// A transfer's staging session stays hidden until activation and remains diff --git a/packages/albedo_sdk/src/albedo_sdk/capability.gleam b/packages/albedo_sdk/src/albedo_sdk/capability.gleam index 6c8e553fc..e430148e3 100644 --- a/packages/albedo_sdk/src/albedo_sdk/capability.gleam +++ b/packages/albedo_sdk/src/albedo_sdk/capability.gleam @@ -217,6 +217,7 @@ pub const session_functions = [ #("albedo_sdk/history", "latest_user_seq", History), #("albedo_sdk/history", "rows_before", History), #("albedo_sdk/history", "fold_sources", History), + #("albedo_sdk/history", "conversation_page", History), #("albedo_sdk/history", "uploaded_image", History), #("albedo_sdk/history", "seqs", History), #("albedo_sdk/history", "spans", History), diff --git a/packages/albedo_sdk/src/albedo_sdk/conversation_text.gleam b/packages/albedo_sdk/src/albedo_sdk/conversation_text.gleam new file mode 100644 index 000000000..6cc716b8e --- /dev/null +++ b/packages/albedo_sdk/src/albedo_sdk/conversation_text.gleam @@ -0,0 +1,17 @@ +//// Human-authored text and visible assistant replies in a committed transcript. + +import gleam/option.{type Option} + +pub type Speaker { + Human + Assistant +} + +pub type Message { + Message(seq: Int, timestamp: Option(Int), speaker: Speaker, text: String) +} + +/// Advance to `through` even when the page contains no eligible messages. +pub type Page { + Page(through: Int, messages: List(Message)) +} diff --git a/packages/albedo_sdk/src/albedo_sdk/core_tables.gleam b/packages/albedo_sdk/src/albedo_sdk/core_tables.gleam index 52b0be93a..2475f7e10 100644 --- a/packages/albedo_sdk/src/albedo_sdk/core_tables.gleam +++ b/packages/albedo_sdk/src/albedo_sdk/core_tables.gleam @@ -19,6 +19,7 @@ //// and `sdk/session_identity`, so the SDK modules that hand them on can //// name them. +import albedo_sdk/conversation_text import albedo_sdk/session_identity.{type Address, type Member, type Stub} import albedo_sdk/session_info.{ type ContextUse, type Info, type Names, type Standing, type Summary, @@ -147,6 +148,8 @@ pub type Transcript { /// The lower bound is exclusive and the upper bound is inclusive. sources: fn(Connection, String, Int, Int, Int) -> Result(List(SourceRow), String), + conversation_page: fn(Connection, Snapshot, Int, Int) -> + Result(conversation_text.Page, String), /// The payload of the user row at the sequence, if there is one. user_payload: fn(Connection, String, Int) -> Result(Option(BitArray), String), diff --git a/packages/albedo_sdk/src/albedo_sdk/history.gleam b/packages/albedo_sdk/src/albedo_sdk/history.gleam index 679c6afa0..f648246cd 100644 --- a/packages/albedo_sdk/src/albedo_sdk/history.gleam +++ b/packages/albedo_sdk/src/albedo_sdk/history.gleam @@ -7,6 +7,7 @@ //// snapshot it shared before. import albedo_model as types +import albedo_sdk/conversation_text import albedo_sdk/core_tables import albedo_sdk/image_fit import albedo_sdk/images @@ -161,6 +162,24 @@ pub fn seqs( storage.reading(ledger, transcript_of(ledger).seqs(_, session, after, limit)) } +/// Read attributed conversation text through the captured upper boundary. +/// `after` is exclusive. Resume at the returned page's `through`, even when +/// it only examined tool calls, internal notes or unattributed legacy rows. +pub fn conversation_page( + ledger: storage.Storage, + snapshot: Snapshot, + after: Int, + limit: Int, +) -> Result(conversation_text.Page, String) { + use _ <- result.try(permitted(ledger, "conversation_page", snapshot.session)) + storage.snapshot(ledger, transcript_of(ledger).conversation_page( + _, + snapshot, + after, + limit, + )) +} + /// When each of `ranges` of `session`'s transcript was written, in one read /// snapshot: for a range `#(first, last)` of sequences, the timestamp of /// the first row at or after `first` and of the last row at or before diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index 0206e10ac..6fb6a963f 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -608,9 +608,10 @@ The typed core reads that extensions use are: some sessions are `importing`; `describe`, its names with its workspace; and the discovery reads `find`, `all`, and `occupied`. - [`sdk/history`](../packages/albedo_sdk/src/albedo_sdk/history.gleam): besides - the history reads, `seqs` for a page of a session's transcript rows, `spans` - for when ranges of them were written, and `context_use` for its pin and the - prompt tokens of its latest request. + the history reads, `conversation_page` for bounded pages of identified human + submissions and visible assistant replies, `seqs` for a page of a session's + transcript rows, `spans` for when ranges of them were written, and + `context_use` for its pin and the prompt tokens of its latest request. - [`sdk/mail`](../packages/albedo_sdk/src/albedo_sdk/mail.gleam): `statuses_in` for the letters an extension posted, by ID, `queued_in` for the letters still waiting for some sessions, `undelivered_in` and `delivered_in` for a page of @@ -1030,12 +1031,13 @@ or a `transcript.Snapshot` or kernel cell that names one, and read that session's stored history: its `sessions` row (`history.get`, `history.acp_marker`, `history.context_use`), its transcript (`history.snapshot`, `source_stats`, `latest_user_seq`, `rows_before`, -`fold_sources`, `uploaded_image`, `seqs`, `spans`, `tool.transcript_read`, -`tool.source_page`, `search.newest`, `page`, `page_without`, `running_call`, and -`transcript_grep`), its request history (`request_history.last_turn`), and its -kernel journal (`journal.get`, `find_call`, `draft`, `prepare`, `unfinished`, -`recent`, `trace`, and `saved_trace`). Each one calls `storage.permit_session` -with the session it reads. The rule is: +`fold_sources`, `conversation_page`, `uploaded_image`, `seqs`, `spans`, +`tool.transcript_read`, `tool.source_page`, `search.newest`, `page`, +`page_without`, `running_call`, and `transcript_grep`), its request history +(`request_history.last_turn`), and its kernel journal (`journal.get`, +`find_call`, `draft`, `prepare`, `unfinished`, `recent`, `trace`, and +`saved_trace`). Each one calls `storage.permit_session` with the session it +reads. The rule is: - a host handle reads any session; - a handle bound to a session reads that session without a capability;