Skip to content

createSession()

Creates a session for multi-turn conversations with automatic history management.

Signature

typescript
function createSession(config?: SessionConfig): Session

Parameters

config (optional)

PropertyTypeRequiredDescription
sessionIdstringNoUnique session ID. Auto-generated if not provided.
storeSessionStoreNoStorage backend. Defaults to MemoryStore (in-memory).
runner(prompt, config) => Promise<AgentRun>NoCustom runner function (for testing).

Returns

A Session object:

Property/MethodTypeDescription
idstringThe session ID
run(prompt, config)Promise<AgentRun>Run with conversation history prepended
getHistory()Promise<Message[]>Retrieve conversation history
clear()Promise<void>Clear conversation history

Example

typescript
import { createSession, defineAgent } from "one-agent-sdk";

const agent = defineAgent({
  name: "assistant",
  description: "A helpful assistant",
  prompt: "You are a helpful assistant.",
});

const session = createSession();

const { stream } = await session.run("My name is Alice.", {
  provider: "claude-code",
  agent,
});

for await (const chunk of stream) {
  if (chunk.type === "text") process.stdout.write(chunk.text);
}

SessionStore Interface

Implement this interface for custom storage:

typescript
interface SessionStore {
  load(sessionId: string): Promise<Message[]>;
  save(sessionId: string, messages: Message[]): Promise<void>;
}

MemoryStore

The default in-memory store:

typescript
import { MemoryStore } from "one-agent-sdk";

const store = new MemoryStore();
const session = createSession({ store });

Message

typescript
interface Message {
  role: "user" | "assistant";
  content: string;
}

See Also