Files
my-pi/pi-notify/src/config.ts
T
云服务部-叶林立andDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> e996f59469 refactor: notify/footer 持久配置收编到 config/,chrome bridge 地址固定
仓库规则要求 config/ 为唯一配置来源、不以环境变量作持久默认值:
- pi-notify 改读 config/pi-notify.json(逐字段校验回退,复用原 parsers 钳制)
- pi-minimal-footer 改读 config/pi-minimal-footer.json
- pi-chrome 移除 PI_CHROME_BRIDGE_HOST/PORT,固定 127.0.0.1:17318
  (浏览器端 service_worker.js 硬编码同一地址,单侧可配只会断连)

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 19:16:47 -07:00

51 lines
2.2 KiB
TypeScript

import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
import { extractMessageText, truncateText } from "./messages.ts";
import { readNotifySettings, type NotifySettings } from "./settings.ts";
import { renderTemplate } from "./template.ts";
import type { NotificationRuntimeValues, NotifyConfig } from "./types.ts";
const DEFAULT_TITLE = "π - {project}";
const DEFAULT_BODY = "{message}";
export function getConfig(
ctx: ExtensionContext | undefined,
startedAt: number | undefined,
messages: unknown[] | undefined,
runtime: NotificationRuntimeValues,
now = Date.now(),
settings: NotifySettings = readNotifySettings(),
): NotifyConfig {
const durationMs = startedAt === undefined ? 0 : Math.max(0, now - startedAt);
const messageMax = settings.messageMax;
const userMessage = truncateText(extractMessageText(messages, "user"), messageMax);
const assistantMessage = truncateText(extractMessageText(messages, "assistant"), messageMax);
const messageSource = settings.messageSource;
const selectedMessage = messageSource === "user" ? userMessage : messageSource === "assistant" ? assistantMessage : "";
const message = selectedMessage || "Ready for input";
const toolErrorCount = Math.max(0, runtime.toolErrorCount ?? 0);
const values = {
user_message: userMessage,
assistant_message: assistantMessage,
message,
status: toolErrorCount > 0 ? `ready · ${toolErrorCount} tool error${toolErrorCount === 1 ? "" : "s"}` : "ready",
status_icon: toolErrorCount > 0 ? "⚠" : "✓",
tool_error_count: String(toolErrorCount),
};
return {
enabled: settings.enabled,
title: renderTemplate(settings.title || DEFAULT_TITLE, ctx, durationMs, values),
body: renderTemplate(settings.body || DEFAULT_BODY, ctx, durationMs, values),
durationMs,
minDurationMs: settings.minDurationMs,
notificationId: runtime.notificationId,
visibility: settings.visibility,
action: settings.action,
soundCommand: settings.soundCommand,
};
}
export function shouldNotify(config: NotifyConfig, enabledOverride?: boolean): boolean {
return (enabledOverride ?? config.enabled) && config.durationMs >= config.minDurationMs;
}