Skip to content

feat: add preview session store functions to utils#270

Merged
WillieRuemmele merged 2 commits intomainfrom
W-22203667/preview-session-store-clean
Apr 27, 2026
Merged

feat: add preview session store functions to utils#270
WillieRuemmele merged 2 commits intomainfrom
W-22203667/preview-session-store-clean

Conversation

@andresrivas-sf
Copy link
Copy Markdown
Contributor

Summary

  • Moves all preview session store logic out of plugin-agent and into this library so the VS Code extension and the CLI share the same storage layer (per review feedback on W-22203667: add timestamp, session type, and index to agent preview sessions output salesforcecli/plugin-agent#400)
  • Adds createPreviewSessionCache, validatePreviewSession, removePreviewSessionCache, getCachedPreviewSessionIds, getCurrentPreviewSessionId, listCachedPreviewSessions to src/utils.ts
  • Introduces SessionType, PreviewSessionMeta, CachedPreviewSessionInfo, CachedPreviewSessionEntry types
  • Atomic write (temp file + rename) for the sessions index to reduce race conditions on concurrent start calls
  • All new functions exported from src/index.ts

Test plan

  • 19 new tests added in test/utils.test.ts covering create, validate, remove, list, index ordering, removal from index, and directory-scan fallback
  • All 132 unit tests pass (yarn test:only)
  • Lint clean (yarn lint)

@W-22203667@

🤖 Generated with Claude Code

W-22203667

- Add createPreviewSessionCache, validatePreviewSession, removePreviewSessionCache,
  getCachedPreviewSessionIds, getCurrentPreviewSessionId, listCachedPreviewSessions
  to utils.ts so VS Code extension and CLI share the same storage layer
- Introduce SessionType, PreviewSessionMeta, CachedPreviewSessionInfo,
  CachedPreviewSessionEntry types
- Use atomic write (temp file + rename) for the index to avoid partial writes
- Export all new functions and types from index.ts
- Add tests covering create, validate, remove, list, ordering, fallback dir scan

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread src/utils.ts
* Caller must set sessionId on the agent (agent.setSessionId) before calling.
* Throws SfError if the session marker is not found.
*/
export async function validatePreviewSession(agent: { getHistoryDir: () => Promise<string> }): Promise<void> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to update something on the Agents class so that we can just pass one in here, instead of just an object with getHistoryDir method defined?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agentBase.ts already imports from utils.ts (getHistoryDir, SessionHistoryBuffer, TranscriptEntry), so importing AgentBase back into utils.ts would create a circular dependency. The structural type ({ getHistoryDir: () => Promise<string> }) avoids the cycle and still accepts any AgentBase subclass at the call sites. Happy to restructure if you have a preferred way to break the cycle (e.g. a separate types.ts for the interface), but wanted to flag the constraint.

Comment thread src/utils.ts Outdated
try {
await readFile(metaPath, 'utf-8');
} catch {
throw new SfError(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be SfError.create()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — pushed in the latest commit.

Comment thread src/utils.ts
* Remove the session marker so this session is no longer considered "active" for send/end without --session-id.
* Call after ending the session. Caller must set sessionId on the agent before calling.
*/
export async function removePreviewSessionCache(agent: { getHistoryDir: () => Promise<string> }): Promise<void> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like a growing issue that we're putting these file manipulator methods in a utils file... we probably need to figure out a better, and more accessible way to manage all of the different files... if you want to try, that'd be great, but we can also create a new WI to consolidate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — utils.ts is getting unwieldy. I'll create a follow-up WI to consolidate the file I/O helpers into a dedicated module so this doesn't keep growing.

@andresrivas-sf andresrivas-sf changed the title W-22203667: add preview session store functions to utils feat: add preview session store functions to utils Apr 27, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
andresrivas-sf added a commit to salesforcecli/plugin-agent that referenced this pull request Apr 27, 2026
…agents

All session store logic now lives in the agents library (forcedotcom/agents#270).
This file is a thin re-export shim; the plugin-agent tests are removed since
the library ships its own test coverage.

CI will fail until agents#270 merges and the dependency is updated to the
published semver.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@WillieRuemmele WillieRuemmele merged commit b670a38 into main Apr 27, 2026
15 of 17 checks passed
@WillieRuemmele WillieRuemmele deleted the W-22203667/preview-session-store-clean branch April 27, 2026 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants