Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yohasebe committed Sep 16, 2024
1 parent 2af40a1 commit 6b04223
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin/monadic_dev
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [ "$1" == "start" ]; then
echo "Monadic script executed with 'start' argument 🚀"
echo "Run 'monadic_dev stop' to stop the server"
elif [ "$1" == "debug" ]; then
./bin/monadic_dev start --log
./bin/monadic_dev start
echo "Monadic script executed with 'debug' argument 🛑"
elif [ "$1" == "stop" ]; then
./bin/monadic_dev stop
Expand Down
6 changes: 6 additions & 0 deletions docker/services/ruby/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ RUN chmod +x $WORKSPACE/scripts/*
ENV PATH="$WORKSPACE/scripts:/usr/local/bundle/bin:${PATH}"

COPY Dockerfile $WORKSPACE/Dockerfile

# Copy the entrypoint script into the container
COPY entrypoint.sh /usr/local/bin/entrypoint.sh

# Give execute permission to the script
RUN chmod +x /usr/local/bin/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class ContentReader < MonadicApp
initiate_from_assistant: true,
mathjax: true,
image: true,
audio_video: true,
tools: [
{
type: "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class LanguagePractice < MonadicApp
description: description,
icon: icon,
initiate_from_assistant: true,
speech_rate: 1.0,
image: true,
pdf: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class LanguagePracticePlus < MonadicApp
icon: icon,
initiate_from_assistant: true,
image: true,
pdf: false,
monadic: true,
response_format: {
type: "json_schema",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class SpeechDraftHelper < MonadicApp
icon: icon,
initiate_from_assistant: true,
image: true,
audio_video: true,
tools: [
{
type: "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class ChatWithMistral < MonadicApp
initiate_from_assistant: false,
pdf: false,
image: false,
toggle: false,
models: MistralHelper.list_models,
model: "mistral-large-latest"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ class CodeWithMistral < MonadicApp
initiate_from_assistant: false,
pdf: false,
image: false,
toggle: false,
models: ["mistral-large-latest"],
model: "mistral-large-latest",
tools: [
Expand Down
21 changes: 17 additions & 4 deletions docker/services/ruby/bin/monadic_dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "optimist"
require_relative "../lib/monadic/version"

# change current directory to the parent directory of the directory containing this file
# Change current directory to the parent directory of the directory containing this file
Dir.chdir(File.expand_path(File.join(__dir__, "..")))

DEFAULT_PORT = 4567
Expand Down Expand Up @@ -66,11 +66,24 @@ def start_server(opts)
else
cmd = "thin start -R #{DOCKER_HOME}/ruby/config.ru -p #{opts[:port] || DEFAULT_PORT} --pid #{PID_FILE}"
cmd += " --daemonize" if opts[:daemonize]
cmd += " --log thin.log" if opts[:log]
if system(cmd)

# Capture the command output
if opts[:daemonize]
output = `#{cmd} 2>&1`
else
output = `#{cmd}`
end
success = $?.success?

if success
puts "Server started on port #{opts[:port] || DEFAULT_PORT}"
else
puts "Server failed to start"
# Save detailed error log to the specified directory on the host
File.open(File.join(DATA_DIR, "error.log"), "a") do |file|
file.puts("Failed to start server at #{Time.now}")
file.puts(output) # Write the detailed error output
end
end
end
end
Expand Down Expand Up @@ -128,7 +141,7 @@ def export_document_db
pg_dump monadic | gzip > "#{DATA_DIR}/monadic.gz"
SHELL

# run command and get the result with error reporting
# Run command and get the result with error reporting
res = system(command)

if res
Expand Down
3 changes: 1 addition & 2 deletions docker/services/ruby/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
restart: always
stdin_open: true
tty: true
command: ["thin", "start", "-R", "config.ru", "-p", "4567", "-e", "development"]
command: ["sh", "/usr/local/bin/entrypoint.sh"]
networks:
- monadic-chat-network
healthcheck:
Expand All @@ -28,4 +28,3 @@ services:
depends_on:
pgvector_service:
condition: service_healthy

12 changes: 12 additions & 0 deletions docker/services/ruby/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# Attempt to start the thin server and capture any errors
thin start -R config.ru -p 4567 -e development > /monadic/data/error.log 2>&1

# Check if the thin server started successfully
if [ $? -ne 0 ]; then
echo "Failed to start thin server at $(date)" >> /monadic/data/error.log
fi

# Keep the container running
tail -f /dev/null
17 changes: 17 additions & 0 deletions docker/services/ruby/lib/monadic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ def init_apps
app = a.new
app.settings = ActiveSupport::HashWithIndifferentAccess.new(a.instance_variable_get(:@settings))

app.settings["description"] ||= ""
if !app.settings["initial_prompt"]
app.settings["initial_prompt"] = "You are an AI assistant but the initial prompt is missing. Tell the user they should provide a prompt."
app.settings["description"] << "<p><i class='fa-solid fa-triangle-exclamation'></i> The initial prompt is missing.</p>"
end
if !app.settings["description"]
app.settings["description"] << "<p><i class='fa-solid fa-triangle-exclamation'></i> The description is missing.</p>"
end
if !app.settings["icon"]
app.settings["icon"] = "<i class='fa-solid fa-circle-question'></i>"
app.settings["description"] << "<p><i class='fa-solid fa-triangle-exclamation'></i> The icon is missing.</p>"
end
if !app.settings["app_name"]
app.settings["app_name"] = "User App (#{SecureRandom.hex(4)})"
app.settings["description"] << "<p><i class='fa-solid fa-triangle-exclamation'></i> The app name is missing.</p>"
end

next if app.settings["disabled"]

app_name = app.settings["app_name"]
Expand Down
6 changes: 4 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function checkForUpdates() {
type: 'info',
buttons: ['OK'],
title: 'Up to Date',
message: `You are already using the latest version (${latestVersion}) of the app.`,
message: `You are already using the latest version of the app.`,
icon: path.join(iconDir, 'monadic-chat.png')
});
}
Expand Down Expand Up @@ -652,7 +652,7 @@ function toUnixPath(p) {
}

// Fetch a URL with retries and a delay between attempts
function fetchWithRetry(url, options = {}, retries = 60, delay = 1000, timeout = 20000) {
function fetchWithRetry(url, options = {}, retries = 30, delay = 1000, timeout = 20000) {
const attemptFetch = async (attempt) => {
try {
const controller = new AbortController();
Expand All @@ -672,6 +672,8 @@ function fetchWithRetry(url, options = {}, retries = 60, delay = 1000, timeout =
console.log(`Retrying in ${delay}ms . . .`);
await new Promise(resolve => setTimeout(resolve, delay));
return attemptFetch(attempt + 1);
} else {
console.log(`Failed to connect to server after ${retries} attempts. Please check the error log in the shared folder.`);
}
throw error;
}
Expand Down

0 comments on commit 6b04223

Please sign in to comment.