ollama_chat 0.0.138 → 0.0.140

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 170c9e2778ed0d581bda082eebc43d07a6c6e6a6cd99a0d1e0d3634a5281ca9f
4
- data.tar.gz: e57e2eb4d990f10617e1574ae1f70d22741dd814cf945575cc2cb7fb882dd4be
3
+ metadata.gz: c1cff963a5116fbe9c1a4a94fff41df7128c5f31c4daaaf96e2a4334c97d77d5
4
+ data.tar.gz: 97ec4d9f4f90c71b4bc2eee13f2000bb4f8fb7ad6b9fe308c82342734e3d1629
5
5
  SHA512:
6
- metadata.gz: b5e6781664a38632da9d7a4f11900864b86b9e0049fcb66d6084777d0f8b4a85e762a57217c23817c77f0fb365592d5d659e3eab874aa270fa97b52a3e727c45
7
- data.tar.gz: 6aa806be34d04830f4788dd452d6e8a078e848767978d6bf49d6fef39f3dbd92b3f61fd4dc203c9914491c1ab2d8fa432ff75bc3543bf385dffcda2333acbd83
6
+ metadata.gz: f1abfdfaab1002d8503a7a72b204ee247f2375df5e6b3de417c483826ad88208ceca28c73921afcdd0b8ec2575e15c4cf9ade009fd7aab99b18a707a9451891f
7
+ data.tar.gz: ddd78c34605c7035c0d24e9ba76e59a28edd8a85f2116478b97474e4233fb47077b297134814a3e829abd4af43d343a6ce5ca191a33cc46e174d46664186dbce
data/CHANGES.md CHANGED
@@ -1,5 +1,78 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-11-02 v0.0.140
4
+
5
+ ### Improvements
6
+
7
+ - Accept Invidious instance URLs for video transcripts. `fetch_video_info` now
8
+ delegates ID extraction to `extract_video_id`, which supports standard
9
+ YouTube URLs and watch, embed, and shorts paths on the configured Invidious
10
+ instance via `invidious_pattern`.
11
+ - Extract the confirmation gate logic in `ToolCallProcessor` into a private
12
+ `confirm_tool` method, returning a `[confirmed, resolve]` tuple to simplify
13
+ the `handle` method.
14
+ - In `ExecuteShell`, pressing `C-c` at the `📝 Instructions:` prompt now
15
+ re-prompts the user for the y/n/i/a/d choice instead of submitting a blank
16
+ instruction.
17
+ - Updated the instruction prompt in `ExecuteShell` to display a `(C-c ⇨
18
+ cancel)` escape hint.
19
+
20
+ ### Testing
21
+
22
+ - Added a spec context verifying that an Invidious instance watch URL correctly
23
+ extracts the video ID and produces a transcript.
24
+
25
+ ## 2026-10-30 v0.0.139
26
+
27
+ ### Features
28
+
29
+ - Added terminal table wrapping for `/help` and `/session list` output. New
30
+ private methods `wrap_table_rows` and `table_column_budgets` in
31
+ `OllamaChat::KramdownANSI` compute per-column width budgets and wrap cell
32
+ content to fit within a configurable percentage of the terminal width.
33
+ - Updated `help_message` and `list_sessions` to collect rows into arrays and
34
+ route them through `wrap_table_rows`, removing manual truncation and heading
35
+ assignment.
36
+
37
+ ### Fixes
38
+
39
+ - Fixed a `TypeError` in `Collection.sync` when `chat.documents.collections`
40
+ yielded no collections (e.g., fresh database) by using `.to_a` instead of
41
+ relying on `Tins::NullClass#to_ary`.
42
+ - Improved error handling in `fetch_source` for `OllamaChat::HTTPError` by
43
+ adding a dedicated rescue clause that logs a warning without a backtrace and
44
+ stamps `response_status` and `response_reason` onto the
45
+ `ResponseMetadata.failed` IO.
46
+ - Added a non-2xx HTTP status guard in the `get_url` tool to short-circuit
47
+ processing before document policy dispatch, returning the raw body and a
48
+ status message instead of feeding error pages to the summarizer.
49
+
50
+ ### Improvements
51
+
52
+ - Updated the compaction voice report to format context fill percentages as
53
+ rounded integers with the unit "percent" for grammatical completeness
54
+ (e.g., "from 38 percent to 19 percent").
55
+ - Moved `configure_kramdown_ansi_styles` to a private method in
56
+ `OllamaChat::KramdownANSI`.
57
+ - Expanded YARD documentation for `OllamaChat::KramdownANSI` to describe
58
+ styling, parsing, and table wrapping concerns.
59
+
60
+ ### Build & CI
61
+
62
+ - Bumped `kramdown-ansi` dependency from `~> 0.7` to `~> 0.9` in `Rakefile`
63
+ and `ollama_chat.gemspec`.
64
+
65
+ ### Testing
66
+
67
+ - Updated `compaction_spec.rb` to expect rounded integer percentages in the
68
+ spoken compaction report.
69
+ - Updated `kramdown_ansi_spec.rb` to use a real `OllamaChat::Chat` instance
70
+ instead of a double and added examples for `wrap_table_rows` and
71
+ `table_column_budgets`.
72
+ - Updated `get_url_spec.rb` to stub `response_status: nil` on `SourceIO`
73
+ doubles and added a spec verifying that non-200 statuses skip summarization
74
+ and import.
75
+
3
76
  ## 2026-10-28 v0.0.138
4
77
 
5
78
  ### Features
data/Rakefile CHANGED
@@ -47,7 +47,7 @@ GemHadar do
47
47
  dependency 'redis', '~> 5.0'
48
48
  dependency 'mime-types', '~> 3.0'
49
49
  dependency 'reverse_markdown', '~> 3.0'
50
- dependency 'kramdown-ansi', '~> 0.7'
50
+ dependency 'kramdown-ansi', '~> 0.9'
51
51
  dependency 'complex_config', '~> 0.22', '>= 0.22.2'
52
52
  dependency 'tins', '~> 1.61'
53
53
  dependency 'search_ui', '>= 0.2'
@@ -6,6 +6,8 @@ module OllamaChat::CommandConcern
6
6
  extend Tins::Concern
7
7
 
8
8
  class_methods do
9
+ include OllamaChat::KramdownANSI
10
+
9
11
  # @!attribute [rw] commands
10
12
  # @return [Hash<Symbol, Command>] A hash mapping command names to
11
13
  # `Command` instances.
@@ -76,8 +78,8 @@ module OllamaChat::CommandConcern
76
78
  all_separators: true,
77
79
  border: :unicode_round,
78
80
  }
79
- table.headings = %w[ CMD SUBCMD OPTS HELP ]
80
81
  sorted_commands = self.commands.sort_by { [ _2.category, _2.name ] }.map(&:last)
82
+ rows = []
81
83
  cat = nil
82
84
  sorted_commands.each do |command|
83
85
  command.help or next
@@ -93,15 +95,16 @@ module OllamaChat::CommandConcern
93
95
  }
94
96
  if cat != command.category
95
97
  cat = command.category
96
- table << [ { value: bold{cat}, colspan: 4 } ]
98
+ rows << [ { value: bold{cat}, colspan: 4 } ]
97
99
  end
98
- table << [
99
- "%s" % command.command_names.map { ?/ + _1 }.join(?\n),
100
+ rows << [
101
+ '%s' % command.command_names.map { ?/ + _1 }.join(?\n),
100
102
  '%s' % subcommands,
101
103
  command.options,
102
104
  command.help,
103
105
  ]
104
106
  end
107
+ wrap_table_rows(rows, 4, headings: %w[ CMD SUBCMD OPTS HELP ], percentage: 95).each { |row| table << row }
105
108
  table
106
109
  end
107
110
  end
@@ -327,7 +327,7 @@ module OllamaChat::Compaction
327
327
  duration.minutes? ? '%M minutes and %u seconds' : '%u seconds'
328
328
  )
329
329
  speak(
330
- 'Compressed conversation context from %.f to %.f in %s.' % [
330
+ 'Compressed conversation context from %.0f percent to %.0f percent in %s.' % [
331
331
  100 * result.context_filled_before,
332
332
  100 * result.context_filled_after,
333
333
  duration_string,
@@ -51,7 +51,7 @@ class OllamaChat::Database::Models::Collection < Sequel::Model(OllamaChat::DB)
51
51
  #
52
52
  # @param chat [OllamaChat::Chat] the chat instance providing the configuration
53
53
  def self.sync(chat)
54
- all_collections = (%w[ default ] + chat.documents.collections).to_set
54
+ all_collections = (%w[ default ] + chat.documents.collections.to_a).to_set
55
55
  all_collections.each do |name|
56
56
  where(name: name.to_s).first and next
57
57
  create(
@@ -2,7 +2,9 @@
2
2
  # companion instance.
3
3
  #
4
4
  # Only activates when OC::OLLAMA::CHAT::INVIDIOUS::URL is set.
5
- # Non-YouTube URLs and unconfigured environments return nil.
5
+ # Accepts standard YouTube URLs and watch URLs on the configured
6
+ # Invidious instance. Unmatched URLs and unconfigured environments
7
+ # return nil.
6
8
  #
7
9
  # @example Fetching video info for a YouTube watch URL
8
10
  # text = OllamaChat::Invidious.fetch_video_info(
@@ -30,18 +32,18 @@ module OllamaChat
30
32
  # 3. GET the matched track's baseUrl → timedtext XML
31
33
  # 4. Extract <text> elements via Nokogiri → plain text
32
34
  #
33
- # @param url [String] the YouTube source URL
35
+ # @param url [String] a YouTube or Invidious instance watch URL
34
36
  # @param chat [OllamaChat::Chat] the chat instance (for HTTP
35
37
  # middleware)
36
38
  # @return [StringIO, nil] video info + transcript as a text IO,
37
- # or nil if not a YouTube URL, Invidious not configured, or
39
+ # or nil if URL unmatched, Invidious not configured, or
38
40
  # video details unavailable
39
41
  def fetch_video_info(url, chat:)
40
42
  invidious_url = OC::OLLAMA::CHAT::INVIDIOUS::URL? or return
41
- video_id = url[YOUTUBE_URLS, 1] or return
43
+ base = invidious_url.to_s
44
+ video_id = extract_video_id(url, base) or return
42
45
 
43
- base = invidious_url.to_s
44
- key = OC::OLLAMA::CHAT::INVIDIOUS::COMPANION_KEY
46
+ key = OC::OLLAMA::CHAT::INVIDIOUS::COMPANION_KEY
45
47
 
46
48
  # Step 1: POST /companion/youtubei/v1/player
47
49
  player_response = nil
@@ -157,6 +159,25 @@ module OllamaChat
157
159
  def format_duration(seconds)
158
160
  Tins::Duration.new(seconds).to_s
159
161
  end
162
+
163
+ # Extracts the 11-character video ID from a YouTube or Invidious
164
+ # instance watch URL.
165
+ #
166
+ # @param url [String] the source URL
167
+ # @param base [String] the configured Invidious base URL
168
+ # @return [String, nil] the video ID
169
+ def extract_video_id(url, base)
170
+ url[YOUTUBE_URLS, 1] || url[invidious_pattern(base), 1]
171
+ end
172
+
173
+ # Builds a regex for watch/embed/shorts paths on the configured
174
+ # Invidious instance.
175
+ #
176
+ # @param base [String] the Invidious instance base URL
177
+ # @return [Regexp]
178
+ def invidious_pattern(base)
179
+ /\A#{Regexp.escape(base)}\/(?:watch\?v=|embed\/|shorts\/)([\w-]{11})/
180
+ end
160
181
  end
161
182
  end
162
183
  end
@@ -1,30 +1,22 @@
1
- # A module that provides Kramdown::ANSI styling configuration and parsing
2
- # functionality for OllamaChat.
1
+ # A module that provides Kramdown::ANSI styling, parsing, and
2
+ # terminal table wrapping for OllamaChat.
3
3
  #
4
- # This module handles the setup and application of ANSI styles for markdown
5
- # rendering, allowing for customizable terminal output formatting. It manages
6
- # the configuration of ANSI styles either from environment variables or falls
7
- # back to default settings, and provides methods to parse content with the
8
- # configured styling.
4
+ # This module handles three concerns:
5
+ #
6
+ # * **Styling configuration** – sets up ANSI styles either from the
7
+ # `KRAMDOWN_ANSI_OLLAMA_CHAT_STYLES` environment variable or from
8
+ # built-in defaults.
9
+ # * **Markdown parsing** – converts raw markdown into ANSI-formatted
10
+ # terminal output using the configured styles, with a fallback for
11
+ # invalid encodings.
12
+ # * **Table wrapping** – computes per-column width budgets and wraps
13
+ # cell content so that `Terminal::Table` output stays within a
14
+ # configurable percentage of the terminal width.
9
15
  #
10
16
  # @example Configuring custom ANSI styles via environment variable
11
17
  # Set KRAMDOWN_ANSI_OLLAMA_CHAT_STYLES to a JSON object containing style
12
18
  # definitions for customizing markdown output formatting in the terminal.
13
19
  module OllamaChat::KramdownANSI
14
- # The configure_kramdown_ansi_styles method sets up ANSI styling for
15
- # Kramdown::ANSI output by checking for specific environment variables and
16
- # falling back to default styles.
17
- #
18
- # @return [ Hash ] a hash of ANSI styles configured either from environment
19
- # variables or using default settings
20
- def configure_kramdown_ansi_styles
21
- if json = OC::KRAMDOWN_ANSI_OLLAMA_CHAT_STYLES?
22
- Kramdown::ANSI::Styles.from_json(json).ansi_styles
23
- else
24
- Kramdown::ANSI::Styles.new.ansi_styles
25
- end
26
- end
27
-
28
20
  # The kramdown_ansi_parse method processes content using Kramdown::ANSI with
29
21
  # custom ANSI styles.
30
22
  #
@@ -49,6 +41,22 @@ module OllamaChat::KramdownANSI
49
41
  retry
50
42
  end
51
43
 
44
+ private
45
+
46
+ # The configure_kramdown_ansi_styles method sets up ANSI styling for
47
+ # Kramdown::ANSI output by checking for specific environment variables and
48
+ # falling back to default styles.
49
+ #
50
+ # @return [ Hash ] a hash of ANSI styles configured either from environment
51
+ # variables or using default settings
52
+ def configure_kramdown_ansi_styles
53
+ if json = OC::KRAMDOWN_ANSI_OLLAMA_CHAT_STYLES?
54
+ Kramdown::ANSI::Styles.from_json(json).ansi_styles
55
+ else
56
+ Kramdown::ANSI::Styles.new.ansi_styles
57
+ end
58
+ end
59
+
52
60
  # The kramdown_markdown_remove method strips markdown formatting, emoji,
53
61
  # ANSI escape sequences, and box-drawing characters from content, yielding
54
62
  # plain text suitable for TTS playback.
@@ -74,4 +82,82 @@ module OllamaChat::KramdownANSI
74
82
  .gsub(/\s{2,}/, ' ')
75
83
  .strip
76
84
  end
85
+
86
+ # Wraps the cells of a terminal table to fit within a terminal-width
87
+ # budget.
88
+ #
89
+ # Terminal::Table sizes each column to its widest cell, so a single long
90
+ # cell forces the whole table wider than the terminal. This computes a
91
+ # per-column budget from the widest cell in each column across all rows,
92
+ # then wraps each cell to it. Cells expressed as Hashes (e.g. colspan
93
+ # rows) are passed through untouched.
94
+ #
95
+ # @param rows [Array<Array<String, Hash>>] the data rows to wrap
96
+ # @param cols [Integer] the number of columns in the table
97
+ # @param headings [Array<String>, nil] the heading row; when given it is
98
+ # measured for the budget and returned first, right-padded to each
99
+ # column's allocated width so Terminal::Table sizes columns to the
100
+ # full budget even when no data cell reaches it
101
+ # @param percentage [Numeric] the percentage of terminal width to use for
102
+ # the total budget (default: 90)
103
+ # @return [Array<Array<String, Hash>>] wrapped rows; the padded heading
104
+ # row is returned first when `headings` was supplied
105
+ def wrap_table_rows(rows, cols, headings: nil, percentage: 90)
106
+ all = rows + (headings ? [headings] : [])
107
+ budget = table_column_budgets(all, cols, percentage:)
108
+ wrap_row = ->(row) {
109
+ row.each_with_index.map { |cell, i|
110
+ cell.is_a?(Hash) and next cell
111
+ Kramdown::ANSI::Width.wrap(cell.to_s, length: budget[i], reflow: false)
112
+ }
113
+ }
114
+ result = rows.map(&wrap_row)
115
+ if headings
116
+ padded = headings.each_with_index.map { |h, i|
117
+ w = Kramdown::ANSI::Width.display_width(h)
118
+ w < budget[i] ? h + ' ' * (budget[i] - w) : h
119
+ }
120
+ result.unshift(padded)
121
+ end
122
+ result
123
+ end
124
+
125
+ # Computes a per-column display-width budget for a terminal table.
126
+ #
127
+ # @param rows [Array<Array<String, Hash>>] the rows to measure (Hash cells
128
+ # are ignored)
129
+ # @param cols [Integer] the number of columns
130
+ # @param percentage [Numeric] the percentage of terminal width to use for
131
+ # the total budget (default: 90)
132
+ # @return [Array<Integer>] the per-column width budget
133
+ def table_column_budgets(rows, cols, percentage: 90)
134
+ max_w = Array.new(cols, 0)
135
+ rows.each do |row|
136
+ row.each_with_index do |cell, i|
137
+ cell.is_a?(Hash) and next
138
+ w = cell.to_s.lines.map { |l| Kramdown::ANSI::Width.display_width(l) }.max || 0
139
+ max_w[i] = [max_w[i], w].max
140
+ end
141
+ end
142
+ total = max_w.sum
143
+ budget = Kramdown::ANSI::Width.width(percentage:) - (3 * cols + 1)
144
+ return Array.new(cols, (budget.to_f / cols).to_i) if total.zero?
145
+ if total <= budget
146
+ extra = budget - total
147
+ max_w.map { |w| w + ((w.to_f / total) * extra).floor }
148
+ else
149
+ share = (budget.to_f / cols).to_i
150
+ alloc = max_w.map { |w| [w, share].min }
151
+ unused = budget - alloc.sum
152
+ return alloc if unused.zero?
153
+ capped = (0...cols).to_a.select { |i| alloc[i] < max_w[i] }
154
+ need = capped.sum { |i| max_w[i] - alloc[i] }
155
+ return alloc if need.zero?
156
+ capped.each do |i|
157
+ extra = ((max_w[i] - alloc[i]).to_f / need * unused).floor
158
+ alloc[i] = [max_w[i], alloc[i] + extra].min
159
+ end
160
+ alloc
161
+ end
162
+ end
77
163
  end
@@ -107,22 +107,23 @@ module OllamaChat::SessionManagement
107
107
  all_separators: true,
108
108
  border: :unicode_round,
109
109
  }
110
- table.headings = %w[ ID NAME SIZE #TOK COUNT UPDATED ].map { |header| bold { header } }
111
- now = Time.now
110
+ headings = %w[ ID NAME SIZE #TOK COUNT UPDATED ].map { |h| bold { h } }
111
+ now = Time.now
112
+ rows = []
112
113
  models::Session.order(Sequel.desc(:updated_at)).each do |s|
113
- name = truncate(s.name, length: 32)
114
- name = session.id == s.id ? bold { name } : name
115
- name = if pid = s.locked?
116
- if pid == $$
117
- "#{name} 🔓"
118
- else
119
- "#{name} 🔐"
120
- end
121
- else
122
- name
123
- end
124
- es = s.estimate_tokens
125
- table << [
114
+ name = s.name
115
+ name = session.id == s.id ? bold { name } : name
116
+ name = if pid = s.locked?
117
+ if pid == $$
118
+ "#{name} 🔓"
119
+ else
120
+ "#{name} 🔐"
121
+ end
122
+ else
123
+ name
124
+ end
125
+ es = s.estimate_tokens
126
+ rows << [
126
127
  s.id.to_s,
127
128
  name,
128
129
  es.bytes_formatted,
@@ -131,6 +132,7 @@ module OllamaChat::SessionManagement
131
132
  s.age(now:),
132
133
  ]
133
134
  end
135
+ wrap_table_rows(rows, 6, headings:).each { |row| table << row }
134
136
  table.align_column 0, :right
135
137
  table.align_column 2, :left
136
138
  table.align_column 3, :right
@@ -33,6 +33,7 @@ module OllamaChat::SourceFetching
33
33
  #
34
34
  # @yield [ tmp ]
35
35
  def fetch_source(source, check_exist: false, &block)
36
+ confirm_notification = false
36
37
  source = source.ask_and_send_or_self(:to_path).to_s
37
38
  case source
38
39
  when %r{\A!(.*)}
@@ -72,14 +73,28 @@ module OllamaChat::SourceFetching
72
73
  else
73
74
  raise "invalid source #{source.inspect}"
74
75
  end
76
+ rescue OllamaChat::HTTPError => e
77
+ msg = "Fetching source #{source.to_s.inspect}: #{e.class} #{e}"
78
+ feedback(msg, type: :warn)
79
+ log(:warn, msg, data: { source: source.to_s, status: e.status })
80
+ confirm_notification = true
81
+ msg = OllamaChat::Utils::Fetcher::ResponseMetadata.failed(msg)
82
+ msg.response_status = e.status
83
+ msg.response_reason = e.message
84
+ block.(msg)
85
+ msg
75
86
  rescue => e
76
87
  msg = "Fetching source #{source.to_s.inspect}: #{e.class} #{e}"
77
88
  feedback("#{msg}\n#{e.backtrace * ?\n}", type: :warn)
78
89
  log(:error, msg, data: { source: source.to_s, error: e.class })
79
- confirm?(prompt: '⏎ Press any key to continue (%s). ', output: STDERR, timeout: 3)
90
+ confirm_notification = true
80
91
  msg = OllamaChat::Utils::Fetcher::ResponseMetadata.failed(msg)
81
92
  block.(msg)
82
93
  msg
94
+ ensure
95
+ if confirm_notification
96
+ confirm?(prompt: "\n⏎ Press any key to continue (%s). ", output: STDERR, timeout: 3)
97
+ end
83
98
  end
84
99
 
85
100
  # Adds an image to the images collection from the given source IO and source
@@ -61,33 +61,9 @@ class OllamaChat::ToolCallProcessor
61
61
  next
62
62
  end
63
63
  feedback(?\n, output:)
64
- confirmed = :implicit
65
- resolve = nil
66
64
  function = JSON.pretty_generate(tool_call.function)
67
65
  chat.log(:info, "Tool call received", data: { tool: name, function: })
68
- if chat.tool_function(name).require_confirmation?
69
- feedback(
70
- ("I want to execute tool %s\n%s\n" % [ bold { name }, italic { function }, ]),
71
- type: :alarm
72
- )
73
- prompt = '❓ Allow ✅[y]es / ⛔️[n]o / 📝[i]nstruct? '
74
- answer = chat.confirm?(prompt:)&.to_s&.downcase
75
- resolve = 'You **MUST** ask the user for instructions on how to proceed!!!'
76
- case answer
77
- when 'y'
78
- confirmed = :explicit
79
- when 'n'
80
- confirmed = :denied
81
- else
82
- confirmed = :denied
83
- instr = chat.ask?(prompt: '📝 Instructions: ')&.strip
84
- resolve = instr.full? || resolve
85
- end
86
- else
87
- feedback(
88
- "Executing tool %s\n%s" % [ bold { name }, italic { function }, ]
89
- )
90
- end
66
+ confirmed, resolve = confirm_tool(name, function)
91
67
  start = Time.now
92
68
  result = nil
93
69
  case confirmed
@@ -173,4 +149,49 @@ class OllamaChat::ToolCallProcessor
173
149
  def clear!
174
150
  @results.clear
175
151
  end
152
+
153
+ private
154
+
155
+ # Prompts the user for confirmation on a tool call that requires it.
156
+ # Returns a `[confirmed, resolve]` tuple where `confirmed` is one of
157
+ # `:implicit`, `:explicit`, `:denied` and `resolve` is an optional
158
+ # instruction string for the LLM.
159
+ #
160
+ # @param name [String] the tool name
161
+ # @param function [String] pretty-printed JSON of the tool call
162
+ # @return [Array(Symbol, String?)]
163
+ def confirm_tool(name, function)
164
+ confirmed = :implicit
165
+ resolve = nil
166
+
167
+ 1.times do
168
+ if chat.tool_function(name).require_confirmation?
169
+ feedback(
170
+ ("I want to execute tool %s\n%s\n" % [ bold { name }, italic { function }, ]),
171
+ type: :alarm
172
+ )
173
+ prompt = '❓ Allow ✅[y]es / ⛔️[n]o / 📝[i]nstruct? '
174
+ answer = chat.confirm?(prompt:)&.to_s&.downcase
175
+ resolve = 'You **MUST** ask the user for instructions on how to proceed!!!'
176
+ case answer
177
+ when 'y'
178
+ confirmed = :explicit
179
+ when 'n'
180
+ confirmed = :denied
181
+ else
182
+ confirmed = :denied
183
+ instr = chat.ask?(prompt: '📝 Instructions (C-c ⇨ cancel): ')
184
+ instr.nil? and redo
185
+ instr = instr&.strip
186
+ resolve = instr.full? || resolve
187
+ end
188
+ else
189
+ feedback(
190
+ "Executing tool %s\n%s" % [ bold { name }, italic { function }, ]
191
+ )
192
+ end
193
+ end
194
+
195
+ [confirmed, resolve]
196
+ end
176
197
  end
@@ -102,25 +102,29 @@ class OllamaChat::Tools::ExecuteShell
102
102
  y once · n cancel · i instruct me · a always (pager) · d always (direct)
103
103
 
104
104
  EOT
105
- answer = chat.confirm?(
106
- prompt: '❓ Allow ✅[y]es / ⛔️[n]o / 📝[i]nstruct / 🔁[a]lways / 📄[d]irect? '
107
- )&.to_s&.downcase
108
-
109
- case answer
110
- when 'y'
111
- # proceed to execution
112
- when 'a'
113
- self.class.always_allowed[command] = :always
114
- when 'd'
115
- self.class.always_allowed[command] = :direct
116
- when 'n'
117
- raise OllamaChat::ToolFunctionArgumentError,
118
- "Shell command cancelled by user: #{command}"
119
- else
120
- instr = chat.ask?(prompt: '📝 Instructions: ')&.strip
121
- resolve = instr.full? || 'You **MUST** ask the user for instructions!'
122
- raise OllamaChat::ToolFunctionArgumentError,
123
- "User instruct: #{resolve} (command: #{command})"
105
+ 1.times do
106
+ answer = chat.confirm?(
107
+ prompt: '❓ Allow ✅[y]es / ⛔️[n]o / 📝[i]nstruct / 🔁[a]lways / 📄[d]irect? '
108
+ )&.to_s&.downcase
109
+
110
+ case answer
111
+ when 'y'
112
+ # proceed to execution
113
+ when 'a'
114
+ self.class.always_allowed[command] = :always
115
+ when 'd'
116
+ self.class.always_allowed[command] = :direct
117
+ when 'n'
118
+ raise OllamaChat::ToolFunctionArgumentError,
119
+ "Shell command cancelled by user: #{command}"
120
+ else
121
+ instr = chat.ask?(prompt: '📝 Instructions (C-c ⇨ cancel): ')
122
+ instr.nil? and redo
123
+ instr = instr&.strip
124
+ resolve = instr.full? || 'You **MUST** ask the user for instructions!'
125
+ raise OllamaChat::ToolFunctionArgumentError,
126
+ "User instruct: #{resolve} (command: #{command})"
127
+ end
124
128
  end
125
129
  end
126
130
  end
@@ -116,6 +116,11 @@ class OllamaChat::Tools::GetURL
116
116
 
117
117
  source, message, content = url, nil, ''
118
118
  chat.fetch_source(source, check_exist: false) do |source_io|
119
+ if (status = source_io&.response_status) && status != 200
120
+ content = source_io.read
121
+ message = "Received HTTP status #{status} from #{source}."
122
+ next
123
+ end
119
124
  if source_io&.content_type&.to_s&.match?(OllamaChat::Parsing::HAS_AUDIO)
120
125
  text = chat.parse_audio(source_io, language:)
121
126
  source_io = OllamaChat::Utils::Fetcher::ResponseMetadata.as_text(text)
@@ -5,7 +5,7 @@
5
5
  # by hand; it is rewritten on every `rake version` invocation.
6
6
  module OllamaChat
7
7
  # The current version of the OllamaChat gem as a String.
8
- VERSION = '0.0.138'
8
+ VERSION = '0.0.140'
9
9
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
10
10
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
11
11
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/ollama_chat.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: ollama_chat 0.0.138 ruby lib
2
+ # stub: ollama_chat 0.0.140 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ollama_chat".freeze
6
- s.version = "0.0.138".freeze
6
+ s.version = "0.0.140".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
@@ -41,7 +41,7 @@ Gem::Specification.new do |s|
41
41
  s.add_runtime_dependency(%q<redis>.freeze, ["~> 5.0".freeze])
42
42
  s.add_runtime_dependency(%q<mime-types>.freeze, ["~> 3.0".freeze])
43
43
  s.add_runtime_dependency(%q<reverse_markdown>.freeze, ["~> 3.0".freeze])
44
- s.add_runtime_dependency(%q<kramdown-ansi>.freeze, ["~> 0.7".freeze])
44
+ s.add_runtime_dependency(%q<kramdown-ansi>.freeze, ["~> 0.9".freeze])
45
45
  s.add_runtime_dependency(%q<complex_config>.freeze, ["~> 0.22".freeze, ">= 0.22.2".freeze])
46
46
  s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.61".freeze])
47
47
  s.add_runtime_dependency(%q<search_ui>.freeze, [">= 0.2".freeze])
@@ -351,7 +351,7 @@ describe OllamaChat::Compaction do
351
351
  .with(a_string_matching(/Summarized:\s+12 messages/), type: :success)
352
352
  expect(chat).to receive(:speak)
353
353
  .with(a_string_matching(
354
- /Compressed conversation context from \d+ to \d+ in \d+ (?:seconds|minutes and \d+ seconds)\./
354
+ /Compressed conversation context from \d+ percent to \d+ percent in \d+ (?:seconds|minutes and \d+ seconds)\./
355
355
  ))
356
356
 
357
357
  expect(chat.compact_with_retry).to be true
@@ -232,6 +232,28 @@ describe OllamaChat::Invidious do
232
232
  )
233
233
  expect(result).not_to be_nil
234
234
  end
235
+ end
236
+
237
+ context 'with an Invidious instance watch URL' do
238
+ let :tracks do
239
+ [
240
+ track(label: 'English', language_code: 'en',
241
+ base_url: 'https://www.youtube.com/api/timedtext' \
242
+ '?v=MmpZ5Db9JXi&lang=en'),
243
+ ]
244
+ end
245
+
246
+ it 'extracts the video ID correctly' do
247
+ stub_player(tracks:)
248
+ expect(chat).to receive(:get_url)
249
+ .with(anything, remember: false)
250
+ .and_yield(StringIO.new(xml_content))
251
+
252
+ result = described_class.fetch_video_info(
253
+ 'https://invidious.gate.ping.de/watch?v=MmpZ5Db9JXi', chat:
254
+ )
255
+ expect(result).not_to be_nil
256
+ end
235
257
  end
236
258
  end
237
259
  end
@@ -1,8 +1,14 @@
1
1
  describe OllamaChat::KramdownANSI do
2
+ let :argv do
3
+ chat_default_config
4
+ end
5
+
2
6
  let :chat do
3
- double('Chat').extend(described_class)
7
+ OllamaChat::Chat.new(argv:).expose
4
8
  end
5
9
 
10
+ connect_to_ollama_server
11
+
6
12
  describe '#configure_kramdown_ansi_styles', protect_env: true do
7
13
  it 'can be configured via env var' do
8
14
  const_conf_as(
@@ -12,7 +18,6 @@ describe OllamaChat::KramdownANSI do
12
18
  expect(Kramdown::ANSI::Styles).to receive(:from_json).
13
19
  with('{"foo":"bar"}').
14
20
  and_return(double(ansi_styles: styles))
15
-
16
21
  expect(chat.configure_kramdown_ansi_styles).to eq(styles)
17
22
  end
18
23
 
@@ -100,4 +105,51 @@ describe OllamaChat::KramdownANSI do
100
105
  expect(result).to include('**bold**')
101
106
  end
102
107
  end
108
+
109
+ describe '#wrap_table_rows' do
110
+ it 'wraps long cells to fit within terminal budget' do
111
+ long_text = 'word ' * 50
112
+ rows = [[ long_text, 'short' ]]
113
+ result = chat.wrap_table_rows(rows, 2)
114
+ expect(result[0][0]).to include("\n")
115
+ end
116
+
117
+ it 'passes Hash cells (colspan) through untouched' do
118
+ rows = [[ { value: 'category', colspan: 4 } ]]
119
+ result = chat.wrap_table_rows(rows, 4)
120
+ expect(result).to eq(rows)
121
+ end
122
+
123
+ it 'returns wrapped headings first when supplied' do
124
+ rows = [[ 'a', 'b' ]]
125
+ result = chat.wrap_table_rows(rows, 2, headings: ['H1', 'H2'])
126
+ expect(result.first.map(&:strip)).to eq(['H1', 'H2'])
127
+ expect(result.size).to eq(2)
128
+ end
129
+
130
+ it 'handles nil cells by converting to empty string' do
131
+ result = chat.wrap_table_rows([[ nil, 'text' ]], 2)
132
+ expect(result[0][0]).to eq('')
133
+ end
134
+
135
+ it 'preserves short content without wrapping' do
136
+ rows = [[ 'hi', 'there' ]]
137
+ result = chat.wrap_table_rows(rows, 2)
138
+ expect(result[0]).to eq(['hi', 'there'])
139
+ end
140
+ end
141
+
142
+ describe '#table_column_budgets' do
143
+ it 'returns equal budgets when total width is zero' do
144
+ result = chat.table_column_budgets([[ '', '' ]], 2)
145
+ expect(result[0]).to eq(result[1])
146
+ end
147
+
148
+ it 'allocates proportionally based on content width' do
149
+ narrow = 'a'
150
+ wide = 'a' * 20
151
+ result = chat.table_column_budgets([[ narrow, wide ]], 2)
152
+ expect(result[1]).to be > result[0]
153
+ end
154
+ end
103
155
  end
@@ -109,7 +109,7 @@ describe OllamaChat::Tools::GetURL do
109
109
 
110
110
  context 'with different document policies' do
111
111
  let(:url) { 'https://www.example.com/foo' }
112
- let(:source_io) { double('SourceIO', content_type: double(media_type: 'text'), read: 'raw content') }
112
+ let(:source_io) { double('SourceIO', content_type: double(media_type: 'text'), read: 'raw content', response_status: nil) }
113
113
 
114
114
  before do
115
115
  expect(chat).to receive(:fetch_source).and_yield(source_io)
@@ -191,7 +191,7 @@ describe OllamaChat::Tools::GetURL do
191
191
  let(:url) { 'https://www.example.com/foo' }
192
192
 
193
193
  it 'handles image content types' do
194
- source_io = double('SourceIO', content_type: double(media_type: 'image', sub_type: 'jpeg'))
194
+ source_io = double('SourceIO', content_type: double(media_type: 'image', sub_type: 'jpeg'), response_status: nil)
195
195
  expect(chat).to receive(:fetch_source).and_yield(source_io)
196
196
  expect(chat).to receive(:add_image).with(chat.images, source_io, URI.parse(url))
197
197
 
@@ -222,7 +222,7 @@ describe OllamaChat::Tools::GetURL do
222
222
  end
223
223
 
224
224
  it 'handles audio content types via ASR transcription' do
225
- source_io = double('SourceIO', content_type: double(media_type: 'audio', to_s: 'audio/mp3'))
225
+ source_io = double('SourceIO', content_type: double(media_type: 'audio', to_s: 'audio/mp3'), response_status: nil)
226
226
  expect(chat).to receive(:fetch_source).and_yield(source_io)
227
227
  expect(chat).to receive(:parse_audio).with(source_io, language: 'de').and_return('transcribed text')
228
228
 
@@ -235,7 +235,7 @@ describe OllamaChat::Tools::GetURL do
235
235
  end
236
236
 
237
237
  it 'handles video content types via ASR transcription' do
238
- source_io = double('SourceIO', content_type: double(media_type: 'video', to_s: 'video/mp4'))
238
+ source_io = double('SourceIO', content_type: double(media_type: 'video', to_s: 'video/mp4'), response_status: nil)
239
239
  expect(chat).to receive(:fetch_source).and_yield(source_io)
240
240
  expect(chat).to receive(:parse_audio).with(source_io, language: nil).and_return('video text')
241
241
 
@@ -248,7 +248,7 @@ describe OllamaChat::Tools::GetURL do
248
248
  end
249
249
 
250
250
  it 'handles unsupported media types' do
251
- source_io = double('SourceIO', content_type: double(media_type: 'font'))
251
+ source_io = double('SourceIO', content_type: double(media_type: 'font'), response_status: nil)
252
252
  expect(chat).to receive(:fetch_source).and_yield(source_io)
253
253
 
254
254
  args = double(url:, document_policy: 'ignoring', words: nil, instruction: nil, language: nil)
@@ -258,5 +258,24 @@ describe OllamaChat::Tools::GetURL do
258
258
  json = json_object(result)
259
259
  expect(json.message).to match(/Cannot fetch.*with content type/)
260
260
  end
261
+
262
+ it 'short-circuits on non-200 HTTP status' do
263
+ source_io = double(
264
+ 'FailedSourceIO',
265
+ response_status: 403,
266
+ read: 'Access Denied',
267
+ )
268
+ expect(chat).to receive(:fetch_source).and_yield(source_io)
269
+ expect(chat).not_to receive(:summarize_source)
270
+ expect(chat).not_to receive(:import_source)
271
+
272
+ args = double(url:, document_policy: 'summarizing', words: nil, instruction: nil, language: nil)
273
+ tool_call = double(function: double(arguments: args))
274
+
275
+ result = described_class.new(chat).execute(tool_call)
276
+ json = json_object(result)
277
+ expect(json.content).to eq('Access Denied')
278
+ expect(json.message).to match(/HTTP status 403/)
279
+ end
261
280
  end
262
281
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ollama_chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.138
4
+ version: 0.0.140
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -253,14 +253,14 @@ dependencies:
253
253
  requirements:
254
254
  - - "~>"
255
255
  - !ruby/object:Gem::Version
256
- version: '0.7'
256
+ version: '0.9'
257
257
  type: :runtime
258
258
  prerelease: false
259
259
  version_requirements: !ruby/object:Gem::Requirement
260
260
  requirements:
261
261
  - - "~>"
262
262
  - !ruby/object:Gem::Version
263
- version: '0.7'
263
+ version: '0.9'
264
264
  - !ruby/object:Gem::Dependency
265
265
  name: complex_config
266
266
  requirement: !ruby/object:Gem::Requirement