2026-01-16 03:00:40 +00:00
|
|
|
import { sanitizeUserFacingText } from "../../agents/pi-embedded-helpers.js";
|
2026-02-01 10:03:47 +09:00
|
|
|
import { stripHeartbeatToken } from "../heartbeat.js";
|
|
|
|
|
import { HEARTBEAT_TOKEN, isSilentReplyText, SILENT_REPLY_TOKEN } from "../tokens.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { ReplyPayload } from "../types.js";
|
2026-02-01 10:03:47 +09:00
|
|
|
import { hasLineDirectives, parseLineDirectives } from "./line-directives.js";
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
import {
|
|
|
|
|
resolveResponsePrefixTemplate,
|
|
|
|
|
type ResponsePrefixContext,
|
|
|
|
|
} from "./response-prefix-template.js";
|
2026-01-09 13:47:03 +01:00
|
|
|
|
2026-01-29 11:34:47 +05:30
|
|
|
export type NormalizeReplySkipReason = "empty" | "silent" | "heartbeat";
|
|
|
|
|
|
2026-01-09 13:47:03 +01:00
|
|
|
export type NormalizeReplyOptions = {
|
|
|
|
|
responsePrefix?: string;
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
/** Context for template variable interpolation in responsePrefix */
|
|
|
|
|
responsePrefixContext?: ResponsePrefixContext;
|
2026-01-09 13:47:03 +01:00
|
|
|
onHeartbeatStrip?: () => void;
|
|
|
|
|
stripHeartbeat?: boolean;
|
|
|
|
|
silentToken?: string;
|
2026-01-29 11:34:47 +05:30
|
|
|
onSkip?: (reason: NormalizeReplySkipReason) => void;
|
2026-01-09 13:47:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function normalizeReplyPayload(
|
|
|
|
|
payload: ReplyPayload,
|
|
|
|
|
opts: NormalizeReplyOptions = {},
|
|
|
|
|
): ReplyPayload | null {
|
2026-01-14 14:31:43 +00:00
|
|
|
const hasMedia = Boolean(payload.mediaUrl || (payload.mediaUrls?.length ?? 0) > 0);
|
2026-01-25 07:22:36 -05:00
|
|
|
const hasChannelData = Boolean(
|
|
|
|
|
payload.channelData && Object.keys(payload.channelData).length > 0,
|
|
|
|
|
);
|
2026-01-09 13:47:03 +01:00
|
|
|
const trimmed = payload.text?.trim() ?? "";
|
2026-01-29 11:34:47 +05:30
|
|
|
if (!trimmed && !hasMedia && !hasChannelData) {
|
|
|
|
|
opts.onSkip?.("empty");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-01-09 13:47:03 +01:00
|
|
|
|
|
|
|
|
const silentToken = opts.silentToken ?? SILENT_REPLY_TOKEN;
|
|
|
|
|
let text = payload.text ?? undefined;
|
2026-01-09 23:29:01 +00:00
|
|
|
if (text && isSilentReplyText(text, silentToken)) {
|
2026-01-29 11:34:47 +05:30
|
|
|
if (!hasMedia && !hasChannelData) {
|
|
|
|
|
opts.onSkip?.("silent");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-01-09 23:29:01 +00:00
|
|
|
text = "";
|
|
|
|
|
}
|
2026-01-09 13:47:03 +01:00
|
|
|
if (text && !trimmed) {
|
|
|
|
|
// Keep empty text when media exists so media-only replies still send.
|
|
|
|
|
text = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldStripHeartbeat = opts.stripHeartbeat ?? true;
|
|
|
|
|
if (shouldStripHeartbeat && text?.includes(HEARTBEAT_TOKEN)) {
|
|
|
|
|
const stripped = stripHeartbeatToken(text, { mode: "message" });
|
2026-01-31 16:19:20 +09:00
|
|
|
if (stripped.didStrip) {
|
|
|
|
|
opts.onHeartbeatStrip?.();
|
|
|
|
|
}
|
2026-01-29 11:34:47 +05:30
|
|
|
if (stripped.shouldSkip && !hasMedia && !hasChannelData) {
|
|
|
|
|
opts.onSkip?.("heartbeat");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-01-09 13:47:03 +01:00
|
|
|
text = stripped.text;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 03:00:40 +00:00
|
|
|
if (text) {
|
2026-02-09 19:52:24 -06:00
|
|
|
text = sanitizeUserFacingText(text, { errorContext: Boolean(payload.isError) });
|
2026-01-16 03:00:40 +00:00
|
|
|
}
|
2026-01-29 11:34:47 +05:30
|
|
|
if (!text?.trim() && !hasMedia && !hasChannelData) {
|
|
|
|
|
opts.onSkip?.("empty");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-01-25 07:22:36 -05:00
|
|
|
|
|
|
|
|
// Parse LINE-specific directives from text (quick_replies, location, confirm, buttons)
|
|
|
|
|
let enrichedPayload: ReplyPayload = { ...payload, text };
|
|
|
|
|
if (text && hasLineDirectives(text)) {
|
|
|
|
|
enrichedPayload = parseLineDirectives(enrichedPayload);
|
|
|
|
|
text = enrichedPayload.text;
|
|
|
|
|
}
|
2026-01-16 03:00:40 +00:00
|
|
|
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
// Resolve template variables in responsePrefix if context is provided
|
|
|
|
|
const effectivePrefix = opts.responsePrefixContext
|
|
|
|
|
? resolveResponsePrefixTemplate(opts.responsePrefix, opts.responsePrefixContext)
|
|
|
|
|
: opts.responsePrefix;
|
|
|
|
|
|
2026-01-09 13:47:03 +01:00
|
|
|
if (
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
effectivePrefix &&
|
2026-01-09 13:47:03 +01:00
|
|
|
text &&
|
|
|
|
|
text.trim() !== HEARTBEAT_TOKEN &&
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
!text.startsWith(effectivePrefix)
|
2026-01-09 13:47:03 +01:00
|
|
|
) {
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
text = `${effectivePrefix} ${text}`;
|
2026-01-09 13:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-25 07:22:36 -05:00
|
|
|
return { ...enrichedPayload, text };
|
2026-01-09 13:47:03 +01:00
|
|
|
}
|