@openclaw/copilot 2026.9.4 → 2026.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/activity.svg +30 -0
- package/assets/icon.png +0 -0
- package/dist/{attempt-EPgLnjoV.js → .setup/attempt-DKgdMTpc.mjs} +240 -233
- package/dist/{harness-C_65tSNm.js → .setup/harness-BH2_ctMF.mjs} +105 -75
- package/dist/{isolated-completion-YG0Z8FkF.js → .setup/isolated-completion-CpLMQyd3.mjs} +2 -2
- package/dist/{runtime-LtZ5NAjV.js → .setup/runtime-D6DFeSZX.mjs} +2 -2
- package/dist/harness.js +1 -1
- package/dist/index.js +2 -2
- package/openclaw.plugin.json +9 -1
- package/package.json +8 -6
- /package/dist/{usage-bridge-Cpa-ZYwL.js → .setup/usage-bridge-Cpa-ZYwL.mjs} +0 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { a as resolveCopilotAuth, i as createCopilotByokAuth, n as resolveCopilotProvider, r as createCopilotByokProxy } from "./harness-
|
|
2
|
-
import { n as normalizeCopilotUsage, r as createCopilotIsolatedSessionRestrictions, t as buildCopilotAssistantUsage } from "./usage-bridge-Cpa-ZYwL.
|
|
3
|
-
import { asNonArrayRecord,
|
|
4
|
-
import { SKILL_WORKSHOP_TOOL_NAME, applyEmbeddedAttemptToolsAllow, awaitAgentEndSideEffects, buildAgentHookContextChannelFields, buildCredentialSafetyPrompt, buildDelegationGuidanceSection, buildEmbeddedAttemptToolRunContext, buildEmbeddedForegroundPromptContext, buildHarnessVisibleReplyGuidance, buildSkillWorkshopPromptSection, cancelPendingAgentQuestionForSession, claimPendingAgentQuestionAnswer, clearActiveEmbeddedRun, detectAndLoadAgentHarnessPromptImages, embeddedAgentLog, extractToolErrorMessage, getModelProviderRequestTransport, getPluginToolMeta, getPluginToolSideEffectOwnerKey, isHostScopedAgentToolActive, isSubagentSessionKey, isToolResultError, projectAgentHarnessTranscriptMessageForDisplay, resolveAgentDir, resolveAgentHarnessBeforePromptBuildResult, resolveAttemptFsWorkspaceOnly, resolveAttemptSpawnWorkspaceDir, resolveBootstrapContextForRun, resolveCompactionTimeoutMs, resolveEmbeddedAttemptToolConstructionPlan, resolveMainSessionDelegationMode, resolveModelAuthMode, resolveSandboxContext, resolveUserPath, restorePreparedUserTurnOperationalMetaForRuntime, runAgentEndSideEffects, runAgentHarnessAfterCompactionHook, runAgentHarnessAfterToolCallHook, runAgentHarnessBeforeCompactionHook, runAgentHarnessBeforeMessageWriteHook, runAgentHarnessGatewayQuestion, runAgentHarnessLlmInputHook, runAgentHarnessLlmOutputHook, sanitizeToolResult, setActiveEmbeddedRun } from "openclaw/plugin-sdk/agent-harness-runtime";
|
|
1
|
+
import { a as resolveCopilotAuth, i as createCopilotByokAuth, n as resolveCopilotProvider, r as createCopilotByokProxy } from "./harness-BH2_ctMF.mjs";
|
|
2
|
+
import { n as normalizeCopilotUsage, r as createCopilotIsolatedSessionRestrictions, t as buildCopilotAssistantUsage } from "./usage-bridge-Cpa-ZYwL.mjs";
|
|
3
|
+
import { asNonArrayRecord, normalizeOptionalString, normalizeUniqueStringEntries, readNonBlankString, readNonEmptyStringPreservingWhitespace, readNonEmptyStringPreservingWhitespace as readNonEmptyString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
4
|
+
import { SKILL_WORKSHOP_TOOL_NAME, applyEmbeddedAttemptToolsAllow, awaitAgentEndSideEffects, buildAgentHookContextChannelFields, buildCredentialSafetyPrompt, buildDelegationGuidanceSection, buildEmbeddedAttemptToolRunContext, buildEmbeddedForegroundPromptContext, buildHarnessVisibleReplyGuidance, buildSkillWorkshopPromptSection, cancelPendingAgentQuestionForSession, claimPendingAgentQuestionAnswer, clearActiveEmbeddedRun, detectAndLoadAgentHarnessPromptImages, embeddedAgentLog, emitAgentEvent, extractToolErrorMessage, getModelProviderRequestTransport, getPluginToolMeta, getPluginToolSideEffectOwnerKey, isHostScopedAgentToolActive, isSubagentSessionKey, isToolResultError, projectAgentHarnessTranscriptMessageForDisplay, projectAgentToolActivity, resolveAgentDir, resolveAgentHarnessBeforePromptBuildResult, resolveAttemptFsWorkspaceOnly, resolveAttemptSpawnWorkspaceDir, resolveBootstrapContextForRun, resolveCompactionTimeoutMs, resolveEmbeddedAttemptToolConstructionPlan, resolveMainSessionDelegationMode, resolveModelAuthMode, resolveSandboxContext, resolveUserPath, restorePreparedUserTurnOperationalMetaForRuntime, runAgentEndSideEffects, runAgentHarnessAfterCompactionHook, runAgentHarnessAfterToolCallHook, runAgentHarnessBeforeCompactionHook, runAgentHarnessBeforeMessageWriteHook, runAgentHarnessGatewayQuestion, runAgentHarnessLlmInputHook, runAgentHarnessLlmOutputHook, sanitizeToolResult, setActiveEmbeddedRun } from "openclaw/plugin-sdk/agent-harness-runtime";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { formatErrorMessage, toErrorObject, toStringifiedError, toStringifiedError as toCopilotError } from "openclaw/plugin-sdk/error-runtime";
|
|
7
7
|
import { parseDateStringTimestampMs } from "openclaw/plugin-sdk/number-runtime";
|
|
8
|
+
import { createAgentHarnessToolSurfaceRuntime, normalizeAcceptedSessionSpawnResult } from "openclaw/plugin-sdk/agent-harness-tool-runtime";
|
|
8
9
|
import fsp from "node:fs/promises";
|
|
9
10
|
import { resolveSessionAgentIdsStrict } from "openclaw/plugin-sdk/agent-scope-runtime";
|
|
10
11
|
import { convertMcpCallToolResult } from "@github/copilot-sdk";
|
|
11
|
-
import { createAgentHarnessToolSurfaceRuntime } from "openclaw/plugin-sdk/agent-harness-tool-runtime";
|
|
12
12
|
import { isDeepStrictEqual } from "node:util";
|
|
13
13
|
import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
|
|
14
14
|
import { appendSessionTranscriptMessageByIdentityStrict, appendSessionTranscriptMessagesByIdentity, publishSessionTranscriptUpdateByIdentity, readVisibleSessionTranscriptMessageEntries } from "openclaw/plugin-sdk/session-transcript-runtime";
|
|
@@ -565,6 +565,69 @@ function buildAssistantMessage(params) {
|
|
|
565
565
|
})
|
|
566
566
|
};
|
|
567
567
|
}
|
|
568
|
+
function buildAssistantProjectionGroup(group, modelRef, resolveTimestamp, usageByApiCallId, latestUsage, forTranscript) {
|
|
569
|
+
const messages = group.chunks.flatMap((chunk) => {
|
|
570
|
+
const message = buildAssistantMessage({
|
|
571
|
+
event: chunk.event,
|
|
572
|
+
modelRef,
|
|
573
|
+
now: () => resolveTimestamp(chunk.event),
|
|
574
|
+
reasoningText: forTranscript ? chunk.transcriptReasoningText : chunk.reasoningText,
|
|
575
|
+
usage: resolveAssistantUsage(chunk.event, latestUsage, usageByApiCallId),
|
|
576
|
+
assistantTexts: forTranscript ? chunk.transcriptAssistantTexts : chunk.assistantTexts
|
|
577
|
+
});
|
|
578
|
+
return message ? [message] : [];
|
|
579
|
+
});
|
|
580
|
+
const replayIncomplete = group.chunks.some(({ event }) => hasUnprojectedAssistantReplayState(event));
|
|
581
|
+
const last = messages.at(-1);
|
|
582
|
+
if (!last) return {
|
|
583
|
+
message: void 0,
|
|
584
|
+
replayIncomplete,
|
|
585
|
+
toolCallIds: []
|
|
586
|
+
};
|
|
587
|
+
const narrative = [];
|
|
588
|
+
let terminalThinking;
|
|
589
|
+
const toolCallOrder = [];
|
|
590
|
+
const toolCallsById = /* @__PURE__ */ new Map();
|
|
591
|
+
for (const message of messages) for (const part of message.content) {
|
|
592
|
+
if (part.type === "toolCall") {
|
|
593
|
+
if (!toolCallsById.has(part.id)) toolCallOrder.push(part.id);
|
|
594
|
+
toolCallsById.set(part.id, part);
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
if (part.type === "thinking") {
|
|
598
|
+
terminalThinking = part;
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
const previous = narrative.at(-1);
|
|
602
|
+
if (part.type === "text" && previous?.type === "text") narrative[narrative.length - 1] = {
|
|
603
|
+
...previous,
|
|
604
|
+
text: previous.text + part.text
|
|
605
|
+
};
|
|
606
|
+
else narrative.push(part);
|
|
607
|
+
}
|
|
608
|
+
const toolCalls = toolCallOrder.flatMap((id) => {
|
|
609
|
+
const toolCall = toolCallsById.get(id);
|
|
610
|
+
return toolCall ? [toolCall] : [];
|
|
611
|
+
});
|
|
612
|
+
const content = [
|
|
613
|
+
...terminalThinking ? [terminalThinking] : [],
|
|
614
|
+
...narrative,
|
|
615
|
+
...toolCalls
|
|
616
|
+
];
|
|
617
|
+
const toolCallIds = [...toolCallOrder];
|
|
618
|
+
return {
|
|
619
|
+
message: {
|
|
620
|
+
...last,
|
|
621
|
+
content,
|
|
622
|
+
stopReason: toolCallIds.length > 0 ? "toolUse" : "stop"
|
|
623
|
+
},
|
|
624
|
+
replayIncomplete,
|
|
625
|
+
toolCallIds
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
function hasUnprojectedAssistantReplayState(event) {
|
|
629
|
+
return event.data.citations !== void 0 || event.data.serverTools !== void 0 || event.data.reasoningWireField !== void 0 || event.data.reasoningOpaque !== void 0 || event.data.encryptedContent !== void 0 || event.data.toolRequests?.some((request) => request.type === "custom") === true;
|
|
630
|
+
}
|
|
568
631
|
function resolveAssistantUsage(event, latest, byApiCallId) {
|
|
569
632
|
const apiCallId = readNonEmptyStringPreservingWhitespace(event?.data.apiCallId);
|
|
570
633
|
return apiCallId ? byApiCallId.get(apiCallId) ?? latest : latest;
|
|
@@ -644,6 +707,8 @@ function attachEventBridge(session, options) {
|
|
|
644
707
|
let streamError;
|
|
645
708
|
const toolMetas = [];
|
|
646
709
|
const toolMetaIndexByCallId = /* @__PURE__ */ new Map();
|
|
710
|
+
const toolCallsById = /* @__PURE__ */ new Map();
|
|
711
|
+
const preparedToolCompletions = /* @__PURE__ */ new Set();
|
|
647
712
|
const projectedToolNamesByCallId = /* @__PURE__ */ new Map();
|
|
648
713
|
const userRequestedToolCallIds = /* @__PURE__ */ new Set();
|
|
649
714
|
let startedCount = 0;
|
|
@@ -766,12 +831,38 @@ function attachEventBridge(session, options) {
|
|
|
766
831
|
if (isRootSessionEvent(event)) startedCount += 1;
|
|
767
832
|
toolMetaIndexByCallId.set(event.data.toolCallId, toolMetas.length);
|
|
768
833
|
toolMetas.push({ toolName: event.data.toolName });
|
|
834
|
+
toolCallsById.set(event.data.toolCallId, {
|
|
835
|
+
args: event.data.arguments,
|
|
836
|
+
root: isRootSessionEvent(event)
|
|
837
|
+
});
|
|
838
|
+
if (isRootSessionEvent(event)) enqueueAgentEvent({
|
|
839
|
+
stream: "item",
|
|
840
|
+
data: projectAgentToolActivity({
|
|
841
|
+
toolCallId: event.data.toolCallId,
|
|
842
|
+
name: event.data.toolName,
|
|
843
|
+
phase: "start",
|
|
844
|
+
args: event.data.arguments
|
|
845
|
+
})
|
|
846
|
+
});
|
|
769
847
|
});
|
|
770
848
|
registerListener(session, unsubscribeFns, "tool.execution_complete", (event) => {
|
|
771
849
|
flushPendingAssistantProjectionForToolCall(event.data.toolCallId);
|
|
772
850
|
if (isRootSessionEvent(event)) completedCount += 1;
|
|
773
851
|
const toolMetaIndex = toolMetaIndexByCallId.get(event.data.toolCallId);
|
|
774
852
|
const toolName = toolMetaIndex === void 0 ? void 0 : toolMetas[toolMetaIndex]?.toolName;
|
|
853
|
+
const args = toolCallsById.get(event.data.toolCallId)?.args;
|
|
854
|
+
toolCallsById.delete(event.data.toolCallId);
|
|
855
|
+
const completionPrepared = preparedToolCompletions.delete(event.data.toolCallId);
|
|
856
|
+
if (toolName && isRootSessionEvent(event) && !completionPrepared) enqueueAgentEvent({
|
|
857
|
+
stream: "item",
|
|
858
|
+
data: projectAgentToolActivity({
|
|
859
|
+
toolCallId: event.data.toolCallId,
|
|
860
|
+
name: toolName,
|
|
861
|
+
phase: "result",
|
|
862
|
+
args,
|
|
863
|
+
isError: !event.data.success
|
|
864
|
+
})
|
|
865
|
+
});
|
|
775
866
|
const meta = event.data.success ? event.data.result?.detailedContent ?? event.data.result?.content : event.data.error?.message;
|
|
776
867
|
if (toolName && toolMetaIndex !== void 0) toolMetas[toolMetaIndex] = {
|
|
777
868
|
...meta ? { meta } : {},
|
|
@@ -975,6 +1066,19 @@ function attachEventBridge(session, options) {
|
|
|
975
1066
|
finalizeAssistantTexts() {
|
|
976
1067
|
return finalizeAssistantTexts(messageOrder, messagesById, lastAssistantEvent);
|
|
977
1068
|
},
|
|
1069
|
+
completeTool(tool) {
|
|
1070
|
+
const owner = toolCallsById.get(tool.parentToolCallId ?? tool.toolCallId);
|
|
1071
|
+
if (detached || !owner?.root) return;
|
|
1072
|
+
if (!tool.parentToolCallId) preparedToolCompletions.add(tool.toolCallId);
|
|
1073
|
+
enqueueAgentEvent({
|
|
1074
|
+
stream: "item",
|
|
1075
|
+
data: projectAgentToolActivity({
|
|
1076
|
+
...tool,
|
|
1077
|
+
name: tool.toolName,
|
|
1078
|
+
phase: "result"
|
|
1079
|
+
})
|
|
1080
|
+
});
|
|
1081
|
+
},
|
|
978
1082
|
detach() {
|
|
979
1083
|
if (detached) return;
|
|
980
1084
|
detached = true;
|
|
@@ -1072,6 +1176,11 @@ function attachEventBridge(session, options) {
|
|
|
1072
1176
|
compactionChain = compactionChain.then(callback, callback).catch(() => void 0);
|
|
1073
1177
|
}
|
|
1074
1178
|
function enqueueAgentEvent(event) {
|
|
1179
|
+
if (options.runId) emitAgentEvent({
|
|
1180
|
+
runId: options.runId,
|
|
1181
|
+
sessionKey: options.sessionKey,
|
|
1182
|
+
...event
|
|
1183
|
+
});
|
|
1075
1184
|
const callback = options.onAgentEvent;
|
|
1076
1185
|
if (!callback) return;
|
|
1077
1186
|
const invoke = () => callback(event);
|
|
@@ -1114,69 +1223,6 @@ function finalizeAssistantTexts(messageOrder, messagesById, event) {
|
|
|
1114
1223
|
if (event?.data.content) return [event.data.content];
|
|
1115
1224
|
return [];
|
|
1116
1225
|
}
|
|
1117
|
-
function buildAssistantProjectionGroup(group, modelRef, resolveTimestamp, usageByApiCallId, latestUsage, forTranscript) {
|
|
1118
|
-
const messages = group.chunks.flatMap((chunk) => {
|
|
1119
|
-
const message = buildAssistantMessage({
|
|
1120
|
-
event: chunk.event,
|
|
1121
|
-
modelRef,
|
|
1122
|
-
now: () => resolveTimestamp(chunk.event),
|
|
1123
|
-
reasoningText: forTranscript ? chunk.transcriptReasoningText : chunk.reasoningText,
|
|
1124
|
-
usage: resolveAssistantUsage(chunk.event, latestUsage, usageByApiCallId),
|
|
1125
|
-
assistantTexts: forTranscript ? chunk.transcriptAssistantTexts : chunk.assistantTexts
|
|
1126
|
-
});
|
|
1127
|
-
return message ? [message] : [];
|
|
1128
|
-
});
|
|
1129
|
-
const replayIncomplete = group.chunks.some(({ event }) => hasUnprojectedAssistantReplayState(event));
|
|
1130
|
-
const last = messages.at(-1);
|
|
1131
|
-
if (!last) return {
|
|
1132
|
-
message: void 0,
|
|
1133
|
-
replayIncomplete,
|
|
1134
|
-
toolCallIds: []
|
|
1135
|
-
};
|
|
1136
|
-
const narrative = [];
|
|
1137
|
-
let terminalThinking;
|
|
1138
|
-
const toolCallOrder = [];
|
|
1139
|
-
const toolCallsById = /* @__PURE__ */ new Map();
|
|
1140
|
-
for (const message of messages) for (const part of message.content) {
|
|
1141
|
-
if (part.type === "toolCall") {
|
|
1142
|
-
if (!toolCallsById.has(part.id)) toolCallOrder.push(part.id);
|
|
1143
|
-
toolCallsById.set(part.id, part);
|
|
1144
|
-
continue;
|
|
1145
|
-
}
|
|
1146
|
-
if (part.type === "thinking") {
|
|
1147
|
-
terminalThinking = part;
|
|
1148
|
-
continue;
|
|
1149
|
-
}
|
|
1150
|
-
const previous = narrative.at(-1);
|
|
1151
|
-
if (part.type === "text" && previous?.type === "text") narrative[narrative.length - 1] = {
|
|
1152
|
-
...previous,
|
|
1153
|
-
text: previous.text + part.text
|
|
1154
|
-
};
|
|
1155
|
-
else narrative.push(part);
|
|
1156
|
-
}
|
|
1157
|
-
const toolCalls = toolCallOrder.flatMap((id) => {
|
|
1158
|
-
const toolCall = toolCallsById.get(id);
|
|
1159
|
-
return toolCall ? [toolCall] : [];
|
|
1160
|
-
});
|
|
1161
|
-
const content = [
|
|
1162
|
-
...terminalThinking ? [terminalThinking] : [],
|
|
1163
|
-
...narrative,
|
|
1164
|
-
...toolCalls
|
|
1165
|
-
];
|
|
1166
|
-
const toolCallIds = [...toolCallOrder];
|
|
1167
|
-
return {
|
|
1168
|
-
message: {
|
|
1169
|
-
...last,
|
|
1170
|
-
content,
|
|
1171
|
-
stopReason: toolCallIds.length > 0 ? "toolUse" : "stop"
|
|
1172
|
-
},
|
|
1173
|
-
replayIncomplete,
|
|
1174
|
-
toolCallIds
|
|
1175
|
-
};
|
|
1176
|
-
}
|
|
1177
|
-
function hasUnprojectedAssistantReplayState(event) {
|
|
1178
|
-
return event.data.citations !== void 0 || event.data.serverTools !== void 0 || event.data.reasoningWireField !== void 0 || event.data.reasoningOpaque !== void 0 || event.data.encryptedContent !== void 0 || event.data.toolRequests?.some((request) => request.type === "custom") === true;
|
|
1179
|
-
}
|
|
1180
1226
|
function isAssistantMessageEvent(event) {
|
|
1181
1227
|
return event?.type === "assistant.message";
|
|
1182
1228
|
}
|
|
@@ -1224,6 +1270,7 @@ async function finalizeCopilotAttempt(params, result, ctx, attemptStartedAt, now
|
|
|
1224
1270
|
function deferBackgroundCompactionCleanup(params) {
|
|
1225
1271
|
return (async () => {
|
|
1226
1272
|
let outcome = "deadline";
|
|
1273
|
+
let finalizationError;
|
|
1227
1274
|
try {
|
|
1228
1275
|
outcome = await awaitDeferredCleanupBeforeDeadline({
|
|
1229
1276
|
abortSignal: params.abortSignal,
|
|
@@ -1236,7 +1283,11 @@ function deferBackgroundCompactionCleanup(params) {
|
|
|
1236
1283
|
await cancelBackgroundCompactionBeforeTeardown(params.session);
|
|
1237
1284
|
params.bridge.settleCompactionWait();
|
|
1238
1285
|
}
|
|
1239
|
-
|
|
1286
|
+
try {
|
|
1287
|
+
params.finalizeNativeSubagents?.();
|
|
1288
|
+
} catch (error) {
|
|
1289
|
+
finalizationError = toCopilotError(error);
|
|
1290
|
+
}
|
|
1240
1291
|
params.bridge.detach();
|
|
1241
1292
|
try {
|
|
1242
1293
|
await params.session.disconnect();
|
|
@@ -1250,6 +1301,7 @@ function deferBackgroundCompactionCleanup(params) {
|
|
|
1250
1301
|
await params.pool.release(params.handle);
|
|
1251
1302
|
} catch {}
|
|
1252
1303
|
}
|
|
1304
|
+
if (finalizationError) throw finalizationError;
|
|
1253
1305
|
return outcome;
|
|
1254
1306
|
})();
|
|
1255
1307
|
}
|
|
@@ -1397,6 +1449,49 @@ async function waitForPersistenceReceipt(receipt, requestedTimeoutMs) {
|
|
|
1397
1449
|
}
|
|
1398
1450
|
}
|
|
1399
1451
|
//#endregion
|
|
1452
|
+
//#region extensions/copilot/src/attempt-transcript-replay.ts
|
|
1453
|
+
function userText(content) {
|
|
1454
|
+
if (typeof content === "string") return content;
|
|
1455
|
+
if (Array.isArray(content) && content.length === 1) {
|
|
1456
|
+
const part = content[0];
|
|
1457
|
+
if (part?.type === "text" && typeof part.text === "string") return part.text;
|
|
1458
|
+
}
|
|
1459
|
+
return JSON.stringify(content) ?? "";
|
|
1460
|
+
}
|
|
1461
|
+
function readAssistantToolCallIds(message) {
|
|
1462
|
+
return message.role === "assistant" ? message.content.flatMap((part) => part.type === "toolCall" ? [part.id] : []) : [];
|
|
1463
|
+
}
|
|
1464
|
+
function isCompatibleSingletonRewrite(original, prepared) {
|
|
1465
|
+
return original.role === prepared.role && (original.role !== "assistant" || JSON.stringify(readAssistantToolCallIds(original)) === JSON.stringify(readAssistantToolCallIds(prepared)));
|
|
1466
|
+
}
|
|
1467
|
+
function projectReplayPayload(message) {
|
|
1468
|
+
switch (message.role) {
|
|
1469
|
+
case "user": return {
|
|
1470
|
+
role: message.role,
|
|
1471
|
+
content: message.content
|
|
1472
|
+
};
|
|
1473
|
+
case "assistant": return {
|
|
1474
|
+
role: message.role,
|
|
1475
|
+
content: message.content,
|
|
1476
|
+
api: message.api,
|
|
1477
|
+
model: message.model,
|
|
1478
|
+
provider: message.provider,
|
|
1479
|
+
stopReason: message.stopReason
|
|
1480
|
+
};
|
|
1481
|
+
case "toolResult": return {
|
|
1482
|
+
role: message.role,
|
|
1483
|
+
content: message.content,
|
|
1484
|
+
isError: message.isError,
|
|
1485
|
+
toolCallId: message.toolCallId,
|
|
1486
|
+
toolName: message.toolName
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
function isCompleteToolGroup(messages, order) {
|
|
1491
|
+
const [assistant, ...results] = messages;
|
|
1492
|
+
return assistant?.role === "assistant" && JSON.stringify(readAssistantToolCallIds(assistant)) === JSON.stringify(order) && results.length === order.length && results.every((message, index) => message.role === "toolResult" && message.toolCallId === order[index]);
|
|
1493
|
+
}
|
|
1494
|
+
//#endregion
|
|
1400
1495
|
//#region extensions/copilot/src/attempt-finalize.ts
|
|
1401
1496
|
async function completeCopilotAttempt(params) {
|
|
1402
1497
|
const { acceptedSessionSpawns, aborted, attemptStartedAt, bridge, codeModeEngaged, downgradedFromResume, externalAbort, hookContext, hookContextWindowFields, input, lastToolError, messages, nativeSessionHistoryUnvalidated, transcriptJournal, modelRef, now, promptError, releaseError, resumeFailureRecovered, sdkSessionId, sentTurnStarted, settledFinalizationAssistantCompleted, settledToolFinalization, timedOut, timedOutDuringCompaction, yieldDetected, yieldAcknowledgment } = params;
|
|
@@ -1489,15 +1584,7 @@ function isSamePreparedUser(candidate, prepared, currentRunUserKey) {
|
|
|
1489
1584
|
if (typeof candidateKey === "string" && typeof preparedKey === "string") return candidateKey === preparedKey;
|
|
1490
1585
|
if (typeof candidateKey !== "string" || typeof preparedKey === "string" || !candidateKey.startsWith("copilot:") && candidateKey !== currentRunUserKey) return false;
|
|
1491
1586
|
}
|
|
1492
|
-
return candidate.timestamp === prepared.timestamp && userText
|
|
1493
|
-
}
|
|
1494
|
-
function userText$1(content) {
|
|
1495
|
-
if (typeof content === "string") return content;
|
|
1496
|
-
if (Array.isArray(content) && content.length === 1) {
|
|
1497
|
-
const part = content[0];
|
|
1498
|
-
if (part?.type === "text" && typeof part.text === "string") return part.text;
|
|
1499
|
-
}
|
|
1500
|
-
return JSON.stringify(content) ?? "";
|
|
1587
|
+
return candidate.timestamp === prepared.timestamp && userText(candidate.content) === userText(prepared.content);
|
|
1501
1588
|
}
|
|
1502
1589
|
//#endregion
|
|
1503
1590
|
//#region extensions/copilot/src/attempt-mode.ts
|
|
@@ -1511,16 +1598,6 @@ const EMPTY_PROMPT_TOOL_POLICY = { apply: () => ({
|
|
|
1511
1598
|
callableToolNames: []
|
|
1512
1599
|
}) };
|
|
1513
1600
|
const SUPPORTED_TOOL_PROVIDERS = /* @__PURE__ */ new Set(["github-copilot"]);
|
|
1514
|
-
const BASE_COPILOT_CODING_TOOL_NAMES = /* @__PURE__ */ new Set([
|
|
1515
|
-
"edit",
|
|
1516
|
-
"read",
|
|
1517
|
-
"write"
|
|
1518
|
-
]);
|
|
1519
|
-
const SHELL_COPILOT_CODING_TOOL_NAMES = /* @__PURE__ */ new Set([
|
|
1520
|
-
"apply_patch",
|
|
1521
|
-
"exec",
|
|
1522
|
-
"process"
|
|
1523
|
-
]);
|
|
1524
1601
|
async function createCopilotToolBridge(input) {
|
|
1525
1602
|
if (!input.allowModelTools && !SUPPORTED_TOOL_PROVIDERS.has(input.modelProvider)) return {
|
|
1526
1603
|
codeModeEngaged: false,
|
|
@@ -1542,9 +1619,8 @@ async function createCopilotToolBridge(input) {
|
|
|
1542
1619
|
promptToolPolicy: EMPTY_PROMPT_TOOL_POLICY,
|
|
1543
1620
|
sourceTools: []
|
|
1544
1621
|
};
|
|
1545
|
-
const createOpenClawCodingTools = input.createOpenClawCodingTools ?? (await import("openclaw/plugin-sdk/agent-harness")).createOpenClawCodingTools;
|
|
1546
1622
|
const toolSurfaceRuntime = createAgentHarnessToolSurfaceRuntime({
|
|
1547
|
-
abortSignal:
|
|
1623
|
+
abortSignal: attemptParams.abortSignal,
|
|
1548
1624
|
agentId: attemptParams.sandboxAgentId ?? input.agentId,
|
|
1549
1625
|
config: attemptParams.config,
|
|
1550
1626
|
codeModeOverride: attemptParams.codeModeOverride,
|
|
@@ -1557,11 +1633,12 @@ async function createCopilotToolBridge(input) {
|
|
|
1557
1633
|
modelId: input.modelId,
|
|
1558
1634
|
modelProvider: input.modelProvider,
|
|
1559
1635
|
modelToolsEnabled: true,
|
|
1636
|
+
supportsDeferredToolCalls: false,
|
|
1560
1637
|
prompt: attemptParams.prompt,
|
|
1561
1638
|
runId: attemptParams.runId,
|
|
1562
1639
|
runtimeToolAllowlist: toolPlan.runtimeToolAllowlist,
|
|
1563
1640
|
sessionId: input.sessionId,
|
|
1564
|
-
sessionKey: attemptParams.sandboxSessionKey ?? attemptParams.sessionKey
|
|
1641
|
+
sessionKey: attemptParams.sandboxSessionKey ?? attemptParams.sessionKey,
|
|
1565
1642
|
scheduledToolPolicy: attemptParams.scheduledToolPolicy,
|
|
1566
1643
|
sourceReplyDeliveryMode: attemptParams.sourceReplyDeliveryMode,
|
|
1567
1644
|
toolsAllow: attemptParams.toolsAllow
|
|
@@ -1570,23 +1647,16 @@ async function createCopilotToolBridge(input) {
|
|
|
1570
1647
|
...toolPlan,
|
|
1571
1648
|
runtimeToolAllowlist: toolSurfaceRuntime.runtimeToolAllowlist
|
|
1572
1649
|
}, toolSurfaceRuntime);
|
|
1573
|
-
let sourceTools;
|
|
1574
|
-
const boundSourceTools = /* @__PURE__ */ new Set();
|
|
1575
1650
|
const hostCapabilities = attemptParams.hostCapabilities;
|
|
1576
1651
|
if (!hostCapabilities) throw new Error("Copilot attempt tools require host-bound capabilities.");
|
|
1577
1652
|
const bindingCwd = toolOptions.cwd ?? toolOptions.workspaceDir;
|
|
1578
1653
|
const bindingOptions = bindingCwd ? { cwd: bindingCwd } : void 0;
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
} catch (error) {
|
|
1586
|
-
throw createError(`[copilot-tool-bridge] createOpenClawCodingTools failed: ${toStringifiedError(error).message}`, error);
|
|
1587
|
-
}
|
|
1588
|
-
const plannedSourceTools = filterCopilotToolsForConstructionPlan(applyEmbeddedAttemptToolsAllow(sourceTools, toolSurfaceRuntime.runtimeToolAllowlist, { toolMeta: (tool) => getPluginToolMeta(tool) ?? readInlinePluginToolMeta(tool) }), toolPlan.codingToolConstructionPlan, { preserveToolNames: toolSurfaceRuntime.runtimeToolAllowlist });
|
|
1589
|
-
const compactedTools = toolSurfaceRuntime.compactTools(plannedSourceTools, { localModelLeanApplied: true });
|
|
1654
|
+
const createToolSurface = hostCapabilities.createToolSurface;
|
|
1655
|
+
if (!createToolSurface) throw new Error("Copilot tool construction requires a current host capability");
|
|
1656
|
+
const sourceTools = createToolSurface(toolOptions, bindingOptions);
|
|
1657
|
+
const boundSourceTools = new Set(sourceTools);
|
|
1658
|
+
const allowedSourceTools = applyEmbeddedAttemptToolsAllow(sourceTools, toolSurfaceRuntime.runtimeToolAllowlist, { toolMeta: getPluginToolMeta });
|
|
1659
|
+
const compactedTools = toolSurfaceRuntime.compactTools(allowedSourceTools, { localModelLeanApplied: true });
|
|
1590
1660
|
const newlyConstructedTools = compactedTools.tools.filter((tool) => !boundSourceTools.has(tool));
|
|
1591
1661
|
const boundNewlyConstructedTools = newlyConstructedTools.length > 0 ? hostCapabilities.bindToolSurface(newlyConstructedTools, bindingOptions) : newlyConstructedTools;
|
|
1592
1662
|
if (boundNewlyConstructedTools.length !== newlyConstructedTools.length) throw new Error("Copilot host capability changed the tool surface length.");
|
|
@@ -1623,7 +1693,8 @@ async function createCopilotToolBridge(input) {
|
|
|
1623
1693
|
const directToolNames = new Set(result.tools.map((tool) => tool.name));
|
|
1624
1694
|
return {
|
|
1625
1695
|
tools: sdkTools.filter((tool) => directToolNames.has(tool.name)),
|
|
1626
|
-
callableToolNames: result.callableToolNames
|
|
1696
|
+
callableToolNames: result.callableToolNames,
|
|
1697
|
+
toolSchemaDirectoryPrompt: result.toolSchemaDirectoryPrompt
|
|
1627
1698
|
};
|
|
1628
1699
|
}
|
|
1629
1700
|
},
|
|
@@ -1637,8 +1708,8 @@ async function createCopilotToolBridge(input) {
|
|
|
1637
1708
|
*/
|
|
1638
1709
|
function buildOpenClawCodingToolsOptions(input, toolPlan, toolSurfaceRuntime) {
|
|
1639
1710
|
const a = input.attemptParams;
|
|
1640
|
-
const sandboxSessionKey = a.sandboxSessionKey?.trim() || a.sessionKey?.trim() || input.
|
|
1641
|
-
const liveSessionKey = a.sessionKey
|
|
1711
|
+
const sandboxSessionKey = a.sandboxSessionKey?.trim() || a.sessionKey?.trim() || input.sessionId;
|
|
1712
|
+
const liveSessionKey = a.sessionKey;
|
|
1642
1713
|
const runSessionKey = liveSessionKey && liveSessionKey !== sandboxSessionKey ? liveSessionKey : void 0;
|
|
1643
1714
|
const workspaceDir = input.workspaceDir ?? a.workspaceDir;
|
|
1644
1715
|
const cwd = input.cwd ?? a.cwd;
|
|
@@ -1673,7 +1744,8 @@ function buildOpenClawCodingToolsOptions(input, toolPlan, toolSurfaceRuntime) {
|
|
|
1673
1744
|
sandbox,
|
|
1674
1745
|
spawnWorkspaceDir: input.spawnWorkspaceDir,
|
|
1675
1746
|
config: toolSurfaceRuntime?.config ?? a.config,
|
|
1676
|
-
|
|
1747
|
+
skillsSnapshot: a.skillsSnapshot,
|
|
1748
|
+
abortSignal: a.abortSignal,
|
|
1677
1749
|
modelProvider: input.modelProvider,
|
|
1678
1750
|
modelId: input.modelId,
|
|
1679
1751
|
includeCoreTools: toolPlan.includeCoreTools,
|
|
@@ -1734,7 +1806,7 @@ function convertOpenClawToolToSdkTool(sourceTool, input, scheduleToolExecution)
|
|
|
1734
1806
|
};
|
|
1735
1807
|
const failureResult = (executedArgs, invocation, startedAt, message, error, executionStarted) => {
|
|
1736
1808
|
const errorMessage = toStringifiedError(error).message;
|
|
1737
|
-
input.attemptParams.observeToolTerminal?.({
|
|
1809
|
+
const terminal = input.attemptParams.observeToolTerminal?.({
|
|
1738
1810
|
toolCallId: invocation.toolCallId,
|
|
1739
1811
|
toolName: sourceTool.name,
|
|
1740
1812
|
result: error,
|
|
@@ -1757,7 +1829,8 @@ function convertOpenClawToolToSdkTool(sourceTool, input, scheduleToolExecution)
|
|
|
1757
1829
|
notifyToolCompleted({
|
|
1758
1830
|
toolName: sourceTool.name,
|
|
1759
1831
|
toolCallId: invocation.toolCallId,
|
|
1760
|
-
args: toToolStartArgs(executedArgs),
|
|
1832
|
+
args: toToolStartArgs(terminal?.executedArguments ?? executedArgs),
|
|
1833
|
+
isError: true,
|
|
1761
1834
|
error: errorMessage,
|
|
1762
1835
|
startedAt
|
|
1763
1836
|
});
|
|
@@ -1765,21 +1838,10 @@ function convertOpenClawToolToSdkTool(sourceTool, input, scheduleToolExecution)
|
|
|
1765
1838
|
};
|
|
1766
1839
|
const executeOnce = async (args, invocation) => {
|
|
1767
1840
|
const startedAt = Date.now();
|
|
1768
|
-
if (input.abortSignal?.aborted) {
|
|
1841
|
+
if (input.attemptParams.abortSignal?.aborted) {
|
|
1769
1842
|
const error = /* @__PURE__ */ new Error("[copilot-tool-bridge] aborted before execution");
|
|
1770
1843
|
return failureResult(args, invocation, startedAt, error.message, error, false);
|
|
1771
1844
|
}
|
|
1772
|
-
try {
|
|
1773
|
-
await input.beforeExecute?.({
|
|
1774
|
-
args,
|
|
1775
|
-
invocation,
|
|
1776
|
-
sourceTool,
|
|
1777
|
-
toolCallId: invocation.toolCallId,
|
|
1778
|
-
toolName: sourceTool.name
|
|
1779
|
-
});
|
|
1780
|
-
} catch (error) {
|
|
1781
|
-
return failureResult(args, invocation, startedAt, `[copilot-tool-bridge] beforeExecute failed for tool '${sourceTool.name}': ${toStringifiedError(error).message}`, error, false);
|
|
1782
|
-
}
|
|
1783
1845
|
let preparedArgs;
|
|
1784
1846
|
try {
|
|
1785
1847
|
preparedArgs = sourceTool.prepareArguments ? sourceTool.prepareArguments(args) : args;
|
|
@@ -1788,7 +1850,7 @@ function convertOpenClawToolToSdkTool(sourceTool, input, scheduleToolExecution)
|
|
|
1788
1850
|
}
|
|
1789
1851
|
let result;
|
|
1790
1852
|
try {
|
|
1791
|
-
result = await sourceTool.execute(invocation.toolCallId, preparedArgs, input.abortSignal, void 0);
|
|
1853
|
+
result = await sourceTool.execute(invocation.toolCallId, preparedArgs, input.attemptParams.abortSignal, void 0);
|
|
1792
1854
|
} catch (error) {
|
|
1793
1855
|
return failureResult(preparedArgs, invocation, startedAt, `[copilot-tool-bridge] tool '${sourceTool.name}' failed: ${toStringifiedError(error).message}`, error, true);
|
|
1794
1856
|
}
|
|
@@ -1799,7 +1861,7 @@ function convertOpenClawToolToSdkTool(sourceTool, input, scheduleToolExecution)
|
|
|
1799
1861
|
isError: resultIsError
|
|
1800
1862
|
});
|
|
1801
1863
|
const resultError = resultIsError ? extractToolErrorMessage(sanitizedResult) : void 0;
|
|
1802
|
-
input.attemptParams.observeToolTerminal?.({
|
|
1864
|
+
const terminal = input.attemptParams.observeToolTerminal?.({
|
|
1803
1865
|
toolCallId: invocation.toolCallId,
|
|
1804
1866
|
toolName: sourceTool.name,
|
|
1805
1867
|
result,
|
|
@@ -1813,8 +1875,9 @@ function convertOpenClawToolToSdkTool(sourceTool, input, scheduleToolExecution)
|
|
|
1813
1875
|
notifyToolCompleted({
|
|
1814
1876
|
toolName: sourceTool.name,
|
|
1815
1877
|
toolCallId: invocation.toolCallId,
|
|
1816
|
-
args: toToolStartArgs(preparedArgs),
|
|
1878
|
+
args: toToolStartArgs(terminal?.executedArguments ?? preparedArgs),
|
|
1817
1879
|
result: sanitizedResult,
|
|
1880
|
+
isError: resultIsError,
|
|
1818
1881
|
...resultError ? { error: resultError } : {},
|
|
1819
1882
|
startedAt
|
|
1820
1883
|
});
|
|
@@ -1841,12 +1904,12 @@ async function executeCatalogTool(input, params) {
|
|
|
1841
1904
|
try {
|
|
1842
1905
|
preparedArgs = sourceTool.prepareArguments ? sourceTool.prepareArguments(params.input) : params.input;
|
|
1843
1906
|
executionStarted = true;
|
|
1844
|
-
const result = await sourceTool.execute(params.toolCallId, preparedArgs, params.signal ?? input.abortSignal, params.onUpdate);
|
|
1907
|
+
const result = await sourceTool.execute(params.toolCallId, preparedArgs, params.signal ?? input.attemptParams.abortSignal, params.onUpdate);
|
|
1845
1908
|
const sanitizedResult = sanitizeToolResult(result);
|
|
1846
1909
|
const isError = isToolResultError(sanitizedResult);
|
|
1847
1910
|
const error = isError ? extractToolErrorMessage(sanitizedResult) ?? "tool returned an error" : void 0;
|
|
1848
1911
|
terminalObserved = true;
|
|
1849
|
-
input.attemptParams?.observeToolTerminal?.({
|
|
1912
|
+
const terminal = input.attemptParams?.observeToolTerminal?.({
|
|
1850
1913
|
toolCallId: params.toolCallId,
|
|
1851
1914
|
toolName: params.toolName,
|
|
1852
1915
|
result,
|
|
@@ -1864,15 +1927,17 @@ async function executeCatalogTool(input, params) {
|
|
|
1864
1927
|
await input.onToolCompleted?.({
|
|
1865
1928
|
toolName: params.toolName,
|
|
1866
1929
|
toolCallId: params.toolCallId,
|
|
1867
|
-
|
|
1930
|
+
parentToolCallId: params.parentToolCallId,
|
|
1931
|
+
args: toToolStartArgs(terminal?.executedArguments ?? preparedArgs),
|
|
1868
1932
|
result: sanitizedResult,
|
|
1933
|
+
isError,
|
|
1869
1934
|
...error ? { error } : {},
|
|
1870
1935
|
startedAt
|
|
1871
1936
|
});
|
|
1872
1937
|
return result;
|
|
1873
1938
|
} catch (error) {
|
|
1874
1939
|
const message = toStringifiedError(error).message;
|
|
1875
|
-
if (!terminalObserved) input.attemptParams?.observeToolTerminal?.({
|
|
1940
|
+
if (!terminalObserved) preparedArgs = (input.attemptParams?.observeToolTerminal?.({
|
|
1876
1941
|
toolCallId: params.toolCallId,
|
|
1877
1942
|
toolName: params.toolName,
|
|
1878
1943
|
result: error,
|
|
@@ -1881,7 +1946,7 @@ async function executeCatalogTool(input, params) {
|
|
|
1881
1946
|
outcome: "failure",
|
|
1882
1947
|
failure: { error: message },
|
|
1883
1948
|
...ownerMutation ? { ownerMutation } : {}
|
|
1884
|
-
});
|
|
1949
|
+
}))?.executedArguments ?? preparedArgs;
|
|
1885
1950
|
const failure = sanitizeToolResult({
|
|
1886
1951
|
content: [{
|
|
1887
1952
|
type: "text",
|
|
@@ -1900,7 +1965,9 @@ async function executeCatalogTool(input, params) {
|
|
|
1900
1965
|
await input.onToolCompleted?.({
|
|
1901
1966
|
toolName: params.toolName,
|
|
1902
1967
|
toolCallId: params.toolCallId,
|
|
1968
|
+
parentToolCallId: params.parentToolCallId,
|
|
1903
1969
|
args: toToolStartArgs(preparedArgs),
|
|
1970
|
+
isError: true,
|
|
1904
1971
|
error: message,
|
|
1905
1972
|
startedAt
|
|
1906
1973
|
});
|
|
@@ -1917,38 +1984,10 @@ function createFailureResult(message, error) {
|
|
|
1917
1984
|
textResultForLlm: message
|
|
1918
1985
|
};
|
|
1919
1986
|
}
|
|
1920
|
-
function createError(message, cause) {
|
|
1921
|
-
const error = new Error(message);
|
|
1922
|
-
error.cause = cause;
|
|
1923
|
-
return error;
|
|
1924
|
-
}
|
|
1925
|
-
/**
|
|
1926
|
-
* Mirrors PI's `shouldForceMessageTool` semantics: a message tool is
|
|
1927
|
-
* forced when the caller asked for it explicitly or when the source
|
|
1928
|
-
* reply delivery mode is `message_tool_only`, but never when
|
|
1929
|
-
* `disableMessageTool` is set (the suppress flag always wins). Compare
|
|
1930
|
-
* `src/agents/pi-embedded-runner/run/attempt.ts:1361-1366` and the
|
|
1931
|
-
* codex equivalent at
|
|
1932
|
-
* `extensions/codex/src/app-server/run-attempt.ts:4253-4258`.
|
|
1933
|
-
*/
|
|
1934
1987
|
function shouldForceCopilotMessageTool(params) {
|
|
1935
1988
|
if (params.disableMessageTool === true) return false;
|
|
1936
1989
|
return params.forceMessageTool === true || params.sourceReplyDeliveryMode === "message_tool_only";
|
|
1937
1990
|
}
|
|
1938
|
-
function filterCopilotToolsForConstructionPlan(tools, plan, options = {}) {
|
|
1939
|
-
if (plan.includeBaseCodingTools && plan.includeShellTools) return tools;
|
|
1940
|
-
const preserveToolNames = new Set(options.preserveToolNames);
|
|
1941
|
-
return tools.filter((tool) => {
|
|
1942
|
-
if (preserveToolNames.has(tool.name)) return true;
|
|
1943
|
-
if (!plan.includeBaseCodingTools && BASE_COPILOT_CODING_TOOL_NAMES.has(tool.name)) return false;
|
|
1944
|
-
if (!plan.includeShellTools && SHELL_COPILOT_CODING_TOOL_NAMES.has(tool.name)) return false;
|
|
1945
|
-
return true;
|
|
1946
|
-
});
|
|
1947
|
-
}
|
|
1948
|
-
function readInlinePluginToolMeta(tool) {
|
|
1949
|
-
const pluginId = tool.pluginId;
|
|
1950
|
-
return typeof pluginId === "string" && pluginId.trim() ? { pluginId } : void 0;
|
|
1951
|
-
}
|
|
1952
1991
|
function findDuplicateToolNames(sourceTools) {
|
|
1953
1992
|
const counts = /* @__PURE__ */ new Map();
|
|
1954
1993
|
for (const sourceTool of sourceTools) {
|
|
@@ -2069,6 +2108,7 @@ function buildCopilotPromptGuidance(params) {
|
|
|
2069
2108
|
}).join("\n") : void 0;
|
|
2070
2109
|
return [
|
|
2071
2110
|
COPILOT_HARNESS_IDENTITY,
|
|
2111
|
+
params.toolSchemaDirectoryPrompt,
|
|
2072
2112
|
callableTools.has(SKILL_WORKSHOP_TOOL_NAME) ? buildSkillWorkshopPromptSection().join("\n") : void 0,
|
|
2073
2113
|
delegationGuidance,
|
|
2074
2114
|
buildHarnessVisibleReplyGuidance({
|
|
@@ -2352,6 +2392,7 @@ async function createCopilotSessionSetup(params) {
|
|
|
2352
2392
|
return buildCopilotPromptGuidance({
|
|
2353
2393
|
attempt: input,
|
|
2354
2394
|
callableToolNames: promptPolicyResult.callableToolNames,
|
|
2395
|
+
toolSchemaDirectoryPrompt: promptPolicyResult.toolSchemaDirectoryPrompt,
|
|
2355
2396
|
requireExplicitMessageTarget: promptToolPolicy.requireExplicitMessageTarget,
|
|
2356
2397
|
workspaceBootstrapInstructions: workspaceBootstrap.instructions
|
|
2357
2398
|
});
|
|
@@ -2421,41 +2462,6 @@ async function createCopilotSessionSetup(params) {
|
|
|
2421
2462
|
};
|
|
2422
2463
|
}
|
|
2423
2464
|
//#endregion
|
|
2424
|
-
//#region extensions/copilot/src/attempt-transcript-replay.ts
|
|
2425
|
-
function readAssistantToolCallIds(message) {
|
|
2426
|
-
return message.role === "assistant" ? message.content.flatMap((part) => part.type === "toolCall" ? [part.id] : []) : [];
|
|
2427
|
-
}
|
|
2428
|
-
function isCompatibleSingletonRewrite(original, prepared) {
|
|
2429
|
-
return original.role === prepared.role && (original.role !== "assistant" || JSON.stringify(readAssistantToolCallIds(original)) === JSON.stringify(readAssistantToolCallIds(prepared)));
|
|
2430
|
-
}
|
|
2431
|
-
function projectReplayPayload(message) {
|
|
2432
|
-
switch (message.role) {
|
|
2433
|
-
case "user": return {
|
|
2434
|
-
role: message.role,
|
|
2435
|
-
content: message.content
|
|
2436
|
-
};
|
|
2437
|
-
case "assistant": return {
|
|
2438
|
-
role: message.role,
|
|
2439
|
-
content: message.content,
|
|
2440
|
-
api: message.api,
|
|
2441
|
-
model: message.model,
|
|
2442
|
-
provider: message.provider,
|
|
2443
|
-
stopReason: message.stopReason
|
|
2444
|
-
};
|
|
2445
|
-
case "toolResult": return {
|
|
2446
|
-
role: message.role,
|
|
2447
|
-
content: message.content,
|
|
2448
|
-
isError: message.isError,
|
|
2449
|
-
toolCallId: message.toolCallId,
|
|
2450
|
-
toolName: message.toolName
|
|
2451
|
-
};
|
|
2452
|
-
}
|
|
2453
|
-
}
|
|
2454
|
-
function isCompleteToolGroup(messages, order) {
|
|
2455
|
-
const [assistant, ...results] = messages;
|
|
2456
|
-
return assistant?.role === "assistant" && JSON.stringify(readAssistantToolCallIds(assistant)) === JSON.stringify(order) && results.length === order.length && results.every((message, index) => message.role === "toolResult" && message.toolCallId === order[index]);
|
|
2457
|
-
}
|
|
2458
|
-
//#endregion
|
|
2459
2465
|
//#region extensions/copilot/src/attempt-transcript-journal.ts
|
|
2460
2466
|
function readTurnTaintMetadata(message) {
|
|
2461
2467
|
const metadata = message["__openclaw"];
|
|
@@ -2886,14 +2892,6 @@ function isSameUserTurn(candidate, current, currentRunUserKey) {
|
|
|
2886
2892
|
}
|
|
2887
2893
|
return candidate.timestamp === current.timestamp && userText(candidate.content) === userText(current.content);
|
|
2888
2894
|
}
|
|
2889
|
-
function userText(content) {
|
|
2890
|
-
if (typeof content === "string") return content;
|
|
2891
|
-
if (Array.isArray(content) && content.length === 1) {
|
|
2892
|
-
const part = content[0];
|
|
2893
|
-
if (part?.type === "text" && typeof part.text === "string") return part.text;
|
|
2894
|
-
}
|
|
2895
|
-
return JSON.stringify(content) ?? "";
|
|
2896
|
-
}
|
|
2897
2895
|
//#endregion
|
|
2898
2896
|
//#region extensions/copilot/src/native-subagent-task-mirror.ts
|
|
2899
2897
|
const COPILOT_NATIVE_SUBAGENT_TASK_KIND = "copilot-native";
|
|
@@ -2916,8 +2914,7 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
2916
2914
|
this.runtime = runtime;
|
|
2917
2915
|
this.runIdByAgentId = /* @__PURE__ */ new Map();
|
|
2918
2916
|
this.runIdByToolCallId = /* @__PURE__ */ new Map();
|
|
2919
|
-
this.
|
|
2920
|
-
this.activeRunIds = /* @__PURE__ */ new Set();
|
|
2917
|
+
this.activeRuns = /* @__PURE__ */ new Map();
|
|
2921
2918
|
this.now = params.now ?? Date.now;
|
|
2922
2919
|
}
|
|
2923
2920
|
handleEvent(event) {
|
|
@@ -2936,9 +2933,9 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
2936
2933
|
}
|
|
2937
2934
|
finalizeActiveRuns() {
|
|
2938
2935
|
const eventAt = this.now();
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
this.
|
|
2936
|
+
let failure;
|
|
2937
|
+
for (const runId of this.activeRuns.keys()) try {
|
|
2938
|
+
this.finalizeRun({
|
|
2942
2939
|
runId,
|
|
2943
2940
|
status: "cancelled",
|
|
2944
2941
|
endedAt: eventAt,
|
|
@@ -2947,8 +2944,10 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
2947
2944
|
progressSummary: "Subagent cancelled with its parent attempt.",
|
|
2948
2945
|
terminalSummary: "Subagent cancelled."
|
|
2949
2946
|
});
|
|
2947
|
+
} catch (error) {
|
|
2948
|
+
failure ??= { error };
|
|
2950
2949
|
}
|
|
2951
|
-
|
|
2950
|
+
if (failure) throw failure.error;
|
|
2952
2951
|
}
|
|
2953
2952
|
handleStarted(event, runId, toolCallId) {
|
|
2954
2953
|
const agentId = event.agentId?.trim();
|
|
@@ -2956,7 +2955,7 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
2956
2955
|
const eventAt = this.now();
|
|
2957
2956
|
const label = event.data.agentDisplayName.trim() || event.data.agentName.trim();
|
|
2958
2957
|
const task = event.data.agentDescription.trim() || `Subagent ${label}`;
|
|
2959
|
-
|
|
2958
|
+
const taskRecord = this.runtime.tryCreateRunningTaskRun({
|
|
2960
2959
|
sourceId: toolCallId,
|
|
2961
2960
|
agentId: this.params.agentId,
|
|
2962
2961
|
runId,
|
|
@@ -2968,18 +2967,15 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
2968
2967
|
startedAt: eventAt,
|
|
2969
2968
|
lastEventAt: eventAt,
|
|
2970
2969
|
progressSummary: "Subagent started."
|
|
2971
|
-
})
|
|
2970
|
+
});
|
|
2971
|
+
if (!taskRecord) return;
|
|
2972
2972
|
if (agentId) this.runIdByAgentId.set(agentId, runId);
|
|
2973
2973
|
else this.runIdByToolCallId.set(toolCallId, runId);
|
|
2974
|
-
this.
|
|
2975
|
-
this.activeRunIds.add(runId);
|
|
2974
|
+
this.activeRuns.set(runId, { taskId: taskRecord.taskId });
|
|
2976
2975
|
}
|
|
2977
2976
|
handleCompleted(event, runId) {
|
|
2978
|
-
if (this.terminalRunIds.has(runId)) return;
|
|
2979
2977
|
const eventAt = this.now();
|
|
2980
|
-
this.
|
|
2981
|
-
this.activeRunIds.delete(runId);
|
|
2982
|
-
this.runtime.finalizeTaskRunByRunId({
|
|
2978
|
+
this.finalizeRun({
|
|
2983
2979
|
runId,
|
|
2984
2980
|
status: "succeeded",
|
|
2985
2981
|
endedAt: eventAt,
|
|
@@ -2989,11 +2985,8 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
2989
2985
|
});
|
|
2990
2986
|
}
|
|
2991
2987
|
handleFailed(event, runId) {
|
|
2992
|
-
if (this.terminalRunIds.has(runId)) return;
|
|
2993
2988
|
const eventAt = this.now();
|
|
2994
|
-
this.
|
|
2995
|
-
this.activeRunIds.delete(runId);
|
|
2996
|
-
this.runtime.finalizeTaskRunByRunId({
|
|
2989
|
+
this.finalizeRun({
|
|
2997
2990
|
runId,
|
|
2998
2991
|
status: "failed",
|
|
2999
2992
|
endedAt: eventAt,
|
|
@@ -3003,6 +2996,20 @@ var CopilotNativeSubagentTaskMirror = class {
|
|
|
3003
2996
|
terminalSummary: "Subagent failed."
|
|
3004
2997
|
});
|
|
3005
2998
|
}
|
|
2999
|
+
finalizeRun(params) {
|
|
3000
|
+
const run = this.activeRuns.get(params.runId);
|
|
3001
|
+
if (!run) return;
|
|
3002
|
+
run.terminal ??= params;
|
|
3003
|
+
const matchesRun = (task) => task.taskId === run.taskId && task.runId === params.runId;
|
|
3004
|
+
const before = this.runtime.listTaskRecords().find(matchesRun);
|
|
3005
|
+
if (!before || before.status !== "queued" && before.status !== "running") {
|
|
3006
|
+
this.activeRuns.delete(params.runId);
|
|
3007
|
+
return;
|
|
3008
|
+
}
|
|
3009
|
+
const current = this.runtime.finalizeTaskRunByRunId(run.terminal).find(matchesRun) ?? this.runtime.listTaskRecords().find(matchesRun);
|
|
3010
|
+
if (current?.status === "queued" || current?.status === "running") throw new Error(`Native subagent task finalization did not persist: ${params.runId}`);
|
|
3011
|
+
this.activeRuns.delete(params.runId);
|
|
3012
|
+
}
|
|
3006
3013
|
resolveRunId(event) {
|
|
3007
3014
|
const agentId = event.agentId?.trim();
|
|
3008
3015
|
if (agentId) {
|
|
@@ -3159,13 +3166,11 @@ async function runCopilotExecution(context) {
|
|
|
3159
3166
|
modelId: modelRef.id,
|
|
3160
3167
|
agentId: readNonEmptyString(params.agentId) ?? "copilot",
|
|
3161
3168
|
sessionId: readNonEmptyString(input.sessionId) ?? "copilot-session",
|
|
3162
|
-
sessionKey: readNonEmptyString(input.sessionKey),
|
|
3163
3169
|
agentDir: readNonEmptyString(input.agentDir),
|
|
3164
3170
|
workspaceDir: effectiveWorkspaceDir,
|
|
3165
3171
|
cwd: effectiveCwd,
|
|
3166
3172
|
sandbox,
|
|
3167
3173
|
spawnWorkspaceDir: sandboxAwareSpawnWorkspaceDir,
|
|
3168
|
-
abortSignal: params.abortSignal,
|
|
3169
3174
|
attemptParams: observeToolTerminal ? {
|
|
3170
3175
|
...input,
|
|
3171
3176
|
observeToolTerminal
|
|
@@ -3176,15 +3181,11 @@ async function runCopilotExecution(context) {
|
|
|
3176
3181
|
yieldDetected = true;
|
|
3177
3182
|
yieldAcknowledgment = acknowledgment;
|
|
3178
3183
|
},
|
|
3179
|
-
onToolCompleted: async (
|
|
3180
|
-
|
|
3181
|
-
const
|
|
3182
|
-
const
|
|
3183
|
-
if (
|
|
3184
|
-
runId,
|
|
3185
|
-
childSessionKey,
|
|
3186
|
-
expectsCompletionMessage: acceptedSessionSpawnDetails.expectsCompletionMessage === true
|
|
3187
|
-
});
|
|
3184
|
+
onToolCompleted: async (completion) => {
|
|
3185
|
+
bridge?.completeTool(completion);
|
|
3186
|
+
const { args, error, result, startedAt, toolCallId, toolName } = completion;
|
|
3187
|
+
const acceptedSessionSpawn = toolName === "sessions_spawn" && !completion.isError ? normalizeAcceptedSessionSpawnResult(result) : null;
|
|
3188
|
+
if (acceptedSessionSpawn) acceptedSessionSpawns.push(acceptedSessionSpawn);
|
|
3188
3189
|
await runAgentHarnessAfterToolCallHook({
|
|
3189
3190
|
toolName,
|
|
3190
3191
|
toolCallId,
|
|
@@ -3262,7 +3263,7 @@ async function runCopilotExecution(context) {
|
|
|
3262
3263
|
sdkSessionId = readNonEmptyString(session.sessionId) ?? readNonEmptyString(session.id) ?? (resumeFailureRecovered ? void 0 : resumeSessionId);
|
|
3263
3264
|
if (!sdkSessionId) throw createPromptError$1("transcript_persistence_failed", "[copilot-attempt] canonical transcript persistence requires the Copilot SDK session id");
|
|
3264
3265
|
if (sdkSessionId && deps.onSessionEstablished && !settledToolFinalization) try {
|
|
3265
|
-
deps.onSessionEstablished({
|
|
3266
|
+
await deps.onSessionEstablished({
|
|
3266
3267
|
compactionSessionConfig,
|
|
3267
3268
|
sdkSessionId,
|
|
3268
3269
|
pooledClient: handle,
|
|
@@ -3279,6 +3280,8 @@ async function runCopilotExecution(context) {
|
|
|
3279
3280
|
sdkSessionId
|
|
3280
3281
|
});
|
|
3281
3282
|
bridge = attachEventBridge(session, {
|
|
3283
|
+
runId: input.runId,
|
|
3284
|
+
sessionKey: input.sessionKey,
|
|
3282
3285
|
onAssistantDelta: settledToolFinalization ? void 0 : input.onAssistantDelta,
|
|
3283
3286
|
onAgentEvent: settledToolFinalization ? void 0 : input.onAgentEvent,
|
|
3284
3287
|
onNativeSubagentEvent: (event) => nativeSubagentTaskMirror?.handleEvent(event),
|
|
@@ -3422,7 +3425,7 @@ async function runCopilotExecution(context) {
|
|
|
3422
3425
|
params.abortSignal?.removeEventListener("abort", abortCleanup);
|
|
3423
3426
|
}).catch(() => void 0);
|
|
3424
3427
|
if (sdkSessionId && !settledToolFinalization) try {
|
|
3425
|
-
deps.onDeferredCompaction?.({
|
|
3428
|
+
await deps.onDeferredCompaction?.({
|
|
3426
3429
|
abort: () => cleanupAbort.abort(),
|
|
3427
3430
|
cleanup,
|
|
3428
3431
|
sdkSessionId
|
|
@@ -3432,7 +3435,11 @@ async function runCopilotExecution(context) {
|
|
|
3432
3435
|
} else {
|
|
3433
3436
|
await bridge?.awaitCompactionChain();
|
|
3434
3437
|
await bridge?.awaitAgentEventChain();
|
|
3435
|
-
|
|
3438
|
+
try {
|
|
3439
|
+
nativeSubagentTaskMirror?.finalizeActiveRuns();
|
|
3440
|
+
} catch (error) {
|
|
3441
|
+
promptError ??= toCopilotError(error);
|
|
3442
|
+
}
|
|
3436
3443
|
cleanupToolBridge?.();
|
|
3437
3444
|
await cleanupByokProxy?.();
|
|
3438
3445
|
bridge?.detach();
|