Skip to content

Commit a88fbf0

Browse files
committed
fix(cycles): split reply payload and option contracts
1 parent 25665dd commit a88fbf0

48 files changed

Lines changed: 256 additions & 240 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/agents/btw.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
type TextContent,
99
} from "@mariozechner/pi-ai";
1010
import { SessionManager } from "@mariozechner/pi-coding-agent";
11+
import type { GetReplyOptions } from "../auto-reply/get-reply-options.types.js";
12+
import type { ReplyPayload } from "../auto-reply/reply-payload.js";
1113
import type { ReasoningLevel, ThinkLevel } from "../auto-reply/thinking.js";
12-
import type { GetReplyOptions, ReplyPayload } from "../auto-reply/types.js";
1314
import {
1415
resolveSessionFilePath,
1516
resolveSessionFilePathOptions,

‎src/agents/command/delivery.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolveSessionAgentId } from "../../agents/agent-scope.js";
2+
import type { ReplyPayload } from "../../auto-reply/reply-payload.js";
23
import { normalizeReplyPayload } from "../../auto-reply/reply/normalize-reply.js";
3-
import type { ReplyPayload } from "../../auto-reply/types.js";
44
import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js";
55
import { createReplyPrefixContext } from "../../channels/reply-prefix.js";
66
import { createOutboundSendDeps, type CliDeps } from "../../cli/outbound-send-deps.js";

‎src/agents/pi-embedded-runner/run/params.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ImageContent } from "@mariozechner/pi-ai";
2+
import type { ReplyPayload } from "../../../auto-reply/reply-payload.js";
23
import type { ReplyOperation } from "../../../auto-reply/reply/reply-run-registry.js";
34
import type { ReasoningLevel, ThinkLevel, VerboseLevel } from "../../../auto-reply/thinking.js";
4-
import type { ReplyPayload } from "../../../auto-reply/types.js";
55
import type { OpenClawConfig } from "../../../config/types.openclaw.js";
66
import type { PromptImageOrderEntry } from "../../../media/prompt-image-order.js";
77
import type { CommandQueueEnqueueFn } from "../../../process/command-queue.types.js";

‎src/agents/pi-embedded-subscribe.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { AgentMessage } from "@mariozechner/pi-agent-core";
2+
import { setReplyPayloadMetadata } from "../auto-reply/reply-payload.js";
23
import { parseReplyDirectives } from "../auto-reply/reply/reply-directives.js";
34
import { createStreamingDirectiveAccumulator } from "../auto-reply/reply/streaming-directives.js";
45
import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
56
import { formatToolAggregate } from "../auto-reply/tool-meta.js";
6-
import { setReplyPayloadMetadata } from "../auto-reply/types.js";
77
import { emitAgentEvent } from "../infra/agent-events.js";
88
import { createSubsystemLogger } from "../logging/subsystem.js";
99
import type { InlineCodeState } from "../markdown/code-spans.js";

‎src/agents/pi-embedded-subscribe.types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AgentSession } from "@mariozechner/pi-coding-agent";
2+
import type { ReplyPayload } from "../auto-reply/reply-payload.js";
23
import type { ReasoningLevel, VerboseLevel } from "../auto-reply/thinking.js";
3-
import type { ReplyPayload } from "../auto-reply/types.js";
44
import type { OpenClawConfig } from "../config/types.openclaw.js";
55
import type { HookRunner } from "../plugins/hooks.js";
66
import type { AgentInternalEvent } from "./internal-events.js";
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import type { ImageContent } from "@mariozechner/pi-ai";
2+
import type { PromptImageOrderEntry } from "../media/prompt-image-order.js";
3+
import type { ReplyPayload } from "./reply-payload.js";
4+
import type { TypingController } from "./reply/typing.js";
5+
6+
export type BlockReplyContext = {
7+
abortSignal?: AbortSignal;
8+
timeoutMs?: number;
9+
/** Source assistant message index from the upstream stream, when available. */
10+
assistantMessageIndex?: number;
11+
};
12+
13+
/** Context passed to onModelSelected callback with actual model used. */
14+
export type ModelSelectedContext = {
15+
provider: string;
16+
model: string;
17+
thinkLevel: string | undefined;
18+
};
19+
20+
export type TypingPolicy =
21+
| "auto"
22+
| "user_message"
23+
| "system_event"
24+
| "internal_webchat"
25+
| "heartbeat";
26+
27+
export type ReplyThreadingPolicy = {
28+
/** Override implicit reply-to-current behavior for the current turn. */
29+
implicitCurrentMessage?: "default" | "allow" | "deny";
30+
};
31+
32+
export type GetReplyOptions = {
33+
/** Override run id for agent events (defaults to random UUID). */
34+
runId?: string;
35+
/** Abort signal for the underlying agent run. */
36+
abortSignal?: AbortSignal;
37+
/** Optional inbound images (used for webchat attachments). */
38+
images?: ImageContent[];
39+
/** Original inline/offloaded attachment order for inbound images. */
40+
imageOrder?: PromptImageOrderEntry[];
41+
/** Notifies when an agent run actually starts (useful for webchat command handling). */
42+
onAgentRunStart?: (runId: string) => void;
43+
onReplyStart?: () => Promise<void> | void;
44+
/** Called when the typing controller cleans up (e.g., run ended with NO_REPLY). */
45+
onTypingCleanup?: () => void;
46+
onTypingController?: (typing: TypingController) => void;
47+
isHeartbeat?: boolean;
48+
/** Policy-level typing control for run classes (user/system/internal/heartbeat). */
49+
typingPolicy?: TypingPolicy;
50+
/** Force-disable typing indicators for this run (system/internal/cross-channel routes). */
51+
suppressTyping?: boolean;
52+
/** Resolved heartbeat model override (provider/model string from merged per-agent config). */
53+
heartbeatModelOverride?: string;
54+
/** Controls bootstrap workspace context injection (default: full). */
55+
bootstrapContextMode?: "full" | "lightweight";
56+
/** If true, suppress tool error warning payloads for this run. */
57+
suppressToolErrorWarnings?: boolean;
58+
onPartialReply?: (payload: ReplyPayload) => Promise<void> | void;
59+
onReasoningStream?: (payload: ReplyPayload) => Promise<void> | void;
60+
/** Called when a thinking/reasoning block ends. */
61+
onReasoningEnd?: () => Promise<void> | void;
62+
/** Called when a new assistant message starts (e.g., after tool call or thinking block). */
63+
onAssistantMessageStart?: () => Promise<void> | void;
64+
/** Called synchronously when a block reply is logically emitted, before async
65+
* delivery drains. Useful for channels that need to rotate preview state at
66+
* block boundaries without waiting for transport acks. */
67+
onBlockReplyQueued?: (payload: ReplyPayload, context?: BlockReplyContext) => Promise<void> | void;
68+
onBlockReply?: (payload: ReplyPayload, context?: BlockReplyContext) => Promise<void> | void;
69+
onToolResult?: (payload: ReplyPayload) => Promise<void> | void;
70+
/** Called when a tool phase starts/updates, before summary payloads are emitted. */
71+
onToolStart?: (payload: { name?: string; phase?: string }) => Promise<void> | void;
72+
/** Called when a concrete work item starts, updates, or completes. */
73+
onItemEvent?: (payload: {
74+
itemId?: string;
75+
kind?: string;
76+
title?: string;
77+
name?: string;
78+
phase?: string;
79+
status?: string;
80+
summary?: string;
81+
progressText?: string;
82+
approvalId?: string;
83+
approvalSlug?: string;
84+
}) => Promise<void> | void;
85+
/** Called when the agent emits a structured plan update. */
86+
onPlanUpdate?: (payload: {
87+
phase?: string;
88+
title?: string;
89+
explanation?: string;
90+
steps?: string[];
91+
source?: string;
92+
}) => Promise<void> | void;
93+
/** Called when an approval becomes pending or resolves. */
94+
onApprovalEvent?: (payload: {
95+
phase?: string;
96+
kind?: string;
97+
status?: string;
98+
title?: string;
99+
itemId?: string;
100+
toolCallId?: string;
101+
approvalId?: string;
102+
approvalSlug?: string;
103+
command?: string;
104+
host?: string;
105+
reason?: string;
106+
message?: string;
107+
}) => Promise<void> | void;
108+
/** Called when command output streams or completes. */
109+
onCommandOutput?: (payload: {
110+
itemId?: string;
111+
phase?: string;
112+
title?: string;
113+
toolCallId?: string;
114+
name?: string;
115+
output?: string;
116+
status?: string;
117+
exitCode?: number | null;
118+
durationMs?: number;
119+
cwd?: string;
120+
}) => Promise<void> | void;
121+
/** Called when a patch completes with a file summary. */
122+
onPatchSummary?: (payload: {
123+
itemId?: string;
124+
phase?: string;
125+
title?: string;
126+
toolCallId?: string;
127+
name?: string;
128+
added?: string[];
129+
modified?: string[];
130+
deleted?: string[];
131+
summary?: string;
132+
}) => Promise<void> | void;
133+
/** Called when context auto-compaction starts (allows UX feedback during the pause). */
134+
onCompactionStart?: () => Promise<void> | void;
135+
/** Called when context auto-compaction completes. */
136+
onCompactionEnd?: () => Promise<void> | void;
137+
/** Called when the actual model is selected (including after fallback).
138+
* Use this to get model/provider/thinkLevel for responsePrefix template interpolation. */
139+
onModelSelected?: (ctx: ModelSelectedContext) => void;
140+
disableBlockStreaming?: boolean;
141+
/** Timeout for block reply delivery (ms). */
142+
blockReplyTimeoutMs?: number;
143+
/** If provided, only load these skills for this session (empty = no skills). */
144+
skillFilter?: string[];
145+
/** Mutable ref to track if a reply was sent (for Slack "first" threading mode). */
146+
hasRepliedRef?: { value: boolean };
147+
/** Override agent timeout in seconds (0 = no timeout). Threads through to resolveAgentTimeoutMs. */
148+
timeoutOverrideSeconds?: number;
149+
};

‎src/auto-reply/reply-payload.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { InteractiveReply } from "../interactive/payload.js";
2+
3+
export type ReplyPayload = {
4+
text?: string;
5+
mediaUrl?: string;
6+
mediaUrls?: string[];
7+
interactive?: InteractiveReply;
8+
btw?: {
9+
question: string;
10+
};
11+
replyToId?: string;
12+
replyToTag?: boolean;
13+
/** True when [[reply_to_current]] was present but not yet mapped to a message id. */
14+
replyToCurrent?: boolean;
15+
/** Send audio as voice message (bubble) instead of audio file. Defaults to false. */
16+
audioAsVoice?: boolean;
17+
isError?: boolean;
18+
/** Marks this payload as a reasoning/thinking block. Channels that do not
19+
* have a dedicated reasoning lane (e.g. WhatsApp, web) should suppress it. */
20+
isReasoning?: boolean;
21+
/** Marks this payload as a compaction status notice (start/end).
22+
* Should be excluded from TTS transcript accumulation so compaction
23+
* status lines are not synthesised into the spoken assistant reply. */
24+
isCompactionNotice?: boolean;
25+
/** Channel-specific payload data (per-channel envelope). */
26+
channelData?: Record<string, unknown>;
27+
};
28+
29+
export type ReplyPayloadMetadata = {
30+
assistantMessageIndex?: number;
31+
};
32+
33+
const replyPayloadMetadata = new WeakMap<object, ReplyPayloadMetadata>();
34+
35+
export function setReplyPayloadMetadata<T extends object>(
36+
payload: T,
37+
metadata: ReplyPayloadMetadata,
38+
): T {
39+
const previous = replyPayloadMetadata.get(payload);
40+
replyPayloadMetadata.set(payload, { ...previous, ...metadata });
41+
return payload;
42+
}
43+
44+
export function getReplyPayloadMetadata(payload: object): ReplyPayloadMetadata | undefined {
45+
return replyPayloadMetadata.get(payload);
46+
}

‎src/auto-reply/reply/dispatch-from-config.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ import {
4848
shouldAttemptTtsPayload,
4949
} from "../../tts/tts-config.js";
5050
import { INTERNAL_MESSAGE_CHANNEL, normalizeMessageChannel } from "../../utils/message-channel.js";
51+
import type { BlockReplyContext } from "../get-reply-options.types.js";
52+
import { getReplyPayloadMetadata, type ReplyPayload } from "../reply-payload.js";
5153
import type { FinalizedMsgContext } from "../templating.js";
5254
import { normalizeVerboseLevel } from "../thinking.js";
53-
import { getReplyPayloadMetadata, type BlockReplyContext, type ReplyPayload } from "../types.js";
5455
import {
5556
createInternalHookEvent,
5657
loadSessionStore,

‎src/auto-reply/reply/dispatch-from-config.types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OpenClawConfig } from "../../config/types.openclaw.js";
2+
import type { GetReplyOptions } from "../get-reply-options.types.js";
23
import type { FinalizedMsgContext } from "../templating.js";
3-
import type { GetReplyOptions } from "../types.js";
44
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
55

66
export type DispatchFromConfigResult = {

‎src/auto-reply/reply/get-reply.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import { type OpenClawConfig, loadConfig } from "../../config/config.js";
1313
import { defaultRuntime } from "../../runtime.js";
1414
import { normalizeOptionalString } from "../../shared/string-coerce.js";
1515
import { normalizeStringEntries } from "../../shared/string-normalization.js";
16+
import type { GetReplyOptions } from "../get-reply-options.types.js";
17+
import type { ReplyPayload } from "../reply-payload.js";
1618
import type { MsgContext } from "../templating.js";
1719
import { normalizeVerboseLevel } from "../thinking.js";
1820
import { SILENT_REPLY_TOKEN } from "../tokens.js";
19-
import type { GetReplyOptions, ReplyPayload } from "../types.js";
2021
import { resolveDefaultModel } from "./directive-handling.defaults.js";
2122
import { clearInlineDirectives } from "./get-reply-directives-utils.js";
2223
import { resolveReplyDirectives } from "./get-reply-directives.js";

0 commit comments

Comments
 (0)