File size: 2,325 Bytes
d613519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * 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';


// Runtime storage for session IDs (per account)
// This mimics the behavior of the binary which generates a session ID at startup
// and keeps it for the process lifetime.
// Key: accountEmail, Value: sessionId
const runtimeSessionStore = new Map();

/**
 * Get or create a session ID for the given account.
 * 
 * The binary generates a session ID once at startup: `p.sessionID = rs() + Date.now()`.
 * Since our proxy is long-running, we simulate this "per-launch" behavior by storing
 * a generated ID in memory for each account.
 *
 * - If the proxy restarts, the ID changes (matching binary/VS Code restart behavior).
 * - Within a running proxy instance, the ID is stable for that account.
 * - This enables prompt caching while using the EXACT random logic of the binary.
 *
 * @param {Object} anthropicRequest - The Anthropic-format request (unused for ID generation now)
 * @param {string} accountEmail - The account email to scope the session ID
 * @returns {string} A stable session ID string matching binary format
 */
export function deriveSessionId(anthropicRequest, accountEmail) {
    if (!accountEmail) {
        // Fallback for requests without an account (should differ every time)
        return generateBinaryStyleId();
    }

    // Check if we already have a session ID for this account in this process run
    if (runtimeSessionStore.has(accountEmail)) {
        return runtimeSessionStore.get(accountEmail);
    }

    // Generate a new ID using the binary's exact logic
    const newSessionId = generateBinaryStyleId();

    // Store it for future requests from this account
    runtimeSessionStore.set(accountEmail, newSessionId);

    return newSessionId;
}

/**
 * Generate a Session ID using the binary's exact logic.
 * logic: `rs() + Date.now()` where `rs()` is randomUUID
 */
function generateBinaryStyleId() {
    return crypto.randomUUID() + Date.now().toString();
}

/**
 * Clears all session IDs (e.g. useful for testing or explicit reset)
 */
export function clearSessionStore() {
    runtimeSessionStore.clear();
}