opentool_dart 1.1.0
opentool_dart: ^1.1.0 copied to clipboard
OpenTool client and server SDK for Dart, providing a JSON Spec Parser.
OpenTool SDK for Dart #
English | 中文
Dart SDK for OpenTool client and server, including OpenTool JSON parser.
Example #
- Run
/example/server/main.dartto start an OpenTool Server - Run
/example/client/main.dartto start an OpenTool Client
Installation #
Add the following to your Dart project dependencies:
dependencies:
opentool_dart: ^1.1.0
Usage #
-
Implement the
Toolinterface:class MockTool extends Tool { MockUtil mockUtil = MockUtil(); @override Future<Map<String, dynamic>> call(String name, Map<String, dynamic>? arguments) async { if(name == "count") { int count = mockUtil.count(); return {"count": count}; } else { return FunctionNotSupportedException(functionName: name).toJson(); } } @override Future<OpenTool?> load() async { String folder = "${io.Directory.current.path}${io.Platform.pathSeparator}example${io.Platform.pathSeparator}server"; String fileName = "mock_tool.json"; String jsonPath = "$folder${io.Platform.pathSeparator}$fileName"; OpenTool openTool = await OpenToolJsonLoader().loadFromFile(jsonPath); return openTool; } } -
Start the
Server:Future<void> main() async { Tool tool = MockTool(); Server server = OpenToolServer(tool, "1.0.0", apiKeys: ["6621c8a3-2110-4e6a-9d62-70ccd467e789", "bb31b6a6-1fda-4214-8cd6-b1403842070c"]); await server.start(); }
Notes #
- The default port is
9627. Both Client and Server can change the port, just make sure they match. - New tools must implement the
callmethod. Theloadmethod is optional, but it's recommended to use the OpenTool JSON specification to describe tools. Programmatic creation of theOpenToolobject is also supported.