summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoricebaker <icebaker@proton.me>2023-05-13 10:45:32 -0300
committericebaker <icebaker@proton.me>2023-05-13 10:45:32 -0300
commitd83ec5192442a9bd8d914c5a4856917b9653583c (patch)
treeca97e1618dd937594dc1da0a13584a46b87613eb
parent805e5073117bc717f46da0fa7f8ab628b4fa2a7d (diff)
improving streams
-rw-r--r--README.md1
-rw-r--r--controllers/instance.rb32
-rw-r--r--controllers/interfaces/cli.rb4
-rw-r--r--controllers/session.rb15
-rw-r--r--docker-compose.example.yml1
-rw-r--r--ports/dsl/nano-bots.rb2
-rw-r--r--static/cartridges/default.yml14
7 files changed, 57 insertions, 12 deletions
diff --git a/README.md b/README.md
index 5d0bb3b..0371b94 100644
--- a/README.md
+++ b/README.md
@@ -72,6 +72,7 @@ services:
OPENAI_API_USER_IDENTIFIER: your-user
volumes:
- ./your-cartridges:/cartridges
+ # - /home/user/data:/data
```
Enter the container:
diff --git a/controllers/instance.rb b/controllers/instance.rb
index f3cbd74..243e266 100644
--- a/controllers/instance.rb
+++ b/controllers/instance.rb
@@ -12,12 +12,14 @@ require_relative './session'
module NanoBot
module Controllers
class Instance
- def initialize(cartridge_path:, state: nil)
+ def initialize(cartridge_path:, stream:, state: nil)
+ @stream = stream
+
load_cartridge!(cartridge_path)
provider = Components::Provider.new(@cartridge[:provider])
- @session = Session.new(provider:, cartridge: @cartridge, state:)
+ @session = Session.new(provider:, cartridge: @cartridge, state:, stream: @stream)
end
def debug
@@ -26,18 +28,42 @@ module NanoBot
def eval(input)
Interfaces::Eval.evaluate(input, @cartridge, @session)
+
+ return unless @stream.is_a?(StringIO)
+
+ @stream.flush
+ result = @stream.string.clone
+ @stream.truncate(0)
+ @stream.rewind
+ result
end
def repl
+ if @stream.is_a?(StringIO)
+ @stream.flush
+ @stream = $stdout
+ @session.stream = @stream
+ end
Interfaces::REPL.start(@cartridge, @session)
end
private
def load_cartridge!(path)
+ elected_path = if path.strip == '-'
+ File.expand_path('../static/cartridges/default.yml', __dir__)
+ else
+ Components::Storage.cartridge_path(path)
+ end
+
+ if elected_path.nil?
+ @stream.write("Cartridge file not found: \"#{path}\"\n")
+ raise StandardError, "Cartridge file not found: \"#{path}\""
+ end
+
@cartridge = Logic::Helpers::Hash.symbolize_keys(
YAML.safe_load(
- File.read(Components::Storage.cartridge_path(path)),
+ File.read(elected_path),
permitted_classes: [Symbol]
)
)
diff --git a/controllers/interfaces/cli.rb b/controllers/interfaces/cli.rb
index 3574a80..92e468b 100644
--- a/controllers/interfaces/cli.rb
+++ b/controllers/interfaces/cli.rb
@@ -26,7 +26,9 @@ module NanoBot
params = { cartridge_path: ARGV[0], state: ARGV[1], command: ARGV[2] }
- bot = Instance.new(cartridge_path: params[:cartridge_path], state: params[:state])
+ bot = Instance.new(
+ cartridge_path: params[:cartridge_path], state: params[:state], stream: $stdout
+ )
case params[:command]
when 'eval'
diff --git a/controllers/session.rb b/controllers/session.rb
index ff3b8ad..ad640c8 100644
--- a/controllers/session.rb
+++ b/controllers/session.rb
@@ -12,12 +12,13 @@ module NanoBot
STREAM_TIMEOUT_IN_SECONDS = 5
class Session
- def initialize(provider:, cartridge:, state: nil)
+ attr_accessor :stream
+
+ def initialize(provider:, cartridge:, state: nil, stream: $stdout)
+ @stream = stream
@provider = provider
@cartridge = cartridge
- @output = $stdout
-
@stateless = state.nil? || state.strip == '-' || state.strip.empty?
if @stateless
@@ -67,7 +68,7 @@ module NanoBot
process(input, mode:)
end
- def stream(interface)
+ def streaming(interface)
provider = @provider.settings.key?(:stream) ? @provider.settings[:stream] : true
interface = interface.key?(:stream) ? interface[:stream] : true
@@ -77,7 +78,7 @@ module NanoBot
def process(input, mode:)
interface = Logic::Helpers::Hash.fetch(@cartridge, [:interfaces, mode.to_sym]) || {}
- streaming = stream(interface)
+ streaming = streaming(interface)
input[:interface] = interface
@@ -105,11 +106,11 @@ module NanoBot
end
def flush
- @output.flush
+ @stream.flush
end
def print(content)
- @output.write(content)
+ @stream.write(content)
end
end
end
diff --git a/docker-compose.example.yml b/docker-compose.example.yml
index 1ec49c1..1d98e49 100644
--- a/docker-compose.example.yml
+++ b/docker-compose.example.yml
@@ -10,3 +10,4 @@ services:
OPENAI_API_USER_IDENTIFIER: your-user
volumes:
- ./your-cartridges:/cartridges
+ # - /home/user/data:/data
diff --git a/ports/dsl/nano-bots.rb b/ports/dsl/nano-bots.rb
index bce169c..b6f4462 100644
--- a/ports/dsl/nano-bots.rb
+++ b/ports/dsl/nano-bots.rb
@@ -8,7 +8,7 @@ require_relative '../../controllers/interfaces/cli'
module NanoBot
def self.new(cartridge:, state: '-')
- Controllers::Instance.new(cartridge_path: cartridge, state:)
+ Controllers::Instance.new(cartridge_path: cartridge, state:, stream: StringIO.new)
end
def self.cli
diff --git a/static/cartridges/default.yml b/static/cartridges/default.yml
new file mode 100644
index 0000000..b5eed62
--- /dev/null
+++ b/static/cartridges/default.yml
@@ -0,0 +1,14 @@
+---
+meta:
+ name: Unknown
+ author: Nobody
+ version: 0.0.0
+
+provider:
+ name: openai
+ settings:
+ model: gpt-3.5-turbo
+ credentials:
+ address: ENV/OPENAI_API_ADDRESS
+ access-token: ENV/OPENAI_API_ACCESS_TOKEN
+ user-identifier: ENV/OPENAI_API_USER_IDENTIFIER