Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions packages/core/src/config/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ vi.unmock('./storageMigration.js');

import * as os from 'node:os';
import * as path from 'node:path';
import * as fs from 'node:fs';

vi.mock('fs', async (importOriginal) => {
const actual = await importOriginal<typeof import('fs')>();
Expand Down Expand Up @@ -172,6 +173,82 @@ describe('Storage – additional helpers', () => {
const expected = path.join(tempDir, sessionId, 'plans');
expect(storageWithSession.getProjectTempPlansDir()).toBe(expected);
});

describe('Session and JSON Loading', () => {
beforeEach(async () => {
await storage.initialize();
});

it('listProjectChatFiles returns sorted sessions from chats directory', async () => {
const readdirSpy = vi
.spyOn(fs.promises, 'readdir')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockResolvedValue([
'session-1.json',
'session-2.json',
'not-a-session.txt',
] as any);

const statSpy = vi
.spyOn(fs.promises, 'stat')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockImplementation(async (p: any) => {
if (p.endsWith('session-1.json')) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { mtime: new Date('2026-02-01'), mtimeMs: 1000 } as any;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { mtime: new Date('2026-02-02'), mtimeMs: 2000 } as any;
});

const sessions = await storage.listProjectChatFiles();

expect(readdirSpy).toHaveBeenCalledWith(expect.stringContaining('chats'));
expect(sessions).toHaveLength(2);
// Sorted by mtime desc
expect(sessions[0].filePath).toBe(path.join('chats', 'session-2.json'));
expect(sessions[1].filePath).toBe(path.join('chats', 'session-1.json'));
expect(sessions[0].lastUpdated).toBe(
new Date('2026-02-02').toISOString(),
);

readdirSpy.mockRestore();
statSpy.mockRestore();
});

it('loadProjectTempFile loads and parses JSON from relative path', async () => {
const readFileSpy = vi
.spyOn(fs.promises, 'readFile')
.mockResolvedValue(JSON.stringify({ hello: 'world' }));

const result = await storage.loadProjectTempFile<{ hello: string }>(
'some/file.json',
);

expect(readFileSpy).toHaveBeenCalledWith(
expect.stringContaining(path.join(PROJECT_SLUG, 'some/file.json')),
'utf8',
);
expect(result).toEqual({ hello: 'world' });

readFileSpy.mockRestore();
});

it('loadProjectTempFile returns null if file does not exist', async () => {
const error = new Error('File not found');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error as any).code = 'ENOENT';
const readFileSpy = vi
.spyOn(fs.promises, 'readFile')
.mockRejectedValue(error);

const result = await storage.loadProjectTempFile('missing.json');

expect(result).toBeNull();

readFileSpy.mockRestore();
});
});
});

describe('Storage - System Paths', () => {
Expand Down
57 changes: 57 additions & 0 deletions packages/core/src/config/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,63 @@ export class Storage {
return path.join(this.getProjectTempDir(), 'tasks');
}

async listProjectChatFiles(): Promise<
Array<{ filePath: string; lastUpdated: string }>
> {
const chatsDir = path.join(this.getProjectTempDir(), 'chats');
try {
const files = await fs.promises.readdir(chatsDir);
const jsonFiles = files.filter((f) => f.endsWith('.json'));

const sessions = await Promise.all(
jsonFiles.map(async (file) => {
const absolutePath = path.join(chatsDir, file);
const stats = await fs.promises.stat(absolutePath);
return {
filePath: path.join('chats', file),
lastUpdated: stats.mtime.toISOString(),
mtimeMs: stats.mtimeMs,
};
}),
);

return sessions
.sort((a, b) => b.mtimeMs - a.mtimeMs)
.map(({ filePath, lastUpdated }) => ({ filePath, lastUpdated }));
} catch (e) {
// If directory doesn't exist, return empty
if (
e instanceof Error &&
'code' in e &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
(e as NodeJS.ErrnoException).code === 'ENOENT'
) {
return [];
}
throw e;
}
}

async loadProjectTempFile<T>(filePath: string): Promise<T | null> {
const absolutePath = path.join(this.getProjectTempDir(), filePath);
try {
const content = await fs.promises.readFile(absolutePath, 'utf8');
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return JSON.parse(content) as T;
} catch (e) {
// If file doesn't exist, return null
if (
e instanceof Error &&
'code' in e &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
(e as NodeJS.ErrnoException).code === 'ENOENT'
) {
return null;
}
throw e;
}
}

getExtensionsDir(): string {
return path.join(this.getGeminiDir(), 'extensions');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export { OAuthUtils } from './mcp/oauth-utils.js';

// Export telemetry functions
export * from './telemetry/index.js';
export { sessionId } from './utils/session.js';
export { sessionId, createSessionId } from './utils/session.js';
export * from './utils/compatibility.js';
export * from './utils/browser.js';
export { Storage } from './config/storage.js';
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
import { randomUUID } from 'node:crypto';

export const sessionId = randomUUID();

export function createSessionId(): string {
return randomUUID();
}
18 changes: 12 additions & 6 deletions packages/sdk/SDK_DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

## `Simple Example`

> **Status:** Implemented. `GeminiCliAgent` supports `cwd` and `sendStream`.
> **Status:** Implemented. `GeminiCliAgent` supports `session()` and
> `resumeSession()`.

Equivalent to `gemini -p "what does this project do?"`. Loads all workspace and
user settings.
Expand All @@ -20,10 +21,14 @@ const simpleAgent = new GeminiCliAgent({
cwd: '/path/to/some/dir',
});

for await (const chunk of simpleAgent.sendStream(
'what does this project do?',
)) {
console.log(chunk); // equivalent to JSON streaming chunks (probably?) for now
// Create a new empty session
const session = simpleAgent.session();

// Resume a specific session by ID
// const session = await simpleAgent.resumeSession('some-session-id');

for await (const chunk of session.sendStream('what does this project do?')) {
console.log(chunk); // equivalent to JSON streaming chunks
}
```

Expand Down Expand Up @@ -268,8 +273,9 @@ export interface SessionContext {
// helpers to access files and run shell commands while adhering to policies/validation
fs: AgentFilesystem;
shell: AgentShell;
// the agent itself is passed as context
// the agent and session are passed as context
agent: GeminiCliAgent;
session: GeminiCliSession;
}

export interface AgentFilesystem {
Expand Down
77 changes: 43 additions & 34 deletions packages/sdk/src/agent.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ describe('GeminiCliAgent Integration', () => {
fakeResponses: RECORD_MODE ? undefined : goldenFile,
});

const session = agent.session();
expect(session.id).toMatch(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
);
const events = [];
const stream = agent.sendStream('Say hello.');
const stream = session.sendStream('Say hello.');

for await (const event of stream) {
events.push(event);
Expand Down Expand Up @@ -60,9 +64,11 @@ describe('GeminiCliAgent Integration', () => {
fakeResponses: RECORD_MODE ? undefined : goldenFile,
});

const session = agent.session();

// First turn
const stream1 = agent.sendStream('What is the secret number?');
const events1 = [];
const stream1 = session.sendStream('What is the secret number?');
for await (const event of stream1) {
events1.push(event);
}
Expand All @@ -72,11 +78,10 @@ describe('GeminiCliAgent Integration', () => {
.join('');

expect(responseText1).toContain('1');
expect(callCount).toBe(1);

// Second turn
const stream2 = agent.sendStream('What is the secret number now?');
const events2 = [];
const stream2 = session.sendStream('What is the secret number now?');
for await (const event of stream2) {
events2.push(event);
}
Expand All @@ -85,65 +90,69 @@ describe('GeminiCliAgent Integration', () => {
.map((e) => (typeof e.value === 'string' ? e.value : ''))
.join('');

// Should still be 1 because instructions are only loaded once per session
expect(responseText2).toContain('1');
expect(callCount).toBe(1);
expect(responseText2).toContain('2');
}, 30000);

it('handles async dynamic instructions', async () => {
const goldenFile = getGoldenPath('agent-async-instructions');
it('resumes a session', async () => {
const goldenFile = getGoldenPath('agent-resume-session');

let callCount = 0;
// Create initial session
const agent = new GeminiCliAgent({
instructions: async (_ctx) => {
await new Promise((resolve) => setTimeout(resolve, 10)); // Simulate async work
callCount++;
return `You are a helpful assistant. The secret number is ${callCount}. Always mention the secret number when asked.`;
},
instructions: 'You are a memory test. Remember the word "BANANA".',
model: 'gemini-2.0-flash',
recordResponses: RECORD_MODE ? goldenFile : undefined,
fakeResponses: RECORD_MODE ? undefined : goldenFile,
});

// First turn
const stream1 = agent.sendStream('What is the secret number?');
const events1 = [];
for await (const event of stream1) {
events1.push(event);
const session1 = agent.session({ sessionId: 'resume-test-fixed-id' });
const sessionId = session1.id;
const stream1 = session1.sendStream('What is the word?');
for await (const _ of stream1) {
// consume stream
}
const responseText1 = events1
.filter((e) => e.type === 'content')
.map((e) => (typeof e.value === 'string' ? e.value : ''))
.join('');

expect(responseText1).toContain('1');
expect(callCount).toBe(1);
// Resume session
// Allow some time for async writes if any
await new Promise((resolve) => setTimeout(resolve, 500));

const session2 = await agent.resumeSession(sessionId);
expect(session2.id).toBe(sessionId);

// Second turn
const stream2 = agent.sendStream('What is the secret number now?');
const events2 = [];
const stream2 = session2.sendStream('What is the word again?');
for await (const event of stream2) {
events2.push(event);
}
const responseText2 = events2

const responseText = events2
.filter((e) => e.type === 'content')
.map((e) => (typeof e.value === 'string' ? e.value : ''))
.join('');

// Should still be 1 because instructions are only loaded once per session
expect(responseText2).toContain('1');
expect(callCount).toBe(1);
expect(responseText).toContain('BANANA');
}, 30000);

it('throws when dynamic instructions fail', async () => {
it('throws on invalid instructions', () => {
// Missing instructions should be fine
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(() => new GeminiCliAgent({} as any).session()).not.toThrow();

expect(() =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new GeminiCliAgent({ instructions: 123 as any }).session(),
).toThrow('Instructions must be a string or a function.');
});

it('propagates errors from dynamic instructions', async () => {
const agent = new GeminiCliAgent({
instructions: () => {
throw new Error('Dynamic instruction failure');
},
model: 'gemini-2.0-flash',
});

const stream = agent.sendStream('Say hello.');
const session = agent.session();
const stream = session.sendStream('Say hello.');

await expect(async () => {
for await (const _event of stream) {
Expand Down
Loading
Loading