Class: Kward::RPC::SessionManager
- Inherits:
-
Object
- Object
- Kward::RPC::SessionManager
- Includes:
- MemoryMethods
- Defined in:
- lib/kward/rpc/session_manager.rb
Overview
Owns RPC-visible session lifecycle, async turn queues, and frontend events.
Server handles JSON-RPC framing/dispatch; SessionManager handles the
product state behind those methods. It creates/resumes SessionStore
sessions, builds agents with RPC prompt bridges, serializes turn events for
clients, coordinates cancellation and follow-up queues, and integrates
memory/plugin hooks for RPC sessions.
Keep JSON-RPC wire shape normalization in the RPC::*Normalizer classes,
persistence in SessionStore, and model/tool behavior in Agent and
ToolRegistry. This class should coordinate those pieces rather than own
their low-level mechanics.
Defined Under Namespace
Classes: RpcSession, Turn
Constant Summary collapse
- RECENT_EVENT_LIMIT =
1_000- RPC_ATTACHMENT_MAX_BYTES =
AttachmentNormalizer::MAX_BYTES
- RPC_IMAGE_MIME_TYPES =
AttachmentNormalizer::IMAGE_MIME_TYPES
- STREAMING_BEHAVIORS =
["newTurn", "followUp", "steer"].freeze
- FOOTER_REFRESH_INTERVAL =
1.0- WORKER_STOP_TIMEOUT =
2.0- WORKER_STOP =
Object.new.freeze
Instance Method Summary collapse
- #active_sessions ⇒ Object
- #answer_question(session_id:, question_request_id:, answers:) ⇒ Object
- #answer_tool_approval(session_id:, approval_request_id:, approved:) ⇒ Object
- #available_models ⇒ Object
- #cancel_turn(turn_id:) ⇒ Object
- #capture_skill(session_path:) ⇒ Object
-
#cleanup_unused_sessions ⇒ Object
Closes idle empty sessions left behind by UI lifecycle transitions.
-
#clone_session(session_id:) ⇒ Object
Creates an independent copy of the current conversation branch.
-
#close_session(session_id:) ⇒ Object
Stops workers and removes an RPC session from the live session map.
-
#compact_session(session_id:, custom_instructions: "") ⇒ Object
Compacts an RPC session and emits start/end events for UI progress.
-
#create_session(workspace_root: Dir.pwd, name: nil, resume_last: false) ⇒ Object
Creates a new RPC session or resumes the remembered session when allowed.
- #current_model ⇒ Object
-
#delete_session(session_id:) ⇒ Object
Deletes the backing session file through the configured trash strategy.
-
#export_session(session_id:, path: nil, format: nil) ⇒ Object
Exports the current transcript in markdown or JSON format.
-
#fork_messages(session_id:) ⇒ Object
Lists user-message entries that can be used as fork points.
-
#fork_session(session_id:, entry_id:) ⇒ Object
Creates a new session from history before the selected user message.
- #in_flight_steer_supported? ⇒ Boolean
-
#initialize(server:, client: Client.new, config_dir: ConfigFiles.config_dir, config_manager: ConfigManager.new(config_path: File.join(config_dir, "config.json")), context_usage: ContextUsage.new, session_trash: SessionTrash.new, worker_stop_timeout: WORKER_STOP_TIMEOUT) ⇒ SessionManager
constructor
Creates an object for RPC session lifecycle and turn coordination.
- #list_sessions(workspace_root: Dir.pwd, limit: nil, current_session_path: nil) ⇒ Object
- #list_turns(session_id: nil, active: false) ⇒ Object
-
#navigate_tree(session_id:, entry_id:, summarize: false, custom_instructions: nil) ⇒ Object
Moves the active branch to a tree entry, optionally summarizing abandoned history.
- #plugin_commands ⇒ Object
-
#plugin_registry ⇒ Object
Returns the plugin registry shared by RPC sessions and plugin chats.
- #refresh_client_config ⇒ Object
- #reload_plugins ⇒ Object
-
#rename_session(session_id:, name:) ⇒ Object
Renames the persisted session attached to an RPC session id.
- #resume_session(path:, workspace_root: nil, include_transcript: false) ⇒ Object
- #run_command(session_id:, command:, arguments: "") ⇒ Object
- #run_plugin_command(session_id:, command:, arguments: "") ⇒ Object
- #run_skill_command(session_id:, name:) ⇒ Object
- #runtime_state(session_id:) ⇒ Object
- #runtime_stats(session_id:) ⇒ Object
- #save_captured_skill(content:, overwrite: false) ⇒ Object
- #session_model(rpc_session) ⇒ Object
- #session_modified_at(session) ⇒ Object
- #session_payload(rpc_session) ⇒ Object
-
#session_tree(session_id:) ⇒ Object
Returns the flattened session tree rows consumed by RPC clients.
-
#set_tree_label(session_id:, entry_id:, label: nil) ⇒ Object
Persists a label override for one tree entry.
-
#shutdown_sessions ⇒ Object
Stops all live RPC session workers during server shutdown.
- #skill_capture_sessions ⇒ Object
-
#start_turn(session_id:, input:, streaming_behavior: nil, attachments: [], options: {}, context: nil) ⇒ Object
Queues or starts an async model turn for an RPC session.
- #tool_schemas(session_id: nil) ⇒ Object
-
#transcript(session_id:) ⇒ Object
Returns the normalized transcript for the active RPC session.
- #turn_events(turn_id:, after_sequence: 0) ⇒ Object
- #turn_status(turn_id:) ⇒ Object
- #validate_workspace_root(root) ⇒ Object
Methods included from MemoryMethods
#memory_add, #memory_add_core, #memory_auto_summary_disable, #memory_auto_summary_enable, #memory_disable, #memory_enable, #memory_forget, #memory_inspect, #memory_list, #memory_manager, #memory_promote, #memory_relax, #memory_status, #memory_summarize, #memory_why
Constructor Details
#initialize(server:, client: Client.new, config_dir: ConfigFiles.config_dir, config_manager: ConfigManager.new(config_path: File.join(config_dir, "config.json")), context_usage: ContextUsage.new, session_trash: SessionTrash.new, worker_stop_timeout: WORKER_STOP_TIMEOUT) ⇒ SessionManager
Creates an object for RPC session lifecycle and turn coordination.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kward/rpc/session_manager.rb', line 73 def initialize( server:, client: Client.new, config_dir: ConfigFiles.config_dir, config_manager: ConfigManager.new(config_path: File.join(config_dir, "config.json")), context_usage: ContextUsage.new, session_trash: SessionTrash.new, worker_stop_timeout: WORKER_STOP_TIMEOUT ) @server = server @client = client @config_dir = config_dir @config_manager = config_manager @context_usage = context_usage @session_metrics = SessionMetrics.new(context_usage: context_usage) @session_trash = session_trash @worker_stop_timeout = worker_stop_timeout @sessions = {} @turns = {} @mutex = Mutex.new end |
Instance Method Details
#active_sessions ⇒ Object
155 156 157 |
# File 'lib/kward/rpc/session_manager.rb', line 155 def active_sessions { sessions: @mutex.synchronize { @sessions.values.map { |rpc_session| session_payload(rpc_session) } } } end |
#answer_question(session_id:, question_request_id:, answers:) ⇒ Object
460 461 462 463 464 |
# File 'lib/kward/rpc/session_manager.rb', line 460 def answer_question(session_id:, question_request_id:, answers:) rpc_session = fetch_session(session_id) rpc_session.prompt.answer(question_request_id, answers) { ok: true } end |
#answer_tool_approval(session_id:, approval_request_id:, approved:) ⇒ Object
466 467 468 469 470 |
# File 'lib/kward/rpc/session_manager.rb', line 466 def answer_tool_approval(session_id:, approval_request_id:, approved:) rpc_session = fetch_session(session_id) rpc_session.prompt.answer_tool_approval(approval_request_id, approved: approved) { ok: true } end |
#available_models ⇒ Object
513 514 515 516 517 518 519 |
# File 'lib/kward/rpc/session_manager.rb', line 513 def available_models models = @client.respond_to?(:available_models) ? Array(@client.available_models) : [] normalized = models.map { |model| normalize_model(model) } current = current_model normalized << current if normalized.none? { |model| model[:provider] == current[:provider] && model[:id] == current[:id] } normalized end |
#cancel_turn(turn_id:) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/kward/rpc/session_manager.rb', line 426 def cancel_turn(turn_id:) turn = fetch_turn(turn_id) queued, event = turn.mutex.synchronize do turn.cancel_requested = true [turn.status == "queued", append_turn_event_locked(turn, "turnCancelRequested", {})] end @server.notify("turn/event", event) turn.cancellation&.cancel! finish_turn(turn, "canceled") if queued turn_payload(turn) end |
#capture_skill(session_path:) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/kward/rpc/session_manager.rb', line 171 def capture_skill(session_path:) capture = Skills::Capture.new( session_store: SessionStore.new(config_dir: @config_dir), client: @client, config_dir: @config_dir ) draft = capture.generate(session_path) { content: draft.content, sourcePath: draft.source_path, name: draft.name, description: draft.description } end |
#cleanup_unused_sessions ⇒ Object
Closes idle empty sessions left behind by UI lifecycle transitions.
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/kward/rpc/session_manager.rb', line 358 def cleanup_unused_sessions rpc_sessions = @mutex.synchronize { @sessions.values.dup } rpc_sessions.reverse_each do |rpc_session| next unless session_idle?(rpc_session) next unless rpc_session.session.respond_to?(:delete_if_unused) next unless rpc_session.session.delete_if_unused remove_live_session(rpc_session) end { closed: true } end |
#clone_session(session_id:) ⇒ Object
Creates an independent copy of the current conversation branch.
213 214 215 216 217 218 219 220 221 |
# File 'lib/kward/rpc/session_manager.rb', line 213 def clone_session(session_id:) source = fetch_session(session_id) session, conversation = source.store.create_independent_from_conversation(source.conversation, parent_session: source.session) rpc_session = build_rpc_session(source.store, session, conversation, source.workspace_root) remember_session(rpc_session) cleanup_other_unused_sessions(rpc_session) (rpc_session) session_payload(rpc_session) end |
#close_session(session_id:) ⇒ Object
Stops workers and removes an RPC session from the live session map.
351 352 353 354 355 |
# File 'lib/kward/rpc/session_manager.rb', line 351 def close_session(session_id:) rpc_session = fetch_session(session_id) close_rpc_session(rpc_session) { closed: true } end |
#compact_session(session_id:, custom_instructions: "") ⇒ Object
Compacts an RPC session and emits start/end events for UI progress.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/kward/rpc/session_manager.rb', line 224 def compact_session(session_id:, custom_instructions: "") rpc_session = fetch_session(session_id) emit_session_event(rpc_session, "compactionStart", {}) result = Compactor.new(conversation: rpc_session.conversation, client: @client, settings: compaction_settings).compact(custom_instructions: custom_instructions) payload = { summary: result.summary, firstKeptEntryId: result.first_kept_entry_id, tokensBefore: result.tokens_before, details: result.details }.compact emit_session_event(rpc_session, "compactionEnd", { result: payload, aborted: false, willRetry: false, errorMessage: nil }) payload rescue StandardError => e emit_session_event(rpc_session, "compactionEnd", { result: nil, aborted: true, willRetry: false, errorMessage: e. }) if rpc_session raise e end |
#create_session(workspace_root: Dir.pwd, name: nil, resume_last: false) ⇒ Object
Creates a new RPC session or resumes the remembered session when allowed.
Returns the normalized session payload expected by RPC clients. The RPC session id is separate from the persisted session id so one persisted file can be closed and reopened by different client connections.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/kward/rpc/session_manager.rb', line 100 def create_session(workspace_root: Dir.pwd, name: nil, resume_last: false) workspace_root = validate_workspace_root(workspace_root) store = SessionStore.new(config_dir: @config_dir, cwd: workspace_root) if resume_last && session_auto_resume_enabled? && name.to_s.strip.empty? path = store.remembered_last_session_path return resume_session(path: path, workspace_root: workspace_root, include_transcript: true) if path end conversation = new_conversation(workspace_root: workspace_root) session = store.create(provider: conversation.provider, model: conversation.model, reasoning_effort: conversation.reasoning_effort) session.rename(name) unless name.to_s.strip.empty? session.attach(conversation) rpc_session = build_rpc_session(store, session, conversation, workspace_root) remember_session(rpc_session) cleanup_other_unused_sessions(rpc_session) (rpc_session) session_payload(rpc_session) end |
#current_model ⇒ Object
521 522 523 524 525 526 527 |
# File 'lib/kward/rpc/session_manager.rb', line 521 def current_model provider = @client.respond_to?(:current_provider) ? @client.current_provider : nil model = @client.respond_to?(:current_model) ? @client.current_model : nil reasoning_effort = @client.respond_to?(:current_reasoning_effort) ? @client.current_reasoning_effort : nil context_window = @client.respond_to?(:current_context_window) ? @client.current_context_window : nil ModelInfo.current_payload(provider: provider, model: model, reasoning_effort: reasoning_effort, context_window: context_window) end |
#delete_session(session_id:) ⇒ Object
Deletes the backing session file through the configured trash strategy.
342 343 344 345 346 347 348 |
# File 'lib/kward/rpc/session_manager.rb', line 342 def delete_session(session_id:) rpc_session = fetch_session(session_id) path = rpc_session.session.path close_rpc_session(rpc_session, delete_unused: false) deleted = @session_trash.delete(path) { deleted: deleted, path: path } end |
#export_session(session_id:, path: nil, format: nil) ⇒ Object
Exports the current transcript in markdown or JSON format.
332 333 334 335 336 337 338 339 |
# File 'lib/kward/rpc/session_manager.rb', line 332 def export_session(session_id:, path: nil, format: nil) rpc_session = fetch_session(session_id) format = export_format(format) path = export_path(rpc_session, path, format) content = export_content(rpc_session.conversation, format) File.write(path, content) { path: path, format: format } end |
#fork_messages(session_id:) ⇒ Object
Lists user-message entries that can be used as fork points.
242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/kward/rpc/session_manager.rb', line 242 def (session_id:) rpc_session = fetch_session(session_id) { messages: session_tree_helper(rpc_session).entries.filter_map do |record| = record["message"] next unless .is_a?(Hash) && () == "user" { entryId: record["id"], text: () } end } end |
#fork_session(session_id:, entry_id:) ⇒ Object
Creates a new session from history before the selected user message.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/kward/rpc/session_manager.rb', line 255 def fork_session(session_id:, entry_id:) source = fetch_session(session_id) tree = session_tree_helper(source) entries = tree.entries resolved_entry_id = tree.resolve_entry_id(entry_id, entries: entries) selected_index = entries.index { |record| record["id"].to_s == resolved_entry_id.to_s } selected = selected_index && entries[selected_index] raise ArgumentError, "Unknown fork entryId: #{entry_id}" unless selected = selected["message"] raise ArgumentError, "Entry is not forkable: #{entry_id}" unless .is_a?(Hash) && () == "user" session, conversation = source.store.( entries[0...selected_index].filter_map { |record| record["message"] }, provider: source.conversation.provider, model: source.conversation.model, reasoning_effort: source.conversation.reasoning_effort, parent_session: source.session ) rpc_session = build_rpc_session(source.store, session, conversation, source.workspace_root) remember_session(rpc_session) cleanup_other_unused_sessions(rpc_session) { session: session_payload(rpc_session), text: (), cancelled: false } end |
#in_flight_steer_supported? ⇒ Boolean
544 545 546 |
# File 'lib/kward/rpc/session_manager.rb', line 544 def in_flight_steer_supported? supports_in_flight_steer? end |
#list_sessions(workspace_root: Dir.pwd, limit: nil, current_session_path: nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/kward/rpc/session_manager.rb', line 142 def list_sessions(workspace_root: Dir.pwd, limit: nil, current_session_path: nil) root = validate_workspace_root(workspace_root) store = SessionStore.new(config_dir: @config_dir, cwd: root) requested_limit = limit.to_i if limit requested_limit = nil unless requested_limit&.positive? live_paths = @mutex.synchronize do @sessions.values.filter_map { |rpc_session| rpc_session.session.path if rpc_session.workspace_root == root } end live_paths << current_session_path unless current_session_path.to_s.empty? store.recent(limit: requested_limit, keep_empty_path: live_paths) .map { |info| session_info_payload(info, workspace_root: root) } end |
#list_turns(session_id: nil, active: false) ⇒ Object
453 454 455 456 457 458 |
# File 'lib/kward/rpc/session_manager.rb', line 453 def list_turns(session_id: nil, active: false) turns = @mutex.synchronize { @turns.values.dup } turns.select! { |turn| turn.session_id == session_id.to_s } if session_id turns.select! { |turn| ["queued", "running"].include?(turn.status) } if active { turns: turns.map { |turn| turn_payload(turn) } } end |
#navigate_tree(session_id:, entry_id:, summarize: false, custom_instructions: nil) ⇒ Object
Moves the active branch to a tree entry, optionally summarizing abandoned history.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/kward/rpc/session_manager.rb', line 299 def navigate_tree(session_id:, entry_id:, summarize: false, custom_instructions: nil) rpc_session = fetch_session(session_id) tree = session_tree_helper(rpc_session) entries = tree.entries resolved_entry_id = tree.resolve_entry_id(entry_id, entries: entries) entry = rpc_session.store.session_entry(rpc_session.session.path, resolved_entry_id) raise ArgumentError, "Unknown tree entryId: #{entry_id}" unless entry raise ArgumentError, "Tree entry is not selectable: #{entry_id}" unless tree.selectable_entry?(entry) = entry["message"] user_entry = tree.user_entry?(entry) target_leaf = user_entry ? entry["parentId"] : entry["id"] editor_text = user_entry ? () : nil previous_leaf = rpc_session.session.leaf_id if summarize summary = summarize_branch(rpc_session, from_id: previous_leaf, to_id: target_leaf, custom_instructions: custom_instructions) target_leaf = rpc_session.session.append_branch_summary(target_leaf, from_id: previous_leaf, summary: summary, details: {}) elsif target_leaf rpc_session.session.branch(target_leaf) end reload_rpc_session(rpc_session) { session: session_payload(rpc_session), editorText: editor_text, cancelled: false, aborted: false }.compact end |
#plugin_commands ⇒ Object
509 510 511 |
# File 'lib/kward/rpc/session_manager.rb', line 509 def plugin_commands plugin_registry.commands end |
#plugin_registry ⇒ Object
Returns the plugin registry shared by RPC sessions and plugin chats.
201 202 203 |
# File 'lib/kward/rpc/session_manager.rb', line 201 def plugin_registry @plugin_registry ||= PluginRegistry.load(reserved_commands: reserved_plugin_command_names) end |
#refresh_client_config ⇒ Object
591 592 593 594 595 |
# File 'lib/kward/rpc/session_manager.rb', line 591 def refresh_client_config @client.reload_config if @client.respond_to?(:reload_config) refresh_session_runtime_contexts refresh_session_tool_registries end |
#reload_plugins ⇒ Object
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/kward/rpc/session_manager.rb', line 597 def reload_plugins registry = PluginRegistry.load(reserved_commands: reserved_plugin_command_names) sessions = @mutex.synchronize do @plugin_registry = registry @sessions.values end sessions.each do |rpc_session| rpc_session.conversation.plugin_registry = registry if rpc_session.conversation.respond_to?(:plugin_registry=) rpc_session.conversation. if rpc_session.conversation.respond_to?(:refresh_system_message!) rebuild_session_tools(rpc_session) if registry. (rpc_session) (rpc_session) else (rpc_session) (rpc_session) end end end |
#rename_session(session_id:, name:) ⇒ Object
Renames the persisted session attached to an RPC session id.
206 207 208 209 210 |
# File 'lib/kward/rpc/session_manager.rb', line 206 def rename_session(session_id:, name:) rpc_session = fetch_session(session_id) rpc_session.session.rename(name) session_payload(rpc_session) end |
#resume_session(path:, workspace_root: nil, include_transcript: false) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/kward/rpc/session_manager.rb', line 119 def resume_session(path:, workspace_root: nil, include_transcript: false) root = validate_workspace_root(workspace_root || Dir.pwd) store = SessionStore.new(config_dir: @config_dir, cwd: root) location = store.session_location(path) root = validate_workspace_root(location[:cwd]) store = SessionStore.new(config_dir: @config_dir, cwd: root) session, conversation = store.load( location[:path], workspace: configured_workspace(root), provider: current_model[:provider], model: current_model_id, reasoning_effort: current_reasoning_effort ) rpc_session = build_rpc_session(store, session, conversation, root) remember_session(rpc_session) cleanup_other_unused_sessions(rpc_session) (rpc_session) payload = session_payload(rpc_session) payload[:messages] = TranscriptNormalizer.new(rpc_session.conversation.).normalize if include_transcript payload[:resumed] = true payload end |
#run_command(session_id:, command:, arguments: "") ⇒ Object
472 473 474 475 476 477 478 479 |
# File 'lib/kward/rpc/session_manager.rb', line 472 def run_command(session_id:, command:, arguments: "") name = command.to_s.delete_prefix("/") return { ok: false, error: "unsupported", reason: "clientClipboardOwnedByUi" } if name == "copy" return run_skill_command(session_id: session_id, name: name.delete_prefix("skill:")) if name.start_with?("skill:") return run_skill_command(session_id: session_id, name: arguments) if name == "skill" run_plugin_command(session_id: session_id, command: name, arguments: arguments) end |
#run_plugin_command(session_id:, command:, arguments: "") ⇒ Object
499 500 501 502 503 504 505 506 507 |
# File 'lib/kward/rpc/session_manager.rb', line 499 def run_plugin_command(session_id:, command:, arguments: "") rpc_session = fetch_session(session_id) command = plugin_registry.command_for(command.to_s.delete_prefix("/")) || raise(ArgumentError, "Unknown plugin command: #{command}") output = [] context = plugin_context(rpc_session, args: arguments.to_s, say_callback: lambda { || output << .to_s }) result = command.handler.call(arguments.to_s, context) output = rpc_session.plugin_output.shift(rpc_session.plugin_output.length) + output { command: command.name, output: output, result: result.nil? ? nil : result.to_s } end |
#run_skill_command(session_id:, name:) ⇒ Object
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/kward/rpc/session_manager.rb', line 481 def run_skill_command(session_id:, name:) skill_name = name.to_s.strip raise ArgumentError, "Missing skill name" if skill_name.empty? rpc_session = fetch_session(session_id) tool_call = { "id" => "skill_#{skill_name.gsub(/[^a-zA-Z0-9_-]/, "_")}", "type" => "function", "function" => { "name" => "read_skill", "arguments" => JSON.dump({ name: skill_name }) } } rpc_session.conversation.append_assistant("role" => "assistant", "content" => nil, "tool_calls" => [tool_call]) result = rpc_session.tool_registry.dispatch(tool_call, rpc_session.conversation) { command: "skill:#{skill_name}", output: [result.start_with?("Error:") ? result : "Activated skill: #{skill_name}"], result: result } end |
#runtime_state(session_id:) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/kward/rpc/session_manager.rb', line 548 def runtime_state(session_id:) rpc_session = fetch_session(session_id) model = session_model(rpc_session) compaction_settings = self.compaction_settings auto_compaction_reserve_tokens = compaction_reserve_tokens( context_window: model[:contextWindow], compaction_settings: compaction_settings ) session = session_payload(rpc_session) RuntimePayloads.state( session: session, model: model, streaming: streaming?(rpc_session), steering_supported: supports_in_flight_steer?, auto_compaction_reserve_tokens: auto_compaction_reserve_tokens, active_persona_label: active_persona_label(rpc_session), message_count: @session_metrics.(rpc_session.conversation), pending_count: pending_turn_count(rpc_session.id), compaction_enabled: compaction_settings.enabled, workspace_guardrails_enabled: workspace_guardrails_enabled? ) end |
#runtime_stats(session_id:) ⇒ Object
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/kward/rpc/session_manager.rb', line 571 def runtime_stats(session_id:) rpc_session = fetch_session(session_id) session = session_payload(rpc_session) counts = @session_metrics.(rpc_session.conversation) model = session_model(rpc_session) compaction_settings = self.compaction_settings auto_compaction_reserve_tokens = compaction_reserve_tokens( context_window: model[:contextWindow], compaction_settings: compaction_settings ) RuntimePayloads.stats( session: session, counts: counts, model: model, auto_compaction_reserve_tokens: auto_compaction_reserve_tokens, context_usage: @session_metrics.context_usage(rpc_session, model, client: @client), compaction_enabled: compaction_settings.enabled ) end |
#save_captured_skill(content:, overwrite: false) ⇒ Object
181 182 183 184 185 186 187 188 189 |
# File 'lib/kward/rpc/session_manager.rb', line 181 def save_captured_skill(content:, overwrite: false) capture = Skills::Capture.new( session_store: SessionStore.new(config_dir: @config_dir), client: @client, config_dir: @config_dir ) draft = capture.save(content, overwrite: overwrite) { path: capture.skill_path(draft.name), name: draft.name, description: draft.description } end |
#session_model(rpc_session) ⇒ Object
529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/kward/rpc/session_manager.rb', line 529 def session_model(rpc_session) current = current_model provider = rpc_session.conversation.provider || current[:provider] model = rpc_session.conversation.model || current[:id] reasoning_effort = rpc_session.conversation.reasoning_effort || current_reasoning_effort reasoning_effort = nil unless ModelInfo.reasoning_supported?(provider, model) context_window = context_window_for(provider, model) ModelInfo.current_payload( provider: provider, model: model, reasoning_effort: reasoning_effort, context_window: context_window ) end |
#session_modified_at(session) ⇒ Object
625 626 627 |
# File 'lib/kward/rpc/session_manager.rb', line 625 def session_modified_at(session) File.exist?(session.path) ? File.mtime(session.path) : nil end |
#session_payload(rpc_session) ⇒ Object
617 618 619 620 621 622 623 |
# File 'lib/kward/rpc/session_manager.rb', line 617 def session_payload(rpc_session) RuntimePayloads.session( rpc_session, modified_at: session_modified_at(rpc_session.session), active_persona_label: active_persona_label(rpc_session) ) end |
#session_tree(session_id:) ⇒ Object
Returns the flattened session tree rows consumed by RPC clients.
286 287 288 289 |
# File 'lib/kward/rpc/session_manager.rb', line 286 def session_tree(session_id:) rpc_session = fetch_session(session_id) { items: flatten_session_tree(rpc_session) } end |
#set_tree_label(session_id:, entry_id:, label: nil) ⇒ Object
Persists a label override for one tree entry.
292 293 294 295 296 |
# File 'lib/kward/rpc/session_manager.rb', line 292 def set_tree_label(session_id:, entry_id:, label: nil) rpc_session = fetch_session(session_id) rpc_session.session.append_label_change(entry_id, label) { ok: true } end |
#shutdown_sessions ⇒ Object
Stops all live RPC session workers during server shutdown.
371 372 373 374 375 |
# File 'lib/kward/rpc/session_manager.rb', line 371 def shutdown_sessions rpc_sessions = @mutex.synchronize { @sessions.values.dup } rpc_sessions.reverse_each { |rpc_session| close_rpc_session(rpc_session) } { closed: true } end |
#skill_capture_sessions ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/kward/rpc/session_manager.rb', line 159 def skill_capture_sessions SessionStore.new(config_dir: @config_dir).capture_candidates.map do |session| { path: session.path, workspaceRoot: session.cwd, name: session.name, firstMessage: session., modifiedAt: session.modified_at.utc.iso8601(3) } end end |
#start_turn(session_id:, input:, streaming_behavior: nil, attachments: [], options: {}, context: nil) ⇒ Object
Queues or starts an async model turn for an RPC session.
streaming_behavior controls busy-session behavior: create a new turn,
queue a follow-up, or steer the running turn when the active provider
supports native steering. The returned turn id is used for status,
cancellation, and event replay.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/kward/rpc/session_manager.rb', line 389 def start_turn(session_id:, input:, streaming_behavior: nil, attachments: [], options: {}, context: nil) rpc_session = fetch_session(session_id) = () normalized_context = TurnContext.normalize(context) = () plugin_command, plugin_arguments = plugin_command_turn(input, ) display_input = input.to_s if input.is_a?(String) content = plugin_command ? input.to_s : user_turn_content(turn_input_with_context((input), normalized_context), ) streaming_behavior = validate_streaming_behavior(default_streaming_behavior(rpc_session, streaming_behavior), rpc_session: rpc_session) if streaming_behavior == "steer" return steer_running_turn(rpc_session, content) end turn = Turn.new( id: SecureRandom.uuid, session_id: rpc_session.id, input: content, display_input: display_input, status: "queued", cancel_requested: false, cancellation: Cancellation.new, created_at: now, events: [], next_sequence: 1, streaming_behavior: streaming_behavior, plugin_command_name: plugin_command&.name, plugin_arguments: plugin_arguments, options: , tool_registry: scoped_tool_registry(rpc_session, ), mutex: Mutex.new ) @mutex.synchronize { @turns[turn.id] = turn } rpc_session.queue << turn.id ensure_worker(rpc_session) emit_turn_event(turn, "turnQueued", { status: "queued" }) turn_payload(turn) end |
#tool_schemas(session_id: nil) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/kward/rpc/session_manager.rb', line 191 def tool_schemas(session_id: nil) registry = if session_id fetch_session(session_id).tool_registry else build_tool_registry(Dir.pwd, nil) end { tools: registry.schemas } end |
#transcript(session_id:) ⇒ Object
Returns the normalized transcript for the active RPC session.
378 379 380 381 |
# File 'lib/kward/rpc/session_manager.rb', line 378 def transcript(session_id:) rpc_session = fetch_session(session_id) { session: session_payload(rpc_session), messages: TranscriptNormalizer.new(rpc_session.conversation.).normalize } end |
#turn_events(turn_id:, after_sequence: 0) ⇒ Object
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/kward/rpc/session_manager.rb', line 442 def turn_events(turn_id:, after_sequence: 0) turn = fetch_turn(turn_id) after_sequence = after_sequence.to_i turn.mutex.synchronize do { turn: turn_payload_unlocked(turn), events: turn.events.select { |event| event[:sequence].to_i > after_sequence } } end end |
#turn_status(turn_id:) ⇒ Object
438 439 440 |
# File 'lib/kward/rpc/session_manager.rb', line 438 def turn_status(turn_id:) turn_payload(fetch_turn(turn_id)) end |