Skip to content

Commit

Permalink
Refactored the app to client and relay inside wcgw
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman Rusia committed Oct 28, 2024
1 parent 50ca68e commit 04fecc5
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 52 deletions.
131 changes: 98 additions & 33 deletions gpt_action_json_schema.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@

{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"servers": [
{"url": "https://wcgw.arcfu.com"}
{
"url": "https://604f-2401-4900-1c8e-e61d-40fc-eab7-f5c2-c8ad.ngrok-free.app"
}
],
"paths": {
"/write_file": {
"/v1/write_file": {
"post": {
"x-openai-isConsequential": false,
"summary": "Write File",
"operationId": "write_file_write_file__uuid__post",
"operationId": "write_file_v1_write_file_post",
"parameters": [
{
"name": "user_id",
Expand Down Expand Up @@ -43,7 +44,7 @@
"application/json": {
"schema": {
"type": "string",
"title": "Response Write File Write File Uuid Post"
"title": "Response Write File V1 Write File Post"
}
}
}
Expand All @@ -61,12 +62,21 @@
}
}
},
"/execute_bash": {
"/v1/bash_command": {
"post": {
"x-openai-isConsequential": false,
"summary": "Execute Bash",
"operationId": "execute_bash_execute_bash__uuid__post",
"summary": "Bash Command",
"operationId": "bash_command_v1_bash_command_post",
"parameters": [
{
"name": "command",
"in": "query",
"required": true,
"schema": {
"type": "string",
"title": "Command"
}
},
{
"name": "user_id",
"in": "query",
Expand All @@ -78,12 +88,69 @@
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"type": "string",
"title": "Response Bash Command V1 Bash Command Post"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/v1/bash_interaction": {
"post": {
"x-openai-isConsequential": false,
"summary": "Bash Interaction",
"operationId": "bash_interaction_v1_bash_interaction_post",
"parameters": [
{
"name": "user_id",
"in": "query",
"required": true,
"schema": {
"type": "string",
"format": "uuid",
"title": "User Id"
}
},
{
"name": "send_text",
"in": "query",
"required": false,
"schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Send Text"
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ExecuteBash"
"$ref": "#/components/schemas/Body_bash_interaction_v1_bash_interaction_post"
}
}
}
Expand All @@ -95,7 +162,7 @@
"application/json": {
"schema": {
"type": "string",
"title": "Response Execute Bash Execute Bash Uuid Post"
"title": "Response Bash Interaction V1 Bash Interaction Post"
}
}
}
Expand All @@ -116,39 +183,37 @@
},
"components": {
"schemas": {
"ExecuteBash": {
"Body_bash_interaction_v1_bash_interaction_post": {
"properties": {
"execute_command": {
"send_specials": {
"anyOf": [
{
"type": "string"
"items": {
"type": "string",
"enum": [
"Key-up",
"Key-down",
"Key-left",
"Key-right",
"Enter",
"Ctrl-c",
"Ctrl-d",
"Ctrl-z"
]
},
"type": "array"
},
{
"type": "null"
}
],
"title": "Execute Command"
"title": "Send Specials"
},
"send_ascii": {
"anyOf": [
{
"items": {
"anyOf": [
{
"type": "integer"
},
{
"type": "string",
"enum": [
"Key-up",
"Key-down",
"Key-left",
"Key-right",
"Enter",
"Ctrl-c"
]
}
]
"type": "integer"
},
"type": "array"
},
Expand All @@ -160,7 +225,7 @@
}
},
"type": "object",
"title": "ExecuteBash"
"title": "Body_bash_interaction_v1_bash_interaction_post"
},
"HTTPValidationError": {
"properties": {
Expand Down Expand Up @@ -228,4 +293,4 @@
}
}
}
}
}
5 changes: 5 additions & 0 deletions gpt_instructions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ Instructions for `BashCommand`:

Instructions for `Write File`
- Write content to a file. Provide file path and content. Use this instead of BashCommand for writing files.
- Important: all relative paths are relative to last CWD.

Instructions for `BashInteraction`
- Interact with running program using this tool
- Special keys like arrows, interrupts, enter, etc.
- Send text input to the running program.

---

Always critically think and debate with yourself to solve the problem. Understand the context and the code by reading as much resources as possible before writing a single piece of code.
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ requires = ["hatchling"]
build-backend = "hatchling.build"

[project.scripts]
wcgw_local = "wcgw:local_app"
wcgw = "wcgw:wcgw_tools_app"
wcgw_local = "wcgw:app"
wcgw = "wcgw:listen"
wcgw_relay = "wcgw.relay.serve:run"

[tool.uv]
dev-dependencies = [
"mypy>=1.11.2",
"types-toml>=0.10.8.20240310",
"autoflake",
"ipython>=8.12.3",
"gunicorn>=23.0.0",
]
4 changes: 2 additions & 2 deletions src/wcgw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .basic import app, loop
from .tools import run as listen
from .client.basic import app, loop
from .client.tools import run as listen
3 changes: 0 additions & 3 deletions src/wcgw/__main__.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/wcgw/client/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .tools import run

run()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 11 additions & 5 deletions src/wcgw/tools.py → src/wcgw/client/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def execute_bash(

SHELL.sendline(command)
elif bash_arg.send_specials:
console.print(f"Sending ASCII sequence: {bash_arg.send_specials}")
console.print(f"Sending special sequence: {bash_arg.send_specials}")
for char in bash_arg.send_specials:
if char == "Key-up":
SHELL.send("\033[A")
Expand All @@ -251,6 +251,7 @@ def execute_bash(
else:
raise Exception(f"Unknown special character: {char}")
elif bash_arg.send_ascii:
console.print(f"Sending ASCII sequence: {bash_arg.send_ascii}")
for ascii_char in bash_arg.send_ascii:
SHELL.send(chr(ascii_char))
if ascii_char == 3:
Expand All @@ -273,7 +274,7 @@ def execute_bash(
response,
0,
)

console.print(f"Interact text: {bash_arg.send_text}")
SHELL.sendline(bash_arg.send_text)

BASH_STATE = "repl"
Expand Down Expand Up @@ -395,13 +396,17 @@ def read_image_from_shell(file_path: str) -> ImageData:


def write_file(writefile: Writefile) -> str:
if not os.path.isabs(writefile.file_path):
path_ = os.path.join(CWD, writefile.file_path)
else:
path_ = writefile.file_path
try:
with open(writefile.file_path, "w") as f:
with open(path_, "w") as f:
f.write(writefile.file_content)
except OSError as e:
console.print(f"Error: {e}", style="red")
return f"Error: {e}"
console.print(f"File written to {writefile.file_path}")
console.print(f"File written to {path_}")
return "Success"


Expand Down Expand Up @@ -583,13 +588,14 @@ async def register_client(server_url: str, client_uuid: str = "") -> None:

@run.command()
def app(
server_url: str = "wss://wcgw.arcfu.com/register",
server_url: str = "wss://wcgw.arcfu.com/v1/register",
client_uuid: Optional[str] = None,
version: bool = typer.Option(False, "--version", "-v"),
) -> None:
if version:
version_ = importlib.metadata.version("wcgw")
print(f"wcgw version: {version_}")
exit()

thread1 = threading.Thread(target=execute_user_input)
thread2 = threading.Thread(
Expand Down
10 changes: 4 additions & 6 deletions src/relay/serve.py → src/wcgw/relay/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ async def get_image(uuid: UUID, name: str) -> fastapi.responses.Response:
@app.websocket("/register/{uuid}")
async def register_websocket_deprecated(websocket: WebSocket, uuid: UUID) -> None:
await websocket.accept()

await websocket.receive_text()

await websocket.send_text(
"This version of the API is deprecated. Please upgrade your client."
"Outdated client used. Deprecated api is being used. Upgrade the wcgw app."
)
await websocket.close(
reason="This endpoint is deprecated. Please use /v1/register/{uuid}", code=1002
)

raise fastapi.HTTPException(status_code=400, detail="Deprecated API version")


@app.websocket("/v1/register/{uuid}")
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions src/wcgw/types_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Literal, Optional, Sequence
from pydantic import BaseModel


class BashCommand(BaseModel):
command: str


Specials = Literal[
"Key-up", "Key-down", "Key-left", "Key-right", "Enter", "Ctrl-c", "Ctrl-d", "Ctrl-z"
]


class BashInteraction(BaseModel):
send_text: Optional[str] = None
send_specials: Optional[Sequence[Specials]] = None
send_ascii: Optional[Sequence[int]] = None


class ReadImage(BaseModel):
file_path: str
type: Literal["ReadImage"] = "ReadImage"


class Writefile(BaseModel):
file_path: str
file_content: str
Loading

0 comments on commit 04fecc5

Please sign in to comment.