File size: 5,146 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { AsyncLocalStorage } from "node:async_hooks";
import type { ExecutionIdentityAdmissionToken } from "../audit/execution-identity-admission.js";
import type {
  PluginHookMessageContext,
  PluginHookMessageSendingEvent,
} from "../plugins/hook-message.types.js";
import { resolveGlobalSingleton } from "../shared/global-singleton.js";
import type { TurnAdoptionLifecycle } from "./get-reply-options.types.js";
import type { FinalizedMsgContext } from "./templating.js";
import { isSilentReplyPayloadText } from "./tokens.js";
import type { ReplyPayload } from "./types.js";

export type GroupThreadParticipant = { agentId: string; name: string };
export type GroupThreadTurn = GroupThreadParticipant & {
  round: number;
  messageId: string;
  digest?: string;
};

type GroupThreadRunState = {
  runId?: string;
  executionIdentityToken?: ExecutionIdentityAdmissionToken;
};

type GroupThreadScope = {
  turn: GroupThreadTurn;
  participant?: GroupThreadParticipant;
  recordReply: (payload: ReplyPayload) => void;
  formatReply?: (text: string, participant: GroupThreadParticipant) => string;
  adopt?: (lifecycle: TurnAdoptionLifecycle) => Promise<void>;
  dispatch?: { ctx: FinalizedMsgContext; runState: GroupThreadRunState };
};

const turns = resolveGlobalSingleton(
  Symbol.for("openclaw.groupThreadContext"),
  () => new AsyncLocalStorage<GroupThreadScope & { active: boolean }>(),
);

type ToolReplySource = {
  matches: (event: PluginHookMessageSendingEvent, context: PluginHookMessageContext) => boolean;
  format: (text: string) => string;
};
type ToolReplyCapture = {
  active: boolean;
  observed: boolean;
  text?: string;
  source?: ToolReplySource;
};
const toolReplies = resolveGlobalSingleton(
  Symbol.for("openclaw.groupThreadToolReplies"),
  () => new AsyncLocalStorage<ToolReplyCapture>(),
);

export async function captureGroupThreadToolReply<T>(
  run: () => Promise<T>,
  source?: ToolReplySource,
): Promise<{ result: T; observed: boolean; text?: string }> {
  if (!currentScope()) {
    return { result: await run(), observed: false };
  }
  const capture: ToolReplyCapture = { active: true, observed: false, source };
  return toolReplies.run(capture, async () => {
    try {
      const result = await run();
      return { result, observed: capture.observed, text: capture.text };
    } finally {
      capture.active = false;
    }
  });
}

/** Restore source attribution after modifying hooks; undefined preserves cancellation. */
export function finalizeGroupThreadToolReply(
  text: string | undefined,
  event: PluginHookMessageSendingEvent,
  context: PluginHookMessageContext,
): string | undefined {
  const capture = toolReplies.getStore();
  let content = text;
  if (
    capture?.active &&
    currentScope() &&
    (!capture.source || capture.source.matches(event, context))
  ) {
    if (content?.trim() && !isSilentReplyPayloadText(content)) {
      content = capture.source?.format(content) ?? content;
    }
    capture.observed = true;
    capture.text = content?.slice(0, 4_000);
  }
  return content;
}

export function withGroupThreadTurn<T>(scope: GroupThreadScope, run: () => Promise<T>): Promise<T> {
  const owned = { ...scope, active: true };
  return turns.run(owned, async () => {
    try {
      return await run();
    } finally {
      owned.active = false;
    }
  });
}

function currentScope(): GroupThreadScope | undefined {
  const scope = turns.getStore();
  return scope?.active ? scope : undefined;
}

export function getGroupThreadTurn(): GroupThreadTurn | undefined {
  return currentScope()?.turn;
}

export function getGroupThreadParticipant(): GroupThreadParticipant | undefined {
  return currentScope()?.participant;
}

export async function adoptGroupThreadRoot(
  lifecycle: TurnAdoptionLifecycle | undefined,
): Promise<void> {
  if (lifecycle) {
    await currentScope()?.adopt?.(lifecycle);
  }
}

export function bindGroupThreadDispatchContext(ctx: FinalizedMsgContext): GroupThreadRunState {
  const runState: GroupThreadRunState = {};
  const scope = currentScope();
  if (scope) {
    scope.dispatch = { ctx, runState };
  }
  return runState;
}

export function getGroupThreadDispatchContext(): GroupThreadScope["dispatch"] {
  return currentScope()?.dispatch;
}

/** Data-only delivery identity, including unlabeled single-participant groups. */
export function getGroupThreadDeliverySession():
  | { agentId: string; sessionKey: string }
  | undefined {
  const ctx = currentScope()?.dispatch?.ctx;
  return ctx?.AgentId && ctx.SessionKey
    ? { agentId: ctx.AgentId, sessionKey: ctx.SessionKey }
    : undefined;
}

export function recordGroupThreadReply(payload: ReplyPayload): void {
  currentScope()?.recordReply(payload);
}

/** Message actions use the initiating channel's encoder, without changing delivery ownership. */
export function formatGroupThreadReply(text: string): string {
  const scope = currentScope();
  if (!scope?.participant || !scope.formatReply) {
    return text;
  }
  const label = scope.formatReply("", scope.participant);
  return label && text.startsWith(label) ? text : scope.formatReply(text, scope.participant);
}