liteagent_sdk_dart 0.2.1
liteagent_sdk_dart: ^0.2.1 copied to clipboard
A LiteAgent Dart SDK, easy way to access your agent.
LiteAgent SDK for Dart #
English · 中文
The LiteAgent Dart SDK is used for interacting with LiteAgent in Dart and Flutter applications.
Features #
- Agent management: list/get/create/update/delete
- Initialize SessionAgent or SimpleAgent sessions (by capability or agentId)
- Send chat requests (SSE stream) or one-shot simple chat
- Subscribe to an existing session stream
- Handle both normal and stream function calls (
onFunctionCall/onStreamFunctionCall) - Send tool callbacks (callback / streamCallback)
streamCallbackis emitted fromonStreamFunctionCall; SDK auto-emitsDONEwhen user code does not.
- Set preset OpenSpec list
- Get history, stop sessions, and clear sessions
Installation #
Add the following dependency in your pubspec.yaml file:
dependencies:
liteagent_sdk_dart: ^0.2.1
Then run:
dart pub get
Usage #
- Implement AgentMessageHandler to subscribe to various Agent push messages
- Examples under
example/:example.dart: basic streaming chat with Capabilityliteagent_sdk_example.dart: simple end-to-end flow (Capability)liteagent_sdk_client_function_call_example.dart: function call flow (Capability)
Future<void> main() async {
String baseUrl = "<BASE_URL>";
String apiKey = "<API_KEY>";
String llmApiKey = "<LLM_API_KEY>";
String llmBaseUrl = "<LLM_BASE_URL>";
String llmModel = "<LLM_MODEL>";
String userPrompt = "hi";
LiteAgentSDK liteAgent = LiteAgentSDK(baseUrl: baseUrl, apiKey: apiKey);
// Option A: init by capability
Capability capability = Capability(
llmConfig: LLMConfig(
baseUrl: llmBaseUrl,
apiKey: llmApiKey,
model: llmModel,
),
systemPrompt: "You are a helpful assistant.",
);
Session session = await liteAgent.initSession(capability: capability);
// Option B: init by agentId
// String agentId = "<AGENT_ID>";
// Session session = await liteAgent.initSession(agentId: agentId);
UserTask userTaskDto = UserTask(
content: [Content(type: ContentType.text, message: userPrompt)],
isChunk: true,
);
AgentMessageHandler agentMessageHandler = AgentMessageHandlerImpl();
await liteAgent.chat(session, userTaskDto, agentMessageHandler);
}
class AgentMessageHandlerImpl extends AgentMessageHandler {
@override
Future<ToolReturn> onFunctionCall(String sessionId, FunctionCall functionCall) async {
print(functionCall.toJson().toString());
return ToolReturn(id: functionCall.id, result: {"name": functionCall.name, "params": {"status": "success"}});
}
@override
Future<void> onStreamFunctionCall(
String sessionId,
FunctionCall functionCall,
void Function(EventToolReturn) onToolReturn,
) async {
onToolReturn(
EventToolReturn(
eventType: EventType.DATA,
toolReturn: ToolReturn(
id: functionCall.id,
result: {"status": "streaming"},
),
),
);
}
@override
Future<void> onDone() async {
print("[onDone]");
}
@override
Future<void> onError(Exception e) async {
print("[onError]$e");
}
@override
Future<void> onMessage(String sessionId, AgentMessage agentMessageDto) async {
print("sessionId: $sessionId, agentMessage: ${agentMessageDto.toJson().toString()}");
}
@override
Future<void> onChunk(String sessionId, AgentMessageChunk agentMessageChunkDto) async {
print("sessionId: $sessionId, agentMessageChunk: agentMessageChunkDto.toJson().toString()}");
}
}
Notes:
- For
functionCallSSE events, SDK auto-detects stream mode viastream/isStreamflags and routes toonStreamFunctionCall. onStreamFunctionCallis optional to override. If not overridden, SDK returnsFunctionNotSupported.- SDK will automatically send a
DONEstreamCallback afteronStreamFunctionCallcompletes if user code did not emit one. LLMConfigis aligned with core field names: writesupportsToolCall/supportsReasoning(while still reading legacy names).