ollama_chat 0.0.20 → 0.0.22
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/CHANGES.md +16 -0
- data/README.md +8 -0
- data/VERSION +1 -1
- data/bin/ollama_chat_send +3 -2
- data/lib/ollama_chat/chat.rb +176 -8
- data/lib/ollama_chat/dialog.rb +8 -5
- data/lib/ollama_chat/follow_chat.rb +36 -0
- data/lib/ollama_chat/information.rb +1 -0
- data/lib/ollama_chat/ollama_chat_config/default_config.yml +1 -0
- data/lib/ollama_chat/server_socket.rb +36 -15
- data/lib/ollama_chat/source_fetching.rb +91 -1
- data/lib/ollama_chat/switches.rb +79 -16
- data/lib/ollama_chat/utils/cache_fetcher.rb +30 -0
- data/lib/ollama_chat/utils/fetcher.rb +117 -0
- data/lib/ollama_chat/version.rb +1 -1
- data/lib/ollama_chat/vim.rb +53 -0
- data/lib/ollama_chat/web_searching.rb +35 -1
- data/lib/ollama_chat.rb +1 -0
- data/ollama_chat.gemspec +6 -6
- data/spec/assets/api_tags.json +17 -0
- data/spec/ollama_chat/chat_spec.rb +1 -1
- data/spec/ollama_chat/server_socket_spec.rb +133 -0
- data/spec/ollama_chat/switches_spec.rb +9 -14
- data/spec/spec_helper.rb +2 -6
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bffe3cc2184b33a1dec71799752eeb90f9f89f1120644195c138d4b0f2c61f67
|
4
|
+
data.tar.gz: 0024f487568be42097d48d44bc75284bd3bf756035e1aff01492c6ee509d7754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14128bc440a4698b8ddda93d46f94ad43b756a028f4a11680703000d6b21410ff346d989112bbee3cb62af487382b398ab68e4c07a463b347d35d4faefaf22dd
|
7
|
+
data.tar.gz: 1a6b94ea5e570c50f3549f36f44a3cc3a8698837c7d1e66bc211d8139361d98d845807775e90162213453fe53d2bd20209383f2306cbf408a8c58b1a2ed02a93
|
data/CHANGES.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# Changes
|
2
2
|
|
3
|
+
## 2025-08-11 v0.0.21
|
4
|
+
|
5
|
+
* **Vim Integration**: The `/vim` command allows users to insert the last chat
|
6
|
+
message into a Vim server, improving workflow integration. It uses
|
7
|
+
`--servername` and `--remote-send` to insert text at the cursor position and
|
8
|
+
automatically indents based on the current column.
|
9
|
+
* **Improved Documentation**: Comprehensive documentation has been added to
|
10
|
+
various modules and classes, making it easier for developers to understand
|
11
|
+
and use the gem's features.
|
12
|
+
* **Model Selection Logic**: When only a single model is available, the code
|
13
|
+
now automatically selects that model instead of showing a prompt, improving
|
14
|
+
usability.
|
15
|
+
* **Configuration Handling**: Configuration file error handling has been
|
16
|
+
updated to use `STDERR` for output, ensuring errors are displayed
|
17
|
+
appropriately.
|
18
|
+
|
3
19
|
## 2025-08-11 v0.0.20
|
4
20
|
|
5
21
|
### Documentation
|
data/README.md
CHANGED
@@ -191,6 +191,14 @@ The `ollama_chat_send` command now supports additional parameters to enhance fun
|
|
191
191
|
$ echo "$response"
|
192
192
|
```
|
193
193
|
|
194
|
+
- **Source Parsing (`-p`)**: Enables automatic parsing of URLs, file paths, and
|
195
|
+
similar tokens in input content. When enabled, the system will attempt to
|
196
|
+
resolve and include external resources.
|
197
|
+
|
198
|
+
```bash
|
199
|
+
$ echo "Visit https://example.com for more info" | ollama_chat_send -p
|
200
|
+
```
|
201
|
+
|
194
202
|
- **Help (`-h` or `--help`)**: Displays usage information and available options.
|
195
203
|
|
196
204
|
```bash
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.22
|
data/bin/ollama_chat_send
CHANGED
@@ -5,7 +5,7 @@ require 'tins/go'
|
|
5
5
|
include Tins::GO
|
6
6
|
|
7
7
|
|
8
|
-
opts = go 'f:
|
8
|
+
opts = go 'f:rtph', ARGV
|
9
9
|
|
10
10
|
def usage(rc = 0)
|
11
11
|
puts <<~EOT
|
@@ -14,6 +14,7 @@ def usage(rc = 0)
|
|
14
14
|
Options:
|
15
15
|
-r Wait for the response from Ollama Chat and output it
|
16
16
|
-t Send input as terminal input including commands, e. g. /import
|
17
|
+
-p Send input with source parsing enabled (defaults to disabled)
|
17
18
|
-f CONFIG file to read
|
18
19
|
-h Show this help message
|
19
20
|
|
@@ -30,7 +31,7 @@ begin
|
|
30
31
|
else
|
31
32
|
opts[?r] ? :socket_input_with_response : :socket_input
|
32
33
|
end
|
33
|
-
response = OllamaChat::ServerSocket.send_to_server_socket(STDIN.read, type:, config:)
|
34
|
+
response = OllamaChat::ServerSocket.send_to_server_socket(STDIN.read, type:, config:, parse: !!opts[?p])
|
34
35
|
type == :socket_input_with_response and puts response.content
|
35
36
|
rescue => e
|
36
37
|
warn "Caught #{e.class}: #{e}"
|
data/lib/ollama_chat/chat.rb
CHANGED
@@ -34,6 +34,33 @@ class OllamaChat::Chat
|
|
34
34
|
include OllamaChat::History
|
35
35
|
include OllamaChat::ServerSocket
|
36
36
|
|
37
|
+
# Initializes a new OllamaChat::Chat instance with the given command-line
|
38
|
+
# arguments.
|
39
|
+
#
|
40
|
+
# Sets up the chat environment including configuration parsing, Ollama client
|
41
|
+
# initialization, model selection, system prompt handling, document
|
42
|
+
# processing setup, and history management. This method handles all the
|
43
|
+
# bootstrapping necessary to create a functional chat session that can
|
44
|
+
# communicate with an Ollama server and process various input types including
|
45
|
+
# text, documents, web content, and images.
|
46
|
+
#
|
47
|
+
# The initialization process includes parsing command-line options using
|
48
|
+
# Tins::GO for robust argument handling, setting up the Ollama client with
|
49
|
+
# configurable timeouts (connect, read, write), validating Ollama API version
|
50
|
+
# compatibility (requires >= 0.9.0 for features used), configuring model
|
51
|
+
# selection based on command-line or configuration defaults, initializing
|
52
|
+
# system prompts from files or inline content, setting up document processing
|
53
|
+
# pipeline with embedding capabilities through Documentrix::Documents,
|
54
|
+
# creating message history management through OllamaChat::MessageList,
|
55
|
+
# initializing cache systems for document embeddings, setting up
|
56
|
+
# voice support and image handling for multimodal interactions, enabling
|
57
|
+
# optional server socket functionality for remote input, and handling
|
58
|
+
# configuration errors with interactive recovery mechanisms.
|
59
|
+
#
|
60
|
+
# @param argv [Array<String>] Command-line arguments to parse (defaults to ARGV.dup)
|
61
|
+
#
|
62
|
+
# @raise [ArgumentError] If the Ollama API version is less than 0.9.0, indicating
|
63
|
+
# incompatibility with required API features
|
37
64
|
def initialize(argv: ARGV.dup)
|
38
65
|
@opts = go 'f:u:m:s:c:C:D:MESVh', argv
|
39
66
|
@opts[?h] and exit usage
|
@@ -80,28 +107,66 @@ class OllamaChat::Chat
|
|
80
107
|
fix_config(e)
|
81
108
|
end
|
82
109
|
|
110
|
+
# The ollama reader returns the Ollama API client instance.
|
111
|
+
#
|
112
|
+
# @return [Ollama::Client] the configured Ollama API client
|
83
113
|
attr_reader :ollama
|
84
114
|
|
115
|
+
|
116
|
+
# Returns the documents set for this object, initializing it lazily if needed.
|
117
|
+
#
|
118
|
+
# The documents set is memoized, meaning it will only be created once per
|
119
|
+
# object instance and subsequent calls will return the same
|
120
|
+
# Documentrix::Documents instance.
|
121
|
+
#
|
122
|
+
# @return [Documentrix::Documents] A Documentrix::Documents object containing
|
123
|
+
# all documents associated with this instance
|
85
124
|
attr_reader :documents
|
86
125
|
|
126
|
+
# Returns the messages set for this object, initializing it lazily if needed.
|
127
|
+
#
|
128
|
+
# The messages set is memoized, meaning it will only be created once per
|
129
|
+
# object instance and subsequent calls will return the same
|
130
|
+
# OllamaChat::MessageList instance.
|
131
|
+
#
|
132
|
+
# @return [OllamaChat::MessageList] A MessageList object containing all
|
133
|
+
# messages associated with this instance
|
87
134
|
attr_reader :messages
|
88
135
|
|
136
|
+
# Returns the links set for this object, initializing it lazily if needed.
|
137
|
+
#
|
138
|
+
# The links set is memoized, meaning it will only be created once per object
|
139
|
+
# instance and subsequent calls will return the same Set instance.
|
140
|
+
#
|
141
|
+
# @return [Set] A Set object containing all links associated with this instance
|
89
142
|
def links
|
90
143
|
@links ||= Set.new
|
91
144
|
end
|
92
145
|
|
93
146
|
class << self
|
147
|
+
# The config attribute accessor provides read and write access to the
|
148
|
+
# configuration object associated with this instance.
|
94
149
|
attr_accessor :config
|
95
150
|
end
|
96
151
|
|
152
|
+
# The config= method assigns a new configuration object to the class.
|
153
|
+
#
|
154
|
+
# @param config [ ComplexConfig::Settings ] the configuration object to be set
|
97
155
|
def config=(config)
|
98
156
|
self.class.config = config
|
99
157
|
end
|
100
158
|
|
159
|
+
# The config method returns the configuration object associated with the
|
160
|
+
# class.
|
161
|
+
#
|
162
|
+
# @return [ ComplexConfig::Settings ] the configuration instance
|
101
163
|
def config
|
102
164
|
self.class.config
|
103
165
|
end
|
104
166
|
|
167
|
+
# The start method initializes the chat session by displaying information and
|
168
|
+
# conversation history, then prompts the user for input to begin interacting
|
169
|
+
# with the chat.
|
105
170
|
def start
|
106
171
|
info
|
107
172
|
if messages.size > 1
|
@@ -152,7 +217,7 @@ class OllamaChat::Chat
|
|
152
217
|
:next
|
153
218
|
when %r(^/drop(?:\s+(\d*))?$)
|
154
219
|
messages.drop($1)
|
155
|
-
messages.
|
220
|
+
messages.show_last
|
156
221
|
:next
|
157
222
|
when %r(^/model$)
|
158
223
|
@model = choose_model('', @model)
|
@@ -235,7 +300,7 @@ class OllamaChat::Chat
|
|
235
300
|
if messages.save_conversation(filename)
|
236
301
|
STDOUT.puts "Saved conversation to #{filename.inspect}."
|
237
302
|
else
|
238
|
-
|
303
|
+
STDERR.puts "Saving conversation to #{filename.inspect} failed."
|
239
304
|
end
|
240
305
|
:next
|
241
306
|
when %r(^/links(?:\s+(clear))?$)
|
@@ -250,7 +315,7 @@ class OllamaChat::Chat
|
|
250
315
|
if success
|
251
316
|
STDOUT.puts "Loaded conversation from #{filename.inspect}."
|
252
317
|
else
|
253
|
-
|
318
|
+
STDERR.puts "Loading conversation from #{filename.inspect} failed."
|
254
319
|
end
|
255
320
|
:next
|
256
321
|
when %r(^/pipe\s+(.+)$)
|
@@ -259,6 +324,13 @@ class OllamaChat::Chat
|
|
259
324
|
when %r(^/output\s+(.+)$)
|
260
325
|
output($1)
|
261
326
|
:next
|
327
|
+
when %r(^/vim(?:\s+(.+))?$)
|
328
|
+
if message = messages.last
|
329
|
+
OllamaChat::Vim.new($1).insert message.content
|
330
|
+
else
|
331
|
+
STDERR.puts "Warning: No message found to insert into Vim"
|
332
|
+
end
|
333
|
+
:next
|
262
334
|
when %r(^/config$)
|
263
335
|
display_config
|
264
336
|
:next
|
@@ -274,6 +346,14 @@ class OllamaChat::Chat
|
|
274
346
|
end
|
275
347
|
end
|
276
348
|
|
349
|
+
# The web method searches for URLs based on a query and processes them by
|
350
|
+
# fetching, embedding, and summarizing their content, then formats the
|
351
|
+
# results into a prompt string.
|
352
|
+
#
|
353
|
+
# @param count [ String ] the number of URLs to search for
|
354
|
+
# @param query [ String ] the search query string
|
355
|
+
#
|
356
|
+
# @return [ String ] the formatted prompt string containing the query and summarized results
|
277
357
|
def web(count, query)
|
278
358
|
urls = search_web(query, count.to_i) or return :next
|
279
359
|
urls.each do |url|
|
@@ -285,6 +365,16 @@ class OllamaChat::Chat
|
|
285
365
|
config.prompts.web % { query:, results: }
|
286
366
|
end
|
287
367
|
|
368
|
+
# The manage_links method handles operations on a collection of links, such
|
369
|
+
# as displaying them or clearing specific entries.
|
370
|
+
#
|
371
|
+
# It supports two main commands: 'clear' and nil (default).
|
372
|
+
# When the command is 'clear', it presents an interactive menu to either
|
373
|
+
# clear all links or individual links.
|
374
|
+
# When the command is nil, it displays the current list of links with
|
375
|
+
# hyperlinks.
|
376
|
+
#
|
377
|
+
# @param command [ String, nil ] the operation to perform on the links
|
288
378
|
def manage_links(command)
|
289
379
|
case command
|
290
380
|
when 'clear'
|
@@ -322,10 +412,14 @@ class OllamaChat::Chat
|
|
322
412
|
end
|
323
413
|
end
|
324
414
|
|
415
|
+
# The clean method clears various parts of the chat session based on the
|
416
|
+
# specified parameter.
|
417
|
+
#
|
418
|
+
# @param what [ String, nil ] the type of data to clear, defaults to
|
419
|
+
# 'messages' if nil
|
325
420
|
def clean(what)
|
326
|
-
what = 'messages' if what.nil?
|
327
421
|
case what
|
328
|
-
when 'messages'
|
422
|
+
when 'messages', nil
|
329
423
|
messages.clear
|
330
424
|
STDOUT.puts "Cleared messages."
|
331
425
|
when 'links'
|
@@ -350,6 +444,11 @@ class OllamaChat::Chat
|
|
350
444
|
end
|
351
445
|
end
|
352
446
|
|
447
|
+
# The display_config method renders the configuration and displays it using a
|
448
|
+
# pager.
|
449
|
+
# It determines an appropriate pager command based on environment variables
|
450
|
+
# and available system commands, then uses Kramdown::ANSI::Pager to show the
|
451
|
+
# formatted configuration output.
|
353
452
|
def display_config
|
354
453
|
default_pager = ENV['PAGER'].full?
|
355
454
|
if fallback_pager = `which less`.chomp.full? || `which more`.chomp.full?
|
@@ -365,6 +464,18 @@ class OllamaChat::Chat
|
|
365
464
|
end
|
366
465
|
end
|
367
466
|
|
467
|
+
# The interact_with_user method manages the interactive loop for user input
|
468
|
+
# and chat processing.
|
469
|
+
# It handles reading user input, processing commands, managing messages, and
|
470
|
+
# communicating with the Ollama server.
|
471
|
+
# The method supports command completion, prefilling prompts, socket input
|
472
|
+
# handling, and various chat features including embedding context and voice
|
473
|
+
# support.
|
474
|
+
# It processes user input through command handling, content parsing, and
|
475
|
+
# message formatting before sending requests to the Ollama server.
|
476
|
+
# The method also handles server socket messages, manages chat history, and
|
477
|
+
# ensures proper cleanup and configuration handling throughout the
|
478
|
+
# interaction.
|
368
479
|
def interact_with_user
|
369
480
|
loop do
|
370
481
|
@parse_content = true
|
@@ -384,8 +495,9 @@ class OllamaChat::Chat
|
|
384
495
|
end
|
385
496
|
rescue Interrupt
|
386
497
|
if message = server_socket_message
|
387
|
-
type
|
388
|
-
content
|
498
|
+
type = message.type.full?(:to_sym) || :socket_input
|
499
|
+
content = message.content
|
500
|
+
@parse_content = message.parse
|
389
501
|
STDOUT.puts color(112) { "Received a server socket message. Processing now…" }
|
390
502
|
else
|
391
503
|
raise
|
@@ -479,6 +591,13 @@ class OllamaChat::Chat
|
|
479
591
|
|
480
592
|
private
|
481
593
|
|
594
|
+
# The setup_documents method initializes the document processing pipeline by
|
595
|
+
# configuring the embedding model and database connection.
|
596
|
+
# It then loads specified documents into the system and returns the
|
597
|
+
# configured document collection.
|
598
|
+
#
|
599
|
+
# @return [ Documentrix::Documents, NULL ] the initialized document
|
600
|
+
# collection if embedding is enabled, otherwise NULL
|
482
601
|
def setup_documents
|
483
602
|
if embedding.on?
|
484
603
|
@embedding_model = config.embedding.model.name
|
@@ -501,10 +620,35 @@ class OllamaChat::Chat
|
|
501
620
|
add_documents_from_argv(document_list)
|
502
621
|
@documents
|
503
622
|
else
|
504
|
-
|
623
|
+
NULL
|
505
624
|
end
|
506
625
|
end
|
507
626
|
|
627
|
+
# Adds documents from command line arguments to the document collection
|
628
|
+
#
|
629
|
+
# Processes a list of document paths or URLs, handling both local files and
|
630
|
+
# remote resources.
|
631
|
+
#
|
632
|
+
# @param document_list [Array<String>] List of document paths or URLs to process
|
633
|
+
#
|
634
|
+
# @return [void]
|
635
|
+
#
|
636
|
+
# @example Adding local files
|
637
|
+
# add_documents_from_argv(['/path/to/file1.txt', '/path/to/file2.pdf'])
|
638
|
+
#
|
639
|
+
# @example Adding remote URLs
|
640
|
+
# add_documents_from_argv(['https://example.com/page1', 'http://example.com/page2'])
|
641
|
+
#
|
642
|
+
# @example Mixed local and remote
|
643
|
+
# add_documents_from_argv(['/local/file.txt', 'https://remote.com/document'])
|
644
|
+
#
|
645
|
+
# @note Empty entries in the document list will trigger a collection clear operation
|
646
|
+
# @note Documents are processed in batches of 25 to manage memory usage
|
647
|
+
# @note Progress is reported to STDOUT during processing
|
648
|
+
#
|
649
|
+
# @see fetch_source
|
650
|
+
# @see embed_source
|
651
|
+
# @see documents.clear
|
508
652
|
def add_documents_from_argv(document_list)
|
509
653
|
if document_list.any?(&:empty?)
|
510
654
|
STDOUT.puts "Clearing collection #{bold{documents.collection}}."
|
@@ -532,6 +676,11 @@ class OllamaChat::Chat
|
|
532
676
|
end
|
533
677
|
end
|
534
678
|
|
679
|
+
# The setup_cache method initializes and returns a Redis cache instance with
|
680
|
+
# expiring keys if a Redis URL is configured.
|
681
|
+
#
|
682
|
+
# @return [ Documentrix::Documents::RedisCache, nil ] the configured Redis
|
683
|
+
# cache instance or nil if no URL is set.
|
535
684
|
def setup_cache
|
536
685
|
if url = config.redis.expiring.url?
|
537
686
|
ex = config.redis.expiring.ex?.to_i
|
@@ -543,6 +692,14 @@ class OllamaChat::Chat
|
|
543
692
|
end
|
544
693
|
end
|
545
694
|
|
695
|
+
# The fix_config method handles configuration file errors by informing the
|
696
|
+
# user about the exception and prompting them to fix it.
|
697
|
+
# It then executes a diff tool to compare the current config file with the
|
698
|
+
# default one.
|
699
|
+
# This method exits the program after handling the configuration error
|
700
|
+
#
|
701
|
+
# @param exception [ Exception ] the exception that occurred while reading
|
702
|
+
# the config file
|
546
703
|
def fix_config(exception)
|
547
704
|
STDOUT.puts "When reading the config file, a #{exception.class} "\
|
548
705
|
"exception was caught: #{exception.message.inspect}"
|
@@ -558,6 +715,17 @@ class OllamaChat::Chat
|
|
558
715
|
end
|
559
716
|
end
|
560
717
|
|
718
|
+
# Enables tab completion for chat commands within the interactive session
|
719
|
+
#
|
720
|
+
# Temporarily replaces the current Reline completion procedure with a custom
|
721
|
+
# one that provides command completion based on the chat help message.
|
722
|
+
#
|
723
|
+
# @param block [Proc] The block to execute with enhanced tab completion enabled
|
724
|
+
#
|
725
|
+
# @return [Object] The return value of the executed block
|
726
|
+
#
|
727
|
+
# @see display_chat_help_message
|
728
|
+
# @see Reline.completion_proc
|
561
729
|
def enable_command_completion(&block)
|
562
730
|
old = Reline.completion_proc
|
563
731
|
commands = display_chat_help_message.scan(/^\s*(\S+)/).inject(&:concat)
|
data/lib/ollama_chat/dialog.rb
CHANGED
@@ -32,11 +32,14 @@ module OllamaChat::Dialog
|
|
32
32
|
end
|
33
33
|
models = ollama.tags.models.sort_by(&:name).map { |m| model_with_size(m) }
|
34
34
|
selector and models = models.grep(selector)
|
35
|
-
model =
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
model =
|
36
|
+
if models.size == 1
|
37
|
+
models.first
|
38
|
+
elsif cli_model == ''
|
39
|
+
OllamaChat::Utils::Chooser.choose(models) || current_model
|
40
|
+
else
|
41
|
+
cli_model || current_model
|
42
|
+
end
|
40
43
|
ensure
|
41
44
|
STDOUT.puts green { "Connecting to #{model}@#{ollama.base_url} now…" }
|
42
45
|
end
|
@@ -60,6 +60,13 @@ class OllamaChat::FollowChat
|
|
60
60
|
|
61
61
|
private
|
62
62
|
|
63
|
+
# The ensure_assistant_response_exists method ensures that the last message
|
64
|
+
# in the conversation is from the assistant role.
|
65
|
+
#
|
66
|
+
# If the last message is not from an assistant, it adds a new assistant
|
67
|
+
# message with empty content and optionally includes thinking content if the
|
68
|
+
# chat's think mode is enabled. It also updates the user display variable to
|
69
|
+
# reflect the assistant's message type and styling.
|
63
70
|
def ensure_assistant_response_exists
|
64
71
|
if @messages&.last&.role != 'assistant'
|
65
72
|
@messages << Message.new(
|
@@ -72,6 +79,12 @@ class OllamaChat::FollowChat
|
|
72
79
|
end
|
73
80
|
end
|
74
81
|
|
82
|
+
# The update_last_message method appends the content of a response to the
|
83
|
+
# last message in the conversation. It also appends thinking content to the
|
84
|
+
# last message if thinking is enabled and thinking content is present.
|
85
|
+
#
|
86
|
+
# @param response [ Object ] the response object containing message content
|
87
|
+
# and thinking
|
75
88
|
def update_last_message(response)
|
76
89
|
@messages.last.content << response.message&.content
|
77
90
|
if @chat.think.on? and response_thinking = response.message&.thinking.full?
|
@@ -79,6 +92,13 @@ class OllamaChat::FollowChat
|
|
79
92
|
end
|
80
93
|
end
|
81
94
|
|
95
|
+
# The display_formatted_terminal_output method formats and outputs the
|
96
|
+
# terminal content by processing the last message's content and thinking,
|
97
|
+
# then prints it to the output. It handles markdown parsing and annotation
|
98
|
+
# based on chat settings, and ensures proper formatting with clear screen and
|
99
|
+
# move home commands. The method takes into account whether markdown and
|
100
|
+
# thinking modes are enabled to determine how to process and display the
|
101
|
+
# content.
|
82
102
|
def display_formatted_terminal_output
|
83
103
|
content, thinking = @messages.last.content, @messages.last.thinking
|
84
104
|
if @chat.markdown.on?
|
@@ -95,6 +115,14 @@ class OllamaChat::FollowChat
|
|
95
115
|
].compact))
|
96
116
|
end
|
97
117
|
|
118
|
+
# The eval_stats method processes response statistics and formats them into a
|
119
|
+
# colored, readable string output.
|
120
|
+
#
|
121
|
+
# @param response [ Object ] the response object containing evaluation metrics
|
122
|
+
#
|
123
|
+
# @return [ String ] a formatted string with statistical information about
|
124
|
+
# the evaluation process including durations, counts, and rates, styled with
|
125
|
+
# colors and formatting
|
98
126
|
def eval_stats(response)
|
99
127
|
eval_duration = response.eval_duration / 1e9
|
100
128
|
prompt_eval_duration = response.prompt_eval_duration / 1e9
|
@@ -113,11 +141,19 @@ class OllamaChat::FollowChat
|
|
113
141
|
}
|
114
142
|
end
|
115
143
|
|
144
|
+
# The output_eval_stats method outputs evaluation statistics to the specified
|
145
|
+
# output stream.
|
146
|
+
#
|
147
|
+
# @param response [ Object ] the response object containing evaluation data
|
116
148
|
def output_eval_stats(response)
|
117
149
|
response.done or return
|
118
150
|
@output.puts "", eval_stats(response)
|
119
151
|
end
|
120
152
|
|
153
|
+
# The debug_output method conditionally outputs the response object using jj
|
154
|
+
# when debugging is enabled.
|
155
|
+
#
|
156
|
+
# @param response [ Object ] the response object to be outputted
|
121
157
|
def debug_output(response)
|
122
158
|
OllamaChat::Chat.config.debug and jj response
|
123
159
|
end
|
@@ -116,6 +116,7 @@ module OllamaChat::Information
|
|
116
116
|
/load filename load conversation messages
|
117
117
|
/output filename save last response to filename
|
118
118
|
/pipe command write last response to command's stdin
|
119
|
+
/vim insert the last message into a vim server
|
119
120
|
/quit to quit
|
120
121
|
/help to view this help
|
121
122
|
EOT
|
@@ -1,24 +1,48 @@
|
|
1
1
|
module OllamaChat::ServerSocket
|
2
2
|
class << self
|
3
|
-
# The send_to_server_socket method
|
4
|
-
#
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
3
|
+
# The send_to_server_socket method transmits a message to a Unix domain
|
4
|
+
# socket server for processing by the Ollama Chat client.
|
5
|
+
#
|
6
|
+
# This method creates a socket server instance using the provided
|
7
|
+
# configuration, prepares a message with the given content, type, and parse
|
8
|
+
# flag, then sends it either as a simple transmission or with a response
|
9
|
+
# expectation depending on the message type. It is used to enable
|
10
|
+
# communication between external processes and the chat session via a named
|
11
|
+
# Unix socket.
|
8
12
|
#
|
9
|
-
# @
|
10
|
-
#
|
11
|
-
|
13
|
+
# @param content [ String ] the message content to be sent
|
14
|
+
# @param config [ ComplexConfig::Settings ] the configuration object containing server settings
|
15
|
+
# @param type [ Symbol ] the type of message transmission, defaults to :socket_input
|
16
|
+
# @param parse [ TrueClass, FalseClass ] whether to parse the response, defaults to false
|
17
|
+
#
|
18
|
+
# @return [ UnixSocks::Message, nil ] the response from transmit_with_response if type
|
19
|
+
# is :socket_input_with_response, otherwise nil
|
20
|
+
def send_to_server_socket(content, config:, type: :socket_input, parse: false)
|
12
21
|
server = create_socket_server(config:)
|
13
|
-
message = { content:, type: }
|
22
|
+
message = { content:, type:, parse: }
|
14
23
|
if type.to_sym == :socket_input_with_response
|
15
|
-
|
24
|
+
server.transmit_with_response(message)
|
16
25
|
else
|
17
|
-
|
18
|
-
|
26
|
+
server.transmit(message)
|
27
|
+
nil
|
19
28
|
end
|
20
29
|
end
|
21
30
|
|
31
|
+
# The create_socket_server method constructs and returns a Unix domain
|
32
|
+
# socket server instance for communication with the Ollama Chat client.
|
33
|
+
#
|
34
|
+
# This method initializes a UnixSocks::Server object configured to listen
|
35
|
+
# for incoming messages on a named socket file. It supports specifying a
|
36
|
+
# custom runtime directory for the socket, which is useful for isolating
|
37
|
+
# multiple instances or environments. If no runtime directory is provided
|
38
|
+
# in the configuration, it defaults to using the standard system location
|
39
|
+
# for Unix domain sockets.
|
40
|
+
#
|
41
|
+
# @param config [ComplexConfig::Settings] the configuration object
|
42
|
+
# containing server settings
|
43
|
+
#
|
44
|
+
# @return [UnixSocks::Server] a configured Unix domain socket server
|
45
|
+
# instance ready to receive messages
|
22
46
|
def create_socket_server(config:)
|
23
47
|
if runtime_dir = config.server_socket_runtime_dir
|
24
48
|
UnixSocks::Server.new(socket_name: 'ollama_chat.sock', runtime_dir:)
|
@@ -37,9 +61,6 @@ module OllamaChat::ServerSocket
|
|
37
61
|
# messages in the background. When a message is received, it updates the
|
38
62
|
# instance variable `server_socket_message` and sends an interrupt signal
|
39
63
|
# to the current process in order to handle the message.
|
40
|
-
#
|
41
|
-
# @return [ nil ] This method does not return any value, it only sets up the
|
42
|
-
# server socket and kills the process when a message is received.
|
43
64
|
def init_server_socket
|
44
65
|
server = OllamaChat::ServerSocket.create_socket_server(config:)
|
45
66
|
server.receive_in_background do |message|
|