File size: 1,440 Bytes
e18726e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** Stable active-node identity projected into the dynamic model runtime line. */
type ActiveNodeContext = {
  nodeId: string;
  pairingGeneration?: string;
};

type ActiveNodeContextState = ActiveNodeContext & {
  isCurrent?: () => boolean;
};

let activeNodeContext: ActiveNodeContextState | null = null;

function snapshotActiveNodeContext(context: ActiveNodeContextState): ActiveNodeContext {
  return {
    nodeId: context.nodeId,
    ...(context.pairingGeneration ? { pairingGeneration: context.pairingGeneration } : {}),
  };
}

/** Publishes the gateway's current active-node choice without volatile timestamps. */
export function setActiveNodeContext(
  next: ActiveNodeContext | null,
  options?: { isCurrent?: () => boolean },
): void {
  activeNodeContext = next ? { ...next, ...options } : null;
}

/** Revalidates the published node before projecting it into an agent prompt. */
export function getCurrentActiveNodeContext(): ActiveNodeContext | null {
  if (!activeNodeContext) {
    return null;
  }
  try {
    if (activeNodeContext.isCurrent && !activeNodeContext.isCurrent()) {
      return null;
    }
  } catch {
    return null;
  }
  return snapshotActiveNodeContext(activeNodeContext);
}

/** Formats the stable authenticated id; node-controlled labels stay out of prompt text. */
export function formatActiveNodeContextLabel(
  context: ActiveNodeContext | null,
): string | undefined {
  return context?.nodeId;
}