File size: 909 Bytes
95eb75a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// S528 — estratto da agentLoop.ts: S274 Fix#6 conversationId derivation
// Calcola un namespace ID non-criptograficamente per isolare la cache per conversazione.

export interface ConvIdOptions {
  conversationId?: string | null;
}

/**
 * S528/S274-Fix6: deriva il conversationId per la cache persistente.
 * - Usa options.conversationId se fornito
 * - Altrimenti hash semplice (non-security) dei primi 64 char del primo messaggio utente
 */
export function computeConversationId(
  options: ConvIdOptions | null | undefined,
  initialMessages: Array<{ role: string; content: string }>,
): string {
  if (options?.conversationId) return options.conversationId;
  const _fu = initialMessages.find(m => m.role === "user")?.content ?? "";
  let _h = 0;
  for (let _i = 0; _i < Math.min(_fu.length, 64); _i++) {
    _h = ((_h << 5) - _h + _fu.charCodeAt(_i)) >>> 0;
  }
  return "anon-" + _h.toString(36);
}