From 1037af01adbaf2afea72bb27121c10580bef3309 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 4 Apr 2026 12:19:00 +0100 Subject: [PATCH] style(agents): normalize runtime prompt formatting --- .../msteams/src/attachments/graph.test.ts | 54 ++++++++++--------- .../prompt-cache-observability.test.ts | 7 +-- .../prompt-cache-observability.ts | 4 +- .../proxy-stream-wrappers.ts | 2 +- src/agents/pi-embedded-runner/run.ts | 5 +- .../run/attempt.thread-helpers.ts | 7 ++- src/agents/prompt-cache-stability.ts | 5 +- src/agents/system-prompt.ts | 12 ++--- 8 files changed, 42 insertions(+), 54 deletions(-) diff --git a/extensions/msteams/src/attachments/graph.test.ts b/extensions/msteams/src/attachments/graph.test.ts index b248c071ff5..594c8cef439 100644 --- a/extensions/msteams/src/attachments/graph.test.ts +++ b/extensions/msteams/src/attachments/graph.test.ts @@ -45,9 +45,9 @@ vi.mock("./remote-media.js", () => ({ })); import { fetchWithSsrFGuard } from "../../runtime-api.js"; +import { downloadMSTeamsGraphMedia } from "./graph.js"; import { downloadAndStoreMSTeamsRemoteMedia } from "./remote-media.js"; import { safeFetchWithPolicy } from "./shared.js"; -import { downloadMSTeamsGraphMedia } from "./graph.js"; function mockFetchResponse(body: unknown, status = 200) { const bodyStr = typeof body === "string" ? body : JSON.stringify(body); @@ -279,35 +279,37 @@ describe("downloadMSTeamsGraphMedia hosted content $value fallback", () => { }); it("adds the OpenClaw User-Agent to guarded Graph attachment fetches", async () => { - vi.mocked(fetchWithSsrFGuard).mockImplementation(async (params: { url: string; init?: RequestInit }) => { - const url = params.url; - if (url.endsWith("/messages/msg-ua") && !url.includes("hostedContents")) { + vi.mocked(fetchWithSsrFGuard).mockImplementation( + async (params: { url: string; init?: RequestInit }) => { + const url = params.url; + if (url.endsWith("/messages/msg-ua") && !url.includes("hostedContents")) { + return { + response: mockFetchResponse({ body: {}, attachments: [] }), + release: async () => {}, + finalUrl: params.url, + }; + } + if (url.endsWith("/hostedContents")) { + return { + response: mockFetchResponse({ value: [] }), + release: async () => {}, + finalUrl: params.url, + }; + } + if (url.endsWith("/attachments")) { + return { + response: mockFetchResponse({ value: [] }), + release: async () => {}, + finalUrl: params.url, + }; + } return { - response: mockFetchResponse({ body: {}, attachments: [] }), + response: mockFetchResponse({}, 404), release: async () => {}, finalUrl: params.url, }; - } - if (url.endsWith("/hostedContents")) { - return { - response: mockFetchResponse({ value: [] }), - release: async () => {}, - finalUrl: params.url, - }; - } - if (url.endsWith("/attachments")) { - return { - response: mockFetchResponse({ value: [] }), - release: async () => {}, - finalUrl: params.url, - }; - } - return { - response: mockFetchResponse({}, 404), - release: async () => {}, - finalUrl: params.url, - }; - }); + }, + ); await downloadMSTeamsGraphMedia({ messageUrl: "https://graph.microsoft.com/v1.0/chats/c/messages/msg-ua", diff --git a/src/agents/pi-embedded-runner/prompt-cache-observability.test.ts b/src/agents/pi-embedded-runner/prompt-cache-observability.test.ts index 81ea04e615b..b981eb04120 100644 --- a/src/agents/pi-embedded-runner/prompt-cache-observability.test.ts +++ b/src/agents/pi-embedded-runner/prompt-cache-observability.test.ts @@ -13,12 +13,7 @@ describe("prompt cache observability", () => { it("collects trimmed tool names only", () => { expect( - collectPromptCacheToolNames([ - { name: " read " }, - { name: "" }, - {}, - { name: "write" }, - ]), + collectPromptCacheToolNames([{ name: " read " }, { name: "" }, {}, { name: "write" }]), ).toEqual(["read", "write"]); }); diff --git a/src/agents/pi-embedded-runner/prompt-cache-observability.ts b/src/agents/pi-embedded-runner/prompt-cache-observability.ts index 6df1912a07b..74fd9b4805c 100644 --- a/src/agents/pi-embedded-runner/prompt-cache-observability.ts +++ b/src/agents/pi-embedded-runner/prompt-cache-observability.ts @@ -130,9 +130,7 @@ function diffSnapshots( } export function collectPromptCacheToolNames(tools: Array<{ name?: string }>): string[] { - return tools - .map((tool) => tool.name?.trim()) - .filter((name): name is string => Boolean(name)); + return tools.map((tool) => tool.name?.trim()).filter((name): name is string => Boolean(name)); } export function beginPromptCacheObservation(params: { diff --git a/src/agents/pi-embedded-runner/proxy-stream-wrappers.ts b/src/agents/pi-embedded-runner/proxy-stream-wrappers.ts index 0f092e9a864..be72832adb3 100644 --- a/src/agents/pi-embedded-runner/proxy-stream-wrappers.ts +++ b/src/agents/pi-embedded-runner/proxy-stream-wrappers.ts @@ -1,8 +1,8 @@ import type { StreamFn } from "@mariozechner/pi-agent-core"; import { streamSimple } from "@mariozechner/pi-ai"; import type { ThinkLevel } from "../../auto-reply/thinking.js"; -import { resolveProviderRequestPolicy } from "../provider-attribution.js"; import { isProxyReasoningUnsupportedModelHint } from "../../plugin-sdk/provider-model-shared.js"; +import { resolveProviderRequestPolicy } from "../provider-attribution.js"; import { resolveProviderRequestPolicyConfig } from "../provider-request-config.js"; import { applyAnthropicEphemeralCacheControlMarkers } from "./anthropic-cache-control-payload.js"; import { isAnthropicModelRef } from "./anthropic-family-cache-semantics.js"; diff --git a/src/agents/pi-embedded-runner/run.ts b/src/agents/pi-embedded-runner/run.ts index 1e123c3511d..698f7e862c8 100644 --- a/src/agents/pi-embedded-runner/run.ts +++ b/src/agents/pi-embedded-runner/run.ts @@ -26,10 +26,7 @@ import { resolveFailoverStatus, } from "../failover-error.js"; import { LiveSessionModelSwitchError } from "../live-model-switch-error.js"; -import { - shouldSwitchToLiveModel, - clearLiveModelSwitchPending, -} from "../live-model-switch.js"; +import { shouldSwitchToLiveModel, clearLiveModelSwitchPending } from "../live-model-switch.js"; import { applyAuthHeaderOverride, applyLocalNoAuthHeaderOverride, diff --git a/src/agents/pi-embedded-runner/run/attempt.thread-helpers.ts b/src/agents/pi-embedded-runner/run/attempt.thread-helpers.ts index 0a9ff170022..f75bc3836b9 100644 --- a/src/agents/pi-embedded-runner/run/attempt.thread-helpers.ts +++ b/src/agents/pi-embedded-runner/run/attempt.thread-helpers.ts @@ -20,10 +20,9 @@ export function composeSystemPromptWithHookContext(params: { if (!prependSystem && !appendSystem) { return undefined; } - return joinPresentTextSegments( - [prependSystem, params.baseSystemPrompt, appendSystem], - { trim: true }, - ); + return joinPresentTextSegments([prependSystem, params.baseSystemPrompt, appendSystem], { + trim: true, + }); } export function resolveAttemptSpawnWorkspaceDir(params: { diff --git a/src/agents/prompt-cache-stability.ts b/src/agents/prompt-cache-stability.ts index 1bff3ad992a..a2966b85ba5 100644 --- a/src/agents/prompt-cache-stability.ts +++ b/src/agents/prompt-cache-stability.ts @@ -1,5 +1,8 @@ export function normalizeStructuredPromptSection(text: string): string { - return text.replace(/\r\n?/g, "\n").replace(/[ \t]+$/gm, "").trim(); + return text + .replace(/\r\n?/g, "\n") + .replace(/[ \t]+$/gm, "") + .trim(); } export function normalizePromptCapabilityIds(capabilities: ReadonlyArray): string[] { diff --git a/src/agents/system-prompt.ts b/src/agents/system-prompt.ts index d5ed68c5735..14d05df5c32 100644 --- a/src/agents/system-prompt.ts +++ b/src/agents/system-prompt.ts @@ -387,9 +387,7 @@ export function buildAgentSystemPrompt(params: { const runtimeChannel = runtimeInfo?.channel?.trim().toLowerCase(); const runtimeCapabilities = runtimeInfo?.capabilities ?? []; const runtimeCapabilitiesLower = new Set( - runtimeCapabilities - .map((cap) => String(cap).trim().toLowerCase()) - .filter(Boolean), + runtimeCapabilities.map((cap) => String(cap).trim().toLowerCase()).filter(Boolean), ); const inlineButtonsEnabled = runtimeCapabilitiesLower.has("inlinebuttons"); const messageChannelOptions = listDeliverableMessageChannels().join("|"); @@ -519,15 +517,11 @@ export function buildAgentSystemPrompt(params: { hasGateway && !isMinimal ? "" : "", "", // Skip model aliases for subagent/none modes - modelAliasLines.length > 0 && !isMinimal - ? "## Model Aliases" - : "", + modelAliasLines.length > 0 && !isMinimal ? "## Model Aliases" : "", modelAliasLines.length > 0 && !isMinimal ? "Prefer aliases when specifying model overrides; full provider/model is also accepted." : "", - modelAliasLines.length > 0 && !isMinimal - ? modelAliasLines.join("\n") - : "", + modelAliasLines.length > 0 && !isMinimal ? modelAliasLines.join("\n") : "", modelAliasLines.length > 0 && !isMinimal ? "" : "", userTimezone ? "If you need the current date, time, or day of week, run session_status (📊 session_status)."