samagotchi 0.170.0 → 0.172.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -1
- data/bin/chi +6 -4
- data/docs/architecture.md +26 -9
- data/docs/bundles.md +4 -21
- data/docs/cli.md +81 -97
- data/docs/configuration.md +12 -14
- data/docs/context.md +5 -6
- data/docs/guardrails.md +57 -96
- data/docs/hooks.md +3 -4
- data/docs/internals/attached-context.md +6 -7
- data/docs/internals/background-tasks.md +4 -3
- data/docs/internals/block-renderers.md +2 -2
- data/docs/internals/bridge.md +34 -7
- data/docs/internals/context-telemetry.md +13 -1
- data/docs/internals/engine.md +25 -13
- data/docs/internals/inline-widgets.md +1 -1
- data/docs/internals/objectives.md +4 -6
- data/docs/internals/participants.md +340 -249
- data/docs/internals/prompt-caching.md +4 -5
- data/docs/internals/web-frontend.md +185 -35
- data/docs/internals/working-set.md +71 -5
- data/docs/memory.md +1 -1
- data/docs/plugins.md +2 -16
- data/docs/releasing.md +2 -1
- data/docs/scheduling.md +1 -1
- data/docs/sessions.md +17 -48
- data/docs/sub-agent.md +2 -2
- data/docs/system-one.md +2 -2
- data/docs/testing.md +56 -7
- data/lib/samagotchi/bridge/card_store.rb +1 -0
- data/lib/samagotchi/bridge/sse_writer.rb +1 -0
- data/lib/samagotchi/bridge/turn_accumulator.rb +8 -5
- data/lib/samagotchi/bridge.rb +64 -20
- data/lib/samagotchi/bridge_client/event_stream.rb +2 -0
- data/lib/samagotchi/bundle_command/test_run.rb +1 -0
- data/lib/samagotchi/bundle_command/try/run.rb +1 -0
- data/lib/samagotchi/bundles/coordinator/manifest.yml +4 -5
- data/lib/samagotchi/bundles/coordinator/plugin.rb +6 -79
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +1 -16
- data/lib/samagotchi/bundles/github-pr/manifest.yml +2 -2
- data/lib/samagotchi/bundles/github-pr/plugin.rb +0 -1
- data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
- data/lib/samagotchi/bundles/inline-widgets/plugin.rb +4 -5
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +126 -67
- data/lib/samagotchi/command_intake.rb +15 -3
- data/lib/samagotchi/compat.rb +6 -1
- data/lib/samagotchi/context_candidate_choice.rb +1 -3
- data/lib/samagotchi/context_candidates.rb +3 -4
- data/lib/samagotchi/continue_offer.rb +9 -2
- data/lib/samagotchi/delivery.rb +36 -12
- data/lib/samagotchi/empty_answer_error.rb +5 -0
- data/lib/samagotchi/engine/cards.rb +3 -0
- data/lib/samagotchi/engine/context_judge.rb +5 -3
- data/lib/samagotchi/engine/events.rb +4 -0
- data/lib/samagotchi/engine/hooks.rb +3 -0
- data/lib/samagotchi/engine/messages.rb +5 -0
- data/lib/samagotchi/engine/model_settings.rb +2 -0
- data/lib/samagotchi/engine/models.rb +10 -0
- data/lib/samagotchi/engine/objectives.rb +6 -0
- data/lib/samagotchi/engine/participants.rb +18 -14
- data/lib/samagotchi/engine/plugins.rb +8 -0
- data/lib/samagotchi/engine/questions.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -0
- data/lib/samagotchi/engine/session.rb +4 -0
- data/lib/samagotchi/engine/side_requests.rb +9 -0
- data/lib/samagotchi/engine/steering.rb +2 -0
- data/lib/samagotchi/engine/toolkit.rb +5 -0
- data/lib/samagotchi/engine/turn_end.rb +7 -0
- data/lib/samagotchi/engine/turn_helpers.rb +8 -0
- data/lib/samagotchi/engine/verify.rb +2 -0
- data/lib/samagotchi/engine.rb +8 -3
- data/lib/samagotchi/event_fanout.rb +22 -1
- data/lib/samagotchi/grants.rb +33 -22
- data/lib/samagotchi/guardrail_wiring.rb +23 -18
- data/lib/samagotchi/guardrails/approval_use.rb +59 -0
- data/lib/samagotchi/guardrails/approvals.rb +9 -11
- data/lib/samagotchi/guardrails/child_boundary.rb +1 -0
- data/lib/samagotchi/guardrails/gate.rb +10 -5
- data/lib/samagotchi/guardrails/implicit_writers.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +6 -6
- data/lib/samagotchi/guardrails/participant_refusal.rb +33 -0
- data/lib/samagotchi/guardrails/review_paths.rb +2 -3
- data/lib/samagotchi/guardrails.rb +1 -1
- data/lib/samagotchi/image_store.rb +51 -12
- data/lib/samagotchi/kernel_loop.rb +7 -1
- data/lib/samagotchi/llm/stray_tool_call.rb +7 -4
- data/lib/samagotchi/llm_context_stale.rb +4 -1
- data/lib/samagotchi/llm_context_strategy.rb +2 -0
- data/lib/samagotchi/objectives.rb +1 -4
- data/lib/samagotchi/objectives_audit.rb +1 -1
- data/lib/samagotchi/objectives_track.rb +1 -1
- data/lib/samagotchi/owner_prompt.rb +3 -6
- data/lib/samagotchi/parent_report.rb +1 -0
- data/lib/samagotchi/participant_messages.rb +48 -4
- data/lib/samagotchi/participant_revise.rb +61 -0
- data/lib/samagotchi/participant_turn_mode.rb +50 -0
- data/lib/samagotchi/participant_uploads.rb +98 -0
- data/lib/samagotchi/participants.rb +21 -12
- data/lib/samagotchi/participants_watch.rb +2 -2
- data/lib/samagotchi/plugin/api.rb +2 -2
- data/lib/samagotchi/plugin/attached_context.rb +1 -1
- data/lib/samagotchi/plugin/context.rb +0 -4
- data/lib/samagotchi/plugin/sessions.rb +1 -25
- data/lib/samagotchi/reply_wait/poll.rb +5 -1
- data/lib/samagotchi/revise_anchor.rb +9 -0
- data/lib/samagotchi/run_options.rb +2 -3
- data/lib/samagotchi/send_command.rb +15 -10
- data/lib/samagotchi/session.rb +0 -3
- data/lib/samagotchi/session_access/contract.rb +1 -10
- data/lib/samagotchi/session_access/local.rb +10 -23
- data/lib/samagotchi/session_access/remote.rb +9 -20
- data/lib/samagotchi/session_access.rb +12 -16
- data/lib/samagotchi/session_chain.rb +4 -6
- data/lib/samagotchi/session_commands.rb +6 -3
- data/lib/samagotchi/session_continue.rb +1 -2
- data/lib/samagotchi/session_inbox.rb +20 -12
- data/lib/samagotchi/session_manager/delivery.rb +32 -5
- data/lib/samagotchi/session_manager/lifecycle.rb +12 -0
- data/lib/samagotchi/session_manager/listing.rb +5 -0
- data/lib/samagotchi/session_manager/spawn.rb +2 -2
- data/lib/samagotchi/session_manager/worker_command.rb +9 -0
- data/lib/samagotchi/session_participants.rb +12 -8
- data/lib/samagotchi/session_review.rb +5 -7
- data/lib/samagotchi/session_stores.rb +3 -1
- data/lib/samagotchi/share_command.rb +10 -12
- data/lib/samagotchi/share_terms.rb +41 -0
- data/lib/samagotchi/steer.rb +8 -14
- data/lib/samagotchi/system_prompt.rb +16 -25
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +2 -1
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -0
- data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +1 -0
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -17
- data/lib/samagotchi/terminal_ui/participant_commands.rb +2 -2
- data/lib/samagotchi/terminal_ui/status_row.rb +2 -2
- data/lib/samagotchi/terminal_ui.rb +1 -2
- data/lib/samagotchi/testing/sessions.rb +2 -10
- data/lib/samagotchi/tool_runner.rb +42 -9
- data/lib/samagotchi/toolkit.rb +4 -22
- data/lib/samagotchi/toolkit_suggest.rb +3 -3
- data/lib/samagotchi/tools/deferred_tools.rb +1 -0
- data/lib/samagotchi/tools/execute.rb +11 -45
- data/lib/samagotchi/tools/leftovers.rb +122 -0
- data/lib/samagotchi/tools/memory.rb +88 -25
- data/lib/samagotchi/tools/task_get.rb +1 -0
- data/lib/samagotchi/tools/task_list.rb +3 -2
- data/lib/samagotchi/tools/task_record.rb +6 -4
- data/lib/samagotchi/tools/task_runtime.rb +199 -20
- data/lib/samagotchi/tools/task_wait.rb +4 -3
- data/lib/samagotchi/turn_anchors.rb +0 -3
- data/lib/samagotchi/turn_context.rb +1 -1
- data/lib/samagotchi/turn_origin.rb +1 -1
- data/lib/samagotchi/turn_stages/context_judge.rb +4 -4
- data/lib/samagotchi/turn_stages/objectives.rb +2 -2
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +2 -2
- data/lib/samagotchi/turn_stages/verify_anchors.rb +13 -1
- data/lib/samagotchi/update_hint.rb +3 -1
- data/lib/samagotchi/verify_anchor.rb +6 -0
- data/lib/samagotchi/verify_fresh.rb +1 -1
- data/lib/samagotchi/verify_fresh_origin.rb +13 -11
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +45 -82
- data/lib/samagotchi/web/participant_access.rb +139 -40
- data/lib/samagotchi/web/participant_images.rb +185 -0
- data/lib/samagotchi/web/participant_review.rb +22 -142
- data/lib/samagotchi/web/participant_show.rb +241 -0
- data/lib/samagotchi/web/participant_stream_filter.rb +25 -2
- data/lib/samagotchi/web/participant_view.rb +334 -27
- data/lib/samagotchi/web/participants_admin.rb +21 -40
- data/lib/samagotchi/web/public/annotate_bar.css +1 -2
- data/lib/samagotchi/web/public/annotate_bar.js +1 -2
- data/lib/samagotchi/web/public/app.js +126 -132
- data/lib/samagotchi/web/public/block_host.js +7 -4
- data/lib/samagotchi/web/public/bubbles.js +9 -1
- data/lib/samagotchi/web/public/cards_offscreen.js +69 -0
- data/lib/samagotchi/web/public/chip_popover.js +5 -4
- data/lib/samagotchi/web/public/claim_link.js +4 -4
- data/lib/samagotchi/web/public/composer.css +4 -6
- data/lib/samagotchi/web/public/composer_mode.js +11 -6
- data/lib/samagotchi/web/public/composer_mount.js +4 -4
- data/lib/samagotchi/web/public/ctx.js +12 -3
- data/lib/samagotchi/web/public/data.js +2 -10
- data/lib/samagotchi/web/public/delivery_line.js +49 -63
- data/lib/samagotchi/web/public/history_shape.js +5 -1
- data/lib/samagotchi/web/public/images.js +7 -1
- data/lib/samagotchi/web/public/index.html +50 -34
- data/lib/samagotchi/web/public/info_bar_model.js +2 -1
- data/lib/samagotchi/web/public/live_turn.js +1 -1
- data/lib/samagotchi/web/public/markdown_place.js +5 -4
- data/lib/samagotchi/web/public/owner_claim.js +5 -5
- data/lib/samagotchi/web/public/owner_verify.js +0 -2
- data/lib/samagotchi/web/public/participant_bar.js +107 -0
- data/lib/samagotchi/web/public/participant_main.js +289 -0
- data/lib/samagotchi/web/public/participant_model.js +5 -292
- data/lib/samagotchi/web/public/participant_names.js +17 -8
- data/lib/samagotchi/web/public/participant_revise.js +66 -0
- data/lib/samagotchi/web/public/participants_panel.js +32 -74
- data/lib/samagotchi/web/public/question_cards.js +14 -3
- data/lib/samagotchi/web/public/refs.js +53 -36
- data/lib/samagotchi/web/public/reload_check.js +70 -14
- data/lib/samagotchi/web/public/revise_stack.js +21 -10
- data/lib/samagotchi/web/public/send_turn.js +28 -29
- data/lib/samagotchi/web/public/session_actions.js +4 -45
- data/lib/samagotchi/web/public/session_fields.js +1 -2
- data/lib/samagotchi/web/public/session_state.js +12 -7
- data/lib/samagotchi/web/public/session_sync.js +9 -2
- data/lib/samagotchi/web/public/session_view.js +14 -5
- data/lib/samagotchi/web/public/settings_panel.js +4 -18
- data/lib/samagotchi/web/public/stage_answer.js +56 -0
- data/lib/samagotchi/web/public/stage_asking.js +7 -5
- data/lib/samagotchi/web/public/stage_cloud.js +93 -0
- data/lib/samagotchi/web/public/stage_dom.js +100 -0
- data/lib/samagotchi/web/public/stage_handoff.js +154 -0
- data/lib/samagotchi/web/public/stage_owner_wait.js +35 -0
- data/lib/samagotchi/web/public/stage_pager.js +59 -0
- data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
- data/lib/samagotchi/web/public/stage_view.js +103 -410
- data/lib/samagotchi/web/public/stage_waiting.js +74 -6
- data/lib/samagotchi/web/public/start_page.js +12 -33
- data/lib/samagotchi/web/public/start_settings.js +24 -78
- data/lib/samagotchi/web/public/start_toolkit.js +6 -10
- data/lib/samagotchi/web/public/storage.js +22 -0
- data/lib/samagotchi/web/public/stream_handlers.js +3 -10
- data/lib/samagotchi/web/public/tokens.css +1 -1
- data/lib/samagotchi/web/public/turn_block.js +2 -5
- data/lib/samagotchi/web/public/turn_events.js +30 -12
- data/lib/samagotchi/web/public/turn_html.js +9 -2
- data/lib/samagotchi/web/public/turn_pieces.js +3 -4
- data/lib/samagotchi/web/public/turn_range.js +1 -2
- data/lib/samagotchi/web/public/verify_target.js +2 -4
- data/lib/samagotchi/web/public/viewer.js +59 -0
- data/lib/samagotchi/web/public/viewer_self.js +59 -0
- data/lib/samagotchi/web/public/working_set.js +11 -5
- data/lib/samagotchi/web/public/working_set_view.js +53 -8
- data/lib/samagotchi/web/public/ws_cards.js +89 -0
- data/lib/samagotchi/web/public/ws_peek.js +106 -0
- data/lib/samagotchi/web/public/ws_preview.css +126 -0
- data/lib/samagotchi/web/public/ws_reader.js +319 -0
- data/lib/samagotchi/web/public/ws_timeline.js +200 -0
- data/lib/samagotchi/web/public/ws_way_in.js +25 -0
- data/lib/samagotchi/web/qr.rb +1 -1
- data/lib/samagotchi/web/session_control.rb +42 -15
- data/lib/samagotchi/web/session_create.rb +6 -72
- data/lib/samagotchi/web/session_show.rb +75 -28
- data/lib/samagotchi/web/session_summary.rb +1 -4
- data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
- data/lib/samagotchi/web/viewer.rb +70 -0
- data/lib/samagotchi/web/working_set_routes.rb +132 -0
- data/lib/samagotchi/worker.rb +35 -28
- data/lib/samagotchi/worker_inbound.rb +4 -3
- data/lib/samagotchi/worker_link.rb +17 -0
- data/lib/samagotchi/working_set.rb +322 -11
- metadata +32 -13
- data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
- data/lib/samagotchi/participant_mode.rb +0 -86
- data/lib/samagotchi/review_apply.rb +0 -131
- data/lib/samagotchi/review_command.rb +0 -123
- data/lib/samagotchi/review_start.rb +0 -196
- data/lib/samagotchi/web/public/participant.html +0 -218
- data/lib/samagotchi/web/public/participant.js +0 -719
- data/lib/samagotchi/web/public/participant_annotate.js +0 -76
- data/lib/samagotchi/web/public/review_apply.js +0 -34
- data/lib/samagotchi/web/public/review_history.js +0 -72
- data/lib/samagotchi/web/public/review_start.js +0 -55
- data/lib/samagotchi/web/public/tools_overview.js +0 -42
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// What the model has of one working-set item, as the reader (ws_reader.js)
|
|
2
|
+
// and the popover's peek (ws_peek.js) show it: the pure part. No DOM, so
|
|
3
|
+
// `node --test` covers it. The server's timeline (GET …/working_set/item,
|
|
4
|
+
// WorkingSet.timeline_of: docs/internals/working-set.md, Timeline) is every
|
|
5
|
+
// run of the item in conversation order; here it becomes cards, newest
|
|
6
|
+
// first:
|
|
7
|
+
// - a slice: reads whose lines overlap or touch, merged (each line from the
|
|
8
|
+
// newest read holding it), its header listing the reads; reads merge only
|
|
9
|
+
// with reads in the same state (still sent, dropped, forgotten), so a
|
|
10
|
+
// slice the model no longer has is greyed whole, and only between the same
|
|
11
|
+
// edits or writes (an edit moves the lines after it);
|
|
12
|
+
// - a partial read (a head/tail preview, a cut output), shown as it is;
|
|
13
|
+
// - an edit or a write, its saved diff (diff_view.js);
|
|
14
|
+
// - a memory read (its text, its overlay apart), a memory write, a fetch.
|
|
15
|
+
// The file strip draws the merged ranges over the file's length (the
|
|
16
|
+
// largest last line any read knows: a whole read's is the file's).
|
|
17
|
+
//
|
|
18
|
+
// An event's place on the page (its step tag, "go to the call") is the
|
|
19
|
+
// item's row with its tool id (working_set.js runs); a legacy row has
|
|
20
|
+
// none, so the server's global step names it.
|
|
21
|
+
|
|
22
|
+
import { stepTag } from "./working_set.js";
|
|
23
|
+
|
|
24
|
+
// A slice longer than this shows its first lines and a "show all" fold.
|
|
25
|
+
export const SLICE_FOLD_LINES = 200;
|
|
26
|
+
// The peek's lines of a read or a text.
|
|
27
|
+
export const PEEK_LINES = 12;
|
|
28
|
+
|
|
29
|
+
const STATES = ["sent", "dropped", "forgotten"];
|
|
30
|
+
|
|
31
|
+
// An event's state: sent, dropped (the user's) or forgotten.
|
|
32
|
+
export function eventState(event) {
|
|
33
|
+
if (event?.dropped) return "dropped";
|
|
34
|
+
if (event && event.sent === false) return "forgotten";
|
|
35
|
+
return "sent";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The path of the item route for session +id+ (+now+: a memory's text now).
|
|
39
|
+
export function itemPath(id, kind, key, { now = false } = {}) {
|
|
40
|
+
const query = new URLSearchParams({ kind, key });
|
|
41
|
+
if (now) query.set("now", "1");
|
|
42
|
+
return `/api/sessions/${encodeURIComponent(id)}/working_set/item?${query}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A read's lines as numbered rows ([{n, text}]), its text split as the
|
|
46
|
+
// file's (a last newline ends a line), at most up to its last line.
|
|
47
|
+
export function readRows(event) {
|
|
48
|
+
const first = Number(event?.lines?.first) || 1;
|
|
49
|
+
const lines = String(event?.text ?? "").split("\n");
|
|
50
|
+
if (lines.at(-1) === "") lines.pop();
|
|
51
|
+
const last = Number(event?.lines?.last);
|
|
52
|
+
const count = Number.isFinite(last) && last >= first ? Math.min(lines.length, last - first + 1) : lines.length;
|
|
53
|
+
return lines.slice(0, count).map((text, i) => ({ n: first + i, text }));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const mergeable = (event) => event.op === "read" && !event.lines?.partial && Number.isFinite(event.lines?.first) && Number.isFinite(event.lines?.last);
|
|
57
|
+
|
|
58
|
+
// The reads of +events+ merged into slices: [{type: "slice", first, last,
|
|
59
|
+
// state, reads, rows, newest}], +newest+ the place of its newest read.
|
|
60
|
+
function slices(events) {
|
|
61
|
+
const out = [];
|
|
62
|
+
// An edit or a write starts a new version of the file: its lines move, so
|
|
63
|
+
// only reads between the same edits merge (line numbers are one file's).
|
|
64
|
+
let version = 0;
|
|
65
|
+
const versions = events.map((event) => {
|
|
66
|
+
if (event.op === "edit" || event.op === "write") version += 1;
|
|
67
|
+
return version;
|
|
68
|
+
});
|
|
69
|
+
for (let v = 0; v <= version; v += 1) for (const state of STATES) {
|
|
70
|
+
const reads = events.map((event, place) => ({ event, place }))
|
|
71
|
+
.filter(({ event, place }) => versions[place] === v && mergeable(event) && eventState(event) === state)
|
|
72
|
+
.sort((a, b) => a.event.lines.first - b.event.lines.first || a.place - b.place);
|
|
73
|
+
let open = null;
|
|
74
|
+
for (const read of reads) {
|
|
75
|
+
const { first, last } = read.event.lines;
|
|
76
|
+
if (open && first <= open.last + 1) {
|
|
77
|
+
open.last = Math.max(open.last, last);
|
|
78
|
+
open.reads.push(read);
|
|
79
|
+
} else {
|
|
80
|
+
open = { type: "slice", first, last, state, reads: [read] };
|
|
81
|
+
out.push(open);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return out.map((slice) => {
|
|
86
|
+
// Each line from the newest read that holds it.
|
|
87
|
+
const lines = new Map();
|
|
88
|
+
for (const { event } of [...slice.reads].sort((a, b) => a.place - b.place)) {
|
|
89
|
+
for (const row of readRows(event)) lines.set(row.n, row.text);
|
|
90
|
+
}
|
|
91
|
+
const rows = [...lines.keys()].sort((a, b) => a - b).map((n) => ({ n, text: lines.get(n) }));
|
|
92
|
+
const reads = slice.reads.sort((a, b) => b.place - a.place).map(({ event }) => event);
|
|
93
|
+
return { ...slice, reads, rows, newest: Math.max(...slice.reads.map((r) => r.place)) };
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const TYPES = { edit: "edit", write: "edit", memory_read: "memory", memory_write: "written", fetch: "fetch" };
|
|
98
|
+
|
|
99
|
+
// The timeline's cards, newest first: [{type, newest, ...}] — slices
|
|
100
|
+
// (above), and {type: "partial" | "edit" | "memory" | "written" | "fetch",
|
|
101
|
+
// event, state}. The newest card is +latest+.
|
|
102
|
+
export function timelineCards(events = []) {
|
|
103
|
+
const list = Array.isArray(events) ? events : [];
|
|
104
|
+
const cards = slices(list);
|
|
105
|
+
list.forEach((event, place) => {
|
|
106
|
+
if (event.op === "read" && !mergeable(event)) cards.push({ type: "partial", event, state: eventState(event), newest: place });
|
|
107
|
+
else if (TYPES[event.op]) cards.push({ type: TYPES[event.op], event, state: eventState(event), newest: place });
|
|
108
|
+
});
|
|
109
|
+
cards.sort((a, b) => b.newest - a.newest);
|
|
110
|
+
if (cards.length) cards[0] = { ...cards[0], latest: true };
|
|
111
|
+
return cards;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// The file strip: the merged ranges over the file's length (+length+: the
|
|
115
|
+
// largest last line known), the newest read's range +latest+.
|
|
116
|
+
// @return {length, bands: [{first, last, state, latest?}]} or null without one
|
|
117
|
+
export function fileStrip(events = []) {
|
|
118
|
+
const reads = (events || []).filter(mergeable);
|
|
119
|
+
if (!reads.length) return null;
|
|
120
|
+
const length = Math.max(...reads.map((e) => e.lines.last));
|
|
121
|
+
const bands = slices(events).map(({ first, last, state }) => ({ first, last, state }));
|
|
122
|
+
const newest = reads.at(-1);
|
|
123
|
+
bands.push({ first: newest.lines.first, last: newest.lines.last, state: eventState(newest), latest: true });
|
|
124
|
+
return { length, bands };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// A band's place on the strip as percentages ({left, width}).
|
|
128
|
+
export function bandBox(band, length) {
|
|
129
|
+
const total = Math.max(1, Number(length) || 1);
|
|
130
|
+
const left = ((band.first - 1) / total) * 100;
|
|
131
|
+
const width = Math.max(0.5, ((band.last - band.first + 1) / total) * 100);
|
|
132
|
+
return { left: `${left.toFixed(2)}%`, width: `${Math.min(width, 100 - left).toFixed(2)}%` };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// "3 reads, 1 edit" (the peek's link and the reader's head).
|
|
136
|
+
export function countsText(events = []) {
|
|
137
|
+
const reads = events.filter((e) => e.op === "read" || e.op === "memory_read" || e.op === "fetch").length;
|
|
138
|
+
const edits = events.filter((e) => e.op === "edit" || e.op === "write" || e.op === "memory_write").length;
|
|
139
|
+
const word = (n, one) => `${n} ${one}${n === 1 ? "" : "s"}`;
|
|
140
|
+
return [reads ? word(reads, "read") : "", edits ? word(edits, "edit") : ""].filter(Boolean).join(", ");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// The place of +event+ on the page: its row's at, from the item's runs
|
|
144
|
+
// (working_set.js), or null (a legacy row, a row not drawn).
|
|
145
|
+
export function placeOf(event, item) {
|
|
146
|
+
const run = (item?.runs || []).find((r) => r.id === event?.tool_id);
|
|
147
|
+
return run ? run.at : null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// An event's step tag: the row's "t3·s14", else the server's "step 14".
|
|
151
|
+
export function eventTag(event, item) {
|
|
152
|
+
const at = placeOf(event, item);
|
|
153
|
+
if (at) return stepTag({ at }, { session: true });
|
|
154
|
+
return Number.isFinite(event?.step) ? `step ${event.step}` : "";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// A slice's or a card's steps for its header ("t3·s13, t3·s162").
|
|
158
|
+
export function tagsText(events, item) {
|
|
159
|
+
return events.map((event) => eventTag(event, item)).filter(Boolean).join(", ");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// A read's lines in words: "lines 204–265", "line 3", "whole file".
|
|
163
|
+
export function linesText(lines) {
|
|
164
|
+
if (!lines) return "";
|
|
165
|
+
if (lines.partial) return lines.whole ? "a preview of the file" : `from line ${lines.first}, cut`;
|
|
166
|
+
return lines.first === lines.last ? `line ${lines.first}` : `lines ${lines.first}–${lines.last}`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const NO_LONGER = { dropped: "dropped by you", forgotten: "forgotten" };
|
|
170
|
+
|
|
171
|
+
// Why chi no longer has a card's text, or "" while it does.
|
|
172
|
+
export function goneText(state) {
|
|
173
|
+
return NO_LONGER[state] || "";
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// What the peek shows of an item: the latest edit's diff when it has one,
|
|
177
|
+
// else the newest read's (or text's) first lines; +after+ the newest event
|
|
178
|
+
// after the one shown (a read after the edit).
|
|
179
|
+
// @return {event, kind: "diff" | "lines" | "text", rows?, after} or null
|
|
180
|
+
export function peekOf(events = []) {
|
|
181
|
+
const list = Array.isArray(events) ? events : [];
|
|
182
|
+
if (!list.length) return null;
|
|
183
|
+
const editAt = list.findLastIndex((e) => (e.op === "edit" || e.op === "write") && e.diff);
|
|
184
|
+
if (editAt >= 0) {
|
|
185
|
+
const after = list.slice(editAt + 1).at(-1) || null;
|
|
186
|
+
return { event: list[editAt], kind: "diff", after };
|
|
187
|
+
}
|
|
188
|
+
// An edit without a diff (a no-op) shows nothing: the newest other run.
|
|
189
|
+
const event = list.findLast((e) => e.op !== "edit" && e.op !== "write") || list.at(-1);
|
|
190
|
+
if (event.op === "read") return { event, kind: "lines", rows: readRows(event).slice(0, PEEK_LINES), after: null };
|
|
191
|
+
const rows = String(event.text ?? "").split("\n").slice(0, PEEK_LINES).map((text, i) => ({ n: i + 1, text }));
|
|
192
|
+
return { event, kind: "text", rows, after: null };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// What an event is, in a header's words ("edit", "read 236–250", "memory read").
|
|
196
|
+
export function eventWords(event) {
|
|
197
|
+
if (!event) return "";
|
|
198
|
+
if (event.op === "read") return `read ${linesText(event.lines).replace(/^lines? /, "")}`.trim();
|
|
199
|
+
return { edit: "edit", write: "write", memory_read: "memory read", memory_write: "memory write", fetch: "fetch" }[event.op] || event.op;
|
|
200
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// A tool row's way into the working set's reader (ws_reader.js): a small
|
|
2
|
+
// "timeline" button on an ok read, edit, write, memory_read or memory_write
|
|
3
|
+
// row (the item it touched: working_set.js rowEntries), which the reader's
|
|
4
|
+
// delegated click opens. Off until the page turns it on (app.js, for an
|
|
5
|
+
// owner with the working set): a participant's rows never get it (the
|
|
6
|
+
// null-control idiom: not drawn, not wired). turn_html.js draws it.
|
|
7
|
+
|
|
8
|
+
import { html, nothing } from "./vendor/lit-html.js";
|
|
9
|
+
import { rowEntries } from "./working_set.js";
|
|
10
|
+
|
|
11
|
+
const TOOLS = new Set(["read", "edit", "write", "memory_read", "memory_write"]);
|
|
12
|
+
let on = false;
|
|
13
|
+
|
|
14
|
+
// The page has a reader: rows drawn from now on carry the button.
|
|
15
|
+
export function setTimelineWayIn(value) {
|
|
16
|
+
on = !!value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The button for +row+ (activity.js's row model), or nothing.
|
|
20
|
+
export function wayInTemplate(row) {
|
|
21
|
+
if (!on || !TOOLS.has(row?.tool)) return nothing;
|
|
22
|
+
const entry = rowEntries(row)[0];
|
|
23
|
+
if (!entry) return nothing;
|
|
24
|
+
return html`<button type="button" class="activity-ws" data-ws-kind=${entry.kind} data-ws-key=${entry.key} title="What chi has of it: open its timeline">timeline</button>`;
|
|
25
|
+
}
|
data/lib/samagotchi/web/qr.rb
CHANGED
|
@@ -42,7 +42,7 @@ module Samagotchi
|
|
|
42
42
|
row.each_index.select { |x| row[x] }.map { |x| "M#{x} #{y}h1v1h-1z" }
|
|
43
43
|
end
|
|
44
44
|
%(<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 #{size} #{size}" shape-rendering="crispEdges" role="img" ) +
|
|
45
|
-
%(aria-label="QR code of the
|
|
45
|
+
%(aria-label="QR code of the invite link"><rect width="#{size}" height="#{size}" fill="#fff"/>) +
|
|
46
46
|
%(<path d="#{dark.join}" fill="#000"/></svg>)
|
|
47
47
|
end
|
|
48
48
|
|
|
@@ -272,45 +272,64 @@ module Samagotchi
|
|
|
272
272
|
# POST /api/sessions/:id/images: the raw image as the body (a paste or
|
|
273
273
|
# a drop), ?name= for its file name. Stored like any other image
|
|
274
274
|
# (converted, downscaled); answers the ref the turn then names.
|
|
275
|
+
# A participant's upload is theirs (ParticipantImages).
|
|
275
276
|
def handle_image_upload(req, id)
|
|
276
277
|
session_dir = image_session_dir(id)
|
|
277
278
|
return session_not_found(id) unless session_dir
|
|
279
|
+
return participant_image_upload(req, id, session_dir) unless principal(req).owner?
|
|
278
280
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
return error_response(413, "too_large", "an image may be up to #{MAX_IMAGE_UPLOAD_BYTES / 1024 / 1024} MB")
|
|
282
|
-
end
|
|
283
|
-
|
|
284
|
-
bytes = request_body(req, MAX_IMAGE_UPLOAD_BYTES + 1).to_s.b
|
|
285
|
-
if bytes.bytesize > MAX_IMAGE_UPLOAD_BYTES
|
|
286
|
-
return error_response(413, "too_large", "an image may be up to #{MAX_IMAGE_UPLOAD_BYTES / 1024 / 1024} MB")
|
|
287
|
-
end
|
|
281
|
+
bytes = image_upload_bytes(req)
|
|
282
|
+
return error_response(413, "too_large", image_too_large_detail) unless bytes
|
|
288
283
|
|
|
289
|
-
|
|
290
|
-
ref = ImageStore.ingest(session_dir, bytes: bytes, name: name.empty? ? "image" : name, source: "user")
|
|
284
|
+
ref = ImageStore.ingest(session_dir, bytes: bytes, name: upload_name(req), source: "user")
|
|
291
285
|
json_response(201, ref)
|
|
292
286
|
rescue ImageStore::Error => e
|
|
293
287
|
error_response(422, "bad_image", e.message)
|
|
294
288
|
end
|
|
295
289
|
|
|
290
|
+
# An upload's body, or nil when it is over MAX_IMAGE_UPLOAD_BYTES (by
|
|
291
|
+
# its Content-Length or what came).
|
|
292
|
+
def image_upload_bytes(req)
|
|
293
|
+
return nil if req.content_length.to_i > MAX_IMAGE_UPLOAD_BYTES
|
|
294
|
+
|
|
295
|
+
bytes = request_body(req, MAX_IMAGE_UPLOAD_BYTES + 1).to_s.b
|
|
296
|
+
bytes.bytesize > MAX_IMAGE_UPLOAD_BYTES ? nil : bytes
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def image_too_large_detail = "an image may be up to #{MAX_IMAGE_UPLOAD_BYTES / 1024 / 1024} MB"
|
|
300
|
+
|
|
301
|
+
# An upload's ?name=, a base name without control characters; "image"
|
|
302
|
+
# without one.
|
|
303
|
+
def upload_name(req)
|
|
304
|
+
name = File.basename(req.params["name"].to_s).gsub(/[[:cntrl:]]/, "")[0, 120]
|
|
305
|
+
name.empty? ? "image" : name
|
|
306
|
+
end
|
|
307
|
+
|
|
296
308
|
# GET /api/sessions/:id/images/<hash>.<ext>: a stored image (only
|
|
297
|
-
# raster types; never svg), for the page's thumbnails.
|
|
298
|
-
|
|
309
|
+
# raster types; never svg), for the page's thumbnails. A participant
|
|
310
|
+
# reads only an image they uploaded (ParticipantImages), never
|
|
311
|
+
# cached; any other is the 404 an unknown file gets.
|
|
312
|
+
def handle_image(req, id, name)
|
|
299
313
|
session_dir = image_session_dir(id)
|
|
300
314
|
return not_found(path: "images/#{name}") unless session_dir && IMAGE_NAME_RE.match?(name.to_s)
|
|
301
315
|
|
|
316
|
+
participant = !principal(req).owner?
|
|
317
|
+
return not_found(path: "images/#{name}") if participant && !participant_owns_image?(req, session_dir, name)
|
|
318
|
+
|
|
302
319
|
path = File.join(session_dir, ImageStore::DIR, name)
|
|
303
320
|
return not_found(path: "images/#{name}") unless File.file?(path) && !File.symlink?(path)
|
|
304
321
|
|
|
305
322
|
body = File.binread(path)
|
|
306
323
|
[200, { "Content-Type" => IMAGE_TYPES.fetch(File.extname(name).delete(".")), "Content-Length" => body.bytesize.to_s,
|
|
307
|
-
"X-Content-Type-Options" => "nosniff", "Cache-Control" => "private, max-age=86400" },
|
|
324
|
+
"X-Content-Type-Options" => "nosniff", "Cache-Control" => participant ? "no-store" : "private, max-age=86400" },
|
|
325
|
+
[body]]
|
|
308
326
|
end
|
|
309
327
|
|
|
310
328
|
# The Bridge decides the reason (Bridge::CANCEL_REASONS; anything
|
|
311
329
|
# else is manual) and the reply echoes its choice. No reason: user.
|
|
312
330
|
def handle_cancel(req, id)
|
|
313
331
|
@session_class.load(id, state_dir: default_state_dir)
|
|
332
|
+
return participant_cancel(req, id) unless principal(req).owner?
|
|
314
333
|
|
|
315
334
|
parsed = parse_json(request_body(req))
|
|
316
335
|
reason = (parsed["reason"] if parsed.is_a?(Hash)).to_s.strip
|
|
@@ -318,6 +337,12 @@ module Samagotchi
|
|
|
318
337
|
|
|
319
338
|
turn_id = (parsed["turn_id"] if parsed.is_a?(Hash)).to_s.strip
|
|
320
339
|
request = ->(client) { turn_id.empty? ? client.cancel(reason: reason) : client.cancel(reason: reason, turn_id: turn_id) }
|
|
340
|
+
relay_cancel(id, request)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
# The cancel's relay to the worker and its answers (the owner's and a
|
|
344
|
+
# participant's, ParticipantAccess#participant_cancel).
|
|
345
|
+
def relay_cancel(id, request)
|
|
321
346
|
relay(id, bridge_client(id), request, what: "the turn was not cancelled", cant: "cancel turns") do |reply|
|
|
322
347
|
case reply.status
|
|
323
348
|
when 202
|
|
@@ -368,7 +393,9 @@ module Samagotchi
|
|
|
368
393
|
unless (who = principal(req)).owner?
|
|
369
394
|
return not_permitted_for(req, who) unless own_turn?(who.grant, id, enqueued_id)
|
|
370
395
|
|
|
371
|
-
|
|
396
|
+
# Only echoed in the worker's turn_dequeued (no shim: an older
|
|
397
|
+
# worker's echo of it is a cosmetic mismatch).
|
|
398
|
+
client_id = ClientId.participant(who.grant.id)
|
|
372
399
|
end
|
|
373
400
|
|
|
374
401
|
request = ->(client) { client.dequeue(enqueued_id: enqueued_id, client_id: client_id) }
|
|
@@ -6,7 +6,6 @@ require_relative "../llm_context_override"
|
|
|
6
6
|
require_relative "../model_profile"
|
|
7
7
|
require_relative "../parent_answers_command"
|
|
8
8
|
require_relative "../prompt_history"
|
|
9
|
-
require_relative "../review_start"
|
|
10
9
|
require_relative "../send_command"
|
|
11
10
|
require_relative "../session"
|
|
12
11
|
require_relative "../session_access"
|
|
@@ -22,8 +21,7 @@ require_relative "../turn_flow"
|
|
|
22
21
|
module Samagotchi
|
|
23
22
|
module Web
|
|
24
23
|
# POST /api/sessions: a new session (handle_create), a continue
|
|
25
|
-
# (handle_continue, continue_from),
|
|
26
|
-
# handle_review_fork), and their refusals (continue_refusal,
|
|
24
|
+
# (handle_continue, continue_from), and their refusals (continue_refusal,
|
|
27
25
|
# create_refusal, parent_gate, vision_refused) and request readers
|
|
28
26
|
# (request_toolkit, request_parent_answers, request_llm_context,
|
|
29
27
|
# request_thinking). Included in App: its ROUTES name handle_create, and
|
|
@@ -32,9 +30,6 @@ module Samagotchi
|
|
|
32
30
|
# ivars (@manager, @session_class, @state_dir, @hub) answer. See
|
|
33
31
|
# docs/architecture.md (chi web's routes).
|
|
34
32
|
module SessionCreate
|
|
35
|
-
# What a review session's create refuses beside review.
|
|
36
|
-
REVIEW_REFUSES = %w[prompt preview idle continues].freeze
|
|
37
|
-
|
|
38
33
|
# What a continue takes from the previous link: refused beside it.
|
|
39
34
|
CONTINUE_TAKES = %w[dir llm_context thinking settings toolkit tools no_tools parent_answers].freeze
|
|
40
35
|
|
|
@@ -54,11 +49,7 @@ module Samagotchi
|
|
|
54
49
|
# idle: a session with no first turn, for a first message with
|
|
55
50
|
# images (the page uploads them into it, then sends the turn).
|
|
56
51
|
idle = body["idle"] == true
|
|
57
|
-
|
|
58
|
-
return error if error
|
|
59
|
-
return handle_review_fork(body, review) if review&.from
|
|
60
|
-
|
|
61
|
-
if prompt.to_s.strip.empty? && !idle && !review
|
|
52
|
+
if prompt.to_s.strip.empty? && !idle
|
|
62
53
|
return error_response(400, "missing_fields", "prompt is required")
|
|
63
54
|
end
|
|
64
55
|
return handle_continue(body, prompt, idle) if body.key?("continues")
|
|
@@ -118,19 +109,12 @@ module Samagotchi
|
|
|
118
109
|
parent_answers: parent_answers)
|
|
119
110
|
folder[:setup] = setup unless setup.empty?
|
|
120
111
|
begin
|
|
121
|
-
session =
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
else
|
|
125
|
-
@manager.spawn_session(prompt: idle ? nil : prompt.to_s,
|
|
126
|
-
client_id: body["client_id"] || ClientId::WEB_CREATE,
|
|
127
|
-
state_dir: @state_dir, **folder)
|
|
128
|
-
end
|
|
112
|
+
session = @manager.spawn_session(prompt: idle ? nil : prompt.to_s,
|
|
113
|
+
client_id: body["client_id"] || ClientId::WEB_CREATE,
|
|
114
|
+
state_dir: @state_dir, **folder)
|
|
129
115
|
rescue ModelProfile::MissingModel => e
|
|
130
116
|
# No model configured, or one qualified with an unknown host.
|
|
131
117
|
return error_response(400, "invalid_model", e.message)
|
|
132
|
-
rescue ReviewStart::Refused => e
|
|
133
|
-
return error_response(400, "invalid_review", e.message)
|
|
134
118
|
end
|
|
135
119
|
# The worker's Bridge is the single live transport: wait (bounded) for
|
|
136
120
|
# it so the client can attach without client-side polling.
|
|
@@ -189,56 +173,6 @@ module Samagotchi
|
|
|
189
173
|
first_turn_id: session.first_turn_id))
|
|
190
174
|
end
|
|
191
175
|
|
|
192
|
-
# The create's review: {subjects: [paths], from: id} (chi review, the
|
|
193
|
-
# page's Start review) as a SessionAccess::Review: [review, nil] (nil
|
|
194
|
-
# when absent), or [nil, a 400 invalid_review]. A review session
|
|
195
|
-
# starts idle: prompt, preview, idle and continues are refused beside
|
|
196
|
-
# it. Its subjects are checked where it starts (ReviewStart).
|
|
197
|
-
def request_review(body)
|
|
198
|
-
return [nil, nil] unless body.key?("review")
|
|
199
|
-
|
|
200
|
-
raw = body["review"]
|
|
201
|
-
subjects = raw.is_a?(Hash) ? raw.fetch("subjects", []) : nil
|
|
202
|
-
from = raw.is_a?(Hash) ? raw["from"] : nil
|
|
203
|
-
unless subjects.is_a?(Array) && subjects.all?(String) && (from.nil? || (from.is_a?(String) && !from.strip.empty?))
|
|
204
|
-
return [nil, error_response(400, "invalid_review", "review is {subjects: [project files], from: a session id}")]
|
|
205
|
-
end
|
|
206
|
-
if (fields = REVIEW_REFUSES.select { |key| body.key?(key) }).any?
|
|
207
|
-
return [nil, error_response(400, "invalid_review", "a review session starts idle; leave out #{fields.join(", ")}")]
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
[SessionAccess::Review.new(subjects: subjects, from: from&.strip), nil]
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
# POST /api/sessions with review.from: a review fork of that session
|
|
214
|
-
# (ReviewStart.fork), in its folder and with its setup (dir and the
|
|
215
|
-
# setup fields refused beside it, as for a continue), on model when
|
|
216
|
-
# given. 201 as a create's; 400 invalid_review (a subject, its folder
|
|
217
|
-
# gone), invalid_model, ambiguous_id; 404 not_found.
|
|
218
|
-
def handle_review_fork(body, review)
|
|
219
|
-
if (fields = CONTINUE_TAKES.select { |key| body.key?(key) }).any?
|
|
220
|
-
return error_response(400, "invalid_review",
|
|
221
|
-
"a review of a session takes its folder and setup; leave out #{fields.join(", ")}")
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
model = (body["model"] || body["model_name"]).to_s.strip
|
|
225
|
-
begin
|
|
226
|
-
session = ReviewStart.fork(from: review.from, subjects: review.subjects, model: model.empty? ? nil : model,
|
|
227
|
-
state_dir: @state_dir)
|
|
228
|
-
rescue ReviewStart::Refused => e
|
|
229
|
-
return error_response(400, "invalid_review", e.message)
|
|
230
|
-
rescue ModelProfile::MissingModel => e
|
|
231
|
-
return error_response(400, "invalid_model", e.message)
|
|
232
|
-
rescue Session::AmbiguousId => e
|
|
233
|
-
return refusal_response(400, "ambiguous_id", e, ids: e.ids)
|
|
234
|
-
rescue ArgumentError => e
|
|
235
|
-
return error_response(404, "not_found", e.message)
|
|
236
|
-
end
|
|
237
|
-
port = await_worker_link(session.id).then { |link| link.live? ? link.port : nil }
|
|
238
|
-
@hub&.touch(session.id)
|
|
239
|
-
json_response(201, session_to_json(session).merge(bridge_port: port, model_warning: session.model_warning))
|
|
240
|
-
end
|
|
241
|
-
|
|
242
176
|
# The previous link's parent_answers and the model the new one runs,
|
|
243
177
|
# for #create_refusal; nil when the previous link isn't there
|
|
244
178
|
# (continue_from says so).
|
|
@@ -300,7 +234,7 @@ module Samagotchi
|
|
|
300
234
|
# default) is known not to take images; nil when it does or is
|
|
301
235
|
# unknown (the worker decides when the turn runs).
|
|
302
236
|
def vision_refused(name, typed)
|
|
303
|
-
refusal = SendCommand.
|
|
237
|
+
refusal = SendCommand.vision_refusal_for(name, typed: typed)
|
|
304
238
|
refusal && error_response(422, "no_vision", refusal)
|
|
305
239
|
end
|
|
306
240
|
|
|
@@ -17,9 +17,26 @@ require_relative "../session_metrics"
|
|
|
17
17
|
require_relative "../session_setting"
|
|
18
18
|
require_relative "../system_one/log"
|
|
19
19
|
require_relative "../toolkit"
|
|
20
|
+
require_relative "viewer"
|
|
20
21
|
|
|
21
22
|
module Samagotchi
|
|
22
23
|
module Web
|
|
24
|
+
# What a session's show is read from, the same for the owner's and a
|
|
25
|
+
# participant's (SessionShow#show_reads): the session file, a live
|
|
26
|
+
# worker's turn snapshot and state snapshot (both nil without one), the
|
|
27
|
+
# messages shown (the snapshot's, else the file's) and the stream cursor
|
|
28
|
+
# the page goes on from (+event_seq+, and the snapshot's +event_id+
|
|
29
|
+
# `<seq>-<epoch>`). Each assembler makes its own payload from it.
|
|
30
|
+
ShowReads = Data.define(:id, :session, :turn_snapshot, :state, :event_seq, :event_id, :raw_messages) do
|
|
31
|
+
# The turn in progress, as the worker's snapshot has it (nil: none).
|
|
32
|
+
def current_turn = turn_snapshot && turn_snapshot["current_turn"]
|
|
33
|
+
|
|
34
|
+
def current_turn_id
|
|
35
|
+
turn = current_turn
|
|
36
|
+
turn.is_a?(Hash) ? turn["turn_id"] : nil
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
23
40
|
# GET /api/sessions/:id, the page's session read, and its light re-reads
|
|
24
41
|
# (?cards=1, ?tail=1, ?timing=1; ?brief=1 is SessionAccessRoutes'
|
|
25
42
|
# handle_brief_read), GET /api/sessions/:id/output, and the timing
|
|
@@ -33,7 +50,7 @@ module Samagotchi
|
|
|
33
50
|
private
|
|
34
51
|
|
|
35
52
|
def handle_show(req, id)
|
|
36
|
-
return
|
|
53
|
+
return participant_show_read(req, id) unless principal(req).owner?
|
|
37
54
|
|
|
38
55
|
# The page's light re-reads: never the whole conversation.
|
|
39
56
|
return handle_cards_read(id) if req.params["cards"] == "1"
|
|
@@ -46,35 +63,59 @@ module Samagotchi
|
|
|
46
63
|
# (The light re-reads come after a full read only.)
|
|
47
64
|
return scratch_refused(id) if session.scratch
|
|
48
65
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
owner_show(req, show_reads(id, session))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The reads a session's show is made of, whoever it is for (ShowReads).
|
|
70
|
+
# Read-only preview: selecting a session never spawns a worker.
|
|
71
|
+
# The worker is woken only on POST /turn (handle_turn) via
|
|
72
|
+
# SessionManager.resume_session + write_turn_input. This avoids
|
|
73
|
+
# replaying last_prompt and spamming workers on preview scrub.
|
|
74
|
+
# event_seq is nil when no live bridge, so the frontend stays
|
|
75
|
+
# silent until the first Send.
|
|
76
|
+
#
|
|
77
|
+
# A live worker's snapshot is the truth (the session file lags its
|
|
78
|
+
# Engine): the messages, the turn in progress and the prompts queued
|
|
79
|
+
# behind it, all at one event_seq the client streams on from.
|
|
80
|
+
# @return [ShowReads]
|
|
81
|
+
def show_reads(id, session)
|
|
59
82
|
live = bridge_get_json(id, "snapshot")
|
|
60
83
|
turn_snapshot = live && live["snapshot"]
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
84
|
+
state = live && live["session_state_snapshot"]
|
|
85
|
+
ShowReads.new(
|
|
86
|
+
id: id, session: session, turn_snapshot: turn_snapshot, state: state,
|
|
87
|
+
event_seq: live_or_saved(state, "event_seq") { bridge_event_seq(id) },
|
|
88
|
+
# The stream cursor `<seq>-<epoch>`: a later worker resets it
|
|
89
|
+
# instead of taking the seq as its own. Only the snapshot has it.
|
|
90
|
+
event_id: turn_snapshot && turn_snapshot["event_id"],
|
|
91
|
+
raw_messages: live_or_saved(turn_snapshot, "messages") { session.messages }
|
|
92
|
+
)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Every finished turn of +reads+ drawn from a line (a real one, else
|
|
96
|
+
# one made from its messages, #drawn_lines): its items are stamped,
|
|
97
|
+
# and its drawing goes in turn_drawings. The running turn has none yet.
|
|
98
|
+
def show_drawn_lines(reads, timing:, entries:)
|
|
99
|
+
drawn_lines(reads.id, reads.raw_messages, timing: timing, entries: entries, cwd: reads.session.working_directory,
|
|
100
|
+
skip_turn_id: reads.current_turn_id)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The owner's show of +reads+: the whole page's read (its cards,
|
|
104
|
+
# history, commands, recap, timing and question too).
|
|
105
|
+
def owner_show(req, reads)
|
|
106
|
+
id = reads.id
|
|
107
|
+
session = reads.session
|
|
108
|
+
turn_snapshot = reads.turn_snapshot
|
|
109
|
+
raw_messages = reads.raw_messages
|
|
64
110
|
owner = session_owner(id)
|
|
65
111
|
pending = shown_pending_question(session, turn_snapshot, owner)
|
|
66
112
|
# What the worker's server said it served for that model (after a turn);
|
|
67
113
|
# without a worker, what the session's analytics.json last saved.
|
|
68
|
-
persisted =
|
|
69
|
-
session_json = shown_session_json(session,
|
|
70
|
-
|
|
71
|
-
timing = timing_payload(id, live_metrics: snapshot && snapshot["metrics"], persisted: persisted)
|
|
72
|
-
# Every finished turn is drawn from a line (a real one, else one
|
|
73
|
-
# made from its messages): its items are stamped, and its drawing
|
|
74
|
-
# goes in turn_drawings. The running turn has none yet.
|
|
114
|
+
persisted = reads.state ? nil : read_analytics(id)
|
|
115
|
+
session_json = shown_session_json(session, reads.state, owner, persisted)
|
|
116
|
+
timing = timing_payload(id, live_metrics: reads.state && reads.state["metrics"], persisted: persisted)
|
|
75
117
|
raw_cards = live_or_saved(turn_snapshot, "cards") { saved_cards(id, messages: raw_messages) }
|
|
76
|
-
drawn =
|
|
77
|
-
skip_turn_id: current_turn.is_a?(Hash) ? current_turn["turn_id"] : nil)
|
|
118
|
+
drawn = show_drawn_lines(reads, timing: timing, entries: raw_cards)
|
|
78
119
|
messages = messages_for_display(raw_messages, cwd: session.working_directory, drawings: drawn)
|
|
79
120
|
json_response(200, {
|
|
80
121
|
session: session_json,
|
|
@@ -82,7 +123,7 @@ module Samagotchi
|
|
|
82
123
|
messages: messages,
|
|
83
124
|
**turn_drawings_field(drawn, messages),
|
|
84
125
|
failed_turn: failed_turn_for(session, raw_messages),
|
|
85
|
-
current_turn: current_turn,
|
|
126
|
+
current_turn: reads.current_turn,
|
|
86
127
|
# The page's join: the turn in progress and the queue behind it as
|
|
87
128
|
# the live events that draw them (TurnAccumulator.join_events, one
|
|
88
129
|
# unfold, here: any worker's snapshot works).
|
|
@@ -104,11 +145,11 @@ module Samagotchi
|
|
|
104
145
|
markdown_warning: @markdown_renderer.warning,
|
|
105
146
|
markdown_tip: @markdown_renderer.tip,
|
|
106
147
|
**viewer_capabilities(req),
|
|
148
|
+
# Who looks and what they may do (Viewer), as <body> says it at boot.
|
|
149
|
+
viewer: Viewer.for(principal(req)).to_h,
|
|
107
150
|
pending_question: pending,
|
|
108
|
-
last_event_seq:
|
|
109
|
-
|
|
110
|
-
# instead of taking the seq as its own. Only the snapshot has it.
|
|
111
|
-
last_event_id: turn_snapshot && turn_snapshot["event_id"],
|
|
151
|
+
last_event_seq: reads.event_seq,
|
|
152
|
+
last_event_id: reads.event_id,
|
|
112
153
|
timing: timing,
|
|
113
154
|
**objectives_field(id, raw_messages, turn_snapshot)
|
|
114
155
|
})
|
|
@@ -231,6 +272,12 @@ module Samagotchi
|
|
|
231
272
|
served_expected_by: snapshot["served_expected_by"])
|
|
232
273
|
# The model notes its prompt carries now (a /model there changes them).
|
|
233
274
|
session_json = session_json.merge(prompt_notes: snapshot["prompt_notes"]) if snapshot["prompt_notes"].is_a?(Array)
|
|
275
|
+
# The newest context estimate the worker has (a turn in flight: the
|
|
276
|
+
# last request's), so a page opened mid-turn shows the percent at
|
|
277
|
+
# once. Owner show only: a participant's show is ParticipantView's
|
|
278
|
+
# allowlist. A snapshot without the key (an older worker) sends
|
|
279
|
+
# nothing, so its page reads as it always did.
|
|
280
|
+
session_json = session_json.merge(context_status: snapshot["context_status"]) if snapshot.key?("context_status")
|
|
234
281
|
else
|
|
235
282
|
session_json = session_json.merge(saved_served_model(session, persisted))
|
|
236
283
|
end
|
|
@@ -26,7 +26,7 @@ module Samagotchi
|
|
|
26
26
|
# compares with ==, a 0.156 page reads them by name).
|
|
27
27
|
WEB_KEYS = %i[id status mode model_name working_directory created_at updated_at last_prompt short_id test_run
|
|
28
28
|
used_memory_names preloaded_memory_names muted_memory_names prompt_notes parent_id delegate
|
|
29
|
-
continues
|
|
29
|
+
continues first_preview title owner recap auto_title ctx_pct tokens memory_index side
|
|
30
30
|
archived project_root bridge_up worker_version worker_features worker_compat pending_question pending_card
|
|
31
31
|
last_turn verdicts verify_fresh last_verdict].freeze
|
|
32
32
|
|
|
@@ -84,9 +84,6 @@ module Samagotchi
|
|
|
84
84
|
# The session this one continues (the previous link of its
|
|
85
85
|
# chain), else nil: the lists draw chains from it.
|
|
86
86
|
continues: row.continues,
|
|
87
|
-
# A review session's {subjects, from} (SessionReview), else nil:
|
|
88
|
-
# the session bar offers Start review on the others.
|
|
89
|
-
review: session.review&.to_file,
|
|
90
87
|
first_preview: first_preview_for(session),
|
|
91
88
|
# The session's own name (/title), else nil: the pages show it in
|
|
92
89
|
# place of the first message.
|