Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions crates/thulp-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# thulp-adapter

Adapter for converting external tool definitions to Thulp format.

## Overview

This crate provides functionality to convert OpenAPI v2/v3 specifications into Thulp tool definitions. It enables automatic generation of tool adapters from existing API specifications, making it easy to integrate external services with AI agents.

## Features

- Parse OpenAPI v2 and v3 specifications (JSON and YAML)
- Convert API endpoints into Thulp tool definitions
- Extract authentication requirements
- Generate adapter configuration files
- Support for path, query, and body parameters

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
thulp-adapter = "0.2"
```

## Usage

### Basic Conversion

```rust
use thulp_adapter::AdapterGenerator;
use serde_json::Value;

// Load an OpenAPI specification
let spec: Value = serde_json::from_str(r#"{
"openapi": "3.0.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {
"/users": {
"get": {
"operationId": "listUsers",
"summary": "List all users",
"responses": {
"200": {
"description": "A list of users"
}
}
}
}
}
}"#).unwrap();

// Create an adapter generator
let generator = AdapterGenerator::new(spec, Some("test-api".to_string()));

// Generate tool definitions
let tools = generator.generate_tools().unwrap();
println!("Generated {} tools", tools.len());
```

### Loading from URL

```rust
use thulp_adapter::AdapterGenerator;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let generator = AdapterGenerator::from_url(
"https://api.example.com/openapi.json",
Some("example-api".to_string())
).await?;

let tools = generator.generate_tools()?;
Ok(())
}
```

### Extract Authentication Configuration

```rust
use thulp_adapter::AdapterGenerator;
use serde_json::json;

let spec = json!({
"openapi": "3.0.0",
"info": {"title": "Test API", "version": "1.0.0"},
"components": {
"securitySchemes": {
"ApiKeyAuth": {
"type": "apiKey",
"in": "header",
"name": "X-API-Key"
}
}
},
"paths": {}
});

let generator = AdapterGenerator::new(spec, Some("test-api".to_string()));
let auth_configs = generator.extract_auth_config();
```

### Generate Configuration File

```rust
use thulp_adapter::AdapterGenerator;
use serde_json::json;

let spec = json!({
"openapi": "3.0.0",
"info": {"title": "API", "version": "1.0.0"},
"paths": {
"/items": {
"get": {
"operationId": "listItems",
"summary": "List items"
}
}
}
});

let generator = AdapterGenerator::new(spec, Some("items-api".to_string()));
let yaml_config = generator.generate_config()?;
```

## Parameter Type Mapping

The adapter automatically maps OpenAPI types to Thulp parameter types:

| OpenAPI Type | Thulp ParameterType |
|--------------|---------------------|
| `string` | `ParameterType::String` |
| `integer` | `ParameterType::Integer` |
| `number` | `ParameterType::Number` |
| `boolean` | `ParameterType::Boolean` |
| `array` | `ParameterType::Array` |
| `object` | `ParameterType::Object` |

## CLI Usage

Use the Thulp CLI to convert OpenAPI specifications:

```bash
# Convert OpenAPI spec to tool definitions
thulp convert openapi spec.yaml --output tools.yaml

# Show conversion examples
thulp convert examples
```

## Testing

```bash
cargo test -p thulp-adapter
```

## License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE](../../LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](../../LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.
161 changes: 161 additions & 0 deletions crates/thulp-browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# thulp-browser

Browser automation and web scraping utilities for Thulp.

## Overview

This crate provides tools for web page fetching, HTML content extraction, and browser automation. It supports basic HTTP fetching out of the box and optional Chrome DevTools Protocol (CDP) integration for full browser automation.

## Features

- **Web Page Fetching**: Simple async HTTP client for fetching web pages
- **HTML Content Extraction**: Extract text content and page titles from HTML
- **CDP Support**: Optional Chrome DevTools Protocol integration for advanced browser automation
- **Page Metadata**: Access page URL, status code, title, and content
- **Async Design**: Built on tokio and reqwest for efficient async operations

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
thulp-browser = "0.2"
```

For CDP browser automation support:

```toml
[dependencies]
thulp-browser = { version = "0.2", features = ["cdp"] }
```

## Usage

### Basic Web Fetching

```rust
use thulp_browser::WebClient;

#[tokio::main]
async fn main() -> Result<(), thulp_browser::BrowserError> {
let client = WebClient::new();
let page = client.fetch("https://example.com").await?;

println!("URL: {}", page.url);
println!("Status: {}", page.status);
println!("Title: {:?}", page.title);
println!("Content length: {} bytes", page.len());

// Extract text content (HTML tags stripped)
println!("Text: {}", page.text());

Ok(())
}
```

### Working with Page Content

```rust
use thulp_browser::{Page, WebClient};

#[tokio::main]
async fn main() -> Result<(), thulp_browser::BrowserError> {
let client = WebClient::new();
let page = client.fetch("https://example.com").await?;

// Check if fetch was successful
if page.status == 200 {
// Access raw HTML
println!("HTML: {}", page.html);

// Get text without HTML tags
let text = page.text();

// Check page title
if let Some(title) = &page.title {
println!("Page title: {}", title);
}
}

Ok(())
}
```

### CDP Browser Automation (requires `cdp` feature)

```rust
use thulp_browser::cdp::{Browser, BrowserConfig};

#[tokio::main]
async fn main() -> Result<(), thulp_browser::BrowserError> {
// Configure browser (headless mode)
let config = BrowserConfig::new().headless(true);

// Launch browser
let browser = Browser::launch(config).await?;

// Create a new page
let page = browser.new_page().await?;

// Navigate to URL
page.navigate("https://example.com").await?;

// Take a screenshot
let screenshot = page.screenshot().await?;

// Evaluate JavaScript
let result = page.evaluate("document.title").await?;

Ok(())
}
```

## Page Structure

The `Page` struct contains:

- **url**: The URL of the fetched page
- **html**: Raw HTML content
- **title**: Extracted page title (if found)
- **status**: HTTP status code

## Error Types

The crate provides detailed error types for different failure scenarios:

- `BrowserError::Http`: HTTP request failures
- `BrowserError::Parse`: HTML parsing errors
- `BrowserError::InvalidUrl`: Invalid URL format
- `BrowserError::CdpConnection`: CDP connection failures
- `BrowserError::CdpProtocol`: CDP protocol errors
- `BrowserError::BrowserLaunch`: Browser launch failures
- `BrowserError::Navigation`: Page navigation failures
- `BrowserError::JavaScriptEval`: JavaScript evaluation failures
- `BrowserError::Screenshot`: Screenshot capture failures
- `BrowserError::Timeout`: Operation timeout

## Feature Flags

| Flag | Description |
|------|-------------|
| `cdp` | Enable Chrome DevTools Protocol support |

## Testing

```bash
# Run tests
cargo test -p thulp-browser

# Run tests with CDP feature
cargo test -p thulp-browser --features cdp
```

## License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE](../../LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](../../LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.
Loading