<@U06QJHRBLQG> - with regard to `ToolRequests`: `...
# proj-sdk
s
@chilly-france-44439 - with regard to `ToolRequests`:
Copy code
from abc import ABC, abstractmethod
from openhands.sdk import Tool
from pydantic import BaseModel
from openhands.tools import BashTool, FileEditorTool, TaskTrackerTool


class ToolRequest(BaseModel, ABC):
    
    @abstractmethod
    def create_tool(self) -> Tool:
        """ Create a tool """


class BashToolRequest(ToolRequest):
    working_dir: str

    def create_tool(self):
        return BashTool.create(working_dir=self.working_dir)
    

class FileEditorToolRequest(ToolRequest):

    def create_tool(self):
        return FileEditorTool.create()
 

class TaskTrackerToolRequest(ToolRequest):
    save_dir: str

    def create_tool(self):
        return TaskTrackerTool.create(save_dir=self.save_dir)
    

ToolRequestType = BashToolRequest | FileEditorToolRequest | TaskTrackerToolRequest
(So I use ToolRequestType in my AgentRequest in my CreateConversationRequest so FastAPI / Pydantic use it by default)
w
Are these HTTP reqeusts?
Don't all tool calls happen in python?
s
These are the equivalent of the
specs
Xingyao mentioned
w
I guess my question is, why do you need these for SDK-in-server? Or is this just a sketch?
s
My start_conversation is not finished yet, but roughly looks like this:
Copy code
@router.post("/")
async def start_conversation(request: ConversationStartRequest):
    conversation = request.create_conversation(cwd)
    loop = asyncio.get_running_loop()
    await loop.run_in_executor(None, conversation.run)
(I still need to add callbacks)
(And a response type)
But this allows you to start a new conversation (locally) - handling all the tool calls to be specified using pydantic
w
What I'd expect is something like
Copy code
@router.post("/")
async def start_conversation(request: ConversationStartRequest):
    runtime = LocalRuntime()
    conversation = request.create_conversation(runtime.get_tools(cwd))
    loop = asyncio.get_running_loop()
    await loop.run_in_executor(None, conversation.run)
so the tools are pre-defined by the runtime
s
That make sense. Do we ever want to define tools through the http request?
Like - is there ever a case where somebody might need custom tools per conversation?
w
No, I don't think we should support that
at least not yet
Xingyao was bringing up something similar today, where you could at least set cwd for the bash tool through the API
but I think a good rule of thumb is that when initializing an agent over HTTP, the tools are set in stone. at most you can disable tools
s
That is what spawned my original comment
w
we can expand as needed in the future
s
I posted the API I had written for doing what Xingyao was suggesting
👍 1
👆 here's the current draft version i've put together, LMK what do you think!
(still fighting some annoying Pydantic issue to generate a valid
openapi.json
😅
s
I suppose it depends on how useful you want the OpenAPI spec to be
https://github.com/All-Hands-AI/agent-sdk/blob/a15a2ffd30a82c8729f0db0f6b8e367c6252e655/openhands/sdk/tool/tool.py#L211 Will generate a json schema something like...
Copy code
{
  type: "object",
  properties: {
    "name": {type: "string"}
    "params": {}
  },
  additionalProperties: false
}
The example I posted earlier would generate ...
Copy code
{
  '$defs': {
    'BashToolRequest': {
      'properties': {
        'working_dir': {
          'title': 'Working Dir',
          'type': 'string'
        }
      },
      'required': [
        'working_dir'
      ],
      'title': 'BashToolRequest',
      'type': 'object'
    },
    'FileEditorToolRequest': {
      'properties': {
        
      },
      'title': 'FileEditorToolRequest',
      'type': 'object'
    },
    'TaskTrackerToolRequest': {
      'properties': {
        'save_dir': {
          'title': 'Save Dir',
          'type': 'string'
        }
      },
      'required': [
        'save_dir'
      ],
      'title': 'TaskTrackerToolRequest',
      'type': 'object'
    }
  },
  'anyOf': [
    {
      '$ref': '#/$defs/BashToolRequest'
    },
    {
      '$ref': '#/$defs/FileEditorToolRequest'
    },
    {
      '$ref': '#/$defs/TaskTrackerToolRequest'
    }
  ]
}
Rules of thumb I have found work well: In any case where I need a json schema, the base class won't do - I need a type that is the union of all implementations.
(In my case
ToolRequestType = BashToolRequest | FileEditorToolRequest | TaskTrackerToolRequest
)
This will give you type safety even in the parameters you pass into fastapi - if they are invalid it will usually fail before it even reaches your code
c
The tricky thing is taht we will probably need to define one of these for each tool.. I was hoping we could have something generic 😭
w
this mostly LGTM xingyao!
do we need
agent.working_dir
AND
bash_tool.workdir
?
c
not really, i'm thinking about removing agent.working_dir though
👍 1
w
overall this looks right
c
@wooden-umbrella-84098 lmk if you are good with that, mainly for internal use: i break the LLMSpec (just the old LLMConfig) from LLM class to fix some really weird JSON schema bug for openapi 😅 Just one more level of inheritance but we got the JSON schema for OpenAPI https://github.com/All-Hands-AI/agent-sdk/pull/194/commits/22b4995ab071b2c0b0e6e0ff964d3c8e62449f1f
w
thonk this looks off--what's the issue with
service_id
?
it's not super uncommon for request/response schemas to differ from internal schemas (since some fields are immutable or should be hidden)
but I'd maybe call it
LLMRequest
or sth
c
it was with
Metrics
for some reason 😅
Metrics
only exists at runtime though
w
right
is
Metrics
not pydantic?
c
yes, it is, but somehow it causes werid issue 😅
w
or can we like...exclude it from the pydantic model, so OpenAPI doesn't try to interpret it?
there must be some OpenAPI annotations that let you ignore things...
c
that's also an option, making it a private attribute
w
that actually makes a lot of sense to me
you can add
get_metrics
if someone needs access
👍 1
but the POST request shouldn't be able to set it
c
Maybe just @property metrics and
_metrics
as attribute
👍 2