diff --git a/apps/albedo/src/albedo/http/http_sessions.gleam b/apps/albedo/src/albedo/http/http_sessions.gleam index c9d4b2041..dc9678243 100644 --- a/apps/albedo/src/albedo/http/http_sessions.gleam +++ b/apps/albedo/src/albedo/http/http_sessions.gleam @@ -1009,7 +1009,7 @@ fn prepare_input( images, Some(input_id), Some(input_id), - None, + Some(text), attached, None, )) @@ -1105,7 +1105,7 @@ fn prepare_input( [], Some(input_id), Some(input_id), - None, + Some(skills.activation_display(activation)), [], Some(skills.activation_loaded(activation)), )) diff --git a/apps/albedo/src/albedo/model_loop/loop.gleam b/apps/albedo/src/albedo/model_loop/loop.gleam index b0a7a9c7f..ed931e54d 100644 --- a/apps/albedo/src/albedo/model_loop/loop.gleam +++ b/apps/albedo/src/albedo/model_loop/loop.gleam @@ -160,6 +160,8 @@ pub type Loop { /// compacts once for it without replaying the refused request, and a /// second refusal ends it. overflowed: Bool, + /// Original human text admitted for this turn. + human_prompt: Option(String), ) } @@ -1277,7 +1279,7 @@ fn prompted( -> { use hook.Answer(verdict, _, given) <- result.try(hooked( state, - hook.Prompted(text), + hook.Prompted(text, state.human_prompt), )) case verdict.block { Some(reason) -> { diff --git a/apps/albedo/src/albedo/session/session_run.gleam b/apps/albedo/src/albedo/session/session_run.gleam index 386ddf38a..0c067f742 100644 --- a/apps/albedo/src/albedo/session/session_run.gleam +++ b/apps/albedo/src/albedo/session/session_run.gleam @@ -623,6 +623,7 @@ pub fn start( repetition.new(), turn.isolated_event(state.active_submissions) != None, False, + session_submission.human_prompt(state.active_submissions), ) let pid = process.spawn_unlinked(fn() { diff --git a/apps/albedo/src/albedo/session/session_submission.gleam b/apps/albedo/src/albedo/session/session_submission.gleam index 7afc6ea09..7dde0eef5 100644 --- a/apps/albedo/src/albedo/session/session_submission.gleam +++ b/apps/albedo/src/albedo/session/session_submission.gleam @@ -14,6 +14,7 @@ import albedo_sdk/transcript import gleam/list import gleam/option.{None, Some} import gleam/result +import gleam/string pub fn input(submission: Submission) -> types.Input { // The kernel posts its notices already framed as notices. @@ -73,6 +74,23 @@ fn display_source(source: turn.Source) -> operations.DisplaySource { } } +/// The opening submissions' original human text in admission order. +pub fn human_prompt(submissions: List(Submission)) -> option.Option(String) { + let texts = + list.filter_map(submissions, fn(submission) { + operations.human_text( + submission.operation_id, + display_source(submission.source), + submission.title_text, + ) + |> option.to_result(Nil) + }) + case texts { + [] -> None + _ -> Some(string.join(texts, "\n\n")) + } +} + pub fn display(submission: Submission) -> operations.Display { operations.Display( submission.display, diff --git a/apps/albedo/src/albedo/storage/operations.gleam b/apps/albedo/src/albedo/storage/operations.gleam index 0e2b51855..7591e5c38 100644 --- a/apps/albedo/src/albedo/storage/operations.gleam +++ b/apps/albedo/src/albedo/storage/operations.gleam @@ -18,6 +18,7 @@ import gleam/json import gleam/list import gleam/option.{type Option, None, Some} import gleam/result +import gleam/string import sqlight /// Loads this module, whose code names the constructors a submission's attached @@ -367,6 +368,26 @@ pub type Display { ) } +/// The submitter's own text. ACP resources can add model-only content and +/// older editor displays cannot separate it from what the person typed. +pub fn human_text( + operation: Option(String), + source: DisplaySource, + original: Option(String), +) -> Option(String) { + let text = case operation, source, original { + Some(_), DisplayChat, Some(text) | Some(_), DisplayAcp, Some(text) -> + Some(text) + _, _, _ -> None + } + option.then(text, fn(value) { + case string.trim(value) { + "" -> None + _ -> Some(value) + } + }) +} + /// The transcript offset is absent for continuations, which append no row. pub type Commit { Commit(id: String, offset: Option(Int), display: Display) diff --git a/apps/albedo/test/albedo/composition/extension_storage_test.gleam b/apps/albedo/test/albedo/composition/extension_storage_test.gleam index bc7bd6b24..46ec2f6bb 100644 --- a/apps/albedo/test/albedo/composition/extension_storage_test.gleam +++ b/apps/albedo/test/albedo/composition/extension_storage_test.gleam @@ -624,7 +624,7 @@ pub fn hooks_see_their_own_extensions_handle_bound_to_the_caller_test() -> Nil { list.each(["s", "t"], fn(session) { hook.ask( composition.hooks(composed, session, "/w/" <> session), - hook.Prompted(""), + hook.Prompted("", None), ).verdict |> should.equal(hook.proceed) let wanted = fn(name, tables, sessions) { @@ -702,7 +702,7 @@ pub fn hooks_are_asked_in_extension_order_static_or_managed_test() -> Nil { ) let hooks = composition.hooks(composed, "s", "/tmp") list.map(hooks, fn(item) { item.0 }) |> should.equal(["first", "second"]) - let answer = hook.ask(hooks, hook.Prompted("")) + let answer = hook.ask(hooks, hook.Prompted("", None)) answer.verdict.block |> should.equal(Some("first")) answer.by |> should.equal("first") composition.close(composed) diff --git a/apps/albedo/test/albedo/history/conversation_test.gleam b/apps/albedo/test/albedo/history/conversation_test.gleam index c304d70b4..5b40cf51e 100644 --- a/apps/albedo/test/albedo/history/conversation_test.gleam +++ b/apps/albedo/test/albedo/history/conversation_test.gleam @@ -5,6 +5,8 @@ import albedo/generated/sql/storage/migrations/conversation_columns_sql import albedo/history/conversation import albedo/runtime import albedo/session/family +import albedo/session/session_submission +import albedo/session/turn import albedo/storage/core_store import albedo/storage/migrations/session_titles import albedo/storage/session_rows @@ -230,6 +232,45 @@ pub fn transcript_timestamps_migrate_without_invention_and_roundtrip_test() -> N scratch.remove_database(path) } +pub fn human_prompt_excludes_generated_submissions_test() -> Nil { + let human = + turn.Submission( + "/skill (daemon annotation)", + "expanded instructions", + "client", + turn.Chat, + [], + Some("submitted"), + Some("submitted"), + Some("/skill"), + [], + None, + ) + let schedule = + turn.Submission( + "scheduled", + "scheduled", + "schedule", + turn.Chat, + [], + Some("scheduled"), + None, + None, + [], + None, + ) + let command = + turn.Submission( + ..human, + display: "extension output", + operation_id: Some("command"), + submission_id: Some("command"), + title_text: None, + ) + let submissions = [schedule, human, command] + session_submission.human_prompt(submissions) |> should.equal(Some("/skill")) +} + /// A transfer's staging session stays hidden until activation and remains /// discoverable by the daemon as an importing identity. pub fn importing_session_is_hidden_until_activation_test() -> Nil { diff --git a/apps/albedo/test/albedo/model_loop/quiet_reset_test.gleam b/apps/albedo/test/albedo/model_loop/quiet_reset_test.gleam index cc1379d6d..82ee7bcf6 100644 --- a/apps/albedo/test/albedo/model_loop/quiet_reset_test.gleam +++ b/apps/albedo/test/albedo/model_loop/quiet_reset_test.gleam @@ -183,6 +183,7 @@ fn run_case(hold_by_hook: Bool, empty_after_reset: Bool) -> #(Int, Int) { repetition.new(), True, False, + Some("start"), ) let initial = [ transcript.Entry(types.User("start"), Some(1), None, None, None, None, True), diff --git a/extensions/hooks/src/albedo_ext_hooks/extension.gleam b/extensions/hooks/src/albedo_ext_hooks/extension.gleam index 6d5b2a219..b7dd3aa8c 100644 --- a/extensions/hooks/src/albedo_ext_hooks/extension.gleam +++ b/extensions/hooks/src/albedo_ext_hooks/extension.gleam @@ -39,7 +39,9 @@ fn fire( event: hook.Event, ) -> hook.Verdict { let #(name, subject, fields) = case event { - hook.Prompted(text) -> #("UserPromptSubmit", "", [#("prompt", quote(text))]) + hook.Prompted(text, _) -> #("UserPromptSubmit", "", [ + #("prompt", quote(text)), + ]) hook.Calling(call) -> #("PreToolUse", tool(call.name), tool_fields(call)) hook.Called(call, output, ..) -> #("PostToolUse", tool(call.name), [ #("tool_response", raw(output)), diff --git a/packages/albedo_sdk/src/albedo_sdk/hook.gleam b/packages/albedo_sdk/src/albedo_sdk/hook.gleam index 1538ef5b6..d1df7ec55 100644 --- a/packages/albedo_sdk/src/albedo_sdk/hook.gleam +++ b/packages/albedo_sdk/src/albedo_sdk/hook.gleam @@ -22,7 +22,7 @@ pub type Context { pub type Event { /// A user message is about to start a turn. - Prompted(text: String) + Prompted(text: String, human: Option(String)) /// A tool call is about to run. Calling(call: Call) /// It finished; a refused call's `output` is its refusal. diff --git a/robot-docs/hooks.md b/robot-docs/hooks.md index 06aa9bd0c..65fa85cfa 100644 --- a/robot-docs/hooks.md +++ b/robot-docs/hooks.md @@ -7,15 +7,19 @@ turns and kernel calls. ## Events -`Prompted` carries user text before a turn's first model call. `Calling` carries -a tool call before execution. `Called` carries the call, output, and start and -finish times. Outer model calls report refusals through `Called`. An inner -Python call refused before execution raises without that follow-up report. -`Stopping` carries the reply, the number of times hooks have held the turn open, -whether `end_turn()` ended the step (`quieted`), and whether a live non-service -background job or backgrounded cell has a completion wake pending (`awaiting`). -`Stopping` hooks run only when no steering input is already continuing the turn. -See +`Prompted(text, human)` carries the newest model-facing user text before a +turn's first model call. `human` holds the original text from the opening +batch's identified chat and editor submissions, in admission order. A turn +started by a letter, schedule, shell receipt, or wake alone carries `None`. Use +this field for services that query on a person's words. Command hooks continue +to receive the model-facing `text`. `Calling` carries a tool call before +execution. `Called` carries the call, output, and start and finish times. Outer +model calls report refusals through `Called`. An inner Python call refused +before execution raises without that follow-up report. `Stopping` carries the +reply, the number of times hooks have held the turn open, whether `end_turn()` +ended the step (`quieted`), and whether a live non-service background job or +backgrounded cell has a completion wake pending (`awaiting`). `Stopping` hooks +run only when no steering input is already continuing the turn. See [`hook.Event` and `hook.Call`](../packages/albedo_sdk/src/albedo_sdk/hook.gleam), [the model loop](../apps/albedo/src/albedo/model_loop/loop.gleam), and [Python reporting](../python/albedo_plugins/hook.py).