feat(worker-service): integrate real Claude Code session ID retrieval
- Added `claudeSessionId` to the `ActiveSession` interface to store the real Claude Code session ID. - Updated session initialization to fetch the real Claude Code session ID from the database. - Modified session creation logic to ensure the real session ID is used consistently throughout the worker service. - Adjusted message generation to utilize the real session ID instead of a placeholder.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -37,6 +37,7 @@ type WorkerMessage = ObservationMessage | SummarizeMessage;
|
|||||||
*/
|
*/
|
||||||
interface ActiveSession {
|
interface ActiveSession {
|
||||||
sessionDbId: number;
|
sessionDbId: number;
|
||||||
|
claudeSessionId: string; // Real Claude Code session ID
|
||||||
sdkSessionId: string | null;
|
sdkSessionId: string | null;
|
||||||
project: string;
|
project: string;
|
||||||
userPrompt: string;
|
userPrompt: string;
|
||||||
@@ -118,9 +119,22 @@ class WorkerService {
|
|||||||
const correlationId = logger.sessionId(sessionDbId);
|
const correlationId = logger.sessionId(sessionDbId);
|
||||||
logger.info('WORKER', 'Session init', { correlationId, project });
|
logger.info('WORKER', 'Session init', { correlationId, project });
|
||||||
|
|
||||||
|
// Fetch real Claude Code session ID from database
|
||||||
|
const db = new SessionStore();
|
||||||
|
const dbSession = db.getSessionById(sessionDbId);
|
||||||
|
if (!dbSession) {
|
||||||
|
db.close();
|
||||||
|
res.status(404).json({ error: 'Session not found in database' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the real claude_session_id (which is the same as sdk_session_id now)
|
||||||
|
const claudeSessionId = dbSession.sdk_session_id || `session-${sessionDbId}`;
|
||||||
|
|
||||||
// Create session state
|
// Create session state
|
||||||
const session: ActiveSession = {
|
const session: ActiveSession = {
|
||||||
sessionDbId,
|
sessionDbId,
|
||||||
|
claudeSessionId,
|
||||||
sdkSessionId: null,
|
sdkSessionId: null,
|
||||||
project,
|
project,
|
||||||
userPrompt,
|
userPrompt,
|
||||||
@@ -135,7 +149,6 @@ class WorkerService {
|
|||||||
this.sessions.set(sessionDbId, session);
|
this.sessions.set(sessionDbId, session);
|
||||||
|
|
||||||
// Update port in database
|
// Update port in database
|
||||||
const db = new SessionStore();
|
|
||||||
db.setWorkerPort(sessionDbId, this.port!);
|
db.setWorkerPort(sessionDbId, this.port!);
|
||||||
db.close();
|
db.close();
|
||||||
|
|
||||||
@@ -167,11 +180,19 @@ 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 dbSession = db.getSessionById(sessionDbId);
|
||||||
|
db.close();
|
||||||
|
|
||||||
|
const claudeSessionId = dbSession?.sdk_session_id || `session-${sessionDbId}`;
|
||||||
|
|
||||||
session = {
|
session = {
|
||||||
sessionDbId,
|
sessionDbId,
|
||||||
|
claudeSessionId,
|
||||||
sdkSessionId: null,
|
sdkSessionId: null,
|
||||||
project: '',
|
project: dbSession?.project || '',
|
||||||
userPrompt: '',
|
userPrompt: dbSession?.user_prompt || '',
|
||||||
pendingMessages: [],
|
pendingMessages: [],
|
||||||
abortController: new AbortController(),
|
abortController: new AbortController(),
|
||||||
generatorPromise: null,
|
generatorPromise: null,
|
||||||
@@ -223,11 +244,19 @@ 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 dbSession = db.getSessionById(sessionDbId);
|
||||||
|
db.close();
|
||||||
|
|
||||||
|
const claudeSessionId = dbSession?.sdk_session_id || `session-${sessionDbId}`;
|
||||||
|
|
||||||
session = {
|
session = {
|
||||||
sessionDbId,
|
sessionDbId,
|
||||||
|
claudeSessionId,
|
||||||
sdkSessionId: null,
|
sdkSessionId: null,
|
||||||
project: '',
|
project: dbSession?.project || '',
|
||||||
userPrompt: '',
|
userPrompt: dbSession?.user_prompt || '',
|
||||||
pendingMessages: [],
|
pendingMessages: [],
|
||||||
abortController: new AbortController(),
|
abortController: new AbortController(),
|
||||||
generatorPromise: null,
|
generatorPromise: null,
|
||||||
@@ -404,18 +433,19 @@ class WorkerService {
|
|||||||
* Keeps running continuously - no finalize, agent stays alive for entire Claude Code session
|
* Keeps running continuously - no finalize, agent stays alive for entire Claude Code session
|
||||||
*/
|
*/
|
||||||
private async* createMessageGenerator(session: ActiveSession): AsyncIterable<SDKUserMessage> {
|
private async* createMessageGenerator(session: ActiveSession): AsyncIterable<SDKUserMessage> {
|
||||||
const claudeSessionId = `session-${session.sessionDbId}`;
|
// Use real Claude Code session ID instead of fake session-{dbId}
|
||||||
const initPrompt = buildInitPrompt(session.project, claudeSessionId, session.userPrompt);
|
const initPrompt = buildInitPrompt(session.project, session.claudeSessionId, session.userPrompt);
|
||||||
|
|
||||||
logger.dataIn('SDK', `Init prompt sent (${initPrompt.length} chars)`, {
|
logger.dataIn('SDK', `Init prompt sent (${initPrompt.length} chars)`, {
|
||||||
sessionId: session.sessionDbId,
|
sessionId: session.sessionDbId,
|
||||||
|
claudeSessionId: session.claudeSessionId,
|
||||||
project: session.project
|
project: session.project
|
||||||
});
|
});
|
||||||
logger.debug('SDK', 'Full init prompt', { sessionId: session.sessionDbId }, initPrompt);
|
logger.debug('SDK', 'Full init prompt', { sessionId: session.sessionDbId }, initPrompt);
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: 'user',
|
type: 'user',
|
||||||
session_id: session.sdkSessionId || claudeSessionId,
|
session_id: session.claudeSessionId, // Use real session ID from the start
|
||||||
parent_tool_use_id: null,
|
parent_tool_use_id: null,
|
||||||
message: {
|
message: {
|
||||||
role: 'user',
|
role: 'user',
|
||||||
@@ -455,7 +485,7 @@ class WorkerService {
|
|||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: 'user',
|
type: 'user',
|
||||||
session_id: session.sdkSessionId || claudeSessionId,
|
session_id: session.claudeSessionId, // Use real session ID
|
||||||
parent_tool_use_id: null,
|
parent_tool_use_id: null,
|
||||||
message: {
|
message: {
|
||||||
role: 'user',
|
role: 'user',
|
||||||
@@ -486,7 +516,7 @@ class WorkerService {
|
|||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: 'user',
|
type: 'user',
|
||||||
session_id: session.sdkSessionId || claudeSessionId,
|
session_id: session.claudeSessionId, // Use real session ID
|
||||||
parent_tool_use_id: null,
|
parent_tool_use_id: null,
|
||||||
message: {
|
message: {
|
||||||
role: 'user',
|
role: 'user',
|
||||||
|
|||||||
Reference in New Issue
Block a user