kward 0.85.0 → 0.86.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 +130 -0
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +6 -2
- data/README.md +3 -3
- data/Rakefile +3 -0
- data/doc/agent-tools.md +11 -0
- data/doc/api.md +4 -1
- data/doc/composer.md +4 -3
- data/doc/configuration.md +60 -11
- data/doc/extensibility.md +2 -0
- data/doc/getting-started.md +3 -3
- data/doc/git.md +1 -1
- data/doc/memory.md +1 -1
- data/doc/plugins.md +40 -5
- data/doc/providers.md +10 -4
- data/doc/rpc.md +125 -7
- data/doc/session-management.md +36 -1
- data/doc/shell.md +643 -27
- data/doc/tabs.md +2 -1
- data/doc/troubleshooting.md +1 -1
- data/doc/usage.md +21 -11
- data/examples/plugins/openrouter_proxy/README.md +103 -0
- data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
- data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
- data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
- data/examples/plugins/openrouter_proxy/server.rb +186 -0
- data/examples/plugins/space_invaders.rb +0 -1
- data/exe/kward +2 -2
- data/exe/kwsh +7 -0
- data/kward.gemspec +3 -1
- data/lib/kward/agent.rb +43 -9
- data/lib/kward/cli/commands.rb +41 -11
- data/lib/kward/cli/interactive_turn.rb +10 -0
- data/lib/kward/cli/plugins.rb +5 -1
- data/lib/kward/cli/prompt_interface.rb +4 -1
- data/lib/kward/cli/rendering.rb +21 -28
- data/lib/kward/cli/runtime_helpers.rb +52 -34
- data/lib/kward/cli/settings/model.rb +1 -12
- data/lib/kward/cli/tabs.rb +91 -18
- data/lib/kward/cli/tool_summaries.rb +2 -0
- data/lib/kward/cli/transcript_formatter.rb +3 -10
- data/lib/kward/cli/worktrees.rb +1 -0
- data/lib/kward/cli.rb +56 -7
- data/lib/kward/config/core.rb +16 -5
- data/lib/kward/config/extensions.rb +3 -0
- data/lib/kward/config/paths.rb +11 -0
- data/lib/kward/config/settings.rb +7 -0
- data/lib/kward/conversation.rb +11 -1
- data/lib/kward/entrypoint.rb +14 -0
- data/lib/kward/events.rb +2 -0
- data/lib/kward/generated_images.rb +237 -0
- data/lib/kward/model/catalog.rb +6 -3
- data/lib/kward/model/client.rb +105 -36
- data/lib/kward/model/codex_images.rb +83 -0
- data/lib/kward/model/copilot_models.rb +8 -4
- data/lib/kward/model/model_info.rb +27 -9
- data/lib/kward/model/openrouter_model_cache.rb +4 -2
- data/lib/kward/model/payloads.rb +6 -5
- data/lib/kward/model/sources.rb +3 -1
- data/lib/kward/model/stream_parser.rb +4 -7
- data/lib/kward/pan/index.html.erb +8 -1
- data/lib/kward/pan/server.rb +13 -1
- data/lib/kward/plugins/codex_responses.rb +73 -0
- data/lib/kward/plugins/host.rb +16 -0
- data/lib/kward/plugins/http_server.rb +83 -0
- data/lib/kward/plugins/registry.rb +39 -0
- data/lib/kward/prompt_interface/composer_controller.rb +0 -1
- data/lib/kward/prompt_interface/composer_renderer.rb +159 -72
- data/lib/kward/prompt_interface/editor/controller.rb +1 -1
- data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
- data/lib/kward/prompt_interface/file_overlay.rb +3 -3
- data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
- data/lib/kward/prompt_interface/git_prompt.rb +18 -11
- data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
- data/lib/kward/prompt_interface/interactive/state.rb +18 -20
- data/lib/kward/prompt_interface/key_handler.rb +108 -25
- data/lib/kward/prompt_interface/layout.rb +11 -1
- data/lib/kward/prompt_interface/overlay_renderer.rb +25 -8
- data/lib/kward/prompt_interface/project_browser.rb +27 -54
- data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
- data/lib/kward/prompt_interface/question_prompt.rb +19 -28
- data/lib/kward/prompt_interface/runtime_state.rb +31 -9
- data/lib/kward/prompt_interface/screen.rb +24 -21
- data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
- data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
- data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
- data/lib/kward/prompt_interface.rb +109 -39
- data/lib/kward/prompts.rb +7 -1
- data/lib/kward/pty/adaptive_output_sink.rb +38 -4
- data/lib/kward/pty/interactive_runner.rb +20 -28
- data/lib/kward/pty/transcript_normalizer.rb +65 -21
- data/lib/kward/rpc/config_manager.rb +3 -0
- data/lib/kward/rpc/server.rb +27 -5
- data/lib/kward/rpc/session_manager.rb +62 -17
- data/lib/kward/rpc/transcript_normalizer.rb +11 -7
- data/lib/kward/sessions/store.rb +30 -3
- data/lib/kward/shell/kwsh.rb +27 -7
- data/lib/kward/shell/kwshrc.rb +11 -2
- data/lib/kward/shell/native_arithmetic.rb +255 -0
- data/lib/kward/shell/native_arrays.rb +102 -0
- data/lib/kward/shell/native_braces.rb +96 -0
- data/lib/kward/shell/native_builtins.rb +476 -0
- data/lib/kward/shell/native_completion.rb +169 -0
- data/lib/kward/shell/native_condition.rb +75 -0
- data/lib/kward/shell/native_directory.rb +116 -0
- data/lib/kward/shell/native_exec_state.rb +73 -0
- data/lib/kward/shell/native_expansion.rb +252 -0
- data/lib/kward/shell/native_fields.rb +62 -0
- data/lib/kward/shell/native_glob.rb +96 -0
- data/lib/kward/shell/native_history.rb +242 -0
- data/lib/kward/shell/native_interactive.rb +164 -0
- data/lib/kward/shell/native_invocation.rb +42 -0
- data/lib/kward/shell/native_jobs.rb +250 -0
- data/lib/kward/shell/native_line_editor.rb +382 -0
- data/lib/kward/shell/native_line_terminal.rb +286 -0
- data/lib/kward/shell/native_locale.rb +109 -0
- data/lib/kward/shell/native_locals.rb +57 -0
- data/lib/kward/shell/native_pattern.rb +200 -0
- data/lib/kward/shell/native_process.rb +279 -0
- data/lib/kward/shell/native_program.rb +40 -0
- data/lib/kward/shell/native_programmable_completion.rb +115 -0
- data/lib/kward/shell/native_prompt_format.rb +89 -0
- data/lib/kward/shell/native_runtime.rb +964 -0
- data/lib/kward/shell/native_substitution.rb +110 -0
- data/lib/kward/shell/native_syntax.rb +946 -0
- data/lib/kward/shell/native_traps.rb +196 -0
- data/lib/kward/shell/native_vi.rb +407 -0
- data/lib/kward/shell/native_vi_terminal.rb +19 -0
- data/lib/kward/shell/native_worker.rb +82 -0
- data/lib/kward/shell/persistent_session.rb +184 -190
- data/lib/kward/shell/prompt.rb +14 -0
- data/lib/kward/shell/prompt_agent.rb +17 -0
- data/lib/kward/shell/script_runner.rb +244 -0
- data/lib/kward/shell/standalone_agent.rb +31 -0
- data/lib/kward/shell/standalone_link.rb +78 -0
- data/lib/kward/shell/standalone_terminal.rb +287 -0
- data/lib/kward/terminal/keys.rb +22 -1
- data/lib/kward/terminal/locale_text.rb +29 -0
- data/lib/kward/terminal/sequences.rb +45 -4
- data/lib/kward/tools/datetime.rb +85 -0
- data/lib/kward/tools/generate_image.rb +36 -0
- data/lib/kward/tools/registry.rb +35 -9
- data/lib/kward/tools/tool_call.rb +3 -1
- data/lib/kward/transcripts/markdown_transcript.rb +8 -0
- data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
- data/lib/kward/transport/gateway.rb +2 -1
- data/lib/kward/turn_summary.rb +68 -0
- data/lib/kward/version.rb +1 -1
- data/lib/main.rb +6 -3
- metadata +84 -1
data/lib/kward/model/client.rb
CHANGED
|
@@ -15,6 +15,7 @@ require_relative "copilot_models"
|
|
|
15
15
|
require_relative "model_info"
|
|
16
16
|
require_relative "sources"
|
|
17
17
|
require_relative "payloads"
|
|
18
|
+
require_relative "codex_images"
|
|
18
19
|
require_relative "../telemetry/logger"
|
|
19
20
|
require_relative "stream_parser"
|
|
20
21
|
|
|
@@ -110,7 +111,7 @@ module Kward
|
|
|
110
111
|
@local_models = nil
|
|
111
112
|
end
|
|
112
113
|
|
|
113
|
-
def chat(messages, tools: [], on_reasoning_delta: nil, on_reasoning_boundary: nil, on_assistant_delta: nil, on_retry: nil, cancellation: nil, steering: nil, max_tokens: nil, provider: nil, model: nil, reasoning: nil, provider_required: false)
|
|
114
|
+
def chat(messages, tools: [], on_reasoning_delta: nil, on_reasoning_boundary: nil, on_assistant_delta: nil, on_retry: nil, cancellation: nil, steering: nil, max_tokens: nil, provider: nil, model: nil, reasoning: nil, provider_required: false, text_format: nil)
|
|
114
115
|
cancellation&.raise_if_cancelled!
|
|
115
116
|
requested_provider = provider
|
|
116
117
|
validate_required_provider!(requested_provider) if provider_required
|
|
@@ -126,7 +127,8 @@ module Kward
|
|
|
126
127
|
current_model = resolved_copilot_chat_model(current_model) if resolved_provider == "Copilot" && model.nil?
|
|
127
128
|
|
|
128
129
|
validate_image_support!(resolved_provider, current_model, messages)
|
|
129
|
-
|
|
130
|
+
tools = tools.reject { |tool| ToolCall.name(tool) == "generate_image" } if resolved_provider != "Codex"
|
|
131
|
+
request_body = JSON.dump(request_body_payload(resolved_provider, messages, tools, max_tokens: max_tokens, model: current_model, reasoning: reasoning, text_format: text_format))
|
|
130
132
|
with_retries(resolved_provider, current_model, request_bytes: request_body.bytesize, on_retry: on_retry, cancellation: cancellation) do
|
|
131
133
|
request_started_at = @telemetry_logger.monotonic_now
|
|
132
134
|
message = nil
|
|
@@ -166,6 +168,16 @@ module Kward
|
|
|
166
168
|
raise e
|
|
167
169
|
end
|
|
168
170
|
|
|
171
|
+
# Generates via the Codex subscription only, never using a public API key.
|
|
172
|
+
def generate_codex_image(prompt, turn_id: nil, cancellation: nil)
|
|
173
|
+
_url, token, provider, account_id = credentials(provider: "Codex")
|
|
174
|
+
unless provider == "Codex" && !token.to_s.empty?
|
|
175
|
+
raise CodexImages::Error, "Sign in to Codex to generate images."
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
CodexImages.generate(prompt, token: token, account_id: account_id, turn_id: turn_id, cancellation: cancellation)
|
|
179
|
+
end
|
|
180
|
+
|
|
169
181
|
# Returns the active provider label after applying env/config/credential fallback rules.
|
|
170
182
|
def current_provider
|
|
171
183
|
_url, _token, provider = credentials
|
|
@@ -226,11 +238,12 @@ module Kward
|
|
|
226
238
|
metadata = openrouter_cached_model_entries.find { |entry| (entry["id"] || entry[:id]).to_s == id }
|
|
227
239
|
model_entry("OpenRouter", id, current: provider == "OpenRouter" && openrouter_model == id, metadata: metadata)
|
|
228
240
|
end
|
|
241
|
+
models << model_entry("OpenRouter", openrouter_model, current: provider == "OpenRouter") unless openrouter_model.to_s.empty? || openrouter_choices.include?(openrouter_model)
|
|
229
242
|
end
|
|
230
243
|
|
|
231
244
|
if provider_logged_in?("Copilot")
|
|
232
245
|
copilot_model = model_for("Copilot")
|
|
233
|
-
copilot_choices = provider == "Copilot" ? copilot_model_choices : static_copilot_model_choices
|
|
246
|
+
copilot_choices = provider == "Copilot" || @copilot_models ? copilot_model_choices : static_copilot_model_choices
|
|
234
247
|
models += copilot_choices.map do |id|
|
|
235
248
|
model_entry("Copilot", id, current: provider == "Copilot" && copilot_model == id)
|
|
236
249
|
end
|
|
@@ -239,10 +252,13 @@ module Kward
|
|
|
239
252
|
|
|
240
253
|
if provider_logged_in?("Anthropic")
|
|
241
254
|
anthropic_model = model_for("Anthropic")
|
|
242
|
-
|
|
243
|
-
|
|
255
|
+
anthropic_entries = ModelSources.new(provider_id: "anthropic", api_key: @api_key_store.fetch("anthropic")).models
|
|
256
|
+
anthropic_choices = (anthropic_entries.map { |entry| entry.fetch("id") } + ModelInfo::ANTHROPIC_MODEL_CHOICES).uniq
|
|
257
|
+
models += anthropic_choices.map do |id|
|
|
258
|
+
metadata = anthropic_entries.find { |entry| entry.fetch("id") == id }
|
|
259
|
+
model_entry("Anthropic", id, current: provider == "Anthropic" && anthropic_model == id, metadata: metadata)
|
|
244
260
|
end
|
|
245
|
-
models << model_entry("Anthropic", anthropic_model, current: provider == "Anthropic") unless
|
|
261
|
+
models << model_entry("Anthropic", anthropic_model, current: provider == "Anthropic") unless anthropic_choices.include?(anthropic_model)
|
|
246
262
|
end
|
|
247
263
|
|
|
248
264
|
ProviderCatalog.api_key_providers.each do |catalog_provider|
|
|
@@ -270,15 +286,43 @@ module Kward
|
|
|
270
286
|
models.sort_by { |model| [model[:provider], model[:id]] }
|
|
271
287
|
end
|
|
272
288
|
|
|
273
|
-
#
|
|
289
|
+
# Retains the array return value used by terminal pickers and internal callers.
|
|
274
290
|
def refresh_available_models(provider: nil)
|
|
291
|
+
refresh_model_catalog(provider: provider)
|
|
292
|
+
available_models
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Refreshes only one provider and reports a safe, frontend-neutral outcome.
|
|
296
|
+
# Failed discovery never replaces the last usable catalog or returns raw errors.
|
|
297
|
+
def refresh_model_catalog(provider: nil)
|
|
275
298
|
provider ||= current_provider
|
|
299
|
+
provider = "OpenAI" if provider.to_s == "openai_api"
|
|
276
300
|
catalog_provider = ProviderCatalog.find_by_name(provider) || ProviderCatalog.find(provider.to_s.downcase)
|
|
277
|
-
|
|
278
|
-
|
|
301
|
+
provider = ModelInfo.provider_label(catalog_provider&.id == "openai" ? "OpenAI" : catalog_provider&.id || provider)
|
|
302
|
+
status = "success"
|
|
279
303
|
|
|
280
|
-
|
|
281
|
-
|
|
304
|
+
case provider
|
|
305
|
+
when "OpenRouter"
|
|
306
|
+
@openrouter_models = openrouter_model_cache.refresh.fetch("models")
|
|
307
|
+
when "Copilot"
|
|
308
|
+
fetch_copilot_models(refresh: true)
|
|
309
|
+
when "Local"
|
|
310
|
+
local_model_choices(refresh: true)
|
|
311
|
+
else
|
|
312
|
+
if catalog_provider&.api_key? && ProviderCatalog.runtime(catalog_provider.id).automatic_model_discovery?
|
|
313
|
+
ModelSources.new(provider_id: catalog_provider.id, api_key: @api_key_store.fetch(catalog_provider.id)).refresh(strict: true)
|
|
314
|
+
else
|
|
315
|
+
status = "unsupported"
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
{ provider: provider, status: status, fallbackUsed: status != "success" }
|
|
320
|
+
rescue StandardError
|
|
321
|
+
# Avoid an implicit retry during the following list read when no prior
|
|
322
|
+
# in-memory catalog exists. Configured/curated choices remain available.
|
|
323
|
+
@copilot_models ||= [] if provider == "Copilot"
|
|
324
|
+
@local_models ||= [] if provider == "Local"
|
|
325
|
+
{ provider: provider, status: "failed", fallbackUsed: true }
|
|
282
326
|
end
|
|
283
327
|
|
|
284
328
|
# Projects messages/tools into the provider-specific context shape without sending it.
|
|
@@ -691,7 +735,7 @@ module Kward
|
|
|
691
735
|
end
|
|
692
736
|
end
|
|
693
737
|
|
|
694
|
-
def request_body_payload(provider, messages, tools, max_tokens: nil, model: nil, reasoning: nil)
|
|
738
|
+
def request_body_payload(provider, messages, tools, max_tokens: nil, model: nil, reasoning: nil, text_format: nil)
|
|
695
739
|
reasoning = false unless ModelInfo.reasoning_supported?(provider, model)
|
|
696
740
|
|
|
697
741
|
if provider == "Azure OpenAI"
|
|
@@ -699,9 +743,9 @@ module Kward
|
|
|
699
743
|
elsif provider == "Google Gemini"
|
|
700
744
|
gemini_payload(messages, tools, max_tokens: max_tokens, model: model)
|
|
701
745
|
elsif provider == "OpenAI"
|
|
702
|
-
openai_responses_payload(messages, tools, max_tokens: max_tokens, model: model, reasoning: reasoning)
|
|
746
|
+
openai_responses_payload(messages, tools, max_tokens: max_tokens, model: model, reasoning: reasoning, text_format: text_format)
|
|
703
747
|
elsif provider == "Codex"
|
|
704
|
-
codex_payload(messages, tools, max_tokens: max_tokens, model: model, reasoning: reasoning)
|
|
748
|
+
codex_payload(messages, tools, max_tokens: max_tokens, model: model, reasoning: reasoning, text_format: text_format)
|
|
705
749
|
elsif provider == "Anthropic"
|
|
706
750
|
anthropic_payload(messages, tools, max_tokens: max_tokens, model: model, reasoning: reasoning)
|
|
707
751
|
elsif provider == "Copilot" && copilot_responses_model?(model)
|
|
@@ -733,23 +777,27 @@ module Kward
|
|
|
733
777
|
ModelInfo.context_window(provider, id, openrouter_models: openrouter_cached_model_entries)
|
|
734
778
|
end
|
|
735
779
|
|
|
736
|
-
def local_model_choices
|
|
737
|
-
return @local_models if @local_models
|
|
780
|
+
def local_model_choices(refresh: false)
|
|
781
|
+
return @local_models if @local_models && !refresh
|
|
738
782
|
|
|
739
783
|
url = local_models_url
|
|
740
784
|
request = Http.apply_user_agent(Net::HTTP::Get.new(url))
|
|
741
785
|
request["Authorization"] = "Bearer #{local_api_key}" unless local_api_key.to_s.empty?
|
|
742
786
|
request["Accept"] = "application/json"
|
|
743
787
|
response = Net::HTTP.start(url.hostname, url.port, use_ssl: url.scheme == "https", read_timeout: stream_idle_timeout_seconds) { |http| http.request(request) }
|
|
744
|
-
|
|
788
|
+
raise "Local model discovery failed" unless response.is_a?(Net::HTTPSuccess)
|
|
745
789
|
|
|
746
790
|
body = JSON.parse(response.body)
|
|
747
|
-
entries = body["data"] || body["models"]
|
|
748
|
-
|
|
791
|
+
entries = body.is_a?(Hash) && (body["data"] || body["models"])
|
|
792
|
+
raise "Invalid Local model catalog" unless entries.is_a?(Array)
|
|
793
|
+
|
|
794
|
+
@local_models = entries.filter_map do |entry|
|
|
749
795
|
entry.is_a?(Hash) ? entry["id"] || entry[:id] || entry["model"] || entry[:model] : entry
|
|
750
796
|
end.map(&:to_s).map(&:strip).reject(&:empty?).uniq
|
|
751
797
|
rescue StandardError
|
|
752
|
-
|
|
798
|
+
raise if refresh
|
|
799
|
+
|
|
800
|
+
@local_models ||= []
|
|
753
801
|
end
|
|
754
802
|
|
|
755
803
|
def openrouter_model_choices
|
|
@@ -787,11 +835,11 @@ module Kward
|
|
|
787
835
|
CopilotModels.resolved_chat_model(configured_model, fetch_copilot_models)
|
|
788
836
|
end
|
|
789
837
|
|
|
790
|
-
def fetch_copilot_models
|
|
791
|
-
return @copilot_models if @copilot_models
|
|
838
|
+
def fetch_copilot_models(refresh: false)
|
|
839
|
+
return @copilot_models if @copilot_models && !refresh
|
|
792
840
|
|
|
793
841
|
token = github_access_token.to_s
|
|
794
|
-
|
|
842
|
+
raise "No Copilot token found" if token.empty?
|
|
795
843
|
|
|
796
844
|
url = URI("#{@github_oauth.base_url}/models")
|
|
797
845
|
request = Http.apply_user_agent(Net::HTTP::Get.new(url))
|
|
@@ -800,10 +848,12 @@ module Kward
|
|
|
800
848
|
copilot_headers([]).each { |key, value| request[key] = value }
|
|
801
849
|
|
|
802
850
|
response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) { |http| http.request(request) }
|
|
803
|
-
|
|
851
|
+
raise "Copilot model discovery failed" unless response.is_a?(Net::HTTPSuccess)
|
|
804
852
|
|
|
805
|
-
@copilot_models = CopilotModels.parse(response.body)
|
|
853
|
+
@copilot_models = CopilotModels.parse(response.body, strict: refresh)
|
|
806
854
|
rescue StandardError
|
|
855
|
+
raise if refresh
|
|
856
|
+
|
|
807
857
|
[]
|
|
808
858
|
end
|
|
809
859
|
|
|
@@ -972,24 +1022,38 @@ module Kward
|
|
|
972
1022
|
|
|
973
1023
|
input_tokens = integer_value(usage, "input_tokens", "prompt_tokens")
|
|
974
1024
|
output_tokens = integer_value(usage, "output_tokens", "completion_tokens")
|
|
975
|
-
cache_read_tokens =
|
|
1025
|
+
cache_read_tokens = token_count(
|
|
976
1026
|
nested_value(usage, "input_tokens_details", "cached_tokens") ||
|
|
977
1027
|
nested_value(usage, "prompt_tokens_details", "cached_tokens") ||
|
|
978
1028
|
usage["cache_read_tokens"] || usage[:cache_read_tokens] || usage["cacheReadTokens"] || usage[:cacheReadTokens] || usage["cache_read_input_tokens"] || usage[:cache_read_input_tokens]
|
|
979
1029
|
)
|
|
980
|
-
cache_write_tokens =
|
|
1030
|
+
cache_write_tokens = token_count(
|
|
1031
|
+
nested_value(usage, "input_tokens_details", "cache_write_tokens") ||
|
|
1032
|
+
integer_value(usage, "cache_write_tokens", "cacheWriteTokens", "cache_creation_input_tokens")
|
|
1033
|
+
)
|
|
1034
|
+
reasoning_tokens = token_count(
|
|
1035
|
+
nested_value(usage, "output_tokens_details", "reasoning_tokens") ||
|
|
1036
|
+
nested_value(usage, "completion_tokens_details", "reasoning_tokens") ||
|
|
1037
|
+
integer_value(usage, "reasoning_tokens")
|
|
1038
|
+
)
|
|
1039
|
+
# Anthropic reports uncached input separately; OpenAI/Gemini include cache
|
|
1040
|
+
# tokens in input. Normalize to inclusive input before computing totals.
|
|
1041
|
+
if input_tokens && %w[cache_read_input_tokens cache_creation_input_tokens].any? { |key| usage.key?(key) || usage.key?(key.to_sym) }
|
|
1042
|
+
input_tokens += cache_read_tokens.to_i + cache_write_tokens.to_i
|
|
1043
|
+
end
|
|
981
1044
|
total_tokens = integer_value(usage, "total_tokens", "totalTokens")
|
|
982
|
-
total_tokens ||=
|
|
983
|
-
return nil
|
|
1045
|
+
total_tokens ||= input_tokens.to_i + output_tokens.to_i if input_tokens || output_tokens
|
|
1046
|
+
return nil if total_tokens.nil?
|
|
984
1047
|
|
|
985
1048
|
{
|
|
986
|
-
"input_tokens" => input_tokens
|
|
987
|
-
"output_tokens" => output_tokens
|
|
988
|
-
"cache_read_tokens" => cache_read_tokens
|
|
989
|
-
"cache_write_tokens" => cache_write_tokens
|
|
990
|
-
"
|
|
1049
|
+
"input_tokens" => input_tokens,
|
|
1050
|
+
"output_tokens" => output_tokens,
|
|
1051
|
+
"cache_read_tokens" => cache_read_tokens,
|
|
1052
|
+
"cache_write_tokens" => cache_write_tokens,
|
|
1053
|
+
"reasoning_tokens" => reasoning_tokens,
|
|
1054
|
+
"total_tokens" => total_tokens,
|
|
991
1055
|
"estimated" => false
|
|
992
|
-
}
|
|
1056
|
+
}.compact
|
|
993
1057
|
end
|
|
994
1058
|
|
|
995
1059
|
def integer_value(source, *keys)
|
|
@@ -998,7 +1062,7 @@ module Kward
|
|
|
998
1062
|
key = keys.find { |candidate| source.key?(candidate) || source.key?(candidate.to_sym) }
|
|
999
1063
|
return nil unless key
|
|
1000
1064
|
|
|
1001
|
-
|
|
1065
|
+
token_count(source[key] || source[key.to_sym])
|
|
1002
1066
|
end
|
|
1003
1067
|
|
|
1004
1068
|
def nested_value(source, outer_key, inner_key)
|
|
@@ -1008,6 +1072,11 @@ module Kward
|
|
|
1008
1072
|
outer[inner_key] || outer[inner_key.to_sym]
|
|
1009
1073
|
end
|
|
1010
1074
|
|
|
1075
|
+
def token_count(value)
|
|
1076
|
+
integer = Integer(value, exception: false)
|
|
1077
|
+
integer if integer && integer >= 0
|
|
1078
|
+
end
|
|
1079
|
+
|
|
1011
1080
|
def positive_integer(value)
|
|
1012
1081
|
integer = value.to_i
|
|
1013
1082
|
integer.positive? ? integer : nil
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "base64"
|
|
2
|
+
require "json"
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
require_relative "../cancellation"
|
|
6
|
+
require_relative "../http"
|
|
7
|
+
|
|
8
|
+
module Kward
|
|
9
|
+
# Codex subscription transport, separate from the chat Responses stream.
|
|
10
|
+
class CodexImages
|
|
11
|
+
URL = URI("https://chatgpt.com/backend-api/codex/images/generations")
|
|
12
|
+
MAX_IMAGE_BYTES = 20 * 1024 * 1024
|
|
13
|
+
MAX_RESPONSE_BYTES = (MAX_IMAGE_BYTES + 2) / 3 * 4 + 65_536
|
|
14
|
+
PNG_HEADER = "\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR".b.freeze
|
|
15
|
+
Error = Class.new(StandardError)
|
|
16
|
+
|
|
17
|
+
def self.generate(prompt, token:, account_id:, turn_id:, cancellation: nil)
|
|
18
|
+
request = Http.apply_user_agent(Net::HTTP::Post.new(URL))
|
|
19
|
+
request["Authorization"] = "Bearer #{token}"
|
|
20
|
+
request["ChatGPT-Account-Id"] = account_id unless account_id.to_s.empty?
|
|
21
|
+
request["x-codex-image-turn-id"] = turn_id unless turn_id.to_s.empty?
|
|
22
|
+
request["originator"] = "kward"
|
|
23
|
+
request["Content-Type"] = "application/json"
|
|
24
|
+
request["Accept"] = "application/json"
|
|
25
|
+
request.body = JSON.generate(prompt: prompt, model: "gpt-image-2", background: "opaque", quality: "auto", size: "auto")
|
|
26
|
+
|
|
27
|
+
body = +""
|
|
28
|
+
cancellation&.raise_if_cancelled!
|
|
29
|
+
Net::HTTP.start(URL.hostname, URL.port, use_ssl: true, open_timeout: 10, read_timeout: 180) do |http|
|
|
30
|
+
http.max_retries = 0
|
|
31
|
+
cancellation&.on_cancel { http.finish if http.started? }
|
|
32
|
+
cancellation&.raise_if_cancelled!
|
|
33
|
+
http.request(request) do |response|
|
|
34
|
+
raise Error, error_message(response.code.to_i) unless response.is_a?(Net::HTTPSuccess)
|
|
35
|
+
|
|
36
|
+
response.read_body do |chunk|
|
|
37
|
+
cancellation&.raise_if_cancelled!
|
|
38
|
+
raise Error, "Codex image exceeds the preview size limit." if body.bytesize + chunk.bytesize > MAX_RESPONSE_BYTES
|
|
39
|
+
|
|
40
|
+
body << chunk
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
cancellation&.raise_if_cancelled!
|
|
45
|
+
decode(body)
|
|
46
|
+
rescue IOError, SystemCallError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError, Net::HTTPBadResponse
|
|
47
|
+
cancellation&.raise_if_cancelled!
|
|
48
|
+
raise Error, "The connection to Codex image generation was interrupted; it was not retried."
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.decode(body)
|
|
52
|
+
result = JSON.parse(body)
|
|
53
|
+
data = result.is_a?(Hash) ? result["data"] : nil
|
|
54
|
+
first = data.first if data.is_a?(Array)
|
|
55
|
+
encoded = first["b64_json"] if first.is_a?(Hash)
|
|
56
|
+
raise Error, "Codex did not return an image." unless encoded.is_a?(String)
|
|
57
|
+
|
|
58
|
+
bytes = Base64.strict_decode64(encoded)
|
|
59
|
+
raise Error, "Codex returned an invalid or oversized PNG." unless valid_png?(bytes)
|
|
60
|
+
|
|
61
|
+
bytes
|
|
62
|
+
rescue JSON::ParserError, ArgumentError
|
|
63
|
+
raise Error, "Codex returned an invalid image response."
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.valid_png?(bytes)
|
|
67
|
+
return false unless bytes.is_a?(String) && bytes.bytesize.between?(45, MAX_IMAGE_BYTES) && bytes.b.start_with?(PNG_HEADER)
|
|
68
|
+
|
|
69
|
+
width, height = bytes.byteslice(16, 8).unpack("NN")
|
|
70
|
+
width.between?(1, 8192) && height.between?(1, 8192) && bytes.end_with?("\x00\x00\x00\x00IEND\xaeB`\x82".b)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.error_message(status)
|
|
74
|
+
case status
|
|
75
|
+
when 401 then "Codex sign-in expired; sign in again to generate an image."
|
|
76
|
+
when 403, 404 then "Image generation is not available for this Codex account."
|
|
77
|
+
when 429 then "Codex image generation is rate-limited or the account has reached its usage limit."
|
|
78
|
+
else "Codex could not generate an image (HTTP #{status})."
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
private_class_method :decode, :error_message
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -6,9 +6,11 @@ module Kward
|
|
|
6
6
|
module CopilotModels
|
|
7
7
|
module_function
|
|
8
8
|
|
|
9
|
-
def parse(body)
|
|
10
|
-
catalog_entries(body).filter_map { |entry| model_id(entry) }.uniq
|
|
9
|
+
def parse(body, strict: false)
|
|
10
|
+
catalog_entries(body, strict: strict).filter_map { |entry| model_id(entry) }.uniq
|
|
11
11
|
rescue JSON::ParserError
|
|
12
|
+
raise if strict
|
|
13
|
+
|
|
12
14
|
[]
|
|
13
15
|
end
|
|
14
16
|
|
|
@@ -27,9 +29,11 @@ module Kward
|
|
|
27
29
|
choices.find { |model| supported?(model) } || raise("No Copilot models supported by Kward are available for this account. Kward currently supports Copilot GPT-5/GPT-6 Responses and Gemini/GPT-4.1 chat models.")
|
|
28
30
|
end
|
|
29
31
|
|
|
30
|
-
def catalog_entries(body)
|
|
32
|
+
def catalog_entries(body, strict: false)
|
|
31
33
|
data = JSON.parse(body.to_s)
|
|
32
|
-
entries = data.is_a?(Hash) ? data["data"] || data["models"] || data["items"]
|
|
34
|
+
entries = data.is_a?(Hash) ? data["data"] || data["models"] || data["items"] : data
|
|
35
|
+
raise "Invalid Copilot model catalog" if strict && !entries.is_a?(Array)
|
|
36
|
+
|
|
33
37
|
Array(entries)
|
|
34
38
|
end
|
|
35
39
|
|
|
@@ -9,8 +9,13 @@ module Kward
|
|
|
9
9
|
DEFAULT_COPILOT_MODEL = "gpt-5-mini"
|
|
10
10
|
DEFAULT_ANTHROPIC_MODEL = "claude-sonnet-5"
|
|
11
11
|
DEFAULT_REASONING_EFFORT = "medium"
|
|
12
|
-
OPENAI_MODEL_CHOICES = %w[gpt-6-astra gpt-6.1-sol gpt-6-luna gpt-5.6-sol gpt-5.6-terra gpt-5.6-luna gpt-5.5 gpt-5.4 gpt-5.4-mini gpt-5.3-codex-spark].freeze
|
|
12
|
+
OPENAI_MODEL_CHOICES = %w[gpt-6.2-astra gpt-6.2-sol gpt-6.1-terra gpt-6-astra gpt-6.1-sol gpt-6-luna gpt-5.6-sol gpt-5.6-terra gpt-5.6-luna gpt-5.5 gpt-5.4 gpt-5.4-mini gpt-5.3-codex-spark].freeze
|
|
13
13
|
ANTHROPIC_MODEL_CHOICES = %w[
|
|
14
|
+
claude-fable-5-2
|
|
15
|
+
claude-mythos-5-2
|
|
16
|
+
claude-opus-5-5
|
|
17
|
+
claude-sonnet-5-5
|
|
18
|
+
claude-haiku-5-5
|
|
14
19
|
claude-fable-5
|
|
15
20
|
claude-opus-5
|
|
16
21
|
claude-opus-4-8
|
|
@@ -87,7 +92,7 @@ module Kward
|
|
|
87
92
|
].freeze
|
|
88
93
|
|
|
89
94
|
CODEX_CONTEXT_WINDOWS = [
|
|
90
|
-
[/\Agpt-6(
|
|
95
|
+
[/\Agpt-6(?:\.\d+)?-(?:astra|sol|luna|terra)(?:\z|-)/, 1_050_000],
|
|
91
96
|
[/\Agpt-5\.6/, 1_050_000],
|
|
92
97
|
[/\Agpt-5\.5/, 400_000],
|
|
93
98
|
[/\Agpt-5\.4-mini/, 400_000],
|
|
@@ -106,7 +111,7 @@ module Kward
|
|
|
106
111
|
[/\Agpt-3\.5-turbo/, 16_385]
|
|
107
112
|
].freeze
|
|
108
113
|
OPENAI_CONTEXT_WINDOWS = [
|
|
109
|
-
[/\Agpt-6(
|
|
114
|
+
[/\Agpt-6(?:\.\d+)?-(?:astra|sol|luna|terra)(?:\z|-)/, 1_050_000],
|
|
110
115
|
[/\Agpt-5\.6/, 1_050_000],
|
|
111
116
|
[/\Agpt-5\.5/, 1_050_000],
|
|
112
117
|
[/\Agpt-5\.4-mini/, 400_000],
|
|
@@ -125,7 +130,7 @@ module Kward
|
|
|
125
130
|
[/\Agpt-3\.5-turbo/, 16_385]
|
|
126
131
|
].freeze
|
|
127
132
|
ANTHROPIC_CONTEXT_WINDOWS = [
|
|
128
|
-
[/\Aclaude-(?:fable|mythos|opus|sonnet)-5(?:\z|-)/, 1_000_000],
|
|
133
|
+
[/\Aclaude-(?:fable|mythos|opus|sonnet|haiku)-5(?:\z|-)/, 1_000_000],
|
|
129
134
|
[/\Aclaude-opus-4-(?:6|7|8)(?:\z|-)/, 1_000_000],
|
|
130
135
|
[/\Aclaude-sonnet-4-6(?:\z|-)/, 1_000_000],
|
|
131
136
|
[/\Aclaude-(?:haiku|opus|sonnet)-4-5(?:\z|-)/, 200_000],
|
|
@@ -435,7 +440,7 @@ module Kward
|
|
|
435
440
|
when "Codex", "OpenRouter"
|
|
436
441
|
true
|
|
437
442
|
when "OpenAI"
|
|
438
|
-
id.to_s.match?(/\Agpt-6(
|
|
443
|
+
id.to_s.match?(/\Agpt-6(?:\.\d+)?-(?:astra|sol|luna|terra)(?:\z|-)/)
|
|
439
444
|
when "Anthropic"
|
|
440
445
|
!reasoning_effort_choices(provider, id).empty?
|
|
441
446
|
when "Copilot"
|
|
@@ -463,7 +468,8 @@ module Kward
|
|
|
463
468
|
def openai_reasoning_effort_choices(id)
|
|
464
469
|
text = id.to_s.delete_prefix("openai/")
|
|
465
470
|
return GPT_6_LUNA_REASONING_EFFORT_CHOICES if text.match?(/\Agpt-6-luna(?:\z|-)/)
|
|
466
|
-
return
|
|
471
|
+
return OPENAI_MAX_REASONING_EFFORT_CHOICES if text.match?(/\Agpt-6\.1-terra(?:\z|-)/)
|
|
472
|
+
return GPT_6_REASONING_EFFORT_CHOICES if text.match?(/\Agpt-6\.2-(?:astra|sol)(?:\z|-)|\Agpt-6\.1-sol(?:\z|-)|\Agpt-6-astra(?:\z|-)/)
|
|
467
473
|
return REASONING_EFFORT_CHOICES if text.match?(/\Agpt-5\.[23]-codex/)
|
|
468
474
|
return OPENAI_MAX_REASONING_EFFORT_CHOICES if text.match?(/\Agpt-5\.6(?:\z|-)/)
|
|
469
475
|
|
|
@@ -472,13 +478,25 @@ module Kward
|
|
|
472
478
|
|
|
473
479
|
def anthropic_reasoning_effort_choices(id)
|
|
474
480
|
text = normalize_anthropic_model(id)
|
|
475
|
-
return ANTHROPIC_HIGH_REASONING_EFFORT_CHOICES if text.match?(/\Aclaude-(?:fable|mythos|opus|sonnet)-5(?:\z|-)|\Aclaude-opus-4-(?:7|8)(?:\z|-)/)
|
|
481
|
+
return ANTHROPIC_HIGH_REASONING_EFFORT_CHOICES if text.match?(/\Aclaude-(?:fable|mythos|opus|sonnet|haiku)-5(?:\z|-)|\Aclaude-opus-4-(?:7|8)(?:\z|-)/)
|
|
476
482
|
return ANTHROPIC_STANDARD_REASONING_EFFORT_CHOICES if text.match?(/\Aclaude-(?:opus-4-6|sonnet-4-6)(?:\z|-)/)
|
|
477
483
|
return ANTHROPIC_OPUS_4_5_REASONING_EFFORT_CHOICES if text.match?(/\Aclaude-opus-4-5(?:\z|-)/)
|
|
478
484
|
|
|
479
485
|
[]
|
|
480
486
|
end
|
|
481
487
|
|
|
488
|
+
# Unknown capabilities and the selection in the caller's context stay visible.
|
|
489
|
+
def default_selectable_model?(model)
|
|
490
|
+
return true if model[:current]
|
|
491
|
+
|
|
492
|
+
capabilities = Array(model[:supportedParameters]).map { |value| value.to_s.downcase }
|
|
493
|
+
return true if capabilities.empty?
|
|
494
|
+
|
|
495
|
+
capabilities.any? do |capability|
|
|
496
|
+
["tools", "toolchoice", "functioncalling", "generatecontent", "streamgeneratecontent"].include?(capability.delete("_-"))
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
|
|
482
500
|
def normalize(model, current_provider: nil, current_model: nil, current_reasoning_effort: nil)
|
|
483
501
|
model = stringify_keys(model || {})
|
|
484
502
|
provider = model["provider"]
|
|
@@ -494,8 +512,8 @@ module Kward
|
|
|
494
512
|
contextWindow: model["contextWindow"] || model["context_window"] || context_window(provider, id),
|
|
495
513
|
supportedParameters: model["supportedParameters"] || model["supported_parameters"],
|
|
496
514
|
source: model["source"],
|
|
497
|
-
current:
|
|
498
|
-
}.compact
|
|
515
|
+
current: current_provider && current_model ? provider == current_provider && id == current_model : boolean_value(model["current"])
|
|
516
|
+
}.compact.then { |entry| entry.merge(defaultSelectable: default_selectable_model?(entry)) }
|
|
499
517
|
end
|
|
500
518
|
|
|
501
519
|
def normalize_available(models, current_provider:, current_model:, current_reasoning_effort: nil)
|
|
@@ -80,8 +80,10 @@ module Kward
|
|
|
80
80
|
|
|
81
81
|
def model_entries(body)
|
|
82
82
|
data = JSON.parse(body.to_s)
|
|
83
|
-
entries = data.is_a?(Hash) ? data["data"]
|
|
84
|
-
|
|
83
|
+
entries = data.is_a?(Hash) ? data["data"] : data
|
|
84
|
+
raise "Invalid OpenRouter model catalog" unless entries.is_a?(Array)
|
|
85
|
+
|
|
86
|
+
entries.select { |entry| entry.is_a?(Hash) }
|
|
85
87
|
rescue JSON::ParserError
|
|
86
88
|
raise "OpenRouter model refresh returned invalid JSON"
|
|
87
89
|
end
|
data/lib/kward/model/payloads.rb
CHANGED
|
@@ -167,17 +167,17 @@ module Kward
|
|
|
167
167
|
}
|
|
168
168
|
end
|
|
169
169
|
|
|
170
|
-
def codex_payload(messages, tools, max_tokens: nil, model: nil, reasoning: nil)
|
|
171
|
-
responses_payload("Codex", messages, tools, model: model, reasoning: reasoning)
|
|
170
|
+
def codex_payload(messages, tools, max_tokens: nil, model: nil, reasoning: nil, text_format: nil)
|
|
171
|
+
responses_payload("Codex", messages, tools, model: model, reasoning: reasoning, text_format: text_format)
|
|
172
172
|
end
|
|
173
173
|
|
|
174
|
-
def openai_responses_payload(messages, tools, max_tokens: nil, model: nil, reasoning: nil)
|
|
175
|
-
payload = responses_payload("OpenAI", messages, tools, model: model, reasoning: reasoning)
|
|
174
|
+
def openai_responses_payload(messages, tools, max_tokens: nil, model: nil, reasoning: nil, text_format: nil)
|
|
175
|
+
payload = responses_payload("OpenAI", messages, tools, model: model, reasoning: reasoning, text_format: text_format)
|
|
176
176
|
payload[:max_output_tokens] = max_tokens.to_i if max_tokens.to_i.positive?
|
|
177
177
|
payload
|
|
178
178
|
end
|
|
179
179
|
|
|
180
|
-
def responses_payload(provider, messages, tools, model:, reasoning:)
|
|
180
|
+
def responses_payload(provider, messages, tools, model:, reasoning:, text_format: nil)
|
|
181
181
|
parts = build_context_parts(provider, messages, tools, model: model)
|
|
182
182
|
payload = {
|
|
183
183
|
model: parts[:model],
|
|
@@ -194,6 +194,7 @@ module Kward
|
|
|
194
194
|
effort = reasoning || reasoning_effort(provider)
|
|
195
195
|
payload[:reasoning] = { effort: effort, summary: "auto" }
|
|
196
196
|
end
|
|
197
|
+
payload[:text] = text_format unless text_format.nil?
|
|
197
198
|
payload
|
|
198
199
|
end
|
|
199
200
|
|
data/lib/kward/model/sources.rb
CHANGED
|
@@ -347,7 +347,7 @@ module Kward
|
|
|
347
347
|
when "response.content_part.added"
|
|
348
348
|
codex_content_part_added(state, event["part"])
|
|
349
349
|
when "response.output_text.delta", "response.refusal.delta"
|
|
350
|
-
codex_output_text_delta(state, event["delta"],
|
|
350
|
+
codex_output_text_delta(state, event["delta"], on_assistant_delta: on_assistant_delta)
|
|
351
351
|
when "response.reasoning_summary_part.added"
|
|
352
352
|
codex_reasoning_summary_part_added(state, event["part"])
|
|
353
353
|
when "response.reasoning_summary_text.delta"
|
|
@@ -417,7 +417,7 @@ module Kward
|
|
|
417
417
|
state[:current_text_content_part] = item["content"].last
|
|
418
418
|
end
|
|
419
419
|
|
|
420
|
-
def codex_output_text_delta(state, delta,
|
|
420
|
+
def codex_output_text_delta(state, delta, on_assistant_delta: nil)
|
|
421
421
|
text = delta.to_s
|
|
422
422
|
return if text.empty?
|
|
423
423
|
|
|
@@ -429,12 +429,9 @@ module Kward
|
|
|
429
429
|
part["type"] ||= "output_text"
|
|
430
430
|
text_key = part["type"] == "refusal" ? "refusal" : "text"
|
|
431
431
|
part[text_key] = part[text_key].to_s + text
|
|
432
|
-
if codex_streamable_message_item?(item)
|
|
432
|
+
if codex_streamable_message_item?(item) || codex_commentary_message_item?(item)
|
|
433
433
|
on_assistant_delta&.call(text)
|
|
434
434
|
state[:emitted_message_keys] << codex_item_key(item)
|
|
435
|
-
elsif codex_commentary_message_item?(item)
|
|
436
|
-
on_reasoning_delta&.call(text)
|
|
437
|
-
state[:emitted_message_keys] << codex_item_key(item)
|
|
438
435
|
end
|
|
439
436
|
end
|
|
440
437
|
state[:raw_content] << text
|
|
@@ -655,7 +652,7 @@ module Kward
|
|
|
655
652
|
key = codex_item_key(item)
|
|
656
653
|
if codex_commentary_message_item?(item)
|
|
657
654
|
unless state[:emitted_message_keys].include?(key)
|
|
658
|
-
|
|
655
|
+
on_assistant_delta&.call(text)
|
|
659
656
|
state[:emitted_message_keys] << key
|
|
660
657
|
end
|
|
661
658
|
return
|
|
@@ -80,6 +80,8 @@
|
|
|
80
80
|
.startup-logo { display: block; width: 7.5rem; height: 7.5rem; margin: 0 auto 1.4rem; object-fit: contain; filter: drop-shadow(0 12px 22px #0008); }
|
|
81
81
|
.entry { margin: 0 0 1.15rem; animation: appear .18s ease-out; }
|
|
82
82
|
@keyframes appear { from { opacity: 0; transform: translateY(3px); } }
|
|
83
|
+
.entry.turn-stats .label { display: none; }
|
|
84
|
+
.entry.turn-stats .text { color: var(--subtle); font-size: .75rem; white-space: pre-line; }
|
|
83
85
|
.entry.user { display: flex; justify-content: flex-end; }
|
|
84
86
|
.message { max-width: min(88%, 42rem); }
|
|
85
87
|
.label { margin: 0 0 .35rem; color: var(--muted); font-size: .68rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
|
|
@@ -228,7 +230,8 @@
|
|
|
228
230
|
labelNode.textContent = label;
|
|
229
231
|
const textNode = document.createElement('div');
|
|
230
232
|
textNode.className = 'text';
|
|
231
|
-
|
|
233
|
+
if (role === 'turn-stats') textNode.textContent = text || '';
|
|
234
|
+
else renderMarkdown(textNode, text || '');
|
|
232
235
|
message.append(labelNode, textNode);
|
|
233
236
|
node.appendChild(message);
|
|
234
237
|
transcriptInner.appendChild(node);
|
|
@@ -559,6 +562,10 @@
|
|
|
559
562
|
events.addEventListener('answer', event => {
|
|
560
563
|
if (!currentAssistant) entry('assistant', assistantLabel, JSON.parse(event.data).content);
|
|
561
564
|
});
|
|
565
|
+
events.addEventListener('turn_summary', event => {
|
|
566
|
+
const data = JSON.parse(event.data);
|
|
567
|
+
if (data.text) entry('turn-stats', 'Turn summary', data.text);
|
|
568
|
+
});
|
|
562
569
|
events.addEventListener('retry', event => entry('tool', 'Retry', JSON.parse(event.data).message));
|
|
563
570
|
events.addEventListener('error', event => {
|
|
564
571
|
if (typeof event.data === 'string' && event.data.startsWith('{')) showError(JSON.parse(event.data).message);
|
data/lib/kward/pan/server.rb
CHANGED
|
@@ -9,6 +9,7 @@ require "uri"
|
|
|
9
9
|
require_relative "../agent"
|
|
10
10
|
require_relative "../config_files"
|
|
11
11
|
require_relative "../events"
|
|
12
|
+
require_relative "../transcripts/turn_summary_formatter"
|
|
12
13
|
require_relative "../model/retry_message"
|
|
13
14
|
require_relative "../plugins/registry"
|
|
14
15
|
require_relative "../prompts/commands"
|
|
@@ -113,7 +114,12 @@ module Kward
|
|
|
113
114
|
end
|
|
114
115
|
|
|
115
116
|
def transcript_items
|
|
116
|
-
RPC::TranscriptNormalizer.new(@conversation.messages).normalize.flat_map
|
|
117
|
+
RPC::TranscriptNormalizer.new(@conversation.messages).normalize.flat_map do |message|
|
|
118
|
+
items = pan_transcript_items(message)
|
|
119
|
+
text = turn_summary_text(message[:turnSummary])
|
|
120
|
+
items << { role: "turn-stats", label: "Turn summary", text: text } unless text.empty?
|
|
121
|
+
items
|
|
122
|
+
end
|
|
117
123
|
end
|
|
118
124
|
|
|
119
125
|
private
|
|
@@ -200,6 +206,8 @@ module Kward
|
|
|
200
206
|
broadcast("turn_started", queue_payload)
|
|
201
207
|
@agent.ask(prompt) do |event|
|
|
202
208
|
case event
|
|
209
|
+
when Events::TurnSummary
|
|
210
|
+
broadcast("turn_summary", { summary: event.summary, text: turn_summary_text(event.summary) })
|
|
203
211
|
when Events::ReasoningDelta
|
|
204
212
|
broadcast("reasoning_delta", { delta: event.delta.to_s })
|
|
205
213
|
when Events::ReasoningBoundary
|
|
@@ -652,6 +660,10 @@ module Kward
|
|
|
652
660
|
{ name: tool_call_name(tool_call), content: content.to_s }
|
|
653
661
|
end
|
|
654
662
|
|
|
663
|
+
def turn_summary_text(summary)
|
|
664
|
+
TurnSummaryFormatter.render(summary, mode: ConfigFiles.transcript_turn_stats(@full_config))
|
|
665
|
+
end
|
|
666
|
+
|
|
655
667
|
def pan_transcript_items(message)
|
|
656
668
|
case message[:role] || message["role"]
|
|
657
669
|
when "user"
|