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/config/core.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
# Core configuration paths, persistence, and shared configuration values.
|
|
2
|
+
require_relative "paths"
|
|
3
|
+
|
|
2
4
|
module Kward
|
|
3
5
|
module ConfigFiles
|
|
4
6
|
class ConfigError < StandardError
|
|
@@ -37,6 +39,7 @@ module Kward
|
|
|
37
39
|
end
|
|
38
40
|
|
|
39
41
|
@skip_config = false
|
|
42
|
+
@ignore_local_instructions = false
|
|
40
43
|
@warning_sink = nil
|
|
41
44
|
|
|
42
45
|
module Core
|
|
@@ -46,6 +49,14 @@ module Kward
|
|
|
46
49
|
@skip_config = value
|
|
47
50
|
end
|
|
48
51
|
|
|
52
|
+
def ignore_local_instructions=(value)
|
|
53
|
+
@ignore_local_instructions = value
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def ignore_local_instructions?
|
|
57
|
+
@ignore_local_instructions == true
|
|
58
|
+
end
|
|
59
|
+
|
|
49
60
|
def warning_sink=(sink)
|
|
50
61
|
@warning_sink = sink
|
|
51
62
|
end
|
|
@@ -63,10 +74,7 @@ module Kward
|
|
|
63
74
|
#
|
|
64
75
|
# @return [String] expanded config directory path
|
|
65
76
|
def config_dir
|
|
66
|
-
|
|
67
|
-
return File.expand_path(File.dirname(config_path)) if config_path && !config_path.empty?
|
|
68
|
-
|
|
69
|
-
File.expand_path("~/.kward")
|
|
77
|
+
ConfigPaths.config_dir
|
|
70
78
|
end
|
|
71
79
|
|
|
72
80
|
# @return [String] expanded JSON config file path
|
|
@@ -86,7 +94,7 @@ module Kward
|
|
|
86
94
|
|
|
87
95
|
# @return [Array<String>] embedded-shell rc config paths in load order
|
|
88
96
|
def kwshrc_paths
|
|
89
|
-
|
|
97
|
+
Kwshrc.paths(kwshrc_path)
|
|
90
98
|
end
|
|
91
99
|
|
|
92
100
|
def workspace_hooks_path(root = Dir.pwd)
|
|
@@ -107,6 +115,9 @@ module Kward
|
|
|
107
115
|
"enabled" => false,
|
|
108
116
|
"auto_summary" => false
|
|
109
117
|
},
|
|
118
|
+
"transcript" => {
|
|
119
|
+
"turn_stats" => "compact"
|
|
120
|
+
},
|
|
110
121
|
"composer" => {
|
|
111
122
|
"busy_help" => true,
|
|
112
123
|
"tab_keybindings" => "auto"
|
|
@@ -12,6 +12,8 @@ module Kward
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def project_skill_candidates(workspace_root: Dir.pwd, warning_sink: nil)
|
|
15
|
+
return [] if ignore_local_instructions?
|
|
16
|
+
|
|
15
17
|
skills_registry(workspace_root: workspace_root, warning_sink: warning_sink).project_skill_candidates
|
|
16
18
|
end
|
|
17
19
|
|
|
@@ -56,6 +58,7 @@ module Kward
|
|
|
56
58
|
end
|
|
57
59
|
|
|
58
60
|
def skills_registry(workspace_root: Dir.pwd, warning_sink: nil, project_skill_paths: nil)
|
|
61
|
+
project_skill_paths = [] if ignore_local_instructions?
|
|
59
62
|
Skills::Registry.new(
|
|
60
63
|
config_dir: config_dir,
|
|
61
64
|
workspace_root: workspace_root,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module Kward
|
|
2
|
+
# Shared user configuration location; resolving it does not load any config.
|
|
3
|
+
module ConfigPaths
|
|
4
|
+
def self.config_dir
|
|
5
|
+
path = ENV["KWARD_CONFIG_PATH"]
|
|
6
|
+
return File.expand_path(File.dirname(path)) if path && !path.empty?
|
|
7
|
+
|
|
8
|
+
File.expand_path("~/.kward")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -4,6 +4,13 @@ module Kward
|
|
|
4
4
|
module Settings
|
|
5
5
|
extend self
|
|
6
6
|
|
|
7
|
+
# Controls transcript presentation only; stats remain available over RPC.
|
|
8
|
+
def transcript_turn_stats(config = read_config)
|
|
9
|
+
transcript = config["transcript"].is_a?(Hash) ? config["transcript"] : {}
|
|
10
|
+
value = transcript["turn_stats"].to_s
|
|
11
|
+
%w[off compact detailed].include?(value) ? value : "compact"
|
|
12
|
+
end
|
|
13
|
+
|
|
7
14
|
def overlay_settings(config = read_config)
|
|
8
15
|
overlay = config["overlay"].is_a?(Hash) ? config["overlay"] : {}
|
|
9
16
|
settings = DEFAULT_OVERLAY_SETTINGS.dup
|
data/lib/kward/conversation.rb
CHANGED
|
@@ -47,6 +47,8 @@ module Kward
|
|
|
47
47
|
attr_accessor :on_compact
|
|
48
48
|
# @return [Proc, nil] callback invoked when a tool execution record should be persisted
|
|
49
49
|
attr_accessor :on_tool_execution
|
|
50
|
+
# @return [Proc, nil] callback for branch-anchored turn summary metadata
|
|
51
|
+
attr_accessor :on_turn_summary
|
|
50
52
|
# @return [Proc, nil] callback invoked when runtime metadata should be persisted
|
|
51
53
|
attr_accessor :on_runtime_update
|
|
52
54
|
# @return [Proc, nil] callback invoked when the system prompt runtime state changes
|
|
@@ -123,6 +125,14 @@ module Kward
|
|
|
123
125
|
append_message(message)
|
|
124
126
|
end
|
|
125
127
|
|
|
128
|
+
def finish_turn(summary)
|
|
129
|
+
message = @messages.last
|
|
130
|
+
return unless message
|
|
131
|
+
|
|
132
|
+
message["turn_summary"] = summary
|
|
133
|
+
@on_turn_summary&.call(message, summary)
|
|
134
|
+
end
|
|
135
|
+
|
|
126
136
|
def append_tool(tool_call_id:, name:, content:)
|
|
127
137
|
append_message({
|
|
128
138
|
role: "tool",
|
|
@@ -198,7 +208,7 @@ module Kward
|
|
|
198
208
|
inserted = false
|
|
199
209
|
messages = @messages.filter_map do |message|
|
|
200
210
|
record = message[:plugin_system_turn] || message["plugin_system_turn"]
|
|
201
|
-
next message unless record
|
|
211
|
+
next message.reject { |key, _| key.to_s == "turn_summary" } unless record
|
|
202
212
|
next unless @system_turn && (record[:id] || record["id"]) == @system_turn.id
|
|
203
213
|
|
|
204
214
|
inserted = true
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Kward
|
|
2
|
+
# Select shell mode before loading the coding agent or parsing its options.
|
|
3
|
+
module Entrypoint
|
|
4
|
+
def self.run(arguments = ARGV, name: $PROGRAM_NAME)
|
|
5
|
+
if arguments.first == "shell"
|
|
6
|
+
require_relative "shell/native_process"
|
|
7
|
+
NativeShell::NativeProcess.standalone(arguments.drop(1), name: "#{name} shell", program: "kward shell", edit_lines: true, load_rc: true, shell_agent: true)
|
|
8
|
+
else
|
|
9
|
+
require_relative "../kward"
|
|
10
|
+
CLI.new(argv: arguments).run
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/kward/events.rb
CHANGED
|
@@ -11,7 +11,9 @@ module Kward
|
|
|
11
11
|
SteeringApplied = Struct.new(:count, keyword_init: true)
|
|
12
12
|
ToolCall = Struct.new(:tool_call, keyword_init: true)
|
|
13
13
|
ToolUpdate = Struct.new(:tool_call, :content, :elapsed_ms, keyword_init: true)
|
|
14
|
+
ImageReady = Struct.new(:tool_call, :image, keyword_init: true)
|
|
14
15
|
ToolResult = Struct.new(:tool_call, :content, keyword_init: true)
|
|
15
16
|
Answer = Struct.new(:content, keyword_init: true)
|
|
17
|
+
TurnSummary = Struct.new(:summary, keyword_init: true)
|
|
16
18
|
end
|
|
17
19
|
end
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
require "fileutils"
|
|
2
|
+
require "securerandom"
|
|
3
|
+
require "tmpdir"
|
|
4
|
+
require_relative "model/codex_images"
|
|
5
|
+
require_relative "shell/native_directory"
|
|
6
|
+
|
|
7
|
+
module Kward
|
|
8
|
+
# Owns unsaved preview bytes. Only the frontend can export or discard them.
|
|
9
|
+
class GeneratedImages
|
|
10
|
+
Error = Class.new(StandardError)
|
|
11
|
+
Preview = Struct.new(:id, :filename, :root, :size, keyword_init: true)
|
|
12
|
+
Entry = Struct.new(:owner, :preview, :path, keyword_init: true)
|
|
13
|
+
MAX_PREVIEWS = 16
|
|
14
|
+
FETCH_CHUNK_BYTES = 256 * 1024
|
|
15
|
+
LOCK_FLAGS = File::RDWR | File::CREAT | File::NOFOLLOW
|
|
16
|
+
|
|
17
|
+
def initialize(base: File.join(Dir.tmpdir, "kward-images-#{Process.uid}"))
|
|
18
|
+
@base = base
|
|
19
|
+
@entries = {}
|
|
20
|
+
@mutex = Mutex.new
|
|
21
|
+
reap_crashed_runs
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def add(owner, bytes, cancellation: nil)
|
|
25
|
+
raise Error, "Invalid or oversized PNG." unless CodexImages.valid_png?(bytes)
|
|
26
|
+
|
|
27
|
+
@mutex.synchronize do
|
|
28
|
+
cancellation&.raise_if_cancelled!
|
|
29
|
+
raise Error, "Image preview storage is closed." if @closed
|
|
30
|
+
raise Error, "Too many pending images; discard a preview before generating another." if @entries.size >= MAX_PREVIEWS
|
|
31
|
+
|
|
32
|
+
initialize_run unless @run
|
|
33
|
+
id = SecureRandom.hex(16)
|
|
34
|
+
preview = Preview.new(id: id, filename: "kward-image-#{id[0, 8]}.png", root: File.realpath(owner.workspace_root), size: bytes.bytesize).freeze
|
|
35
|
+
path = File.join(@run, "#{id}.png")
|
|
36
|
+
begin
|
|
37
|
+
File.open(path, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |file| file.binmode.write(bytes) }
|
|
38
|
+
cancellation&.raise_if_cancelled!
|
|
39
|
+
@entries[id] = Entry.new(owner: owner, preview: preview, path: path)
|
|
40
|
+
ensure
|
|
41
|
+
FileUtils.rm_f(path) unless @entries.key?(id)
|
|
42
|
+
end
|
|
43
|
+
preview
|
|
44
|
+
end
|
|
45
|
+
rescue SystemCallError
|
|
46
|
+
raise Error, "Could not store the generated image preview."
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def path(owner, id)
|
|
50
|
+
@mutex.synchronize { entry_for(owner, id).path }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def fetch(owner, id, offset: 0)
|
|
54
|
+
raise Error, "Invalid image offset." unless offset.is_a?(Integer) && offset >= 0
|
|
55
|
+
|
|
56
|
+
@mutex.synchronize do
|
|
57
|
+
entry = entry_for(owner, id)
|
|
58
|
+
raise Error, "Invalid image offset." if offset > entry.preview.size
|
|
59
|
+
return [entry.preview, "".b] if offset == entry.preview.size
|
|
60
|
+
|
|
61
|
+
bytes = File.binread(entry.path, FETCH_CHUNK_BYTES, offset)
|
|
62
|
+
raise Error, "Image preview is unavailable or has been discarded." unless bytes
|
|
63
|
+
|
|
64
|
+
[entry.preview, bytes]
|
|
65
|
+
end
|
|
66
|
+
rescue SystemCallError
|
|
67
|
+
raise Error, "Image preview is unavailable or has been discarded."
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def save_to_project(owner, id, directory = ".")
|
|
71
|
+
export(owner, id, directory, project: true)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def save_to_downloads(owner, id)
|
|
75
|
+
export(owner, id, downloads_directory)
|
|
76
|
+
rescue SystemCallError
|
|
77
|
+
raise Error, "Could not locate the Downloads folder."
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def discard(owner, id)
|
|
81
|
+
@mutex.synchronize do
|
|
82
|
+
entry = entry_for(owner, id)
|
|
83
|
+
FileUtils.rm_f(entry.path)
|
|
84
|
+
@entries.delete(id)
|
|
85
|
+
end
|
|
86
|
+
true
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def discard_owner(owner)
|
|
90
|
+
@mutex.synchronize do
|
|
91
|
+
@entries.delete_if do |_id, entry|
|
|
92
|
+
next false unless entry.owner.equal?(owner)
|
|
93
|
+
|
|
94
|
+
FileUtils.rm_f(entry.path)
|
|
95
|
+
true
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def close
|
|
101
|
+
@mutex.synchronize do
|
|
102
|
+
@closed = true
|
|
103
|
+
@entries.clear
|
|
104
|
+
with_base_lock { FileUtils.remove_entry_secure(@run) } if @run && File.directory?(@run)
|
|
105
|
+
ensure
|
|
106
|
+
@run = nil
|
|
107
|
+
@run_lock&.close
|
|
108
|
+
@run_lock = nil
|
|
109
|
+
end
|
|
110
|
+
rescue Error, SystemCallError
|
|
111
|
+
# A later instance will reap the run if removal could not finish.
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def initialize_run
|
|
118
|
+
begin
|
|
119
|
+
Dir.mkdir(@base, 0o700)
|
|
120
|
+
rescue Errno::EEXIST
|
|
121
|
+
nil
|
|
122
|
+
end
|
|
123
|
+
with_base_lock do
|
|
124
|
+
run = Dir.mktmpdir("run-", @base)
|
|
125
|
+
run_lock = File.open(File.join(run, "lock"), LOCK_FLAGS, 0o600)
|
|
126
|
+
run_lock.flock(File::LOCK_EX)
|
|
127
|
+
@run, @run_lock = run, run_lock
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def reap_crashed_runs
|
|
132
|
+
with_base_lock { remove_stale_runs }
|
|
133
|
+
rescue Error, SystemCallError
|
|
134
|
+
# The directory may not exist yet. Report storage errors on generation.
|
|
135
|
+
nil
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def with_base_lock
|
|
139
|
+
stat = File.lstat(@base)
|
|
140
|
+
raise Error, "Image preview directory is not private." unless stat.directory? && stat.uid == Process.uid && (stat.mode & 0o077).zero?
|
|
141
|
+
|
|
142
|
+
File.open(File.join(@base, "lock"), LOCK_FLAGS, 0o600) do |lock|
|
|
143
|
+
lock.flock(File::LOCK_EX)
|
|
144
|
+
yield
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def remove_stale_runs
|
|
149
|
+
Dir.each_child(@base) do |name|
|
|
150
|
+
next unless name.start_with?("run-")
|
|
151
|
+
|
|
152
|
+
path = File.join(@base, name)
|
|
153
|
+
next unless File.lstat(path).directory?
|
|
154
|
+
|
|
155
|
+
File.open(File.join(path, "lock"), LOCK_FLAGS, 0o600) do |lock|
|
|
156
|
+
FileUtils.remove_entry_secure(path) if lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
157
|
+
end
|
|
158
|
+
rescue SystemCallError
|
|
159
|
+
next
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def entry_for(owner, id)
|
|
164
|
+
entry = @entries[id]
|
|
165
|
+
raise Error, "Image preview is unavailable or has been discarded." unless entry&.owner&.equal?(owner)
|
|
166
|
+
|
|
167
|
+
entry
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def export(owner, id, directory, project: false)
|
|
171
|
+
@mutex.synchronize do
|
|
172
|
+
entry = entry_for(owner, id)
|
|
173
|
+
root = entry.preview.root if project
|
|
174
|
+
destination = File.expand_path(directory.to_s, root)
|
|
175
|
+
handle = NativeShell::Directory.new(destination)
|
|
176
|
+
begin
|
|
177
|
+
resolved = handle.physical_path
|
|
178
|
+
if root && !within?(resolved, root)
|
|
179
|
+
raise Error, "Choose a directory inside the project."
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
write_exclusively(handle, resolved, entry)
|
|
183
|
+
ensure
|
|
184
|
+
handle.close
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
rescue IOError, SystemCallError, ArgumentError
|
|
188
|
+
raise Error, "Could not save the image. Choose an existing, writable directory."
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def within?(path, root)
|
|
192
|
+
path == root || path.start_with?(root.end_with?(File::SEPARATOR) ? root : "#{root}#{File::SEPARATOR}")
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def write_exclusively(handle, directory, entry)
|
|
196
|
+
stem = File.basename(entry.preview.filename, ".png")
|
|
197
|
+
1000.times do |index|
|
|
198
|
+
name = index.zero? ? "#{stem}.png" : "#{stem}-#{index + 1}.png"
|
|
199
|
+
path = File.join(directory, name)
|
|
200
|
+
flags = File::WRONLY | File::CREAT | File::EXCL
|
|
201
|
+
output = handle.open_file(name, flags)
|
|
202
|
+
begin
|
|
203
|
+
stat = output.stat
|
|
204
|
+
output.binmode
|
|
205
|
+
IO.copy_stream(entry.path, output)
|
|
206
|
+
output.close
|
|
207
|
+
rescue IOError, SystemCallError
|
|
208
|
+
remove_failed_export(path, stat) if stat
|
|
209
|
+
raise
|
|
210
|
+
ensure
|
|
211
|
+
output.close unless output.closed?
|
|
212
|
+
end
|
|
213
|
+
return path
|
|
214
|
+
rescue Errno::EEXIST
|
|
215
|
+
next
|
|
216
|
+
end
|
|
217
|
+
raise Error, "Could not find an unused image filename."
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def remove_failed_export(path, stat)
|
|
221
|
+
current = File.lstat(path)
|
|
222
|
+
File.unlink(path) if stat.ino == current.ino && stat.dev == current.dev
|
|
223
|
+
rescue SystemCallError
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def downloads_directory
|
|
228
|
+
home = Dir.home
|
|
229
|
+
config = File.join(ENV.fetch("XDG_CONFIG_HOME", File.join(home, ".config")), "user-dirs.dirs")
|
|
230
|
+
if RUBY_PLATFORM.include?("linux") && File.file?(config)
|
|
231
|
+
value = File.read(config, 16_384)[/^XDG_DOWNLOAD_DIR="(\$HOME(?:\/[^"\n]*)?|\/[^"\n]*)"$/, 1]
|
|
232
|
+
return value.sub(/\A\$HOME(?=\/|\z)/) { home } if value
|
|
233
|
+
end
|
|
234
|
+
File.join(home, "Downloads")
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|
data/lib/kward/model/catalog.rb
CHANGED
|
@@ -80,8 +80,10 @@ module Kward
|
|
|
80
80
|
|
|
81
81
|
def normalize(body)
|
|
82
82
|
payload = JSON.parse(body.to_s)
|
|
83
|
-
entries = payload["data"] || payload["models"]
|
|
84
|
-
|
|
83
|
+
entries = payload.is_a?(Hash) && (payload["data"] || payload["models"])
|
|
84
|
+
raise "Invalid model catalog" unless entries.is_a?(Array)
|
|
85
|
+
|
|
86
|
+
entries.filter_map { |entry| normalize_entry(entry) }.sort_by { |entry| entry["id"] }
|
|
85
87
|
rescue JSON::ParserError
|
|
86
88
|
raise "#{@provider.name} model refresh returned invalid JSON"
|
|
87
89
|
end
|
|
@@ -89,7 +91,8 @@ module Kward
|
|
|
89
91
|
def normalize_entry(entry)
|
|
90
92
|
return unless entry.is_a?(Hash)
|
|
91
93
|
|
|
92
|
-
id = (entry["id"] || entry["name"]).to_s
|
|
94
|
+
id = (entry["id"] || entry["name"]).to_s
|
|
95
|
+
id = id.delete_prefix("models/") if @provider.id == "gemini"
|
|
93
96
|
return if id.empty?
|
|
94
97
|
|
|
95
98
|
{
|