concevent-ai-agent-sdk 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -84,6 +84,7 @@ console.log(result.message);
|
|
|
84
84
|
- [Tool Definitions](#tool-definitions)
|
|
85
85
|
- [Callbacks & Events](#callbacks--events)
|
|
86
86
|
- [Types](#types)
|
|
87
|
+
- [Streaming](#streaming)
|
|
87
88
|
- [Advanced Usage](#advanced-usage)
|
|
88
89
|
- [Conversation Summarization](#conversation-summarization)
|
|
89
90
|
- [Error Handling](#error-handling)
|
|
@@ -134,6 +135,7 @@ const agent = createAgent(config: AgentConfig): Agent;
|
|
|
134
135
|
| `temperature` | `number` | ❌ | `0.1` | Sampling temperature (0-2) |
|
|
135
136
|
| `reasoningEffort` | `'low' \| 'medium' \| 'high'` | ❌ | `'high'` | Reasoning effort level for supported models |
|
|
136
137
|
| `maxIterations` | `number` | ❌ | `20` | Maximum tool execution iterations per chat |
|
|
138
|
+
| `stream` | `boolean` | ❌ | `true` | Enable streaming responses with delta callbacks |
|
|
137
139
|
| `summarization` | `SummarizationConfig` | ❌ | `{ enabled: true }` | Summarization settings |
|
|
138
140
|
| `errorMessages` | `ErrorMessages` | ❌ | Default messages | Custom error messages |
|
|
139
141
|
|
|
@@ -408,6 +410,8 @@ interface AgentCallbacks {
|
|
|
408
410
|
tokenCount?: number;
|
|
409
411
|
}
|
|
410
412
|
) => void;
|
|
413
|
+
onMessageDelta?: (delta: string) => void;
|
|
414
|
+
onReasoningDelta?: (detail: ReasoningDetail) => void;
|
|
411
415
|
onToolCallStart?: (calls: ToolCallStartData[]) => void;
|
|
412
416
|
onToolResult?: (result: ToolResultData) => void;
|
|
413
417
|
onUsageUpdate?: (usage: UsageMetadata) => void;
|
|
@@ -441,6 +445,18 @@ await agent.chat("Help me with my task", context, {
|
|
|
441
445
|
}
|
|
442
446
|
},
|
|
443
447
|
|
|
448
|
+
// Called for each chunk of streaming message content
|
|
449
|
+
onMessageDelta: (delta) => {
|
|
450
|
+
process.stdout.write(delta); // Real-time output
|
|
451
|
+
},
|
|
452
|
+
|
|
453
|
+
// Called for each chunk of streaming reasoning (for reasoning models)
|
|
454
|
+
onReasoningDelta: (detail) => {
|
|
455
|
+
if (detail.text) {
|
|
456
|
+
process.stdout.write(detail.text);
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
|
|
444
460
|
// Called before tool execution starts
|
|
445
461
|
onToolCallStart: (calls) => {
|
|
446
462
|
calls.forEach((call) => {
|
|
@@ -586,6 +602,154 @@ interface ToolResultData {
|
|
|
586
602
|
|
|
587
603
|
---
|
|
588
604
|
|
|
605
|
+
## Streaming
|
|
606
|
+
|
|
607
|
+
The SDK supports streaming responses by default, providing real-time updates as the model generates content.
|
|
608
|
+
|
|
609
|
+
### Enabling/Disabling Streaming
|
|
610
|
+
|
|
611
|
+
Streaming is **enabled by default**. You can disable it in the agent configuration:
|
|
612
|
+
|
|
613
|
+
```typescript
|
|
614
|
+
const agent = createAgent({
|
|
615
|
+
apiKey: process.env.OPENROUTER_API_KEY!,
|
|
616
|
+
model: "anthropic/claude-3.5-sonnet",
|
|
617
|
+
systemPrompts: ["You are a helpful assistant."],
|
|
618
|
+
tools: [],
|
|
619
|
+
stream: false, // Disable streaming (default: true)
|
|
620
|
+
});
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
### Streaming Callbacks
|
|
624
|
+
|
|
625
|
+
When streaming is enabled, you can use delta callbacks to receive real-time updates:
|
|
626
|
+
|
|
627
|
+
#### onMessageDelta
|
|
628
|
+
|
|
629
|
+
Called whenever a new chunk of the message content is received:
|
|
630
|
+
|
|
631
|
+
```typescript
|
|
632
|
+
await agent.chat("Tell me a story", context, {
|
|
633
|
+
onMessageDelta: (delta) => {
|
|
634
|
+
// Append each chunk to your UI in real-time
|
|
635
|
+
process.stdout.write(delta);
|
|
636
|
+
},
|
|
637
|
+
onMessage: (fullMessage) => {
|
|
638
|
+
// Called when the complete message is ready
|
|
639
|
+
console.log("\n\nFull message:", fullMessage);
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
#### onReasoningDelta
|
|
645
|
+
|
|
646
|
+
Called whenever a new chunk of model reasoning is received (for models that support reasoning):
|
|
647
|
+
|
|
648
|
+
```typescript
|
|
649
|
+
await agent.chat("Solve this problem step by step", context, {
|
|
650
|
+
onReasoningDelta: (detail) => {
|
|
651
|
+
if (detail.type === "reasoning.text" && detail.text) {
|
|
652
|
+
// Stream the reasoning/thinking output
|
|
653
|
+
process.stdout.write(detail.text);
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
onThinking: (fullThinking, details) => {
|
|
657
|
+
// Called when reasoning is complete
|
|
658
|
+
console.log("\n\nFull reasoning:", fullThinking);
|
|
659
|
+
},
|
|
660
|
+
});
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
### Complete Streaming Example
|
|
664
|
+
|
|
665
|
+
```typescript
|
|
666
|
+
import { createAgent } from "concevent-ai-agent-sdk";
|
|
667
|
+
|
|
668
|
+
const agent = createAgent({
|
|
669
|
+
apiKey: process.env.OPENROUTER_API_KEY!,
|
|
670
|
+
model: "anthropic/claude-3.5-sonnet",
|
|
671
|
+
systemPrompts: ["You are a helpful assistant."],
|
|
672
|
+
tools: myTools,
|
|
673
|
+
stream: true, // Enabled by default
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
let messageBuffer = "";
|
|
677
|
+
let reasoningBuffer = "";
|
|
678
|
+
|
|
679
|
+
const result = await agent.chat(
|
|
680
|
+
"Explain quantum computing",
|
|
681
|
+
{ userId: "user-123", timezone: "UTC" },
|
|
682
|
+
{
|
|
683
|
+
// Real-time message chunks
|
|
684
|
+
onMessageDelta: (delta) => {
|
|
685
|
+
messageBuffer += delta;
|
|
686
|
+
updateUI(messageBuffer); // Update your UI with partial content
|
|
687
|
+
},
|
|
688
|
+
|
|
689
|
+
// Real-time reasoning chunks (for reasoning models)
|
|
690
|
+
onReasoningDelta: (detail) => {
|
|
691
|
+
if (detail.text) {
|
|
692
|
+
reasoningBuffer += detail.text;
|
|
693
|
+
updateReasoningUI(reasoningBuffer);
|
|
694
|
+
}
|
|
695
|
+
},
|
|
696
|
+
|
|
697
|
+
// Tool execution still works with streaming
|
|
698
|
+
onToolCallStart: (calls) => {
|
|
699
|
+
showToolIndicator(calls.map((c) => c.name));
|
|
700
|
+
},
|
|
701
|
+
|
|
702
|
+
onToolResult: (result) => {
|
|
703
|
+
hideToolIndicator(result.functionName);
|
|
704
|
+
},
|
|
705
|
+
|
|
706
|
+
// Final complete message
|
|
707
|
+
onMessage: (message, reasoning) => {
|
|
708
|
+
console.log("Complete message received");
|
|
709
|
+
},
|
|
710
|
+
}
|
|
711
|
+
);
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
### Streaming Event Types
|
|
715
|
+
|
|
716
|
+
The SDK exports event types for building event-driven streaming systems:
|
|
717
|
+
|
|
718
|
+
```typescript
|
|
719
|
+
import { createEvent } from "concevent-ai-agent-sdk";
|
|
720
|
+
import type {
|
|
721
|
+
MessageDeltaEventData,
|
|
722
|
+
ReasoningDeltaEventData,
|
|
723
|
+
} from "concevent-ai-agent-sdk";
|
|
724
|
+
|
|
725
|
+
// Create typed streaming events
|
|
726
|
+
const messageDeltaEvent = createEvent("message_delta", {
|
|
727
|
+
delta: "Hello, ",
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
const reasoningDeltaEvent = createEvent("reasoning_delta", {
|
|
731
|
+
detail: {
|
|
732
|
+
type: "reasoning.text",
|
|
733
|
+
text: "Let me think about this...",
|
|
734
|
+
format: "text",
|
|
735
|
+
index: 0,
|
|
736
|
+
},
|
|
737
|
+
});
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
### Streaming vs Non-Streaming
|
|
741
|
+
|
|
742
|
+
| Feature | Streaming (`stream: true`) | Non-Streaming (`stream: false`) |
|
|
743
|
+
| ------------------- | --------------------------------------- | ------------------------------- |
|
|
744
|
+
| Message delivery | Real-time chunks via `onMessageDelta` | Complete message only |
|
|
745
|
+
| Reasoning output | Real-time via `onReasoningDelta` | Complete reasoning only |
|
|
746
|
+
| Perceived latency | Lower (immediate feedback) | Higher (wait for completion) |
|
|
747
|
+
| Tool calls | Fully supported | Fully supported |
|
|
748
|
+
| Token usage | Included in final chunk | Included in response |
|
|
749
|
+
| Default | ✅ Enabled | Must explicitly disable |
|
|
750
|
+
|
|
751
|
+
---
|
|
752
|
+
|
|
589
753
|
## Advanced Usage
|
|
590
754
|
|
|
591
755
|
### Conversation Summarization
|
|
@@ -726,6 +890,76 @@ agent.chat("Hello", {
|
|
|
726
890
|
abortController.abort();
|
|
727
891
|
```
|
|
728
892
|
|
|
893
|
+
### Serverless / Stateless Deployments
|
|
894
|
+
|
|
895
|
+
When deploying in serverless environments (e.g., AWS Lambda, Vercel, Cloudflare Workers) or stateless API routes (e.g., Next.js API routes), the agent instance is created fresh for each request. To maintain conversation continuity, **the client must store and forward the conversation history with each request**.
|
|
896
|
+
|
|
897
|
+
#### Pattern
|
|
898
|
+
|
|
899
|
+
1. **Client** maintains `conversationHistory` state
|
|
900
|
+
2. **Client** sends the history with each chat request
|
|
901
|
+
3. **Server** creates a fresh agent, restores history via `setHistory()`, processes the message
|
|
902
|
+
4. **Server** returns the result including `conversationHistory`
|
|
903
|
+
5. **Client** updates its local history from the response
|
|
904
|
+
|
|
905
|
+
#### Server-Side Example (Next.js API Route)
|
|
906
|
+
|
|
907
|
+
```typescript
|
|
908
|
+
import { createAgent } from "concevent-ai-agent-sdk";
|
|
909
|
+
import type { ChatMessage } from "concevent-ai-agent-sdk";
|
|
910
|
+
|
|
911
|
+
export async function POST(request: Request) {
|
|
912
|
+
const { message, conversationHistory = [] } = await request.json();
|
|
913
|
+
|
|
914
|
+
const agent = createAgent({
|
|
915
|
+
apiKey: process.env.API_KEY!,
|
|
916
|
+
model: "anthropic/claude-3.5-sonnet",
|
|
917
|
+
systemPrompts: ["You are a helpful assistant."],
|
|
918
|
+
tools: myTools,
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
// Restore conversation history from the client
|
|
922
|
+
if (conversationHistory.length > 0) {
|
|
923
|
+
agent.setHistory(conversationHistory);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
const result = await agent.chat(message, {
|
|
927
|
+
userId: "user-123",
|
|
928
|
+
timezone: "UTC",
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
// Return result - client should use result.conversationHistory for next request
|
|
932
|
+
return Response.json({
|
|
933
|
+
message: result.message,
|
|
934
|
+
conversationHistory: result.conversationHistory,
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
```
|
|
938
|
+
|
|
939
|
+
#### Client-Side Example
|
|
940
|
+
|
|
941
|
+
```typescript
|
|
942
|
+
const [conversationHistory, setConversationHistory] = useState<ChatMessage[]>(
|
|
943
|
+
[]
|
|
944
|
+
);
|
|
945
|
+
|
|
946
|
+
async function sendMessage(message: string) {
|
|
947
|
+
const response = await fetch("/api/chat", {
|
|
948
|
+
method: "POST",
|
|
949
|
+
body: JSON.stringify({ message, conversationHistory }),
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
const result = await response.json();
|
|
953
|
+
|
|
954
|
+
// Update local history for the next request
|
|
955
|
+
setConversationHistory(result.conversationHistory);
|
|
956
|
+
|
|
957
|
+
return result.message;
|
|
958
|
+
}
|
|
959
|
+
```
|
|
960
|
+
|
|
961
|
+
> **Note:** The SDK handles summarization automatically when context limits are approached. The summarized history is included in `result.conversationHistory`, so clients always receive the properly managed history state.
|
|
962
|
+
|
|
729
963
|
---
|
|
730
964
|
|
|
731
965
|
## Exports Summary
|
|
@@ -759,6 +993,8 @@ export type {
|
|
|
759
993
|
UsageUpdateEventData,
|
|
760
994
|
ErrorEventData,
|
|
761
995
|
CompleteEventData,
|
|
996
|
+
MessageDeltaEventData,
|
|
997
|
+
ReasoningDeltaEventData,
|
|
762
998
|
AgentEvent,
|
|
763
999
|
} from "./types";
|
|
764
1000
|
```
|
package/package.json
CHANGED