OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / chatbot / ask.sh

52 lines · bash ref:
#!/bin/bash
# Query the AtomSpace learning daemon
# Usage: ./ask.sh "what is keypair"
#        ./ask.sh /stats
#        ./ask.sh /layers
#        ./ask.sh              # interactive mode

SOCK="/tmp/atomspace-learner.sock"

if [ ! -S "$SOCK" ]; then
    echo "Daemon not running. Start with: ./start-learner.sh"
    exit 1
fi

send_query() {
    # Use python3 for unix socket if available, otherwise try socat, then nc
    if command -v python3 &>/dev/null; then
        python3 -c "
import socket, sys
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect('$SOCK')
s.sendall((sys.argv[1] + '\n').encode())
s.shutdown(socket.SHUT_WR)
data = b''
while True:
    chunk = s.recv(4096)
    if not chunk: break
    data += chunk
print(data.decode(), end='')
s.close()
" "$1"
    elif command -v socat &>/dev/null; then
        echo "$1" | socat - UNIX-CONNECT:"$SOCK"
    else
        echo "Need python3 or socat for socket communication"
        exit 1
    fi
}

if [ $# -eq 0 ]; then
    # Interactive mode
    echo "AtomSpace Learner (type /help for commands, Ctrl+D to quit)"
    echo ""
    while IFS= read -r -p "> " line; do
        send_query "$line"
        echo ""
    done
else
    # Single query
    send_query "$*"
fi