squad-openclaw 2026.2.2205 → 2026.2.2207
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/dist/index.js +122 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2682,6 +2682,9 @@ function registerQuestionMethods(api) {
|
|
|
2682
2682
|
}
|
|
2683
2683
|
|
|
2684
2684
|
// src/sessions.ts
|
|
2685
|
+
import fs10 from "fs";
|
|
2686
|
+
import path11 from "path";
|
|
2687
|
+
import crypto4 from "crypto";
|
|
2685
2688
|
function asRecord(value) {
|
|
2686
2689
|
return value && typeof value === "object" ? value : null;
|
|
2687
2690
|
}
|
|
@@ -2704,16 +2707,104 @@ function extractSessionKey(value) {
|
|
|
2704
2707
|
}
|
|
2705
2708
|
return null;
|
|
2706
2709
|
}
|
|
2707
|
-
function
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2710
|
+
function isInvoker(fn) {
|
|
2711
|
+
return typeof fn === "function";
|
|
2712
|
+
}
|
|
2713
|
+
async function callGatewayAny(ctx, api, method, params) {
|
|
2714
|
+
const ctxGateway = asRecord(ctx.gateway);
|
|
2715
|
+
const apiGateway = asRecord(api?.gateway);
|
|
2716
|
+
const candidates = [
|
|
2717
|
+
ctx.request,
|
|
2718
|
+
ctx.callGatewayMethod,
|
|
2719
|
+
ctx.gatewayRequest,
|
|
2720
|
+
ctx.invokeGatewayMethod,
|
|
2721
|
+
ctxGateway?.request,
|
|
2722
|
+
ctxGateway?.callGatewayMethod,
|
|
2723
|
+
api?.request,
|
|
2724
|
+
api?.callGatewayMethod,
|
|
2725
|
+
api?.gatewayRequest,
|
|
2726
|
+
api?.invokeGatewayMethod,
|
|
2727
|
+
apiGateway?.request,
|
|
2728
|
+
apiGateway?.callGatewayMethod
|
|
2729
|
+
];
|
|
2730
|
+
let lastErr = null;
|
|
2731
|
+
for (const candidate of candidates) {
|
|
2732
|
+
if (!isInvoker(candidate)) continue;
|
|
2733
|
+
try {
|
|
2734
|
+
return await candidate(method, params);
|
|
2735
|
+
} catch (err2) {
|
|
2736
|
+
lastErr = err2;
|
|
2737
|
+
const msg = err2 instanceof Error ? err2.message : String(err2);
|
|
2738
|
+
if (/unknown method|method .* unavailable|not found|invalid[_ ]request|does not exist/i.test(
|
|
2739
|
+
msg
|
|
2740
|
+
)) {
|
|
2741
|
+
continue;
|
|
2742
|
+
}
|
|
2743
|
+
throw err2;
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
if (lastErr) throw lastErr;
|
|
2747
|
+
throw new Error("Gateway method invocation API unavailable in plugin context");
|
|
2748
|
+
}
|
|
2749
|
+
function parseAgentIdFromSessionKey(sessionKey) {
|
|
2750
|
+
const m = sessionKey.match(/^agent:([^:]+):/);
|
|
2751
|
+
return m?.[1] ?? null;
|
|
2752
|
+
}
|
|
2753
|
+
function ensureDir(dirPath) {
|
|
2754
|
+
fs10.mkdirSync(dirPath, { recursive: true });
|
|
2755
|
+
}
|
|
2756
|
+
function readSessionsMap(sessionsJsonPath) {
|
|
2757
|
+
try {
|
|
2758
|
+
const raw = fs10.readFileSync(sessionsJsonPath, "utf-8");
|
|
2759
|
+
const parsed = JSON.parse(raw);
|
|
2760
|
+
if (parsed && typeof parsed === "object") {
|
|
2761
|
+
return parsed;
|
|
2762
|
+
}
|
|
2763
|
+
} catch {
|
|
2764
|
+
}
|
|
2765
|
+
return {};
|
|
2766
|
+
}
|
|
2767
|
+
function writeSessionsMap(sessionsJsonPath, sessionsMap) {
|
|
2768
|
+
fs10.writeFileSync(sessionsJsonPath, JSON.stringify(sessionsMap, null, 2), "utf-8");
|
|
2769
|
+
}
|
|
2770
|
+
function createSessionOnDisk(input) {
|
|
2771
|
+
const requestedSessionKey = input.requestedSessionKey?.trim();
|
|
2772
|
+
const requestedAgentId = input.requestedAgentId?.trim();
|
|
2773
|
+
const derivedAgentId = requestedAgentId || (requestedSessionKey ? parseAgentIdFromSessionKey(requestedSessionKey) : null) || "main";
|
|
2774
|
+
const sessionKey = requestedSessionKey || `agent:${derivedAgentId}:${crypto4.randomUUID()}`;
|
|
2775
|
+
const stateDir = getOpenclawStateDir();
|
|
2776
|
+
const sessionsDir = path11.join(stateDir, "agents", derivedAgentId, "sessions");
|
|
2777
|
+
const sessionsJsonPath = path11.join(sessionsDir, "sessions.json");
|
|
2778
|
+
ensureDir(sessionsDir);
|
|
2779
|
+
const now = Date.now();
|
|
2780
|
+
const sessionsMap = readSessionsMap(sessionsJsonPath);
|
|
2781
|
+
const existing = sessionsMap[sessionKey];
|
|
2782
|
+
const sessionId = typeof existing?.sessionId === "string" && existing.sessionId.trim() ? existing.sessionId : crypto4.randomUUID();
|
|
2783
|
+
const jsonlPath = path11.join(sessionsDir, `${sessionId}.jsonl`);
|
|
2784
|
+
sessionsMap[sessionKey] = {
|
|
2785
|
+
...existing ?? {},
|
|
2786
|
+
sessionId,
|
|
2787
|
+
updatedAt: now,
|
|
2788
|
+
label: typeof existing?.label === "string" && existing.label.trim() ? existing.label : "New Session",
|
|
2789
|
+
totalTokens: typeof existing?.totalTokens === "number" ? existing.totalTokens : 0,
|
|
2790
|
+
lastMessageRole: existing?.lastMessageRole ?? "assistant",
|
|
2791
|
+
lastAssistantHasText: typeof existing?.lastAssistantHasText === "boolean" ? existing.lastAssistantHasText : true
|
|
2716
2792
|
};
|
|
2793
|
+
writeSessionsMap(sessionsJsonPath, sessionsMap);
|
|
2794
|
+
if (!fs10.existsSync(jsonlPath)) {
|
|
2795
|
+
fs10.writeFileSync(jsonlPath, "", "utf-8");
|
|
2796
|
+
}
|
|
2797
|
+
return {
|
|
2798
|
+
key: sessionKey,
|
|
2799
|
+
sessionKey,
|
|
2800
|
+
sessionId,
|
|
2801
|
+
agentId: derivedAgentId,
|
|
2802
|
+
displayName: sessionKey,
|
|
2803
|
+
status: "active",
|
|
2804
|
+
createdAt: now
|
|
2805
|
+
};
|
|
2806
|
+
}
|
|
2807
|
+
function registerSessionMethods(api) {
|
|
2717
2808
|
api.registerGatewayMethod(
|
|
2718
2809
|
"squad.sessions.create",
|
|
2719
2810
|
async (ctx) => {
|
|
@@ -2737,7 +2828,12 @@ function registerSessionMethods(api) {
|
|
|
2737
2828
|
...typeof agentId === "string" ? { agentId: agentId.trim() } : {}
|
|
2738
2829
|
};
|
|
2739
2830
|
try {
|
|
2740
|
-
const nativeResult = await
|
|
2831
|
+
const nativeResult = await callGatewayAny(
|
|
2832
|
+
ctx,
|
|
2833
|
+
api,
|
|
2834
|
+
"sessions.create",
|
|
2835
|
+
createParams
|
|
2836
|
+
);
|
|
2741
2837
|
const normalizedKey = extractSessionKey(nativeResult) ?? (typeof sessionKey === "string" ? sessionKey.trim() : null);
|
|
2742
2838
|
if (!normalizedKey) {
|
|
2743
2839
|
respond(false, {
|
|
@@ -2753,6 +2849,22 @@ function registerSessionMethods(api) {
|
|
|
2753
2849
|
});
|
|
2754
2850
|
} catch (err2) {
|
|
2755
2851
|
const msg = err2 instanceof Error ? err2.message : String(err2);
|
|
2852
|
+
if (/gateway method invocation api unavailable in plugin context/i.test(msg)) {
|
|
2853
|
+
try {
|
|
2854
|
+
const local = createSessionOnDisk({
|
|
2855
|
+
requestedSessionKey: typeof sessionKey === "string" ? sessionKey : void 0,
|
|
2856
|
+
requestedAgentId: typeof agentId === "string" ? agentId : void 0
|
|
2857
|
+
});
|
|
2858
|
+
respond(true, local);
|
|
2859
|
+
return;
|
|
2860
|
+
} catch (fallbackErr) {
|
|
2861
|
+
const fallbackMsg = fallbackErr instanceof Error ? fallbackErr.message : String(fallbackErr);
|
|
2862
|
+
respond(false, {
|
|
2863
|
+
error: `Failed to create session (local fallback): ${fallbackMsg}`.slice(0, 500)
|
|
2864
|
+
});
|
|
2865
|
+
return;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2756
2868
|
respond(false, {
|
|
2757
2869
|
error: `Failed to create session: ${msg}`.slice(0, 500)
|
|
2758
2870
|
});
|
package/package.json
CHANGED