MCPcopy Create free account
hub / github.com/echoVic/blade-code / agentWithContextExample

Function agentWithContextExample

examples/agent-with-context.ts:8–142  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6import { Agent } from '../src/agent/index.js';
7
8async function agentWithContextExample() {
9 console.log('🚀 开始 Agent 上下文管理示例...\n');
10
11 // 创建配置了上下文管理的 Agent
12 const agent = new Agent({
13 debug: true,
14 llm: {
15 provider: 'qwen',
16 apiKey: process.env.QWEN_API_KEY, // 需要配置环境变量
17 model: 'qwen-turbo',
18 },
19 tools: {
20 enabled: true,
21 includeBuiltinTools: true,
22 },
23 context: {
24 enabled: true,
25 debug: true,
26 storage: {
27 maxMemorySize: 500,
28 persistentPath: './example-agent-context',
29 cacheSize: 50,
30 },
31 defaultFilter: {
32 maxTokens: 3000,
33 maxMessages: 20,
34 },
35 },
36 });
37
38 try {
39 // 初始化 Agent
40 await agent.init();
41 console.log('✅ Agent 初始化完成\n');
42
43 // 创建新的上下文会话
44 const sessionId = await agent.createContextSession('demo-user', {
45 language: 'zh-CN',
46 expertise: 'TypeScript开发',
47 });
48 console.log(`📋 创建会话: ${sessionId}\n`);
49
50 // 进行多轮对话,验证上下文记忆功能
51 console.log('💬 开始多轮对话测试...\n');
52
53 // 第一轮:介绍项目
54 const response1 = await agent.chatWithContext(
55 '我正在开发一个基于TypeScript的AI代理CLI工具,叫做blade-ai。目前已经实现了LLM对话和工具调用功能。',
56 '你是一个专业的TypeScript开发助手,善于提供技术建议和代码审查。'
57 );
58 console.log('👤 用户: 我正在开发一个基于TypeScript的AI代理CLI工具...');
59 console.log(`🤖 助手: ${response1}\n`);
60
61 // 第二轮:询问技术细节(测试上下文记忆)
62 const response2 = await agent.chatWithContext(
63 '现在我想为这个项目添加上下文管理功能,你觉得应该如何设计?'
64 );
65 console.log('👤 用户: 现在我想为这个项目添加上下文管理功能...');

Callers 1

Calls 10

initMethod · 0.95
createContextSessionMethod · 0.95
chatWithContextMethod · 0.95
smartChatWithContextMethod · 0.95
getContextStatsMethod · 0.95
searchContextSessionsMethod · 0.95
loadContextSessionMethod · 0.95
destroyMethod · 0.95
logMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected