Create kilo_client.py
Browse files
SKILLS/kilo_agent/references/kilo_client.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import subprocess
|
| 5 |
+
import time
|
| 6 |
+
import re
|
| 7 |
+
from typing import Optional
|
| 8 |
+
|
| 9 |
+
class KiloLocalClient:
|
| 10 |
+
def __init__(self, base_url: str, password: Optional[str] = None, directory: Optional[str] = None):
|
| 11 |
+
self.base_url = base_url.rstrip('/')
|
| 12 |
+
self.session = requests.Session()
|
| 13 |
+
|
| 14 |
+
# Set up basic auth if password provided
|
| 15 |
+
if password:
|
| 16 |
+
self.session.auth = ('kilo', password)
|
| 17 |
+
|
| 18 |
+
# Set directory header if provided
|
| 19 |
+
if directory:
|
| 20 |
+
self.session.headers.update({'x-kilo-directory': directory})
|
| 21 |
+
|
| 22 |
+
def create_session(self, title: Optional[str] = None) -> dict:
|
| 23 |
+
"""Create a new session"""
|
| 24 |
+
data = {}
|
| 25 |
+
if title:
|
| 26 |
+
data['title'] = title
|
| 27 |
+
|
| 28 |
+
response = self.session.post(f"{self.base_url}/session", json=data)
|
| 29 |
+
response.raise_for_status()
|
| 30 |
+
return response.json()
|
| 31 |
+
|
| 32 |
+
def send_message(self, session_id: str, message: str) -> dict:
|
| 33 |
+
"""Send a message to a session"""
|
| 34 |
+
data = {
|
| 35 |
+
'parts': [{'type': 'text', 'text': message}]
|
| 36 |
+
}
|
| 37 |
+
response = self.session.post(
|
| 38 |
+
f"{self.base_url}/session/{session_id}/prompt",
|
| 39 |
+
json=data
|
| 40 |
+
)
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
return response.json()
|
| 43 |
+
|
| 44 |
+
def get_events(self, session_id: str):
|
| 45 |
+
"""Get event stream for a session"""
|
| 46 |
+
response = self.session.get(f"{self.base_url}/global/event", stream=True)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
|
| 49 |
+
for line in response.iter_lines():
|
| 50 |
+
if line:
|
| 51 |
+
line = line.decode('utf-8')
|
| 52 |
+
if line.startswith('data: '):
|
| 53 |
+
yield json.loads(line[6:])
|
| 54 |
+
|
| 55 |
+
def start_local_server() -> tuple[str, str]:
|
| 56 |
+
"""Start a local kilo server and return (url, password)"""
|
| 57 |
+
# Generate random password
|
| 58 |
+
password = os.urandom(16).hex()
|
| 59 |
+
|
| 60 |
+
# Start server in background
|
| 61 |
+
cmd = [
|
| 62 |
+
'bun', 'dev', 'serve', '--port', '0'
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
env = os.environ.copy()
|
| 66 |
+
env['KILO_SERVER_PASSWORD'] = password
|
| 67 |
+
|
| 68 |
+
with open('/tmp/kilo-serve.log', 'w') as f:
|
| 69 |
+
process = subprocess.Popen(
|
| 70 |
+
cmd, env=env, stdout=f, stderr=subprocess.STDOUT
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Wait for server to start
|
| 74 |
+
for _ in range(50): # Wait up to 5 seconds
|
| 75 |
+
time.sleep(0.1)
|
| 76 |
+
try:
|
| 77 |
+
with open('/tmp/kilo-serve.log', 'r') as f:
|
| 78 |
+
content = f.read()
|
| 79 |
+
match = re.search(r'listening on http://[^:]+:(\d+)', content)
|
| 80 |
+
if match:
|
| 81 |
+
port = match.group(1)
|
| 82 |
+
return f"http://127.0.0.1:{port}", password
|
| 83 |
+
except FileNotFoundError:
|
| 84 |
+
pass
|
| 85 |
+
|
| 86 |
+
raise RuntimeError("Server failed to start")
|
| 87 |
+
|
| 88 |
+
# Usage example
|
| 89 |
+
def main():
|
| 90 |
+
# Start server
|
| 91 |
+
server_url, password = start_local_server()
|
| 92 |
+
print(f"Server started at {server_url}")
|
| 93 |
+
|
| 94 |
+
# Create client
|
| 95 |
+
client = KiloLocalClient(
|
| 96 |
+
base_url=server_url,
|
| 97 |
+
password=password,
|
| 98 |
+
directory=os.getcwd()
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Create session
|
| 102 |
+
session = client.create_session(title="Python Test Session")
|
| 103 |
+
session_id = session['id']
|
| 104 |
+
print(f"Created session: {session_id}")
|
| 105 |
+
|
| 106 |
+
# Send message
|
| 107 |
+
response = client.send_message(
|
| 108 |
+
session_id=session_id,
|
| 109 |
+
message="List the files in the current directory"
|
| 110 |
+
)
|
| 111 |
+
print("Message sent, waiting for response...")
|
| 112 |
+
|
| 113 |
+
# Listen for events
|
| 114 |
+
for event in client.get_events(session_id):
|
| 115 |
+
if event.get('payload', {}).get('type') == 'session.message':
|
| 116 |
+
content = event['payload'].get('content', [])
|
| 117 |
+
for part in content:
|
| 118 |
+
if part.get('type') == 'text':
|
| 119 |
+
print(part.get('text', ''))
|
| 120 |
+
elif event.get('payload', {}).get('type') == 'session.idle':
|
| 121 |
+
break
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
main()
|