Refactor WorkerService to use claude_session_id directly from the database

- Updated session initialization to retrieve claude_session_id instead of sdk_session_id.
- Removed redundant comments and code related to sdk_session_id handling.
- Simplified session creation logic by directly using values from the database.
- Cleaned up message handling logic to focus on assistant messages and removed unnecessary checks for system init messages.
This commit is contained in:
Alex Newman
2025-10-24 21:54:07 -04:00
parent f4217cb2b9
commit 5b28c23b20
2 changed files with 41 additions and 68 deletions
File diff suppressed because one or more lines are too long
+25 -52
View File
@@ -128,14 +128,13 @@ class WorkerService {
return; return;
} }
// Get the real claude_session_id (which is the same as sdk_session_id now) const claudeSessionId = dbSession.claude_session_id;
const claudeSessionId = dbSession.sdk_session_id || `session-${sessionDbId}`;
// Create session state // Create session state
const session: ActiveSession = { const session: ActiveSession = {
sessionDbId, sessionDbId,
claudeSessionId, claudeSessionId,
sdkSessionId: dbSession.sdk_session_id || null, // Set from database since we set both fields now sdkSessionId: null,
project, project,
userPrompt, userPrompt,
pendingMessages: [], pendingMessages: [],
@@ -180,19 +179,16 @@ class WorkerService {
let session = this.sessions.get(sessionDbId); let session = this.sessions.get(sessionDbId);
if (!session) { if (!session) {
// Auto-create session if it doesn't exist (e.g., worker restarted) // Auto-create session if it doesn't exist (e.g., worker restarted)
// Fetch real session ID from database
const db = new SessionStore(); const db = new SessionStore();
const dbSession = db.getSessionById(sessionDbId); const dbSession = db.getSessionById(sessionDbId);
db.close(); db.close();
const claudeSessionId = dbSession?.sdk_session_id || `session-${sessionDbId}`;
session = { session = {
sessionDbId, sessionDbId,
claudeSessionId, claudeSessionId: dbSession!.claude_session_id,
sdkSessionId: null, sdkSessionId: null,
project: dbSession?.project || '', project: dbSession!.project,
userPrompt: dbSession?.user_prompt || '', userPrompt: dbSession!.user_prompt,
pendingMessages: [], pendingMessages: [],
abortController: new AbortController(), abortController: new AbortController(),
generatorPromise: null, generatorPromise: null,
@@ -244,19 +240,16 @@ class WorkerService {
let session = this.sessions.get(sessionDbId); let session = this.sessions.get(sessionDbId);
if (!session) { if (!session) {
// Auto-create session if it doesn't exist (e.g., worker restarted) // Auto-create session if it doesn't exist (e.g., worker restarted)
// Fetch real session ID from database
const db = new SessionStore(); const db = new SessionStore();
const dbSession = db.getSessionById(sessionDbId); const dbSession = db.getSessionById(sessionDbId);
db.close(); db.close();
const claudeSessionId = dbSession?.sdk_session_id || `session-${sessionDbId}`;
session = { session = {
sessionDbId, sessionDbId,
claudeSessionId, claudeSessionId: dbSession!.claude_session_id,
sdkSessionId: null, sdkSessionId: null,
project: dbSession?.project || '', project: dbSession!.project,
userPrompt: dbSession?.user_prompt || '', userPrompt: dbSession!.user_prompt,
pendingMessages: [], pendingMessages: [],
abortController: new AbortController(), abortController: new AbortController(),
generatorPromise: null, generatorPromise: null,
@@ -366,26 +359,8 @@ class WorkerService {
}); });
for await (const message of queryResult) { for await (const message of queryResult) {
// Handle system init message
if (message.type === 'system' && message.subtype === 'init') {
const systemMsg = message as SDKSystemMessage;
if (systemMsg.session_id) {
// Update in database first, check if it succeeded
const db = new SessionStore();
const updated = db.updateSDKSessionId(session.sessionDbId, systemMsg.session_id);
db.close();
if (updated) {
logger.success('SDK', 'Session initialized', {
sessionId: session.sessionDbId,
sdkSessionId: systemMsg.session_id
});
session.sdkSessionId = systemMsg.session_id;
}
}
}
// Handle assistant messages // Handle assistant messages
else if (message.type === 'assistant') { if (message.type === 'assistant') {
const content = message.message.content; const content = message.message.content;
const textContent = Array.isArray(content) const textContent = Array.isArray(content)
? content.filter((c: any) => c.type === 'text').map((c: any) => c.text).join('\n') ? content.filter((c: any) => c.type === 'text').map((c: any) => c.text).join('\n')
@@ -471,28 +446,26 @@ class WorkerService {
session.lastPromptNumber = message.prompt_number; session.lastPromptNumber = message.prompt_number;
const db = new SessionStore(); const db = new SessionStore();
const dbSession = db.getSessionById(session.sessionDbId) as SDKSession | undefined; const dbSession = db.getSessionById(session.sessionDbId) as SDKSession;
db.close(); db.close();
if (dbSession) { const summarizePrompt = buildSummaryPrompt(dbSession);
const summarizePrompt = buildSummaryPrompt(dbSession);
logger.dataIn('SDK', `Summary prompt sent (${summarizePrompt.length} chars)`, { logger.dataIn('SDK', `Summary prompt sent (${summarizePrompt.length} chars)`, {
sessionId: session.sessionDbId, sessionId: session.sessionDbId,
promptNumber: message.prompt_number promptNumber: message.prompt_number
}); });
logger.debug('SDK', 'Full summary prompt', { sessionId: session.sessionDbId }, summarizePrompt); logger.debug('SDK', 'Full summary prompt', { sessionId: session.sessionDbId }, summarizePrompt);
yield { yield {
type: 'user', type: 'user',
session_id: session.claudeSessionId, // Use real session ID session_id: session.claudeSessionId,
parent_tool_use_id: null, parent_tool_use_id: null,
message: { message: {
role: 'user', role: 'user',
content: summarizePrompt content: summarizePrompt
} }
}; };
}
} else if (message.type === 'observation') { } else if (message.type === 'observation') {
session.lastPromptNumber = message.prompt_number; session.lastPromptNumber = message.prompt_number;