refactor: Reorganize src/ into modular folder structure
Split large monolithic files into focused modules: - cloudcode-client.js (1,107 lines) → src/cloudcode/ (9 files) - account-manager.js (639 lines) → src/account-manager/ (5 files) - Move auth files to src/auth/ (oauth, token-extractor, database) - Move CLI to src/cli/accounts.js Update all import paths and documentation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
47
src/cloudcode/session-manager.js
Normal file
47
src/cloudcode/session-manager.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Session Management for Cloud Code
|
||||
*
|
||||
* Handles session ID derivation for prompt caching continuity.
|
||||
* Session IDs are derived from the first user message to ensure
|
||||
* the same conversation uses the same session across turns.
|
||||
*/
|
||||
|
||||
import crypto from 'crypto';
|
||||
|
||||
/**
|
||||
* Derive a stable session ID from the first user message in the conversation.
|
||||
* This ensures the same conversation uses the same session ID across turns,
|
||||
* enabling prompt caching (cache is scoped to session + organization).
|
||||
*
|
||||
* @param {Object} anthropicRequest - The Anthropic-format request
|
||||
* @returns {string} A stable session ID (32 hex characters) or random UUID if no user message
|
||||
*/
|
||||
export function deriveSessionId(anthropicRequest) {
|
||||
const messages = anthropicRequest.messages || [];
|
||||
|
||||
// Find the first user message
|
||||
for (const msg of messages) {
|
||||
if (msg.role === 'user') {
|
||||
let content = '';
|
||||
|
||||
if (typeof msg.content === 'string') {
|
||||
content = msg.content;
|
||||
} else if (Array.isArray(msg.content)) {
|
||||
// Extract text from content blocks
|
||||
content = msg.content
|
||||
.filter(block => block.type === 'text' && block.text)
|
||||
.map(block => block.text)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
if (content) {
|
||||
// Hash the content with SHA256, return first 32 hex chars
|
||||
const hash = crypto.createHash('sha256').update(content).digest('hex');
|
||||
return hash.substring(0, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to random UUID if no user message found
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
Reference in New Issue
Block a user