elelem-ollama 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ed18986dc0288ed968cbfacfec76a970d8ff096df3ebc3ca8e7f0d72c9128c5e
4
+ data.tar.gz: ba32c8f12ae8c4bda87cab6de6c7aa09d390ac38608d1e45a4c5624ddf18bca9
5
+ SHA512:
6
+ metadata.gz: 7ebb2bdd4cd7cc9eb396f60007400c976976e8b0622f053395e852c25023906003ae6d815678998d7d994b59daea68653aa75fed47784b2b1b13aeaf372a48c7
7
+ data.tar.gz: f9e679d352b8bc01268e7cee6c09af48b6974db4781b650ba369467a4a974b1903081273730551658fb697157d70c873b223140e10edd696ea9a7cd64ece0e5a
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 mo khan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/exe/elelem-ollama ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "elelem/cli"
5
+
6
+ Signal.trap("INT") { exit 1 }
7
+
8
+ Elelem::CLI.new(ARGV, provider: "ollama").run
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module Ollama
5
+ class Client
6
+ def initialize(
7
+ model:,
8
+ host: "localhost:11434",
9
+ think: false,
10
+ keep_alive: "5m",
11
+ options: {},
12
+ params: {},
13
+ read_timeout: 3600,
14
+ open_timeout: 10
15
+ )
16
+ @uri = normalize_uri(host)
17
+ @model = model
18
+ @think = think
19
+ @keep_alive = keep_alive
20
+ @options = options
21
+ @params = params
22
+ @read_timeout = read_timeout
23
+ @open_timeout = open_timeout
24
+ end
25
+
26
+ def fetch(messages, tools = [], &block)
27
+ tool_calls = []
28
+ body = build_request_body(messages, tools)
29
+
30
+ stream(body) do |event|
31
+ handle_event(event, tool_calls, &block)
32
+ end
33
+
34
+ tool_calls
35
+ end
36
+
37
+ private
38
+
39
+ def normalize_uri(host)
40
+ base = host.start_with?("http") ? host : "http://#{host}"
41
+ URI.join(base, "/api/chat")
42
+ end
43
+
44
+ def build_request_body(messages, tools)
45
+ {
46
+ model: @model,
47
+ messages: normalize(messages),
48
+ stream: true,
49
+ tools: presence(tools),
50
+ think: @think,
51
+ keep_alive: @keep_alive,
52
+ options: presence(@options)
53
+ }.merge(@params).compact
54
+ end
55
+
56
+ def presence(value)
57
+ value unless value.nil? || value.empty?
58
+ end
59
+
60
+ def normalize(messages)
61
+ pending = []
62
+
63
+ messages.map do |message|
64
+ case message[:role]
65
+ when "assistant" then normalize_calls(message, pending)
66
+ when "tool" then normalize_result(message, pending)
67
+ else message
68
+ end
69
+ end
70
+ end
71
+
72
+ def normalize_calls(message, pending)
73
+ calls = message[:tool_calls]
74
+ return message unless calls
75
+
76
+ pending.clear
77
+ message.merge(tool_calls: calls.map do |call|
78
+ pending << call[:name]
79
+ { function: { name: call[:name], arguments: call[:arguments] } }
80
+ end)
81
+ end
82
+
83
+ def normalize_result(message, pending)
84
+ name = pending.shift
85
+ return message unless name
86
+
87
+ message.except(:tool_call_id).merge(tool_name: name)
88
+ end
89
+
90
+ def handle_event(event, tool_calls, &block)
91
+ message = event["message"] || {}
92
+
93
+ unless event["done"]
94
+ block.call(type: "saying", text: message["content"]) if message["content"]
95
+ block.call(type: "thinking", text: message["thinking"]) if message["thinking"]
96
+ end
97
+
98
+ if message["tool_calls"]
99
+ parsed = parse_tool_calls(message["tool_calls"])
100
+ parsed.each { |tc| block.call(tc.merge(type: "doing")) }
101
+ tool_calls.concat(parsed)
102
+ end
103
+ end
104
+
105
+ def stream(body)
106
+ request = Net::HTTP::Post.new(@uri)
107
+ request["content-type"] = "application/json"
108
+ request.body = JSON.generate(body)
109
+
110
+ http = Net::HTTP.new(@uri.host, @uri.port)
111
+ http.use_ssl = @uri.scheme == "https"
112
+ http.read_timeout = @read_timeout
113
+ http.open_timeout = @open_timeout
114
+
115
+ http.start do |conn|
116
+ conn.request(request) do |response|
117
+ raise "HTTP #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
118
+
119
+ read_ndjson_stream(response) { |event| yield event }
120
+ end
121
+ end
122
+ end
123
+
124
+ def read_ndjson_stream(response)
125
+ buffer = String.new
126
+
127
+ response.read_body do |chunk|
128
+ buffer << chunk
129
+
130
+ while (index = buffer.index("\n"))
131
+ line = buffer.slice!(0, index + 1)
132
+ next if line.strip.empty?
133
+
134
+ yield JSON.parse(line)
135
+ end
136
+ end
137
+ end
138
+
139
+ def parse_tool_calls(tool_calls)
140
+ tool_calls.map do |tool_call|
141
+ {
142
+ id: tool_call["id"],
143
+ name: tool_call.dig("function", "name"),
144
+ arguments: tool_call.dig("function", "arguments") || {}
145
+ }
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module Ollama
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elelem"
4
+ require "json"
5
+ require "net/http"
6
+ require "uri"
7
+
8
+ require_relative "ollama/version"
9
+ require_relative "ollama/client"
10
+
11
+ Elelem::Providers.register(:ollama) do
12
+ Elelem::Ollama::Client.new(
13
+ model: ENV.fetch("OLLAMA_MODEL", "gpt-oss:latest"),
14
+ host: ENV.fetch("OLLAMA_HOST", "localhost:11434")
15
+ )
16
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elelem-ollama
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mo khan
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: elelem
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.10'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.10'
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.21'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.21'
40
+ - !ruby/object:Gem::Dependency
41
+ name: net-http
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ - !ruby/object:Gem::Dependency
55
+ name: uri
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.1'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.1'
68
+ description: An Ollama provider plugin for elelem.
69
+ email:
70
+ - mo@mokhan.ca
71
+ executables:
72
+ - elelem-ollama
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE.txt
77
+ - Rakefile
78
+ - exe/elelem-ollama
79
+ - lib/elelem/ollama.rb
80
+ - lib/elelem/ollama/client.rb
81
+ - lib/elelem/ollama/version.rb
82
+ homepage: https://src.mokhan.ca/elelem/ollama
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ allowed_push_host: https://rubygems.org
87
+ homepage_uri: https://src.mokhan.ca/elelem/ollama
88
+ source_code_uri: https://src.mokhan.ca/elelem/ollama
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.20
104
+ specification_version: 4
105
+ summary: An Ollama provider plugin for elelem.
106
+ test_files: []