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

Method createSession

src/context/ContextManager.ts:90–137  ·  view source on GitHub ↗

* 创建新会话

(
    userId?: string,
    preferences: Record<string, any> = {},
    configuration: Record<string, any> = {}
  )

Source from the content-addressed store, hash-verified

88 * 创建新会话
89 */
90 async createSession(
91 userId?: string,
92 preferences: Record<string, any> = {},
93 configuration: Record<string, any> = {}
94 ): Promise<string> {
95 // 优先使用配置中的sessionId,否则生成新的
96 const sessionId = configuration.sessionId || this.generateSessionId();
97 const now = Date.now();
98
99 // 创建初始上下文数据
100 const contextData: ContextData = {
101 layers: {
102 system: await this.createSystemContext(),
103 session: {
104 sessionId,
105 userId,
106 preferences,
107 configuration,
108 startTime: now,
109 },
110 conversation: {
111 messages: [],
112 topics: [],
113 lastActivity: now,
114 },
115 tool: {
116 recentCalls: [],
117 toolStates: {},
118 dependencies: {},
119 },
120 workspace: await this.createWorkspaceContext(),
121 },
122 metadata: {
123 totalTokens: 0,
124 priority: 1,
125 lastUpdated: now,
126 },
127 };
128
129 // 存储到内存和持久化存储
130 this.memory.setContext(contextData);
131 await this.persistent.saveContext(sessionId, contextData);
132
133 this.currentSessionId = sessionId;
134
135 console.log(`新会话已创建: ${sessionId}`);
136 return sessionId;
137 }
138
139 /**
140 * 加载现有会话

Callers 1

exampleFunction · 0.45

Calls 6

generateSessionIdMethod · 0.95
createSystemContextMethod · 0.95
setContextMethod · 0.80
saveContextMethod · 0.80
logMethod · 0.45

Tested by

no test coverage detected