* Creates a new session and inserts a row into the database. * @param sessionId - The session identifier. * @param options - Optional agent_type and cwd (default from config). * @returns A Session instance with isNewSession: true. * @throws SessionAlreadyExistsError if the session alread
(
sessionId = uuid(),
options?: SessionResolveOptions,
)
| 100 | * @throws SessionAlreadyExistsError if the session already exists. |
| 101 | */ |
| 102 | async createSession( |
| 103 | sessionId = uuid(), |
| 104 | options?: SessionResolveOptions, |
| 105 | ): Promise<Session> { |
| 106 | if (this.existsSession(sessionId)) { |
| 107 | throw new SessionAlreadyExistsError(sessionId); |
| 108 | } |
| 109 | |
| 110 | const agentType = options?.agentType ?? config.agents.default.type; |
| 111 | const cwd = options?.cwd ?? config.paths.home; |
| 112 | const channelId = options?.channelId ?? null; |
| 113 | const now = Date.now(); |
| 114 | |
| 115 | this._db |
| 116 | .insert(sessions) |
| 117 | .values({ |
| 118 | id: sessionId, |
| 119 | agent_type: agentType, |
| 120 | cwd, |
| 121 | channel_id: channelId, |
| 122 | last_message_created_at: null, |
| 123 | runner_session_id: null, |
| 124 | created_at: now, |
| 125 | updated_at: now, |
| 126 | }) |
| 127 | .run(); |
| 128 | |
| 129 | if (options?.firstMessage) { |
| 130 | this._updateFirstMessage( |
| 131 | sessionId, |
| 132 | extractTextContent(options.firstMessage), |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | this._logger.info(`Creating session: ${sessionId}`); |
| 137 | const session = new Session(sessionId, agentType, { |
| 138 | isNewSession: true, |
| 139 | cwd, |
| 140 | runnerSessionId: undefined, |
| 141 | }); |
| 142 | this._attachWriter(session, sessionId); |
| 143 | |
| 144 | return session; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Creates a session for a handoff from an external Claude Code instance. |
no test coverage detected