Skip to content

Commit b238c36

Browse files
authored
Merge pull request #25 from fancyboi999/fix/agent-session-id
fix: align agent session ids across service and providers
2 parents cc9a641 + 9f268b6 commit b238c36

5 files changed

Lines changed: 44 additions & 13 deletions

File tree

src-api/src/core/agent/base.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ export abstract class BaseAgent implements IAgent {
6060
/**
6161
* Create a new session
6262
*/
63-
protected createSession(phase: AgentSession['phase'] = 'idle'): AgentSession {
63+
protected createSession(
64+
phase: AgentSession['phase'] = 'idle',
65+
overrides?: { id?: string; abortController?: AbortController }
66+
): AgentSession {
6467
const session: AgentSession = {
65-
id: nanoid(),
68+
id: overrides?.id || nanoid(),
6669
createdAt: new Date(),
6770
phase,
6871
isAborted: false,
69-
abortController: new AbortController(),
72+
abortController: overrides?.abortController || new AbortController(),
7073
config: this.config,
7174
};
7275
this.sessions.set(session.id, session);

src-api/src/extensions/agent/claude/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,10 @@ ${formattedMessages}${truncationNotice}\n\n---\n## Current Request\n`;
13971397
prompt: string,
13981398
options?: AgentOptions
13991399
): AsyncGenerator<AgentMessage> {
1400-
const session = this.createSession('executing');
1400+
const session = this.createSession('executing', {
1401+
id: options?.sessionId,
1402+
abortController: options?.abortController,
1403+
});
14011404
yield { type: 'session', sessionId: session.id };
14021405

14031406
const sessionCwd = getSessionWorkDir(
@@ -1701,7 +1704,10 @@ User's request (answer this AFTER reading the images):
17011704
prompt: string,
17021705
options?: PlanOptions
17031706
): AsyncGenerator<AgentMessage> {
1704-
const session = this.createSession('planning');
1707+
const session = this.createSession('planning', {
1708+
id: options?.sessionId,
1709+
abortController: options?.abortController,
1710+
});
17051711
yield { type: 'session', sessionId: session.id };
17061712

17071713
// Get session working directory
@@ -1820,7 +1826,10 @@ If you need to create any files during planning, use this directory.
18201826
* Execute an approved plan
18211827
*/
18221828
async *execute(options: ExecuteOptions): AsyncGenerator<AgentMessage> {
1823-
const session = this.createSession('executing');
1829+
const session = this.createSession('executing', {
1830+
id: options.sessionId,
1831+
abortController: options.abortController,
1832+
});
18241833
yield { type: 'session', sessionId: session.id };
18251834

18261835
// Use the plan passed in options, or fall back to local lookup

src-api/src/extensions/agent/codex/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ export class CodexAgent extends BaseAgent {
299299
prompt: string,
300300
options?: AgentOptions
301301
): AsyncGenerator<AgentMessage> {
302-
const session = this.createSession('executing');
302+
const session = this.createSession('executing', {
303+
id: options?.sessionId,
304+
abortController: options?.abortController,
305+
});
303306
yield { type: 'session', sessionId: session.id };
304307

305308
const sessionCwd = await getSessionWorkDir(
@@ -382,7 +385,10 @@ export class CodexAgent extends BaseAgent {
382385
prompt: string,
383386
options?: PlanOptions
384387
): AsyncGenerator<AgentMessage> {
385-
const session = this.createSession('planning');
388+
const session = this.createSession('planning', {
389+
id: options?.sessionId,
390+
abortController: options?.abortController,
391+
});
386392
yield { type: 'session', sessionId: session.id };
387393

388394
const sessionCwd = await getSessionWorkDir(
@@ -483,7 +489,10 @@ Please respond ONLY with JSON in this exact format, no other text:
483489
* Execute an approved plan
484490
*/
485491
async *execute(options: ExecuteOptions): AsyncGenerator<AgentMessage> {
486-
const session = this.createSession('executing');
492+
const session = this.createSession('executing', {
493+
id: options.sessionId,
494+
abortController: options.abortController,
495+
});
487496
yield { type: 'session', sessionId: session.id };
488497

489498
// Use the plan passed in options, or fall back to local lookup

src-api/src/extensions/agent/deepagents/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ export class DeepAgentsAdapter extends BaseAgent {
117117
prompt: string,
118118
options?: AgentOptions
119119
): AsyncGenerator<AgentMessage> {
120-
const session = this.createSession('executing');
120+
const session = this.createSession('executing', {
121+
id: options?.sessionId,
122+
abortController: options?.abortController,
123+
});
121124
yield { type: 'session', sessionId: session.id };
122125

123126
console.log(`[DeepAgents ${session.id}] Direct execution started`);
@@ -191,7 +194,10 @@ export class DeepAgentsAdapter extends BaseAgent {
191194
prompt: string,
192195
_options?: PlanOptions
193196
): AsyncGenerator<AgentMessage> {
194-
const session = this.createSession('planning');
197+
const session = this.createSession('planning', {
198+
id: _options?.sessionId,
199+
abortController: _options?.abortController,
200+
});
195201
yield { type: 'session', sessionId: session.id };
196202

197203
console.log(`[DeepAgents ${session.id}] Planning phase started`);
@@ -259,7 +265,10 @@ export class DeepAgentsAdapter extends BaseAgent {
259265
* Execute an approved plan
260266
*/
261267
async *execute(options: ExecuteOptions): AsyncGenerator<AgentMessage> {
262-
const session = this.createSession('executing');
268+
const session = this.createSession('executing', {
269+
id: options.sessionId,
270+
abortController: options.abortController,
271+
});
263272
yield { type: 'session', sessionId: session.id };
264273

265274
// Use the plan passed in options, or fall back to local lookup

src-api/src/shared/services/agent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type SkillsConfig,
2020
type TaskPlan,
2121
} from '@/core/agent';
22+
import { nanoid } from 'nanoid';
2223
// ============================================================================
2324
// Logging - uses shared logger (writes to ~/.workany/logs/workany.log)
2425
// ============================================================================
@@ -75,7 +76,7 @@ export function createSession(
7576
phase: 'plan' | 'execute' = 'plan'
7677
): AgentSession {
7778
const session: AgentSession = {
78-
id: Date.now().toString(),
79+
id: nanoid(),
7980
createdAt: new Date(),
8081
phase: phase === 'plan' ? 'planning' : 'executing',
8182
isAborted: false,

0 commit comments

Comments
 (0)