|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import cast |
| 6 | + |
| 7 | +from anthropic import Anthropic |
| 8 | +from anthropic.types import ToolParam, MessageParam |
| 9 | + |
| 10 | +import gitpod.lib as util |
| 11 | +from gitpod import AsyncGitpod |
| 12 | +from gitpod.types.environment_initializer_param import Spec |
| 13 | + |
| 14 | +gpclient = AsyncGitpod() |
| 15 | +llmclient = Anthropic() |
| 16 | + |
| 17 | +user_message: MessageParam = { |
| 18 | + "role": "user", |
| 19 | + "content": "What is the test coverage for this repository: https://github.com/gitpod-io/gitpod-sdk-go", |
| 20 | +} |
| 21 | +tools: list[ToolParam] = [ |
| 22 | + { |
| 23 | + "name": "create_environment", |
| 24 | + "description": "Create a new environment for a given context URL. This will create a new environment and return the ID of the environment.", |
| 25 | + "input_schema": { |
| 26 | + "type": "object", |
| 27 | + "properties": {"context_url": {"type": "string"}}, |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + "name": "execute_command", |
| 32 | + "description": "Execute a command in a given environment ID. This will execute the command in the given environment and return the output of the command.", |
| 33 | + "input_schema": { |
| 34 | + "type": "object", |
| 35 | + "properties": {"environment_id": {"type": "string"}, "command": {"type": "string"}}, |
| 36 | + }, |
| 37 | + }, |
| 38 | +] |
| 39 | + |
| 40 | +async def create_environment(args: dict[str, str], cleanup: util.Disposables) -> str: |
| 41 | + env_class = await util.find_most_used_environment_class(gpclient) |
| 42 | + if not env_class: |
| 43 | + raise Exception("No environment class found. Please create one first.") |
| 44 | + env_class_id = env_class.id |
| 45 | + assert env_class_id is not None |
| 46 | + |
| 47 | + environment = (await gpclient.environments.create( |
| 48 | + spec={ |
| 49 | + "desired_phase": "ENVIRONMENT_PHASE_RUNNING", |
| 50 | + "content": { |
| 51 | + "initializer": {"specs": [Spec( |
| 52 | + context_url={ |
| 53 | + "url": args["context_url"] |
| 54 | + } |
| 55 | + )]}, |
| 56 | + }, |
| 57 | + "machine": {"class": env_class_id}, |
| 58 | + } |
| 59 | + )).environment |
| 60 | + assert environment is not None |
| 61 | + environment_id = environment.id |
| 62 | + assert environment_id is not None |
| 63 | + cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id))) |
| 64 | + |
| 65 | + print(f"\nCreated environment: {environment_id} - waiting for it to be ready...") |
| 66 | + await util.wait_for_environment_ready(gpclient, environment_id) |
| 67 | + print(f"\nEnvironment is ready: {environment_id}") |
| 68 | + return environment_id |
| 69 | + |
| 70 | +async def execute_command(args: dict[str, str]) -> str: |
| 71 | + lines_iter = await util.run_command(gpclient, args["environment_id"], args["command"]) |
| 72 | + lines: list[str] = [] |
| 73 | + async for line in lines_iter: |
| 74 | + lines.append(line) |
| 75 | + return "\n".join(lines) |
| 76 | + |
| 77 | +async def main(cleanup: util.Disposables) -> None: |
| 78 | + messages = [user_message] |
| 79 | + while True: |
| 80 | + message = llmclient.messages.create( |
| 81 | + model="claude-3-5-sonnet-latest", |
| 82 | + max_tokens=1024, |
| 83 | + messages=messages, |
| 84 | + tools=tools, |
| 85 | + ) |
| 86 | + print(f"\nResponse: {message.model_dump_json(indent=2)}") |
| 87 | + |
| 88 | + if message.stop_reason != "tool_use": |
| 89 | + print(f"\nFinal response reached! {message.model_dump_json(indent=2)}") |
| 90 | + break |
| 91 | + |
| 92 | + messages.extend([ |
| 93 | + {"role": message.role, "content": message.content} |
| 94 | + ]) |
| 95 | + |
| 96 | + # Handle all tool calls in this response |
| 97 | + for tool in (c for c in message.content if c.type == "tool_use"): |
| 98 | + try: |
| 99 | + if tool.name == "create_environment": |
| 100 | + args = cast(dict[str, str], tool.input) |
| 101 | + environment_id = await create_environment(args, cleanup) |
| 102 | + messages.append({ |
| 103 | + "role": "user", |
| 104 | + "content": [{ |
| 105 | + "type": "tool_result", |
| 106 | + "tool_use_id": tool.id, |
| 107 | + "content": [{"type": "text", "text": f"The environment ID is {environment_id}"}], |
| 108 | + }], |
| 109 | + }) |
| 110 | + elif tool.name == "execute_command": |
| 111 | + args = cast(dict[str, str], tool.input) |
| 112 | + output = await execute_command(args) |
| 113 | + messages.append({ |
| 114 | + "role": "user", |
| 115 | + "content": [{ |
| 116 | + "type": "tool_result", |
| 117 | + "tool_use_id": tool.id, |
| 118 | + "content": [{"type": "text", "text": output}], |
| 119 | + }], |
| 120 | + }) |
| 121 | + else: |
| 122 | + raise Exception(f"Unknown tool: {tool.name}") |
| 123 | + except Exception as e: |
| 124 | + messages.append({ |
| 125 | + "role": "user", |
| 126 | + "content": [{ |
| 127 | + "type": "tool_result", |
| 128 | + "tool_use_id": tool.id, |
| 129 | + "is_error": True, |
| 130 | + "content": [{"type": "text", "text": f"Error: {e}"}], |
| 131 | + }], |
| 132 | + }) |
| 133 | + |
| 134 | + print("\nFinal response reached!") |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + import asyncio |
| 138 | + disposables = util.Disposables() |
| 139 | + with disposables: |
| 140 | + asyncio.run(main(disposables)) |
0 commit comments