()
| 5 | import { createContextManager } from './index.js'; |
| 6 | |
| 7 | async function example() { |
| 8 | // 创建上下文管理器 |
| 9 | const contextManager = createContextManager({ |
| 10 | storage: { |
| 11 | maxMemorySize: 500, |
| 12 | persistentPath: './example-context', |
| 13 | cacheSize: 50, |
| 14 | compressionEnabled: true, |
| 15 | }, |
| 16 | defaultFilter: { |
| 17 | maxTokens: 2000, |
| 18 | maxMessages: 30, |
| 19 | }, |
| 20 | compressionThreshold: 3000, |
| 21 | }); |
| 22 | |
| 23 | try { |
| 24 | // 初始化 |
| 25 | await contextManager.initialize(); |
| 26 | console.log('✅ 上下文管理器初始化成功'); |
| 27 | |
| 28 | // 创建新会话 |
| 29 | const sessionId = await contextManager.createSession('demo-user', { |
| 30 | language: 'zh-CN', |
| 31 | projectType: 'typescript', |
| 32 | }); |
| 33 | console.log(`✅ 创建会话: ${sessionId}`); |
| 34 | |
| 35 | // 添加系统消息 |
| 36 | await contextManager.addMessage('system', '你是一个专业的 TypeScript 开发助手。'); |
| 37 | |
| 38 | // 添加用户消息 |
| 39 | await contextManager.addMessage('user', '我想创建一个 TypeScript 项目,可以帮我设计架构吗?'); |
| 40 | |
| 41 | // 添加助手回复 |
| 42 | await contextManager.addMessage( |
| 43 | 'assistant', |
| 44 | '当然可以!我来帮您设计一个现代化的 TypeScript 项目架构。首先让我了解一下您的项目需求...' |
| 45 | ); |
| 46 | |
| 47 | // 添加工具调用记录 |
| 48 | await contextManager.addToolCall({ |
| 49 | id: 'tool_001', |
| 50 | name: 'create_file', |
| 51 | input: { path: './src/index.ts', content: 'console.log("Hello World");' }, |
| 52 | output: { success: true, message: '文件创建成功' }, |
| 53 | timestamp: Date.now(), |
| 54 | status: 'success', |
| 55 | }); |
| 56 | |
| 57 | // 更新工作空间信息 |
| 58 | contextManager.updateWorkspace({ |
| 59 | currentFiles: ['src/index.ts', 'package.json'], |
| 60 | recentFiles: ['src/index.ts'], |
| 61 | }); |
| 62 | |
| 63 | // 获取格式化的上下文 |
| 64 | const { context, compressed, tokenCount } = await contextManager.getFormattedContext({ |
no test coverage detected