mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:58:28 +00:00
chore: merge origin/main into main
This commit is contained in:
@@ -217,7 +217,7 @@ export const agentHandlers: GatewayRequestHandlers = {
|
||||
}
|
||||
const normalizedAttachments = normalizeRpcAttachmentsToChatAttachments(request.attachments);
|
||||
|
||||
let message = request.message.trim();
|
||||
let message = (request.message ?? "").trim();
|
||||
let images: Array<{ type: "image"; data: string; mimeType: string }> = [];
|
||||
if (normalizedAttachments.length > 0) {
|
||||
try {
|
||||
@@ -695,7 +695,7 @@ export const agentHandlers: GatewayRequestHandlers = {
|
||||
return;
|
||||
}
|
||||
const p = params;
|
||||
const runId = p.runId.trim();
|
||||
const runId = (p.runId ?? "").trim();
|
||||
const timeoutMs =
|
||||
typeof p.timeoutMs === "number" && Number.isFinite(p.timeoutMs)
|
||||
? Math.max(0, Math.floor(p.timeoutMs))
|
||||
|
||||
@@ -1,41 +1,28 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { CURRENT_SESSION_VERSION } from "@mariozechner/pi-coding-agent";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createMockSessionEntry, createTranscriptFixtureSync } from "./chat.test-helpers.js";
|
||||
import type { GatewayRequestContext } from "./types.js";
|
||||
|
||||
// Guardrail: Ensure gateway "injected" assistant transcript messages are appended via SessionManager,
|
||||
// so they are attached to the current leaf with a `parentId` and do not sever compaction history.
|
||||
describe("gateway chat.inject transcript writes", () => {
|
||||
it("appends a Pi session entry that includes parentId", async () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-chat-inject-"));
|
||||
const transcriptPath = path.join(dir, "sess.jsonl");
|
||||
|
||||
// Minimal Pi session header so SessionManager can open/append safely.
|
||||
fs.writeFileSync(
|
||||
transcriptPath,
|
||||
`${JSON.stringify({
|
||||
type: "session",
|
||||
version: CURRENT_SESSION_VERSION,
|
||||
id: "sess-1",
|
||||
timestamp: new Date(0).toISOString(),
|
||||
cwd: "/tmp",
|
||||
})}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
const sessionId = "sess-1";
|
||||
const { transcriptPath } = createTranscriptFixtureSync({
|
||||
prefix: "openclaw-chat-inject-",
|
||||
sessionId,
|
||||
});
|
||||
|
||||
vi.doMock("../session-utils.js", async (importOriginal) => {
|
||||
const original = await importOriginal<typeof import("../session-utils.js")>();
|
||||
return {
|
||||
...original,
|
||||
loadSessionEntry: () => ({
|
||||
storePath: path.join(dir, "sessions.json"),
|
||||
entry: {
|
||||
sessionId: "sess-1",
|
||||
sessionFile: transcriptPath,
|
||||
},
|
||||
}),
|
||||
loadSessionEntry: () =>
|
||||
createMockSessionEntry({
|
||||
transcriptPath,
|
||||
sessionId,
|
||||
canonicalKey: "k1",
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
42
src/gateway/server-methods/chat.test-helpers.ts
Normal file
42
src/gateway/server-methods/chat.test-helpers.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { CURRENT_SESSION_VERSION } from "@mariozechner/pi-coding-agent";
|
||||
|
||||
export function createTranscriptFixtureSync(params: {
|
||||
prefix: string;
|
||||
sessionId: string;
|
||||
fileName?: string;
|
||||
}) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), params.prefix));
|
||||
const transcriptPath = path.join(dir, params.fileName ?? "sess.jsonl");
|
||||
fs.writeFileSync(
|
||||
transcriptPath,
|
||||
`${JSON.stringify({
|
||||
type: "session",
|
||||
version: CURRENT_SESSION_VERSION,
|
||||
id: params.sessionId,
|
||||
timestamp: new Date(0).toISOString(),
|
||||
cwd: "/tmp",
|
||||
})}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
return { dir, transcriptPath };
|
||||
}
|
||||
|
||||
export function createMockSessionEntry(params: {
|
||||
transcriptPath: string;
|
||||
sessionId: string;
|
||||
canonicalKey?: string;
|
||||
cfg?: Record<string, unknown>;
|
||||
}) {
|
||||
return {
|
||||
cfg: params.cfg ?? {},
|
||||
storePath: path.join(path.dirname(params.transcriptPath), "sessions.json"),
|
||||
entry: {
|
||||
sessionId: params.sessionId,
|
||||
sessionFile: params.transcriptPath,
|
||||
},
|
||||
canonicalKey: params.canonicalKey ?? "main",
|
||||
};
|
||||
}
|
||||
@@ -10,7 +10,10 @@ import type { MsgContext } from "../../auto-reply/templating.js";
|
||||
import { createReplyPrefixOptions } from "../../channels/reply-prefix.js";
|
||||
import { resolveSessionFilePath } from "../../config/sessions.js";
|
||||
import { resolveSendPolicy } from "../../sessions/send-policy.js";
|
||||
import { stripInlineDirectiveTagsForDisplay } from "../../utils/directive-tags.js";
|
||||
import {
|
||||
stripInlineDirectiveTagsForDisplay,
|
||||
stripInlineDirectiveTagsFromMessageForDisplay,
|
||||
} from "../../utils/directive-tags.js";
|
||||
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
|
||||
import {
|
||||
abortChatRunById,
|
||||
@@ -527,25 +530,6 @@ function nextChatSeq(context: { agentRunSeq: Map<string, number> }, runId: strin
|
||||
return next;
|
||||
}
|
||||
|
||||
function stripMessageDirectiveTags(
|
||||
message: Record<string, unknown> | undefined,
|
||||
): Record<string, unknown> | undefined {
|
||||
if (!message) {
|
||||
return message;
|
||||
}
|
||||
const content = message.content;
|
||||
if (!Array.isArray(content)) {
|
||||
return message;
|
||||
}
|
||||
const cleaned = content.map((part: Record<string, unknown>) => {
|
||||
if (part.type === "text" && typeof part.text === "string") {
|
||||
return { ...part, text: stripInlineDirectiveTagsForDisplay(part.text).text };
|
||||
}
|
||||
return part;
|
||||
});
|
||||
return { ...message, content: cleaned };
|
||||
}
|
||||
|
||||
function broadcastChatFinal(params: {
|
||||
context: Pick<GatewayRequestContext, "broadcast" | "nodeSendToSession" | "agentRunSeq">;
|
||||
runId: string;
|
||||
@@ -558,7 +542,7 @@ function broadcastChatFinal(params: {
|
||||
sessionKey: params.sessionKey,
|
||||
seq,
|
||||
state: "final" as const,
|
||||
message: stripMessageDirectiveTags(params.message),
|
||||
message: stripInlineDirectiveTagsFromMessageForDisplay(params.message),
|
||||
};
|
||||
params.context.broadcast("chat", payload);
|
||||
params.context.nodeSendToSession(params.sessionKey, "chat", payload);
|
||||
@@ -1089,7 +1073,7 @@ export const chatHandlers: GatewayRequestHandlers = {
|
||||
sessionKey: rawSessionKey,
|
||||
seq: 0,
|
||||
state: "final" as const,
|
||||
message: stripMessageDirectiveTags(appended.message),
|
||||
message: stripInlineDirectiveTagsFromMessageForDisplay(appended.message),
|
||||
};
|
||||
context.broadcast("chat", chatPayload);
|
||||
context.nodeSendToSession(rawSessionKey, "chat", chatPayload);
|
||||
|
||||
Reference in New Issue
Block a user