diff --git a/src/albedo/daemon/active_output.gleam b/src/albedo/daemon/active_output.gleam index 65e0f213c..237c8aa14 100644 --- a/src/albedo/daemon/active_output.gleam +++ b/src/albedo/daemon/active_output.gleam @@ -1,6 +1,8 @@ //// Session-owned provisional output, with bounded memory and leased spill files. -import albedo/daemon/active_output_store.{Draft, DraftSegment, SegmentOrigin} +import albedo/daemon/active_output_store.{ + type OutputKind, Draft, DraftSegment, SegmentOrigin, Text, +} import gleam/dict import gleam/int import gleam/json @@ -34,7 +36,7 @@ type Segment { Segment( message_id: String, run_id: String, - kind: String, + kind: OutputKind, order: Int, bytes: Int, elapsed_ms: Option(Int), @@ -49,7 +51,7 @@ pub type Snapshot { Snapshot( message_id: String, run_id: String, - kind: String, + kind: OutputKind, bytes: Int, elapsed_ms: Option(Int), text: Option(String), @@ -105,8 +107,8 @@ pub fn namespace(projection: Projection, message_id: String) -> String { <> message_id } -/// Each provisional segment's id with its kind, `text` or `thinking`. -pub fn kinds(projection: Projection) -> List(#(String, String)) { +/// Each provisional segment's id with its kind. +pub fn kinds(projection: Projection) -> List(#(String, OutputKind)) { list.map(projection.segments, fn(segment) { #(segment.message_id, segment.kind) }) @@ -117,7 +119,7 @@ pub fn observe( projection: Projection, run_id: String, message_id: String, - kind: String, + kind: OutputKind, text: String, elapsed_ms: Option(Int), ) -> Projection { @@ -170,7 +172,7 @@ pub fn observe( Ok(_) -> projection.next_order }, draft_dirty: projection.draft_dirty - || { kind == "text" && text != "" }, + || { kind == Text && text != "" }, encoded: case existing { Error(_) -> projection.encoded + 2 Ok(_) -> projection.encoded @@ -222,7 +224,7 @@ fn failed_segment( projection: Projection, run_id: String, message_id: String, - kind: String, + kind: OutputKind, elapsed_ms: Option(Int), reason: String, ) -> Projection { @@ -298,7 +300,7 @@ fn append_segment( segment.kind, segment.order, projection.high_water, - segment.kind == "text", + segment.kind == Text, ), )) case @@ -395,7 +397,7 @@ fn capture_segments(projection: Projection) -> Result(List(Snapshot), String) { /// reading spilled text back from its file. pub fn contents( projection: Projection, -) -> Result(List(#(String, String, Option(Int))), String) { +) -> Result(List(#(OutputKind, String, Option(Int))), String) { use snapshots <- result.try(capture(projection)) list.try_map(snapshots, fn(snapshot) { case snapshot.text, snapshot.content_id { @@ -444,7 +446,7 @@ pub fn flush_text(projection: Projection) -> Projection { let #(segments, flushed_bytes, inline_texts) = list.fold(projection.segments, #([], 0, []), fn(acc, segment) { let #(rev_segments, flushed, inline) = acc - case segment.kind == "text", segment.content_id { + case segment.kind == Text, segment.content_id { True, Some(content_id) if segment.pending != "" -> case active_output_store.append( @@ -524,7 +526,9 @@ pub fn recover( active_output_store.list_session_metas(home, session) |> list.filter_map(fn(meta) { case - meta.recoverable && meta.kind == "text" && meta.high_water == high_water + meta.recoverable + && meta.kind == Some(Text) + && meta.high_water == high_water { False -> Error(Nil) True -> diff --git a/src/albedo/daemon/active_output_store.gleam b/src/albedo/daemon/active_output_store.gleam index 237ab16e5..1afd22b15 100644 --- a/src/albedo/daemon/active_output_store.gleam +++ b/src/albedo/daemon/active_output_store.gleam @@ -34,12 +34,36 @@ const meta_limit = 4096 const unavailable = "active output storage is unavailable" +/// Which stream a segment holds. +pub type OutputKind { + /// The answer people read. + Text + /// The model's reasoning. + Thinking +} + +/// A kind's stored and wire spelling. +pub fn kind_name(kind: OutputKind) -> String { + case kind { + Text -> "text" + Thinking -> "thinking" + } +} + +pub fn parse_kind(name: String) -> Result(OutputKind, Nil) { + case name { + "text" -> Ok(Text) + "thinking" -> Ok(Thinking) + _ -> Error(Nil) + } +} + /// Segment identity and recovery metadata stored on a spilled `.meta` file. pub type SegmentOrigin { SegmentOrigin( run_id: String, message_id: String, - kind: String, + kind: OutputKind, order: Int, high_water: Int, recoverable: Bool, @@ -62,7 +86,8 @@ pub type SpillMeta { id: String, run_id: String, message_id: String, - kind: String, + /// None for a file whose stored kind is not one albedo writes. + kind: Option(OutputKind), order: Int, high_water: Int, recoverable: Bool, @@ -431,7 +456,9 @@ fn read_spill_meta( id: id, run_id: meta_string(atom("run_id"), meta, ""), message_id: meta_string(atom("message_id"), meta, id), - kind: meta_string(atom("kind"), meta, ""), + kind: meta_string(atom("kind"), meta, "") + |> parse_kind + |> option.from_result, order: meta_int(atom("order"), meta, 0), high_water: meta_int(atom("high_water"), meta, -1), recoverable: meta_bool(atom("recoverable"), meta, False), @@ -661,7 +688,7 @@ fn new_meta( |> map_put(atom("recoverable"), origin.recoverable, _) |> map_put(atom("run_id"), origin.run_id, _) |> map_put(atom("message_id"), origin.message_id, _) - |> map_put(atom("kind"), origin.kind, _) + |> map_put(atom("kind"), kind_name(origin.kind), _) |> map_put(atom("order"), origin.order, _) |> map_put(atom("high_water"), origin.high_water, _) } diff --git a/src/albedo/daemon/http_active_output.gleam b/src/albedo/daemon/http_active_output.gleam index 53f63e168..ab4d16aaf 100644 --- a/src/albedo/daemon/http_active_output.gleam +++ b/src/albedo/daemon/http_active_output.gleam @@ -21,7 +21,11 @@ import mist const content_page_bytes = 262_144 type Boundary { - Boundary(generation: String, field: String, bytes: Int) + Boundary( + generation: String, + field: active_output_store.OutputKind, + bytes: Int, + ) } pub fn snapshot( @@ -47,7 +51,7 @@ pub fn snapshot( ) json.object([ #("url", json.string(path <> "?snapshot=" <> token)), - #("field", json.string(output.kind)), + #("field", json.string(active_output_store.kind_name(output.kind))), #("bytes", json.int(output.bytes)), ]) } @@ -55,7 +59,7 @@ pub fn snapshot( json.object([ #("message_id", json.string(output.message_id)), #("run_id", json.string(output.run_id)), - #("kind", json.string(output.kind)), + #("kind", json.string(active_output_store.kind_name(output.kind))), #("bytes", json.int(output.bytes)), #("elapsed_ms", json.nullable(output.elapsed_ms, json.int)), #("text", json.nullable(output.text, json.string)), @@ -66,7 +70,7 @@ pub fn snapshot( fn boundary_json(boundary: Boundary) -> json.Json { json.object([ #("generation", json.string(boundary.generation)), - #("field", json.string(boundary.field)), + #("field", json.string(active_output_store.kind_name(boundary.field))), #("bytes", json.int(boundary.bytes)), ]) } @@ -96,7 +100,13 @@ fn captured_boundary( use boundary <- result.try( json.parse(state, { use generation <- decode.field("generation", decode.string) - use field <- decode.field("field", decode.string) + use field <- decode.field("field", { + use name <- decode.then(decode.string) + case active_output_store.parse_kind(name) { + Ok(kind) -> decode.success(kind) + Error(Nil) -> decode.failure(active_output_store.Text, "output kind") + } + }) use bytes <- decode.field("bytes", decode.int) decode.success(Boundary(generation, field, bytes)) }) @@ -104,11 +114,7 @@ fn captured_boundary( "invalid active output snapshot", )), ) - case - boundary.generation != "" - && boundary.bytes >= 0 - && { boundary.field == "text" || boundary.field == "thinking" } - { + case boundary.generation != "" && boundary.bytes >= 0 { True -> Ok(boundary) False -> Error(sdk_http_api.invalid("invalid active output snapshot")) } @@ -215,7 +221,10 @@ pub fn content( json.array( [ json.object([ - #("field", json.string(boundary.field)), + #( + "field", + json.string(active_output_store.kind_name(boundary.field)), + ), #("offset_bytes", json.int(offset)), #("encoding", json.string("utf8")), #("text", page.0), diff --git a/src/albedo/daemon/session_history.gleam b/src/albedo/daemon/session_history.gleam index d0ab004a1..f6e0506fb 100644 --- a/src/albedo/daemon/session_history.gleam +++ b/src/albedo/daemon/session_history.gleam @@ -3,6 +3,7 @@ import albedo/harness/sdk/session_info import albedo/daemon/active_output +import albedo/daemon/active_output_store import albedo/daemon/conversation import albedo/daemon/events as view import albedo/daemon/http_history @@ -473,8 +474,11 @@ fn publish_range( after: Int, continuation_after: Int, include_rows: Bool, - live: List(#(String, String)), -) -> #(session_state.State(message), List(#(String, String))) { + live: List(#(String, active_output_store.OutputKind)), +) -> #( + session_state.State(message), + List(#(String, active_output_store.OutputKind)), +) { case conversation.read_range( runtime.ledger(state.host), @@ -540,15 +544,18 @@ fn publish_range( fn publish_entry( state: session_state.State(message), entry: http_history.Entry, - live: List(#(String, String)), -) -> #(session_state.State(message), List(#(String, String))) { + live: List(#(String, active_output_store.OutputKind)), +) -> #( + session_state.State(message), + List(#(String, active_output_store.OutputKind)), +) { let claims = case entry.kind { - "assistant" if entry.parts != [] -> "text" - "thinking" -> "thinking" - _ -> "" + "assistant" if entry.parts != [] -> Some(active_output_store.Text) + "thinking" -> Some(active_output_store.Thinking) + _ -> None } let group = - list.find(live, fn(segment) { segment.1 == claims }) + list.find(live, fn(segment) { Some(segment.1) == claims }) |> result.map(fn(segment) { segment.0 |> string.split(":") @@ -559,7 +566,7 @@ fn publish_entry( }) let #(claimed, rest) = list.partition(live, fn(segment) { - segment.1 == claims + Some(segment.1) == claims && case group { Ok(prefix) -> string.starts_with(segment.0, prefix <> ":") Error(_) -> False diff --git a/src/albedo/daemon/session_salvage.gleam b/src/albedo/daemon/session_salvage.gleam index ae0a97fe1..30bc15d93 100644 --- a/src/albedo/daemon/session_salvage.gleam +++ b/src/albedo/daemon/session_salvage.gleam @@ -6,6 +6,7 @@ import albedo/harness/sdk/session_info import albedo/daemon/active_output +import albedo/daemon/active_output_store import albedo/daemon/conversation import albedo/daemon/session_history import albedo/daemon/session_state @@ -132,7 +133,7 @@ pub fn recover_crashed( False } text -> - case partial([#("text", text, None)]) { + case partial([#(active_output_store.Text, text, None)]) { Error(reason) -> { io.println_error("crash output recovery skipped: " <> reason) False @@ -162,9 +163,12 @@ pub fn recover_crashed( /// The streamed text and thinking as one unfinished assistant item. fn partial( - segments: List(#(String, String, Option(Int))), + segments: List(#(active_output_store.OutputKind, String, Option(Int))), ) -> Result(List(types.Input), String) { - case joined(segments, "text"), joined(segments, "thinking") { + case + joined(segments, active_output_store.Text), + joined(segments, active_output_store.Thinking) + { "", "" -> Ok([]) text, thinking -> projection.unfinished_output(text, thinking) @@ -173,8 +177,8 @@ fn partial( } fn joined( - segments: List(#(String, String, Option(Int))), - kind: String, + segments: List(#(active_output_store.OutputKind, String, Option(Int))), + kind: active_output_store.OutputKind, ) -> String { segments |> list.filter_map(fn(segment) { @@ -187,7 +191,9 @@ fn joined( } /// The longest the model thought in any streamed thinking segment. -fn thought_ms(segments: List(#(String, String, Option(Int)))) -> Option(Int) { +fn thought_ms( + segments: List(#(active_output_store.OutputKind, String, Option(Int))), +) -> Option(Int) { segments |> list.filter_map(fn(segment) { option.to_result(segment.2, Nil) }) |> list.max(int.compare) diff --git a/src/albedo/daemon/session_state.gleam b/src/albedo/daemon/session_state.gleam index a1fb8b370..5d5858374 100644 --- a/src/albedo/daemon/session_state.gleam +++ b/src/albedo/daemon/session_state.gleam @@ -2,6 +2,7 @@ import albedo/clock import albedo/daemon/active_output +import albedo/daemon/active_output_store import albedo/daemon/bus import albedo/daemon/context_snapshot import albedo/daemon/conversation @@ -299,7 +300,7 @@ fn publish_output(state: State(message), event: view.Event) -> State(message) { state.active_output, run_id, message_id, - "text", + active_output_store.Text, text, None, ) @@ -314,7 +315,7 @@ fn publish_output(state: State(message), event: view.Event) -> State(message) { state.active_output, run_id, message_id, - "thinking", + active_output_store.Thinking, text, elapsed, )