Skip to content

Commit a3565bd

Browse files
authored
Merge pull request #6 from 0xeb/docs/fluent-tool-readme
docs: use fluent tool builder syntax in README
2 parents 38fa0af + a16cb53 commit a3565bd

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

README.md

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,74 +35,67 @@ Snapshot conformance tests (optional):
3535

3636
## Custom Tools
3737

38-
Custom tools must be provided when creating or resuming a session. The SDK registers tool handlers locally and sends tool definitions to the server:
38+
Custom tools are provided when creating or resuming a session. The SDK auto-generates JSON schemas from C++ types.
39+
40+
### Fluent Builder (Recommended)
3941

4042
```cpp
41-
// Define your tool
42-
copilot::Tool calc_tool;
43-
calc_tool.name = "calculator";
44-
calc_tool.description = "Perform math calculations";
45-
calc_tool.parameters_schema = copilot::json{
46-
{"type", "object"},
47-
{"properties", {{"expression", {{"type", "string"}}}}},
48-
{"required", {"expression"}}
49-
};
50-
calc_tool.handler = [](const copilot::ToolInvocation& inv) {
51-
copilot::ToolResultObject result;
52-
// Handle the tool call...
53-
return result;
54-
};
55-
56-
// Pass tools when creating the session
43+
#include <copilot/tool_builder.hpp>
44+
45+
// Fluent builder with full control over parameters
46+
auto calc = copilot::ToolBuilder("calculator", "Perform math calculations")
47+
.param<std::string>("expression", "Math expression to evaluate")
48+
.handler([](std::string expression) {
49+
// Evaluate expression...
50+
return "42";
51+
});
52+
53+
// With enum constraints and default values
54+
auto search = copilot::ToolBuilder("search", "Search the web")
55+
.param<std::string>("query", "Search query")
56+
.param<int>("limit", "Max results").default_value(10)
57+
.param<std::string>("sort", "Sort order").one_of("relevance", "date")
58+
.handler([](std::string query, int limit, std::string sort) {
59+
return "Results for: " + query;
60+
});
61+
62+
// Use in session
5763
copilot::SessionConfig config;
58-
config.tools = {calc_tool};
64+
config.tools = {calc, search};
5965
auto session = client.create_session(config).get();
60-
61-
// Or when resuming an existing session
62-
copilot::ResumeSessionConfig resume_config;
63-
resume_config.tools = {calc_tool};
64-
auto session = client.resume_session(session_id, resume_config).get();
6566
```
6667
67-
See `examples/tools.cpp` and `examples/resume_with_tools.cpp` for complete examples.
68-
69-
### Fluent Tool Builder
70-
71-
Use `make_tool` to create tools with automatic schema generation from lambda signatures:
68+
### Quick One-Liner with `make_tool`
7269
7370
```cpp
74-
#include <copilot/tool_builder.hpp>
75-
76-
// Single parameter - schema auto-generated
77-
auto echo_tool = copilot::make_tool(
71+
// Simple tools with auto-generated schema
72+
auto echo = copilot::make_tool(
7873
"echo", "Echo a message",
7974
[](std::string message) { return message; },
80-
{"message"} // Parameter names
75+
{"message"}
8176
);
8277
83-
// Multiple parameters
84-
auto calc_tool = copilot::make_tool(
78+
auto add = copilot::make_tool(
8579
"add", "Add two numbers",
8680
[](double a, double b) { return std::to_string(a + b); },
8781
{"first", "second"}
8882
);
8983
90-
// Optional parameters (not added to "required" in schema)
91-
auto greet_tool = copilot::make_tool(
84+
// Optional parameters (use std::optional)
85+
auto greet = copilot::make_tool(
9286
"greet", "Greet someone",
9387
[](std::string name, std::optional<std::string> title) {
94-
if (title)
95-
return "Hello, " + *title + " " + name + "!";
96-
return "Hello, " + name + "!";
88+
return title ? "Hello, " + *title + " " + name + "!"
89+
: "Hello, " + name + "!";
9790
},
9891
{"name", "title"}
9992
);
10093
101-
// Use in session config
102-
copilot::SessionConfig config;
103-
config.tools = {echo_tool, calc_tool, greet_tool};
94+
config.tools = {echo, add, greet};
10495
```
10596

97+
See `examples/tools.cpp` and `examples/resume_with_tools.cpp` for complete examples.
98+
10699
## BYOK (Bring Your Own Key)
107100

108101
Use your own API key instead of GitHub Copilot authentication.

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
byok.env

0 commit comments

Comments
 (0)