liteagent_sdk_dart 0.2.9
liteagent_sdk_dart: ^0.2.9 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 persisted history (including pagination), stop sessions, and clear sessions
- Resume persisted sessions after server runtime eviction or restart
Installation #
Add the following dependency in your pubspec.yaml file:
dependencies:
liteagent_sdk_dart: ^0.2.5
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
final plannerSkill = '''
---
name: task-planner
description: Break work into clear ordered steps before execution.
---
1. Summarize the goal.
2. Propose a short plan.
3. Execute step by step and report blockers clearly.
''';
Capability capability = Capability(
llmConfig: LLMConfig(
baseUrl: llmBaseUrl,
apiKey: llmApiKey,
model: llmModel,
),
systemPrompt: "You are a helpful assistant.",
skills: [plannerSkill],
);
Session session = await liteAgent.initSession(capability: capability);
// `skills` entries are full Agent Skills documents, not just names.
// Spec: https://agentskills.io/specification
// 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);
// A persisted session can be reused after server runtime eviction or restart.
final restoredSession = Session(sessionId: '<SESSION_ID>');
await liteAgent.chat(restoredSession, userTaskDto, agentMessageHandler);
// Root-session history includes descendant-session messages. Inspect each
// AgentMessage.sessionId when the originating session matters.
// page and pageSize must be supplied together and are 1-based. Page 1 is
// the newest page; messages within the page remain chronological.
final firstPage = await liteAgent.getHistory(
restoredSession,
page: 1,
pageSize: 50,
);
// For history UIs, load one server-computed summary per task first.
final summaries = await liteAgent.getHistorySummary(
restoredSession.sessionId,
page: 1,
pageSize: 30,
);
// Lazily fetch process messages only when the user expands a task.
final turn = summaries.first;
if (turn.processMessageCount > 0) {
final process = await liteAgent.getHistoryProcess(
restoredSession.sessionId,
turn.originalTaskId,
page: 1,
pageSize: 50,
);
}
// Standalone model reasoning is returned as a regular AgentMessage with
// type == AgentMessageType.REASONING and String content.
}
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. - SSE messages, chunks, function handlers, and HTTP callbacks preserve the event's child
sessionId; legacy payloads without one fall back to the outer session. 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).